xref: /freebsd/sys/kern/kern_environment.c (revision 7660b554bc59a07be0431c17e0e33815818baa69)
1 /*-
2  * Copyright (c) 1998 Michael Smith
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  */
26 
27 /*
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.
31  *
32  * We make these available through the kenv(2) syscall for userland
33  * and through getenv()/freeenv() setenv() unsetenv() testenv() for
34  * the kernel.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "opt_mac.h"
41 
42 #include <sys/types.h>
43 #include <sys/param.h>
44 #include <sys/proc.h>
45 #include <sys/queue.h>
46 #include <sys/lock.h>
47 #include <sys/mac.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
50 #include <sys/kernel.h>
51 #include <sys/sx.h>
52 #include <sys/systm.h>
53 #include <sys/sysent.h>
54 #include <sys/sysproto.h>
55 #include <sys/libkern.h>
56 #include <sys/kenv.h>
57 
58 MALLOC_DEFINE(M_KENV, "kenv", "kernel environment");
59 
60 #define KENV_SIZE	512	/* Maximum number of environment strings */
61 
62 /* pointer to the static environment */
63 char		*kern_envp;
64 static char	*kernenv_next(char *);
65 
66 /* dynamic environment variables */
67 char		**kenvp;
68 struct sx	kenv_lock;
69 
70 /*
71  * No need to protect this with a mutex
72  * since SYSINITS are single threaded.
73  */
74 int	dynamic_kenv = 0;
75 
76 #define KENV_CHECK	if (!dynamic_kenv) \
77 			    panic("%s: called before SI_SUB_KMEM", __func__)
78 
79 int
80 kenv(td, uap)
81 	struct thread *td;
82 	struct kenv_args /* {
83 		int what;
84 		const char *name;
85 		char *value;
86 		int len;
87 	} */ *uap;
88 {
89 	char *name, *value;
90 	size_t len, done;
91 	int error, i;
92 
93 	KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = 0"));
94 
95 	error = 0;
96 	if (uap->what == KENV_DUMP) {
97 #ifdef MAC
98 		error = mac_check_kenv_dump(td->td_ucred);
99 		if (error)
100 			return (error);
101 #endif
102 		len = 0;
103 		/* Return the size if called with a NULL buffer */
104 		if (uap->value == NULL) {
105 			sx_slock(&kenv_lock);
106 			for (i = 0; kenvp[i] != NULL; i++)
107 				len += strlen(kenvp[i]) + 1;
108 			sx_sunlock(&kenv_lock);
109 			td->td_retval[0] = len;
110 			return (0);
111 		}
112 		done = 0;
113 		sx_slock(&kenv_lock);
114 		for (i = 0; kenvp[i] != NULL && done < uap->len; i++) {
115 			len = min(strlen(kenvp[i]) + 1, uap->len - done);
116 			error = copyout(kenvp[i], uap->value + done,
117 			    len);
118 			if (error) {
119 				sx_sunlock(&kenv_lock);
120 				return (error);
121 			}
122 			done += len;
123 		}
124 		sx_sunlock(&kenv_lock);
125 		return (0);
126 	}
127 
128 	if ((uap->what == KENV_SET) ||
129 	    (uap->what == KENV_UNSET)) {
130 		error = suser(td);
131 		if (error)
132 			return (error);
133 	}
134 
135 	name = malloc(KENV_MNAMELEN, M_TEMP, M_WAITOK);
136 
137 	error = copyinstr(uap->name, name, KENV_MNAMELEN, NULL);
138 	if (error)
139 		goto done;
140 
141 	switch (uap->what) {
142 	case KENV_GET:
143 #ifdef MAC
144 		error = mac_check_kenv_get(td->td_ucred, name);
145 		if (error)
146 			goto done;
147 #endif
148 		value = getenv(name);
149 		if (value == NULL) {
150 			error = ENOENT;
151 			goto done;
152 		}
153 		len = strlen(value) + 1;
154 		if (len > uap->len)
155 			len = uap->len;
156 		error = copyout(value, uap->value, len);
157 		freeenv(value);
158 		if (error)
159 			goto done;
160 		td->td_retval[0] = len;
161 		break;
162 	case KENV_SET:
163 		len = uap->len;
164 		if (len < 1) {
165 			error = EINVAL;
166 			goto done;
167 		}
168 		if (len > KENV_MVALLEN)
169 			len = KENV_MVALLEN;
170 		value = malloc(len, M_TEMP, M_WAITOK);
171 		error = copyinstr(uap->value, value, len, NULL);
172 		if (error) {
173 			free(value, M_TEMP);
174 			goto done;
175 		}
176 #ifdef MAC
177 		error = mac_check_kenv_set(td->td_ucred, name, value);
178 		if (error == 0)
179 #endif
180 			setenv(name, value);
181 		free(value, M_TEMP);
182 		break;
183 	case KENV_UNSET:
184 #ifdef MAC
185 		error = mac_check_kenv_unset(td->td_ucred, name);
186 		if (error)
187 			goto done;
188 #endif
189 		error = unsetenv(name);
190 		if (error)
191 			error = ENOENT;
192 		break;
193 	default:
194 		error = EINVAL;
195 		break;
196 	}
197 done:
198 	free(name, M_TEMP);
199 	return (error);
200 }
201 
202 /*
203  * Setup the dynamic kernel environment.
204  */
205 static void
206 init_dynamic_kenv(void *data __unused)
207 {
208 	char *cp;
209 	int len, i;
210 
211 	kenvp = malloc(KENV_SIZE * sizeof(char *), M_KENV, M_WAITOK | M_ZERO);
212 	i = 0;
213 	for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
214 		len = strlen(cp) + 1;
215 		kenvp[i] = malloc(len, M_KENV, M_WAITOK);
216 		strcpy(kenvp[i++], cp);
217 	}
218 	kenvp[i] = NULL;
219 
220 	sx_init(&kenv_lock, "kernel environment");
221 	dynamic_kenv = 1;
222 }
223 SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL);
224 
225 void
226 freeenv(char *env)
227 {
228 
229 	if (dynamic_kenv)
230 		free(env, M_KENV);
231 }
232 
233 /*
234  * Internal functions for string lookup.
235  */
236 static char *
237 _getenv_dynamic(const char *name, int *idx)
238 {
239 	char *cp;
240 	int len, i;
241 
242 	sx_assert(&kenv_lock, SX_LOCKED);
243 	len = strlen(name);
244 	for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
245 		if ((cp[len] == '=') &&
246 		    (strncmp(cp, name, len) == 0)) {
247 			if (idx != NULL)
248 				*idx = i;
249 			return (cp + len + 1);
250 		}
251 	}
252 	return (NULL);
253 }
254 
255 static char *
256 _getenv_static(const char *name)
257 {
258 	char *cp, *ep;
259 	int len;
260 
261 	for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
262 		for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
263 			;
264 		if (*ep != '=')
265 			continue;
266 		len = ep - cp;
267 		ep++;
268 		if (!strncmp(name, cp, len) && name[len] == 0)
269 			return (ep);
270 	}
271 	return (NULL);
272 }
273 
274 /*
275  * Look up an environment variable by name.
276  * Return a pointer to the string if found.
277  * The pointer has to be freed with freeenv()
278  * after use.
279  */
280 char *
281 getenv(const char *name)
282 {
283 	char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
284 	char *ret, *cp;
285 	int len;
286 
287 	if (dynamic_kenv) {
288 		sx_slock(&kenv_lock);
289 		cp = _getenv_dynamic(name, NULL);
290 		if (cp != NULL) {
291 			strcpy(buf, cp);
292 			sx_sunlock(&kenv_lock);
293 			len = strlen(buf) + 1;
294 			ret = malloc(len, M_KENV, M_WAITOK);
295 			strcpy(ret, buf);
296 		} else {
297 			sx_sunlock(&kenv_lock);
298 			ret = NULL;
299 		}
300 	} else
301 		ret = _getenv_static(name);
302 	return (ret);
303 }
304 
305 /*
306  * Test if an environment variable is defined.
307  */
308 int
309 testenv(const char *name)
310 {
311 	char *cp;
312 
313 	if (dynamic_kenv) {
314 		sx_slock(&kenv_lock);
315 		cp = _getenv_dynamic(name, NULL);
316 		sx_sunlock(&kenv_lock);
317 	} else
318 		cp = _getenv_static(name);
319 	if (cp != NULL)
320 		return (1);
321 	return (0);
322 }
323 
324 /*
325  * Set an environment variable by name.
326  */
327 int
328 setenv(const char *name, const char *value)
329 {
330 	char *buf, *cp, *oldenv;
331 	int namelen, vallen, i;
332 
333 	KENV_CHECK;
334 
335 	namelen = strlen(name) + 1;
336 	if (namelen > KENV_MNAMELEN)
337 		return (-1);
338 	vallen = strlen(value) + 1;
339 	if (vallen > KENV_MVALLEN)
340 		return (-1);
341 	buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
342 	sprintf(buf, "%s=%s", name, value);
343 
344 	sx_xlock(&kenv_lock);
345 	cp = _getenv_dynamic(name, &i);
346 	if (cp != NULL) {
347 		oldenv = kenvp[i];
348 		kenvp[i] = buf;
349 		sx_xunlock(&kenv_lock);
350 		free(oldenv, M_KENV);
351 	} else {
352 		/* We add the option if it wasn't found */
353 		for (i = 0; (cp = kenvp[i]) != NULL; i++)
354 			;
355 		kenvp[i] = buf;
356 		kenvp[i + 1] = NULL;
357 		sx_xunlock(&kenv_lock);
358 	}
359 	return (0);
360 }
361 
362 /*
363  * Unset an environment variable string.
364  */
365 int
366 unsetenv(const char *name)
367 {
368 	char *cp, *oldenv;
369 	int i, j;
370 
371 	KENV_CHECK;
372 
373 	sx_xlock(&kenv_lock);
374 	cp = _getenv_dynamic(name, &i);
375 	if (cp != NULL) {
376 		oldenv = kenvp[i];
377 		for (j = i + 1; kenvp[j] != NULL; j++)
378 			kenvp[i++] = kenvp[j];
379 		kenvp[i] = NULL;
380 		sx_xunlock(&kenv_lock);
381 		free(oldenv, M_KENV);
382 		return (0);
383 	}
384 	sx_xunlock(&kenv_lock);
385 	return (-1);
386 }
387 
388 /*
389  * Return a string value from an environment variable.
390  */
391 int
392 getenv_string(const char *name, char *data, int size)
393 {
394 	char *tmp;
395 
396 	tmp = getenv(name);
397 	if (tmp != NULL) {
398 		strlcpy(data, tmp, size);
399 		freeenv(tmp);
400 		return (1);
401 	} else
402 		return (0);
403 }
404 
405 /*
406  * Return an integer value from an environment variable.
407  */
408 int
409 getenv_int(const char *name, int *data)
410 {
411 	quad_t tmp;
412 	int rval;
413 
414 	rval = getenv_quad(name, &tmp);
415 	if (rval)
416 		*data = (int) tmp;
417 	return (rval);
418 }
419 
420 /*
421  * Return a quad_t value from an environment variable.
422  */
423 int
424 getenv_quad(const char *name, quad_t *data)
425 {
426 	char	*value;
427 	char	*vtp;
428 	quad_t	iv;
429 
430 	value = getenv(name);
431 	if (value == NULL)
432 		return (0);
433 	iv = strtoq(value, &vtp, 0);
434 	if ((vtp == value) || (*vtp != '\0')) {
435 		freeenv(value);
436 		return (0);
437 	}
438 	freeenv(value);
439 	*data = iv;
440 	return (1);
441 }
442 
443 /*
444  * Find the next entry after the one which (cp) falls within, return a
445  * pointer to its start or NULL if there are no more.
446  */
447 static char *
448 kernenv_next(char *cp)
449 {
450 
451 	if (cp != NULL) {
452 		while (*cp != 0)
453 			cp++;
454 		cp++;
455 		if (*cp == 0)
456 			cp = NULL;
457 	}
458 	return (cp);
459 }
460 
461 void
462 tunable_int_init(void *data)
463 {
464 	struct tunable_int *d = (struct tunable_int *)data;
465 
466 	TUNABLE_INT_FETCH(d->path, d->var);
467 }
468 
469 void
470 tunable_quad_init(void *data)
471 {
472 	struct tunable_quad *d = (struct tunable_quad *)data;
473 
474 	TUNABLE_QUAD_FETCH(d->path, d->var);
475 }
476 
477 void
478 tunable_str_init(void *data)
479 {
480 	struct tunable_str *d = (struct tunable_str *)data;
481 
482 	TUNABLE_STR_FETCH(d->path, d->var, d->size);
483 }
484