Link to home
Start Free TrialLog in
Avatar of magenta
magenta

asked on

How do I get the current Process Name?

I need to be able to retrieve the process name from a C/C++ shared library. "getpid" retrieves the process ID, what is the equivalent for getting the process name?

Thanks!
Avatar of ufolk123
ufolk123

You can  use pstat_getproc() in which you can get all information about a process including name given the pid.

  int pstat_getproc(
           struct pst_status *buf, size_t elemsize, size_t elemcount,
           int index
      );

It is defined in     #include <sys/pstat.h>

Please look at man pages for detailed info.




if pstat_getproc() is available on the specific OS, does it also return the
real, not faked, process name? I'm not shure about that.
Avatar of magenta

ASKER

It seems that I don't have "sys/pstat.h" on any of our Sun Solaris machines. Any ideas?

Also,I found an API call kvm_getproc() that seems to return process information, but the returned struct doesn't appear to have the process name, although I'm not sure what to look for.

Thanks!
Frank
man kvm_getcmd
then look at the user parameter, it contains a pointer to the argument list: **u_argv
If you're lucky  *u_argv[0] contains what you want to know
Avatar of magenta

ASKER

It turns out that I need to be root to call any of the "kvm_*()" functions.

However, there is a lot of  process info on Solaris under /proc/<pid>. Do you know if the process binary name might be somewhere under there? I looked but couldn't find it. There is a link to the binary (called unfortunately, "a.out").

Thanks!
of cause you need to be root to access the process table, no way around this.
To read the process information  you need to be root. So the best thing would be to use the ps command(which is a setuserid program).
For eg.:

You could use:
sprintf(s1, "/usr/bin/ps -f -p %d | awk '{print $8}' | grep -v CMD ", getpid()));

system(s1);
ps did not give the name/path as the process was started, but that one which the programmer decided to be displayed in the process list (ps)
Then try /usr/proc/bin/pmap <process id>. The second line gives the name
of the process.  It might help.
You can also try other commands from /usr/proc/bin.
Then try /usr/proc/bin/pmap <process id>. The second line gives the name
of the process.  It might help.
You can also try other commands from /usr/proc/bin.
Try:

ps -ef | grep "`echo $$`"

Probably to easy, worth trying...?
If you arent worried that th code is SunOs only then take a look at the stuff
under:
man proc_service
ASKER CERTIFIED SOLUTION
Avatar of mapc
mapc

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial