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 **, const 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 /* Trim spaces */ 266 cp = bufp + strlen(bufp) - 1; 267 while (cp >= bufp && isspace((int)*cp)) { 268 *cp = '\0'; 269 cp--; 270 } 271 len = name2oid(bufp, mib); 272 273 if (len < 0) { 274 if (iflag) 275 return (0); 276 if (qflag) 277 return (1); 278 else { 279 if (errno == ENOENT) { 280 warnx("unknown oid '%s'%s", bufp, line); 281 } else { 282 warn("unknown oid '%s'%s", bufp, line); 283 } 284 return (1); 285 } 286 } 287 288 if (oidfmt(mib, len, fmt, &kind)) { 289 warn("couldn't find format of oid '%s'%s", bufp, line); 290 if (iflag) 291 return (1); 292 else 293 exit(1); 294 } 295 296 if (newvalstr == NULL || dflag) { 297 if ((kind & CTLTYPE) == CTLTYPE_NODE) { 298 if (dflag) { 299 i = show_var(mib, len); 300 if (!i && !bflag) 301 putchar('\n'); 302 } 303 sysctl_all(mib, len); 304 } else { 305 i = show_var(mib, len); 306 if (!i && !bflag) 307 putchar('\n'); 308 } 309 } else { 310 if ((kind & CTLTYPE) == CTLTYPE_NODE) { 311 warnx("oid '%s' isn't a leaf node%s", bufp, line); 312 return (1); 313 } 314 315 if (!(kind & CTLFLAG_WR)) { 316 if (kind & CTLFLAG_TUN) { 317 warnx("oid '%s' is a read only tunable%s", bufp, line); 318 warnx("Tunable values are set in /boot/loader.conf"); 319 } else 320 warnx("oid '%s' is read only%s", bufp, line); 321 return (1); 322 } 323 324 switch (kind & CTLTYPE) { 325 case CTLTYPE_INT: 326 case CTLTYPE_UINT: 327 case CTLTYPE_LONG: 328 case CTLTYPE_ULONG: 329 case CTLTYPE_S64: 330 case CTLTYPE_U64: 331 if (strlen(newvalstr) == 0) { 332 warnx("empty numeric value"); 333 return (1); 334 } 335 /* FALLTHROUGH */ 336 case CTLTYPE_STRING: 337 break; 338 default: 339 warnx("oid '%s' is type %d," 340 " cannot set that%s", bufp, 341 kind & CTLTYPE, line); 342 return (1); 343 } 344 345 errno = 0; 346 347 switch (kind & CTLTYPE) { 348 case CTLTYPE_INT: 349 if (strncmp(fmt, "IK", 2) == 0) 350 intval = strIKtoi(newvalstr, &endptr, fmt); 351 else 352 intval = (int)strtol(newvalstr, &endptr, 353 0); 354 newval = &intval; 355 newsize = sizeof(intval); 356 break; 357 case CTLTYPE_UINT: 358 uintval = (int) strtoul(newvalstr, &endptr, 0); 359 newval = &uintval; 360 newsize = sizeof(uintval); 361 break; 362 case CTLTYPE_LONG: 363 longval = strtol(newvalstr, &endptr, 0); 364 newval = &longval; 365 newsize = sizeof(longval); 366 break; 367 case CTLTYPE_ULONG: 368 ulongval = strtoul(newvalstr, &endptr, 0); 369 newval = &ulongval; 370 newsize = sizeof(ulongval); 371 break; 372 case CTLTYPE_STRING: 373 newval = newvalstr; 374 break; 375 case CTLTYPE_S64: 376 i64val = strtoimax(newvalstr, &endptr, 0); 377 newval = &i64val; 378 newsize = sizeof(i64val); 379 break; 380 case CTLTYPE_U64: 381 u64val = strtoumax(newvalstr, &endptr, 0); 382 newval = &u64val; 383 newsize = sizeof(u64val); 384 break; 385 default: 386 /* NOTREACHED */ 387 abort(); 388 } 389 390 if (errno != 0 || endptr == newvalstr || 391 (endptr != NULL && *endptr != '\0')) { 392 warnx("invalid %s '%s'%s", ctl_typename[kind & CTLTYPE], 393 newvalstr, line); 394 return (1); 395 } 396 397 i = show_var(mib, len); 398 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) { 399 if (!i && !bflag) 400 putchar('\n'); 401 switch (errno) { 402 case EOPNOTSUPP: 403 warnx("%s: value is not available%s", 404 string, line); 405 return (1); 406 case ENOTDIR: 407 warnx("%s: specification is incomplete%s", 408 string, line); 409 return (1); 410 case ENOMEM: 411 warnx("%s: type is unknown to this program%s", 412 string, line); 413 return (1); 414 default: 415 warn("%s%s", string, line); 416 return (1); 417 } 418 } 419 if (!bflag) 420 printf(" -> "); 421 i = nflag; 422 nflag = 1; 423 j = show_var(mib, len); 424 if (!j && !bflag) 425 putchar('\n'); 426 nflag = i; 427 } 428 429 return (0); 430 } 431 432 static int 433 parsefile(const char *filename) 434 { 435 FILE *file; 436 char line[BUFSIZ], *p, *pq, *pdq; 437 int warncount = 0, lineno = 0; 438 439 file = fopen(filename, "r"); 440 if (file == NULL) 441 err(EX_NOINPUT, "%s", filename); 442 while (fgets(line, sizeof(line), file) != NULL) { 443 lineno++; 444 p = line; 445 pq = strchr(line, '\''); 446 pdq = strchr(line, '\"'); 447 /* Replace the first # with \0. */ 448 while((p = strchr(p, '#')) != NULL) { 449 if (pq != NULL && p > pq) { 450 if ((p = strchr(pq+1, '\'')) != NULL) 451 *(++p) = '\0'; 452 break; 453 } else if (pdq != NULL && p > pdq) { 454 if ((p = strchr(pdq+1, '\"')) != NULL) 455 *(++p) = '\0'; 456 break; 457 } else if (p == line || *(p-1) != '\\') { 458 *p = '\0'; 459 break; 460 } 461 p++; 462 } 463 /* Trim spaces */ 464 p = line + strlen(line) - 1; 465 while (p >= line && isspace((int)*p)) { 466 *p = '\0'; 467 p--; 468 } 469 p = line; 470 while (isspace((int)*p)) 471 p++; 472 if (*p == '\0') 473 continue; 474 else 475 warncount += parse(p, lineno); 476 } 477 fclose(file); 478 479 return (warncount); 480 } 481 482 /* These functions will dump out various interesting structures. */ 483 484 static int 485 S_clockinfo(size_t l2, void *p) 486 { 487 struct clockinfo *ci = (struct clockinfo*)p; 488 489 if (l2 != sizeof(*ci)) { 490 warnx("S_clockinfo %zu != %zu", l2, sizeof(*ci)); 491 return (1); 492 } 493 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" : 494 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }", 495 ci->hz, ci->tick, ci->profhz, ci->stathz); 496 return (0); 497 } 498 499 static int 500 S_loadavg(size_t l2, void *p) 501 { 502 struct loadavg *tv = (struct loadavg*)p; 503 504 if (l2 != sizeof(*tv)) { 505 warnx("S_loadavg %zu != %zu", l2, sizeof(*tv)); 506 return (1); 507 } 508 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }", 509 (double)tv->ldavg[0]/(double)tv->fscale, 510 (double)tv->ldavg[1]/(double)tv->fscale, 511 (double)tv->ldavg[2]/(double)tv->fscale); 512 return (0); 513 } 514 515 static int 516 S_timeval(size_t l2, void *p) 517 { 518 struct timeval *tv = (struct timeval*)p; 519 time_t tv_sec; 520 char *p1, *p2; 521 522 if (l2 != sizeof(*tv)) { 523 warnx("S_timeval %zu != %zu", l2, sizeof(*tv)); 524 return (1); 525 } 526 printf(hflag ? "{ sec = %'jd, usec = %'ld } " : 527 "{ sec = %jd, usec = %ld } ", 528 (intmax_t)tv->tv_sec, tv->tv_usec); 529 tv_sec = tv->tv_sec; 530 p1 = strdup(ctime(&tv_sec)); 531 for (p2=p1; *p2 ; p2++) 532 if (*p2 == '\n') 533 *p2 = '\0'; 534 fputs(p1, stdout); 535 free(p1); 536 return (0); 537 } 538 539 static int 540 S_vmtotal(size_t l2, void *p) 541 { 542 struct vmtotal *v = (struct vmtotal *)p; 543 int pageKilo = getpagesize() / 1024; 544 545 if (l2 != sizeof(*v)) { 546 warnx("S_vmtotal %zu != %zu", l2, sizeof(*v)); 547 return (1); 548 } 549 550 printf( 551 "\nSystem wide totals computed every five seconds:" 552 " (values in kilobytes)\n"); 553 printf("===============================================\n"); 554 printf( 555 "Processes:\t\t(RUNQ: %hd Disk Wait: %hd Page Wait: " 556 "%hd Sleep: %hd)\n", 557 v->t_rq, v->t_dw, v->t_pw, v->t_sl); 558 printf( 559 "Virtual Memory:\t\t(Total: %dK Active: %dK)\n", 560 v->t_vm * pageKilo, v->t_avm * pageKilo); 561 printf("Real Memory:\t\t(Total: %dK Active: %dK)\n", 562 v->t_rm * pageKilo, v->t_arm * pageKilo); 563 printf("Shared Virtual Memory:\t(Total: %dK Active: %dK)\n", 564 v->t_vmshr * pageKilo, v->t_avmshr * pageKilo); 565 printf("Shared Real Memory:\t(Total: %dK Active: %dK)\n", 566 v->t_rmshr * pageKilo, v->t_armshr * pageKilo); 567 printf("Free Memory:\t%dK", v->t_free * pageKilo); 568 569 return (0); 570 } 571 572 #ifdef __amd64__ 573 #define efi_next_descriptor(ptr, size) \ 574 ((struct efi_md *)(((uint8_t *) ptr) + size)) 575 576 static int 577 S_efi_map(size_t l2, void *p) 578 { 579 struct efi_map_header *efihdr; 580 struct efi_md *map; 581 const char *type; 582 size_t efisz; 583 int ndesc, i; 584 585 static const char *types[] = { 586 "Reserved", 587 "LoaderCode", 588 "LoaderData", 589 "BootServicesCode", 590 "BootServicesData", 591 "RuntimeServicesCode", 592 "RuntimeServicesData", 593 "ConventionalMemory", 594 "UnusableMemory", 595 "ACPIReclaimMemory", 596 "ACPIMemoryNVS", 597 "MemoryMappedIO", 598 "MemoryMappedIOPortSpace", 599 "PalCode" 600 }; 601 602 /* 603 * Memory map data provided by UEFI via the GetMemoryMap 604 * Boot Services API. 605 */ 606 if (l2 < sizeof(*efihdr)) { 607 warnx("S_efi_map length less than header"); 608 return (1); 609 } 610 efihdr = p; 611 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf; 612 map = (struct efi_md *)((uint8_t *)efihdr + efisz); 613 614 if (efihdr->descriptor_size == 0) 615 return (0); 616 if (l2 != efisz + efihdr->memory_size) { 617 warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz + 618 efihdr->memory_size); 619 return (1); 620 } 621 ndesc = efihdr->memory_size / efihdr->descriptor_size; 622 623 printf("\n%23s %12s %12s %8s %4s", 624 "Type", "Physical", "Virtual", "#Pages", "Attr"); 625 626 for (i = 0; i < ndesc; i++, 627 map = efi_next_descriptor(map, efihdr->descriptor_size)) { 628 if (map->md_type <= EFI_MD_TYPE_PALCODE) 629 type = types[map->md_type]; 630 else 631 type = "<INVALID>"; 632 printf("\n%23s %012lx %12p %08lx ", type, map->md_phys, 633 map->md_virt, map->md_pages); 634 if (map->md_attr & EFI_MD_ATTR_UC) 635 printf("UC "); 636 if (map->md_attr & EFI_MD_ATTR_WC) 637 printf("WC "); 638 if (map->md_attr & EFI_MD_ATTR_WT) 639 printf("WT "); 640 if (map->md_attr & EFI_MD_ATTR_WB) 641 printf("WB "); 642 if (map->md_attr & EFI_MD_ATTR_UCE) 643 printf("UCE "); 644 if (map->md_attr & EFI_MD_ATTR_WP) 645 printf("WP "); 646 if (map->md_attr & EFI_MD_ATTR_RP) 647 printf("RP "); 648 if (map->md_attr & EFI_MD_ATTR_XP) 649 printf("XP "); 650 if (map->md_attr & EFI_MD_ATTR_RT) 651 printf("RUNTIME"); 652 } 653 return (0); 654 } 655 #endif 656 657 #if defined(__amd64__) || defined(__i386__) 658 static int 659 S_bios_smap_xattr(size_t l2, void *p) 660 { 661 struct bios_smap_xattr *smap, *end; 662 663 if (l2 % sizeof(*smap) != 0) { 664 warnx("S_bios_smap_xattr %zu is not a multiple of %zu", l2, 665 sizeof(*smap)); 666 return (1); 667 } 668 669 end = (struct bios_smap_xattr *)((char *)p + l2); 670 for (smap = p; smap < end; smap++) 671 printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx", 672 smap->type, smap->xattr, (uintmax_t)smap->base, 673 (uintmax_t)smap->length); 674 return (0); 675 } 676 #endif 677 678 static int 679 strIKtoi(const char *str, char **endptrp, const char *fmt) 680 { 681 int kelv; 682 float temp; 683 size_t len; 684 const char *p; 685 int prec, i; 686 687 assert(errno == 0); 688 689 len = strlen(str); 690 /* caller already checked this */ 691 assert(len > 0); 692 693 /* 694 * A format of "IK" is in deciKelvin. A format of "IK3" is in 695 * milliKelvin. The single digit following IK is log10 of the 696 * multiplying factor to convert Kelvin into the untis of this sysctl, 697 * or the dividing factor to convert the sysctl value to Kelvin. Numbers 698 * larger than 6 will run into precision issues with 32-bit integers. 699 * Characters that aren't ASCII digits after the 'K' are ignored. No 700 * localization is present because this is an interface from the kernel 701 * to this program (eg not an end-user interface), so isdigit() isn't 702 * used here. 703 */ 704 if (fmt[2] != '\0' && fmt[2] >= '0' && fmt[2] <= '9') 705 prec = fmt[2] - '0'; 706 else 707 prec = 1; 708 p = &str[len - 1]; 709 if (*p == 'C' || *p == 'F' || *p == 'K') { 710 temp = strtof(str, endptrp); 711 if (*endptrp != str && *endptrp == p && errno == 0) { 712 if (*p == 'F') 713 temp = (temp - 32) * 5 / 9; 714 *endptrp = NULL; 715 if (*p != 'K') 716 temp += 273.15; 717 for (i = 0; i < prec; i++) 718 temp *= 10.0; 719 return ((int)(temp + 0.5)); 720 } 721 } else { 722 /* No unit specified -> treat it as a raw number */ 723 kelv = (int)strtol(str, endptrp, 10); 724 if (*endptrp != str && *endptrp == p && errno == 0) { 725 *endptrp = NULL; 726 return (kelv); 727 } 728 } 729 730 errno = ERANGE; 731 return (0); 732 } 733 734 /* 735 * These functions uses a presently undocumented interface to the kernel 736 * to walk the tree and get the type so it can print the value. 737 * This interface is under work and consideration, and should probably 738 * be killed with a big axe by the first person who can find the time. 739 * (be aware though, that the proper interface isn't as obvious as it 740 * may seem, there are various conflicting requirements. 741 */ 742 743 static int 744 name2oid(const char *name, int *oidp) 745 { 746 int oid[2]; 747 int i; 748 size_t j; 749 750 oid[0] = 0; 751 oid[1] = 3; 752 753 j = CTL_MAXNAME * sizeof(int); 754 i = sysctl(oid, 2, oidp, &j, name, strlen(name)); 755 if (i < 0) 756 return (i); 757 j /= sizeof(int); 758 return (j); 759 } 760 761 static int 762 oidfmt(int *oid, int len, char *fmt, u_int *kind) 763 { 764 int qoid[CTL_MAXNAME+2]; 765 u_char buf[BUFSIZ]; 766 int i; 767 size_t j; 768 769 qoid[0] = 0; 770 qoid[1] = 4; 771 memcpy(qoid + 2, oid, len * sizeof(int)); 772 773 j = sizeof(buf); 774 i = sysctl(qoid, len + 2, buf, &j, 0, 0); 775 if (i) 776 err(1, "sysctl fmt %d %zu %d", i, j, errno); 777 778 if (kind) 779 *kind = *(u_int *)buf; 780 781 if (fmt) 782 strcpy(fmt, (char *)(buf + sizeof(u_int))); 783 return (0); 784 } 785 786 /* 787 * This formats and outputs the value of one variable 788 * 789 * Returns zero if anything was actually output. 790 * Returns one if didn't know what to do with this. 791 * Return minus one if we had errors. 792 */ 793 static int 794 show_var(int *oid, int nlen) 795 { 796 u_char buf[BUFSIZ], *val, *oval, *p; 797 char name[BUFSIZ], fmt[BUFSIZ]; 798 const char *sep, *sep1; 799 int qoid[CTL_MAXNAME+2]; 800 uintmax_t umv; 801 intmax_t mv; 802 int i, hexlen, sign, ctltype; 803 size_t intlen; 804 size_t j, len; 805 u_int kind; 806 float base; 807 int (*func)(size_t, void *); 808 int prec; 809 810 /* Silence GCC. */ 811 umv = mv = intlen = 0; 812 813 bzero(buf, BUFSIZ); 814 bzero(fmt, BUFSIZ); 815 bzero(name, BUFSIZ); 816 qoid[0] = 0; 817 memcpy(qoid + 2, oid, nlen * sizeof(int)); 818 819 qoid[1] = 1; 820 j = sizeof(name); 821 i = sysctl(qoid, nlen + 2, name, &j, 0, 0); 822 if (i || !j) 823 err(1, "sysctl name %d %zu %d", i, j, errno); 824 825 oidfmt(oid, nlen, fmt, &kind); 826 /* if Wflag then only list sysctls that are writeable and not stats. */ 827 if (Wflag && ((kind & CTLFLAG_WR) == 0 || (kind & CTLFLAG_STATS) != 0)) 828 return 1; 829 830 /* if Tflag then only list sysctls that are tuneables. */ 831 if (Tflag && (kind & CTLFLAG_TUN) == 0) 832 return 1; 833 834 if (Nflag) { 835 printf("%s", name); 836 return (0); 837 } 838 839 if (eflag) 840 sep = "="; 841 else 842 sep = ": "; 843 844 if (dflag) { /* just print description */ 845 qoid[1] = 5; 846 j = sizeof(buf); 847 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0); 848 if (!nflag) 849 printf("%s%s", name, sep); 850 printf("%s", buf); 851 return (0); 852 } 853 /* find an estimate of how much we need for this var */ 854 if (Bflag) 855 j = Bflag; 856 else { 857 j = 0; 858 i = sysctl(oid, nlen, 0, &j, 0, 0); 859 j += j; /* we want to be sure :-) */ 860 } 861 862 val = oval = malloc(j + 1); 863 if (val == NULL) { 864 warnx("malloc failed"); 865 return (1); 866 } 867 ctltype = (kind & CTLTYPE); 868 len = j; 869 i = sysctl(oid, nlen, val, &len, 0, 0); 870 if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) { 871 free(oval); 872 return (1); 873 } 874 875 if (bflag) { 876 fwrite(val, 1, len, stdout); 877 free(oval); 878 return (0); 879 } 880 val[len] = '\0'; 881 p = val; 882 sign = ctl_sign[ctltype]; 883 intlen = ctl_size[ctltype]; 884 885 switch (ctltype) { 886 case CTLTYPE_STRING: 887 if (!nflag) 888 printf("%s%s", name, sep); 889 printf("%.*s", (int)len, p); 890 free(oval); 891 return (0); 892 893 case CTLTYPE_INT: 894 case CTLTYPE_UINT: 895 case CTLTYPE_LONG: 896 case CTLTYPE_ULONG: 897 case CTLTYPE_S64: 898 case CTLTYPE_U64: 899 if (!nflag) 900 printf("%s%s", name, sep); 901 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4; 902 sep1 = ""; 903 while (len >= intlen) { 904 switch (kind & CTLTYPE) { 905 case CTLTYPE_INT: 906 case CTLTYPE_UINT: 907 umv = *(u_int *)p; 908 mv = *(int *)p; 909 break; 910 case CTLTYPE_LONG: 911 case CTLTYPE_ULONG: 912 umv = *(u_long *)p; 913 mv = *(long *)p; 914 break; 915 case CTLTYPE_S64: 916 case CTLTYPE_U64: 917 umv = *(uint64_t *)p; 918 mv = *(int64_t *)p; 919 break; 920 } 921 fputs(sep1, stdout); 922 if (xflag) 923 printf("%#0*jx", hexlen, umv); 924 else if (!sign) 925 printf(hflag ? "%'ju" : "%ju", umv); 926 else if (fmt[1] == 'K') { 927 if (mv < 0) 928 printf("%jd", mv); 929 else { 930 /* 931 * See strIKtoi for details on fmt. 932 */ 933 prec = 1; 934 if (fmt[2] != '\0') 935 prec = fmt[2] - '0'; 936 base = 1.0; 937 for (int i = 0; i < prec; i++) 938 base *= 10.0; 939 printf("%.*fC", prec, 940 (float)mv / base - 273.15); 941 } 942 } else 943 printf(hflag ? "%'jd" : "%jd", mv); 944 sep1 = " "; 945 len -= intlen; 946 p += intlen; 947 } 948 free(oval); 949 return (0); 950 951 case CTLTYPE_OPAQUE: 952 i = 0; 953 if (strcmp(fmt, "S,clockinfo") == 0) 954 func = S_clockinfo; 955 else if (strcmp(fmt, "S,timeval") == 0) 956 func = S_timeval; 957 else if (strcmp(fmt, "S,loadavg") == 0) 958 func = S_loadavg; 959 else if (strcmp(fmt, "S,vmtotal") == 0) 960 func = S_vmtotal; 961 #ifdef __amd64__ 962 else if (strcmp(fmt, "S,efi_map_header") == 0) 963 func = S_efi_map; 964 #endif 965 #if defined(__amd64__) || defined(__i386__) 966 else if (strcmp(fmt, "S,bios_smap_xattr") == 0) 967 func = S_bios_smap_xattr; 968 #endif 969 else 970 func = NULL; 971 if (func) { 972 if (!nflag) 973 printf("%s%s", name, sep); 974 i = (*func)(len, p); 975 free(oval); 976 return (i); 977 } 978 /* FALLTHROUGH */ 979 default: 980 if (!oflag && !xflag) { 981 free(oval); 982 return (1); 983 } 984 if (!nflag) 985 printf("%s%s", name, sep); 986 printf("Format:%s Length:%zu Dump:0x", fmt, len); 987 while (len-- && (xflag || p < val + 16)) 988 printf("%02x", *p++); 989 if (!xflag && len > 16) 990 printf("..."); 991 free(oval); 992 return (0); 993 } 994 free(oval); 995 return (1); 996 } 997 998 static int 999 sysctl_all(int *oid, int len) 1000 { 1001 int name1[22], name2[22]; 1002 int i, j; 1003 size_t l1, l2; 1004 1005 name1[0] = 0; 1006 name1[1] = 2; 1007 l1 = 2; 1008 if (len) { 1009 memcpy(name1+2, oid, len * sizeof(int)); 1010 l1 += len; 1011 } else { 1012 name1[2] = 1; 1013 l1++; 1014 } 1015 for (;;) { 1016 l2 = sizeof(name2); 1017 j = sysctl(name1, l1, name2, &l2, 0, 0); 1018 if (j < 0) { 1019 if (errno == ENOENT) 1020 return (0); 1021 else 1022 err(1, "sysctl(getnext) %d %zu", j, l2); 1023 } 1024 1025 l2 /= sizeof(int); 1026 1027 if (len < 0 || l2 < (unsigned int)len) 1028 return (0); 1029 1030 for (i = 0; i < len; i++) 1031 if (name2[i] != oid[i]) 1032 return (0); 1033 1034 i = show_var(name2, l2); 1035 if (!i && !bflag) 1036 putchar('\n'); 1037 1038 memcpy(name1+2, name2, l2 * sizeof(int)); 1039 l1 = 2 + l2; 1040 } 1041 } 1042