2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998 Michael Smith
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * 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 kern_getenv()/freeenv() kern_setenv() kern_unsetenv() testenv() for
39 #include <sys/cdefs.h>
40 __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 */
67 static char *kernenv_next(char *);
69 /* dynamic environment variables */
74 * No need to protect this with a mutex since SYSINITS are single threaded.
78 #define KENV_CHECK if (!dynamic_kenv) \
79 panic("%s: called before SI_SUB_KMEM", __func__)
91 char *name, *value, *buffer = NULL;
92 size_t len, done, needed, buflen;
95 KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = 0"));
98 if (uap->what == KENV_DUMP) {
100 error = mac_kenv_check_dump(td->td_ucred);
106 if (buflen > KENV_SIZE * (KENV_MNAMELEN + KENV_MVALLEN + 2))
107 buflen = KENV_SIZE * (KENV_MNAMELEN +
109 if (uap->len > 0 && uap->value != NULL)
110 buffer = malloc(buflen, M_TEMP, M_WAITOK|M_ZERO);
111 mtx_lock(&kenv_lock);
112 for (i = 0; kenvp[i] != NULL; i++) {
113 len = strlen(kenvp[i]) + 1;
115 len = min(len, buflen - done);
117 * If called with a NULL or insufficiently large
118 * buffer, just keep computing the required size.
120 if (uap->value != NULL && buffer != NULL && len > 0) {
121 bcopy(kenvp[i], buffer + done, len);
125 mtx_unlock(&kenv_lock);
126 if (buffer != NULL) {
127 error = copyout(buffer, uap->value, done);
128 free(buffer, M_TEMP);
130 td->td_retval[0] = ((done == needed) ? 0 : needed);
136 error = priv_check(td, PRIV_KENV_SET);
142 error = priv_check(td, PRIV_KENV_UNSET);
148 name = malloc(KENV_MNAMELEN + 1, M_TEMP, M_WAITOK);
150 error = copyinstr(uap->name, name, KENV_MNAMELEN + 1, NULL);
157 error = mac_kenv_check_get(td->td_ucred, name);
161 value = kern_getenv(name);
166 len = strlen(value) + 1;
169 error = copyout(value, uap->value, len);
173 td->td_retval[0] = len;
181 if (len > KENV_MVALLEN + 1)
182 len = KENV_MVALLEN + 1;
183 value = malloc(len, M_TEMP, M_WAITOK);
184 error = copyinstr(uap->value, value, len, NULL);
190 error = mac_kenv_check_set(td->td_ucred, name, value);
193 kern_setenv(name, value);
198 error = mac_kenv_check_unset(td->td_ucred, name);
202 error = kern_unsetenv(name);
216 * Populate the initial kernel environment.
218 * This is called very early in MD startup, either to provide a copy of the
219 * environment obtained from a boot loader, or to provide an empty buffer into
220 * which MD code can store an initial environment using kern_setenv() calls.
222 * When a copy of an initial environment is passed in, we start by scanning that
223 * env for overrides to the compiled-in envmode and hintmode variables.
225 * If the global envmode is 1, the environment is initialized from the global
226 * static_env[], regardless of the arguments passed. This implements the env
227 * keyword described in config(5). In this case env_pos is set to env_len,
228 * causing kern_setenv() to return -1 (if len > 0) or panic (if len == 0) until
229 * the dynamic environment is available. The envmode and static_env variables
230 * are defined in env.c which is generated by config(8).
232 * If len is non-zero, the caller is providing an empty buffer. The caller will
233 * subsequently use kern_setenv() to add up to len bytes of initial environment
234 * before the dynamic environment is available.
236 * If len is zero, the caller is providing a pre-loaded buffer containing
237 * environment strings. Additional strings cannot be added until the dynamic
238 * environment is available. The memory pointed to must remain stable at least
239 * until sysinit runs init_dynamic_kenv(). If no initial environment is
240 * available from the boot loader, passing a NULL pointer allows the static_env
241 * to be installed if it is configured.
244 init_static_kenv(char *buf, size_t len)
248 for (cp = buf; cp != NULL && cp[0] != '\0'; cp += strlen(cp) + 1) {
249 if (strcmp(cp, "static_env.disabled=1") == 0)
251 if (strcmp(cp, "static_hints.disabled=1") == 0)
256 kern_envp = static_env;
267 * Setup the dynamic kernel environment.
270 init_dynamic_kenv(void *data __unused)
276 kenvp = malloc((KENV_SIZE + 1) * sizeof(char *), M_KENV,
279 if (kern_envp && *kern_envp != '\0') {
280 for (cp = kern_envp; cp != NULL; cp = cpnext) {
281 cpnext = kernenv_next(cp);
282 len = strlen(cp) + 1;
283 if (len > KENV_MNAMELEN + 1 + KENV_MVALLEN + 1) {
285 "WARNING: too long kenv string, ignoring %s\n",
290 kenvp[i] = malloc(len, M_KENV, M_WAITOK);
291 strcpy(kenvp[i++], cp);
292 explicit_bzero(cp, strlen(cp));
295 "WARNING: too many kenv strings, ignoring %s\n",
301 mtx_init(&kenv_lock, "kernel environment", NULL, MTX_DEF);
304 SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL);
310 if (dynamic_kenv && env != NULL) {
311 explicit_bzero(env, strlen(env));
317 * Internal functions for string lookup.
320 _getenv_dynamic(const char *name, int *idx)
325 mtx_assert(&kenv_lock, MA_OWNED);
327 for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
328 if ((strncmp(cp, name, len) == 0) &&
332 return (cp + len + 1);
339 _getenv_static(const char *name)
344 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
345 for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
351 if (!strncmp(name, cp, len) && name[len] == 0)
358 * Look up an environment variable by name.
359 * Return a pointer to the string if found.
360 * The pointer has to be freed with freeenv()
364 kern_getenv(const char *name)
366 char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
370 if (getenv_string(name, buf, sizeof(buf))) {
371 ret = strdup(buf, M_KENV);
374 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
378 ret = _getenv_static(name);
383 * Test if an environment variable is defined.
386 testenv(const char *name)
391 mtx_lock(&kenv_lock);
392 cp = _getenv_dynamic(name, NULL);
393 mtx_unlock(&kenv_lock);
395 cp = _getenv_static(name);
402 setenv_static(const char *name, const char *value)
406 if (env_pos >= env_len)
409 /* Check space for x=y and two nuls */
410 len = strlen(name) + strlen(value);
411 if (len + 3 < env_len - env_pos) {
412 len = sprintf(&kern_envp[env_pos], "%s=%s", name, value);
414 kern_envp[env_pos] = '\0';
422 * Set an environment variable by name.
425 kern_setenv(const char *name, const char *value)
427 char *buf, *cp, *oldenv;
428 int namelen, vallen, i;
430 if (dynamic_kenv == 0 && env_len > 0)
431 return (setenv_static(name, value));
435 namelen = strlen(name) + 1;
436 if (namelen > KENV_MNAMELEN + 1)
438 vallen = strlen(value) + 1;
439 if (vallen > KENV_MVALLEN + 1)
441 buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
442 sprintf(buf, "%s=%s", name, value);
444 mtx_lock(&kenv_lock);
445 cp = _getenv_dynamic(name, &i);
449 mtx_unlock(&kenv_lock);
450 free(oldenv, M_KENV);
452 /* We add the option if it wasn't found */
453 for (i = 0; (cp = kenvp[i]) != NULL; i++)
456 /* Bounds checking */
457 if (i < 0 || i >= KENV_SIZE) {
459 mtx_unlock(&kenv_lock);
465 mtx_unlock(&kenv_lock);
471 * Unset an environment variable string.
474 kern_unsetenv(const char *name)
481 mtx_lock(&kenv_lock);
482 cp = _getenv_dynamic(name, &i);
485 for (j = i + 1; kenvp[j] != NULL; j++)
486 kenvp[i++] = kenvp[j];
488 mtx_unlock(&kenv_lock);
489 explicit_bzero(oldenv, strlen(oldenv));
490 free(oldenv, M_KENV);
493 mtx_unlock(&kenv_lock);
498 * Return a string value from an environment variable.
501 getenv_string(const char *name, char *data, int size)
506 mtx_lock(&kenv_lock);
507 cp = _getenv_dynamic(name, NULL);
509 strlcpy(data, cp, size);
510 mtx_unlock(&kenv_lock);
512 cp = _getenv_static(name);
514 strlcpy(data, cp, size);
520 * Return an integer value from an environment variable.
523 getenv_int(const char *name, int *data)
528 rval = getenv_quad(name, &tmp);
535 * Return an unsigned integer value from an environment variable.
538 getenv_uint(const char *name, unsigned int *data)
543 rval = getenv_quad(name, &tmp);
545 *data = (unsigned int) tmp;
550 * Return an int64_t value from an environment variable.
553 getenv_int64(const char *name, int64_t *data)
558 rval = getenv_quad(name, &tmp);
560 *data = (int64_t) tmp;
565 * Return an uint64_t value from an environment variable.
568 getenv_uint64(const char *name, uint64_t *data)
573 rval = getenv_quad(name, &tmp);
575 *data = (uint64_t) tmp;
580 * Return a long value from an environment variable.
583 getenv_long(const char *name, long *data)
588 rval = getenv_quad(name, &tmp);
595 * Return an unsigned long value from an environment variable.
598 getenv_ulong(const char *name, unsigned long *data)
603 rval = getenv_quad(name, &tmp);
605 *data = (unsigned long) tmp;
610 * Return a quad_t value from an environment variable.
613 getenv_quad(const char *name, quad_t *data)
615 char value[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
619 if (!getenv_string(name, value, sizeof(value)))
621 iv = strtoq(value, &vtp, 0);
622 if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0'))
643 * Find the next entry after the one which (cp) falls within, return a
644 * pointer to its start or NULL if there are no more.
647 kernenv_next(char *cp)
661 tunable_int_init(void *data)
663 struct tunable_int *d = (struct tunable_int *)data;
665 TUNABLE_INT_FETCH(d->path, d->var);
669 tunable_long_init(void *data)
671 struct tunable_long *d = (struct tunable_long *)data;
673 TUNABLE_LONG_FETCH(d->path, d->var);
677 tunable_ulong_init(void *data)
679 struct tunable_ulong *d = (struct tunable_ulong *)data;
681 TUNABLE_ULONG_FETCH(d->path, d->var);
685 tunable_int64_init(void *data)
687 struct tunable_int64 *d = (struct tunable_int64 *)data;
689 TUNABLE_INT64_FETCH(d->path, d->var);
693 tunable_uint64_init(void *data)
695 struct tunable_uint64 *d = (struct tunable_uint64 *)data;
697 TUNABLE_UINT64_FETCH(d->path, d->var);
701 tunable_quad_init(void *data)
703 struct tunable_quad *d = (struct tunable_quad *)data;
705 TUNABLE_QUAD_FETCH(d->path, d->var);
709 tunable_str_init(void *data)
711 struct tunable_str *d = (struct tunable_str *)data;
713 TUNABLE_STR_FETCH(d->path, d->var, d->size);