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)
347 int len, i, j, save_errno;
349 char *newvalstr = NULL;
351 size_t newsize = Bflag;
352 int mib[CTL_MAXNAME];
353 char *cp, *bufp, *buf, 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.
368 cp = buf = strdup(string);
369 bufp = strsep(&cp, "=:");
371 /* Tflag just lists tunables, do not allow assignment */
372 if (Tflag || Wflag) {
373 warnx("Can't set variables when using -T or -W");
376 /* Trim whitespace before the value. */
379 /* Strip a pair of " or ' if any. */
383 if (cp[strlen(cp) - 1] == *cp)
384 cp[strlen(cp) - 1] = '\0';
388 newsize = strlen(cp);
390 /* Trim whitespace after the name. */
391 cp = bufp + strlen(bufp) - 1;
392 while (cp >= bufp && isspace((int)*cp)) {
398 * Check the name is a useable oid.
400 len = name2oid(bufp, mib);
407 if (errno == ENOENT) {
408 warnx("unknown oid '%s'%s", bufp, line);
410 warn("unknown oid '%s'%s", bufp, line);
417 if (oidfmt(mib, len, fmt, &kind)) {
418 warn("couldn't find format of oid '%s'%s", bufp, line);
427 * We have a useable oid to work with. If there is no value given,
428 * show the node and its children. Otherwise, set the new value.
430 if (newvalstr == NULL || dflag) {
432 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
434 i = show_var(mib, len, false);
438 sysctl_all(mib, len);
440 i = show_var(mib, len, false);
448 * We have a new value to set. Check its validity and parse if numeric.
450 if ((kind & CTLTYPE) == CTLTYPE_NODE) {
451 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);
466 switch (kind & CTLTYPE) {
479 if (strlen(newvalstr) == 0) {
480 warnx("empty numeric value");
488 warnx("oid '%s' is type %d, cannot set that%s",
489 bufp, kind & CTLTYPE, line);
496 switch (kind & CTLTYPE) {
502 while ((cp = strsep(&newvalstr, " ,")) != NULL) {
505 if (!parse_numeric(cp, fmt, kind, &newbuf, &newsize)) {
506 warnx("invalid %s '%s'%s",
507 ctl_typename[kind & CTLTYPE], cp, line);
518 * Show the current value, then set and show the new value.
520 i = show_var(mib, len, false);
521 if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
527 switch (save_errno) {
529 warnx("%s: value is not available%s",
533 warnx("%s: specification is incomplete%s",
537 warnx("%s: type is unknown to this program%s",
541 warnc(save_errno, "%s%s", string, line);
551 j = show_var(mib, len, false);
560 parsefile(const char *filename)
563 char line[BUFSIZ], *p, *pq, *pdq;
564 int warncount = 0, lineno = 0;
566 file = fopen(filename, "r");
568 err(EX_NOINPUT, "%s", filename);
569 while (fgets(line, sizeof(line), file) != NULL) {
572 pq = strchr(line, '\'');
573 pdq = strchr(line, '\"');
574 /* Replace the first # with \0. */
575 while((p = strchr(p, '#')) != NULL) {
576 if (pq != NULL && p > pq) {
577 if ((p = strchr(pq+1, '\'')) != NULL)
580 } else if (pdq != NULL && p > pdq) {
581 if ((p = strchr(pdq+1, '\"')) != NULL)
584 } else if (p == line || *(p-1) != '\\') {
591 p = line + strlen(line) - 1;
592 while (p >= line && isspace((int)*p)) {
597 while (isspace((int)*p))
602 warncount += parse(p, lineno);
609 /* These functions will dump out various interesting structures. */
612 S_clockinfo(size_t l2, void *p)
614 struct clockinfo *ci = (struct clockinfo*)p;
616 if (l2 != sizeof(*ci)) {
617 warnx("S_clockinfo %zu != %zu", l2, sizeof(*ci));
620 printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
621 "{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
622 ci->hz, ci->tick, ci->profhz, ci->stathz);
627 S_loadavg(size_t l2, void *p)
629 struct loadavg *tv = (struct loadavg*)p;
631 if (l2 != sizeof(*tv)) {
632 warnx("S_loadavg %zu != %zu", l2, sizeof(*tv));
635 printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
636 (double)tv->ldavg[0]/(double)tv->fscale,
637 (double)tv->ldavg[1]/(double)tv->fscale,
638 (double)tv->ldavg[2]/(double)tv->fscale);
643 S_timeval(size_t l2, void *p)
645 struct timeval *tv = (struct timeval*)p;
649 if (l2 != sizeof(*tv)) {
650 warnx("S_timeval %zu != %zu", l2, sizeof(*tv));
653 printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
654 "{ sec = %jd, usec = %ld } ",
655 (intmax_t)tv->tv_sec, tv->tv_usec);
657 p1 = strdup(ctime(&tv_sec));
658 for (p2=p1; *p2 ; p2++)
667 S_vmtotal(size_t l2, void *p)
672 if (l2 != sizeof(*v)) {
673 warnx("S_vmtotal %zu != %zu", l2, sizeof(*v));
678 pageKilo = getpagesize() / 1024;
680 #define pg2k(a) ((uintmax_t)(a) * pageKilo)
681 printf("\nSystem wide totals computed every five seconds:"
682 " (values in kilobytes)\n");
683 printf("===============================================\n");
684 printf("Processes:\t\t(RUNQ: %d Disk Wait: %d Page Wait: "
686 v->t_rq, v->t_dw, v->t_pw, v->t_sl);
687 printf("Virtual Memory:\t\t(Total: %juK Active: %juK)\n",
688 pg2k(v->t_vm), pg2k(v->t_avm));
689 printf("Real Memory:\t\t(Total: %juK Active: %juK)\n",
690 pg2k(v->t_rm), pg2k(v->t_arm));
691 printf("Shared Virtual Memory:\t(Total: %juK Active: %juK)\n",
692 pg2k(v->t_vmshr), pg2k(v->t_avmshr));
693 printf("Shared Real Memory:\t(Total: %juK Active: %juK)\n",
694 pg2k(v->t_rmshr), pg2k(v->t_armshr));
695 printf("Free Memory:\t%juK", pg2k(v->t_free));
700 S_input_id(size_t l2, void *p)
702 struct input_id *id = p;
704 if (l2 != sizeof(*id)) {
705 warnx("S_input_id %zu != %zu", l2, sizeof(*id));
709 printf("{ bustype = 0x%04x, vendor = 0x%04x, "
710 "product = 0x%04x, version = 0x%04x }",
711 id->bustype, id->vendor, id->product, id->version);
716 S_pagesizes(size_t l2, void *p)
723 l = snprintf(buf, sizeof(buf), "{ ");
725 for (i = 0; i * sizeof(*ps) < l2 && ps[i] != 0 && l < sizeof(buf);
727 l += snprintf(&buf[l], sizeof(buf) - l,
728 "%s%lu", i == 0 ? "" : ", ", ps[i]);
731 (void)snprintf(&buf[l], sizeof(buf) - l, " }");
740 S_efi_map(size_t l2, void *p)
742 struct efi_map_header *efihdr;
748 static const char * const types[] = {
749 [EFI_MD_TYPE_NULL] = "Reserved",
750 [EFI_MD_TYPE_CODE] = "LoaderCode",
751 [EFI_MD_TYPE_DATA] = "LoaderData",
752 [EFI_MD_TYPE_BS_CODE] = "BootServicesCode",
753 [EFI_MD_TYPE_BS_DATA] = "BootServicesData",
754 [EFI_MD_TYPE_RT_CODE] = "RuntimeServicesCode",
755 [EFI_MD_TYPE_RT_DATA] = "RuntimeServicesData",
756 [EFI_MD_TYPE_FREE] = "ConventionalMemory",
757 [EFI_MD_TYPE_BAD] = "UnusableMemory",
758 [EFI_MD_TYPE_RECLAIM] = "ACPIReclaimMemory",
759 [EFI_MD_TYPE_FIRMWARE] = "ACPIMemoryNVS",
760 [EFI_MD_TYPE_IOMEM] = "MemoryMappedIO",
761 [EFI_MD_TYPE_IOPORT] = "MemoryMappedIOPortSpace",
762 [EFI_MD_TYPE_PALCODE] = "PalCode",
763 [EFI_MD_TYPE_PERSISTENT] = "PersistentMemory",
767 * Memory map data provided by UEFI via the GetMemoryMap
770 if (l2 < sizeof(*efihdr)) {
771 warnx("S_efi_map length less than header");
775 efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
776 map = (struct efi_md *)((uint8_t *)efihdr + efisz);
778 if (efihdr->descriptor_size == 0)
780 if (l2 != efisz + efihdr->memory_size) {
781 warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz +
782 efihdr->memory_size);
785 ndesc = efihdr->memory_size / efihdr->descriptor_size;
787 printf("\n%23s %12s %12s %8s %4s",
788 "Type", "Physical", "Virtual", "#Pages", "Attr");
790 for (i = 0; i < ndesc; i++,
791 map = efi_next_descriptor(map, efihdr->descriptor_size)) {
793 if (map->md_type < nitems(types))
794 type = types[map->md_type];
797 printf("\n%23s %012jx %12p %08jx ", type,
798 (uintmax_t)map->md_phys, map->md_virt,
799 (uintmax_t)map->md_pages);
800 if (map->md_attr & EFI_MD_ATTR_UC)
802 if (map->md_attr & EFI_MD_ATTR_WC)
804 if (map->md_attr & EFI_MD_ATTR_WT)
806 if (map->md_attr & EFI_MD_ATTR_WB)
808 if (map->md_attr & EFI_MD_ATTR_UCE)
810 if (map->md_attr & EFI_MD_ATTR_WP)
812 if (map->md_attr & EFI_MD_ATTR_RP)
814 if (map->md_attr & EFI_MD_ATTR_XP)
816 if (map->md_attr & EFI_MD_ATTR_RT)
823 #if defined(__amd64__) || defined(__i386__)
825 S_bios_smap_xattr(size_t l2, void *p)
827 struct bios_smap_xattr *smap, *end;
829 if (l2 % sizeof(*smap) != 0) {
830 warnx("S_bios_smap_xattr %zu is not a multiple of %zu", l2,
835 end = (struct bios_smap_xattr *)((char *)p + l2);
836 for (smap = p; smap < end; smap++)
837 printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx",
838 smap->type, smap->xattr, (uintmax_t)smap->base,
839 (uintmax_t)smap->length);
845 strIKtoi(const char *str, char **endptrp, const char *fmt)
856 /* caller already checked this */
860 * A format of "IK" is in deciKelvin. A format of "IK3" is in
861 * milliKelvin. The single digit following IK is log10 of the
862 * multiplying factor to convert Kelvin into the untis of this sysctl,
863 * or the dividing factor to convert the sysctl value to Kelvin. Numbers
864 * larger than 6 will run into precision issues with 32-bit integers.
865 * Characters that aren't ASCII digits after the 'K' are ignored. No
866 * localization is present because this is an interface from the kernel
867 * to this program (eg not an end-user interface), so isdigit() isn't
870 if (fmt[2] != '\0' && fmt[2] >= '0' && fmt[2] <= '9')
875 if (*p == 'C' || *p == 'F' || *p == 'K') {
876 temp = strtof(str, endptrp);
877 if (*endptrp != str && *endptrp == p && errno == 0) {
879 temp = (temp - 32) * 5 / 9;
883 for (i = 0; i < prec; i++)
885 return ((int)(temp + 0.5));
888 /* No unit specified -> treat it as a raw number */
889 kelv = (int)strtol(str, endptrp, 10);
890 if (*endptrp != str && *endptrp == p && errno == 0) {
901 * These functions uses a presently undocumented interface to the kernel
902 * to walk the tree and get the type so it can print the value.
903 * This interface is under work and consideration, and should probably
904 * be killed with a big axe by the first person who can find the time.
905 * (be aware though, that the proper interface isn't as obvious as it
906 * may seem, there are various conflicting requirements.
910 name2oid(const char *name, int *oidp)
917 oid[1] = CTL_SYSCTL_NAME2OID;
919 j = CTL_MAXNAME * sizeof(int);
920 i = sysctl(oid, 2, oidp, &j, name, strlen(name));
928 oidfmt(int *oid, int len, char *fmt, u_int *kind)
930 int qoid[CTL_MAXNAME+2];
935 qoid[0] = CTL_SYSCTL;
936 qoid[1] = CTL_SYSCTL_OIDFMT;
937 memcpy(qoid + 2, oid, len * sizeof(int));
940 i = sysctl(qoid, len + 2, buf, &j, 0, 0);
942 err(1, "sysctl fmt %d %zu %d", i, j, errno);
945 *kind = *(u_int *)buf;
948 strcpy(fmt, (char *)(buf + sizeof(u_int)));
953 * This formats and outputs the value of one variable
955 * Returns zero if anything was actually output.
956 * Returns one if didn't know what to do with this.
957 * Return minus one if we had errors.
960 show_var(int *oid, int nlen, bool honor_skip)
962 static int skip_len = 0, skip_oid[CTL_MAXNAME];
963 u_char buf[BUFSIZ], *val, *oval, *p;
964 char name[BUFSIZ], fmt[BUFSIZ];
965 const char *sep, *sep1, *prntype;
966 int qoid[CTL_MAXNAME+2];
969 int i, hexlen, sign, ctltype;
974 int (*func)(size_t, void *);
978 umv = mv = intlen = 0;
983 qoid[0] = CTL_SYSCTL;
984 memcpy(qoid + 2, oid, nlen * sizeof(int));
986 qoid[1] = CTL_SYSCTL_NAME;
988 i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
990 err(1, "sysctl name %d %zu %d", i, j, errno);
992 oidfmt(oid, nlen, fmt, &kind);
993 /* if Wflag then only list sysctls that are writeable and not stats. */
994 if (Wflag && ((kind & CTLFLAG_WR) == 0 || (kind & CTLFLAG_STATS) != 0))
997 /* if Tflag then only list sysctls that are tuneables. */
998 if (Tflag && (kind & CTLFLAG_TUN) == 0)
1011 ctltype = (kind & CTLTYPE);
1012 if (tflag || dflag) {
1014 printf("%s%s", name, sep);
1015 if (ctl_typename[ctltype] != NULL)
1016 prntype = ctl_typename[ctltype];
1018 prntype = "unknown";
1020 printf("%s%s", prntype, sep);
1022 printf("%s", prntype);
1025 qoid[1] = CTL_SYSCTL_OIDDESCR;
1027 i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
1032 /* keep track of encountered skip nodes, ignoring descendants */
1033 if (skip_len == 0 && (kind & CTLFLAG_SKIP) != 0) {
1034 /* Save this oid so we can skip descendants. */
1035 skip_len = nlen * sizeof(int);
1036 memcpy(skip_oid, oid, skip_len);
1039 /* bail before fetching the value if we're honoring skip */
1041 if (0 < skip_len && skip_len <= nlen * (int)sizeof(int) &&
1042 memcmp(skip_oid, oid, skip_len) == 0)
1044 /* Not a skip node or descendant of a skip node. */
1048 /* don't fetch opaques that we don't know how to print */
1049 if (ctltype == CTLTYPE_OPAQUE) {
1050 if (strcmp(fmt, "S,clockinfo") == 0)
1052 else if (strcmp(fmt, "S,timeval") == 0)
1054 else if (strcmp(fmt, "S,loadavg") == 0)
1056 else if (strcmp(fmt, "S,vmtotal") == 0)
1058 else if (strcmp(fmt, "S,input_id") == 0)
1060 else if (strcmp(fmt, "S,pagesizes") == 0)
1063 else if (strcmp(fmt, "S,efi_map_header") == 0)
1066 #if defined(__amd64__) || defined(__i386__)
1067 else if (strcmp(fmt, "S,bios_smap_xattr") == 0)
1068 func = S_bios_smap_xattr;
1072 if (!bflag && !oflag && !xflag)
1077 /* find an estimate of how much we need for this var */
1082 i = sysctl(oid, nlen, 0, &j, 0, 0);
1083 j += j; /* we want to be sure :-) */
1086 val = oval = malloc(j + 1);
1088 warnx("malloc failed");
1092 i = sysctl(oid, nlen, val, &len, 0, 0);
1093 if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) {
1099 fwrite(val, 1, len, stdout);
1105 sign = ctl_sign[ctltype];
1106 intlen = ctl_size[ctltype];
1109 case CTLTYPE_STRING:
1111 printf("%s%s", name, sep);
1112 printf("%.*s", (int)len, p);
1129 printf("%s%s", name, sep);
1130 hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
1132 while (len >= intlen) {
1133 switch (kind & CTLTYPE) {
1146 umv = *(uint8_t *)p;
1151 umv = *(uint16_t *)p;
1156 umv = *(uint32_t *)p;
1161 umv = *(uint64_t *)p;
1165 fputs(sep1, stdout);
1167 printf("%#0*jx", hexlen, umv);
1169 printf(hflag ? "%'ju" : "%ju", umv);
1170 else if (fmt[1] == 'K') {
1175 * See strIKtoi for details on fmt.
1179 prec = fmt[2] - '0';
1181 for (int i = 0; i < prec; i++)
1183 printf("%.*fC", prec,
1184 (float)mv / base - 273.15);
1187 printf(hflag ? "%'jd" : "%jd", mv);
1195 case CTLTYPE_OPAQUE:
1199 printf("%s%s", name, sep);
1200 i = (*func)(len, p);
1206 if (!oflag && !xflag) {
1211 printf("%s%s", name, sep);
1212 printf("Format:%s Length:%zu Dump:0x", fmt, len);
1213 while (len-- && (xflag || p < val + 16))
1214 printf("%02x", *p++);
1215 if (!xflag && len > 16)
1225 sysctl_all(int *oid, int len)
1227 int name1[22], name2[22];
1230 bool honor_skip = false;
1232 name1[0] = CTL_SYSCTL;
1233 name1[1] = (oid != NULL || Nflag || dflag || tflag) ?
1234 CTL_SYSCTL_NEXTNOSKIP : CTL_SYSCTL_NEXT;
1237 memcpy(name1 + 2, oid, len * sizeof(int));
1240 name1[2] = CTL_KERN;
1245 j = sysctl(name1, l1, name2, &l2, 0, 0);
1247 if (errno == ENOENT)
1250 err(1, "sysctl(getnext) %d %zu", j, l2);
1255 if (len < 0 || l2 < (unsigned int)len)
1258 if (memcmp(name2, oid, len * sizeof(int)) != 0)
1261 i = show_var(name2, l2, honor_skip);
1265 memcpy(name1 + 2, name2, l2 * sizeof(int));