Solaris / OpenSolaris This forum is for the discussion of Solaris and OpenSolaris.
General Sun, SunOS and Sparc related questions also go here.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
12-21-2004, 02:08 AM
#1
LQ Newbie
Registered: Nov 2003
Posts: 7
Rep:
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.
12-21-2004, 03:45 AM
#2
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OEL
Posts: 9,165
Here's one way:
Code:
pmap <process-id> | head -2 | tail -1 | nawk '{print $4}'
12-21-2004, 03:57 AM
#3
LQ Newbie
Registered: Nov 2003
Posts: 7
Original Poster
Rep:
I want it programmatically.
12-21-2004, 06:32 AM
#4
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OEL
Posts: 9,165
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 .
12-21-2004, 06:55 AM
#5
LQ Newbie
Registered: Nov 2003
Posts: 7
Original Poster
Rep:
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.
12-21-2004, 10:01 AM
#6
Moderator
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Ubuntu, OEL
Posts: 9,165
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 .
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 08:06 PM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News