Solaris: getting executable name from pid

Solaris: getting executable name from pid

Post by Stefan Brozinsk » Wed, 10 Oct 2001 17:37:37



Hi,

I am looking for a method to get a process-executable-name from a pid. Any
ideas?

Best regards
Stefan

 
 
 

Solaris: getting executable name from pid

Post by Casper H.S. Dik - Network Security Engine » Wed, 10 Oct 2001 18:27:49


[[ PLEASE DON'T SEND ME EMAIL COPIES OF POSTINGS ]]


>I am looking for a method to get a process-executable-name from a pid. Any
>ideas?

man proc(4)

read /proc/pid/psinfo

The actual executable itself can be found as:

/proc/pid/object/a.out

Casper
--
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.

 
 
 

Solaris: getting executable name from pid

Post by Timothy Stou » Wed, 10 Oct 2001 22:46:46


If you're working on the command line, try the following:
ps -o pid -o comm | grep PID | cut -d' ' -f3
where PID is the known pid

If you're talking about doing this from C code:
system("ps -o pid -o comm | grep 1367 | cut -d' ' -f3 > outifle");
then open and read the file "outfile"  (might work...)

Good luck,
Tim


> Hi,

> I am looking for a method to get a process-executable-name from a pid. Any
> ideas?

> Best regards
> Stefan

 
 
 

Solaris: getting executable name from pid

Post by those who know me have no need of my nam » Fri, 12 Oct 2001 00:26:16



Quote:>If you're working on the command line, try the following:
>ps -o pid -o comm | grep PID | cut -d' ' -f3
>where PID is the known pid

if you know the pid why would you grep a complete list for it?  just have
ps display only that pid's information.  (besides, your grep would match
any process who's "command" is also that number -- unlikely, but not
impossible.)  and by doing that and dropping the heading line you don't
need either grep or cut, i.e., just

  ps -h -o comm -p PID

Quote:>If you're talking about doing this from C code:
>system("ps -o pid -o comm | grep 1367 | cut -d' ' -f3 > outifle");
>then open and read the file "outfile"  (might work...)

it's easier and somewhat safer to use popen, e.g.,

  char buf[100] = {0};
  FILE *p = popen("ps -h -o comm -p 1367", "r");
  if (NULL == p) perror("popen");
  else {
    if (NULL == fgets(buf,sizeof buf,p)) if (0 != errno) perror("fgets");
    pclose(p);
  }

it would be much safer to use exec, but there's more code you have to write
to handle it, e.g.,

  char buf[100] = {0}; int p[2];
  if (0 > pipe(p)) perror("pipe");
  else {
    pid_t pid = fork();
    if ((pid_t)0 > pid) perror("fork");
    else if ((pid_t)0 == pid) {
      if (0 > dup2(p[1],STDOUT_FILENO)) perror("dup2");
      else execl("/bin/ps","ps","-h","-o","comm","-p","1367",NULL);
      _exit(127);
    }
    else {
      pid_t n;
      do n = waitpid(pid,NULL,0); while ((pid_t)0 > n && EINTR == errno);
      if (n != pid) error("waitpid");
      else {
        FILE *f = fdopen(p[0],"r");
        if (NULL == f) perror("fdopen");
        else {
          if (NULL == fgets(buf,sizeof buf,f)) if (0 != errno) perror("fgets");
          if (0 > fclose(f)) perror("fclose");
        }
      }
    }
    close(p[0]); close(p[1]);
  }

note: i've excluded some stuff, such as signal handling and closing unused
pipe fd's, but is the basic form.

--
okay, have a sig then

 
 
 

1. Getting the executable name.

Hi all,
What's the best way to get the executable name of a program ?

I mean :

int main (int argc, char *argv[])
{

  printf("myname is: %s\n", argv[0]);

  return 0;

This is ok but if the program is executed like this ./program
or /usr/mysuperlong/path/program the output wouldn't be
the executable name only (path is included).

So how could I do ?

Thanks.

2. "Serial Line is Looped Back". ?

3. getting pid by name

4. connecting to remote windows machines via samba ?

5. Getting the executable's name without using argv[0]

6. About Pinnacle PCTV support for Linux

7. Getting executable name of a running process

8. catman - help!

9. Getting children's PID with parent's pid?

10. Named - Not writing named.pid anymore

11. Which named ?...Solaris 2.5 in.named or Berkley named ?

12. getting full domain name from Solaris 2.4

13. How to make a RSH and getting is PID.