xref: /f-stack/tools/sysctl/sysctl.c (revision d4a07e70)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34 
35 #ifndef FSTACK
36 #include <sys/param.h>
37 #include <sys/time.h>
38 #include <sys/resource.h>
39 #include <sys/stat.h>
40 #include <sys/sysctl.h>
41 #include <sys/vmmeter.h>
42 #include <dev/evdev/input.h>
43 
44 #ifdef __amd64__
45 #include <sys/efi.h>
46 #include <machine/metadata.h>
47 #endif
48 
49 #if defined(__amd64__) || defined(__i386__)
50 #include <machine/pc/bios.h>
51 #endif
52 
53 #endif
54 
55 #include <assert.h>
56 #include <ctype.h>
57 #include <err.h>
58 #include <errno.h>
59 #include <inttypes.h>
60 #include <locale.h>
61 #include <stdbool.h>
62 #include <stdio.h>
63 #include <stdlib.h>
64 #include <string.h>
65 #include <sysexits.h>
66 #include <unistd.h>
67 
68 #ifdef FSTACK
69 #include <time.h>
70 #include <limits.h>
71 
72 #include "sys/sysctl.h"
73 #include "ff_ipc.h"
74 
75 struct clockinfo {
76     int hz;        /* clock frequency */
77     int tick;      /* micro-seconds per hz tick */
78     int spare;
79     int stathz;    /* statistics clock frequency */
80     int profhz;    /* profiling clock frequency */
81 };
82 
83 struct loadavg {
84     __uint32_t ldavg[3];
85     long fscale;
86 };
87 
88 /* Structure extended to include extended attribute field in ACPI 3.0. */
89 struct bios_smap_xattr {
90     u_int64_t base;
91     u_int64_t length;
92     u_int32_t type;
93     u_int32_t xattr;
94 } __packed;
95 
96 /* systemwide totals computed every five seconds */
97 struct vmtotal {
98     int16_t t_rq;        /* length of the run queue */
99     int16_t t_dw;        /* jobs in ``disk wait'' (neg priority) */
100     int16_t t_pw;        /* jobs in page wait */
101     int16_t t_sl;        /* jobs sleeping in core */
102     int16_t t_sw;        /* swapped out runnable/short block jobs */
103     int32_t t_vm;        /* total virtual memory */
104     int32_t t_avm;       /* active virtual memory */
105     int32_t t_rm;        /* total real memory in use */
106     int32_t t_arm;       /* active real memory */
107     int32_t t_vmshr;     /* shared virtual memory */
108     int32_t t_avmshr;    /* active shared virtual memory */
109     int32_t t_rmshr;     /* shared real memory */
110     int32_t t_armshr;    /* active shared real memory */
111     int32_t t_free;      /* free memory pages */
112 };
113 
114 struct efi_md {
115     uint32_t md_type;
116 #define EFI_MD_TYPE_NULL    0
117 #define EFI_MD_TYPE_CODE    1   /* Loader text. */
118 #define EFI_MD_TYPE_DATA    2   /* Loader data. */
119 #define EFI_MD_TYPE_BS_CODE 3   /* Boot services text. */
120 #define EFI_MD_TYPE_BS_DATA 4   /* Boot services data. */
121 #define EFI_MD_TYPE_RT_CODE 5   /* Runtime services text. */
122 #define EFI_MD_TYPE_RT_DATA 6   /* Runtime services data. */
123 #define EFI_MD_TYPE_FREE    7   /* Unused/free memory. */
124 #define EFI_MD_TYPE_BAD     8   /* Bad memory */
125 #define EFI_MD_TYPE_RECLAIM 9   /* ACPI reclaimable memory. */
126 #define EFI_MD_TYPE_FIRMWARE    10  /* ACPI NV memory */
127 #define EFI_MD_TYPE_IOMEM   11  /* Memory-mapped I/O. */
128 #define EFI_MD_TYPE_IOPORT  12  /* I/O port space. */
129 #define EFI_MD_TYPE_PALCODE 13  /* PAL */
130 #define	EFI_MD_TYPE_PERSISTENT	14	/* Persistent memory. */
131     uint32_t __pad;
132     uint64_t md_phys;
133     void *md_virt;
134     uint64_t md_pages;
135     uint64_t md_attr;
136 #define EFI_MD_ATTR_UC      0x0000000000000001UL
137 #define EFI_MD_ATTR_WC      0x0000000000000002UL
138 #define EFI_MD_ATTR_WT      0x0000000000000004UL
139 #define EFI_MD_ATTR_WB      0x0000000000000008UL
140 #define EFI_MD_ATTR_UCE     0x0000000000000010UL
141 #define EFI_MD_ATTR_WP      0x0000000000001000UL
142 #define EFI_MD_ATTR_RP      0x0000000000002000UL
143 #define EFI_MD_ATTR_XP      0x0000000000004000UL
144 #define EFI_MD_ATTR_RT      0x8000000000000000UL
145 };
146 
147 struct efi_map_header {
148     uint64_t memory_size;
149     uint64_t descriptor_size;
150     uint32_t descriptor_version;
151 };
152 
153 struct input_id {
154 	uint16_t	bustype;
155 	uint16_t	vendor;
156 	uint16_t	product;
157 	uint16_t	version;
158 };
159 
160 /*
161  * Top-level identifiers
162  */
163 #define	CTL_SYSCTL	0		/* "magic" numbers */
164 #define	CTL_KERN	1		/* "high kernel": proc, limits */
165 #define	CTL_VM		2		/* virtual memory */
166 #define	CTL_VFS		3		/* filesystem, mount type is next */
167 #define	CTL_NET		4		/* network, see socket.h */
168 #define	CTL_DEBUG	5		/* debugging parameters */
169 #define	CTL_HW		6		/* generic cpu/io */
170 #define	CTL_MACHDEP	7		/* machine dependent */
171 #define	CTL_USER	8		/* user-level */
172 #define	CTL_P1003_1B	9		/* POSIX 1003.1B */
173 
174 /*
175  * CTL_SYSCTL identifiers
176  */
177 #define	CTL_SYSCTL_DEBUG	0	/* printf all nodes */
178 #define	CTL_SYSCTL_NAME		1	/* string name of OID */
179 #define	CTL_SYSCTL_NEXT		2	/* next OID, honoring CTLFLAG_SKIP */
180 #define	CTL_SYSCTL_NAME2OID	3	/* int array of name */
181 #define	CTL_SYSCTL_OIDFMT	4	/* OID's kind and format */
182 #define	CTL_SYSCTL_OIDDESCR	5	/* OID's description */
183 #define	CTL_SYSCTL_OIDLABEL	6	/* aggregation label */
184 #define	CTL_SYSCTL_NEXTNOSKIP	7	/* next OID, ignoring CTLFLAG_SKIP */
185 
186 #endif
187 
188 static const char *conffile;
189 
190 static int	aflag, bflag, Bflag, dflag, eflag, hflag, iflag;
191 static int	Nflag, nflag, oflag, qflag, tflag, Tflag, Wflag, xflag;
192 
193 static int	oidfmt(int *, int, char *, u_int *);
194 static int	parsefile(const char *);
195 static int	parse(const char *, int);
196 static int	show_var(int *, int, bool);
197 static int	sysctl_all(int *, int);
198 static int	name2oid(const char *, int *);
199 
200 static int	strIKtoi(const char *, char **, const char *);
201 
202 static int ctl_sign[CTLTYPE+1] = {
203 	[CTLTYPE_INT] = 1,
204 	[CTLTYPE_LONG] = 1,
205 	[CTLTYPE_S8] = 1,
206 	[CTLTYPE_S16] = 1,
207 	[CTLTYPE_S32] = 1,
208 	[CTLTYPE_S64] = 1,
209 };
210 
211 static int ctl_size[CTLTYPE+1] = {
212 	[CTLTYPE_INT] = sizeof(int),
213 	[CTLTYPE_UINT] = sizeof(u_int),
214 	[CTLTYPE_LONG] = sizeof(long),
215 	[CTLTYPE_ULONG] = sizeof(u_long),
216 	[CTLTYPE_S8] = sizeof(int8_t),
217 	[CTLTYPE_S16] = sizeof(int16_t),
218 	[CTLTYPE_S32] = sizeof(int32_t),
219 	[CTLTYPE_S64] = sizeof(int64_t),
220 	[CTLTYPE_U8] = sizeof(uint8_t),
221 	[CTLTYPE_U16] = sizeof(uint16_t),
222 	[CTLTYPE_U32] = sizeof(uint32_t),
223 	[CTLTYPE_U64] = sizeof(uint64_t),
224 };
225 
226 static const char *ctl_typename[CTLTYPE+1] = {
227 	[CTLTYPE_INT] = "integer",
228 	[CTLTYPE_UINT] = "unsigned integer",
229 	[CTLTYPE_LONG] = "long integer",
230 	[CTLTYPE_ULONG] = "unsigned long",
231 	[CTLTYPE_U8] = "uint8_t",
232 	[CTLTYPE_U16] = "uint16_t",
233 	[CTLTYPE_U32] = "uint32_t",
234 	[CTLTYPE_U64] = "uint64_t",
235 	[CTLTYPE_S8] = "int8_t",
236 	[CTLTYPE_S16] = "int16_t",
237 	[CTLTYPE_S32] = "int32_t",
238 	[CTLTYPE_S64] = "int64_t",
239 	[CTLTYPE_NODE] = "node",
240 	[CTLTYPE_STRING] = "string",
241 	[CTLTYPE_OPAQUE] = "opaque",
242 };
243 
244 static void
usage(void)245 usage(void)
246 {
247 #ifndef FSTACK
248 	(void)fprintf(stderr, "%s\n%s\n",
249 	    "usage: sysctl [-bdehiNnoqTtWx] [ -B <bufsize> ] [-f filename] name[=value] ...",
250 	    "       sysctl [-bdehNnoqTtWx] [ -B <bufsize> ] -a");
251 #else
252         (void)fprintf(stderr, "%s\n%s\n",
253             "usage: sysctl -p <f-stack proc_id> [-bdehiNnoqTtWx] [ -B <bufsize> ] [-f filename] name[=value] ...",
254             "       sysctl -p <f-stack proc_id> [-bdehNnoqTtWx] [ -B <bufsize> ] -a");
255         ff_ipc_exit();
256 #endif
257 	exit(1);
258 }
259 
260 int
main(int argc,char ** argv)261 main(int argc, char **argv)
262 {
263 	int ch;
264 	int warncount = 0;
265 
266 #ifdef FSTACK
267 	ff_ipc_init();
268 #endif
269 
270 	setlocale(LC_NUMERIC, "");
271 	setbuf(stdout,0);
272 	setbuf(stderr,0);
273 
274 #ifndef FSTACK
275 	while ((ch = getopt(argc, argv, "AabB:def:hiNnoqtTwWxX")) != -1) {
276 #else
277 	while ((ch = getopt(argc, argv, "AabB:def:hiNnoqtTwWxXp:")) != -1) {
278 #endif
279 		switch (ch) {
280 		case 'A':
281 			/* compatibility */
282 			aflag = oflag = 1;
283 			break;
284 		case 'a':
285 			aflag = 1;
286 			break;
287 		case 'b':
288 			bflag = 1;
289 			break;
290 		case 'B':
291 			Bflag = strtol(optarg, NULL, 0);
292 			break;
293 		case 'd':
294 			dflag = 1;
295 			break;
296 		case 'e':
297 			eflag = 1;
298 			break;
299 		case 'f':
300 			conffile = optarg;
301 			break;
302 		case 'h':
303 			hflag = 1;
304 			break;
305 		case 'i':
306 			iflag = 1;
307 			break;
308 		case 'N':
309 			Nflag = 1;
310 			break;
311 		case 'n':
312 			nflag = 1;
313 			break;
314 		case 'o':
315 			oflag = 1;
316 			break;
317 		case 'q':
318 			qflag = 1;
319 			break;
320 		case 't':
321 			tflag = 1;
322 			break;
323 		case 'T':
324 			Tflag = 1;
325 			break;
326 		case 'w':
327 			/* compatibility */
328 			/* ignored */
329 			break;
330 		case 'W':
331 			Wflag = 1;
332 			break;
333 		case 'X':
334 			/* compatibility */
335 			aflag = xflag = 1;
336 			break;
337 		case 'x':
338 			xflag = 1;
339 			break;
340 #ifdef FSTACK
341 		case 'p':
342 			ff_set_proc_id(atoi(optarg));
343 			break;
344 #endif
345 		default:
346 			usage();
347 		}
348 	}
349 	argc -= optind;
350 	argv += optind;
351 
352 	if (Nflag && nflag)
353 		usage();
354 	if (aflag && argc == 0)
355 #ifndef FSTACK
356 		exit(sysctl_all(NULL, 0));
357 #else
358 	{
359 		int ret = sysctl_all(0, 0);
360 		ff_ipc_exit();
361 		exit(ret);
362 	}
363 #endif
364 
365 	if (argc == 0 && conffile == NULL)
366 		usage();
367 
368 	warncount = 0;
369 	if (conffile != NULL)
370 		warncount += parsefile(conffile);
371 
372 	while (argc-- > 0)
373 		warncount += parse(*argv++, 0);
374 
375 #ifdef FSTACK
376 	ff_ipc_exit();
377 #endif
378 
379 	return (warncount);
380 }
381 
382 /*
383  * Parse a single numeric value, append it to 'newbuf', and update
384  * 'newsize'.  Returns true if the value was parsed and false if the
385  * value was invalid.  Non-numeric types (strings) are handled
386  * directly in parse().
387  */
388 static bool
389 parse_numeric(const char *newvalstr, const char *fmt, u_int kind,
390     void **newbufp, size_t *newsizep)
391 {
392 	void *newbuf;
393 	const void *newval;
394 	int8_t i8val;
395 	uint8_t u8val;
396 	int16_t i16val;
397 	uint16_t u16val;
398 	int32_t i32val;
399 	uint32_t u32val;
400 	int intval;
401 	unsigned int uintval;
402 	long longval;
403 	unsigned long ulongval;
404 	int64_t i64val;
405 	uint64_t u64val;
406 	size_t valsize;
407 	char *endptr = NULL;
408 
409 	errno = 0;
410 
411 	switch (kind & CTLTYPE) {
412 	case CTLTYPE_INT:
413 		if (strncmp(fmt, "IK", 2) == 0)
414 			intval = strIKtoi(newvalstr, &endptr, fmt);
415 		else
416 			intval = (int)strtol(newvalstr, &endptr, 0);
417 		newval = &intval;
418 		valsize = sizeof(intval);
419 		break;
420 	case CTLTYPE_UINT:
421 		uintval = (int) strtoul(newvalstr, &endptr, 0);
422 		newval = &uintval;
423 		valsize = sizeof(uintval);
424 		break;
425 	case CTLTYPE_LONG:
426 		longval = strtol(newvalstr, &endptr, 0);
427 		newval = &longval;
428 		valsize = sizeof(longval);
429 		break;
430 	case CTLTYPE_ULONG:
431 		ulongval = strtoul(newvalstr, &endptr, 0);
432 		newval = &ulongval;
433 		valsize = sizeof(ulongval);
434 		break;
435 	case CTLTYPE_S8:
436 		i8val = (int8_t)strtol(newvalstr, &endptr, 0);
437 		newval = &i8val;
438 		valsize = sizeof(i8val);
439 		break;
440 	case CTLTYPE_S16:
441 		i16val = (int16_t)strtol(newvalstr, &endptr, 0);
442 		newval = &i16val;
443 		valsize = sizeof(i16val);
444 		break;
445 	case CTLTYPE_S32:
446 		i32val = (int32_t)strtol(newvalstr, &endptr, 0);
447 		newval = &i32val;
448 		valsize = sizeof(i32val);
449 		break;
450 	case CTLTYPE_S64:
451 		i64val = strtoimax(newvalstr, &endptr, 0);
452 		newval = &i64val;
453 		valsize = sizeof(i64val);
454 		break;
455 	case CTLTYPE_U8:
456 		u8val = (uint8_t)strtoul(newvalstr, &endptr, 0);
457 		newval = &u8val;
458 		valsize = sizeof(u8val);
459 		break;
460 	case CTLTYPE_U16:
461 		u16val = (uint16_t)strtoul(newvalstr, &endptr, 0);
462 		newval = &u16val;
463 		valsize = sizeof(u16val);
464 		break;
465 	case CTLTYPE_U32:
466 		u32val = (uint32_t)strtoul(newvalstr, &endptr, 0);
467 		newval = &u32val;
468 		valsize = sizeof(u32val);
469 		break;
470 	case CTLTYPE_U64:
471 		u64val = strtoumax(newvalstr, &endptr, 0);
472 		newval = &u64val;
473 		valsize = sizeof(u64val);
474 		break;
475 	default:
476 		/* NOTREACHED */
477 		abort();
478 	}
479 
480 	if (errno != 0 || endptr == newvalstr ||
481 	    (endptr != NULL && *endptr != '\0'))
482 		return (false);
483 
484 	newbuf = realloc(*newbufp, *newsizep + valsize);
485 	if (newbuf == NULL)
486 		err(1, "out of memory");
487 	memcpy((char *)newbuf + *newsizep, newval, valsize);
488 	*newbufp = newbuf;
489 	*newsizep += valsize;
490 
491 	return (true);
492 }
493 
494 /*
495  * Parse a name into a MIB entry.
496  * Lookup and print out the MIB entry if it exists.
497  * Set a new value if requested.
498  */
499 static int
500 parse(const char *string, int lineno)
501 {
502 	int len, i, j, save_errno;
503 	const void *newval;
504 	char *newvalstr = NULL;
505 	void *newbuf;
506 	size_t newsize = Bflag;
507 	int mib[CTL_MAXNAME];
508 	char *cp, *bufp, *buf, fmt[BUFSIZ], line[BUFSIZ];
509 	u_int kind;
510 
511 	if (lineno)
512 		snprintf(line, sizeof(line), " at line %d", lineno);
513 	else
514 		line[0] = '\0';
515 
516 	/*
517 	 * Split the string into name and value.
518 	 *
519 	 * Either = or : may be used as the delimiter.
520 	 * Whitespace surrounding the delimiter is trimmed.
521 	 * Quotes around the value are stripped.
522 	 */
523 	cp = buf = strdup(string);
524 	bufp = strsep(&cp, "=:");
525 	if (cp != NULL) {
526 		/* Tflag just lists tunables, do not allow assignment */
527 		if (Tflag || Wflag) {
528 			warnx("Can't set variables when using -T or -W");
529 			usage();
530 		}
531 		/* Trim whitespace before the value. */
532 		while (isspace(*cp))
533 			cp++;
534 		/* Strip a pair of " or ' if any. */
535 		switch (*cp) {
536 		case '\"':
537 		case '\'':
538 			if (cp[strlen(cp) - 1] == *cp)
539 				cp[strlen(cp) - 1] = '\0';
540 			cp++;
541 		}
542 		newvalstr = cp;
543 		newsize = strlen(cp);
544 	}
545 	/* Trim whitespace after the name. */
546 	cp = bufp + strlen(bufp) - 1;
547 	while (cp >= bufp && isspace((int)*cp)) {
548 		*cp = '\0';
549 		cp--;
550 	}
551 
552 	/*
553 	 * Check the name is a useable oid.
554 	 */
555 	len = name2oid(bufp, mib);
556 	if (len < 0) {
557 		if (iflag) {
558 			free(buf);
559 			return (0);
560 		}
561 		if (!qflag) {
562 			if (errno == ENOENT) {
563 				warnx("unknown oid '%s'%s", bufp, line);
564 			} else {
565 				warn("unknown oid '%s'%s", bufp, line);
566 			}
567 		}
568 		free(buf);
569 		return (1);
570 	}
571 
572 	if (oidfmt(mib, len, fmt, &kind)) {
573 		warn("couldn't find format of oid '%s'%s", bufp, line);
574 		free(buf);
575 		if (iflag)
576 			return (1);
577 		else
578 #ifndef FSTACK
579 			exit(1);
580 #else
581 		{
582 			ff_ipc_exit();
583 			exit(1);
584 		}
585 #endif
586 	}
587 
588 	/*
589 	 * We have a useable oid to work with.  If there is no value given,
590 	 * show the node and its children.  Otherwise, set the new value.
591 	 */
592 	if (newvalstr == NULL || dflag) {
593 		free(buf);
594 		if ((kind & CTLTYPE) == CTLTYPE_NODE) {
595 			if (dflag) {
596 				i = show_var(mib, len, false);
597 				if (!i && !bflag)
598 					putchar('\n');
599 			}
600 			sysctl_all(mib, len);
601 		} else {
602 			i = show_var(mib, len, false);
603 			if (!i && !bflag)
604 				putchar('\n');
605 		}
606 		return (0);
607 	}
608 
609 	/*
610 	 * We have a new value to set.  Check its validity and parse if numeric.
611 	 */
612 	if ((kind & CTLTYPE) == CTLTYPE_NODE) {
613 		warnx("oid '%s' isn't a leaf node%s", bufp, line);
614 		free(buf);
615 		return (1);
616 	}
617 
618 	if (!(kind & CTLFLAG_WR)) {
619 		if (kind & CTLFLAG_TUN) {
620 			warnx("oid '%s' is a read only tunable%s", bufp, line);
621 			warnx("Tunable values are set in /boot/loader.conf");
622 		} else
623 			warnx("oid '%s' is read only%s", bufp, line);
624 		free(buf);
625 		return (1);
626 	}
627 
628 	switch (kind & CTLTYPE) {
629 	case CTLTYPE_INT:
630 	case CTLTYPE_UINT:
631 	case CTLTYPE_LONG:
632 	case CTLTYPE_ULONG:
633 	case CTLTYPE_S8:
634 	case CTLTYPE_S16:
635 	case CTLTYPE_S32:
636 	case CTLTYPE_S64:
637 	case CTLTYPE_U8:
638 	case CTLTYPE_U16:
639 	case CTLTYPE_U32:
640 	case CTLTYPE_U64:
641 		if (strlen(newvalstr) == 0) {
642 			warnx("empty numeric value");
643 			free(buf);
644 			return (1);
645 		}
646 		/* FALLTHROUGH */
647 	case CTLTYPE_STRING:
648 		break;
649 	default:
650 		warnx("oid '%s' is type %d, cannot set that%s",
651 		    bufp, kind & CTLTYPE, line);
652 		free(buf);
653 		return (1);
654 	}
655 
656 	newbuf = NULL;
657 
658 	switch (kind & CTLTYPE) {
659 	case CTLTYPE_STRING:
660 		newval = newvalstr;
661 		break;
662 	default:
663 		newsize = 0;
664 		while ((cp = strsep(&newvalstr, " ,")) != NULL) {
665 			if (*cp == '\0')
666 				continue;
667 			if (!parse_numeric(cp, fmt, kind, &newbuf, &newsize)) {
668 				warnx("invalid %s '%s'%s",
669 				    ctl_typename[kind & CTLTYPE], cp, line);
670 				free(newbuf);
671 				free(buf);
672 				return (1);
673 			}
674 		}
675 		newval = newbuf;
676 		break;
677 	}
678 
679 	/*
680 	 * Show the current value, then set and show the new value.
681 	 */
682 	i = show_var(mib, len, false);
683 	if (sysctl(mib, len, 0, 0, newval, newsize) == -1) {
684 		save_errno = errno;
685 		free(newbuf);
686 		free(buf);
687 		if (!i && !bflag)
688 			putchar('\n');
689 		switch (save_errno) {
690 		case EOPNOTSUPP:
691 			warnx("%s: value is not available%s",
692 			    string, line);
693 			return (1);
694 		case ENOTDIR:
695 			warnx("%s: specification is incomplete%s",
696 			    string, line);
697 			return (1);
698 		case ENOMEM:
699 			warnx("%s: type is unknown to this program%s",
700 			    string, line);
701 			return (1);
702 		default:
703 #ifndef FSTACK
704 			warnc(save_errno, "%s%s", string, line);
705 #else
706 			warn("%s%s", string, line);
707 #endif
708 			return (1);
709 		}
710 	}
711 	free(newbuf);
712 	free(buf);
713 	if (!bflag)
714 		printf(" -> ");
715 	i = nflag;
716 	nflag = 1;
717 	j = show_var(mib, len, false);
718 	if (!j && !bflag)
719 		putchar('\n');
720 	nflag = i;
721 
722 	return (0);
723 }
724 
725 static int
726 parsefile(const char *filename)
727 {
728 	FILE *file;
729 	char line[BUFSIZ], *p, *pq, *pdq;
730 	int warncount = 0, lineno = 0;
731 
732 	file = fopen(filename, "r");
733 	if (file == NULL)
734 		err(EX_NOINPUT, "%s", filename);
735 	while (fgets(line, sizeof(line), file) != NULL) {
736 		lineno++;
737 		p = line;
738 		pq = strchr(line, '\'');
739 		pdq = strchr(line, '\"');
740 		/* Replace the first # with \0. */
741 		while((p = strchr(p, '#')) != NULL) {
742 			if (pq != NULL && p > pq) {
743 				if ((p = strchr(pq+1, '\'')) != NULL)
744 					*(++p) = '\0';
745 				break;
746 			} else if (pdq != NULL && p > pdq) {
747 				if ((p = strchr(pdq+1, '\"')) != NULL)
748 					*(++p) = '\0';
749 				break;
750 			} else if (p == line || *(p-1) != '\\') {
751 				*p = '\0';
752 				break;
753 			}
754 			p++;
755 		}
756 		/* Trim spaces */
757 		p = line + strlen(line) - 1;
758 		while (p >= line && isspace((int)*p)) {
759 			*p = '\0';
760 			p--;
761 		}
762 		p = line;
763 		while (isspace((int)*p))
764 			p++;
765 		if (*p == '\0')
766 			continue;
767 		else
768 			warncount += parse(p, lineno);
769 	}
770 	fclose(file);
771 
772 	return (warncount);
773 }
774 
775 /* These functions will dump out various interesting structures. */
776 
777 static int
778 S_clockinfo(size_t l2, void *p)
779 {
780 	struct clockinfo *ci = (struct clockinfo*)p;
781 
782 	if (l2 != sizeof(*ci)) {
783 		warnx("S_clockinfo %zu != %zu", l2, sizeof(*ci));
784 		return (1);
785 	}
786 	printf(hflag ? "{ hz = %'d, tick = %'d, profhz = %'d, stathz = %'d }" :
787 		"{ hz = %d, tick = %d, profhz = %d, stathz = %d }",
788 		ci->hz, ci->tick, ci->profhz, ci->stathz);
789 	return (0);
790 }
791 
792 static int
793 S_loadavg(size_t l2, void *p)
794 {
795 	struct loadavg *tv = (struct loadavg*)p;
796 
797 	if (l2 != sizeof(*tv)) {
798 		warnx("S_loadavg %zu != %zu", l2, sizeof(*tv));
799 		return (1);
800 	}
801 	printf(hflag ? "{ %'.2f %'.2f %'.2f }" : "{ %.2f %.2f %.2f }",
802 		(double)tv->ldavg[0]/(double)tv->fscale,
803 		(double)tv->ldavg[1]/(double)tv->fscale,
804 		(double)tv->ldavg[2]/(double)tv->fscale);
805 	return (0);
806 }
807 
808 static int
809 S_timeval(size_t l2, void *p)
810 {
811 	struct timeval *tv = (struct timeval*)p;
812 	time_t tv_sec;
813 	char *p1, *p2;
814 
815 	if (l2 != sizeof(*tv)) {
816 		warnx("S_timeval %zu != %zu", l2, sizeof(*tv));
817 		return (1);
818 	}
819 	printf(hflag ? "{ sec = %'jd, usec = %'ld } " :
820 		"{ sec = %jd, usec = %ld } ",
821 		(intmax_t)tv->tv_sec, tv->tv_usec);
822 	tv_sec = tv->tv_sec;
823 	p1 = strdup(ctime(&tv_sec));
824 	for (p2=p1; *p2 ; p2++)
825 		if (*p2 == '\n')
826 			*p2 = '\0';
827 	fputs(p1, stdout);
828 	free(p1);
829 	return (0);
830 }
831 
832 static int
833 S_vmtotal(size_t l2, void *p)
834 {
835 	struct vmtotal *v;
836 	int pageKilo;
837 
838 	if (l2 != sizeof(*v)) {
839 		warnx("S_vmtotal %zu != %zu", l2, sizeof(*v));
840 		return (1);
841 	}
842 
843 	v = p;
844 	pageKilo = getpagesize() / 1024;
845 
846 #define	pg2k(a)	((uintmax_t)(a) * pageKilo)
847 	printf("\nSystem wide totals computed every five seconds:"
848 	    " (values in kilobytes)\n");
849 	printf("===============================================\n");
850 	printf("Processes:\t\t(RUNQ: %d Disk Wait: %d Page Wait: "
851 	    "%d Sleep: %d)\n",
852 	    v->t_rq, v->t_dw, v->t_pw, v->t_sl);
853 	printf("Virtual Memory:\t\t(Total: %juK Active: %juK)\n",
854 	    pg2k(v->t_vm), pg2k(v->t_avm));
855 	printf("Real Memory:\t\t(Total: %juK Active: %juK)\n",
856 	    pg2k(v->t_rm), pg2k(v->t_arm));
857 	printf("Shared Virtual Memory:\t(Total: %juK Active: %juK)\n",
858 	    pg2k(v->t_vmshr), pg2k(v->t_avmshr));
859 	printf("Shared Real Memory:\t(Total: %juK Active: %juK)\n",
860 	    pg2k(v->t_rmshr), pg2k(v->t_armshr));
861 	printf("Free Memory:\t%juK", pg2k(v->t_free));
862 	return (0);
863 }
864 
865 static int
866 S_input_id(size_t l2, void *p)
867 {
868 	struct input_id *id = p;
869 
870 	if (l2 != sizeof(*id)) {
871 		warnx("S_input_id %zu != %zu", l2, sizeof(*id));
872 		return (1);
873 	}
874 
875 	printf("{ bustype = 0x%04x, vendor = 0x%04x, "
876 	    "product = 0x%04x, version = 0x%04x }",
877 	    id->bustype, id->vendor, id->product, id->version);
878 	return (0);
879 }
880 
881 static int
882 S_pagesizes(size_t l2, void *p)
883 {
884 	char buf[256];
885 	u_long *ps;
886 	size_t l;
887 	int i;
888 
889 	l = snprintf(buf, sizeof(buf), "{ ");
890 	ps = p;
891 	for (i = 0; i * sizeof(*ps) < l2 && ps[i] != 0 && l < sizeof(buf);
892 	    i++) {
893 		l += snprintf(&buf[l], sizeof(buf) - l,
894 		    "%s%lu", i == 0 ? "" : ", ", ps[i]);
895 	}
896 	if (l < sizeof(buf))
897 		(void)snprintf(&buf[l], sizeof(buf) - l, " }");
898 
899 	printf("%s", buf);
900 
901 	return (0);
902 }
903 
904 #ifdef __amd64__
905 #ifdef FSTACK
906 #define efi_next_descriptor(ptr, size) \
907     ((struct efi_md *)(((uint8_t *)(ptr)) + (size)))
908 #endif
909 
910 static int
911 S_efi_map(size_t l2, void *p)
912 {
913 	struct efi_map_header *efihdr;
914 	struct efi_md *map;
915 	const char *type;
916 	size_t efisz;
917 	int ndesc, i;
918 
919 	static const char * const types[] = {
920 		[EFI_MD_TYPE_NULL] =	"Reserved",
921 		[EFI_MD_TYPE_CODE] =	"LoaderCode",
922 		[EFI_MD_TYPE_DATA] =	"LoaderData",
923 		[EFI_MD_TYPE_BS_CODE] =	"BootServicesCode",
924 		[EFI_MD_TYPE_BS_DATA] =	"BootServicesData",
925 		[EFI_MD_TYPE_RT_CODE] =	"RuntimeServicesCode",
926 		[EFI_MD_TYPE_RT_DATA] =	"RuntimeServicesData",
927 		[EFI_MD_TYPE_FREE] =	"ConventionalMemory",
928 		[EFI_MD_TYPE_BAD] =	"UnusableMemory",
929 		[EFI_MD_TYPE_RECLAIM] =	"ACPIReclaimMemory",
930 		[EFI_MD_TYPE_FIRMWARE] = "ACPIMemoryNVS",
931 		[EFI_MD_TYPE_IOMEM] =	"MemoryMappedIO",
932 		[EFI_MD_TYPE_IOPORT] =	"MemoryMappedIOPortSpace",
933 		[EFI_MD_TYPE_PALCODE] =	"PalCode",
934 		[EFI_MD_TYPE_PERSISTENT] = "PersistentMemory",
935 	};
936 
937 	/*
938 	 * Memory map data provided by UEFI via the GetMemoryMap
939 	 * Boot Services API.
940 	 */
941 	if (l2 < sizeof(*efihdr)) {
942 		warnx("S_efi_map length less than header");
943 		return (1);
944 	}
945 	efihdr = p;
946 	efisz = (sizeof(struct efi_map_header) + 0xf) & ~0xf;
947 	map = (struct efi_md *)((uint8_t *)efihdr + efisz);
948 
949 	if (efihdr->descriptor_size == 0)
950 		return (0);
951 	if (l2 != efisz + efihdr->memory_size) {
952 		warnx("S_efi_map length mismatch %zu vs %zu", l2, efisz +
953 		    efihdr->memory_size);
954 		return (1);
955 	}
956 	ndesc = efihdr->memory_size / efihdr->descriptor_size;
957 
958 	printf("\n%23s %12s %12s %8s %4s",
959 	    "Type", "Physical", "Virtual", "#Pages", "Attr");
960 
961 	for (i = 0; i < ndesc; i++,
962 	    map = efi_next_descriptor(map, efihdr->descriptor_size)) {
963 		type = NULL;
964 		if (map->md_type < nitems(types))
965 			type = types[map->md_type];
966 		if (type == NULL)
967 			type = "<INVALID>";
968 		printf("\n%23s %012jx %12p %08jx ", type,
969 		    (uintmax_t)map->md_phys, map->md_virt,
970 		    (uintmax_t)map->md_pages);
971 		if (map->md_attr & EFI_MD_ATTR_UC)
972 			printf("UC ");
973 		if (map->md_attr & EFI_MD_ATTR_WC)
974 			printf("WC ");
975 		if (map->md_attr & EFI_MD_ATTR_WT)
976 			printf("WT ");
977 		if (map->md_attr & EFI_MD_ATTR_WB)
978 			printf("WB ");
979 		if (map->md_attr & EFI_MD_ATTR_UCE)
980 			printf("UCE ");
981 		if (map->md_attr & EFI_MD_ATTR_WP)
982 			printf("WP ");
983 		if (map->md_attr & EFI_MD_ATTR_RP)
984 			printf("RP ");
985 		if (map->md_attr & EFI_MD_ATTR_XP)
986 			printf("XP ");
987 		if (map->md_attr & EFI_MD_ATTR_RT)
988 			printf("RUNTIME");
989 	}
990 	return (0);
991 }
992 #endif
993 
994 #if defined(__amd64__) || defined(__i386__)
995 static int
996 S_bios_smap_xattr(size_t l2, void *p)
997 {
998 	struct bios_smap_xattr *smap, *end;
999 
1000 	if (l2 % sizeof(*smap) != 0) {
1001 		warnx("S_bios_smap_xattr %zu is not a multiple of %zu", l2,
1002 		    sizeof(*smap));
1003 		return (1);
1004 	}
1005 
1006 	end = (struct bios_smap_xattr *)((char *)p + l2);
1007 	for (smap = p; smap < end; smap++)
1008 		printf("\nSMAP type=%02x, xattr=%02x, base=%016jx, len=%016jx",
1009 		    smap->type, smap->xattr, (uintmax_t)smap->base,
1010 		    (uintmax_t)smap->length);
1011 	return (0);
1012 }
1013 #endif
1014 
1015 static int
1016 strIKtoi(const char *str, char **endptrp, const char *fmt)
1017 {
1018 	int kelv;
1019 	float temp;
1020 	size_t len;
1021 	const char *p;
1022 	int prec, i;
1023 
1024 	assert(errno == 0);
1025 
1026 	len = strlen(str);
1027 	/* caller already checked this */
1028 	assert(len > 0);
1029 
1030 	/*
1031 	 * A format of "IK" is in deciKelvin. A format of "IK3" is in
1032 	 * milliKelvin. The single digit following IK is log10 of the
1033 	 * multiplying factor to convert Kelvin into the untis of this sysctl,
1034 	 * or the dividing factor to convert the sysctl value to Kelvin. Numbers
1035 	 * larger than 6 will run into precision issues with 32-bit integers.
1036 	 * Characters that aren't ASCII digits after the 'K' are ignored. No
1037 	 * localization is present because this is an interface from the kernel
1038 	 * to this program (eg not an end-user interface), so isdigit() isn't
1039 	 * used here.
1040 	 */
1041 	if (fmt[2] != '\0' && fmt[2] >= '0' && fmt[2] <= '9')
1042 		prec = fmt[2] - '0';
1043 	else
1044 		prec = 1;
1045 	p = &str[len - 1];
1046 	if (*p == 'C' || *p == 'F' || *p == 'K') {
1047 		temp = strtof(str, endptrp);
1048 		if (*endptrp != str && *endptrp == p && errno == 0) {
1049 			if (*p == 'F')
1050 				temp = (temp - 32) * 5 / 9;
1051 			*endptrp = NULL;
1052 			if (*p != 'K')
1053 				temp += 273.15;
1054 			for (i = 0; i < prec; i++)
1055 				temp *= 10.0;
1056 			return ((int)(temp + 0.5));
1057 		}
1058 	} else {
1059 		/* No unit specified -> treat it as a raw number */
1060 		kelv = (int)strtol(str, endptrp, 10);
1061 		if (*endptrp != str && *endptrp == p && errno == 0) {
1062 			*endptrp = NULL;
1063 			return (kelv);
1064 		}
1065 	}
1066 
1067 	errno = ERANGE;
1068 	return (0);
1069 }
1070 
1071 /*
1072  * These functions uses a presently undocumented interface to the kernel
1073  * to walk the tree and get the type so it can print the value.
1074  * This interface is under work and consideration, and should probably
1075  * be killed with a big axe by the first person who can find the time.
1076  * (be aware though, that the proper interface isn't as obvious as it
1077  * may seem, there are various conflicting requirements.
1078  */
1079 
1080 static int
1081 name2oid(const char *name, int *oidp)
1082 {
1083 	int oid[2];
1084 	int i;
1085 	size_t j;
1086 
1087 	oid[0] = CTL_SYSCTL;
1088 	oid[1] = CTL_SYSCTL_NAME2OID;
1089 
1090 	j = CTL_MAXNAME * sizeof(int);
1091 	i = sysctl(oid, 2, oidp, &j, name, strlen(name));
1092 	if (i < 0)
1093 		return (i);
1094 	j /= sizeof(int);
1095 	return (j);
1096 }
1097 
1098 static int
1099 oidfmt(int *oid, int len, char *fmt, u_int *kind)
1100 {
1101 	int qoid[CTL_MAXNAME+2];
1102 	u_char buf[BUFSIZ];
1103 	int i;
1104 	size_t j;
1105 
1106 	qoid[0] = CTL_SYSCTL;
1107 	qoid[1] = CTL_SYSCTL_OIDFMT;
1108 	memcpy(qoid + 2, oid, len * sizeof(int));
1109 
1110 	j = sizeof(buf);
1111 	i = sysctl(qoid, len + 2, buf, &j, 0, 0);
1112 	if (i)
1113 		err(1, "sysctl fmt %d %zu %d", i, j, errno);
1114 
1115 	if (kind)
1116 		*kind = *(u_int *)buf;
1117 
1118 	if (fmt)
1119 		strcpy(fmt, (char *)(buf + sizeof(u_int)));
1120 	return (0);
1121 }
1122 
1123 /*
1124  * This formats and outputs the value of one variable
1125  *
1126  * Returns zero if anything was actually output.
1127  * Returns one if didn't know what to do with this.
1128  * Return minus one if we had errors.
1129  */
1130 static int
1131 show_var(int *oid, int nlen, bool honor_skip)
1132 {
1133 	static int skip_len = 0, skip_oid[CTL_MAXNAME];
1134 	u_char buf[BUFSIZ], *val, *oval, *p;
1135 	char name[BUFSIZ], fmt[BUFSIZ];
1136 	const char *sep, *sep1, *prntype;
1137 	int qoid[CTL_MAXNAME+2];
1138 	uintmax_t umv;
1139 	intmax_t mv;
1140 	int i, hexlen, sign, ctltype;
1141 	size_t intlen;
1142 	size_t j, len;
1143 	u_int kind;
1144 	float base;
1145 	int (*func)(size_t, void *);
1146 	int prec;
1147 
1148 	/* Silence GCC. */
1149 	umv = mv = intlen = 0;
1150 
1151 	bzero(buf, BUFSIZ);
1152 	bzero(fmt, BUFSIZ);
1153 	bzero(name, BUFSIZ);
1154 	qoid[0] = CTL_SYSCTL;
1155 	memcpy(qoid + 2, oid, nlen * sizeof(int));
1156 
1157 	qoid[1] = CTL_SYSCTL_NAME;
1158 	j = sizeof(name);
1159 	i = sysctl(qoid, nlen + 2, name, &j, 0, 0);
1160 	if (i || !j)
1161 		err(1, "sysctl name %d %zu %d", i, j, errno);
1162 
1163 	oidfmt(oid, nlen, fmt, &kind);
1164 	/* if Wflag then only list sysctls that are writeable and not stats. */
1165 	if (Wflag && ((kind & CTLFLAG_WR) == 0 || (kind & CTLFLAG_STATS) != 0))
1166 		return (1);
1167 
1168 	/* if Tflag then only list sysctls that are tuneables. */
1169 	if (Tflag && (kind & CTLFLAG_TUN) == 0)
1170 		return (1);
1171 
1172 	if (Nflag) {
1173 		printf("%s", name);
1174 		return (0);
1175 	}
1176 
1177 	if (eflag)
1178 		sep = "=";
1179 	else
1180 		sep = ": ";
1181 
1182 	ctltype = (kind & CTLTYPE);
1183 	if (tflag || dflag) {
1184 		if (!nflag)
1185 			printf("%s%s", name, sep);
1186         	if (ctl_typename[ctltype] != NULL)
1187             		prntype = ctl_typename[ctltype];
1188         	else
1189             		prntype = "unknown";
1190 		if (tflag && dflag)
1191 			printf("%s%s", prntype, sep);
1192 		else if (tflag) {
1193 			printf("%s", prntype);
1194 			return (0);
1195 		}
1196 		qoid[1] = CTL_SYSCTL_OIDDESCR;
1197 		j = sizeof(buf);
1198 		i = sysctl(qoid, nlen + 2, buf, &j, 0, 0);
1199 		printf("%s", buf);
1200 		return (0);
1201 	}
1202 
1203 	/* keep track of encountered skip nodes, ignoring descendants */
1204 	if ((skip_len == 0 || skip_len >= nlen * (int)sizeof(int)) &&
1205 	    (kind & CTLFLAG_SKIP) != 0) {
1206 		/* Save this oid so we can skip descendants. */
1207 		skip_len = nlen * sizeof(int);
1208 		memcpy(skip_oid, oid, skip_len);
1209 	}
1210 
1211 	/* bail before fetching the value if we're honoring skip */
1212 	if (honor_skip) {
1213 		if (0 < skip_len && skip_len <= nlen * (int)sizeof(int) &&
1214 		    memcmp(skip_oid, oid, skip_len) == 0)
1215 			return (1);
1216 		/* Not a skip node or descendant of a skip node. */
1217 		skip_len = 0;
1218 	}
1219 
1220 	/* don't fetch opaques that we don't know how to print */
1221 	if (ctltype == CTLTYPE_OPAQUE) {
1222 		if (strcmp(fmt, "S,clockinfo") == 0)
1223 			func = S_clockinfo;
1224 		else if (strcmp(fmt, "S,timeval") == 0)
1225 			func = S_timeval;
1226 		else if (strcmp(fmt, "S,loadavg") == 0)
1227 			func = S_loadavg;
1228 		else if (strcmp(fmt, "S,vmtotal") == 0)
1229 			func = S_vmtotal;
1230 		else if (strcmp(fmt, "S,input_id") == 0)
1231 			func = S_input_id;
1232 		else if (strcmp(fmt, "S,pagesizes") == 0)
1233 			func = S_pagesizes;
1234 #ifdef __amd64__
1235 		else if (strcmp(fmt, "S,efi_map_header") == 0)
1236 			func = S_efi_map;
1237 #endif
1238 #if defined(__amd64__) || defined(__i386__)
1239 		else if (strcmp(fmt, "S,bios_smap_xattr") == 0)
1240 			func = S_bios_smap_xattr;
1241 #endif
1242 		else {
1243 			func = NULL;
1244 			if (!bflag && !oflag && !xflag)
1245 				return (1);
1246 		}
1247 	}
1248 
1249 	/* find an estimate of how much we need for this var */
1250 	if (Bflag)
1251 		j = Bflag;
1252 	else {
1253 		j = 0;
1254 		i = sysctl(oid, nlen, 0, &j, 0, 0);
1255 		j += j; /* we want to be sure :-) */
1256 	}
1257 
1258 	val = oval = malloc(j + 1);
1259 	if (val == NULL) {
1260 		warnx("malloc failed");
1261 		return (1);
1262 	}
1263 	len = j;
1264 	i = sysctl(oid, nlen, val, &len, 0, 0);
1265 	if (i != 0 || (len == 0 && ctltype != CTLTYPE_STRING)) {
1266 		free(oval);
1267 		return (1);
1268 	}
1269 
1270 	if (bflag) {
1271 		fwrite(val, 1, len, stdout);
1272 		free(oval);
1273 		return (0);
1274 	}
1275 	val[len] = '\0';
1276 	p = val;
1277 	sign = ctl_sign[ctltype];
1278 	intlen = ctl_size[ctltype];
1279 
1280 	switch (ctltype) {
1281 	case CTLTYPE_STRING:
1282 		if (!nflag)
1283 			printf("%s%s", name, sep);
1284 		printf("%.*s", (int)len, p);
1285 		free(oval);
1286 		return (0);
1287 
1288 	case CTLTYPE_INT:
1289 	case CTLTYPE_UINT:
1290 	case CTLTYPE_LONG:
1291 	case CTLTYPE_ULONG:
1292 	case CTLTYPE_S8:
1293 	case CTLTYPE_S16:
1294 	case CTLTYPE_S32:
1295 	case CTLTYPE_S64:
1296 	case CTLTYPE_U8:
1297 	case CTLTYPE_U16:
1298 	case CTLTYPE_U32:
1299 	case CTLTYPE_U64:
1300 		if (!nflag)
1301 			printf("%s%s", name, sep);
1302 		hexlen = 2 + (intlen * CHAR_BIT + 3) / 4;
1303 		sep1 = "";
1304 		while (len >= intlen) {
1305 			switch (kind & CTLTYPE) {
1306 			case CTLTYPE_INT:
1307 			case CTLTYPE_UINT:
1308 				umv = *(u_int *)p;
1309 				mv = *(int *)p;
1310 				break;
1311 			case CTLTYPE_LONG:
1312 			case CTLTYPE_ULONG:
1313 				umv = *(u_long *)p;
1314 				mv = *(long *)p;
1315 				break;
1316 			case CTLTYPE_S8:
1317 			case CTLTYPE_U8:
1318 				umv = *(uint8_t *)p;
1319 				mv = *(int8_t *)p;
1320 				break;
1321 			case CTLTYPE_S16:
1322 			case CTLTYPE_U16:
1323 				umv = *(uint16_t *)p;
1324 				mv = *(int16_t *)p;
1325 				break;
1326 			case CTLTYPE_S32:
1327 			case CTLTYPE_U32:
1328 				umv = *(uint32_t *)p;
1329 				mv = *(int32_t *)p;
1330 				break;
1331 			case CTLTYPE_S64:
1332 			case CTLTYPE_U64:
1333 				umv = *(uint64_t *)p;
1334 				mv = *(int64_t *)p;
1335 				break;
1336 			}
1337 			fputs(sep1, stdout);
1338 			if (xflag)
1339 				printf("%#0*jx", hexlen, umv);
1340 			else if (!sign)
1341 				printf(hflag ? "%'ju" : "%ju", umv);
1342 			else if (fmt[1] == 'K') {
1343 				if (mv < 0)
1344 					printf("%jd", mv);
1345 				else {
1346 					/*
1347 					 * See strIKtoi for details on fmt.
1348 					 */
1349 					prec = 1;
1350 					if (fmt[2] != '\0')
1351 						prec = fmt[2] - '0';
1352 					base = 1.0;
1353 					for (int i = 0; i < prec; i++)
1354 						base *= 10.0;
1355 					printf("%.*fC", prec,
1356 					    (float)mv / base - 273.15);
1357 				}
1358 			} else
1359 				printf(hflag ? "%'jd" : "%jd", mv);
1360 			sep1 = " ";
1361 			len -= intlen;
1362 			p += intlen;
1363 		}
1364 		free(oval);
1365 		return (0);
1366 
1367 	case CTLTYPE_OPAQUE:
1368 		i = 0;
1369 		if (func) {
1370 			if (!nflag)
1371 				printf("%s%s", name, sep);
1372 			i = (*func)(len, p);
1373 			free(oval);
1374 			return (i);
1375 		}
1376 		/* FALLTHROUGH */
1377 	default:
1378 		if (!oflag && !xflag) {
1379 			free(oval);
1380 			return (1);
1381 		}
1382 		if (!nflag)
1383 			printf("%s%s", name, sep);
1384 		printf("Format:%s Length:%zu Dump:0x", fmt, len);
1385 		while (len-- && (xflag || p < val + 16))
1386 			printf("%02x", *p++);
1387 		if (!xflag && len > 16)
1388 			printf("...");
1389 		free(oval);
1390 		return (0);
1391 	}
1392 	free(oval);
1393 	return (1);
1394 }
1395 
1396 static int
1397 sysctl_all(int *oid, int len)
1398 {
1399 	int name1[22], name2[22];
1400 	int i, j;
1401 	size_t l1, l2;
1402 
1403 	name1[0] = CTL_SYSCTL;
1404 	name1[1] = (oid != NULL || Nflag || dflag || tflag) ?
1405 	    CTL_SYSCTL_NEXTNOSKIP : CTL_SYSCTL_NEXT;
1406 	l1 = 2;
1407 	if (len) {
1408 		memcpy(name1 + 2, oid, len * sizeof(int));
1409 		l1 += len;
1410 	} else {
1411 		name1[2] = CTL_KERN;
1412 		l1++;
1413 	}
1414 	for (;;) {
1415 		l2 = sizeof(name2);
1416 		j = sysctl(name1, l1, name2, &l2, 0, 0);
1417 		if (j < 0) {
1418 			if (errno == ENOENT)
1419 				return (0);
1420 			else
1421 				err(1, "sysctl(getnext) %d %zu", j, l2);
1422 		}
1423 
1424 		l2 /= sizeof(int);
1425 
1426 		if (len < 0 || l2 < (unsigned int)len)
1427 			return (0);
1428 
1429 		if (memcmp(name2, oid, len * sizeof(int)) != 0)
1430 			return (0);
1431 
1432 		i = show_var(name2, l2, true);
1433 		if (!i && !bflag)
1434 			putchar('\n');
1435 
1436 		memcpy(name1 + 2, name2, l2 * sizeof(int));
1437 		l1 = 2 + l2;
1438 	}
1439 }
1440