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 kern_getenv()/freeenv() kern_setenv() kern_unsetenv() testenv() for
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
40 #include <sys/param.h>
42 #include <sys/queue.h>
44 #include <sys/malloc.h>
45 #include <sys/mutex.h>
47 #include <sys/kernel.h>
48 #include <sys/systm.h>
49 #include <sys/sysent.h>
50 #include <sys/sysproto.h>
51 #include <sys/libkern.h>
53 #include <sys/limits.h>
55 #include <security/mac/mac_framework.h>
57 static char *_getenv_dynamic_locked(const char *name, int *idx);
58 static char *_getenv_dynamic(const char *name, int *idx);
60 static MALLOC_DEFINE(M_KENV, "kenv", "kernel environment");
62 #define KENV_SIZE 512 /* Maximum number of environment strings */
64 /* pointer to the config-generated static environment */
67 /* pointer to the md-static environment */
69 static int md_env_len;
70 static int md_env_pos;
72 static char *kernenv_next(char *);
74 /* dynamic environment variables */
79 * No need to protect this with a mutex since SYSINITS are single threaded.
83 #define KENV_CHECK if (!dynamic_kenv) \
84 panic("%s: called before SI_SUB_KMEM", __func__)
96 char *name, *value, *buffer = NULL;
97 size_t len, done, needed, buflen;
100 KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = false"));
103 if (uap->what == KENV_DUMP) {
105 error = mac_kenv_check_dump(td->td_ucred);
111 if (buflen > KENV_SIZE * (KENV_MNAMELEN + KENV_MVALLEN + 2))
112 buflen = KENV_SIZE * (KENV_MNAMELEN +
114 if (uap->len > 0 && uap->value != NULL)
115 buffer = malloc(buflen, M_TEMP, M_WAITOK|M_ZERO);
116 mtx_lock(&kenv_lock);
117 for (i = 0; kenvp[i] != NULL; i++) {
118 len = strlen(kenvp[i]) + 1;
120 len = min(len, buflen - done);
122 * If called with a NULL or insufficiently large
123 * buffer, just keep computing the required size.
125 if (uap->value != NULL && buffer != NULL && len > 0) {
126 bcopy(kenvp[i], buffer + done, len);
130 mtx_unlock(&kenv_lock);
131 if (buffer != NULL) {
132 error = copyout(buffer, uap->value, done);
133 free(buffer, M_TEMP);
135 td->td_retval[0] = ((done == needed) ? 0 : needed);
141 error = priv_check(td, PRIV_KENV_SET);
147 error = priv_check(td, PRIV_KENV_UNSET);
153 name = malloc(KENV_MNAMELEN + 1, M_TEMP, M_WAITOK);
155 error = copyinstr(uap->name, name, KENV_MNAMELEN + 1, NULL);
162 error = mac_kenv_check_get(td->td_ucred, name);
166 value = kern_getenv(name);
171 len = strlen(value) + 1;
174 error = copyout(value, uap->value, len);
178 td->td_retval[0] = len;
186 if (len > KENV_MVALLEN + 1)
187 len = KENV_MVALLEN + 1;
188 value = malloc(len, M_TEMP, M_WAITOK);
189 error = copyinstr(uap->value, value, len, NULL);
195 error = mac_kenv_check_set(td->td_ucred, name, value);
198 kern_setenv(name, value);
203 error = mac_kenv_check_unset(td->td_ucred, name);
207 error = kern_unsetenv(name);
221 * Populate the initial kernel environment.
223 * This is called very early in MD startup, either to provide a copy of the
224 * environment obtained from a boot loader, or to provide an empty buffer into
225 * which MD code can store an initial environment using kern_setenv() calls.
227 * kern_envp is set to the static_env generated by config(8). This implements
228 * the env keyword described in config(5).
230 * If len is non-zero, the caller is providing an empty buffer. The caller will
231 * subsequently use kern_setenv() to add up to len bytes of initial environment
232 * before the dynamic environment is available.
234 * If len is zero, the caller is providing a pre-loaded buffer containing
235 * environment strings. Additional strings cannot be added until the dynamic
236 * environment is available. The memory pointed to must remain stable at least
237 * until sysinit runs init_dynamic_kenv() and preferably until after SI_SUB_KMEM
238 * is finished so that subr_hints routines may continue to use it until the
239 * environments have been fully merged at the end of the pass. If no initial
240 * environment is available from the boot loader, passing a NULL pointer allows
241 * the static_env to be installed if it is configured. In this case, any call
242 * to kern_setenv() prior to the setup of the dynamic environment will result in
246 init_static_kenv(char *buf, size_t len)
248 char *eval, *loader_eval;
250 KASSERT(!dynamic_kenv, ("kenv: dynamic_kenv already initialized"));
252 * Give the static environment a chance to disable the loader(8)
253 * environment first. This is done with loader_env.disabled=1.
255 * static_env and static_hints may both be disabled, but in slightly
256 * different ways. For static_env, we just don't setup kern_envp and
257 * it's as if a static env wasn't even provided. For static_hints,
258 * we effectively zero out the buffer to stop the rest of the kernel
259 * from being able to use it.
261 * We're intentionally setting this up so that static_hints.disabled may
262 * be specified in either the MD env or the static env. This keeps us
263 * consistent in our new world view.
265 * As a warning, the static environment may not be disabled in any way
266 * if the static environment has disabled the loader environment.
268 * We're setting up the static environment early here because it will
269 * either be used or empty.
271 kern_envp = static_env;
272 loader_eval = kern_getenv("loader_env.disabled");
273 if (loader_eval != NULL && strcmp(loader_eval, "1") == 0)
274 /* Bail out early, the loader environment is disabled. */
278 * Next, the loader env is checked for the status of the static env. We
279 * are allowing static_env and static_hints to disable themselves here for
280 * the sake of simplicity.
286 eval = kern_getenv("static_env.disabled");
287 if (eval != NULL && strcmp(eval, "1") == 0)
290 eval = kern_getenv("static_hints.disabled");
291 if (eval != NULL && strcmp(eval, "1") == 0)
292 *static_hints = '\0';
295 * Now we see if we need to tear the loader environment back down due
296 * to the presence of a non-empty static environment and lack of request
297 * to keep it enabled.
299 if (*static_env != '\0' &&
300 (loader_eval == NULL || strcmp(loader_eval, "0") != 0)) {
307 init_dynamic_kenv_from(char *init_env, int *curpos)
309 char *cp, *cpnext, *eqpos, *found;
313 if (init_env && *init_env != '\0') {
316 for (cp = init_env; cp != NULL; cp = cpnext) {
317 cpnext = kernenv_next(cp);
318 len = strlen(cp) + 1;
319 if (len > KENV_MNAMELEN + 1 + KENV_MVALLEN + 1) {
321 "WARNING: too long kenv string, ignoring %s\n",
325 eqpos = strchr(cp, '=');
328 "WARNING: malformed static env value, ignoring %s\n",
334 * De-dupe the environment as we go. We don't add the
335 * duplicated assignments because config(8) will flip
336 * the order of the static environment around to make
337 * kernel processing match the order of specification
338 * in the kernel config.
340 found = _getenv_dynamic_locked(cp, NULL);
346 "WARNING: too many kenv strings, ignoring %s\n",
351 kenvp[i] = malloc(len, M_KENV, M_WAITOK);
352 strcpy(kenvp[i++], cp);
354 explicit_bzero(cp, len - 1);
361 * Setup the dynamic kernel environment.
364 init_dynamic_kenv(void *data __unused)
368 kenvp = malloc((KENV_SIZE + 1) * sizeof(char *), M_KENV,
372 init_dynamic_kenv_from(md_envp, &dynamic_envpos);
373 init_dynamic_kenv_from(kern_envp, &dynamic_envpos);
374 kenvp[dynamic_envpos] = NULL;
376 mtx_init(&kenv_lock, "kernel environment", NULL, MTX_DEF);
379 SYSINIT(kenv, SI_SUB_KMEM + 1, SI_ORDER_FIRST, init_dynamic_kenv, NULL);
385 if (dynamic_kenv && env != NULL) {
386 explicit_bzero(env, strlen(env));
392 * Internal functions for string lookup.
395 _getenv_dynamic_locked(const char *name, int *idx)
401 for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
402 if ((strncmp(cp, name, len) == 0) &&
406 return (cp + len + 1);
413 _getenv_dynamic(const char *name, int *idx)
416 mtx_assert(&kenv_lock, MA_OWNED);
417 return (_getenv_dynamic_locked(name, idx));
421 _getenv_static_from(char *chkenv, const char *name)
426 for (cp = chkenv; cp != NULL; cp = kernenv_next(cp)) {
427 for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
433 if (!strncmp(name, cp, len) && name[len] == 0)
440 _getenv_static(const char *name)
444 val = _getenv_static_from(md_envp, name);
447 val = _getenv_static_from(kern_envp, name);
454 * Look up an environment variable by name.
455 * Return a pointer to the string if found.
456 * The pointer has to be freed with freeenv()
460 kern_getenv(const char *name)
462 char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
466 if (getenv_string(name, buf, sizeof(buf))) {
467 ret = strdup(buf, M_KENV);
470 WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
474 ret = _getenv_static(name);
479 * Test if an environment variable is defined.
482 testenv(const char *name)
487 mtx_lock(&kenv_lock);
488 cp = _getenv_dynamic(name, NULL);
489 mtx_unlock(&kenv_lock);
491 cp = _getenv_static(name);
498 * Set an environment variable in the MD-static environment. This cannot
499 * feasibly be done on config(8)-generated static environments as they don't
500 * generally include space for extra variables.
503 setenv_static(const char *name, const char *value)
507 if (md_env_pos >= md_env_len)
510 /* Check space for x=y and two nuls */
511 len = strlen(name) + strlen(value);
512 if (len + 3 < md_env_len - md_env_pos) {
513 len = sprintf(&md_envp[md_env_pos], "%s=%s", name, value);
515 md_envp[md_env_pos] = '\0';
523 * Set an environment variable by name.
526 kern_setenv(const char *name, const char *value)
528 char *buf, *cp, *oldenv;
529 int namelen, vallen, i;
531 if (!dynamic_kenv && md_env_len > 0)
532 return (setenv_static(name, value));
536 namelen = strlen(name) + 1;
537 if (namelen > KENV_MNAMELEN + 1)
539 vallen = strlen(value) + 1;
540 if (vallen > KENV_MVALLEN + 1)
542 buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
543 sprintf(buf, "%s=%s", name, value);
545 mtx_lock(&kenv_lock);
546 cp = _getenv_dynamic(name, &i);
550 mtx_unlock(&kenv_lock);
551 free(oldenv, M_KENV);
553 /* We add the option if it wasn't found */
554 for (i = 0; (cp = kenvp[i]) != NULL; i++)
557 /* Bounds checking */
558 if (i < 0 || i >= KENV_SIZE) {
560 mtx_unlock(&kenv_lock);
566 mtx_unlock(&kenv_lock);
572 * Unset an environment variable string.
575 kern_unsetenv(const char *name)
582 mtx_lock(&kenv_lock);
583 cp = _getenv_dynamic(name, &i);
586 for (j = i + 1; kenvp[j] != NULL; j++)
587 kenvp[i++] = kenvp[j];
589 mtx_unlock(&kenv_lock);
590 explicit_bzero(oldenv, strlen(oldenv));
591 free(oldenv, M_KENV);
594 mtx_unlock(&kenv_lock);
599 * Return a string value from an environment variable.
602 getenv_string(const char *name, char *data, int size)
607 mtx_lock(&kenv_lock);
608 cp = _getenv_dynamic(name, NULL);
610 strlcpy(data, cp, size);
611 mtx_unlock(&kenv_lock);
613 cp = _getenv_static(name);
615 strlcpy(data, cp, size);
621 * Return an array of integers at the given type size and signedness.
624 getenv_array(const char *name, void *pdata, int size, int *psize,
625 int type_size, bool allow_signed)
627 char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
635 if (getenv_string(name, buf, sizeof(buf)) == 0)
638 /* get maximum number of elements */
643 for (ptr = buf; *ptr != 0; ) {
645 value = strtoq(ptr, &end, 0);
647 /* check if signed numbers are allowed */
648 if (value < 0 && !allow_signed)
651 /* check for invalid value */
655 /* check for valid suffix */
684 /* garbage after numeric value */
688 /* skip till next value, if any */
689 while (*end == '\t' || *end == ',' || *end == ' ')
700 if ((value >> shift) != old)
703 /* check for buffer overflow */
707 /* store value according to type size */
711 if (value < SCHAR_MIN || value > SCHAR_MAX)
714 if (value < 0 || value > UCHAR_MAX)
717 ((uint8_t *)pdata)[n] = (uint8_t)value;
721 if (value < SHRT_MIN || value > SHRT_MAX)
724 if (value < 0 || value > USHRT_MAX)
727 ((uint16_t *)pdata)[n] = (uint16_t)value;
731 if (value < INT_MIN || value > INT_MAX)
734 if (value > UINT_MAX)
737 ((uint32_t *)pdata)[n] = (uint32_t)value;
740 ((uint64_t *)pdata)[n] = (uint64_t)value;
747 *psize = n * type_size;
750 return (1); /* success */
752 return (0); /* failure */
756 * Return an integer value from an environment variable.
759 getenv_int(const char *name, int *data)
764 rval = getenv_quad(name, &tmp);
771 * Return an unsigned integer value from an environment variable.
774 getenv_uint(const char *name, unsigned int *data)
779 rval = getenv_quad(name, &tmp);
781 *data = (unsigned int) tmp;
786 * Return an int64_t value from an environment variable.
789 getenv_int64(const char *name, int64_t *data)
794 rval = getenv_quad(name, &tmp);
796 *data = (int64_t) tmp;
801 * Return an uint64_t value from an environment variable.
804 getenv_uint64(const char *name, uint64_t *data)
809 rval = getenv_quad(name, &tmp);
811 *data = (uint64_t) tmp;
816 * Return a long value from an environment variable.
819 getenv_long(const char *name, long *data)
824 rval = getenv_quad(name, &tmp);
831 * Return an unsigned long value from an environment variable.
834 getenv_ulong(const char *name, unsigned long *data)
839 rval = getenv_quad(name, &tmp);
841 *data = (unsigned long) tmp;
846 * Return a quad_t value from an environment variable.
849 getenv_quad(const char *name, quad_t *data)
851 char value[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
855 if (!getenv_string(name, value, sizeof(value)))
857 iv = strtoq(value, &vtp, 0);
858 if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0'))
879 * Find the next entry after the one which (cp) falls within, return a
880 * pointer to its start or NULL if there are no more.
883 kernenv_next(char *cp)
897 tunable_int_init(void *data)
899 struct tunable_int *d = (struct tunable_int *)data;
901 TUNABLE_INT_FETCH(d->path, d->var);
905 tunable_long_init(void *data)
907 struct tunable_long *d = (struct tunable_long *)data;
909 TUNABLE_LONG_FETCH(d->path, d->var);
913 tunable_ulong_init(void *data)
915 struct tunable_ulong *d = (struct tunable_ulong *)data;
917 TUNABLE_ULONG_FETCH(d->path, d->var);
921 tunable_int64_init(void *data)
923 struct tunable_int64 *d = (struct tunable_int64 *)data;
925 TUNABLE_INT64_FETCH(d->path, d->var);
929 tunable_uint64_init(void *data)
931 struct tunable_uint64 *d = (struct tunable_uint64 *)data;
933 TUNABLE_UINT64_FETCH(d->path, d->var);
937 tunable_quad_init(void *data)
939 struct tunable_quad *d = (struct tunable_quad *)data;
941 TUNABLE_QUAD_FETCH(d->path, d->var);
945 tunable_str_init(void *data)
947 struct tunable_str *d = (struct tunable_str *)data;
949 TUNABLE_STR_FETCH(d->path, d->var, d->size);