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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#) Copyright (c) 1993 The Regents of the University of California. All rights reserved.
34 * @(#)from: sysctl.c 8.1 (Berkeley) 6/6/93
35 * $FreeBSD: src/sbin/sysctl/sysctl.c,v 1.25.2.11 2003/05/01 22:48:08 trhodes Exp $
39 #include <sys/diskslice.h> /* used for bootdev parsing */
40 #include <sys/reboot.h> /* used for bootdev parsing */
42 #include <sys/types.h>
44 #include <sys/sysctl.h>
45 #include <sys/resource.h>
46 #include <sys/sensors.h>
47 #include <sys/param.h>
49 #include <machine/inttypes.h>
59 static int aflag, bflag, dflag, eflag, Nflag, nflag, oflag, xflag;
61 static int oidfmt(int *, size_t, char *, u_int *);
62 static void parse(const char *);
63 static int show_var(int *, size_t);
64 static int sysctl_all(int *, size_t);
65 static void set_T_dev_t(const char *, void **, size_t *);
66 static int set_IK(const char *, int *);
72 fprintf(stderr, "%s\n%s\n",
73 "usage: sysctl [-bdeNnox] variable[=value] ...",
74 " sysctl [-bdeNnox] -a");
79 main(int argc, char **argv)
85 while ((ch = getopt(argc, argv, "AabdeNnowxX")) != -1) {
132 if (aflag && argc == 0)
133 exit(sysctl_all(0, 0));
142 * Parse a name into a MIB entry.
143 * Lookup and print out the MIB entry if it exists.
144 * Set a new value if requested.
147 parse(const char *string)
153 unsigned int uintval;
155 unsigned long ulongval;
159 int mib[CTL_MAXNAME];
160 char *cp, fmt[BUFSIZ];
162 char *name_allocated = NULL;
165 if ((cp = strchr(string, '=')) != NULL) {
166 if ((name_allocated = malloc(cp - string + 1)) == NULL)
167 err(1, "malloc failed");
168 strlcpy(name_allocated, string, cp - string + 1);
169 name = name_allocated;
171 while (isspace(*++cp))
175 newsize = strlen(cp);
181 if (sysctlnametomib(name, mib, &len) < 0) {
182 if (errno == ENOENT) {
183 errx(1, "unknown oid '%s'", name);
185 err(1, "sysctlnametomib(\"%s\")", name);
189 if (oidfmt(mib, len, fmt, &kind))
190 err(1, "couldn't find format of oid '%s'", name);
192 if (newval == NULL) {
193 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
194 sysctl_all(mib, len);
196 i = show_var(mib, len);
201 if ((kind & CTLTYPE) == CTLTYPE_NODE)
202 errx(1, "oid '%s' isn't a leaf node", name);
204 if (!(kind&CTLFLAG_WR))
205 errx(1, "oid '%s' is read only", name);
207 switch (kind & CTLTYPE) {
209 if (!strcmp(fmt, "IK") == 0) {
210 if (!set_IK(newval, &intval))
211 errx(1, "invalid value '%s'",
214 intval = (int) strtol(newval, NULL, 0);
216 newsize = sizeof(intval);
219 uintval = (int) strtoul(newval, NULL, 0);
221 newsize = sizeof uintval;
224 longval = strtol(newval, NULL, 0);
226 newsize = sizeof longval;
229 ulongval = strtoul(newval, NULL, 0);
231 newsize = sizeof ulongval;
236 quadval = strtoq(newval, NULL, 0);
238 newsize = sizeof(quadval);
241 uquadval = strtouq(newval, NULL, 0);
243 newsize = sizeof(uquadval);
246 if (strcmp(fmt, "T,dev_t") == 0 ||
247 strcmp(fmt, "T,udev_t") == 0
249 set_T_dev_t((char*)newval, &newval,
255 errx(1, "oid '%s' is type %d,"
256 " cannot set that", name,
260 i = show_var(mib, len);
261 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
266 errx(1, "%s: value is not available",
269 errx(1, "%s: specification is incomplete",
272 errx(1, "%s: type is unknown to this program",
283 j = show_var(mib, len);
289 if (name_allocated != NULL)
290 free(name_allocated);
293 /* These functions will dump out various interesting structures. */
296 S_clockinfo(int l2, void *p)
298 struct clockinfo *ci = (struct clockinfo*)p;
299 if (l2 != sizeof(*ci))
300 err(1, "S_clockinfo %d != %zu", l2, sizeof(*ci));
301 printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
302 ci->hz, ci->tick, ci->tickadj, ci->profhz, ci->stathz);
307 S_loadavg(int l2, void *p)
309 struct loadavg *tv = (struct loadavg*)p;
311 if (l2 != sizeof(*tv))
312 err(1, "S_loadavg %d != %zu", l2, sizeof(*tv));
314 printf("{ %.2f %.2f %.2f }",
315 (double)tv->ldavg[0]/(double)tv->fscale,
316 (double)tv->ldavg[1]/(double)tv->fscale,
317 (double)tv->ldavg[2]/(double)tv->fscale);
322 S_timespec(int l2, void *p)
324 struct timespec *ts = (struct timespec*)p;
328 if (l2 != sizeof(*ts))
329 err(1, "S_timespec %d != %zu", l2, sizeof(*ts));
330 printf("{ sec = %ld, nsec = %ld } ",
331 ts->tv_sec, ts->tv_nsec);
333 p1 = strdup(ctime(&tv_sec));
334 for (p2=p1; *p2 ; p2++)
342 S_timeval(int l2, void *p)
344 struct timeval *tv = (struct timeval*)p;
348 if (l2 != sizeof(*tv))
349 err(1, "S_timeval %d != %zu", l2, sizeof(*tv));
350 printf("{ sec = %ld, usec = %ld } ",
351 tv->tv_sec, tv->tv_usec);
353 p1 = strdup(ctime(&tv_sec));
354 for (p2=p1; *p2 ; p2++)
362 S_sensor(int l2, void *p)
364 struct sensor *s = (struct sensor *)p;
366 if (l2 != sizeof(*s)) {
367 warnx("S_sensor %d != %zu", l2, sizeof(*s));
371 if (s->flags & SENSOR_FINVALID) {
373 * XXX: with this flag, the node should be entirely ignored,
374 * but as the magic-based sysctl(8) is not too flexible, we
375 * simply have to print out that the sensor is invalid.
381 if (s->flags & SENSOR_FUNKNOWN)
387 (s->value - 273150000) / 1000000.0);
390 printf("%jd RPM", (intmax_t)s->value);
392 case SENSOR_VOLTS_DC:
393 printf("%.2f VDC", s->value / 1000000.0);
396 printf("%.2f A", s->value / 1000000.0);
398 case SENSOR_WATTHOUR:
399 printf("%.2f Wh", s->value / 1000000.0);
402 printf("%.2f Ah", s->value / 1000000.0);
404 case SENSOR_INDICATOR:
405 printf("%s", s->value ? "On" : "Off");
408 printf("%jd", (intmax_t)s->value);
411 printf("%.2f%%", s->value / 1000.0);
414 printf("%.2f lx", s->value / 1000000.0);
421 case SENSOR_DRIVE_EMPTY:
424 case SENSOR_DRIVE_READY:
427 case SENSOR_DRIVE_POWERUP:
428 name = "powering up";
430 case SENSOR_DRIVE_ONLINE:
433 case SENSOR_DRIVE_IDLE:
436 case SENSOR_DRIVE_ACTIVE:
439 case SENSOR_DRIVE_REBUILD:
442 case SENSOR_DRIVE_POWERDOWN:
443 name = "powering down";
445 case SENSOR_DRIVE_FAIL:
448 case SENSOR_DRIVE_PFAIL:
458 case SENSOR_TIMEDELTA:
459 printf("%.6f secs", s->value / 1000000000.0);
466 if (s->desc[0] != '\0')
467 printf(" (%s)", s->desc);
470 case SENSOR_S_UNSPEC:
479 printf(", CRITICAL");
481 case SENSOR_S_UNKNOWN:
487 time_t t = s->tv.tv_sec;
492 printf(", %s.%03ld", ct, s->tv.tv_usec / 1000);
499 T_dev_t(int l2, void *p)
501 dev_t *d = (dev_t *)p;
502 if (l2 != sizeof(*d))
503 err(1, "T_dev_T %d != %zu", l2, sizeof(*d));
504 if ((int)(*d) != -1) {
505 if (minor(*d) > 255 || minor(*d) < 0)
506 printf("{ major = %d, minor = 0x%x }",
507 major(*d), minor(*d));
509 printf("{ major = %d, minor = %d }",
510 major(*d), minor(*d));
516 set_T_dev_t(const char *path, void **val, size_t *size)
518 static struct stat statb;
520 if (strcmp(path, "none") && strcmp(path, "off")) {
521 int rc = stat (path, &statb);
523 err(1, "cannot stat %s", path);
526 if (!S_ISCHR(statb.st_mode)) {
527 errx(1, "must specify a device special file.");
530 statb.st_rdev = NODEV;
532 *val = (char*) &statb.st_rdev;
533 *size = sizeof statb.st_rdev;
537 set_IK(const char *str, int *val)
544 if ((len = strlen(str)) == 0)
547 if (*p == 'C' || *p == 'F') {
548 temp = strtof(str, &endptr);
549 if (endptr == str || endptr != p)
552 temp = (temp - 32) * 5 / 9;
553 kelv = temp * 10 + 2732;
556 * I would like to just use, 0 but it would make numbers
557 * like '023' which were interpreted as decimal before
558 * suddenly interpreted as octal.
560 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
561 kelv = (int)strtol(str, &endptr, 0);
563 kelv = (int)strtol(str, &endptr, 10);
564 if (endptr == str || *endptr != '\0')
572 * These functions uses a presently undocumented interface to the kernel
573 * to walk the tree and get the type so it can print the value.
574 * This interface is under work and consideration, and should probably
575 * be killed with a big axe by the first person who can find the time.
576 * (be aware though, that the proper interface isn't as obvious as it
577 * may seem, there are various conflicting requirements.
581 oidfmt(int *oid, size_t len, char *fmt, u_int *kind)
583 int qoid[CTL_MAXNAME+2];
590 memcpy(qoid + 2, oid, len * sizeof(int));
593 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
595 err(1, "sysctl fmt %d %zu %d", i, j, errno);
598 *kind = *(u_int *)buf;
601 strcpy(fmt, (char *)(buf + sizeof(u_int)));
607 * Code to map a bootdev major number into a suitable device name.
608 * Major numbers are mapped into names as in boot2.c
619 { -1, NULL } /* terminator */
623 machdep_bootdev(u_long value)
625 int majdev, unit, slice, part;
628 if ((value & B_MAGICMASK) != B_DEVMAGIC) {
629 printf("invalid (0x%08lx)", value);
632 majdev = B_TYPE(value);
633 unit = B_UNIT(value);
634 slice = B_SLICE(value);
635 part = B_PARTITION(value);
636 if (majdev == 2) { /* floppy, as known to the boot block... */
637 printf("/dev/fd%d", unit);
640 for (p = maj2name; p->name != NULL && p->majdev != majdev ; p++) ;
641 if (p->name != NULL) { /* found */
642 if (slice == WHOLE_DISK_SLICE)
643 printf("/dev/%s%d%c", p->name, unit, part);
645 printf("/dev/%s%ds%d%c",
646 p->name, unit, slice - BASE_SLICE + 1, part + 'a');
648 printf("unknown (major %d unit %d slice %d part %d)",
649 majdev, unit, slice, part);
655 * This formats and outputs the value of one variable
657 * Returns zero if anything was actually output.
658 * Returns one if didn't know what to do with this.
659 * Return minus one if we had errors.
663 show_var(int *oid, size_t nlen)
665 u_char buf[BUFSIZ], *val = NULL, *p, *nul;
666 char name[BUFSIZ], *fmt;
667 const char *sep, *spacer;
668 int qoid[CTL_MAXNAME+2];
672 int (*func)(int, void *);
676 memcpy(qoid + 2, oid, nlen * sizeof(int));
680 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
682 err(1, "sysctl name %d %zu %d", i, j, errno);
694 if (dflag) { /* just print description */
697 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
699 printf("%s%s", name, sep);
703 /* find an estimate of how much we need for this var */
705 i = sysctl(oid, nlen, 0, &j, 0, 0);
706 j += j; /* we want to be sure :-) */
713 i = sysctl(oid, nlen, val, &len, 0, 0);
720 fwrite(val, 1, len, stdout);
726 oidfmt(oid, nlen, fmt, &kind);
731 printf("%s%s", name, sep);
732 nul = memchr(p, '\0', len);
733 fwrite(p, nul == NULL ? (int)len : nul - p, 1, stdout);
738 printf("%s%s", name, sep);
741 while (len >= sizeof(int)) {
743 printf("%s%u", spacer, *(unsigned int *)p);
744 else if (*fmt == 'K' && *(int *)p >= 0)
745 printf("%s%.1fC", spacer, (*(int *)p - 2732) / 10.0);
747 printf("%s%d", spacer, *(int *)p);
756 printf("%s%s", name, sep);
759 if (!strcmp(name, "machdep.guessed_bootdev"))
760 return machdep_bootdev(*(unsigned long *)p);
763 while (len >= sizeof(long)) {
765 printf("%s%lu", spacer, *(unsigned long *)p);
767 printf("%s%ld", spacer, *(long *)p);
776 printf("%s%s", name, sep);
777 printf("%p", *(void **)p);
782 printf("%s%s", name, sep);
785 while (len >= sizeof(quad_t)) {
788 spacer, (uintmax_t)*(u_quad_t *)p);
791 spacer, (intmax_t)*(quad_t *)p);
794 len -= sizeof(int64_t);
795 p += sizeof(int64_t);
801 if (!oflag && !xflag) {
803 if (strcmp(fmt, "S,clockinfo") == 0)
805 else if (strcmp(fmt, "S,timespec") == 0)
807 else if (strcmp(fmt, "S,timeval") == 0)
809 else if (strcmp(fmt, "S,loadavg") == 0)
811 else if (strcmp(fmt, "S,sensor") == 0)
813 else if (strcmp(fmt, "T,dev_t") == 0)
815 else if (strcmp(fmt, "T,udev_t") == 0)
821 printf("%s%s", name, sep);
822 error = (*func)(len, p);
828 if (!oflag && !xflag) {
833 printf("%s%s", name, sep);
834 printf("Format:%s Length:%zu Dump:0x", fmt, len);
835 while (len-- && (xflag || p < val + 16))
836 printf("%02x", *p++);
837 if (!xflag && len > 16)
849 sysctl_all(int *oid, size_t len)
851 int name1[22], name2[22];
859 memcpy(name1+2, oid, len * sizeof(int));
867 retval = sysctl(name1, l1, name2, &l2, 0, 0);
872 err(1, "sysctl(getnext) %d %zu", retval, l2);
880 for (i = 0; i < len; i++)
881 if (name2[i] != oid[i])
884 retval = show_var(name2, l2);
885 if (retval == 0 && !bflag)
888 memcpy(name1+2, name2, l2 * sizeof(int));