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. 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
29 * @(#) Copyright (c) 1993 The Regents of the University of California. All rights reserved.
30 * @(#)from: sysctl.c 8.1 (Berkeley) 6/6/93
31 * $FreeBSD: src/sbin/sysctl/sysctl.c,v 1.25.2.11 2003/05/01 22:48:08 trhodes Exp $
35 #include <sys/diskslice.h> /* used for bootdev parsing */
36 #include <sys/reboot.h> /* used for bootdev parsing */
38 #include <sys/types.h>
40 #include <sys/sysctl.h>
41 #include <sys/resource.h>
42 #include <sys/sensors.h>
43 #include <sys/param.h>
45 #include <machine/inttypes.h>
55 static int aflag, bflag, dflag, eflag, Nflag, nflag, oflag, xflag;
57 static int oidfmt(int *, size_t, char *, u_int *);
58 static void parse(const char *);
59 static int show_var(int *, size_t);
60 static int sysctl_all(int *, size_t);
61 static void set_T_dev_t(const char *, void **, size_t *);
62 static int set_IK(const char *, int *);
68 fprintf(stderr, "%s\n%s\n",
69 "usage: sysctl [-bdeNnox] variable[=value] ...",
70 " sysctl [-bdeNnox] -a");
75 main(int argc, char **argv)
81 while ((ch = getopt(argc, argv, "AabdeNnowxX")) != -1) {
128 if (aflag && argc == 0)
129 exit(sysctl_all(0, 0));
138 * Parse a name into a MIB entry.
139 * Lookup and print out the MIB entry if it exists.
140 * Set a new value if requested.
143 parse(const char *string)
149 unsigned int uintval;
151 unsigned long ulongval;
155 int mib[CTL_MAXNAME];
156 char *cp, fmt[BUFSIZ];
158 char *name_allocated = NULL;
161 if ((cp = strchr(string, '=')) != NULL) {
162 if ((name_allocated = malloc(cp - string + 1)) == NULL)
163 err(1, "malloc failed");
164 strlcpy(name_allocated, string, cp - string + 1);
165 name = name_allocated;
167 while (isspace(*++cp))
171 newsize = strlen(cp);
177 if (sysctlnametomib(name, mib, &len) < 0) {
178 if (errno == ENOENT) {
179 errx(1, "unknown oid '%s'", name);
181 err(1, "sysctlnametomib(\"%s\")", name);
185 if (oidfmt(mib, len, fmt, &kind))
186 err(1, "couldn't find format of oid '%s'", name);
188 if (newval == NULL) {
189 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
190 sysctl_all(mib, len);
192 i = show_var(mib, len);
197 if ((kind & CTLTYPE) == CTLTYPE_NODE)
198 errx(1, "oid '%s' isn't a leaf node", name);
200 if (!(kind&CTLFLAG_WR))
201 errx(1, "oid '%s' is read only", name);
203 switch (kind & CTLTYPE) {
205 if (!strcmp(fmt, "IK") == 0) {
206 if (!set_IK(newval, &intval))
207 errx(1, "invalid value '%s'",
210 intval = (int) strtol(newval, NULL, 0);
212 newsize = sizeof(intval);
215 uintval = (int) strtoul(newval, NULL, 0);
217 newsize = sizeof uintval;
220 longval = strtol(newval, NULL, 0);
222 newsize = sizeof longval;
225 ulongval = strtoul(newval, NULL, 0);
227 newsize = sizeof ulongval;
232 quadval = strtoq(newval, NULL, 0);
234 newsize = sizeof(quadval);
237 uquadval = strtouq(newval, NULL, 0);
239 newsize = sizeof(uquadval);
242 if (strcmp(fmt, "T,dev_t") == 0 ||
243 strcmp(fmt, "T,udev_t") == 0
245 set_T_dev_t((char*)newval, &newval,
251 errx(1, "oid '%s' is type %d,"
252 " cannot set that", name,
256 i = show_var(mib, len);
257 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
262 errx(1, "%s: value is not available",
265 errx(1, "%s: specification is incomplete",
268 errx(1, "%s: type is unknown to this program",
279 j = show_var(mib, len);
285 if (name_allocated != NULL)
286 free(name_allocated);
289 /* These functions will dump out various interesting structures. */
292 S_clockinfo(int l2, void *p)
294 struct clockinfo *ci = (struct clockinfo*)p;
295 if (l2 != sizeof(*ci))
296 err(1, "S_clockinfo %d != %zu", l2, sizeof(*ci));
297 printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
298 ci->hz, ci->tick, ci->tickadj, ci->profhz, ci->stathz);
303 S_loadavg(int l2, void *p)
305 struct loadavg *tv = (struct loadavg*)p;
307 if (l2 != sizeof(*tv))
308 err(1, "S_loadavg %d != %zu", l2, sizeof(*tv));
310 printf("{ %.2f %.2f %.2f }",
311 (double)tv->ldavg[0]/(double)tv->fscale,
312 (double)tv->ldavg[1]/(double)tv->fscale,
313 (double)tv->ldavg[2]/(double)tv->fscale);
318 S_timespec(int l2, void *p)
320 struct timespec *ts = (struct timespec*)p;
324 if (l2 != sizeof(*ts))
325 err(1, "S_timespec %d != %zu", l2, sizeof(*ts));
326 printf("{ sec = %ld, nsec = %ld } ",
327 ts->tv_sec, ts->tv_nsec);
329 p1 = strdup(ctime(&tv_sec));
330 for (p2=p1; *p2 ; p2++)
338 S_timeval(int l2, void *p)
340 struct timeval *tv = (struct timeval*)p;
344 if (l2 != sizeof(*tv))
345 err(1, "S_timeval %d != %zu", l2, sizeof(*tv));
346 printf("{ sec = %ld, usec = %ld } ",
347 tv->tv_sec, tv->tv_usec);
349 p1 = strdup(ctime(&tv_sec));
350 for (p2=p1; *p2 ; p2++)
358 S_sensor(int l2, void *p)
360 struct sensor *s = (struct sensor *)p;
362 if (l2 != sizeof(*s)) {
363 warnx("S_sensor %d != %zu", l2, sizeof(*s));
367 if (s->flags & SENSOR_FINVALID) {
369 * XXX: with this flag, the node should be entirely ignored,
370 * but as the magic-based sysctl(8) is not too flexible, we
371 * simply have to print out that the sensor is invalid.
377 if (s->flags & SENSOR_FUNKNOWN)
383 (s->value - 273150000) / 1000000.0);
386 printf("%jd RPM", (intmax_t)s->value);
388 case SENSOR_VOLTS_DC:
389 printf("%.2f VDC", s->value / 1000000.0);
392 printf("%.2f A", s->value / 1000000.0);
394 case SENSOR_WATTHOUR:
395 printf("%.2f Wh", s->value / 1000000.0);
398 printf("%.2f Ah", s->value / 1000000.0);
400 case SENSOR_INDICATOR:
401 printf("%s", s->value ? "On" : "Off");
404 printf("%jd", (intmax_t)s->value);
407 printf("%.2f%%", s->value / 1000.0);
410 printf("%.2f lx", s->value / 1000000.0);
417 case SENSOR_DRIVE_EMPTY:
420 case SENSOR_DRIVE_READY:
423 case SENSOR_DRIVE_POWERUP:
424 name = "powering up";
426 case SENSOR_DRIVE_ONLINE:
429 case SENSOR_DRIVE_IDLE:
432 case SENSOR_DRIVE_ACTIVE:
435 case SENSOR_DRIVE_REBUILD:
438 case SENSOR_DRIVE_POWERDOWN:
439 name = "powering down";
441 case SENSOR_DRIVE_FAIL:
444 case SENSOR_DRIVE_PFAIL:
454 case SENSOR_TIMEDELTA:
455 printf("%.6f secs", s->value / 1000000000.0);
462 if (s->desc[0] != '\0')
463 printf(" (%s)", s->desc);
466 case SENSOR_S_UNSPEC:
475 printf(", CRITICAL");
477 case SENSOR_S_UNKNOWN:
483 time_t t = s->tv.tv_sec;
488 printf(", %s.%03ld", ct, s->tv.tv_usec / 1000);
495 T_dev_t(int l2, void *p)
497 dev_t *d = (dev_t *)p;
498 if (l2 != sizeof(*d))
499 err(1, "T_dev_T %d != %zu", l2, sizeof(*d));
500 if ((int)(*d) != -1) {
501 if (minor(*d) > 255 || minor(*d) < 0)
502 printf("{ major = %d, minor = 0x%x }",
503 major(*d), minor(*d));
505 printf("{ major = %d, minor = %d }",
506 major(*d), minor(*d));
512 set_T_dev_t(const char *path, void **val, size_t *size)
514 static struct stat statb;
516 if (strcmp(path, "none") && strcmp(path, "off")) {
517 int rc = stat (path, &statb);
519 err(1, "cannot stat %s", path);
522 if (!S_ISCHR(statb.st_mode)) {
523 errx(1, "must specify a device special file.");
526 statb.st_rdev = NODEV;
528 *val = (char*) &statb.st_rdev;
529 *size = sizeof statb.st_rdev;
533 set_IK(const char *str, int *val)
540 if ((len = strlen(str)) == 0)
543 if (*p == 'C' || *p == 'F') {
544 temp = strtof(str, &endptr);
545 if (endptr == str || endptr != p)
548 temp = (temp - 32) * 5 / 9;
549 kelv = temp * 10 + 2732;
552 * I would like to just use, 0 but it would make numbers
553 * like '023' which were interpreted as decimal before
554 * suddenly interpreted as octal.
556 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
557 kelv = (int)strtol(str, &endptr, 0);
559 kelv = (int)strtol(str, &endptr, 10);
560 if (endptr == str || *endptr != '\0')
568 * These functions uses a presently undocumented interface to the kernel
569 * to walk the tree and get the type so it can print the value.
570 * This interface is under work and consideration, and should probably
571 * be killed with a big axe by the first person who can find the time.
572 * (be aware though, that the proper interface isn't as obvious as it
573 * may seem, there are various conflicting requirements.
577 oidfmt(int *oid, size_t len, char *fmt, u_int *kind)
579 int qoid[CTL_MAXNAME+2];
586 memcpy(qoid + 2, oid, len * sizeof(int));
589 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
591 err(1, "sysctl fmt %d %zu %d", i, j, errno);
594 *kind = *(u_int *)buf;
597 strcpy(fmt, (char *)(buf + sizeof(u_int)));
603 * Code to map a bootdev major number into a suitable device name.
604 * Major numbers are mapped into names as in boot2.c
615 { -1, NULL } /* terminator */
619 machdep_bootdev(u_long value)
621 int majdev, unit, slice, part;
624 if ((value & B_MAGICMASK) != B_DEVMAGIC) {
625 printf("invalid (0x%08lx)", value);
628 majdev = B_TYPE(value);
629 unit = B_UNIT(value);
630 slice = B_SLICE(value);
631 part = B_PARTITION(value);
632 if (majdev == 2) { /* floppy, as known to the boot block... */
633 printf("/dev/fd%d", unit);
636 for (p = maj2name; p->name != NULL && p->majdev != majdev ; p++) ;
637 if (p->name != NULL) { /* found */
638 if (slice == WHOLE_DISK_SLICE)
639 printf("/dev/%s%d%c", p->name, unit, part);
641 printf("/dev/%s%ds%d%c",
642 p->name, unit, slice - BASE_SLICE + 1, part + 'a');
644 printf("unknown (major %d unit %d slice %d part %d)",
645 majdev, unit, slice, part);
651 * This formats and outputs the value of one variable
653 * Returns zero if anything was actually output.
654 * Returns one if didn't know what to do with this.
655 * Return minus one if we had errors.
659 show_var(int *oid, size_t nlen)
661 u_char buf[BUFSIZ], *val = NULL, *p, *nul;
662 char name[BUFSIZ], *fmt;
663 const char *sep, *spacer;
664 int qoid[CTL_MAXNAME+2];
668 int (*func)(int, void *);
672 memcpy(qoid + 2, oid, nlen * sizeof(int));
676 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
678 err(1, "sysctl name %d %zu %d", i, j, errno);
690 if (dflag) { /* just print description */
693 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
695 printf("%s%s", name, sep);
699 /* find an estimate of how much we need for this var */
701 i = sysctl(oid, nlen, 0, &j, 0, 0);
702 j += j; /* we want to be sure :-) */
709 i = sysctl(oid, nlen, val, &len, 0, 0);
716 fwrite(val, 1, len, stdout);
722 oidfmt(oid, nlen, fmt, &kind);
727 printf("%s%s", name, sep);
728 nul = memchr(p, '\0', len);
729 fwrite(p, nul == NULL ? (int)len : nul - p, 1, stdout);
734 printf("%s%s", name, sep);
737 while (len >= sizeof(int)) {
739 printf("%s%u", spacer, *(unsigned int *)p);
740 else if (*fmt == 'K' && *(int *)p >= 0)
741 printf("%s%.1fC", spacer, (*(int *)p - 2732) / 10.0);
743 printf("%s%d", spacer, *(int *)p);
752 printf("%s%s", name, sep);
755 if (!strcmp(name, "machdep.guessed_bootdev"))
756 return machdep_bootdev(*(unsigned long *)p);
759 while (len >= sizeof(long)) {
761 printf("%s%lu", spacer, *(unsigned long *)p);
763 printf("%s%ld", spacer, *(long *)p);
772 printf("%s%s", name, sep);
773 printf("%p", *(void **)p);
778 printf("%s%s", name, sep);
781 while (len >= sizeof(quad_t)) {
784 spacer, (uintmax_t)*(u_quad_t *)p);
787 spacer, (intmax_t)*(quad_t *)p);
790 len -= sizeof(int64_t);
791 p += sizeof(int64_t);
797 if (!oflag && !xflag) {
799 if (strcmp(fmt, "S,clockinfo") == 0)
801 else if (strcmp(fmt, "S,timespec") == 0)
803 else if (strcmp(fmt, "S,timeval") == 0)
805 else if (strcmp(fmt, "S,loadavg") == 0)
807 else if (strcmp(fmt, "S,sensor") == 0)
809 else if (strcmp(fmt, "T,dev_t") == 0)
811 else if (strcmp(fmt, "T,udev_t") == 0)
817 printf("%s%s", name, sep);
818 error = (*func)(len, p);
824 if (!oflag && !xflag) {
829 printf("%s%s", name, sep);
830 printf("Format:%s Length:%zu Dump:0x", fmt, len);
831 while (len-- && (xflag || p < val + 16))
832 printf("%02x", *p++);
833 if (!xflag && len > 16)
845 sysctl_all(int *oid, size_t len)
847 int name1[22], name2[22];
855 memcpy(name1+2, oid, len * sizeof(int));
863 retval = sysctl(name1, l1, name2, &l2, 0, 0);
868 err(1, "sysctl(getnext) %d %zu", retval, l2);
876 for (i = 0; i < len; i++)
877 if (name2[i] != oid[i])
880 retval = show_var(name2, l2);
881 if (retval == 0 && !bflag)
884 memcpy(name1+2, name2, l2 * sizeof(int));