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