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