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
28 * The unified bootloader passes us a pointer to a preserved copy of
29 * bootstrap/kernel environment variables. We convert them to a
30 * dynamic array of strings later when the VM subsystem is up.
32 * We make these available through the kenv(2) syscall for userland
33 * and through getenv()/freeenv() setenv() unsetenv() testenv() for
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
42 #include <sys/types.h>
43 #include <sys/param.h>
45 #include <sys/queue.h>
47 #include <sys/malloc.h>
48 #include <sys/mutex.h>
50 #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 #include <security/mac/mac_framework.h>
59 static MALLOC_DEFINE(M_KENV, "kenv", "kernel environment");
61 #define KENV_SIZE 512 /* Maximum number of environment strings */
63 /* pointer to the static environment */
65 static char *kernenv_next(char *);
67 /* dynamic environment variables */
72 * No need to protect this with a mutex
73 * since SYSINITS are single threaded.
77 #define KENV_CHECK if (!dynamic_kenv) \
78 panic("%s: called before SI_SUB_KMEM", __func__)
90 char *name, *value, *buffer = NULL;
91 size_t len, done, needed;
94 KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = 0"));
97 if (uap->what == KENV_DUMP) {
99 error = mac_check_kenv_dump(td->td_ucred);
104 if (uap->len > 0 && uap->value != NULL)
105 buffer = malloc(uap->len, M_TEMP, M_WAITOK|M_ZERO);
106 mtx_lock(&kenv_lock);
107 for (i = 0; kenvp[i] != NULL; i++) {
108 len = strlen(kenvp[i]) + 1;
110 len = min(len, uap->len - done);
112 * If called with a NULL or insufficiently large
113 * buffer, just keep computing the required size.
115 if (uap->value != NULL && buffer != NULL && len > 0) {
116 bcopy(kenvp[i], buffer + done, len);
120 mtx_unlock(&kenv_lock);
121 if (buffer != NULL) {
122 error = copyout(buffer, uap->value, done);
123 free(buffer, M_TEMP);
125 td->td_retval[0] = ((done == needed) ? 0 : needed);
131 error = priv_check(td, PRIV_KENV_SET);
137 error = priv_check(td, PRIV_KENV_UNSET);
143 name = malloc(KENV_MNAMELEN, M_TEMP, M_WAITOK);
145 error = copyinstr(uap->name, name, KENV_MNAMELEN, NULL);
152 error = mac_check_kenv_get(td->td_ucred, name);
156 value = getenv(name);
161 len = strlen(value) + 1;
164 error = copyout(value, uap->value, len);
168 td->td_retval[0] = len;
176 if (len > KENV_MVALLEN)
178 value = malloc(len, M_TEMP, M_WAITOK);
179 error = copyinstr(uap->value, value, len, NULL);
185 error = mac_check_kenv_set(td->td_ucred, name, value);
193 error = mac_check_kenv_unset(td->td_ucred, name);
197 error = unsetenv(name);
211 * Setup the dynamic kernel environment.
214 init_dynamic_kenv(void *data __unused)
219 kenvp = malloc((KENV_SIZE + 1) * sizeof(char *), M_KENV,
222 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
223 len = strlen(cp) + 1;
225 kenvp[i] = malloc(len, M_KENV, M_WAITOK);
226 strcpy(kenvp[i++], cp);
229 "WARNING: too many kenv strings, ignoring %s\n",
234 mtx_init(&kenv_lock, "kernel environment", NULL, MTX_DEF);
237 SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL);
248 * Internal functions for string lookup.
251 _getenv_dynamic(const char *name, int *idx)
256 mtx_assert(&kenv_lock, MA_OWNED);
258 for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
259 if ((strncmp(cp, name, len) == 0) &&
263 return (cp + len + 1);
270 _getenv_static(const char *name)
275 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
276 for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
282 if (!strncmp(name, cp, len) && name[len] == 0)
289 * Look up an environment variable by name.
290 * Return a pointer to the string if found.
291 * The pointer has to be freed with freeenv()
295 getenv(const char *name)
297 char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
302 mtx_lock(&kenv_lock);
303 cp = _getenv_dynamic(name, NULL);
306 mtx_unlock(&kenv_lock);
307 len = strlen(buf) + 1;
308 ret = malloc(len, M_KENV, M_WAITOK);
311 mtx_unlock(&kenv_lock);
315 ret = _getenv_static(name);
320 * Test if an environment variable is defined.
323 testenv(const char *name)
328 mtx_lock(&kenv_lock);
329 cp = _getenv_dynamic(name, NULL);
330 mtx_unlock(&kenv_lock);
332 cp = _getenv_static(name);
339 * Set an environment variable by name.
342 setenv(const char *name, const char *value)
344 char *buf, *cp, *oldenv;
345 int namelen, vallen, i;
349 namelen = strlen(name) + 1;
350 if (namelen > KENV_MNAMELEN)
352 vallen = strlen(value) + 1;
353 if (vallen > KENV_MVALLEN)
355 buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
356 sprintf(buf, "%s=%s", name, value);
358 mtx_lock(&kenv_lock);
359 cp = _getenv_dynamic(name, &i);
363 mtx_unlock(&kenv_lock);
364 free(oldenv, M_KENV);
366 /* We add the option if it wasn't found */
367 for (i = 0; (cp = kenvp[i]) != NULL; i++)
370 /* Bounds checking */
371 if (i < 0 || i >= KENV_SIZE) {
373 mtx_unlock(&kenv_lock);
379 mtx_unlock(&kenv_lock);
385 * Unset an environment variable string.
388 unsetenv(const char *name)
395 mtx_lock(&kenv_lock);
396 cp = _getenv_dynamic(name, &i);
399 for (j = i + 1; kenvp[j] != NULL; j++)
400 kenvp[i++] = kenvp[j];
402 mtx_unlock(&kenv_lock);
403 free(oldenv, M_KENV);
406 mtx_unlock(&kenv_lock);
411 * Return a string value from an environment variable.
414 getenv_string(const char *name, char *data, int size)
420 strlcpy(data, tmp, size);
428 * Return an integer value from an environment variable.
431 getenv_int(const char *name, int *data)
436 rval = getenv_quad(name, &tmp);
443 * Return a long value from an environment variable.
446 getenv_long(const char *name, long *data)
451 rval = getenv_quad(name, &tmp);
458 * Return an unsigned long value from an environment variable.
461 getenv_ulong(const char *name, unsigned long *data)
466 rval = getenv_quad(name, &tmp);
468 *data = (unsigned long) tmp;
473 * Return a quad_t value from an environment variable.
476 getenv_quad(const char *name, quad_t *data)
482 value = getenv(name);
485 iv = strtoq(value, &vtp, 0);
486 if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0')) {
511 * Find the next entry after the one which (cp) falls within, return a
512 * pointer to its start or NULL if there are no more.
515 kernenv_next(char *cp)
529 tunable_int_init(void *data)
531 struct tunable_int *d = (struct tunable_int *)data;
533 TUNABLE_INT_FETCH(d->path, d->var);
537 tunable_long_init(void *data)
539 struct tunable_long *d = (struct tunable_long *)data;
541 TUNABLE_LONG_FETCH(d->path, d->var);
545 tunable_ulong_init(void *data)
547 struct tunable_ulong *d = (struct tunable_ulong *)data;
549 TUNABLE_ULONG_FETCH(d->path, d->var);
553 tunable_str_init(void *data)
555 struct tunable_str *d = (struct tunable_str *)data;
557 TUNABLE_STR_FETCH(d->path, d->var, d->size);