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 $
34 #include <sys/types.h>
36 #include <sys/sysctl.h>
37 #include <sys/resource.h>
38 #include <sys/sensors.h>
39 #include <sys/param.h>
43 #include <machine/metadata.h>
55 static int aflag, bflag, dflag, eflag, Nflag, nflag, oflag, xflag;
56 static int iflag, qflag;
58 static int oidfmt(int *, size_t, char *, u_int *);
59 static int parse(const char *);
60 static int show_var(int *, size_t);
61 static int sysctl_all(int *, size_t);
62 static void set_T_dev_t(const char *, void **, size_t *);
63 static int set_IK(const char *, int *);
69 fprintf(stderr, "%s\n%s\n",
70 "usage: sysctl [-AbdeiNnoqwxX] variable[=value] ...",
71 " sysctl [-bdeiNnoqwxX] -a");
76 main(int argc, char **argv)
84 while ((ch = getopt(argc, argv, "AabdeiNnoqwxX")) != -1) {
137 if (aflag && argc == 0)
138 exit(sysctl_all(0, 0));
143 warncount += parse(*argv++);
149 * Parse a name into a MIB entry.
150 * Lookup and print out the MIB entry if it exists.
151 * Set a new value if requested.
154 parse(const char *string)
168 unsigned int uintval;
170 unsigned long ulongval;
172 int mib[CTL_MAXNAME];
173 char *cp, fmt[BUFSIZ];
175 char *name_allocated = NULL;
178 if ((cp = strchr(string, '=')) != NULL) {
179 if ((name_allocated = malloc(cp - string + 1)) == NULL)
180 err(1, "malloc failed");
181 strlcpy(name_allocated, string, cp - string + 1);
182 name = name_allocated;
184 while (isspace(*++cp))
188 newsize = strlen(cp);
194 if (sysctlnametomib(name, mib, &len) < 0) {
199 if (errno == ENOENT) {
200 errx(1, "unknown oid '%s'", name);
202 err(1, "sysctlnametomib(\"%s\")", name);
206 if (oidfmt(mib, len, fmt, &kind)) {
207 warn("couldn't find format of oid '%s'", name);
213 if (newval == NULL) {
214 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
215 sysctl_all(mib, len);
217 i = show_var(mib, len);
222 if ((kind & CTLTYPE) == CTLTYPE_NODE)
223 errx(1, "oid '%s' isn't a leaf node", name);
225 if (!(kind&CTLFLAG_WR))
226 errx(1, "oid '%s' is read only", name);
228 switch (kind & CTLTYPE) {
230 case CTLTYPE_BIT32(0):
231 case CTLTYPE_BIT64(0):
232 if (!(strcmp(fmt, "IK") == 0)) {
233 if (!set_IK(newval, &intval))
234 errx(1, "invalid value '%s'",
237 intval = (int) strtol(newval, NULL, 0);
240 newsize = sizeof(intval);
243 uintval = (int) strtoul(newval, NULL, 0);
245 newsize = sizeof uintval;
248 longval = strtol(newval, NULL, 0);
250 newsize = sizeof longval;
253 ulongval = strtoul(newval, NULL, 0);
255 newsize = sizeof ulongval;
260 i8val = (int8_t)strtol(newval, NULL, 0);
262 newsize = sizeof(i8val);
265 i16val = (int16_t)strtol(newval, NULL, 0);
267 newsize = sizeof(i16val);
270 i32val = (int32_t)strtol(newval, NULL, 0);
272 newsize = sizeof(i32val);
275 i64val = strtoimax(newval, NULL, 0);
277 newsize = sizeof(i64val);
280 u8val = (uint8_t)strtoul(newval, NULL, 0);
282 newsize = sizeof(u8val);
285 u16val = (uint16_t)strtoul(newval, NULL, 0);
287 newsize = sizeof(u16val);
290 u32val = (uint32_t)strtoul(newval, NULL, 0);
292 newsize = sizeof(u32val);
295 u64val = strtoumax(newval, NULL, 0);
297 newsize = sizeof(u64val);
300 if (strcmp(fmt, "T,dev_t") == 0 ||
301 strcmp(fmt, "T,udev_t") == 0
303 set_T_dev_t((char*)newval, &newval,
309 errx(1, "oid '%s' is type %d,"
310 " cannot set that", name,
314 i = show_var(mib, len);
315 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
320 errx(1, "%s: value is not available",
323 errx(1, "%s: specification is incomplete",
326 errx(1, "%s: type is unknown to this program",
337 j = show_var(mib, len);
343 if (name_allocated != NULL)
344 free(name_allocated);
349 /* These functions will dump out various interesting structures. */
352 S_clockinfo(size_t l2, void *p)
354 struct clockinfo *ci = (struct clockinfo*)p;
355 if (l2 != sizeof(*ci))
356 err(1, "S_clockinfo %zu != %zu", l2, sizeof(*ci));
357 printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
358 ci->hz, ci->tick, ci->tickadj, ci->profhz, ci->stathz);
363 S_loadavg(size_t l2, void *p)
365 struct loadavg *tv = (struct loadavg*)p;
367 if (l2 != sizeof(*tv))
368 err(1, "S_loadavg %zu != %zu", l2, sizeof(*tv));
370 printf("{ %.2f %.2f %.2f }",
371 (double)tv->ldavg[0]/(double)tv->fscale,
372 (double)tv->ldavg[1]/(double)tv->fscale,
373 (double)tv->ldavg[2]/(double)tv->fscale);
378 S_timespec(size_t l2, void *p)
380 struct timespec *ts = (struct timespec*)p;
384 if (l2 != sizeof(*ts))
385 err(1, "S_timespec %zu != %zu", l2, sizeof(*ts));
386 printf("{ sec = %ld, nsec = %ld } ",
387 ts->tv_sec, ts->tv_nsec);
389 p1 = strdup(ctime(&tv_sec));
390 for (p2=p1; *p2 ; p2++)
398 S_timeval(size_t l2, void *p)
400 struct timeval *tv = (struct timeval*)p;
404 if (l2 != sizeof(*tv))
405 err(1, "S_timeval %zu != %zu", l2, sizeof(*tv));
406 printf("{ sec = %ld, usec = %ld } ",
407 tv->tv_sec, tv->tv_usec);
409 p1 = strdup(ctime(&tv_sec));
410 for (p2=p1; *p2 ; p2++)
418 S_sensor(size_t l2, void *p)
420 struct sensor *s = (struct sensor *)p;
422 if (l2 != sizeof(*s)) {
423 warnx("S_sensor %zu != %zu", l2, sizeof(*s));
427 if (s->flags & SENSOR_FINVALID) {
429 * XXX: with this flag, the node should be entirely ignored,
430 * but as the magic-based sysctl(8) is not too flexible, we
431 * simply have to print out that the sensor is invalid.
437 if (s->flags & SENSOR_FUNKNOWN)
443 (s->value - 273150000) / 1000000.0);
446 printf("%jd RPM", (intmax_t)s->value);
448 case SENSOR_VOLTS_DC:
449 printf("%.2f VDC", s->value / 1000000.0);
452 printf("%.2f W", s->value / 1000000.0);
455 printf("%.2f A", s->value / 1000000.0);
457 case SENSOR_WATTHOUR:
458 printf("%.2f Wh", s->value / 1000000.0);
461 printf("%.2f Ah", s->value / 1000000.0);
463 case SENSOR_INDICATOR:
464 printf("%s", s->value ? "On" : "Off");
467 printf("%jd Hz", (intmax_t)s->value);
471 printf("%jd", (intmax_t)s->value);
474 printf("%.2f%%", s->value / 1000.0);
477 printf("%.2f lx", s->value / 1000000.0);
484 case SENSOR_DRIVE_EMPTY:
487 case SENSOR_DRIVE_READY:
490 case SENSOR_DRIVE_POWERUP:
491 name = "powering up";
493 case SENSOR_DRIVE_ONLINE:
496 case SENSOR_DRIVE_IDLE:
499 case SENSOR_DRIVE_ACTIVE:
502 case SENSOR_DRIVE_REBUILD:
505 case SENSOR_DRIVE_POWERDOWN:
506 name = "powering down";
508 case SENSOR_DRIVE_FAIL:
511 case SENSOR_DRIVE_PFAIL:
521 case SENSOR_TIMEDELTA:
522 printf("%.6f secs", s->value / 1000000000.0);
529 if (s->desc[0] != '\0')
530 printf(" (%s)", s->desc);
533 case SENSOR_S_UNSPEC:
542 printf(", CRITICAL");
544 case SENSOR_S_UNKNOWN:
550 time_t t = s->tv.tv_sec;
555 printf(", %s.%03ld", ct, s->tv.tv_usec / 1000);
563 S_efi_map(size_t l2, void *p)
565 struct efi_map_header *efihdr;
571 static const char *types[] = {
577 "RuntimeServicesCode",
578 "RuntimeServicesData",
579 "ConventionalMemory",
584 "MemoryMappedIOPortSpace",
589 * Memory map data provided by UEFI via the GetMemoryMap
592 if (l2 < sizeof(*efihdr)) {
593 warnx("S_efi_map length less than header");
597 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
598 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
600 if (efihdr->descriptor_size == 0)
602 if (l2 != efisz + efihdr->memory_size) {
603 warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz +
604 efihdr->memory_size);
607 ndesc = efihdr->memory_size / efihdr->descriptor_size;
609 printf("\n%23s %12s %12s %8s %4s",
610 "Type", "Physical", "Virtual", "#Pages", "Attr");
612 for (i = 0; i < ndesc; i++,
613 map = efi_next_descriptor(map, efihdr->descriptor_size)) {
614 if (map->md_type <= EFI_MD_TYPE_PALCODE)
615 type = types[map->md_type];
618 printf("\n%23s %012lx %12p %08lx ", type, map->md_phys,
619 map->md_virt, map->md_pages);
620 if (map->md_attr & EFI_MD_ATTR_UC)
622 if (map->md_attr & EFI_MD_ATTR_WC)
624 if (map->md_attr & EFI_MD_ATTR_WT)
626 if (map->md_attr & EFI_MD_ATTR_WB)
628 if (map->md_attr & EFI_MD_ATTR_UCE)
630 if (map->md_attr & EFI_MD_ATTR_WP)
632 if (map->md_attr & EFI_MD_ATTR_RP)
634 if (map->md_attr & EFI_MD_ATTR_XP)
636 if (map->md_attr & EFI_MD_ATTR_RT)
644 T_dev_t(size_t l2, void *p)
646 dev_t *d = (dev_t *)p;
647 if (l2 != sizeof(*d))
648 err(1, "T_dev_T %zu != %zu", l2, sizeof(*d));
649 if ((int)(*d) != -1) {
650 if (minor(*d) > 255 || minor(*d) < 0)
651 printf("{ major = %d, minor = 0x%x }",
652 major(*d), minor(*d));
654 printf("{ major = %d, minor = %d }",
655 major(*d), minor(*d));
661 set_T_dev_t(const char *path, void **val, size_t *size)
663 static struct stat statb;
665 if (strcmp(path, "none") && strcmp(path, "off")) {
666 int rc = stat (path, &statb);
668 err(1, "cannot stat %s", path);
671 if (!S_ISCHR(statb.st_mode)) {
672 errx(1, "must specify a device special file.");
675 statb.st_rdev = NODEV;
677 *val = (char*) &statb.st_rdev;
678 *size = sizeof statb.st_rdev;
682 set_IK(const char *str, int *val)
689 if ((len = strlen(str)) == 0)
692 if (*p == 'C' || *p == 'F') {
693 temp = strtof(str, &endptr);
694 if (endptr == str || endptr != p)
697 temp = (temp - 32) * 5 / 9;
698 kelv = temp * 10 + 2732;
701 * I would like to just use, 0 but it would make numbers
702 * like '023' which were interpreted as decimal before
703 * suddenly interpreted as octal.
705 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
706 kelv = (int)strtol(str, &endptr, 0);
708 kelv = (int)strtol(str, &endptr, 10);
709 if (endptr == str || *endptr != '\0')
717 * These functions uses a presently undocumented interface to the kernel
718 * to walk the tree and get the type so it can print the value.
719 * This interface is under work and consideration, and should probably
720 * be killed with a big axe by the first person who can find the time.
721 * (be aware though, that the proper interface isn't as obvious as it
722 * may seem, there are various conflicting requirements.
726 oidfmt(int *oid, size_t len, char *fmt, u_int *kind)
728 int qoid[CTL_MAXNAME+2];
735 memcpy(qoid + 2, oid, len * sizeof(int));
738 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
740 err(1, "sysctl fmt %d %zu %d", i, j, errno);
743 *kind = *(u_int *)buf;
746 strcpy(fmt, (char *)(buf + sizeof(u_int)));
751 * This formats and outputs the value of one variable
753 * Returns zero if anything was actually output.
754 * Returns one if didn't know what to do with this.
755 * Return minus one if we had errors.
759 show_var(int *oid, size_t nlen)
761 u_char buf[BUFSIZ], *val = NULL, *p, *nul;
762 char name[BUFSIZ], *fmt;
763 const char *sep, *spacer;
764 int qoid[CTL_MAXNAME+2];
768 int (*func)(size_t, void *);
772 memcpy(qoid + 2, oid, nlen * sizeof(int));
776 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
778 err(1, "sysctl name %d %zu %d", i, j, errno);
790 if (dflag) { /* just print description */
793 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
795 printf("%s%s", name, sep);
799 /* find an estimate of how much we need for this var */
801 i = sysctl(oid, nlen, 0, &j, 0, 0);
802 j += j; /* we want to be sure :-) */
809 i = sysctl(oid, nlen, val, &len, 0, 0);
816 fwrite(val, 1, len, stdout);
822 oidfmt(oid, nlen, fmt, &kind);
827 printf("%s%s", name, sep);
828 nul = memchr(p, '\0', len);
829 fwrite(p, nul == NULL ? (int)len : nul - p, 1, stdout);
834 printf("%s%s", name, sep);
837 while (len >= sizeof(char)) {
839 printf("%s%hhu", spacer, *(unsigned char *)p);
841 printf("%s%hhd", spacer, *(char *)p);
850 printf("%s%s", name, sep);
853 while (len >= sizeof(int)) {
855 printf("%s%u", spacer, *(unsigned int *)p);
856 else if (*fmt == 'K' && *(int *)p >= 0)
857 printf("%s%.1fC", spacer, (*(int *)p - 2732) / 10.0);
859 printf("%s%d", spacer, *(int *)p);
868 printf("%s%s", name, sep);
871 while (len >= sizeof(long)) {
873 printf("%s%lu", spacer, *(unsigned long *)p);
875 printf("%s%ld", spacer, *(long *)p);
884 printf("%s%s", name, sep);
885 printf("%p", *(void **)p);
890 printf("%s%s", name, sep);
893 while (len >= sizeof(quad_t)) {
896 spacer, (uintmax_t)*(u_quad_t *)p);
899 spacer, (intmax_t)*(quad_t *)p);
902 len -= sizeof(int64_t);
903 p += sizeof(int64_t);
909 if (fmt[0] == 'S' && fmt[1] != ',') {
911 printf("%s%s", name, sep);
914 while (len >= sizeof(short)) {
916 printf("%s%hu", spacer,
917 *(unsigned short *)p);
919 printf("%s%hd", spacer, *(short *)p);
921 len -= sizeof(short);
927 if (!oflag && !xflag) {
929 if (strcmp(fmt, "S,clockinfo") == 0)
931 else if (strcmp(fmt, "S,timespec") == 0)
933 else if (strcmp(fmt, "S,timeval") == 0)
935 else if (strcmp(fmt, "S,loadavg") == 0)
937 else if (strcmp(fmt, "S,sensor") == 0)
940 else if (strcmp(fmt, "S,efi_map_header") == 0)
943 else if (strcmp(fmt, "T,dev_t") == 0)
945 else if (strcmp(fmt, "T,udev_t") == 0)
951 printf("%s%s", name, sep);
952 error = (*func)(len, p);
958 if (!oflag && !xflag) {
963 printf("%s%s", name, sep);
964 printf("Format:%s Length:%zu Dump:0x", fmt, len);
965 while (len-- && (xflag || p < val + 16))
966 printf("%02x", *p++);
967 if (!xflag && len > 16)
979 sysctl_all(int *oid, size_t len)
981 int name1[22], name2[22];
989 memcpy(name1+2, oid, len * sizeof(int));
997 retval = sysctl(name1, l1, name2, &l2, 0, 0);
1002 err(1, "sysctl(getnext) %d %zu", retval, l2);
1010 for (i = 0; i < len; i++)
1011 if (name2[i] != oid[i])
1014 retval = show_var(name2, l2);
1015 if (retval == 0 && !bflag)
1018 memcpy(name1+2, name2, l2 * sizeof(int));