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>
48 #include <sys/malloc.h>
49 #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 static 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__)
88 char *name, *value, *buffer = NULL;
89 size_t len, done, needed;
92 KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = 0"));
95 if (uap->what == KENV_DUMP) {
97 error = mac_check_kenv_dump(td->td_ucred);
102 if (uap->len > 0 && uap->value != NULL)
103 buffer = malloc(uap->len, M_TEMP, M_WAITOK|M_ZERO);
104 mtx_lock(&kenv_lock);
105 for (i = 0; kenvp[i] != NULL; i++) {
106 len = strlen(kenvp[i]) + 1;
108 len = min(len, uap->len - done);
110 * If called with a NULL or insufficiently large
111 * buffer, just keep computing the required size.
113 if (uap->value != NULL && buffer != NULL && len > 0) {
114 bcopy(kenvp[i], buffer + done, len);
118 mtx_unlock(&kenv_lock);
119 if (buffer != NULL) {
120 error = copyout(buffer, uap->value, done);
121 free(buffer, M_TEMP);
123 td->td_retval[0] = ((done == needed) ? 0 : needed);
127 if ((uap->what == KENV_SET) ||
128 (uap->what == KENV_UNSET)) {
134 name = malloc(KENV_MNAMELEN, M_TEMP, M_WAITOK);
136 error = copyinstr(uap->name, name, KENV_MNAMELEN, NULL);
143 error = mac_check_kenv_get(td->td_ucred, name);
147 value = getenv(name);
152 len = strlen(value) + 1;
155 error = copyout(value, uap->value, len);
159 td->td_retval[0] = len;
167 if (len > KENV_MVALLEN)
169 value = malloc(len, M_TEMP, M_WAITOK);
170 error = copyinstr(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 + 1) * sizeof(char *), M_KENV,
213 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
214 len = strlen(cp) + 1;
216 kenvp[i] = malloc(len, M_KENV, M_WAITOK);
217 strcpy(kenvp[i++], cp);
220 "WARNING: too many kenv strings, ignoring %s\n",
225 mtx_init(&kenv_lock, "kernel environment", NULL, MTX_DEF);
228 SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL);
239 * Internal functions for string lookup.
242 _getenv_dynamic(const char *name, int *idx)
247 mtx_assert(&kenv_lock, MA_OWNED);
249 for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
250 if ((strncmp(cp, name, len) == 0) &&
254 return (cp + len + 1);
261 _getenv_static(const char *name)
266 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
267 for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
273 if (!strncmp(name, cp, len) && name[len] == 0)
280 * Look up an environment variable by name.
281 * Return a pointer to the string if found.
282 * The pointer has to be freed with freeenv()
286 getenv(const char *name)
288 char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
293 mtx_lock(&kenv_lock);
294 cp = _getenv_dynamic(name, NULL);
297 mtx_unlock(&kenv_lock);
298 len = strlen(buf) + 1;
299 ret = malloc(len, M_KENV, M_WAITOK);
302 mtx_unlock(&kenv_lock);
306 ret = _getenv_static(name);
311 * Test if an environment variable is defined.
314 testenv(const char *name)
319 mtx_lock(&kenv_lock);
320 cp = _getenv_dynamic(name, NULL);
321 mtx_unlock(&kenv_lock);
323 cp = _getenv_static(name);
330 * Set an environment variable by name.
333 setenv(const char *name, const char *value)
335 char *buf, *cp, *oldenv;
336 int namelen, vallen, i;
340 namelen = strlen(name) + 1;
341 if (namelen > KENV_MNAMELEN)
343 vallen = strlen(value) + 1;
344 if (vallen > KENV_MVALLEN)
346 buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
347 sprintf(buf, "%s=%s", name, value);
349 mtx_lock(&kenv_lock);
350 cp = _getenv_dynamic(name, &i);
354 mtx_unlock(&kenv_lock);
355 free(oldenv, M_KENV);
357 /* We add the option if it wasn't found */
358 for (i = 0; (cp = kenvp[i]) != NULL; i++)
361 /* Bounds checking */
362 if (i < 0 || i >= KENV_SIZE) {
364 mtx_unlock(&kenv_lock);
370 mtx_unlock(&kenv_lock);
376 * Unset an environment variable string.
379 unsetenv(const char *name)
386 mtx_lock(&kenv_lock);
387 cp = _getenv_dynamic(name, &i);
390 for (j = i + 1; kenvp[j] != NULL; j++)
391 kenvp[i++] = kenvp[j];
393 mtx_unlock(&kenv_lock);
394 free(oldenv, M_KENV);
397 mtx_unlock(&kenv_lock);
402 * Return a string value from an environment variable.
405 getenv_string(const char *name, char *data, int size)
411 strlcpy(data, tmp, size);
419 * Return an integer value from an environment variable.
422 getenv_int(const char *name, int *data)
427 rval = getenv_quad(name, &tmp);
434 * Return a long value from an environment variable.
437 getenv_long(const char *name, long *data)
442 rval = getenv_quad(name, &tmp);
449 * Return an unsigned long value from an environment variable.
452 getenv_ulong(const char *name, unsigned long *data)
457 rval = getenv_quad(name, &tmp);
459 *data = (unsigned long) tmp;
464 * Return a quad_t value from an environment variable.
467 getenv_quad(const char *name, quad_t *data)
473 value = getenv(name);
476 iv = strtoq(value, &vtp, 0);
477 if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0')) {
502 * Find the next entry after the one which (cp) falls within, return a
503 * pointer to its start or NULL if there are no more.
506 kernenv_next(char *cp)
520 tunable_int_init(void *data)
522 struct tunable_int *d = (struct tunable_int *)data;
524 TUNABLE_INT_FETCH(d->path, d->var);
528 tunable_long_init(void *data)
530 struct tunable_long *d = (struct tunable_long *)data;
532 TUNABLE_LONG_FETCH(d->path, d->var);
536 tunable_ulong_init(void *data)
538 struct tunable_ulong *d = (struct tunable_ulong *)data;
540 TUNABLE_ULONG_FETCH(d->path, d->var);
544 tunable_str_init(void *data)
546 struct tunable_str *d = (struct tunable_str *)data;
548 TUNABLE_STR_FETCH(d->path, d->var, d->size);