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 65static const char *conffile; 66 67static int aflag, bflag, Bflag, dflag, eflag, hflag, iflag; 68static int Nflag, nflag, oflag, qflag, tflag, Tflag, Wflag, xflag; 69 70static int oidfmt(int *, int, char *, u_int *); 71static int parsefile(const char *); 72static int parse(const char *, int); 73static int show_var(int *, int, bool); 74static int sysctl_all(int *oid, int len); 75static int name2oid(const char *, int *); 76 77static int strIKtoi(const char *, char **, const char *); 78 79static 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 88static 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 103static 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 121static void 122usage(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 131int 132main(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 */ 233static bool 234parse_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 */ 344static int 345parse(const char *string, int lineno) 346{ 347 int len, i, j, save_errno; 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, 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 = strdup(string); 369 bufp = strsep(&cp, "=:"); 370 if (cp != NULL) { 371 /* Tflag just lists tunables, do not allow assignment */ 372 if (Tflag || Wflag) { 373 warnx("Can't set variables when using -T or -W"); 374 usage(); 375 } 376 /* Trim whitespace before the value. */ 377 while (isspace(*cp)) 378 cp++; 379 /* Strip a pair of " or ' if any. */ 380 switch (*cp) { 381 case '\"': 382 case '\'': 383 if (cp[strlen(cp) - 1] == *cp) 384 cp[strlen(cp) - 1] = '\0'; 385 cp++; 386 } 387 newvalstr = cp; 388 newsize = strlen(cp); 389 } 390 /* Trim whitespace after the name. */ 391 cp = bufp + strlen(bufp) - 1; 392 while (cp >= bufp && isspace((int)*cp)) { 393 *cp = '\0'; 394 cp--; 395 } 396 397 /* 398 * Check the name is a useable oid. 399 */ 400 len = name2oid(bufp, mib); 401 if (len < 0) { 402 if (iflag) { 403 free(buf); 404 return (0); 405 } 406 if (!qflag) { 407 if (errno == ENOENT) { 408 warnx("unknown oid '%s'%s", bufp, line); 409 } else { 410 warn("unknown oid '%s'%s", bufp, line); 411 } 412 } 413 free(buf); 414 return (1); 415 } 416 417 if (oidfmt(mib, len, fmt, &kind)) { 418 warn("couldn't find format of oid '%s'%s", bufp, line); 419 free(buf); 420 if (iflag) 421 return (1); 422 else 423 exit(1); 424 } 425 426 /* 427 * We have a useable oid to work with. If there is no value given, 428 * show the node and its children. Otherwise, set the new value. 429 */ 430 if (newvalstr == NULL || dflag) { 431 free(buf); 432 if ((kind & CTLTYPE) == CTLTYPE_NODE) { 433 if (dflag) { 434 i = show_var(mib, len, false); 435 if (!i && !bflag) 436 putchar('\n'); 437 } 438 sysctl_all(mib, len); 439 } else { 440 i = show_var(mib, len, false); 441 if (!i && !bflag) 442 putchar('\n'); 443 } 444 return (0); 445 } 446 447 /* 448 * We have a new value to set. Check its validity and parse if numeric. 449 */ 450 if ((kind & CTLTYPE) == CTLTYPE_NODE) { 451 warnx("oid '%s' isn't a leaf node%s", bufp, line); 452 free(buf); 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 free(buf); 463 return (1); 464 } 465 466 switch (kind & CTLTYPE) { 467 case CTLTYPE_INT: 468 case CTLTYPE_UINT: 469 case CTLTYPE_LONG: 470 case CTLTYPE_ULONG: 471 case CTLTYPE_S8: 472 case CTLTYPE_S16: 473 case CTLTYPE_S32: 474 case CTLTYPE_S64: 475 case CTLTYPE_U8: 476 case CTLTYPE_U16: 477 case CTLTYPE_U32: 478 case CTLTYPE_U64: 479 if (strlen(newvalstr) == 0) { 480 warnx("empty numeric value"); 481 free(buf); 482 return (1); 483 } 484 /* FALLTHROUGH */ 485 case CTLTYPE_STRING: 486 break; 487 default: 488 warnx("oid '%s' is type %d, cannot set that%s", 489 bufp, kind & CTLTYPE, line); 490 free(buf); 491 return (1); 492 } 493 494 newbuf = NULL; 495 496 switch (kind & CTLTYPE) { 497 case CTLTYPE_STRING: 498 newval = newvalstr; 499 break; 500 default: 501 newsize = 0; 502 while ((cp = strsep(&newvalstr, " ,")) != NULL) { 503 if (*cp == '\0') 504 continue; 505 if (!parse_numeric(cp, fmt, kind, &newbuf, &newsize)) { 506 warnx("invalid %s '%s'%s", 507 ctl_typename[kind & CTLTYPE], cp, line); 508 free(newbuf); 509 free(buf); 510 return (1); 511 } 512 } 513 newval = newbuf; 514 break; 515 } 516 517 /* 518 * Show the current value, then set and show the new value. 519 */ 520 i = show_var(mib, len, false); 521 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) { 522 save_errno = errno; 523 free(newbuf); 524 free(buf); 525 if (!i && !bflag) 526 putchar('\n'); 527 switch (save_errno) { 528 case EOPNOTSUPP: 529 warnx("%s: value is not available%s", 530 string, line); 531 return (1); 532 case ENOTDIR: 533 warnx("%s: specification is incomplete%s", 534 string, line); 535 return (1); 536 case ENOMEM: 537 warnx("%s: type is unknown to this program%s", 538 string, line); 539 return (1); 540 default: 541 warnc(save_errno, "%s%s", string, line); 542 return (1); 543 } 544 } 545 free(newbuf); 546 free(buf); 547 if (!bflag) 548 printf(" -> "); 549 i = nflag; 550 nflag = 1; 551 j = show_var(mib, len, false); 552 if (!j && !bflag) 553 putchar('\n'); 554 nflag = i; 555 556 return (0); 557} 558 559static int 560parsefile(const char *filename) 561{ 562 FILE *file; 563 char line[BUFSIZ], *p, *pq, *pdq; 564 int warncount = 0, lineno = 0; 565 566 file = fopen(filename, "r"); 567 if (file == NULL) 568 err(EX_NOINPUT, "%s", filename); 569 while (fgets(line, sizeof(line), file) != NULL) { 570 lineno++; 571 p = line; 572 pq = strchr(line, '\''); 573 pdq = strchr(line, '\"'); 574 /* Replace the first # with \0. */ 575 while((p = strchr(p, '#')) != NULL) { 576 if (pq != NULL && p > pq) { 577 if ((p = strchr(pq+1, '\'')) != NULL) 578 *(++p) = '\0'; 579 break; 580 } else if (pdq != NULL && p > pdq) { 581 if ((p = strchr(pdq+1, '\"')) != NULL) 582 *(++p) = '\0'; 583 break; 584 } else if (p == line || *(p-1) != '\\') { 585 *p = '\0'; 586 break; 587 } 588 p++; 589 } 590 /* Trim spaces */ 591 p = line + strlen(line) - 1; 592 while (p >= line && isspace((int)*p)) { 593 *p = '\0'; 594 p--; 595 } 596 p = line; 597 while (isspace((int)*p)) 598 p++; 599 if (*p == '\0') 600 continue; 601 else 602 warncount += parse(p, lineno); 603 } 604 fclose(file); 605 606 return (warncount); 607} 608 609/* These functions will dump out various interesting structures. */ 610 611static int 612S_clockinfo(size_t l2, void *p) 613{ 614 struct clockinfo *ci = (struct clockinfo*)p; 615 616 if (l2 != sizeof(*ci)) { 617 warnx("S_clockinfo %zu != %zu", l2, sizeof(*ci)); 618 return (1); 619 } 620 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" : 621 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }", 622 ci->hz, ci->tick, ci->profhz, ci->stathz); 623 return (0); 624} 625 626static int 627S_loadavg(size_t l2, void *p) 628{ 629 struct loadavg *tv = (struct loadavg*)p; 630 631 if (l2 != sizeof(*tv)) { 632 warnx("S_loadavg %zu != %zu", l2, sizeof(*tv)); 633 return (1); 634 } 635 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }", 636 (double)tv->ldavg[0]/(double)tv->fscale, 637 (double)tv->ldavg[1]/(double)tv->fscale, 638 (double)tv->ldavg[2]/(double)tv->fscale); 639 return (0); 640} 641 642static int 643S_timeval(size_t l2, void *p) 644{ 645 struct timeval *tv = (struct timeval*)p; 646 time_t tv_sec; 647 char *p1, *p2; 648 649 if (l2 != sizeof(*tv)) { 650 warnx("S_timeval %zu != %zu", l2, sizeof(*tv)); 651 return (1); 652 } 653 printf(hflag ? "{ sec = %'jd, usec = %'ld } " : 654 "{ sec = %jd, usec = %ld } ", 655 (intmax_t)tv->tv_sec, tv->tv_usec); 656 tv_sec = tv->tv_sec; 657 p1 = strdup(ctime(&tv_sec)); 658 for (p2=p1; *p2 ; p2++) 659 if (*p2 == '\n') 660 *p2 = '\0'; 661 fputs(p1, stdout); 662 free(p1); 663 return (0); 664} 665 666static int 667S_vmtotal(size_t l2, void *p) 668{ 669 struct vmtotal *v; 670 int pageKilo; 671 672 if (l2 != sizeof(*v)) { 673 warnx("S_vmtotal %zu != %zu", l2, sizeof(*v)); 674 return (1); 675 } 676 677 v = p; 678 pageKilo = getpagesize() / 1024; 679 680#define pg2k(a) ((uintmax_t)(a) * pageKilo) 681 printf("\nSystem wide totals computed every five seconds:" 682 " (values in kilobytes)\n"); 683 printf("===============================================\n"); 684 printf("Processes:\t\t(RUNQ: %d Disk Wait: %d Page Wait: " 685 "%d Sleep: %d)\n", 686 v->t_rq, v->t_dw, v->t_pw, v->t_sl); 687 printf("Virtual Memory:\t\t(Total: %juK Active: %juK)\n", 688 pg2k(v->t_vm), pg2k(v->t_avm)); 689 printf("Real Memory:\t\t(Total: %juK Active: %juK)\n", 690 pg2k(v->t_rm), pg2k(v->t_arm)); 691 printf("Shared Virtual Memory:\t(Total: %juK Active: %juK)\n", 692 pg2k(v->t_vmshr), pg2k(v->t_avmshr)); 693 printf("Shared Real Memory:\t(Total: %juK Active: %juK)\n", 694 pg2k(v->t_rmshr), pg2k(v->t_armshr)); 695 printf("Free Memory:\t%juK", pg2k(v->t_free)); 696 return (0); 697} 698 699static int 700S_input_id(size_t l2, void *p) 701{ 702 struct input_id *id = p; 703 704 if (l2 != sizeof(*id)) { 705 warnx("S_input_id %zu != %zu", l2, sizeof(*id)); 706 return (1); 707 } 708 709 printf("{ bustype = 0x%04x, vendor = 0x%04x, " 710 "product = 0x%04x, version = 0x%04x }", 711 id->bustype, id->vendor, id->product, id->version); 712 return (0); 713} 714 715#ifdef __amd64__ 716static int 717S_efi_map(size_t l2, void *p) 718{ 719 struct efi_map_header *efihdr; 720 struct efi_md *map; 721 const char *type; 722 size_t efisz; 723 int ndesc, i; 724 725 static const char * const types[] = { 726 [EFI_MD_TYPE_NULL] = "Reserved", 727 [EFI_MD_TYPE_CODE] = "LoaderCode", 728 [EFI_MD_TYPE_DATA] = "LoaderData", 729 [EFI_MD_TYPE_BS_CODE] = "BootServicesCode", 730 [EFI_MD_TYPE_BS_DATA] = "BootServicesData", 731 [EFI_MD_TYPE_RT_CODE] = "RuntimeServicesCode", 732 [EFI_MD_TYPE_RT_DATA] = "RuntimeServicesData", 733 [EFI_MD_TYPE_FREE] = "ConventionalMemory", 734 [EFI_MD_TYPE_BAD] = "UnusableMemory", 735 [EFI_MD_TYPE_RECLAIM] = "ACPIReclaimMemory", 736 [EFI_MD_TYPE_FIRMWARE] = "ACPIMemoryNVS", 737 [EFI_MD_TYPE_IOMEM] = "MemoryMappedIO", 738 [EFI_MD_TYPE_IOPORT] = "MemoryMappedIOPortSpace", 739 [EFI_MD_TYPE_PALCODE] = "PalCode", 740 [EFI_MD_TYPE_PERSISTENT] = "PersistentMemory", 741 }; 742 743 /* 744 * Memory map data provided by UEFI via the GetMemoryMap 745 * Boot Services API. 746 */ 747 if (l2 < sizeof(*efihdr)) { 748 warnx("S_efi_map length less than header"); 749 return (1); 750 } 751 efihdr = p; 752 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf; 753 map = (struct efi_md *)((uint8_t *)efihdr + efisz); 754 755 if (efihdr->descriptor_size == 0) 756 return (0); 757 if (l2 != efisz + efihdr->memory_size) { 758 warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz + 759 efihdr->memory_size); 760 return (1); 761 } 762 ndesc = efihdr->memory_size / efihdr->descriptor_size; 763 764 printf("\n%23s %12s %12s %8s %4s", 765 "Type", "Physical", "Virtual", "#Pages", "Attr"); 766 767 for (i = 0; i < ndesc; i++, 768 map = efi_next_descriptor(map, efihdr->descriptor_size)) { 769 type = NULL; 770 if (map->md_type < nitems(types)) 771 type = types[map->md_type]; 772 if (type == NULL) 773 type = "<INVALID>"; 774 printf("\n%23s %012jx %12p %08jx ", type, 775 (uintmax_t)map->md_phys, map->md_virt, 776 (uintmax_t)map->md_pages); 777 if (map->md_attr & EFI_MD_ATTR_UC) 778 printf("UC "); 779 if (map->md_attr & EFI_MD_ATTR_WC) 780 printf("WC "); 781 if (map->md_attr & EFI_MD_ATTR_WT) 782 printf("WT "); 783 if (map->md_attr & EFI_MD_ATTR_WB) 784 printf("WB "); 785 if (map->md_attr & EFI_MD_ATTR_UCE) 786 printf("UCE "); 787 if (map->md_attr & EFI_MD_ATTR_WP) 788 printf("WP "); 789 if (map->md_attr & EFI_MD_ATTR_RP) 790 printf("RP "); 791 if (map->md_attr & EFI_MD_ATTR_XP) 792 printf("XP "); 793 if (map->md_attr & EFI_MD_ATTR_RT) 794 printf("RUNTIME"); 795 } 796 return (0); 797} 798#endif 799 800#if defined(__amd64__) || defined(__i386__) 801static int 802S_bios_smap_xattr(size_t l2, void *p) 803{ 804 struct bios_smap_xattr *smap, *end; 805 806 if (l2 % sizeof(*smap) != 0) { 807 warnx("S_bios_smap_xattr %zu is not a multiple of %zu", l2, 808 sizeof(*smap)); 809 return (1); 810 } 811 812 end = (struct bios_smap_xattr *)((char *)p + l2); 813 for (smap = p; smap < end; smap++) 814 printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx", 815 smap->type, smap->xattr, (uintmax_t)smap->base, 816 (uintmax_t)smap->length); 817 return (0); 818} 819#endif 820 821static int 822strIKtoi(const char *str, char **endptrp, const char *fmt) 823{ 824 int kelv; 825 float temp; 826 size_t len; 827 const char *p; 828 int prec, i; 829 830 assert(errno == 0); 831 832 len = strlen(str); 833 /* caller already checked this */ 834 assert(len > 0); 835 836 /* 837 * A format of "IK" is in deciKelvin. A format of "IK3" is in 838 * milliKelvin. The single digit following IK is log10 of the 839 * multiplying factor to convert Kelvin into the untis of this sysctl, 840 * or the dividing factor to convert the sysctl value to Kelvin. Numbers 841 * larger than 6 will run into precision issues with 32-bit integers. 842 * Characters that aren't ASCII digits after the 'K' are ignored. No 843 * localization is present because this is an interface from the kernel 844 * to this program (eg not an end-user interface), so isdigit() isn't 845 * used here. 846 */ 847 if (fmt[2] != '\0' && fmt[2] >= '0' && fmt[2] <= '9') 848 prec = fmt[2] - '0'; 849 else 850 prec = 1; 851 p = &str[len - 1]; 852 if (*p == 'C' || *p == 'F' || *p == 'K') { 853 temp = strtof(str, endptrp); 854 if (*endptrp != str && *endptrp == p && errno == 0) { 855 if (*p == 'F') 856 temp = (temp - 32) * 5 / 9; 857 *endptrp = NULL; 858 if (*p != 'K') 859 temp += 273.15; 860 for (i = 0; i < prec; i++) 861 temp *= 10.0; 862 return ((int)(temp + 0.5)); 863 } 864 } else { 865 /* No unit specified -> treat it as a raw number */ 866 kelv = (int)strtol(str, endptrp, 10); 867 if (*endptrp != str && *endptrp == p && errno == 0) { 868 *endptrp = NULL; 869 return (kelv); 870 } 871 } 872 873 errno = ERANGE; 874 return (0); 875} 876 877/* 878 * These functions uses a presently undocumented interface to the kernel 879 * to walk the tree and get the type so it can print the value. 880 * This interface is under work and consideration, and should probably 881 * be killed with a big axe by the first person who can find the time. 882 * (be aware though, that the proper interface isn't as obvious as it 883 * may seem, there are various conflicting requirements. 884 */ 885 886static int 887name2oid(const char *name, int *oidp) 888{ 889 int oid[2]; 890 int i; 891 size_t j; 892 893 oid[0] = CTL_SYSCTL; 894 oid[1] = CTL_SYSCTL_NAME2OID; 895 896 j = CTL_MAXNAME * sizeof(int); 897 i = sysctl(oid, 2, oidp, &j, name, strlen(name)); 898 if (i < 0) 899 return (i); 900 j /= sizeof(int); 901 return (j); 902} 903 904static int 905oidfmt(int *oid, int len, char *fmt, u_int *kind) 906{ 907 int qoid[CTL_MAXNAME+2]; 908 u_char buf[BUFSIZ]; 909 int i; 910 size_t j; 911 912 qoid[0] = CTL_SYSCTL; 913 qoid[1] = CTL_SYSCTL_OIDFMT; 914 memcpy(qoid + 2, oid, len * sizeof(int)); 915 916 j = sizeof(buf); 917 i = sysctl(qoid, len + 2, buf, &j, 0, 0); 918 if (i) 919 err(1, "sysctl fmt %d %zu %d", i, j, errno); 920 921 if (kind) 922 *kind = *(u_int *)buf; 923 924 if (fmt) 925 strcpy(fmt, (char *)(buf + sizeof(u_int))); 926 return (0); 927} 928 929/* 930 * This formats and outputs the value of one variable 931 * 932 * Returns zero if anything was actually output. 933 * Returns one if didn't know what to do with this. 934 * Return minus one if we had errors. 935 */ 936static int 937show_var(int *oid, int nlen, bool honor_skip) 938{ 939 static int skip_len = 0, skip_oid[CTL_MAXNAME]; 940 u_char buf[BUFSIZ], *val, *oval, *p; 941 char name[BUFSIZ], fmt[BUFSIZ]; 942 const char *sep, *sep1, *prntype; 943 int qoid[CTL_MAXNAME+2]; 944 uintmax_t umv; 945 intmax_t mv; 946 int i, hexlen, sign, ctltype; 947 size_t intlen; 948 size_t j, len; 949 u_int kind; 950 float base; 951 int (*func)(size_t, void *); 952 int prec; 953 954 /* Silence GCC. */ 955 umv = mv = intlen = 0; 956 957 bzero(buf, BUFSIZ); 958 bzero(fmt, BUFSIZ); 959 bzero(name, BUFSIZ); 960 qoid[0] = CTL_SYSCTL; 961 memcpy(qoid + 2, oid, nlen * sizeof(int)); 962 963 qoid[1] = CTL_SYSCTL_NAME; 964 j = sizeof(name); 965 i = sysctl(qoid, nlen + 2, name, &j, 0, 0); 966 if (i || !j) 967 err(1, "sysctl name %d %zu %d", i, j, errno); 968 969 oidfmt(oid, nlen, fmt, &kind); 970 /* if Wflag then only list sysctls that are writeable and not stats. */ 971 if (Wflag && ((kind & CTLFLAG_WR) == 0 || (kind & CTLFLAG_STATS) != 0)) 972 return (1); 973 974 /* if Tflag then only list sysctls that are tuneables. */ 975 if (Tflag && (kind & CTLFLAG_TUN) == 0) 976 return (1); 977 978 if (Nflag) { 979 printf("%s", name); 980 return (0); 981 } 982 983 if (eflag) 984 sep = "="; 985 else 986 sep = ": "; 987 988 ctltype = (kind & CTLTYPE); 989 if (tflag || dflag) { 990 if (!nflag) 991 printf("%s%s", name, sep); 992 if (ctl_typename[ctltype] != NULL) 993 prntype = ctl_typename[ctltype]; 994 else 995 prntype = "unknown"; 996 if (tflag && dflag) 997 printf("%s%s", prntype, sep); 998 else if (tflag) { 999 printf("%s", prntype); 1000 return (0); 1001 } 1002 qoid[1] = CTL_SYSCTL_OIDDESCR; 1003 j = sizeof(buf); 1004 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0); 1005 printf("%s", buf); 1006 return (0); 1007 } 1008 1009 /* keep track of encountered skip nodes, ignoring descendants */ 1010 if (skip_len == 0 && (kind & CTLFLAG_SKIP) != 0) { 1011 /* Save this oid so we can skip descendants. */ 1012 skip_len = nlen * sizeof(int); 1013 memcpy(skip_oid, oid, skip_len); 1014 } 1015 1016 /* bail before fetching the value if we're honoring skip */ 1017 if (honor_skip) { 1018 if (0 < skip_len && skip_len <= nlen * (int)sizeof(int) && 1019 memcmp(skip_oid, oid, skip_len) == 0) 1020 return (1); 1021 /* Not a skip node or descendant of a skip node. */ 1022 skip_len = 0; 1023 } 1024 1025 /* don't fetch opaques that we don't know how to print */ 1026 if (ctltype == CTLTYPE_OPAQUE) { 1027 if (strcmp(fmt, "S,clockinfo") == 0) 1028 func = S_clockinfo; 1029 else if (strcmp(fmt, "S,timeval") == 0) 1030 func = S_timeval; 1031 else if (strcmp(fmt, "S,loadavg") == 0) 1032 func = S_loadavg; 1033 else if (strcmp(fmt, "S,vmtotal") == 0) 1034 func = S_vmtotal; 1035 else if (strcmp(fmt, "S,input_id") == 0) 1036 func = S_input_id; 1037#ifdef __amd64__ 1038 else if (strcmp(fmt, "S,efi_map_header") == 0) 1039 func = S_efi_map; 1040#endif 1041#if defined(__amd64__) || defined(__i386__) 1042 else if (strcmp(fmt, "S,bios_smap_xattr") == 0) 1043 func = S_bios_smap_xattr; 1044#endif 1045 else { 1046 func = NULL; 1047 if (!bflag && !oflag && !xflag) 1048 return (1); 1049 } 1050 } 1051 1052 /* find an estimate of how much we need for this var */ 1053 if (Bflag) 1054 j = Bflag; 1055 else { 1056 j = 0; 1057 i = sysctl(oid, nlen, 0, &j, 0, 0); 1058 j += j; /* we want to be sure :-) */ 1059 } 1060 1061 val = oval = malloc(j + 1); 1062 if (val == NULL) { 1063 warnx("malloc failed"); 1064 return (1); 1065 } 1066 len = j; 1067 i = sysctl(oid, nlen, val, &len, 0, 0); 1068 if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) { 1069 free(oval); 1070 return (1); 1071 } 1072 1073 if (bflag) { 1074 fwrite(val, 1, len, stdout); 1075 free(oval); 1076 return (0); 1077 } 1078 val[len] = '\0'; 1079 p = val; 1080 sign = ctl_sign[ctltype]; 1081 intlen = ctl_size[ctltype]; 1082 1083 switch (ctltype) { 1084 case CTLTYPE_STRING: 1085 if (!nflag) 1086 printf("%s%s", name, sep); 1087 printf("%.*s", (int)len, p); 1088 free(oval); 1089 return (0); 1090 1091 case CTLTYPE_INT: 1092 case CTLTYPE_UINT: 1093 case CTLTYPE_LONG: 1094 case CTLTYPE_ULONG: 1095 case CTLTYPE_S8: 1096 case CTLTYPE_S16: 1097 case CTLTYPE_S32: 1098 case CTLTYPE_S64: 1099 case CTLTYPE_U8: 1100 case CTLTYPE_U16: 1101 case CTLTYPE_U32: 1102 case CTLTYPE_U64: 1103 if (!nflag) 1104 printf("%s%s", name, sep); 1105 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4; 1106 sep1 = ""; 1107 while (len >= intlen) { 1108 switch (kind & CTLTYPE) { 1109 case CTLTYPE_INT: 1110 case CTLTYPE_UINT: 1111 umv = *(u_int *)p; 1112 mv = *(int *)p; 1113 break; 1114 case CTLTYPE_LONG: 1115 case CTLTYPE_ULONG: 1116 umv = *(u_long *)p; 1117 mv = *(long *)p; 1118 break; 1119 case CTLTYPE_S8: 1120 case CTLTYPE_U8: 1121 umv = *(uint8_t *)p; 1122 mv = *(int8_t *)p; 1123 break; 1124 case CTLTYPE_S16: 1125 case CTLTYPE_U16: 1126 umv = *(uint16_t *)p; 1127 mv = *(int16_t *)p; 1128 break; 1129 case CTLTYPE_S32: 1130 case CTLTYPE_U32: 1131 umv = *(uint32_t *)p; 1132 mv = *(int32_t *)p; 1133 break; 1134 case CTLTYPE_S64: 1135 case CTLTYPE_U64: 1136 umv = *(uint64_t *)p; 1137 mv = *(int64_t *)p; 1138 break; 1139 } 1140 fputs(sep1, stdout); 1141 if (xflag) 1142 printf("%#0*jx", hexlen, umv); 1143 else if (!sign) 1144 printf(hflag ? "%'ju" : "%ju", umv); 1145 else if (fmt[1] == 'K') { 1146 if (mv < 0) 1147 printf("%jd", mv); 1148 else { 1149 /* 1150 * See strIKtoi for details on fmt. 1151 */ 1152 prec = 1; 1153 if (fmt[2] != '\0') 1154 prec = fmt[2] - '0'; 1155 base = 1.0; 1156 for (int i = 0; i < prec; i++) 1157 base *= 10.0; 1158 printf("%.*fC", prec, 1159 (float)mv / base - 273.15); 1160 } 1161 } else 1162 printf(hflag ? "%'jd" : "%jd", mv); 1163 sep1 = " "; 1164 len -= intlen; 1165 p += intlen; 1166 } 1167 free(oval); 1168 return (0); 1169 1170 case CTLTYPE_OPAQUE: 1171 i = 0; 1172 if (func) { 1173 if (!nflag) 1174 printf("%s%s", name, sep); 1175 i = (*func)(len, p); 1176 free(oval); 1177 return (i); 1178 } 1179 /* FALLTHROUGH */ 1180 default: 1181 if (!oflag && !xflag) { 1182 free(oval); 1183 return (1); 1184 } 1185 if (!nflag) 1186 printf("%s%s", name, sep); 1187 printf("Format:%s Length:%zu Dump:0x", fmt, len); 1188 while (len-- && (xflag || p < val + 16)) 1189 printf("%02x", *p++); 1190 if (!xflag && len > 16) 1191 printf("..."); 1192 free(oval); 1193 return (0); 1194 } 1195 free(oval); 1196 return (1); 1197} 1198 1199static int 1200sysctl_all(int *oid, int len) 1201{ 1202 int name1[22], name2[22]; 1203 int i, j; 1204 size_t l1, l2; 1205 bool honor_skip = false; 1206 1207 name1[0] = CTL_SYSCTL; 1208 name1[1] = (oid != NULL || Nflag || dflag || tflag) ? 1209 CTL_SYSCTL_NEXTNOSKIP : CTL_SYSCTL_NEXT; 1210 l1 = 2; 1211 if (len) { 1212 memcpy(name1 + 2, oid, len * sizeof(int)); 1213 l1 += len; 1214 } else { 1215 name1[2] = CTL_KERN; 1216 l1++; 1217 } 1218 for (;;) { 1219 l2 = sizeof(name2); 1220 j = sysctl(name1, l1, name2, &l2, 0, 0); 1221 if (j < 0) { 1222 if (errno == ENOENT) 1223 return (0); 1224 else 1225 err(1, "sysctl(getnext) %d %zu", j, l2); 1226 } 1227 1228 l2 /= sizeof(int); 1229 1230 if (len < 0 || l2 < (unsigned int)len) 1231 return (0); 1232 1233 if (memcmp(name2, oid, len * sizeof(int)) != 0) 1234 return (0); 1235 1236 i = show_var(name2, l2, honor_skip); 1237 if (!i && !bflag) 1238 putchar('\n'); 1239 1240 memcpy(name1 + 2, name2, l2 * sizeof(int)); 1241 l1 = 2 + l2; 1242 honor_skip = true; 1243 } 1244} 1245