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) ?

LVL 39
Who is Participating?
Is that a newer version of Solaris? If so, you could read '/proc/<pid>/psinfo' from 'psinfo.pr_psargs' as demonstrated in (man, that was not easy to find an example online...)
itsmeandnobodyelseAuthor Commented:
I could do it by

  char strpid[] = "11111";
  char stmt[256] = { '\0' };
  sprintf(stmt, "ps -f -p %s >ps.out", strpid);

and parse the ps.out after that.

But I would prefer an api.

>>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'"
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.

OK, Solaris 9 should definitely be fine:
itsmeandnobodyelseAuthor Commented:
I found a link at

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.

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

   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)...
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.
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).

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.
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.