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[] =
44 #include <sys/param.h>
46 #include <sys/resource.h>
48 #include <sys/sysctl.h>
49 #include <sys/vmmeter.h>
62 static const char *conffile;
64 static int aflag, bflag, dflag, eflag, hflag, iflag;
65 static int Nflag, nflag, oflag, qflag, Tflag, Wflag, xflag;
67 static int oidfmt(int *, int, char *, u_int *);
68 static int parsefile(const char *);
69 static int parse(const char *, int);
70 static int show_var(int *, int);
71 static int sysctl_all(int *oid, int len);
72 static int name2oid(char *, int *);
74 static int set_IK(const char *, int *);
80 (void)fprintf(stderr, "%s\n%s\n",
81 "usage: sysctl [-bdehiNnoqTWx] [-f filename] name[=value] ...",
82 " sysctl [-bdehNnoqTWx] -a");
87 main(int argc, char **argv)
92 setlocale(LC_NUMERIC, "");
96 while ((ch = getopt(argc, argv, "Aabdef:hiNnoqTwWxX")) != -1) {
161 if (aflag && argc == 0)
162 exit(sysctl_all(0, 0));
163 if (argc == 0 && conffile == NULL)
167 if (conffile != NULL)
168 warncount += parsefile(conffile);
171 warncount += parse(*argv++, 0);
177 * Parse a name into a MIB entry.
178 * Lookup and print out the MIB entry if it exists.
179 * Set a new value if requested.
182 parse(const char *string, int lineno)
187 unsigned int uintval;
189 unsigned long ulongval;
193 int mib[CTL_MAXNAME];
194 char *cp, *bufp, buf[BUFSIZ], *endptr, fmt[BUFSIZ], line[BUFSIZ];
198 snprintf(line, sizeof(line), " at line %d", lineno);
203 if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ) {
204 warn("oid too long: '%s'%s", string, line);
207 bufp = strsep(&cp, "=:");
209 /* Tflag just lists tunables, do not allow assignment */
210 if (Tflag || Wflag) {
211 warnx("Can't set variables when using -T or -W");
216 /* Strip a pair of " or ' if any. */
220 if (cp[strlen(cp) - 1] == *cp)
221 cp[strlen(cp) - 1] = '\0';
225 newsize = strlen(cp);
227 len = name2oid(bufp, mib);
235 warn("unknown oid '%s'%s", bufp, line);
240 if (oidfmt(mib, len, fmt, &kind)) {
241 warn("couldn't find format of oid '%s'%s", bufp, line);
248 if (newval == NULL || dflag) {
249 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
251 i = show_var(mib, len);
255 sysctl_all(mib, len);
257 i = show_var(mib, len);
262 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
263 warn("oid '%s' isn't a leaf node%s", bufp, line);
267 if (!(kind & CTLFLAG_WR)) {
268 if (kind & CTLFLAG_TUN) {
269 warnx("oid '%s' is a read only tunable%s", bufp, line);
270 warnx("Tunable values are set in /boot/loader.conf");
272 warnx("oid '%s' is read only%s", bufp, line);
276 if ((kind & CTLTYPE) == CTLTYPE_INT ||
277 (kind & CTLTYPE) == CTLTYPE_UINT ||
278 (kind & CTLTYPE) == CTLTYPE_LONG ||
279 (kind & CTLTYPE) == CTLTYPE_ULONG ||
280 (kind & CTLTYPE) == CTLTYPE_S64 ||
281 (kind & CTLTYPE) == CTLTYPE_U64) {
282 if (strlen(newval) == 0) {
283 warnx("empty numeric value");
288 switch (kind & CTLTYPE) {
290 if (strcmp(fmt, "IK") == 0) {
291 if (!set_IK(newval, &intval)) {
292 warnx("invalid value '%s'%s",
293 (char *)newval, line);
297 intval = (int)strtol(newval, &endptr,
299 if (endptr == newval || *endptr != '\0') {
300 warnx("invalid integer '%s'%s",
301 (char *)newval, line);
306 newsize = sizeof(intval);
309 uintval = (int) strtoul(newval, &endptr, 0);
310 if (endptr == newval || *endptr != '\0') {
311 warnx("invalid unsigned integer '%s'%s",
312 (char *)newval, line);
316 newsize = sizeof(uintval);
319 longval = strtol(newval, &endptr, 0);
320 if (endptr == newval || *endptr != '\0') {
321 warnx("invalid long integer '%s'%s",
322 (char *)newval, line);
326 newsize = sizeof(longval);
329 ulongval = strtoul(newval, &endptr, 0);
330 if (endptr == newval || *endptr != '\0') {
331 warnx("invalid unsigned long integer"
332 " '%s'%s", (char *)newval, line);
336 newsize = sizeof(ulongval);
341 i64val = strtoimax(newval, &endptr, 0);
342 if (endptr == newval || *endptr != '\0') {
343 warnx("invalid int64_t '%s'%s",
344 (char *)newval, line);
348 newsize = sizeof(i64val);
351 u64val = strtoumax(newval, &endptr, 0);
352 if (endptr == newval || *endptr != '\0') {
353 warnx("invalid uint64_t '%s'%s",
354 (char *)newval, line);
358 newsize = sizeof(u64val);
363 warnx("oid '%s' is type %d,"
364 " cannot set that%s", bufp,
365 kind & CTLTYPE, line);
369 i = show_var(mib, len);
370 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
375 warnx("%s: value is not available%s",
379 warnx("%s: specification is incomplete%s",
383 warnx("%s: type is unknown to this program%s",
387 warn("%s%s", string, line);
395 j = show_var(mib, len);
405 parsefile(const char *filename)
408 char line[BUFSIZ], *p, *pq, *pdq;
409 int warncount = 0, lineno = 0;
411 file = fopen(filename, "r");
413 err(EX_NOINPUT, "%s", filename);
414 while (fgets(line, sizeof(line), file) != NULL) {
417 pq = strchr(line, '\'');
418 pdq = strchr(line, '\"');
419 /* Replace the first # with \0. */
420 while((p = strchr(p, '#')) != NULL) {
421 if (pq != NULL && p > pq) {
422 if ((p = strchr(pq+1, '\'')) != NULL)
425 } else if (pdq != NULL && p > pdq) {
426 if ((p = strchr(pdq+1, '\"')) != NULL)
429 } else if (p == line || *(p-1) != '\\') {
436 p = line + strlen(line) - 1;
437 while (p >= line && isspace((int)*p)) {
442 while (isspace((int)*p))
447 warncount += parse(p, lineno);
454 /* These functions will dump out various interesting structures. */
457 S_clockinfo(int l2, void *p)
459 struct clockinfo *ci = (struct clockinfo*)p;
461 if (l2 != sizeof(*ci)) {
462 warnx("S_clockinfo %d != %zu", l2, sizeof(*ci));
465 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
466 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
467 ci->hz, ci->tick, ci->profhz, ci->stathz);
472 S_loadavg(int l2, void *p)
474 struct loadavg *tv = (struct loadavg*)p;
476 if (l2 != sizeof(*tv)) {
477 warnx("S_loadavg %d != %zu", l2, sizeof(*tv));
480 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
481 (double)tv->ldavg[0]/(double)tv->fscale,
482 (double)tv->ldavg[1]/(double)tv->fscale,
483 (double)tv->ldavg[2]/(double)tv->fscale);
488 S_timeval(int l2, void *p)
490 struct timeval *tv = (struct timeval*)p;
494 if (l2 != sizeof(*tv)) {
495 warnx("S_timeval %d != %zu", l2, sizeof(*tv));
498 printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
499 "{ sec = %jd, usec = %ld } ",
500 (intmax_t)tv->tv_sec, tv->tv_usec);
502 p1 = strdup(ctime(&tv_sec));
503 for (p2=p1; *p2 ; p2++)
512 S_vmtotal(int l2, void *p)
514 struct vmtotal *v = (struct vmtotal *)p;
515 int pageKilo = getpagesize() / 1024;
517 if (l2 != sizeof(*v)) {
518 warnx("S_vmtotal %d != %zu", l2, sizeof(*v));
523 "\nSystem wide totals computed every five seconds:"
524 " (values in kilobytes)\n");
525 printf("===============================================\n");
527 "Processes:\t\t(RUNQ: %hd Disk Wait: %hd Page Wait: "
529 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
531 "Virtual Memory:\t\t(Total: %dK Active: %dK)\n",
532 v->t_vm * pageKilo, v->t_avm * pageKilo);
533 printf("Real Memory:\t\t(Total: %dK Active: %dK)\n",
534 v->t_rm * pageKilo, v->t_arm * pageKilo);
535 printf("Shared Virtual Memory:\t(Total: %dK Active: %dK)\n",
536 v->t_vmshr * pageKilo, v->t_avmshr * pageKilo);
537 printf("Shared Real Memory:\t(Total: %dK Active: %dK)\n",
538 v->t_rmshr * pageKilo, v->t_armshr * pageKilo);
539 printf("Free Memory:\t%dK\n", v->t_free * pageKilo);
545 set_IK(const char *str, int *val)
552 if ((len = strlen(str)) == 0)
555 if (*p == 'C' || *p == 'F') {
556 temp = strtof(str, &endptr);
557 if (endptr == str || endptr != p)
560 temp = (temp - 32) * 5 / 9;
561 kelv = temp * 10 + 2732;
563 kelv = (int)strtol(str, &endptr, 10);
564 if (endptr == str || *endptr != '\0')
572 * These functions uses a presently undocumented interface to the kernel
573 * to walk the tree and get the type so it can print the value.
574 * This interface is under work and consideration, and should probably
575 * be killed with a big axe by the first person who can find the time.
576 * (be aware though, that the proper interface isn't as obvious as it
577 * may seem, there are various conflicting requirements.
581 name2oid(char *name, int *oidp)
590 j = CTL_MAXNAME * sizeof(int);
591 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
599 oidfmt(int *oid, int len, char *fmt, u_int *kind)
601 int qoid[CTL_MAXNAME+2];
608 memcpy(qoid + 2, oid, len * sizeof(int));
611 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
613 err(1, "sysctl fmt %d %zu %d", i, j, errno);
616 *kind = *(u_int *)buf;
619 strcpy(fmt, (char *)(buf + sizeof(u_int)));
623 static int ctl_sign[CTLTYPE+1] = {
629 static int ctl_size[CTLTYPE+1] = {
630 [CTLTYPE_INT] = sizeof(int),
631 [CTLTYPE_UINT] = sizeof(u_int),
632 [CTLTYPE_LONG] = sizeof(long),
633 [CTLTYPE_ULONG] = sizeof(u_long),
634 [CTLTYPE_S64] = sizeof(int64_t),
635 [CTLTYPE_U64] = sizeof(int64_t),
639 * This formats and outputs the value of one variable
641 * Returns zero if anything was actually output.
642 * Returns one if didn't know what to do with this.
643 * Return minus one if we had errors.
646 show_var(int *oid, int nlen)
648 u_char buf[BUFSIZ], *val, *oval, *p;
649 char name[BUFSIZ], fmt[BUFSIZ];
650 const char *sep, *sep1;
651 int qoid[CTL_MAXNAME+2];
654 int i, hexlen, sign, ctltype;
658 int (*func)(int, void *);
661 umv = mv = intlen = 0;
667 memcpy(qoid + 2, oid, nlen * sizeof(int));
671 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
673 err(1, "sysctl name %d %zu %d", i, j, errno);
675 oidfmt(oid, nlen, fmt, &kind);
676 /* if Wflag then only list sysctls that are writeable and not stats. */
677 if (Wflag && ((kind & CTLFLAG_WR) == 0 || (kind & CTLFLAG_STATS) != 0))
680 /* if Tflag then only list sysctls that are tuneables. */
681 if (Tflag && (kind & CTLFLAG_TUN) == 0)
694 if (dflag) { /* just print description */
697 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
699 printf("%s%s", name, sep);
703 /* find an estimate of how much we need for this var */
705 i = sysctl(oid, nlen, 0, &j, 0, 0);
706 j += j; /* we want to be sure :-) */
708 val = oval = malloc(j + 1);
710 warnx("malloc failed");
714 i = sysctl(oid, nlen, val, &len, 0, 0);
721 fwrite(val, 1, len, stdout);
727 ctltype = (kind & CTLTYPE);
728 sign = ctl_sign[ctltype];
729 intlen = ctl_size[ctltype];
734 printf("%s%s", name, sep);
735 printf("%.*s", (int)len, p);
746 printf("%s%s", name, sep);
747 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
749 while (len >= intlen) {
750 switch (kind & CTLTYPE) {
763 umv = *(uint64_t *)p;
769 printf("%#0*jx", hexlen, umv);
771 printf(hflag ? "%'ju" : "%ju", umv);
772 else if (fmt[1] == 'K') {
776 printf("%.1fC", (mv - 2732.0) / 10);
778 printf(hflag ? "%'jd" : "%jd", mv);
788 if (strcmp(fmt, "S,clockinfo") == 0)
790 else if (strcmp(fmt, "S,timeval") == 0)
792 else if (strcmp(fmt, "S,loadavg") == 0)
794 else if (strcmp(fmt, "S,vmtotal") == 0)
800 printf("%s%s", name, sep);
807 if (!oflag && !xflag) {
812 printf("%s%s", name, sep);
813 printf("Format:%s Length:%zu Dump:0x", fmt, len);
814 while (len-- && (xflag || p < val + 16))
815 printf("%02x", *p++);
816 if (!xflag && len > 16)
826 sysctl_all(int *oid, int len)
828 int name1[22], name2[22];
836 memcpy(name1+2, oid, len * sizeof(int));
844 j = sysctl(name1, l1, name2, &l2, 0, 0);
849 err(1, "sysctl(getnext) %d %zu", j, l2);
854 if (len < 0 || l2 < (unsigned int)len)
857 for (i = 0; i < len; i++)
858 if (name2[i] != oid[i])
861 i = show_var(name2, l2);
865 memcpy(name1+2, name2, l2 * sizeof(int));