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