LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Solaris / OpenSolaris (https://www.linuxquestions.org/questions/solaris-opensolaris-20/)
-   -   Finding process path (https://www.linuxquestions.org/questions/solaris-opensolaris-20/finding-process-path-268677/)

praj_linux 12-21-2004 02:08 AM

Finding process path
 
Hi,
I am having difficulty in finding process path.

On Linux, I was doing it using
readlink("/proc/self/exe", szAppPath, MAX_PATH);

or

sprintf(szStr, "/proc/%d/exe", nProcID);
readlink(szStr, szAppPath, MAX_PATH);

But on Solaris 9 x86 platform, this function fails,
as there is no "exe" entry in /proc/self.

Is there any other way to find the process path?.


Praj.

jlliagre 12-21-2004 03:45 AM

Here's one way:
Code:

pmap <process-id> | head -2 | tail -1 | nawk '{print $4}'

praj_linux 12-21-2004 03:57 AM

:confused:

I want it programmatically.

jlliagre 12-21-2004 06:32 AM

Well, I did it programmatically ... with shell and a piece of awk ;)

If by programmatically you mean you want it in C, what about:
Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

char *getpath(int pid)
{
        FILE *fp;
        char command[128];
        char output[64];
        char reply[32];
        strcpy(reply, "");
        sprintf(output, "/tmp/.path.%d", pid);
        sprintf(command, "/usr/proc/bin/pmap %d | head -2 | tail -1 | nawk '{print $4}' >%s", pid, output);
        system(command);
        if((fp=fopen(output, "r"))!=NULL)
        {
                fgets(reply, 32, fp);
                fclose(fp);
                unlink(output);
        }
        return(reply);
}

main(int argc, char **argv)
{
        printf("%s", getpath(atoi(argv[1])));
}


praj_linux 12-21-2004 06:55 AM

Thanks a lot.

But it prints some brackets
[

I am working on

Sun Solaris 9 - x86 Platform Edition
gcc version 3.3.2
GNU ld version 2.11.2
GNU Make 3.80

Am I on the wrong track.

jlliagre 12-21-2004 10:01 AM

Here's a slightly better version that should work for you:
Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

char *getpath(int pid)
{
  FILE *fp;
  char command[256];
  char output[64];
  char reply[32];
  strcpy(reply, "");
  sprintf(output, "/tmp/.path.%d", pid);
  sprintf(command, "/usr/proc/bin/pmap %d | nawk '\n{\nif(substr($3,3,1)==\"x\") {\nprint $4\nexit(0);\n}\n}\n'>%s",
    pid, output);
  system(command);
  if((fp=fopen(output, "r"))!=NULL)
  {
    fgets(reply, 32, fp);
    fclose(fp);
    unlink(output);
  }
  return(reply);
}

main(int argc, char **argv)
{
  printf("%s", getpath(atoi(argv[1])));
}



All times are GMT -5. The time now is 11:27 AM.