xref: /freebsd/sys/kern/kern_environment.c (revision fd253945ac76a54ff9c11cf02f5458561f711866)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 1998 Michael Smith
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * The unified bootloader passes us a pointer to a preserved copy of
31  * bootstrap/kernel environment variables.  We convert them to a
32  * dynamic array of strings later when the VM subsystem is up.
33  *
34  * We make these available through the kenv(2) syscall for userland
35  * and through kern_getenv()/freeenv() kern_setenv() kern_unsetenv() testenv() for
36  * the kernel.
37  */
38 
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
41 
42 #include <sys/param.h>
43 #include <sys/proc.h>
44 #include <sys/queue.h>
45 #include <sys/lock.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
48 #include <sys/priv.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
51 #include <sys/sysent.h>
52 #include <sys/sysproto.h>
53 #include <sys/libkern.h>
54 #include <sys/kenv.h>
55 #include <sys/limits.h>
56 
57 #include <security/mac/mac_framework.h>
58 
59 static char *_getenv_dynamic_locked(const char *name, int *idx);
60 static char *_getenv_dynamic(const char *name, int *idx);
61 
62 static MALLOC_DEFINE(M_KENV, "kenv", "kernel environment");
63 
64 #define KENV_SIZE	512	/* Maximum number of environment strings */
65 
66 /* pointer to the config-generated static environment */
67 char		*kern_envp;
68 
69 /* pointer to the md-static environment */
70 char		*md_envp;
71 static int	md_env_len;
72 static int	md_env_pos;
73 
74 static char	*kernenv_next(char *);
75 
76 /* dynamic environment variables */
77 char		**kenvp;
78 struct mtx	kenv_lock;
79 
80 /*
81  * No need to protect this with a mutex since SYSINITS are single threaded.
82  */
83 bool	dynamic_kenv;
84 
85 #define KENV_CHECK	if (!dynamic_kenv) \
86 			    panic("%s: called before SI_SUB_KMEM", __func__)
87 
88 int
89 sys_kenv(td, uap)
90 	struct thread *td;
91 	struct kenv_args /* {
92 		int what;
93 		const char *name;
94 		char *value;
95 		int len;
96 	} */ *uap;
97 {
98 	char *name, *value, *buffer = NULL;
99 	size_t len, done, needed, buflen;
100 	int error, i;
101 
102 	KASSERT(dynamic_kenv, ("kenv: dynamic_kenv = false"));
103 
104 	error = 0;
105 	if (uap->what == KENV_DUMP) {
106 #ifdef MAC
107 		error = mac_kenv_check_dump(td->td_ucred);
108 		if (error)
109 			return (error);
110 #endif
111 		done = needed = 0;
112 		buflen = uap->len;
113 		if (buflen > KENV_SIZE * (KENV_MNAMELEN + KENV_MVALLEN + 2))
114 			buflen = KENV_SIZE * (KENV_MNAMELEN +
115 			    KENV_MVALLEN + 2);
116 		if (uap->len > 0 && uap->value != NULL)
117 			buffer = malloc(buflen, M_TEMP, M_WAITOK|M_ZERO);
118 		mtx_lock(&kenv_lock);
119 		for (i = 0; kenvp[i] != NULL; i++) {
120 			len = strlen(kenvp[i]) + 1;
121 			needed += len;
122 			len = min(len, buflen - done);
123 			/*
124 			 * If called with a NULL or insufficiently large
125 			 * buffer, just keep computing the required size.
126 			 */
127 			if (uap->value != NULL && buffer != NULL && len > 0) {
128 				bcopy(kenvp[i], buffer + done, len);
129 				done += len;
130 			}
131 		}
132 		mtx_unlock(&kenv_lock);
133 		if (buffer != NULL) {
134 			error = copyout(buffer, uap->value, done);
135 			free(buffer, M_TEMP);
136 		}
137 		td->td_retval[0] = ((done == needed) ? 0 : needed);
138 		return (error);
139 	}
140 
141 	switch (uap->what) {
142 	case KENV_SET:
143 		error = priv_check(td, PRIV_KENV_SET);
144 		if (error)
145 			return (error);
146 		break;
147 
148 	case KENV_UNSET:
149 		error = priv_check(td, PRIV_KENV_UNSET);
150 		if (error)
151 			return (error);
152 		break;
153 	}
154 
155 	name = malloc(KENV_MNAMELEN + 1, M_TEMP, M_WAITOK);
156 
157 	error = copyinstr(uap->name, name, KENV_MNAMELEN + 1, NULL);
158 	if (error)
159 		goto done;
160 
161 	switch (uap->what) {
162 	case KENV_GET:
163 #ifdef MAC
164 		error = mac_kenv_check_get(td->td_ucred, name);
165 		if (error)
166 			goto done;
167 #endif
168 		value = kern_getenv(name);
169 		if (value == NULL) {
170 			error = ENOENT;
171 			goto done;
172 		}
173 		len = strlen(value) + 1;
174 		if (len > uap->len)
175 			len = uap->len;
176 		error = copyout(value, uap->value, len);
177 		freeenv(value);
178 		if (error)
179 			goto done;
180 		td->td_retval[0] = len;
181 		break;
182 	case KENV_SET:
183 		len = uap->len;
184 		if (len < 1) {
185 			error = EINVAL;
186 			goto done;
187 		}
188 		if (len > KENV_MVALLEN + 1)
189 			len = KENV_MVALLEN + 1;
190 		value = malloc(len, M_TEMP, M_WAITOK);
191 		error = copyinstr(uap->value, value, len, NULL);
192 		if (error) {
193 			free(value, M_TEMP);
194 			goto done;
195 		}
196 #ifdef MAC
197 		error = mac_kenv_check_set(td->td_ucred, name, value);
198 		if (error == 0)
199 #endif
200 			kern_setenv(name, value);
201 		free(value, M_TEMP);
202 		break;
203 	case KENV_UNSET:
204 #ifdef MAC
205 		error = mac_kenv_check_unset(td->td_ucred, name);
206 		if (error)
207 			goto done;
208 #endif
209 		error = kern_unsetenv(name);
210 		if (error)
211 			error = ENOENT;
212 		break;
213 	default:
214 		error = EINVAL;
215 		break;
216 	}
217 done:
218 	free(name, M_TEMP);
219 	return (error);
220 }
221 
222 /*
223  * Populate the initial kernel environment.
224  *
225  * This is called very early in MD startup, either to provide a copy of the
226  * environment obtained from a boot loader, or to provide an empty buffer into
227  * which MD code can store an initial environment using kern_setenv() calls.
228  *
229  * kern_envp is set to the static_env generated by config(8).  This implements
230  * the env keyword described in config(5).
231  *
232  * If len is non-zero, the caller is providing an empty buffer.  The caller will
233  * subsequently use kern_setenv() to add up to len bytes of initial environment
234  * before the dynamic environment is available.
235  *
236  * If len is zero, the caller is providing a pre-loaded buffer containing
237  * environment strings.  Additional strings cannot be added until the dynamic
238  * environment is available.  The memory pointed to must remain stable at least
239  * until sysinit runs init_dynamic_kenv() and preferably until after SI_SUB_KMEM
240  * is finished so that subr_hints routines may continue to use it until the
241  * environments have been fully merged at the end of the pass.  If no initial
242  * environment is available from the boot loader, passing a NULL pointer allows
243  * the static_env to be installed if it is configured.  In this case, any call
244  * to kern_setenv() prior to the setup of the dynamic environment will result in
245  * a panic.
246  */
247 void
248 init_static_kenv(char *buf, size_t len)
249 {
250 	char *eval;
251 
252 	KASSERT(!dynamic_kenv, ("kenv: dynamic_kenv already initialized"));
253 	/*
254 	 * Give the static environment a chance to disable the loader(8)
255 	 * environment first.  This is done with loader_env.disabled=1.
256 	 *
257 	 * static_env and static_hints may both be disabled, but in slightly
258 	 * different ways.  For static_env, we just don't setup kern_envp and
259 	 * it's as if a static env wasn't even provided.  For static_hints,
260 	 * we effectively zero out the buffer to stop the rest of the kernel
261 	 * from being able to use it.
262 	 *
263 	 * We're intentionally setting this up so that static_hints.disabled may
264 	 * be specified in either the MD env or the static env. This keeps us
265 	 * consistent in our new world view.
266 	 *
267 	 * As a warning, the static environment may not be disabled in any way
268 	 * if the static environment has disabled the loader environment.
269 	 */
270 	kern_envp = static_env;
271 	eval = kern_getenv("loader_env.disabled");
272 	if (eval == NULL || strcmp(eval, "1") != 0) {
273 		md_envp = buf;
274 		md_env_len = len;
275 		md_env_pos = 0;
276 
277 		eval = kern_getenv("static_env.disabled");
278 		if (eval != NULL && strcmp(eval, "1") == 0)
279 			*kern_envp = '\0';
280 	}
281 	eval = kern_getenv("static_hints.disabled");
282 	if (eval != NULL && strcmp(eval, "1") == 0)
283 		*static_hints = '\0';
284 }
285 
286 static void
287 init_dynamic_kenv_from(char *init_env, int *curpos)
288 {
289 	char *cp, *cpnext, *eqpos, *found;
290 	size_t len;
291 	int i;
292 
293 	if (init_env && *init_env != '\0') {
294 		found = NULL;
295 		i = *curpos;
296 		for (cp = init_env; cp != NULL; cp = cpnext) {
297 			cpnext = kernenv_next(cp);
298 			len = strlen(cp) + 1;
299 			if (len > KENV_MNAMELEN + 1 + KENV_MVALLEN + 1) {
300 				printf(
301 				"WARNING: too long kenv string, ignoring %s\n",
302 				    cp);
303 				goto sanitize;
304 			}
305 			eqpos = strchr(cp, '=');
306 			if (eqpos == NULL) {
307 				printf(
308 				"WARNING: malformed static env value, ignoring %s\n",
309 				    cp);
310 				goto sanitize;
311 			}
312 			*eqpos = 0;
313 			/*
314 			 * De-dupe the environment as we go.  We don't add the
315 			 * duplicated assignments because config(8) will flip
316 			 * the order of the static environment around to make
317 			 * kernel processing match the order of specification
318 			 * in the kernel config.
319 			 */
320 			found = _getenv_dynamic_locked(cp, NULL);
321 			*eqpos = '=';
322 			if (found != NULL)
323 				goto sanitize;
324 			if (i > KENV_SIZE) {
325 				printf(
326 				"WARNING: too many kenv strings, ignoring %s\n",
327 				    cp);
328 				goto sanitize;
329 			}
330 
331 			kenvp[i] = malloc(len, M_KENV, M_WAITOK);
332 			strcpy(kenvp[i++], cp);
333 sanitize:
334 			explicit_bzero(cp, len - 1);
335 		}
336 		*curpos = i;
337 	}
338 }
339 
340 /*
341  * Setup the dynamic kernel environment.
342  */
343 static void
344 init_dynamic_kenv(void *data __unused)
345 {
346 	int dynamic_envpos;
347 
348 	kenvp = malloc((KENV_SIZE + 1) * sizeof(char *), M_KENV,
349 		M_WAITOK | M_ZERO);
350 
351 	dynamic_envpos = 0;
352 	init_dynamic_kenv_from(md_envp, &dynamic_envpos);
353 	init_dynamic_kenv_from(kern_envp, &dynamic_envpos);
354 	kenvp[dynamic_envpos] = NULL;
355 
356 	mtx_init(&kenv_lock, "kernel environment", NULL, MTX_DEF);
357 	dynamic_kenv = true;
358 }
359 SYSINIT(kenv, SI_SUB_KMEM + 1, SI_ORDER_FIRST, init_dynamic_kenv, NULL);
360 
361 void
362 freeenv(char *env)
363 {
364 
365 	if (dynamic_kenv && env != NULL) {
366 		explicit_bzero(env, strlen(env));
367 		free(env, M_KENV);
368 	}
369 }
370 
371 /*
372  * Internal functions for string lookup.
373  */
374 static char *
375 _getenv_dynamic_locked(const char *name, int *idx)
376 {
377 	char *cp;
378 	int len, i;
379 
380 	len = strlen(name);
381 	for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
382 		if ((strncmp(cp, name, len) == 0) &&
383 		    (cp[len] == '=')) {
384 			if (idx != NULL)
385 				*idx = i;
386 			return (cp + len + 1);
387 		}
388 	}
389 	return (NULL);
390 }
391 
392 static char *
393 _getenv_dynamic(const char *name, int *idx)
394 {
395 
396 	mtx_assert(&kenv_lock, MA_OWNED);
397 	return (_getenv_dynamic_locked(name, idx));
398 }
399 
400 static char *
401 _getenv_static_from(char *chkenv, const char *name)
402 {
403 	char *cp, *ep;
404 	int len;
405 
406 	for (cp = chkenv; cp != NULL; cp = kernenv_next(cp)) {
407 		for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
408 			;
409 		if (*ep != '=')
410 			continue;
411 		len = ep - cp;
412 		ep++;
413 		if (!strncmp(name, cp, len) && name[len] == 0)
414 			return (ep);
415 	}
416 	return (NULL);
417 }
418 
419 static char *
420 _getenv_static(const char *name)
421 {
422 	char *val;
423 
424 	val = _getenv_static_from(md_envp, name);
425 	if (val != NULL)
426 		return (val);
427 	val = _getenv_static_from(kern_envp, name);
428 	if (val != NULL)
429 		return (val);
430 	return (NULL);
431 }
432 
433 /*
434  * Look up an environment variable by name.
435  * Return a pointer to the string if found.
436  * The pointer has to be freed with freeenv()
437  * after use.
438  */
439 char *
440 kern_getenv(const char *name)
441 {
442 	char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
443 	char *ret;
444 
445 	if (dynamic_kenv) {
446 		if (getenv_string(name, buf, sizeof(buf))) {
447 			ret = strdup(buf, M_KENV);
448 		} else {
449 			ret = NULL;
450 			WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
451 			    "getenv");
452 		}
453 	} else
454 		ret = _getenv_static(name);
455 	return (ret);
456 }
457 
458 /*
459  * Test if an environment variable is defined.
460  */
461 int
462 testenv(const char *name)
463 {
464 	char *cp;
465 
466 	if (dynamic_kenv) {
467 		mtx_lock(&kenv_lock);
468 		cp = _getenv_dynamic(name, NULL);
469 		mtx_unlock(&kenv_lock);
470 	} else
471 		cp = _getenv_static(name);
472 	if (cp != NULL)
473 		return (1);
474 	return (0);
475 }
476 
477 /*
478  * Set an environment variable in the MD-static environment.  This cannot
479  * feasibly be done on config(8)-generated static environments as they don't
480  * generally include space for extra variables.
481  */
482 static int
483 setenv_static(const char *name, const char *value)
484 {
485 	int len;
486 
487 	if (md_env_pos >= md_env_len)
488 		return (-1);
489 
490 	/* Check space for x=y and two nuls */
491 	len = strlen(name) + strlen(value);
492 	if (len + 3 < md_env_len - md_env_pos) {
493 		len = sprintf(&md_envp[md_env_pos], "%s=%s", name, value);
494 		md_env_pos += len+1;
495 		md_envp[md_env_pos] = '\0';
496 		return (0);
497 	} else
498 		return (-1);
499 
500 }
501 
502 /*
503  * Set an environment variable by name.
504  */
505 int
506 kern_setenv(const char *name, const char *value)
507 {
508 	char *buf, *cp, *oldenv;
509 	int namelen, vallen, i;
510 
511 	if (!dynamic_kenv && md_env_len > 0)
512 		return (setenv_static(name, value));
513 
514 	KENV_CHECK;
515 
516 	namelen = strlen(name) + 1;
517 	if (namelen > KENV_MNAMELEN + 1)
518 		return (-1);
519 	vallen = strlen(value) + 1;
520 	if (vallen > KENV_MVALLEN + 1)
521 		return (-1);
522 	buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
523 	sprintf(buf, "%s=%s", name, value);
524 
525 	mtx_lock(&kenv_lock);
526 	cp = _getenv_dynamic(name, &i);
527 	if (cp != NULL) {
528 		oldenv = kenvp[i];
529 		kenvp[i] = buf;
530 		mtx_unlock(&kenv_lock);
531 		free(oldenv, M_KENV);
532 	} else {
533 		/* We add the option if it wasn't found */
534 		for (i = 0; (cp = kenvp[i]) != NULL; i++)
535 			;
536 
537 		/* Bounds checking */
538 		if (i < 0 || i >= KENV_SIZE) {
539 			free(buf, M_KENV);
540 			mtx_unlock(&kenv_lock);
541 			return (-1);
542 		}
543 
544 		kenvp[i] = buf;
545 		kenvp[i + 1] = NULL;
546 		mtx_unlock(&kenv_lock);
547 	}
548 	return (0);
549 }
550 
551 /*
552  * Unset an environment variable string.
553  */
554 int
555 kern_unsetenv(const char *name)
556 {
557 	char *cp, *oldenv;
558 	int i, j;
559 
560 	KENV_CHECK;
561 
562 	mtx_lock(&kenv_lock);
563 	cp = _getenv_dynamic(name, &i);
564 	if (cp != NULL) {
565 		oldenv = kenvp[i];
566 		for (j = i + 1; kenvp[j] != NULL; j++)
567 			kenvp[i++] = kenvp[j];
568 		kenvp[i] = NULL;
569 		mtx_unlock(&kenv_lock);
570 		explicit_bzero(oldenv, strlen(oldenv));
571 		free(oldenv, M_KENV);
572 		return (0);
573 	}
574 	mtx_unlock(&kenv_lock);
575 	return (-1);
576 }
577 
578 /*
579  * Return a string value from an environment variable.
580  */
581 int
582 getenv_string(const char *name, char *data, int size)
583 {
584 	char *cp;
585 
586 	if (dynamic_kenv) {
587 		mtx_lock(&kenv_lock);
588 		cp = _getenv_dynamic(name, NULL);
589 		if (cp != NULL)
590 			strlcpy(data, cp, size);
591 		mtx_unlock(&kenv_lock);
592 	} else {
593 		cp = _getenv_static(name);
594 		if (cp != NULL)
595 			strlcpy(data, cp, size);
596 	}
597 	return (cp != NULL);
598 }
599 
600 /*
601  * Return an array of integers at the given type size and signedness.
602  */
603 int
604 getenv_array(const char *name, void *pdata, int size, int *psize,
605     int type_size, bool allow_signed)
606 {
607 	char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
608 	uint8_t shift;
609 	int64_t value;
610 	int64_t old;
611 	char *end;
612 	char *ptr;
613 	int n;
614 
615 	if (getenv_string(name, buf, sizeof(buf)) == 0)
616 		return (0);
617 
618 	/* get maximum number of elements */
619 	size /= type_size;
620 
621 	n = 0;
622 
623 	for (ptr = buf; *ptr != 0; ) {
624 
625 		value = strtoq(ptr, &end, 0);
626 
627 		/* check if signed numbers are allowed */
628 		if (value < 0 && !allow_signed)
629 			goto error;
630 
631 		/* check for invalid value */
632 		if (ptr == end)
633 			goto error;
634 
635 		/* check for valid suffix */
636 		switch (*end) {
637 		case 't':
638 		case 'T':
639 			shift = 40;
640 			end++;
641 			break;
642 		case 'g':
643 		case 'G':
644 			shift = 30;
645 			end++;
646 			break;
647 		case 'm':
648 		case 'M':
649 			shift = 20;
650 			end++;
651 			break;
652 		case 'k':
653 		case 'K':
654 			shift = 10;
655 			end++;
656 			break;
657 		case ' ':
658 		case '\t':
659 		case ',':
660 		case 0:
661 			shift = 0;
662 			break;
663 		default:
664 			/* garbage after numeric value */
665 			goto error;
666 		}
667 
668 		/* skip till next value, if any */
669 		while (*end == '\t' || *end == ',' || *end == ' ')
670 			end++;
671 
672 		/* update pointer */
673 		ptr = end;
674 
675 		/* apply shift */
676 		old = value;
677 		value <<= shift;
678 
679 		/* overflow check */
680 		if ((value >> shift) != old)
681 			goto error;
682 
683 		/* check for buffer overflow */
684 		if (n >= size)
685 			goto error;
686 
687 		/* store value according to type size */
688 		switch (type_size) {
689 		case 1:
690 			if (allow_signed) {
691 				if (value < SCHAR_MIN || value > SCHAR_MAX)
692 					goto error;
693 			} else {
694 				if (value < 0 || value > UCHAR_MAX)
695 					goto error;
696 			}
697 			((uint8_t *)pdata)[n] = (uint8_t)value;
698 			break;
699 		case 2:
700 			if (allow_signed) {
701 				if (value < SHRT_MIN || value > SHRT_MAX)
702 					goto error;
703 			} else {
704 				if (value < 0 || value > USHRT_MAX)
705 					goto error;
706 			}
707 			((uint16_t *)pdata)[n] = (uint16_t)value;
708 			break;
709 		case 4:
710 			if (allow_signed) {
711 				if (value < INT_MIN || value > INT_MAX)
712 					goto error;
713 			} else {
714 				if (value > UINT_MAX)
715 					goto error;
716 			}
717 			((uint32_t *)pdata)[n] = (uint32_t)value;
718 			break;
719 		case 8:
720 			((uint64_t *)pdata)[n] = (uint64_t)value;
721 			break;
722 		default:
723 			goto error;
724 		}
725 		n++;
726 	}
727 	*psize = n * type_size;
728 
729 	if (n != 0)
730 		return (1);	/* success */
731 error:
732 	return (0);	/* failure */
733 }
734 
735 /*
736  * Return an integer value from an environment variable.
737  */
738 int
739 getenv_int(const char *name, int *data)
740 {
741 	quad_t tmp;
742 	int rval;
743 
744 	rval = getenv_quad(name, &tmp);
745 	if (rval)
746 		*data = (int) tmp;
747 	return (rval);
748 }
749 
750 /*
751  * Return an unsigned integer value from an environment variable.
752  */
753 int
754 getenv_uint(const char *name, unsigned int *data)
755 {
756 	quad_t tmp;
757 	int rval;
758 
759 	rval = getenv_quad(name, &tmp);
760 	if (rval)
761 		*data = (unsigned int) tmp;
762 	return (rval);
763 }
764 
765 /*
766  * Return an int64_t value from an environment variable.
767  */
768 int
769 getenv_int64(const char *name, int64_t *data)
770 {
771 	quad_t tmp;
772 	int64_t rval;
773 
774 	rval = getenv_quad(name, &tmp);
775 	if (rval)
776 		*data = (int64_t) tmp;
777 	return (rval);
778 }
779 
780 /*
781  * Return an uint64_t value from an environment variable.
782  */
783 int
784 getenv_uint64(const char *name, uint64_t *data)
785 {
786 	quad_t tmp;
787 	uint64_t rval;
788 
789 	rval = getenv_quad(name, &tmp);
790 	if (rval)
791 		*data = (uint64_t) tmp;
792 	return (rval);
793 }
794 
795 /*
796  * Return a long value from an environment variable.
797  */
798 int
799 getenv_long(const char *name, long *data)
800 {
801 	quad_t tmp;
802 	int rval;
803 
804 	rval = getenv_quad(name, &tmp);
805 	if (rval)
806 		*data = (long) tmp;
807 	return (rval);
808 }
809 
810 /*
811  * Return an unsigned long value from an environment variable.
812  */
813 int
814 getenv_ulong(const char *name, unsigned long *data)
815 {
816 	quad_t tmp;
817 	int rval;
818 
819 	rval = getenv_quad(name, &tmp);
820 	if (rval)
821 		*data = (unsigned long) tmp;
822 	return (rval);
823 }
824 
825 /*
826  * Return a quad_t value from an environment variable.
827  */
828 int
829 getenv_quad(const char *name, quad_t *data)
830 {
831 	char	value[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
832 	char	*vtp;
833 	quad_t	iv;
834 
835 	if (!getenv_string(name, value, sizeof(value)))
836 		return (0);
837 	iv = strtoq(value, &vtp, 0);
838 	if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0'))
839 		return (0);
840 	switch (vtp[0]) {
841 	case 't': case 'T':
842 		iv *= 1024;
843 		/* FALLTHROUGH */
844 	case 'g': case 'G':
845 		iv *= 1024;
846 		/* FALLTHROUGH */
847 	case 'm': case 'M':
848 		iv *= 1024;
849 		/* FALLTHROUGH */
850 	case 'k': case 'K':
851 		iv *= 1024;
852 	case '\0':
853 		break;
854 	default:
855 		return (0);
856 	}
857 	*data = iv;
858 	return (1);
859 }
860 
861 /*
862  * Find the next entry after the one which (cp) falls within, return a
863  * pointer to its start or NULL if there are no more.
864  */
865 static char *
866 kernenv_next(char *cp)
867 {
868 
869 	if (cp != NULL) {
870 		while (*cp != 0)
871 			cp++;
872 		cp++;
873 		if (*cp == 0)
874 			cp = NULL;
875 	}
876 	return (cp);
877 }
878 
879 void
880 tunable_int_init(void *data)
881 {
882 	struct tunable_int *d = (struct tunable_int *)data;
883 
884 	TUNABLE_INT_FETCH(d->path, d->var);
885 }
886 
887 void
888 tunable_long_init(void *data)
889 {
890 	struct tunable_long *d = (struct tunable_long *)data;
891 
892 	TUNABLE_LONG_FETCH(d->path, d->var);
893 }
894 
895 void
896 tunable_ulong_init(void *data)
897 {
898 	struct tunable_ulong *d = (struct tunable_ulong *)data;
899 
900 	TUNABLE_ULONG_FETCH(d->path, d->var);
901 }
902 
903 void
904 tunable_int64_init(void *data)
905 {
906 	struct tunable_int64 *d = (struct tunable_int64 *)data;
907 
908 	TUNABLE_INT64_FETCH(d->path, d->var);
909 }
910 
911 void
912 tunable_uint64_init(void *data)
913 {
914 	struct tunable_uint64 *d = (struct tunable_uint64 *)data;
915 
916 	TUNABLE_UINT64_FETCH(d->path, d->var);
917 }
918 
919 void
920 tunable_quad_init(void *data)
921 {
922 	struct tunable_quad *d = (struct tunable_quad *)data;
923 
924 	TUNABLE_QUAD_FETCH(d->path, d->var);
925 }
926 
927 void
928 tunable_str_init(void *data)
929 {
930 	struct tunable_str *d = (struct tunable_str *)data;
931 
932 	TUNABLE_STR_FETCH(d->path, d->var, d->size);
933 }
934