Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

djberg96 (2603)

djberg96
  (email not shown publicly)

Journal of djberg96 (2603)

Tuesday September 03, 2002
11:00 PM

Parsing environ in /proc

[ #7514 ]
I thought I would add a nice little enhancement to my Sys-ProcTable module for the Linux version. My plan was to add the information in /proc/xxx/environ as a hash to the ProcStat structure. The stuff in the environ file consists of environment variables and their settings in the form of "PWD=/home/blah", etc. Thus, if you wanted the PWD of pid 2345, you could simply do "print p.environ['PWD']".

However, parsing the environ file is proving to be a royal pain in the arse. I got over the first hurdle of dealing with a bunch of NUL terminated strings. I've also been able to split the string on the '=' sign and turn them into key/value pairs. I've hit two snags (so far), however.

The first is that sometimes this file can be empty. Right now that causes a segfault. You can't simply stat the file either - environ files are always size 0. The read and fread functions can't be relied on either. I'm going to play with lseek to see if that will work.

The second is that not all the environ files contain data in "KEY=/some/val" format. I found one that simply contained the string "--suicide", which I thought was appropriate, since it succeeded in killing my program. This could actually be a bigger problem. What should I do for a case like this? I'm thinking of just making the whole string the key, and assigning '1' as the value.

Of course, I could just be a bum and return everything as a big, ugly string for YOU to parse. :)

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.