xref: /freebsd/sys/kern/kern_environment.c (revision 780fb4a2fa9a9aee5ac48a60b790f567c0dc13e9)
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 	md_envp = buf;
253 	md_env_len = len;
254 	md_env_pos = 0;
255 
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 	eval = kern_getenv("static_env.disabled");
268 	if (eval == NULL || strcmp(eval, "1") != 0)
269 		kern_envp = static_env;
270 	eval = kern_getenv("static_hints.disabled");
271 	if (eval != NULL && strcmp(eval, "1") == 0)
272 		*static_hints = '\0';
273 }
274 
275 static void
276 init_dynamic_kenv_from(char *init_env, int *curpos)
277 {
278 	char *cp, *cpnext, *eqpos, *found;
279 	size_t len;
280 	int i;
281 
282 	if (init_env && *init_env != '\0') {
283 		found = NULL;
284 		i = *curpos;
285 		for (cp = init_env; cp != NULL; cp = cpnext) {
286 			cpnext = kernenv_next(cp);
287 			len = strlen(cp) + 1;
288 			if (len > KENV_MNAMELEN + 1 + KENV_MVALLEN + 1) {
289 				printf(
290 				"WARNING: too long kenv string, ignoring %s\n",
291 				    cp);
292 				goto sanitize;
293 			}
294 			eqpos = strchr(cp, '=');
295 			if (eqpos == NULL) {
296 				printf(
297 				"WARNING: malformed static env value, ignoring %s\n",
298 				    cp);
299 				goto sanitize;
300 			}
301 			*eqpos = 0;
302 			/*
303 			 * De-dupe the environment as we go.  We don't add the
304 			 * duplicated assignments because config(8) will flip
305 			 * the order of the static environment around to make
306 			 * kernel processing match the order of specification
307 			 * in the kernel config.
308 			 */
309 			found = _getenv_dynamic_locked(cp, NULL);
310 			*eqpos = '=';
311 			if (found != NULL)
312 				goto sanitize;
313 			if (i > KENV_SIZE) {
314 				printf(
315 				"WARNING: too many kenv strings, ignoring %s\n",
316 				    cp);
317 				goto sanitize;
318 			}
319 
320 			kenvp[i] = malloc(len, M_KENV, M_WAITOK);
321 			strcpy(kenvp[i++], cp);
322 sanitize:
323 			explicit_bzero(cp, len - 1);
324 		}
325 		*curpos = i;
326 	}
327 }
328 
329 /*
330  * Setup the dynamic kernel environment.
331  */
332 static void
333 init_dynamic_kenv(void *data __unused)
334 {
335 	int dynamic_envpos;
336 
337 	kenvp = malloc((KENV_SIZE + 1) * sizeof(char *), M_KENV,
338 		M_WAITOK | M_ZERO);
339 
340 	dynamic_envpos = 0;
341 	init_dynamic_kenv_from(md_envp, &dynamic_envpos);
342 	init_dynamic_kenv_from(kern_envp, &dynamic_envpos);
343 	kenvp[dynamic_envpos] = NULL;
344 
345 	mtx_init(&kenv_lock, "kernel environment", NULL, MTX_DEF);
346 	dynamic_kenv = true;
347 }
348 SYSINIT(kenv, SI_SUB_KMEM + 1, SI_ORDER_FIRST, init_dynamic_kenv, NULL);
349 
350 void
351 freeenv(char *env)
352 {
353 
354 	if (dynamic_kenv && env != NULL) {
355 		explicit_bzero(env, strlen(env));
356 		free(env, M_KENV);
357 	}
358 }
359 
360 /*
361  * Internal functions for string lookup.
362  */
363 static char *
364 _getenv_dynamic_locked(const char *name, int *idx)
365 {
366 	char *cp;
367 	int len, i;
368 
369 	len = strlen(name);
370 	for (cp = kenvp[0], i = 0; cp != NULL; cp = kenvp[++i]) {
371 		if ((strncmp(cp, name, len) == 0) &&
372 		    (cp[len] == '=')) {
373 			if (idx != NULL)
374 				*idx = i;
375 			return (cp + len + 1);
376 		}
377 	}
378 	return (NULL);
379 }
380 
381 static char *
382 _getenv_dynamic(const char *name, int *idx)
383 {
384 
385 	mtx_assert(&kenv_lock, MA_OWNED);
386 	return (_getenv_dynamic_locked(name, idx));
387 }
388 
389 static char *
390 _getenv_static_from(char *chkenv, const char *name)
391 {
392 	char *cp, *ep;
393 	int len;
394 
395 	for (cp = chkenv; cp != NULL; cp = kernenv_next(cp)) {
396 		for (ep = cp; (*ep != '=') && (*ep != 0); ep++)
397 			;
398 		if (*ep != '=')
399 			continue;
400 		len = ep - cp;
401 		ep++;
402 		if (!strncmp(name, cp, len) && name[len] == 0)
403 			return (ep);
404 	}
405 	return (NULL);
406 }
407 
408 static char *
409 _getenv_static(const char *name)
410 {
411 	char *val;
412 
413 	val = _getenv_static_from(md_envp, name);
414 	if (val != NULL)
415 		return (val);
416 	val = _getenv_static_from(kern_envp, name);
417 	if (val != NULL)
418 		return (val);
419 	return (NULL);
420 }
421 
422 /*
423  * Look up an environment variable by name.
424  * Return a pointer to the string if found.
425  * The pointer has to be freed with freeenv()
426  * after use.
427  */
428 char *
429 kern_getenv(const char *name)
430 {
431 	char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
432 	char *ret;
433 
434 	if (dynamic_kenv) {
435 		if (getenv_string(name, buf, sizeof(buf))) {
436 			ret = strdup(buf, M_KENV);
437 		} else {
438 			ret = NULL;
439 			WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
440 			    "getenv");
441 		}
442 	} else
443 		ret = _getenv_static(name);
444 	return (ret);
445 }
446 
447 /*
448  * Test if an environment variable is defined.
449  */
450 int
451 testenv(const char *name)
452 {
453 	char *cp;
454 
455 	if (dynamic_kenv) {
456 		mtx_lock(&kenv_lock);
457 		cp = _getenv_dynamic(name, NULL);
458 		mtx_unlock(&kenv_lock);
459 	} else
460 		cp = _getenv_static(name);
461 	if (cp != NULL)
462 		return (1);
463 	return (0);
464 }
465 
466 /*
467  * Set an environment variable in the MD-static environment.  This cannot
468  * feasibly be done on config(8)-generated static environments as they don't
469  * generally include space for extra variables.
470  */
471 static int
472 setenv_static(const char *name, const char *value)
473 {
474 	int len;
475 
476 	if (md_env_pos >= md_env_len)
477 		return (-1);
478 
479 	/* Check space for x=y and two nuls */
480 	len = strlen(name) + strlen(value);
481 	if (len + 3 < md_env_len - md_env_pos) {
482 		len = sprintf(&md_envp[md_env_pos], "%s=%s", name, value);
483 		md_env_pos += len+1;
484 		md_envp[md_env_pos] = '\0';
485 		return (0);
486 	} else
487 		return (-1);
488 
489 }
490 
491 /*
492  * Set an environment variable by name.
493  */
494 int
495 kern_setenv(const char *name, const char *value)
496 {
497 	char *buf, *cp, *oldenv;
498 	int namelen, vallen, i;
499 
500 	if (!dynamic_kenv && md_env_len > 0)
501 		return (setenv_static(name, value));
502 
503 	KENV_CHECK;
504 
505 	namelen = strlen(name) + 1;
506 	if (namelen > KENV_MNAMELEN + 1)
507 		return (-1);
508 	vallen = strlen(value) + 1;
509 	if (vallen > KENV_MVALLEN + 1)
510 		return (-1);
511 	buf = malloc(namelen + vallen, M_KENV, M_WAITOK);
512 	sprintf(buf, "%s=%s", name, value);
513 
514 	mtx_lock(&kenv_lock);
515 	cp = _getenv_dynamic(name, &i);
516 	if (cp != NULL) {
517 		oldenv = kenvp[i];
518 		kenvp[i] = buf;
519 		mtx_unlock(&kenv_lock);
520 		free(oldenv, M_KENV);
521 	} else {
522 		/* We add the option if it wasn't found */
523 		for (i = 0; (cp = kenvp[i]) != NULL; i++)
524 			;
525 
526 		/* Bounds checking */
527 		if (i < 0 || i >= KENV_SIZE) {
528 			free(buf, M_KENV);
529 			mtx_unlock(&kenv_lock);
530 			return (-1);
531 		}
532 
533 		kenvp[i] = buf;
534 		kenvp[i + 1] = NULL;
535 		mtx_unlock(&kenv_lock);
536 	}
537 	return (0);
538 }
539 
540 /*
541  * Unset an environment variable string.
542  */
543 int
544 kern_unsetenv(const char *name)
545 {
546 	char *cp, *oldenv;
547 	int i, j;
548 
549 	KENV_CHECK;
550 
551 	mtx_lock(&kenv_lock);
552 	cp = _getenv_dynamic(name, &i);
553 	if (cp != NULL) {
554 		oldenv = kenvp[i];
555 		for (j = i + 1; kenvp[j] != NULL; j++)
556 			kenvp[i++] = kenvp[j];
557 		kenvp[i] = NULL;
558 		mtx_unlock(&kenv_lock);
559 		explicit_bzero(oldenv, strlen(oldenv));
560 		free(oldenv, M_KENV);
561 		return (0);
562 	}
563 	mtx_unlock(&kenv_lock);
564 	return (-1);
565 }
566 
567 /*
568  * Return a string value from an environment variable.
569  */
570 int
571 getenv_string(const char *name, char *data, int size)
572 {
573 	char *cp;
574 
575 	if (dynamic_kenv) {
576 		mtx_lock(&kenv_lock);
577 		cp = _getenv_dynamic(name, NULL);
578 		if (cp != NULL)
579 			strlcpy(data, cp, size);
580 		mtx_unlock(&kenv_lock);
581 	} else {
582 		cp = _getenv_static(name);
583 		if (cp != NULL)
584 			strlcpy(data, cp, size);
585 	}
586 	return (cp != NULL);
587 }
588 
589 /*
590  * Return an array of integers at the given type size and signedness.
591  */
592 int
593 getenv_array(const char *name, void *pdata, int size, int *psize,
594     int type_size, bool allow_signed)
595 {
596 	char buf[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
597 	uint8_t shift;
598 	int64_t value;
599 	int64_t old;
600 	char *end;
601 	char *ptr;
602 	int n;
603 
604 	if (getenv_string(name, buf, sizeof(buf)) == 0)
605 		return (0);
606 
607 	/* get maximum number of elements */
608 	size /= type_size;
609 
610 	n = 0;
611 
612 	for (ptr = buf; *ptr != 0; ) {
613 
614 		value = strtoq(ptr, &end, 0);
615 
616 		/* check if signed numbers are allowed */
617 		if (value < 0 && !allow_signed)
618 			goto error;
619 
620 		/* check for invalid value */
621 		if (ptr == end)
622 			goto error;
623 
624 		/* check for valid suffix */
625 		switch (*end) {
626 		case 't':
627 		case 'T':
628 			shift = 40;
629 			end++;
630 			break;
631 		case 'g':
632 		case 'G':
633 			shift = 30;
634 			end++;
635 			break;
636 		case 'm':
637 		case 'M':
638 			shift = 20;
639 			end++;
640 			break;
641 		case 'k':
642 		case 'K':
643 			shift = 10;
644 			end++;
645 			break;
646 		case ' ':
647 		case '\t':
648 		case ',':
649 		case 0:
650 			shift = 0;
651 			break;
652 		default:
653 			/* garbage after numeric value */
654 			goto error;
655 		}
656 
657 		/* skip till next value, if any */
658 		while (*end == '\t' || *end == ',' || *end == ' ')
659 			end++;
660 
661 		/* update pointer */
662 		ptr = end;
663 
664 		/* apply shift */
665 		old = value;
666 		value <<= shift;
667 
668 		/* overflow check */
669 		if ((value >> shift) != old)
670 			goto error;
671 
672 		/* check for buffer overflow */
673 		if (n >= size)
674 			goto error;
675 
676 		/* store value according to type size */
677 		switch (type_size) {
678 		case 1:
679 			if (allow_signed) {
680 				if (value < SCHAR_MIN || value > SCHAR_MAX)
681 					goto error;
682 			} else {
683 				if (value < 0 || value > UCHAR_MAX)
684 					goto error;
685 			}
686 			((uint8_t *)pdata)[n] = (uint8_t)value;
687 			break;
688 		case 2:
689 			if (allow_signed) {
690 				if (value < SHRT_MIN || value > SHRT_MAX)
691 					goto error;
692 			} else {
693 				if (value < 0 || value > USHRT_MAX)
694 					goto error;
695 			}
696 			((uint16_t *)pdata)[n] = (uint16_t)value;
697 			break;
698 		case 4:
699 			if (allow_signed) {
700 				if (value < INT_MIN || value > INT_MAX)
701 					goto error;
702 			} else {
703 				if (value > UINT_MAX)
704 					goto error;
705 			}
706 			((uint32_t *)pdata)[n] = (uint32_t)value;
707 			break;
708 		case 8:
709 			((uint64_t *)pdata)[n] = (uint64_t)value;
710 			break;
711 		default:
712 			goto error;
713 		}
714 		n++;
715 	}
716 	*psize = n * type_size;
717 
718 	if (n != 0)
719 		return (1);	/* success */
720 error:
721 	return (0);	/* failure */
722 }
723 
724 /*
725  * Return an integer value from an environment variable.
726  */
727 int
728 getenv_int(const char *name, int *data)
729 {
730 	quad_t tmp;
731 	int rval;
732 
733 	rval = getenv_quad(name, &tmp);
734 	if (rval)
735 		*data = (int) tmp;
736 	return (rval);
737 }
738 
739 /*
740  * Return an unsigned integer value from an environment variable.
741  */
742 int
743 getenv_uint(const char *name, unsigned int *data)
744 {
745 	quad_t tmp;
746 	int rval;
747 
748 	rval = getenv_quad(name, &tmp);
749 	if (rval)
750 		*data = (unsigned int) tmp;
751 	return (rval);
752 }
753 
754 /*
755  * Return an int64_t value from an environment variable.
756  */
757 int
758 getenv_int64(const char *name, int64_t *data)
759 {
760 	quad_t tmp;
761 	int64_t rval;
762 
763 	rval = getenv_quad(name, &tmp);
764 	if (rval)
765 		*data = (int64_t) tmp;
766 	return (rval);
767 }
768 
769 /*
770  * Return an uint64_t value from an environment variable.
771  */
772 int
773 getenv_uint64(const char *name, uint64_t *data)
774 {
775 	quad_t tmp;
776 	uint64_t rval;
777 
778 	rval = getenv_quad(name, &tmp);
779 	if (rval)
780 		*data = (uint64_t) tmp;
781 	return (rval);
782 }
783 
784 /*
785  * Return a long value from an environment variable.
786  */
787 int
788 getenv_long(const char *name, long *data)
789 {
790 	quad_t tmp;
791 	int rval;
792 
793 	rval = getenv_quad(name, &tmp);
794 	if (rval)
795 		*data = (long) tmp;
796 	return (rval);
797 }
798 
799 /*
800  * Return an unsigned long value from an environment variable.
801  */
802 int
803 getenv_ulong(const char *name, unsigned long *data)
804 {
805 	quad_t tmp;
806 	int rval;
807 
808 	rval = getenv_quad(name, &tmp);
809 	if (rval)
810 		*data = (unsigned long) tmp;
811 	return (rval);
812 }
813 
814 /*
815  * Return a quad_t value from an environment variable.
816  */
817 int
818 getenv_quad(const char *name, quad_t *data)
819 {
820 	char	value[KENV_MNAMELEN + 1 + KENV_MVALLEN + 1];
821 	char	*vtp;
822 	quad_t	iv;
823 
824 	if (!getenv_string(name, value, sizeof(value)))
825 		return (0);
826 	iv = strtoq(value, &vtp, 0);
827 	if (vtp == value || (vtp[0] != '\0' && vtp[1] != '\0'))
828 		return (0);
829 	switch (vtp[0]) {
830 	case 't': case 'T':
831 		iv *= 1024;
832 	case 'g': case 'G':
833 		iv *= 1024;
834 	case 'm': case 'M':
835 		iv *= 1024;
836 	case 'k': case 'K':
837 		iv *= 1024;
838 	case '\0':
839 		break;
840 	default:
841 		return (0);
842 	}
843 	*data = iv;
844 	return (1);
845 }
846 
847 /*
848  * Find the next entry after the one which (cp) falls within, return a
849  * pointer to its start or NULL if there are no more.
850  */
851 static char *
852 kernenv_next(char *cp)
853 {
854 
855 	if (cp != NULL) {
856 		while (*cp != 0)
857 			cp++;
858 		cp++;
859 		if (*cp == 0)
860 			cp = NULL;
861 	}
862 	return (cp);
863 }
864 
865 void
866 tunable_int_init(void *data)
867 {
868 	struct tunable_int *d = (struct tunable_int *)data;
869 
870 	TUNABLE_INT_FETCH(d->path, d->var);
871 }
872 
873 void
874 tunable_long_init(void *data)
875 {
876 	struct tunable_long *d = (struct tunable_long *)data;
877 
878 	TUNABLE_LONG_FETCH(d->path, d->var);
879 }
880 
881 void
882 tunable_ulong_init(void *data)
883 {
884 	struct tunable_ulong *d = (struct tunable_ulong *)data;
885 
886 	TUNABLE_ULONG_FETCH(d->path, d->var);
887 }
888 
889 void
890 tunable_int64_init(void *data)
891 {
892 	struct tunable_int64 *d = (struct tunable_int64 *)data;
893 
894 	TUNABLE_INT64_FETCH(d->path, d->var);
895 }
896 
897 void
898 tunable_uint64_init(void *data)
899 {
900 	struct tunable_uint64 *d = (struct tunable_uint64 *)data;
901 
902 	TUNABLE_UINT64_FETCH(d->path, d->var);
903 }
904 
905 void
906 tunable_quad_init(void *data)
907 {
908 	struct tunable_quad *d = (struct tunable_quad *)data;
909 
910 	TUNABLE_QUAD_FETCH(d->path, d->var);
911 }
912 
913 void
914 tunable_str_init(void *data)
915 {
916 	struct tunable_str *d = (struct tunable_str *)data;
917 
918 	TUNABLE_STR_FETCH(d->path, d->var, d->size);
919 }
920