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