itsmeandnobodyelse
asked on
How to get the process name for a given pid in Solaris?
We have huge log files where the process pid is one of the informations given with a log entry.
Does anybody know an api function to retrieve the process names (or better the start command process + arguments) ?
TIA
Alex
Does anybody know an api function to retrieve the process names (or better the start command process + arguments) ?
TIA
Alex
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>>you could read '/proc/<pid>/psinfo' from 'psinfo.pr_psargs'
Sorry, make that "you could read '/proc/<pid>/psinfo' and get the name from 'psinfo.pr_psargs'"
Sorry, make that "you could read '/proc/<pid>/psinfo' and get the name from 'psinfo.pr_psargs'"
ASKER
>>>> man, that was not easy to find an example online...
;-)
>>>> This executable works by reading process address structures, so needs to be executed as root
That could be a problem cause the logfiles were written by non-root processes.
>>>> Is that a newer version of Solaris?
It is Solaris 9.
;-)
>>>> This executable works by reading process address structures, so needs to be executed as root
That could be a problem cause the logfiles were written by non-root processes.
>>>> Is that a newer version of Solaris?
It is Solaris 9.
OK, Solaris 9 should definitely be fine: http://en.wikipedia.org/wiki/Procfs#Solaris
ASKER
I found a link at
http://www.sunmanagers.org/pipermail/summaries/2002-May/001674.html
which does similar, i. e.
- open /proc/<pid> what gives file descriptor fd of <pid> folder
- iocntl on the fd what fills prpsinfo structure (from old_procfs.h).
- Then prpsinfo has member pr_fname what is the process name
(actually it is not really simple)
But main drawback also seems to be that we would need more access rights to read that information at runtime as we have today.
http://www.sunmanagers.org/pipermail/summaries/2002-May/001674.html
which does similar, i. e.
- open /proc/<pid> what gives file descriptor fd of <pid> folder
- iocntl on the fd what fills prpsinfo structure (from old_procfs.h).
- Then prpsinfo has member pr_fname what is the process name
(actually it is not really simple)
But main drawback also seems to be that we would need more access rights to read that information at runtime as we have today.
ASKER
What do you think of catching the output of
pfiles <pid>
with popen ?
pfiles <pid>
with popen ?
Might be an option, but the question is whether the exectuable image is listed as an opened file (can't test it, no Solaris here)...
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
>>>> .... unless the process name is also logged in your log files.
Yes, that is the goal. We want to have logfile entries with timestamp, pid, processname, severity, errorcode, errortext (free).
Yes, that is the goal. We want to have logfile entries with timestamp, pid, processname, severity, errorcode, errortext (free).
ASKER
We probably use the solution to redirect the output of ps to a file. Though it is about 150 processes which could write to the logfiles, the pid's mostly are the same (the processes do not restart but keep running permanently). So the overhead for retrieving the processname once for a new pid is acceptable.
ASKER
char strpid[] = "11111";
char stmt[256] = { '\0' };
sprintf(stmt, "ps -f -p %s >ps.out", strpid);
system(stmt);
and parse the ps.out after that.
But I would prefer an api.