1*0a6a1f1dSLionel Sambuc /* $NetBSD: _env.c,v 1.9 2015/01/20 18:31:25 christos Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*-
42fe8fb19SBen Gras * Copyright (c) 2010 The NetBSD Foundation, Inc.
52fe8fb19SBen Gras * All rights reserved.
62fe8fb19SBen Gras *
72fe8fb19SBen Gras * This code is derived from software contributed to The NetBSD Foundation
82fe8fb19SBen Gras * by Matthias Scheler.
92fe8fb19SBen Gras *
102fe8fb19SBen Gras * Redistribution and use in source and binary forms, with or without
112fe8fb19SBen Gras * modification, are permitted provided that the following conditions
122fe8fb19SBen Gras * are met:
132fe8fb19SBen Gras * 1. Redistributions of source code must retain the above copyright
142fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer.
152fe8fb19SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
162fe8fb19SBen Gras * notice, this list of conditions and the following disclaimer in the
172fe8fb19SBen Gras * documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras *
192fe8fb19SBen Gras * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
202fe8fb19SBen Gras * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
212fe8fb19SBen Gras * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
222fe8fb19SBen Gras * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
232fe8fb19SBen Gras * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
242fe8fb19SBen Gras * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
252fe8fb19SBen Gras * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
262fe8fb19SBen Gras * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
272fe8fb19SBen Gras * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
282fe8fb19SBen Gras * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
292fe8fb19SBen Gras * POSSIBILITY OF SUCH DAMAGE.
302fe8fb19SBen Gras */
312fe8fb19SBen Gras
322fe8fb19SBen Gras #include <sys/cdefs.h>
332fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
34*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: _env.c,v 1.9 2015/01/20 18:31:25 christos Exp $");
352fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
362fe8fb19SBen Gras
372fe8fb19SBen Gras #include "namespace.h"
382fe8fb19SBen Gras
392fe8fb19SBen Gras #include <sys/rbtree.h>
402fe8fb19SBen Gras
412fe8fb19SBen Gras #include <assert.h>
422fe8fb19SBen Gras #include <errno.h>
432fe8fb19SBen Gras #include <limits.h>
442fe8fb19SBen Gras #include <stdlib.h>
452fe8fb19SBen Gras #include <stddef.h>
462fe8fb19SBen Gras #include <string.h>
472fe8fb19SBen Gras
482fe8fb19SBen Gras #include "env.h"
492fe8fb19SBen Gras #include "local.h"
502fe8fb19SBen Gras
512fe8fb19SBen Gras /*
522fe8fb19SBen Gras * Red-Black tree node for tracking memory used by environment variables.
532fe8fb19SBen Gras * The tree is sorted by the address of the nodes themselves.
542fe8fb19SBen Gras */
552fe8fb19SBen Gras typedef struct {
562fe8fb19SBen Gras rb_node_t rb_node;
572fe8fb19SBen Gras size_t length;
582fe8fb19SBen Gras uint8_t marker;
592fe8fb19SBen Gras char data[];
602fe8fb19SBen Gras } env_node_t;
612fe8fb19SBen Gras
622fe8fb19SBen Gras /* Compare functions for above tree. */
632fe8fb19SBen Gras static signed int env_tree_compare_nodes(void *, const void *, const void *);
642fe8fb19SBen Gras static signed int env_tree_compare_key(void *, const void *, const void *);
652fe8fb19SBen Gras
662fe8fb19SBen Gras /* Operations for above tree. */
672fe8fb19SBen Gras static const rb_tree_ops_t env_tree_ops = {
682fe8fb19SBen Gras .rbto_compare_nodes = env_tree_compare_nodes,
692fe8fb19SBen Gras .rbto_compare_key = env_tree_compare_key,
702fe8fb19SBen Gras .rbto_node_offset = offsetof(env_node_t, rb_node),
712fe8fb19SBen Gras .rbto_context = NULL
722fe8fb19SBen Gras };
732fe8fb19SBen Gras
742fe8fb19SBen Gras /* The single instance of above tree. */
752fe8fb19SBen Gras static rb_tree_t env_tree;
762fe8fb19SBen Gras
772fe8fb19SBen Gras /* The allocated environment. */
782fe8fb19SBen Gras static char **allocated_environ;
792fe8fb19SBen Gras static size_t allocated_environ_size;
802fe8fb19SBen Gras
812fe8fb19SBen Gras #define ENV_ARRAY_SIZE_MIN 16
822fe8fb19SBen Gras
832fe8fb19SBen Gras /* The lock protecting access to the environment. */
842fe8fb19SBen Gras #ifdef _REENTRANT
852fe8fb19SBen Gras static rwlock_t env_lock = RWLOCK_INITIALIZER;
862fe8fb19SBen Gras #endif
872fe8fb19SBen Gras
882fe8fb19SBen Gras /* Compatibility function. */
892fe8fb19SBen Gras char *__findenv(const char *name, int *offsetp);
902fe8fb19SBen Gras
912fe8fb19SBen Gras __warn_references(__findenv,
922fe8fb19SBen Gras "warning: __findenv is an internal obsolete function.")
932fe8fb19SBen Gras
942fe8fb19SBen Gras /* Our initialization function. */
952fe8fb19SBen Gras void __libc_env_init(void);
962fe8fb19SBen Gras
97f14fb602SLionel Sambuc char **environ;
98f14fb602SLionel Sambuc
992fe8fb19SBen Gras /*ARGSUSED*/
1002fe8fb19SBen Gras static signed int
env_tree_compare_nodes(void * ctx,const void * node_a,const void * node_b)1012fe8fb19SBen Gras env_tree_compare_nodes(void *ctx, const void *node_a, const void *node_b)
1022fe8fb19SBen Gras {
1032fe8fb19SBen Gras uintptr_t addr_a, addr_b;
1042fe8fb19SBen Gras
1052fe8fb19SBen Gras addr_a = (uintptr_t)node_a;
1062fe8fb19SBen Gras addr_b = (uintptr_t)node_b;
1072fe8fb19SBen Gras
1082fe8fb19SBen Gras if (addr_a < addr_b)
1092fe8fb19SBen Gras return -1;
1102fe8fb19SBen Gras
1112fe8fb19SBen Gras if (addr_a > addr_b)
1122fe8fb19SBen Gras return 1;
1132fe8fb19SBen Gras
1142fe8fb19SBen Gras return 0;
1152fe8fb19SBen Gras }
1162fe8fb19SBen Gras
1172fe8fb19SBen Gras static signed int
env_tree_compare_key(void * ctx,const void * node,const void * key)1182fe8fb19SBen Gras env_tree_compare_key(void *ctx, const void *node, const void *key)
1192fe8fb19SBen Gras {
1202fe8fb19SBen Gras return env_tree_compare_nodes(ctx, node,
1212fe8fb19SBen Gras (const uint8_t *)key - offsetof(env_node_t, data));
1222fe8fb19SBen Gras }
1232fe8fb19SBen Gras
1242fe8fb19SBen Gras /*
1252fe8fb19SBen Gras * Determine the of the name in an environment string. Return 0 if the
1262fe8fb19SBen Gras * name is not valid.
1272fe8fb19SBen Gras */
1282fe8fb19SBen Gras size_t
__envvarnamelen(const char * str,bool withequal)1292fe8fb19SBen Gras __envvarnamelen(const char *str, bool withequal)
1302fe8fb19SBen Gras {
1312fe8fb19SBen Gras size_t l_name;
1322fe8fb19SBen Gras
1332fe8fb19SBen Gras if (str == NULL)
1342fe8fb19SBen Gras return 0;
1352fe8fb19SBen Gras
1362fe8fb19SBen Gras l_name = strcspn(str, "=");
1372fe8fb19SBen Gras if (l_name == 0)
1382fe8fb19SBen Gras return 0;
1392fe8fb19SBen Gras
1402fe8fb19SBen Gras if (withequal) {
1412fe8fb19SBen Gras if (str[l_name] != '=')
1422fe8fb19SBen Gras return 0;
1432fe8fb19SBen Gras } else {
1442fe8fb19SBen Gras if (str[l_name] == '=')
1452fe8fb19SBen Gras return 0;
1462fe8fb19SBen Gras }
1472fe8fb19SBen Gras
1482fe8fb19SBen Gras return l_name;
1492fe8fb19SBen Gras }
1502fe8fb19SBen Gras
1512fe8fb19SBen Gras /*
1522fe8fb19SBen Gras * Free memory occupied by environment variable if possible. This function
1532fe8fb19SBen Gras * must be called with the environment write locked.
1542fe8fb19SBen Gras */
1552fe8fb19SBen Gras void
__freeenvvar(char * envvar)1562fe8fb19SBen Gras __freeenvvar(char *envvar)
1572fe8fb19SBen Gras {
1582fe8fb19SBen Gras env_node_t *node;
1592fe8fb19SBen Gras
1602fe8fb19SBen Gras _DIAGASSERT(envvar != NULL);
1612fe8fb19SBen Gras node = rb_tree_find_node(&env_tree, envvar);
1622fe8fb19SBen Gras if (node != NULL) {
1632fe8fb19SBen Gras rb_tree_remove_node(&env_tree, node);
1642fe8fb19SBen Gras free(node);
1652fe8fb19SBen Gras }
1662fe8fb19SBen Gras }
1672fe8fb19SBen Gras
1682fe8fb19SBen Gras /*
1692fe8fb19SBen Gras * Allocate memory for an environment variable. This function must be called
1702fe8fb19SBen Gras * with the environment write locked.
1712fe8fb19SBen Gras */
1722fe8fb19SBen Gras char *
__allocenvvar(size_t length)1732fe8fb19SBen Gras __allocenvvar(size_t length)
1742fe8fb19SBen Gras {
1752fe8fb19SBen Gras env_node_t *node;
1762fe8fb19SBen Gras
1772fe8fb19SBen Gras node = malloc(sizeof(*node) + length);
1782fe8fb19SBen Gras if (node != NULL) {
1792fe8fb19SBen Gras node->length = length;
1802fe8fb19SBen Gras node->marker = 0;
1812fe8fb19SBen Gras rb_tree_insert_node(&env_tree, node);
1822fe8fb19SBen Gras return node->data;
1832fe8fb19SBen Gras } else {
1842fe8fb19SBen Gras return NULL;
1852fe8fb19SBen Gras }
1862fe8fb19SBen Gras }
1872fe8fb19SBen Gras
1882fe8fb19SBen Gras /*
1892fe8fb19SBen Gras * Check whether an environment variable is writable. This function must be
1902fe8fb19SBen Gras * called with the environment write locked as the caller will probably
1912fe8fb19SBen Gras * overwrite the environment variable afterwards.
1922fe8fb19SBen Gras */
1932fe8fb19SBen Gras bool
__canoverwriteenvvar(char * envvar,size_t length)1942fe8fb19SBen Gras __canoverwriteenvvar(char *envvar, size_t length)
1952fe8fb19SBen Gras {
1962fe8fb19SBen Gras env_node_t *node;
1972fe8fb19SBen Gras
1982fe8fb19SBen Gras _DIAGASSERT(envvar != NULL);
1992fe8fb19SBen Gras
2002fe8fb19SBen Gras node = rb_tree_find_node(&env_tree, envvar);
2012fe8fb19SBen Gras return (node != NULL && length <= node->length);
2022fe8fb19SBen Gras }
2032fe8fb19SBen Gras
2042fe8fb19SBen Gras /* Free all allocated environment variables that are no longer used. */
2052fe8fb19SBen Gras static void
__scrubenv(void)2062fe8fb19SBen Gras __scrubenv(void)
2072fe8fb19SBen Gras {
2082fe8fb19SBen Gras static uint8_t marker = 0;
2092fe8fb19SBen Gras size_t num_entries;
2102fe8fb19SBen Gras env_node_t *node, *next;
2112fe8fb19SBen Gras
2122fe8fb19SBen Gras while (++marker == 0);
2132fe8fb19SBen Gras
2142fe8fb19SBen Gras /* Mark all nodes which are currently used. */
2152fe8fb19SBen Gras for (num_entries = 0; environ[num_entries] != NULL; num_entries++) {
2162fe8fb19SBen Gras node = rb_tree_find_node(&env_tree, environ[num_entries]);
2172fe8fb19SBen Gras if (node != NULL)
2182fe8fb19SBen Gras node->marker = marker;
2192fe8fb19SBen Gras }
2202fe8fb19SBen Gras
2212fe8fb19SBen Gras /* Free all nodes which are currently not used. */
2222fe8fb19SBen Gras for (node = RB_TREE_MIN(&env_tree); node != NULL; node = next) {
2232fe8fb19SBen Gras next = rb_tree_iterate(&env_tree, node, RB_DIR_RIGHT);
2242fe8fb19SBen Gras
2252fe8fb19SBen Gras if (node->marker != marker) {
2262fe8fb19SBen Gras rb_tree_remove_node(&env_tree, node);
2272fe8fb19SBen Gras free(node);
2282fe8fb19SBen Gras }
2292fe8fb19SBen Gras }
2302fe8fb19SBen Gras
2312fe8fb19SBen Gras /* Deal with the environment array itself. */
2322fe8fb19SBen Gras if (environ == allocated_environ) {
2332fe8fb19SBen Gras /* Clear out spurious entries in the environment. */
2342fe8fb19SBen Gras (void)memset(&environ[num_entries + 1], 0,
2352fe8fb19SBen Gras (allocated_environ_size - num_entries - 1) *
2362fe8fb19SBen Gras sizeof(*environ));
2372fe8fb19SBen Gras } else {
2382fe8fb19SBen Gras /*
2392fe8fb19SBen Gras * The environment array was not allocated by "libc".
2402fe8fb19SBen Gras * Free our array if we allocated one.
2412fe8fb19SBen Gras */
2422fe8fb19SBen Gras free(allocated_environ);
2432fe8fb19SBen Gras allocated_environ = NULL;
2442fe8fb19SBen Gras allocated_environ_size = 0;
2452fe8fb19SBen Gras }
2462fe8fb19SBen Gras }
2472fe8fb19SBen Gras
2482fe8fb19SBen Gras /*
2492fe8fb19SBen Gras * Get a (new) slot in the environment. This function must be called with
2502fe8fb19SBen Gras * the environment write locked.
2512fe8fb19SBen Gras */
2522fe8fb19SBen Gras ssize_t
__getenvslot(const char * name,size_t l_name,bool allocate)2532fe8fb19SBen Gras __getenvslot(const char *name, size_t l_name, bool allocate)
2542fe8fb19SBen Gras {
2552fe8fb19SBen Gras size_t new_size, num_entries, required_size;
2562fe8fb19SBen Gras char **new_environ;
2572fe8fb19SBen Gras
2582fe8fb19SBen Gras /* Search for an existing environment variable of the given name. */
2592fe8fb19SBen Gras num_entries = 0;
2602fe8fb19SBen Gras while (environ[num_entries] != NULL) {
2612fe8fb19SBen Gras if (strncmp(environ[num_entries], name, l_name) == 0 &&
2622fe8fb19SBen Gras environ[num_entries][l_name] == '=') {
2632fe8fb19SBen Gras /* We found a match. */
2642fe8fb19SBen Gras return num_entries;
2652fe8fb19SBen Gras }
2662fe8fb19SBen Gras num_entries ++;
2672fe8fb19SBen Gras }
2682fe8fb19SBen Gras
2692fe8fb19SBen Gras /* No match found, return if we don't want to allocate a new slot. */
2702fe8fb19SBen Gras if (!allocate)
2712fe8fb19SBen Gras return -1;
2722fe8fb19SBen Gras
27384d9c625SLionel Sambuc /* Does the environ need scrubbing? */
27484d9c625SLionel Sambuc if (environ != allocated_environ && allocated_environ != NULL)
27584d9c625SLionel Sambuc __scrubenv();
27684d9c625SLionel Sambuc
2772fe8fb19SBen Gras /* Create a new slot in the environment. */
2782fe8fb19SBen Gras required_size = num_entries + 1;
2792fe8fb19SBen Gras if (environ == allocated_environ &&
2802fe8fb19SBen Gras required_size < allocated_environ_size) {
2812fe8fb19SBen Gras /* Does the environment need scrubbing? */
2822fe8fb19SBen Gras if (required_size < allocated_environ_size &&
2832fe8fb19SBen Gras allocated_environ[required_size] != NULL) {
2842fe8fb19SBen Gras __scrubenv();
2852fe8fb19SBen Gras }
2862fe8fb19SBen Gras
2872fe8fb19SBen Gras /* Return a free slot. */
2882fe8fb19SBen Gras return num_entries;
2892fe8fb19SBen Gras }
2902fe8fb19SBen Gras
2912fe8fb19SBen Gras /* Determine size of a new environment array. */
2922fe8fb19SBen Gras new_size = ENV_ARRAY_SIZE_MIN;
2932fe8fb19SBen Gras while (new_size <= required_size)
2942fe8fb19SBen Gras new_size <<= 1;
2952fe8fb19SBen Gras
2962fe8fb19SBen Gras /* Allocate a new environment array. */
2972fe8fb19SBen Gras if (environ == allocated_environ) {
2982fe8fb19SBen Gras new_environ = realloc(environ,
2992fe8fb19SBen Gras new_size * sizeof(*new_environ));
3002fe8fb19SBen Gras if (new_environ == NULL)
3012fe8fb19SBen Gras return -1;
3022fe8fb19SBen Gras } else {
3032fe8fb19SBen Gras free(allocated_environ);
3042fe8fb19SBen Gras allocated_environ = NULL;
3052fe8fb19SBen Gras allocated_environ_size = 0;
3062fe8fb19SBen Gras
3072fe8fb19SBen Gras new_environ = malloc(new_size * sizeof(*new_environ));
3082fe8fb19SBen Gras if (new_environ == NULL)
3092fe8fb19SBen Gras return -1;
3102fe8fb19SBen Gras (void)memcpy(new_environ, environ,
3112fe8fb19SBen Gras num_entries * sizeof(*new_environ));
3122fe8fb19SBen Gras }
3132fe8fb19SBen Gras
3142fe8fb19SBen Gras /* Clear remaining entries. */
3152fe8fb19SBen Gras (void)memset(&new_environ[num_entries], 0,
3162fe8fb19SBen Gras (new_size - num_entries) * sizeof(*new_environ));
3172fe8fb19SBen Gras
3182fe8fb19SBen Gras /* Use the new environment array. */
3192fe8fb19SBen Gras environ = allocated_environ = new_environ;
3202fe8fb19SBen Gras allocated_environ_size = new_size;
3212fe8fb19SBen Gras
3222fe8fb19SBen Gras /* Return a free slot. */
3232fe8fb19SBen Gras return num_entries;
3242fe8fb19SBen Gras }
3252fe8fb19SBen Gras
3262fe8fb19SBen Gras /* Find a string in the environment. */
3272fe8fb19SBen Gras char *
__findenvvar(const char * name,size_t l_name)3282fe8fb19SBen Gras __findenvvar(const char *name, size_t l_name)
3292fe8fb19SBen Gras {
3302fe8fb19SBen Gras ssize_t offset;
3312fe8fb19SBen Gras
3322fe8fb19SBen Gras offset = __getenvslot(name, l_name, false);
3332fe8fb19SBen Gras return (offset != -1) ? environ[offset] + l_name + 1 : NULL;
3342fe8fb19SBen Gras }
3352fe8fb19SBen Gras
3362fe8fb19SBen Gras /* Compatibility interface, do *not* call this function. */
3372fe8fb19SBen Gras char *
__findenv(const char * name,int * offsetp)3382fe8fb19SBen Gras __findenv(const char *name, int *offsetp)
3392fe8fb19SBen Gras {
3402fe8fb19SBen Gras size_t l_name;
3412fe8fb19SBen Gras ssize_t offset;
3422fe8fb19SBen Gras
3432fe8fb19SBen Gras l_name = __envvarnamelen(name, false);
3442fe8fb19SBen Gras if (l_name == 0)
3452fe8fb19SBen Gras return NULL;
3462fe8fb19SBen Gras
3472fe8fb19SBen Gras offset = __getenvslot(name, l_name, false);
3482fe8fb19SBen Gras if (offset < 0 || offset > INT_MAX)
3492fe8fb19SBen Gras return NULL;
3502fe8fb19SBen Gras
3512fe8fb19SBen Gras *offsetp = (int)offset;
3522fe8fb19SBen Gras return environ[offset] + l_name + 1;
3532fe8fb19SBen Gras }
3542fe8fb19SBen Gras
3552fe8fb19SBen Gras #ifdef _REENTRANT
3562fe8fb19SBen Gras
3572fe8fb19SBen Gras /* Lock the environment for read. */
3582fe8fb19SBen Gras bool
__readlockenv(void)3592fe8fb19SBen Gras __readlockenv(void)
3602fe8fb19SBen Gras {
3612fe8fb19SBen Gras int error;
3622fe8fb19SBen Gras
3632fe8fb19SBen Gras error = rwlock_rdlock(&env_lock);
3642fe8fb19SBen Gras if (error == 0)
3652fe8fb19SBen Gras return true;
3662fe8fb19SBen Gras
3672fe8fb19SBen Gras errno = error;
3682fe8fb19SBen Gras return false;
3692fe8fb19SBen Gras }
3702fe8fb19SBen Gras
3712fe8fb19SBen Gras /* Lock the environment for write. */
3722fe8fb19SBen Gras bool
__writelockenv(void)3732fe8fb19SBen Gras __writelockenv(void)
3742fe8fb19SBen Gras {
3752fe8fb19SBen Gras int error;
3762fe8fb19SBen Gras
3772fe8fb19SBen Gras error = rwlock_wrlock(&env_lock);
3782fe8fb19SBen Gras if (error == 0)
3792fe8fb19SBen Gras return true;
3802fe8fb19SBen Gras
3812fe8fb19SBen Gras errno = error;
3822fe8fb19SBen Gras return false;
3832fe8fb19SBen Gras }
3842fe8fb19SBen Gras
3852fe8fb19SBen Gras /* Unlock the environment for write. */
3862fe8fb19SBen Gras bool
__unlockenv(void)3872fe8fb19SBen Gras __unlockenv(void)
3882fe8fb19SBen Gras {
3892fe8fb19SBen Gras int error;
3902fe8fb19SBen Gras
3912fe8fb19SBen Gras error = rwlock_unlock(&env_lock);
3922fe8fb19SBen Gras if (error == 0)
3932fe8fb19SBen Gras return true;
3942fe8fb19SBen Gras
3952fe8fb19SBen Gras errno = error;
3962fe8fb19SBen Gras return false;
3972fe8fb19SBen Gras }
3982fe8fb19SBen Gras
3992fe8fb19SBen Gras #endif
4002fe8fb19SBen Gras
4012fe8fb19SBen Gras /* Initialize environment memory RB tree. */
40284d9c625SLionel Sambuc void __section(".text.startup")
__libc_env_init(void)4032fe8fb19SBen Gras __libc_env_init(void)
4042fe8fb19SBen Gras {
4052fe8fb19SBen Gras rb_tree_init(&env_tree, &env_tree_ops);
4062fe8fb19SBen Gras }
407