1 /* 2 * Copyright (c) 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93"; 43 #endif 44 static const char rcsid[] = 45 "$FreeBSD$"; 46 #endif /* not lint */ 47 48 #ifdef __i386__ 49 #include <sys/diskslice.h> /* used for bootdev parsing */ 50 #include <sys/reboot.h> /* used for bootdev parsing */ 51 #endif 52 #include <sys/param.h> 53 #include <sys/time.h> 54 #include <sys/resource.h> 55 #include <sys/stat.h> 56 #include <sys/sysctl.h> 57 58 #include <ctype.h> 59 #include <err.h> 60 #include <errno.h> 61 #include <stdio.h> 62 #include <stdlib.h> 63 #include <string.h> 64 #include <unistd.h> 65 66 static int aflag, bflag, dflag, eflag, Nflag, nflag, oflag, xflag; 67 68 static int oidfmt(int *, int, char *, u_int *); 69 static void parse(char *); 70 static int show_var(int *, int); 71 static int sysctl_all (int *oid, int len); 72 static int name2oid(char *, int *); 73 74 static void set_T_dev_t (char *, void **, int *); 75 76 static void 77 usage(void) 78 { 79 80 (void)fprintf(stderr, "%s\n%s\n", 81 "usage: sysctl [-bdeNnox] variable[=value] ...", 82 " sysctl [-bdeNnox] -a"); 83 exit(1); 84 } 85 86 int 87 main(int argc, char **argv) 88 { 89 int ch; 90 setbuf(stdout,0); 91 setbuf(stderr,0); 92 93 while ((ch = getopt(argc, argv, "AabdeNnowxX")) != -1) { 94 switch (ch) { 95 case 'A': 96 /* compatibility */ 97 aflag = oflag = 1; 98 break; 99 case 'a': 100 aflag = 1; 101 break; 102 case 'b': 103 bflag = 1; 104 break; 105 case 'd': 106 dflag = 1; 107 break; 108 case 'e': 109 eflag = 1; 110 break; 111 case 'N': 112 Nflag = 1; 113 break; 114 case 'n': 115 nflag = 1; 116 break; 117 case 'o': 118 oflag = 1; 119 break; 120 case 'w': 121 /* compatibility */ 122 /* ignored */ 123 break; 124 case 'X': 125 /* compatibility */ 126 aflag = xflag = 1; 127 break; 128 case 'x': 129 xflag = 1; 130 break; 131 default: 132 usage(); 133 } 134 } 135 argc -= optind; 136 argv += optind; 137 138 if (Nflag && nflag) 139 usage(); 140 if (aflag && argc == 0) 141 exit(sysctl_all(0, 0)); 142 if (argc == 0) 143 usage(); 144 while (argc-- > 0) 145 parse(*argv++); 146 exit(0); 147 } 148 149 /* 150 * Parse a name into a MIB entry. 151 * Lookup and print out the MIB entry if it exists. 152 * Set a new value if requested. 153 */ 154 static void 155 parse(char *string) 156 { 157 int len, i, j; 158 void *newval = 0; 159 int intval; 160 unsigned int uintval; 161 long longval; 162 unsigned long ulongval; 163 size_t newsize = 0; 164 quad_t quadval; 165 int mib[CTL_MAXNAME]; 166 char *cp, *bufp, buf[BUFSIZ], fmt[BUFSIZ]; 167 u_int kind; 168 169 bufp = buf; 170 snprintf(buf, BUFSIZ, "%s", string); 171 if ((cp = strchr(string, '=')) != NULL) { 172 *strchr(buf, '=') = '\0'; 173 *cp++ = '\0'; 174 while (isspace(*cp)) 175 cp++; 176 newval = cp; 177 newsize = strlen(cp); 178 } 179 len = name2oid(bufp, mib); 180 181 if (len < 0) 182 errx(1, "unknown oid '%s'", bufp); 183 184 if (oidfmt(mib, len, fmt, &kind)) 185 err(1, "couldn't find format of oid '%s'", bufp); 186 187 if (newval == NULL) { 188 if ((kind & CTLTYPE) == CTLTYPE_NODE) { 189 sysctl_all(mib, len); 190 } else { 191 i = show_var(mib, len); 192 if (!i && !bflag) 193 putchar('\n'); 194 } 195 } else { 196 if ((kind & CTLTYPE) == CTLTYPE_NODE) 197 errx(1, "oid '%s' isn't a leaf node", bufp); 198 199 if (!(kind&CTLFLAG_WR)) 200 errx(1, "oid '%s' is read only", bufp); 201 202 switch (kind & CTLTYPE) { 203 case CTLTYPE_INT: 204 intval = (int) strtol(newval, NULL, 0); 205 newval = &intval; 206 newsize = sizeof(intval); 207 break; 208 case CTLTYPE_UINT: 209 uintval = (int) strtoul(newval, NULL, 0); 210 newval = &uintval; 211 newsize = sizeof uintval; 212 break; 213 case CTLTYPE_LONG: 214 longval = strtol(newval, NULL, 0); 215 newval = &longval; 216 newsize = sizeof longval; 217 break; 218 case CTLTYPE_ULONG: 219 ulongval = strtoul(newval, NULL, 0); 220 newval = &ulongval; 221 newsize = sizeof ulongval; 222 break; 223 case CTLTYPE_STRING: 224 break; 225 case CTLTYPE_QUAD: 226 sscanf(newval, "%qd", &quadval); 227 newval = &quadval; 228 newsize = sizeof(quadval); 229 break; 230 case CTLTYPE_OPAQUE: 231 if (strcmp(fmt, "T,dev_t") == 0) { 232 set_T_dev_t ((char*)newval, &newval, &newsize); 233 break; 234 } 235 /* FALLTHROUGH */ 236 default: 237 errx(1, "oid '%s' is type %d," 238 " cannot set that", bufp, 239 kind & CTLTYPE); 240 } 241 242 i = show_var(mib, len); 243 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) { 244 if (!i && !bflag) 245 putchar('\n'); 246 switch (errno) { 247 case EOPNOTSUPP: 248 errx(1, "%s: value is not available", 249 string); 250 case ENOTDIR: 251 errx(1, "%s: specification is incomplete", 252 string); 253 case ENOMEM: 254 errx(1, "%s: type is unknown to this program", 255 string); 256 default: 257 warn("%s", string); 258 return; 259 } 260 } 261 if (!bflag) 262 printf(" -> "); 263 i = nflag; 264 nflag = 1; 265 j = show_var(mib, len); 266 if (!j && !bflag) 267 putchar('\n'); 268 nflag = i; 269 } 270 } 271 272 /* These functions will dump out various interesting structures. */ 273 274 static int 275 S_clockinfo(int l2, void *p) 276 { 277 struct clockinfo *ci = (struct clockinfo*)p; 278 if (l2 != sizeof(*ci)) { 279 warnx("S_clockinfo %d != %d", l2, sizeof(*ci)); 280 return (0); 281 } 282 printf("{ hz = %d, tick = %d, profhz = %d, stathz = %d }", 283 ci->hz, ci->tick, ci->profhz, ci->stathz); 284 return (0); 285 } 286 287 static int 288 S_loadavg(int l2, void *p) 289 { 290 struct loadavg *tv = (struct loadavg*)p; 291 292 if (l2 != sizeof(*tv)) { 293 warnx("S_loadavg %d != %d", l2, sizeof(*tv)); 294 return (0); 295 } 296 printf("{ %.2f %.2f %.2f }", 297 (double)tv->ldavg[0]/(double)tv->fscale, 298 (double)tv->ldavg[1]/(double)tv->fscale, 299 (double)tv->ldavg[2]/(double)tv->fscale); 300 return (0); 301 } 302 303 static int 304 S_timeval(int l2, void *p) 305 { 306 struct timeval *tv = (struct timeval*)p; 307 time_t tv_sec; 308 char *p1, *p2; 309 310 if (l2 != sizeof(*tv)) { 311 warnx("S_timeval %d != %d", l2, sizeof(*tv)); 312 return (0); 313 } 314 printf("{ sec = %ld, usec = %ld } ", 315 tv->tv_sec, tv->tv_usec); 316 tv_sec = tv->tv_sec; 317 p1 = strdup(ctime(&tv_sec)); 318 for (p2=p1; *p2 ; p2++) 319 if (*p2 == '\n') 320 *p2 = '\0'; 321 fputs(p1, stdout); 322 return (0); 323 } 324 325 static int 326 T_dev_t(int l2, void *p) 327 { 328 dev_t *d = (dev_t *)p; 329 if (l2 != sizeof(*d)) { 330 warnx("T_dev_T %d != %d", l2, sizeof(*d)); 331 return (0); 332 } 333 if ((int)(*d) != -1) { 334 if (minor(*d) > 255 || minor(*d) < 0) 335 printf("{ major = %d, minor = 0x%x }", 336 major(*d), minor(*d)); 337 else 338 printf("{ major = %d, minor = %d }", 339 major(*d), minor(*d)); 340 } 341 return (0); 342 } 343 344 static void 345 set_T_dev_t (char *path, void **val, int *size) 346 { 347 static struct stat statb; 348 349 if (strcmp(path, "none") && strcmp(path, "off")) { 350 int rc = stat (path, &statb); 351 if (rc) { 352 err(1, "cannot stat %s", path); 353 } 354 355 if (!S_ISCHR(statb.st_mode)) { 356 errx(1, "must specify a device special file."); 357 } 358 } else { 359 statb.st_rdev = NODEV; 360 } 361 *val = (char*) &statb.st_rdev; 362 *size = sizeof statb.st_rdev; 363 } 364 365 /* 366 * These functions uses a presently undocumented interface to the kernel 367 * to walk the tree and get the type so it can print the value. 368 * This interface is under work and consideration, and should probably 369 * be killed with a big axe by the first person who can find the time. 370 * (be aware though, that the proper interface isn't as obvious as it 371 * may seem, there are various conflicting requirements. 372 */ 373 374 static int 375 name2oid(char *name, int *oidp) 376 { 377 int oid[2]; 378 int i; 379 size_t j; 380 381 oid[0] = 0; 382 oid[1] = 3; 383 384 j = CTL_MAXNAME * sizeof(int); 385 i = sysctl(oid, 2, oidp, &j, name, strlen(name)); 386 if (i < 0) 387 return i; 388 j /= sizeof(int); 389 return (j); 390 } 391 392 static int 393 oidfmt(int *oid, int len, char *fmt, u_int *kind) 394 { 395 int qoid[CTL_MAXNAME+2]; 396 u_char buf[BUFSIZ]; 397 int i; 398 size_t j; 399 400 qoid[0] = 0; 401 qoid[1] = 4; 402 memcpy(qoid + 2, oid, len * sizeof(int)); 403 404 j = sizeof(buf); 405 i = sysctl(qoid, len + 2, buf, &j, 0, 0); 406 if (i) 407 err(1, "sysctl fmt %d %d %d", i, j, errno); 408 409 if (kind) 410 *kind = *(u_int *)buf; 411 412 if (fmt) 413 strcpy(fmt, (char *)(buf + sizeof(u_int))); 414 return 0; 415 } 416 417 #ifdef __i386__ 418 /* 419 * Code to map a bootdev major number into a suitable device name. 420 * Major numbers are mapped into names as in boot2.c 421 */ 422 struct _foo { 423 int majdev; 424 char *name; 425 } maj2name[] = { 426 30, "ad", 427 0, "wd", 428 1, "wfd", 429 2, "fd", 430 4, "da", 431 -1, NULL /* terminator */ 432 }; 433 434 static int 435 machdep_bootdev(u_long value) 436 { 437 int majdev, unit, slice, part; 438 struct _foo *p; 439 440 if (value & B_MAGICMASK != B_DEVMAGIC) { 441 printf("invalid (0x%08x)", value); 442 return 0; 443 } 444 majdev = B_TYPE(value); 445 unit = B_UNIT(value); 446 slice = B_SLICE(value); 447 part = B_PARTITION(value); 448 if (majdev == 2) { /* floppy, as known to the boot block... */ 449 printf("/dev/fd%d", unit); 450 return 0; 451 } 452 for (p = maj2name; p->name != NULL && p->majdev != majdev ; p++) ; 453 if (p->name != NULL) { /* found */ 454 if (slice == WHOLE_DISK_SLICE) 455 printf("/dev/%s%d%c", p->name, unit, part); 456 else 457 printf("/dev/%s%ds%d%c", 458 p->name, unit, slice - BASE_SLICE + 1, part + 'a'); 459 } else 460 printf("unknown (major %d unit %d slice %d part %d)", 461 majdev, unit, slice, part); 462 return 0; 463 } 464 #endif 465 466 /* 467 * This formats and outputs the value of one variable 468 * 469 * Returns zero if anything was actually output. 470 * Returns one if didn't know what to do with this. 471 * Return minus one if we had errors. 472 */ 473 474 static int 475 show_var(int *oid, int nlen) 476 { 477 u_char buf[BUFSIZ], *val, *p; 478 char name[BUFSIZ], *fmt, *sep; 479 int qoid[CTL_MAXNAME+2]; 480 int i; 481 size_t j, len; 482 u_int kind; 483 int (*func)(int, void *); 484 485 qoid[0] = 0; 486 memcpy(qoid + 2, oid, nlen * sizeof(int)); 487 488 qoid[1] = 1; 489 j = sizeof(name); 490 i = sysctl(qoid, nlen + 2, name, &j, 0, 0); 491 if (i || !j) 492 err(1, "sysctl name %d %d %d", i, j, errno); 493 494 if (Nflag) { 495 printf("%s", name); 496 return (0); 497 } 498 499 if (eflag) 500 sep = "="; 501 else 502 sep = ": "; 503 504 if (dflag) { /* just print description */ 505 qoid[1] = 5; 506 j = sizeof(buf); 507 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0); 508 if (!nflag) 509 printf("%s%s", name, sep); 510 printf("%s", buf); 511 return (0); 512 } 513 /* find an estimate of how much we need for this var */ 514 j = 0; 515 i = sysctl(oid, nlen, 0, &j, 0, 0); 516 j += j; /* we want to be sure :-) */ 517 518 val = alloca(j); 519 len = j; 520 i = sysctl(oid, nlen, val, &len, 0, 0); 521 if (i || !len) 522 return (1); 523 524 if (bflag) { 525 fwrite(val, 1, len, stdout); 526 return (0); 527 } 528 val[len] = '\0'; 529 fmt = buf; 530 oidfmt(oid, nlen, fmt, &kind); 531 p = val; 532 switch (*fmt) { 533 case 'A': 534 if (!nflag) 535 printf("%s%s", name, sep); 536 printf("%s", p); 537 return (0); 538 539 case 'I': 540 if (!nflag) 541 printf("%s%s", name, sep); 542 fmt++; 543 val = ""; 544 while (len >= sizeof(int)) { 545 if(*fmt == 'U') 546 printf("%s%u", val, *(unsigned int *)p); 547 else 548 printf("%s%d", val, *(int *)p); 549 val = " "; 550 len -= sizeof(int); 551 p += sizeof(int); 552 } 553 return (0); 554 555 case 'L': 556 if (!nflag) 557 printf("%s%s", name, sep); 558 fmt++; 559 #ifdef __i386__ 560 if (!strcmp(name, "machdep.guessed_bootdev")) 561 return machdep_bootdev(*(unsigned long *)p); 562 #endif 563 val = ""; 564 while (len >= sizeof(long)) { 565 if(*fmt == 'U') 566 printf("%s%lu", val, *(unsigned long *)p); 567 else 568 printf("%s%ld", val, *(long *)p); 569 val = " "; 570 len -= sizeof(long); 571 p += sizeof(long); 572 } 573 return (0); 574 575 case 'P': 576 if (!nflag) 577 printf("%s%s", name, sep); 578 printf("%p", *(void **)p); 579 return (0); 580 581 case 'T': 582 case 'S': 583 i = 0; 584 if (strcmp(fmt, "S,clockinfo") == 0) 585 func = S_clockinfo; 586 else if (strcmp(fmt, "S,timeval") == 0) 587 func = S_timeval; 588 else if (strcmp(fmt, "S,loadavg") == 0) 589 func = S_loadavg; 590 else if (strcmp(fmt, "T,dev_t") == 0) 591 func = T_dev_t; 592 else 593 func = NULL; 594 if (func) { 595 if (!nflag) 596 printf("%s%s", name, sep); 597 return ((*func)(len, p)); 598 } 599 /* FALLTHROUGH */ 600 default: 601 if (!oflag && !xflag) 602 return (1); 603 if (!nflag) 604 printf("%s%s", name, sep); 605 printf("Format:%s Length:%d Dump:0x", fmt, len); 606 while (len-- && (xflag || p < val + 16)) 607 printf("%02x", *p++); 608 if (!xflag && len > 16) 609 printf("..."); 610 return (0); 611 } 612 return (1); 613 } 614 615 static int 616 sysctl_all (int *oid, int len) 617 { 618 int name1[22], name2[22]; 619 int i, j; 620 size_t l1, l2; 621 622 name1[0] = 0; 623 name1[1] = 2; 624 l1 = 2; 625 if (len) { 626 memcpy(name1+2, oid, len * sizeof(int)); 627 l1 += len; 628 } else { 629 name1[2] = 1; 630 l1++; 631 } 632 for (;;) { 633 l2 = sizeof(name2); 634 j = sysctl(name1, l1, name2, &l2, 0, 0); 635 if (j < 0) { 636 if (errno == ENOENT) 637 return 0; 638 else 639 err(1, "sysctl(getnext) %d %d", j, l2); 640 } 641 642 l2 /= sizeof(int); 643 644 if (l2 < len) 645 return 0; 646 647 for (i = 0; i < len; i++) 648 if (name2[i] != oid[i]) 649 return 0; 650 651 i = show_var(name2, l2); 652 if (!i && !bflag) 653 putchar('\n'); 654 655 memcpy(name1+2, name2, l2 * sizeof(int)); 656 l1 = 2 + l2; 657 } 658 } 659