sysctl.c revision 121306
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 121306 2003-10-21 16:49:30Z silby $"; 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 <stdio.h> 62#include <stdlib.h> 63#include <string.h> 64#include <unistd.h> 65 66static int aflag, bflag, dflag, eflag, Nflag, nflag, oflag, xflag; 67 68static int oidfmt(int *, int, char *, u_int *); 69static void parse(char *); 70static int show_var(int *, int); 71static int sysctl_all (int *oid, int len); 72static int name2oid(char *, int *); 73 74static void set_T_dev_t (char *, void **, int *); 75 76static void 77usage(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 86int 87main(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 */ 154static void 155parse(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], *endptr, 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 if (kind & CTLFLAG_TUN) { 201 fprintf(stderr, "Tunable values are set in /boot/loader.conf and require a reboot to take effect.\n"); 202 errx(1, "oid '%s' is a tunable.", bufp); 203 } else { 204 errx(1, "oid '%s' is read only", bufp); 205 } 206 207 if ((kind & CTLTYPE) == CTLTYPE_INT || 208 (kind & CTLTYPE) == CTLTYPE_UINT || 209 (kind & CTLTYPE) == CTLTYPE_LONG || 210 (kind & CTLTYPE) == CTLTYPE_ULONG) { 211 if (strlen(newval) == 0) 212 errx(1, "empty numeric value"); 213 } 214 215 switch (kind & CTLTYPE) { 216 case CTLTYPE_INT: 217 intval = (int) strtol(newval, &endptr, 0); 218 if (endptr == newval || *endptr != '\0') 219 errx(1, "invalid integer '%s'", 220 newval); 221 newval = &intval; 222 newsize = sizeof(intval); 223 break; 224 case CTLTYPE_UINT: 225 uintval = (int) strtoul(newval, &endptr, 0); 226 if (endptr == newval || *endptr != '\0') 227 errx(1, "invalid unsigned integer '%s'", 228 newval); 229 newval = &uintval; 230 newsize = sizeof uintval; 231 break; 232 case CTLTYPE_LONG: 233 longval = strtol(newval, &endptr, 0); 234 if (endptr == newval || *endptr != '\0') 235 errx(1, "invalid long integer '%s'", 236 newval); 237 newval = &longval; 238 newsize = sizeof longval; 239 break; 240 case CTLTYPE_ULONG: 241 ulongval = strtoul(newval, &endptr, 0); 242 if (endptr == newval || *endptr != '\0') 243 errx(1, "invalid unsigned long integer" 244 " '%s'", newval); 245 newval = &ulongval; 246 newsize = sizeof ulongval; 247 break; 248 case CTLTYPE_STRING: 249 break; 250 case CTLTYPE_QUAD: 251 sscanf(newval, "%qd", &quadval); 252 newval = &quadval; 253 newsize = sizeof(quadval); 254 break; 255 case CTLTYPE_OPAQUE: 256 if (strcmp(fmt, "T,dev_t") == 0) { 257 set_T_dev_t ((char*)newval, &newval, &newsize); 258 break; 259 } 260 /* FALLTHROUGH */ 261 default: 262 errx(1, "oid '%s' is type %d," 263 " cannot set that", bufp, 264 kind & CTLTYPE); 265 } 266 267 i = show_var(mib, len); 268 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) { 269 if (!i && !bflag) 270 putchar('\n'); 271 switch (errno) { 272 case EOPNOTSUPP: 273 errx(1, "%s: value is not available", 274 string); 275 case ENOTDIR: 276 errx(1, "%s: specification is incomplete", 277 string); 278 case ENOMEM: 279 errx(1, "%s: type is unknown to this program", 280 string); 281 default: 282 warn("%s", string); 283 return; 284 } 285 } 286 if (!bflag) 287 printf(" -> "); 288 i = nflag; 289 nflag = 1; 290 j = show_var(mib, len); 291 if (!j && !bflag) 292 putchar('\n'); 293 nflag = i; 294 } 295} 296 297/* These functions will dump out various interesting structures. */ 298 299static int 300S_clockinfo(int l2, void *p) 301{ 302 struct clockinfo *ci = (struct clockinfo*)p; 303 if (l2 != sizeof(*ci)) { 304 warnx("S_clockinfo %d != %d", l2, sizeof(*ci)); 305 return (0); 306 } 307 printf("{ hz = %d, tick = %d, profhz = %d, stathz = %d }", 308 ci->hz, ci->tick, ci->profhz, ci->stathz); 309 return (0); 310} 311 312static int 313S_loadavg(int l2, void *p) 314{ 315 struct loadavg *tv = (struct loadavg*)p; 316 317 if (l2 != sizeof(*tv)) { 318 warnx("S_loadavg %d != %d", l2, sizeof(*tv)); 319 return (0); 320 } 321 printf("{ %.2f %.2f %.2f }", 322 (double)tv->ldavg[0]/(double)tv->fscale, 323 (double)tv->ldavg[1]/(double)tv->fscale, 324 (double)tv->ldavg[2]/(double)tv->fscale); 325 return (0); 326} 327 328static int 329S_timeval(int l2, void *p) 330{ 331 struct timeval *tv = (struct timeval*)p; 332 time_t tv_sec; 333 char *p1, *p2; 334 335 if (l2 != sizeof(*tv)) { 336 warnx("S_timeval %d != %d", l2, sizeof(*tv)); 337 return (0); 338 } 339 printf("{ sec = %ld, usec = %ld } ", 340 tv->tv_sec, tv->tv_usec); 341 tv_sec = tv->tv_sec; 342 p1 = strdup(ctime(&tv_sec)); 343 for (p2=p1; *p2 ; p2++) 344 if (*p2 == '\n') 345 *p2 = '\0'; 346 fputs(p1, stdout); 347 return (0); 348} 349 350static int 351S_vmtotal(int l2, void *p) 352{ 353 struct vmtotal *v = (struct vmtotal *)p; 354 int pageKilo = getpagesize() / 1024; 355 356 if (l2 != sizeof(*v)) { 357 warnx("S_vmtotal %d != %d", l2, sizeof(*v)); 358 return (0); 359 } 360 361 printf( 362 "\nSystem wide totals computed every five seconds:" 363 " (values in kilobytes)\n"); 364 printf("===============================================\n"); 365 printf( 366 "Processes:\t\t(RUNQ: %hu Disk Wait: %hu Page Wait: " 367 "%hu Sleep: %hu)\n", 368 v->t_rq, v->t_dw, v->t_pw, v->t_sl); 369 printf( 370 "Virtual Memory:\t\t(Total: %luK, Active %lldK)\n", 371 (unsigned long)v->t_vm / 1024, 372 (long long)v->t_avm * pageKilo); 373 printf("Real Memory:\t\t(Total: %lldK Active %lldK)\n", 374 (long long)v->t_rm * pageKilo, (long long)v->t_arm * pageKilo); 375 printf("Shared Virtual Memory:\t(Total: %lldK Active: %lldK)\n", 376 (long long)v->t_vmshr * pageKilo, 377 (long long)v->t_avmshr * pageKilo); 378 printf("Shared Real Memory:\t(Total: %lldK Active: %lldK)\n", 379 (long long)v->t_rmshr * pageKilo, 380 (long long)v->t_armshr * pageKilo); 381 printf("Free Memory Pages:\t%ldK\n", (long long)v->t_free * pageKilo); 382 383 return (0); 384} 385 386static int 387T_dev_t(int l2, void *p) 388{ 389 dev_t *d = (dev_t *)p; 390 if (l2 != sizeof(*d)) { 391 warnx("T_dev_T %d != %d", l2, sizeof(*d)); 392 return (0); 393 } 394 if ((int)(*d) != -1) { 395 if (minor(*d) > 255 || minor(*d) < 0) 396 printf("{ major = %d, minor = 0x%x }", 397 major(*d), minor(*d)); 398 else 399 printf("{ major = %d, minor = %d }", 400 major(*d), minor(*d)); 401 } 402 return (0); 403} 404 405static void 406set_T_dev_t (char *path, void **val, int *size) 407{ 408 static struct stat statb; 409 410 if (strcmp(path, "none") && strcmp(path, "off")) { 411 int rc = stat (path, &statb); 412 if (rc) { 413 err(1, "cannot stat %s", path); 414 } 415 416 if (!S_ISCHR(statb.st_mode)) { 417 errx(1, "must specify a device special file."); 418 } 419 } else { 420 statb.st_rdev = NODEV; 421 } 422 *val = (char*) &statb.st_rdev; 423 *size = sizeof statb.st_rdev; 424} 425 426/* 427 * These functions uses a presently undocumented interface to the kernel 428 * to walk the tree and get the type so it can print the value. 429 * This interface is under work and consideration, and should probably 430 * be killed with a big axe by the first person who can find the time. 431 * (be aware though, that the proper interface isn't as obvious as it 432 * may seem, there are various conflicting requirements. 433 */ 434 435static int 436name2oid(char *name, int *oidp) 437{ 438 int oid[2]; 439 int i; 440 size_t j; 441 442 oid[0] = 0; 443 oid[1] = 3; 444 445 j = CTL_MAXNAME * sizeof(int); 446 i = sysctl(oid, 2, oidp, &j, name, strlen(name)); 447 if (i < 0) 448 return i; 449 j /= sizeof(int); 450 return (j); 451} 452 453static int 454oidfmt(int *oid, int len, char *fmt, u_int *kind) 455{ 456 int qoid[CTL_MAXNAME+2]; 457 u_char buf[BUFSIZ]; 458 int i; 459 size_t j; 460 461 qoid[0] = 0; 462 qoid[1] = 4; 463 memcpy(qoid + 2, oid, len * sizeof(int)); 464 465 j = sizeof(buf); 466 i = sysctl(qoid, len + 2, buf, &j, 0, 0); 467 if (i) 468 err(1, "sysctl fmt %d %d %d", i, j, errno); 469 470 if (kind) 471 *kind = *(u_int *)buf; 472 473 if (fmt) 474 strcpy(fmt, (char *)(buf + sizeof(u_int))); 475 return 0; 476} 477 478/* 479 * This formats and outputs the value of one variable 480 * 481 * Returns zero if anything was actually output. 482 * Returns one if didn't know what to do with this. 483 * Return minus one if we had errors. 484 */ 485 486static int 487show_var(int *oid, int nlen) 488{ 489 u_char buf[BUFSIZ], *val, *p; 490 char name[BUFSIZ], *fmt, *sep; 491 int qoid[CTL_MAXNAME+2]; 492 int i; 493 size_t j, len; 494 u_int kind; 495 int (*func)(int, void *); 496 497 qoid[0] = 0; 498 memcpy(qoid + 2, oid, nlen * sizeof(int)); 499 500 qoid[1] = 1; 501 j = sizeof(name); 502 i = sysctl(qoid, nlen + 2, name, &j, 0, 0); 503 if (i || !j) 504 err(1, "sysctl name %d %d %d", i, j, errno); 505 506 if (Nflag) { 507 printf("%s", name); 508 return (0); 509 } 510 511 if (eflag) 512 sep = "="; 513 else 514 sep = ": "; 515 516 if (dflag) { /* just print description */ 517 qoid[1] = 5; 518 j = sizeof(buf); 519 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0); 520 if (!nflag) 521 printf("%s%s", name, sep); 522 printf("%s", buf); 523 return (0); 524 } 525 /* find an estimate of how much we need for this var */ 526 j = 0; 527 i = sysctl(oid, nlen, 0, &j, 0, 0); 528 j += j; /* we want to be sure :-) */ 529 530 val = alloca(j + 1); 531 len = j; 532 i = sysctl(oid, nlen, val, &len, 0, 0); 533 if (i || !len) 534 return (1); 535 536 if (bflag) { 537 fwrite(val, 1, len, stdout); 538 return (0); 539 } 540 val[len] = '\0'; 541 fmt = buf; 542 oidfmt(oid, nlen, fmt, &kind); 543 p = val; 544 switch (*fmt) { 545 case 'A': 546 if (!nflag) 547 printf("%s%s", name, sep); 548 printf("%.*s", len, p); 549 return (0); 550 551 case 'I': 552 if (!nflag) 553 printf("%s%s", name, sep); 554 fmt++; 555 val = ""; 556 while (len >= sizeof(int)) { 557 if(*fmt == 'U') 558 printf("%s%u", val, *(unsigned int *)p); 559 else 560 printf("%s%d", val, *(int *)p); 561 val = " "; 562 len -= sizeof(int); 563 p += sizeof(int); 564 } 565 return (0); 566 567 case 'L': 568 if (!nflag) 569 printf("%s%s", name, sep); 570 fmt++; 571 val = ""; 572 while (len >= sizeof(long)) { 573 if(*fmt == 'U') 574 printf("%s%lu", val, *(unsigned long *)p); 575 else 576 printf("%s%ld", val, *(long *)p); 577 val = " "; 578 len -= sizeof(long); 579 p += sizeof(long); 580 } 581 return (0); 582 583 case 'P': 584 if (!nflag) 585 printf("%s%s", name, sep); 586 printf("%p", *(void **)p); 587 return (0); 588 589 case 'T': 590 case 'S': 591 i = 0; 592 if (strcmp(fmt, "S,clockinfo") == 0) 593 func = S_clockinfo; 594 else if (strcmp(fmt, "S,timeval") == 0) 595 func = S_timeval; 596 else if (strcmp(fmt, "S,loadavg") == 0) 597 func = S_loadavg; 598 else if (strcmp(fmt, "S,vmtotal") == 0) 599 func = S_vmtotal; 600 else if (strcmp(fmt, "T,dev_t") == 0) 601 func = T_dev_t; 602 else 603 func = NULL; 604 if (func) { 605 if (!nflag) 606 printf("%s%s", name, sep); 607 return ((*func)(len, p)); 608 } 609 /* FALLTHROUGH */ 610 default: 611 if (!oflag && !xflag) 612 return (1); 613 if (!nflag) 614 printf("%s%s", name, sep); 615 printf("Format:%s Length:%d Dump:0x", fmt, len); 616 while (len-- && (xflag || p < val + 16)) 617 printf("%02x", *p++); 618 if (!xflag && len > 16) 619 printf("..."); 620 return (0); 621 } 622 return (1); 623} 624 625static int 626sysctl_all (int *oid, int len) 627{ 628 int name1[22], name2[22]; 629 int i, j; 630 size_t l1, l2; 631 632 name1[0] = 0; 633 name1[1] = 2; 634 l1 = 2; 635 if (len) { 636 memcpy(name1+2, oid, len * sizeof(int)); 637 l1 += len; 638 } else { 639 name1[2] = 1; 640 l1++; 641 } 642 for (;;) { 643 l2 = sizeof(name2); 644 j = sysctl(name1, l1, name2, &l2, 0, 0); 645 if (j < 0) { 646 if (errno == ENOENT) 647 return 0; 648 else 649 err(1, "sysctl(getnext) %d %d", j, l2); 650 } 651 652 l2 /= sizeof(int); 653 654 if (l2 < len) 655 return 0; 656 657 for (i = 0; i < len; i++) 658 if (name2[i] != oid[i]) 659 return 0; 660 661 i = show_var(name2, l2); 662 if (!i && !bflag) 663 putchar('\n'); 664 665 memcpy(name1+2, name2, l2 * sizeof(int)); 666 l1 = 2 + l2; 667 } 668} 669