getenv.c revision 60b81b86c4b2eb3a0481176c344f4b6e7a6276fa
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
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 *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * or http://www.opensolaris.org/os/licensing.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
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 *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER END
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Copyright 2016 Joyent, Inc.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* Copyright (c) 1988 AT&T */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* All Rights Reserved */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#pragma weak _putenv = putenv
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "lint.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <mtlib.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/types.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <thread.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <synch.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <stdlib.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <errno.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <string.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <atomic.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define MIN_ENV_SIZE 128
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteextern const char **_environ;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteextern void clean_env();
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto/*
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 *
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 *
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * The setenv() API is inherently leaky but we are completely at the mercy
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto * of the application.
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto *
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 *
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.
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto */
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef struct chunk {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct chunk *next;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte} chunk_t;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szetostatic mutex_t update_lock = DEFAULTMUTEX;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szetostatic const char **orig_environ = NULL;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szetostatic const char **my_environ = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic const char **environ_base = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int environ_size = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int environ_gen = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int initenv_done = 0;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szetostatic chunk_t *chunk_list = NULL;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto/*
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.
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto */
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szetostatic int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteenvsize(const char **e)
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int size;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (e == NULL)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (size = 1; *e != NULL; e++)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (size);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto/*
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 *
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 */
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szetostatic void
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szetoinitenv()
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto{
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto if ((my_environ != _environ) || !initenv_done) {
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto lmutex_lock(&update_lock);
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto if ((my_environ != _environ) || !initenv_done) {
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto if (!initenv_done) {
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto /* Call the NLSPATH janitor in. */
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto clean_env();
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto /* Pacify leak detectors in normal operation. */
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto orig_environ = _environ;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto#ifdef __lint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte my_environ = orig_environ;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte my_environ = _environ;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte environ_base = my_environ;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte environ_size = envsize(environ_base);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte membar_producer();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte initenv_done = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lmutex_unlock(&update_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte membar_consumer();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
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 Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic const char **
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortefindenv(const char **e, const char *string, int name_only, char **value)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char target;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *s1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char *s2;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *value = NULL;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto if (e == NULL)
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto return (NULL);
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto target = name_only ? '\0' : '=';
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto for (; (s2 = *e) != NULL; e++) {
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto s1 = string;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto /* Fast comparison for first char. */
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto if (*s1 != *s2)
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto continue;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto /* Slow comparison for rest of string. */
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto while (*s1 == *s2 && *s2 != '=') {
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto s1++;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto s2++;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto }
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto if (*s1 == target && *s2 == '=') {
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto *value = (char *)s2 + 1;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto return (e);
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto }
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto }
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto return (NULL);
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto}
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto/*
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 *
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 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteaddtoenv(char *string, int overwrite)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *value;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char **p;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte chunk_t *new_chunk;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char **new_environ;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char **new_base;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int new_size;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int old_gen;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte initenv();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lmutex_lock(&update_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (;;) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
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 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((p = findenv(my_environ, string, 0, &value)) != NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (overwrite) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
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 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *p = string;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lmutex_unlock(&update_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* No change. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lmutex_unlock(&update_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-2);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Try to insert the new entry at the bottom of the list. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (environ_base < my_environ) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The new value must be visible before we decrement
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the _environ list pointer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte my_environ[-1] = string;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte membar_producer();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte my_environ--;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte _environ = my_environ;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * We've added a name, so bump the generation number.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte environ_gen++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lmutex_unlock(&update_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
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 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_size = environ_size * 2;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (new_size < MIN_ENV_SIZE)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_size = MIN_ENV_SIZE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto old_gen = environ_gen;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lmutex_unlock(&update_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_chunk = malloc(sizeof (chunk_t) +
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_size * sizeof (char *));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (new_chunk == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte errno = ENOMEM;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lmutex_lock(&update_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
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 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (environ_gen == old_gen)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
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 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lmutex_unlock(&update_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte free(new_chunk);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lmutex_lock(&update_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Add the new chunk to chunk_list to hide potential future leak. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_chunk->next = chunk_list;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte chunk_list = new_chunk;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto /* Copy the old _environ list into the top of the new _environ list. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_base = (const char **)(new_chunk + 1);
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
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Insert the new entry at the bottom of the new _environ list. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_environ[-1] = string;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_environ--;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Ensure that the new _environ list is visible to all. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte membar_producer();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Make the switch (dropping the old _environ list on the floor). */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte environ_base = new_base;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte my_environ = new_environ;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte _environ = my_environ;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte environ_size = new_size;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* We've added a name, so bump the generation number. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte environ_gen++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lmutex_unlock(&update_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * All the work for putenv() is done in addtoenv().
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteputenv(char *string)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
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().
5c8cac22647b12e79982cf052e5df46dc7402bd5tim szeto */
5c8cac22647b12e79982cf052e5df46dc7402bd5tim szeto if (strchr(string, '=') == NULL)
5c8cac22647b12e79982cf052e5df46dc7402bd5tim szeto return (unsetenv(string));
5c8cac22647b12e79982cf052e5df46dc7402bd5tim szeto
5c8cac22647b12e79982cf052e5df46dc7402bd5tim szeto return (addtoenv(string, 1));
5c8cac22647b12e79982cf052e5df46dc7402bd5tim szeto}
5c8cac22647b12e79982cf052e5df46dc7402bd5tim szeto
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
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 Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortesetenv(const char *envname, const char *envval, int overwrite)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte chunk_t *new_chunk;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *new_string;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size_t name_len;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte size_t val_len;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int res;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (envname == NULL || *envname == 0 || strchr(envname, '=') != NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte errno = EINVAL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte name_len = strlen(envname);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte val_len = strlen(envval);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_chunk = malloc(sizeof (chunk_t) + name_len + val_len + 2);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (new_chunk == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte errno = ENOMEM;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_string = (char *)(new_chunk + 1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) memcpy(new_string, envname, name_len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_string[name_len] = '=';
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) memcpy(new_string + name_len + 1, envval, val_len);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte new_string[name_len + 1 + val_len] = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((res = addtoenv(new_string, overwrite)) < 0) {
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto free(new_chunk);
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto if (res == -2) {
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto /* The name already existed, but not an error. */
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto return (0);
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto } else {
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto /* i.e. res == -1 which means only one thing. */
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto errno = ENOMEM;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto return (-1);
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto }
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto }
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto /* Hide potential leak of new_string. */
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto lmutex_lock(&update_lock);
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto new_chunk->next = chunk_list;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto chunk_list = new_chunk;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto lmutex_unlock(&update_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
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 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteunsetenv(const char *name)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte const char **p;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *value;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (name == NULL || *name == 0 || strchr(name, '=') != NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte errno = EINVAL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (-1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte initenv();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lmutex_lock(&update_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Find the target, overwrite it with the first entry, increment the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * _environ pointer.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((p = findenv(my_environ, name, 1, &value)) != NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Overwrite target with the first entry. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *p = my_environ[0];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Ensure that the moved entry is visible to all. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte membar_producer();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Shrink the _environ list. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte my_environ++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte _environ = my_environ;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Make sure addtoenv() knows that we've removed a name. */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte environ_gen++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lmutex_unlock(&update_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Dump entire environment.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteclearenv(void)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
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 */
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto static const char *nullp = NULL;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto lmutex_lock(&update_lock);
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto _environ = &nullp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte my_environ = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte environ_base = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte environ_size = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte environ_gen++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte membar_producer();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lmutex_unlock(&update_lock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * At last, a lockless implementation of getenv()!
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortechar *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortegetenv(const char *name)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *value;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte initenv();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (findenv(_environ, name, 1, &value) != NULL)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (value);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte