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>
61 static int aflag, bflag, dflag, eflag, hflag, iflag;
62 static int Nflag, nflag, oflag, qflag, xflag, warncount;
64 static int oidfmt(int *, int, char *, u_int *);
65 static void parse(char *);
66 static int show_var(int *, int);
67 static int sysctl_all(int *oid, int len);
68 static int name2oid(char *, int *);
70 static int set_IK(const char *, int *);
76 (void)fprintf(stderr, "%s\n%s\n",
77 "usage: sysctl [-bdehiNnoqx] name[=value] ...",
78 " sysctl [-bdehNnoqx] -a");
83 main(int argc, char **argv)
87 setlocale(LC_NUMERIC, "");
91 while ((ch = getopt(argc, argv, "AabdehiNnoqwxX")) != -1) {
147 if (aflag && argc == 0)
148 exit(sysctl_all(0, 0));
159 * Parse a name into a MIB entry.
160 * Lookup and print out the MIB entry if it exists.
161 * Set a new value if requested.
169 unsigned int uintval;
171 unsigned long ulongval;
175 int mib[CTL_MAXNAME];
176 char *cp, *bufp, buf[BUFSIZ], *endptr, fmt[BUFSIZ];
180 if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ)
181 errx(1, "oid too long: '%s'", string);
182 if ((cp = strchr(string, '=')) != NULL) {
183 *strchr(buf, '=') = '\0';
188 newsize = strlen(cp);
190 len = name2oid(bufp, mib);
198 errx(1, "unknown oid '%s'", bufp);
201 if (oidfmt(mib, len, fmt, &kind))
202 err(1, "couldn't find format of oid '%s'", bufp);
204 if (newval == NULL || dflag) {
205 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
207 i = show_var(mib, len);
211 sysctl_all(mib, len);
213 i = show_var(mib, len);
218 if ((kind & CTLTYPE) == CTLTYPE_NODE)
219 errx(1, "oid '%s' isn't a leaf node", bufp);
221 if (!(kind & CTLFLAG_WR)) {
222 if (kind & CTLFLAG_TUN) {
223 warnx("oid '%s' is a read only tunable", bufp);
224 errx(1, "Tunable values are set in /boot/loader.conf");
226 errx(1, "oid '%s' is read only", bufp);
230 if ((kind & CTLTYPE) == CTLTYPE_INT ||
231 (kind & CTLTYPE) == CTLTYPE_UINT ||
232 (kind & CTLTYPE) == CTLTYPE_LONG ||
233 (kind & CTLTYPE) == CTLTYPE_ULONG ||
234 (kind & CTLTYPE) == CTLTYPE_S64 ||
235 (kind & CTLTYPE) == CTLTYPE_U64) {
236 if (strlen(newval) == 0)
237 errx(1, "empty numeric value");
240 switch (kind & CTLTYPE) {
242 if (strcmp(fmt, "IK") == 0) {
243 if (!set_IK(newval, &intval))
244 errx(1, "invalid value '%s'",
247 intval = (int)strtol(newval, &endptr,
249 if (endptr == newval || *endptr != '\0')
250 errx(1, "invalid integer '%s'",
254 newsize = sizeof(intval);
257 uintval = (int) strtoul(newval, &endptr, 0);
258 if (endptr == newval || *endptr != '\0')
259 errx(1, "invalid unsigned integer '%s'",
262 newsize = sizeof(uintval);
265 longval = strtol(newval, &endptr, 0);
266 if (endptr == newval || *endptr != '\0')
267 errx(1, "invalid long integer '%s'",
270 newsize = sizeof(longval);
273 ulongval = strtoul(newval, &endptr, 0);
274 if (endptr == newval || *endptr != '\0')
275 errx(1, "invalid unsigned long integer"
276 " '%s'", (char *)newval);
278 newsize = sizeof(ulongval);
283 i64val = strtoimax(newval, &endptr, 0);
284 if (endptr == newval || *endptr != '\0')
285 errx(1, "invalid int64_t '%s'",
288 newsize = sizeof(i64val);
291 u64val = strtoumax(newval, &endptr, 0);
292 if (endptr == newval || *endptr != '\0')
293 errx(1, "invalid uint64_t '%s'",
296 newsize = sizeof(u64val);
301 errx(1, "oid '%s' is type %d,"
302 " cannot set that", bufp,
306 i = show_var(mib, len);
307 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
312 errx(1, "%s: value is not available",
315 errx(1, "%s: specification is incomplete",
318 errx(1, "%s: type is unknown to this program",
330 j = show_var(mib, len);
337 /* These functions will dump out various interesting structures. */
340 S_clockinfo(int l2, void *p)
342 struct clockinfo *ci = (struct clockinfo*)p;
344 if (l2 != sizeof(*ci)) {
345 warnx("S_clockinfo %d != %zu", l2, sizeof(*ci));
348 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
349 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
350 ci->hz, ci->tick, ci->profhz, ci->stathz);
355 S_loadavg(int l2, void *p)
357 struct loadavg *tv = (struct loadavg*)p;
359 if (l2 != sizeof(*tv)) {
360 warnx("S_loadavg %d != %zu", l2, sizeof(*tv));
363 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
364 (double)tv->ldavg[0]/(double)tv->fscale,
365 (double)tv->ldavg[1]/(double)tv->fscale,
366 (double)tv->ldavg[2]/(double)tv->fscale);
371 S_timeval(int l2, void *p)
373 struct timeval *tv = (struct timeval*)p;
377 if (l2 != sizeof(*tv)) {
378 warnx("S_timeval %d != %zu", l2, sizeof(*tv));
381 printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
382 "{ sec = %jd, usec = %ld } ",
383 (intmax_t)tv->tv_sec, tv->tv_usec);
385 p1 = strdup(ctime(&tv_sec));
386 for (p2=p1; *p2 ; p2++)
395 S_vmtotal(int l2, void *p)
397 struct vmtotal *v = (struct vmtotal *)p;
398 int pageKilo = getpagesize() / 1024;
400 if (l2 != sizeof(*v)) {
401 warnx("S_vmtotal %d != %zu", l2, sizeof(*v));
406 "\nSystem wide totals computed every five seconds:"
407 " (values in kilobytes)\n");
408 printf("===============================================\n");
410 "Processes:\t\t(RUNQ: %hd Disk Wait: %hd Page Wait: "
412 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
414 "Virtual Memory:\t\t(Total: %dK Active: %dK)\n",
415 v->t_vm * pageKilo, v->t_avm * pageKilo);
416 printf("Real Memory:\t\t(Total: %dK Active: %dK)\n",
417 v->t_rm * pageKilo, v->t_arm * pageKilo);
418 printf("Shared Virtual Memory:\t(Total: %dK Active: %dK)\n",
419 v->t_vmshr * pageKilo, v->t_avmshr * pageKilo);
420 printf("Shared Real Memory:\t(Total: %dK Active: %dK)\n",
421 v->t_rmshr * pageKilo, v->t_armshr * pageKilo);
422 printf("Free Memory Pages:\t%dK\n", v->t_free * pageKilo);
428 set_IK(const char *str, int *val)
435 if ((len = strlen(str)) == 0)
438 if (*p == 'C' || *p == 'F') {
439 temp = strtof(str, &endptr);
440 if (endptr == str || endptr != p)
443 temp = (temp - 32) * 5 / 9;
444 kelv = temp * 10 + 2732;
446 kelv = (int)strtol(str, &endptr, 10);
447 if (endptr == str || *endptr != '\0')
455 * These functions uses a presently undocumented interface to the kernel
456 * to walk the tree and get the type so it can print the value.
457 * This interface is under work and consideration, and should probably
458 * be killed with a big axe by the first person who can find the time.
459 * (be aware though, that the proper interface isn't as obvious as it
460 * may seem, there are various conflicting requirements.
464 name2oid(char *name, int *oidp)
473 j = CTL_MAXNAME * sizeof(int);
474 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
482 oidfmt(int *oid, int len, char *fmt, u_int *kind)
484 int qoid[CTL_MAXNAME+2];
491 memcpy(qoid + 2, oid, len * sizeof(int));
494 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
496 err(1, "sysctl fmt %d %zu %d", i, j, errno);
499 *kind = *(u_int *)buf;
502 strcpy(fmt, (char *)(buf + sizeof(u_int)));
506 static int ctl_sign[CTLTYPE+1] = {
512 static int ctl_size[CTLTYPE+1] = {
513 [CTLTYPE_INT] = sizeof(int),
514 [CTLTYPE_UINT] = sizeof(u_int),
515 [CTLTYPE_LONG] = sizeof(long),
516 [CTLTYPE_ULONG] = sizeof(u_long),
517 [CTLTYPE_S64] = sizeof(int64_t),
518 [CTLTYPE_U64] = sizeof(int64_t),
522 * This formats and outputs the value of one variable
524 * Returns zero if anything was actually output.
525 * Returns one if didn't know what to do with this.
526 * Return minus one if we had errors.
529 show_var(int *oid, int nlen)
531 u_char buf[BUFSIZ], *val, *oval, *p;
532 char name[BUFSIZ], *fmt;
533 const char *sep, *sep1;
534 int qoid[CTL_MAXNAME+2];
537 int i, hexlen, sign, ctltype;
541 int (*func)(int, void *);
544 umv = mv = intlen = 0;
549 memcpy(qoid + 2, oid, nlen * sizeof(int));
553 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
555 err(1, "sysctl name %d %zu %d", i, j, errno);
567 if (dflag) { /* just print description */
570 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
572 printf("%s%s", name, sep);
576 /* find an estimate of how much we need for this var */
578 i = sysctl(oid, nlen, 0, &j, 0, 0);
579 j += j; /* we want to be sure :-) */
581 val = oval = malloc(j + 1);
583 warnx("malloc failed");
587 i = sysctl(oid, nlen, val, &len, 0, 0);
594 fwrite(val, 1, len, stdout);
600 oidfmt(oid, nlen, fmt, &kind);
602 ctltype = (kind & CTLTYPE);
603 sign = ctl_sign[ctltype];
604 intlen = ctl_size[ctltype];
609 printf("%s%s", name, sep);
610 printf("%.*s", (int)len, p);
621 printf("%s%s", name, sep);
622 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
624 while (len >= intlen) {
625 switch (kind & CTLTYPE) {
638 umv = *(uint64_t *)p;
644 printf("%#0*jx", hexlen, umv);
646 printf(hflag ? "%'ju" : "%ju", umv);
647 else if (fmt[1] == 'K') {
651 printf("%.1fC", (mv - 2732.0) / 10);
653 printf(hflag ? "%'jd" : "%jd", mv);
663 if (strcmp(fmt, "S,clockinfo") == 0)
665 else if (strcmp(fmt, "S,timeval") == 0)
667 else if (strcmp(fmt, "S,loadavg") == 0)
669 else if (strcmp(fmt, "S,vmtotal") == 0)
675 printf("%s%s", name, sep);
682 if (!oflag && !xflag) {
687 printf("%s%s", name, sep);
688 printf("Format:%s Length:%zu Dump:0x", fmt, len);
689 while (len-- && (xflag || p < val + 16))
690 printf("%02x", *p++);
691 if (!xflag && len > 16)
701 sysctl_all(int *oid, int len)
703 int name1[22], name2[22];
711 memcpy(name1+2, oid, len * sizeof(int));
719 j = sysctl(name1, l1, name2, &l2, 0, 0);
724 err(1, "sysctl(getnext) %d %zu", j, l2);
729 if (len < 0 || l2 < (unsigned int)len)
732 for (i = 0; i < len; i++)
733 if (name2[i] != oid[i])
736 i = show_var(name2, l2);
740 memcpy(name1+2, name2, l2 * sizeof(int));