1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1993 5 * The Regents of the University of California. 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 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __FBSDID("$FreeBSD$"); 34 35 #include <sys/param.h> 36 #include <sys/time.h> 37 #include <sys/resource.h> 38 #include <sys/stat.h> 39 #include <sys/sysctl.h> 40 #include <sys/vmmeter.h> 41 #include <dev/evdev/input.h> 42 43 #ifdef __amd64__ 44 #include <sys/efi.h> 45 #include <machine/metadata.h> 46 #endif 47 48 #if defined(__amd64__) || defined(__i386__) 49 #include <machine/pc/bios.h> 50 #endif 51 52 #include <assert.h> 53 #include <ctype.h> 54 #include <err.h> 55 #include <errno.h> 56 #include <inttypes.h> 57 #include <locale.h> 58 #include <stdbool.h> 59 #include <stdio.h> 60 #include <stdlib.h> 61 #include <string.h> 62 #include <sysexits.h> 63 #include <unistd.h> 64 65 static const char *conffile; 66 67 static int aflag, bflag, Bflag, dflag, eflag, hflag, iflag; 68 static int Nflag, nflag, oflag, qflag, tflag, Tflag, Wflag, xflag; 69 70 static int oidfmt(int *, int, char *, u_int *); 71 static int parsefile(const char *); 72 static int parse(const char *, int); 73 static int show_var(int *, int, bool); 74 static int sysctl_all(int *oid, int len); 75 static int name2oid(const char *, int *); 76 77 static int strIKtoi(const char *, char **, const char *); 78 79 static int ctl_sign[CTLTYPE+1] = { 80 [CTLTYPE_INT] = 1, 81 [CTLTYPE_LONG] = 1, 82 [CTLTYPE_S8] = 1, 83 [CTLTYPE_S16] = 1, 84 [CTLTYPE_S32] = 1, 85 [CTLTYPE_S64] = 1, 86 }; 87 88 static int ctl_size[CTLTYPE+1] = { 89 [CTLTYPE_INT] = sizeof(int), 90 [CTLTYPE_UINT] = sizeof(u_int), 91 [CTLTYPE_LONG] = sizeof(long), 92 [CTLTYPE_ULONG] = sizeof(u_long), 93 [CTLTYPE_S8] = sizeof(int8_t), 94 [CTLTYPE_S16] = sizeof(int16_t), 95 [CTLTYPE_S32] = sizeof(int32_t), 96 [CTLTYPE_S64] = sizeof(int64_t), 97 [CTLTYPE_U8] = sizeof(uint8_t), 98 [CTLTYPE_U16] = sizeof(uint16_t), 99 [CTLTYPE_U32] = sizeof(uint32_t), 100 [CTLTYPE_U64] = sizeof(uint64_t), 101 }; 102 103 static const char *ctl_typename[CTLTYPE+1] = { 104 [CTLTYPE_INT] = "integer", 105 [CTLTYPE_UINT] = "unsigned integer", 106 [CTLTYPE_LONG] = "long integer", 107 [CTLTYPE_ULONG] = "unsigned long", 108 [CTLTYPE_U8] = "uint8_t", 109 [CTLTYPE_U16] = "uint16_t", 110 [CTLTYPE_U32] = "uint32_t", 111 [CTLTYPE_U64] = "uint64_t", 112 [CTLTYPE_S8] = "int8_t", 113 [CTLTYPE_S16] = "int16_t", 114 [CTLTYPE_S32] = "int32_t", 115 [CTLTYPE_S64] = "int64_t", 116 [CTLTYPE_NODE] = "node", 117 [CTLTYPE_STRING] = "string", 118 [CTLTYPE_OPAQUE] = "opaque", 119 }; 120 121 static void 122 usage(void) 123 { 124 125 (void)fprintf(stderr, "%s\n%s\n", 126 "usage: sysctl [-bdehiNnoqTtWx] [ -B <bufsize> ] [-f filename] name[=value] ...", 127 " sysctl [-bdehNnoqTtWx] [ -B <bufsize> ] -a"); 128 exit(1); 129 } 130 131 int 132 main(int argc, char **argv) 133 { 134 int ch; 135 int warncount = 0; 136 137 setlocale(LC_NUMERIC, ""); 138 setbuf(stdout,0); 139 setbuf(stderr,0); 140 141 while ((ch = getopt(argc, argv, "AabB:def:hiNnoqtTwWxX")) != -1) { 142 switch (ch) { 143 case 'A': 144 /* compatibility */ 145 aflag = oflag = 1; 146 break; 147 case 'a': 148 aflag = 1; 149 break; 150 case 'b': 151 bflag = 1; 152 break; 153 case 'B': 154 Bflag = strtol(optarg, NULL, 0); 155 break; 156 case 'd': 157 dflag = 1; 158 break; 159 case 'e': 160 eflag = 1; 161 break; 162 case 'f': 163 conffile = optarg; 164 break; 165 case 'h': 166 hflag = 1; 167 break; 168 case 'i': 169 iflag = 1; 170 break; 171 case 'N': 172 Nflag = 1; 173 break; 174 case 'n': 175 nflag = 1; 176 break; 177 case 'o': 178 oflag = 1; 179 break; 180 case 'q': 181 qflag = 1; 182 break; 183 case 't': 184 tflag = 1; 185 break; 186 case 'T': 187 Tflag = 1; 188 break; 189 case 'w': 190 /* compatibility */ 191 /* ignored */ 192 break; 193 case 'W': 194 Wflag = 1; 195 break; 196 case 'X': 197 /* compatibility */ 198 aflag = xflag = 1; 199 break; 200 case 'x': 201 xflag = 1; 202 break; 203 default: 204 usage(); 205 } 206 } 207 argc -= optind; 208 argv += optind; 209 210 if (Nflag && nflag) 211 usage(); 212 if (aflag && argc == 0) 213 exit(sysctl_all(NULL, 0)); 214 if (argc == 0 && conffile == NULL) 215 usage(); 216 217 warncount = 0; 218 if (conffile != NULL) 219 warncount += parsefile(conffile); 220 221 while (argc-- > 0) 222 warncount += parse(*argv++, 0); 223 224 return (warncount); 225 } 226 227 /* 228 * Parse a single numeric value, append it to 'newbuf', and update 229 * 'newsize'. Returns true if the value was parsed and false if the 230 * value was invalid. Non-numeric types (strings) are handled 231 * directly in parse(). 232 */ 233 static bool 234 parse_numeric(const char *newvalstr, const char *fmt, u_int kind, 235 void **newbufp, size_t *newsizep) 236 { 237 void *newbuf; 238 const void *newval; 239 int8_t i8val; 240 uint8_t u8val; 241 int16_t i16val; 242 uint16_t u16val; 243 int32_t i32val; 244 uint32_t u32val; 245 int intval; 246 unsigned int uintval; 247 long longval; 248 unsigned long ulongval; 249 int64_t i64val; 250 uint64_t u64val; 251 size_t valsize; 252 char *endptr = NULL; 253 254 errno = 0; 255 256 switch (kind & CTLTYPE) { 257 case CTLTYPE_INT: 258 if (strncmp(fmt, "IK", 2) == 0) 259 intval = strIKtoi(newvalstr, &endptr, fmt); 260 else 261 intval = (int)strtol(newvalstr, &endptr, 0); 262 newval = &intval; 263 valsize = sizeof(intval); 264 break; 265 case CTLTYPE_UINT: 266 uintval = (int) strtoul(newvalstr, &endptr, 0); 267 newval = &uintval; 268 valsize = sizeof(uintval); 269 break; 270 case CTLTYPE_LONG: 271 longval = strtol(newvalstr, &endptr, 0); 272 newval = &longval; 273 valsize = sizeof(longval); 274 break; 275 case CTLTYPE_ULONG: 276 ulongval = strtoul(newvalstr, &endptr, 0); 277 newval = &ulongval; 278 valsize = sizeof(ulongval); 279 break; 280 case CTLTYPE_S8: 281 i8val = (int8_t)strtol(newvalstr, &endptr, 0); 282 newval = &i8val; 283 valsize = sizeof(i8val); 284 break; 285 case CTLTYPE_S16: 286 i16val = (int16_t)strtol(newvalstr, &endptr, 0); 287 newval = &i16val; 288 valsize = sizeof(i16val); 289 break; 290 case CTLTYPE_S32: 291 i32val = (int32_t)strtol(newvalstr, &endptr, 0); 292 newval = &i32val; 293 valsize = sizeof(i32val); 294 break; 295 case CTLTYPE_S64: 296 i64val = strtoimax(newvalstr, &endptr, 0); 297 newval = &i64val; 298 valsize = sizeof(i64val); 299 break; 300 case CTLTYPE_U8: 301 u8val = (uint8_t)strtoul(newvalstr, &endptr, 0); 302 newval = &u8val; 303 valsize = sizeof(u8val); 304 break; 305 case CTLTYPE_U16: 306 u16val = (uint16_t)strtoul(newvalstr, &endptr, 0); 307 newval = &u16val; 308 valsize = sizeof(u16val); 309 break; 310 case CTLTYPE_U32: 311 u32val = (uint32_t)strtoul(newvalstr, &endptr, 0); 312 newval = &u32val; 313 valsize = sizeof(u32val); 314 break; 315 case CTLTYPE_U64: 316 u64val = strtoumax(newvalstr, &endptr, 0); 317 newval = &u64val; 318 valsize = sizeof(u64val); 319 break; 320 default: 321 /* NOTREACHED */ 322 abort(); 323 } 324 325 if (errno != 0 || endptr == newvalstr || 326 (endptr != NULL && *endptr != '\0')) 327 return (false); 328 329 newbuf = realloc(*newbufp, *newsizep + valsize); 330 if (newbuf == NULL) 331 err(1, "out of memory"); 332 memcpy((char *)newbuf + *newsizep, newval, valsize); 333 *newbufp = newbuf; 334 *newsizep += valsize; 335 336 return (true); 337 } 338 339 /* 340 * Parse a name into a MIB entry. 341 * Lookup and print out the MIB entry if it exists. 342 * Set a new value if requested. 343 */ 344 static int 345 parse(const char *string, int lineno) 346 { 347 int len, i, j; 348 const void *newval; 349 char *newvalstr = NULL; 350 void *newbuf; 351 size_t newsize = Bflag; 352 int mib[CTL_MAXNAME]; 353 char *cp, *bufp, buf[BUFSIZ], fmt[BUFSIZ], line[BUFSIZ]; 354 u_int kind; 355 356 if (lineno) 357 snprintf(line, sizeof(line), " at line %d", lineno); 358 else 359 line[0] = '\0'; 360 361 /* 362 * Split the string into name and value. 363 * 364 * Either = or : may be used as the delimiter. 365 * Whitespace surrounding the delimiter is trimmed. 366 * Quotes around the value are stripped. 367 */ 368 cp = buf; 369 if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ) { 370 warnx("oid too long: '%s'%s", string, line); 371 return (1); 372 } 373 bufp = strsep(&cp, "=:"); 374 if (cp != NULL) { 375 /* Tflag just lists tunables, do not allow assignment */ 376 if (Tflag || Wflag) { 377 warnx("Can't set variables when using -T or -W"); 378 usage(); 379 } 380 /* Trim whitespace before the value. */ 381 while (isspace(*cp)) 382 cp++; 383 /* Strip a pair of " or ' if any. */ 384 switch (*cp) { 385 case '\"': 386 case '\'': 387 if (cp[strlen(cp) - 1] == *cp) 388 cp[strlen(cp) - 1] = '\0'; 389 cp++; 390 } 391 newvalstr = cp; 392 newsize = strlen(cp); 393 } 394 /* Trim whitespace after the name. */ 395 cp = bufp + strlen(bufp) - 1; 396 while (cp >= bufp && isspace((int)*cp)) { 397 *cp = '\0'; 398 cp--; 399 } 400 401 /* 402 * Check the name is a useable oid. 403 */ 404 len = name2oid(bufp, mib); 405 if (len < 0) { 406 if (iflag) 407 return (0); 408 if (qflag) 409 return (1); 410 else { 411 if (errno == ENOENT) { 412 warnx("unknown oid '%s'%s", bufp, line); 413 } else { 414 warn("unknown oid '%s'%s", bufp, line); 415 } 416 return (1); 417 } 418 } 419 420 if (oidfmt(mib, len, fmt, &kind)) { 421 warn("couldn't find format of oid '%s'%s", bufp, line); 422 if (iflag) 423 return (1); 424 else 425 exit(1); 426 } 427 428 /* 429 * We have a useable oid to work with. If there is no value given, 430 * show the node and its children. Otherwise, set the new value. 431 */ 432 if (newvalstr == NULL || dflag) { 433 if ((kind & CTLTYPE) == CTLTYPE_NODE) { 434 if (dflag) { 435 i = show_var(mib, len, false); 436 if (!i && !bflag) 437 putchar('\n'); 438 } 439 sysctl_all(mib, len); 440 } else { 441 i = show_var(mib, len, false); 442 if (!i && !bflag) 443 putchar('\n'); 444 } 445 return (0); 446 } 447 448 /* 449 * We have a new value to set. Check its validity and parse if numeric. 450 */ 451 if ((kind & CTLTYPE) == CTLTYPE_NODE) { 452 warnx("oid '%s' isn't a leaf node%s", bufp, line); 453 return (1); 454 } 455 456 if (!(kind & CTLFLAG_WR)) { 457 if (kind & CTLFLAG_TUN) { 458 warnx("oid '%s' is a read only tunable%s", bufp, line); 459 warnx("Tunable values are set in /boot/loader.conf"); 460 } else 461 warnx("oid '%s' is read only%s", bufp, line); 462 return (1); 463 } 464 465 switch (kind & CTLTYPE) { 466 case CTLTYPE_INT: 467 case CTLTYPE_UINT: 468 case CTLTYPE_LONG: 469 case CTLTYPE_ULONG: 470 case CTLTYPE_S8: 471 case CTLTYPE_S16: 472 case CTLTYPE_S32: 473 case CTLTYPE_S64: 474 case CTLTYPE_U8: 475 case CTLTYPE_U16: 476 case CTLTYPE_U32: 477 case CTLTYPE_U64: 478 if (strlen(newvalstr) == 0) { 479 warnx("empty numeric value"); 480 return (1); 481 } 482 /* FALLTHROUGH */ 483 case CTLTYPE_STRING: 484 break; 485 default: 486 warnx("oid '%s' is type %d, cannot set that%s", 487 bufp, kind & CTLTYPE, line); 488 return (1); 489 } 490 491 newbuf = NULL; 492 493 switch (kind & CTLTYPE) { 494 case CTLTYPE_STRING: 495 newval = newvalstr; 496 break; 497 default: 498 newsize = 0; 499 while ((cp = strsep(&newvalstr, " ,")) != NULL) { 500 if (*cp == '\0') 501 continue; 502 if (!parse_numeric(cp, fmt, kind, &newbuf, &newsize)) { 503 warnx("invalid %s '%s'%s", 504 ctl_typename[kind & CTLTYPE], cp, line); 505 free(newbuf); 506 return (1); 507 } 508 } 509 newval = newbuf; 510 break; 511 } 512 513 /* 514 * Show the current value, then set and show the new value. 515 */ 516 i = show_var(mib, len, false); 517 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) { 518 free(newbuf); 519 if (!i && !bflag) 520 putchar('\n'); 521 switch (errno) { 522 case EOPNOTSUPP: 523 warnx("%s: value is not available%s", 524 string, line); 525 return (1); 526 case ENOTDIR: 527 warnx("%s: specification is incomplete%s", 528 string, line); 529 return (1); 530 case ENOMEM: 531 warnx("%s: type is unknown to this program%s", 532 string, line); 533 return (1); 534 default: 535 warn("%s%s", string, line); 536 return (1); 537 } 538 } 539 free(newbuf); 540 if (!bflag) 541 printf(" -> "); 542 i = nflag; 543 nflag = 1; 544 j = show_var(mib, len, false); 545 if (!j && !bflag) 546 putchar('\n'); 547 nflag = i; 548 549 return (0); 550 } 551 552 static int 553 parsefile(const char *filename) 554 { 555 FILE *file; 556 char line[BUFSIZ], *p, *pq, *pdq; 557 int warncount = 0, lineno = 0; 558 559 file = fopen(filename, "r"); 560 if (file == NULL) 561 err(EX_NOINPUT, "%s", filename); 562 while (fgets(line, sizeof(line), file) != NULL) { 563 lineno++; 564 p = line; 565 pq = strchr(line, '\''); 566 pdq = strchr(line, '\"'); 567 /* Replace the first # with \0. */ 568 while((p = strchr(p, '#')) != NULL) { 569 if (pq != NULL && p > pq) { 570 if ((p = strchr(pq+1, '\'')) != NULL) 571 *(++p) = '\0'; 572 break; 573 } else if (pdq != NULL && p > pdq) { 574 if ((p = strchr(pdq+1, '\"')) != NULL) 575 *(++p) = '\0'; 576 break; 577 } else if (p == line || *(p-1) != '\\') { 578 *p = '\0'; 579 break; 580 } 581 p++; 582 } 583 /* Trim spaces */ 584 p = line + strlen(line) - 1; 585 while (p >= line && isspace((int)*p)) { 586 *p = '\0'; 587 p--; 588 } 589 p = line; 590 while (isspace((int)*p)) 591 p++; 592 if (*p == '\0') 593 continue; 594 else 595 warncount += parse(p, lineno); 596 } 597 fclose(file); 598 599 return (warncount); 600 } 601 602 /* These functions will dump out various interesting structures. */ 603 604 static int 605 S_clockinfo(size_t l2, void *p) 606 { 607 struct clockinfo *ci = (struct clockinfo*)p; 608 609 if (l2 != sizeof(*ci)) { 610 warnx("S_clockinfo %zu != %zu", l2, sizeof(*ci)); 611 return (1); 612 } 613 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" : 614 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }", 615 ci->hz, ci->tick, ci->profhz, ci->stathz); 616 return (0); 617 } 618 619 static int 620 S_loadavg(size_t l2, void *p) 621 { 622 struct loadavg *tv = (struct loadavg*)p; 623 624 if (l2 != sizeof(*tv)) { 625 warnx("S_loadavg %zu != %zu", l2, sizeof(*tv)); 626 return (1); 627 } 628 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }", 629 (double)tv->ldavg[0]/(double)tv->fscale, 630 (double)tv->ldavg[1]/(double)tv->fscale, 631 (double)tv->ldavg[2]/(double)tv->fscale); 632 return (0); 633 } 634 635 static int 636 S_timeval(size_t l2, void *p) 637 { 638 struct timeval *tv = (struct timeval*)p; 639 time_t tv_sec; 640 char *p1, *p2; 641 642 if (l2 != sizeof(*tv)) { 643 warnx("S_timeval %zu != %zu", l2, sizeof(*tv)); 644 return (1); 645 } 646 printf(hflag ? "{ sec = %'jd, usec = %'ld } " : 647 "{ sec = %jd, usec = %ld } ", 648 (intmax_t)tv->tv_sec, tv->tv_usec); 649 tv_sec = tv->tv_sec; 650 p1 = strdup(ctime(&tv_sec)); 651 for (p2=p1; *p2 ; p2++) 652 if (*p2 == '\n') 653 *p2 = '\0'; 654 fputs(p1, stdout); 655 free(p1); 656 return (0); 657 } 658 659 static int 660 S_vmtotal(size_t l2, void *p) 661 { 662 struct vmtotal *v; 663 int pageKilo; 664 665 if (l2 != sizeof(*v)) { 666 warnx("S_vmtotal %zu != %zu", l2, sizeof(*v)); 667 return (1); 668 } 669 670 v = p; 671 pageKilo = getpagesize() / 1024; 672 673 #define pg2k(a) ((uintmax_t)(a) * pageKilo) 674 printf("\nSystem wide totals computed every five seconds:" 675 " (values in kilobytes)\n"); 676 printf("===============================================\n"); 677 printf("Processes:\t\t(RUNQ: %d Disk Wait: %d Page Wait: " 678 "%d Sleep: %d)\n", 679 v->t_rq, v->t_dw, v->t_pw, v->t_sl); 680 printf("Virtual Memory:\t\t(Total: %juK Active: %juK)\n", 681 pg2k(v->t_vm), pg2k(v->t_avm)); 682 printf("Real Memory:\t\t(Total: %juK Active: %juK)\n", 683 pg2k(v->t_rm), pg2k(v->t_arm)); 684 printf("Shared Virtual Memory:\t(Total: %juK Active: %juK)\n", 685 pg2k(v->t_vmshr), pg2k(v->t_avmshr)); 686 printf("Shared Real Memory:\t(Total: %juK Active: %juK)\n", 687 pg2k(v->t_rmshr), pg2k(v->t_armshr)); 688 printf("Free Memory:\t%juK", pg2k(v->t_free)); 689 return (0); 690 } 691 692 static int 693 S_input_id(size_t l2, void *p) 694 { 695 struct input_id *id = p; 696 697 if (l2 != sizeof(*id)) { 698 warnx("S_input_id %zu != %zu", l2, sizeof(*id)); 699 return (1); 700 } 701 702 printf("{ bustype = 0x%04x, vendor = 0x%04x, " 703 "product = 0x%04x, version = 0x%04x }", 704 id->bustype, id->vendor, id->product, id->version); 705 return (0); 706 } 707 708 static int 709 S_pagesizes(size_t l2, void *p) 710 { 711 char buf[256]; 712 u_long *ps; 713 size_t l; 714 int i; 715 716 l = snprintf(buf, sizeof(buf), "{ "); 717 ps = p; 718 for (i = 0; i * sizeof(*ps) < l2 && ps[i] != 0 && l < sizeof(buf); 719 i++) { 720 l += snprintf(&buf[l], sizeof(buf) - l, 721 "%s%lu", i == 0 ? "" : ", ", ps[i]); 722 } 723 if (l < sizeof(buf)) 724 (void)snprintf(&buf[l], sizeof(buf) - l, " }"); 725 726 printf("%s", buf); 727 728 return (0); 729 } 730 731 #ifdef __amd64__ 732 static int 733 S_efi_map(size_t l2, void *p) 734 { 735 struct efi_map_header *efihdr; 736 struct efi_md *map; 737 const char *type; 738 size_t efisz; 739 int ndesc, i; 740 741 static const char * const types[] = { 742 [EFI_MD_TYPE_NULL] = "Reserved", 743 [EFI_MD_TYPE_CODE] = "LoaderCode", 744 [EFI_MD_TYPE_DATA] = "LoaderData", 745 [EFI_MD_TYPE_BS_CODE] = "BootServicesCode", 746 [EFI_MD_TYPE_BS_DATA] = "BootServicesData", 747 [EFI_MD_TYPE_RT_CODE] = "RuntimeServicesCode", 748 [EFI_MD_TYPE_RT_DATA] = "RuntimeServicesData", 749 [EFI_MD_TYPE_FREE] = "ConventionalMemory", 750 [EFI_MD_TYPE_BAD] = "UnusableMemory", 751 [EFI_MD_TYPE_RECLAIM] = "ACPIReclaimMemory", 752 [EFI_MD_TYPE_FIRMWARE] = "ACPIMemoryNVS", 753 [EFI_MD_TYPE_IOMEM] = "MemoryMappedIO", 754 [EFI_MD_TYPE_IOPORT] = "MemoryMappedIOPortSpace", 755 [EFI_MD_TYPE_PALCODE] = "PalCode", 756 [EFI_MD_TYPE_PERSISTENT] = "PersistentMemory", 757 }; 758 759 /* 760 * Memory map data provided by UEFI via the GetMemoryMap 761 * Boot Services API. 762 */ 763 if (l2 < sizeof(*efihdr)) { 764 warnx("S_efi_map length less than header"); 765 return (1); 766 } 767 efihdr = p; 768 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf; 769 map = (struct efi_md *)((uint8_t *)efihdr + efisz); 770 771 if (efihdr->descriptor_size == 0) 772 return (0); 773 if (l2 != efisz + efihdr->memory_size) { 774 warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz + 775 efihdr->memory_size); 776 return (1); 777 } 778 ndesc = efihdr->memory_size / efihdr->descriptor_size; 779 780 printf("\n%23s %12s %12s %8s %4s", 781 "Type", "Physical", "Virtual", "#Pages", "Attr"); 782 783 for (i = 0; i < ndesc; i++, 784 map = efi_next_descriptor(map, efihdr->descriptor_size)) { 785 type = NULL; 786 if (map->md_type < nitems(types)) 787 type = types[map->md_type]; 788 if (type == NULL) 789 type = "<INVALID>"; 790 printf("\n%23s %012jx %12p %08jx ", type, 791 (uintmax_t)map->md_phys, map->md_virt, 792 (uintmax_t)map->md_pages); 793 if (map->md_attr & EFI_MD_ATTR_UC) 794 printf("UC "); 795 if (map->md_attr & EFI_MD_ATTR_WC) 796 printf("WC "); 797 if (map->md_attr & EFI_MD_ATTR_WT) 798 printf("WT "); 799 if (map->md_attr & EFI_MD_ATTR_WB) 800 printf("WB "); 801 if (map->md_attr & EFI_MD_ATTR_UCE) 802 printf("UCE "); 803 if (map->md_attr & EFI_MD_ATTR_WP) 804 printf("WP "); 805 if (map->md_attr & EFI_MD_ATTR_RP) 806 printf("RP "); 807 if (map->md_attr & EFI_MD_ATTR_XP) 808 printf("XP "); 809 if (map->md_attr & EFI_MD_ATTR_RT) 810 printf("RUNTIME"); 811 } 812 return (0); 813 } 814 #endif 815 816 #if defined(__amd64__) || defined(__i386__) 817 static int 818 S_bios_smap_xattr(size_t l2, void *p) 819 { 820 struct bios_smap_xattr *smap, *end; 821 822 if (l2 % sizeof(*smap) != 0) { 823 warnx("S_bios_smap_xattr %zu is not a multiple of %zu", l2, 824 sizeof(*smap)); 825 return (1); 826 } 827 828 end = (struct bios_smap_xattr *)((char *)p + l2); 829 for (smap = p; smap < end; smap++) 830 printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx", 831 smap->type, smap->xattr, (uintmax_t)smap->base, 832 (uintmax_t)smap->length); 833 return (0); 834 } 835 #endif 836 837 static int 838 strIKtoi(const char *str, char **endptrp, const char *fmt) 839 { 840 int kelv; 841 float temp; 842 size_t len; 843 const char *p; 844 int prec, i; 845 846 assert(errno == 0); 847 848 len = strlen(str); 849 /* caller already checked this */ 850 assert(len > 0); 851 852 /* 853 * A format of "IK" is in deciKelvin. A format of "IK3" is in 854 * milliKelvin. The single digit following IK is log10 of the 855 * multiplying factor to convert Kelvin into the untis of this sysctl, 856 * or the dividing factor to convert the sysctl value to Kelvin. Numbers 857 * larger than 6 will run into precision issues with 32-bit integers. 858 * Characters that aren't ASCII digits after the 'K' are ignored. No 859 * localization is present because this is an interface from the kernel 860 * to this program (eg not an end-user interface), so isdigit() isn't 861 * used here. 862 */ 863 if (fmt[2] != '\0' && fmt[2] >= '0' && fmt[2] <= '9') 864 prec = fmt[2] - '0'; 865 else 866 prec = 1; 867 p = &str[len - 1]; 868 if (*p == 'C' || *p == 'F' || *p == 'K') { 869 temp = strtof(str, endptrp); 870 if (*endptrp != str && *endptrp == p && errno == 0) { 871 if (*p == 'F') 872 temp = (temp - 32) * 5 / 9; 873 *endptrp = NULL; 874 if (*p != 'K') 875 temp += 273.15; 876 for (i = 0; i < prec; i++) 877 temp *= 10.0; 878 return ((int)(temp + 0.5)); 879 } 880 } else { 881 /* No unit specified -> treat it as a raw number */ 882 kelv = (int)strtol(str, endptrp, 10); 883 if (*endptrp != str && *endptrp == p && errno == 0) { 884 *endptrp = NULL; 885 return (kelv); 886 } 887 } 888 889 errno = ERANGE; 890 return (0); 891 } 892 893 /* 894 * These functions uses a presently undocumented interface to the kernel 895 * to walk the tree and get the type so it can print the value. 896 * This interface is under work and consideration, and should probably 897 * be killed with a big axe by the first person who can find the time. 898 * (be aware though, that the proper interface isn't as obvious as it 899 * may seem, there are various conflicting requirements. 900 */ 901 902 static int 903 name2oid(const char *name, int *oidp) 904 { 905 int oid[2]; 906 int i; 907 size_t j; 908 909 oid[0] = CTL_SYSCTL; 910 oid[1] = CTL_SYSCTL_NAME2OID; 911 912 j = CTL_MAXNAME * sizeof(int); 913 i = sysctl(oid, 2, oidp, &j, name, strlen(name)); 914 if (i < 0) 915 return (i); 916 j /= sizeof(int); 917 return (j); 918 } 919 920 static int 921 oidfmt(int *oid, int len, char *fmt, u_int *kind) 922 { 923 int qoid[CTL_MAXNAME+2]; 924 u_char buf[BUFSIZ]; 925 int i; 926 size_t j; 927 928 qoid[0] = CTL_SYSCTL; 929 qoid[1] = CTL_SYSCTL_OIDFMT; 930 memcpy(qoid + 2, oid, len * sizeof(int)); 931 932 j = sizeof(buf); 933 i = sysctl(qoid, len + 2, buf, &j, 0, 0); 934 if (i) 935 err(1, "sysctl fmt %d %zu %d", i, j, errno); 936 937 if (kind) 938 *kind = *(u_int *)buf; 939 940 if (fmt) 941 strcpy(fmt, (char *)(buf + sizeof(u_int))); 942 return (0); 943 } 944 945 /* 946 * This formats and outputs the value of one variable 947 * 948 * Returns zero if anything was actually output. 949 * Returns one if didn't know what to do with this. 950 * Return minus one if we had errors. 951 */ 952 static int 953 show_var(int *oid, int nlen, bool honor_skip) 954 { 955 static int skip_len = 0, skip_oid[CTL_MAXNAME]; 956 u_char buf[BUFSIZ], *val, *oval, *p; 957 char name[BUFSIZ], fmt[BUFSIZ]; 958 const char *sep, *sep1, *prntype; 959 int qoid[CTL_MAXNAME+2]; 960 uintmax_t umv; 961 intmax_t mv; 962 int i, hexlen, sign, ctltype; 963 size_t intlen; 964 size_t j, len; 965 u_int kind; 966 float base; 967 int (*func)(size_t, void *); 968 int prec; 969 970 /* Silence GCC. */ 971 umv = mv = intlen = 0; 972 973 bzero(buf, BUFSIZ); 974 bzero(fmt, BUFSIZ); 975 bzero(name, BUFSIZ); 976 qoid[0] = CTL_SYSCTL; 977 memcpy(qoid + 2, oid, nlen * sizeof(int)); 978 979 qoid[1] = CTL_SYSCTL_NAME; 980 j = sizeof(name); 981 i = sysctl(qoid, nlen + 2, name, &j, 0, 0); 982 if (i || !j) 983 err(1, "sysctl name %d %zu %d", i, j, errno); 984 985 oidfmt(oid, nlen, fmt, &kind); 986 /* if Wflag then only list sysctls that are writeable and not stats. */ 987 if (Wflag && ((kind & CTLFLAG_WR) == 0 || (kind & CTLFLAG_STATS) != 0)) 988 return (1); 989 990 /* if Tflag then only list sysctls that are tuneables. */ 991 if (Tflag && (kind & CTLFLAG_TUN) == 0) 992 return (1); 993 994 if (Nflag) { 995 printf("%s", name); 996 return (0); 997 } 998 999 if (eflag) 1000 sep = "="; 1001 else 1002 sep = ": "; 1003 1004 ctltype = (kind & CTLTYPE); 1005 if (tflag || dflag) { 1006 if (!nflag) 1007 printf("%s%s", name, sep); 1008 if (ctl_typename[ctltype] != NULL) 1009 prntype = ctl_typename[ctltype]; 1010 else 1011 prntype = "unknown"; 1012 if (tflag && dflag) 1013 printf("%s%s", prntype, sep); 1014 else if (tflag) { 1015 printf("%s", prntype); 1016 return (0); 1017 } 1018 qoid[1] = CTL_SYSCTL_OIDDESCR; 1019 j = sizeof(buf); 1020 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0); 1021 printf("%s", buf); 1022 return (0); 1023 } 1024 1025 /* keep track of encountered skip nodes, ignoring descendants */ 1026 if (skip_len == 0 && (kind & CTLFLAG_SKIP) != 0) { 1027 /* Save this oid so we can skip descendants. */ 1028 skip_len = nlen * sizeof(int); 1029 memcpy(skip_oid, oid, skip_len); 1030 } 1031 1032 /* bail before fetching the value if we're honoring skip */ 1033 if (honor_skip) { 1034 if (0 < skip_len && skip_len <= nlen * (int)sizeof(int) && 1035 memcmp(skip_oid, oid, skip_len) == 0) 1036 return (1); 1037 /* Not a skip node or descendant of a skip node. */ 1038 skip_len = 0; 1039 } 1040 1041 /* don't fetch opaques that we don't know how to print */ 1042 if (ctltype == CTLTYPE_OPAQUE) { 1043 if (strcmp(fmt, "S,clockinfo") == 0) 1044 func = S_clockinfo; 1045 else if (strcmp(fmt, "S,timeval") == 0) 1046 func = S_timeval; 1047 else if (strcmp(fmt, "S,loadavg") == 0) 1048 func = S_loadavg; 1049 else if (strcmp(fmt, "S,vmtotal") == 0) 1050 func = S_vmtotal; 1051 else if (strcmp(fmt, "S,input_id") == 0) 1052 func = S_input_id; 1053 else if (strcmp(fmt, "S,pagesizes") == 0) 1054 func = S_pagesizes; 1055 #ifdef __amd64__ 1056 else if (strcmp(fmt, "S,efi_map_header") == 0) 1057 func = S_efi_map; 1058 #endif 1059 #if defined(__amd64__) || defined(__i386__) 1060 else if (strcmp(fmt, "S,bios_smap_xattr") == 0) 1061 func = S_bios_smap_xattr; 1062 #endif 1063 else { 1064 func = NULL; 1065 if (!bflag && !oflag && !xflag) 1066 return (1); 1067 } 1068 } 1069 1070 /* find an estimate of how much we need for this var */ 1071 if (Bflag) 1072 j = Bflag; 1073 else { 1074 j = 0; 1075 i = sysctl(oid, nlen, 0, &j, 0, 0); 1076 j += j; /* we want to be sure :-) */ 1077 } 1078 1079 val = oval = malloc(j + 1); 1080 if (val == NULL) { 1081 warnx("malloc failed"); 1082 return (1); 1083 } 1084 len = j; 1085 i = sysctl(oid, nlen, val, &len, 0, 0); 1086 if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) { 1087 free(oval); 1088 return (1); 1089 } 1090 1091 if (bflag) { 1092 fwrite(val, 1, len, stdout); 1093 free(oval); 1094 return (0); 1095 } 1096 val[len] = '\0'; 1097 p = val; 1098 sign = ctl_sign[ctltype]; 1099 intlen = ctl_size[ctltype]; 1100 1101 switch (ctltype) { 1102 case CTLTYPE_STRING: 1103 if (!nflag) 1104 printf("%s%s", name, sep); 1105 printf("%.*s", (int)len, p); 1106 free(oval); 1107 return (0); 1108 1109 case CTLTYPE_INT: 1110 case CTLTYPE_UINT: 1111 case CTLTYPE_LONG: 1112 case CTLTYPE_ULONG: 1113 case CTLTYPE_S8: 1114 case CTLTYPE_S16: 1115 case CTLTYPE_S32: 1116 case CTLTYPE_S64: 1117 case CTLTYPE_U8: 1118 case CTLTYPE_U16: 1119 case CTLTYPE_U32: 1120 case CTLTYPE_U64: 1121 if (!nflag) 1122 printf("%s%s", name, sep); 1123 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4; 1124 sep1 = ""; 1125 while (len >= intlen) { 1126 switch (kind & CTLTYPE) { 1127 case CTLTYPE_INT: 1128 case CTLTYPE_UINT: 1129 umv = *(u_int *)p; 1130 mv = *(int *)p; 1131 break; 1132 case CTLTYPE_LONG: 1133 case CTLTYPE_ULONG: 1134 umv = *(u_long *)p; 1135 mv = *(long *)p; 1136 break; 1137 case CTLTYPE_S8: 1138 case CTLTYPE_U8: 1139 umv = *(uint8_t *)p; 1140 mv = *(int8_t *)p; 1141 break; 1142 case CTLTYPE_S16: 1143 case CTLTYPE_U16: 1144 umv = *(uint16_t *)p; 1145 mv = *(int16_t *)p; 1146 break; 1147 case CTLTYPE_S32: 1148 case CTLTYPE_U32: 1149 umv = *(uint32_t *)p; 1150 mv = *(int32_t *)p; 1151 break; 1152 case CTLTYPE_S64: 1153 case CTLTYPE_U64: 1154 umv = *(uint64_t *)p; 1155 mv = *(int64_t *)p; 1156 break; 1157 } 1158 fputs(sep1, stdout); 1159 if (xflag) 1160 printf("%#0*jx", hexlen, umv); 1161 else if (!sign) 1162 printf(hflag ? "%'ju" : "%ju", umv); 1163 else if (fmt[1] == 'K') { 1164 if (mv < 0) 1165 printf("%jd", mv); 1166 else { 1167 /* 1168 * See strIKtoi for details on fmt. 1169 */ 1170 prec = 1; 1171 if (fmt[2] != '\0') 1172 prec = fmt[2] - '0'; 1173 base = 1.0; 1174 for (int i = 0; i < prec; i++) 1175 base *= 10.0; 1176 printf("%.*fC", prec, 1177 (float)mv / base - 273.15); 1178 } 1179 } else 1180 printf(hflag ? "%'jd" : "%jd", mv); 1181 sep1 = " "; 1182 len -= intlen; 1183 p += intlen; 1184 } 1185 free(oval); 1186 return (0); 1187 1188 case CTLTYPE_OPAQUE: 1189 i = 0; 1190 if (func) { 1191 if (!nflag) 1192 printf("%s%s", name, sep); 1193 i = (*func)(len, p); 1194 free(oval); 1195 return (i); 1196 } 1197 /* FALLTHROUGH */ 1198 default: 1199 if (!oflag && !xflag) { 1200 free(oval); 1201 return (1); 1202 } 1203 if (!nflag) 1204 printf("%s%s", name, sep); 1205 printf("Format:%s Length:%zu Dump:0x", fmt, len); 1206 while (len-- && (xflag || p < val + 16)) 1207 printf("%02x", *p++); 1208 if (!xflag && len > 16) 1209 printf("..."); 1210 free(oval); 1211 return (0); 1212 } 1213 free(oval); 1214 return (1); 1215 } 1216 1217 static int 1218 sysctl_all(int *oid, int len) 1219 { 1220 int name1[22], name2[22]; 1221 int i, j; 1222 size_t l1, l2; 1223 bool honor_skip = false; 1224 1225 name1[0] = CTL_SYSCTL; 1226 name1[1] = (oid != NULL || Nflag || dflag || tflag) ? 1227 CTL_SYSCTL_NEXTNOSKIP : CTL_SYSCTL_NEXT; 1228 l1 = 2; 1229 if (len) { 1230 memcpy(name1 + 2, oid, len * sizeof(int)); 1231 l1 += len; 1232 } else { 1233 name1[2] = CTL_KERN; 1234 l1++; 1235 } 1236 for (;;) { 1237 l2 = sizeof(name2); 1238 j = sysctl(name1, l1, name2, &l2, 0, 0); 1239 if (j < 0) { 1240 if (errno == ENOENT) 1241 return (0); 1242 else 1243 err(1, "sysctl(getnext) %d %zu", j, l2); 1244 } 1245 1246 l2 /= sizeof(int); 1247 1248 if (len < 0 || l2 < (unsigned int)len) 1249 return (0); 1250 1251 if (memcmp(name2, oid, len * sizeof(int)) != 0) 1252 return (0); 1253 1254 i = show_var(name2, l2, honor_skip); 1255 if (!i && !bflag) 1256 putchar('\n'); 1257 1258 memcpy(name1 + 2, name2, l2 * sizeof(int)); 1259 l1 = 2 + l2; 1260 honor_skip = true; 1261 } 1262 } 1263