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
26 * $FreeBSD: src/sys/kern/kern_environment.c,v 1.10.2.7 2002/05/07 09:57:16 bde Exp $
27 * $DragonFly: src/sys/kern/kern_environment.c,v 1.7 2008/07/23 16:39:28 dillon Exp $
31 * The unified bootloader passes us a pointer to a preserved copy of
32 * bootstrap/kernel environment variables. We convert them to a dynamic array
33 * of strings later when the VM subsystem is up.
34 * We make these available using sysctl for both in-kernel and
35 * out-of-kernel consumers, as well as the k{get,set,unset,free,test}env()
36 * functions for in-kernel consumers.
38 * Note that the current sysctl infrastructure doesn't allow
39 * dynamic insertion or traversal through handled spaces. Grr.
41 * TODO: implement a sysctl handler to provide the functionality mentioned
45 #include <sys/param.h>
46 #include <sys/kernel.h>
47 #include <sys/libkern.h>
48 #include <sys/malloc.h>
49 #include <sys/spinlock.h>
50 #include <sys/spinlock2.h>
51 #include <sys/systm.h>
52 #include <sys/sysctl.h>
53 #include <sys/libkern.h>
55 /* exported variables */
56 char *kern_envp; /* <sys/systm.h> */
61 struct spinlock kenv_dynlock;
64 MALLOC_DEFINE(M_KENV, "kenv", "kernel environment dynamic storage");
65 #define KENV_DYNMAXNUM 512
66 #define KENV_MAXNAMELEN 128
67 #define KENV_MAXVALLEN 128
69 /* local prototypes */
70 static char *kenv_getstring_dynamic(const char *name, int *idx);
71 static char *kenv_getstring_static(const char *name);
72 static char *kernenv_next(char *cp);
75 * Look up a string in the dynamic environment array. Must be called with
79 kenv_getstring_dynamic(const char *name, int *idx)
85 /* note: kunsetenv() never leaves NULL holes in the array */
86 for (i = 0; (cp = kenv_dynp[i]) != NULL; i++) {
87 if ((strncmp(cp, name, len) == 0) && (cp[len] == '=')) {
97 * Look up a string in the static environment array.
100 kenv_getstring_static(const char *name)
105 for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
106 for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
112 if (!strncmp(name, cp, len) && name[len] == 0)
119 * Look up an environment variable by name.
122 kgetenv(const char *name)
124 char buf[KENV_MAXNAMELEN + 1 + KENV_MAXVALLEN + 1];
128 if (kenv_isdynamic) {
129 spin_lock(&kenv_dynlock);
130 cp = kenv_getstring_dynamic(name, NULL);
133 spin_unlock(&kenv_dynlock);
134 len = strlen(buf) + 1;
135 ret = kmalloc(len, M_KENV, M_WAITOK);
138 spin_unlock(&kenv_dynlock);
142 ret = kenv_getstring_static(name);
147 * Set an environment variable by name.
150 ksetenv(const char *name, const char *value)
152 char *cp, *buf, *oldenv;
153 int namelen, vallen, i;
155 if (kenv_isdynamic) {
156 namelen = strlen(name) + 1;
157 vallen = strlen(value) + 1;
158 if ((namelen > KENV_MAXNAMELEN) || (vallen > KENV_MAXVALLEN))
160 buf = kmalloc(namelen + vallen, M_KENV, M_WAITOK);
161 ksprintf(buf, "%s=%s", name, value);
162 spin_lock(&kenv_dynlock);
163 cp = kenv_getstring_dynamic(name, &i);
165 /* replace existing environment variable */
166 oldenv = kenv_dynp[i];
168 spin_unlock(&kenv_dynlock);
169 kfree(oldenv, M_KENV);
171 /* append new environment variable */
172 for (i = 0; (cp = kenv_dynp[i]) != NULL; i++)
174 /* bounds checking */
175 if (i < 0 || i >= (KENV_DYNMAXNUM - 1)) {
177 spin_unlock(&kenv_dynlock);
181 kenv_dynp[i + 1] = NULL;
182 spin_unlock(&kenv_dynlock);
186 kprintf("WARNING: ksetenv: dynamic array not created yet\n");
192 * Unset an environment variable by name.
195 kunsetenv(const char *name)
200 if (kenv_isdynamic) {
201 spin_lock(&kenv_dynlock);
202 cp = kenv_getstring_dynamic(name, &i);
204 oldenv = kenv_dynp[i];
205 /* move all pointers beyond the unset one down 1 step */
206 for (j = i + 1; kenv_dynp[j] != NULL; j++)
207 kenv_dynp[i++] = kenv_dynp[j];
209 spin_unlock(&kenv_dynlock);
210 kfree(oldenv, M_KENV);
213 spin_unlock(&kenv_dynlock);
216 kprintf("WARNING: kunsetenv: dynamic array not created yet\n");
222 * Free an environment variable that has been copied for a consumer.
232 * Test if an environment variable is defined.
235 ktestenv(const char *name)
239 if (kenv_isdynamic) {
240 spin_lock(&kenv_dynlock);
241 cp = kenv_getstring_dynamic(name, NULL);
242 spin_unlock(&kenv_dynlock);
244 cp = kenv_getstring_static(name);
251 * Return a string value from an environment variable.
254 kgetenv_string(const char *name, char *data, int size)
260 strncpy(data, tmp, size);
269 * Return an integer value from an environment variable.
272 kgetenv_int(const char *name, int *data)
277 rval = kgetenv_quad(name, &tmp);
284 * Return a long value from an environment variable.
287 kgetenv_long(const char *name, long *data)
292 rval = kgetenv_quad(name, &tmp);
299 * Return an unsigned long value from an environment variable.
302 kgetenv_ulong(const char *name, unsigned long *data)
307 rval = kgetenv_quad(name, &tmp);
309 *data = (unsigned long) tmp;
314 * Return a quad_t value from an environment variable.
316 * A single character kmgtKMGT extension multiplies the value
317 * by 1024, 1024*1024, etc.
320 kgetenv_quad(const char *name, quad_t *data)
326 if ((value = kgetenv(name)) == NULL)
329 iv = strtoq(value, &vtp, 0);
352 if ((vtp == value) || (*vtp != '\0')) {
363 * Boottime (static) kernel environment sysctl handler.
366 sysctl_kenv_boot(SYSCTL_HANDLER_ARGS)
368 int *name = (int *)arg1;
369 u_int namelen = arg2;
373 if (kern_envp == NULL)
383 for (i = 0; i < name[0]; i++) {
384 cp = kernenv_next(cp);
392 error = SYSCTL_OUT(req, cp, strlen(cp) + 1);
396 SYSCTL_NODE(_kern, OID_AUTO, environment, CTLFLAG_RD, sysctl_kenv_boot,
397 "boottime (static) kernel environment space");
400 * Find the next entry after the one which (cp) falls within, return a
401 * pointer to its start or NULL if there are no more.
404 kernenv_next(char *cp)
417 * TUNABLE_INT init functions.
420 tunable_int_init(void *data)
422 struct tunable_int *d = (struct tunable_int *)data;
424 TUNABLE_INT_FETCH(d->path, d->var);
428 tunable_long_init(void *data)
430 struct tunable_long *d = (struct tunable_long *)data;
432 TUNABLE_LONG_FETCH(d->path, d->var);
436 tunable_ulong_init(void *data)
438 struct tunable_ulong *d = (struct tunable_ulong *)data;
440 TUNABLE_ULONG_FETCH(d->path, d->var);
444 tunable_quad_init(void *data)
446 struct tunable_quad *d = (struct tunable_quad *)data;
448 TUNABLE_QUAD_FETCH(d->path, d->var);
452 tunable_str_init(void *data)
454 struct tunable_str *d = (struct tunable_str *)data;
456 TUNABLE_STR_FETCH(d->path, d->var, d->size);
460 * Create the dynamic environment array, and copy in the values from the static
461 * environment as passed by the bootloader.
464 kenv_init(void *dummy)
469 kenv_dynp = kmalloc(KENV_DYNMAXNUM * sizeof(char *), M_KENV,
472 /* copy the static environment to our dynamic environment */
473 for (i = 0, cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
474 len = strlen(cp) + 1;
475 if (i < (KENV_DYNMAXNUM - 1)) {
476 kenv_dynp[i] = kmalloc(len, M_KENV, M_WAITOK);
477 strcpy(kenv_dynp[i++], cp);
479 kprintf("WARNING: kenv: exhausted dynamic storage, "
480 "ignoring string %s\n", cp);
484 spin_init(&kenv_dynlock);
487 SYSINIT(kenv, SI_BOOT1_POST, SI_ORDER_ANY, kenv_init, NULL);