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;
69 static bool Fflag, lflag;
71 static int oidfmt(int *, int, char *, u_int *);
72 static int parsefile(const char *);
73 static int parse(const char *, int);
74 static int show_var(int *, int, bool);
75 static int sysctl_all(int *, int);
76 static int name2oid(const char *, int *);
78 static int strIKtoi(const char *, char **, const char *);
80 static int ctl_sign[CTLTYPE+1] = {
89 static int ctl_size[CTLTYPE+1] = {
90 [CTLTYPE_INT] = sizeof(int),
91 [CTLTYPE_UINT] = sizeof(u_int),
92 [CTLTYPE_LONG] = sizeof(long),
93 [CTLTYPE_ULONG] = sizeof(u_long),
94 [CTLTYPE_S8] = sizeof(int8_t),
95 [CTLTYPE_S16] = sizeof(int16_t),
96 [CTLTYPE_S32] = sizeof(int32_t),
97 [CTLTYPE_S64] = sizeof(int64_t),
98 [CTLTYPE_U8] = sizeof(uint8_t),
99 [CTLTYPE_U16] = sizeof(uint16_t),
100 [CTLTYPE_U32] = sizeof(uint32_t),
101 [CTLTYPE_U64] = sizeof(uint64_t),
104 static const char *ctl_typename[CTLTYPE+1] = {
105 [CTLTYPE_INT] = "integer",
106 [CTLTYPE_UINT] = "unsigned integer",
107 [CTLTYPE_LONG] = "long integer",
108 [CTLTYPE_ULONG] = "unsigned long",
109 [CTLTYPE_U8] = "uint8_t",
110 [CTLTYPE_U16] = "uint16_t",
111 [CTLTYPE_U32] = "uint32_t",
112 [CTLTYPE_U64] = "uint64_t",
113 [CTLTYPE_S8] = "int8_t",
114 [CTLTYPE_S16] = "int16_t",
115 [CTLTYPE_S32] = "int32_t",
116 [CTLTYPE_S64] = "int64_t",
117 [CTLTYPE_NODE] = "node",
118 [CTLTYPE_STRING] = "string",
119 [CTLTYPE_OPAQUE] = "opaque",
126 (void)fprintf(stderr, "%s\n%s\n",
127 "usage: sysctl [-bdeFhilNnoqTtWx] [ -B <bufsize> ] [-f filename] name[=value] ...",
128 " sysctl [-bdeFhlNnoqTtWx] [ -B <bufsize> ] -a");
133 main(int argc, char **argv)
138 setlocale(LC_NUMERIC, "");
142 while ((ch = getopt(argc, argv, "AabB:def:FhilNnoqtTwWxX")) != -1) {
155 Bflag = strtol(optarg, NULL, 0);
217 /* Nflag is name only and doesn't make sense to combind with these */
218 /* TODO: few other combinations do not make sense but come back later */
219 if (Nflag && (lflag || nflag))
221 if (aflag && argc == 0)
222 exit(sysctl_all(NULL, 0));
223 if (argc == 0 && conffile == NULL)
226 if (conffile != NULL)
227 warncount += parsefile(conffile);
230 warncount += parse(*argv++, 0);
236 * Parse a single numeric value, append it to 'newbuf', and update
237 * 'newsize'. Returns true if the value was parsed and false if the
238 * value was invalid. Non-numeric types (strings) are handled
239 * directly in parse().
242 parse_numeric(const char *newvalstr, const char *fmt, u_int kind,
243 void **newbufp, size_t *newsizep)
254 unsigned int uintval;
256 unsigned long ulongval;
264 switch (kind & CTLTYPE) {
266 if (strncmp(fmt, "IK", 2) == 0)
267 intval = strIKtoi(newvalstr, &endptr, fmt);
269 intval = (int)strtol(newvalstr, &endptr, 0);
271 valsize = sizeof(intval);
274 uintval = (int) strtoul(newvalstr, &endptr, 0);
276 valsize = sizeof(uintval);
279 longval = strtol(newvalstr, &endptr, 0);
281 valsize = sizeof(longval);
284 ulongval = strtoul(newvalstr, &endptr, 0);
286 valsize = sizeof(ulongval);
289 i8val = (int8_t)strtol(newvalstr, &endptr, 0);
291 valsize = sizeof(i8val);
294 i16val = (int16_t)strtol(newvalstr, &endptr, 0);
296 valsize = sizeof(i16val);
299 i32val = (int32_t)strtol(newvalstr, &endptr, 0);
301 valsize = sizeof(i32val);
304 i64val = strtoimax(newvalstr, &endptr, 0);
306 valsize = sizeof(i64val);
309 u8val = (uint8_t)strtoul(newvalstr, &endptr, 0);
311 valsize = sizeof(u8val);
314 u16val = (uint16_t)strtoul(newvalstr, &endptr, 0);
316 valsize = sizeof(u16val);
319 u32val = (uint32_t)strtoul(newvalstr, &endptr, 0);
321 valsize = sizeof(u32val);
324 u64val = strtoumax(newvalstr, &endptr, 0);
326 valsize = sizeof(u64val);
333 if (errno != 0 || endptr == newvalstr ||
334 (endptr != NULL && *endptr != '\0'))
337 newbuf = realloc(*newbufp, *newsizep + valsize);
339 err(1, "out of memory");
340 memcpy((char *)newbuf + *newsizep, newval, valsize);
342 *newsizep += valsize;
348 * Parse a name into a MIB entry.
349 * Lookup and print out the MIB entry if it exists.
350 * Set a new value if requested.
353 parse(const char *string, int lineno)
355 int len, i, j, save_errno;
357 char *newvalstr = NULL;
359 size_t newsize = Bflag;
360 int mib[CTL_MAXNAME];
361 char *cp, *bufp, *buf, fmt[BUFSIZ], line[BUFSIZ];
365 snprintf(line, sizeof(line), " at line %d", lineno);
370 * Split the string into name and value.
372 * Either = or : may be used as the delimiter.
373 * Whitespace surrounding the delimiter is trimmed.
374 * Quotes around the value are stripped.
376 cp = buf = strdup(string);
377 bufp = strsep(&cp, "=:");
379 /* Tflag just lists tunables, do not allow assignment */
380 if (Tflag || Wflag) {
381 warnx("Can't set variables when using -T or -W");
384 /* Trim whitespace before the value. */
387 /* Strip a pair of " or ' if any. */
391 if (cp[strlen(cp) - 1] == *cp)
392 cp[strlen(cp) - 1] = '\0';
396 newsize = strlen(cp);
398 /* Trim whitespace after the name. */
399 cp = bufp + strlen(bufp) - 1;
400 while (cp >= bufp && isspace((int)*cp)) {
406 * Check the name is a useable oid.
408 len = name2oid(bufp, mib);
415 if (errno == ENOENT) {
416 warnx("unknown oid '%s'%s", bufp, line);
418 warn("unknown oid '%s'%s", bufp, line);
425 if (oidfmt(mib, len, fmt, &kind)) {
426 warn("couldn't find format of oid '%s'%s", bufp, line);
435 * We have a useable oid to work with. If there is no value given,
436 * show the node and its children. Otherwise, set the new value.
438 if (newvalstr == NULL || dflag) {
440 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
442 i = show_var(mib, len, false);
446 sysctl_all(mib, len);
448 i = show_var(mib, len, false);
456 * We have a new value to set. Check its validity and parse if numeric.
458 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
459 warnx("oid '%s' isn't a leaf node%s", bufp, line);
464 if (!(kind & CTLFLAG_WR)) {
465 if (kind & CTLFLAG_TUN) {
466 warnx("oid '%s' is a read only tunable%s", bufp, line);
467 warnx("Tunable values are set in /boot/loader.conf");
469 warnx("oid '%s' is read only%s", bufp, line);
474 switch (kind & CTLTYPE) {
487 if (strlen(newvalstr) == 0) {
488 warnx("empty numeric value");
496 warnx("oid '%s' is type %d, cannot set that%s",
497 bufp, kind & CTLTYPE, line);
504 switch (kind & CTLTYPE) {
510 while ((cp = strsep(&newvalstr, " ,")) != NULL) {
513 if (!parse_numeric(cp, fmt, kind, &newbuf, &newsize)) {
514 warnx("invalid %s '%s'%s",
515 ctl_typename[kind & CTLTYPE], cp, line);
526 * Show the current value, then set and show the new value.
528 i = show_var(mib, len, false);
529 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
535 switch (save_errno) {
537 warnx("%s: value is not available%s",
541 warnx("%s: specification is incomplete%s",
545 warnx("%s: type is unknown to this program%s",
549 warnc(save_errno, "%s%s", string, line);
559 j = show_var(mib, len, false);
568 parsefile(const char *filename)
571 char line[BUFSIZ], *p, *pq, *pdq;
572 int warncount = 0, lineno = 0;
574 file = fopen(filename, "r");
576 err(EX_NOINPUT, "%s", filename);
577 while (fgets(line, sizeof(line), file) != NULL) {
580 pq = strchr(line, '\'');
581 pdq = strchr(line, '\"');
582 /* Replace the first # with \0. */
583 while((p = strchr(p, '#')) != NULL) {
584 if (pq != NULL && p > pq) {
585 if ((p = strchr(pq+1, '\'')) != NULL)
588 } else if (pdq != NULL && p > pdq) {
589 if ((p = strchr(pdq+1, '\"')) != NULL)
592 } else if (p == line || *(p-1) != '\\') {
599 p = line + strlen(line) - 1;
600 while (p >= line && isspace((int)*p)) {
605 while (isspace((int)*p))
610 warncount += parse(p, lineno);
617 /* These functions will dump out various interesting structures. */
620 S_clockinfo(size_t l2, void *p)
622 struct clockinfo *ci = (struct clockinfo*)p;
624 if (l2 != sizeof(*ci)) {
625 warnx("S_clockinfo %zu != %zu", l2, sizeof(*ci));
628 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
629 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
630 ci->hz, ci->tick, ci->profhz, ci->stathz);
635 S_loadavg(size_t l2, void *p)
637 struct loadavg *tv = (struct loadavg*)p;
639 if (l2 != sizeof(*tv)) {
640 warnx("S_loadavg %zu != %zu", l2, sizeof(*tv));
643 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
644 (double)tv->ldavg[0]/(double)tv->fscale,
645 (double)tv->ldavg[1]/(double)tv->fscale,
646 (double)tv->ldavg[2]/(double)tv->fscale);
651 S_timeval(size_t l2, void *p)
653 struct timeval *tv = (struct timeval*)p;
657 if (l2 != sizeof(*tv)) {
658 warnx("S_timeval %zu != %zu", l2, sizeof(*tv));
661 printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
662 "{ sec = %jd, usec = %ld } ",
663 (intmax_t)tv->tv_sec, tv->tv_usec);
665 p1 = strdup(ctime(&tv_sec));
666 for (p2=p1; *p2 ; p2++)
675 S_vmtotal(size_t l2, void *p)
680 if (l2 != sizeof(*v)) {
681 warnx("S_vmtotal %zu != %zu", l2, sizeof(*v));
686 pageKilo = getpagesize() / 1024;
688 #define pg2k(a) ((uintmax_t)(a) * pageKilo)
689 printf("\nSystem wide totals computed every five seconds:"
690 " (values in kilobytes)\n");
691 printf("===============================================\n");
692 printf("Processes:\t\t(RUNQ: %d Disk Wait: %d Page Wait: "
694 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
695 printf("Virtual Memory:\t\t(Total: %juK Active: %juK)\n",
696 pg2k(v->t_vm), pg2k(v->t_avm));
697 printf("Real Memory:\t\t(Total: %juK Active: %juK)\n",
698 pg2k(v->t_rm), pg2k(v->t_arm));
699 printf("Shared Virtual Memory:\t(Total: %juK Active: %juK)\n",
700 pg2k(v->t_vmshr), pg2k(v->t_avmshr));
701 printf("Shared Real Memory:\t(Total: %juK Active: %juK)\n",
702 pg2k(v->t_rmshr), pg2k(v->t_armshr));
703 printf("Free Memory:\t%juK", pg2k(v->t_free));
708 S_input_id(size_t l2, void *p)
710 struct input_id *id = p;
712 if (l2 != sizeof(*id)) {
713 warnx("S_input_id %zu != %zu", l2, sizeof(*id));
717 printf("{ bustype = 0x%04x, vendor = 0x%04x, "
718 "product = 0x%04x, version = 0x%04x }",
719 id->bustype, id->vendor, id->product, id->version);
724 S_pagesizes(size_t l2, void *p)
731 l = snprintf(buf, sizeof(buf), "{ ");
733 for (i = 0; i * sizeof(*ps) < l2 && ps[i] != 0 && l < sizeof(buf);
735 l += snprintf(&buf[l], sizeof(buf) - l,
736 "%s%lu", i == 0 ? "" : ", ", ps[i]);
739 (void)snprintf(&buf[l], sizeof(buf) - l, " }");
748 S_efi_map(size_t l2, void *p)
750 struct efi_map_header *efihdr;
756 static const char * const types[] = {
757 [EFI_MD_TYPE_NULL] = "Reserved",
758 [EFI_MD_TYPE_CODE] = "LoaderCode",
759 [EFI_MD_TYPE_DATA] = "LoaderData",
760 [EFI_MD_TYPE_BS_CODE] = "BootServicesCode",
761 [EFI_MD_TYPE_BS_DATA] = "BootServicesData",
762 [EFI_MD_TYPE_RT_CODE] = "RuntimeServicesCode",
763 [EFI_MD_TYPE_RT_DATA] = "RuntimeServicesData",
764 [EFI_MD_TYPE_FREE] = "ConventionalMemory",
765 [EFI_MD_TYPE_BAD] = "UnusableMemory",
766 [EFI_MD_TYPE_RECLAIM] = "ACPIReclaimMemory",
767 [EFI_MD_TYPE_FIRMWARE] = "ACPIMemoryNVS",
768 [EFI_MD_TYPE_IOMEM] = "MemoryMappedIO",
769 [EFI_MD_TYPE_IOPORT] = "MemoryMappedIOPortSpace",
770 [EFI_MD_TYPE_PALCODE] = "PalCode",
771 [EFI_MD_TYPE_PERSISTENT] = "PersistentMemory",
775 * Memory map data provided by UEFI via the GetMemoryMap
778 if (l2 < sizeof(*efihdr)) {
779 warnx("S_efi_map length less than header");
783 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
784 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
786 if (efihdr->descriptor_size == 0)
788 if (l2 != efisz + efihdr->memory_size) {
789 warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz +
790 efihdr->memory_size);
793 ndesc = efihdr->memory_size / efihdr->descriptor_size;
795 printf("\n%23s %12s %12s %8s %4s",
796 "Type", "Physical", "Virtual", "#Pages", "Attr");
798 for (i = 0; i < ndesc; i++,
799 map = efi_next_descriptor(map, efihdr->descriptor_size)) {
801 if (map->md_type < nitems(types))
802 type = types[map->md_type];
805 printf("\n%23s %012jx %012jx %08jx ", type,
806 (uintmax_t)map->md_phys, (uintmax_t)map->md_virt,
807 (uintmax_t)map->md_pages);
808 if (map->md_attr & EFI_MD_ATTR_UC)
810 if (map->md_attr & EFI_MD_ATTR_WC)
812 if (map->md_attr & EFI_MD_ATTR_WT)
814 if (map->md_attr & EFI_MD_ATTR_WB)
816 if (map->md_attr & EFI_MD_ATTR_UCE)
818 if (map->md_attr & EFI_MD_ATTR_WP)
820 if (map->md_attr & EFI_MD_ATTR_RP)
822 if (map->md_attr & EFI_MD_ATTR_XP)
824 if (map->md_attr & EFI_MD_ATTR_RT)
831 #if defined(__amd64__) || defined(__i386__)
833 S_bios_smap_xattr(size_t l2, void *p)
835 struct bios_smap_xattr *smap, *end;
837 if (l2 % sizeof(*smap) != 0) {
838 warnx("S_bios_smap_xattr %zu is not a multiple of %zu", l2,
843 end = (struct bios_smap_xattr *)((char *)p + l2);
844 for (smap = p; smap < end; smap++)
845 printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx",
846 smap->type, smap->xattr, (uintmax_t)smap->base,
847 (uintmax_t)smap->length);
853 strIKtoi(const char *str, char **endptrp, const char *fmt)
864 /* caller already checked this */
868 * A format of "IK" is in deciKelvin. A format of "IK3" is in
869 * milliKelvin. The single digit following IK is log10 of the
870 * multiplying factor to convert Kelvin into the untis of this sysctl,
871 * or the dividing factor to convert the sysctl value to Kelvin. Numbers
872 * larger than 6 will run into precision issues with 32-bit integers.
873 * Characters that aren't ASCII digits after the 'K' are ignored. No
874 * localization is present because this is an interface from the kernel
875 * to this program (eg not an end-user interface), so isdigit() isn't
878 if (fmt[2] != '\0' && fmt[2] >= '0' && fmt[2] <= '9')
883 if (*p == 'C' || *p == 'F' || *p == 'K') {
884 temp = strtof(str, endptrp);
885 if (*endptrp != str && *endptrp == p && errno == 0) {
887 temp = (temp - 32) * 5 / 9;
891 for (i = 0; i < prec; i++)
893 return ((int)(temp + 0.5));
896 /* No unit specified -> treat it as a raw number */
897 kelv = (int)strtol(str, endptrp, 10);
898 if (*endptrp != str && *endptrp == p && errno == 0) {
909 * These functions uses a presently undocumented interface to the kernel
910 * to walk the tree and get the type so it can print the value.
911 * This interface is under work and consideration, and should probably
912 * be killed with a big axe by the first person who can find the time.
913 * (be aware though, that the proper interface isn't as obvious as it
914 * may seem, there are various conflicting requirements.
918 name2oid(const char *name, int *oidp)
925 oid[1] = CTL_SYSCTL_NAME2OID;
927 j = CTL_MAXNAME * sizeof(int);
928 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
936 oidfmt(int *oid, int len, char *fmt, u_int *kind)
938 int qoid[CTL_MAXNAME+2];
943 qoid[0] = CTL_SYSCTL;
944 qoid[1] = CTL_SYSCTL_OIDFMT;
945 memcpy(qoid + 2, oid, len * sizeof(int));
948 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
950 err(1, "sysctl fmt %d %zu %d", i, j, errno);
953 *kind = *(u_int *)buf;
956 strcpy(fmt, (char *)(buf + sizeof(u_int)));
961 * This displays a combination of name, type, format, and/or description.
963 * Returns zero if anything was actually output.
964 * Returns one if there is an error.
967 show_info(char *name, const char *sep, int ctltype, char *fmt, int *qoid, int nlen)
975 printf("%s%s", name, sep);
977 if (ctl_typename[ctltype] != NULL)
978 prntype = ctl_typename[ctltype];
984 printf("%s%s", prntype, sep);
986 fputs(prntype, stdout);
989 if (!isprint(fmt[0])) /* Few codes doesn't have formats */
992 printf("%s%s", fmt, sep);
999 qoid[1] = CTL_SYSCTL_OIDDESCR;
1002 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
1010 * This formats and outputs the value of one variable
1012 * Returns zero if anything was actually output.
1013 * Returns one if didn't know what to do with this.
1014 * Return minus one if we had errors.
1017 show_var(int *oid, int nlen, bool honor_skip)
1019 static int skip_len = 0, skip_oid[CTL_MAXNAME];
1020 u_char *val, *oval, *p;
1021 char name[BUFSIZ], fmt[BUFSIZ];
1022 const char *sep, *sep1;
1023 int qoid[CTL_MAXNAME+2];
1026 int i, hexlen, sign, ctltype;
1031 int (*func)(size_t, void *);
1035 umv = mv = intlen = 0;
1038 bzero(name, BUFSIZ);
1039 qoid[0] = CTL_SYSCTL;
1040 qoid[1] = CTL_SYSCTL_NAME;
1041 memcpy(qoid + 2, oid, nlen * sizeof(int));
1043 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
1045 err(1, "sysctl name %d %zu %d", i, j, errno);
1047 oidfmt(oid, nlen, fmt, &kind);
1048 /* if Wflag then only list sysctls that are writeable and not stats. */
1049 if (Wflag && ((kind & CTLFLAG_WR) == 0 || (kind & CTLFLAG_STATS) != 0))
1052 /* if Tflag then only list sysctls that are tuneables. */
1053 if (Tflag && (kind & CTLFLAG_TUN) == 0)
1066 ctltype = (kind & CTLTYPE);
1067 if (tflag || Fflag || dflag)
1068 return show_info(name, sep, ctltype, fmt, qoid, nlen);
1070 /* keep track of encountered skip nodes, ignoring descendants */
1071 if ((skip_len == 0 || skip_len >= nlen * (int)sizeof(int)) &&
1072 (kind & CTLFLAG_SKIP) != 0) {
1073 /* Save this oid so we can skip descendants. */
1074 skip_len = nlen * sizeof(int);
1075 memcpy(skip_oid, oid, skip_len);
1078 /* bail before fetching the value if we're honoring skip */
1080 if (0 < skip_len && skip_len <= nlen * (int)sizeof(int) &&
1081 memcmp(skip_oid, oid, skip_len) == 0)
1083 /* Not a skip node or descendant of a skip node. */
1087 /* don't fetch opaques that we don't know how to print */
1088 if (ctltype == CTLTYPE_OPAQUE) {
1089 if (strcmp(fmt, "S,clockinfo") == 0)
1091 else if (strcmp(fmt, "S,timeval") == 0)
1093 else if (strcmp(fmt, "S,loadavg") == 0)
1095 else if (strcmp(fmt, "S,vmtotal") == 0)
1097 else if (strcmp(fmt, "S,input_id") == 0)
1099 else if (strcmp(fmt, "S,pagesizes") == 0)
1102 else if (strcmp(fmt, "S,efi_map_header") == 0)
1105 #if defined(__amd64__) || defined(__i386__)
1106 else if (strcmp(fmt, "S,bios_smap_xattr") == 0)
1107 func = S_bios_smap_xattr;
1111 if (!bflag && !oflag && !xflag)
1116 /* find an estimate of how much we need for this var */
1121 i = sysctl(oid, nlen, 0, &j, 0, 0);
1122 j += j; /* we want to be sure :-) */
1125 val = oval = malloc(j + 1);
1127 warnx("malloc failed");
1131 i = sysctl(oid, nlen, val, &len, 0, 0);
1132 if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) {
1138 fwrite(val, 1, len, stdout);
1144 sign = ctl_sign[ctltype];
1145 intlen = ctl_size[ctltype];
1148 case CTLTYPE_STRING:
1150 printf("%s%s", name, sep);
1152 printf("%zd%s", len, sep);
1153 printf("%.*s", (int)len, p);
1170 printf("%s%s", name, sep);
1172 printf("%zd%s", len, sep);
1173 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
1175 while (len >= intlen) {
1176 switch (kind & CTLTYPE) {
1189 umv = *(uint8_t *)p;
1194 umv = *(uint16_t *)p;
1199 umv = *(uint32_t *)p;
1204 umv = *(uint64_t *)p;
1208 fputs(sep1, stdout);
1210 printf("%#0*jx", hexlen, umv);
1212 printf(hflag ? "%'ju" : "%ju", umv);
1213 else if (fmt[1] == 'K') {
1218 * See strIKtoi for details on fmt.
1222 prec = fmt[2] - '0';
1224 for (int i = 0; i < prec; i++)
1226 printf("%.*fC", prec,
1227 (float)mv / base - 273.15);
1230 printf(hflag ? "%'jd" : "%jd", mv);
1238 case CTLTYPE_OPAQUE:
1242 printf("%s%s", name, sep);
1244 printf("%zd%s", len, sep);
1245 i = (*func)(len, p);
1251 if (!oflag && !xflag) {
1256 printf("%s%s", name, sep);
1258 printf("%zd%s", len, sep);
1259 printf("Format:%s Length:%zu Dump:0x", fmt, len);
1260 while (len-- && (xflag || p < val + 16))
1261 printf("%02x", *p++);
1262 if (!xflag && len > 16)
1272 sysctl_all(int *oid, int len)
1274 int name1[22], name2[22];
1278 name1[0] = CTL_SYSCTL;
1279 name1[1] = (oid != NULL || Nflag || dflag || tflag) ?
1280 CTL_SYSCTL_NEXTNOSKIP : CTL_SYSCTL_NEXT;
1283 memcpy(name1 + 2, oid, len * sizeof(int));
1286 name1[2] = CTL_KERN;
1291 j = sysctl(name1, l1, name2, &l2, 0, 0);
1293 if (errno == ENOENT)
1296 err(1, "sysctl(getnext) %d %zu", j, l2);
1301 if (len < 0 || l2 < (unsigned int)len)
1304 if (memcmp(name2, oid, len * sizeof(int)) != 0)
1307 i = show_var(name2, l2, true);
1311 memcpy(name1 + 2, name2, l2 * sizeof(int));