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>
74 static const char *conffile;
76 static int aflag, bflag, Bflag, dflag, eflag, hflag, iflag;
77 static int Nflag, nflag, oflag, qflag, tflag, Tflag, Wflag, xflag;
79 static int oidfmt(int *, int, char *, u_int *);
80 static int parsefile(const char *);
81 static int parse(const char *, int);
82 static int show_var(int *, int);
83 static int sysctl_all(int *oid, int len);
84 static int name2oid(const char *, int *);
86 static int strIKtoi(const char *, char **, const char *);
88 static int ctl_sign[CTLTYPE+1] = {
97 static int ctl_size[CTLTYPE+1] = {
98 [CTLTYPE_INT] = sizeof(int),
99 [CTLTYPE_UINT] = sizeof(u_int),
100 [CTLTYPE_LONG] = sizeof(long),
101 [CTLTYPE_ULONG] = sizeof(u_long),
102 [CTLTYPE_S8] = sizeof(int8_t),
103 [CTLTYPE_S16] = sizeof(int16_t),
104 [CTLTYPE_S32] = sizeof(int32_t),
105 [CTLTYPE_S64] = sizeof(int64_t),
106 [CTLTYPE_U8] = sizeof(uint8_t),
107 [CTLTYPE_U16] = sizeof(uint16_t),
108 [CTLTYPE_U32] = sizeof(uint32_t),
109 [CTLTYPE_U64] = sizeof(uint64_t),
112 static const char *ctl_typename[CTLTYPE+1] = {
113 [CTLTYPE_INT] = "integer",
114 [CTLTYPE_UINT] = "unsigned integer",
115 [CTLTYPE_LONG] = "long integer",
116 [CTLTYPE_ULONG] = "unsigned long",
117 [CTLTYPE_U8] = "uint8_t",
118 [CTLTYPE_U16] = "uint16_t",
119 [CTLTYPE_U32] = "uint32_t",
120 [CTLTYPE_U64] = "uint64_t",
121 [CTLTYPE_S8] = "int8_t",
122 [CTLTYPE_S16] = "int16_t",
123 [CTLTYPE_S32] = "int32_t",
124 [CTLTYPE_S64] = "int64_t",
125 [CTLTYPE_NODE] = "node",
126 [CTLTYPE_STRING] = "string",
127 [CTLTYPE_OPAQUE] = "opaque",
134 (void)fprintf(stderr, "%s\n%s\n",
135 "usage: sysctl [-bdehiNnoqTtWx] [ -B <bufsize> ] [-f filename] name[=value] ...",
136 " sysctl [-bdehNnoqTtWx] [ -B <bufsize> ] -a");
141 main(int argc, char **argv)
146 setlocale(LC_NUMERIC, "");
150 while ((ch = getopt(argc, argv, "AabB:def:hiNnoqtTwWxX")) != -1) {
163 Bflag = strtol(optarg, NULL, 0);
221 if (aflag && argc == 0)
222 exit(sysctl_all(0, 0));
223 if (argc == 0 && conffile == NULL)
227 if (conffile != NULL)
228 warncount += parsefile(conffile);
231 warncount += parse(*argv++, 0);
237 * Parse a name into a MIB entry.
238 * Lookup and print out the MIB entry if it exists.
239 * Set a new value if requested.
242 parse(const char *string, int lineno)
246 const char *newvalstr = NULL;
254 unsigned int uintval;
256 unsigned long ulongval;
257 size_t newsize = Bflag;
260 int mib[CTL_MAXNAME];
261 char *cp, *bufp, buf[BUFSIZ], *endptr = NULL, fmt[BUFSIZ], line[BUFSIZ];
265 snprintf(line, sizeof(line), " at line %d", lineno);
270 if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ) {
271 warnx("oid too long: '%s'%s", string, line);
274 bufp = strsep(&cp, "=:");
276 /* Tflag just lists tunables, do not allow assignment */
277 if (Tflag || Wflag) {
278 warnx("Can't set variables when using -T or -W");
283 /* Strip a pair of " or ' if any. */
287 if (cp[strlen(cp) - 1] == *cp)
288 cp[strlen(cp) - 1] = '\0';
292 newsize = strlen(cp);
295 cp = bufp + strlen(bufp) - 1;
296 while (cp >= bufp && isspace((int)*cp)) {
300 len = name2oid(bufp, mib);
308 if (errno == ENOENT) {
309 warnx("unknown oid '%s'%s", bufp, line);
311 warn("unknown oid '%s'%s", bufp, line);
317 if (oidfmt(mib, len, fmt, &kind)) {
318 warn("couldn't find format of oid '%s'%s", bufp, line);
325 if (newvalstr == NULL || dflag) {
326 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
328 i = show_var(mib, len);
332 sysctl_all(mib, len);
334 i = show_var(mib, len);
339 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
340 warnx("oid '%s' isn't a leaf node%s", bufp, line);
344 if (!(kind & CTLFLAG_WR)) {
345 if (kind & CTLFLAG_TUN) {
346 warnx("oid '%s' is a read only tunable%s", bufp, line);
347 warnx("Tunable values are set in /boot/loader.conf");
349 warnx("oid '%s' is read only%s", bufp, line);
353 switch (kind & CTLTYPE) {
366 if (strlen(newvalstr) == 0) {
367 warnx("empty numeric value");
374 warnx("oid '%s' is type %d,"
375 " cannot set that%s", bufp,
376 kind & CTLTYPE, line);
382 switch (kind & CTLTYPE) {
384 if (strncmp(fmt, "IK", 2) == 0)
385 intval = strIKtoi(newvalstr, &endptr, fmt);
387 intval = (int)strtol(newvalstr, &endptr,
390 newsize = sizeof(intval);
393 uintval = (int) strtoul(newvalstr, &endptr, 0);
395 newsize = sizeof(uintval);
398 longval = strtol(newvalstr, &endptr, 0);
400 newsize = sizeof(longval);
403 ulongval = strtoul(newvalstr, &endptr, 0);
405 newsize = sizeof(ulongval);
411 i8val = (int8_t)strtol(newvalstr, &endptr, 0);
413 newsize = sizeof(i8val);
416 i16val = (int16_t)strtol(newvalstr, &endptr,
419 newsize = sizeof(i16val);
422 i32val = (int32_t)strtol(newvalstr, &endptr,
425 newsize = sizeof(i32val);
428 i64val = strtoimax(newvalstr, &endptr, 0);
430 newsize = sizeof(i64val);
433 u8val = (uint8_t)strtoul(newvalstr, &endptr, 0);
435 newsize = sizeof(u8val);
438 u16val = (uint16_t)strtoul(newvalstr, &endptr,
441 newsize = sizeof(u16val);
444 u32val = (uint32_t)strtoul(newvalstr, &endptr,
447 newsize = sizeof(u32val);
450 u64val = strtoumax(newvalstr, &endptr, 0);
452 newsize = sizeof(u64val);
459 if (errno != 0 || endptr == newvalstr ||
460 (endptr != NULL && *endptr != '\0')) {
461 warnx("invalid %s '%s'%s", ctl_typename[kind & CTLTYPE],
466 i = show_var(mib, len);
467 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
472 warnx("%s: value is not available%s",
476 warnx("%s: specification is incomplete%s",
480 warnx("%s: type is unknown to this program%s",
484 warn("%s%s", string, line);
492 j = show_var(mib, len);
502 parsefile(const char *filename)
505 char line[BUFSIZ], *p, *pq, *pdq;
506 int warncount = 0, lineno = 0;
508 file = fopen(filename, "r");
510 err(EX_NOINPUT, "%s", filename);
511 while (fgets(line, sizeof(line), file) != NULL) {
514 pq = strchr(line, '\'');
515 pdq = strchr(line, '\"');
516 /* Replace the first # with \0. */
517 while((p = strchr(p, '#')) != NULL) {
518 if (pq != NULL && p > pq) {
519 if ((p = strchr(pq+1, '\'')) != NULL)
522 } else if (pdq != NULL && p > pdq) {
523 if ((p = strchr(pdq+1, '\"')) != NULL)
526 } else if (p == line || *(p-1) != '\\') {
533 p = line + strlen(line) - 1;
534 while (p >= line && isspace((int)*p)) {
539 while (isspace((int)*p))
544 warncount += parse(p, lineno);
551 /* These functions will dump out various interesting structures. */
554 S_clockinfo(size_t l2, void *p)
556 struct clockinfo *ci = (struct clockinfo*)p;
558 if (l2 != sizeof(*ci)) {
559 warnx("S_clockinfo %zu != %zu", l2, sizeof(*ci));
562 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
563 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
564 ci->hz, ci->tick, ci->profhz, ci->stathz);
569 S_loadavg(size_t l2, void *p)
571 struct loadavg *tv = (struct loadavg*)p;
573 if (l2 != sizeof(*tv)) {
574 warnx("S_loadavg %zu != %zu", l2, sizeof(*tv));
577 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
578 (double)tv->ldavg[0]/(double)tv->fscale,
579 (double)tv->ldavg[1]/(double)tv->fscale,
580 (double)tv->ldavg[2]/(double)tv->fscale);
585 S_timeval(size_t l2, void *p)
587 struct timeval *tv = (struct timeval*)p;
591 if (l2 != sizeof(*tv)) {
592 warnx("S_timeval %zu != %zu", l2, sizeof(*tv));
595 printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
596 "{ sec = %jd, usec = %ld } ",
597 (intmax_t)tv->tv_sec, tv->tv_usec);
599 p1 = strdup(ctime(&tv_sec));
600 for (p2=p1; *p2 ; p2++)
609 S_vmtotal(size_t l2, void *p)
614 if (l2 != sizeof(*v)) {
615 warnx("S_vmtotal %zu != %zu", l2, sizeof(*v));
620 pageKilo = getpagesize() / 1024;
622 #define pg2k(a) ((uintmax_t)(a) * pageKilo)
623 printf("\nSystem wide totals computed every five seconds:"
624 " (values in kilobytes)\n");
625 printf("===============================================\n");
626 printf("Processes:\t\t(RUNQ: %d Disk Wait: %d Page Wait: "
628 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
629 printf("Virtual Memory:\t\t(Total: %juK Active: %juK)\n",
630 pg2k(v->t_vm), pg2k(v->t_avm));
631 printf("Real Memory:\t\t(Total: %juK Active: %juK)\n",
632 pg2k(v->t_rm), pg2k(v->t_arm));
633 printf("Shared Virtual Memory:\t(Total: %juK Active: %juK)\n",
634 pg2k(v->t_vmshr), pg2k(v->t_avmshr));
635 printf("Shared Real Memory:\t(Total: %juK Active: %juK)\n",
636 pg2k(v->t_rmshr), pg2k(v->t_armshr));
637 printf("Free Memory:\t%juK", pg2k(v->t_free));
643 S_efi_map(size_t l2, void *p)
645 struct efi_map_header *efihdr;
651 static const char *types[] = {
657 "RuntimeServicesCode",
658 "RuntimeServicesData",
659 "ConventionalMemory",
664 "MemoryMappedIOPortSpace",
669 * Memory map data provided by UEFI via the GetMemoryMap
672 if (l2 < sizeof(*efihdr)) {
673 warnx("S_efi_map length less than header");
677 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
678 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
680 if (efihdr->descriptor_size == 0)
682 if (l2 != efisz + efihdr->memory_size) {
683 warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz +
684 efihdr->memory_size);
687 ndesc = efihdr->memory_size / efihdr->descriptor_size;
689 printf("\n%23s %12s %12s %8s %4s",
690 "Type", "Physical", "Virtual", "#Pages", "Attr");
692 for (i = 0; i < ndesc; i++,
693 map = efi_next_descriptor(map, efihdr->descriptor_size)) {
694 if (map->md_type <= EFI_MD_TYPE_PALCODE)
695 type = types[map->md_type];
698 printf("\n%23s %012jx %12p %08jx ", type,
699 (uintmax_t)map->md_phys, map->md_virt,
700 (uintmax_t)map->md_pages);
701 if (map->md_attr & EFI_MD_ATTR_UC)
703 if (map->md_attr & EFI_MD_ATTR_WC)
705 if (map->md_attr & EFI_MD_ATTR_WT)
707 if (map->md_attr & EFI_MD_ATTR_WB)
709 if (map->md_attr & EFI_MD_ATTR_UCE)
711 if (map->md_attr & EFI_MD_ATTR_WP)
713 if (map->md_attr & EFI_MD_ATTR_RP)
715 if (map->md_attr & EFI_MD_ATTR_XP)
717 if (map->md_attr & EFI_MD_ATTR_RT)
724 #if defined(__amd64__) || defined(__i386__)
726 S_bios_smap_xattr(size_t l2, void *p)
728 struct bios_smap_xattr *smap, *end;
730 if (l2 % sizeof(*smap) != 0) {
731 warnx("S_bios_smap_xattr %zu is not a multiple of %zu", l2,
736 end = (struct bios_smap_xattr *)((char *)p + l2);
737 for (smap = p; smap < end; smap++)
738 printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx",
739 smap->type, smap->xattr, (uintmax_t)smap->base,
740 (uintmax_t)smap->length);
746 strIKtoi(const char *str, char **endptrp, const char *fmt)
757 /* caller already checked this */
761 * A format of "IK" is in deciKelvin. A format of "IK3" is in
762 * milliKelvin. The single digit following IK is log10 of the
763 * multiplying factor to convert Kelvin into the untis of this sysctl,
764 * or the dividing factor to convert the sysctl value to Kelvin. Numbers
765 * larger than 6 will run into precision issues with 32-bit integers.
766 * Characters that aren't ASCII digits after the 'K' are ignored. No
767 * localization is present because this is an interface from the kernel
768 * to this program (eg not an end-user interface), so isdigit() isn't
771 if (fmt[2] != '\0' && fmt[2] >= '0' && fmt[2] <= '9')
776 if (*p == 'C' || *p == 'F' || *p == 'K') {
777 temp = strtof(str, endptrp);
778 if (*endptrp != str && *endptrp == p && errno == 0) {
780 temp = (temp - 32) * 5 / 9;
784 for (i = 0; i < prec; i++)
786 return ((int)(temp + 0.5));
789 /* No unit specified -> treat it as a raw number */
790 kelv = (int)strtol(str, endptrp, 10);
791 if (*endptrp != str && *endptrp == p && errno == 0) {
802 * These functions uses a presently undocumented interface to the kernel
803 * to walk the tree and get the type so it can print the value.
804 * This interface is under work and consideration, and should probably
805 * be killed with a big axe by the first person who can find the time.
806 * (be aware though, that the proper interface isn't as obvious as it
807 * may seem, there are various conflicting requirements.
811 name2oid(const char *name, int *oidp)
820 j = CTL_MAXNAME * sizeof(int);
821 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
829 oidfmt(int *oid, int len, char *fmt, u_int *kind)
831 int qoid[CTL_MAXNAME+2];
838 memcpy(qoid + 2, oid, len * sizeof(int));
841 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
843 err(1, "sysctl fmt %d %zu %d", i, j, errno);
846 *kind = *(u_int *)buf;
849 strcpy(fmt, (char *)(buf + sizeof(u_int)));
854 * This formats and outputs the value of one variable
856 * Returns zero if anything was actually output.
857 * Returns one if didn't know what to do with this.
858 * Return minus one if we had errors.
861 show_var(int *oid, int nlen)
863 u_char buf[BUFSIZ], *val, *oval, *p;
864 char name[BUFSIZ], fmt[BUFSIZ];
865 const char *sep, *sep1, *prntype;
866 int qoid[CTL_MAXNAME+2];
869 int i, hexlen, sign, ctltype;
874 int (*func)(size_t, void *);
878 umv = mv = intlen = 0;
884 memcpy(qoid + 2, oid, nlen * sizeof(int));
888 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
890 err(1, "sysctl name %d %zu %d", i, j, errno);
892 oidfmt(oid, nlen, fmt, &kind);
893 /* if Wflag then only list sysctls that are writeable and not stats. */
894 if (Wflag && ((kind & CTLFLAG_WR) == 0 || (kind & CTLFLAG_STATS) != 0))
897 /* if Tflag then only list sysctls that are tuneables. */
898 if (Tflag && (kind & CTLFLAG_TUN) == 0)
911 ctltype = (kind & CTLTYPE);
912 if (tflag || dflag) {
914 printf("%s%s", name, sep);
915 if (ctl_typename[ctltype] != NULL)
916 prntype = ctl_typename[ctltype];
920 printf("%s%s", prntype, sep);
922 printf("%s", prntype);
927 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
932 /* don't fetch opaques that we don't know how to print */
933 if (ctltype == CTLTYPE_OPAQUE) {
934 if (strcmp(fmt, "S,clockinfo") == 0)
936 else if (strcmp(fmt, "S,timeval") == 0)
938 else if (strcmp(fmt, "S,loadavg") == 0)
940 else if (strcmp(fmt, "S,vmtotal") == 0)
943 else if (strcmp(fmt, "S,efi_map_header") == 0)
946 #if defined(__amd64__) || defined(__i386__)
947 else if (strcmp(fmt, "S,bios_smap_xattr") == 0)
948 func = S_bios_smap_xattr;
952 if (!bflag && !oflag && !xflag)
957 /* find an estimate of how much we need for this var */
962 i = sysctl(oid, nlen, 0, &j, 0, 0);
963 j += j; /* we want to be sure :-) */
966 val = oval = malloc(j + 1);
968 warnx("malloc failed");
972 i = sysctl(oid, nlen, val, &len, 0, 0);
973 if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) {
979 fwrite(val, 1, len, stdout);
985 sign = ctl_sign[ctltype];
986 intlen = ctl_size[ctltype];
991 printf("%s%s", name, sep);
992 printf("%.*s", (int)len, p);
1009 printf("%s%s", name, sep);
1010 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
1012 while (len >= intlen) {
1013 switch (kind & CTLTYPE) {
1026 umv = *(uint8_t *)p;
1031 umv = *(uint16_t *)p;
1036 umv = *(uint32_t *)p;
1041 umv = *(uint64_t *)p;
1045 fputs(sep1, stdout);
1047 printf("%#0*jx", hexlen, umv);
1049 printf(hflag ? "%'ju" : "%ju", umv);
1050 else if (fmt[1] == 'K') {
1055 * See strIKtoi for details on fmt.
1059 prec = fmt[2] - '0';
1061 for (int i = 0; i < prec; i++)
1063 printf("%.*fC", prec,
1064 (float)mv / base - 273.15);
1067 printf(hflag ? "%'jd" : "%jd", mv);
1075 case CTLTYPE_OPAQUE:
1079 printf("%s%s", name, sep);
1080 i = (*func)(len, p);
1086 if (!oflag && !xflag) {
1091 printf("%s%s", name, sep);
1092 printf("Format:%s Length:%zu Dump:0x", fmt, len);
1093 while (len-- && (xflag || p < val + 16))
1094 printf("%02x", *p++);
1095 if (!xflag && len > 16)
1105 sysctl_all(int *oid, int len)
1107 int name1[22], name2[22];
1115 memcpy(name1+2, oid, len * sizeof(int));
1123 j = sysctl(name1, l1, name2, &l2, 0, 0);
1125 if (errno == ENOENT)
1128 err(1, "sysctl(getnext) %d %zu", j, l2);
1133 if (len < 0 || l2 < (unsigned int)len)
1136 for (i = 0; i < len; i++)
1137 if (name2[i] != oid[i])
1140 i = show_var(name2, l2);
1144 memcpy(name1+2, name2, l2 * sizeof(int));