10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
53864Sraf * Common Development and Distribution License (the "License").
63864Sraf * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
213864Sraf
220Sstevel@tonic-gate /*
23*13093SRoger.Faulkner@Oracle.COM * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate */
250Sstevel@tonic-gate
260Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */
270Sstevel@tonic-gate /* All Rights Reserved */
280Sstevel@tonic-gate
296812Sraf #pragma weak _putenv = putenv
300Sstevel@tonic-gate
316812Sraf #include "lint.h"
320Sstevel@tonic-gate #include <mtlib.h>
330Sstevel@tonic-gate #include <sys/types.h>
340Sstevel@tonic-gate #include <thread.h>
350Sstevel@tonic-gate #include <synch.h>
360Sstevel@tonic-gate #include <stdlib.h>
370Sstevel@tonic-gate #include <errno.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <atomic.h>
400Sstevel@tonic-gate
410Sstevel@tonic-gate #define MIN_ENV_SIZE 128
420Sstevel@tonic-gate
436879Sraf extern const char **_environ;
440Sstevel@tonic-gate extern void clean_env();
450Sstevel@tonic-gate
460Sstevel@tonic-gate /*
476879Sraf * For performance and consistency reasons we expand the _environ list using
480Sstevel@tonic-gate * the trusted "power of two, drop it on the floor" method. This allows for
490Sstevel@tonic-gate * a lockless, single pass implementation of getenv(), yet the memory leak
500Sstevel@tonic-gate * is bounded - in normal circumstances total wastage is never greater than
516879Sraf * 3x the space needed to hold any _environ list.
520Sstevel@tonic-gate *
536879Sraf * The only abnormal circumstance is if an application modifies the _environ
540Sstevel@tonic-gate * list pointer directly. Such an application does not conform to POSIX.1
550Sstevel@tonic-gate * 2001. However, we also care about standards which did not foresee this
566879Sraf * issue. For this reason we keep a working copy of our notion of _environ in
576879Sraf * my_environ. If, when we are called upon to modify _environ, we ever detect
586879Sraf * a mismatch between _environ and my_environ we discard all our assumptions
596879Sraf * concerning the location and size of the _environ list. As an additional
606879Sraf * precaution we only ever update _environ once we have finished manipulating
610Sstevel@tonic-gate * our working copy.
620Sstevel@tonic-gate *
630Sstevel@tonic-gate * The setenv() API is inherently leaky but we are completely at the mercy
640Sstevel@tonic-gate * of the application.
650Sstevel@tonic-gate *
660Sstevel@tonic-gate * To pacify leak detectors we chain all allocations which are at risk of
670Sstevel@tonic-gate * being leaked in either of the above two scenarios. chunk_list must only
680Sstevel@tonic-gate * be updated under the protection of update_lock.
690Sstevel@tonic-gate *
706879Sraf * Although we don't allocate the original _environ list it is likely that
710Sstevel@tonic-gate * we will leak this too. Accordingly, we create a reference in initenv().
720Sstevel@tonic-gate * However, we can't be held responsible for such leaks in abnormal (see
730Sstevel@tonic-gate * above) circumstances.
740Sstevel@tonic-gate */
750Sstevel@tonic-gate
760Sstevel@tonic-gate typedef struct chunk {
770Sstevel@tonic-gate struct chunk *next;
780Sstevel@tonic-gate } chunk_t;
790Sstevel@tonic-gate
800Sstevel@tonic-gate static mutex_t update_lock = DEFAULTMUTEX;
810Sstevel@tonic-gate static const char **orig_environ = NULL;
820Sstevel@tonic-gate static const char **my_environ = NULL;
830Sstevel@tonic-gate static const char **environ_base = NULL;
840Sstevel@tonic-gate static int environ_size = 0;
850Sstevel@tonic-gate static int environ_gen = 0;
860Sstevel@tonic-gate static int initenv_done = 0;
870Sstevel@tonic-gate static chunk_t *chunk_list = NULL;
880Sstevel@tonic-gate
890Sstevel@tonic-gate /*
906879Sraf * Compute the size an _environ list including the terminating NULL entry.
916879Sraf * This is the only way we have to determine the size of an _environ list
920Sstevel@tonic-gate * we didn't allocate.
930Sstevel@tonic-gate */
940Sstevel@tonic-gate static int
envsize(const char ** e)950Sstevel@tonic-gate envsize(const char **e)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate int size;
980Sstevel@tonic-gate
990Sstevel@tonic-gate if (e == NULL)
1000Sstevel@tonic-gate return (0);
1010Sstevel@tonic-gate
1020Sstevel@tonic-gate for (size = 1; *e != NULL; e++)
1030Sstevel@tonic-gate size++;
1040Sstevel@tonic-gate
1050Sstevel@tonic-gate return (size);
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
1080Sstevel@tonic-gate /*
1090Sstevel@tonic-gate * Initialization for the following scenarios:
1106879Sraf * 1. The very first time we reference the _environ list we must call in the
1116879Sraf * NLSPATH janitor, make a reference to the original _environ list to keep
1120Sstevel@tonic-gate * leak detectors happy, initialize my_environ and environ_base, and then
1130Sstevel@tonic-gate * compute environ_size.
1146879Sraf * 2. Whenever we detect that someone else has hijacked _environ (something
1150Sstevel@tonic-gate * very abnormal) we need to reinitialize my_environ and environ_base,
1160Sstevel@tonic-gate * and then recompute environ_size.
1170Sstevel@tonic-gate *
1180Sstevel@tonic-gate * The local globals my_environ, environ_base and environ_size may be used
1190Sstevel@tonic-gate * by others only if initenv_done is true and only under the protection of
1200Sstevel@tonic-gate * update_lock. However, our callers, who must NOT be holding update_lock,
1216879Sraf * may safely test initenv_done or my_environ against _environ just prior to
1220Sstevel@tonic-gate * calling us because we test these again whilst holding update_lock.
1230Sstevel@tonic-gate */
1240Sstevel@tonic-gate static void
initenv()1250Sstevel@tonic-gate initenv()
1260Sstevel@tonic-gate {
1276879Sraf if ((my_environ != _environ) || !initenv_done) {
1283864Sraf lmutex_lock(&update_lock);
1296879Sraf if ((my_environ != _environ) || !initenv_done) {
1303864Sraf if (!initenv_done) {
1313864Sraf /* Call the NLSPATH janitor in. */
1323864Sraf clean_env();
1330Sstevel@tonic-gate
1343864Sraf /* Pacify leak detectors in normal operation. */
1356879Sraf orig_environ = _environ;
1360Sstevel@tonic-gate #ifdef __lint
1373864Sraf my_environ = orig_environ;
1380Sstevel@tonic-gate #endif
1393864Sraf }
1400Sstevel@tonic-gate
1416879Sraf my_environ = _environ;
1423864Sraf environ_base = my_environ;
1433864Sraf environ_size = envsize(environ_base);
1443864Sraf membar_producer();
1450Sstevel@tonic-gate initenv_done = 1;
1460Sstevel@tonic-gate }
1473864Sraf lmutex_unlock(&update_lock);
1480Sstevel@tonic-gate }
1493864Sraf membar_consumer();
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate /*
1536879Sraf * Search an _environ list for a particular entry. If name_only is set, then
1540Sstevel@tonic-gate * string must be the entry name only, and we return the value of the first
1550Sstevel@tonic-gate * match. Otherwise, string must be of the form "name=value", and we return
1560Sstevel@tonic-gate * the address of the first matching entry.
1570Sstevel@tonic-gate */
1580Sstevel@tonic-gate static const char **
findenv(const char ** e,const char * string,int name_only,char ** value)1590Sstevel@tonic-gate findenv(const char **e, const char *string, int name_only, char **value)
1600Sstevel@tonic-gate {
1610Sstevel@tonic-gate char target;
1620Sstevel@tonic-gate const char *s1;
1630Sstevel@tonic-gate const char *s2;
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate *value = NULL;
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate if (e == NULL)
1680Sstevel@tonic-gate return (NULL);
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate target = name_only ? '\0' : '=';
1710Sstevel@tonic-gate
1720Sstevel@tonic-gate for (; (s2 = *e) != NULL; e++) {
1730Sstevel@tonic-gate s1 = string;
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate /* Fast comparison for first char. */
1760Sstevel@tonic-gate if (*s1 != *s2)
1770Sstevel@tonic-gate continue;
1780Sstevel@tonic-gate
1790Sstevel@tonic-gate /* Slow comparison for rest of string. */
1800Sstevel@tonic-gate while (*s1 == *s2 && *s2 != '=') {
1810Sstevel@tonic-gate s1++;
1820Sstevel@tonic-gate s2++;
1830Sstevel@tonic-gate }
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate if (*s1 == target && *s2 == '=') {
1860Sstevel@tonic-gate *value = (char *)s2 + 1;
1870Sstevel@tonic-gate return (e);
1880Sstevel@tonic-gate }
1890Sstevel@tonic-gate }
1900Sstevel@tonic-gate return (NULL);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate
1930Sstevel@tonic-gate /*
1940Sstevel@tonic-gate * Common code for putenv() and setenv(). We support the lockless getenv()
1950Sstevel@tonic-gate * by inserting new entries at the bottom of the list, and by growing the
1960Sstevel@tonic-gate * list using the trusted "power of two, drop it on the floor" method. We
1976879Sraf * use a lock (update_lock) to protect all updates to the _environ list, but
1980Sstevel@tonic-gate * we are obliged to release this lock whenever we call malloc() or free().
1990Sstevel@tonic-gate * A generation number (environ_gen) is bumped whenever names are added to,
2006879Sraf * or removed from, the _environ list so that we can detect collisions with
2010Sstevel@tonic-gate * other updaters.
2020Sstevel@tonic-gate *
2030Sstevel@tonic-gate * Return values
2040Sstevel@tonic-gate * 0 : success
2050Sstevel@tonic-gate * -1 : with errno set
2060Sstevel@tonic-gate * -2 : an entry already existed and overwrite was zero
2070Sstevel@tonic-gate */
2080Sstevel@tonic-gate static int
addtoenv(char * string,int overwrite)2090Sstevel@tonic-gate addtoenv(char *string, int overwrite)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate char *value;
2120Sstevel@tonic-gate const char **p;
2130Sstevel@tonic-gate chunk_t *new_chunk;
2140Sstevel@tonic-gate const char **new_environ;
2150Sstevel@tonic-gate const char **new_base;
2160Sstevel@tonic-gate int new_size;
2170Sstevel@tonic-gate int old_gen;
2180Sstevel@tonic-gate
2193864Sraf initenv();
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate lmutex_lock(&update_lock);
2220Sstevel@tonic-gate
2230Sstevel@tonic-gate for (;;) {
2240Sstevel@tonic-gate /*
2250Sstevel@tonic-gate * If the name already exists just overwrite the existing
2260Sstevel@tonic-gate * entry -- except when we were called by setenv() without
2270Sstevel@tonic-gate * the overwrite flag.
2280Sstevel@tonic-gate */
2290Sstevel@tonic-gate if ((p = findenv(my_environ, string, 0, &value)) != NULL) {
2300Sstevel@tonic-gate if (overwrite) {
2310Sstevel@tonic-gate /*
2320Sstevel@tonic-gate * Replace the value in situ. No name was
2330Sstevel@tonic-gate * added, so there is no need to bump the
2340Sstevel@tonic-gate * generation number.
2350Sstevel@tonic-gate */
2360Sstevel@tonic-gate *p = string;
2370Sstevel@tonic-gate lmutex_unlock(&update_lock);
2380Sstevel@tonic-gate return (0);
2390Sstevel@tonic-gate } else {
2400Sstevel@tonic-gate /* No change. */
2410Sstevel@tonic-gate lmutex_unlock(&update_lock);
2420Sstevel@tonic-gate return (-2);
2430Sstevel@tonic-gate }
2440Sstevel@tonic-gate }
2450Sstevel@tonic-gate
2460Sstevel@tonic-gate /* Try to insert the new entry at the bottom of the list. */
2470Sstevel@tonic-gate if (environ_base < my_environ) {
2480Sstevel@tonic-gate /*
2490Sstevel@tonic-gate * The new value must be visible before we decrement
2506879Sraf * the _environ list pointer.
2510Sstevel@tonic-gate */
2520Sstevel@tonic-gate my_environ[-1] = string;
2530Sstevel@tonic-gate membar_producer();
2540Sstevel@tonic-gate my_environ--;
2556879Sraf _environ = my_environ;
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate /*
2580Sstevel@tonic-gate * We've added a name, so bump the generation number.
2590Sstevel@tonic-gate */
2600Sstevel@tonic-gate environ_gen++;
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate lmutex_unlock(&update_lock);
2630Sstevel@tonic-gate return (0);
2640Sstevel@tonic-gate }
2650Sstevel@tonic-gate
2660Sstevel@tonic-gate /*
2676879Sraf * There is no room. Attempt to allocate a new _environ list
2680Sstevel@tonic-gate * which is at least double the size of the current one. See
2690Sstevel@tonic-gate * comment above concerning locking and malloc() etc.
2700Sstevel@tonic-gate */
2710Sstevel@tonic-gate new_size = environ_size * 2;
2720Sstevel@tonic-gate if (new_size < MIN_ENV_SIZE)
2730Sstevel@tonic-gate new_size = MIN_ENV_SIZE;
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate old_gen = environ_gen;
2760Sstevel@tonic-gate lmutex_unlock(&update_lock);
2770Sstevel@tonic-gate
2780Sstevel@tonic-gate new_chunk = malloc(sizeof (chunk_t) +
2790Sstevel@tonic-gate new_size * sizeof (char *));
2800Sstevel@tonic-gate if (new_chunk == NULL) {
2810Sstevel@tonic-gate errno = ENOMEM;
2820Sstevel@tonic-gate return (-1);
2830Sstevel@tonic-gate }
2840Sstevel@tonic-gate
2850Sstevel@tonic-gate lmutex_lock(&update_lock);
2860Sstevel@tonic-gate
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate * If no other thread added or removed names while the lock
2890Sstevel@tonic-gate * was dropped, it is time to break out of this loop.
2900Sstevel@tonic-gate */
2910Sstevel@tonic-gate if (environ_gen == old_gen)
2920Sstevel@tonic-gate break;
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate /*
2950Sstevel@tonic-gate * At least one name has been added or removed, so we need to
2960Sstevel@tonic-gate * try again. It is very likely that we will find sufficient
2970Sstevel@tonic-gate * space the next time around.
2980Sstevel@tonic-gate */
2990Sstevel@tonic-gate lmutex_unlock(&update_lock);
3000Sstevel@tonic-gate free(new_chunk);
3010Sstevel@tonic-gate lmutex_lock(&update_lock);
3020Sstevel@tonic-gate }
3030Sstevel@tonic-gate
3040Sstevel@tonic-gate /* Add the new chunk to chunk_list to hide potential future leak. */
3050Sstevel@tonic-gate new_chunk->next = chunk_list;
3060Sstevel@tonic-gate chunk_list = new_chunk;
3070Sstevel@tonic-gate
3086879Sraf /* Copy the old _environ list into the top of the new _environ list. */
3090Sstevel@tonic-gate new_base = (const char **)(new_chunk + 1);
3100Sstevel@tonic-gate new_environ = &new_base[(new_size - 1) - environ_size];
3110Sstevel@tonic-gate (void) memcpy(new_environ, my_environ, environ_size * sizeof (char *));
3120Sstevel@tonic-gate
3136879Sraf /* Insert the new entry at the bottom of the new _environ list. */
3140Sstevel@tonic-gate new_environ[-1] = string;
3150Sstevel@tonic-gate new_environ--;
3160Sstevel@tonic-gate
3176879Sraf /* Ensure that the new _environ list is visible to all. */
3180Sstevel@tonic-gate membar_producer();
3190Sstevel@tonic-gate
3206879Sraf /* Make the switch (dropping the old _environ list on the floor). */
3210Sstevel@tonic-gate environ_base = new_base;
3220Sstevel@tonic-gate my_environ = new_environ;
3236879Sraf _environ = my_environ;
3240Sstevel@tonic-gate environ_size = new_size;
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate /* We've added a name, so bump the generation number. */
3270Sstevel@tonic-gate environ_gen++;
3280Sstevel@tonic-gate
3290Sstevel@tonic-gate lmutex_unlock(&update_lock);
3300Sstevel@tonic-gate return (0);
3310Sstevel@tonic-gate }
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate /*
3340Sstevel@tonic-gate * All the work for putenv() is done in addtoenv().
3350Sstevel@tonic-gate */
3360Sstevel@tonic-gate int
putenv(char * string)3370Sstevel@tonic-gate putenv(char *string)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate return (addtoenv(string, 1));
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate /*
3430Sstevel@tonic-gate * setenv() is a little more complex than putenv() because we have to allocate
3446879Sraf * and construct an _environ entry on behalf of the caller. The bulk of the
3450Sstevel@tonic-gate * work is still done in addtoenv().
3460Sstevel@tonic-gate */
3470Sstevel@tonic-gate
3480Sstevel@tonic-gate int
setenv(const char * envname,const char * envval,int overwrite)3490Sstevel@tonic-gate setenv(const char *envname, const char *envval, int overwrite)
3500Sstevel@tonic-gate {
3510Sstevel@tonic-gate chunk_t *new_chunk;
3520Sstevel@tonic-gate char *new_string;
3530Sstevel@tonic-gate size_t name_len;
3540Sstevel@tonic-gate size_t val_len;
3550Sstevel@tonic-gate int res;
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate if (envname == NULL || *envname == 0 || strchr(envname, '=') != NULL) {
3580Sstevel@tonic-gate errno = EINVAL;
3590Sstevel@tonic-gate return (-1);
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate name_len = strlen(envname);
3630Sstevel@tonic-gate val_len = strlen(envval);
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate new_chunk = malloc(sizeof (chunk_t) + name_len + val_len + 2);
3660Sstevel@tonic-gate if (new_chunk == NULL) {
3670Sstevel@tonic-gate errno = ENOMEM;
3680Sstevel@tonic-gate return (-1);
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate new_string = (char *)(new_chunk + 1);
3710Sstevel@tonic-gate
3720Sstevel@tonic-gate (void) memcpy(new_string, envname, name_len);
3730Sstevel@tonic-gate new_string[name_len] = '=';
3740Sstevel@tonic-gate (void) memcpy(new_string + name_len + 1, envval, val_len);
3750Sstevel@tonic-gate new_string[name_len + 1 + val_len] = 0;
3760Sstevel@tonic-gate
3770Sstevel@tonic-gate if ((res = addtoenv(new_string, overwrite)) < 0) {
3780Sstevel@tonic-gate free(new_chunk);
3790Sstevel@tonic-gate if (res == -2) {
3800Sstevel@tonic-gate /* The name already existed, but not an error. */
3810Sstevel@tonic-gate return (0);
3820Sstevel@tonic-gate } else {
3830Sstevel@tonic-gate /* i.e. res == -1 which means only one thing. */
3840Sstevel@tonic-gate errno = ENOMEM;
3850Sstevel@tonic-gate return (-1);
3860Sstevel@tonic-gate }
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate /* Hide potential leak of new_string. */
3900Sstevel@tonic-gate lmutex_lock(&update_lock);
3910Sstevel@tonic-gate new_chunk->next = chunk_list;
3920Sstevel@tonic-gate chunk_list = new_chunk;
3930Sstevel@tonic-gate lmutex_unlock(&update_lock);
3940Sstevel@tonic-gate
3950Sstevel@tonic-gate return (0);
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate
3980Sstevel@tonic-gate /*
3996879Sraf * unsetenv() is tricky because we need to compress the _environ list in a way
4000Sstevel@tonic-gate * which supports a lockless getenv(). The approach here is to move the first
4010Sstevel@tonic-gate * entry from the enrivon list into the space occupied by the entry to be
4026879Sraf * deleted, and then to increment _environ. This has the added advantage of
4036879Sraf * making _any_ incremental linear search of the _environ list consistent (i.e.
4040Sstevel@tonic-gate * we will not break any naughty apps which read the list without our help).
4050Sstevel@tonic-gate */
4060Sstevel@tonic-gate int
unsetenv(const char * name)4070Sstevel@tonic-gate unsetenv(const char *name)
4080Sstevel@tonic-gate {
4090Sstevel@tonic-gate const char **p;
4100Sstevel@tonic-gate char *value;
4110Sstevel@tonic-gate
4120Sstevel@tonic-gate if (name == NULL || *name == 0 || strchr(name, '=') != NULL) {
4130Sstevel@tonic-gate errno = EINVAL;
4140Sstevel@tonic-gate return (-1);
4150Sstevel@tonic-gate }
4160Sstevel@tonic-gate
4173864Sraf initenv();
4180Sstevel@tonic-gate
4190Sstevel@tonic-gate lmutex_lock(&update_lock);
4200Sstevel@tonic-gate
4210Sstevel@tonic-gate /*
4220Sstevel@tonic-gate * Find the target, overwrite it with the first entry, increment the
4236879Sraf * _environ pointer.
4240Sstevel@tonic-gate */
4250Sstevel@tonic-gate if ((p = findenv(my_environ, name, 1, &value)) != NULL) {
4260Sstevel@tonic-gate /* Overwrite target with the first entry. */
4270Sstevel@tonic-gate *p = my_environ[0];
4280Sstevel@tonic-gate
4290Sstevel@tonic-gate /* Ensure that the moved entry is visible to all. */
4300Sstevel@tonic-gate membar_producer();
4310Sstevel@tonic-gate
4326879Sraf /* Shrink the _environ list. */
4330Sstevel@tonic-gate my_environ++;
4346879Sraf _environ = my_environ;
4350Sstevel@tonic-gate
4360Sstevel@tonic-gate /* Make sure addtoenv() knows that we've removed a name. */
4370Sstevel@tonic-gate environ_gen++;
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate
4400Sstevel@tonic-gate lmutex_unlock(&update_lock);
4410Sstevel@tonic-gate return (0);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate /*
445*13093SRoger.Faulkner@Oracle.COM * Dump entire environment.
446*13093SRoger.Faulkner@Oracle.COM */
447*13093SRoger.Faulkner@Oracle.COM int
clearenv(void)448*13093SRoger.Faulkner@Oracle.COM clearenv(void)
449*13093SRoger.Faulkner@Oracle.COM {
450*13093SRoger.Faulkner@Oracle.COM /*
451*13093SRoger.Faulkner@Oracle.COM * Just drop the entire environment list on the floor, as it
452*13093SRoger.Faulkner@Oracle.COM * would be non-trivial to try and free the used memory.
453*13093SRoger.Faulkner@Oracle.COM */
454*13093SRoger.Faulkner@Oracle.COM static const char *nullp = NULL;
455*13093SRoger.Faulkner@Oracle.COM
456*13093SRoger.Faulkner@Oracle.COM lmutex_lock(&update_lock);
457*13093SRoger.Faulkner@Oracle.COM _environ = &nullp;
458*13093SRoger.Faulkner@Oracle.COM my_environ = NULL;
459*13093SRoger.Faulkner@Oracle.COM environ_base = NULL;
460*13093SRoger.Faulkner@Oracle.COM environ_size = 0;
461*13093SRoger.Faulkner@Oracle.COM environ_gen++;
462*13093SRoger.Faulkner@Oracle.COM membar_producer();
463*13093SRoger.Faulkner@Oracle.COM lmutex_unlock(&update_lock);
464*13093SRoger.Faulkner@Oracle.COM
465*13093SRoger.Faulkner@Oracle.COM return (0);
466*13093SRoger.Faulkner@Oracle.COM }
467*13093SRoger.Faulkner@Oracle.COM
468*13093SRoger.Faulkner@Oracle.COM /*
4690Sstevel@tonic-gate * At last, a lockless implementation of getenv()!
4700Sstevel@tonic-gate */
4710Sstevel@tonic-gate char *
getenv(const char * name)4720Sstevel@tonic-gate getenv(const char *name)
4730Sstevel@tonic-gate {
4740Sstevel@tonic-gate char *value;
4750Sstevel@tonic-gate
4763864Sraf initenv();
4770Sstevel@tonic-gate
4786879Sraf if (findenv(_environ, name, 1, &value) != NULL)
4790Sstevel@tonic-gate return (value);
4800Sstevel@tonic-gate
4810Sstevel@tonic-gate return (NULL);
4820Sstevel@tonic-gate }
483