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