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
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
37 #include <sys/resource.h>
39 #include <sys/sysctl.h>
40 #include <sys/vmmeter.h>
41 #include <dev/evdev/input.h>
45 #include <machine/metadata.h>
48 #if defined(__amd64__) || defined(__i386__)
49 #include <machine/pc/bios.h>
65 static const char *conffile;
67 static int aflag, bflag, Bflag, dflag, eflag, hflag, iflag;
68 static int Nflag, nflag, oflag, qflag, tflag, Tflag, Wflag, xflag;
70 static int oidfmt(int *, int, char *, u_int *);
71 static int parsefile(const char *);
72 static int parse(const char *, int);
73 static int show_var(int *, int, bool);
74 static int sysctl_all(int *oid, int len);
75 static int name2oid(const char *, int *);
77 static int strIKtoi(const char *, char **, const char *);
79 static int ctl_sign[CTLTYPE+1] = {
88 static int ctl_size[CTLTYPE+1] = {
89 [CTLTYPE_INT] = sizeof(int),
90 [CTLTYPE_UINT] = sizeof(u_int),
91 [CTLTYPE_LONG] = sizeof(long),
92 [CTLTYPE_ULONG] = sizeof(u_long),
93 [CTLTYPE_S8] = sizeof(int8_t),
94 [CTLTYPE_S16] = sizeof(int16_t),
95 [CTLTYPE_S32] = sizeof(int32_t),
96 [CTLTYPE_S64] = sizeof(int64_t),
97 [CTLTYPE_U8] = sizeof(uint8_t),
98 [CTLTYPE_U16] = sizeof(uint16_t),
99 [CTLTYPE_U32] = sizeof(uint32_t),
100 [CTLTYPE_U64] = sizeof(uint64_t),
103 static const char *ctl_typename[CTLTYPE+1] = {
104 [CTLTYPE_INT] = "integer",
105 [CTLTYPE_UINT] = "unsigned integer",
106 [CTLTYPE_LONG] = "long integer",
107 [CTLTYPE_ULONG] = "unsigned long",
108 [CTLTYPE_U8] = "uint8_t",
109 [CTLTYPE_U16] = "uint16_t",
110 [CTLTYPE_U32] = "uint32_t",
111 [CTLTYPE_U64] = "uint64_t",
112 [CTLTYPE_S8] = "int8_t",
113 [CTLTYPE_S16] = "int16_t",
114 [CTLTYPE_S32] = "int32_t",
115 [CTLTYPE_S64] = "int64_t",
116 [CTLTYPE_NODE] = "node",
117 [CTLTYPE_STRING] = "string",
118 [CTLTYPE_OPAQUE] = "opaque",
125 (void)fprintf(stderr, "%s\n%s\n",
126 "usage: sysctl [-bdehiNnoqTtWx] [ -B <bufsize> ] [-f filename] name[=value] ...",
127 " sysctl [-bdehNnoqTtWx] [ -B <bufsize> ] -a");
132 main(int argc, char **argv)
137 setlocale(LC_NUMERIC, "");
141 while ((ch = getopt(argc, argv, "AabB:def:hiNnoqtTwWxX")) != -1) {
154 Bflag = strtol(optarg, NULL, 0);
212 if (aflag && argc == 0)
213 exit(sysctl_all(NULL, 0));
214 if (argc == 0 && conffile == NULL)
218 if (conffile != NULL)
219 warncount += parsefile(conffile);
222 warncount += parse(*argv++, 0);
228 * Parse a single numeric value, append it to 'newbuf', and update
229 * 'newsize'. Returns true if the value was parsed and false if the
230 * value was invalid. Non-numeric types (strings) are handled
231 * directly in parse().
234 parse_numeric(const char *newvalstr, const char *fmt, u_int kind,
235 void **newbufp, size_t *newsizep)
246 unsigned int uintval;
248 unsigned long ulongval;
256 switch (kind & CTLTYPE) {
258 if (strncmp(fmt, "IK", 2) == 0)
259 intval = strIKtoi(newvalstr, &endptr, fmt);
261 intval = (int)strtol(newvalstr, &endptr, 0);
263 valsize = sizeof(intval);
266 uintval = (int) strtoul(newvalstr, &endptr, 0);
268 valsize = sizeof(uintval);
271 longval = strtol(newvalstr, &endptr, 0);
273 valsize = sizeof(longval);
276 ulongval = strtoul(newvalstr, &endptr, 0);
278 valsize = sizeof(ulongval);
281 i8val = (int8_t)strtol(newvalstr, &endptr, 0);
283 valsize = sizeof(i8val);
286 i16val = (int16_t)strtol(newvalstr, &endptr, 0);
288 valsize = sizeof(i16val);
291 i32val = (int32_t)strtol(newvalstr, &endptr, 0);
293 valsize = sizeof(i32val);
296 i64val = strtoimax(newvalstr, &endptr, 0);
298 valsize = sizeof(i64val);
301 u8val = (uint8_t)strtoul(newvalstr, &endptr, 0);
303 valsize = sizeof(u8val);
306 u16val = (uint16_t)strtoul(newvalstr, &endptr, 0);
308 valsize = sizeof(u16val);
311 u32val = (uint32_t)strtoul(newvalstr, &endptr, 0);
313 valsize = sizeof(u32val);
316 u64val = strtoumax(newvalstr, &endptr, 0);
318 valsize = sizeof(u64val);
325 if (errno != 0 || endptr == newvalstr ||
326 (endptr != NULL && *endptr != '\0'))
329 newbuf = realloc(*newbufp, *newsizep + valsize);
331 err(1, "out of memory");
332 memcpy((char *)newbuf + *newsizep, newval, valsize);
334 *newsizep += valsize;
340 * Parse a name into a MIB entry.
341 * Lookup and print out the MIB entry if it exists.
342 * Set a new value if requested.
345 parse(const char *string, int lineno)
349 char *newvalstr = NULL;
351 size_t newsize = Bflag;
352 int mib[CTL_MAXNAME];
353 char *cp, *bufp, buf[BUFSIZ], fmt[BUFSIZ], line[BUFSIZ];
357 snprintf(line, sizeof(line), " at line %d", lineno);
362 * Split the string into name and value.
364 * Either = or : may be used as the delimiter.
365 * Whitespace surrounding the delimiter is trimmed.
366 * Quotes around the value are stripped.
369 if (snprintf(buf, BUFSIZ, "%s", string) >= BUFSIZ) {
370 warnx("oid too long: '%s'%s", string, line);
373 bufp = strsep(&cp, "=:");
375 /* Tflag just lists tunables, do not allow assignment */
376 if (Tflag || Wflag) {
377 warnx("Can't set variables when using -T or -W");
380 /* Trim whitespace before the value. */
383 /* Strip a pair of " or ' if any. */
387 if (cp[strlen(cp) - 1] == *cp)
388 cp[strlen(cp) - 1] = '\0';
392 newsize = strlen(cp);
394 /* Trim whitespace after the name. */
395 cp = bufp + strlen(bufp) - 1;
396 while (cp >= bufp && isspace((int)*cp)) {
402 * Check the name is a useable oid.
404 len = name2oid(bufp, mib);
411 if (errno == ENOENT) {
412 warnx("unknown oid '%s'%s", bufp, line);
414 warn("unknown oid '%s'%s", bufp, line);
420 if (oidfmt(mib, len, fmt, &kind)) {
421 warn("couldn't find format of oid '%s'%s", bufp, line);
429 * We have a useable oid to work with. If there is no value given,
430 * show the node and its children. Otherwise, set the new value.
432 if (newvalstr == NULL || dflag) {
433 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
435 i = show_var(mib, len, false);
439 sysctl_all(mib, len);
441 i = show_var(mib, len, false);
449 * We have a new value to set. Check its validity and parse if numeric.
451 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
452 warnx("oid '%s' isn't a leaf node%s", bufp, line);
456 if (!(kind & CTLFLAG_WR)) {
457 if (kind & CTLFLAG_TUN) {
458 warnx("oid '%s' is a read only tunable%s", bufp, line);
459 warnx("Tunable values are set in /boot/loader.conf");
461 warnx("oid '%s' is read only%s", bufp, line);
465 switch (kind & CTLTYPE) {
478 if (strlen(newvalstr) == 0) {
479 warnx("empty numeric value");
486 warnx("oid '%s' is type %d, cannot set that%s",
487 bufp, kind & CTLTYPE, line);
493 switch (kind & CTLTYPE) {
499 while ((cp = strsep(&newvalstr, " ,")) != NULL) {
502 if (!parse_numeric(cp, fmt, kind, &newbuf, &newsize)) {
503 warnx("invalid %s '%s'%s",
504 ctl_typename[kind & CTLTYPE], cp, line);
514 * Show the current value, then set and show the new value.
516 i = show_var(mib, len, false);
517 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
523 warnx("%s: value is not available%s",
527 warnx("%s: specification is incomplete%s",
531 warnx("%s: type is unknown to this program%s",
535 warn("%s%s", string, line);
544 j = show_var(mib, len, false);
553 parsefile(const char *filename)
556 char line[BUFSIZ], *p, *pq, *pdq;
557 int warncount = 0, lineno = 0;
559 file = fopen(filename, "r");
561 err(EX_NOINPUT, "%s", filename);
562 while (fgets(line, sizeof(line), file) != NULL) {
565 pq = strchr(line, '\'');
566 pdq = strchr(line, '\"');
567 /* Replace the first # with \0. */
568 while((p = strchr(p, '#')) != NULL) {
569 if (pq != NULL && p > pq) {
570 if ((p = strchr(pq+1, '\'')) != NULL)
573 } else if (pdq != NULL && p > pdq) {
574 if ((p = strchr(pdq+1, '\"')) != NULL)
577 } else if (p == line || *(p-1) != '\\') {
584 p = line + strlen(line) - 1;
585 while (p >= line && isspace((int)*p)) {
590 while (isspace((int)*p))
595 warncount += parse(p, lineno);
602 /* These functions will dump out various interesting structures. */
605 S_clockinfo(size_t l2, void *p)
607 struct clockinfo *ci = (struct clockinfo*)p;
609 if (l2 != sizeof(*ci)) {
610 warnx("S_clockinfo %zu != %zu", l2, sizeof(*ci));
613 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
614 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
615 ci->hz, ci->tick, ci->profhz, ci->stathz);
620 S_loadavg(size_t l2, void *p)
622 struct loadavg *tv = (struct loadavg*)p;
624 if (l2 != sizeof(*tv)) {
625 warnx("S_loadavg %zu != %zu", l2, sizeof(*tv));
628 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
629 (double)tv->ldavg[0]/(double)tv->fscale,
630 (double)tv->ldavg[1]/(double)tv->fscale,
631 (double)tv->ldavg[2]/(double)tv->fscale);
636 S_timeval(size_t l2, void *p)
638 struct timeval *tv = (struct timeval*)p;
642 if (l2 != sizeof(*tv)) {
643 warnx("S_timeval %zu != %zu", l2, sizeof(*tv));
646 printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
647 "{ sec = %jd, usec = %ld } ",
648 (intmax_t)tv->tv_sec, tv->tv_usec);
650 p1 = strdup(ctime(&tv_sec));
651 for (p2=p1; *p2 ; p2++)
660 S_vmtotal(size_t l2, void *p)
665 if (l2 != sizeof(*v)) {
666 warnx("S_vmtotal %zu != %zu", l2, sizeof(*v));
671 pageKilo = getpagesize() / 1024;
673 #define pg2k(a) ((uintmax_t)(a) * pageKilo)
674 printf("\nSystem wide totals computed every five seconds:"
675 " (values in kilobytes)\n");
676 printf("===============================================\n");
677 printf("Processes:\t\t(RUNQ: %d Disk Wait: %d Page Wait: "
679 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
680 printf("Virtual Memory:\t\t(Total: %juK Active: %juK)\n",
681 pg2k(v->t_vm), pg2k(v->t_avm));
682 printf("Real Memory:\t\t(Total: %juK Active: %juK)\n",
683 pg2k(v->t_rm), pg2k(v->t_arm));
684 printf("Shared Virtual Memory:\t(Total: %juK Active: %juK)\n",
685 pg2k(v->t_vmshr), pg2k(v->t_avmshr));
686 printf("Shared Real Memory:\t(Total: %juK Active: %juK)\n",
687 pg2k(v->t_rmshr), pg2k(v->t_armshr));
688 printf("Free Memory:\t%juK", pg2k(v->t_free));
693 S_input_id(size_t l2, void *p)
695 struct input_id *id = p;
697 if (l2 != sizeof(*id)) {
698 warnx("S_input_id %zu != %zu", l2, sizeof(*id));
702 printf("{ bustype = 0x%04x, vendor = 0x%04x, "
703 "product = 0x%04x, version = 0x%04x }",
704 id->bustype, id->vendor, id->product, id->version);
709 S_pagesizes(size_t l2, void *p)
716 l = snprintf(buf, sizeof(buf), "{ ");
718 for (i = 0; i * sizeof(*ps) < l2 && ps[i] != 0 && l < sizeof(buf);
720 l += snprintf(&buf[l], sizeof(buf) - l,
721 "%s%lu", i == 0 ? "" : ", ", ps[i]);
724 (void)snprintf(&buf[l], sizeof(buf) - l, " }");
733 S_efi_map(size_t l2, void *p)
735 struct efi_map_header *efihdr;
741 static const char * const types[] = {
742 [EFI_MD_TYPE_NULL] = "Reserved",
743 [EFI_MD_TYPE_CODE] = "LoaderCode",
744 [EFI_MD_TYPE_DATA] = "LoaderData",
745 [EFI_MD_TYPE_BS_CODE] = "BootServicesCode",
746 [EFI_MD_TYPE_BS_DATA] = "BootServicesData",
747 [EFI_MD_TYPE_RT_CODE] = "RuntimeServicesCode",
748 [EFI_MD_TYPE_RT_DATA] = "RuntimeServicesData",
749 [EFI_MD_TYPE_FREE] = "ConventionalMemory",
750 [EFI_MD_TYPE_BAD] = "UnusableMemory",
751 [EFI_MD_TYPE_RECLAIM] = "ACPIReclaimMemory",
752 [EFI_MD_TYPE_FIRMWARE] = "ACPIMemoryNVS",
753 [EFI_MD_TYPE_IOMEM] = "MemoryMappedIO",
754 [EFI_MD_TYPE_IOPORT] = "MemoryMappedIOPortSpace",
755 [EFI_MD_TYPE_PALCODE] = "PalCode",
756 [EFI_MD_TYPE_PERSISTENT] = "PersistentMemory",
760 * Memory map data provided by UEFI via the GetMemoryMap
763 if (l2 < sizeof(*efihdr)) {
764 warnx("S_efi_map length less than header");
768 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
769 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
771 if (efihdr->descriptor_size == 0)
773 if (l2 != efisz + efihdr->memory_size) {
774 warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz +
775 efihdr->memory_size);
778 ndesc = efihdr->memory_size / efihdr->descriptor_size;
780 printf("\n%23s %12s %12s %8s %4s",
781 "Type", "Physical", "Virtual", "#Pages", "Attr");
783 for (i = 0; i < ndesc; i++,
784 map = efi_next_descriptor(map, efihdr->descriptor_size)) {
786 if (map->md_type < nitems(types))
787 type = types[map->md_type];
790 printf("\n%23s %012jx %12p %08jx ", type,
791 (uintmax_t)map->md_phys, map->md_virt,
792 (uintmax_t)map->md_pages);
793 if (map->md_attr & EFI_MD_ATTR_UC)
795 if (map->md_attr & EFI_MD_ATTR_WC)
797 if (map->md_attr & EFI_MD_ATTR_WT)
799 if (map->md_attr & EFI_MD_ATTR_WB)
801 if (map->md_attr & EFI_MD_ATTR_UCE)
803 if (map->md_attr & EFI_MD_ATTR_WP)
805 if (map->md_attr & EFI_MD_ATTR_RP)
807 if (map->md_attr & EFI_MD_ATTR_XP)
809 if (map->md_attr & EFI_MD_ATTR_RT)
816 #if defined(__amd64__) || defined(__i386__)
818 S_bios_smap_xattr(size_t l2, void *p)
820 struct bios_smap_xattr *smap, *end;
822 if (l2 % sizeof(*smap) != 0) {
823 warnx("S_bios_smap_xattr %zu is not a multiple of %zu", l2,
828 end = (struct bios_smap_xattr *)((char *)p + l2);
829 for (smap = p; smap < end; smap++)
830 printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx",
831 smap->type, smap->xattr, (uintmax_t)smap->base,
832 (uintmax_t)smap->length);
838 strIKtoi(const char *str, char **endptrp, const char *fmt)
849 /* caller already checked this */
853 * A format of "IK" is in deciKelvin. A format of "IK3" is in
854 * milliKelvin. The single digit following IK is log10 of the
855 * multiplying factor to convert Kelvin into the untis of this sysctl,
856 * or the dividing factor to convert the sysctl value to Kelvin. Numbers
857 * larger than 6 will run into precision issues with 32-bit integers.
858 * Characters that aren't ASCII digits after the 'K' are ignored. No
859 * localization is present because this is an interface from the kernel
860 * to this program (eg not an end-user interface), so isdigit() isn't
863 if (fmt[2] != '\0' && fmt[2] >= '0' && fmt[2] <= '9')
868 if (*p == 'C' || *p == 'F' || *p == 'K') {
869 temp = strtof(str, endptrp);
870 if (*endptrp != str && *endptrp == p && errno == 0) {
872 temp = (temp - 32) * 5 / 9;
876 for (i = 0; i < prec; i++)
878 return ((int)(temp + 0.5));
881 /* No unit specified -> treat it as a raw number */
882 kelv = (int)strtol(str, endptrp, 10);
883 if (*endptrp != str && *endptrp == p && errno == 0) {
894 * These functions uses a presently undocumented interface to the kernel
895 * to walk the tree and get the type so it can print the value.
896 * This interface is under work and consideration, and should probably
897 * be killed with a big axe by the first person who can find the time.
898 * (be aware though, that the proper interface isn't as obvious as it
899 * may seem, there are various conflicting requirements.
903 name2oid(const char *name, int *oidp)
910 oid[1] = CTL_SYSCTL_NAME2OID;
912 j = CTL_MAXNAME * sizeof(int);
913 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
921 oidfmt(int *oid, int len, char *fmt, u_int *kind)
923 int qoid[CTL_MAXNAME+2];
928 qoid[0] = CTL_SYSCTL;
929 qoid[1] = CTL_SYSCTL_OIDFMT;
930 memcpy(qoid + 2, oid, len * sizeof(int));
933 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
935 err(1, "sysctl fmt %d %zu %d", i, j, errno);
938 *kind = *(u_int *)buf;
941 strcpy(fmt, (char *)(buf + sizeof(u_int)));
946 * This formats and outputs the value of one variable
948 * Returns zero if anything was actually output.
949 * Returns one if didn't know what to do with this.
950 * Return minus one if we had errors.
953 show_var(int *oid, int nlen, bool honor_skip)
955 static int skip_len = 0, skip_oid[CTL_MAXNAME];
956 u_char buf[BUFSIZ], *val, *oval, *p;
957 char name[BUFSIZ], fmt[BUFSIZ];
958 const char *sep, *sep1, *prntype;
959 int qoid[CTL_MAXNAME+2];
962 int i, hexlen, sign, ctltype;
967 int (*func)(size_t, void *);
971 umv = mv = intlen = 0;
976 qoid[0] = CTL_SYSCTL;
977 memcpy(qoid + 2, oid, nlen * sizeof(int));
979 qoid[1] = CTL_SYSCTL_NAME;
981 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
983 err(1, "sysctl name %d %zu %d", i, j, errno);
985 oidfmt(oid, nlen, fmt, &kind);
986 /* if Wflag then only list sysctls that are writeable and not stats. */
987 if (Wflag && ((kind & CTLFLAG_WR) == 0 || (kind & CTLFLAG_STATS) != 0))
990 /* if Tflag then only list sysctls that are tuneables. */
991 if (Tflag && (kind & CTLFLAG_TUN) == 0)
1004 ctltype = (kind & CTLTYPE);
1005 if (tflag || dflag) {
1007 printf("%s%s", name, sep);
1008 if (ctl_typename[ctltype] != NULL)
1009 prntype = ctl_typename[ctltype];
1011 prntype = "unknown";
1013 printf("%s%s", prntype, sep);
1015 printf("%s", prntype);
1018 qoid[1] = CTL_SYSCTL_OIDDESCR;
1020 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
1025 /* keep track of encountered skip nodes, ignoring descendants */
1026 if (skip_len == 0 && (kind & CTLFLAG_SKIP) != 0) {
1027 /* Save this oid so we can skip descendants. */
1028 skip_len = nlen * sizeof(int);
1029 memcpy(skip_oid, oid, skip_len);
1032 /* bail before fetching the value if we're honoring skip */
1034 if (0 < skip_len && skip_len <= nlen * (int)sizeof(int) &&
1035 memcmp(skip_oid, oid, skip_len) == 0)
1037 /* Not a skip node or descendant of a skip node. */
1041 /* don't fetch opaques that we don't know how to print */
1042 if (ctltype == CTLTYPE_OPAQUE) {
1043 if (strcmp(fmt, "S,clockinfo") == 0)
1045 else if (strcmp(fmt, "S,timeval") == 0)
1047 else if (strcmp(fmt, "S,loadavg") == 0)
1049 else if (strcmp(fmt, "S,vmtotal") == 0)
1051 else if (strcmp(fmt, "S,input_id") == 0)
1053 else if (strcmp(fmt, "S,pagesizes") == 0)
1056 else if (strcmp(fmt, "S,efi_map_header") == 0)
1059 #if defined(__amd64__) || defined(__i386__)
1060 else if (strcmp(fmt, "S,bios_smap_xattr") == 0)
1061 func = S_bios_smap_xattr;
1065 if (!bflag && !oflag && !xflag)
1070 /* find an estimate of how much we need for this var */
1075 i = sysctl(oid, nlen, 0, &j, 0, 0);
1076 j += j; /* we want to be sure :-) */
1079 val = oval = malloc(j + 1);
1081 warnx("malloc failed");
1085 i = sysctl(oid, nlen, val, &len, 0, 0);
1086 if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) {
1092 fwrite(val, 1, len, stdout);
1098 sign = ctl_sign[ctltype];
1099 intlen = ctl_size[ctltype];
1102 case CTLTYPE_STRING:
1104 printf("%s%s", name, sep);
1105 printf("%.*s", (int)len, p);
1122 printf("%s%s", name, sep);
1123 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
1125 while (len >= intlen) {
1126 switch (kind & CTLTYPE) {
1139 umv = *(uint8_t *)p;
1144 umv = *(uint16_t *)p;
1149 umv = *(uint32_t *)p;
1154 umv = *(uint64_t *)p;
1158 fputs(sep1, stdout);
1160 printf("%#0*jx", hexlen, umv);
1162 printf(hflag ? "%'ju" : "%ju", umv);
1163 else if (fmt[1] == 'K') {
1168 * See strIKtoi for details on fmt.
1172 prec = fmt[2] - '0';
1174 for (int i = 0; i < prec; i++)
1176 printf("%.*fC", prec,
1177 (float)mv / base - 273.15);
1180 printf(hflag ? "%'jd" : "%jd", mv);
1188 case CTLTYPE_OPAQUE:
1192 printf("%s%s", name, sep);
1193 i = (*func)(len, p);
1199 if (!oflag && !xflag) {
1204 printf("%s%s", name, sep);
1205 printf("Format:%s Length:%zu Dump:0x", fmt, len);
1206 while (len-- && (xflag || p < val + 16))
1207 printf("%02x", *p++);
1208 if (!xflag && len > 16)
1218 sysctl_all(int *oid, int len)
1220 int name1[22], name2[22];
1223 bool honor_skip = false;
1225 name1[0] = CTL_SYSCTL;
1226 name1[1] = (oid != NULL || Nflag || dflag || tflag) ?
1227 CTL_SYSCTL_NEXTNOSKIP : CTL_SYSCTL_NEXT;
1230 memcpy(name1 + 2, oid, len * sizeof(int));
1233 name1[2] = CTL_KERN;
1238 j = sysctl(name1, l1, name2, &l2, 0, 0);
1240 if (errno == ENOENT)
1243 err(1, "sysctl(getnext) %d %zu", j, l2);
1248 if (len < 0 || l2 < (unsigned int)len)
1251 if (memcmp(name2, oid, len * sizeof(int)) != 0)
1254 i = show_var(name2, l2, honor_skip);
1258 memcpy(name1 + 2, name2, l2 * sizeof(int));