On Fri, Apr 18, 2008 at 03:20:56PM +0200, Jurjen Oskam wrote:
> Hi there,
>
> I'm seeing something I don't quite understand concerning environment
> variables. (This is on an OpenBSD 4.2 amd64 system) I hope someone here
> can explain.
>
> Given the following C-program:
>
> #include <stdio.h>
> #include <errno.h>
> #include <stdlib.h>
>
> int main(int argc, char **argv)
> {
> char *var1 = "FOO=TESTING";
> int rc;
>
> sleep(10);
>
> rc = putenv(var1);
> if (rc < 0) {
> printf("Error inserting <%s> in environ, errno = %d\n",
> var1, errno);
> return 1;
> }
>
> printf("<%s> inserted in environ\n", var1);
> sleep(10);
>
> return 0;
> }
>
> In another terminal, I start a while loop:
>
> $ while true ; do ps -eww | grep F[O]O ; sleep 1 ; done
>
> When I run this program using "env -i ./a.out", the while loop
> in the other terminal doesn't show any output at all. ps doesn't
> seem to see FOO being put in the environment.
>
> However, when I start the program using "env -i FOO=BAR ./a.out", the
> while loop in the other terminal shows this output, beginning right after
> the start of the program:
>
> 20571 p2 I+ 0:00.00 FOO=BAR (a.out)
> 20571 p2 I+ 0:00.00 FOO=BAR (a.out)
> 20571 p2 I+ 0:00.00 FOO=BAR (a.out)
> 20571 p2 I+ 0:00.00 FOO=BAR (a.out)
> 20571 p2 I+ 0:00.00 FOO=BAR (a.out)
> 20571 p2 I+ 0:00.00 FOO=BAR (a.out)
> 20571 p2 I+ 0:00.00 FOO=BAR (a.out)
> 20571 p2 I+ 0:00.00 FOO=BAR (a.out)
> 20571 p2 I+ 0:00.00 FOO=BAR (a.out)
> 20571 p2 I+ 0:00.00 FOO=BAR (a.out)
> 20571 p2 I+ 0:00.00 FOO=TESTING (a.out)
> 20571 p2 I+ 0:00.00 FOO=TESTING (a.out)
> 20571 p2 I+ 0:00.00 FOO=TESTING (a.out)
> 20571 p2 I+ 0:00.00 FOO=TESTING (a.out)
> 20571 p2 I+ 0:00.00 FOO=TESTING (a.out)
> 20571 p2 I+ 0:00.00 FOO=TESTING (a.out)
> 20571 p2 I+ 0:00.00 FOO=TESTING (a.out)
> 20571 p2 I+ 0:00.00 FOO=TESTING (a.out)
> 20571 p2 I+ 0:00.00 FOO=TESTING (a.out)
> 20571 p2 I+ 0:00.00 FOO=TESTING (a.out)
>
>
> So ps does show FOO, *and* it shows the value of FOO changing after
> ten seconds.
>
> I don't understand this behaviour. On another system (AIX), ps does pick
> up newly set environment variables. Is this behaviour implementation
> dependent?
It might be the number of env vars is fixed in the process info, while
the environ array can be modified by the process, I suspect that
ps_nenvstr in struct ps_strings on the kernel side is not updated.
-Otto