From owner-freebsd-stable@FreeBSD.ORG Tue Jun 10 03:38:32 2008 Return-Path: Delivered-To: freebsd-stable@FreeBSD.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id C4E641065683 for ; Tue, 10 Jun 2008 03:38:32 +0000 (UTC) (envelope-from scf@FreeBSD.org) Received: from mail.farley.org (farley.org [67.64.95.201]) by mx1.freebsd.org (Postfix) with ESMTP id 83A4B8FC15 for ; Tue, 10 Jun 2008 03:38:32 +0000 (UTC) (envelope-from scf@FreeBSD.org) Received: from thor.farley.org (HPooka@thor.farley.org [192.168.1.5]) by mail.farley.org (8.14.3/8.14.3) with ESMTP id m5A3R0ct083439; Mon, 9 Jun 2008 22:27:00 -0500 (CDT) (envelope-from scf@FreeBSD.org) Date: Mon, 9 Jun 2008 22:27:00 -0500 (CDT) From: "Sean C. Farley" To: Timo Sirainen In-Reply-To: <1213036854.3904.967.camel@hurina> Message-ID: References: <1213036854.3904.967.camel@hurina> User-Agent: Alpine 1.10 (BSF 962 2008-03-14) MIME-Version: 1.0 Content-Type: TEXT/PLAIN; format=flowed; charset=US-ASCII Cc: freebsd-stable@FreeBSD.org Subject: Re: Environment clearing broken in 7.0 X-BeenThere: freebsd-stable@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Production branch of FreeBSD source code List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 10 Jun 2008 03:38:32 -0000 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