Home | History | Annotate | Line # | Download | only in stdlib
_env.c revision 1.2
      1  1.2  tron /*	$NetBSD: _env.c,v 1.2 2010/11/14 22:04:36 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.1  tron #include <sys/rbtree.h>
     33  1.1  tron 
     34  1.1  tron #include <assert.h>
     35  1.1  tron #include <errno.h>
     36  1.2  tron #include <limits.h>
     37  1.1  tron #include <stdlib.h>
     38  1.1  tron #include <stddef.h>
     39  1.1  tron #include <string.h>
     40  1.1  tron 
     41  1.1  tron #include "env.h"
     42  1.1  tron #include "reentrant.h"
     43  1.1  tron #include "local.h"
     44  1.1  tron 
     45  1.1  tron /*
     46  1.1  tron  * Red-Black tree node for tracking memory used by environment variables.
     47  1.1  tron  * The tree is sorted by the address of the nodes themselves.
     48  1.1  tron  */
     49  1.1  tron typedef struct {
     50  1.1  tron 	rb_node_t	rb_node;
     51  1.1  tron 	size_t		length;
     52  1.1  tron 	char		data[];
     53  1.1  tron } env_node_t;
     54  1.1  tron 
     55  1.1  tron /* Compare functions for above tree. */
     56  1.1  tron static signed int env_tree_compare_nodes(void *, const void *, const void *);
     57  1.1  tron static signed int env_tree_compare_key(void *, const void *, const void *);
     58  1.1  tron 
     59  1.1  tron /* Operations for above tree. */
     60  1.1  tron static const rb_tree_ops_t env_tree_ops = {
     61  1.1  tron 	.rbto_compare_nodes = env_tree_compare_nodes,
     62  1.1  tron 	.rbto_compare_key = env_tree_compare_key,
     63  1.1  tron 	.rbto_node_offset = offsetof(env_node_t, rb_node),
     64  1.1  tron 	.rbto_context = NULL
     65  1.1  tron };
     66  1.1  tron 
     67  1.1  tron /* The single instance of above tree. */
     68  1.1  tron static rb_tree_t	env_tree;
     69  1.1  tron 
     70  1.1  tron /* The allocated environment. */
     71  1.1  tron static char	**allocated_environ;
     72  1.1  tron static size_t	allocated_environ_size;
     73  1.1  tron 
     74  1.1  tron #define	ENV_ARRAY_SIZE_MIN	16
     75  1.1  tron 
     76  1.1  tron /* The lock protecting accces to the environment. */
     77  1.1  tron #ifdef _REENTRANT
     78  1.1  tron static rwlock_t env_lock = RWLOCK_INITIALIZER;
     79  1.1  tron #endif
     80  1.1  tron 
     81  1.2  tron /* Compatibility function. */
     82  1.2  tron char *__findenv(const char *name, int *offsetp);
     83  1.2  tron 
     84  1.2  tron __warn_references(__findenv,
     85  1.2  tron     "warning: __findenv is an internal obsolete function.")
     86  1.2  tron 
     87  1.1  tron /* Our initialization function. */
     88  1.1  tron void __libc_env_init(void);
     89  1.1  tron 
     90  1.1  tron /*ARGSUSED*/
     91  1.1  tron static signed int
     92  1.1  tron env_tree_compare_nodes(void *ctx, const void *node_a, const void *node_b)
     93  1.1  tron {
     94  1.1  tron 	uintptr_t addr_a, addr_b;
     95  1.1  tron 
     96  1.1  tron 	addr_a = (uintptr_t)node_a;
     97  1.1  tron 	addr_b = (uintptr_t)node_b;
     98  1.1  tron 
     99  1.1  tron 	if (addr_a < addr_b)
    100  1.1  tron 		return -1;
    101  1.1  tron 
    102  1.1  tron 	if (addr_a > addr_b)
    103  1.1  tron 		return 1;
    104  1.1  tron 
    105  1.1  tron 	return 0;
    106  1.1  tron }
    107  1.1  tron 
    108  1.1  tron static signed int
    109  1.1  tron env_tree_compare_key(void *ctx, const void *node, const void *key)
    110  1.1  tron {
    111  1.1  tron 	return env_tree_compare_nodes(ctx, node,
    112  1.1  tron 	    (const uint8_t *)key - offsetof(env_node_t, data));
    113  1.1  tron }
    114  1.1  tron 
    115  1.1  tron /*
    116  1.1  tron  * Determine the of the name in an environment string. Return 0 if the
    117  1.1  tron  * name is not valid.
    118  1.1  tron  */
    119  1.1  tron size_t
    120  1.1  tron __envvarnamelen(const char *str, bool withequal)
    121  1.1  tron {
    122  1.1  tron 	size_t l_name;
    123  1.1  tron 
    124  1.1  tron 	if (str == NULL)
    125  1.1  tron 		return 0;
    126  1.1  tron 
    127  1.1  tron 	l_name = strcspn(str, "=");
    128  1.1  tron 	if (l_name == 0)
    129  1.1  tron 		return 0;
    130  1.1  tron 
    131  1.1  tron 	if (withequal) {
    132  1.1  tron 		if (str[l_name] != '=')
    133  1.1  tron 			return 0;
    134  1.1  tron 	} else {
    135  1.1  tron 		if (str[l_name] == '=')
    136  1.1  tron 			return 0;
    137  1.1  tron 	}
    138  1.1  tron 
    139  1.1  tron 	return l_name;
    140  1.1  tron }
    141  1.1  tron 
    142  1.1  tron /*
    143  1.1  tron  * Free memory occupied by environment variable if possible. This function
    144  1.1  tron  * must be called with the environment write locked.
    145  1.1  tron  */
    146  1.1  tron void
    147  1.1  tron __freeenvvar(char *envvar)
    148  1.1  tron {
    149  1.1  tron 	env_node_t *node;
    150  1.1  tron 
    151  1.1  tron 	_DIAGASSERT(envvar != NULL);
    152  1.1  tron 	node = rb_tree_find_node(&env_tree, envvar);
    153  1.1  tron 	if (node != NULL) {
    154  1.1  tron 		rb_tree_remove_node(&env_tree, node);
    155  1.1  tron 		free(node);
    156  1.1  tron 	}
    157  1.1  tron }
    158  1.1  tron 
    159  1.1  tron /*
    160  1.1  tron  * Allocate memory for an environment variable. This function must be called
    161  1.1  tron  * with the environment write locked.
    162  1.1  tron  */
    163  1.1  tron char *
    164  1.1  tron __allocenvvar(size_t length)
    165  1.1  tron {
    166  1.1  tron 	env_node_t *node;
    167  1.1  tron 
    168  1.1  tron 	node = malloc(sizeof(*node) + length);
    169  1.1  tron 	if (node != NULL) {
    170  1.1  tron 		node->length = length;
    171  1.1  tron 		rb_tree_insert_node(&env_tree, node);
    172  1.1  tron 		return node->data;
    173  1.1  tron 	} else {
    174  1.1  tron 		return NULL;
    175  1.1  tron 	}
    176  1.1  tron }
    177  1.1  tron 
    178  1.1  tron /*
    179  1.1  tron  * Check whether an enviroment variable is writable. This function must be
    180  1.1  tron  * called with the environment write locked as the caller will probably
    181  1.1  tron  * overwrite the enviroment variable afterwards.
    182  1.1  tron  */
    183  1.1  tron bool
    184  1.1  tron __canoverwriteenvvar(char *envvar, size_t length)
    185  1.1  tron {
    186  1.1  tron 	env_node_t *node;
    187  1.1  tron 
    188  1.1  tron 	_DIAGASSERT(envvar != NULL);
    189  1.1  tron 
    190  1.1  tron 	node = rb_tree_find_node(&env_tree, envvar);
    191  1.1  tron 	return (node != NULL && length <= node->length);
    192  1.1  tron }
    193  1.1  tron 
    194  1.1  tron /*
    195  1.1  tron  * Get a (new) slot in the environment. This function must be called with
    196  1.1  tron  * the environment write locked.
    197  1.1  tron  */
    198  1.1  tron ssize_t
    199  1.1  tron __getenvslot(const char *name, size_t l_name, bool allocate)
    200  1.1  tron {
    201  1.1  tron 	size_t new_size, num_entries, required_size;
    202  1.1  tron 	char **new_environ;
    203  1.1  tron 
    204  1.1  tron 	/* Search for an existing environment variable of the given name. */
    205  1.1  tron 	num_entries = 0;
    206  1.1  tron 	while (environ[num_entries] != NULL) {
    207  1.1  tron 		if (strncmp(environ[num_entries], name, l_name) == 0 &&
    208  1.1  tron 		    environ[num_entries][l_name] == '=') {
    209  1.1  tron 			/* We found a match. */
    210  1.1  tron 			return num_entries;
    211  1.1  tron 		}
    212  1.1  tron 		num_entries ++;
    213  1.1  tron 	}
    214  1.1  tron 
    215  1.1  tron 	/* No match found, return if we don't want to allocate a new slot. */
    216  1.1  tron 	if (!allocate)
    217  1.1  tron 		return -1;
    218  1.1  tron 
    219  1.1  tron 	/* Create a new slot in the environment. */
    220  1.1  tron 	required_size = num_entries + 1;
    221  1.1  tron 	if (environ == allocated_environ &&
    222  1.1  tron 	    required_size < allocated_environ_size) {
    223  1.1  tron 		size_t offset;
    224  1.1  tron 
    225  1.1  tron 		/*
    226  1.1  tron 		 * Scrub the environment if somebody erased its contents by
    227  1.1  tron 		 * e.g. setting environ[0] to NULL.
    228  1.1  tron 		 */
    229  1.1  tron 		offset = required_size;
    230  1.1  tron 		while (offset < allocated_environ_size &&
    231  1.1  tron 		    environ[offset] != NULL) {
    232  1.1  tron 			__freeenvvar(environ[offset]);
    233  1.1  tron 			environ[offset] = NULL;
    234  1.1  tron 			offset++;
    235  1.1  tron 		}
    236  1.1  tron 
    237  1.1  tron 		/* Return a free slot. */
    238  1.1  tron 		return num_entries;
    239  1.1  tron 	}
    240  1.1  tron 
    241  1.1  tron 	/* Determine size of a new environment array. */
    242  1.1  tron 	new_size = ENV_ARRAY_SIZE_MIN;
    243  1.1  tron 	while (new_size <= required_size)
    244  1.1  tron 		new_size <<= 1;
    245  1.1  tron 
    246  1.1  tron 	/* Allocate a new environment array. */
    247  1.1  tron 	if (environ == allocated_environ) {
    248  1.1  tron 		new_environ = realloc(environ,
    249  1.1  tron 		    new_size * sizeof(*new_environ));
    250  1.1  tron 		if (new_environ == NULL)
    251  1.1  tron 			return -1;
    252  1.1  tron 	} else {
    253  1.1  tron 		free(allocated_environ);
    254  1.1  tron 		allocated_environ = NULL;
    255  1.1  tron 		allocated_environ_size = 0;
    256  1.1  tron 
    257  1.1  tron 		new_environ = malloc(new_size * sizeof(*new_environ));
    258  1.1  tron 		if (new_environ == NULL)
    259  1.1  tron 			return -1;
    260  1.1  tron 		(void)memcpy(new_environ, environ,
    261  1.1  tron 		    num_entries * sizeof(*new_environ));
    262  1.1  tron 	}
    263  1.1  tron 
    264  1.1  tron 	/* Clear remaining entries. */
    265  1.1  tron 	(void)memset(&new_environ[num_entries], 0,
    266  1.1  tron 	    (new_size - num_entries) * sizeof(*new_environ));
    267  1.1  tron 
    268  1.1  tron 	/* Use the new environent array. */
    269  1.1  tron 	environ = allocated_environ = new_environ;
    270  1.1  tron 	allocated_environ_size = new_size;
    271  1.1  tron 
    272  1.1  tron 	/* Return a free slot. */
    273  1.1  tron 	return num_entries;
    274  1.1  tron }
    275  1.1  tron 
    276  1.1  tron /* Find a string in the environment. */
    277  1.1  tron char *
    278  1.2  tron __findenvvar(const char *name, size_t l_name)
    279  1.1  tron {
    280  1.1  tron 	ssize_t offset;
    281  1.1  tron 
    282  1.1  tron 	offset = __getenvslot(name, l_name, false);
    283  1.1  tron 	return (offset != -1) ? environ[offset] + l_name + 1 : NULL;
    284  1.1  tron }
    285  1.1  tron 
    286  1.2  tron /* Compatibility interface, do *not* call this function. */
    287  1.2  tron char *
    288  1.2  tron __findenv(const char *name, int *offsetp)
    289  1.2  tron {
    290  1.2  tron 	size_t l_name;
    291  1.2  tron 	ssize_t offset;
    292  1.2  tron 
    293  1.2  tron 	l_name = __envvarnamelen(name, false);
    294  1.2  tron 	if (l_name == 0)
    295  1.2  tron 		return NULL;
    296  1.2  tron 
    297  1.2  tron 	offset = __getenvslot(name, l_name, false);
    298  1.2  tron 	if (offset < 0 || offset > INT_MAX)
    299  1.2  tron 		return NULL;
    300  1.2  tron 
    301  1.2  tron 	*offsetp = (int)offset;
    302  1.2  tron 	return environ[offset] + l_name + 1;
    303  1.2  tron }
    304  1.2  tron 
    305  1.1  tron #ifdef _REENTRANT
    306  1.1  tron 
    307  1.1  tron /* Lock the environment for read. */
    308  1.1  tron bool
    309  1.1  tron __readlockenv(void)
    310  1.1  tron {
    311  1.1  tron 	int error;
    312  1.1  tron 
    313  1.1  tron 	error = rwlock_rdlock(&env_lock);
    314  1.1  tron 	if (error == 0)
    315  1.1  tron 		return true;
    316  1.1  tron 
    317  1.1  tron 	errno = error;
    318  1.1  tron 	return false;
    319  1.1  tron }
    320  1.1  tron 
    321  1.1  tron /* Lock the environment for write. */
    322  1.1  tron bool
    323  1.1  tron __writelockenv(void)
    324  1.1  tron {
    325  1.1  tron 	int error;
    326  1.1  tron 
    327  1.1  tron 	error = rwlock_wrlock(&env_lock);
    328  1.1  tron 	if (error == 0)
    329  1.1  tron 		return true;
    330  1.1  tron 
    331  1.1  tron 	errno = error;
    332  1.1  tron 	return false;
    333  1.1  tron }
    334  1.1  tron 
    335  1.1  tron /* Unlock the environment for write. */
    336  1.1  tron bool
    337  1.1  tron __unlockenv(void)
    338  1.1  tron {
    339  1.1  tron 	int error;
    340  1.1  tron 
    341  1.1  tron 	error = rwlock_unlock(&env_lock);
    342  1.1  tron 	if (error == 0)
    343  1.1  tron 		return true;
    344  1.1  tron 
    345  1.1  tron 	errno = error;
    346  1.1  tron 	return false;
    347  1.1  tron }
    348  1.1  tron 
    349  1.1  tron #endif
    350  1.1  tron 
    351  1.1  tron /* Initialize environment memory RB tree. */
    352  1.1  tron void
    353  1.1  tron __libc_env_init(void)
    354  1.1  tron {
    355  1.1  tron 	rb_tree_init(&env_tree, &env_tree_ops);
    356  1.1  tron }
    357