3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 static const char copyright[] =
32 "@(#) Copyright (c) 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93";
40 static const char rcsid[] =
45 #include <sys/reboot.h> /* used for bootdev parsing */
47 #include <sys/param.h>
49 #include <sys/resource.h>
51 #include <sys/sysctl.h>
52 #include <sys/vmmeter.h>
63 static int aflag, bflag, dflag, eflag, hflag, Nflag, nflag, oflag;
64 static int qflag, xflag;
66 static int oidfmt(int *, int, char *, u_int *);
67 static void parse(char *);
68 static int show_var(int *, int);
69 static int sysctl_all (int *oid, int len);
70 static int name2oid(char *, int *);
72 static void set_T_dev_t (char *, void **, size_t *);
73 static int set_IK(char *, int *);
79 (void)fprintf(stderr, "%s\n%s\n",
80 "usage: sysctl [-bdehNnoqx] name[=value] ...",
81 " sysctl [-bdehNnoqx] -a");
86 main(int argc, char **argv)
90 setlocale(LC_NUMERIC, "");
94 while ((ch = getopt(argc, argv, "AabdehNnoqwxX")) != -1) {
147 if (aflag && argc == 0)
148 exit(sysctl_all(0, 0));
157 * Parse a name into a MIB entry.
158 * Lookup and print out the MIB entry if it exists.
159 * Set a new value if requested.
167 unsigned int uintval;
169 unsigned long ulongval;
172 int mib[CTL_MAXNAME];
173 char *cp, *bufp, buf[BUFSIZ], *endptr, fmt[BUFSIZ];
177 if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ)
178 errx(1, "oid too long: '%s'", string);
179 if ((cp = strchr(string, '=')) != NULL) {
180 *strchr(buf, '=') = '\0';
185 newsize = strlen(cp);
187 len = name2oid(bufp, mib);
193 errx(1, "unknown oid '%s'", bufp);
196 if (oidfmt(mib, len, fmt, &kind))
197 err(1, "couldn't find format of oid '%s'", bufp);
199 if (newval == NULL) {
200 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
202 i = show_var(mib, len);
206 sysctl_all(mib, len);
208 i = show_var(mib, len);
213 if ((kind & CTLTYPE) == CTLTYPE_NODE)
214 errx(1, "oid '%s' isn't a leaf node", bufp);
216 if (!(kind & CTLFLAG_WR)) {
217 if (kind & CTLFLAG_TUN) {
218 warnx("oid '%s' is a read only tunable", bufp);
219 errx(1, "Tunable values are set in /boot/loader.conf");
221 errx(1, "oid '%s' is read only", bufp);
225 if ((kind & CTLTYPE) == CTLTYPE_INT ||
226 (kind & CTLTYPE) == CTLTYPE_UINT ||
227 (kind & CTLTYPE) == CTLTYPE_LONG ||
228 (kind & CTLTYPE) == CTLTYPE_ULONG ||
229 (kind & CTLTYPE) == CTLTYPE_QUAD) {
230 if (strlen(newval) == 0)
231 errx(1, "empty numeric value");
234 switch (kind & CTLTYPE) {
236 if (strcmp(fmt, "IK") == 0) {
237 if (!set_IK((char*)newval, &intval))
238 errx(1, "invalid value '%s'",
241 intval = (int)strtol(newval, &endptr,
243 if (endptr == newval || *endptr != '\0')
244 errx(1, "invalid integer '%s'",
248 newsize = sizeof(intval);
251 uintval = (int) strtoul(newval, &endptr, 0);
252 if (endptr == newval || *endptr != '\0')
253 errx(1, "invalid unsigned integer '%s'",
256 newsize = sizeof uintval;
259 longval = strtol(newval, &endptr, 0);
260 if (endptr == newval || *endptr != '\0')
261 errx(1, "invalid long integer '%s'",
264 newsize = sizeof longval;
267 ulongval = strtoul(newval, &endptr, 0);
268 if (endptr == newval || *endptr != '\0')
269 errx(1, "invalid unsigned long integer"
272 newsize = sizeof ulongval;
277 sscanf(newval, "%qd", &quadval);
279 newsize = sizeof(quadval);
282 if (strcmp(fmt, "T,dev_t") == 0) {
283 set_T_dev_t ((char*)newval, &newval, &newsize);
288 errx(1, "oid '%s' is type %d,"
289 " cannot set that", bufp,
293 i = show_var(mib, len);
294 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
299 errx(1, "%s: value is not available",
302 errx(1, "%s: specification is incomplete",
305 errx(1, "%s: type is unknown to this program",
316 j = show_var(mib, len);
323 /* These functions will dump out various interesting structures. */
326 S_clockinfo(int l2, void *p)
328 struct clockinfo *ci = (struct clockinfo*)p;
329 if (l2 != sizeof(*ci)) {
330 warnx("S_clockinfo %d != %d", l2, sizeof(*ci));
333 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
334 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
335 ci->hz, ci->tick, ci->profhz, ci->stathz);
340 S_loadavg(int l2, void *p)
342 struct loadavg *tv = (struct loadavg*)p;
344 if (l2 != sizeof(*tv)) {
345 warnx("S_loadavg %d != %d", l2, sizeof(*tv));
348 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
349 (double)tv->ldavg[0]/(double)tv->fscale,
350 (double)tv->ldavg[1]/(double)tv->fscale,
351 (double)tv->ldavg[2]/(double)tv->fscale);
356 S_timeval(int l2, void *p)
358 struct timeval *tv = (struct timeval*)p;
362 if (l2 != sizeof(*tv)) {
363 warnx("S_timeval %d != %d", l2, sizeof(*tv));
366 printf(hflag ? "{ sec = %'ld, usec = %'ld } " :
367 "{ sec = %ld, usec = %ld } ",
368 tv->tv_sec, tv->tv_usec);
370 p1 = strdup(ctime(&tv_sec));
371 for (p2=p1; *p2 ; p2++)
379 S_vmtotal(int l2, void *p)
381 struct vmtotal *v = (struct vmtotal *)p;
382 int pageKilo = getpagesize() / 1024;
384 if (l2 != sizeof(*v)) {
385 warnx("S_vmtotal %d != %d", l2, sizeof(*v));
390 "\nSystem wide totals computed every five seconds:"
391 " (values in kilobytes)\n");
392 printf("===============================================\n");
394 "Processes:\t\t(RUNQ: %hd Disk Wait: %hd Page Wait: "
396 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
398 "Virtual Memory:\t\t(Total: %dK, Active %dK)\n",
399 v->t_vm * pageKilo, v->t_avm * pageKilo);
400 printf("Real Memory:\t\t(Total: %dK Active %dK)\n",
401 v->t_rm * pageKilo, v->t_arm * pageKilo);
402 printf("Shared Virtual Memory:\t(Total: %dK Active: %dK)\n",
403 v->t_vmshr * pageKilo, v->t_avmshr * pageKilo);
404 printf("Shared Real Memory:\t(Total: %dK Active: %dK)\n",
405 v->t_rmshr * pageKilo, v->t_armshr * pageKilo);
406 printf("Free Memory Pages:\t%dK\n", v->t_free * pageKilo);
412 T_dev_t(int l2, void *p)
414 dev_t *d = (dev_t *)p;
415 if (l2 != sizeof(*d)) {
416 warnx("T_dev_T %d != %d", l2, sizeof(*d));
419 if ((int)(*d) != -1) {
420 if (minor(*d) > 255 || minor(*d) < 0)
421 printf("{ major = %d, minor = 0x%x }",
422 major(*d), minor(*d));
424 printf("{ major = %d, minor = %d }",
425 major(*d), minor(*d));
431 set_T_dev_t (char *path, void **val, size_t *size)
433 static struct stat statb;
435 if (strcmp(path, "none") && strcmp(path, "off")) {
436 int rc = stat (path, &statb);
438 err(1, "cannot stat %s", path);
441 if (!S_ISCHR(statb.st_mode)) {
442 errx(1, "must specify a device special file.");
445 statb.st_rdev = NODEV;
447 *val = (char*) &statb.st_rdev;
448 *size = sizeof statb.st_rdev;
452 set_IK(char *str, int *val)
458 if ((len = strlen(str)) == 0)
461 if (*p == 'C' || *p == 'F') {
463 temp = strtof(str, &endptr);
464 if (endptr == str || *endptr != '\0')
467 temp = (temp - 32) * 5 / 9;
468 kelv = temp * 10 + 2732;
470 kelv = (int)strtol(str, &endptr, 10);
471 if (endptr == str || *endptr != '\0')
479 * These functions uses a presently undocumented interface to the kernel
480 * to walk the tree and get the type so it can print the value.
481 * This interface is under work and consideration, and should probably
482 * be killed with a big axe by the first person who can find the time.
483 * (be aware though, that the proper interface isn't as obvious as it
484 * may seem, there are various conflicting requirements.
488 name2oid(char *name, int *oidp)
497 j = CTL_MAXNAME * sizeof(int);
498 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
506 oidfmt(int *oid, int len, char *fmt, u_int *kind)
508 int qoid[CTL_MAXNAME+2];
515 memcpy(qoid + 2, oid, len * sizeof(int));
518 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
520 err(1, "sysctl fmt %d %d %d", i, j, errno);
523 *kind = *(u_int *)buf;
526 strcpy(fmt, (char *)(buf + sizeof(u_int)));
531 * This formats and outputs the value of one variable
533 * Returns zero if anything was actually output.
534 * Returns one if didn't know what to do with this.
535 * Return minus one if we had errors.
539 show_var(int *oid, int nlen)
541 u_char buf[BUFSIZ], *val, *oval, *p;
542 char name[BUFSIZ], *fmt, *sep;
543 int qoid[CTL_MAXNAME+2];
547 int (*func)(int, void *);
552 memcpy(qoid + 2, oid, nlen * sizeof(int));
556 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
558 err(1, "sysctl name %d %d %d", i, j, errno);
570 if (dflag) { /* just print description */
573 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
575 printf("%s%s", name, sep);
579 /* find an estimate of how much we need for this var */
581 i = sysctl(oid, nlen, 0, &j, 0, 0);
582 j += j; /* we want to be sure :-) */
584 val = oval = malloc(j + 1);
586 warnx("malloc failed");
590 i = sysctl(oid, nlen, val, &len, 0, 0);
597 fwrite(val, 1, len, stdout);
603 oidfmt(oid, nlen, fmt, &kind);
608 printf("%s%s", name, sep);
609 printf("%.*s", len, p);
615 printf("%s%s", name, sep);
618 while (len >= sizeof(int)) {
621 printf(hflag ? "%'u" : "%u",
623 else if (*fmt == 'X')
624 printf(hflag ? "%'#010x" : "%#010x",
626 else if (*fmt == 'K') {
628 printf("%d", *(int *)p);
631 (*(int *)p - 2732.0) / 10);
633 printf(hflag ? "%'d" : "%d", *(int *)p);
643 printf("%s%s", name, sep);
646 while (len >= sizeof(long)) {
649 printf(hflag ? "%'lu" : "%lu",
650 *(unsigned long *)p);
651 else if (*fmt == 'X')
652 printf(hflag ? "%'#018lx" : "%#018lx",
653 *(unsigned long *)p);
654 else if (*fmt == 'K') {
656 printf("%ld", *(long *)p);
659 (*(long *)p - 2732.0) / 10);
661 printf(hflag ? "%'ld" : "%ld", *(long *)p);
671 printf("%s%s", name, sep);
672 printf("%p", *(void **)p);
679 if (strcmp(fmt, "S,clockinfo") == 0)
681 else if (strcmp(fmt, "S,timeval") == 0)
683 else if (strcmp(fmt, "S,loadavg") == 0)
685 else if (strcmp(fmt, "S,vmtotal") == 0)
687 else if (strcmp(fmt, "T,dev_t") == 0)
693 printf("%s%s", name, sep);
700 if (!oflag && !xflag) {
705 printf("%s%s", name, sep);
706 printf("Format:%s Length:%d Dump:0x", fmt, len);
707 while (len-- && (xflag || p < val + 16))
708 printf("%02x", *p++);
709 if (!xflag && len > 16)
719 sysctl_all (int *oid, int len)
721 int name1[22], name2[22];
729 memcpy(name1+2, oid, len * sizeof(int));
737 j = sysctl(name1, l1, name2, &l2, 0, 0);
742 err(1, "sysctl(getnext) %d %d", j, l2);
750 for (i = 0; i < len; i++)
751 if (name2[i] != oid[i])
754 i = show_var(name2, l2);
758 memcpy(name1+2, name2, l2 * sizeof(int));