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 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 [-bdehiNnoqx] name[=value] ...",
79 " sysctl [-bdehNnoqx] -a");
84 main(int argc, char **argv)
88 setlocale(LC_NUMERIC, "");
92 while ((ch = getopt(argc, argv, "AabdehiNnoqwxX")) != -1) {
148 if (aflag && argc == 0)
149 exit(sysctl_all(0, 0));
160 * Parse a name into a MIB entry.
161 * Lookup and print out the MIB entry if it exists.
162 * Set a new value if requested.
170 unsigned int uintval;
172 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) {
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_QUAD) {
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 sscanf(newval, "%qd", &quadval);
284 newsize = sizeof(quadval);
287 if (strcmp(fmt, "T,dev_t") == 0) {
288 set_T_dev_t (newval, &newval, &newsize);
293 errx(1, "oid '%s' is type %d,"
294 " cannot set that", bufp,
298 i = show_var(mib, len);
299 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
304 errx(1, "%s: value is not available",
307 errx(1, "%s: specification is incomplete",
310 errx(1, "%s: type is unknown to this program",
322 j = show_var(mib, len);
329 /* These functions will dump out various interesting structures. */
332 S_clockinfo(int l2, void *p)
334 struct clockinfo *ci = (struct clockinfo*)p;
336 if (l2 != sizeof(*ci)) {
337 warnx("S_clockinfo %d != %d", l2, sizeof(*ci));
340 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
341 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
342 ci->hz, ci->tick, ci->profhz, ci->stathz);
347 S_loadavg(int l2, void *p)
349 struct loadavg *tv = (struct loadavg*)p;
351 if (l2 != sizeof(*tv)) {
352 warnx("S_loadavg %d != %d", l2, sizeof(*tv));
355 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
356 (double)tv->ldavg[0]/(double)tv->fscale,
357 (double)tv->ldavg[1]/(double)tv->fscale,
358 (double)tv->ldavg[2]/(double)tv->fscale);
363 S_timeval(int l2, void *p)
365 struct timeval *tv = (struct timeval*)p;
369 if (l2 != sizeof(*tv)) {
370 warnx("S_timeval %d != %d", l2, sizeof(*tv));
373 printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
374 "{ sec = %jd, usec = %ld } ",
375 (intmax_t)tv->tv_sec, tv->tv_usec);
377 p1 = strdup(ctime(&tv_sec));
378 for (p2=p1; *p2 ; p2++)
387 S_vmtotal(int l2, void *p)
389 struct vmtotal *v = (struct vmtotal *)p;
390 int pageKilo = getpagesize() / 1024;
392 if (l2 != sizeof(*v)) {
393 warnx("S_vmtotal %d != %d", l2, sizeof(*v));
398 "\nSystem wide totals computed every five seconds:"
399 " (values in kilobytes)\n");
400 printf("===============================================\n");
402 "Processes:\t\t(RUNQ: %hd Disk Wait: %hd Page Wait: "
404 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
406 "Virtual Memory:\t\t(Total: %dK Active: %dK)\n",
407 v->t_vm * pageKilo, v->t_avm * pageKilo);
408 printf("Real Memory:\t\t(Total: %dK Active: %dK)\n",
409 v->t_rm * pageKilo, v->t_arm * pageKilo);
410 printf("Shared Virtual Memory:\t(Total: %dK Active: %dK)\n",
411 v->t_vmshr * pageKilo, v->t_avmshr * pageKilo);
412 printf("Shared Real Memory:\t(Total: %dK Active: %dK)\n",
413 v->t_rmshr * pageKilo, v->t_armshr * pageKilo);
414 printf("Free Memory Pages:\t%dK\n", v->t_free * pageKilo);
420 T_dev_t(int l2, void *p)
422 dev_t *d = (dev_t *)p;
424 if (l2 != sizeof(*d)) {
425 warnx("T_dev_T %d != %d", l2, sizeof(*d));
428 if ((int)(*d) != -1) {
429 if (minor(*d) > 255 || minor(*d) < 0)
430 printf("{ major = %d, minor = 0x%x }",
431 major(*d), minor(*d));
433 printf("{ major = %d, minor = %d }",
434 major(*d), minor(*d));
440 set_T_dev_t(char *path, void **val, size_t *size)
442 static struct stat statb;
444 if (strcmp(path, "none") && strcmp(path, "off")) {
445 int rc = stat (path, &statb);
447 err(1, "cannot stat %s", path);
450 if (!S_ISCHR(statb.st_mode)) {
451 errx(1, "must specify a device special file.");
454 statb.st_rdev = NODEV;
456 *val = (void *) &statb.st_rdev;
457 *size = sizeof(statb.st_rdev);
461 set_IK(char *str, int *val)
467 if ((len = strlen(str)) == 0)
470 if (*p == 'C' || *p == 'F') {
472 temp = strtof(str, &endptr);
473 if (endptr == str || *endptr != '\0')
476 temp = (temp - 32) * 5 / 9;
477 kelv = temp * 10 + 2732;
479 kelv = (int)strtol(str, &endptr, 10);
480 if (endptr == str || *endptr != '\0')
488 * These functions uses a presently undocumented interface to the kernel
489 * to walk the tree and get the type so it can print the value.
490 * This interface is under work and consideration, and should probably
491 * be killed with a big axe by the first person who can find the time.
492 * (be aware though, that the proper interface isn't as obvious as it
493 * may seem, there are various conflicting requirements.
497 name2oid(char *name, int *oidp)
506 j = CTL_MAXNAME * sizeof(int);
507 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
515 oidfmt(int *oid, int len, char *fmt, u_int *kind)
517 int qoid[CTL_MAXNAME+2];
524 memcpy(qoid + 2, oid, len * sizeof(int));
527 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
529 err(1, "sysctl fmt %d %d %d", i, j, errno);
532 *kind = *(u_int *)buf;
535 strcpy(fmt, (char *)(buf + sizeof(u_int)));
540 * This formats and outputs the value of one variable
542 * Returns zero if anything was actually output.
543 * Returns one if didn't know what to do with this.
544 * Return minus one if we had errors.
548 show_var(int *oid, int nlen)
550 u_char buf[BUFSIZ], *val, *oval, *p;
551 char name[BUFSIZ], *fmt;
552 const char *sep, *sep1;
553 int qoid[CTL_MAXNAME+2];
560 int (*func)(int, void *);
565 memcpy(qoid + 2, oid, nlen * sizeof(int));
569 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
571 err(1, "sysctl name %d %d %d", i, j, errno);
583 if (dflag) { /* just print description */
586 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
588 printf("%s%s", name, sep);
592 /* find an estimate of how much we need for this var */
594 i = sysctl(oid, nlen, 0, &j, 0, 0);
595 j += j; /* we want to be sure :-) */
597 val = oval = malloc(j + 1);
599 warnx("malloc failed");
603 i = sysctl(oid, nlen, val, &len, 0, 0);
610 fwrite(val, 1, len, stdout);
616 oidfmt(oid, nlen, fmt, &kind);
621 printf("%s%s", name, sep);
622 printf("%.*s", len, p);
630 printf("%s%s", name, sep);
632 case 'I': intlen = sizeof(int); break;
633 case 'L': intlen = sizeof(long); break;
634 case 'Q': intlen = sizeof(quad_t); break;
636 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
638 while (len >= intlen) {
649 umv = *(u_quad_t *)p;
655 printf(hflag ? "%'ju" : "%ju", umv);
656 else if (fmt[1] == 'X')
657 printf("%#0*jx", hexlen, umv);
658 else if (fmt[1] == 'K') {
662 printf("%.1fC", (mv - 2732.0) / 10);
664 printf(hflag ? "%'jd" : "%jd", mv);
674 printf("%s%s", name, sep);
675 printf("%p", *(void **)p);
682 if (strcmp(fmt, "S,clockinfo") == 0)
684 else if (strcmp(fmt, "S,timeval") == 0)
686 else if (strcmp(fmt, "S,loadavg") == 0)
688 else if (strcmp(fmt, "S,vmtotal") == 0)
690 else if (strcmp(fmt, "T,dev_t") == 0)
696 printf("%s%s", name, sep);
703 if (!oflag && !xflag) {
708 printf("%s%s", name, sep);
709 printf("Format:%s Length:%d Dump:0x", fmt, len);
710 while (len-- && (xflag || p < val + 16))
711 printf("%02x", *p++);
712 if (!xflag && len > 16)
722 sysctl_all(int *oid, int len)
724 int name1[22], name2[22];
732 memcpy(name1+2, oid, len * sizeof(int));
740 j = sysctl(name1, l1, name2, &l2, 0, 0);
745 err(1, "sysctl(getnext) %d %d", j, l2);
750 if (len < 0 || l2 < (unsigned int)len)
753 for (i = 0; i < len; i++)
754 if (name2[i] != oid[i])
757 i = show_var(name2, l2);
761 memcpy(name1+2, name2, l2 * sizeof(int));