Home | History | Annotate | Line # | Download | only in stdlib
_env.c revision 1.14
      1  1.14  riastrad /*	$NetBSD: _env.c,v 1.14 2023/07/18 11:44:32 riastradh 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.14  riastrad __RCSID("$NetBSD: _env.c,v 1.14 2023/07/18 11:44:32 riastradh 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.11  christos #include "csu-common.h"
     48   1.1      tron 
     49   1.1      tron #include "env.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.14  riastrad static rb_tree_t	env_tree =
     77  1.14  riastrad     RB_TREE_INITIALIZER(env_tree, &env_tree_ops);
     78   1.1      tron 
     79   1.1      tron /* The allocated environment. */
     80   1.1      tron static char	**allocated_environ;
     81   1.1      tron static size_t	allocated_environ_size;
     82   1.1      tron 
     83   1.1      tron #define	ENV_ARRAY_SIZE_MIN	16
     84   1.1      tron 
     85   1.3     enami /* The lock protecting access to the environment. */
     86   1.1      tron #ifdef _REENTRANT
     87   1.1      tron static rwlock_t env_lock = RWLOCK_INITIALIZER;
     88   1.1      tron #endif
     89   1.1      tron 
     90   1.2      tron /* Compatibility function. */
     91   1.2      tron char *__findenv(const char *name, int *offsetp);
     92   1.2      tron 
     93   1.2      tron __warn_references(__findenv,
     94   1.2      tron     "warning: __findenv is an internal obsolete function.")
     95   1.2      tron 
     96   1.1      tron /* Our initialization function. */
     97   1.1      tron void __libc_env_init(void);
     98   1.1      tron 
     99   1.1      tron /*ARGSUSED*/
    100   1.1      tron static signed int
    101   1.1      tron env_tree_compare_nodes(void *ctx, const void *node_a, const void *node_b)
    102   1.1      tron {
    103   1.1      tron 	uintptr_t addr_a, addr_b;
    104   1.1      tron 
    105   1.1      tron 	addr_a = (uintptr_t)node_a;
    106   1.1      tron 	addr_b = (uintptr_t)node_b;
    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 	if (addr_a > addr_b)
    112   1.1      tron 		return 1;
    113   1.1      tron 
    114   1.1      tron 	return 0;
    115   1.1      tron }
    116   1.1      tron 
    117   1.1      tron static signed int
    118   1.1      tron env_tree_compare_key(void *ctx, const void *node, const void *key)
    119   1.1      tron {
    120   1.1      tron 	return env_tree_compare_nodes(ctx, node,
    121   1.1      tron 	    (const uint8_t *)key - offsetof(env_node_t, data));
    122   1.1      tron }
    123   1.1      tron 
    124   1.1      tron /*
    125   1.1      tron  * Determine the of the name in an environment string. Return 0 if the
    126   1.1      tron  * name is not valid.
    127   1.1      tron  */
    128   1.1      tron size_t
    129   1.1      tron __envvarnamelen(const char *str, bool withequal)
    130   1.1      tron {
    131   1.1      tron 	size_t l_name;
    132   1.1      tron 
    133   1.1      tron 	if (str == NULL)
    134   1.1      tron 		return 0;
    135   1.1      tron 
    136   1.1      tron 	l_name = strcspn(str, "=");
    137   1.1      tron 	if (l_name == 0)
    138   1.1      tron 		return 0;
    139   1.1      tron 
    140   1.1      tron 	if (withequal) {
    141   1.1      tron 		if (str[l_name] != '=')
    142   1.1      tron 			return 0;
    143   1.1      tron 	} else {
    144   1.1      tron 		if (str[l_name] == '=')
    145   1.1      tron 			return 0;
    146   1.1      tron 	}
    147   1.1      tron 
    148   1.1      tron 	return l_name;
    149   1.1      tron }
    150   1.1      tron 
    151   1.1      tron /*
    152   1.1      tron  * Free memory occupied by environment variable if possible. This function
    153   1.1      tron  * must be called with the environment write locked.
    154   1.1      tron  */
    155   1.1      tron void
    156   1.1      tron __freeenvvar(char *envvar)
    157   1.1      tron {
    158   1.1      tron 	env_node_t *node;
    159   1.1      tron 
    160   1.1      tron 	_DIAGASSERT(envvar != NULL);
    161   1.1      tron 	node = rb_tree_find_node(&env_tree, envvar);
    162   1.1      tron 	if (node != NULL) {
    163   1.1      tron 		rb_tree_remove_node(&env_tree, node);
    164   1.1      tron 		free(node);
    165   1.1      tron 	}
    166   1.1      tron }
    167   1.1      tron 
    168   1.1      tron /*
    169   1.1      tron  * Allocate memory for an environment variable. This function must be called
    170   1.1      tron  * with the environment write locked.
    171   1.1      tron  */
    172   1.1      tron char *
    173   1.1      tron __allocenvvar(size_t length)
    174   1.1      tron {
    175   1.1      tron 	env_node_t *node;
    176   1.1      tron 
    177   1.1      tron 	node = malloc(sizeof(*node) + length);
    178   1.1      tron 	if (node != NULL) {
    179   1.1      tron 		node->length = length;
    180   1.4      tron 		node->marker = 0;
    181   1.1      tron 		rb_tree_insert_node(&env_tree, node);
    182   1.1      tron 		return node->data;
    183   1.1      tron 	} else {
    184   1.1      tron 		return NULL;
    185   1.1      tron 	}
    186   1.1      tron }
    187   1.1      tron 
    188   1.1      tron /*
    189   1.3     enami  * Check whether an environment variable is writable. This function must be
    190   1.1      tron  * called with the environment write locked as the caller will probably
    191   1.3     enami  * overwrite the environment variable afterwards.
    192   1.1      tron  */
    193   1.1      tron bool
    194   1.1      tron __canoverwriteenvvar(char *envvar, size_t length)
    195   1.1      tron {
    196   1.1      tron 	env_node_t *node;
    197   1.1      tron 
    198   1.1      tron 	_DIAGASSERT(envvar != NULL);
    199   1.1      tron 
    200   1.1      tron 	node = rb_tree_find_node(&env_tree, envvar);
    201   1.1      tron 	return (node != NULL && length <= node->length);
    202   1.1      tron }
    203   1.1      tron 
    204   1.4      tron /* Free all allocated environment variables that are no longer used. */
    205   1.4      tron static void
    206   1.4      tron __scrubenv(void)
    207   1.4      tron {
    208   1.4      tron 	static uint8_t marker = 0;
    209   1.4      tron 	size_t num_entries;
    210   1.4      tron 	env_node_t *node, *next;
    211   1.4      tron 
    212   1.4      tron 	while (++marker == 0);
    213   1.4      tron 
    214   1.4      tron 	/* Mark all nodes which are currently used. */
    215   1.4      tron 	for (num_entries = 0; environ[num_entries] != NULL; num_entries++) {
    216   1.4      tron 		node = rb_tree_find_node(&env_tree, environ[num_entries]);
    217   1.4      tron 		if (node != NULL)
    218   1.4      tron 			node->marker = marker;
    219   1.4      tron 	}
    220   1.4      tron 
    221   1.4      tron 	/* Free all nodes which are currently not used. */
    222   1.4      tron 	for (node = RB_TREE_MIN(&env_tree); node != NULL; node = next) {
    223   1.4      tron 		next = rb_tree_iterate(&env_tree, node, RB_DIR_RIGHT);
    224   1.4      tron 
    225   1.4      tron 		if (node->marker != marker) {
    226   1.4      tron 			rb_tree_remove_node(&env_tree, node);
    227   1.4      tron 			free(node);
    228   1.4      tron 		}
    229   1.4      tron 	}
    230   1.4      tron 
    231   1.4      tron 	/* Deal with the environment array itself. */
    232   1.4      tron 	if (environ == allocated_environ) {
    233   1.4      tron 		/* Clear out spurious entries in the environment. */
    234   1.4      tron 		(void)memset(&environ[num_entries + 1], 0,
    235   1.4      tron 		    (allocated_environ_size - num_entries - 1) *
    236   1.4      tron 		    sizeof(*environ));
    237   1.4      tron 	} else {
    238   1.4      tron 		/*
    239   1.4      tron 		 * The environment array was not allocated by "libc".
    240   1.4      tron 		 * Free our array if we allocated one.
    241   1.4      tron 		 */
    242   1.4      tron 		free(allocated_environ);
    243   1.4      tron 		allocated_environ = NULL;
    244   1.4      tron 		allocated_environ_size = 0;
    245   1.4      tron 	}
    246   1.4      tron }
    247   1.4      tron 
    248   1.1      tron /*
    249   1.1      tron  * Get a (new) slot in the environment. This function must be called with
    250   1.1      tron  * the environment write locked.
    251   1.1      tron  */
    252   1.1      tron ssize_t
    253   1.1      tron __getenvslot(const char *name, size_t l_name, bool allocate)
    254   1.1      tron {
    255   1.1      tron 	size_t new_size, num_entries, required_size;
    256   1.1      tron 	char **new_environ;
    257   1.1      tron 
    258   1.1      tron 	/* Search for an existing environment variable of the given name. */
    259   1.1      tron 	num_entries = 0;
    260  1.10     kamil 	if (environ != NULL) {
    261  1.10     kamil 		while (environ[num_entries] != NULL) {
    262  1.10     kamil 			if (strncmp(environ[num_entries], name, l_name) == 0 &&
    263  1.10     kamil 			    environ[num_entries][l_name] == '=') {
    264  1.10     kamil 				/* We found a match. */
    265  1.10     kamil 				return num_entries;
    266  1.10     kamil 			}
    267  1.10     kamil 			num_entries ++;
    268   1.1      tron 		}
    269   1.1      tron 	}
    270   1.1      tron 
    271   1.1      tron 	/* No match found, return if we don't want to allocate a new slot. */
    272   1.1      tron 	if (!allocate)
    273   1.1      tron 		return -1;
    274   1.1      tron 
    275   1.8      tron 	/* Does the environ need scrubbing? */
    276   1.8      tron 	if (environ != allocated_environ && allocated_environ != NULL)
    277   1.8      tron 		__scrubenv();
    278   1.8      tron 
    279   1.1      tron 	/* Create a new slot in the environment. */
    280   1.1      tron 	required_size = num_entries + 1;
    281   1.1      tron 	if (environ == allocated_environ &&
    282   1.1      tron 	    required_size < allocated_environ_size) {
    283   1.4      tron 		/* Does the environment need scrubbing? */
    284   1.4      tron 		if (required_size < allocated_environ_size &&
    285   1.4      tron 		    allocated_environ[required_size] != NULL) {
    286   1.4      tron 			__scrubenv();
    287   1.1      tron 		}
    288   1.1      tron 
    289   1.1      tron 		/* Return a free slot. */
    290   1.1      tron 		return num_entries;
    291   1.1      tron 	}
    292   1.1      tron 
    293   1.1      tron 	/* Determine size of a new environment array. */
    294   1.1      tron 	new_size = ENV_ARRAY_SIZE_MIN;
    295   1.1      tron 	while (new_size <= required_size)
    296   1.1      tron 		new_size <<= 1;
    297   1.1      tron 
    298   1.1      tron 	/* Allocate a new environment array. */
    299   1.1      tron 	if (environ == allocated_environ) {
    300  1.12       nia 		new_environ = environ;
    301  1.13  christos 		errno = reallocarr(&new_environ,
    302  1.13  christos 		    new_size, sizeof(*new_environ));
    303  1.13  christos 		if (errno)
    304   1.1      tron 			return -1;
    305   1.1      tron 	} else {
    306   1.1      tron 		free(allocated_environ);
    307   1.1      tron 		allocated_environ = NULL;
    308   1.1      tron 		allocated_environ_size = 0;
    309   1.1      tron 
    310  1.12       nia 		new_environ = NULL;
    311  1.13  christos 		errno = reallocarr(&new_environ,
    312  1.13  christos 		    new_size, sizeof(*new_environ));
    313  1.13  christos 		if (errno)
    314   1.1      tron 			return -1;
    315   1.1      tron 		(void)memcpy(new_environ, environ,
    316   1.1      tron 		    num_entries * sizeof(*new_environ));
    317   1.1      tron 	}
    318   1.1      tron 
    319   1.1      tron 	/* Clear remaining entries. */
    320   1.1      tron 	(void)memset(&new_environ[num_entries], 0,
    321   1.1      tron 	    (new_size - num_entries) * sizeof(*new_environ));
    322   1.1      tron 
    323   1.3     enami 	/* Use the new environment array. */
    324   1.1      tron 	environ = allocated_environ = new_environ;
    325   1.1      tron 	allocated_environ_size = new_size;
    326   1.1      tron 
    327   1.1      tron 	/* Return a free slot. */
    328   1.1      tron 	return num_entries;
    329   1.1      tron }
    330   1.1      tron 
    331   1.1      tron /* Find a string in the environment. */
    332   1.1      tron char *
    333   1.2      tron __findenvvar(const char *name, size_t l_name)
    334   1.1      tron {
    335   1.1      tron 	ssize_t offset;
    336   1.1      tron 
    337   1.1      tron 	offset = __getenvslot(name, l_name, false);
    338   1.1      tron 	return (offset != -1) ? environ[offset] + l_name + 1 : NULL;
    339   1.1      tron }
    340   1.1      tron 
    341   1.2      tron /* Compatibility interface, do *not* call this function. */
    342   1.2      tron char *
    343   1.2      tron __findenv(const char *name, int *offsetp)
    344   1.2      tron {
    345   1.2      tron 	size_t l_name;
    346   1.2      tron 	ssize_t offset;
    347   1.2      tron 
    348   1.2      tron 	l_name = __envvarnamelen(name, false);
    349   1.2      tron 	if (l_name == 0)
    350   1.2      tron 		return NULL;
    351   1.2      tron 
    352   1.2      tron 	offset = __getenvslot(name, l_name, false);
    353   1.2      tron 	if (offset < 0 || offset > INT_MAX)
    354   1.2      tron 		return NULL;
    355   1.2      tron 
    356   1.2      tron 	*offsetp = (int)offset;
    357   1.2      tron 	return environ[offset] + l_name + 1;
    358   1.2      tron }
    359   1.2      tron 
    360   1.1      tron #ifdef _REENTRANT
    361   1.1      tron 
    362   1.1      tron /* Lock the environment for read. */
    363   1.1      tron bool
    364   1.1      tron __readlockenv(void)
    365   1.1      tron {
    366   1.1      tron 	int error;
    367   1.1      tron 
    368   1.1      tron 	error = rwlock_rdlock(&env_lock);
    369   1.1      tron 	if (error == 0)
    370   1.1      tron 		return true;
    371   1.1      tron 
    372   1.1      tron 	errno = error;
    373   1.1      tron 	return false;
    374   1.1      tron }
    375   1.1      tron 
    376   1.1      tron /* Lock the environment for write. */
    377   1.1      tron bool
    378   1.1      tron __writelockenv(void)
    379   1.1      tron {
    380   1.1      tron 	int error;
    381   1.1      tron 
    382   1.1      tron 	error = rwlock_wrlock(&env_lock);
    383   1.1      tron 	if (error == 0)
    384   1.1      tron 		return true;
    385   1.1      tron 
    386   1.1      tron 	errno = error;
    387   1.1      tron 	return false;
    388   1.1      tron }
    389   1.1      tron 
    390   1.1      tron /* Unlock the environment for write. */
    391   1.1      tron bool
    392   1.1      tron __unlockenv(void)
    393   1.1      tron {
    394   1.1      tron 	int error;
    395   1.1      tron 
    396   1.1      tron 	error = rwlock_unlock(&env_lock);
    397   1.1      tron 	if (error == 0)
    398   1.1      tron 		return true;
    399   1.1      tron 
    400   1.1      tron 	errno = error;
    401   1.1      tron 	return false;
    402   1.1      tron }
    403   1.1      tron 
    404   1.1      tron #endif
    405