Program name from PID?

Guy Harris guy at auspex.auspex.com
Mon Feb 18 10:34:28 AEST 1991


>On a Sun, it looks like you can use kvm_getu() from libkvm.a .

"kvm_getu()" gets the U area of the process, which contains "u_comm",
which is one of the possible "names" of the process.  The other possible
"name" is the command and arguments for the process, which can be
obtained with "kvm_getcmd()".

>Otherwise, you're going to have to nlist the kernel to find the proc
>structure, use that to find your pid's proc entry, then use the pointer
>there to go find the corresponding user structure, and then there you'll
>find the command name.  Of course, all this assumes a fairly standard
>looking kernel -- I've not tried it on anything that's not a BSD system,
>i.e. Suns, Vaxen, and Convexen.

Later System V systems store a character string fabricated from the
arguments passed in an "exec()" in "u_psargs".  "u_comm" is there in
AT&T S5 releases as well.  The systems I'd worry about are the ones not
derived from AT&T source, or at least the ones where the lower levels of
the system aren't derived from AT&T source.  (Enough of older BSD
systems is sufficiently derived from AT&T source; 4.3-reno's U area
isn't AT&T-derived - or, at least, "user.h" has the "AT&T-free"
copyright notice - and it does *not* have anything like "u_comm"; it
stores that information in the proc structure instead.)

I think S5R4's "/proc" file system lets you fetch that string without
having to fetch the user structure yourself.



More information about the Comp.unix.questions mailing list