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[] =
45 #include <sys/reboot.h> /* used for bootdev parsing */
47 #include <sys/param.h>
49 #include <sys/resource.h>
51 #include <sys/sysctl.h>
52 #include <sys/vmmeter.h>
63 static int aflag, bflag, dflag, eflag, hflag, Nflag, nflag, oflag;
64 static int qflag, xflag;
66 static int oidfmt(int *, int, char *, u_int *);
67 static void parse(char *);
68 static int show_var(int *, int);
69 static int sysctl_all (int *oid, int len);
70 static int name2oid(char *, int *);
72 static void set_T_dev_t (char *, void **, size_t *);
78 (void)fprintf(stderr, "%s\n%s\n",
79 "usage: sysctl [-bdehNnoqx] name[=value] ...",
80 " sysctl [-bdehNnoqx] -a");
85 main(int argc, char **argv)
89 setlocale(LC_NUMERIC, "");
93 while ((ch = getopt(argc, argv, "AabdehNnoqwxX")) != -1) {
146 if (aflag && argc == 0)
147 exit(sysctl_all(0, 0));
156 * Parse a name into a MIB entry.
157 * Lookup and print out the MIB entry if it exists.
158 * Set a new value if requested.
166 unsigned int uintval;
168 unsigned long ulongval;
171 int mib[CTL_MAXNAME];
172 char *cp, *bufp, buf[BUFSIZ], *endptr, fmt[BUFSIZ];
176 if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ)
177 errx(1, "oid too long: '%s'", string);
178 if ((cp = strchr(string, '=')) != NULL) {
179 *strchr(buf, '=') = '\0';
184 newsize = strlen(cp);
186 len = name2oid(bufp, mib);
192 errx(1, "unknown oid '%s'", bufp);
195 if (oidfmt(mib, len, fmt, &kind))
196 err(1, "couldn't find format of oid '%s'", bufp);
198 if (newval == NULL) {
199 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
201 i = show_var(mib, len);
205 sysctl_all(mib, len);
207 i = show_var(mib, len);
212 if ((kind & CTLTYPE) == CTLTYPE_NODE)
213 errx(1, "oid '%s' isn't a leaf node", bufp);
215 if (!(kind & CTLFLAG_WR)) {
216 if (kind & CTLFLAG_TUN) {
217 warnx("oid '%s' is a read only tunable", bufp);
218 errx(1, "Tunable values are set in /boot/loader.conf");
220 errx(1, "oid '%s' is read only", bufp);
224 if ((kind & CTLTYPE) == CTLTYPE_INT ||
225 (kind & CTLTYPE) == CTLTYPE_UINT ||
226 (kind & CTLTYPE) == CTLTYPE_LONG ||
227 (kind & CTLTYPE) == CTLTYPE_ULONG ||
228 (kind & CTLTYPE) == CTLTYPE_QUAD) {
229 if (strlen(newval) == 0)
230 errx(1, "empty numeric value");
233 switch (kind & CTLTYPE) {
235 intval = (int)strtol(newval, &endptr, 0);
236 if (endptr == newval || *endptr != '\0')
237 errx(1, "invalid integer '%s'",
240 newsize = sizeof(intval);
243 uintval = (int) strtoul(newval, &endptr, 0);
244 if (endptr == newval || *endptr != '\0')
245 errx(1, "invalid unsigned integer '%s'",
248 newsize = sizeof uintval;
251 longval = strtol(newval, &endptr, 0);
252 if (endptr == newval || *endptr != '\0')
253 errx(1, "invalid long integer '%s'",
256 newsize = sizeof longval;
259 ulongval = strtoul(newval, &endptr, 0);
260 if (endptr == newval || *endptr != '\0')
261 errx(1, "invalid unsigned long integer"
264 newsize = sizeof ulongval;
269 sscanf(newval, "%qd", &quadval);
271 newsize = sizeof(quadval);
274 if (strcmp(fmt, "T,dev_t") == 0) {
275 set_T_dev_t ((char*)newval, &newval, &newsize);
280 errx(1, "oid '%s' is type %d,"
281 " cannot set that", bufp,
285 i = show_var(mib, len);
286 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
291 errx(1, "%s: value is not available",
294 errx(1, "%s: specification is incomplete",
297 errx(1, "%s: type is unknown to this program",
308 j = show_var(mib, len);
315 /* These functions will dump out various interesting structures. */
318 S_clockinfo(int l2, void *p)
320 struct clockinfo *ci = (struct clockinfo*)p;
321 if (l2 != sizeof(*ci)) {
322 warnx("S_clockinfo %d != %d", l2, sizeof(*ci));
325 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
326 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
327 ci->hz, ci->tick, ci->profhz, ci->stathz);
332 S_loadavg(int l2, void *p)
334 struct loadavg *tv = (struct loadavg*)p;
336 if (l2 != sizeof(*tv)) {
337 warnx("S_loadavg %d != %d", l2, sizeof(*tv));
340 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
341 (double)tv->ldavg[0]/(double)tv->fscale,
342 (double)tv->ldavg[1]/(double)tv->fscale,
343 (double)tv->ldavg[2]/(double)tv->fscale);
348 S_timeval(int l2, void *p)
350 struct timeval *tv = (struct timeval*)p;
354 if (l2 != sizeof(*tv)) {
355 warnx("S_timeval %d != %d", l2, sizeof(*tv));
358 printf(hflag ? "{ sec = %'ld, usec = %'ld } " :
359 "{ sec = %ld, usec = %ld } ",
360 tv->tv_sec, tv->tv_usec);
362 p1 = strdup(ctime(&tv_sec));
363 for (p2=p1; *p2 ; p2++)
371 S_vmtotal(int l2, void *p)
373 struct vmtotal *v = (struct vmtotal *)p;
374 int pageKilo = getpagesize() / 1024;
376 if (l2 != sizeof(*v)) {
377 warnx("S_vmtotal %d != %d", l2, sizeof(*v));
382 "\nSystem wide totals computed every five seconds:"
383 " (values in kilobytes)\n");
384 printf("===============================================\n");
386 "Processes:\t\t(RUNQ: %hu Disk Wait: %hu Page Wait: "
388 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
390 "Virtual Memory:\t\t(Total: %luK, Active %lldK)\n",
391 (unsigned long)v->t_vm / 1024,
392 (long long)v->t_avm * pageKilo);
393 printf("Real Memory:\t\t(Total: %lldK Active %lldK)\n",
394 (long long)v->t_rm * pageKilo, (long long)v->t_arm * pageKilo);
395 printf("Shared Virtual Memory:\t(Total: %lldK Active: %lldK)\n",
396 (long long)v->t_vmshr * pageKilo,
397 (long long)v->t_avmshr * pageKilo);
398 printf("Shared Real Memory:\t(Total: %lldK Active: %lldK)\n",
399 (long long)v->t_rmshr * pageKilo,
400 (long long)v->t_armshr * pageKilo);
401 printf("Free Memory Pages:\t%lldK\n", (long long)v->t_free * pageKilo);
407 T_dev_t(int l2, void *p)
409 dev_t *d = (dev_t *)p;
410 if (l2 != sizeof(*d)) {
411 warnx("T_dev_T %d != %d", l2, sizeof(*d));
414 if ((int)(*d) != -1) {
415 if (minor(*d) > 255 || minor(*d) < 0)
416 printf("{ major = %d, minor = 0x%x }",
417 major(*d), minor(*d));
419 printf("{ major = %d, minor = %d }",
420 major(*d), minor(*d));
426 set_T_dev_t (char *path, void **val, size_t *size)
428 static struct stat statb;
430 if (strcmp(path, "none") && strcmp(path, "off")) {
431 int rc = stat (path, &statb);
433 err(1, "cannot stat %s", path);
436 if (!S_ISCHR(statb.st_mode)) {
437 errx(1, "must specify a device special file.");
440 statb.st_rdev = NODEV;
442 *val = (char*) &statb.st_rdev;
443 *size = sizeof statb.st_rdev;
447 * These functions uses a presently undocumented interface to the kernel
448 * to walk the tree and get the type so it can print the value.
449 * This interface is under work and consideration, and should probably
450 * be killed with a big axe by the first person who can find the time.
451 * (be aware though, that the proper interface isn't as obvious as it
452 * may seem, there are various conflicting requirements.
456 name2oid(char *name, int *oidp)
465 j = CTL_MAXNAME * sizeof(int);
466 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
474 oidfmt(int *oid, int len, char *fmt, u_int *kind)
476 int qoid[CTL_MAXNAME+2];
483 memcpy(qoid + 2, oid, len * sizeof(int));
486 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
488 err(1, "sysctl fmt %d %d %d", i, j, errno);
491 *kind = *(u_int *)buf;
494 strcpy(fmt, (char *)(buf + sizeof(u_int)));
499 * This formats and outputs the value of one variable
501 * Returns zero if anything was actually output.
502 * Returns one if didn't know what to do with this.
503 * Return minus one if we had errors.
507 show_var(int *oid, int nlen)
509 u_char buf[BUFSIZ], *val, *p;
510 char name[BUFSIZ], *fmt, *sep;
511 int qoid[CTL_MAXNAME+2];
515 int (*func)(int, void *);
520 memcpy(qoid + 2, oid, nlen * sizeof(int));
524 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
526 err(1, "sysctl name %d %d %d", i, j, errno);
538 if (dflag) { /* just print description */
541 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
543 printf("%s%s", name, sep);
547 /* find an estimate of how much we need for this var */
549 i = sysctl(oid, nlen, 0, &j, 0, 0);
550 j += j; /* we want to be sure :-) */
554 i = sysctl(oid, nlen, val, &len, 0, 0);
559 fwrite(val, 1, len, stdout);
564 oidfmt(oid, nlen, fmt, &kind);
569 printf("%s%s", name, sep);
570 printf("%.*s", len, p);
575 printf("%s%s", name, sep);
578 while (len >= sizeof(int)) {
581 printf(hflag ? "%'u" : "%u",
583 else if (*fmt == 'X')
584 printf(hflag ? "%'#010x" : "%#010x",
586 else if (*fmt == 'K') {
588 printf("%d", *(int *)p);
591 (*(int *)p - 2732.0) / 10);
593 printf(hflag ? "%'d" : "%d", *(int *)p);
602 printf("%s%s", name, sep);
605 while (len >= sizeof(long)) {
608 printf(hflag ? "%'lu" : "%lu",
609 *(unsigned long *)p);
610 else if (*fmt == 'X')
611 printf(hflag ? "%'#018lx" : "%#018lx",
612 *(unsigned long *)p);
613 else if (*fmt == 'K') {
615 printf("%ld", *(long *)p);
618 (*(long *)p - 2732.0) / 10);
620 printf(hflag ? "%'ld" : "%ld", *(long *)p);
629 printf("%s%s", name, sep);
630 printf("%p", *(void **)p);
636 if (strcmp(fmt, "S,clockinfo") == 0)
638 else if (strcmp(fmt, "S,timeval") == 0)
640 else if (strcmp(fmt, "S,loadavg") == 0)
642 else if (strcmp(fmt, "S,vmtotal") == 0)
644 else if (strcmp(fmt, "T,dev_t") == 0)
650 printf("%s%s", name, sep);
651 return ((*func)(len, p));
655 if (!oflag && !xflag)
658 printf("%s%s", name, sep);
659 printf("Format:%s Length:%d Dump:0x", fmt, len);
660 while (len-- && (xflag || p < val + 16))
661 printf("%02x", *p++);
662 if (!xflag && len > 16)
670 sysctl_all (int *oid, int len)
672 int name1[22], name2[22];
680 memcpy(name1+2, oid, len * sizeof(int));
688 j = sysctl(name1, l1, name2, &l2, 0, 0);
693 err(1, "sysctl(getnext) %d %d", j, l2);
701 for (i = 0; i < len; i++)
702 if (name2[i] != oid[i])
705 i = show_var(name2, l2);
709 memcpy(name1+2, name2, l2 * sizeof(int));