Home | History | Annotate | Line # | Download | only in stdlib
_env.c revision 1.6.8.1
      1  1.6.8.1       tls /*	$NetBSD: _env.c,v 1.6.8.1 2014/08/20 00:02:16 tls 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.6.8.1       tls __RCSID("$NetBSD: _env.c,v 1.6.8.1 2014/08/20 00:02:16 tls Exp $");
     35      1.4      tron #endif /* LIBC_SCCS and not lint */
     36      1.4      tron 
     37      1.5      tron #include "namespace.h"
     38      1.5      tron 
     39      1.1      tron #include <sys/rbtree.h>
     40      1.1      tron 
     41      1.1      tron #include <assert.h>
     42      1.1      tron #include <errno.h>
     43      1.2      tron #include <limits.h>
     44      1.1      tron #include <stdlib.h>
     45      1.1      tron #include <stddef.h>
     46      1.1      tron #include <string.h>
     47      1.1      tron 
     48      1.1      tron #include "env.h"
     49      1.1      tron #include "reentrant.h"
     50      1.1      tron #include "local.h"
     51      1.1      tron 
     52      1.1      tron /*
     53      1.1      tron  * Red-Black tree node for tracking memory used by environment variables.
     54      1.1      tron  * The tree is sorted by the address of the nodes themselves.
     55      1.1      tron  */
     56      1.1      tron typedef struct {
     57      1.1      tron 	rb_node_t	rb_node;
     58      1.1      tron 	size_t		length;
     59      1.4      tron 	uint8_t		marker;
     60      1.1      tron 	char		data[];
     61      1.1      tron } env_node_t;
     62      1.1      tron 
     63      1.1      tron /* Compare functions for above tree. */
     64      1.1      tron static signed int env_tree_compare_nodes(void *, const void *, const void *);
     65      1.1      tron static signed int env_tree_compare_key(void *, const void *, const void *);
     66      1.1      tron 
     67      1.1      tron /* Operations for above tree. */
     68      1.1      tron static const rb_tree_ops_t env_tree_ops = {
     69      1.1      tron 	.rbto_compare_nodes = env_tree_compare_nodes,
     70      1.1      tron 	.rbto_compare_key = env_tree_compare_key,
     71      1.1      tron 	.rbto_node_offset = offsetof(env_node_t, rb_node),
     72      1.1      tron 	.rbto_context = NULL
     73      1.1      tron };
     74      1.1      tron 
     75      1.1      tron /* The single instance of above tree. */
     76      1.1      tron static rb_tree_t	env_tree;
     77      1.1      tron 
     78      1.1      tron /* The allocated environment. */
     79      1.1      tron static char	**allocated_environ;
     80      1.1      tron static size_t	allocated_environ_size;
     81      1.1      tron 
     82      1.1      tron #define	ENV_ARRAY_SIZE_MIN	16
     83      1.1      tron 
     84      1.3     enami /* The lock protecting access to the environment. */
     85      1.1      tron #ifdef _REENTRANT
     86      1.1      tron static rwlock_t env_lock = RWLOCK_INITIALIZER;
     87      1.1      tron #endif
     88      1.1      tron 
     89      1.2      tron /* Compatibility function. */
     90      1.2      tron char *__findenv(const char *name, int *offsetp);
     91      1.2      tron 
     92      1.2      tron __warn_references(__findenv,
     93      1.2      tron     "warning: __findenv is an internal obsolete function.")
     94      1.2      tron 
     95      1.1      tron /* Our initialization function. */
     96      1.1      tron void __libc_env_init(void);
     97      1.1      tron 
     98      1.6  christos char **environ;
     99      1.6  christos 
    100      1.1      tron /*ARGSUSED*/
    101      1.1      tron static signed int
    102      1.1      tron env_tree_compare_nodes(void *ctx, const void *node_a, const void *node_b)
    103      1.1      tron {
    104      1.1      tron 	uintptr_t addr_a, addr_b;
    105      1.1      tron 
    106      1.1      tron 	addr_a = (uintptr_t)node_a;
    107      1.1      tron 	addr_b = (uintptr_t)node_b;
    108      1.1      tron 
    109      1.1      tron 	if (addr_a < addr_b)
    110      1.1      tron 		return -1;
    111      1.1      tron 
    112      1.1      tron 	if (addr_a > addr_b)
    113      1.1      tron 		return 1;
    114      1.1      tron 
    115      1.1      tron 	return 0;
    116      1.1      tron }
    117      1.1      tron 
    118      1.1      tron static signed int
    119      1.1      tron env_tree_compare_key(void *ctx, const void *node, const void *key)
    120      1.1      tron {
    121      1.1      tron 	return env_tree_compare_nodes(ctx, node,
    122      1.1      tron 	    (const uint8_t *)key - offsetof(env_node_t, data));
    123      1.1      tron }
    124      1.1      tron 
    125      1.1      tron /*
    126      1.1      tron  * Determine the of the name in an environment string. Return 0 if the
    127      1.1      tron  * name is not valid.
    128      1.1      tron  */
    129      1.1      tron size_t
    130      1.1      tron __envvarnamelen(const char *str, bool withequal)
    131      1.1      tron {
    132      1.1      tron 	size_t l_name;
    133      1.1      tron 
    134      1.1      tron 	if (str == NULL)
    135      1.1      tron 		return 0;
    136      1.1      tron 
    137      1.1      tron 	l_name = strcspn(str, "=");
    138      1.1      tron 	if (l_name == 0)
    139      1.1      tron 		return 0;
    140      1.1      tron 
    141      1.1      tron 	if (withequal) {
    142      1.1      tron 		if (str[l_name] != '=')
    143      1.1      tron 			return 0;
    144      1.1      tron 	} else {
    145      1.1      tron 		if (str[l_name] == '=')
    146      1.1      tron 			return 0;
    147      1.1      tron 	}
    148      1.1      tron 
    149      1.1      tron 	return l_name;
    150      1.1      tron }
    151      1.1      tron 
    152      1.1      tron /*
    153      1.1      tron  * Free memory occupied by environment variable if possible. This function
    154      1.1      tron  * must be called with the environment write locked.
    155      1.1      tron  */
    156      1.1      tron void
    157      1.1      tron __freeenvvar(char *envvar)
    158      1.1      tron {
    159      1.1      tron 	env_node_t *node;
    160      1.1      tron 
    161      1.1      tron 	_DIAGASSERT(envvar != NULL);
    162      1.1      tron 	node = rb_tree_find_node(&env_tree, envvar);
    163      1.1      tron 	if (node != NULL) {
    164      1.1      tron 		rb_tree_remove_node(&env_tree, node);
    165      1.1      tron 		free(node);
    166      1.1      tron 	}
    167      1.1      tron }
    168      1.1      tron 
    169      1.1      tron /*
    170      1.1      tron  * Allocate memory for an environment variable. This function must be called
    171      1.1      tron  * with the environment write locked.
    172      1.1      tron  */
    173      1.1      tron char *
    174      1.1      tron __allocenvvar(size_t length)
    175      1.1      tron {
    176      1.1      tron 	env_node_t *node;
    177      1.1      tron 
    178      1.1      tron 	node = malloc(sizeof(*node) + length);
    179      1.1      tron 	if (node != NULL) {
    180      1.1      tron 		node->length = length;
    181      1.4      tron 		node->marker = 0;
    182      1.1      tron 		rb_tree_insert_node(&env_tree, node);
    183      1.1      tron 		return node->data;
    184      1.1      tron 	} else {
    185      1.1      tron 		return NULL;
    186      1.1      tron 	}
    187      1.1      tron }
    188      1.1      tron 
    189      1.1      tron /*
    190      1.3     enami  * Check whether an environment variable is writable. This function must be
    191      1.1      tron  * called with the environment write locked as the caller will probably
    192      1.3     enami  * overwrite the environment variable afterwards.
    193      1.1      tron  */
    194      1.1      tron bool
    195      1.1      tron __canoverwriteenvvar(char *envvar, size_t length)
    196      1.1      tron {
    197      1.1      tron 	env_node_t *node;
    198      1.1      tron 
    199      1.1      tron 	_DIAGASSERT(envvar != NULL);
    200      1.1      tron 
    201      1.1      tron 	node = rb_tree_find_node(&env_tree, envvar);
    202      1.1      tron 	return (node != NULL && length <= node->length);
    203      1.1      tron }
    204      1.1      tron 
    205      1.4      tron /* Free all allocated environment variables that are no longer used. */
    206      1.4      tron static void
    207      1.4      tron __scrubenv(void)
    208      1.4      tron {
    209      1.4      tron 	static uint8_t marker = 0;
    210      1.4      tron 	size_t num_entries;
    211      1.4      tron 	env_node_t *node, *next;
    212      1.4      tron 
    213      1.4      tron 	while (++marker == 0);
    214      1.4      tron 
    215      1.4      tron 	/* Mark all nodes which are currently used. */
    216      1.4      tron 	for (num_entries = 0; environ[num_entries] != NULL; num_entries++) {
    217      1.4      tron 		node = rb_tree_find_node(&env_tree, environ[num_entries]);
    218      1.4      tron 		if (node != NULL)
    219      1.4      tron 			node->marker = marker;
    220      1.4      tron 	}
    221      1.4      tron 
    222      1.4      tron 	/* Free all nodes which are currently not used. */
    223      1.4      tron 	for (node = RB_TREE_MIN(&env_tree); node != NULL; node = next) {
    224      1.4      tron 		next = rb_tree_iterate(&env_tree, node, RB_DIR_RIGHT);
    225      1.4      tron 
    226      1.4      tron 		if (node->marker != marker) {
    227      1.4      tron 			rb_tree_remove_node(&env_tree, node);
    228      1.4      tron 			free(node);
    229      1.4      tron 		}
    230      1.4      tron 	}
    231      1.4      tron 
    232      1.4      tron 	/* Deal with the environment array itself. */
    233      1.4      tron 	if (environ == allocated_environ) {
    234      1.4      tron 		/* Clear out spurious entries in the environment. */
    235      1.4      tron 		(void)memset(&environ[num_entries + 1], 0,
    236      1.4      tron 		    (allocated_environ_size - num_entries - 1) *
    237      1.4      tron 		    sizeof(*environ));
    238      1.4      tron 	} else {
    239      1.4      tron 		/*
    240      1.4      tron 		 * The environment array was not allocated by "libc".
    241      1.4      tron 		 * Free our array if we allocated one.
    242      1.4      tron 		 */
    243      1.4      tron 		free(allocated_environ);
    244      1.4      tron 		allocated_environ = NULL;
    245      1.4      tron 		allocated_environ_size = 0;
    246      1.4      tron 	}
    247      1.4      tron }
    248      1.4      tron 
    249      1.1      tron /*
    250      1.1      tron  * Get a (new) slot in the environment. This function must be called with
    251      1.1      tron  * the environment write locked.
    252      1.1      tron  */
    253      1.1      tron ssize_t
    254      1.1      tron __getenvslot(const char *name, size_t l_name, bool allocate)
    255      1.1      tron {
    256      1.1      tron 	size_t new_size, num_entries, required_size;
    257      1.1      tron 	char **new_environ;
    258      1.1      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.6.8.1       tls 	/* Does the environ need scrubbing? */
    275  1.6.8.1       tls 	if (environ != allocated_environ && allocated_environ != NULL)
    276  1.6.8.1       tls 		__scrubenv();
    277  1.6.8.1       tls 
    278      1.1      tron 	/* Create a new slot in the environment. */
    279      1.1      tron 	required_size = num_entries + 1;
    280      1.1      tron 	if (environ == allocated_environ &&
    281      1.1      tron 	    required_size < allocated_environ_size) {
    282      1.4      tron 		/* Does the environment need scrubbing? */
    283      1.4      tron 		if (required_size < allocated_environ_size &&
    284      1.4      tron 		    allocated_environ[required_size] != NULL) {
    285      1.4      tron 			__scrubenv();
    286      1.1      tron 		}
    287      1.1      tron 
    288      1.1      tron 		/* Return a free slot. */
    289      1.1      tron 		return num_entries;
    290      1.1      tron 	}
    291      1.1      tron 
    292      1.1      tron 	/* Determine size of a new environment array. */
    293      1.1      tron 	new_size = ENV_ARRAY_SIZE_MIN;
    294      1.1      tron 	while (new_size <= required_size)
    295      1.1      tron 		new_size <<= 1;
    296      1.1      tron 
    297      1.1      tron 	/* Allocate a new environment array. */
    298      1.1      tron 	if (environ == allocated_environ) {
    299      1.1      tron 		new_environ = realloc(environ,
    300      1.1      tron 		    new_size * sizeof(*new_environ));
    301      1.1      tron 		if (new_environ == NULL)
    302      1.1      tron 			return -1;
    303      1.1      tron 	} else {
    304      1.1      tron 		free(allocated_environ);
    305      1.1      tron 		allocated_environ = NULL;
    306      1.1      tron 		allocated_environ_size = 0;
    307      1.1      tron 
    308      1.1      tron 		new_environ = malloc(new_size * sizeof(*new_environ));
    309      1.1      tron 		if (new_environ == NULL)
    310      1.1      tron 			return -1;
    311      1.1      tron 		(void)memcpy(new_environ, environ,
    312      1.1      tron 		    num_entries * sizeof(*new_environ));
    313      1.1      tron 	}
    314      1.1      tron 
    315      1.1      tron 	/* Clear remaining entries. */
    316      1.1      tron 	(void)memset(&new_environ[num_entries], 0,
    317      1.1      tron 	    (new_size - num_entries) * sizeof(*new_environ));
    318      1.1      tron 
    319      1.3     enami 	/* Use the new environment array. */
    320      1.1      tron 	environ = allocated_environ = new_environ;
    321      1.1      tron 	allocated_environ_size = new_size;
    322      1.1      tron 
    323      1.1      tron 	/* Return a free slot. */
    324      1.1      tron 	return num_entries;
    325      1.1      tron }
    326      1.1      tron 
    327      1.1      tron /* Find a string in the environment. */
    328      1.1      tron char *
    329      1.2      tron __findenvvar(const char *name, size_t l_name)
    330      1.1      tron {
    331      1.1      tron 	ssize_t offset;
    332      1.1      tron 
    333      1.1      tron 	offset = __getenvslot(name, l_name, false);
    334      1.1      tron 	return (offset != -1) ? environ[offset] + l_name + 1 : NULL;
    335      1.1      tron }
    336      1.1      tron 
    337      1.2      tron /* Compatibility interface, do *not* call this function. */
    338      1.2      tron char *
    339      1.2      tron __findenv(const char *name, int *offsetp)
    340      1.2      tron {
    341      1.2      tron 	size_t l_name;
    342      1.2      tron 	ssize_t offset;
    343      1.2      tron 
    344      1.2      tron 	l_name = __envvarnamelen(name, false);
    345      1.2      tron 	if (l_name == 0)
    346      1.2      tron 		return NULL;
    347      1.2      tron 
    348      1.2      tron 	offset = __getenvslot(name, l_name, false);
    349      1.2      tron 	if (offset < 0 || offset > INT_MAX)
    350      1.2      tron 		return NULL;
    351      1.2      tron 
    352      1.2      tron 	*offsetp = (int)offset;
    353      1.2      tron 	return environ[offset] + l_name + 1;
    354      1.2      tron }
    355      1.2      tron 
    356      1.1      tron #ifdef _REENTRANT
    357      1.1      tron 
    358      1.1      tron /* Lock the environment for read. */
    359      1.1      tron bool
    360      1.1      tron __readlockenv(void)
    361      1.1      tron {
    362      1.1      tron 	int error;
    363      1.1      tron 
    364      1.1      tron 	error = rwlock_rdlock(&env_lock);
    365      1.1      tron 	if (error == 0)
    366      1.1      tron 		return true;
    367      1.1      tron 
    368      1.1      tron 	errno = error;
    369      1.1      tron 	return false;
    370      1.1      tron }
    371      1.1      tron 
    372      1.1      tron /* Lock the environment for write. */
    373      1.1      tron bool
    374      1.1      tron __writelockenv(void)
    375      1.1      tron {
    376      1.1      tron 	int error;
    377      1.1      tron 
    378      1.1      tron 	error = rwlock_wrlock(&env_lock);
    379      1.1      tron 	if (error == 0)
    380      1.1      tron 		return true;
    381      1.1      tron 
    382      1.1      tron 	errno = error;
    383      1.1      tron 	return false;
    384      1.1      tron }
    385      1.1      tron 
    386      1.1      tron /* Unlock the environment for write. */
    387      1.1      tron bool
    388      1.1      tron __unlockenv(void)
    389      1.1      tron {
    390      1.1      tron 	int error;
    391      1.1      tron 
    392      1.1      tron 	error = rwlock_unlock(&env_lock);
    393      1.1      tron 	if (error == 0)
    394      1.1      tron 		return true;
    395      1.1      tron 
    396      1.1      tron 	errno = error;
    397      1.1      tron 	return false;
    398      1.1      tron }
    399      1.1      tron 
    400      1.1      tron #endif
    401      1.1      tron 
    402      1.1      tron /* Initialize environment memory RB tree. */
    403  1.6.8.1       tls void __section(".text.startup")
    404      1.1      tron __libc_env_init(void)
    405      1.1      tron {
    406      1.1      tron 	rb_tree_init(&env_tree, &env_tree_ops);
    407      1.1      tron }
    408