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
LVL 39
itsmeandnobodyelseAsked:
Who is Participating?
 
jkrCommented:
Is that a newer version of Solaris? If so, you could read '/proc/<pid>/psinfo' from 'psinfo.pr_psargs' as demonstrated in http://nagiosplug.svn.sourceforge.net/viewvc/nagiosplug/nagiosplug/trunk/plugins-root/pst3.c?view=markup&pathrev=2016 (man, that was not easy to find an example online...)
0
 
itsmeandnobodyelseAuthor Commented:
I could do it by

  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.

0
 
jkrCommented:
>>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'"
0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
itsmeandnobodyelseAuthor Commented:
>>>> 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.

0
 
jkrCommented:
OK, Solaris 9 should definitely be fine: http://en.wikipedia.org/wiki/Procfs#Solaris
0
 
itsmeandnobodyelseAuthor Commented:
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.


0
 
itsmeandnobodyelseAuthor Commented:
What do you think of catching the output of

   pfiles <pid>

with popen ?

0
 
jkrCommented:
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)...
0
 
jdarwinCommented:
I think when a process is run and re-run, each time a different PID is asigned to it, except for process like init.

If so, then would it be possible to relate an old PID with a process in your log file, unless the process name is also logged in your log files.
0
 
itsmeandnobodyelseAuthor Commented:
>>>> .... 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).

0
 
itsmeandnobodyelseAuthor Commented:
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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.