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