Operating System - HP-UX
1753783 Members
7141 Online
108799 Solutions
New Discussion юеВ

Get full path of executable of running process...

 
rakeshcs123
Advisor

Get full path of executable of running process...

Hi All,
I want to get the full path of the running process (executable) without having root permission using C++ code. Can someone suggest a way to achieve this.

on Linux platforms i can do it by using following way.

char exepath[1024] = {0};
char procid[1024] = {0};
char exelink[1024] = {0};

sprintf(procid, "%u", getpid());

strcpy(exelink, "/proc/");
strcat(exelink, procid);
strcat(exelink, "/exe");

readlink(exelink, exepath, sizeof(exepath));

Here exepath gives us the full path of the executable.

Similarly for windows we do it using
GetModuleFileName(NULL, exepath, sizeof(exepath)); /* get fullpath of the service */

Please help me how to do it on HP-UX since there is no /proc directory in HP-UX.
13 REPLIES 13
Venkatesh BL
Honored Contributor

Re: Get full path of executable of running process...

I am not sure, but, see if 'pstat_getcommandline()' is what you are looking for. Check out its man page for details.
rakeshcs123
Advisor

Re: Get full path of executable of running process...

I see that there is a function pstat_getpathname() which returns the full pathname of the process but is limited to UID=0 , but my requirement is a non root user should be able to get the full path name of the executable from the process.
Dennis Handly
Acclaimed Contributor

Re: Get full path of executable of running process...

If argv[0] is an absolute path, you are done. If relative, you use getcwd(3) and concatenate. Otherwise you get getenv("PATH") and then stat(2) argv[0] concatenated to it. And find the first that is executable and is a file.
rakeshcs123
Advisor

Re: Get full path of executable of running process...

Hi All, ACtually i don have option to append argv[0] to getcwd() output, because my code has to detect the excecutable path (which is running). my code to find the full executable path is in some shared library loaded by executable, so i dont really have argv[0] option to use. Only thing that i have is the process id, under which my shared library is loaded. so if someone can suggest a way to use process id and then from that find the executalbe full path, it would be great. As you can see in my initial description i have used way to find full path of executable using process id or directly with some system function (which also uses current process to return result). I see there is one function pstat_getpathname() available on hpux but is restricted to root or equivalent user, but my requirement is any user should be able to get the path (obviously user who is running the process).

Any help would be greatly appreciated.
James R. Ferguson
Acclaimed Contributor

Re: Get full path of executable of running process...

Hi:

Stating the HP-UX *release* on which you are running would be useful. The 'pstat()' module has more functionality in newer releases than in older ones.

Regards!

...JRF...
Peter Nikitka
Honored Contributor

Re: Get full path of executable of running process...

Hi,

as a try I would set the s-bit for root on the executable, which uses the function pstat_getpathname() .

It depends on other stuff done by this program, if that is a security risk.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Dennis Handly
Acclaimed Contributor

Re: Get full path of executable of running process...

>I don't really have argv[0] option to use.

Sure you do, you just have to work smarter. :-)
Also, if you were using a real shell the variable "_" would have the relative or absolute path of the executable. sh may not have the "./".

>directly with some system function

No such function for ordinary users.

This program only shows you how to get argv:
#include
#include
#include /* _environ, __argv (ELF) */

#if defined(__LP64__) || defined(__ia64)
#define ARGVS __argv
#else
extern char **__argv_value;
#define ARGVS __argv_value
#endif

int main() {
int i;
const char *path = getenv("PATH");
printf("PATH=%s\n", path);
for (i = 0; ARGVS[i]; ++i) {
printf("argv[%03d]: %s\n", i, ARGVS[i]);
}
for (i = 0; _environ[i]; ++i) {
printf("env[%03d]: %s\n", i, _environ[i]);
}
return 0;
}
rakeshcs123
Advisor

Re: Get full path of executable of running process...

It seems either i should use pstat_getpathname() function or use getcwd()/argv[0].

Signature of pstat_getpathname
int pstat_getpathname(
char *buf,
size_t elemcount,
struct pst_fid *fid
);

I see the man page of pstat_getpathname() which states we can use pstat_getfile2() or pstat_getproc() to obtain fid. but when i see signature of pstat_getproc then it takes struct pst_status as an argument. how to get the fid from struct pst_status variable.

from pstat_getfile2 things works, but i need to open the file to get this details (which i dont have).

Thanks in advance...
Dennis Handly
Acclaimed Contributor

Re: Get full path of executable of running process...

>but I need to open the file to get this details

You don't know the name and it isn't open. So you are left with getcwd()/argv[0].

I suppose you could use shl_get(3) or dlget(3) and then use advanced AI technology to figure out which is the executable.