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;
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));
155 * Parse a name into a MIB entry.
156 * Lookup and print out the MIB entry if it exists.
157 * Set a new value if requested.
165 unsigned int uintval;
167 unsigned long ulongval;
170 int mib[CTL_MAXNAME];
171 char *cp, *bufp, buf[BUFSIZ], *endptr, fmt[BUFSIZ];
175 if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ)
176 errx(1, "oid too long: '%s'", string);
177 if ((cp = strchr(string, '=')) != NULL) {
178 *strchr(buf, '=') = '\0';
183 newsize = strlen(cp);
185 len = name2oid(bufp, mib);
191 errx(1, "unknown oid '%s'", bufp);
194 if (oidfmt(mib, len, fmt, &kind))
195 err(1, "couldn't find format of oid '%s'", bufp);
197 if (newval == NULL) {
198 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
200 i = show_var(mib, len);
204 sysctl_all(mib, len);
206 i = show_var(mib, len);
211 if ((kind & CTLTYPE) == CTLTYPE_NODE)
212 errx(1, "oid '%s' isn't a leaf node", bufp);
214 if (!(kind & CTLFLAG_WR)) {
215 if (kind & CTLFLAG_TUN) {
216 warnx("oid '%s' is a read only tunable", bufp);
217 errx(1, "Tunable values are set in /boot/loader.conf");
219 errx(1, "oid '%s' is read only", bufp);
223 if ((kind & CTLTYPE) == CTLTYPE_INT ||
224 (kind & CTLTYPE) == CTLTYPE_UINT ||
225 (kind & CTLTYPE) == CTLTYPE_LONG ||
226 (kind & CTLTYPE) == CTLTYPE_ULONG ||
227 (kind & CTLTYPE) == CTLTYPE_QUAD) {
228 if (strlen(newval) == 0)
229 errx(1, "empty numeric value");
232 switch (kind & CTLTYPE) {
234 if (strcmp(fmt, "IK") == 0) {
235 if (!set_IK(newval, &intval))
236 errx(1, "invalid value '%s'",
239 intval = (int)strtol(newval, &endptr,
241 if (endptr == newval || *endptr != '\0')
242 errx(1, "invalid integer '%s'",
246 newsize = sizeof(intval);
249 uintval = (int) strtoul(newval, &endptr, 0);
250 if (endptr == newval || *endptr != '\0')
251 errx(1, "invalid unsigned integer '%s'",
254 newsize = sizeof(uintval);
257 longval = strtol(newval, &endptr, 0);
258 if (endptr == newval || *endptr != '\0')
259 errx(1, "invalid long integer '%s'",
262 newsize = sizeof(longval);
265 ulongval = strtoul(newval, &endptr, 0);
266 if (endptr == newval || *endptr != '\0')
267 errx(1, "invalid unsigned long integer"
268 " '%s'", (char *)newval);
270 newsize = sizeof(ulongval);
275 sscanf(newval, "%qd", &quadval);
277 newsize = sizeof(quadval);
280 if (strcmp(fmt, "T,dev_t") == 0) {
281 set_T_dev_t (newval, &newval, &newsize);
286 errx(1, "oid '%s' is type %d,"
287 " cannot set that", bufp,
291 i = show_var(mib, len);
292 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
297 errx(1, "%s: value is not available",
300 errx(1, "%s: specification is incomplete",
303 errx(1, "%s: type is unknown to this program",
314 j = show_var(mib, len);
321 /* These functions will dump out various interesting structures. */
324 S_clockinfo(int l2, void *p)
326 struct clockinfo *ci = (struct clockinfo*)p;
328 if (l2 != sizeof(*ci)) {
329 warnx("S_clockinfo %d != %d", l2, sizeof(*ci));
332 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
333 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
334 ci->hz, ci->tick, ci->profhz, ci->stathz);
339 S_loadavg(int l2, void *p)
341 struct loadavg *tv = (struct loadavg*)p;
343 if (l2 != sizeof(*tv)) {
344 warnx("S_loadavg %d != %d", l2, sizeof(*tv));
347 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
348 (double)tv->ldavg[0]/(double)tv->fscale,
349 (double)tv->ldavg[1]/(double)tv->fscale,
350 (double)tv->ldavg[2]/(double)tv->fscale);
355 S_timeval(int l2, void *p)
357 struct timeval *tv = (struct timeval*)p;
361 if (l2 != sizeof(*tv)) {
362 warnx("S_timeval %d != %d", l2, sizeof(*tv));
365 printf(hflag ? "{ sec = %'ld, usec = %'ld } " :
366 "{ sec = %ld, usec = %ld } ",
367 tv->tv_sec, tv->tv_usec);
369 p1 = strdup(ctime(&tv_sec));
370 for (p2=p1; *p2 ; p2++)
378 S_vmtotal(int l2, void *p)
380 struct vmtotal *v = (struct vmtotal *)p;
381 int pageKilo = getpagesize() / 1024;
383 if (l2 != sizeof(*v)) {
384 warnx("S_vmtotal %d != %d", l2, sizeof(*v));
389 "\nSystem wide totals computed every five seconds:"
390 " (values in kilobytes)\n");
391 printf("===============================================\n");
393 "Processes:\t\t(RUNQ: %hd Disk Wait: %hd Page Wait: "
395 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
397 "Virtual Memory:\t\t(Total: %dK, Active %dK)\n",
398 v->t_vm * pageKilo, v->t_avm * pageKilo);
399 printf("Real Memory:\t\t(Total: %dK Active %dK)\n",
400 v->t_rm * pageKilo, v->t_arm * pageKilo);
401 printf("Shared Virtual Memory:\t(Total: %dK Active: %dK)\n",
402 v->t_vmshr * pageKilo, v->t_avmshr * pageKilo);
403 printf("Shared Real Memory:\t(Total: %dK Active: %dK)\n",
404 v->t_rmshr * pageKilo, v->t_armshr * pageKilo);
405 printf("Free Memory Pages:\t%dK\n", v->t_free * pageKilo);
411 T_dev_t(int l2, void *p)
413 dev_t *d = (dev_t *)p;
415 if (l2 != sizeof(*d)) {
416 warnx("T_dev_T %d != %d", l2, sizeof(*d));
419 if ((int)(*d) != -1) {
420 if (minor(*d) > 255 || minor(*d) < 0)
421 printf("{ major = %d, minor = 0x%x }",
422 major(*d), minor(*d));
424 printf("{ major = %d, minor = %d }",
425 major(*d), minor(*d));
431 set_T_dev_t(char *path, void **val, size_t *size)
433 static struct stat statb;
435 if (strcmp(path, "none") && strcmp(path, "off")) {
436 int rc = stat (path, &statb);
438 err(1, "cannot stat %s", path);
441 if (!S_ISCHR(statb.st_mode)) {
442 errx(1, "must specify a device special file.");
445 statb.st_rdev = NODEV;
447 *val = (void *) &statb.st_rdev;
448 *size = sizeof(statb.st_rdev);
452 set_IK(char *str, int *val)
458 if ((len = strlen(str)) == 0)
461 if (*p == 'C' || *p == 'F') {
463 temp = strtof(str, &endptr);
464 if (endptr == str || *endptr != '\0')
467 temp = (temp - 32) * 5 / 9;
468 kelv = temp * 10 + 2732;
470 kelv = (int)strtol(str, &endptr, 10);
471 if (endptr == str || *endptr != '\0')
479 * These functions uses a presently undocumented interface to the kernel
480 * to walk the tree and get the type so it can print the value.
481 * This interface is under work and consideration, and should probably
482 * be killed with a big axe by the first person who can find the time.
483 * (be aware though, that the proper interface isn't as obvious as it
484 * may seem, there are various conflicting requirements.
488 name2oid(char *name, int *oidp)
497 j = CTL_MAXNAME * sizeof(int);
498 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
506 oidfmt(int *oid, int len, char *fmt, u_int *kind)
508 int qoid[CTL_MAXNAME+2];
515 memcpy(qoid + 2, oid, len * sizeof(int));
518 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
520 err(1, "sysctl fmt %d %d %d", i, j, errno);
523 *kind = *(u_int *)buf;
526 strcpy(fmt, (char *)(buf + sizeof(u_int)));
531 * This formats and outputs the value of one variable
533 * Returns zero if anything was actually output.
534 * Returns one if didn't know what to do with this.
535 * Return minus one if we had errors.
539 show_var(int *oid, int nlen)
541 u_char buf[BUFSIZ], *val, *oval, *p;
542 char name[BUFSIZ], *fmt;
543 const char *sep, *sep1;
544 int qoid[CTL_MAXNAME+2];
551 int (*func)(int, void *);
556 memcpy(qoid + 2, oid, nlen * sizeof(int));
560 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
562 err(1, "sysctl name %d %d %d", i, j, errno);
574 if (dflag) { /* just print description */
577 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
579 printf("%s%s", name, sep);
583 /* find an estimate of how much we need for this var */
585 i = sysctl(oid, nlen, 0, &j, 0, 0);
586 j += j; /* we want to be sure :-) */
588 val = oval = malloc(j + 1);
590 warnx("malloc failed");
594 i = sysctl(oid, nlen, val, &len, 0, 0);
601 fwrite(val, 1, len, stdout);
607 oidfmt(oid, nlen, fmt, &kind);
612 printf("%s%s", name, sep);
613 printf("%.*s", len, p);
621 printf("%s%s", name, sep);
623 case 'I': intlen = sizeof(int); break;
624 case 'L': intlen = sizeof(long); break;
625 case 'Q': intlen = sizeof(quad_t); break;
627 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
629 while (len >= intlen) {
640 umv = *(u_quad_t *)p;
646 printf(hflag ? "%'ju" : "%ju", umv);
647 else if (fmt[1] == 'X')
648 printf("%#0*jx", hexlen, umv);
649 else if (fmt[1] == 'K') {
653 printf("%.1fC", (mv - 2732.0) / 10);
655 printf(hflag ? "%'jd" : "%jd", mv);
665 printf("%s%s", name, sep);
666 printf("%p", *(void **)p);
673 if (strcmp(fmt, "S,clockinfo") == 0)
675 else if (strcmp(fmt, "S,timeval") == 0)
677 else if (strcmp(fmt, "S,loadavg") == 0)
679 else if (strcmp(fmt, "S,vmtotal") == 0)
681 else if (strcmp(fmt, "T,dev_t") == 0)
687 printf("%s%s", name, sep);
694 if (!oflag && !xflag) {
699 printf("%s%s", name, sep);
700 printf("Format:%s Length:%d Dump:0x", fmt, len);
701 while (len-- && (xflag || p < val + 16))
702 printf("%02x", *p++);
703 if (!xflag && len > 16)
713 sysctl_all(int *oid, int len)
715 int name1[22], name2[22];
723 memcpy(name1+2, oid, len * sizeof(int));
731 j = sysctl(name1, l1, name2, &l2, 0, 0);
736 err(1, "sysctl(getnext) %d %d", j, l2);
741 if (len < 0 || l2 < (unsigned int)len)
744 for (i = 0; i < len; i++)
745 if (name2[i] != oid[i])
748 i = show_var(name2, l2);
752 memcpy(name1+2, name2, l2 * sizeof(int));