LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Solaris / OpenSolaris
User Name
Password
Solaris / OpenSolaris This forum is for the discussion of Solaris and OpenSolaris.
General Sun, SunOS and Sparc related questions also go here.

Notices

Reply
 
Search this Thread
Old 12-21-2004, 02:08 AM   #1
praj_linux
LQ Newbie
 
Registered: Nov 2003
Posts: 7

Rep: Reputation: 0
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.
 
Old 12-21-2004, 03:45 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,482

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Here's one way:
Code:
pmap <process-id> | head -2 | tail -1 | nawk '{print $4}'
 
Old 12-21-2004, 03:57 AM   #3
praj_linux
LQ Newbie
 
Registered: Nov 2003
Posts: 7

Original Poster
Rep: Reputation: 0


I want it programmatically.
 
Old 12-21-2004, 06:32 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,482

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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])));
}

Last edited by jlliagre; 12-21-2004 at 06:35 AM.
 
Old 12-21-2004, 06:55 AM   #5
praj_linux
LQ Newbie
 
Registered: Nov 2003
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 12-21-2004, 10:01 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,482

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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])));
}

Last edited by jlliagre; 12-21-2004 at 10:06 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
finding the PATH tahiche Linux - Software 7 10-12-2005 12:47 PM
finding process manager.. navaladi Mandriva 3 01-11-2005 03:25 PM
Finding the kernel path blacksunshine Linux - Software 2 10-03-2004 02:45 PM
finding java path? pyropenguin Linux - Newbie 2 09-19-2002 01:02 PM
finding java path? pyropenguin Linux - Software 2 09-15-2002 07:55 AM


All times are GMT -5. The time now is 10:43 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration