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 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 (strncmp(fmt, "IK", 2) == 0) 340 intval = strIKtoi(newvalstr, &endptr, fmt); 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, const char *fmt) 670 { 671 int kelv; 672 float temp; 673 size_t len; 674 const char *p; 675 int prec, i; 676 677 assert(errno == 0); 678 679 len = strlen(str); 680 /* caller already checked this */ 681 assert(len > 0); 682 683 /* 684 * A format of "IK" is in deciKelvin. A format of "IK3" is in 685 * milliKelvin. The single digit following IK is log10 of the 686 * multiplying factor to convert Kelvin into the untis of this sysctl, 687 * or the dividing factor to convert the sysctl value to Kelvin. Numbers 688 * larger than 6 will run into precision issues with 32-bit integers. 689 * Characters that aren't ASCII digits after the 'K' are ignored. No 690 * localization is present because this is an interface from the kernel 691 * to this program (eg not an end-user interface), so isdigit() isn't 692 * used here. 693 */ 694 if (fmt[2] != '\0' && fmt[2] >= '0' && fmt[2] <= '9') 695 prec = fmt[2] - '0'; 696 else 697 prec = 1; 698 p = &str[len - 1]; 699 if (*p == 'C' || *p == 'F' || *p == 'K') { 700 temp = strtof(str, endptrp); 701 if (*endptrp != str && *endptrp == p && errno == 0) { 702 if (*p == 'F') 703 temp = (temp - 32) * 5 / 9; 704 *endptrp = NULL; 705 if (*p != 'K') 706 temp += 273.15; 707 for (i = 0; i < prec; i++) 708 temp *= 10.0; 709 return ((int)(temp + 0.5)); 710 } 711 } else { 712 /* No unit specified -> treat it as a raw number */ 713 kelv = (int)strtol(str, endptrp, 10); 714 if (*endptrp != str && *endptrp == p && errno == 0) { 715 *endptrp = NULL; 716 return (kelv); 717 } 718 } 719 720 errno = ERANGE; 721 return (0); 722 } 723 724 /* 725 * These functions uses a presently undocumented interface to the kernel 726 * to walk the tree and get the type so it can print the value. 727 * This interface is under work and consideration, and should probably 728 * be killed with a big axe by the first person who can find the time. 729 * (be aware though, that the proper interface isn't as obvious as it 730 * may seem, there are various conflicting requirements. 731 */ 732 733 static int 734 name2oid(const char *name, int *oidp) 735 { 736 int oid[2]; 737 int i; 738 size_t j; 739 740 oid[0] = 0; 741 oid[1] = 3; 742 743 j = CTL_MAXNAME * sizeof(int); 744 i = sysctl(oid, 2, oidp, &j, name, strlen(name)); 745 if (i < 0) 746 return (i); 747 j /= sizeof(int); 748 return (j); 749 } 750 751 static int 752 oidfmt(int *oid, int len, char *fmt, u_int *kind) 753 { 754 int qoid[CTL_MAXNAME+2]; 755 u_char buf[BUFSIZ]; 756 int i; 757 size_t j; 758 759 qoid[0] = 0; 760 qoid[1] = 4; 761 memcpy(qoid + 2, oid, len * sizeof(int)); 762 763 j = sizeof(buf); 764 i = sysctl(qoid, len + 2, buf, &j, 0, 0); 765 if (i) 766 err(1, "sysctl fmt %d %zu %d", i, j, errno); 767 768 if (kind) 769 *kind = *(u_int *)buf; 770 771 if (fmt) 772 strcpy(fmt, (char *)(buf + sizeof(u_int))); 773 return (0); 774 } 775 776 /* 777 * This formats and outputs the value of one variable 778 * 779 * Returns zero if anything was actually output. 780 * Returns one if didn't know what to do with this. 781 * Return minus one if we had errors. 782 */ 783 static int 784 show_var(int *oid, int nlen) 785 { 786 u_char buf[BUFSIZ], *val, *oval, *p; 787 char name[BUFSIZ], fmt[BUFSIZ]; 788 const char *sep, *sep1; 789 int qoid[CTL_MAXNAME+2]; 790 uintmax_t umv; 791 intmax_t mv; 792 int i, hexlen, sign, ctltype; 793 size_t intlen; 794 size_t j, len; 795 u_int kind; 796 float base; 797 int (*func)(size_t, void *); 798 int prec; 799 800 /* Silence GCC. */ 801 umv = mv = intlen = 0; 802 803 bzero(buf, BUFSIZ); 804 bzero(fmt, BUFSIZ); 805 bzero(name, BUFSIZ); 806 qoid[0] = 0; 807 memcpy(qoid + 2, oid, nlen * sizeof(int)); 808 809 qoid[1] = 1; 810 j = sizeof(name); 811 i = sysctl(qoid, nlen + 2, name, &j, 0, 0); 812 if (i || !j) 813 err(1, "sysctl name %d %zu %d", i, j, errno); 814 815 oidfmt(oid, nlen, fmt, &kind); 816 /* if Wflag then only list sysctls that are writeable and not stats. */ 817 if (Wflag && ((kind & CTLFLAG_WR) == 0 || (kind & CTLFLAG_STATS) != 0)) 818 return 1; 819 820 /* if Tflag then only list sysctls that are tuneables. */ 821 if (Tflag && (kind & CTLFLAG_TUN) == 0) 822 return 1; 823 824 if (Nflag) { 825 printf("%s", name); 826 return (0); 827 } 828 829 if (eflag) 830 sep = "="; 831 else 832 sep = ": "; 833 834 if (dflag) { /* just print description */ 835 qoid[1] = 5; 836 j = sizeof(buf); 837 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0); 838 if (!nflag) 839 printf("%s%s", name, sep); 840 printf("%s", buf); 841 return (0); 842 } 843 /* find an estimate of how much we need for this var */ 844 if (Bflag) 845 j = Bflag; 846 else { 847 j = 0; 848 i = sysctl(oid, nlen, 0, &j, 0, 0); 849 j += j; /* we want to be sure :-) */ 850 } 851 852 val = oval = malloc(j + 1); 853 if (val == NULL) { 854 warnx("malloc failed"); 855 return (1); 856 } 857 ctltype = (kind & CTLTYPE); 858 len = j; 859 i = sysctl(oid, nlen, val, &len, 0, 0); 860 if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) { 861 free(oval); 862 return (1); 863 } 864 865 if (bflag) { 866 fwrite(val, 1, len, stdout); 867 free(oval); 868 return (0); 869 } 870 val[len] = '\0'; 871 p = val; 872 sign = ctl_sign[ctltype]; 873 intlen = ctl_size[ctltype]; 874 875 switch (ctltype) { 876 case CTLTYPE_STRING: 877 if (!nflag) 878 printf("%s%s", name, sep); 879 printf("%.*s", (int)len, p); 880 free(oval); 881 return (0); 882 883 case CTLTYPE_INT: 884 case CTLTYPE_UINT: 885 case CTLTYPE_LONG: 886 case CTLTYPE_ULONG: 887 case CTLTYPE_S64: 888 case CTLTYPE_U64: 889 if (!nflag) 890 printf("%s%s", name, sep); 891 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4; 892 sep1 = ""; 893 while (len >= intlen) { 894 switch (kind & CTLTYPE) { 895 case CTLTYPE_INT: 896 case CTLTYPE_UINT: 897 umv = *(u_int *)p; 898 mv = *(int *)p; 899 break; 900 case CTLTYPE_LONG: 901 case CTLTYPE_ULONG: 902 umv = *(u_long *)p; 903 mv = *(long *)p; 904 break; 905 case CTLTYPE_S64: 906 case CTLTYPE_U64: 907 umv = *(uint64_t *)p; 908 mv = *(int64_t *)p; 909 break; 910 } 911 fputs(sep1, stdout); 912 if (xflag) 913 printf("%#0*jx", hexlen, umv); 914 else if (!sign) 915 printf(hflag ? "%'ju" : "%ju", umv); 916 else if (fmt[1] == 'K') { 917 if (mv < 0) 918 printf("%jd", mv); 919 else { 920 /* 921 * See strIKtoi for details on fmt. 922 */ 923 prec = 1; 924 if (fmt[2] != '\0') 925 prec = fmt[2] - '0'; 926 base = 1.0; 927 for (int i = 0; i < prec; i++) 928 base *= 10.0; 929 printf("%.*fC", prec, 930 (float)mv / base - 273.15); 931 } 932 } else 933 printf(hflag ? "%'jd" : "%jd", mv); 934 sep1 = " "; 935 len -= intlen; 936 p += intlen; 937 } 938 free(oval); 939 return (0); 940 941 case CTLTYPE_OPAQUE: 942 i = 0; 943 if (strcmp(fmt, "S,clockinfo") == 0) 944 func = S_clockinfo; 945 else if (strcmp(fmt, "S,timeval") == 0) 946 func = S_timeval; 947 else if (strcmp(fmt, "S,loadavg") == 0) 948 func = S_loadavg; 949 else if (strcmp(fmt, "S,vmtotal") == 0) 950 func = S_vmtotal; 951 #ifdef __amd64__ 952 else if (strcmp(fmt, "S,efi_map_header") == 0) 953 func = S_efi_map; 954 #endif 955 #if defined(__amd64__) || defined(__i386__) 956 else if (strcmp(fmt, "S,bios_smap_xattr") == 0) 957 func = S_bios_smap_xattr; 958 #endif 959 else 960 func = NULL; 961 if (func) { 962 if (!nflag) 963 printf("%s%s", name, sep); 964 i = (*func)(len, p); 965 free(oval); 966 return (i); 967 } 968 /* FALLTHROUGH */ 969 default: 970 if (!oflag && !xflag) { 971 free(oval); 972 return (1); 973 } 974 if (!nflag) 975 printf("%s%s", name, sep); 976 printf("Format:%s Length:%zu Dump:0x", fmt, len); 977 while (len-- && (xflag || p < val + 16)) 978 printf("%02x", *p++); 979 if (!xflag && len > 16) 980 printf("..."); 981 free(oval); 982 return (0); 983 } 984 free(oval); 985 return (1); 986 } 987 988 static int 989 sysctl_all(int *oid, int len) 990 { 991 int name1[22], name2[22]; 992 int i, j; 993 size_t l1, l2; 994 995 name1[0] = 0; 996 name1[1] = 2; 997 l1 = 2; 998 if (len) { 999 memcpy(name1+2, oid, len * sizeof(int)); 1000 l1 += len; 1001 } else { 1002 name1[2] = 1; 1003 l1++; 1004 } 1005 for (;;) { 1006 l2 = sizeof(name2); 1007 j = sysctl(name1, l1, name2, &l2, 0, 0); 1008 if (j < 0) { 1009 if (errno == ENOENT) 1010 return (0); 1011 else 1012 err(1, "sysctl(getnext) %d %zu", j, l2); 1013 } 1014 1015 l2 /= sizeof(int); 1016 1017 if (len < 0 || l2 < (unsigned int)len) 1018 return (0); 1019 1020 for (i = 0; i < len; i++) 1021 if (name2[i] != oid[i]) 1022 return (0); 1023 1024 i = show_var(name2, l2); 1025 if (!i && !bflag) 1026 putchar('\n'); 1027 1028 memcpy(name1+2, name2, l2 * sizeof(int)); 1029 l1 = 2 + l2; 1030 } 1031 } 1032