2 * Copyright (c) 1998 Michael Smith
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * The unified bootloader passes us a pointer to a preserved copy of
31 * bootstrap/kernel environment variables. We convert them to a
32 * dynamic array of strings later when the VM subsystem is up.
34 * We make these available through the kenv(2) syscall for userland
35 * and through getenv()/freeenv() setenv() unsetenv() testenv() for
41 #include <sys/types.h>
42 #include <sys/param.h>
44 #include <sys/queue.h>
47 #include <sys/malloc.h>
48 #include <sys/mutex.h>
49 #include <sys/kernel.h>
51 #include <sys/systm.h>
52 #include <sys/sysent.h>
53 #include <sys/sysproto.h>
54 #include <sys/libkern.h>
57 MALLOC_DEFINE(M_KENV, "kenv", "kernel environment");
59 #define KENV_SIZE 512 /* Maximum number of environment strings */
61 /* pointer to the static environment */
63 static char *kernenv_next(char *);
65 /* dynamic environment variables */
70 * No need to protect this with a mutex
71 * since SYSINITS are single threaded.
75 #define KENV_CHECK if (!dynamic_kenv) \
76 panic("%s: called before SI_SUB_KMEM", __func__)
83 syscallarg(const char *) name;
84 syscallarg(char *) value;
92 KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = 0"));
95 if (SCARG(uap, what) == KENV_DUMP) {
97 error = mac_check_kenv_dump(td->td_ucred);
102 /* Return the size if called with a NULL buffer */
103 if (SCARG(uap, value) == NULL) {
104 sx_slock(&kenv_lock);
105 for (i = 0; kenvp[i] != NULL; i++)
106 len += strlen(kenvp[i]) + 1;
107 sx_sunlock(&kenv_lock);
108 td->td_retval[0] = len;
112 sx_slock(&kenv_lock);
113 for (i = 0; kenvp[i] != NULL && done < SCARG(uap, len); i++) {
114 len = min(strlen(kenvp[i]) + 1, SCARG(uap, len) - done);
115 error = copyout(kenvp[i], SCARG(uap, value) + done,
118 sx_sunlock(&kenv_lock);
123 sx_sunlock(&kenv_lock);
127 if ((SCARG(uap, what) == KENV_SET) ||
128 (SCARG(uap, what) == KENV_UNSET)) {
134 name = malloc(KENV_MNAMELEN, M_TEMP, M_WAITOK);
136 error = copyinstr(SCARG(uap, name), name, KENV_MNAMELEN, NULL);
140 switch (SCARG(uap, what)) {
143 error = mac_check_kenv_get(td->td_ucred, name);
147 value = getenv(name);
152 len = strlen(value) + 1;
153 if (len > SCARG(uap, len))
154 len = SCARG(uap, len);
155 error = copyout(value, SCARG(uap, value), len);
159 td->td_retval[0] = len;
162 len = SCARG(uap, len);
167 if (len > KENV_MVALLEN)
169 value = malloc(len, M_TEMP, M_WAITOK);
170 error = copyinstr(SCARG(uap, value), value, len, NULL);
176 error = mac_check_kenv_set(td->td_ucred, name, value);
184 error = mac_check_kenv_unset(td->td_ucred, name);
188 error = unsetenv(name);
202 * Setup the dynamic kernel environment.
205 init_dynamic_kenv(void *data __unused)
210 kenvp = malloc(KENV_SIZE * sizeof(char *), M_KENV, M_WAITOK | M_ZERO);
212 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
213 len = strlen(cp) + 1;
214 kenvp[i] = malloc(len, M_KENV, M_WAITOK);
215 strcpy(kenvp[i++], cp);
219 sx_init(&kenv_lock, "kernel environment");
222 SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL);
233 * Internal functions for string lookup.
236 _getenv_dynamic(const char *name, int *idx)
241 sx_assert(&kenv_lock, SX_LOCKED);
243 for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
244 if ((cp[len] == '=') &&
245 (strncmp(cp, name, len) == 0)) {
248 return (cp + len + 1);
255 _getenv_static(const char *name)
260 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
261 for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
267 if (!strncmp(name, cp, len) && name[len] == 0)
274 * Look up an environment variable by name.
275 * Return a pointer to the string if found.
276 * The pointer has to be freed with freeenv()
280 getenv(const char *name)
282 char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
287 sx_slock(&kenv_lock);
288 cp = _getenv_dynamic(name, NULL);
291 sx_sunlock(&kenv_lock);
292 len = strlen(buf) + 1;
293 ret = malloc(len, M_KENV, M_WAITOK);
296 sx_sunlock(&kenv_lock);
300 ret = _getenv_static(name);
305 * Test if an environment variable is defined.
308 testenv(const char *name)
313 sx_slock(&kenv_lock);
314 cp = _getenv_dynamic(name, NULL);
315 sx_sunlock(&kenv_lock);
317 cp = _getenv_static(name);
324 * Set an environment variable by name.
327 setenv(const char *name, const char *value)
329 char *buf, *cp, *oldenv;
330 int namelen, vallen, i;
334 namelen = strlen(name) + 1;
335 if (namelen > KENV_MNAMELEN)
337 vallen = strlen(value) + 1;
338 if (vallen > KENV_MVALLEN)
340 buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
341 sprintf(buf, "%s=%s", name, value);
343 sx_xlock(&kenv_lock);
344 cp = _getenv_dynamic(name, &i);
348 sx_xunlock(&kenv_lock);
349 free(oldenv, M_KENV);
351 /* We add the option if it wasn't found */
352 for (i = 0; (cp = kenvp[i]) != NULL; i++)
356 sx_xunlock(&kenv_lock);
362 * Unset an environment variable string.
365 unsetenv(const char *name)
372 sx_xlock(&kenv_lock);
373 cp = _getenv_dynamic(name, &i);
376 for (j = i + 1; kenvp[j] != NULL; j++)
377 kenvp[i++] = kenvp[j];
379 sx_xunlock(&kenv_lock);
380 free(oldenv, M_KENV);
383 sx_xunlock(&kenv_lock);
388 * Return a string value from an environment variable.
391 getenv_string(const char *name, char *data, int size)
397 strlcpy(data, tmp, size);
405 * Return an integer value from an environment variable.
408 getenv_int(const char *name, int *data)
413 rval = getenv_quad(name, &tmp);
420 * Return a quad_t value from an environment variable.
423 getenv_quad(const char *name, quad_t *data)
429 value = getenv(name);
432 iv = strtoq(value, &vtp, 0);
433 if ((vtp == value) || (*vtp != '\0')) {
443 * Find the next entry after the one which (cp) falls within, return a
444 * pointer to its start or NULL if there are no more.
447 kernenv_next(char *cp)
461 tunable_int_init(void *data)
463 struct tunable_int *d = (struct tunable_int *)data;
465 TUNABLE_INT_FETCH(d->path, d->var);
469 tunable_quad_init(void *data)
471 struct tunable_quad *d = (struct tunable_quad *)data;
473 TUNABLE_QUAD_FETCH(d->path, d->var);
477 tunable_str_init(void *data)
479 struct tunable_str *d = (struct tunable_str *)data;
481 TUNABLE_STR_FETCH(d->path, d->var, d->size);