Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 9 Jun 2008 22:27:00 -0500 (CDT)
From:      "Sean C. Farley" <scf@FreeBSD.org>
To:        Timo Sirainen <tss@iki.fi>
Cc:        freebsd-stable@FreeBSD.org
Subject:   Re: Environment clearing broken in 7.0
Message-ID:  <alpine.BSF.1.10.0806092118250.3974@thor.farley.org>
In-Reply-To: <1213036854.3904.967.camel@hurina>
References:  <1213036854.3904.967.camel@hurina>

next in thread | previous in thread | raw e-mail | index | archive | help
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
-- 
scf@FreeBSD.org



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?alpine.BSF.1.10.0806092118250.3974>