Home | History | Annotate | Line # | Download | only in stdlib
_env.c revision 1.4
      1  1.4   tron /*	$NetBSD: _env.c,v 1.4 2010/11/16 17:23:10 tron Exp $ */
      2  1.1   tron 
      3  1.1   tron /*-
      4  1.1   tron  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  1.1   tron  * All rights reserved.
      6  1.1   tron  *
      7  1.1   tron  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1   tron  * by Matthias Scheler.
      9  1.1   tron  *
     10  1.1   tron  * Redistribution and use in source and binary forms, with or without
     11  1.1   tron  * modification, are permitted provided that the following conditions
     12  1.1   tron  * are met:
     13  1.1   tron  * 1. Redistributions of source code must retain the above copyright
     14  1.1   tron  *    notice, this list of conditions and the following disclaimer.
     15  1.1   tron  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1   tron  *    notice, this list of conditions and the following disclaimer in the
     17  1.1   tron  *    documentation and/or other materials provided with the distribution.
     18  1.1   tron  *
     19  1.1   tron  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1   tron  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1   tron  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1   tron  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1   tron  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1   tron  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1   tron  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1   tron  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1   tron  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1   tron  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1   tron  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1   tron  */
     31  1.1   tron 
     32  1.4   tron #include <sys/cdefs.h>
     33  1.4   tron #if defined(LIBC_SCCS) && !defined(lint)
     34  1.4   tron __RCSID("$NetBSD: _env.c,v 1.4 2010/11/16 17:23:10 tron Exp $");
     35  1.4   tron #endif /* LIBC_SCCS and not lint */
     36  1.4   tron 
     37  1.1   tron #include <sys/rbtree.h>
     38  1.1   tron 
     39  1.1   tron #include <assert.h>
     40  1.1   tron #include <errno.h>
     41  1.2   tron #include <limits.h>
     42  1.1   tron #include <stdlib.h>
     43  1.1   tron #include <stddef.h>
     44  1.1   tron #include <string.h>
     45  1.1   tron 
     46  1.1   tron #include "env.h"
     47  1.1   tron #include "reentrant.h"
     48  1.1   tron #include "local.h"
     49  1.1   tron 
     50  1.1   tron /*
     51  1.1   tron  * Red-Black tree node for tracking memory used by environment variables.
     52  1.1   tron  * The tree is sorted by the address of the nodes themselves.
     53  1.1   tron  */
     54  1.1   tron typedef struct {
     55  1.1   tron 	rb_node_t	rb_node;
     56  1.1   tron 	size_t		length;
     57  1.4   tron 	uint8_t		marker;
     58  1.1   tron 	char		data[];
     59  1.1   tron } env_node_t;
     60  1.1   tron 
     61  1.1   tron /* Compare functions for above tree. */
     62  1.1   tron static signed int env_tree_compare_nodes(void *, const void *, const void *);
     63  1.1   tron static signed int env_tree_compare_key(void *, const void *, const void *);
     64  1.1   tron 
     65  1.1   tron /* Operations for above tree. */
     66  1.1   tron static const rb_tree_ops_t env_tree_ops = {
     67  1.1   tron 	.rbto_compare_nodes = env_tree_compare_nodes,
     68  1.1   tron 	.rbto_compare_key = env_tree_compare_key,
     69  1.1   tron 	.rbto_node_offset = offsetof(env_node_t, rb_node),
     70  1.1   tron 	.rbto_context = NULL
     71  1.1   tron };
     72  1.1   tron 
     73  1.1   tron /* The single instance of above tree. */
     74  1.1   tron static rb_tree_t	env_tree;
     75  1.1   tron 
     76  1.1   tron /* The allocated environment. */
     77  1.1   tron static char	**allocated_environ;
     78  1.1   tron static size_t	allocated_environ_size;
     79  1.1   tron 
     80  1.1   tron #define	ENV_ARRAY_SIZE_MIN	16
     81  1.1   tron 
     82  1.3  enami /* The lock protecting access to the environment. */
     83  1.1   tron #ifdef _REENTRANT
     84  1.1   tron static rwlock_t env_lock = RWLOCK_INITIALIZER;
     85  1.1   tron #endif
     86  1.1   tron 
     87  1.2   tron /* Compatibility function. */
     88  1.2   tron char *__findenv(const char *name, int *offsetp);
     89  1.2   tron 
     90  1.2   tron __warn_references(__findenv,
     91  1.2   tron     "warning: __findenv is an internal obsolete function.")
     92  1.2   tron 
     93  1.1   tron /* Our initialization function. */
     94  1.1   tron void __libc_env_init(void);
     95  1.1   tron 
     96  1.1   tron /*ARGSUSED*/
     97  1.1   tron static signed int
     98  1.1   tron env_tree_compare_nodes(void *ctx, const void *node_a, const void *node_b)
     99  1.1   tron {
    100  1.1   tron 	uintptr_t addr_a, addr_b;
    101  1.1   tron 
    102  1.1   tron 	addr_a = (uintptr_t)node_a;
    103  1.1   tron 	addr_b = (uintptr_t)node_b;
    104  1.1   tron 
    105  1.1   tron 	if (addr_a < addr_b)
    106  1.1   tron 		return -1;
    107  1.1   tron 
    108  1.1   tron 	if (addr_a > addr_b)
    109  1.1   tron 		return 1;
    110  1.1   tron 
    111  1.1   tron 	return 0;
    112  1.1   tron }
    113  1.1   tron 
    114  1.1   tron static signed int
    115  1.1   tron env_tree_compare_key(void *ctx, const void *node, const void *key)
    116  1.1   tron {
    117  1.1   tron 	return env_tree_compare_nodes(ctx, node,
    118  1.1   tron 	    (const uint8_t *)key - offsetof(env_node_t, data));
    119  1.1   tron }
    120  1.1   tron 
    121  1.1   tron /*
    122  1.1   tron  * Determine the of the name in an environment string. Return 0 if the
    123  1.1   tron  * name is not valid.
    124  1.1   tron  */
    125  1.1   tron size_t
    126  1.1   tron __envvarnamelen(const char *str, bool withequal)
    127  1.1   tron {
    128  1.1   tron 	size_t l_name;
    129  1.1   tron 
    130  1.1   tron 	if (str == NULL)
    131  1.1   tron 		return 0;
    132  1.1   tron 
    133  1.1   tron 	l_name = strcspn(str, "=");
    134  1.1   tron 	if (l_name == 0)
    135  1.1   tron 		return 0;
    136  1.1   tron 
    137  1.1   tron 	if (withequal) {
    138  1.1   tron 		if (str[l_name] != '=')
    139  1.1   tron 			return 0;
    140  1.1   tron 	} else {
    141  1.1   tron 		if (str[l_name] == '=')
    142  1.1   tron 			return 0;
    143  1.1   tron 	}
    144  1.1   tron 
    145  1.1   tron 	return l_name;
    146  1.1   tron }
    147  1.1   tron 
    148  1.1   tron /*
    149  1.1   tron  * Free memory occupied by environment variable if possible. This function
    150  1.1   tron  * must be called with the environment write locked.
    151  1.1   tron  */
    152  1.1   tron void
    153  1.1   tron __freeenvvar(char *envvar)
    154  1.1   tron {
    155  1.1   tron 	env_node_t *node;
    156  1.1   tron 
    157  1.1   tron 	_DIAGASSERT(envvar != NULL);
    158  1.1   tron 	node = rb_tree_find_node(&env_tree, envvar);
    159  1.1   tron 	if (node != NULL) {
    160  1.1   tron 		rb_tree_remove_node(&env_tree, node);
    161  1.1   tron 		free(node);
    162  1.1   tron 	}
    163  1.1   tron }
    164  1.1   tron 
    165  1.1   tron /*
    166  1.1   tron  * Allocate memory for an environment variable. This function must be called
    167  1.1   tron  * with the environment write locked.
    168  1.1   tron  */
    169  1.1   tron char *
    170  1.1   tron __allocenvvar(size_t length)
    171  1.1   tron {
    172  1.1   tron 	env_node_t *node;
    173  1.1   tron 
    174  1.1   tron 	node = malloc(sizeof(*node) + length);
    175  1.1   tron 	if (node != NULL) {
    176  1.1   tron 		node->length = length;
    177  1.4   tron 		node->marker = 0;
    178  1.1   tron 		rb_tree_insert_node(&env_tree, node);
    179  1.1   tron 		return node->data;
    180  1.1   tron 	} else {
    181  1.1   tron 		return NULL;
    182  1.1   tron 	}
    183  1.1   tron }
    184  1.1   tron 
    185  1.1   tron /*
    186  1.3  enami  * Check whether an environment variable is writable. This function must be
    187  1.1   tron  * called with the environment write locked as the caller will probably
    188  1.3  enami  * overwrite the environment variable afterwards.
    189  1.1   tron  */
    190  1.1   tron bool
    191  1.1   tron __canoverwriteenvvar(char *envvar, size_t length)
    192  1.1   tron {
    193  1.1   tron 	env_node_t *node;
    194  1.1   tron 
    195  1.1   tron 	_DIAGASSERT(envvar != NULL);
    196  1.1   tron 
    197  1.1   tron 	node = rb_tree_find_node(&env_tree, envvar);
    198  1.1   tron 	return (node != NULL && length <= node->length);
    199  1.1   tron }
    200  1.1   tron 
    201  1.4   tron /* Free all allocated environment variables that are no longer used. */
    202  1.4   tron static void
    203  1.4   tron __scrubenv(void)
    204  1.4   tron {
    205  1.4   tron 	static uint8_t marker = 0;
    206  1.4   tron 	size_t num_entries;
    207  1.4   tron 	env_node_t *node, *next;
    208  1.4   tron 
    209  1.4   tron 	while (++marker == 0);
    210  1.4   tron 
    211  1.4   tron 	/* Mark all nodes which are currently used. */
    212  1.4   tron 	for (num_entries = 0; environ[num_entries] != NULL; num_entries++) {
    213  1.4   tron 		node = rb_tree_find_node(&env_tree, environ[num_entries]);
    214  1.4   tron 		if (node != NULL)
    215  1.4   tron 			node->marker = marker;
    216  1.4   tron 	}
    217  1.4   tron 
    218  1.4   tron 	/* Free all nodes which are currently not used. */
    219  1.4   tron 	for (node = RB_TREE_MIN(&env_tree); node != NULL; node = next) {
    220  1.4   tron 		next = rb_tree_iterate(&env_tree, node, RB_DIR_RIGHT);
    221  1.4   tron 
    222  1.4   tron 		if (node->marker != marker) {
    223  1.4   tron 			rb_tree_remove_node(&env_tree, node);
    224  1.4   tron 			free(node);
    225  1.4   tron 		}
    226  1.4   tron 	}
    227  1.4   tron 
    228  1.4   tron 	/* Deal with the environment array itself. */
    229  1.4   tron 	if (environ == allocated_environ) {
    230  1.4   tron 		/* Clear out spurious entries in the environment. */
    231  1.4   tron 		(void)memset(&environ[num_entries + 1], 0,
    232  1.4   tron 		    (allocated_environ_size - num_entries - 1) *
    233  1.4   tron 		    sizeof(*environ));
    234  1.4   tron 	} else {
    235  1.4   tron 		/*
    236  1.4   tron 		 * The environment array was not allocated by "libc".
    237  1.4   tron 		 * Free our array if we allocated one.
    238  1.4   tron 		 */
    239  1.4   tron 		free(allocated_environ);
    240  1.4   tron 		allocated_environ = NULL;
    241  1.4   tron 		allocated_environ_size = 0;
    242  1.4   tron 	}
    243  1.4   tron }
    244  1.4   tron 
    245  1.1   tron /*
    246  1.1   tron  * Get a (new) slot in the environment. This function must be called with
    247  1.1   tron  * the environment write locked.
    248  1.1   tron  */
    249  1.1   tron ssize_t
    250  1.1   tron __getenvslot(const char *name, size_t l_name, bool allocate)
    251  1.1   tron {
    252  1.1   tron 	size_t new_size, num_entries, required_size;
    253  1.1   tron 	char **new_environ;
    254  1.1   tron 
    255  1.4   tron 	/* Does the environ need scrubbing? */
    256  1.4   tron 	if (environ != allocated_environ && allocated_environ != NULL)
    257  1.4   tron 		__scrubenv();
    258  1.4   tron 
    259  1.1   tron 	/* Search for an existing environment variable of the given name. */
    260  1.1   tron 	num_entries = 0;
    261  1.1   tron 	while (environ[num_entries] != NULL) {
    262  1.1   tron 		if (strncmp(environ[num_entries], name, l_name) == 0 &&
    263  1.1   tron 		    environ[num_entries][l_name] == '=') {
    264  1.1   tron 			/* We found a match. */
    265  1.1   tron 			return num_entries;
    266  1.1   tron 		}
    267  1.1   tron 		num_entries ++;
    268  1.1   tron 	}
    269  1.1   tron 
    270  1.1   tron 	/* No match found, return if we don't want to allocate a new slot. */
    271  1.1   tron 	if (!allocate)
    272  1.1   tron 		return -1;
    273  1.1   tron 
    274  1.1   tron 	/* Create a new slot in the environment. */
    275  1.1   tron 	required_size = num_entries + 1;
    276  1.1   tron 	if (environ == allocated_environ &&
    277  1.1   tron 	    required_size < allocated_environ_size) {
    278  1.4   tron 		/* Does the environment need scrubbing? */
    279  1.4   tron 		if (required_size < allocated_environ_size &&
    280  1.4   tron 		    allocated_environ[required_size] != NULL) {
    281  1.4   tron 			__scrubenv();
    282  1.1   tron 		}
    283  1.1   tron 
    284  1.1   tron 		/* Return a free slot. */
    285  1.1   tron 		return num_entries;
    286  1.1   tron 	}
    287  1.1   tron 
    288  1.1   tron 	/* Determine size of a new environment array. */
    289  1.1   tron 	new_size = ENV_ARRAY_SIZE_MIN;
    290  1.1   tron 	while (new_size <= required_size)
    291  1.1   tron 		new_size <<= 1;
    292  1.1   tron 
    293  1.1   tron 	/* Allocate a new environment array. */
    294  1.1   tron 	if (environ == allocated_environ) {
    295  1.1   tron 		new_environ = realloc(environ,
    296  1.1   tron 		    new_size * sizeof(*new_environ));
    297  1.1   tron 		if (new_environ == NULL)
    298  1.1   tron 			return -1;
    299  1.1   tron 	} else {
    300  1.1   tron 		free(allocated_environ);
    301  1.1   tron 		allocated_environ = NULL;
    302  1.1   tron 		allocated_environ_size = 0;
    303  1.1   tron 
    304  1.1   tron 		new_environ = malloc(new_size * sizeof(*new_environ));
    305  1.1   tron 		if (new_environ == NULL)
    306  1.1   tron 			return -1;
    307  1.1   tron 		(void)memcpy(new_environ, environ,
    308  1.1   tron 		    num_entries * sizeof(*new_environ));
    309  1.1   tron 	}
    310  1.1   tron 
    311  1.1   tron 	/* Clear remaining entries. */
    312  1.1   tron 	(void)memset(&new_environ[num_entries], 0,
    313  1.1   tron 	    (new_size - num_entries) * sizeof(*new_environ));
    314  1.1   tron 
    315  1.3  enami 	/* Use the new environment array. */
    316  1.1   tron 	environ = allocated_environ = new_environ;
    317  1.1   tron 	allocated_environ_size = new_size;
    318  1.1   tron 
    319  1.1   tron 	/* Return a free slot. */
    320  1.1   tron 	return num_entries;
    321  1.1   tron }
    322  1.1   tron 
    323  1.1   tron /* Find a string in the environment. */
    324  1.1   tron char *
    325  1.2   tron __findenvvar(const char *name, size_t l_name)
    326  1.1   tron {
    327  1.1   tron 	ssize_t offset;
    328  1.1   tron 
    329  1.1   tron 	offset = __getenvslot(name, l_name, false);
    330  1.1   tron 	return (offset != -1) ? environ[offset] + l_name + 1 : NULL;
    331  1.1   tron }
    332  1.1   tron 
    333  1.2   tron /* Compatibility interface, do *not* call this function. */
    334  1.2   tron char *
    335  1.2   tron __findenv(const char *name, int *offsetp)
    336  1.2   tron {
    337  1.2   tron 	size_t l_name;
    338  1.2   tron 	ssize_t offset;
    339  1.2   tron 
    340  1.2   tron 	l_name = __envvarnamelen(name, false);
    341  1.2   tron 	if (l_name == 0)
    342  1.2   tron 		return NULL;
    343  1.2   tron 
    344  1.2   tron 	offset = __getenvslot(name, l_name, false);
    345  1.2   tron 	if (offset < 0 || offset > INT_MAX)
    346  1.2   tron 		return NULL;
    347  1.2   tron 
    348  1.2   tron 	*offsetp = (int)offset;
    349  1.2   tron 	return environ[offset] + l_name + 1;
    350  1.2   tron }
    351  1.2   tron 
    352  1.1   tron #ifdef _REENTRANT
    353  1.1   tron 
    354  1.1   tron /* Lock the environment for read. */
    355  1.1   tron bool
    356  1.1   tron __readlockenv(void)
    357  1.1   tron {
    358  1.1   tron 	int error;
    359  1.1   tron 
    360  1.1   tron 	error = rwlock_rdlock(&env_lock);
    361  1.1   tron 	if (error == 0)
    362  1.1   tron 		return true;
    363  1.1   tron 
    364  1.1   tron 	errno = error;
    365  1.1   tron 	return false;
    366  1.1   tron }
    367  1.1   tron 
    368  1.1   tron /* Lock the environment for write. */
    369  1.1   tron bool
    370  1.1   tron __writelockenv(void)
    371  1.1   tron {
    372  1.1   tron 	int error;
    373  1.1   tron 
    374  1.1   tron 	error = rwlock_wrlock(&env_lock);
    375  1.1   tron 	if (error == 0)
    376  1.1   tron 		return true;
    377  1.1   tron 
    378  1.1   tron 	errno = error;
    379  1.1   tron 	return false;
    380  1.1   tron }
    381  1.1   tron 
    382  1.1   tron /* Unlock the environment for write. */
    383  1.1   tron bool
    384  1.1   tron __unlockenv(void)
    385  1.1   tron {
    386  1.1   tron 	int error;
    387  1.1   tron 
    388  1.1   tron 	error = rwlock_unlock(&env_lock);
    389  1.1   tron 	if (error == 0)
    390  1.1   tron 		return true;
    391  1.1   tron 
    392  1.1   tron 	errno = error;
    393  1.1   tron 	return false;
    394  1.1   tron }
    395  1.1   tron 
    396  1.1   tron #endif
    397  1.1   tron 
    398  1.1   tron /* Initialize environment memory RB tree. */
    399  1.1   tron void
    400  1.1   tron __libc_env_init(void)
    401  1.1   tron {
    402  1.1   tron 	rb_tree_init(&env_tree, &env_tree_ops);
    403  1.1   tron }
    404