xref: /freebsd/sys/kern/kern_environment.c (revision 262e143bd46171a6415a5b28af260a5efa2a3db8)
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 static 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, needed;
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 		done = needed = 0;
103 		sx_slock(&kenv_lock);
104 		for (i = 0; kenvp[i] != NULL; i++) {
105 			len = strlen(kenvp[i]) + 1;
106 			needed += len;
107 			len = min(len, uap->len - done);
108 			/*
109 			 * If called with a NULL or insufficiently large
110 			 * buffer, just keep computing the required size.
111 			 */
112 			if (uap->value != NULL && len > 0) {
113 				error = copyout(kenvp[i], uap->value + done,
114 				    len);
115 				if (error)
116 					break;
117 				done += len;
118 			}
119 		}
120 		sx_sunlock(&kenv_lock);
121 		td->td_retval[0] = ((done == needed) ? 0 : needed);
122 		return (error);
123 	}
124 
125 	if ((uap->what == KENV_SET) ||
126 	    (uap->what == KENV_UNSET)) {
127 		error = suser(td);
128 		if (error)
129 			return (error);
130 	}
131 
132 	name = malloc(KENV_MNAMELEN, M_TEMP, M_WAITOK);
133 
134 	error = copyinstr(uap->name, name, KENV_MNAMELEN, NULL);
135 	if (error)
136 		goto done;
137 
138 	switch (uap->what) {
139 	case KENV_GET:
140 #ifdef MAC
141 		error = mac_check_kenv_get(td->td_ucred, name);
142 		if (error)
143 			goto done;
144 #endif
145 		value = getenv(name);
146 		if (value == NULL) {
147 			error = ENOENT;
148 			goto done;
149 		}
150 		len = strlen(value) + 1;
151 		if (len > uap->len)
152 			len = uap->len;
153 		error = copyout(value, uap->value, len);
154 		freeenv(value);
155 		if (error)
156 			goto done;
157 		td->td_retval[0] = len;
158 		break;
159 	case KENV_SET:
160 		len = uap->len;
161 		if (len < 1) {
162 			error = EINVAL;
163 			goto done;
164 		}
165 		if (len > KENV_MVALLEN)
166 			len = KENV_MVALLEN;
167 		value = malloc(len, M_TEMP, M_WAITOK);
168 		error = copyinstr(uap->value, value, len, NULL);
169 		if (error) {
170 			free(value, M_TEMP);
171 			goto done;
172 		}
173 #ifdef MAC
174 		error = mac_check_kenv_set(td->td_ucred, name, value);
175 		if (error == 0)
176 #endif
177 			setenv(name, value);
178 		free(value, M_TEMP);
179 		break;
180 	case KENV_UNSET:
181 #ifdef MAC
182 		error = mac_check_kenv_unset(td->td_ucred, name);
183 		if (error)
184 			goto done;
185 #endif
186 		error = unsetenv(name);
187 		if (error)
188 			error = ENOENT;
189 		break;
190 	default:
191 		error = EINVAL;
192 		break;
193 	}
194 done:
195 	free(name, M_TEMP);
196 	return (error);
197 }
198 
199 /*
200  * Setup the dynamic kernel environment.
201  */
202 static void
203 init_dynamic_kenv(void *data __unused)
204 {
205 	char *cp;
206 	int len, i;
207 
208 	kenvp = malloc((KENV_SIZE + 1) * sizeof(char *), M_KENV,
209 		M_WAITOK | M_ZERO);
210 	i = 0;
211 	for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
212 		len = strlen(cp) + 1;
213 		kenvp[i] = malloc(len, M_KENV, M_WAITOK);
214 		strcpy(kenvp[i++], cp);
215 	}
216 	kenvp[i] = NULL;
217 
218 	sx_init(&kenv_lock, "kernel environment");
219 	dynamic_kenv = 1;
220 }
221 SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL);
222 
223 void
224 freeenv(char *env)
225 {
226 
227 	if (dynamic_kenv)
228 		free(env, M_KENV);
229 }
230 
231 /*
232  * Internal functions for string lookup.
233  */
234 static char *
235 _getenv_dynamic(const char *name, int *idx)
236 {
237 	char *cp;
238 	int len, i;
239 
240 	sx_assert(&kenv_lock, SX_LOCKED);
241 	len = strlen(name);
242 	for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
243 		if ((strncmp(cp, name, len) == 0) &&
244 		    (cp[len] == '=')) {
245 			if (idx != NULL)
246 				*idx = i;
247 			return (cp + len + 1);
248 		}
249 	}
250 	return (NULL);
251 }
252 
253 static char *
254 _getenv_static(const char *name)
255 {
256 	char *cp, *ep;
257 	int len;
258 
259 	for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
260 		for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
261 			;
262 		if (*ep != '=')
263 			continue;
264 		len = ep - cp;
265 		ep++;
266 		if (!strncmp(name, cp, len) && name[len] == 0)
267 			return (ep);
268 	}
269 	return (NULL);
270 }
271 
272 /*
273  * Look up an environment variable by name.
274  * Return a pointer to the string if found.
275  * The pointer has to be freed with freeenv()
276  * after use.
277  */
278 char *
279 getenv(const char *name)
280 {
281 	char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
282 	char *ret, *cp;
283 	int len;
284 
285 	if (dynamic_kenv) {
286 		sx_slock(&kenv_lock);
287 		cp = _getenv_dynamic(name, NULL);
288 		if (cp != NULL) {
289 			strcpy(buf, cp);
290 			sx_sunlock(&kenv_lock);
291 			len = strlen(buf) + 1;
292 			ret = malloc(len, M_KENV, M_WAITOK);
293 			strcpy(ret, buf);
294 		} else {
295 			sx_sunlock(&kenv_lock);
296 			ret = NULL;
297 		}
298 	} else
299 		ret = _getenv_static(name);
300 	return (ret);
301 }
302 
303 /*
304  * Test if an environment variable is defined.
305  */
306 int
307 testenv(const char *name)
308 {
309 	char *cp;
310 
311 	if (dynamic_kenv) {
312 		sx_slock(&kenv_lock);
313 		cp = _getenv_dynamic(name, NULL);
314 		sx_sunlock(&kenv_lock);
315 	} else
316 		cp = _getenv_static(name);
317 	if (cp != NULL)
318 		return (1);
319 	return (0);
320 }
321 
322 /*
323  * Set an environment variable by name.
324  */
325 int
326 setenv(const char *name, const char *value)
327 {
328 	char *buf, *cp, *oldenv;
329 	int namelen, vallen, i;
330 
331 	KENV_CHECK;
332 
333 	namelen = strlen(name) + 1;
334 	if (namelen > KENV_MNAMELEN)
335 		return (-1);
336 	vallen = strlen(value) + 1;
337 	if (vallen > KENV_MVALLEN)
338 		return (-1);
339 	buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
340 	sprintf(buf, "%s=%s", name, value);
341 
342 	sx_xlock(&kenv_lock);
343 	cp = _getenv_dynamic(name, &i);
344 	if (cp != NULL) {
345 		oldenv = kenvp[i];
346 		kenvp[i] = buf;
347 		sx_xunlock(&kenv_lock);
348 		free(oldenv, M_KENV);
349 	} else {
350 		/* We add the option if it wasn't found */
351 		for (i = 0; (cp = kenvp[i]) != NULL; i++)
352 			;
353 
354 		/* Bounds checking */
355 		if (i < 0 || i >= KENV_SIZE) {
356 			free(buf, M_KENV);
357 			sx_xunlock(&kenv_lock);
358 			return (-1);
359 		}
360 
361 		kenvp[i] = buf;
362 		kenvp[i + 1] = NULL;
363 		sx_xunlock(&kenv_lock);
364 	}
365 	return (0);
366 }
367 
368 /*
369  * Unset an environment variable string.
370  */
371 int
372 unsetenv(const char *name)
373 {
374 	char *cp, *oldenv;
375 	int i, j;
376 
377 	KENV_CHECK;
378 
379 	sx_xlock(&kenv_lock);
380 	cp = _getenv_dynamic(name, &i);
381 	if (cp != NULL) {
382 		oldenv = kenvp[i];
383 		for (j = i + 1; kenvp[j] != NULL; j++)
384 			kenvp[i++] = kenvp[j];
385 		kenvp[i] = NULL;
386 		sx_xunlock(&kenv_lock);
387 		free(oldenv, M_KENV);
388 		return (0);
389 	}
390 	sx_xunlock(&kenv_lock);
391 	return (-1);
392 }
393 
394 /*
395  * Return a string value from an environment variable.
396  */
397 int
398 getenv_string(const char *name, char *data, int size)
399 {
400 	char *tmp;
401 
402 	tmp = getenv(name);
403 	if (tmp != NULL) {
404 		strlcpy(data, tmp, size);
405 		freeenv(tmp);
406 		return (1);
407 	} else
408 		return (0);
409 }
410 
411 /*
412  * Return an integer value from an environment variable.
413  */
414 int
415 getenv_int(const char *name, int *data)
416 {
417 	quad_t tmp;
418 	int rval;
419 
420 	rval = getenv_quad(name, &tmp);
421 	if (rval)
422 		*data = (int) tmp;
423 	return (rval);
424 }
425 
426 /*
427  * Return a long value from an environment variable.
428  */
429 long
430 getenv_long(const char *name, long *data)
431 {
432 	quad_t tmp;
433 	long rval;
434 
435 	rval = getenv_quad(name, &tmp);
436 	if (rval)
437 		*data = (long) tmp;
438 	return (rval);
439 }
440 
441 /*
442  * Return an unsigned long value from an environment variable.
443  */
444 unsigned long
445 getenv_ulong(const char *name, unsigned long *data)
446 {
447 	quad_t tmp;
448 	long rval;
449 
450 	rval = getenv_quad(name, &tmp);
451 	if (rval)
452 		*data = (unsigned long) tmp;
453 	return (rval);
454 }
455 
456 /*
457  * Return a quad_t value from an environment variable.
458  */
459 int
460 getenv_quad(const char *name, quad_t *data)
461 {
462 	char	*value;
463 	char	*vtp;
464 	quad_t	iv;
465 
466 	value = getenv(name);
467 	if (value == NULL)
468 		return (0);
469 	iv = strtoq(value, &vtp, 0);
470 	if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0')) {
471 		freeenv(value);
472 		return (0);
473 	}
474 	switch (vtp[0]) {
475 	case 't': case 'T':
476 		iv *= 1024;
477 	case 'g': case 'G':
478 		iv *= 1024;
479 	case 'm': case 'M':
480 		iv *= 1024;
481 	case 'k': case 'K':
482 		iv *= 1024;
483 	case '\0':
484 		break;
485 	default:
486 		freeenv(value);
487 		return (0);
488 	}
489 	*data = iv;
490 	freeenv(value);
491 	return (1);
492 }
493 
494 /*
495  * Find the next entry after the one which (cp) falls within, return a
496  * pointer to its start or NULL if there are no more.
497  */
498 static char *
499 kernenv_next(char *cp)
500 {
501 
502 	if (cp != NULL) {
503 		while (*cp != 0)
504 			cp++;
505 		cp++;
506 		if (*cp == 0)
507 			cp = NULL;
508 	}
509 	return (cp);
510 }
511 
512 void
513 tunable_int_init(void *data)
514 {
515 	struct tunable_int *d = (struct tunable_int *)data;
516 
517 	TUNABLE_INT_FETCH(d->path, d->var);
518 }
519 
520 void
521 tunable_long_init(void *data)
522 {
523 	struct tunable_long *d = (struct tunable_long *)data;
524 
525 	TUNABLE_LONG_FETCH(d->path, d->var);
526 }
527 
528 void
529 tunable_ulong_init(void *data)
530 {
531 	struct tunable_ulong *d = (struct tunable_ulong *)data;
532 
533 	TUNABLE_ULONG_FETCH(d->path, d->var);
534 }
535 
536 void
537 tunable_str_init(void *data)
538 {
539 	struct tunable_str *d = (struct tunable_str *)data;
540 
541 	TUNABLE_STR_FETCH(d->path, d->var, d->size);
542 }
543