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 * See the License for the specific language governing permissions 2N/A * and limitations under the License. 2N/A * When distributing Covered Code, include this CDDL HEADER in each 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 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. 2N/A/* Copyright (c) 1988 AT&T */ 2N/A/* All Rights Reserved */ 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 * 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 * The setenv() API is inherently leaky but we are completely at the mercy 2N/A * of the application. 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 * 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 * 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 * 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 * 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 /* Call the NLSPATH janitor in. */ 2N/A /* Pacify leak detectors in normal operation. */ 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 /* Fast comparison for first char. */ 2N/A /* Slow comparison for rest of string. */ 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 * -1 : with errno set 2N/A * -2 : an entry already existed and overwrite was zero 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 * 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 /* Try to insert the new entry at the bottom of the list. */ 2N/A * The new value must be visible before we decrement 2N/A * the _environ list pointer. 2N/A * We've added a name, so bump the generation number. 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 * 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 * 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 /* Add the new chunk to chunk_list to hide potential future leak. */ 2N/A /* Copy the old _environ list into the top of the new _environ list. */ 2N/A /* Insert the new entry at the bottom of the new _environ list. */ 2N/A /* Ensure that the new _environ list is visible to all. */ 2N/A /* Make the switch (dropping the old _environ list on the floor). */ 2N/A /* We've added a name, so bump the generation number. */ 2N/A * All the work for putenv() is done in addtoenv(). 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 /* The name already existed, but not an error. */ 2N/A /* i.e. res == -1 which means only one thing. */ 2N/A /* Hide potential leak of new_string. */ 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 * Find the target, overwrite it with the first entry, increment the 2N/A /* Overwrite target with the first entry. */ 2N/A /* Ensure that the moved entry is visible to all. */ 2N/A /* Shrink the _environ list. */ 2N/A /* Make sure addtoenv() knows that we've removed a name. */ 2N/A * Dump entire environment. 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 * At last, a lockless implementation of getenv()!