2 * SPDX-License-Identifier: BSD-3-Clause
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)from: sysctl.c 8.1 (Berkeley) 6/6/93";
42 static const char rcsid[] =
46 #include <sys/param.h>
48 #include <sys/resource.h>
50 #include <sys/sysctl.h>
51 #include <sys/vmmeter.h>
55 #include <machine/metadata.h>
58 #if defined(__amd64__) || defined(__i386__)
59 #include <machine/pc/bios.h>
75 static const char *conffile;
77 static int aflag, bflag, Bflag, dflag, eflag, hflag, iflag;
78 static int Nflag, nflag, oflag, qflag, tflag, Tflag, Wflag, xflag;
80 static int oidfmt(int *, int, char *, u_int *);
81 static int parsefile(const char *);
82 static int parse(const char *, int);
83 static int show_var(int *, int);
84 static int sysctl_all(int *oid, int len);
85 static int name2oid(const char *, int *);
87 static int strIKtoi(const char *, char **, const char *);
89 static int ctl_sign[CTLTYPE+1] = {
98 static int ctl_size[CTLTYPE+1] = {
99 [CTLTYPE_INT] = sizeof(int),
100 [CTLTYPE_UINT] = sizeof(u_int),
101 [CTLTYPE_LONG] = sizeof(long),
102 [CTLTYPE_ULONG] = sizeof(u_long),
103 [CTLTYPE_S8] = sizeof(int8_t),
104 [CTLTYPE_S16] = sizeof(int16_t),
105 [CTLTYPE_S32] = sizeof(int32_t),
106 [CTLTYPE_S64] = sizeof(int64_t),
107 [CTLTYPE_U8] = sizeof(uint8_t),
108 [CTLTYPE_U16] = sizeof(uint16_t),
109 [CTLTYPE_U32] = sizeof(uint32_t),
110 [CTLTYPE_U64] = sizeof(uint64_t),
113 static const char *ctl_typename[CTLTYPE+1] = {
114 [CTLTYPE_INT] = "integer",
115 [CTLTYPE_UINT] = "unsigned integer",
116 [CTLTYPE_LONG] = "long integer",
117 [CTLTYPE_ULONG] = "unsigned long",
118 [CTLTYPE_U8] = "uint8_t",
119 [CTLTYPE_U16] = "uint16_t",
120 [CTLTYPE_U32] = "uint32_t",
121 [CTLTYPE_U64] = "uint64_t",
122 [CTLTYPE_S8] = "int8_t",
123 [CTLTYPE_S16] = "int16_t",
124 [CTLTYPE_S32] = "int32_t",
125 [CTLTYPE_S64] = "int64_t",
126 [CTLTYPE_NODE] = "node",
127 [CTLTYPE_STRING] = "string",
128 [CTLTYPE_OPAQUE] = "opaque",
135 (void)fprintf(stderr, "%s\n%s\n",
136 "usage: sysctl [-bdehiNnoqTtWx] [ -B <bufsize> ] [-f filename] name[=value] ...",
137 " sysctl [-bdehNnoqTtWx] [ -B <bufsize> ] -a");
142 main(int argc, char **argv)
147 setlocale(LC_NUMERIC, "");
151 while ((ch = getopt(argc, argv, "AabB:def:hiNnoqtTwWxX")) != -1) {
164 Bflag = strtol(optarg, NULL, 0);
222 if (aflag && argc == 0)
223 exit(sysctl_all(0, 0));
224 if (argc == 0 && conffile == NULL)
228 if (conffile != NULL)
229 warncount += parsefile(conffile);
232 warncount += parse(*argv++, 0);
238 * Parse a single numeric value, append it to 'newbuf', and update
239 * 'newsize'. Returns true if the value was parsed and false if the
240 * value was invalid. Non-numeric types (strings) are handled
241 * directly in parse().
244 parse_numeric(const char *newvalstr, const char *fmt, u_int kind,
245 void **newbufp, size_t *newsizep)
256 unsigned int uintval;
258 unsigned long ulongval;
266 switch (kind & CTLTYPE) {
268 if (strncmp(fmt, "IK", 2) == 0)
269 intval = strIKtoi(newvalstr, &endptr, fmt);
271 intval = (int)strtol(newvalstr, &endptr, 0);
273 valsize = sizeof(intval);
276 uintval = (int) strtoul(newvalstr, &endptr, 0);
278 valsize = sizeof(uintval);
281 longval = strtol(newvalstr, &endptr, 0);
283 valsize = sizeof(longval);
286 ulongval = strtoul(newvalstr, &endptr, 0);
288 valsize = sizeof(ulongval);
291 i8val = (int8_t)strtol(newvalstr, &endptr, 0);
293 valsize = sizeof(i8val);
296 i16val = (int16_t)strtol(newvalstr, &endptr, 0);
298 valsize = sizeof(i16val);
301 i32val = (int32_t)strtol(newvalstr, &endptr, 0);
303 valsize = sizeof(i32val);
306 i64val = strtoimax(newvalstr, &endptr, 0);
308 valsize = sizeof(i64val);
311 u8val = (uint8_t)strtoul(newvalstr, &endptr, 0);
313 valsize = sizeof(u8val);
316 u16val = (uint16_t)strtoul(newvalstr, &endptr, 0);
318 valsize = sizeof(u16val);
321 u32val = (uint32_t)strtoul(newvalstr, &endptr, 0);
323 valsize = sizeof(u32val);
326 u64val = strtoumax(newvalstr, &endptr, 0);
328 valsize = sizeof(u64val);
335 if (errno != 0 || endptr == newvalstr ||
336 (endptr != NULL && *endptr != '\0'))
339 newbuf = realloc(*newbufp, *newsizep + valsize);
341 err(1, "out of memory");
342 memcpy((char *)newbuf + *newsizep, newval, valsize);
344 *newsizep += valsize;
350 * Parse a name into a MIB entry.
351 * Lookup and print out the MIB entry if it exists.
352 * Set a new value if requested.
355 parse(const char *string, int lineno)
359 char *newvalstr = NULL;
361 size_t newsize = Bflag;
362 int mib[CTL_MAXNAME];
363 char *cp, *bufp, buf[BUFSIZ], fmt[BUFSIZ], line[BUFSIZ];
367 snprintf(line, sizeof(line), " at line %d", lineno);
372 if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ) {
373 warnx("oid too long: '%s'%s", string, line);
376 bufp = strsep(&cp, "=:");
378 /* Tflag just lists tunables, do not allow assignment */
379 if (Tflag || Wflag) {
380 warnx("Can't set variables when using -T or -W");
385 /* Strip a pair of " or ' if any. */
389 if (cp[strlen(cp) - 1] == *cp)
390 cp[strlen(cp) - 1] = '\0';
394 newsize = strlen(cp);
397 cp = bufp + strlen(bufp) - 1;
398 while (cp >= bufp && isspace((int)*cp)) {
402 len = name2oid(bufp, mib);
410 if (errno == ENOENT) {
411 warnx("unknown oid '%s'%s", bufp, line);
413 warn("unknown oid '%s'%s", bufp, line);
419 if (oidfmt(mib, len, fmt, &kind)) {
420 warn("couldn't find format of oid '%s'%s", bufp, line);
427 if (newvalstr == NULL || dflag) {
428 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
430 i = show_var(mib, len);
434 sysctl_all(mib, len);
436 i = show_var(mib, len);
441 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
442 warnx("oid '%s' isn't a leaf node%s", bufp, line);
446 if (!(kind & CTLFLAG_WR)) {
447 if (kind & CTLFLAG_TUN) {
448 warnx("oid '%s' is a read only tunable%s", bufp, line);
449 warnx("Tunable values are set in /boot/loader.conf");
451 warnx("oid '%s' is read only%s", bufp, line);
455 switch (kind & CTLTYPE) {
468 if (strlen(newvalstr) == 0) {
469 warnx("empty numeric value");
476 warnx("oid '%s' is type %d,"
477 " cannot set that%s", bufp,
478 kind & CTLTYPE, line);
484 switch (kind & CTLTYPE) {
490 while ((cp = strsep(&newvalstr, " ,")) != NULL) {
493 if (!parse_numeric(cp, fmt, kind, &newbuf,
495 warnx("invalid %s '%s'%s",
496 ctl_typename[kind & CTLTYPE],
506 i = show_var(mib, len);
507 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
513 warnx("%s: value is not available%s",
517 warnx("%s: specification is incomplete%s",
521 warnx("%s: type is unknown to this program%s",
525 warn("%s%s", string, line);
534 j = show_var(mib, len);
544 parsefile(const char *filename)
547 char line[BUFSIZ], *p, *pq, *pdq;
548 int warncount = 0, lineno = 0;
550 file = fopen(filename, "r");
552 err(EX_NOINPUT, "%s", filename);
553 while (fgets(line, sizeof(line), file) != NULL) {
556 pq = strchr(line, '\'');
557 pdq = strchr(line, '\"');
558 /* Replace the first # with \0. */
559 while((p = strchr(p, '#')) != NULL) {
560 if (pq != NULL && p > pq) {
561 if ((p = strchr(pq+1, '\'')) != NULL)
564 } else if (pdq != NULL && p > pdq) {
565 if ((p = strchr(pdq+1, '\"')) != NULL)
568 } else if (p == line || *(p-1) != '\\') {
575 p = line + strlen(line) - 1;
576 while (p >= line && isspace((int)*p)) {
581 while (isspace((int)*p))
586 warncount += parse(p, lineno);
593 /* These functions will dump out various interesting structures. */
596 S_clockinfo(size_t l2, void *p)
598 struct clockinfo *ci = (struct clockinfo*)p;
600 if (l2 != sizeof(*ci)) {
601 warnx("S_clockinfo %zu != %zu", l2, sizeof(*ci));
604 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
605 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
606 ci->hz, ci->tick, ci->profhz, ci->stathz);
611 S_loadavg(size_t l2, void *p)
613 struct loadavg *tv = (struct loadavg*)p;
615 if (l2 != sizeof(*tv)) {
616 warnx("S_loadavg %zu != %zu", l2, sizeof(*tv));
619 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
620 (double)tv->ldavg[0]/(double)tv->fscale,
621 (double)tv->ldavg[1]/(double)tv->fscale,
622 (double)tv->ldavg[2]/(double)tv->fscale);
627 S_timeval(size_t l2, void *p)
629 struct timeval *tv = (struct timeval*)p;
633 if (l2 != sizeof(*tv)) {
634 warnx("S_timeval %zu != %zu", l2, sizeof(*tv));
637 printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
638 "{ sec = %jd, usec = %ld } ",
639 (intmax_t)tv->tv_sec, tv->tv_usec);
641 p1 = strdup(ctime(&tv_sec));
642 for (p2=p1; *p2 ; p2++)
651 S_vmtotal(size_t l2, void *p)
656 if (l2 != sizeof(*v)) {
657 warnx("S_vmtotal %zu != %zu", l2, sizeof(*v));
662 pageKilo = getpagesize() / 1024;
664 #define pg2k(a) ((uintmax_t)(a) * pageKilo)
665 printf("\nSystem wide totals computed every five seconds:"
666 " (values in kilobytes)\n");
667 printf("===============================================\n");
668 printf("Processes:\t\t(RUNQ: %d Disk Wait: %d Page Wait: "
670 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
671 printf("Virtual Memory:\t\t(Total: %juK Active: %juK)\n",
672 pg2k(v->t_vm), pg2k(v->t_avm));
673 printf("Real Memory:\t\t(Total: %juK Active: %juK)\n",
674 pg2k(v->t_rm), pg2k(v->t_arm));
675 printf("Shared Virtual Memory:\t(Total: %juK Active: %juK)\n",
676 pg2k(v->t_vmshr), pg2k(v->t_avmshr));
677 printf("Shared Real Memory:\t(Total: %juK Active: %juK)\n",
678 pg2k(v->t_rmshr), pg2k(v->t_armshr));
679 printf("Free Memory:\t%juK", pg2k(v->t_free));
685 S_efi_map(size_t l2, void *p)
687 struct efi_map_header *efihdr;
693 static const char *types[] = {
699 "RuntimeServicesCode",
700 "RuntimeServicesData",
701 "ConventionalMemory",
706 "MemoryMappedIOPortSpace",
711 * Memory map data provided by UEFI via the GetMemoryMap
714 if (l2 < sizeof(*efihdr)) {
715 warnx("S_efi_map length less than header");
719 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
720 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
722 if (efihdr->descriptor_size == 0)
724 if (l2 != efisz + efihdr->memory_size) {
725 warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz +
726 efihdr->memory_size);
729 ndesc = efihdr->memory_size / efihdr->descriptor_size;
731 printf("\n%23s %12s %12s %8s %4s",
732 "Type", "Physical", "Virtual", "#Pages", "Attr");
734 for (i = 0; i < ndesc; i++,
735 map = efi_next_descriptor(map, efihdr->descriptor_size)) {
736 if (map->md_type <= EFI_MD_TYPE_PALCODE)
737 type = types[map->md_type];
740 printf("\n%23s %012jx %12p %08jx ", type,
741 (uintmax_t)map->md_phys, map->md_virt,
742 (uintmax_t)map->md_pages);
743 if (map->md_attr & EFI_MD_ATTR_UC)
745 if (map->md_attr & EFI_MD_ATTR_WC)
747 if (map->md_attr & EFI_MD_ATTR_WT)
749 if (map->md_attr & EFI_MD_ATTR_WB)
751 if (map->md_attr & EFI_MD_ATTR_UCE)
753 if (map->md_attr & EFI_MD_ATTR_WP)
755 if (map->md_attr & EFI_MD_ATTR_RP)
757 if (map->md_attr & EFI_MD_ATTR_XP)
759 if (map->md_attr & EFI_MD_ATTR_RT)
766 #if defined(__amd64__) || defined(__i386__)
768 S_bios_smap_xattr(size_t l2, void *p)
770 struct bios_smap_xattr *smap, *end;
772 if (l2 % sizeof(*smap) != 0) {
773 warnx("S_bios_smap_xattr %zu is not a multiple of %zu", l2,
778 end = (struct bios_smap_xattr *)((char *)p + l2);
779 for (smap = p; smap < end; smap++)
780 printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx",
781 smap->type, smap->xattr, (uintmax_t)smap->base,
782 (uintmax_t)smap->length);
788 strIKtoi(const char *str, char **endptrp, const char *fmt)
799 /* caller already checked this */
803 * A format of "IK" is in deciKelvin. A format of "IK3" is in
804 * milliKelvin. The single digit following IK is log10 of the
805 * multiplying factor to convert Kelvin into the untis of this sysctl,
806 * or the dividing factor to convert the sysctl value to Kelvin. Numbers
807 * larger than 6 will run into precision issues with 32-bit integers.
808 * Characters that aren't ASCII digits after the 'K' are ignored. No
809 * localization is present because this is an interface from the kernel
810 * to this program (eg not an end-user interface), so isdigit() isn't
813 if (fmt[2] != '\0' && fmt[2] >= '0' && fmt[2] <= '9')
818 if (*p == 'C' || *p == 'F' || *p == 'K') {
819 temp = strtof(str, endptrp);
820 if (*endptrp != str && *endptrp == p && errno == 0) {
822 temp = (temp - 32) * 5 / 9;
826 for (i = 0; i < prec; i++)
828 return ((int)(temp + 0.5));
831 /* No unit specified -> treat it as a raw number */
832 kelv = (int)strtol(str, endptrp, 10);
833 if (*endptrp != str && *endptrp == p && errno == 0) {
844 * These functions uses a presently undocumented interface to the kernel
845 * to walk the tree and get the type so it can print the value.
846 * This interface is under work and consideration, and should probably
847 * be killed with a big axe by the first person who can find the time.
848 * (be aware though, that the proper interface isn't as obvious as it
849 * may seem, there are various conflicting requirements.
853 name2oid(const char *name, int *oidp)
862 j = CTL_MAXNAME * sizeof(int);
863 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
871 oidfmt(int *oid, int len, char *fmt, u_int *kind)
873 int qoid[CTL_MAXNAME+2];
880 memcpy(qoid + 2, oid, len * sizeof(int));
883 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
885 err(1, "sysctl fmt %d %zu %d", i, j, errno);
888 *kind = *(u_int *)buf;
891 strcpy(fmt, (char *)(buf + sizeof(u_int)));
896 * This formats and outputs the value of one variable
898 * Returns zero if anything was actually output.
899 * Returns one if didn't know what to do with this.
900 * Return minus one if we had errors.
903 show_var(int *oid, int nlen)
905 u_char buf[BUFSIZ], *val, *oval, *p;
906 char name[BUFSIZ], fmt[BUFSIZ];
907 const char *sep, *sep1, *prntype;
908 int qoid[CTL_MAXNAME+2];
911 int i, hexlen, sign, ctltype;
916 int (*func)(size_t, void *);
920 umv = mv = intlen = 0;
926 memcpy(qoid + 2, oid, nlen * sizeof(int));
930 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
932 err(1, "sysctl name %d %zu %d", i, j, errno);
934 oidfmt(oid, nlen, fmt, &kind);
935 /* if Wflag then only list sysctls that are writeable and not stats. */
936 if (Wflag && ((kind & CTLFLAG_WR) == 0 || (kind & CTLFLAG_STATS) != 0))
939 /* if Tflag then only list sysctls that are tuneables. */
940 if (Tflag && (kind & CTLFLAG_TUN) == 0)
953 ctltype = (kind & CTLTYPE);
954 if (tflag || dflag) {
956 printf("%s%s", name, sep);
957 if (ctl_typename[ctltype] != NULL)
958 prntype = ctl_typename[ctltype];
962 printf("%s%s", prntype, sep);
964 printf("%s", prntype);
969 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
974 /* don't fetch opaques that we don't know how to print */
975 if (ctltype == CTLTYPE_OPAQUE) {
976 if (strcmp(fmt, "S,clockinfo") == 0)
978 else if (strcmp(fmt, "S,timeval") == 0)
980 else if (strcmp(fmt, "S,loadavg") == 0)
982 else if (strcmp(fmt, "S,vmtotal") == 0)
985 else if (strcmp(fmt, "S,efi_map_header") == 0)
988 #if defined(__amd64__) || defined(__i386__)
989 else if (strcmp(fmt, "S,bios_smap_xattr") == 0)
990 func = S_bios_smap_xattr;
994 if (!bflag && !oflag && !xflag)
999 /* find an estimate of how much we need for this var */
1004 i = sysctl(oid, nlen, 0, &j, 0, 0);
1005 j += j; /* we want to be sure :-) */
1008 val = oval = malloc(j + 1);
1010 warnx("malloc failed");
1014 i = sysctl(oid, nlen, val, &len, 0, 0);
1015 if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) {
1021 fwrite(val, 1, len, stdout);
1027 sign = ctl_sign[ctltype];
1028 intlen = ctl_size[ctltype];
1031 case CTLTYPE_STRING:
1033 printf("%s%s", name, sep);
1034 printf("%.*s", (int)len, p);
1051 printf("%s%s", name, sep);
1052 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
1054 while (len >= intlen) {
1055 switch (kind & CTLTYPE) {
1068 umv = *(uint8_t *)p;
1073 umv = *(uint16_t *)p;
1078 umv = *(uint32_t *)p;
1083 umv = *(uint64_t *)p;
1087 fputs(sep1, stdout);
1089 printf("%#0*jx", hexlen, umv);
1091 printf(hflag ? "%'ju" : "%ju", umv);
1092 else if (fmt[1] == 'K') {
1097 * See strIKtoi for details on fmt.
1101 prec = fmt[2] - '0';
1103 for (int i = 0; i < prec; i++)
1105 printf("%.*fC", prec,
1106 (float)mv / base - 273.15);
1109 printf(hflag ? "%'jd" : "%jd", mv);
1117 case CTLTYPE_OPAQUE:
1121 printf("%s%s", name, sep);
1122 i = (*func)(len, p);
1128 if (!oflag && !xflag) {
1133 printf("%s%s", name, sep);
1134 printf("Format:%s Length:%zu Dump:0x", fmt, len);
1135 while (len-- && (xflag || p < val + 16))
1136 printf("%02x", *p++);
1137 if (!xflag && len > 16)
1147 sysctl_all(int *oid, int len)
1149 int name1[22], name2[22];
1157 memcpy(name1+2, oid, len * sizeof(int));
1165 j = sysctl(name1, l1, name2, &l2, 0, 0);
1167 if (errno == ENOENT)
1170 err(1, "sysctl(getnext) %d %zu", j, l2);
1175 if (len < 0 || l2 < (unsigned int)len)
1178 for (i = 0; i < len; i++)
1179 if (name2[i] != oid[i])
1182 i = show_var(name2, l2);
1186 memcpy(name1+2, name2, l2 * sizeof(int));