_env.c revision 1.12 1 1.12 nia /* $NetBSD: _env.c,v 1.12 2022/03/12 08:44:38 nia 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.12 nia __RCSID("$NetBSD: _env.c,v 1.12 2022/03/12 08:44:38 nia 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.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.1 tron /*ARGSUSED*/
99 1.1 tron static signed int
100 1.1 tron env_tree_compare_nodes(void *ctx, const void *node_a, const void *node_b)
101 1.1 tron {
102 1.1 tron uintptr_t addr_a, addr_b;
103 1.1 tron
104 1.1 tron addr_a = (uintptr_t)node_a;
105 1.1 tron addr_b = (uintptr_t)node_b;
106 1.1 tron
107 1.1 tron if (addr_a < addr_b)
108 1.1 tron return -1;
109 1.1 tron
110 1.1 tron if (addr_a > addr_b)
111 1.1 tron return 1;
112 1.1 tron
113 1.1 tron return 0;
114 1.1 tron }
115 1.1 tron
116 1.1 tron static signed int
117 1.1 tron env_tree_compare_key(void *ctx, const void *node, const void *key)
118 1.1 tron {
119 1.1 tron return env_tree_compare_nodes(ctx, node,
120 1.1 tron (const uint8_t *)key - offsetof(env_node_t, data));
121 1.1 tron }
122 1.1 tron
123 1.1 tron /*
124 1.1 tron * Determine the of the name in an environment string. Return 0 if the
125 1.1 tron * name is not valid.
126 1.1 tron */
127 1.1 tron size_t
128 1.1 tron __envvarnamelen(const char *str, bool withequal)
129 1.1 tron {
130 1.1 tron size_t l_name;
131 1.1 tron
132 1.1 tron if (str == NULL)
133 1.1 tron return 0;
134 1.1 tron
135 1.1 tron l_name = strcspn(str, "=");
136 1.1 tron if (l_name == 0)
137 1.1 tron return 0;
138 1.1 tron
139 1.1 tron if (withequal) {
140 1.1 tron if (str[l_name] != '=')
141 1.1 tron return 0;
142 1.1 tron } else {
143 1.1 tron if (str[l_name] == '=')
144 1.1 tron return 0;
145 1.1 tron }
146 1.1 tron
147 1.1 tron return l_name;
148 1.1 tron }
149 1.1 tron
150 1.1 tron /*
151 1.1 tron * Free memory occupied by environment variable if possible. This function
152 1.1 tron * must be called with the environment write locked.
153 1.1 tron */
154 1.1 tron void
155 1.1 tron __freeenvvar(char *envvar)
156 1.1 tron {
157 1.1 tron env_node_t *node;
158 1.1 tron
159 1.1 tron _DIAGASSERT(envvar != NULL);
160 1.1 tron node = rb_tree_find_node(&env_tree, envvar);
161 1.1 tron if (node != NULL) {
162 1.1 tron rb_tree_remove_node(&env_tree, node);
163 1.1 tron free(node);
164 1.1 tron }
165 1.1 tron }
166 1.1 tron
167 1.1 tron /*
168 1.1 tron * Allocate memory for an environment variable. This function must be called
169 1.1 tron * with the environment write locked.
170 1.1 tron */
171 1.1 tron char *
172 1.1 tron __allocenvvar(size_t length)
173 1.1 tron {
174 1.1 tron env_node_t *node;
175 1.1 tron
176 1.1 tron node = malloc(sizeof(*node) + length);
177 1.1 tron if (node != NULL) {
178 1.1 tron node->length = length;
179 1.4 tron node->marker = 0;
180 1.1 tron rb_tree_insert_node(&env_tree, node);
181 1.1 tron return node->data;
182 1.1 tron } else {
183 1.1 tron return NULL;
184 1.1 tron }
185 1.1 tron }
186 1.1 tron
187 1.1 tron /*
188 1.3 enami * Check whether an environment variable is writable. This function must be
189 1.1 tron * called with the environment write locked as the caller will probably
190 1.3 enami * overwrite the environment variable afterwards.
191 1.1 tron */
192 1.1 tron bool
193 1.1 tron __canoverwriteenvvar(char *envvar, size_t length)
194 1.1 tron {
195 1.1 tron env_node_t *node;
196 1.1 tron
197 1.1 tron _DIAGASSERT(envvar != NULL);
198 1.1 tron
199 1.1 tron node = rb_tree_find_node(&env_tree, envvar);
200 1.1 tron return (node != NULL && length <= node->length);
201 1.1 tron }
202 1.1 tron
203 1.4 tron /* Free all allocated environment variables that are no longer used. */
204 1.4 tron static void
205 1.4 tron __scrubenv(void)
206 1.4 tron {
207 1.4 tron static uint8_t marker = 0;
208 1.4 tron size_t num_entries;
209 1.4 tron env_node_t *node, *next;
210 1.4 tron
211 1.4 tron while (++marker == 0);
212 1.4 tron
213 1.4 tron /* Mark all nodes which are currently used. */
214 1.4 tron for (num_entries = 0; environ[num_entries] != NULL; num_entries++) {
215 1.4 tron node = rb_tree_find_node(&env_tree, environ[num_entries]);
216 1.4 tron if (node != NULL)
217 1.4 tron node->marker = marker;
218 1.4 tron }
219 1.4 tron
220 1.4 tron /* Free all nodes which are currently not used. */
221 1.4 tron for (node = RB_TREE_MIN(&env_tree); node != NULL; node = next) {
222 1.4 tron next = rb_tree_iterate(&env_tree, node, RB_DIR_RIGHT);
223 1.4 tron
224 1.4 tron if (node->marker != marker) {
225 1.4 tron rb_tree_remove_node(&env_tree, node);
226 1.4 tron free(node);
227 1.4 tron }
228 1.4 tron }
229 1.4 tron
230 1.4 tron /* Deal with the environment array itself. */
231 1.4 tron if (environ == allocated_environ) {
232 1.4 tron /* Clear out spurious entries in the environment. */
233 1.4 tron (void)memset(&environ[num_entries + 1], 0,
234 1.4 tron (allocated_environ_size - num_entries - 1) *
235 1.4 tron sizeof(*environ));
236 1.4 tron } else {
237 1.4 tron /*
238 1.4 tron * The environment array was not allocated by "libc".
239 1.4 tron * Free our array if we allocated one.
240 1.4 tron */
241 1.4 tron free(allocated_environ);
242 1.4 tron allocated_environ = NULL;
243 1.4 tron allocated_environ_size = 0;
244 1.4 tron }
245 1.4 tron }
246 1.4 tron
247 1.1 tron /*
248 1.1 tron * Get a (new) slot in the environment. This function must be called with
249 1.1 tron * the environment write locked.
250 1.1 tron */
251 1.1 tron ssize_t
252 1.1 tron __getenvslot(const char *name, size_t l_name, bool allocate)
253 1.1 tron {
254 1.1 tron size_t new_size, num_entries, required_size;
255 1.1 tron char **new_environ;
256 1.1 tron
257 1.1 tron /* Search for an existing environment variable of the given name. */
258 1.1 tron num_entries = 0;
259 1.10 kamil if (environ != NULL) {
260 1.10 kamil while (environ[num_entries] != NULL) {
261 1.10 kamil if (strncmp(environ[num_entries], name, l_name) == 0 &&
262 1.10 kamil environ[num_entries][l_name] == '=') {
263 1.10 kamil /* We found a match. */
264 1.10 kamil return num_entries;
265 1.10 kamil }
266 1.10 kamil num_entries ++;
267 1.1 tron }
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.8 tron /* Does the environ need scrubbing? */
275 1.8 tron if (environ != allocated_environ && allocated_environ != NULL)
276 1.8 tron __scrubenv();
277 1.8 tron
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.12 nia new_environ = environ;
300 1.12 nia if (reallocarr(&new_environ,
301 1.12 nia new_size, sizeof(*new_environ)) != 0) {
302 1.12 nia errno = ENOMEM;
303 1.1 tron return -1;
304 1.12 nia }
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.12 nia if (reallocarr(&new_environ,
312 1.12 nia new_size, sizeof(*new_environ)) != 0) {
313 1.12 nia errno = ENOMEM;
314 1.1 tron return -1;
315 1.12 nia }
316 1.1 tron (void)memcpy(new_environ, environ,
317 1.1 tron num_entries * sizeof(*new_environ));
318 1.1 tron }
319 1.1 tron
320 1.1 tron /* Clear remaining entries. */
321 1.1 tron (void)memset(&new_environ[num_entries], 0,
322 1.1 tron (new_size - num_entries) * sizeof(*new_environ));
323 1.1 tron
324 1.3 enami /* Use the new environment array. */
325 1.1 tron environ = allocated_environ = new_environ;
326 1.1 tron allocated_environ_size = new_size;
327 1.1 tron
328 1.1 tron /* Return a free slot. */
329 1.1 tron return num_entries;
330 1.1 tron }
331 1.1 tron
332 1.1 tron /* Find a string in the environment. */
333 1.1 tron char *
334 1.2 tron __findenvvar(const char *name, size_t l_name)
335 1.1 tron {
336 1.1 tron ssize_t offset;
337 1.1 tron
338 1.1 tron offset = __getenvslot(name, l_name, false);
339 1.1 tron return (offset != -1) ? environ[offset] + l_name + 1 : NULL;
340 1.1 tron }
341 1.1 tron
342 1.2 tron /* Compatibility interface, do *not* call this function. */
343 1.2 tron char *
344 1.2 tron __findenv(const char *name, int *offsetp)
345 1.2 tron {
346 1.2 tron size_t l_name;
347 1.2 tron ssize_t offset;
348 1.2 tron
349 1.2 tron l_name = __envvarnamelen(name, false);
350 1.2 tron if (l_name == 0)
351 1.2 tron return NULL;
352 1.2 tron
353 1.2 tron offset = __getenvslot(name, l_name, false);
354 1.2 tron if (offset < 0 || offset > INT_MAX)
355 1.2 tron return NULL;
356 1.2 tron
357 1.2 tron *offsetp = (int)offset;
358 1.2 tron return environ[offset] + l_name + 1;
359 1.2 tron }
360 1.2 tron
361 1.1 tron #ifdef _REENTRANT
362 1.1 tron
363 1.1 tron /* Lock the environment for read. */
364 1.1 tron bool
365 1.1 tron __readlockenv(void)
366 1.1 tron {
367 1.1 tron int error;
368 1.1 tron
369 1.1 tron error = rwlock_rdlock(&env_lock);
370 1.1 tron if (error == 0)
371 1.1 tron return true;
372 1.1 tron
373 1.1 tron errno = error;
374 1.1 tron return false;
375 1.1 tron }
376 1.1 tron
377 1.1 tron /* Lock the environment for write. */
378 1.1 tron bool
379 1.1 tron __writelockenv(void)
380 1.1 tron {
381 1.1 tron int error;
382 1.1 tron
383 1.1 tron error = rwlock_wrlock(&env_lock);
384 1.1 tron if (error == 0)
385 1.1 tron return true;
386 1.1 tron
387 1.1 tron errno = error;
388 1.1 tron return false;
389 1.1 tron }
390 1.1 tron
391 1.1 tron /* Unlock the environment for write. */
392 1.1 tron bool
393 1.1 tron __unlockenv(void)
394 1.1 tron {
395 1.1 tron int error;
396 1.1 tron
397 1.1 tron error = rwlock_unlock(&env_lock);
398 1.1 tron if (error == 0)
399 1.1 tron return true;
400 1.1 tron
401 1.1 tron errno = error;
402 1.1 tron return false;
403 1.1 tron }
404 1.1 tron
405 1.1 tron #endif
406 1.1 tron
407 1.1 tron /* Initialize environment memory RB tree. */
408 1.7 matt void __section(".text.startup")
409 1.1 tron __libc_env_init(void)
410 1.1 tron {
411 1.1 tron rb_tree_init(&env_tree, &env_tree_ops);
412 1.1 tron }
413