Parsing environ in /proc

djberg96 on 2002-09-04T04:00:50

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