1 /* $NetBSD: sysctl.c,v 1.130 2009/09/30 04:30:50 elad Exp $ */ 2 3 /*- 4 * Copyright (c) 2003 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Brown. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Copyright (c) 1993 34 * The Regents of the University of California. All rights reserved. 35 * 36 * Redistribution and use in source and binary forms, with or without 37 * modification, are permitted provided that the following conditions 38 * are met: 39 * 1. Redistributions of source code must retain the above copyright 40 * notice, this list of conditions and the following disclaimer. 41 * 2. Redistributions in binary form must reproduce the above copyright 42 * notice, this list of conditions and the following disclaimer in the 43 * documentation and/or other materials provided with the distribution. 44 * 3. Neither the name of the University nor the names of its contributors 45 * may be used to endorse or promote products derived from this software 46 * without specific prior written permission. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 58 * SUCH DAMAGE. 59 */ 60 61 #include <sys/cdefs.h> 62 #ifndef lint 63 __COPYRIGHT("@(#) Copyright (c) 1993\ 64 The Regents of the University of California. All rights reserved."); 65 #endif /* not lint */ 66 67 #ifndef lint 68 #if 0 69 static char sccsid[] = "@(#)sysctl.c 8.1 (Berkeley) 6/6/93"; 70 #else 71 __RCSID("$NetBSD: sysctl.c,v 1.130 2009/09/30 04:30:50 elad Exp $"); 72 #endif 73 #endif /* not lint */ 74 75 #include <sys/types.h> 76 #include <sys/param.h> 77 #include <sys/sysctl.h> 78 #include <sys/mount.h> 79 #include <sys/resource.h> 80 #include <sys/stat.h> 81 #include <sys/sched.h> 82 #include <sys/socket.h> 83 #include <netinet/in.h> 84 #include <netinet/ip_var.h> 85 #include <netinet/tcp.h> 86 #include <netinet/tcp_timer.h> 87 #include <netinet/tcp_var.h> 88 #include <netinet/icmp6.h> 89 #include <nfs/rpcv2.h> 90 #include <nfs/nfsproto.h> 91 #include <nfs/nfs.h> 92 #include <machine/cpu.h> 93 #include <netkey/key_var.h> 94 95 #include <assert.h> 96 #include <ctype.h> 97 #include <err.h> 98 #include <errno.h> 99 #include <inttypes.h> 100 #include <regex.h> 101 #include <stdarg.h> 102 #include <stdio.h> 103 #include <stdlib.h> 104 #include <string.h> 105 #include <time.h> 106 #include <unistd.h> 107 108 /* 109 * this needs to be able to do the printing and the setting 110 */ 111 #define HANDLER_PROTO const char *, const char *, char *, \ 112 int *, u_int, const struct sysctlnode *, \ 113 u_int, void * 114 #define HANDLER_ARGS const char *sname, const char *dname, char *value, \ 115 int *name, u_int namelen, const struct sysctlnode *pnode, \ 116 u_int type, void *v 117 #define DISPLAY_VALUE 0 118 #define DISPLAY_OLD 1 119 #define DISPLAY_NEW 2 120 121 /* 122 * generic routines 123 */ 124 static const struct handlespec *findhandler(const char *, int); 125 static void canonicalize(const char *, char *); 126 static void purge_tree(struct sysctlnode *); 127 static void print_tree(int *, u_int, struct sysctlnode *, u_int, int); 128 static void write_number(int *, u_int, struct sysctlnode *, char *); 129 static void write_string(int *, u_int, struct sysctlnode *, char *); 130 static void display_number(const struct sysctlnode *, const char *, 131 const void *, size_t, int); 132 static void display_string(const struct sysctlnode *, const char *, 133 const void *, size_t, int); 134 static void display_struct(const struct sysctlnode *, const char *, 135 const void *, size_t, int); 136 static void hex_dump(const unsigned char *, size_t); 137 static void usage(void); 138 static void parse(char *); 139 static void parse_create(char *); 140 static void parse_destroy(char *); 141 static void parse_describe(char *); 142 static void getdesc1(int *, u_int, struct sysctlnode *); 143 static void getdesc(int *, u_int, struct sysctlnode *); 144 static void trim_whitespace(char *, int); 145 static void sysctlerror(int); 146 static void sysctlparseerror(u_int, const char *); 147 static void sysctlperror(const char *, ...); 148 #define EXIT(n) do { \ 149 if (fn == NULL) exit(n); else return; } while (/*CONSTCOND*/0) 150 151 /* 152 * "borrowed" from libc:sysctlgetmibinfo.c 153 */ 154 int __learn_tree(int *, u_int, struct sysctlnode *); 155 156 /* 157 * "handlers" 158 */ 159 static void printother(HANDLER_PROTO); 160 static void kern_clockrate(HANDLER_PROTO); 161 static void kern_boottime(HANDLER_PROTO); 162 static void kern_consdev(HANDLER_PROTO); 163 static void kern_cp_time(HANDLER_PROTO); 164 static void kern_cp_id(HANDLER_PROTO); 165 static void kern_drivers(HANDLER_PROTO); 166 static void vm_loadavg(HANDLER_PROTO); 167 static void proc_limit(HANDLER_PROTO); 168 #ifdef CPU_DISKINFO 169 static void machdep_diskinfo(HANDLER_PROTO); 170 #endif /* CPU_DISKINFO */ 171 static void mode_bits(HANDLER_PROTO); 172 173 static const struct handlespec { 174 const char *ps_re; 175 void (*ps_p)(HANDLER_PROTO); 176 void (*ps_w)(HANDLER_PROTO); 177 const void *ps_d; 178 } handlers[] = { 179 { "/kern/clockrate", kern_clockrate, NULL, NULL }, 180 { "/kern/vnode", printother, NULL, "pstat" }, 181 { "/kern/proc(2|_args)?", printother, NULL, "ps" }, 182 { "/kern/file2?", printother, NULL, "pstat" }, 183 { "/kern/ntptime", printother, NULL, 184 "ntpdc -c kerninfo" }, 185 { "/kern/msgbuf", printother, NULL, "dmesg" }, 186 { "/kern/boottime", kern_boottime, NULL, NULL }, 187 { "/kern/consdev", kern_consdev, NULL, NULL }, 188 { "/kern/cp_time(/[0-9]+)?", kern_cp_time, NULL, NULL }, 189 { "/kern/sysvipc_info", printother, NULL, "ipcs" }, 190 { "/kern/cp_id(/[0-9]+)?", kern_cp_id, NULL, NULL }, 191 192 { "/kern/coredump/setid/mode", mode_bits, mode_bits, NULL }, 193 { "/kern/drivers", kern_drivers, NULL, NULL }, 194 195 { "/vm/vmmeter", printother, NULL, 196 "vmstat' or 'systat" }, 197 { "/vm/loadavg", vm_loadavg, NULL, NULL }, 198 { "/vm/uvmexp2?", printother, NULL, 199 "vmstat' or 'systat" }, 200 201 { "/vfs/nfs/nfsstats", printother, NULL, "nfsstat" }, 202 203 { "/net/inet6?/tcp6?/ident", printother, NULL, "identd" }, 204 { "/net/inet6/icmp6/nd6_[dp]rlist", printother, NULL, "ndp" }, 205 { "/net/key/dumps[ap]", printother, NULL, "setkey" }, 206 { "/net/[^/]+/[^/]+/pcblist", printother, NULL, 207 "netstat' or 'sockstat" }, 208 { "/net/(inet|inet6)/[^/]+/stats", printother, NULL, "netstat"}, 209 { "/net/bpf/(stats|peers)", printother, NULL, "netstat"}, 210 211 { "/net/inet.*/tcp.*/deb.*", printother, NULL, "trpt" }, 212 213 { "/net/ns/spp/deb.*", printother, NULL, "trsp" }, 214 215 { "/hw/diskstats", printother, NULL, "iostat" }, 216 217 #ifdef CPU_CONSDEV 218 { "/machdep/consdev", kern_consdev, NULL, NULL }, 219 #endif /* CPU_CONSDEV */ 220 #ifdef CPU_DISKINFO 221 { "/machdep/diskinfo", machdep_diskinfo, NULL, NULL }, 222 #endif /* CPU_CONSDEV */ 223 224 { "/proc/[^/]+/rlimit/[^/]+/[^/]+", proc_limit, proc_limit, NULL }, 225 226 /* 227 * these will only be called when the given node has no children 228 */ 229 { "/net/[^/]+", printother, NULL, NULL }, 230 { "/debug", printother, NULL, NULL }, 231 { "/ddb", printother, NULL, NULL }, 232 { "/vendor", printother, NULL, NULL }, 233 234 { NULL, NULL, NULL, NULL }, 235 }; 236 237 struct sysctlnode my_root = { 238 .sysctl_flags = SYSCTL_VERSION|CTLFLAG_ROOT|CTLTYPE_NODE, 239 sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)), 240 .sysctl_num = 0, 241 .sysctl_name = "(prog_root)", 242 }; 243 244 int Aflag, aflag, dflag, Mflag, nflag, qflag, rflag, wflag, xflag; 245 size_t nr; 246 char *fn; 247 int req, stale, errs; 248 FILE *warnfp = stderr; 249 250 /* 251 * vah-riables n stuff 252 */ 253 char gsname[SYSCTL_NAMELEN * CTL_MAXNAME + CTL_MAXNAME], 254 canonname[SYSCTL_NAMELEN * CTL_MAXNAME + CTL_MAXNAME], 255 gdname[10 * CTL_MAXNAME + CTL_MAXNAME]; 256 char sep[] = "."; 257 const char *eq = " = "; 258 const char *lname[] = { 259 "top", "second", "third", "fourth", "fifth", "sixth", 260 "seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth" 261 }; 262 263 /* 264 * you've heard of main, haven't you? 265 */ 266 int 267 main(int argc, char *argv[]) 268 { 269 int name[CTL_MAXNAME]; 270 int ch; 271 272 while ((ch = getopt(argc, argv, "Aabdef:Mnqrwx")) != -1) { 273 switch (ch) { 274 case 'A': 275 Aflag++; 276 break; 277 case 'a': 278 aflag++; 279 break; 280 case 'd': 281 dflag++; 282 break; 283 case 'e': 284 eq = "="; 285 break; 286 case 'f': 287 fn = optarg; 288 wflag++; 289 break; 290 case 'M': 291 Mflag++; 292 break; 293 case 'n': 294 nflag++; 295 break; 296 case 'q': 297 qflag++; 298 break; 299 case 'b': /* FreeBSD compat */ 300 case 'r': 301 rflag++; 302 break; 303 case 'w': 304 wflag++; 305 break; 306 case 'x': 307 xflag++; 308 break; 309 default: 310 usage(); 311 } 312 } 313 314 argc -= optind; 315 argv += optind; 316 317 if (qflag && !wflag) 318 usage(); 319 if (xflag && rflag) 320 usage(); 321 /* if ((xflag || rflag) && wflag) 322 usage(); */ 323 /* if (aflag && Mflag) 324 usage(); */ 325 if ((Aflag || Mflag || dflag) && argc == 0 && fn == NULL) 326 aflag = 1; 327 328 if (Aflag) 329 warnfp = stdout; 330 stale = req = 0; 331 332 if (aflag) { 333 print_tree(&name[0], 0, NULL, CTLTYPE_NODE, 1); 334 /* if (argc == 0) */ 335 return (0); 336 } 337 338 if (fn) { 339 FILE *fp; 340 char *l; 341 342 fp = fopen(fn, "r"); 343 if (fp == NULL) { 344 err(1, "%s", fn); 345 } else { 346 nr = 0; 347 while ((l = fparseln(fp, NULL, &nr, NULL, 0)) != NULL) 348 { 349 if (*l) { 350 parse(l); 351 free(l); 352 } 353 } 354 fclose(fp); 355 } 356 return errs ? 1 : 0; 357 } 358 359 if (argc == 0) 360 usage(); 361 362 while (argc-- > 0) 363 parse(*argv++); 364 365 return errs ? 1 : 0; 366 } 367 368 /* 369 * ******************************************************************** 370 * how to find someone special to handle the reading (or maybe even 371 * writing) of a particular node 372 * ******************************************************************** 373 */ 374 static const struct handlespec * 375 findhandler(const char *s, int w) 376 { 377 const struct handlespec *p; 378 regex_t re; 379 int i, j, l; 380 char eb[64]; 381 regmatch_t match[1]; 382 383 p = &handlers[0]; 384 l = strlen(s); 385 for (i = 0; p[i].ps_re != NULL; i++) { 386 j = regcomp(&re, p[i].ps_re, REG_EXTENDED); 387 if (j != 0) { 388 regerror(j, &re, eb, sizeof(eb)); 389 errx(1, "regcomp: %s: %s", p[i].ps_re, eb); 390 } 391 j = regexec(&re, s, 1, match, 0); 392 if (j == 0) { 393 if (match[0].rm_so == 0 && match[0].rm_eo == l && 394 (w ? p[i].ps_w : p[i].ps_p) != NULL) { 395 regfree(&re); 396 return (&p[i]); 397 } 398 } 399 else if (j != REG_NOMATCH) { 400 regerror(j, &re, eb, sizeof(eb)); 401 errx(1, "regexec: %s: %s", p[i].ps_re, eb); 402 } 403 regfree(&re); 404 } 405 406 return (NULL); 407 } 408 409 /* 410 * after sysctlgetmibinfo is done with the name, we convert all 411 * separators to / and stuff one at the front if it was missing 412 */ 413 static void 414 canonicalize(const char *i, char *o) 415 { 416 const char *t; 417 char p[SYSCTL_NAMELEN + 1]; 418 int l; 419 420 if (i[0] != *sep) { 421 o[0] = '/'; 422 o[1] = '\0'; 423 } 424 else 425 o[0] = '\0'; 426 427 t = i; 428 do { 429 i = t; 430 t = strchr(i, sep[0]); 431 if (t == NULL) 432 strcat(o, i); 433 else { 434 l = t - i; 435 t++; 436 memcpy(p, i, l); 437 p[l] = '\0'; 438 strcat(o, p); 439 strcat(o, "/"); 440 } 441 } while (t != NULL); 442 } 443 444 /* 445 * ******************************************************************** 446 * convert this special number to a special string so we can print the 447 * mib 448 * ******************************************************************** 449 */ 450 static const char * 451 sf(u_int f) 452 { 453 static char s[256]; 454 const char *c; 455 456 s[0] = '\0'; 457 c = ""; 458 459 #define print_flag(_f, _s, _c, _q, _x) \ 460 if (((_f) & (__CONCAT(CTLFLAG_,_x))) == (__CONCAT(CTLFLAG_,_q))) { \ 461 strlcat((_s), (_c), sizeof(_s)); \ 462 strlcat((_s), __STRING(_q), sizeof(_s)); \ 463 (_c) = ","; \ 464 (_f) &= ~(__CONCAT(CTLFLAG_,_x)); \ 465 } 466 print_flag(f, s, c, READONLY, READWRITE); 467 print_flag(f, s, c, READWRITE, READWRITE); 468 print_flag(f, s, c, ANYWRITE, ANYWRITE); 469 print_flag(f, s, c, PRIVATE, PRIVATE); 470 print_flag(f, s, c, PERMANENT, PERMANENT); 471 print_flag(f, s, c, OWNDATA, OWNDATA); 472 print_flag(f, s, c, IMMEDIATE, IMMEDIATE); 473 print_flag(f, s, c, HEX, HEX); 474 print_flag(f, s, c, ROOT, ROOT); 475 print_flag(f, s, c, ANYNUMBER, ANYNUMBER); 476 print_flag(f, s, c, HIDDEN, HIDDEN); 477 print_flag(f, s, c, ALIAS, ALIAS); 478 #undef print_flag 479 480 if (f) { 481 char foo[9]; 482 snprintf(foo, sizeof(foo), "%x", f); 483 strlcat(s, c, sizeof(s)); 484 strlcat(s, foo, sizeof(s)); 485 } 486 487 return (s); 488 } 489 490 static const char * 491 st(u_int t) 492 { 493 494 switch (t) { 495 case CTLTYPE_NODE: 496 return "NODE"; 497 case CTLTYPE_INT: 498 return "INT"; 499 case CTLTYPE_STRING: 500 return "STRING"; 501 case CTLTYPE_QUAD: 502 return "QUAD"; 503 case CTLTYPE_STRUCT: 504 return "STRUCT"; 505 } 506 507 return "???"; 508 } 509 510 /* 511 * ******************************************************************** 512 * recursively eliminate all data belonging to the given node 513 * ******************************************************************** 514 */ 515 static void 516 purge_tree(struct sysctlnode *rnode) 517 { 518 struct sysctlnode *node; 519 520 if (rnode == NULL || 521 SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE || 522 rnode->sysctl_child == NULL) 523 return; 524 525 for (node = rnode->sysctl_child; 526 node < &rnode->sysctl_child[rnode->sysctl_clen]; 527 node++) 528 purge_tree(node); 529 free(rnode->sysctl_child); 530 rnode->sysctl_csize = 0; 531 rnode->sysctl_clen = 0; 532 rnode->sysctl_child = NULL; 533 534 if (rnode->sysctl_desc == (const char*)-1) 535 rnode->sysctl_desc = NULL; 536 if (rnode->sysctl_desc != NULL) 537 free(__UNCONST(rnode->sysctl_desc)); 538 rnode->sysctl_desc = NULL; 539 } 540 541 /* 542 * ******************************************************************** 543 * print this node and any others underneath it 544 * ******************************************************************** 545 */ 546 static void 547 print_tree(int *name, u_int namelen, struct sysctlnode *pnode, u_int type, 548 int add) 549 { 550 struct sysctlnode *node; 551 int rc; 552 size_t ni, sz; 553 char *sp, *dp, n[20]; 554 const struct handlespec *p; 555 556 sp = &gsname[strlen(gsname)]; 557 dp = &gdname[strlen(gdname)]; 558 559 if (sp != &gsname[0] && dp == &gdname[0]) { 560 /* 561 * aw...shucks. now we must play catch up 562 */ 563 for (ni = 0; ni < namelen; ni++) { 564 (void)snprintf(n, sizeof(n), "%d", name[ni]); 565 if (ni > 0) 566 strncat(gdname, ".", sizeof(gdname)); 567 strncat(gdname, n, sizeof(gdname)); 568 } 569 } 570 571 if (pnode == NULL) 572 pnode = &my_root; 573 else if (add) { 574 snprintf(n, sizeof(n), "%d", pnode->sysctl_num); 575 if (namelen > 1) { 576 strncat(gsname, sep, sizeof(gsname)); 577 strncat(gdname, ".", sizeof(gdname)); 578 } 579 strncat(gsname, pnode->sysctl_name, sizeof(gsname)); 580 strncat(gdname, n, sizeof(gdname)); 581 } 582 583 if (Mflag && pnode != &my_root) { 584 if (nflag) 585 printf("%s: ", gdname); 586 else 587 printf("%s (%s): ", gsname, gdname); 588 printf("CTLTYPE_%s", st(type)); 589 if (type == CTLTYPE_NODE) { 590 if (SYSCTL_FLAGS(pnode->sysctl_flags) & CTLFLAG_ALIAS) 591 printf(", alias %d", 592 pnode->sysctl_alias); 593 else 594 printf(", children %d/%d", 595 pnode->sysctl_clen, 596 pnode->sysctl_csize); 597 } 598 printf(", size %zu", pnode->sysctl_size); 599 printf(", flags 0x%x<%s>", 600 SYSCTL_FLAGS(pnode->sysctl_flags), 601 sf(SYSCTL_FLAGS(pnode->sysctl_flags))); 602 if (pnode->sysctl_func) 603 printf(", func=%p", pnode->sysctl_func); 604 printf(", ver=%d", pnode->sysctl_ver); 605 printf("\n"); 606 if (type != CTLTYPE_NODE) { 607 *sp = *dp = '\0'; 608 return; 609 } 610 } 611 612 if (dflag && pnode != &my_root) { 613 if (Aflag || type != CTLTYPE_NODE) { 614 if (pnode->sysctl_desc == NULL) 615 getdesc1(name, namelen, pnode); 616 if (Aflag || !add || 617 (pnode->sysctl_desc != NULL && 618 pnode->sysctl_desc != (const char*)-1)) { 619 if (!nflag) 620 printf("%s: ", gsname); 621 if (pnode->sysctl_desc == NULL || 622 pnode->sysctl_desc == (const char*)-1) 623 printf("(no description)\n"); 624 else 625 printf("%s\n", pnode->sysctl_desc); 626 } 627 } 628 629 if (type != CTLTYPE_NODE) { 630 *sp = *dp = '\0'; 631 return; 632 } 633 } 634 635 /* 636 * if this is an alias and we added our name, that means we 637 * got here by recursing down into the tree, so skip it. The 638 * only way to print an aliased node is with either -M or by 639 * name specifically. 640 */ 641 if (SYSCTL_FLAGS(pnode->sysctl_flags) & CTLFLAG_ALIAS && add) { 642 *sp = *dp = '\0'; 643 return; 644 } 645 646 canonicalize(gsname, canonname); 647 p = findhandler(canonname, 0); 648 if (type != CTLTYPE_NODE && p != NULL) { 649 (*p->ps_p)(gsname, gdname, NULL, name, namelen, pnode, type, 650 __UNCONST(p->ps_d)); 651 *sp = *dp = '\0'; 652 return; 653 } 654 655 if (type != CTLTYPE_NODE && pnode->sysctl_size == 0) { 656 rc = sysctl(&name[0], namelen, NULL, &sz, NULL, 0); 657 if (rc == -1) { 658 sysctlerror(1); 659 *sp = *dp = '\0'; 660 return; 661 } 662 if (sz == 0) { 663 if ((Aflag || req) && !Mflag) 664 printf("%s: node contains no data\n", gsname); 665 *sp = *dp = '\0'; 666 return; 667 } 668 } 669 else 670 sz = pnode->sysctl_size; 671 672 switch (type) { 673 case CTLTYPE_NODE: { 674 __learn_tree(name, namelen, pnode); 675 node = pnode->sysctl_child; 676 if (node == NULL) { 677 if (dflag) 678 /* do nothing */; 679 else if (p != NULL) 680 (*p->ps_p)(gsname, gdname, NULL, name, namelen, 681 pnode, type, __UNCONST(p->ps_d)); 682 else if ((Aflag || req) && !Mflag) 683 printf("%s: no children\n", gsname); 684 } 685 else { 686 if (dflag) 687 /* 688 * get all descriptions for next level 689 * in one chunk 690 */ 691 getdesc(name, namelen, pnode); 692 req = 0; 693 for (ni = 0; ni < pnode->sysctl_clen; ni++) { 694 name[namelen] = node[ni].sysctl_num; 695 if ((node[ni].sysctl_flags & CTLFLAG_HIDDEN) && 696 !(Aflag || req)) 697 continue; 698 print_tree(name, namelen + 1, &node[ni], 699 SYSCTL_TYPE(node[ni].sysctl_flags), 700 1); 701 } 702 } 703 break; 704 } 705 case CTLTYPE_INT: { 706 int i; 707 rc = sysctl(name, namelen, &i, &sz, NULL, 0); 708 if (rc == -1) { 709 sysctlerror(1); 710 break; 711 } 712 display_number(pnode, gsname, &i, sizeof(i), DISPLAY_VALUE); 713 break; 714 } 715 case CTLTYPE_STRING: { 716 unsigned char buf[1024], *tbuf; 717 tbuf = buf; 718 sz = sizeof(buf); 719 rc = sysctl(&name[0], namelen, tbuf, &sz, NULL, 0); 720 if (rc == -1 && errno == ENOMEM) { 721 tbuf = malloc(sz); 722 if (tbuf == NULL) { 723 sysctlerror(1); 724 break; 725 } 726 rc = sysctl(&name[0], namelen, tbuf, &sz, NULL, 0); 727 } 728 if (rc == -1) 729 sysctlerror(1); 730 else 731 display_string(pnode, gsname, tbuf, sz, DISPLAY_VALUE); 732 if (tbuf != buf) 733 free(tbuf); 734 break; 735 } 736 case CTLTYPE_QUAD: { 737 u_quad_t q; 738 sz = sizeof(q); 739 rc = sysctl(&name[0], namelen, &q, &sz, NULL, 0); 740 if (rc == -1) { 741 sysctlerror(1); 742 break; 743 } 744 display_number(pnode, gsname, &q, sizeof(q), DISPLAY_VALUE); 745 break; 746 } 747 case CTLTYPE_STRUCT: { 748 /* 749 * we shouldn't actually get here, but if we 750 * do, would it be nice to have *something* to 751 * do other than completely ignore the 752 * request. 753 */ 754 unsigned char *d; 755 if ((d = malloc(sz)) == NULL) { 756 fprintf(warnfp, "%s: !malloc failed!\n", gsname); 757 break; 758 } 759 rc = sysctl(&name[0], namelen, d, &sz, NULL, 0); 760 if (rc == -1) { 761 sysctlerror(1); 762 break; 763 } 764 display_struct(pnode, gsname, d, sz, DISPLAY_VALUE); 765 free(d); 766 break; 767 } 768 default: 769 /* should i print an error here? */ 770 break; 771 } 772 773 *sp = *dp = '\0'; 774 } 775 776 /* 777 * ******************************************************************** 778 * parse a request, possibly determining that it's a create or destroy 779 * request 780 * ******************************************************************** 781 */ 782 static void 783 parse(char *l) 784 { 785 struct sysctlnode *node; 786 const struct handlespec *w; 787 int name[CTL_MAXNAME], dodesc = 0; 788 u_int namelen, type; 789 char *key, *value, *dot; 790 size_t sz; 791 bool optional = false; 792 793 req = 1; 794 key = l; 795 796 if ((value = strchr(l, '=')) != NULL) { 797 if (value > l && value[-1] == '?') { 798 value[-1] = '\0'; 799 optional = true; 800 } 801 *value++ = '\0'; 802 } 803 804 if ((dot = strpbrk(key, "./")) == NULL) 805 sep[0] = '.'; 806 else 807 sep[0] = dot[0]; 808 sep[1] = '\0'; 809 810 while (key[0] == sep[0] && key[1] == sep[0]) { 811 if (value != NULL) 812 value[-1] = '='; 813 if (strncmp(key + 2, "create", 6) == 0 && 814 (key[8] == '=' || key[8] == sep[0])) 815 parse_create(key + 8 + (key[8] == '=' ? 1 : 0)); 816 else if (strncmp(key + 2, "destroy", 7) == 0 && 817 (key[9] == '=' || key[9] == sep[0])) 818 parse_destroy(key + 9 + (key[9] == '=' ? 1 : 0)); 819 else if (strncmp(key + 2, "describe", 8) == 0 && 820 (key[10] == '=' || key[10] == sep[0])) { 821 key += 10 + (key[10] == '='); 822 if ((value = strchr(key, '=')) != NULL) 823 parse_describe(key); 824 else { 825 if (!dflag) 826 dodesc = 1; 827 break; 828 } 829 } 830 else 831 sysctlperror("unable to parse '%s'\n", key); 832 return; 833 } 834 835 if (stale) { 836 purge_tree(&my_root); 837 stale = 0; 838 } 839 node = &my_root; 840 namelen = CTL_MAXNAME; 841 sz = sizeof(gsname); 842 843 if (sysctlgetmibinfo(key, &name[0], &namelen, gsname, &sz, &node, 844 SYSCTL_VERSION) == -1) { 845 if (optional) 846 return; 847 sysctlparseerror(namelen, l); 848 EXIT(1); 849 } 850 851 type = SYSCTL_TYPE(node->sysctl_flags); 852 853 if (value == NULL) { 854 if (dodesc) 855 dflag = 1; 856 print_tree(&name[0], namelen, node, type, 0); 857 if (dodesc) 858 dflag = 0; 859 gsname[0] = '\0'; 860 return; 861 } 862 863 if (fn) 864 trim_whitespace(value, 1); 865 866 if (!wflag) { 867 sysctlperror("Must specify -w to set variables\n"); 868 exit(1); 869 } 870 871 canonicalize(gsname, canonname); 872 if (type != CTLTYPE_NODE && (w = findhandler(canonname, 1)) != NULL) { 873 (*w->ps_w)(gsname, gdname, value, name, namelen, node, type, 874 __UNCONST(w->ps_d)); 875 gsname[0] = '\0'; 876 return; 877 } 878 879 switch (type) { 880 case CTLTYPE_NODE: 881 /* 882 * XXX old behavior is to print. should we error instead? 883 */ 884 print_tree(&name[0], namelen, node, CTLTYPE_NODE, 1); 885 break; 886 case CTLTYPE_INT: 887 write_number(&name[0], namelen, node, value); 888 break; 889 case CTLTYPE_STRING: 890 write_string(&name[0], namelen, node, value); 891 break; 892 case CTLTYPE_QUAD: 893 write_number(&name[0], namelen, node, value); 894 break; 895 case CTLTYPE_STRUCT: 896 /* 897 * XXX old behavior is to print. should we error instead? 898 */ 899 /* fprintf(warnfp, "you can't write to %s\n", gsname); */ 900 print_tree(&name[0], namelen, node, type, 0); 901 break; 902 } 903 } 904 905 /* 906 907 //create=foo.bar.whatever..., 908 [type=(int|quad|string|struct|node),] 909 [size=###,] 910 [n=###,] 911 [flags=(iohxparw12),] 912 [addr=0x####,|symbol=...|value=...] 913 914 size is optional for some types. type must be set before anything 915 else. nodes can have [rwhp], but nothing else applies. if no 916 size or type is given, node is asserted. writeable is the default, 917 with [rw] being read-only and unconditionally writeable 918 respectively. if you specify addr, it is assumed to be the name of 919 a kernel symbol, if value, CTLFLAG_OWNDATA will be asserted for 920 strings, CTLFLAG_IMMEDIATE for ints and u_quad_ts. you cannot 921 specify both value and addr. 922 923 */ 924 925 static void 926 parse_create(char *l) 927 { 928 struct sysctlnode node; 929 size_t sz; 930 char *nname, *key, *value, *data, *addr, *c, *t; 931 int name[CTL_MAXNAME], i, rc, method, flags, rw; 932 u_int namelen, type; 933 u_quad_t uq; 934 quad_t q; 935 936 if (!wflag) { 937 sysctlperror("Must specify -w to create nodes\n"); 938 exit(1); 939 } 940 941 /* 942 * these are the pieces that make up the description of a new 943 * node 944 */ 945 memset(&node, 0, sizeof(node)); 946 node.sysctl_num = CTL_CREATE; /* any number is fine */ 947 flags = 0; 948 rw = -1; 949 type = 0; 950 sz = 0; 951 data = addr = NULL; 952 memset(name, 0, sizeof(name)); 953 namelen = 0; 954 method = 0; 955 956 /* 957 * misc stuff used when constructing 958 */ 959 i = 0; 960 uq = 0; 961 key = NULL; 962 value = NULL; 963 964 /* 965 * the name of the thing we're trying to create is first, so 966 * pick it off. 967 */ 968 nname = l; 969 if ((c = strchr(nname, ',')) != NULL) 970 *c++ = '\0'; 971 972 while (c != NULL) { 973 974 /* 975 * pull off the next "key=value" pair 976 */ 977 key = c; 978 if ((t = strchr(key, '=')) != NULL) { 979 *t++ = '\0'; 980 value = t; 981 } 982 else 983 value = NULL; 984 985 /* 986 * if the "key" is "value", then that eats the rest of 987 * the string, so we're done, otherwise bite it off at 988 * the next comma. 989 */ 990 if (strcmp(key, "value") == 0) { 991 c = NULL; 992 data = value; 993 break; 994 } 995 else if (value) { 996 if ((c = strchr(value, ',')) != NULL) 997 *c++ = '\0'; 998 } 999 1000 /* 1001 * note that we (mostly) let the invoker of sysctl(8) 1002 * play rampant here and depend on the kernel to tell 1003 * them that they were wrong. well...within reason. 1004 * we later check the various parameters against each 1005 * other to make sure it makes some sort of sense. 1006 */ 1007 if (strcmp(key, "addr") == 0) { 1008 /* 1009 * we can't check these two. only the kernel 1010 * can tell us when it fails to find the name 1011 * (or if the address is invalid). 1012 */ 1013 if (method != 0) { 1014 sysctlperror( 1015 "%s: already have %s for new node\n", 1016 nname, 1017 method == CTL_CREATE ? "addr" : "symbol"); 1018 EXIT(1); 1019 } 1020 if (value == NULL) { 1021 sysctlperror("%s: missing value\n", nname); 1022 EXIT(1); 1023 } 1024 errno = 0; 1025 addr = (void*)strtoul(value, &t, 0); 1026 if (t == value || *t != '\0' || errno != 0) { 1027 sysctlperror( 1028 "%s: '%s' is not a valid address\n", 1029 nname, value); 1030 EXIT(1); 1031 } 1032 method = CTL_CREATE; 1033 } 1034 else if (strcmp(key, "symbol") == 0) { 1035 if (method != 0) { 1036 sysctlperror( 1037 "%s: already have %s for new node\n", 1038 nname, 1039 method == CTL_CREATE ? "addr" : "symbol"); 1040 EXIT(1); 1041 } 1042 addr = value; 1043 method = CTL_CREATESYM; 1044 } 1045 else if (strcmp(key, "type") == 0) { 1046 if (value == NULL) { 1047 sysctlperror("%s: missing value\n", nname); 1048 EXIT(1); 1049 } 1050 if (strcmp(value, "node") == 0) 1051 type = CTLTYPE_NODE; 1052 else if (strcmp(value, "int") == 0) { 1053 sz = sizeof(int); 1054 type = CTLTYPE_INT; 1055 } 1056 else if (strcmp(value, "string") == 0) 1057 type = CTLTYPE_STRING; 1058 else if (strcmp(value, "quad") == 0) { 1059 sz = sizeof(u_quad_t); 1060 type = CTLTYPE_QUAD; 1061 } 1062 else if (strcmp(value, "struct") == 0) 1063 type = CTLTYPE_STRUCT; 1064 else { 1065 sysctlperror( 1066 "%s: '%s' is not a valid type\n", 1067 nname, value); 1068 EXIT(1); 1069 } 1070 } 1071 else if (strcmp(key, "size") == 0) { 1072 if (value == NULL) { 1073 sysctlperror("%s: missing value\n", nname); 1074 EXIT(1); 1075 } 1076 errno = 0; 1077 /* 1078 * yes, i know size_t is not an unsigned long, 1079 * but we can all agree that it ought to be, 1080 * right? 1081 */ 1082 sz = strtoul(value, &t, 0); 1083 if (t == value || *t != '\0' || errno != 0) { 1084 sysctlperror( 1085 "%s: '%s' is not a valid size\n", 1086 nname, value); 1087 EXIT(1); 1088 } 1089 } 1090 else if (strcmp(key, "n") == 0) { 1091 if (value == NULL) { 1092 sysctlperror("%s: missing value\n", nname); 1093 EXIT(1); 1094 } 1095 errno = 0; 1096 q = strtoll(value, &t, 0); 1097 if (t == value || *t != '\0' || errno != 0 || 1098 q < INT_MIN || q > UINT_MAX) { 1099 sysctlperror( 1100 "%s: '%s' is not a valid mib number\n", 1101 nname, value); 1102 EXIT(1); 1103 } 1104 node.sysctl_num = (int)q; 1105 } 1106 else if (strcmp(key, "flags") == 0) { 1107 if (value == NULL) { 1108 sysctlperror("%s: missing value\n", nname); 1109 EXIT(1); 1110 } 1111 t = value; 1112 while (*t != '\0') { 1113 switch (*t) { 1114 case 'a': 1115 flags |= CTLFLAG_ANYWRITE; 1116 break; 1117 case 'h': 1118 flags |= CTLFLAG_HIDDEN; 1119 break; 1120 case 'i': 1121 flags |= CTLFLAG_IMMEDIATE; 1122 break; 1123 case 'o': 1124 flags |= CTLFLAG_OWNDATA; 1125 break; 1126 case 'p': 1127 flags |= CTLFLAG_PRIVATE; 1128 break; 1129 case 'x': 1130 flags |= CTLFLAG_HEX; 1131 break; 1132 1133 case 'r': 1134 rw = CTLFLAG_READONLY; 1135 break; 1136 case 'w': 1137 rw = CTLFLAG_READWRITE; 1138 break; 1139 default: 1140 sysctlperror( 1141 "%s: '%c' is not a valid flag\n", 1142 nname, *t); 1143 EXIT(1); 1144 } 1145 t++; 1146 } 1147 } 1148 else { 1149 sysctlperror("%s: unrecognized keyword '%s'\n", 1150 nname, key); 1151 EXIT(1); 1152 } 1153 } 1154 1155 /* 1156 * now that we've finished parsing the given string, fill in 1157 * anything they didn't specify 1158 */ 1159 if (type == 0) 1160 type = CTLTYPE_NODE; 1161 1162 /* 1163 * the "data" can be interpreted various ways depending on the 1164 * type of node we're creating, as can the size 1165 */ 1166 if (data != NULL) { 1167 if (addr != NULL) { 1168 sysctlperror( 1169 "%s: cannot specify both value and " 1170 "address\n", nname); 1171 EXIT(1); 1172 } 1173 1174 switch (type) { 1175 case CTLTYPE_INT: 1176 errno = 0; 1177 q = strtoll(data, &t, 0); 1178 if (t == data || *t != '\0' || errno != 0 || 1179 q < INT_MIN || q > UINT_MAX) { 1180 sysctlperror( 1181 "%s: '%s' is not a valid integer\n", 1182 nname, value); 1183 EXIT(1); 1184 } 1185 i = (int)q; 1186 if (!(flags & CTLFLAG_OWNDATA)) { 1187 flags |= CTLFLAG_IMMEDIATE; 1188 node.sysctl_idata = i; 1189 } 1190 else 1191 node.sysctl_data = &i; 1192 if (sz == 0) 1193 sz = sizeof(int); 1194 break; 1195 case CTLTYPE_STRING: 1196 flags |= CTLFLAG_OWNDATA; 1197 node.sysctl_data = data; 1198 if (sz == 0) 1199 sz = strlen(data) + 1; 1200 else if (sz < strlen(data) + 1) { 1201 sysctlperror("%s: ignoring size=%zu for " 1202 "string node, too small for given " 1203 "value\n", nname, sz); 1204 sz = strlen(data) + 1; 1205 } 1206 break; 1207 case CTLTYPE_QUAD: 1208 errno = 0; 1209 uq = strtouq(data, &t, 0); 1210 if (t == data || *t != '\0' || errno != 0) { 1211 sysctlperror( 1212 "%s: '%s' is not a valid quad\n", 1213 nname, value); 1214 EXIT(1); 1215 } 1216 if (!(flags & CTLFLAG_OWNDATA)) { 1217 flags |= CTLFLAG_IMMEDIATE; 1218 node.sysctl_qdata = uq; 1219 } 1220 else 1221 node.sysctl_data = &uq; 1222 if (sz == 0) 1223 sz = sizeof(u_quad_t); 1224 break; 1225 case CTLTYPE_STRUCT: 1226 sysctlperror("%s: struct not initializable\n", 1227 nname); 1228 EXIT(1); 1229 } 1230 1231 /* 1232 * these methods have all provided local starting 1233 * values that the kernel must copy in 1234 */ 1235 } 1236 1237 /* 1238 * hmm...no data, but we have an address of data. that's 1239 * fine. 1240 */ 1241 else if (addr != 0) 1242 node.sysctl_data = (void*)addr; 1243 1244 /* 1245 * no data and no address? well...okay. we might be able to 1246 * manage that. 1247 */ 1248 else if (type != CTLTYPE_NODE) { 1249 if (sz == 0) { 1250 sysctlperror( 1251 "%s: need a size or a starting value\n", 1252 nname); 1253 EXIT(1); 1254 } 1255 if (!(flags & CTLFLAG_IMMEDIATE)) 1256 flags |= CTLFLAG_OWNDATA; 1257 } 1258 1259 /* 1260 * now we do a few sanity checks on the description we've 1261 * assembled 1262 */ 1263 if ((flags & CTLFLAG_IMMEDIATE) && 1264 (type == CTLTYPE_STRING || type == CTLTYPE_STRUCT)) { 1265 sysctlperror("%s: cannot make an immediate %s\n", 1266 nname, 1267 (type == CTLTYPE_STRING) ? "string" : "struct"); 1268 EXIT(1); 1269 } 1270 if (type == CTLTYPE_NODE && node.sysctl_data != NULL) { 1271 sysctlperror("%s: nodes do not have data\n", nname); 1272 EXIT(1); 1273 } 1274 1275 /* 1276 * some types must have a particular size 1277 */ 1278 if (sz != 0) { 1279 if ((type == CTLTYPE_INT && sz != sizeof(int)) || 1280 (type == CTLTYPE_QUAD && sz != sizeof(u_quad_t)) || 1281 (type == CTLTYPE_NODE && sz != 0)) { 1282 sysctlperror("%s: wrong size for type\n", nname); 1283 EXIT(1); 1284 } 1285 } 1286 else if (type == CTLTYPE_STRUCT) { 1287 sysctlperror("%s: struct must have size\n", nname); 1288 EXIT(1); 1289 } 1290 1291 /* 1292 * now...if no one said anything yet, we default nodes or 1293 * any type that owns data being writeable, and everything 1294 * else being readonly. 1295 */ 1296 if (rw == -1) { 1297 if (type == CTLTYPE_NODE || 1298 (flags & (CTLFLAG_OWNDATA|CTLFLAG_IMMEDIATE))) 1299 rw = CTLFLAG_READWRITE; 1300 else 1301 rw = CTLFLAG_READONLY; 1302 } 1303 1304 /* 1305 * if a kernel address was specified, that can't be made 1306 * writeable by us. 1307 if (rw != CTLFLAG_READONLY && addr) { 1308 sysctlperror("%s: kernel data can only be readable\n", nname); 1309 EXIT(1); 1310 } 1311 */ 1312 1313 /* 1314 * what separator were they using in the full name of the new 1315 * node? 1316 */ 1317 if ((t = strpbrk(nname, "./")) == NULL) 1318 sep[0] = '.'; 1319 else 1320 sep[0] = t[0]; 1321 sep[1] = '\0'; 1322 1323 /* 1324 * put it all together, now. t'ain't much, is it? 1325 */ 1326 node.sysctl_flags = SYSCTL_VERSION|flags|rw|type; 1327 node.sysctl_size = sz; 1328 t = strrchr(nname, sep[0]); 1329 if (t != NULL) 1330 strlcpy(node.sysctl_name, t + 1, sizeof(node.sysctl_name)); 1331 else 1332 strlcpy(node.sysctl_name, nname, sizeof(node.sysctl_name)); 1333 if (t == nname) 1334 t = NULL; 1335 1336 /* 1337 * if this is a new top-level node, then we don't need to find 1338 * the mib for its parent 1339 */ 1340 if (t == NULL) { 1341 namelen = 0; 1342 gsname[0] = '\0'; 1343 } 1344 1345 /* 1346 * on the other hand, if it's not a top-level node... 1347 */ 1348 else { 1349 namelen = sizeof(name) / sizeof(name[0]); 1350 sz = sizeof(gsname); 1351 *t = '\0'; 1352 rc = sysctlgetmibinfo(nname, &name[0], &namelen, 1353 gsname, &sz, NULL, SYSCTL_VERSION); 1354 *t = sep[0]; 1355 if (rc == -1) { 1356 sysctlparseerror(namelen, nname); 1357 EXIT(1); 1358 } 1359 } 1360 1361 /* 1362 * yes, a new node is being created 1363 */ 1364 if (method != 0) 1365 name[namelen++] = method; 1366 else 1367 name[namelen++] = CTL_CREATE; 1368 1369 sz = sizeof(node); 1370 rc = sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node)); 1371 1372 if (rc == -1) { 1373 sysctlperror("%s: CTL_CREATE failed: %s\n", 1374 nname, strerror(errno)); 1375 EXIT(1); 1376 } 1377 else { 1378 if (!qflag && !nflag) 1379 printf("%s(%s): (created)\n", nname, st(type)); 1380 stale = 1; 1381 } 1382 } 1383 1384 static void 1385 parse_destroy(char *l) 1386 { 1387 struct sysctlnode node; 1388 size_t sz; 1389 int name[CTL_MAXNAME], rc; 1390 u_int namelen; 1391 1392 if (!wflag) { 1393 sysctlperror("Must specify -w to destroy nodes\n"); 1394 exit(1); 1395 } 1396 1397 memset(name, 0, sizeof(name)); 1398 namelen = sizeof(name) / sizeof(name[0]); 1399 sz = sizeof(gsname); 1400 rc = sysctlgetmibinfo(l, &name[0], &namelen, gsname, &sz, NULL, 1401 SYSCTL_VERSION); 1402 if (rc == -1) { 1403 sysctlparseerror(namelen, l); 1404 EXIT(1); 1405 } 1406 1407 memset(&node, 0, sizeof(node)); 1408 node.sysctl_flags = SYSCTL_VERSION; 1409 node.sysctl_num = name[namelen - 1]; 1410 name[namelen - 1] = CTL_DESTROY; 1411 1412 sz = sizeof(node); 1413 rc = sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node)); 1414 1415 if (rc == -1) { 1416 sysctlperror("%s: CTL_DESTROY failed: %s\n", 1417 l, strerror(errno)); 1418 EXIT(1); 1419 } 1420 else { 1421 if (!qflag && !nflag) 1422 printf("%s(%s): (destroyed)\n", gsname, 1423 st(SYSCTL_TYPE(node.sysctl_flags))); 1424 stale = 1; 1425 } 1426 } 1427 1428 static void 1429 parse_describe(char *l) 1430 { 1431 struct sysctlnode newdesc; 1432 char buf[1024], *value; 1433 struct sysctldesc *d = (void*)&buf[0]; 1434 int name[CTL_MAXNAME], rc; 1435 u_int namelen; 1436 size_t sz; 1437 1438 if (!wflag) { 1439 sysctlperror("Must specify -w to set descriptions\n"); 1440 exit(1); 1441 } 1442 1443 value = strchr(l, '='); 1444 *value++ = '\0'; 1445 1446 memset(name, 0, sizeof(name)); 1447 namelen = sizeof(name) / sizeof(name[0]); 1448 sz = sizeof(gsname); 1449 rc = sysctlgetmibinfo(l, &name[0], &namelen, gsname, &sz, NULL, 1450 SYSCTL_VERSION); 1451 if (rc == -1) { 1452 sysctlparseerror(namelen, l); 1453 EXIT(1); 1454 } 1455 1456 sz = sizeof(buf); 1457 memset(&newdesc, 0, sizeof(newdesc)); 1458 newdesc.sysctl_flags = SYSCTL_VERSION|CTLFLAG_OWNDESC; 1459 newdesc.sysctl_num = name[namelen - 1]; 1460 newdesc.sysctl_desc = value; 1461 name[namelen - 1] = CTL_DESCRIBE; 1462 rc = sysctl(name, namelen, d, &sz, &newdesc, sizeof(newdesc)); 1463 if (rc == -1) 1464 sysctlperror("%s: CTL_DESCRIBE failed: %s\n", 1465 gsname, strerror(errno)); 1466 else if (d->descr_len == 1) 1467 sysctlperror("%s: description not set\n", gsname); 1468 else if (!qflag && !nflag) 1469 printf("%s: %s\n", gsname, d->descr_str); 1470 } 1471 1472 /* 1473 * ******************************************************************** 1474 * when things go wrong... 1475 * ******************************************************************** 1476 */ 1477 static void 1478 usage(void) 1479 { 1480 const char *progname = getprogname(); 1481 1482 (void)fprintf(stderr, 1483 "usage:\t%s %s\n" 1484 "\t%s %s\n" 1485 "\t%s %s\n" 1486 "\t%s %s\n" 1487 "\t%s %s\n" 1488 "\t%s %s\n", 1489 progname, "[-dne] [-x[x]|-r] variable ...", 1490 progname, "[-ne] [-q] -w variable=value ...", 1491 progname, "[-dne] -a", 1492 progname, "[-dne] -A", 1493 progname, "[-ne] -M", 1494 progname, "[-dne] [-q] -f file"); 1495 exit(1); 1496 } 1497 1498 static void 1499 getdesc1(int *name, u_int namelen, struct sysctlnode *pnode) 1500 { 1501 struct sysctlnode node; 1502 char buf[1024], *desc; 1503 struct sysctldesc *d = (void*)buf; 1504 size_t sz = sizeof(buf); 1505 int rc; 1506 1507 memset(&node, 0, sizeof(node)); 1508 node.sysctl_flags = SYSCTL_VERSION; 1509 node.sysctl_num = name[namelen - 1]; 1510 name[namelen - 1] = CTL_DESCRIBE; 1511 rc = sysctl(name, namelen, d, &sz, &node, sizeof(node)); 1512 1513 if (rc == -1 || 1514 d->descr_len == 1 || 1515 d->descr_num != pnode->sysctl_num || 1516 d->descr_ver != pnode->sysctl_ver) 1517 desc = (char *)-1; 1518 else 1519 desc = malloc(d->descr_len); 1520 1521 if (desc == NULL) 1522 desc = (char *)-1; 1523 if (desc != (char *)-1) 1524 memcpy(desc, &d->descr_str[0], d->descr_len); 1525 name[namelen - 1] = node.sysctl_num; 1526 if (pnode->sysctl_desc != NULL && 1527 pnode->sysctl_desc != (const char *)-1) 1528 free(__UNCONST(pnode->sysctl_desc)); 1529 pnode->sysctl_desc = desc; 1530 } 1531 1532 static void 1533 getdesc(int *name, u_int namelen, struct sysctlnode *pnode) 1534 { 1535 struct sysctlnode *node = pnode->sysctl_child; 1536 struct sysctldesc *d, *p, *plim; 1537 char *desc; 1538 size_t i, sz; 1539 int rc; 1540 1541 sz = 128 * pnode->sysctl_clen; 1542 name[namelen] = CTL_DESCRIBE; 1543 1544 /* 1545 * attempt *twice* to get the description chunk. if two tries 1546 * doesn't work, give up. 1547 */ 1548 i = 0; 1549 do { 1550 d = malloc(sz); 1551 if (d == NULL) 1552 return; 1553 rc = sysctl(name, namelen + 1, d, &sz, NULL, 0); 1554 if (rc == -1) { 1555 free(d); 1556 d = NULL; 1557 if (i == 0 && errno == ENOMEM) 1558 i = 1; 1559 else 1560 return; 1561 } 1562 } while (d == NULL); 1563 1564 /* 1565 * hokey nested loop here, giving O(n**2) behavior, but should 1566 * suffice for now 1567 */ 1568 plim = /*LINTED ptr cast*/(struct sysctldesc *)((char*)d + sz); 1569 for (i = 0; i < pnode->sysctl_clen; i++) { 1570 node = &pnode->sysctl_child[i]; 1571 for (p = d; p < plim; p = NEXT_DESCR(p)) 1572 if (node->sysctl_num == p->descr_num) 1573 break; 1574 if (p < plim && node->sysctl_ver == p->descr_ver) { 1575 /* 1576 * match found, attempt to attach description 1577 */ 1578 if (p->descr_len == 1) 1579 desc = NULL; 1580 else 1581 desc = malloc(p->descr_len); 1582 if (desc == NULL) 1583 desc = (char *)-1; 1584 else 1585 memcpy(desc, &p->descr_str[0], p->descr_len); 1586 node->sysctl_desc = desc; 1587 } 1588 } 1589 1590 free(d); 1591 } 1592 1593 static void 1594 trim_whitespace(char *s, int dir) 1595 { 1596 char *i, *o; 1597 1598 i = o = s; 1599 if (dir & 1) 1600 while (isspace((unsigned char)*i)) 1601 i++; 1602 while ((*o++ = *i++) != '\0'); 1603 o -= 2; /* already past nul, skip back to before it */ 1604 if (dir & 2) 1605 while (o > s && isspace((unsigned char)*o)) 1606 *o-- = '\0'; 1607 } 1608 1609 void 1610 sysctlerror(int soft) 1611 { 1612 if (soft) { 1613 switch (errno) { 1614 case ENOENT: 1615 case ENOPROTOOPT: 1616 case ENOTDIR: 1617 case EINVAL: 1618 case EOPNOTSUPP: 1619 case EPROTONOSUPPORT: 1620 if (Aflag || req) 1621 sysctlperror("%s: the value is not available\n", 1622 gsname); 1623 return; 1624 } 1625 } 1626 1627 sysctlperror("%s: sysctl() failed with %s\n", 1628 gsname, strerror(errno)); 1629 if (!soft) 1630 EXIT(1); 1631 } 1632 1633 void 1634 sysctlparseerror(u_int namelen, const char *pname) 1635 { 1636 1637 sysctlperror("%s level name '%s' in '%s' is invalid\n", 1638 lname[namelen], gsname, pname); 1639 } 1640 1641 static void 1642 sysctlperror(const char *fmt, ...) 1643 { 1644 va_list ap; 1645 1646 (void)fprintf(warnfp, "%s: ", getprogname()); 1647 if (fn) 1648 (void)fprintf(warnfp, "%s#%zu: ", fn, nr); 1649 va_start(ap, fmt); 1650 (void)vfprintf(warnfp, fmt, ap); 1651 va_end(ap); 1652 errs++; 1653 } 1654 1655 1656 /* 1657 * ******************************************************************** 1658 * how to write to a "simple" node 1659 * ******************************************************************** 1660 */ 1661 static void 1662 write_number(int *name, u_int namelen, struct sysctlnode *node, char *value) 1663 { 1664 u_int ii, io; 1665 u_quad_t qi, qo; 1666 size_t si, so; 1667 int rc; 1668 void *i, *o; 1669 char *t; 1670 1671 if (fn) 1672 trim_whitespace(value, 3); 1673 1674 si = so = 0; 1675 i = o = NULL; 1676 errno = 0; 1677 qi = strtouq(value, &t, 0); 1678 if (qi == UQUAD_MAX && errno == ERANGE) { 1679 sysctlperror("%s: %s\n", value, strerror(errno)); 1680 EXIT(1); 1681 } 1682 if (t == value || *t != '\0') { 1683 sysctlperror("%s: not a number\n", value); 1684 EXIT(1); 1685 } 1686 1687 switch (SYSCTL_TYPE(node->sysctl_flags)) { 1688 case CTLTYPE_INT: 1689 ii = (u_int)qi; 1690 io = (u_int)(qi >> 32); 1691 if (io != (u_int)-1 && io != 0) { 1692 sysctlperror("%s: %s\n", value, strerror(ERANGE)); 1693 EXIT(1); 1694 } 1695 o = &io; 1696 so = sizeof(io); 1697 i = ⅈ 1698 si = sizeof(ii); 1699 break; 1700 case CTLTYPE_QUAD: 1701 o = &qo; 1702 so = sizeof(qo); 1703 i = &qi; 1704 si = sizeof(qi); 1705 break; 1706 } 1707 1708 rc = sysctl(name, namelen, o, &so, i, si); 1709 if (rc == -1) { 1710 sysctlerror(0); 1711 return; 1712 } 1713 1714 switch (SYSCTL_TYPE(node->sysctl_flags)) { 1715 case CTLTYPE_INT: 1716 display_number(node, gsname, &io, sizeof(io), DISPLAY_OLD); 1717 display_number(node, gsname, &ii, sizeof(ii), DISPLAY_NEW); 1718 break; 1719 case CTLTYPE_QUAD: 1720 display_number(node, gsname, &qo, sizeof(qo), DISPLAY_OLD); 1721 display_number(node, gsname, &qi, sizeof(qi), DISPLAY_NEW); 1722 break; 1723 } 1724 } 1725 1726 static void 1727 write_string(int *name, u_int namelen, struct sysctlnode *node, char *value) 1728 { 1729 char *i, *o; 1730 size_t si, so; 1731 int rc; 1732 1733 i = value; 1734 si = strlen(i) + 1; 1735 so = node->sysctl_size; 1736 if (si > so && so != 0) { 1737 sysctlperror("%s: string too long\n", value); 1738 EXIT(1); 1739 } 1740 o = malloc(so); 1741 if (o == NULL) { 1742 sysctlperror("%s: !malloc failed!\n", gsname); 1743 exit(1); 1744 } 1745 1746 rc = sysctl(name, namelen, o, &so, i, si); 1747 if (rc == -1) { 1748 sysctlerror(0); 1749 return; 1750 } 1751 1752 display_string(node, gsname, o, so, DISPLAY_OLD); 1753 display_string(node, gsname, i, si, DISPLAY_NEW); 1754 free(o); 1755 } 1756 1757 /* 1758 * ******************************************************************** 1759 * simple ways to print stuff consistently 1760 * ******************************************************************** 1761 */ 1762 static void 1763 display_number(const struct sysctlnode *node, const char *name, 1764 const void *data, size_t sz, int n) 1765 { 1766 u_quad_t q; 1767 int i; 1768 1769 if (qflag) 1770 return; 1771 if ((nflag || rflag) && (n == DISPLAY_OLD)) 1772 return; 1773 1774 if (rflag && n != DISPLAY_OLD) { 1775 fwrite(data, sz, 1, stdout); 1776 return; 1777 } 1778 1779 if (!nflag) { 1780 if (n == DISPLAY_VALUE) 1781 printf("%s%s", name, eq); 1782 else if (n == DISPLAY_OLD) 1783 printf("%s: ", name); 1784 } 1785 1786 if (xflag > 1) { 1787 if (n != DISPLAY_NEW) 1788 printf("\n"); 1789 hex_dump(data, sz); 1790 return; 1791 } 1792 1793 switch (SYSCTL_TYPE(node->sysctl_flags)) { 1794 case CTLTYPE_INT: 1795 memcpy(&i, data, sz); 1796 if (xflag) 1797 printf("0x%0*x", (int)sz * 2, i); 1798 else if (node->sysctl_flags & CTLFLAG_HEX) 1799 printf("%#x", i); 1800 else 1801 printf("%d", i); 1802 break; 1803 case CTLTYPE_QUAD: 1804 memcpy(&q, data, sz); 1805 if (xflag) 1806 printf("0x%0*" PRIx64, (int)sz * 2, q); 1807 else if (node->sysctl_flags & CTLFLAG_HEX) 1808 printf("%#" PRIx64, q); 1809 else 1810 printf("%" PRIu64, q); 1811 break; 1812 } 1813 1814 if (n == DISPLAY_OLD) 1815 printf(" -> "); 1816 else 1817 printf("\n"); 1818 } 1819 1820 static void 1821 display_string(const struct sysctlnode *node, const char *name, 1822 const void *data, size_t sz, int n) 1823 { 1824 const unsigned char *buf = data; 1825 int ni; 1826 1827 if (qflag) 1828 return; 1829 if ((nflag || rflag) && (n == DISPLAY_OLD)) 1830 return; 1831 1832 if (rflag && n != DISPLAY_OLD) { 1833 fwrite(data, sz, 1, stdout); 1834 return; 1835 } 1836 1837 if (!nflag) { 1838 if (n == DISPLAY_VALUE) 1839 printf("%s%s", name, eq); 1840 else if (n == DISPLAY_OLD) 1841 printf("%s: ", name); 1842 } 1843 1844 if (xflag > 1) { 1845 if (n != DISPLAY_NEW) 1846 printf("\n"); 1847 hex_dump(data, sz); 1848 return; 1849 } 1850 1851 if (xflag || node->sysctl_flags & CTLFLAG_HEX) { 1852 for (ni = 0; ni < (int)sz; ni++) { 1853 if (xflag) 1854 printf("%02x", buf[ni]); 1855 if (buf[ni] == '\0') 1856 break; 1857 if (!xflag) 1858 printf("\\x%2.2x", buf[ni]); 1859 } 1860 } 1861 else 1862 printf("%.*s", (int)sz, buf); 1863 1864 if (n == DISPLAY_OLD) 1865 printf(" -> "); 1866 else 1867 printf("\n"); 1868 } 1869 1870 /*ARGSUSED*/ 1871 static void 1872 display_struct(const struct sysctlnode *node, const char *name, 1873 const void *data, size_t sz, int n) 1874 { 1875 const unsigned char *buf = data; 1876 int ni; 1877 size_t more; 1878 1879 if (qflag) 1880 return; 1881 if (!(xflag || rflag)) { 1882 if (Aflag || req) 1883 sysctlperror( 1884 "%s: this type is unknown to this program\n", 1885 gsname); 1886 return; 1887 } 1888 if ((nflag || rflag) && (n == DISPLAY_OLD)) 1889 return; 1890 1891 if (rflag && n != DISPLAY_OLD) { 1892 fwrite(data, sz, 1, stdout); 1893 return; 1894 } 1895 1896 if (!nflag) { 1897 if (n == DISPLAY_VALUE) 1898 printf("%s%s", name, eq); 1899 else if (n == DISPLAY_OLD) 1900 printf("%s: ", name); 1901 } 1902 1903 if (xflag > 1) { 1904 if (n != DISPLAY_NEW) 1905 printf("\n"); 1906 hex_dump(data, sz); 1907 return; 1908 } 1909 1910 if (sz > 16) { 1911 more = sz - 16; 1912 sz = 16; 1913 } 1914 else 1915 more = 0; 1916 for (ni = 0; ni < (int)sz; ni++) 1917 printf("%02x", buf[ni]); 1918 if (more) 1919 printf("...(%zu more bytes)", more); 1920 printf("\n"); 1921 } 1922 1923 static void 1924 hex_dump(const unsigned char *buf, size_t len) 1925 { 1926 unsigned int i; 1927 int j; 1928 char line[80], tmp[12]; 1929 1930 memset(line, ' ', sizeof(line)); 1931 for (i = 0, j = 15; i < len; i++) { 1932 j = i % 16; 1933 /* reset line */ 1934 if (j == 0) { 1935 line[58] = '|'; 1936 line[77] = '|'; 1937 line[78] = 0; 1938 snprintf(tmp, sizeof(tmp), "%07x", i); 1939 memcpy(&line[0], tmp, 7); 1940 } 1941 /* copy out hex version of byte */ 1942 snprintf(tmp, sizeof(tmp), "%02x", buf[i]); 1943 memcpy(&line[9 + j * 3], tmp, 2); 1944 /* copy out plain version of byte */ 1945 line[60 + j] = (isprint(buf[i])) ? buf[i] : '.'; 1946 /* print a full line and erase it */ 1947 if (j == 15) { 1948 printf("%s\n", line); 1949 memset(line, ' ', sizeof(line)); 1950 } 1951 } 1952 if (line[0] != ' ') 1953 printf("%s\n", line); 1954 printf("%07zu bytes\n", len); 1955 } 1956 1957 /* 1958 * ******************************************************************** 1959 * functions that handle particular nodes 1960 * ******************************************************************** 1961 */ 1962 /*ARGSUSED*/ 1963 static void 1964 printother(HANDLER_ARGS) 1965 { 1966 int rc; 1967 void *p; 1968 size_t sz1, sz2; 1969 1970 if (!(Aflag || req) || Mflag) 1971 return; 1972 1973 /* 1974 * okay...you asked for it, so let's give it a go 1975 */ 1976 while (type != CTLTYPE_NODE && (xflag || rflag)) { 1977 rc = sysctl(name, namelen, NULL, &sz1, NULL, 0); 1978 if (rc == -1 || sz1 == 0) 1979 break; 1980 p = malloc(sz1); 1981 if (p == NULL) 1982 break; 1983 sz2 = sz1; 1984 rc = sysctl(name, namelen, p, &sz2, NULL, 0); 1985 if (rc == -1 || sz1 != sz2) { 1986 free(p); 1987 break; 1988 } 1989 display_struct(pnode, gsname, p, sz1, DISPLAY_VALUE); 1990 free(p); 1991 return; 1992 } 1993 1994 /* 1995 * that didn't work...do we have a specific message for this 1996 * thing? 1997 */ 1998 if (v != NULL) { 1999 sysctlperror("%s: use '%s' to view this information\n", 2000 gsname, (const char *)v); 2001 return; 2002 } 2003 2004 /* 2005 * hmm...i wonder if we have any generic hints? 2006 */ 2007 switch (name[0]) { 2008 case CTL_NET: 2009 sysctlperror("%s: use 'netstat' to view this information\n", 2010 sname); 2011 break; 2012 case CTL_DEBUG: 2013 sysctlperror("%s: missing 'options DEBUG' from kernel?\n", 2014 sname); 2015 break; 2016 case CTL_DDB: 2017 sysctlperror("%s: missing 'options DDB' from kernel?\n", 2018 sname); 2019 break; 2020 case CTL_VENDOR: 2021 sysctlperror("%s: no vendor extensions installed\n", 2022 sname); 2023 break; 2024 } 2025 } 2026 2027 /*ARGSUSED*/ 2028 static void 2029 kern_clockrate(HANDLER_ARGS) 2030 { 2031 struct clockinfo clkinfo; 2032 size_t sz; 2033 int rc; 2034 2035 sz = sizeof(clkinfo); 2036 rc = sysctl(name, namelen, &clkinfo, &sz, NULL, 0); 2037 if (rc == -1) { 2038 sysctlerror(1); 2039 return; 2040 } 2041 if (sz != sizeof(clkinfo)) 2042 errx(1, "%s: !returned size wrong!", sname); 2043 2044 if (xflag || rflag) { 2045 display_struct(pnode, sname, &clkinfo, sz, 2046 DISPLAY_VALUE); 2047 return; 2048 } 2049 else if (!nflag) 2050 printf("%s: ", sname); 2051 printf("tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n", 2052 clkinfo.tick, clkinfo.tickadj, 2053 clkinfo.hz, clkinfo.profhz, clkinfo.stathz); 2054 } 2055 2056 /*ARGSUSED*/ 2057 static void 2058 kern_boottime(HANDLER_ARGS) 2059 { 2060 struct timeval timeval; 2061 time_t boottime; 2062 size_t sz; 2063 int rc; 2064 2065 sz = sizeof(timeval); 2066 rc = sysctl(name, namelen, &timeval, &sz, NULL, 0); 2067 if (rc == -1) { 2068 sysctlerror(1); 2069 return; 2070 } 2071 if (sz != sizeof(timeval)) 2072 errx(1, "%s: !returned size wrong!", sname); 2073 2074 boottime = timeval.tv_sec; 2075 if (xflag || rflag) 2076 display_struct(pnode, sname, &timeval, sz, 2077 DISPLAY_VALUE); 2078 else if (!nflag) 2079 /* ctime() provides the \n */ 2080 printf("%s%s%s", sname, eq, ctime(&boottime)); 2081 else if (nflag == 1) 2082 printf("%ld\n", (long)boottime); 2083 else 2084 printf("%ld.%06ld\n", (long)timeval.tv_sec, 2085 (long)timeval.tv_usec); 2086 } 2087 2088 /*ARGSUSED*/ 2089 static void 2090 kern_consdev(HANDLER_ARGS) 2091 { 2092 dev_t cons; 2093 size_t sz; 2094 int rc; 2095 2096 sz = sizeof(cons); 2097 rc = sysctl(name, namelen, &cons, &sz, NULL, 0); 2098 if (rc == -1) { 2099 sysctlerror(1); 2100 return; 2101 } 2102 if (sz != sizeof(cons)) 2103 errx(1, "%s: !returned size wrong!", sname); 2104 2105 if (xflag || rflag) 2106 display_struct(pnode, sname, &cons, sz, 2107 DISPLAY_VALUE); 2108 else { 2109 if (!nflag) 2110 printf("%s%s", sname, eq); 2111 if (nflag < 2 && (sname = devname(cons, S_IFCHR)) != NULL) 2112 printf("%s\n", sname); 2113 else 2114 printf("0x%llx\n", (unsigned long long)cons); 2115 } 2116 } 2117 2118 /*ARGSUSED*/ 2119 static void 2120 kern_cp_time(HANDLER_ARGS) 2121 { 2122 u_int64_t *cp_time; 2123 size_t sz, osz; 2124 int rc, i, n; 2125 char s[sizeof("kern.cp_time.nnnnnn")]; 2126 const char *tname; 2127 2128 /* 2129 * three things to do here. 2130 * case 1: get sum (no Aflag and namelen == 2) 2131 * case 2: get specific processor (namelen == 3) 2132 * case 3: get all processors (Aflag and namelen == 2) 2133 */ 2134 2135 if (namelen == 2 && Aflag) { 2136 sz = sizeof(n); 2137 rc = sysctlbyname("hw.ncpu", &n, &sz, NULL, 0); 2138 if (rc != 0) 2139 return; /* XXX print an error, eh? */ 2140 n++; /* Add on space for the sum. */ 2141 sz = n * sizeof(u_int64_t) * CPUSTATES; 2142 } 2143 else { 2144 n = -1; /* Just print one data set. */ 2145 sz = sizeof(u_int64_t) * CPUSTATES; 2146 } 2147 2148 cp_time = malloc(sz); 2149 if (cp_time == NULL) { 2150 sysctlerror(1); 2151 return; 2152 } 2153 2154 osz = sz; 2155 rc = sysctl(name, namelen, cp_time + (n != -1) * CPUSTATES, &osz, 2156 NULL, 0); 2157 2158 if (rc == -1) { 2159 sysctlerror(1); 2160 free(cp_time); 2161 return; 2162 } 2163 2164 /* 2165 * Check, but account for space we'll occupy with the sum. 2166 */ 2167 if (osz != sz - (n != -1) * CPUSTATES * sizeof(u_int64_t)) 2168 errx(1, "%s: !returned size wrong!", sname); 2169 2170 /* 2171 * Compute the actual sum. Two calls would be easier (we 2172 * could just call ourselves recursively above), but the 2173 * numbers wouldn't add up. 2174 */ 2175 if (n != -1) { 2176 memset(cp_time, 0, sizeof(u_int64_t) * CPUSTATES); 2177 for (i = 1; i < n; i++) { 2178 cp_time[CP_USER] += cp_time[i * CPUSTATES + CP_USER]; 2179 cp_time[CP_NICE] += cp_time[i * CPUSTATES + CP_NICE]; 2180 cp_time[CP_SYS] += cp_time[i * CPUSTATES + CP_SYS]; 2181 cp_time[CP_INTR] += cp_time[i * CPUSTATES + CP_INTR]; 2182 cp_time[CP_IDLE] += cp_time[i * CPUSTATES + CP_IDLE]; 2183 } 2184 } 2185 2186 tname = sname; 2187 for (i = 0; n == -1 || i < n; i++) { 2188 if (i > 0) { 2189 (void)snprintf(s, sizeof(s), "%s%s%d", sname, sep, 2190 i - 1); 2191 tname = s; 2192 } 2193 if (xflag || rflag) 2194 display_struct(pnode, tname, cp_time + (i * CPUSTATES), 2195 sizeof(u_int64_t) * CPUSTATES, 2196 DISPLAY_VALUE); 2197 else { 2198 if (!nflag) 2199 printf("%s: ", tname); 2200 printf("user = %" PRIu64 2201 ", nice = %" PRIu64 2202 ", sys = %" PRIu64 2203 ", intr = %" PRIu64 2204 ", idle = %" PRIu64 2205 "\n", 2206 cp_time[i * CPUSTATES + CP_USER], 2207 cp_time[i * CPUSTATES + CP_NICE], 2208 cp_time[i * CPUSTATES + CP_SYS], 2209 cp_time[i * CPUSTATES + CP_INTR], 2210 cp_time[i * CPUSTATES + CP_IDLE]); 2211 } 2212 /* 2213 * Just printing the one node. 2214 */ 2215 if (n == -1) 2216 break; 2217 } 2218 2219 free(cp_time); 2220 } 2221 2222 /*ARGSUSED*/ 2223 static void 2224 kern_drivers(HANDLER_ARGS) 2225 { 2226 struct kinfo_drivers *kd; 2227 size_t sz, i; 2228 int rc; 2229 const char *comma; 2230 2231 rc = sysctl(name, namelen, NULL, &sz, NULL, 0); 2232 if (rc == -1) { 2233 sysctlerror(1); 2234 return; 2235 } 2236 2237 if (sz % sizeof(*kd)) 2238 err(1, "bad size %zu for kern.drivers", sz); 2239 2240 kd = malloc(sz); 2241 if (kd == NULL) { 2242 sysctlerror(1); 2243 return; 2244 } 2245 2246 rc = sysctl(name, namelen, kd, &sz, NULL, 0); 2247 if (rc == -1) { 2248 sysctlerror(1); 2249 return; 2250 } 2251 2252 comma = ""; 2253 if (!nflag) 2254 printf("%s%s", sname, eq); 2255 for (i = 0, sz /= sizeof(*kd); i < sz; i++) { 2256 (void)printf("%s[%d %d %s]", comma, kd[i].d_cmajor, 2257 kd[i].d_bmajor, kd[i].d_name); 2258 comma = ", "; 2259 } 2260 (void)printf("\n"); 2261 free(kd); 2262 } 2263 2264 /*ARGSUSED*/ 2265 static void 2266 kern_cp_id(HANDLER_ARGS) 2267 { 2268 u_int64_t *cp_id; 2269 size_t sz, osz; 2270 int rc, i, n; 2271 char s[sizeof("kern.cp_id.nnnnnn")]; 2272 const char *tname; 2273 struct sysctlnode node = *pnode; 2274 2275 /* 2276 * three things to do here. 2277 * case 1: print a specific cpu id (namelen == 3) 2278 * case 2: print all cpu ids separately (Aflag set) 2279 * case 3: print all cpu ids on one line 2280 */ 2281 2282 if (namelen == 2) { 2283 sz = sizeof(n); 2284 rc = sysctlbyname("hw.ncpu", &n, &sz, NULL, 0); 2285 if (rc != 0) 2286 return; /* XXX print an error, eh? */ 2287 sz = n * sizeof(u_int64_t); 2288 } 2289 else { 2290 n = -1; /* Just print one cpu id. */ 2291 sz = sizeof(u_int64_t); 2292 } 2293 2294 cp_id = malloc(sz); 2295 if (cp_id == NULL) { 2296 sysctlerror(1); 2297 return; 2298 } 2299 2300 osz = sz; 2301 rc = sysctl(name, namelen, cp_id, &osz, NULL, 0); 2302 if (rc == -1) { 2303 sysctlerror(1); 2304 free(cp_id); 2305 return; 2306 } 2307 2308 /* 2309 * Check that we got back what we asked for. 2310 */ 2311 if (osz != sz) 2312 errx(1, "%s: !returned size wrong!", sname); 2313 2314 /* pretend for output purposes */ 2315 node.sysctl_flags = SYSCTL_FLAGS(pnode->sysctl_flags) | 2316 SYSCTL_TYPE(CTLTYPE_QUAD); 2317 2318 tname = sname; 2319 if (namelen == 3) 2320 display_number(&node, tname, cp_id, 2321 sizeof(u_int64_t), 2322 DISPLAY_VALUE); 2323 else if (Aflag) { 2324 for (i = 0; i < n; i++) 2325 (void)snprintf(s, sizeof(s), "%s%s%d", sname, sep, i); 2326 tname = s; 2327 display_number(&node, tname, &cp_id[i], 2328 sizeof(u_int64_t), 2329 DISPLAY_VALUE); 2330 } 2331 else { 2332 if (xflag || rflag) 2333 display_struct(pnode, tname, cp_id, sz, DISPLAY_VALUE); 2334 else { 2335 if (!nflag) 2336 printf("%s: ", tname); 2337 for (i = 0; i < n; i++) { 2338 if (i) 2339 printf(", "); 2340 printf("%d = %" PRIu64, i, cp_id[i]); 2341 } 2342 printf("\n"); 2343 } 2344 } 2345 2346 free(cp_id); 2347 } 2348 2349 /*ARGSUSED*/ 2350 static void 2351 vm_loadavg(HANDLER_ARGS) 2352 { 2353 struct loadavg loadavg; 2354 size_t sz; 2355 int rc; 2356 2357 sz = sizeof(loadavg); 2358 rc = sysctl(name, namelen, &loadavg, &sz, NULL, 0); 2359 if (rc == -1) { 2360 sysctlerror(1); 2361 return; 2362 } 2363 if (sz != sizeof(loadavg)) 2364 errx(1, "%s: !returned size wrong!", sname); 2365 2366 if (xflag || rflag) { 2367 display_struct(pnode, sname, &loadavg, sz, 2368 DISPLAY_VALUE); 2369 return; 2370 } 2371 if (!nflag) 2372 printf("%s: ", sname); 2373 printf("%.2f %.2f %.2f\n", 2374 (double) loadavg.ldavg[0] / loadavg.fscale, 2375 (double) loadavg.ldavg[1] / loadavg.fscale, 2376 (double) loadavg.ldavg[2] / loadavg.fscale); 2377 } 2378 2379 /*ARGSUSED*/ 2380 static void 2381 proc_limit(HANDLER_ARGS) 2382 { 2383 u_quad_t olim, *newp, nlim; 2384 size_t osz, nsz; 2385 char *t; 2386 int rc; 2387 2388 if (fn) 2389 trim_whitespace(value, 3); 2390 2391 osz = sizeof(olim); 2392 if (value != NULL) { 2393 nsz = sizeof(nlim); 2394 newp = &nlim; 2395 if (strcmp(value, "unlimited") == 0) 2396 nlim = RLIM_INFINITY; 2397 else { 2398 errno = 0; 2399 nlim = strtouq(value, &t, 0); 2400 if (t == value || *t != '\0' || errno != 0) { 2401 sysctlperror("%s: '%s' is not a valid limit\n", 2402 sname, value); 2403 EXIT(1); 2404 } 2405 } 2406 } 2407 else { 2408 nsz = 0; 2409 newp = NULL; 2410 } 2411 2412 rc = sysctl(name, namelen, &olim, &osz, newp, nsz); 2413 if (rc == -1) { 2414 sysctlerror(newp == NULL); 2415 return; 2416 } 2417 2418 if (newp && qflag) 2419 return; 2420 2421 if (rflag || xflag || olim != RLIM_INFINITY) 2422 display_number(pnode, sname, &olim, sizeof(olim), 2423 newp ? DISPLAY_OLD : DISPLAY_VALUE); 2424 else 2425 display_string(pnode, sname, "unlimited", 10, 2426 newp ? DISPLAY_OLD : DISPLAY_VALUE); 2427 2428 if (newp) { 2429 if (rflag || xflag || nlim != RLIM_INFINITY) 2430 display_number(pnode, sname, &nlim, sizeof(nlim), 2431 DISPLAY_NEW); 2432 else 2433 display_string(pnode, sname, "unlimited", 10, 2434 DISPLAY_NEW); 2435 } 2436 } 2437 2438 #ifdef CPU_DISKINFO 2439 /*ARGSUSED*/ 2440 static void 2441 machdep_diskinfo(HANDLER_ARGS) 2442 { 2443 struct disklist *dl; 2444 struct biosdisk_info *bi; 2445 struct nativedisk_info *ni; 2446 int rc; 2447 size_t sz; 2448 uint i, b, lim; 2449 2450 rc = sysctl(name, namelen, NULL, &sz, NULL, 0); 2451 if (rc == -1) { 2452 sysctlerror(1); 2453 return; 2454 } 2455 dl = malloc(sz); 2456 if (dl == NULL) { 2457 sysctlerror(1); 2458 return; 2459 } 2460 rc = sysctl(name, namelen, dl, &sz, NULL, 0); 2461 if (rc == -1) { 2462 sysctlerror(1); 2463 return; 2464 } 2465 2466 if (!nflag) 2467 printf("%s: ", sname); 2468 lim = dl->dl_nbiosdisks; 2469 if (lim > MAX_BIOSDISKS) 2470 lim = MAX_BIOSDISKS; 2471 for (bi = dl->dl_biosdisks, i = 0; i < lim; bi++, i++) 2472 printf("%x:%" PRIu64 "(%d/%d/%d),%x ", 2473 bi->bi_dev, bi->bi_lbasecs, 2474 bi->bi_cyl, bi->bi_head, bi->bi_sec, 2475 bi->bi_flags); 2476 lim = dl->dl_nnativedisks; 2477 ni = dl->dl_nativedisks; 2478 bi = dl->dl_biosdisks; 2479 /* LINTED -- pointer casts are tedious */ 2480 if ((char *)&ni[lim] != (char *)dl + sz) { 2481 sysctlperror("%s: size mismatch\n", gsname); 2482 return; 2483 } 2484 for (i = 0; i < lim; ni++, i++) { 2485 char t = ':'; 2486 printf(" %.*s", (int)sizeof ni->ni_devname, 2487 ni->ni_devname); 2488 for (b = 0; b < (unsigned int)ni->ni_nmatches; t = ',', b++) 2489 printf("%c%x", t, 2490 bi[ni->ni_biosmatches[b]].bi_dev); 2491 } 2492 printf("\n"); 2493 free(dl); 2494 } 2495 #endif /* CPU_DISKINFO */ 2496 2497 /*ARGSUSED*/ 2498 static void 2499 mode_bits(HANDLER_ARGS) 2500 { 2501 char buf[12], outbuf[100]; 2502 int o, m, *newp, rc; 2503 size_t osz, nsz; 2504 mode_t om, mm; 2505 2506 if (fn) 2507 trim_whitespace(value, 3); 2508 2509 newp = NULL; 2510 osz = sizeof(o); 2511 if (value != NULL) { 2512 void *foo; 2513 int tt; 2514 size_t ttsz = sizeof(tt); 2515 mode_t old_umask; 2516 2517 nsz = sizeof(m); 2518 newp = &m; 2519 errno = 0; 2520 rc = sysctl(name, namelen, &tt, &ttsz, NULL, 0); 2521 if (rc == -1) { 2522 sysctlperror("%s: failed query\n", sname); 2523 return; 2524 } 2525 2526 old_umask = umask(0); 2527 foo = setmode(value); 2528 umask(old_umask); 2529 if (foo == NULL) { 2530 sysctlperror("%s: '%s' is an invalid mode\n", sname, 2531 value); 2532 EXIT(1); 2533 } 2534 old_umask = umask(0); 2535 m = getmode(foo, (mode_t)tt); 2536 umask(old_umask); 2537 if (errno) { 2538 sysctlperror("%s: '%s' is an invalid mode\n", sname, 2539 value); 2540 EXIT(1); 2541 } 2542 } 2543 else { 2544 nsz = 0; 2545 newp = NULL; 2546 } 2547 2548 rc = sysctl(name, namelen, &o, &osz, newp, nsz); 2549 if (rc == -1) { 2550 sysctlerror(newp == NULL); 2551 return; 2552 } 2553 2554 if (newp && qflag) 2555 return; 2556 2557 om = (mode_t)o; 2558 mm = (mode_t)m; 2559 2560 if (rflag || xflag) 2561 display_number(pnode, sname, &o, sizeof(o), 2562 newp ? DISPLAY_OLD : DISPLAY_VALUE); 2563 else { 2564 memset(buf, 0, sizeof(buf)); 2565 strmode(om, buf); 2566 rc = snprintf(outbuf, sizeof(outbuf), "%04o (%s)", om, buf + 1); 2567 display_string(pnode, sname, outbuf, rc, newp ? DISPLAY_OLD : DISPLAY_VALUE); 2568 } 2569 2570 if (newp) { 2571 if (rflag || xflag) 2572 display_number(pnode, sname, &m, sizeof(m), 2573 DISPLAY_NEW); 2574 else { 2575 memset(buf, 0, sizeof(buf)); 2576 strmode(mm, buf); 2577 rc = snprintf(outbuf, sizeof(outbuf), "%04o (%s)", mm, buf + 1); 2578 display_string(pnode, sname, outbuf, rc, DISPLAY_NEW); 2579 } 2580 } 2581 } 2582