xref: /freebsd/sys/kern/kern_environment.c (revision 87569f75a91f298c52a71823c04d41cf53c88889)
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 		if (i < KENV_SIZE) {
214 			kenvp[i] = malloc(len, M_KENV, M_WAITOK);
215 			strcpy(kenvp[i++], cp);
216 		} else
217 			printf(
218 			    "WARNING: too many kenv strings, ignoring %s\n",
219 			    cp);
220 	}
221 	kenvp[i] = NULL;
222 
223 	sx_init(&kenv_lock, "kernel environment");
224 	dynamic_kenv = 1;
225 }
226 SYSINIT(kenv, SI_SUB_KMEM, SI_ORDER_ANY, init_dynamic_kenv, NULL);
227 
228 void
229 freeenv(char *env)
230 {
231 
232 	if (dynamic_kenv)
233 		free(env, M_KENV);
234 }
235 
236 /*
237  * Internal functions for string lookup.
238  */
239 static char *
240 _getenv_dynamic(const char *name, int *idx)
241 {
242 	char *cp;
243 	int len, i;
244 
245 	sx_assert(&kenv_lock, SX_LOCKED);
246 	len = strlen(name);
247 	for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
248 		if ((strncmp(cp, name, len) == 0) &&
249 		    (cp[len] == '=')) {
250 			if (idx != NULL)
251 				*idx = i;
252 			return (cp + len + 1);
253 		}
254 	}
255 	return (NULL);
256 }
257 
258 static char *
259 _getenv_static(const char *name)
260 {
261 	char *cp, *ep;
262 	int len;
263 
264 	for (cp = kern_envp; cp != NULL; cp = kernenv_next(cp)) {
265 		for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
266 			;
267 		if (*ep != '=')
268 			continue;
269 		len = ep - cp;
270 		ep++;
271 		if (!strncmp(name, cp, len) && name[len] == 0)
272 			return (ep);
273 	}
274 	return (NULL);
275 }
276 
277 /*
278  * Look up an environment variable by name.
279  * Return a pointer to the string if found.
280  * The pointer has to be freed with freeenv()
281  * after use.
282  */
283 char *
284 getenv(const char *name)
285 {
286 	char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
287 	char *ret, *cp;
288 	int len;
289 
290 	if (dynamic_kenv) {
291 		sx_slock(&kenv_lock);
292 		cp = _getenv_dynamic(name, NULL);
293 		if (cp != NULL) {
294 			strcpy(buf, cp);
295 			sx_sunlock(&kenv_lock);
296 			len = strlen(buf) + 1;
297 			ret = malloc(len, M_KENV, M_WAITOK);
298 			strcpy(ret, buf);
299 		} else {
300 			sx_sunlock(&kenv_lock);
301 			ret = NULL;
302 		}
303 	} else
304 		ret = _getenv_static(name);
305 	return (ret);
306 }
307 
308 /*
309  * Test if an environment variable is defined.
310  */
311 int
312 testenv(const char *name)
313 {
314 	char *cp;
315 
316 	if (dynamic_kenv) {
317 		sx_slock(&kenv_lock);
318 		cp = _getenv_dynamic(name, NULL);
319 		sx_sunlock(&kenv_lock);
320 	} else
321 		cp = _getenv_static(name);
322 	if (cp != NULL)
323 		return (1);
324 	return (0);
325 }
326 
327 /*
328  * Set an environment variable by name.
329  */
330 int
331 setenv(const char *name, const char *value)
332 {
333 	char *buf, *cp, *oldenv;
334 	int namelen, vallen, i;
335 
336 	KENV_CHECK;
337 
338 	namelen = strlen(name) + 1;
339 	if (namelen > KENV_MNAMELEN)
340 		return (-1);
341 	vallen = strlen(value) + 1;
342 	if (vallen > KENV_MVALLEN)
343 		return (-1);
344 	buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
345 	sprintf(buf, "%s=%s", name, value);
346 
347 	sx_xlock(&kenv_lock);
348 	cp = _getenv_dynamic(name, &i);
349 	if (cp != NULL) {
350 		oldenv = kenvp[i];
351 		kenvp[i] = buf;
352 		sx_xunlock(&kenv_lock);
353 		free(oldenv, M_KENV);
354 	} else {
355 		/* We add the option if it wasn't found */
356 		for (i = 0; (cp = kenvp[i]) != NULL; i++)
357 			;
358 
359 		/* Bounds checking */
360 		if (i < 0 || i >= KENV_SIZE) {
361 			free(buf, M_KENV);
362 			sx_xunlock(&kenv_lock);
363 			return (-1);
364 		}
365 
366 		kenvp[i] = buf;
367 		kenvp[i + 1] = NULL;
368 		sx_xunlock(&kenv_lock);
369 	}
370 	return (0);
371 }
372 
373 /*
374  * Unset an environment variable string.
375  */
376 int
377 unsetenv(const char *name)
378 {
379 	char *cp, *oldenv;
380 	int i, j;
381 
382 	KENV_CHECK;
383 
384 	sx_xlock(&kenv_lock);
385 	cp = _getenv_dynamic(name, &i);
386 	if (cp != NULL) {
387 		oldenv = kenvp[i];
388 		for (j = i + 1; kenvp[j] != NULL; j++)
389 			kenvp[i++] = kenvp[j];
390 		kenvp[i] = NULL;
391 		sx_xunlock(&kenv_lock);
392 		free(oldenv, M_KENV);
393 		return (0);
394 	}
395 	sx_xunlock(&kenv_lock);
396 	return (-1);
397 }
398 
399 /*
400  * Return a string value from an environment variable.
401  */
402 int
403 getenv_string(const char *name, char *data, int size)
404 {
405 	char *tmp;
406 
407 	tmp = getenv(name);
408 	if (tmp != NULL) {
409 		strlcpy(data, tmp, size);
410 		freeenv(tmp);
411 		return (1);
412 	} else
413 		return (0);
414 }
415 
416 /*
417  * Return an integer value from an environment variable.
418  */
419 int
420 getenv_int(const char *name, int *data)
421 {
422 	quad_t tmp;
423 	int rval;
424 
425 	rval = getenv_quad(name, &tmp);
426 	if (rval)
427 		*data = (int) tmp;
428 	return (rval);
429 }
430 
431 /*
432  * Return a long value from an environment variable.
433  */
434 long
435 getenv_long(const char *name, long *data)
436 {
437 	quad_t tmp;
438 	long rval;
439 
440 	rval = getenv_quad(name, &tmp);
441 	if (rval)
442 		*data = (long) tmp;
443 	return (rval);
444 }
445 
446 /*
447  * Return an unsigned long value from an environment variable.
448  */
449 unsigned long
450 getenv_ulong(const char *name, unsigned long *data)
451 {
452 	quad_t tmp;
453 	long rval;
454 
455 	rval = getenv_quad(name, &tmp);
456 	if (rval)
457 		*data = (unsigned long) tmp;
458 	return (rval);
459 }
460 
461 /*
462  * Return a quad_t value from an environment variable.
463  */
464 int
465 getenv_quad(const char *name, quad_t *data)
466 {
467 	char	*value;
468 	char	*vtp;
469 	quad_t	iv;
470 
471 	value = getenv(name);
472 	if (value == NULL)
473 		return (0);
474 	iv = strtoq(value, &vtp, 0);
475 	if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0')) {
476 		freeenv(value);
477 		return (0);
478 	}
479 	switch (vtp[0]) {
480 	case 't': case 'T':
481 		iv *= 1024;
482 	case 'g': case 'G':
483 		iv *= 1024;
484 	case 'm': case 'M':
485 		iv *= 1024;
486 	case 'k': case 'K':
487 		iv *= 1024;
488 	case '\0':
489 		break;
490 	default:
491 		freeenv(value);
492 		return (0);
493 	}
494 	*data = iv;
495 	freeenv(value);
496 	return (1);
497 }
498 
499 /*
500  * Find the next entry after the one which (cp) falls within, return a
501  * pointer to its start or NULL if there are no more.
502  */
503 static char *
504 kernenv_next(char *cp)
505 {
506 
507 	if (cp != NULL) {
508 		while (*cp != 0)
509 			cp++;
510 		cp++;
511 		if (*cp == 0)
512 			cp = NULL;
513 	}
514 	return (cp);
515 }
516 
517 void
518 tunable_int_init(void *data)
519 {
520 	struct tunable_int *d = (struct tunable_int *)data;
521 
522 	TUNABLE_INT_FETCH(d->path, d->var);
523 }
524 
525 void
526 tunable_long_init(void *data)
527 {
528 	struct tunable_long *d = (struct tunable_long *)data;
529 
530 	TUNABLE_LONG_FETCH(d->path, d->var);
531 }
532 
533 void
534 tunable_ulong_init(void *data)
535 {
536 	struct tunable_ulong *d = (struct tunable_ulong *)data;
537 
538 	TUNABLE_ULONG_FETCH(d->path, d->var);
539 }
540 
541 void
542 tunable_str_init(void *data)
543 {
544 	struct tunable_str *d = (struct tunable_str *)data;
545 
546 	TUNABLE_STR_FETCH(d->path, d->var, d->size);
547 }
548