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