[net.unix] Reading environments in C

bill@persci.UUCP (05/16/85)

Here's a problem for you Unix (4.2BSD) gurus out there:

I would like to know how to create a utility (in C) that, given a
process i.d. number, is able to access (print) the environment for that
process. 'ps -e' does this, but the documentation is unclear as to how 
it does it. I have already spent some time on this, poring through code
I have examples for (ex: top.c) that appear to deal with related functions,
but there appears to be a gap in my available documentation (perhaps related
to the way 'csh' keeps the environment). We do not have sources for the
system's utilities like 'ps'.

Executing 'ps -e' is too slow for my purposes.

Any help you can provide, light you can shed, or pointers you can provide
will be gratefully accepted.
-- 
Bill Swan 	{ihnp4|decvax|allegra|...}!uw-beaver!tikal!persci!bill

peter@rlgvax.UUCP (Peter Klosky) (05/20/85)

XXX
> I would like to know how to create a utility (in C) that, given a
> process i.d. number, can access (print) the environment for that
> process. 'ps -e' does this, and how does it work ...
> We do not have source for ps.

Reading the environment or other data for another process is a fairly
complex task.  You have to find out where the data for the process is
located, and it may be on the swap device or possibly in main memory.
And it may be moved while you are trying to get at it, as well.
The knowledge of where the data is located is in the system's process
table, so you have to read that in, too, and the location of the process
table itself is often determined via the namelist of /unix.
Not to worry, this has all been done before.  For an example of a
program that finds out about other processes, see the "ofiles" program
recently posted to net.sources.  

Peter Klosky
XXX