1 /* 2 * Copyright (c) 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef lint 31 static const char copyright[] = 32 "@(#) Copyright (c) 1993\n\ 33 The Regents of the University of California. All rights reserved.\n"; 34 #endif /* not lint */ 35 36 #ifndef lint 37 #if 0 38 static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93"; 39 #endif 40 static const char rcsid[] = 41 "$FreeBSD$"; 42 #endif /* not lint */ 43 44 #include <sys/param.h> 45 #include <sys/time.h> 46 #include <sys/resource.h> 47 #include <sys/stat.h> 48 #include <sys/sysctl.h> 49 #include <sys/vmmeter.h> 50 51 #ifdef __amd64__ 52 #include <sys/efi.h> 53 #include <machine/metadata.h> 54 #endif 55 56 #if defined(__amd64__) || defined(__i386__) 57 #include <machine/pc/bios.h> 58 #endif 59 60 #include <assert.h> 61 #include <ctype.h> 62 #include <err.h> 63 #include <errno.h> 64 #include <inttypes.h> 65 #include <locale.h> 66 #include <stdio.h> 67 #include <stdlib.h> 68 #include <string.h> 69 #include <sysexits.h> 70 #include <unistd.h> 71 72 static const char *conffile; 73 74 static int aflag, bflag, Bflag, dflag, eflag, hflag, iflag; 75 static int Nflag, nflag, oflag, qflag, Tflag, Wflag, xflag; 76 77 static int oidfmt(int *, int, char *, u_int *); 78 static int parsefile(const char *); 79 static int parse(const char *, int); 80 static int show_var(int *, int); 81 static int sysctl_all(int *oid, int len); 82 static int name2oid(const char *, int *); 83 84 static int strIKtoi(const char *, char **); 85 86 static int ctl_sign[CTLTYPE+1] = { 87 [CTLTYPE_INT] = 1, 88 [CTLTYPE_LONG] = 1, 89 [CTLTYPE_S64] = 1, 90 }; 91 92 static int ctl_size[CTLTYPE+1] = { 93 [CTLTYPE_INT] = sizeof(int), 94 [CTLTYPE_UINT] = sizeof(u_int), 95 [CTLTYPE_LONG] = sizeof(long), 96 [CTLTYPE_ULONG] = sizeof(u_long), 97 [CTLTYPE_S64] = sizeof(int64_t), 98 [CTLTYPE_U64] = sizeof(uint64_t), 99 }; 100 101 static const char *ctl_typename[CTLTYPE+1] = { 102 [CTLTYPE_INT] = "integer", 103 [CTLTYPE_UINT] = "unsigned integer", 104 [CTLTYPE_LONG] = "long integer", 105 [CTLTYPE_ULONG] = "unsigned long", 106 [CTLTYPE_S64] = "int64_t", 107 [CTLTYPE_U64] = "uint64_t", 108 }; 109 110 static void 111 usage(void) 112 { 113 114 (void)fprintf(stderr, "%s\n%s\n", 115 "usage: sysctl [-bdehiNnoqTWx] [ -B <bufsize> ] [-f filename] name[=value] ...", 116 " sysctl [-bdehNnoqTWx] [ -B <bufsize> ] -a"); 117 exit(1); 118 } 119 120 int 121 main(int argc, char **argv) 122 { 123 int ch; 124 int warncount = 0; 125 126 setlocale(LC_NUMERIC, ""); 127 setbuf(stdout,0); 128 setbuf(stderr,0); 129 130 while ((ch = getopt(argc, argv, "AabB:def:hiNnoqTwWxX")) != -1) { 131 switch (ch) { 132 case 'A': 133 /* compatibility */ 134 aflag = oflag = 1; 135 break; 136 case 'a': 137 aflag = 1; 138 break; 139 case 'b': 140 bflag = 1; 141 break; 142 case 'B': 143 Bflag = strtol(optarg, NULL, 0); 144 break; 145 case 'd': 146 dflag = 1; 147 break; 148 case 'e': 149 eflag = 1; 150 break; 151 case 'f': 152 conffile = optarg; 153 break; 154 case 'h': 155 hflag = 1; 156 break; 157 case 'i': 158 iflag = 1; 159 break; 160 case 'N': 161 Nflag = 1; 162 break; 163 case 'n': 164 nflag = 1; 165 break; 166 case 'o': 167 oflag = 1; 168 break; 169 case 'q': 170 qflag = 1; 171 break; 172 case 'T': 173 Tflag = 1; 174 break; 175 case 'w': 176 /* compatibility */ 177 /* ignored */ 178 break; 179 case 'W': 180 Wflag = 1; 181 break; 182 case 'X': 183 /* compatibility */ 184 aflag = xflag = 1; 185 break; 186 case 'x': 187 xflag = 1; 188 break; 189 default: 190 usage(); 191 } 192 } 193 argc -= optind; 194 argv += optind; 195 196 if (Nflag && nflag) 197 usage(); 198 if (aflag && argc == 0) 199 exit(sysctl_all(0, 0)); 200 if (argc == 0 && conffile == NULL) 201 usage(); 202 203 warncount = 0; 204 if (conffile != NULL) 205 warncount += parsefile(conffile); 206 207 while (argc-- > 0) 208 warncount += parse(*argv++, 0); 209 210 return (warncount); 211 } 212 213 /* 214 * Parse a name into a MIB entry. 215 * Lookup and print out the MIB entry if it exists. 216 * Set a new value if requested. 217 */ 218 static int 219 parse(const char *string, int lineno) 220 { 221 int len, i, j; 222 const void *newval; 223 const char *newvalstr = NULL; 224 int intval; 225 unsigned int uintval; 226 long longval; 227 unsigned long ulongval; 228 size_t newsize = Bflag; 229 int64_t i64val; 230 uint64_t u64val; 231 int mib[CTL_MAXNAME]; 232 char *cp, *bufp, buf[BUFSIZ], *endptr = NULL, fmt[BUFSIZ], line[BUFSIZ]; 233 u_int kind; 234 235 if (lineno) 236 snprintf(line, sizeof(line), " at line %d", lineno); 237 else 238 line[0] = '\0'; 239 240 cp = buf; 241 if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ) { 242 warnx("oid too long: '%s'%s", string, line); 243 return (1); 244 } 245 bufp = strsep(&cp, "=:"); 246 if (cp != NULL) { 247 /* Tflag just lists tunables, do not allow assignment */ 248 if (Tflag || Wflag) { 249 warnx("Can't set variables when using -T or -W"); 250 usage(); 251 } 252 while (isspace(*cp)) 253 cp++; 254 /* Strip a pair of " or ' if any. */ 255 switch (*cp) { 256 case '\"': 257 case '\'': 258 if (cp[strlen(cp) - 1] == *cp) 259 cp[strlen(cp) - 1] = '\0'; 260 cp++; 261 } 262 newvalstr = cp; 263 newsize = strlen(cp); 264 } 265 len = name2oid(bufp, mib); 266 267 if (len < 0) { 268 if (iflag) 269 return (0); 270 if (qflag) 271 return (1); 272 else { 273 warn("unknown oid '%s'%s", bufp, line); 274 return (1); 275 } 276 } 277 278 if (oidfmt(mib, len, fmt, &kind)) { 279 warn("couldn't find format of oid '%s'%s", bufp, line); 280 if (iflag) 281 return (1); 282 else 283 exit(1); 284 } 285 286 if (newvalstr == NULL || dflag) { 287 if ((kind & CTLTYPE) == CTLTYPE_NODE) { 288 if (dflag) { 289 i = show_var(mib, len); 290 if (!i && !bflag) 291 putchar('\n'); 292 } 293 sysctl_all(mib, len); 294 } else { 295 i = show_var(mib, len); 296 if (!i && !bflag) 297 putchar('\n'); 298 } 299 } else { 300 if ((kind & CTLTYPE) == CTLTYPE_NODE) { 301 warnx("oid '%s' isn't a leaf node%s", bufp, line); 302 return (1); 303 } 304 305 if (!(kind & CTLFLAG_WR)) { 306 if (kind & CTLFLAG_TUN) { 307 warnx("oid '%s' is a read only tunable%s", bufp, line); 308 warnx("Tunable values are set in /boot/loader.conf"); 309 } else 310 warnx("oid '%s' is read only%s", bufp, line); 311 return (1); 312 } 313 314 switch (kind & CTLTYPE) { 315 case CTLTYPE_INT: 316 case CTLTYPE_UINT: 317 case CTLTYPE_LONG: 318 case CTLTYPE_ULONG: 319 case CTLTYPE_S64: 320 case CTLTYPE_U64: 321 if (strlen(newvalstr) == 0) { 322 warnx("empty numeric value"); 323 return (1); 324 } 325 /* FALLTHROUGH */ 326 case CTLTYPE_STRING: 327 break; 328 default: 329 warnx("oid '%s' is type %d," 330 " cannot set that%s", bufp, 331 kind & CTLTYPE, line); 332 return (1); 333 } 334 335 errno = 0; 336 337 switch (kind & CTLTYPE) { 338 case CTLTYPE_INT: 339 if (strcmp(fmt, "IK") == 0) 340 intval = strIKtoi(newvalstr, &endptr); 341 else 342 intval = (int)strtol(newvalstr, &endptr, 343 0); 344 newval = &intval; 345 newsize = sizeof(intval); 346 break; 347 case CTLTYPE_UINT: 348 uintval = (int) strtoul(newvalstr, &endptr, 0); 349 newval = &uintval; 350 newsize = sizeof(uintval); 351 break; 352 case CTLTYPE_LONG: 353 longval = strtol(newvalstr, &endptr, 0); 354 newval = &longval; 355 newsize = sizeof(longval); 356 break; 357 case CTLTYPE_ULONG: 358 ulongval = strtoul(newvalstr, &endptr, 0); 359 newval = &ulongval; 360 newsize = sizeof(ulongval); 361 break; 362 case CTLTYPE_STRING: 363 newval = newvalstr; 364 break; 365 case CTLTYPE_S64: 366 i64val = strtoimax(newvalstr, &endptr, 0); 367 newval = &i64val; 368 newsize = sizeof(i64val); 369 break; 370 case CTLTYPE_U64: 371 u64val = strtoumax(newvalstr, &endptr, 0); 372 newval = &u64val; 373 newsize = sizeof(u64val); 374 break; 375 default: 376 /* NOTREACHED */ 377 abort(); 378 } 379 380 if (errno != 0 || endptr == newvalstr || 381 (endptr != NULL && *endptr != '\0')) { 382 warnx("invalid %s '%s'%s", ctl_typename[kind & CTLTYPE], 383 newvalstr, line); 384 return (1); 385 } 386 387 i = show_var(mib, len); 388 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) { 389 if (!i && !bflag) 390 putchar('\n'); 391 switch (errno) { 392 case EOPNOTSUPP: 393 warnx("%s: value is not available%s", 394 string, line); 395 return (1); 396 case ENOTDIR: 397 warnx("%s: specification is incomplete%s", 398 string, line); 399 return (1); 400 case ENOMEM: 401 warnx("%s: type is unknown to this program%s", 402 string, line); 403 return (1); 404 default: 405 warn("%s%s", string, line); 406 return (1); 407 } 408 } 409 if (!bflag) 410 printf(" -> "); 411 i = nflag; 412 nflag = 1; 413 j = show_var(mib, len); 414 if (!j && !bflag) 415 putchar('\n'); 416 nflag = i; 417 } 418 419 return (0); 420 } 421 422 static int 423 parsefile(const char *filename) 424 { 425 FILE *file; 426 char line[BUFSIZ], *p, *pq, *pdq; 427 int warncount = 0, lineno = 0; 428 429 file = fopen(filename, "r"); 430 if (file == NULL) 431 err(EX_NOINPUT, "%s", filename); 432 while (fgets(line, sizeof(line), file) != NULL) { 433 lineno++; 434 p = line; 435 pq = strchr(line, '\''); 436 pdq = strchr(line, '\"'); 437 /* Replace the first # with \0. */ 438 while((p = strchr(p, '#')) != NULL) { 439 if (pq != NULL && p > pq) { 440 if ((p = strchr(pq+1, '\'')) != NULL) 441 *(++p) = '\0'; 442 break; 443 } else if (pdq != NULL && p > pdq) { 444 if ((p = strchr(pdq+1, '\"')) != NULL) 445 *(++p) = '\0'; 446 break; 447 } else if (p == line || *(p-1) != '\\') { 448 *p = '\0'; 449 break; 450 } 451 p++; 452 } 453 /* Trim spaces */ 454 p = line + strlen(line) - 1; 455 while (p >= line && isspace((int)*p)) { 456 *p = '\0'; 457 p--; 458 } 459 p = line; 460 while (isspace((int)*p)) 461 p++; 462 if (*p == '\0') 463 continue; 464 else 465 warncount += parse(p, lineno); 466 } 467 fclose(file); 468 469 return (warncount); 470 } 471 472 /* These functions will dump out various interesting structures. */ 473 474 static int 475 S_clockinfo(size_t l2, void *p) 476 { 477 struct clockinfo *ci = (struct clockinfo*)p; 478 479 if (l2 != sizeof(*ci)) { 480 warnx("S_clockinfo %zu != %zu", l2, sizeof(*ci)); 481 return (1); 482 } 483 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" : 484 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }", 485 ci->hz, ci->tick, ci->profhz, ci->stathz); 486 return (0); 487 } 488 489 static int 490 S_loadavg(size_t l2, void *p) 491 { 492 struct loadavg *tv = (struct loadavg*)p; 493 494 if (l2 != sizeof(*tv)) { 495 warnx("S_loadavg %zu != %zu", l2, sizeof(*tv)); 496 return (1); 497 } 498 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }", 499 (double)tv->ldavg[0]/(double)tv->fscale, 500 (double)tv->ldavg[1]/(double)tv->fscale, 501 (double)tv->ldavg[2]/(double)tv->fscale); 502 return (0); 503 } 504 505 static int 506 S_timeval(size_t l2, void *p) 507 { 508 struct timeval *tv = (struct timeval*)p; 509 time_t tv_sec; 510 char *p1, *p2; 511 512 if (l2 != sizeof(*tv)) { 513 warnx("S_timeval %zu != %zu", l2, sizeof(*tv)); 514 return (1); 515 } 516 printf(hflag ? "{ sec = %'jd, usec = %'ld } " : 517 "{ sec = %jd, usec = %ld } ", 518 (intmax_t)tv->tv_sec, tv->tv_usec); 519 tv_sec = tv->tv_sec; 520 p1 = strdup(ctime(&tv_sec)); 521 for (p2=p1; *p2 ; p2++) 522 if (*p2 == '\n') 523 *p2 = '\0'; 524 fputs(p1, stdout); 525 free(p1); 526 return (0); 527 } 528 529 static int 530 S_vmtotal(size_t l2, void *p) 531 { 532 struct vmtotal *v = (struct vmtotal *)p; 533 int pageKilo = getpagesize() / 1024; 534 535 if (l2 != sizeof(*v)) { 536 warnx("S_vmtotal %zu != %zu", l2, sizeof(*v)); 537 return (1); 538 } 539 540 printf( 541 "\nSystem wide totals computed every five seconds:" 542 " (values in kilobytes)\n"); 543 printf("===============================================\n"); 544 printf( 545 "Processes:\t\t(RUNQ: %hd Disk Wait: %hd Page Wait: " 546 "%hd Sleep: %hd)\n", 547 v->t_rq, v->t_dw, v->t_pw, v->t_sl); 548 printf( 549 "Virtual Memory:\t\t(Total: %dK Active: %dK)\n", 550 v->t_vm * pageKilo, v->t_avm * pageKilo); 551 printf("Real Memory:\t\t(Total: %dK Active: %dK)\n", 552 v->t_rm * pageKilo, v->t_arm * pageKilo); 553 printf("Shared Virtual Memory:\t(Total: %dK Active: %dK)\n", 554 v->t_vmshr * pageKilo, v->t_avmshr * pageKilo); 555 printf("Shared Real Memory:\t(Total: %dK Active: %dK)\n", 556 v->t_rmshr * pageKilo, v->t_armshr * pageKilo); 557 printf("Free Memory:\t%dK", v->t_free * pageKilo); 558 559 return (0); 560 } 561 562 #ifdef __amd64__ 563 #define efi_next_descriptor(ptr, size) \ 564 ((struct efi_md *)(((uint8_t *) ptr) + size)) 565 566 static int 567 S_efi_map(size_t l2, void *p) 568 { 569 struct efi_map_header *efihdr; 570 struct efi_md *map; 571 const char *type; 572 size_t efisz; 573 int ndesc, i; 574 575 static const char *types[] = { 576 "Reserved", 577 "LoaderCode", 578 "LoaderData", 579 "BootServicesCode", 580 "BootServicesData", 581 "RuntimeServicesCode", 582 "RuntimeServicesData", 583 "ConventionalMemory", 584 "UnusableMemory", 585 "ACPIReclaimMemory", 586 "ACPIMemoryNVS", 587 "MemoryMappedIO", 588 "MemoryMappedIOPortSpace", 589 "PalCode" 590 }; 591 592 /* 593 * Memory map data provided by UEFI via the GetMemoryMap 594 * Boot Services API. 595 */ 596 if (l2 < sizeof(*efihdr)) { 597 warnx("S_efi_map length less than header"); 598 return (1); 599 } 600 efihdr = p; 601 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf; 602 map = (struct efi_md *)((uint8_t *)efihdr + efisz); 603 604 if (efihdr->descriptor_size == 0) 605 return (0); 606 if (l2 != efisz + efihdr->memory_size) { 607 warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz + 608 efihdr->memory_size); 609 return (1); 610 } 611 ndesc = efihdr->memory_size / efihdr->descriptor_size; 612 613 printf("\n%23s %12s %12s %8s %4s", 614 "Type", "Physical", "Virtual", "#Pages", "Attr"); 615 616 for (i = 0; i < ndesc; i++, 617 map = efi_next_descriptor(map, efihdr->descriptor_size)) { 618 if (map->md_type <= EFI_MD_TYPE_PALCODE) 619 type = types[map->md_type]; 620 else 621 type = "<INVALID>"; 622 printf("\n%23s %012lx %12p %08lx ", type, map->md_phys, 623 map->md_virt, map->md_pages); 624 if (map->md_attr & EFI_MD_ATTR_UC) 625 printf("UC "); 626 if (map->md_attr & EFI_MD_ATTR_WC) 627 printf("WC "); 628 if (map->md_attr & EFI_MD_ATTR_WT) 629 printf("WT "); 630 if (map->md_attr & EFI_MD_ATTR_WB) 631 printf("WB "); 632 if (map->md_attr & EFI_MD_ATTR_UCE) 633 printf("UCE "); 634 if (map->md_attr & EFI_MD_ATTR_WP) 635 printf("WP "); 636 if (map->md_attr & EFI_MD_ATTR_RP) 637 printf("RP "); 638 if (map->md_attr & EFI_MD_ATTR_XP) 639 printf("XP "); 640 if (map->md_attr & EFI_MD_ATTR_RT) 641 printf("RUNTIME"); 642 } 643 return (0); 644 } 645 #endif 646 647 #if defined(__amd64__) || defined(__i386__) 648 static int 649 S_bios_smap_xattr(size_t l2, void *p) 650 { 651 struct bios_smap_xattr *smap, *end; 652 653 if (l2 % sizeof(*smap) != 0) { 654 warnx("S_bios_smap_xattr %zu is not a multiple of %zu", l2, 655 sizeof(*smap)); 656 return (1); 657 } 658 659 end = (struct bios_smap_xattr *)((char *)p + l2); 660 for (smap = p; smap < end; smap++) 661 printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx", 662 smap->type, smap->xattr, (uintmax_t)smap->base, 663 (uintmax_t)smap->length); 664 return (0); 665 } 666 #endif 667 668 static int 669 strIKtoi(const char *str, char **endptrp) 670 { 671 int kelv; 672 float temp; 673 size_t len; 674 const char *p; 675 676 assert(errno == 0); 677 678 len = strlen(str); 679 /* caller already checked this */ 680 assert(len > 0); 681 682 p = &str[len - 1]; 683 if (*p == 'C' || *p == 'F') { 684 temp = strtof(str, endptrp); 685 if (*endptrp != str && *endptrp == p && errno == 0) { 686 if (*p == 'F') 687 temp = (temp - 32) * 5 / 9; 688 *endptrp = NULL; 689 return (temp * 10 + 2732); 690 } 691 } else { 692 kelv = (int)strtol(str, endptrp, 10); 693 if (*endptrp != str && *endptrp == p && errno == 0) { 694 *endptrp = NULL; 695 return (kelv); 696 } 697 } 698 699 errno = ERANGE; 700 return (0); 701 } 702 703 /* 704 * These functions uses a presently undocumented interface to the kernel 705 * to walk the tree and get the type so it can print the value. 706 * This interface is under work and consideration, and should probably 707 * be killed with a big axe by the first person who can find the time. 708 * (be aware though, that the proper interface isn't as obvious as it 709 * may seem, there are various conflicting requirements. 710 */ 711 712 static int 713 name2oid(const char *name, int *oidp) 714 { 715 int oid[2]; 716 int i; 717 size_t j; 718 719 oid[0] = 0; 720 oid[1] = 3; 721 722 j = CTL_MAXNAME * sizeof(int); 723 i = sysctl(oid, 2, oidp, &j, name, strlen(name)); 724 if (i < 0) 725 return (i); 726 j /= sizeof(int); 727 return (j); 728 } 729 730 static int 731 oidfmt(int *oid, int len, char *fmt, u_int *kind) 732 { 733 int qoid[CTL_MAXNAME+2]; 734 u_char buf[BUFSIZ]; 735 int i; 736 size_t j; 737 738 qoid[0] = 0; 739 qoid[1] = 4; 740 memcpy(qoid + 2, oid, len * sizeof(int)); 741 742 j = sizeof(buf); 743 i = sysctl(qoid, len + 2, buf, &j, 0, 0); 744 if (i) 745 err(1, "sysctl fmt %d %zu %d", i, j, errno); 746 747 if (kind) 748 *kind = *(u_int *)buf; 749 750 if (fmt) 751 strcpy(fmt, (char *)(buf + sizeof(u_int))); 752 return (0); 753 } 754 755 /* 756 * This formats and outputs the value of one variable 757 * 758 * Returns zero if anything was actually output. 759 * Returns one if didn't know what to do with this. 760 * Return minus one if we had errors. 761 */ 762 static int 763 show_var(int *oid, int nlen) 764 { 765 u_char buf[BUFSIZ], *val, *oval, *p; 766 char name[BUFSIZ], fmt[BUFSIZ]; 767 const char *sep, *sep1; 768 int qoid[CTL_MAXNAME+2]; 769 uintmax_t umv; 770 intmax_t mv; 771 int i, hexlen, sign, ctltype; 772 size_t intlen; 773 size_t j, len; 774 u_int kind; 775 int (*func)(size_t, void *); 776 777 /* Silence GCC. */ 778 umv = mv = intlen = 0; 779 780 bzero(buf, BUFSIZ); 781 bzero(fmt, BUFSIZ); 782 bzero(name, BUFSIZ); 783 qoid[0] = 0; 784 memcpy(qoid + 2, oid, nlen * sizeof(int)); 785 786 qoid[1] = 1; 787 j = sizeof(name); 788 i = sysctl(qoid, nlen + 2, name, &j, 0, 0); 789 if (i || !j) 790 err(1, "sysctl name %d %zu %d", i, j, errno); 791 792 oidfmt(oid, nlen, fmt, &kind); 793 /* if Wflag then only list sysctls that are writeable and not stats. */ 794 if (Wflag && ((kind & CTLFLAG_WR) == 0 || (kind & CTLFLAG_STATS) != 0)) 795 return 1; 796 797 /* if Tflag then only list sysctls that are tuneables. */ 798 if (Tflag && (kind & CTLFLAG_TUN) == 0) 799 return 1; 800 801 if (Nflag) { 802 printf("%s", name); 803 return (0); 804 } 805 806 if (eflag) 807 sep = "="; 808 else 809 sep = ": "; 810 811 if (dflag) { /* just print description */ 812 qoid[1] = 5; 813 j = sizeof(buf); 814 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0); 815 if (!nflag) 816 printf("%s%s", name, sep); 817 printf("%s", buf); 818 return (0); 819 } 820 /* find an estimate of how much we need for this var */ 821 if (Bflag) 822 j = Bflag; 823 else { 824 j = 0; 825 i = sysctl(oid, nlen, 0, &j, 0, 0); 826 j += j; /* we want to be sure :-) */ 827 } 828 829 val = oval = malloc(j + 1); 830 if (val == NULL) { 831 warnx("malloc failed"); 832 return (1); 833 } 834 ctltype = (kind & CTLTYPE); 835 len = j; 836 i = sysctl(oid, nlen, val, &len, 0, 0); 837 if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) { 838 free(oval); 839 return (1); 840 } 841 842 if (bflag) { 843 fwrite(val, 1, len, stdout); 844 free(oval); 845 return (0); 846 } 847 val[len] = '\0'; 848 p = val; 849 sign = ctl_sign[ctltype]; 850 intlen = ctl_size[ctltype]; 851 852 switch (ctltype) { 853 case CTLTYPE_STRING: 854 if (!nflag) 855 printf("%s%s", name, sep); 856 printf("%.*s", (int)len, p); 857 free(oval); 858 return (0); 859 860 case CTLTYPE_INT: 861 case CTLTYPE_UINT: 862 case CTLTYPE_LONG: 863 case CTLTYPE_ULONG: 864 case CTLTYPE_S64: 865 case CTLTYPE_U64: 866 if (!nflag) 867 printf("%s%s", name, sep); 868 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4; 869 sep1 = ""; 870 while (len >= intlen) { 871 switch (kind & CTLTYPE) { 872 case CTLTYPE_INT: 873 case CTLTYPE_UINT: 874 umv = *(u_int *)p; 875 mv = *(int *)p; 876 break; 877 case CTLTYPE_LONG: 878 case CTLTYPE_ULONG: 879 umv = *(u_long *)p; 880 mv = *(long *)p; 881 break; 882 case CTLTYPE_S64: 883 case CTLTYPE_U64: 884 umv = *(uint64_t *)p; 885 mv = *(int64_t *)p; 886 break; 887 } 888 fputs(sep1, stdout); 889 if (xflag) 890 printf("%#0*jx", hexlen, umv); 891 else if (!sign) 892 printf(hflag ? "%'ju" : "%ju", umv); 893 else if (fmt[1] == 'K') { 894 if (mv < 0) 895 printf("%jd", mv); 896 else 897 printf("%.1fC", (mv - 2732.0) / 10); 898 } else 899 printf(hflag ? "%'jd" : "%jd", mv); 900 sep1 = " "; 901 len -= intlen; 902 p += intlen; 903 } 904 free(oval); 905 return (0); 906 907 case CTLTYPE_OPAQUE: 908 i = 0; 909 if (strcmp(fmt, "S,clockinfo") == 0) 910 func = S_clockinfo; 911 else if (strcmp(fmt, "S,timeval") == 0) 912 func = S_timeval; 913 else if (strcmp(fmt, "S,loadavg") == 0) 914 func = S_loadavg; 915 else if (strcmp(fmt, "S,vmtotal") == 0) 916 func = S_vmtotal; 917 #ifdef __amd64__ 918 else if (strcmp(fmt, "S,efi_map_header") == 0) 919 func = S_efi_map; 920 #endif 921 #if defined(__amd64__) || defined(__i386__) 922 else if (strcmp(fmt, "S,bios_smap_xattr") == 0) 923 func = S_bios_smap_xattr; 924 #endif 925 else 926 func = NULL; 927 if (func) { 928 if (!nflag) 929 printf("%s%s", name, sep); 930 i = (*func)(len, p); 931 free(oval); 932 return (i); 933 } 934 /* FALLTHROUGH */ 935 default: 936 if (!oflag && !xflag) { 937 free(oval); 938 return (1); 939 } 940 if (!nflag) 941 printf("%s%s", name, sep); 942 printf("Format:%s Length:%zu Dump:0x", fmt, len); 943 while (len-- && (xflag || p < val + 16)) 944 printf("%02x", *p++); 945 if (!xflag && len > 16) 946 printf("..."); 947 free(oval); 948 return (0); 949 } 950 free(oval); 951 return (1); 952 } 953 954 static int 955 sysctl_all(int *oid, int len) 956 { 957 int name1[22], name2[22]; 958 int i, j; 959 size_t l1, l2; 960 961 name1[0] = 0; 962 name1[1] = 2; 963 l1 = 2; 964 if (len) { 965 memcpy(name1+2, oid, len * sizeof(int)); 966 l1 += len; 967 } else { 968 name1[2] = 1; 969 l1++; 970 } 971 for (;;) { 972 l2 = sizeof(name2); 973 j = sysctl(name1, l1, name2, &l2, 0, 0); 974 if (j < 0) { 975 if (errno == ENOENT) 976 return (0); 977 else 978 err(1, "sysctl(getnext) %d %zu", j, l2); 979 } 980 981 l2 /= sizeof(int); 982 983 if (len < 0 || l2 < (unsigned int)len) 984 return (0); 985 986 for (i = 0; i < len; i++) 987 if (name2[i] != oid[i]) 988 return (0); 989 990 i = show_var(name2, l2); 991 if (!i && !bflag) 992 putchar('\n'); 993 994 memcpy(name1+2, name2, l2 * sizeof(int)); 995 l1 = 2 + l2; 996 } 997 } 998