Link to home
Start Free TrialLog in
Avatar of itsmeandnobodyelse
itsmeandnobodyelseFlag for Germany

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
Avatar of itsmeandnobodyelse
itsmeandnobodyelse
Flag of Germany image

ASKER

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.

ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>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'"
>>>> 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: http://en.wikipedia.org/wiki/Procfs#Solaris
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.


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)...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>>> .... 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).

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.