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!
magentaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.