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