getenv.c revision 60b81b86c4b2eb3a0481176c344f4b6e7a6276fa
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The contents of this file are subject to the terms of the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Common Development and Distribution License (the "License").
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You may not use this file except in compliance with the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When distributing Covered Code, include this CDDL HEADER in each
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If applicable, add the following below this CDDL HEADER, with the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner]
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER END
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright 2016 Joyent, Inc.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* Copyright (c) 1988 AT&T */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* All Rights Reserved */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteextern const char **_environ;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteextern void clean_env();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * For performance and consistency reasons we expand the _environ list using
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * the trusted "power of two, drop it on the floor" method. This allows for
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * a lockless, single pass implementation of getenv(), yet the memory leak
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * is bounded - in normal circumstances total wastage is never greater than
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * 3x the space needed to hold any _environ list.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The only abnormal circumstance is if an application modifies the _environ
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * list pointer directly. Such an application does not conform to POSIX.1
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * 2001. However, we also care about standards which did not foresee this
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * issue. For this reason we keep a working copy of our notion of _environ in
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * my_environ. If, when we are called upon to modify _environ, we ever detect
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * a mismatch between _environ and my_environ we discard all our assumptions
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * concerning the location and size of the _environ list. As an additional
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * precaution we only ever update _environ once we have finished manipulating
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * our working copy.
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * The setenv() API is inherently leaky but we are completely at the mercy
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * of the application.
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * To pacify leak detectors we chain all allocations which are at risk of
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * being leaked in either of the above two scenarios. chunk_list must only
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * be updated under the protection of update_lock.
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * Although we don't allocate the original _environ list it is likely that
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * we will leak this too. Accordingly, we create a reference in initenv().
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * However, we can't be held responsible for such leaks in abnormal (see
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * above) circumstances.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef struct chunk {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int environ_size = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int environ_gen = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int initenv_done = 0;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * Compute the size an _environ list including the terminating NULL entry.
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * This is the only way we have to determine the size of an _environ list
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * we didn't allocate.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteenvsize(const char **e)
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * Initialization for the following scenarios:
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * 1. The very first time we reference the _environ list we must call in the
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * NLSPATH janitor, make a reference to the original _environ list to keep
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * leak detectors happy, initialize my_environ and environ_base, and then
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * compute environ_size.
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * 2. Whenever we detect that someone else has hijacked _environ (something
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * very abnormal) we need to reinitialize my_environ and environ_base,
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * and then recompute environ_size.
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * The local globals my_environ, environ_base and environ_size may be used
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * by others only if initenv_done is true and only under the protection of
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * update_lock. However, our callers, who must NOT be holding update_lock,
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * may safely test initenv_done or my_environ against _environ just prior to
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * calling us because we test these again whilst holding update_lock.
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto /* Call the NLSPATH janitor in. */
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto /* Pacify leak detectors in normal operation. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Search an _environ list for a particular entry. If name_only is set, then
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * string must be the entry name only, and we return the value of the first
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * match. Otherwise, string must be of the form "name=value", and we return
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the address of the first matching entry.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic const char **
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortefindenv(const char **e, const char *string, int name_only, char **value)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *s1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *s2;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto /* Fast comparison for first char. */
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto /* Slow comparison for rest of string. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Common code for putenv() and setenv(). We support the lockless getenv()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * by inserting new entries at the bottom of the list, and by growing the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * list using the trusted "power of two, drop it on the floor" method. We
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * use a lock (update_lock) to protect all updates to the _environ list, but
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * we are obliged to release this lock whenever we call malloc() or free().
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * A generation number (environ_gen) is bumped whenever names are added to,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * or removed from, the _environ list so that we can detect collisions with
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * other updaters.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Return values
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * 0 : success
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * -1 : with errno set
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * -2 : an entry already existed and overwrite was zero
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char **p;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char **new_base;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If the name already exists just overwrite the existing
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * entry -- except when we were called by setenv() without
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the overwrite flag.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((p = findenv(my_environ, string, 0, &value)) != NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Replace the value in situ. No name was
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * added, so there is no need to bump the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * generation number.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* No change. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-2);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Try to insert the new entry at the bottom of the list. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The new value must be visible before we decrement
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the _environ list pointer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * We've added a name, so bump the generation number.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * There is no room. Attempt to allocate a new _environ list
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * which is at least double the size of the current one. See
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * comment above concerning locking and malloc() etc.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_size * sizeof (char *));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If no other thread added or removed names while the lock
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * was dropped, it is time to break out of this loop.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * At least one name has been added or removed, so we need to
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * try again. It is very likely that we will find sufficient
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * space the next time around.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Add the new chunk to chunk_list to hide potential future leak. */
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto /* Copy the old _environ list into the top of the new _environ list. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_environ = &new_base[(new_size - 1) - environ_size];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) memcpy(new_environ, my_environ, environ_size * sizeof (char *));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Insert the new entry at the bottom of the new _environ list. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Ensure that the new _environ list is visible to all. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Make the switch (dropping the old _environ list on the floor). */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* We've added a name, so bump the generation number. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * All the work for putenv() is done in addtoenv().
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Historically a call to putenv() with no '=' in the string would work
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * great until someone called getenv() on that particular environment
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * variable again. As we've always treated this as valid, rather than
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * teaching the rest of the environment code how to handle something
5c8cac22647b12e79982cf052e5df46dc7402bd5tim szeto * without an '=' sign, it instead just calls unsetenv().
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * setenv() is a little more complex than putenv() because we have to allocate
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and construct an _environ entry on behalf of the caller. The bulk of the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * work is still done in addtoenv().
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortesetenv(const char *envname, const char *envval, int overwrite)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (envname == NULL || *envname == 0 || strchr(envname, '=') != NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_chunk = malloc(sizeof (chunk_t) + name_len + val_len + 2);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) memcpy(new_string + name_len + 1, envval, val_len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((res = addtoenv(new_string, overwrite)) < 0) {
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto /* The name already existed, but not an error. */
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto /* i.e. res == -1 which means only one thing. */
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto return (-1);
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto /* Hide potential leak of new_string. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * unsetenv() is tricky because we need to compress the _environ list in a way
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * which supports a lockless getenv(). The approach here is to move the first
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * entry from the enrivon list into the space occupied by the entry to be
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * deleted, and then to increment _environ. This has the added advantage of
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * making _any_ incremental linear search of the _environ list consistent (i.e.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * we will not break any naughty apps which read the list without our help).
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char **p;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (name == NULL || *name == 0 || strchr(name, '=') != NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Find the target, overwrite it with the first entry, increment the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * _environ pointer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((p = findenv(my_environ, name, 1, &value)) != NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Overwrite target with the first entry. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Ensure that the moved entry is visible to all. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Shrink the _environ list. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Make sure addtoenv() knows that we've removed a name. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Dump entire environment.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Just drop the entire environment list on the floor, as it
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * would be non-trivial to try and free the used memory.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * At last, a lockless implementation of getenv()!