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(const 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.
164 parse(const char *string)
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 bufp = strsep(&cp, "=");
187 newsize = strlen(cp);
189 len = name2oid(bufp, mib);
197 errx(1, "unknown oid '%s'", bufp);
200 if (oidfmt(mib, len, fmt, &kind))
201 err(1, "couldn't find format of oid '%s'", bufp);
203 if (newval == NULL || dflag) {
204 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
206 i = show_var(mib, len);
210 sysctl_all(mib, len);
212 i = show_var(mib, len);
217 if ((kind & CTLTYPE) == CTLTYPE_NODE)
218 errx(1, "oid '%s' isn't a leaf node", bufp);
220 if (!(kind & CTLFLAG_WR)) {
221 if (kind & CTLFLAG_TUN) {
222 warnx("oid '%s' is a read only tunable", bufp);
223 errx(1, "Tunable values are set in /boot/loader.conf");
225 errx(1, "oid '%s' is read only", bufp);
229 if ((kind & CTLTYPE) == CTLTYPE_INT ||
230 (kind & CTLTYPE) == CTLTYPE_UINT ||
231 (kind & CTLTYPE) == CTLTYPE_LONG ||
232 (kind & CTLTYPE) == CTLTYPE_ULONG ||
233 (kind & CTLTYPE) == CTLTYPE_S64 ||
234 (kind & CTLTYPE) == CTLTYPE_U64) {
235 if (strlen(newval) == 0)
236 errx(1, "empty numeric value");
239 switch (kind & CTLTYPE) {
241 if (strcmp(fmt, "IK") == 0) {
242 if (!set_IK(newval, &intval))
243 errx(1, "invalid value '%s'",
246 intval = (int)strtol(newval, &endptr,
248 if (endptr == newval || *endptr != '\0')
249 errx(1, "invalid integer '%s'",
253 newsize = sizeof(intval);
256 uintval = (int) strtoul(newval, &endptr, 0);
257 if (endptr == newval || *endptr != '\0')
258 errx(1, "invalid unsigned integer '%s'",
261 newsize = sizeof(uintval);
264 longval = strtol(newval, &endptr, 0);
265 if (endptr == newval || *endptr != '\0')
266 errx(1, "invalid long integer '%s'",
269 newsize = sizeof(longval);
272 ulongval = strtoul(newval, &endptr, 0);
273 if (endptr == newval || *endptr != '\0')
274 errx(1, "invalid unsigned long integer"
275 " '%s'", (char *)newval);
277 newsize = sizeof(ulongval);
282 i64val = strtoimax(newval, &endptr, 0);
283 if (endptr == newval || *endptr != '\0')
284 errx(1, "invalid int64_t '%s'",
287 newsize = sizeof(i64val);
290 u64val = strtoumax(newval, &endptr, 0);
291 if (endptr == newval || *endptr != '\0')
292 errx(1, "invalid uint64_t '%s'",
295 newsize = sizeof(u64val);
300 errx(1, "oid '%s' is type %d,"
301 " cannot set that", bufp,
305 i = show_var(mib, len);
306 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
311 errx(1, "%s: value is not available",
314 errx(1, "%s: specification is incomplete",
317 errx(1, "%s: type is unknown to this program",
329 j = show_var(mib, len);
336 /* These functions will dump out various interesting structures. */
339 S_clockinfo(int l2, void *p)
341 struct clockinfo *ci = (struct clockinfo*)p;
343 if (l2 != sizeof(*ci)) {
344 warnx("S_clockinfo %d != %zu", l2, sizeof(*ci));
347 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
348 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
349 ci->hz, ci->tick, ci->profhz, ci->stathz);
354 S_loadavg(int l2, void *p)
356 struct loadavg *tv = (struct loadavg*)p;
358 if (l2 != sizeof(*tv)) {
359 warnx("S_loadavg %d != %zu", l2, sizeof(*tv));
362 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
363 (double)tv->ldavg[0]/(double)tv->fscale,
364 (double)tv->ldavg[1]/(double)tv->fscale,
365 (double)tv->ldavg[2]/(double)tv->fscale);
370 S_timeval(int l2, void *p)
372 struct timeval *tv = (struct timeval*)p;
376 if (l2 != sizeof(*tv)) {
377 warnx("S_timeval %d != %zu", l2, sizeof(*tv));
380 printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
381 "{ sec = %jd, usec = %ld } ",
382 (intmax_t)tv->tv_sec, tv->tv_usec);
384 p1 = strdup(ctime(&tv_sec));
385 for (p2=p1; *p2 ; p2++)
394 S_vmtotal(int l2, void *p)
396 struct vmtotal *v = (struct vmtotal *)p;
397 int pageKilo = getpagesize() / 1024;
399 if (l2 != sizeof(*v)) {
400 warnx("S_vmtotal %d != %zu", l2, sizeof(*v));
405 "\nSystem wide totals computed every five seconds:"
406 " (values in kilobytes)\n");
407 printf("===============================================\n");
409 "Processes:\t\t(RUNQ: %hd Disk Wait: %hd Page Wait: "
411 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
413 "Virtual Memory:\t\t(Total: %dK Active: %dK)\n",
414 v->t_vm * pageKilo, v->t_avm * pageKilo);
415 printf("Real Memory:\t\t(Total: %dK Active: %dK)\n",
416 v->t_rm * pageKilo, v->t_arm * pageKilo);
417 printf("Shared Virtual Memory:\t(Total: %dK Active: %dK)\n",
418 v->t_vmshr * pageKilo, v->t_avmshr * pageKilo);
419 printf("Shared Real Memory:\t(Total: %dK Active: %dK)\n",
420 v->t_rmshr * pageKilo, v->t_armshr * pageKilo);
421 printf("Free Memory:\t%dK\n", v->t_free * pageKilo);
427 set_IK(const char *str, int *val)
434 if ((len = strlen(str)) == 0)
437 if (*p == 'C' || *p == 'F') {
438 temp = strtof(str, &endptr);
439 if (endptr == str || endptr != p)
442 temp = (temp - 32) * 5 / 9;
443 kelv = temp * 10 + 2732;
445 kelv = (int)strtol(str, &endptr, 10);
446 if (endptr == str || *endptr != '\0')
454 * These functions uses a presently undocumented interface to the kernel
455 * to walk the tree and get the type so it can print the value.
456 * This interface is under work and consideration, and should probably
457 * be killed with a big axe by the first person who can find the time.
458 * (be aware though, that the proper interface isn't as obvious as it
459 * may seem, there are various conflicting requirements.
463 name2oid(char *name, int *oidp)
472 j = CTL_MAXNAME * sizeof(int);
473 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
481 oidfmt(int *oid, int len, char *fmt, u_int *kind)
483 int qoid[CTL_MAXNAME+2];
490 memcpy(qoid + 2, oid, len * sizeof(int));
493 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
495 err(1, "sysctl fmt %d %zu %d", i, j, errno);
498 *kind = *(u_int *)buf;
501 strcpy(fmt, (char *)(buf + sizeof(u_int)));
505 static int ctl_sign[CTLTYPE+1] = {
511 static int ctl_size[CTLTYPE+1] = {
512 [CTLTYPE_INT] = sizeof(int),
513 [CTLTYPE_UINT] = sizeof(u_int),
514 [CTLTYPE_LONG] = sizeof(long),
515 [CTLTYPE_ULONG] = sizeof(u_long),
516 [CTLTYPE_S64] = sizeof(int64_t),
517 [CTLTYPE_U64] = sizeof(int64_t),
521 * This formats and outputs the value of one variable
523 * Returns zero if anything was actually output.
524 * Returns one if didn't know what to do with this.
525 * Return minus one if we had errors.
528 show_var(int *oid, int nlen)
530 u_char buf[BUFSIZ], *val, *oval, *p;
531 char name[BUFSIZ], *fmt;
532 const char *sep, *sep1;
533 int qoid[CTL_MAXNAME+2];
536 int i, hexlen, sign, ctltype;
540 int (*func)(int, void *);
543 umv = mv = intlen = 0;
548 memcpy(qoid + 2, oid, nlen * sizeof(int));
552 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
554 err(1, "sysctl name %d %zu %d", i, j, errno);
566 if (dflag) { /* just print description */
569 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
571 printf("%s%s", name, sep);
575 /* find an estimate of how much we need for this var */
577 i = sysctl(oid, nlen, 0, &j, 0, 0);
578 j += j; /* we want to be sure :-) */
580 val = oval = malloc(j + 1);
582 warnx("malloc failed");
586 i = sysctl(oid, nlen, val, &len, 0, 0);
593 fwrite(val, 1, len, stdout);
599 oidfmt(oid, nlen, fmt, &kind);
601 ctltype = (kind & CTLTYPE);
602 sign = ctl_sign[ctltype];
603 intlen = ctl_size[ctltype];
608 printf("%s%s", name, sep);
609 printf("%.*s", (int)len, p);
620 printf("%s%s", name, sep);
621 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
623 while (len >= intlen) {
624 switch (kind & CTLTYPE) {
637 umv = *(uint64_t *)p;
643 printf("%#0*jx", hexlen, umv);
645 printf(hflag ? "%'ju" : "%ju", umv);
646 else if (fmt[1] == 'K') {
650 printf("%.1fC", (mv - 2732.0) / 10);
652 printf(hflag ? "%'jd" : "%jd", mv);
662 if (strcmp(fmt, "S,clockinfo") == 0)
664 else if (strcmp(fmt, "S,timeval") == 0)
666 else if (strcmp(fmt, "S,loadavg") == 0)
668 else if (strcmp(fmt, "S,vmtotal") == 0)
674 printf("%s%s", name, sep);
681 if (!oflag && !xflag) {
686 printf("%s%s", name, sep);
687 printf("Format:%s Length:%zu Dump:0x", fmt, len);
688 while (len-- && (xflag || p < val + 16))
689 printf("%02x", *p++);
690 if (!xflag && len > 16)
700 sysctl_all(int *oid, int len)
702 int name1[22], name2[22];
710 memcpy(name1+2, oid, len * sizeof(int));
718 j = sysctl(name1, l1, name2, &l2, 0, 0);
723 err(1, "sysctl(getnext) %d %zu", j, l2);
728 if (len < 0 || l2 < (unsigned int)len)
731 for (i = 0; i < len; i++)
732 if (name2[i] != oid[i])
735 i = show_var(name2, l2);
739 memcpy(name1+2, name2, l2 * sizeof(int));