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