sysctl.c revision 122233
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 35static 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 42static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93"; 43#endif 44static const char rcsid[] = 45 "$FreeBSD: head/sbin/sysctl/sysctl.c 122233 2003-11-07 16:33:45Z des $"; 46#endif /* not lint */ 47 48#ifdef __i386__ 49#include <sys/reboot.h> /* used for bootdev parsing */ 50#endif 51#include <sys/param.h> 52#include <sys/time.h> 53#include <sys/resource.h> 54#include <sys/stat.h> 55#include <sys/sysctl.h> 56#include <sys/vmmeter.h> 57 58#include <ctype.h> 59#include <err.h> 60#include <errno.h> 61#include <locale.h> 62#include <stdio.h> 63#include <stdlib.h> 64#include <string.h> 65#include <unistd.h> 66 67static int aflag, bflag, dflag, eflag, hflag, Nflag, nflag, oflag, xflag; 68 69static int oidfmt(int *, int, char *, u_int *); 70static void parse(char *); 71static int show_var(int *, int); 72static int sysctl_all (int *oid, int len); 73static int name2oid(char *, int *); 74 75static void set_T_dev_t (char *, void **, size_t *); 76 77static void 78usage(void) 79{ 80 81 (void)fprintf(stderr, "%s\n%s\n", 82 "usage: sysctl [-bdehNnox] variable[=value] ...", 83 " sysctl [-bdehNnox] -a"); 84 exit(1); 85} 86 87int 88main(int argc, char **argv) 89{ 90 int ch; 91 92 setlocale(LC_NUMERIC, ""); 93 setbuf(stdout,0); 94 setbuf(stderr,0); 95 96 while ((ch = getopt(argc, argv, "AabdehNnowxX")) != -1) { 97 switch (ch) { 98 case 'A': 99 /* compatibility */ 100 aflag = oflag = 1; 101 break; 102 case 'a': 103 aflag = 1; 104 break; 105 case 'b': 106 bflag = 1; 107 break; 108 case 'd': 109 dflag = 1; 110 break; 111 case 'e': 112 eflag = 1; 113 break; 114 case 'h': 115 hflag = 1; 116 break; 117 case 'N': 118 Nflag = 1; 119 break; 120 case 'n': 121 nflag = 1; 122 break; 123 case 'o': 124 oflag = 1; 125 break; 126 case 'w': 127 /* compatibility */ 128 /* ignored */ 129 break; 130 case 'X': 131 /* compatibility */ 132 aflag = xflag = 1; 133 break; 134 case 'x': 135 xflag = 1; 136 break; 137 default: 138 usage(); 139 } 140 } 141 argc -= optind; 142 argv += optind; 143 144 if (Nflag && nflag) 145 usage(); 146 if (aflag && argc == 0) 147 exit(sysctl_all(0, 0)); 148 if (argc == 0) 149 usage(); 150 while (argc-- > 0) 151 parse(*argv++); 152 exit(0); 153} 154 155/* 156 * Parse a name into a MIB entry. 157 * Lookup and print out the MIB entry if it exists. 158 * Set a new value if requested. 159 */ 160static void 161parse(char *string) 162{ 163 int len, i, j; 164 void *newval = 0; 165 int intval; 166 unsigned int uintval; 167 long longval; 168 unsigned long ulongval; 169 size_t newsize = 0; 170 quad_t quadval; 171 int mib[CTL_MAXNAME]; 172 char *cp, *bufp, buf[BUFSIZ], *endptr, fmt[BUFSIZ]; 173 u_int kind; 174 175 bufp = buf; 176 snprintf(buf, BUFSIZ, "%s", string); 177 if ((cp = strchr(string, '=')) != NULL) { 178 *strchr(buf, '=') = '\0'; 179 *cp++ = '\0'; 180 while (isspace(*cp)) 181 cp++; 182 newval = cp; 183 newsize = strlen(cp); 184 } 185 len = name2oid(bufp, mib); 186 187 if (len < 0) 188 errx(1, "unknown oid '%s'", bufp); 189 190 if (oidfmt(mib, len, fmt, &kind)) 191 err(1, "couldn't find format of oid '%s'", bufp); 192 193 if (newval == NULL) { 194 if ((kind & CTLTYPE) == CTLTYPE_NODE) { 195 sysctl_all(mib, len); 196 } else { 197 i = show_var(mib, len); 198 if (!i && !bflag) 199 putchar('\n'); 200 } 201 } else { 202 if ((kind & CTLTYPE) == CTLTYPE_NODE) 203 errx(1, "oid '%s' isn't a leaf node", bufp); 204 205 if (!(kind & CTLFLAG_WR)) { 206 if (kind & CTLFLAG_TUN) { 207 warnx("oid '%s' is a read only tunable", bufp); 208 errx(1, "Tunable values are set in /boot/loader.conf"); 209 } else { 210 errx(1, "oid '%s' is read only", bufp); 211 } 212 } 213 214 if ((kind & CTLTYPE) == CTLTYPE_INT || 215 (kind & CTLTYPE) == CTLTYPE_UINT || 216 (kind & CTLTYPE) == CTLTYPE_LONG || 217 (kind & CTLTYPE) == CTLTYPE_ULONG) { 218 if (strlen(newval) == 0) 219 errx(1, "empty numeric value"); 220 } 221 222 switch (kind & CTLTYPE) { 223 case CTLTYPE_INT: 224 intval = (int)strtol(newval, &endptr, 0); 225 if (endptr == newval || *endptr != '\0') 226 errx(1, "invalid integer '%s'", 227 newval); 228 newval = &intval; 229 newsize = sizeof(intval); 230 break; 231 case CTLTYPE_UINT: 232 uintval = (int) strtoul(newval, &endptr, 0); 233 if (endptr == newval || *endptr != '\0') 234 errx(1, "invalid unsigned integer '%s'", 235 newval); 236 newval = &uintval; 237 newsize = sizeof uintval; 238 break; 239 case CTLTYPE_LONG: 240 longval = strtol(newval, &endptr, 0); 241 if (endptr == newval || *endptr != '\0') 242 errx(1, "invalid long integer '%s'", 243 newval); 244 newval = &longval; 245 newsize = sizeof longval; 246 break; 247 case CTLTYPE_ULONG: 248 ulongval = strtoul(newval, &endptr, 0); 249 if (endptr == newval || *endptr != '\0') 250 errx(1, "invalid unsigned long integer" 251 " '%s'", newval); 252 newval = &ulongval; 253 newsize = sizeof ulongval; 254 break; 255 case CTLTYPE_STRING: 256 break; 257 case CTLTYPE_QUAD: 258 sscanf(newval, "%qd", &quadval); 259 newval = &quadval; 260 newsize = sizeof(quadval); 261 break; 262 case CTLTYPE_OPAQUE: 263 if (strcmp(fmt, "T,dev_t") == 0) { 264 set_T_dev_t ((char*)newval, &newval, &newsize); 265 break; 266 } 267 /* FALLTHROUGH */ 268 default: 269 errx(1, "oid '%s' is type %d," 270 " cannot set that", bufp, 271 kind & CTLTYPE); 272 } 273 274 i = show_var(mib, len); 275 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) { 276 if (!i && !bflag) 277 putchar('\n'); 278 switch (errno) { 279 case EOPNOTSUPP: 280 errx(1, "%s: value is not available", 281 string); 282 case ENOTDIR: 283 errx(1, "%s: specification is incomplete", 284 string); 285 case ENOMEM: 286 errx(1, "%s: type is unknown to this program", 287 string); 288 default: 289 warn("%s", string); 290 return; 291 } 292 } 293 if (!bflag) 294 printf(" -> "); 295 i = nflag; 296 nflag = 1; 297 j = show_var(mib, len); 298 if (!j && !bflag) 299 putchar('\n'); 300 nflag = i; 301 } 302} 303 304/* These functions will dump out various interesting structures. */ 305 306static int 307S_clockinfo(int l2, void *p) 308{ 309 struct clockinfo *ci = (struct clockinfo*)p; 310 if (l2 != sizeof(*ci)) { 311 warnx("S_clockinfo %d != %d", l2, sizeof(*ci)); 312 return (0); 313 } 314 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" : 315 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }", 316 ci->hz, ci->tick, ci->profhz, ci->stathz); 317 return (0); 318} 319 320static int 321S_loadavg(int l2, void *p) 322{ 323 struct loadavg *tv = (struct loadavg*)p; 324 325 if (l2 != sizeof(*tv)) { 326 warnx("S_loadavg %d != %d", l2, sizeof(*tv)); 327 return (0); 328 } 329 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }", 330 (double)tv->ldavg[0]/(double)tv->fscale, 331 (double)tv->ldavg[1]/(double)tv->fscale, 332 (double)tv->ldavg[2]/(double)tv->fscale); 333 return (0); 334} 335 336static int 337S_timeval(int l2, void *p) 338{ 339 struct timeval *tv = (struct timeval*)p; 340 time_t tv_sec; 341 char *p1, *p2; 342 343 if (l2 != sizeof(*tv)) { 344 warnx("S_timeval %d != %d", l2, sizeof(*tv)); 345 return (0); 346 } 347 printf(hflag ? "{ sec = %'ld, usec = %'ld } " : 348 "{ sec = %ld, usec = %ld } ", 349 tv->tv_sec, tv->tv_usec); 350 tv_sec = tv->tv_sec; 351 p1 = strdup(ctime(&tv_sec)); 352 for (p2=p1; *p2 ; p2++) 353 if (*p2 == '\n') 354 *p2 = '\0'; 355 fputs(p1, stdout); 356 return (0); 357} 358 359static int 360S_vmtotal(int l2, void *p) 361{ 362 struct vmtotal *v = (struct vmtotal *)p; 363 int pageKilo = getpagesize() / 1024; 364 365 if (l2 != sizeof(*v)) { 366 warnx("S_vmtotal %d != %d", l2, sizeof(*v)); 367 return (0); 368 } 369 370 printf( 371 "\nSystem wide totals computed every five seconds:" 372 " (values in kilobytes)\n"); 373 printf("===============================================\n"); 374 printf( 375 "Processes:\t\t(RUNQ: %hu Disk Wait: %hu Page Wait: " 376 "%hu Sleep: %hu)\n", 377 v->t_rq, v->t_dw, v->t_pw, v->t_sl); 378 printf( 379 "Virtual Memory:\t\t(Total: %luK, Active %lldK)\n", 380 (unsigned long)v->t_vm / 1024, 381 (long long)v->t_avm * pageKilo); 382 printf("Real Memory:\t\t(Total: %lldK Active %lldK)\n", 383 (long long)v->t_rm * pageKilo, (long long)v->t_arm * pageKilo); 384 printf("Shared Virtual Memory:\t(Total: %lldK Active: %lldK)\n", 385 (long long)v->t_vmshr * pageKilo, 386 (long long)v->t_avmshr * pageKilo); 387 printf("Shared Real Memory:\t(Total: %lldK Active: %lldK)\n", 388 (long long)v->t_rmshr * pageKilo, 389 (long long)v->t_armshr * pageKilo); 390 printf("Free Memory Pages:\t%ldK\n", (long long)v->t_free * pageKilo); 391 392 return (0); 393} 394 395static int 396T_dev_t(int l2, void *p) 397{ 398 dev_t *d = (dev_t *)p; 399 if (l2 != sizeof(*d)) { 400 warnx("T_dev_T %d != %d", l2, sizeof(*d)); 401 return (0); 402 } 403 if ((int)(*d) != -1) { 404 if (minor(*d) > 255 || minor(*d) < 0) 405 printf("{ major = %d, minor = 0x%x }", 406 major(*d), minor(*d)); 407 else 408 printf("{ major = %d, minor = %d }", 409 major(*d), minor(*d)); 410 } 411 return (0); 412} 413 414static void 415set_T_dev_t (char *path, void **val, size_t *size) 416{ 417 static struct stat statb; 418 419 if (strcmp(path, "none") && strcmp(path, "off")) { 420 int rc = stat (path, &statb); 421 if (rc) { 422 err(1, "cannot stat %s", path); 423 } 424 425 if (!S_ISCHR(statb.st_mode)) { 426 errx(1, "must specify a device special file."); 427 } 428 } else { 429 statb.st_rdev = NODEV; 430 } 431 *val = (char*) &statb.st_rdev; 432 *size = sizeof statb.st_rdev; 433} 434 435/* 436 * These functions uses a presently undocumented interface to the kernel 437 * to walk the tree and get the type so it can print the value. 438 * This interface is under work and consideration, and should probably 439 * be killed with a big axe by the first person who can find the time. 440 * (be aware though, that the proper interface isn't as obvious as it 441 * may seem, there are various conflicting requirements. 442 */ 443 444static int 445name2oid(char *name, int *oidp) 446{ 447 int oid[2]; 448 int i; 449 size_t j; 450 451 oid[0] = 0; 452 oid[1] = 3; 453 454 j = CTL_MAXNAME * sizeof(int); 455 i = sysctl(oid, 2, oidp, &j, name, strlen(name)); 456 if (i < 0) 457 return i; 458 j /= sizeof(int); 459 return (j); 460} 461 462static int 463oidfmt(int *oid, int len, char *fmt, u_int *kind) 464{ 465 int qoid[CTL_MAXNAME+2]; 466 u_char buf[BUFSIZ]; 467 int i; 468 size_t j; 469 470 qoid[0] = 0; 471 qoid[1] = 4; 472 memcpy(qoid + 2, oid, len * sizeof(int)); 473 474 j = sizeof(buf); 475 i = sysctl(qoid, len + 2, buf, &j, 0, 0); 476 if (i) 477 err(1, "sysctl fmt %d %d %d", i, j, errno); 478 479 if (kind) 480 *kind = *(u_int *)buf; 481 482 if (fmt) 483 strcpy(fmt, (char *)(buf + sizeof(u_int))); 484 return 0; 485} 486 487/* 488 * This formats and outputs the value of one variable 489 * 490 * Returns zero if anything was actually output. 491 * Returns one if didn't know what to do with this. 492 * Return minus one if we had errors. 493 */ 494 495static int 496show_var(int *oid, int nlen) 497{ 498 u_char buf[BUFSIZ], *val, *p; 499 char name[BUFSIZ], *fmt, *sep; 500 int qoid[CTL_MAXNAME+2]; 501 int i; 502 size_t j, len; 503 u_int kind; 504 int (*func)(int, void *); 505 506 qoid[0] = 0; 507 memcpy(qoid + 2, oid, nlen * sizeof(int)); 508 509 qoid[1] = 1; 510 j = sizeof(name); 511 i = sysctl(qoid, nlen + 2, name, &j, 0, 0); 512 if (i || !j) 513 err(1, "sysctl name %d %d %d", i, j, errno); 514 515 if (Nflag) { 516 printf("%s", name); 517 return (0); 518 } 519 520 if (eflag) 521 sep = "="; 522 else 523 sep = ": "; 524 525 if (dflag) { /* just print description */ 526 qoid[1] = 5; 527 j = sizeof(buf); 528 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0); 529 if (!nflag) 530 printf("%s%s", name, sep); 531 printf("%s", buf); 532 return (0); 533 } 534 /* find an estimate of how much we need for this var */ 535 j = 0; 536 i = sysctl(oid, nlen, 0, &j, 0, 0); 537 j += j; /* we want to be sure :-) */ 538 539 val = alloca(j + 1); 540 len = j; 541 i = sysctl(oid, nlen, val, &len, 0, 0); 542 if (i || !len) 543 return (1); 544 545 if (bflag) { 546 fwrite(val, 1, len, stdout); 547 return (0); 548 } 549 val[len] = '\0'; 550 fmt = buf; 551 oidfmt(oid, nlen, fmt, &kind); 552 p = val; 553 switch (*fmt) { 554 case 'A': 555 if (!nflag) 556 printf("%s%s", name, sep); 557 printf("%.*s", len, p); 558 return (0); 559 560 case 'I': 561 if (!nflag) 562 printf("%s%s", name, sep); 563 fmt++; 564 val = ""; 565 while (len >= sizeof(int)) { 566 fputs(val, stdout); 567 if(*fmt == 'U') 568 printf(hflag ? "%'u" : "%u", *(unsigned int *)p); 569 else 570 printf(hflag ? "%'d" : "%d", *(int *)p); 571 val = " "; 572 len -= sizeof(int); 573 p += sizeof(int); 574 } 575 return (0); 576 577 case 'L': 578 if (!nflag) 579 printf("%s%s", name, sep); 580 fmt++; 581 val = ""; 582 while (len >= sizeof(long)) { 583 fputs(val, stdout); 584 if(*fmt == 'U') 585 printf(hflag ? "%'lu" : "%lu", *(unsigned long *)p); 586 else 587 printf(hflag ? "%'ld" : "%ld", *(long *)p); 588 val = " "; 589 len -= sizeof(long); 590 p += sizeof(long); 591 } 592 return (0); 593 594 case 'P': 595 if (!nflag) 596 printf("%s%s", name, sep); 597 printf("%p", *(void **)p); 598 return (0); 599 600 case 'T': 601 case 'S': 602 i = 0; 603 if (strcmp(fmt, "S,clockinfo") == 0) 604 func = S_clockinfo; 605 else if (strcmp(fmt, "S,timeval") == 0) 606 func = S_timeval; 607 else if (strcmp(fmt, "S,loadavg") == 0) 608 func = S_loadavg; 609 else if (strcmp(fmt, "S,vmtotal") == 0) 610 func = S_vmtotal; 611 else if (strcmp(fmt, "T,dev_t") == 0) 612 func = T_dev_t; 613 else 614 func = NULL; 615 if (func) { 616 if (!nflag) 617 printf("%s%s", name, sep); 618 return ((*func)(len, p)); 619 } 620 /* FALLTHROUGH */ 621 default: 622 if (!oflag && !xflag) 623 return (1); 624 if (!nflag) 625 printf("%s%s", name, sep); 626 printf("Format:%s Length:%d Dump:0x", fmt, len); 627 while (len-- && (xflag || p < val + 16)) 628 printf("%02x", *p++); 629 if (!xflag && len > 16) 630 printf("..."); 631 return (0); 632 } 633 return (1); 634} 635 636static int 637sysctl_all (int *oid, int len) 638{ 639 int name1[22], name2[22]; 640 int i, j; 641 size_t l1, l2; 642 643 name1[0] = 0; 644 name1[1] = 2; 645 l1 = 2; 646 if (len) { 647 memcpy(name1+2, oid, len * sizeof(int)); 648 l1 += len; 649 } else { 650 name1[2] = 1; 651 l1++; 652 } 653 for (;;) { 654 l2 = sizeof(name2); 655 j = sysctl(name1, l1, name2, &l2, 0, 0); 656 if (j < 0) { 657 if (errno == ENOENT) 658 return 0; 659 else 660 err(1, "sysctl(getnext) %d %d", j, l2); 661 } 662 663 l2 /= sizeof(int); 664 665 if (l2 < len) 666 return 0; 667 668 for (i = 0; i < len; i++) 669 if (name2[i] != oid[i]) 670 return 0; 671 672 i = show_var(name2, l2); 673 if (!i && !bflag) 674 putchar('\n'); 675 676 memcpy(name1+2, name2, l2 * sizeof(int)); 677 l1 = 2 + l2; 678 } 679} 680