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