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