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$");
40 #include <sys/types.h>
41 #include <sys/param.h>
43 #include <sys/queue.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
48 #include <sys/kernel.h>
49 #include <sys/systm.h>
50 #include <sys/sysent.h>
51 #include <sys/sysproto.h>
52 #include <sys/libkern.h>
55 #include <security/mac/mac_framework.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 since SYSINITS are single threaded.
74 #define KENV_CHECK if (!dynamic_kenv) \
75 panic("%s: called before SI_SUB_KMEM", __func__)
87 char *name, *value, *buffer = NULL;
88 size_t len, done, needed, buflen;
91 KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = 0"));
94 if (uap->what == KENV_DUMP) {
96 error = mac_kenv_check_dump(td->td_ucred);
102 if (buflen > KENV_SIZE * (KENV_MNAMELEN + KENV_MVALLEN + 2))
103 buflen = KENV_SIZE * (KENV_MNAMELEN +
105 if (uap->len > 0 && uap->value != NULL)
106 buffer = malloc(buflen, M_TEMP, M_WAITOK|M_ZERO);
107 mtx_lock(&kenv_lock);
108 for (i = 0; kenvp[i] != NULL; i++) {
109 len = strlen(kenvp[i]) + 1;
111 len = min(len, buflen - done);
113 * If called with a NULL or insufficiently large
114 * buffer, just keep computing the required size.
116 if (uap->value != NULL && buffer != NULL && len > 0) {
117 bcopy(kenvp[i], buffer + done, len);
121 mtx_unlock(&kenv_lock);
122 if (buffer != NULL) {
123 error = copyout(buffer, uap->value, done);
124 free(buffer, M_TEMP);
126 td->td_retval[0] = ((done == needed) ? 0 : needed);
132 error = priv_check(td, PRIV_KENV_SET);
138 error = priv_check(td, PRIV_KENV_UNSET);
144 name = malloc(KENV_MNAMELEN, M_TEMP, M_WAITOK);
146 error = copyinstr(uap->name, name, KENV_MNAMELEN, NULL);
153 error = mac_kenv_check_get(td->td_ucred, name);
157 value = getenv(name);
162 len = strlen(value) + 1;
165 error = copyout(value, uap->value, len);
169 td->td_retval[0] = len;
177 if (len > KENV_MVALLEN)
179 value = malloc(len, M_TEMP, M_WAITOK);
180 error = copyinstr(uap->value, value, len, NULL);
186 error = mac_kenv_check_set(td->td_ucred, name, value);
194 error = mac_kenv_check_unset(td->td_ucred, name);
198 error = unsetenv(name);
212 * Setup the dynamic kernel environment.
215 init_dynamic_kenv(void *data __unused)
220 kenvp = malloc((KENV_SIZE + 1) * sizeof(char *), M_KENV,
223 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
224 len = strlen(cp) + 1;
226 kenvp[i] = malloc(len, M_KENV, M_WAITOK);
227 strcpy(kenvp[i++], cp);
230 "WARNING: too many kenv strings, ignoring %s\n",
235 mtx_init(&kenv_lock, "kernel environment", NULL, MTX_DEF);
238 SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL);
249 * Internal functions for string lookup.
252 _getenv_dynamic(const char *name, int *idx)
257 mtx_assert(&kenv_lock, MA_OWNED);
259 for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
260 if ((strncmp(cp, name, len) == 0) &&
264 return (cp + len + 1);
271 _getenv_static(const char *name)
276 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
277 for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
283 if (!strncmp(name, cp, len) && name[len] == 0)
290 * Look up an environment variable by name.
291 * Return a pointer to the string if found.
292 * The pointer has to be freed with freeenv()
296 getenv(const char *name)
298 char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
303 mtx_lock(&kenv_lock);
304 cp = _getenv_dynamic(name, NULL);
307 mtx_unlock(&kenv_lock);
308 len = strlen(buf) + 1;
309 ret = malloc(len, M_KENV, M_WAITOK);
312 mtx_unlock(&kenv_lock);
316 ret = _getenv_static(name);
321 * Test if an environment variable is defined.
324 testenv(const char *name)
329 mtx_lock(&kenv_lock);
330 cp = _getenv_dynamic(name, NULL);
331 mtx_unlock(&kenv_lock);
333 cp = _getenv_static(name);
340 * Set an environment variable by name.
343 setenv(const char *name, const char *value)
345 char *buf, *cp, *oldenv;
346 int namelen, vallen, i;
350 namelen = strlen(name) + 1;
351 if (namelen > KENV_MNAMELEN)
353 vallen = strlen(value) + 1;
354 if (vallen > KENV_MVALLEN)
356 buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
357 sprintf(buf, "%s=%s", name, value);
359 mtx_lock(&kenv_lock);
360 cp = _getenv_dynamic(name, &i);
364 mtx_unlock(&kenv_lock);
365 free(oldenv, M_KENV);
367 /* We add the option if it wasn't found */
368 for (i = 0; (cp = kenvp[i]) != NULL; i++)
371 /* Bounds checking */
372 if (i < 0 || i >= KENV_SIZE) {
374 mtx_unlock(&kenv_lock);
380 mtx_unlock(&kenv_lock);
386 * Unset an environment variable string.
389 unsetenv(const char *name)
396 mtx_lock(&kenv_lock);
397 cp = _getenv_dynamic(name, &i);
400 for (j = i + 1; kenvp[j] != NULL; j++)
401 kenvp[i++] = kenvp[j];
403 mtx_unlock(&kenv_lock);
404 free(oldenv, M_KENV);
407 mtx_unlock(&kenv_lock);
412 * Return a string value from an environment variable.
415 getenv_string(const char *name, char *data, int size)
421 strlcpy(data, tmp, size);
429 * Return an integer value from an environment variable.
432 getenv_int(const char *name, int *data)
437 rval = getenv_quad(name, &tmp);
444 * Return an unsigned integer value from an environment variable.
447 getenv_uint(const char *name, unsigned int *data)
452 rval = getenv_quad(name, &tmp);
454 *data = (unsigned int) tmp;
459 * Return a long value from an environment variable.
462 getenv_long(const char *name, long *data)
467 rval = getenv_quad(name, &tmp);
474 * Return an unsigned long value from an environment variable.
477 getenv_ulong(const char *name, unsigned long *data)
482 rval = getenv_quad(name, &tmp);
484 *data = (unsigned long) tmp;
489 * Return a quad_t value from an environment variable.
492 getenv_quad(const char *name, quad_t *data)
498 value = getenv(name);
501 iv = strtoq(value, &vtp, 0);
502 if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0')) {
527 * Find the next entry after the one which (cp) falls within, return a
528 * pointer to its start or NULL if there are no more.
531 kernenv_next(char *cp)
545 tunable_int_init(void *data)
547 struct tunable_int *d = (struct tunable_int *)data;
549 TUNABLE_INT_FETCH(d->path, d->var);
553 tunable_long_init(void *data)
555 struct tunable_long *d = (struct tunable_long *)data;
557 TUNABLE_LONG_FETCH(d->path, d->var);
561 tunable_ulong_init(void *data)
563 struct tunable_ulong *d = (struct tunable_ulong *)data;
565 TUNABLE_ULONG_FETCH(d->path, d->var);
569 tunable_quad_init(void *data)
571 struct tunable_quad *d = (struct tunable_quad *)data;
573 TUNABLE_QUAD_FETCH(d->path, d->var);
577 tunable_str_init(void *data)
579 struct tunable_str *d = (struct tunable_str *)data;
581 TUNABLE_STR_FETCH(d->path, d->var, d->size);