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 $
36 * $DragonFly: src/sbin/sysctl/sysctl.c,v 1.16 2007/10/02 12:57:00 hasso Exp $
40 #include <sys/diskslice.h> /* used for bootdev parsing */
41 #include <sys/reboot.h> /* used for bootdev parsing */
43 #include <sys/types.h>
45 #include <sys/sysctl.h>
46 #include <sys/resource.h>
47 #include <sys/sensors.h>
48 #include <sys/param.h>
50 #include <machine/inttypes.h>
60 static int aflag, bflag, dflag, eflag, Nflag, nflag, oflag, xflag;
62 static int oidfmt(int *, size_t, char *, u_int *);
63 static void parse(const char *);
64 static int show_var(int *, size_t);
65 static int sysctl_all(int *, size_t);
66 static void set_T_dev_t(const char *, void **, size_t *);
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 intval = (int) strtol(newval, NULL, 0);
211 newsize = sizeof(intval);
214 uintval = (int) strtoul(newval, NULL, 0);
216 newsize = sizeof uintval;
219 longval = strtol(newval, NULL, 0);
221 newsize = sizeof longval;
224 ulongval = strtoul(newval, NULL, 0);
226 newsize = sizeof ulongval;
231 quadval = strtoq(newval, NULL, 0);
233 newsize = sizeof(quadval);
236 uquadval = strtouq(newval, NULL, 0);
238 newsize = sizeof(quadval);
241 if (strcmp(fmt, "T,dev_t") == 0 ||
242 strcmp(fmt, "T,udev_t") == 0
244 set_T_dev_t((char*)newval, &newval,
250 errx(1, "oid '%s' is type %d,"
251 " cannot set that", name,
255 i = show_var(mib, len);
256 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
261 errx(1, "%s: value is not available",
264 errx(1, "%s: specification is incomplete",
267 errx(1, "%s: type is unknown to this program",
278 j = show_var(mib, len);
284 if (name_allocated != NULL)
285 free(name_allocated);
288 /* These functions will dump out various interesting structures. */
291 S_clockinfo(int l2, void *p)
293 struct clockinfo *ci = (struct clockinfo*)p;
294 if (l2 != sizeof(*ci))
295 err(1, "S_clockinfo %d != %d", l2, sizeof(*ci));
296 printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
297 ci->hz, ci->tick, ci->tickadj, ci->profhz, ci->stathz);
302 S_loadavg(int l2, void *p)
304 struct loadavg *tv = (struct loadavg*)p;
306 if (l2 != sizeof(*tv))
307 err(1, "S_loadavg %d != %d", l2, sizeof(*tv));
309 printf("{ %.2f %.2f %.2f }",
310 (double)tv->ldavg[0]/(double)tv->fscale,
311 (double)tv->ldavg[1]/(double)tv->fscale,
312 (double)tv->ldavg[2]/(double)tv->fscale);
317 S_timespec(int l2, void *p)
319 struct timespec *ts = (struct timespec*)p;
323 if (l2 != sizeof(*ts))
324 err(1, "S_timespec %d != %d", l2, sizeof(*ts));
325 printf("{ sec = %ld, nsec = %ld } ",
326 ts->tv_sec, ts->tv_nsec);
328 p1 = strdup(ctime(&tv_sec));
329 for (p2=p1; *p2 ; p2++)
337 S_timeval(int l2, void *p)
339 struct timeval *tv = (struct timeval*)p;
343 if (l2 != sizeof(*tv))
344 err(1, "S_timeval %d != %d", l2, sizeof(*tv));
345 printf("{ sec = %ld, usec = %ld } ",
346 tv->tv_sec, tv->tv_usec);
348 p1 = strdup(ctime(&tv_sec));
349 for (p2=p1; *p2 ; p2++)
357 S_sensor(int l2, void *p)
359 struct sensor *s = (struct sensor *)p;
361 if (l2 != sizeof(*s)) {
362 warnx("S_sensor %d != %d", l2, sizeof(*s));
366 if (s->flags & SENSOR_FINVALID) {
368 * XXX: with this flag, the node should be entirely ignored,
369 * but as the magic-based sysctl(8) is not too flexible, we
370 * simply have to print out that the sensor is invalid.
376 if (s->flags & SENSOR_FUNKNOWN)
382 (s->value - 273150000) / 1000000.0);
385 printf("%lld RPM", s->value);
387 case SENSOR_VOLTS_DC:
388 printf("%.2f VDC", s->value / 1000000.0);
391 printf("%.2f A", s->value / 1000000.0);
393 case SENSOR_WATTHOUR:
394 printf("%.2f Wh", s->value / 1000000.0);
397 printf("%.2f Ah", s->value / 1000000.0);
399 case SENSOR_INDICATOR:
400 printf("%s", s->value ? "On" : "Off");
403 printf("%lld", s->value);
406 printf("%.2f%%", s->value / 1000.0);
409 printf("%.2f lx", s->value / 1000000.0);
416 case SENSOR_DRIVE_EMPTY:
419 case SENSOR_DRIVE_READY:
422 case SENSOR_DRIVE_POWERUP:
423 name = "powering up";
425 case SENSOR_DRIVE_ONLINE:
428 case SENSOR_DRIVE_IDLE:
431 case SENSOR_DRIVE_ACTIVE:
434 case SENSOR_DRIVE_REBUILD:
437 case SENSOR_DRIVE_POWERDOWN:
438 name = "powering down";
440 case SENSOR_DRIVE_FAIL:
443 case SENSOR_DRIVE_PFAIL:
453 case SENSOR_TIMEDELTA:
454 printf("%.6f secs", s->value / 1000000000.0);
461 if (s->desc[0] != '\0')
462 printf(" (%s)", s->desc);
465 case SENSOR_S_UNSPEC:
474 printf(", CRITICAL");
476 case SENSOR_S_UNKNOWN:
482 time_t t = s->tv.tv_sec;
487 printf(", %s.%03ld", ct, s->tv.tv_usec / 1000);
494 T_dev_t(int l2, void *p)
496 dev_t *d = (dev_t *)p;
497 if (l2 != sizeof(*d))
498 err(1, "T_dev_T %d != %d", l2, sizeof(*d));
499 if ((int)(*d) != -1) {
500 if (minor(*d) > 255 || minor(*d) < 0)
501 printf("{ major = %d, minor = 0x%x }",
502 major(*d), minor(*d));
504 printf("{ major = %d, minor = %d }",
505 major(*d), minor(*d));
511 set_T_dev_t(const char *path, void **val, size_t *size)
513 static struct stat statb;
515 if (strcmp(path, "none") && strcmp(path, "off")) {
516 int rc = stat (path, &statb);
518 err(1, "cannot stat %s", path);
521 if (!S_ISCHR(statb.st_mode)) {
522 errx(1, "must specify a device special file.");
525 statb.st_rdev = NODEV;
527 *val = (char*) &statb.st_rdev;
528 *size = sizeof statb.st_rdev;
532 * These functions uses a presently undocumented interface to the kernel
533 * to walk the tree and get the type so it can print the value.
534 * This interface is under work and consideration, and should probably
535 * be killed with a big axe by the first person who can find the time.
536 * (be aware though, that the proper interface isn't as obvious as it
537 * may seem, there are various conflicting requirements.
541 oidfmt(int *oid, size_t len, char *fmt, u_int *kind)
543 int qoid[CTL_MAXNAME+2];
550 memcpy(qoid + 2, oid, len * sizeof(int));
553 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
555 err(1, "sysctl fmt %d %d %d", i, j, errno);
558 *kind = *(u_int *)buf;
561 strcpy(fmt, (char *)(buf + sizeof(u_int)));
567 * Code to map a bootdev major number into a suitable device name.
568 * Major numbers are mapped into names as in boot2.c
579 { -1, NULL } /* terminator */
583 machdep_bootdev(u_long value)
585 int majdev, unit, slice, part;
588 if ((value & B_MAGICMASK) != B_DEVMAGIC) {
589 printf("invalid (0x%08lx)", value);
592 majdev = B_TYPE(value);
593 unit = B_UNIT(value);
594 slice = B_SLICE(value);
595 part = B_PARTITION(value);
596 if (majdev == 2) { /* floppy, as known to the boot block... */
597 printf("/dev/fd%d", unit);
600 for (p = maj2name; p->name != NULL && p->majdev != majdev ; p++) ;
601 if (p->name != NULL) { /* found */
602 if (slice == WHOLE_DISK_SLICE)
603 printf("/dev/%s%d%c", p->name, unit, part);
605 printf("/dev/%s%ds%d%c",
606 p->name, unit, slice - BASE_SLICE + 1, part + 'a');
608 printf("unknown (major %d unit %d slice %d part %d)",
609 majdev, unit, slice, part);
615 * This formats and outputs the value of one variable
617 * Returns zero if anything was actually output.
618 * Returns one if didn't know what to do with this.
619 * Return minus one if we had errors.
623 show_var(int *oid, size_t nlen)
625 u_char buf[BUFSIZ], *val, *p, *nul;
626 char name[BUFSIZ], *fmt;
627 const char *sep, *spacer;
628 int qoid[CTL_MAXNAME+2];
632 int (*func)(int, void *);
635 memcpy(qoid + 2, oid, nlen * sizeof(int));
639 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
641 err(1, "sysctl name %d %d %d", i, j, errno);
653 if (dflag) { /* just print description */
656 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
658 printf("%s%s", name, sep);
662 /* find an estimate of how much we need for this var */
664 i = sysctl(oid, nlen, 0, &j, 0, 0);
665 j += j; /* we want to be sure :-) */
669 i = sysctl(oid, nlen, val, &len, 0, 0);
674 fwrite(val, 1, len, stdout);
680 oidfmt(oid, nlen, fmt, &kind);
685 printf("%s%s", name, sep);
686 nul = memchr(p, '\0', len);
687 fwrite(p, nul == NULL ? len : nul - p, 1, stdout);
692 printf("%s%s", name, sep);
695 while (len >= sizeof(int)) {
697 printf("%s%u", spacer, *(unsigned int *)p);
699 printf("%s%d", spacer, *(int *)p);
708 printf("%s%s", name, sep);
711 if (!strcmp(name, "machdep.guessed_bootdev"))
712 return machdep_bootdev(*(unsigned long *)p);
715 while (len >= sizeof(long)) {
717 printf("%s%lu", spacer, *(unsigned long *)p);
719 printf("%s%ld", spacer, *(long *)p);
728 printf("%s%s", name, sep);
729 printf("%p", *(void **)p);
734 printf("%s%s", name, sep);
737 while (len >= sizeof(quad_t)) {
739 printf("%s%qu", spacer, *(u_quad_t *)p);
741 printf("%s%qd", spacer, *(quad_t *)p);
743 len -= sizeof(int64_t);
744 p += sizeof(int64_t);
750 if (!oflag && !xflag) {
752 if (strcmp(fmt, "S,clockinfo") == 0)
754 else if (strcmp(fmt, "S,timespec") == 0)
756 else if (strcmp(fmt, "S,timeval") == 0)
758 else if (strcmp(fmt, "S,loadavg") == 0)
760 else if (strcmp(fmt, "S,sensor") == 0)
762 else if (strcmp(fmt, "T,dev_t") == 0)
764 else if (strcmp(fmt, "T,udev_t") == 0)
770 printf("%s%s", name, sep);
771 return ((*func)(len, p));
776 if (!oflag && !xflag)
779 printf("%s%s", name, sep);
780 printf("Format:%s Length:%d Dump:0x", fmt, len);
781 while (len-- && (xflag || p < val + 16))
782 printf("%02x", *p++);
783 if (!xflag && len > 16)
791 sysctl_all(int *oid, size_t len)
793 int name1[22], name2[22];
801 memcpy(name1+2, oid, len * sizeof(int));
809 retval = sysctl(name1, l1, name2, &l2, 0, 0);
814 err(1, "sysctl(getnext) %d %d", retval, l2);
822 for (i = 0; i < len; i++)
823 if (name2[i] != oid[i])
826 retval = show_var(name2, l2);
827 if (retval == 0 && !bflag)
830 memcpy(name1+2, name2, l2 * sizeof(int));