• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 506
  • Last Modified:

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!
0
magenta
Asked:
magenta
  • 4
  • 3
  • 2
  • +4
1 Solution
 
ufolk123Commented:
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.




0
 
ahoffmannCommented:
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.
0
 
magentaAuthor Commented:
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
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ahoffmannCommented:
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
0
 
magentaAuthor Commented:
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!
0
 
ahoffmannCommented:
of cause you need to be root to access the process table, no way around this.
0
 
meerakCommented:
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);
0
 
ahoffmannCommented:
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)
0
 
meerakCommented:
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.
0
 
meerakCommented:
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.
0
 
paulqnaCommented:
Try:

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

Probably to easy, worth trying...?
0
 
emouraoCommented:
If you arent worried that th code is SunOs only then take a look at the stuff
under:
man proc_service
0
 
mapcCommented:
The most general solution requires reading permission to /dev/mem or /dev/kmem, depending on implementation. (This is the reason ps is SUID), this is not really wanted, since it creates potential security holes.
However, if your unix supports /proc, this kind of information is exported from kernel space to /proc virtual filesystem, thus could be read.
Thus, accessing /proc/pid/psinfo gives you all the needed information.
The structure for psinfo is given in the manpage of proc- on solaris you can do 'man -s 4 proc'.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
  • 2
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now