On Mon, 9 Jun 2008, Timo Sirainen wrote:

I think clearing environment using:

environ[0] = NULL;

has been kind of a semi-standard for a while now. At least Dovecot and
Postfix clears their environment this way. But this no longer works in
FreeBSD 7.0 (putenv(), environ[0]=NULL, putenv() -> everything is
visible again). Was this change intended, or will this be fixed?

It is more or less intended.  When a program sets an environment
variable, the environment is copied for faster/leaner usage.  Changing
individual values within environ is not checked else every pointer would
need to be checked for consistency.  What I did was to write the code to
detect if environ is replaced (NULL or new array of variables).

I suggest reading the two paragraphs from Open Group's getenv()[1]
documentation starting at "Conforming applications are required not to
modify environ directly, ..." for the rationale in the new design.
Obviously, applications are not required to conform, but the
documentation talks about what an OS may be doing under the covers to
environ.

Out of curiosity, do Dovecot and Postfix check that environ is not NULL
before setting environ[0]?  environ may be set to NULL at the start but
not by FreeBSD's /usr/bin/env -i.

Looks like I could work around this by using:

environ = NULL;

That will work on the *BSD's, OpenSolaris and Linux.

Also, this will work:
environ = calloc(1, sizeof(*environ));

but I'm afraid what other OSes that change would break. I guess going
through environ and unsetenv()ing everything would work too, but it
feels annoyingly slow for such a simple operation.

OpenSolaris does something similar with environ[2].  It also detects in
initenv() a replacement of environ but not changes to individual
entries.

Sean
  1. http://www.opengroup.org/onlinepubs/000095399/functions/getenv.html
  2. 
http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/port/gen/getenv.c
--
[EMAIL PROTECTED]
_______________________________________________
freebsd-stable@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-stable
To unsubscribe, send any mail to "[EMAIL PROTECTED]"

Reply via email to