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
31 static const char copyright[] =
32 "@(#) Copyright (c) 1993\n\
33 The Regents of the University of California. All rights reserved.\n";
38 static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93";
40 static const char rcsid[] =
44 #include <sys/param.h>
46 #include <sys/resource.h>
48 #include <sys/sysctl.h>
49 #include <sys/vmmeter.h>
53 #include <machine/metadata.h>
56 #if defined(__amd64__) || defined(__i386__)
57 #include <machine/pc/bios.h>
72 static const char *conffile;
74 static int aflag, bflag, Bflag, dflag, eflag, hflag, iflag;
75 static int Nflag, nflag, oflag, qflag, tflag, Tflag, Wflag, xflag;
77 static int oidfmt(int *, int, char *, u_int *);
78 static int parsefile(const char *);
79 static int parse(const char *, int);
80 static int show_var(int *, int);
81 static int sysctl_all(int *oid, int len);
82 static int name2oid(const char *, int *);
84 static int strIKtoi(const char *, char **, const char *);
86 static int ctl_sign[CTLTYPE+1] = {
95 static int ctl_size[CTLTYPE+1] = {
96 [CTLTYPE_INT] = sizeof(int),
97 [CTLTYPE_UINT] = sizeof(u_int),
98 [CTLTYPE_LONG] = sizeof(long),
99 [CTLTYPE_ULONG] = sizeof(u_long),
100 [CTLTYPE_S8] = sizeof(int8_t),
101 [CTLTYPE_S16] = sizeof(int16_t),
102 [CTLTYPE_S32] = sizeof(int32_t),
103 [CTLTYPE_S64] = sizeof(int64_t),
104 [CTLTYPE_U8] = sizeof(uint8_t),
105 [CTLTYPE_U16] = sizeof(uint16_t),
106 [CTLTYPE_U32] = sizeof(uint32_t),
107 [CTLTYPE_U64] = sizeof(uint64_t),
110 static const char *ctl_typename[CTLTYPE+1] = {
111 [CTLTYPE_INT] = "integer",
112 [CTLTYPE_UINT] = "unsigned integer",
113 [CTLTYPE_LONG] = "long integer",
114 [CTLTYPE_ULONG] = "unsigned long",
115 [CTLTYPE_U8] = "uint8_t",
116 [CTLTYPE_U16] = "uint16_t",
117 [CTLTYPE_U32] = "uint32_t",
118 [CTLTYPE_U64] = "uint64_t",
119 [CTLTYPE_S8] = "int8_t",
120 [CTLTYPE_S16] = "int16_t",
121 [CTLTYPE_S32] = "int32_t",
122 [CTLTYPE_S64] = "int64_t",
123 [CTLTYPE_NODE] = "node",
124 [CTLTYPE_STRING] = "string",
125 [CTLTYPE_OPAQUE] = "opaque",
132 (void)fprintf(stderr, "%s\n%s\n",
133 "usage: sysctl [-bdehiNnoqTtWx] [ -B <bufsize> ] [-f filename] name[=value] ...",
134 " sysctl [-bdehNnoqTtWx] [ -B <bufsize> ] -a");
139 main(int argc, char **argv)
144 setlocale(LC_NUMERIC, "");
148 while ((ch = getopt(argc, argv, "AabB:def:hiNnoqtTwWxX")) != -1) {
161 Bflag = strtol(optarg, NULL, 0);
219 if (aflag && argc == 0)
220 exit(sysctl_all(0, 0));
221 if (argc == 0 && conffile == NULL)
225 if (conffile != NULL)
226 warncount += parsefile(conffile);
229 warncount += parse(*argv++, 0);
235 * Parse a name into a MIB entry.
236 * Lookup and print out the MIB entry if it exists.
237 * Set a new value if requested.
240 parse(const char *string, int lineno)
244 const char *newvalstr = NULL;
252 unsigned int uintval;
254 unsigned long ulongval;
255 size_t newsize = Bflag;
258 int mib[CTL_MAXNAME];
259 char *cp, *bufp, buf[BUFSIZ], *endptr = NULL, fmt[BUFSIZ], line[BUFSIZ];
263 snprintf(line, sizeof(line), " at line %d", lineno);
268 if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ) {
269 warnx("oid too long: '%s'%s", string, line);
272 bufp = strsep(&cp, "=:");
274 /* Tflag just lists tunables, do not allow assignment */
275 if (Tflag || Wflag) {
276 warnx("Can't set variables when using -T or -W");
281 /* Strip a pair of " or ' if any. */
285 if (cp[strlen(cp) - 1] == *cp)
286 cp[strlen(cp) - 1] = '\0';
290 newsize = strlen(cp);
293 cp = bufp + strlen(bufp) - 1;
294 while (cp >= bufp && isspace((int)*cp)) {
298 len = name2oid(bufp, mib);
306 if (errno == ENOENT) {
307 warnx("unknown oid '%s'%s", bufp, line);
309 warn("unknown oid '%s'%s", bufp, line);
315 if (oidfmt(mib, len, fmt, &kind)) {
316 warn("couldn't find format of oid '%s'%s", bufp, line);
323 if (newvalstr == NULL || dflag) {
324 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
326 i = show_var(mib, len);
330 sysctl_all(mib, len);
332 i = show_var(mib, len);
337 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
338 warnx("oid '%s' isn't a leaf node%s", bufp, line);
342 if (!(kind & CTLFLAG_WR)) {
343 if (kind & CTLFLAG_TUN) {
344 warnx("oid '%s' is a read only tunable%s", bufp, line);
345 warnx("Tunable values are set in /boot/loader.conf");
347 warnx("oid '%s' is read only%s", bufp, line);
351 switch (kind & CTLTYPE) {
364 if (strlen(newvalstr) == 0) {
365 warnx("empty numeric value");
372 warnx("oid '%s' is type %d,"
373 " cannot set that%s", bufp,
374 kind & CTLTYPE, line);
380 switch (kind & CTLTYPE) {
382 if (strncmp(fmt, "IK", 2) == 0)
383 intval = strIKtoi(newvalstr, &endptr, fmt);
385 intval = (int)strtol(newvalstr, &endptr,
388 newsize = sizeof(intval);
391 uintval = (int) strtoul(newvalstr, &endptr, 0);
393 newsize = sizeof(uintval);
396 longval = strtol(newvalstr, &endptr, 0);
398 newsize = sizeof(longval);
401 ulongval = strtoul(newvalstr, &endptr, 0);
403 newsize = sizeof(ulongval);
409 i8val = (int8_t)strtol(newvalstr, &endptr, 0);
411 newsize = sizeof(i8val);
414 i16val = (int16_t)strtol(newvalstr, &endptr,
417 newsize = sizeof(i16val);
420 i32val = (int32_t)strtol(newvalstr, &endptr,
423 newsize = sizeof(i32val);
426 i64val = strtoimax(newvalstr, &endptr, 0);
428 newsize = sizeof(i64val);
431 u8val = (uint8_t)strtoul(newvalstr, &endptr, 0);
433 newsize = sizeof(u8val);
436 u16val = (uint16_t)strtoul(newvalstr, &endptr,
439 newsize = sizeof(u16val);
442 u32val = (uint32_t)strtoul(newvalstr, &endptr,
445 newsize = sizeof(u32val);
448 u64val = strtoumax(newvalstr, &endptr, 0);
450 newsize = sizeof(u64val);
457 if (errno != 0 || endptr == newvalstr ||
458 (endptr != NULL && *endptr != '\0')) {
459 warnx("invalid %s '%s'%s", ctl_typename[kind & CTLTYPE],
464 i = show_var(mib, len);
465 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
470 warnx("%s: value is not available%s",
474 warnx("%s: specification is incomplete%s",
478 warnx("%s: type is unknown to this program%s",
482 warn("%s%s", string, line);
490 j = show_var(mib, len);
500 parsefile(const char *filename)
503 char line[BUFSIZ], *p, *pq, *pdq;
504 int warncount = 0, lineno = 0;
506 file = fopen(filename, "r");
508 err(EX_NOINPUT, "%s", filename);
509 while (fgets(line, sizeof(line), file) != NULL) {
512 pq = strchr(line, '\'');
513 pdq = strchr(line, '\"');
514 /* Replace the first # with \0. */
515 while((p = strchr(p, '#')) != NULL) {
516 if (pq != NULL && p > pq) {
517 if ((p = strchr(pq+1, '\'')) != NULL)
520 } else if (pdq != NULL && p > pdq) {
521 if ((p = strchr(pdq+1, '\"')) != NULL)
524 } else if (p == line || *(p-1) != '\\') {
531 p = line + strlen(line) - 1;
532 while (p >= line && isspace((int)*p)) {
537 while (isspace((int)*p))
542 warncount += parse(p, lineno);
549 /* These functions will dump out various interesting structures. */
552 S_clockinfo(size_t l2, void *p)
554 struct clockinfo *ci = (struct clockinfo*)p;
556 if (l2 != sizeof(*ci)) {
557 warnx("S_clockinfo %zu != %zu", l2, sizeof(*ci));
560 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
561 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
562 ci->hz, ci->tick, ci->profhz, ci->stathz);
567 S_loadavg(size_t l2, void *p)
569 struct loadavg *tv = (struct loadavg*)p;
571 if (l2 != sizeof(*tv)) {
572 warnx("S_loadavg %zu != %zu", l2, sizeof(*tv));
575 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
576 (double)tv->ldavg[0]/(double)tv->fscale,
577 (double)tv->ldavg[1]/(double)tv->fscale,
578 (double)tv->ldavg[2]/(double)tv->fscale);
583 S_timeval(size_t l2, void *p)
585 struct timeval *tv = (struct timeval*)p;
589 if (l2 != sizeof(*tv)) {
590 warnx("S_timeval %zu != %zu", l2, sizeof(*tv));
593 printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
594 "{ sec = %jd, usec = %ld } ",
595 (intmax_t)tv->tv_sec, tv->tv_usec);
597 p1 = strdup(ctime(&tv_sec));
598 for (p2=p1; *p2 ; p2++)
607 S_vmtotal(size_t l2, void *p)
609 struct vmtotal *v = (struct vmtotal *)p;
610 int pageKilo = getpagesize() / 1024;
612 if (l2 != sizeof(*v)) {
613 warnx("S_vmtotal %zu != %zu", l2, sizeof(*v));
618 "\nSystem wide totals computed every five seconds:"
619 " (values in kilobytes)\n");
620 printf("===============================================\n");
622 "Processes:\t\t(RUNQ: %hd Disk Wait: %hd Page Wait: "
624 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
626 "Virtual Memory:\t\t(Total: %jdK Active: %jdK)\n",
627 (intmax_t)v->t_vm * pageKilo, (intmax_t)v->t_avm * pageKilo);
628 printf("Real Memory:\t\t(Total: %jdK Active: %jdK)\n",
629 (intmax_t)v->t_rm * pageKilo, (intmax_t)v->t_arm * pageKilo);
630 printf("Shared Virtual Memory:\t(Total: %jdK Active: %jdK)\n",
631 (intmax_t)v->t_vmshr * pageKilo, (intmax_t)v->t_avmshr * pageKilo);
632 printf("Shared Real Memory:\t(Total: %jdK Active: %jdK)\n",
633 (intmax_t)v->t_rmshr * pageKilo, (intmax_t)v->t_armshr * pageKilo);
634 printf("Free Memory:\t%jdK", (intmax_t)v->t_free * pageKilo);
641 S_efi_map(size_t l2, void *p)
643 struct efi_map_header *efihdr;
649 static const char *types[] = {
655 "RuntimeServicesCode",
656 "RuntimeServicesData",
657 "ConventionalMemory",
662 "MemoryMappedIOPortSpace",
667 * Memory map data provided by UEFI via the GetMemoryMap
670 if (l2 < sizeof(*efihdr)) {
671 warnx("S_efi_map length less than header");
675 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
676 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
678 if (efihdr->descriptor_size == 0)
680 if (l2 != efisz + efihdr->memory_size) {
681 warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz +
682 efihdr->memory_size);
685 ndesc = efihdr->memory_size / efihdr->descriptor_size;
687 printf("\n%23s %12s %12s %8s %4s",
688 "Type", "Physical", "Virtual", "#Pages", "Attr");
690 for (i = 0; i < ndesc; i++,
691 map = efi_next_descriptor(map, efihdr->descriptor_size)) {
692 if (map->md_type <= EFI_MD_TYPE_PALCODE)
693 type = types[map->md_type];
696 printf("\n%23s %012lx %12p %08lx ", type, map->md_phys,
697 map->md_virt, map->md_pages);
698 if (map->md_attr & EFI_MD_ATTR_UC)
700 if (map->md_attr & EFI_MD_ATTR_WC)
702 if (map->md_attr & EFI_MD_ATTR_WT)
704 if (map->md_attr & EFI_MD_ATTR_WB)
706 if (map->md_attr & EFI_MD_ATTR_UCE)
708 if (map->md_attr & EFI_MD_ATTR_WP)
710 if (map->md_attr & EFI_MD_ATTR_RP)
712 if (map->md_attr & EFI_MD_ATTR_XP)
714 if (map->md_attr & EFI_MD_ATTR_RT)
721 #if defined(__amd64__) || defined(__i386__)
723 S_bios_smap_xattr(size_t l2, void *p)
725 struct bios_smap_xattr *smap, *end;
727 if (l2 % sizeof(*smap) != 0) {
728 warnx("S_bios_smap_xattr %zu is not a multiple of %zu", l2,
733 end = (struct bios_smap_xattr *)((char *)p + l2);
734 for (smap = p; smap < end; smap++)
735 printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx",
736 smap->type, smap->xattr, (uintmax_t)smap->base,
737 (uintmax_t)smap->length);
743 strIKtoi(const char *str, char **endptrp, const char *fmt)
754 /* caller already checked this */
758 * A format of "IK" is in deciKelvin. A format of "IK3" is in
759 * milliKelvin. The single digit following IK is log10 of the
760 * multiplying factor to convert Kelvin into the untis of this sysctl,
761 * or the dividing factor to convert the sysctl value to Kelvin. Numbers
762 * larger than 6 will run into precision issues with 32-bit integers.
763 * Characters that aren't ASCII digits after the 'K' are ignored. No
764 * localization is present because this is an interface from the kernel
765 * to this program (eg not an end-user interface), so isdigit() isn't
768 if (fmt[2] != '\0' && fmt[2] >= '0' && fmt[2] <= '9')
773 if (*p == 'C' || *p == 'F' || *p == 'K') {
774 temp = strtof(str, endptrp);
775 if (*endptrp != str && *endptrp == p && errno == 0) {
777 temp = (temp - 32) * 5 / 9;
781 for (i = 0; i < prec; i++)
783 return ((int)(temp + 0.5));
786 /* No unit specified -> treat it as a raw number */
787 kelv = (int)strtol(str, endptrp, 10);
788 if (*endptrp != str && *endptrp == p && errno == 0) {
799 * These functions uses a presently undocumented interface to the kernel
800 * to walk the tree and get the type so it can print the value.
801 * This interface is under work and consideration, and should probably
802 * be killed with a big axe by the first person who can find the time.
803 * (be aware though, that the proper interface isn't as obvious as it
804 * may seem, there are various conflicting requirements.
808 name2oid(const char *name, int *oidp)
817 j = CTL_MAXNAME * sizeof(int);
818 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
826 oidfmt(int *oid, int len, char *fmt, u_int *kind)
828 int qoid[CTL_MAXNAME+2];
835 memcpy(qoid + 2, oid, len * sizeof(int));
838 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
840 err(1, "sysctl fmt %d %zu %d", i, j, errno);
843 *kind = *(u_int *)buf;
846 strcpy(fmt, (char *)(buf + sizeof(u_int)));
851 * This formats and outputs the value of one variable
853 * Returns zero if anything was actually output.
854 * Returns one if didn't know what to do with this.
855 * Return minus one if we had errors.
858 show_var(int *oid, int nlen)
860 u_char buf[BUFSIZ], *val, *oval, *p;
861 char name[BUFSIZ], fmt[BUFSIZ];
862 const char *sep, *sep1, *prntype;
863 int qoid[CTL_MAXNAME+2];
866 int i, hexlen, sign, ctltype;
871 int (*func)(size_t, void *);
875 umv = mv = intlen = 0;
881 memcpy(qoid + 2, oid, nlen * sizeof(int));
885 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
887 err(1, "sysctl name %d %zu %d", i, j, errno);
889 oidfmt(oid, nlen, fmt, &kind);
890 /* if Wflag then only list sysctls that are writeable and not stats. */
891 if (Wflag && ((kind & CTLFLAG_WR) == 0 || (kind & CTLFLAG_STATS) != 0))
894 /* if Tflag then only list sysctls that are tuneables. */
895 if (Tflag && (kind & CTLFLAG_TUN) == 0)
908 ctltype = (kind & CTLTYPE);
909 if (tflag || dflag) {
911 printf("%s%s", name, sep);
912 if (ctl_typename[ctltype] != NULL)
913 prntype = ctl_typename[ctltype];
917 printf("%s%s", prntype, sep);
919 printf("%s", prntype);
924 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
928 /* find an estimate of how much we need for this var */
933 i = sysctl(oid, nlen, 0, &j, 0, 0);
934 j += j; /* we want to be sure :-) */
937 val = oval = malloc(j + 1);
939 warnx("malloc failed");
943 i = sysctl(oid, nlen, val, &len, 0, 0);
944 if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) {
950 fwrite(val, 1, len, stdout);
956 sign = ctl_sign[ctltype];
957 intlen = ctl_size[ctltype];
962 printf("%s%s", name, sep);
963 printf("%.*s", (int)len, p);
980 printf("%s%s", name, sep);
981 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
983 while (len >= intlen) {
984 switch (kind & CTLTYPE) {
1002 umv = *(uint16_t *)p;
1007 umv = *(uint32_t *)p;
1012 umv = *(uint64_t *)p;
1016 fputs(sep1, stdout);
1018 printf("%#0*jx", hexlen, umv);
1020 printf(hflag ? "%'ju" : "%ju", umv);
1021 else if (fmt[1] == 'K') {
1026 * See strIKtoi for details on fmt.
1030 prec = fmt[2] - '0';
1032 for (int i = 0; i < prec; i++)
1034 printf("%.*fC", prec,
1035 (float)mv / base - 273.15);
1038 printf(hflag ? "%'jd" : "%jd", mv);
1046 case CTLTYPE_OPAQUE:
1048 if (strcmp(fmt, "S,clockinfo") == 0)
1050 else if (strcmp(fmt, "S,timeval") == 0)
1052 else if (strcmp(fmt, "S,loadavg") == 0)
1054 else if (strcmp(fmt, "S,vmtotal") == 0)
1057 else if (strcmp(fmt, "S,efi_map_header") == 0)
1060 #if defined(__amd64__) || defined(__i386__)
1061 else if (strcmp(fmt, "S,bios_smap_xattr") == 0)
1062 func = S_bios_smap_xattr;
1068 printf("%s%s", name, sep);
1069 i = (*func)(len, p);
1075 if (!oflag && !xflag) {
1080 printf("%s%s", name, sep);
1081 printf("Format:%s Length:%zu Dump:0x", fmt, len);
1082 while (len-- && (xflag || p < val + 16))
1083 printf("%02x", *p++);
1084 if (!xflag && len > 16)
1094 sysctl_all(int *oid, int len)
1096 int name1[22], name2[22];
1104 memcpy(name1+2, oid, len * sizeof(int));
1112 j = sysctl(name1, l1, name2, &l2, 0, 0);
1114 if (errno == ENOENT)
1117 err(1, "sysctl(getnext) %d %zu", j, l2);
1122 if (len < 0 || l2 < (unsigned int)len)
1125 for (i = 0; i < len; i++)
1126 if (name2[i] != oid[i])
1129 i = show_var(name2, l2);
1133 memcpy(name1+2, name2, l2 * sizeof(int));