xref: /illumos-gate/usr/src/lib/libc/port/gen/getenv.c (revision 60b81b86)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5cb620785Sraf  * Common Development and Distribution License (the "License").
6cb620785Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
2323a1cceaSRoger A. Faulkner  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24*60b81b86SRobert Mustacchi  * Copyright 2016 Joyent, Inc.
257c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307257d1b4Sraf #pragma	weak _putenv = putenv
327257d1b4Sraf #include "lint.h"
337c478bd9Sstevel@tonic-gate #include <mtlib.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <thread.h>
367c478bd9Sstevel@tonic-gate #include <synch.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <string.h>
407c478bd9Sstevel@tonic-gate #include <atomic.h>
427c478bd9Sstevel@tonic-gate #define	MIN_ENV_SIZE		128
4459f081edSraf extern const char		**_environ;
457c478bd9Sstevel@tonic-gate extern void			clean_env();
477c478bd9Sstevel@tonic-gate /*
4859f081edSraf  * For performance and consistency reasons we expand the _environ list using
497c478bd9Sstevel@tonic-gate  * the trusted "power of two, drop it on the floor" method. This allows for
507c478bd9Sstevel@tonic-gate  * a lockless, single pass implementation of getenv(), yet the memory leak
517c478bd9Sstevel@tonic-gate  * is bounded - in normal circumstances total wastage is never greater than
5259f081edSraf  * 3x the space needed to hold any _environ list.
537c478bd9Sstevel@tonic-gate  *
5459f081edSraf  * The only abnormal circumstance is if an application modifies the _environ
557c478bd9Sstevel@tonic-gate  * list pointer directly. Such an application does not conform to POSIX.1
567c478bd9Sstevel@tonic-gate  * 2001. However, we also care about standards which did not foresee this
5759f081edSraf  * issue. For this reason we keep a working copy of our notion of _environ in
5859f081edSraf  * my_environ. If, when we are called upon to modify _environ, we ever detect
5959f081edSraf  * a mismatch between _environ and my_environ we discard all our assumptions
6059f081edSraf  * concerning the location and size of the _environ list. As an additional
6159f081edSraf  * precaution we only ever update _environ once we have finished manipulating
627c478bd9Sstevel@tonic-gate  * our working copy.
637c478bd9Sstevel@tonic-gate  *
647c478bd9Sstevel@tonic-gate  * The setenv() API is inherently leaky but we are completely at the mercy
657c478bd9Sstevel@tonic-gate  * of the application.
667c478bd9Sstevel@tonic-gate  *
677c478bd9Sstevel@tonic-gate  * To pacify leak detectors we chain all allocations which are at risk of
687c478bd9Sstevel@tonic-gate  * being leaked in either of the above two scenarios. chunk_list must only
697c478bd9Sstevel@tonic-gate  * be updated under the protection of update_lock.
707c478bd9Sstevel@tonic-gate  *
7159f081edSraf  * Although we don't allocate the original _environ list it is likely that
727c478bd9Sstevel@tonic-gate  * we will leak this too. Accordingly, we create a reference in initenv().
737c478bd9Sstevel@tonic-gate  * However, we can't be held responsible for such leaks in abnormal (see
747c478bd9Sstevel@tonic-gate  * above) circumstances.
757c478bd9Sstevel@tonic-gate  */
777c478bd9Sstevel@tonic-gate typedef struct chunk {
787c478bd9Sstevel@tonic-gate 	struct chunk		*next;
797c478bd9Sstevel@tonic-gate } chunk_t;
817c478bd9Sstevel@tonic-gate static mutex_t			update_lock = DEFAULTMUTEX;
827c478bd9Sstevel@tonic-gate static const char		**orig_environ = NULL;
837c478bd9Sstevel@tonic-gate static const char		**my_environ = NULL;
847c478bd9Sstevel@tonic-gate static const char		**environ_base = NULL;
857c478bd9Sstevel@tonic-gate static int			environ_size = 0;
867c478bd9Sstevel@tonic-gate static int			environ_gen = 0;
877c478bd9Sstevel@tonic-gate static int			initenv_done = 0;
887c478bd9Sstevel@tonic-gate static chunk_t			*chunk_list = NULL;
907c478bd9Sstevel@tonic-gate /*
9159f081edSraf  * Compute the size an _environ list including the terminating NULL entry.
9259f081edSraf  * This is the only way we have to determine the size of an _environ list
937c478bd9Sstevel@tonic-gate  * we didn't allocate.
947c478bd9Sstevel@tonic-gate  */
957c478bd9Sstevel@tonic-gate static int
envsize(const char ** e)967c478bd9Sstevel@tonic-gate envsize(const char **e)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	int			size;
1007c478bd9Sstevel@tonic-gate 	if (e == NULL)
1017c478bd9Sstevel@tonic-gate 		return (0);
1037c478bd9Sstevel@tonic-gate 	for (size = 1; *e != NULL; e++)
1047c478bd9Sstevel@tonic-gate 		size++;
1067c478bd9Sstevel@tonic-gate 	return (size);
1077c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * Initialization for the following scenarios:
11159f081edSraf  * 1. The very first time we reference the _environ list we must call in the
11259f081edSraf  *    NLSPATH janitor, make a reference to the original _environ list to keep
1137c478bd9Sstevel@tonic-gate  *    leak detectors happy, initialize my_environ and environ_base, and then
1147c478bd9Sstevel@tonic-gate  *    compute environ_size.
11559f081edSraf  * 2. Whenever we detect that someone else has hijacked _environ (something
1167c478bd9Sstevel@tonic-gate  *    very abnormal) we need to reinitialize my_environ and environ_base,
1177c478bd9Sstevel@tonic-gate  *    and then recompute environ_size.
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  * The local globals my_environ, environ_base and environ_size may be used
1207c478bd9Sstevel@tonic-gate  * by others only if initenv_done is true and only under the protection of
1217c478bd9Sstevel@tonic-gate  * update_lock. However, our callers, who must NOT be holding update_lock,
12259f081edSraf  * may safely test initenv_done or my_environ against _environ just prior to
1237c478bd9Sstevel@tonic-gate  * calling us because we test these again whilst holding update_lock.
1247c478bd9Sstevel@tonic-gate  */
1257c478bd9Sstevel@tonic-gate static void
initenv()1267c478bd9Sstevel@tonic-gate initenv()
1277c478bd9Sstevel@tonic-gate {
12859f081edSraf 	if ((my_environ != _environ) || !initenv_done) {
129cb620785Sraf 		lmutex_lock(&update_lock);
13059f081edSraf 		if ((my_environ != _environ) || !initenv_done) {
131cb620785Sraf 			if (!initenv_done) {
132cb620785Sraf 				/* Call the NLSPATH janitor in. */
133cb620785Sraf 				clean_env();
135cb620785Sraf 				/* Pacify leak detectors in normal operation. */
13659f081edSraf 				orig_environ = _environ;
1377c478bd9Sstevel@tonic-gate #ifdef __lint
138cb620785Sraf 				my_environ = orig_environ;
1397c478bd9Sstevel@tonic-gate #endif
140cb620785Sraf 			}
14259f081edSraf 			my_environ = _environ;
143cb620785Sraf 			environ_base = my_environ;
144cb620785Sraf 			environ_size = envsize(environ_base);
145cb620785Sraf 			membar_producer();
1467c478bd9Sstevel@tonic-gate 			initenv_done = 1;
1477c478bd9Sstevel@tonic-gate 		}
148cb620785Sraf 		lmutex_unlock(&update_lock);
1497c478bd9Sstevel@tonic-gate 	}
150cb620785Sraf 	membar_consumer();
1517c478bd9Sstevel@tonic-gate }
1537c478bd9Sstevel@tonic-gate /*
15459f081edSraf  * Search an _environ list for a particular entry. If name_only is set, then
1557c478bd9Sstevel@tonic-gate  * string must be the entry name only, and we return the value of the first
1567c478bd9Sstevel@tonic-gate  * match. Otherwise, string must be of the form "name=value", and we return
1577c478bd9Sstevel@tonic-gate  * the address of the first matching entry.
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate static const char **
findenv(const char ** e,const char * string,int name_only,char ** value)1607c478bd9Sstevel@tonic-gate findenv(const char **e, const char *string, int name_only, char **value)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 	char			target;
1637c478bd9Sstevel@tonic-gate 	const char		*s1;
1647c478bd9Sstevel@tonic-gate 	const char		*s2;
1667c478bd9Sstevel@tonic-gate 	*value = NULL;
1687c478bd9Sstevel@tonic-gate 	if (e == NULL)
1697c478bd9Sstevel@tonic-gate 		return (NULL);
1717c478bd9Sstevel@tonic-gate 	target = name_only ? '\0' : '=';
1737c478bd9Sstevel@tonic-gate 	for (; (s2 = *e) != NULL; e++) {
1747c478bd9Sstevel@tonic-gate 		s1 =  string;
1767c478bd9Sstevel@tonic-gate 		/* Fast comparison for first char. */
1777c478bd9Sstevel@tonic-gate 		if (*s1 != *s2)
1787c478bd9Sstevel@tonic-gate 			continue;
1807c478bd9Sstevel@tonic-gate 		/* Slow comparison for rest of string. */
1817c478bd9Sstevel@tonic-gate 		while (*s1 == *s2 && *s2 != '=') {
1827c478bd9Sstevel@tonic-gate 			s1++;
1837c478bd9Sstevel@tonic-gate 			s2++;
1847c478bd9Sstevel@tonic-gate 		}
1867c478bd9Sstevel@tonic-gate 		if (*s1 == target && *s2 == '=') {
1877c478bd9Sstevel@tonic-gate 			*value = (char *)s2 + 1;
1887c478bd9Sstevel@tonic-gate 			return (e);
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 	return (NULL);
1927c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate  * Common code for putenv() and setenv(). We support the lockless getenv()
1967c478bd9Sstevel@tonic-gate  * by inserting new entries at the bottom of the list, and by growing the
1977c478bd9Sstevel@tonic-gate  * list using the trusted "power of two, drop it on the floor" method. We
19859f081edSraf  * use a lock (update_lock) to protect all updates to the _environ list, but
1997c478bd9Sstevel@tonic-gate  * we are obliged to release this lock whenever we call malloc() or free().
2007c478bd9Sstevel@tonic-gate  * A generation number (environ_gen) is bumped whenever names are added to,
20159f081edSraf  * or removed from, the _environ list so that we can detect collisions with
2027c478bd9Sstevel@tonic-gate  * other updaters.
2037c478bd9Sstevel@tonic-gate  *
2047c478bd9Sstevel@tonic-gate  * Return values
2057c478bd9Sstevel@tonic-gate  *   0 : success
2067c478bd9Sstevel@tonic-gate  *  -1 : with errno set
2077c478bd9Sstevel@tonic-gate  *  -2 : an entry already existed and overwrite was zero
2087c478bd9Sstevel@tonic-gate  */
2097c478bd9Sstevel@tonic-gate static int
addtoenv(char * string,int overwrite)2107c478bd9Sstevel@tonic-gate addtoenv(char *string, int overwrite)
2117c478bd9Sstevel@tonic-gate {
2127c478bd9Sstevel@tonic-gate 	char			*value;
2137c478bd9Sstevel@tonic-gate 	const char		**p;
2147c478bd9Sstevel@tonic-gate 	chunk_t			*new_chunk;
2157c478bd9Sstevel@tonic-gate 	const char		**new_environ;
2167c478bd9Sstevel@tonic-gate 	const char		**new_base;
2177c478bd9Sstevel@tonic-gate 	int			new_size;
2187c478bd9Sstevel@tonic-gate 	int			old_gen;
220cb620785Sraf 	initenv();
2227c478bd9Sstevel@tonic-gate 	lmutex_lock(&update_lock);
2247c478bd9Sstevel@tonic-gate 	for (;;) {
2257c478bd9Sstevel@tonic-gate 		/*
2267c478bd9Sstevel@tonic-gate 		 * If the name already exists just overwrite the existing
2277c478bd9Sstevel@tonic-gate 		 * entry -- except when we were called by setenv() without
2287c478bd9Sstevel@tonic-gate 		 * the overwrite flag.
2297c478bd9Sstevel@tonic-gate 		 */
2307c478bd9Sstevel@tonic-gate 		if ((p = findenv(my_environ, string, 0, &value)) != NULL) {
2317c478bd9Sstevel@tonic-gate 			if (overwrite) {
2327c478bd9Sstevel@tonic-gate 				/*
2337c478bd9Sstevel@tonic-gate 				 * Replace the value in situ. No name was
2347c478bd9Sstevel@tonic-gate 				 * added, so there is no need to bump the
2357c478bd9Sstevel@tonic-gate 				 * generation number.
2367c478bd9Sstevel@tonic-gate 				 */
2377c478bd9Sstevel@tonic-gate 				*p = string;
2387c478bd9Sstevel@tonic-gate 				lmutex_unlock(&update_lock);
2397c478bd9Sstevel@tonic-gate 				return (0);
2407c478bd9Sstevel@tonic-gate 			} else {
2417c478bd9Sstevel@tonic-gate 				/* No change. */
2427c478bd9Sstevel@tonic-gate 				lmutex_unlock(&update_lock);
2437c478bd9Sstevel@tonic-gate 				return (-2);
2447c478bd9Sstevel@tonic-gate 			}
2457c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 		/* Try to insert the new entry at the bottom of the list. */
2487c478bd9Sstevel@tonic-gate 		if (environ_base < my_environ) {
2497c478bd9Sstevel@tonic-gate 			/*
2507c478bd9Sstevel@tonic-gate 			 * The new value must be visible before we decrement
25159f081edSraf 			 * the _environ list pointer.
2527c478bd9Sstevel@tonic-gate 			 */
2537c478bd9Sstevel@tonic-gate 			my_environ[-1] = string;
2547c478bd9Sstevel@tonic-gate 			membar_producer();
2557c478bd9Sstevel@tonic-gate 			my_environ--;
25659f081edSraf 			_environ = my_environ;
2587c478bd9Sstevel@tonic-gate 			/*
2597c478bd9Sstevel@tonic-gate 			 * We've added a name, so bump the generation number.
2607c478bd9Sstevel@tonic-gate 			 */
2617c478bd9Sstevel@tonic-gate 			environ_gen++;
2637c478bd9Sstevel@tonic-gate 			lmutex_unlock(&update_lock);
2647c478bd9Sstevel@tonic-gate 			return (0);
2657c478bd9Sstevel@tonic-gate 		}
2677c478bd9Sstevel@tonic-gate 		/*
26859f081edSraf 		 * There is no room. Attempt to allocate a new _environ list
2697c478bd9Sstevel@tonic-gate 		 * which is at least double the size of the current one. See
2707c478bd9Sstevel@tonic-gate 		 * comment above concerning locking and malloc() etc.
2717c478bd9Sstevel@tonic-gate 		 */
2727c478bd9Sstevel@tonic-gate 		new_size = environ_size * 2;
2737c478bd9Sstevel@tonic-gate 		if (new_size < MIN_ENV_SIZE)
2747c478bd9Sstevel@tonic-gate 			new_size = MIN_ENV_SIZE;
2767c478bd9Sstevel@tonic-gate 		old_gen = environ_gen;
2777c478bd9Sstevel@tonic-gate 		lmutex_unlock(&update_lock);
2797c478bd9Sstevel@tonic-gate 		new_chunk = malloc(sizeof (chunk_t) +
2807c478bd9Sstevel@tonic-gate 		    new_size * sizeof (char *));
2817c478bd9Sstevel@tonic-gate 		if (new_chunk == NULL) {
2827c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
2837c478bd9Sstevel@tonic-gate 			return (-1);
2847c478bd9Sstevel@tonic-gate 		}
2867c478bd9Sstevel@tonic-gate 		lmutex_lock(&update_lock);
2887c478bd9Sstevel@tonic-gate 		/*
2897c478bd9Sstevel@tonic-gate 		 * If no other thread added or removed names while the lock
2907c478bd9Sstevel@tonic-gate 		 * was dropped, it is time to break out of this loop.
2917c478bd9Sstevel@tonic-gate 		 */
2927c478bd9Sstevel@tonic-gate 		if (environ_gen == old_gen)
2937c478bd9Sstevel@tonic-gate 			break;
2957c478bd9Sstevel@tonic-gate 		/*
2967c478bd9Sstevel@tonic-gate 		 * At least one name has been added or removed, so we need to
2977c478bd9Sstevel@tonic-gate 		 * try again. It is very likely that we will find sufficient
2987c478bd9Sstevel@tonic-gate 		 * space the next time around.
2997c478bd9Sstevel@tonic-gate 		 */
3007c478bd9Sstevel@tonic-gate 		lmutex_unlock(&update_lock);
3017c478bd9Sstevel@tonic-gate 		free(new_chunk);
3027c478bd9Sstevel@tonic-gate 		lmutex_lock(&update_lock);
3037c478bd9Sstevel@tonic-gate 	}
3057c478bd9Sstevel@tonic-gate 	/* Add the new chunk to chunk_list to hide potential future leak. */
3067c478bd9Sstevel@tonic-gate 	new_chunk->next = chunk_list;
3077c478bd9Sstevel@tonic-gate 	chunk_list = new_chunk;
30959f081edSraf 	/* Copy the old _environ list into the top of the new _environ list. */
3107c478bd9Sstevel@tonic-gate 	new_base = (const char **)(new_chunk + 1);
3117c478bd9Sstevel@tonic-gate 	new_environ = &new_base[(new_size - 1) - environ_size];
3127c478bd9Sstevel@tonic-gate 	(void) memcpy(new_environ, my_environ, environ_size * sizeof (char *));
31459f081edSraf 	/* Insert the new entry at the bottom of the new _environ list. */
3157c478bd9Sstevel@tonic-gate 	new_environ[-1] = string;
3167c478bd9Sstevel@tonic-gate 	new_environ--;
31859f081edSraf 	/* Ensure that the new _environ list is visible to all. */
3197c478bd9Sstevel@tonic-gate 	membar_producer();
32159f081edSraf 	/* Make the switch (dropping the old _environ list on the floor). */
3227c478bd9Sstevel@tonic-gate 	environ_base = new_base;
3237c478bd9Sstevel@tonic-gate 	my_environ = new_environ;
32459f081edSraf 	_environ = my_environ;
3257c478bd9Sstevel@tonic-gate 	environ_size = new_size;
3277c478bd9Sstevel@tonic-gate 	/* We've added a name, so bump the generation number. */
3287c478bd9Sstevel@tonic-gate 	environ_gen++;
3307c478bd9Sstevel@tonic-gate 	lmutex_unlock(&update_lock);
3317c478bd9Sstevel@tonic-gate 	return (0);
3327c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate /*
3357c478bd9Sstevel@tonic-gate  * All the work for putenv() is done in addtoenv().
3367c478bd9Sstevel@tonic-gate  */
3377c478bd9Sstevel@tonic-gate int
putenv(char * string)3387c478bd9Sstevel@tonic-gate putenv(char *string)
3397c478bd9Sstevel@tonic-gate {
340*60b81b86SRobert Mustacchi 	/*
341*60b81b86SRobert Mustacchi 	 * Historically a call to putenv() with no '=' in the string would work
342*60b81b86SRobert Mustacchi 	 * great until someone called getenv() on that particular environment
343*60b81b86SRobert Mustacchi 	 * variable again. As we've always treated this as valid, rather than
344*60b81b86SRobert Mustacchi 	 * teaching the rest of the environment code how to handle something
345*60b81b86SRobert Mustacchi 	 * without an '=' sign, it instead just calls unsetenv().
346*60b81b86SRobert Mustacchi 	 */
347*60b81b86SRobert Mustacchi 	if (strchr(string, '=') == NULL)
348*60b81b86SRobert Mustacchi 		return (unsetenv(string));
349*60b81b86SRobert Mustacchi 
3507c478bd9Sstevel@tonic-gate 	return (addtoenv(string, 1));
3517c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate /*
3547c478bd9Sstevel@tonic-gate  * setenv() is a little more complex than putenv() because we have to allocate
35559f081edSraf  * and construct an _environ entry on behalf of the caller. The bulk of the
3567c478bd9Sstevel@tonic-gate  * work is still done in addtoenv().
3577c478bd9Sstevel@tonic-gate  */
3597c478bd9Sstevel@tonic-gate int
setenv(const char * envname,const char * envval,int overwrite)3607c478bd9Sstevel@tonic-gate setenv(const char *envname, const char *envval, int overwrite)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	chunk_t			*new_chunk;
3637c478bd9Sstevel@tonic-gate 	char			*new_string;
3647c478bd9Sstevel@tonic-gate 	size_t			name_len;
3657c478bd9Sstevel@tonic-gate 	size_t			val_len;
3667c478bd9Sstevel@tonic-gate 	int			res;
3687c478bd9Sstevel@tonic-gate 	if (envname == NULL || *envname == 0 || strchr(envname, '=') != NULL) {
3697c478bd9Sstevel@tonic-gate 		errno = EINVAL;
3707c478bd9Sstevel@tonic-gate 		return (-1);
3717c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 	name_len = strlen(envname);
3747c478bd9Sstevel@tonic-gate 	val_len = strlen(envval);
3767c478bd9Sstevel@tonic-gate 	new_chunk = malloc(sizeof (chunk_t) + name_len + val_len + 2);
3777c478bd9Sstevel@tonic-gate 	if (new_chunk == NULL) {
3787c478bd9Sstevel@tonic-gate 		errno = ENOMEM;
3797c478bd9Sstevel@tonic-gate 		return (-1);
3807c478bd9Sstevel@tonic-gate 	}
3817c478bd9Sstevel@tonic-gate 	new_string = (char *)(new_chunk + 1);
3837c478bd9Sstevel@tonic-gate 	(void) memcpy(new_string, envname, name_len);
3847c478bd9Sstevel@tonic-gate 	new_string[name_len] = '=';
3857c478bd9Sstevel@tonic-gate 	(void) memcpy(new_string + name_len + 1, envval, val_len);
3867c478bd9Sstevel@tonic-gate 	new_string[name_len + 1 + val_len] = 0;
3887c478bd9Sstevel@tonic-gate 	if ((res = addtoenv(new_string, overwrite)) < 0) {
3897c478bd9Sstevel@tonic-gate 		free(new_chunk);
3907c478bd9Sstevel@tonic-gate 		if (res == -2) {
3917c478bd9Sstevel@tonic-gate 			/* The name already existed, but not an error. */
3927c478bd9Sstevel@tonic-gate 			return (0);
3937c478bd9Sstevel@tonic-gate 		} else {
3947c478bd9Sstevel@tonic-gate 			/* i.e. res == -1 which means only one thing. */
3957c478bd9Sstevel@tonic-gate 			errno = ENOMEM;
3967c478bd9Sstevel@tonic-gate 			return (-1);
3977c478bd9Sstevel@tonic-gate 		}
3987c478bd9Sstevel@tonic-gate 	}
4007c478bd9Sstevel@tonic-gate 	/* Hide potential leak of new_string. */
4017c478bd9Sstevel@tonic-gate 	lmutex_lock(&update_lock);
4027c478bd9Sstevel@tonic-gate 	new_chunk->next = chunk_list;
4037c478bd9Sstevel@tonic-gate 	chunk_list = new_chunk;
4047c478bd9Sstevel@tonic-gate 	lmutex_unlock(&update_lock);
4067c478bd9Sstevel@tonic-gate 	return (0);
4077c478bd9Sstevel@tonic-gate }
4097c478bd9Sstevel@tonic-gate /*
41059f081edSraf  * unsetenv() is tricky because we need to compress the _environ list in a way
4117c478bd9Sstevel@tonic-gate  * which supports a lockless getenv(). The approach here is to move the first
4127c478bd9Sstevel@tonic-gate  * entry from the enrivon list into the space occupied by the entry to be
41359f081edSraf  * deleted, and then to increment _environ. This has the added advantage of
41459f081edSraf  * making _any_ incremental linear search of the _environ list consistent (i.e.
4157c478bd9Sstevel@tonic-gate  * we will not break any naughty apps which read the list without our help).
4167c478bd9Sstevel@tonic-gate  */
4177c478bd9Sstevel@tonic-gate int
unsetenv(const char * name)4187c478bd9Sstevel@tonic-gate unsetenv(const char *name)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	const char		**p;
4217c478bd9Sstevel@tonic-gate 	char			*value;
4237c478bd9Sstevel@tonic-gate 	if (name == NULL || *name == 0 || strchr(name, '=') != NULL) {
4247c478bd9Sstevel@tonic-gate 		errno = EINVAL;
4257c478bd9Sstevel@tonic-gate 		return (-1);
4267c478bd9Sstevel@tonic-gate 	}
428cb620785Sraf 	initenv();
4307c478bd9Sstevel@tonic-gate 	lmutex_lock(&update_lock);
4327c478bd9Sstevel@tonic-gate 	/*
4337c478bd9Sstevel@tonic-gate 	 * Find the target, overwrite it with the first entry, increment the
43459f081edSraf 	 * _environ pointer.
4357c478bd9Sstevel@tonic-gate 	 */
4367c478bd9Sstevel@tonic-gate 	if ((p = findenv(my_environ, name, 1, &value)) != NULL) {
4377c478bd9Sstevel@tonic-gate 		/* Overwrite target with the first entry. */
4387c478bd9Sstevel@tonic-gate 		*p = my_environ[0];
4407c478bd9Sstevel@tonic-gate 		/* Ensure that the moved entry is visible to all.  */
4417c478bd9Sstevel@tonic-gate 		membar_producer();
44359f081edSraf 		/* Shrink the _environ list. */
4447c478bd9Sstevel@tonic-gate 		my_environ++;
44559f081edSraf 		_environ = my_environ;
4477c478bd9Sstevel@tonic-gate 		/* Make sure addtoenv() knows that we've removed a name. */
4487c478bd9Sstevel@tonic-gate 		environ_gen++;
4497c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 	lmutex_unlock(&update_lock);
4527c478bd9Sstevel@tonic-gate 	return (0);
4537c478bd9Sstevel@tonic-gate }
45523a1cceaSRoger A. Faulkner /*
45623a1cceaSRoger A. Faulkner  * Dump entire environment.
45723a1cceaSRoger A. Faulkner  */
45823a1cceaSRoger A. Faulkner int
clearenv(void)45923a1cceaSRoger A. Faulkner clearenv(void)
46023a1cceaSRoger A. Faulkner {
46123a1cceaSRoger A. Faulkner 	/*
46223a1cceaSRoger A. Faulkner 	 * Just drop the entire environment list on the floor, as it
46323a1cceaSRoger A. Faulkner 	 * would be non-trivial to try and free the used memory.
46423a1cceaSRoger A. Faulkner 	 */
46523a1cceaSRoger A. Faulkner 	static const char *nullp = NULL;
46623a1cceaSRoger A. Faulkner 
46723a1cceaSRoger A. Faulkner 	lmutex_lock(&update_lock);
46823a1cceaSRoger A. Faulkner 	_environ = &nullp;
46923a1cceaSRoger A. Faulkner 	my_environ = NULL;
47023a1cceaSRoger A. Faulkner 	environ_base = NULL;
47123a1cceaSRoger A. Faulkner 	environ_size = 0;
47223a1cceaSRoger A. Faulkner 	environ_gen++;
47323a1cceaSRoger A. Faulkner 	membar_producer();
47423a1cceaSRoger A. Faulkner 	lmutex_unlock(&update_lock);
47523a1cceaSRoger A. Faulkner 
47623a1cceaSRoger A. Faulkner 	return (0);
47723a1cceaSRoger A. Faulkner }
47823a1cceaSRoger A. Faulkner 
4797c478bd9Sstevel@tonic-gate /*
4807c478bd9Sstevel@tonic-gate  * At last, a lockless implementation of getenv()!
4817c478bd9Sstevel@tonic-gate  */
4827c478bd9Sstevel@tonic-gate char *
getenv(const char * name)4837c478bd9Sstevel@tonic-gate getenv(const char *name)
4847c478bd9Sstevel@tonic-gate {
4857c478bd9Sstevel@tonic-gate 	char			*value;
487cb620785Sraf 	initenv();
48959f081edSraf 	if (findenv(_environ, name, 1, &value) != NULL)
4907c478bd9Sstevel@tonic-gate 		return (value);
4927c478bd9Sstevel@tonic-gate 	return (NULL);
4937c478bd9Sstevel@tonic-gate }