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, Nflag, nflag, oflag;
62 static int 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 void set_T_dev_t(char *, void **, size_t *);
71 static int set_IK(char *, int *);
77 (void)fprintf(stderr, "%s\n%s\n",
78 "usage: sysctl [-bdehNnoqx] name[=value] ...",
79 " sysctl [-bdehNnoqx] -a");
84 main(int argc, char **argv)
88 setlocale(LC_NUMERIC, "");
92 while ((ch = getopt(argc, argv, "AabdehNnoqwxX")) != -1) {
145 if (aflag && argc == 0)
146 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(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"
270 " '%s'", (char *)newval);
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 (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",
317 j = show_var(mib, len);
324 /* These functions will dump out various interesting structures. */
327 S_clockinfo(int l2, void *p)
329 struct clockinfo *ci = (struct clockinfo*)p;
331 if (l2 != sizeof(*ci)) {
332 warnx("S_clockinfo %d != %d", l2, sizeof(*ci));
335 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
336 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
337 ci->hz, ci->tick, ci->profhz, ci->stathz);
342 S_loadavg(int l2, void *p)
344 struct loadavg *tv = (struct loadavg*)p;
346 if (l2 != sizeof(*tv)) {
347 warnx("S_loadavg %d != %d", l2, sizeof(*tv));
350 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
351 (double)tv->ldavg[0]/(double)tv->fscale,
352 (double)tv->ldavg[1]/(double)tv->fscale,
353 (double)tv->ldavg[2]/(double)tv->fscale);
358 S_timeval(int l2, void *p)
360 struct timeval *tv = (struct timeval*)p;
364 if (l2 != sizeof(*tv)) {
365 warnx("S_timeval %d != %d", l2, sizeof(*tv));
368 printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
369 "{ sec = %jd, usec = %ld } ",
370 (intmax_t)tv->tv_sec, tv->tv_usec);
372 p1 = strdup(ctime(&tv_sec));
373 for (p2=p1; *p2 ; p2++)
381 S_vmtotal(int l2, void *p)
383 struct vmtotal *v = (struct vmtotal *)p;
384 int pageKilo = getpagesize() / 1024;
386 if (l2 != sizeof(*v)) {
387 warnx("S_vmtotal %d != %d", l2, sizeof(*v));
392 "\nSystem wide totals computed every five seconds:"
393 " (values in kilobytes)\n");
394 printf("===============================================\n");
396 "Processes:\t\t(RUNQ: %hd Disk Wait: %hd Page Wait: "
398 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
400 "Virtual Memory:\t\t(Total: %dK, Active %dK)\n",
401 v->t_vm * pageKilo, v->t_avm * pageKilo);
402 printf("Real Memory:\t\t(Total: %dK Active %dK)\n",
403 v->t_rm * pageKilo, v->t_arm * pageKilo);
404 printf("Shared Virtual Memory:\t(Total: %dK Active: %dK)\n",
405 v->t_vmshr * pageKilo, v->t_avmshr * pageKilo);
406 printf("Shared Real Memory:\t(Total: %dK Active: %dK)\n",
407 v->t_rmshr * pageKilo, v->t_armshr * pageKilo);
408 printf("Free Memory Pages:\t%dK\n", v->t_free * pageKilo);
414 T_dev_t(int l2, void *p)
416 dev_t *d = (dev_t *)p;
418 if (l2 != sizeof(*d)) {
419 warnx("T_dev_T %d != %d", l2, sizeof(*d));
422 if ((int)(*d) != -1) {
423 if (minor(*d) > 255 || minor(*d) < 0)
424 printf("{ major = %d, minor = 0x%x }",
425 major(*d), minor(*d));
427 printf("{ major = %d, minor = %d }",
428 major(*d), minor(*d));
434 set_T_dev_t(char *path, void **val, size_t *size)
436 static struct stat statb;
438 if (strcmp(path, "none") && strcmp(path, "off")) {
439 int rc = stat (path, &statb);
441 err(1, "cannot stat %s", path);
444 if (!S_ISCHR(statb.st_mode)) {
445 errx(1, "must specify a device special file.");
448 statb.st_rdev = NODEV;
450 *val = (void *) &statb.st_rdev;
451 *size = sizeof(statb.st_rdev);
455 set_IK(char *str, int *val)
461 if ((len = strlen(str)) == 0)
464 if (*p == 'C' || *p == 'F') {
466 temp = strtof(str, &endptr);
467 if (endptr == str || *endptr != '\0')
470 temp = (temp - 32) * 5 / 9;
471 kelv = temp * 10 + 2732;
473 kelv = (int)strtol(str, &endptr, 10);
474 if (endptr == str || *endptr != '\0')
482 * These functions uses a presently undocumented interface to the kernel
483 * to walk the tree and get the type so it can print the value.
484 * This interface is under work and consideration, and should probably
485 * be killed with a big axe by the first person who can find the time.
486 * (be aware though, that the proper interface isn't as obvious as it
487 * may seem, there are various conflicting requirements.
491 name2oid(char *name, int *oidp)
500 j = CTL_MAXNAME * sizeof(int);
501 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
509 oidfmt(int *oid, int len, char *fmt, u_int *kind)
511 int qoid[CTL_MAXNAME+2];
518 memcpy(qoid + 2, oid, len * sizeof(int));
521 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
523 err(1, "sysctl fmt %d %d %d", i, j, errno);
526 *kind = *(u_int *)buf;
529 strcpy(fmt, (char *)(buf + sizeof(u_int)));
534 * This formats and outputs the value of one variable
536 * Returns zero if anything was actually output.
537 * Returns one if didn't know what to do with this.
538 * Return minus one if we had errors.
542 show_var(int *oid, int nlen)
544 u_char buf[BUFSIZ], *val, *oval, *p;
545 char name[BUFSIZ], *fmt;
546 const char *sep, *sep1;
547 int qoid[CTL_MAXNAME+2];
554 int (*func)(int, void *);
559 memcpy(qoid + 2, oid, nlen * sizeof(int));
563 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
565 err(1, "sysctl name %d %d %d", i, j, errno);
577 if (dflag) { /* just print description */
580 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
582 printf("%s%s", name, sep);
586 /* find an estimate of how much we need for this var */
588 i = sysctl(oid, nlen, 0, &j, 0, 0);
589 j += j; /* we want to be sure :-) */
591 val = oval = malloc(j + 1);
593 warnx("malloc failed");
597 i = sysctl(oid, nlen, val, &len, 0, 0);
604 fwrite(val, 1, len, stdout);
610 oidfmt(oid, nlen, fmt, &kind);
615 printf("%s%s", name, sep);
616 printf("%.*s", len, p);
624 printf("%s%s", name, sep);
626 case 'I': intlen = sizeof(int); break;
627 case 'L': intlen = sizeof(long); break;
628 case 'Q': intlen = sizeof(quad_t); break;
630 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
632 while (len >= intlen) {
643 umv = *(u_quad_t *)p;
649 printf(hflag ? "%'ju" : "%ju", umv);
650 else if (fmt[1] == 'X')
651 printf("%#0*jx", hexlen, umv);
652 else if (fmt[1] == 'K') {
656 printf("%.1fC", (mv - 2732.0) / 10);
658 printf(hflag ? "%'jd" : "%jd", mv);
668 printf("%s%s", name, sep);
669 printf("%p", *(void **)p);
676 if (strcmp(fmt, "S,clockinfo") == 0)
678 else if (strcmp(fmt, "S,timeval") == 0)
680 else if (strcmp(fmt, "S,loadavg") == 0)
682 else if (strcmp(fmt, "S,vmtotal") == 0)
684 else if (strcmp(fmt, "T,dev_t") == 0)
690 printf("%s%s", name, sep);
697 if (!oflag && !xflag) {
702 printf("%s%s", name, sep);
703 printf("Format:%s Length:%d Dump:0x", fmt, len);
704 while (len-- && (xflag || p < val + 16))
705 printf("%02x", *p++);
706 if (!xflag && len > 16)
716 sysctl_all(int *oid, int len)
718 int name1[22], name2[22];
726 memcpy(name1+2, oid, len * sizeof(int));
734 j = sysctl(name1, l1, name2, &l2, 0, 0);
739 err(1, "sysctl(getnext) %d %d", j, l2);
744 if (len < 0 || l2 < (unsigned int)len)
747 for (i = 0; i < len; i++)
748 if (name2[i] != oid[i])
751 i = show_var(name2, l2);
755 memcpy(name1+2, name2, l2 * sizeof(int));