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>
46 #include <machine/inttypes.h>
57 static int aflag, bflag, dflag, eflag, Nflag, nflag, oflag, xflag;
58 static int iflag, qflag;
60 static int oidfmt(int *, size_t, char *, u_int *);
61 static int parse(const char *);
62 static int show_var(int *, size_t);
63 static int sysctl_all(int *, size_t);
64 static void set_T_dev_t(const char *, void **, size_t *);
65 static int set_IK(const char *, int *);
71 fprintf(stderr, "%s\n%s\n",
72 "usage: sysctl [-bdeNnox] variable[=value] ...",
73 " sysctl [-bdeNnox] -a");
78 main(int argc, char **argv)
86 while ((ch = getopt(argc, argv, "AabdeiNnoqwxX")) != -1) {
139 if (aflag && argc == 0)
140 exit(sysctl_all(0, 0));
145 warncount += parse(*argv++);
151 * Parse a name into a MIB entry.
152 * Lookup and print out the MIB entry if it exists.
153 * Set a new value if requested.
156 parse(const char *string)
170 unsigned int uintval;
172 unsigned long ulongval;
174 int mib[CTL_MAXNAME];
175 char *cp, fmt[BUFSIZ];
177 char *name_allocated = NULL;
180 if ((cp = strchr(string, '=')) != NULL) {
181 if ((name_allocated = malloc(cp - string + 1)) == NULL)
182 err(1, "malloc failed");
183 strlcpy(name_allocated, string, cp - string + 1);
184 name = name_allocated;
186 while (isspace(*++cp))
190 newsize = strlen(cp);
196 if (sysctlnametomib(name, mib, &len) < 0) {
201 if (errno == ENOENT) {
202 errx(1, "unknown oid '%s'", name);
204 err(1, "sysctlnametomib(\"%s\")", name);
208 if (oidfmt(mib, len, fmt, &kind)) {
209 warn("couldn't find format of oid '%s'", name);
215 if (newval == NULL) {
216 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
217 sysctl_all(mib, len);
219 i = show_var(mib, len);
224 if ((kind & CTLTYPE) == CTLTYPE_NODE)
225 errx(1, "oid '%s' isn't a leaf node", name);
227 if (!(kind&CTLFLAG_WR))
228 errx(1, "oid '%s' is read only", name);
230 switch (kind & CTLTYPE) {
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);
239 newsize = sizeof(intval);
242 uintval = (int) strtoul(newval, NULL, 0);
244 newsize = sizeof uintval;
247 longval = strtol(newval, NULL, 0);
249 newsize = sizeof longval;
252 ulongval = strtoul(newval, NULL, 0);
254 newsize = sizeof ulongval;
259 i8val = (int8_t)strtol(newval, NULL, 0);
261 newsize = sizeof(i8val);
264 i16val = (int16_t)strtol(newval, NULL, 0);
266 newsize = sizeof(i16val);
269 i32val = (int32_t)strtol(newval, NULL, 0);
271 newsize = sizeof(i32val);
274 i64val = strtoimax(newval, NULL, 0);
276 newsize = sizeof(i64val);
279 u8val = (uint8_t)strtoul(newval, NULL, 0);
281 newsize = sizeof(u8val);
284 u16val = (uint16_t)strtoul(newval, NULL, 0);
286 newsize = sizeof(u16val);
289 u32val = (uint32_t)strtoul(newval, NULL, 0);
291 newsize = sizeof(u32val);
294 u64val = strtoumax(newval, NULL, 0);
296 newsize = sizeof(u64val);
299 if (strcmp(fmt, "T,dev_t") == 0 ||
300 strcmp(fmt, "T,udev_t") == 0
302 set_T_dev_t((char*)newval, &newval,
308 errx(1, "oid '%s' is type %d,"
309 " cannot set that", name,
313 i = show_var(mib, len);
314 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
319 errx(1, "%s: value is not available",
322 errx(1, "%s: specification is incomplete",
325 errx(1, "%s: type is unknown to this program",
336 j = show_var(mib, len);
342 if (name_allocated != NULL)
343 free(name_allocated);
348 /* These functions will dump out various interesting structures. */
351 S_clockinfo(size_t l2, void *p)
353 struct clockinfo *ci = (struct clockinfo*)p;
354 if (l2 != sizeof(*ci))
355 err(1, "S_clockinfo %zu != %zu", l2, sizeof(*ci));
356 printf("{ hz = %d, tick = %d, tickadj = %d, profhz = %d, stathz = %d }",
357 ci->hz, ci->tick, ci->tickadj, ci->profhz, ci->stathz);
362 S_loadavg(size_t l2, void *p)
364 struct loadavg *tv = (struct loadavg*)p;
366 if (l2 != sizeof(*tv))
367 err(1, "S_loadavg %zu != %zu", l2, sizeof(*tv));
369 printf("{ %.2f %.2f %.2f }",
370 (double)tv->ldavg[0]/(double)tv->fscale,
371 (double)tv->ldavg[1]/(double)tv->fscale,
372 (double)tv->ldavg[2]/(double)tv->fscale);
377 S_timespec(size_t l2, void *p)
379 struct timespec *ts = (struct timespec*)p;
383 if (l2 != sizeof(*ts))
384 err(1, "S_timespec %zu != %zu", l2, sizeof(*ts));
385 printf("{ sec = %ld, nsec = %ld } ",
386 ts->tv_sec, ts->tv_nsec);
388 p1 = strdup(ctime(&tv_sec));
389 for (p2=p1; *p2 ; p2++)
397 S_timeval(size_t l2, void *p)
399 struct timeval *tv = (struct timeval*)p;
403 if (l2 != sizeof(*tv))
404 err(1, "S_timeval %zu != %zu", l2, sizeof(*tv));
405 printf("{ sec = %ld, usec = %ld } ",
406 tv->tv_sec, tv->tv_usec);
408 p1 = strdup(ctime(&tv_sec));
409 for (p2=p1; *p2 ; p2++)
417 S_sensor(size_t l2, void *p)
419 struct sensor *s = (struct sensor *)p;
421 if (l2 != sizeof(*s)) {
422 warnx("S_sensor %zu != %zu", l2, sizeof(*s));
426 if (s->flags & SENSOR_FINVALID) {
428 * XXX: with this flag, the node should be entirely ignored,
429 * but as the magic-based sysctl(8) is not too flexible, we
430 * simply have to print out that the sensor is invalid.
436 if (s->flags & SENSOR_FUNKNOWN)
442 (s->value - 273150000) / 1000000.0);
445 printf("%jd RPM", (intmax_t)s->value);
447 case SENSOR_VOLTS_DC:
448 printf("%.2f VDC", s->value / 1000000.0);
451 printf("%.2f W", s->value / 1000000.0);
454 printf("%.2f A", s->value / 1000000.0);
456 case SENSOR_WATTHOUR:
457 printf("%.2f Wh", s->value / 1000000.0);
460 printf("%.2f Ah", s->value / 1000000.0);
462 case SENSOR_INDICATOR:
463 printf("%s", s->value ? "On" : "Off");
466 printf("%jd Hz", (intmax_t)s->value);
470 printf("%jd", (intmax_t)s->value);
473 printf("%.2f%%", s->value / 1000.0);
476 printf("%.2f lx", s->value / 1000000.0);
483 case SENSOR_DRIVE_EMPTY:
486 case SENSOR_DRIVE_READY:
489 case SENSOR_DRIVE_POWERUP:
490 name = "powering up";
492 case SENSOR_DRIVE_ONLINE:
495 case SENSOR_DRIVE_IDLE:
498 case SENSOR_DRIVE_ACTIVE:
501 case SENSOR_DRIVE_REBUILD:
504 case SENSOR_DRIVE_POWERDOWN:
505 name = "powering down";
507 case SENSOR_DRIVE_FAIL:
510 case SENSOR_DRIVE_PFAIL:
520 case SENSOR_TIMEDELTA:
521 printf("%.6f secs", s->value / 1000000000.0);
528 if (s->desc[0] != '\0')
529 printf(" (%s)", s->desc);
532 case SENSOR_S_UNSPEC:
541 printf(", CRITICAL");
543 case SENSOR_S_UNKNOWN:
549 time_t t = s->tv.tv_sec;
554 printf(", %s.%03ld", ct, s->tv.tv_usec / 1000);
562 S_efi_map(size_t l2, void *p)
564 struct efi_map_header *efihdr;
570 static const char *types[] = {
576 "RuntimeServicesCode",
577 "RuntimeServicesData",
578 "ConventionalMemory",
583 "MemoryMappedIOPortSpace",
588 * Memory map data provided by UEFI via the GetMemoryMap
591 if (l2 < sizeof(*efihdr)) {
592 warnx("S_efi_map length less than header");
596 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
597 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
599 if (efihdr->descriptor_size == 0)
601 if (l2 != efisz + efihdr->memory_size) {
602 warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz +
603 efihdr->memory_size);
606 ndesc = efihdr->memory_size / efihdr->descriptor_size;
608 printf("\n%23s %12s %12s %8s %4s",
609 "Type", "Physical", "Virtual", "#Pages", "Attr");
611 for (i = 0; i < ndesc; i++,
612 map = efi_next_descriptor(map, efihdr->descriptor_size)) {
613 if (map->md_type <= EFI_MD_TYPE_PALCODE)
614 type = types[map->md_type];
617 printf("\n%23s %012lx %12p %08lx ", type, map->md_phys,
618 map->md_virt, map->md_pages);
619 if (map->md_attr & EFI_MD_ATTR_UC)
621 if (map->md_attr & EFI_MD_ATTR_WC)
623 if (map->md_attr & EFI_MD_ATTR_WT)
625 if (map->md_attr & EFI_MD_ATTR_WB)
627 if (map->md_attr & EFI_MD_ATTR_UCE)
629 if (map->md_attr & EFI_MD_ATTR_WP)
631 if (map->md_attr & EFI_MD_ATTR_RP)
633 if (map->md_attr & EFI_MD_ATTR_XP)
635 if (map->md_attr & EFI_MD_ATTR_RT)
643 T_dev_t(size_t l2, void *p)
645 dev_t *d = (dev_t *)p;
646 if (l2 != sizeof(*d))
647 err(1, "T_dev_T %zu != %zu", l2, sizeof(*d));
648 if ((int)(*d) != -1) {
649 if (minor(*d) > 255 || minor(*d) < 0)
650 printf("{ major = %d, minor = 0x%x }",
651 major(*d), minor(*d));
653 printf("{ major = %d, minor = %d }",
654 major(*d), minor(*d));
660 set_T_dev_t(const char *path, void **val, size_t *size)
662 static struct stat statb;
664 if (strcmp(path, "none") && strcmp(path, "off")) {
665 int rc = stat (path, &statb);
667 err(1, "cannot stat %s", path);
670 if (!S_ISCHR(statb.st_mode)) {
671 errx(1, "must specify a device special file.");
674 statb.st_rdev = NODEV;
676 *val = (char*) &statb.st_rdev;
677 *size = sizeof statb.st_rdev;
681 set_IK(const char *str, int *val)
688 if ((len = strlen(str)) == 0)
691 if (*p == 'C' || *p == 'F') {
692 temp = strtof(str, &endptr);
693 if (endptr == str || endptr != p)
696 temp = (temp - 32) * 5 / 9;
697 kelv = temp * 10 + 2732;
700 * I would like to just use, 0 but it would make numbers
701 * like '023' which were interpreted as decimal before
702 * suddenly interpreted as octal.
704 if (str[0] == '0' && (str[1] == 'x' || str[1] == 'X'))
705 kelv = (int)strtol(str, &endptr, 0);
707 kelv = (int)strtol(str, &endptr, 10);
708 if (endptr == str || *endptr != '\0')
716 * These functions uses a presently undocumented interface to the kernel
717 * to walk the tree and get the type so it can print the value.
718 * This interface is under work and consideration, and should probably
719 * be killed with a big axe by the first person who can find the time.
720 * (be aware though, that the proper interface isn't as obvious as it
721 * may seem, there are various conflicting requirements.
725 oidfmt(int *oid, size_t len, char *fmt, u_int *kind)
727 int qoid[CTL_MAXNAME+2];
734 memcpy(qoid + 2, oid, len * sizeof(int));
737 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
739 err(1, "sysctl fmt %d %zu %d", i, j, errno);
742 *kind = *(u_int *)buf;
745 strcpy(fmt, (char *)(buf + sizeof(u_int)));
750 * This formats and outputs the value of one variable
752 * Returns zero if anything was actually output.
753 * Returns one if didn't know what to do with this.
754 * Return minus one if we had errors.
758 show_var(int *oid, size_t nlen)
760 u_char buf[BUFSIZ], *val = NULL, *p, *nul;
761 char name[BUFSIZ], *fmt;
762 const char *sep, *spacer;
763 int qoid[CTL_MAXNAME+2];
767 int (*func)(size_t, void *);
771 memcpy(qoid + 2, oid, nlen * sizeof(int));
775 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
777 err(1, "sysctl name %d %zu %d", i, j, errno);
789 if (dflag) { /* just print description */
792 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
794 printf("%s%s", name, sep);
798 /* find an estimate of how much we need for this var */
800 i = sysctl(oid, nlen, 0, &j, 0, 0);
801 j += j; /* we want to be sure :-) */
808 i = sysctl(oid, nlen, val, &len, 0, 0);
815 fwrite(val, 1, len, stdout);
821 oidfmt(oid, nlen, fmt, &kind);
826 printf("%s%s", name, sep);
827 nul = memchr(p, '\0', len);
828 fwrite(p, nul == NULL ? (int)len : nul - p, 1, stdout);
833 printf("%s%s", name, sep);
836 while (len >= sizeof(int)) {
838 printf("%s%u", spacer, *(unsigned int *)p);
839 else if (*fmt == 'K' && *(int *)p >= 0)
840 printf("%s%.1fC", spacer, (*(int *)p - 2732) / 10.0);
842 printf("%s%d", spacer, *(int *)p);
851 printf("%s%s", name, sep);
854 while (len >= sizeof(long)) {
856 printf("%s%lu", spacer, *(unsigned long *)p);
858 printf("%s%ld", spacer, *(long *)p);
867 printf("%s%s", name, sep);
868 printf("%p", *(void **)p);
873 printf("%s%s", name, sep);
876 while (len >= sizeof(quad_t)) {
879 spacer, (uintmax_t)*(u_quad_t *)p);
882 spacer, (intmax_t)*(quad_t *)p);
885 len -= sizeof(int64_t);
886 p += sizeof(int64_t);
892 if (!oflag && !xflag) {
894 if (strcmp(fmt, "S,clockinfo") == 0)
896 else if (strcmp(fmt, "S,timespec") == 0)
898 else if (strcmp(fmt, "S,timeval") == 0)
900 else if (strcmp(fmt, "S,loadavg") == 0)
902 else if (strcmp(fmt, "S,sensor") == 0)
905 else if (strcmp(fmt, "S,efi_map_header") == 0)
908 else if (strcmp(fmt, "T,dev_t") == 0)
910 else if (strcmp(fmt, "T,udev_t") == 0)
916 printf("%s%s", name, sep);
917 error = (*func)(len, p);
923 if (!oflag && !xflag) {
928 printf("%s%s", name, sep);
929 printf("Format:%s Length:%zu Dump:0x", fmt, len);
930 while (len-- && (xflag || p < val + 16))
931 printf("%02x", *p++);
932 if (!xflag && len > 16)
944 sysctl_all(int *oid, size_t len)
946 int name1[22], name2[22];
954 memcpy(name1+2, oid, len * sizeof(int));
962 retval = sysctl(name1, l1, name2, &l2, 0, 0);
967 err(1, "sysctl(getnext) %d %zu", retval, l2);
975 for (i = 0; i < len; i++)
976 if (name2[i] != oid[i])
979 retval = show_var(name2, l2);
980 if (retval == 0 && !bflag)
983 memcpy(name1+2, name2, l2 * sizeof(int));