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 [-bdeNnox] variable[=value] ...",
71 " sysctl [-bdeNnox] -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 if (!(strcmp(fmt, "IK") == 0)) {
231 if (!set_IK(newval, &intval))
232 errx(1, "invalid value '%s'",
235 intval = (int) strtol(newval, NULL, 0);
237 newsize = sizeof(intval);
240 uintval = (int) strtoul(newval, NULL, 0);
242 newsize = sizeof uintval;
245 longval = strtol(newval, NULL, 0);
247 newsize = sizeof longval;
250 ulongval = strtoul(newval, NULL, 0);
252 newsize = sizeof ulongval;
257 i8val = (int8_t)strtol(newval, NULL, 0);
259 newsize = sizeof(i8val);
262 i16val = (int16_t)strtol(newval, NULL, 0);
264 newsize = sizeof(i16val);
267 i32val = (int32_t)strtol(newval, NULL, 0);
269 newsize = sizeof(i32val);
272 i64val = strtoimax(newval, NULL, 0);
274 newsize = sizeof(i64val);
277 u8val = (uint8_t)strtoul(newval, NULL, 0);
279 newsize = sizeof(u8val);
282 u16val = (uint16_t)strtoul(newval, NULL, 0);
284 newsize = sizeof(u16val);
287 u32val = (uint32_t)strtoul(newval, NULL, 0);
289 newsize = sizeof(u32val);
292 u64val = strtoumax(newval, NULL, 0);
294 newsize = sizeof(u64val);
297 if (strcmp(fmt, "T,dev_t") == 0 ||
298 strcmp(fmt, "T,udev_t") == 0
300 set_T_dev_t((char*)newval, &newval,
306 errx(1, "oid '%s' is type %d,"
307 " cannot set that", name,
311 i = show_var(mib, len);
312 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
317 errx(1, "%s: value is not available",
320 errx(1, "%s: specification is incomplete",
323 errx(1, "%s: type is unknown to this program",
334 j = show_var(mib, len);
340 if (name_allocated != NULL)
341 free(name_allocated);
346 /* These functions will dump out various interesting structures. */
349 S_clockinfo(size_t l2, void *p)
351 struct clockinfo *ci = (struct clockinfo*)p;
352 if (l2 != sizeof(*ci))
353 err(1, "S_clockinfo %zu != %zu", l2, sizeof(*ci));
354 printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
355 ci->hz, ci->tick, ci->tickadj, ci->profhz, ci->stathz);
360 S_loadavg(size_t l2, void *p)
362 struct loadavg *tv = (struct loadavg*)p;
364 if (l2 != sizeof(*tv))
365 err(1, "S_loadavg %zu != %zu", l2, sizeof(*tv));
367 printf("{ %.2f %.2f %.2f }",
368 (double)tv->ldavg[0]/(double)tv->fscale,
369 (double)tv->ldavg[1]/(double)tv->fscale,
370 (double)tv->ldavg[2]/(double)tv->fscale);
375 S_timespec(size_t l2, void *p)
377 struct timespec *ts = (struct timespec*)p;
381 if (l2 != sizeof(*ts))
382 err(1, "S_timespec %zu != %zu", l2, sizeof(*ts));
383 printf("{ sec = %ld, nsec = %ld } ",
384 ts->tv_sec, ts->tv_nsec);
386 p1 = strdup(ctime(&tv_sec));
387 for (p2=p1; *p2 ; p2++)
395 S_timeval(size_t l2, void *p)
397 struct timeval *tv = (struct timeval*)p;
401 if (l2 != sizeof(*tv))
402 err(1, "S_timeval %zu != %zu", l2, sizeof(*tv));
403 printf("{ sec = %ld, usec = %ld } ",
404 tv->tv_sec, tv->tv_usec);
406 p1 = strdup(ctime(&tv_sec));
407 for (p2=p1; *p2 ; p2++)
415 S_sensor(size_t l2, void *p)
417 struct sensor *s = (struct sensor *)p;
419 if (l2 != sizeof(*s)) {
420 warnx("S_sensor %zu != %zu", l2, sizeof(*s));
424 if (s->flags & SENSOR_FINVALID) {
426 * XXX: with this flag, the node should be entirely ignored,
427 * but as the magic-based sysctl(8) is not too flexible, we
428 * simply have to print out that the sensor is invalid.
434 if (s->flags & SENSOR_FUNKNOWN)
440 (s->value - 273150000) / 1000000.0);
443 printf("%jd RPM", (intmax_t)s->value);
445 case SENSOR_VOLTS_DC:
446 printf("%.2f VDC", s->value / 1000000.0);
449 printf("%.2f W", s->value / 1000000.0);
452 printf("%.2f A", s->value / 1000000.0);
454 case SENSOR_WATTHOUR:
455 printf("%.2f Wh", s->value / 1000000.0);
458 printf("%.2f Ah", s->value / 1000000.0);
460 case SENSOR_INDICATOR:
461 printf("%s", s->value ? "On" : "Off");
464 printf("%jd Hz", (intmax_t)s->value);
468 printf("%jd", (intmax_t)s->value);
471 printf("%.2f%%", s->value / 1000.0);
474 printf("%.2f lx", s->value / 1000000.0);
481 case SENSOR_DRIVE_EMPTY:
484 case SENSOR_DRIVE_READY:
487 case SENSOR_DRIVE_POWERUP:
488 name = "powering up";
490 case SENSOR_DRIVE_ONLINE:
493 case SENSOR_DRIVE_IDLE:
496 case SENSOR_DRIVE_ACTIVE:
499 case SENSOR_DRIVE_REBUILD:
502 case SENSOR_DRIVE_POWERDOWN:
503 name = "powering down";
505 case SENSOR_DRIVE_FAIL:
508 case SENSOR_DRIVE_PFAIL:
518 case SENSOR_TIMEDELTA:
519 printf("%.6f secs", s->value / 1000000000.0);
526 if (s->desc[0] != '\0')
527 printf(" (%s)", s->desc);
530 case SENSOR_S_UNSPEC:
539 printf(", CRITICAL");
541 case SENSOR_S_UNKNOWN:
547 time_t t = s->tv.tv_sec;
552 printf(", %s.%03ld", ct, s->tv.tv_usec / 1000);
560 S_efi_map(size_t l2, void *p)
562 struct efi_map_header *efihdr;
568 static const char *types[] = {
574 "RuntimeServicesCode",
575 "RuntimeServicesData",
576 "ConventionalMemory",
581 "MemoryMappedIOPortSpace",
586 * Memory map data provided by UEFI via the GetMemoryMap
589 if (l2 < sizeof(*efihdr)) {
590 warnx("S_efi_map length less than header");
594 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
595 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
597 if (efihdr->descriptor_size == 0)
599 if (l2 != efisz + efihdr->memory_size) {
600 warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz +
601 efihdr->memory_size);
604 ndesc = efihdr->memory_size / efihdr->descriptor_size;
606 printf("\n%23s %12s %12s %8s %4s",
607 "Type", "Physical", "Virtual", "#Pages", "Attr");
609 for (i = 0; i < ndesc; i++,
610 map = efi_next_descriptor(map, efihdr->descriptor_size)) {
611 if (map->md_type <= EFI_MD_TYPE_PALCODE)
612 type = types[map->md_type];
615 printf("\n%23s %012lx %12p %08lx ", type, map->md_phys,
616 map->md_virt, map->md_pages);
617 if (map->md_attr & EFI_MD_ATTR_UC)
619 if (map->md_attr & EFI_MD_ATTR_WC)
621 if (map->md_attr & EFI_MD_ATTR_WT)
623 if (map->md_attr & EFI_MD_ATTR_WB)
625 if (map->md_attr & EFI_MD_ATTR_UCE)
627 if (map->md_attr & EFI_MD_ATTR_WP)
629 if (map->md_attr & EFI_MD_ATTR_RP)
631 if (map->md_attr & EFI_MD_ATTR_XP)
633 if (map->md_attr & EFI_MD_ATTR_RT)
641 T_dev_t(size_t l2, void *p)
643 dev_t *d = (dev_t *)p;
644 if (l2 != sizeof(*d))
645 err(1, "T_dev_T %zu != %zu", l2, sizeof(*d));
646 if ((int)(*d) != -1) {
647 if (minor(*d) > 255 || minor(*d) < 0)
648 printf("{ major = %d, minor = 0x%x }",
649 major(*d), minor(*d));
651 printf("{ major = %d, minor = %d }",
652 major(*d), minor(*d));
658 set_T_dev_t(const char *path, void **val, size_t *size)
660 static struct stat statb;
662 if (strcmp(path, "none") && strcmp(path, "off")) {
663 int rc = stat (path, &statb);
665 err(1, "cannot stat %s", path);
668 if (!S_ISCHR(statb.st_mode)) {
669 errx(1, "must specify a device special file.");
672 statb.st_rdev = NODEV;
674 *val = (char*) &statb.st_rdev;
675 *size = sizeof statb.st_rdev;
679 set_IK(const char *str, int *val)
686 if ((len = strlen(str)) == 0)
689 if (*p == 'C' || *p == 'F') {
690 temp = strtof(str, &endptr);
691 if (endptr == str || endptr != p)
694 temp = (temp - 32) * 5 / 9;
695 kelv = temp * 10 + 2732;
698 * I would like to just use, 0 but it would make numbers
699 * like '023' which were interpreted as decimal before
700 * suddenly interpreted as octal.
702 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
703 kelv = (int)strtol(str, &endptr, 0);
705 kelv = (int)strtol(str, &endptr, 10);
706 if (endptr == str || *endptr != '\0')
714 * These functions uses a presently undocumented interface to the kernel
715 * to walk the tree and get the type so it can print the value.
716 * This interface is under work and consideration, and should probably
717 * be killed with a big axe by the first person who can find the time.
718 * (be aware though, that the proper interface isn't as obvious as it
719 * may seem, there are various conflicting requirements.
723 oidfmt(int *oid, size_t len, char *fmt, u_int *kind)
725 int qoid[CTL_MAXNAME+2];
732 memcpy(qoid + 2, oid, len * sizeof(int));
735 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
737 err(1, "sysctl fmt %d %zu %d", i, j, errno);
740 *kind = *(u_int *)buf;
743 strcpy(fmt, (char *)(buf + sizeof(u_int)));
748 * This formats and outputs the value of one variable
750 * Returns zero if anything was actually output.
751 * Returns one if didn't know what to do with this.
752 * Return minus one if we had errors.
756 show_var(int *oid, size_t nlen)
758 u_char buf[BUFSIZ], *val = NULL, *p, *nul;
759 char name[BUFSIZ], *fmt;
760 const char *sep, *spacer;
761 int qoid[CTL_MAXNAME+2];
765 int (*func)(size_t, void *);
769 memcpy(qoid + 2, oid, nlen * sizeof(int));
773 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
775 err(1, "sysctl name %d %zu %d", i, j, errno);
787 if (dflag) { /* just print description */
790 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
792 printf("%s%s", name, sep);
796 /* find an estimate of how much we need for this var */
798 i = sysctl(oid, nlen, 0, &j, 0, 0);
799 j += j; /* we want to be sure :-) */
806 i = sysctl(oid, nlen, val, &len, 0, 0);
813 fwrite(val, 1, len, stdout);
819 oidfmt(oid, nlen, fmt, &kind);
824 printf("%s%s", name, sep);
825 nul = memchr(p, '\0', len);
826 fwrite(p, nul == NULL ? (int)len : nul - p, 1, stdout);
831 printf("%s%s", name, sep);
834 while (len >= sizeof(char)) {
836 printf("%s%hhu", spacer, *(unsigned char *)p);
838 printf("%s%hhd", spacer, *(char *)p);
847 printf("%s%s", name, sep);
850 while (len >= sizeof(int)) {
852 printf("%s%u", spacer, *(unsigned int *)p);
853 else if (*fmt == 'K' && *(int *)p >= 0)
854 printf("%s%.1fC", spacer, (*(int *)p - 2732) / 10.0);
856 printf("%s%d", spacer, *(int *)p);
865 printf("%s%s", name, sep);
868 while (len >= sizeof(long)) {
870 printf("%s%lu", spacer, *(unsigned long *)p);
872 printf("%s%ld", spacer, *(long *)p);
881 printf("%s%s", name, sep);
882 printf("%p", *(void **)p);
887 printf("%s%s", name, sep);
890 while (len >= sizeof(quad_t)) {
893 spacer, (uintmax_t)*(u_quad_t *)p);
896 spacer, (intmax_t)*(quad_t *)p);
899 len -= sizeof(int64_t);
900 p += sizeof(int64_t);
906 if (fmt[0] == 'S' && fmt[1] != ',') {
908 printf("%s%s", name, sep);
911 while (len >= sizeof(short)) {
913 printf("%s%hu", spacer,
914 *(unsigned short *)p);
916 printf("%s%hd", spacer, *(short *)p);
918 len -= sizeof(short);
924 if (!oflag && !xflag) {
926 if (strcmp(fmt, "S,clockinfo") == 0)
928 else if (strcmp(fmt, "S,timespec") == 0)
930 else if (strcmp(fmt, "S,timeval") == 0)
932 else if (strcmp(fmt, "S,loadavg") == 0)
934 else if (strcmp(fmt, "S,sensor") == 0)
937 else if (strcmp(fmt, "S,efi_map_header") == 0)
940 else if (strcmp(fmt, "T,dev_t") == 0)
942 else if (strcmp(fmt, "T,udev_t") == 0)
948 printf("%s%s", name, sep);
949 error = (*func)(len, p);
955 if (!oflag && !xflag) {
960 printf("%s%s", name, sep);
961 printf("Format:%s Length:%zu Dump:0x", fmt, len);
962 while (len-- && (xflag || p < val + 16))
963 printf("%02x", *p++);
964 if (!xflag && len > 16)
976 sysctl_all(int *oid, size_t len)
978 int name1[22], name2[22];
986 memcpy(name1+2, oid, len * sizeof(int));
994 retval = sysctl(name1, l1, name2, &l2, 0, 0);
999 err(1, "sysctl(getnext) %d %zu", retval, l2);
1007 for (i = 0; i < len; i++)
1008 if (name2[i] != oid[i])
1011 retval = show_var(name2, l2);
1012 if (retval == 0 && !bflag)
1015 memcpy(name1+2, name2, l2 * sizeof(int));