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 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.
165 parse(const char *string)
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 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_QUAD) {
234 if (strlen(newval) == 0)
235 errx(1, "empty numeric value");
238 switch (kind & CTLTYPE) {
240 if (strcmp(fmt, "IK") == 0) {
241 if (!set_IK(newval, &intval))
242 errx(1, "invalid value '%s'",
245 intval = (int)strtol(newval, &endptr,
247 if (endptr == newval || *endptr != '\0')
248 errx(1, "invalid integer '%s'",
252 newsize = sizeof(intval);
255 uintval = (int) strtoul(newval, &endptr, 0);
256 if (endptr == newval || *endptr != '\0')
257 errx(1, "invalid unsigned integer '%s'",
260 newsize = sizeof(uintval);
263 longval = strtol(newval, &endptr, 0);
264 if (endptr == newval || *endptr != '\0')
265 errx(1, "invalid long integer '%s'",
268 newsize = sizeof(longval);
271 ulongval = strtoul(newval, &endptr, 0);
272 if (endptr == newval || *endptr != '\0')
273 errx(1, "invalid unsigned long integer"
274 " '%s'", (char *)newval);
276 newsize = sizeof(ulongval);
281 sscanf(newval, "%qd", &quadval);
283 newsize = sizeof(quadval);
286 if (strcmp(fmt, "T,dev_t") == 0) {
287 set_T_dev_t (newval, &newval, &newsize);
292 errx(1, "oid '%s' is type %d,"
293 " cannot set that", bufp,
297 i = show_var(mib, len);
298 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
303 errx(1, "%s: value is not available",
306 errx(1, "%s: specification is incomplete",
309 errx(1, "%s: type is unknown to this program",
321 j = show_var(mib, len);
328 /* These functions will dump out various interesting structures. */
331 S_clockinfo(int l2, void *p)
333 struct clockinfo *ci = (struct clockinfo*)p;
335 if (l2 != sizeof(*ci)) {
336 warnx("S_clockinfo %d != %d", l2, sizeof(*ci));
339 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
340 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
341 ci->hz, ci->tick, ci->profhz, ci->stathz);
346 S_loadavg(int l2, void *p)
348 struct loadavg *tv = (struct loadavg*)p;
350 if (l2 != sizeof(*tv)) {
351 warnx("S_loadavg %d != %d", l2, sizeof(*tv));
354 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
355 (double)tv->ldavg[0]/(double)tv->fscale,
356 (double)tv->ldavg[1]/(double)tv->fscale,
357 (double)tv->ldavg[2]/(double)tv->fscale);
362 S_timeval(int l2, void *p)
364 struct timeval *tv = (struct timeval*)p;
368 if (l2 != sizeof(*tv)) {
369 warnx("S_timeval %d != %d", l2, sizeof(*tv));
372 printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
373 "{ sec = %jd, usec = %ld } ",
374 (intmax_t)tv->tv_sec, tv->tv_usec);
376 p1 = strdup(ctime(&tv_sec));
377 for (p2=p1; *p2 ; p2++)
386 S_vmtotal(int l2, void *p)
388 struct vmtotal *v = (struct vmtotal *)p;
389 int pageKilo = getpagesize() / 1024;
391 if (l2 != sizeof(*v)) {
392 warnx("S_vmtotal %d != %d", l2, sizeof(*v));
397 "\nSystem wide totals computed every five seconds:"
398 " (values in kilobytes)\n");
399 printf("===============================================\n");
401 "Processes:\t\t(RUNQ: %hd Disk Wait: %hd Page Wait: "
403 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
405 "Virtual Memory:\t\t(Total: %dK Active: %dK)\n",
406 v->t_vm * pageKilo, v->t_avm * pageKilo);
407 printf("Real Memory:\t\t(Total: %dK Active: %dK)\n",
408 v->t_rm * pageKilo, v->t_arm * pageKilo);
409 printf("Shared Virtual Memory:\t(Total: %dK Active: %dK)\n",
410 v->t_vmshr * pageKilo, v->t_avmshr * pageKilo);
411 printf("Shared Real Memory:\t(Total: %dK Active: %dK)\n",
412 v->t_rmshr * pageKilo, v->t_armshr * pageKilo);
413 printf("Free Memory:\t%dK\n", v->t_free * pageKilo);
419 T_dev_t(int l2, void *p)
421 dev_t *d = (dev_t *)p;
423 if (l2 != sizeof(*d)) {
424 warnx("T_dev_T %d != %d", l2, sizeof(*d));
427 if ((int)(*d) != -1) {
428 if (minor(*d) > 255 || minor(*d) < 0)
429 printf("{ major = %d, minor = 0x%x }",
430 major(*d), minor(*d));
432 printf("{ major = %d, minor = %d }",
433 major(*d), minor(*d));
439 set_T_dev_t(char *path, void **val, size_t *size)
441 static struct stat statb;
443 if (strcmp(path, "none") && strcmp(path, "off")) {
444 int rc = stat (path, &statb);
446 err(1, "cannot stat %s", path);
449 if (!S_ISCHR(statb.st_mode)) {
450 errx(1, "must specify a device special file.");
453 statb.st_rdev = NODEV;
455 *val = (void *) &statb.st_rdev;
456 *size = sizeof(statb.st_rdev);
460 set_IK(char *str, int *val)
466 if ((len = strlen(str)) == 0)
469 if (*p == 'C' || *p == 'F') {
471 temp = strtof(str, &endptr);
472 if (endptr == str || *endptr != '\0')
475 temp = (temp - 32) * 5 / 9;
476 kelv = temp * 10 + 2732;
478 kelv = (int)strtol(str, &endptr, 10);
479 if (endptr == str || *endptr != '\0')
487 * These functions uses a presently undocumented interface to the kernel
488 * to walk the tree and get the type so it can print the value.
489 * This interface is under work and consideration, and should probably
490 * be killed with a big axe by the first person who can find the time.
491 * (be aware though, that the proper interface isn't as obvious as it
492 * may seem, there are various conflicting requirements.
496 name2oid(char *name, int *oidp)
505 j = CTL_MAXNAME * sizeof(int);
506 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
514 oidfmt(int *oid, int len, char *fmt, u_int *kind)
516 int qoid[CTL_MAXNAME+2];
523 memcpy(qoid + 2, oid, len * sizeof(int));
526 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
528 err(1, "sysctl fmt %d %d %d", i, j, errno);
531 *kind = *(u_int *)buf;
534 strcpy(fmt, (char *)(buf + sizeof(u_int)));
539 * This formats and outputs the value of one variable
541 * Returns zero if anything was actually output.
542 * Returns one if didn't know what to do with this.
543 * Return minus one if we had errors.
547 show_var(int *oid, int nlen)
549 u_char buf[BUFSIZ], *val, *oval, *p;
550 char name[BUFSIZ], *fmt;
551 const char *sep, *sep1;
552 int qoid[CTL_MAXNAME+2];
559 int (*func)(int, void *);
564 memcpy(qoid + 2, oid, nlen * sizeof(int));
568 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
570 err(1, "sysctl name %d %d %d", i, j, errno);
582 if (dflag) { /* just print description */
585 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
587 printf("%s%s", name, sep);
591 /* find an estimate of how much we need for this var */
593 i = sysctl(oid, nlen, 0, &j, 0, 0);
594 j += j; /* we want to be sure :-) */
596 val = oval = malloc(j + 1);
598 warnx("malloc failed");
602 i = sysctl(oid, nlen, val, &len, 0, 0);
609 fwrite(val, 1, len, stdout);
615 oidfmt(oid, nlen, fmt, &kind);
620 printf("%s%s", name, sep);
621 printf("%.*s", len, p);
629 printf("%s%s", name, sep);
631 case 'I': intlen = sizeof(int); break;
632 case 'L': intlen = sizeof(long); break;
633 case 'Q': intlen = sizeof(quad_t); break;
635 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
637 while (len >= intlen) {
648 umv = *(u_quad_t *)p;
654 printf(hflag ? "%'ju" : "%ju", umv);
655 else if (fmt[1] == 'X')
656 printf("%#0*jx", hexlen, umv);
657 else if (fmt[1] == 'K') {
661 printf("%.1fC", (mv - 2732.0) / 10);
663 printf(hflag ? "%'jd" : "%jd", mv);
673 printf("%s%s", name, sep);
674 printf("%p", *(void **)p);
681 if (strcmp(fmt, "S,clockinfo") == 0)
683 else if (strcmp(fmt, "S,timeval") == 0)
685 else if (strcmp(fmt, "S,loadavg") == 0)
687 else if (strcmp(fmt, "S,vmtotal") == 0)
689 else if (strcmp(fmt, "T,dev_t") == 0)
695 printf("%s%s", name, sep);
702 if (!oflag && !xflag) {
707 printf("%s%s", name, sep);
708 printf("Format:%s Length:%d Dump:0x", fmt, len);
709 while (len-- && (xflag || p < val + 16))
710 printf("%02x", *p++);
711 if (!xflag && len > 16)
721 sysctl_all(int *oid, int len)
723 int name1[22], name2[22];
731 memcpy(name1+2, oid, len * sizeof(int));
739 j = sysctl(name1, l1, name2, &l2, 0, 0);
744 err(1, "sysctl(getnext) %d %d", j, l2);
749 if (len < 0 || l2 < (unsigned int)len)
752 for (i = 0; i < len; i++)
753 if (name2[i] != oid[i])
756 i = show_var(name2, l2);
760 memcpy(name1+2, name2, l2 * sizeof(int));