Home | History | Annotate | Line # | Download | only in sysctl
sysctl.c revision 1.153
      1 /*	$NetBSD: sysctl.c,v 1.153 2014/05/16 12:22:32 martin Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2003 The NetBSD Foundation, Inc.
      5  *	All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Brown.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  */
     60 
     61 #include <sys/cdefs.h>
     62 #ifndef lint
     63 __COPYRIGHT("@(#) Copyright (c) 1993\
     64  The Regents of the University of California.  All rights reserved.");
     65 #endif /* not lint */
     66 
     67 #ifndef lint
     68 #if 0
     69 static char sccsid[] = "@(#)sysctl.c	8.1 (Berkeley) 6/6/93";
     70 #else
     71 __RCSID("$NetBSD: sysctl.c,v 1.153 2014/05/16 12:22:32 martin Exp $");
     72 #endif
     73 #endif /* not lint */
     74 
     75 #include <sys/types.h>
     76 #include <sys/param.h>
     77 #include <sys/sysctl.h>
     78 #include <sys/mount.h>
     79 #include <sys/resource.h>
     80 #include <sys/stat.h>
     81 #include <sys/sched.h>
     82 #include <sys/socket.h>
     83 #include <sys/bitops.h>
     84 #include <netinet/in.h>
     85 #include <netinet/ip_var.h>
     86 #include <netinet/tcp.h>
     87 #include <netinet/tcp_timer.h>
     88 #include <netinet/tcp_var.h>
     89 #include <netinet/icmp6.h>
     90 #include <nfs/rpcv2.h>
     91 #include <nfs/nfsproto.h>
     92 #include <nfs/nfs.h>
     93 #include <machine/cpu.h>
     94 
     95 #include <assert.h>
     96 #include <ctype.h>
     97 #include <err.h>
     98 #include <errno.h>
     99 #include <inttypes.h>
    100 #include <regex.h>
    101 #include <stdarg.h>
    102 #include <stdio.h>
    103 #include <stdlib.h>
    104 #include <string.h>
    105 #include <time.h>
    106 #include <unistd.h>
    107 
    108 #include "prog_ops.h"
    109 
    110 /*
    111  * this needs to be able to do the printing and the setting
    112  */
    113 #define HANDLER_PROTO const char *, const char *, char *, \
    114 	int *, u_int, const struct sysctlnode *, \
    115 	u_int, void *
    116 #define HANDLER_ARGS const char *sname, const char *dname, char *value, \
    117 	int *name, u_int namelen, const struct sysctlnode *pnode, \
    118 	u_int type, void *v
    119 #define DISPLAY_VALUE	0
    120 #define DISPLAY_OLD	1
    121 #define DISPLAY_NEW	2
    122 
    123 /*
    124  * generic routines
    125  */
    126 static const struct handlespec *findhandler(const char *, regex_t *, size_t *);
    127 static void canonicalize(const char *, char *);
    128 static void purge_tree(struct sysctlnode *);
    129 static void print_tree(int *, u_int, struct sysctlnode *, u_int, int, regex_t *,
    130     size_t *);
    131 static void write_number(int *, u_int, struct sysctlnode *, char *);
    132 static void write_string(int *, u_int, struct sysctlnode *, char *);
    133 static void display_number(const struct sysctlnode *, const char *,
    134 			   const void *, size_t, int);
    135 static void display_string(const struct sysctlnode *, const char *,
    136 			   const void *, size_t, int);
    137 static void display_struct(const struct sysctlnode *, const char *,
    138 			   const void *, size_t, int);
    139 static void hex_dump(const unsigned char *, size_t);
    140 __dead static void usage(void);
    141 static void parse(char *, regex_t *, size_t *);
    142 static void parse_create(char *);
    143 static void parse_destroy(char *);
    144 static void parse_describe(char *);
    145 static void getdesc1(int *, u_int, struct sysctlnode *);
    146 static void getdesc(int *, u_int, struct sysctlnode *);
    147 static void trim_whitespace(char *, int);
    148 static void sysctlerror(int);
    149 static void sysctlparseerror(u_int, const char *);
    150 static void sysctlperror(const char *, ...) __printflike(1, 2);
    151 #define EXIT(n) do { \
    152 	if (fn == NULL) exit(n); else return; } while (/*CONSTCOND*/0)
    153 
    154 /*
    155  * "borrowed" from libc:sysctlgetmibinfo.c
    156  */
    157 int __learn_tree(int *, u_int, struct sysctlnode *);
    158 
    159 /*
    160  * "handlers"
    161  */
    162 static void printother(HANDLER_PROTO);
    163 static void kern_clockrate(HANDLER_PROTO);
    164 static void kern_boottime(HANDLER_PROTO);
    165 static void kern_consdev(HANDLER_PROTO);
    166 static void kern_cp_time(HANDLER_PROTO);
    167 static void kern_cp_id(HANDLER_PROTO);
    168 static void kern_drivers(HANDLER_PROTO);
    169 static void vm_loadavg(HANDLER_PROTO);
    170 static void proc_limit(HANDLER_PROTO);
    171 #ifdef CPU_DISKINFO
    172 static void machdep_diskinfo(HANDLER_PROTO);
    173 #endif /* CPU_DISKINFO */
    174 static void mode_bits(HANDLER_PROTO);
    175 static void reserve(HANDLER_PROTO);
    176 
    177 static const struct handlespec {
    178 	const char *ps_re;
    179 	void (*ps_p)(HANDLER_PROTO);
    180 	void (*ps_w)(HANDLER_PROTO);
    181 	const void *ps_d;
    182 } handlers[] = {
    183 	{ "/kern/clockrate",			kern_clockrate, NULL, NULL },
    184 	{ "/kern/vnode",			printother, NULL, "pstat" },
    185 	{ "/kern/proc(2|_args)?",		printother, NULL, "ps" },
    186 	{ "/kern/file2?",			printother, NULL, "pstat" },
    187 	{ "/kern/ntptime",			printother, NULL,
    188 						"ntpdc -c kerninfo" },
    189 	{ "/kern/msgbuf",			printother, NULL, "dmesg" },
    190 	{ "/kern/boottime",			kern_boottime, NULL, NULL },
    191 	{ "/kern/consdev",			kern_consdev, NULL, NULL },
    192 	{ "/kern/cp_time(/[0-9]+)?",		kern_cp_time, NULL, NULL },
    193 	{ "/kern/sysvipc_info",			printother, NULL, "ipcs" },
    194 	{ "/kern/cp_id(/[0-9]+)?",		kern_cp_id, NULL, NULL },
    195 
    196 	{ "/kern/coredump/setid/mode",		mode_bits, mode_bits, NULL },
    197 	{ "/kern/drivers",			kern_drivers, NULL, NULL },
    198 
    199 	{ "/vm/vmmeter",			printother, NULL,
    200 						"vmstat' or 'systat" },
    201 	{ "/vm/loadavg",			vm_loadavg, NULL, NULL },
    202 	{ "/vm/uvmexp2?",			printother, NULL,
    203 						"vmstat' or 'systat" },
    204 
    205 	{ "/vfs/nfs/nfsstats",			printother, NULL, "nfsstat" },
    206 
    207 	{ "/net/inet6?/tcp6?/ident",		printother, NULL, "identd" },
    208 	{ "/net/inet6/icmp6/nd6_[dp]rlist",	printother, NULL, "ndp" },
    209 	{ "/net/key/dumps[ap]",			printother, NULL, "setkey" },
    210 	{ "/net/[^/]+/[^/]+/pcblist",		printother, NULL,
    211 						"netstat' or 'sockstat" },
    212 	{ "/net/(inet|inet6)/[^/]+/stats",	printother, NULL, "netstat"},
    213 	{ "/net/bpf/(stats|peers)",		printother, NULL, "netstat"},
    214 
    215 	{ "/net/inet.*/tcp.*/deb.*",		printother, NULL, "trpt" },
    216 
    217 	{ "/net/inet.*/ip.*/anonportalgo/reserve", reserve, reserve, NULL },
    218 
    219 	{ "/net/ns/spp/deb.*",			printother, NULL, "trsp" },
    220 
    221 	{ "/hw/diskstats",			printother, NULL, "iostat" },
    222 
    223 #ifdef CPU_CONSDEV
    224 	{ "/machdep/consdev",			kern_consdev, NULL, NULL },
    225 #endif /* CPU_CONSDEV */
    226 #ifdef CPU_DISKINFO
    227 	{ "/machdep/diskinfo",			machdep_diskinfo, NULL, NULL },
    228 #endif /* CPU_CONSDEV */
    229 
    230 	{ "/proc/[^/]+/rlimit/[^/]+/[^/]+",	proc_limit, proc_limit, NULL },
    231 
    232 	/*
    233 	 * these will only be called when the given node has no children
    234 	 */
    235 	{ "/net/[^/]+",				printother, NULL, NULL },
    236 	{ "/debug",				printother, NULL, NULL },
    237 	{ "/ddb",				printother, NULL, NULL },
    238 	{ "/vendor",				printother, NULL, NULL },
    239 
    240 	{ NULL,					NULL, NULL, NULL },
    241 };
    242 
    243 struct sysctlnode my_root = {
    244 	.sysctl_flags = SYSCTL_VERSION|CTLFLAG_ROOT|CTLTYPE_NODE,
    245 	.sysctl_size = sizeof(struct sysctlnode),
    246 	.sysctl_num = 0,
    247 	.sysctl_name = "(prog_root)",
    248 };
    249 
    250 int	Aflag, aflag, dflag, Mflag, nflag, qflag, rflag, wflag, xflag;
    251 size_t	nr;
    252 char	*fn;
    253 int	req, stale, errs;
    254 FILE	*warnfp = stderr;
    255 
    256 #define MAXPORTS	0x10000
    257 
    258 /*
    259  * vah-riables n stuff
    260  */
    261 char gsname[SYSCTL_NAMELEN * CTL_MAXNAME + CTL_MAXNAME],
    262 	canonname[SYSCTL_NAMELEN * CTL_MAXNAME + CTL_MAXNAME],
    263 	gdname[10 * CTL_MAXNAME + CTL_MAXNAME];
    264 char sep[] = ".";
    265 const char *eq = " = ";
    266 const char *lname[] = {
    267 	"top", "second", "third", "fourth", "fifth", "sixth",
    268 	"seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"
    269 };
    270 
    271 /*
    272  * you've heard of main, haven't you?
    273  */
    274 int
    275 main(int argc, char *argv[])
    276 {
    277 	int name[CTL_MAXNAME];
    278 	int ch;
    279 	size_t lastcompiled = 0;
    280 	regex_t *re;
    281 
    282 	setprogname(argv[0]);
    283 	while ((ch = getopt(argc, argv, "Aabdef:Mnqrwx")) != -1) {
    284 		switch (ch) {
    285 		case 'A':
    286 			Aflag++;
    287 			break;
    288 		case 'a':
    289 			aflag++;
    290 			break;
    291 		case 'd':
    292 			dflag++;
    293 			break;
    294 		case 'e':
    295 			eq = "=";
    296 			break;
    297 		case 'f':
    298 			fn = optarg;
    299 			wflag++;
    300 			break;
    301 		case 'M':
    302 			Mflag++;
    303 			break;
    304 		case 'n':
    305 			nflag++;
    306 			break;
    307 		case 'q':
    308 			qflag++;
    309 			break;
    310 		case 'b':	/* FreeBSD compat */
    311 		case 'r':
    312 			rflag++;
    313 			break;
    314 		case 'w':
    315 			wflag++;
    316 			break;
    317 		case 'x':
    318 			xflag++;
    319 			break;
    320 		default:
    321 			usage();
    322 		}
    323 	}
    324 
    325 	argc -= optind;
    326 	argv += optind;
    327 
    328 	if (xflag && rflag)
    329 		usage();
    330 	/* if ((xflag || rflag) && wflag)
    331 		usage(); */
    332 	/* if (aflag && (Mflag || qflag))
    333 		usage(); */
    334 	if ((aflag || Aflag) && qflag)
    335 		usage();
    336 	if ((Aflag || Mflag || dflag) && argc == 0 && fn == NULL)
    337 		aflag = 1;
    338 
    339 	if (prog_init && prog_init() == -1)
    340 		err(EXIT_FAILURE, "prog init failed");
    341 
    342 	if (Aflag)
    343 		warnfp = stdout;
    344 	stale = req = 0;
    345 
    346 	if ((re = malloc(sizeof(*re) * __arraycount(handlers))) == NULL)
    347 		err(EXIT_FAILURE, "malloc regex");
    348 
    349 	if (aflag) {
    350 		print_tree(&name[0], 0, NULL, CTLTYPE_NODE, 1,
    351 		    re, &lastcompiled);
    352 		/* if (argc == 0) */
    353 		return (0);
    354 	}
    355 
    356 	if (fn) {
    357 		FILE *fp;
    358 		char *l;
    359 
    360 		fp = fopen(fn, "r");
    361 		if (fp == NULL) {
    362 			err(EXIT_FAILURE, "%s", fn);
    363 		} else {
    364 			nr = 0;
    365 			while ((l = fparseln(fp, NULL, &nr, NULL, 0)) != NULL)
    366 			{
    367 				if (*l) {
    368 					parse(l, re, &lastcompiled);
    369 					free(l);
    370 				}
    371 			}
    372 			fclose(fp);
    373 		}
    374 		return errs ? 1 : 0;
    375 	}
    376 
    377 	if (argc == 0)
    378 		usage();
    379 
    380 	while (argc-- > 0)
    381 		parse(*argv++, re, &lastcompiled);
    382 
    383 	return errs ? EXIT_FAILURE : EXIT_SUCCESS;
    384 }
    385 
    386 /*
    387  * ********************************************************************
    388  * how to find someone special to handle the reading (or maybe even
    389  * writing) of a particular node
    390  * ********************************************************************
    391  */
    392 static const struct handlespec *
    393 findhandler(const char *s, regex_t *re, size_t *lastcompiled)
    394 {
    395 	const struct handlespec *p;
    396 	size_t i, l;
    397 	int j;
    398 	char eb[64];
    399 	regmatch_t match;
    400 
    401 	p = &handlers[0];
    402 	l = strlen(s);
    403 	for (i = 0; p[i].ps_re != NULL; i++) {
    404 		if (i >= *lastcompiled) {
    405 			j = regcomp(&re[i], p[i].ps_re, REG_EXTENDED);
    406 			if (j != 0) {
    407 				regerror(j, &re[i], eb, sizeof(eb));
    408 				errx(EXIT_FAILURE, "regcomp: %s: %s", p[i].ps_re, eb);
    409 			}
    410 			*lastcompiled = i + 1;
    411 		}
    412 		j = regexec(&re[i], s, 1, &match, 0);
    413 		if (j == 0) {
    414 			if (match.rm_so == 0 && match.rm_eo == (int)l)
    415 				return &p[i];
    416 		}
    417 		else if (j != REG_NOMATCH) {
    418 			regerror(j, &re[i], eb, sizeof(eb));
    419 			errx(EXIT_FAILURE, "regexec: %s: %s", p[i].ps_re, eb);
    420 		}
    421 	}
    422 
    423 	return NULL;
    424 }
    425 
    426 /*
    427  * after sysctlgetmibinfo is done with the name, we convert all
    428  * separators to / and stuff one at the front if it was missing
    429  */
    430 static void
    431 canonicalize(const char *i, char *o)
    432 {
    433 	const char *t;
    434 	char p[SYSCTL_NAMELEN + 1];
    435 	int l;
    436 
    437 	if (i[0] != *sep) {
    438 		o[0] = '/';
    439 		o[1] = '\0';
    440 	}
    441 	else
    442 		o[0] = '\0';
    443 
    444 	t = i;
    445 	do {
    446 		i = t;
    447 		t = strchr(i, sep[0]);
    448 		if (t == NULL)
    449 			strcat(o, i);
    450 		else {
    451 			l = t - i;
    452 			t++;
    453 			memcpy(p, i, l);
    454 			p[l] = '\0';
    455 			strcat(o, p);
    456 			strcat(o, "/");
    457 		}
    458 	} while (t != NULL);
    459 }
    460 
    461 /*
    462  * ********************************************************************
    463  * convert this special number to a special string so we can print the
    464  * mib
    465  * ********************************************************************
    466  */
    467 static const char *
    468 sf(u_int f)
    469 {
    470 	static char s[256];
    471 	const char *c;
    472 
    473 	s[0] = '\0';
    474 	c = "";
    475 
    476 #define print_flag(_f, _s, _c, _q, _x) \
    477 	if (((_f) & (__CONCAT(CTLFLAG_,_x))) == (__CONCAT(CTLFLAG_,_q))) { \
    478 		strlcat((_s), (_c), sizeof(_s)); \
    479 		strlcat((_s), __STRING(_q), sizeof(_s)); \
    480 		(_c) = ","; \
    481 		(_f) &= ~(__CONCAT(CTLFLAG_,_x)); \
    482 	}
    483 	print_flag(f, s, c, READONLY,  READWRITE);
    484 	print_flag(f, s, c, READWRITE, READWRITE);
    485 	print_flag(f, s, c, ANYWRITE,  ANYWRITE);
    486 	print_flag(f, s, c, PRIVATE,   PRIVATE);
    487 	print_flag(f, s, c, PERMANENT, PERMANENT);
    488 	print_flag(f, s, c, OWNDATA,   OWNDATA);
    489 	print_flag(f, s, c, IMMEDIATE, IMMEDIATE);
    490 	print_flag(f, s, c, HEX,       HEX);
    491 	print_flag(f, s, c, ROOT,      ROOT);
    492 	print_flag(f, s, c, ANYNUMBER, ANYNUMBER);
    493 	print_flag(f, s, c, HIDDEN,    HIDDEN);
    494 	print_flag(f, s, c, ALIAS,     ALIAS);
    495 #undef print_flag
    496 
    497 	if (f) {
    498 		char foo[9];
    499 		snprintf(foo, sizeof(foo), "%x", f);
    500 		strlcat(s, c, sizeof(s));
    501 		strlcat(s, foo, sizeof(s));
    502 	}
    503 
    504 	return (s);
    505 }
    506 
    507 static const char *
    508 st(u_int t)
    509 {
    510 
    511 	switch (t) {
    512 	case CTLTYPE_NODE:
    513 		return "NODE";
    514 	case CTLTYPE_INT:
    515 		return "INT";
    516 	case CTLTYPE_STRING:
    517 		return "STRING";
    518 	case CTLTYPE_QUAD:
    519                 return "QUAD";
    520 	case CTLTYPE_STRUCT:
    521 		return "STRUCT";
    522 	case CTLTYPE_BOOL:
    523 		return "BOOL";
    524 	}
    525 
    526 	return "???";
    527 }
    528 
    529 /*
    530  * ********************************************************************
    531  * recursively eliminate all data belonging to the given node
    532  * ********************************************************************
    533  */
    534 static void
    535 purge_tree(struct sysctlnode *rnode)
    536 {
    537 	struct sysctlnode *node;
    538 
    539 	if (rnode == NULL ||
    540 	    SYSCTL_TYPE(rnode->sysctl_flags) != CTLTYPE_NODE ||
    541 	    rnode->sysctl_child == NULL)
    542 		return;
    543 
    544 	for (node = rnode->sysctl_child;
    545 	     node < &rnode->sysctl_child[rnode->sysctl_clen];
    546 	     node++)
    547 		purge_tree(node);
    548 	free(rnode->sysctl_child);
    549 	rnode->sysctl_csize = 0;
    550 	rnode->sysctl_clen = 0;
    551 	rnode->sysctl_child = NULL;
    552 
    553 	if (rnode->sysctl_desc == (const char*)-1)
    554 		rnode->sysctl_desc = NULL;
    555 	if (rnode->sysctl_desc != NULL)
    556 		free(__UNCONST(rnode->sysctl_desc));
    557 	rnode->sysctl_desc = NULL;
    558 }
    559 
    560 static void __attribute__((__format__(__printf__, 3, 4)))
    561 appendprintf(char **bp, size_t *lbp, const char *fmt, ...)
    562 {
    563 	int r;
    564 	va_list ap;
    565 
    566 	va_start(ap, fmt);
    567 	r = vsnprintf(*bp, *lbp, fmt, ap);
    568 	va_end(ap);
    569 	if (r < 0 || (size_t)r > *lbp)
    570 		r = *lbp;
    571 	*bp += r;
    572 	*lbp -= r;
    573 }
    574 
    575 /*
    576  * ********************************************************************
    577  * print this node and any others underneath it
    578  * ********************************************************************
    579  */
    580 static void
    581 print_tree(int *name, u_int namelen, struct sysctlnode *pnode, u_int type,
    582    int add, regex_t *re, size_t *lastcompiled)
    583 {
    584 	struct sysctlnode *node;
    585 	int rc;
    586 	size_t ni, sz, ldp, lsp;
    587 	char *sp, *dp, *tsp, *tdp;
    588 	const struct handlespec *p;
    589 
    590 	sp = tsp = &gsname[strlen(gsname)];
    591 	dp = tdp = &gdname[strlen(gdname)];
    592 	ldp = sizeof(gdname) - (dp - gdname);
    593 	lsp = sizeof(gsname) - (sp - gsname);
    594 
    595 	if (sp != &gsname[0] && dp == &gdname[0]) {
    596 		/*
    597 		 * aw...shucks.  now we must play catch up
    598 		 */
    599 		for (ni = 0; ni < namelen; ni++)
    600 			appendprintf(&tdp, &ldp, "%s%d", ni > 0 ? "." : "",
    601 			    name[ni]);
    602 	}
    603 
    604 	if (pnode == NULL)
    605 		pnode = &my_root;
    606 	else if (add) {
    607 		appendprintf(&tsp, &lsp, "%s%s", namelen > 1 ? sep : "",
    608 			pnode->sysctl_name);
    609 		appendprintf(&tdp, &ldp, "%s%d", namelen > 1 ? "." : "",
    610 			pnode->sysctl_num);
    611 	}
    612 
    613 	if (Mflag && pnode != &my_root) {
    614 		if (nflag)
    615 			printf("%s: ", gdname);
    616 		else
    617 			printf("%s (%s): ", gsname, gdname);
    618 		printf("CTLTYPE_%s", st(type));
    619 		if (type == CTLTYPE_NODE) {
    620 			if (SYSCTL_FLAGS(pnode->sysctl_flags) & CTLFLAG_ALIAS)
    621 				printf(", alias %d",
    622 				       pnode->sysctl_alias);
    623 			else
    624 				printf(", children %d/%d",
    625 				       pnode->sysctl_clen,
    626 				       pnode->sysctl_csize);
    627 		}
    628 		printf(", size %zu", pnode->sysctl_size);
    629 		printf(", flags 0x%x<%s>",
    630 		       SYSCTL_FLAGS(pnode->sysctl_flags),
    631 		       sf(SYSCTL_FLAGS(pnode->sysctl_flags)));
    632 		if (pnode->sysctl_func)
    633 			printf(", func=%p", pnode->sysctl_func);
    634 		printf(", ver=%d", pnode->sysctl_ver);
    635 		printf("\n");
    636 		if (type != CTLTYPE_NODE) {
    637 			*sp = *dp = '\0';
    638 			return;
    639 		}
    640 	}
    641 
    642 	if (dflag && pnode != &my_root) {
    643 		if (Aflag || type != CTLTYPE_NODE) {
    644 			if (pnode->sysctl_desc == NULL)
    645 				getdesc1(name, namelen, pnode);
    646 			if (Aflag || !add ||
    647 			    (pnode->sysctl_desc != NULL &&
    648 			     pnode->sysctl_desc != (const char*)-1)) {
    649 				if (!nflag)
    650 					printf("%s: ", gsname);
    651 				if (pnode->sysctl_desc == NULL ||
    652 				    pnode->sysctl_desc == (const char*)-1)
    653 					printf("(no description)\n");
    654 				else
    655 					printf("%s\n", pnode->sysctl_desc);
    656 			}
    657 		}
    658 
    659 		if (type != CTLTYPE_NODE) {
    660 			*sp = *dp = '\0';
    661 			return;
    662 		}
    663 	}
    664 
    665 	/*
    666 	 * if this is an alias and we added our name, that means we
    667 	 * got here by recursing down into the tree, so skip it.  The
    668 	 * only way to print an aliased node is with either -M or by
    669 	 * name specifically.
    670 	 */
    671 	if (SYSCTL_FLAGS(pnode->sysctl_flags) & CTLFLAG_ALIAS && add) {
    672 		*sp = *dp = '\0';
    673 		return;
    674 	}
    675 
    676 	canonicalize(gsname, canonname);
    677 	p = findhandler(canonname, re, lastcompiled);
    678 	if (type != CTLTYPE_NODE && p != NULL) {
    679 		if (p->ps_p == NULL) {
    680 			sysctlperror("Cannot print `%s': %s\n", gsname,
    681 			    strerror(EOPNOTSUPP));
    682 			exit(EXIT_FAILURE);
    683 		}
    684 		(*p->ps_p)(gsname, gdname, NULL, name, namelen, pnode, type,
    685 			   __UNCONST(p->ps_d));
    686 		*sp = *dp = '\0';
    687 		return;
    688 	}
    689 
    690 	if (type != CTLTYPE_NODE && pnode->sysctl_size == 0) {
    691 		rc = prog_sysctl(&name[0], namelen, NULL, &sz, NULL, 0);
    692 		if (rc == -1) {
    693 			sysctlerror(1);
    694 			*sp = *dp = '\0';
    695 			return;
    696 		}
    697 		if (sz == 0) {
    698 			if ((Aflag || req) && !Mflag)
    699 				printf("%s: node contains no data\n", gsname);
    700 			*sp = *dp = '\0';
    701 			return;
    702 		}
    703 	}
    704 	else
    705 		sz = pnode->sysctl_size;
    706 
    707 	switch (type) {
    708 	case CTLTYPE_NODE: {
    709 		__learn_tree(name, namelen, pnode);
    710 		node = pnode->sysctl_child;
    711 		if (node == NULL) {
    712 			if (dflag)
    713 				/* do nothing */;
    714 			else if (p != NULL)
    715 				(*p->ps_p)(gsname, gdname, NULL, name, namelen,
    716 					   pnode, type, __UNCONST(p->ps_d));
    717 			else if ((Aflag || req) && !Mflag)
    718 				printf("%s: no children\n", gsname);
    719 		}
    720 		else {
    721 			if (dflag)
    722 				/*
    723 				 * get all descriptions for next level
    724 				 * in one chunk
    725 				 */
    726 				getdesc(name, namelen, pnode);
    727 			req = 0;
    728 			for (ni = 0; ni < pnode->sysctl_clen; ni++) {
    729 				name[namelen] = node[ni].sysctl_num;
    730 				if ((node[ni].sysctl_flags & CTLFLAG_HIDDEN) &&
    731 				    !(Aflag || req))
    732 					continue;
    733 				print_tree(name, namelen + 1, &node[ni],
    734 					   SYSCTL_TYPE(node[ni].sysctl_flags),
    735 					   1, re, lastcompiled);
    736 			}
    737 		}
    738 		break;
    739 	}
    740 	case CTLTYPE_INT: {
    741 		int i;
    742 		rc = prog_sysctl(name, namelen, &i, &sz, NULL, 0);
    743 		if (rc == -1) {
    744 			sysctlerror(1);
    745 			break;
    746 		}
    747 		display_number(pnode, gsname, &i, sizeof(i), DISPLAY_VALUE);
    748 		break;
    749 	}
    750 	case CTLTYPE_BOOL: {
    751 		bool b;
    752 		rc = prog_sysctl(name, namelen, &b, &sz, NULL, 0);
    753 		if (rc == -1) {
    754 			sysctlerror(1);
    755 			break;
    756 		}
    757 		display_number(pnode, gsname, &b, sizeof(b), DISPLAY_VALUE);
    758 		break;
    759 	}
    760 	case CTLTYPE_STRING: {
    761 		unsigned char buf[1024], *tbuf;
    762 		tbuf = buf;
    763 		sz = sizeof(buf);
    764 		rc = prog_sysctl(&name[0], namelen, tbuf, &sz, NULL, 0);
    765 		if (rc == -1 && errno == ENOMEM) {
    766 			tbuf = malloc(sz);
    767 			if (tbuf == NULL) {
    768 				sysctlerror(1);
    769 				break;
    770 			}
    771 			rc = prog_sysctl(&name[0], namelen, tbuf, &sz, NULL, 0);
    772 		}
    773 		if (rc == -1)
    774 			sysctlerror(1);
    775 		else
    776 			display_string(pnode, gsname, tbuf, sz, DISPLAY_VALUE);
    777 		if (tbuf != buf)
    778 			free(tbuf);
    779 		break;
    780 	}
    781 	case CTLTYPE_QUAD: {
    782 		u_quad_t q;
    783 		sz = sizeof(q);
    784 		rc = prog_sysctl(&name[0], namelen, &q, &sz, NULL, 0);
    785 		if (rc == -1) {
    786 			sysctlerror(1);
    787 			break;
    788 		}
    789 		display_number(pnode, gsname, &q, sizeof(q), DISPLAY_VALUE);
    790 		break;
    791 	}
    792 	case CTLTYPE_STRUCT: {
    793 		/*
    794 		 * we shouldn't actually get here, but if we
    795 		 * do, would it be nice to have *something* to
    796 		 * do other than completely ignore the
    797 		 * request.
    798 		 */
    799 		unsigned char *d;
    800 		if ((d = malloc(sz)) == NULL) {
    801 			fprintf(warnfp, "%s: !malloc failed!\n", gsname);
    802 			break;
    803 		}
    804 		rc = prog_sysctl(&name[0], namelen, d, &sz, NULL, 0);
    805 		if (rc == -1) {
    806 			sysctlerror(1);
    807 			break;
    808 		}
    809 		display_struct(pnode, gsname, d, sz, DISPLAY_VALUE);
    810 		free(d);
    811 		break;
    812 	}
    813 	default:
    814 		/* should i print an error here? */
    815 		break;
    816 	}
    817 
    818 	*sp = *dp = '\0';
    819 }
    820 
    821 /*
    822  * ********************************************************************
    823  * parse a request, possibly determining that it's a create or destroy
    824  * request
    825  * ********************************************************************
    826  */
    827 static void
    828 parse(char *l, regex_t *re, size_t *lastcompiled)
    829 {
    830 	struct sysctlnode *node;
    831 	const struct handlespec *w;
    832 	int name[CTL_MAXNAME], dodesc = 0;
    833 	u_int namelen, type;
    834 	char *key, *value, *dot;
    835 	size_t sz;
    836 	bool optional = false;
    837 
    838 	req = 1;
    839 	key = l;
    840 
    841 	if ((value = strchr(l, '=')) != NULL) {
    842 		if (value > l && value[-1] == '?') {
    843 			value[-1] = '\0';
    844 			optional = true;
    845 		}
    846 		*value++ = '\0';
    847 	}
    848 
    849 	if ((dot = strpbrk(key, "./")) == NULL)
    850 		sep[0] = '.';
    851 	else
    852 		sep[0] = dot[0];
    853 	sep[1] = '\0';
    854 
    855 	while (key[0] == sep[0] && key[1] == sep[0]) {
    856 		if (value != NULL)
    857 			value[-1] = '=';
    858 		if (strncmp(key + 2, "create", 6) == 0 &&
    859 		    (key[8] == '=' || key[8] == sep[0]))
    860 			parse_create(key + 8 + (key[8] == '=' ? 1 : 0));
    861 		else if (strncmp(key + 2, "destroy", 7) == 0 &&
    862 			 (key[9] == '=' || key[9] == sep[0]))
    863 			parse_destroy(key + 9 + (key[9] == '=' ? 1 : 0));
    864 		else if (strncmp(key + 2, "describe", 8) == 0 &&
    865 			 (key[10] == '=' || key[10] == sep[0])) {
    866 			key += 10 + (key[10] == '=');
    867 			if ((value = strchr(key, '=')) != NULL)
    868 				parse_describe(key);
    869 			else {
    870 				if (!dflag)
    871 					dodesc = 1;
    872 				break;
    873 			}
    874 		}
    875 		else
    876 			sysctlperror("unable to parse '%s'\n", key);
    877 		return;
    878 	}
    879 
    880 	if (stale) {
    881 		purge_tree(&my_root);
    882 		stale = 0;
    883 	}
    884 	node = &my_root;
    885 	namelen = CTL_MAXNAME;
    886 	sz = sizeof(gsname);
    887 
    888 	if (sysctlgetmibinfo(key, &name[0], &namelen, gsname, &sz, &node,
    889 			     SYSCTL_VERSION) == -1) {
    890 		if (optional)
    891 			return;
    892 		sysctlparseerror(namelen, l);
    893 		EXIT(EXIT_FAILURE);
    894 	}
    895 
    896 	type = SYSCTL_TYPE(node->sysctl_flags);
    897 
    898 	if (value == NULL) {
    899 		if (dodesc)
    900 			dflag = 1;
    901 		print_tree(&name[0], namelen, node, type, 0, re, lastcompiled);
    902 		if (dodesc)
    903 			dflag = 0;
    904 		gsname[0] = '\0';
    905 		return;
    906 	}
    907 
    908 	if (fn)
    909 		trim_whitespace(value, 1);
    910 
    911 	if (!wflag) {
    912 		sysctlperror("Must specify -w to set variables\n");
    913 		exit(EXIT_FAILURE);
    914 	}
    915 
    916 	canonicalize(gsname, canonname);
    917 	if (type != CTLTYPE_NODE && (w = findhandler(canonname, re,
    918 	    lastcompiled)) != NULL) {
    919 		if (w->ps_w == NULL) {
    920 			sysctlperror("Cannot write `%s': %s\n", gsname,
    921 			    strerror(EOPNOTSUPP));
    922 			exit(EXIT_FAILURE);
    923 		}
    924 		(*w->ps_w)(gsname, gdname, value, name, namelen, node, type,
    925 			   __UNCONST(w->ps_d));
    926 		gsname[0] = '\0';
    927 		return;
    928 	}
    929 
    930 	switch (type) {
    931 	case CTLTYPE_NODE:
    932 		/*
    933 		 * XXX old behavior is to print.  should we error instead?
    934 		 */
    935 		print_tree(&name[0], namelen, node, CTLTYPE_NODE, 1, re,
    936 		    lastcompiled);
    937 		break;
    938 	case CTLTYPE_INT:
    939 	case CTLTYPE_BOOL:
    940 	case CTLTYPE_QUAD:
    941 		write_number(&name[0], namelen, node, value);
    942 		break;
    943 	case CTLTYPE_STRING:
    944 		write_string(&name[0], namelen, node, value);
    945 		break;
    946 	case CTLTYPE_STRUCT:
    947 		/*
    948 		 * XXX old behavior is to print.  should we error instead?
    949 		 */
    950 		/* fprintf(warnfp, "you can't write to %s\n", gsname); */
    951 		print_tree(&name[0], namelen, node, type, 0, re, lastcompiled);
    952 		break;
    953 	}
    954 }
    955 
    956 /*
    957 
    958   //create=foo.bar.whatever...,
    959   [type=(int|quad|string|struct|node),]
    960   [size=###,]
    961   [n=###,]
    962   [flags=(iohxparw12),]
    963   [addr=0x####,|symbol=...|value=...]
    964 
    965   size is optional for some types.  type must be set before anything
    966   else.  nodes can have [rwhp], but nothing else applies.  if no
    967   size or type is given, node is asserted.  writeable is the default,
    968   with [rw] being read-only and unconditionally writeable
    969   respectively.  if you specify addr, it is assumed to be the name of
    970   a kernel symbol, if value, CTLFLAG_OWNDATA will be asserted for
    971   strings, CTLFLAG_IMMEDIATE for ints and u_quad_ts.  you cannot
    972   specify both value and addr.
    973 
    974 */
    975 
    976 static void
    977 parse_create(char *l)
    978 {
    979 	struct sysctlnode node;
    980 	size_t sz;
    981 	char *nname, *key, *value, *data, *addr, *c, *t;
    982 	int name[CTL_MAXNAME], i, rc, method, flags, rw;
    983 	u_int namelen, type;
    984 	u_quad_t uq;
    985 	quad_t q;
    986 	bool b;
    987 
    988 	if (!wflag) {
    989 		sysctlperror("Must specify -w to create nodes\n");
    990 		exit(EXIT_FAILURE);
    991 	}
    992 
    993 	/*
    994 	 * these are the pieces that make up the description of a new
    995 	 * node
    996 	 */
    997 	memset(&node, 0, sizeof(node));
    998 	node.sysctl_num = CTL_CREATE;  /* any number is fine */
    999 	flags = 0;
   1000 	rw = -1;
   1001 	type = 0;
   1002 	sz = 0;
   1003 	data = addr = NULL;
   1004 	memset(name, 0, sizeof(name));
   1005 	namelen = 0;
   1006 	method = 0;
   1007 
   1008 	/*
   1009 	 * misc stuff used when constructing
   1010 	 */
   1011 	i = 0;
   1012 	b = false;
   1013 	uq = 0;
   1014 	key = NULL;
   1015 	value = NULL;
   1016 
   1017 	/*
   1018 	 * the name of the thing we're trying to create is first, so
   1019 	 * pick it off.
   1020 	 */
   1021 	nname = l;
   1022 	if ((c = strchr(nname, ',')) != NULL)
   1023 		*c++ = '\0';
   1024 
   1025 	while (c != NULL) {
   1026 
   1027 		/*
   1028 		 * pull off the next "key=value" pair
   1029 		 */
   1030 		key = c;
   1031 		if ((t = strchr(key, '=')) != NULL) {
   1032 			*t++ = '\0';
   1033 			value = t;
   1034 		}
   1035 		else
   1036 			value = NULL;
   1037 
   1038 		/*
   1039 		 * if the "key" is "value", then that eats the rest of
   1040 		 * the string, so we're done, otherwise bite it off at
   1041 		 * the next comma.
   1042 		 */
   1043 		if (strcmp(key, "value") == 0) {
   1044 			c = NULL;
   1045 			data = value;
   1046 			break;
   1047 		}
   1048 		else if (value) {
   1049 			if ((c = strchr(value, ',')) != NULL)
   1050 				*c++ = '\0';
   1051 		}
   1052 
   1053 		/*
   1054 		 * note that we (mostly) let the invoker of prog_sysctl(8)
   1055 		 * play rampant here and depend on the kernel to tell
   1056 		 * them that they were wrong.  well...within reason.
   1057 		 * we later check the various parameters against each
   1058 		 * other to make sure it makes some sort of sense.
   1059 		 */
   1060 		if (strcmp(key, "addr") == 0) {
   1061 			/*
   1062 			 * we can't check these two.  only the kernel
   1063 			 * can tell us when it fails to find the name
   1064 			 * (or if the address is invalid).
   1065 			 */
   1066 			if (method != 0) {
   1067 				sysctlperror(
   1068 				    "%s: already have %s for new node\n",
   1069 				    nname,
   1070 				    method == CTL_CREATE ? "addr" : "symbol");
   1071 				EXIT(EXIT_FAILURE);
   1072 			}
   1073 			if (value == NULL) {
   1074 				sysctlperror("%s: missing value\n", nname);
   1075 				EXIT(EXIT_FAILURE);
   1076 			}
   1077 			errno = 0;
   1078 			addr = (void*)strtoul(value, &t, 0);
   1079 			if (t == value || *t != '\0' || errno != 0) {
   1080 				sysctlperror(
   1081 				    "%s: '%s' is not a valid address\n",
   1082 				    nname, value);
   1083 				EXIT(EXIT_FAILURE);
   1084 			}
   1085 			method = CTL_CREATE;
   1086 		}
   1087 		else if (strcmp(key, "symbol") == 0) {
   1088 			if (method != 0) {
   1089 				sysctlperror(
   1090 				    "%s: already have %s for new node\n",
   1091 				    nname,
   1092 				    method == CTL_CREATE ? "addr" : "symbol");
   1093 				EXIT(EXIT_FAILURE);
   1094 			}
   1095 			addr = value;
   1096 			method = CTL_CREATESYM;
   1097 		}
   1098 		else if (strcmp(key, "type") == 0) {
   1099 			if (value == NULL) {
   1100 				sysctlperror("%s: missing value\n", nname);
   1101 				EXIT(EXIT_FAILURE);
   1102 			}
   1103 			if (strcmp(value, "node") == 0)
   1104 				type = CTLTYPE_NODE;
   1105 			else if (strcmp(value, "int") == 0) {
   1106 				sz = sizeof(int);
   1107 				type = CTLTYPE_INT;
   1108 			}
   1109 			else if (strcmp(value, "bool") == 0) {
   1110 				sz = sizeof(bool);
   1111 				type = CTLTYPE_BOOL;
   1112 			}
   1113 			else if (strcmp(value, "string") == 0)
   1114 				type = CTLTYPE_STRING;
   1115 			else if (strcmp(value, "quad") == 0) {
   1116 				sz = sizeof(u_quad_t);
   1117 				type = CTLTYPE_QUAD;
   1118 			}
   1119 			else if (strcmp(value, "struct") == 0)
   1120 				type = CTLTYPE_STRUCT;
   1121 			else {
   1122 				sysctlperror(
   1123 					"%s: '%s' is not a valid type\n",
   1124 					nname, value);
   1125 				EXIT(EXIT_FAILURE);
   1126 			}
   1127 		}
   1128 		else if (strcmp(key, "size") == 0) {
   1129 			if (value == NULL) {
   1130 				sysctlperror("%s: missing value\n", nname);
   1131 				EXIT(EXIT_FAILURE);
   1132 			}
   1133 			errno = 0;
   1134 			/*
   1135 			 * yes, i know size_t is not an unsigned long,
   1136 			 * but we can all agree that it ought to be,
   1137 			 * right?
   1138 			 */
   1139 			sz = strtoul(value, &t, 0);
   1140 			if (t == value || *t != '\0' || errno != 0) {
   1141 				sysctlperror(
   1142 					"%s: '%s' is not a valid size\n",
   1143 					nname, value);
   1144 				EXIT(EXIT_FAILURE);
   1145 			}
   1146 		}
   1147 		else if (strcmp(key, "n") == 0) {
   1148 			if (value == NULL) {
   1149 				sysctlperror("%s: missing value\n", nname);
   1150 				EXIT(EXIT_FAILURE);
   1151 			}
   1152 			errno = 0;
   1153 			q = strtoll(value, &t, 0);
   1154 			if (t == value || *t != '\0' || errno != 0 ||
   1155 			    q < INT_MIN || q > UINT_MAX) {
   1156 				sysctlperror(
   1157 				    "%s: '%s' is not a valid mib number\n",
   1158 				    nname, value);
   1159 				EXIT(EXIT_FAILURE);
   1160 			}
   1161 			node.sysctl_num = (int)q;
   1162 		}
   1163 		else if (strcmp(key, "flags") == 0) {
   1164 			if (value == NULL) {
   1165 				sysctlperror("%s: missing value\n", nname);
   1166 				EXIT(EXIT_FAILURE);
   1167 			}
   1168 			t = value;
   1169 			while (*t != '\0') {
   1170 				switch (*t) {
   1171 				case 'a':
   1172 					flags |= CTLFLAG_ANYWRITE;
   1173 					break;
   1174 				case 'h':
   1175 					flags |= CTLFLAG_HIDDEN;
   1176 					break;
   1177 				case 'i':
   1178 					flags |= CTLFLAG_IMMEDIATE;
   1179 					break;
   1180 				case 'o':
   1181 					flags |= CTLFLAG_OWNDATA;
   1182 					break;
   1183 				case 'p':
   1184 					flags |= CTLFLAG_PRIVATE;
   1185 					break;
   1186 				case 'u':
   1187 					flags |= CTLFLAG_UNSIGNED;
   1188 					break;
   1189 				case 'x':
   1190 					flags |= CTLFLAG_HEX;
   1191 					break;
   1192 
   1193 				case 'r':
   1194 					rw = CTLFLAG_READONLY;
   1195 					break;
   1196 				case 'w':
   1197 					rw = CTLFLAG_READWRITE;
   1198 					break;
   1199 				default:
   1200 					sysctlperror(
   1201 					   "%s: '%c' is not a valid flag\n",
   1202 					    nname, *t);
   1203 					EXIT(EXIT_FAILURE);
   1204 				}
   1205 				t++;
   1206 			}
   1207 		}
   1208 		else {
   1209 			sysctlperror("%s: unrecognized keyword '%s'\n",
   1210 				     nname, key);
   1211 			EXIT(EXIT_FAILURE);
   1212 		}
   1213 	}
   1214 
   1215 	/*
   1216 	 * now that we've finished parsing the given string, fill in
   1217 	 * anything they didn't specify
   1218 	 */
   1219 	if (type == 0)
   1220 		type = CTLTYPE_NODE;
   1221 
   1222 	/*
   1223 	 * the "data" can be interpreted various ways depending on the
   1224 	 * type of node we're creating, as can the size
   1225 	 */
   1226 	if (data != NULL) {
   1227 		if (addr != NULL) {
   1228 			sysctlperror(
   1229 				"%s: cannot specify both value and "
   1230 				"address\n", nname);
   1231 			EXIT(EXIT_FAILURE);
   1232 		}
   1233 
   1234 		switch (type) {
   1235 		case CTLTYPE_INT:
   1236 			errno = 0;
   1237 			q = strtoll(data, &t, 0);
   1238 			if (t == data || *t != '\0' || errno != 0 ||
   1239 				q < INT_MIN || q > UINT_MAX) {
   1240 				sysctlperror(
   1241 				    "%s: '%s' is not a valid integer\n",
   1242 				    nname, value);
   1243 				EXIT(EXIT_FAILURE);
   1244 			}
   1245 			i = (int)q;
   1246 			if (!(flags & CTLFLAG_OWNDATA)) {
   1247 				flags |= CTLFLAG_IMMEDIATE;
   1248 				node.sysctl_idata = i;
   1249 			}
   1250 			else
   1251 				node.sysctl_data = &i;
   1252 			if (sz == 0)
   1253 				sz = sizeof(int);
   1254 			break;
   1255 		case CTLTYPE_BOOL:
   1256 			errno = 0;
   1257 			q = strtoll(data, &t, 0);
   1258 			if (t == data || *t != '\0' || errno != 0 ||
   1259 				(q != 0 && q != 1)) {
   1260 				sysctlperror(
   1261 				    "%s: '%s' is not a valid bool\n",
   1262 				    nname, value);
   1263 				EXIT(EXIT_FAILURE);
   1264 			}
   1265 			b = q == 1;
   1266 			if (!(flags & CTLFLAG_OWNDATA)) {
   1267 				flags |= CTLFLAG_IMMEDIATE;
   1268 				node.sysctl_idata = b;
   1269 			}
   1270 			else
   1271 				node.sysctl_data = &b;
   1272 			if (sz == 0)
   1273 				sz = sizeof(bool);
   1274 			break;
   1275 		case CTLTYPE_STRING:
   1276 			flags |= CTLFLAG_OWNDATA;
   1277 			node.sysctl_data = data;
   1278 			if (sz == 0)
   1279 				sz = strlen(data) + 1;
   1280 			else if (sz < strlen(data) + 1) {
   1281 				sysctlperror("%s: ignoring size=%zu for "
   1282 					"string node, too small for given "
   1283 					"value\n", nname, sz);
   1284 				sz = strlen(data) + 1;
   1285 			}
   1286 			break;
   1287 		case CTLTYPE_QUAD:
   1288 			errno = 0;
   1289 			uq = strtouq(data, &t, 0);
   1290 			if (t == data || *t != '\0' || errno != 0) {
   1291 				sysctlperror(
   1292 					"%s: '%s' is not a valid quad\n",
   1293 					nname, value);
   1294 				EXIT(EXIT_FAILURE);
   1295 			}
   1296 			if (!(flags & CTLFLAG_OWNDATA)) {
   1297 				flags |= CTLFLAG_IMMEDIATE;
   1298 				node.sysctl_qdata = uq;
   1299 			}
   1300 			else
   1301 				node.sysctl_data = &uq;
   1302 			if (sz == 0)
   1303 				sz = sizeof(u_quad_t);
   1304 			break;
   1305 		case CTLTYPE_STRUCT:
   1306 			sysctlperror("%s: struct not initializable\n",
   1307 				     nname);
   1308 			EXIT(EXIT_FAILURE);
   1309 		}
   1310 
   1311 		/*
   1312 		 * these methods have all provided local starting
   1313 		 * values that the kernel must copy in
   1314 		 */
   1315 	}
   1316 
   1317 	/*
   1318 	 * hmm...no data, but we have an address of data.  that's
   1319 	 * fine.
   1320 	 */
   1321 	else if (addr != 0)
   1322 		node.sysctl_data = (void*)addr;
   1323 
   1324 	/*
   1325 	 * no data and no address?  well...okay.  we might be able to
   1326 	 * manage that.
   1327 	 */
   1328 	else if (type != CTLTYPE_NODE) {
   1329 		if (sz == 0) {
   1330 			sysctlperror(
   1331 			    "%s: need a size or a starting value\n",
   1332 			    nname);
   1333                         EXIT(EXIT_FAILURE);
   1334                 }
   1335 		if (!(flags & CTLFLAG_IMMEDIATE))
   1336 			flags |= CTLFLAG_OWNDATA;
   1337 	}
   1338 
   1339 	/*
   1340 	 * now we do a few sanity checks on the description we've
   1341 	 * assembled
   1342 	 */
   1343 	if ((flags & CTLFLAG_IMMEDIATE) &&
   1344 	    (type == CTLTYPE_STRING || type == CTLTYPE_STRUCT)) {
   1345 		sysctlperror("%s: cannot make an immediate %s\n",
   1346 			     nname,
   1347 			     (type == CTLTYPE_STRING) ? "string" : "struct");
   1348 		EXIT(EXIT_FAILURE);
   1349 	}
   1350 	if (type == CTLTYPE_NODE && node.sysctl_data != NULL) {
   1351 		sysctlperror("%s: nodes do not have data\n", nname);
   1352 		EXIT(EXIT_FAILURE);
   1353 	}
   1354 
   1355 	/*
   1356 	 * some types must have a particular size
   1357 	 */
   1358 	if (sz != 0) {
   1359 		if ((type == CTLTYPE_INT && sz != sizeof(int)) ||
   1360 		    (type == CTLTYPE_BOOL && sz != sizeof(bool)) ||
   1361 		    (type == CTLTYPE_QUAD && sz != sizeof(u_quad_t)) ||
   1362 		    (type == CTLTYPE_NODE && sz != 0)) {
   1363 			sysctlperror("%s: wrong size for type\n", nname);
   1364 			EXIT(EXIT_FAILURE);
   1365 		}
   1366 	}
   1367 	else if (type == CTLTYPE_STRUCT) {
   1368 		sysctlperror("%s: struct must have size\n", nname);
   1369 		EXIT(EXIT_FAILURE);
   1370 	}
   1371 
   1372 	/*
   1373 	 * now...if no one said anything yet, we default nodes or
   1374 	 * any type that owns data being writeable, and everything
   1375 	 * else being readonly.
   1376 	 */
   1377 	if (rw == -1) {
   1378 		if (type == CTLTYPE_NODE ||
   1379 		    (flags & (CTLFLAG_OWNDATA|CTLFLAG_IMMEDIATE)))
   1380 			rw = CTLFLAG_READWRITE;
   1381 		else
   1382 			rw = CTLFLAG_READONLY;
   1383 	}
   1384 
   1385 	/*
   1386 	 * if a kernel address was specified, that can't be made
   1387 	 * writeable by us.
   1388 	if (rw != CTLFLAG_READONLY && addr) {
   1389 		sysctlperror("%s: kernel data can only be readable\n", nname);
   1390 		EXIT(EXIT_FAILURE);
   1391 	}
   1392 	 */
   1393 
   1394 	/*
   1395 	 * what separator were they using in the full name of the new
   1396 	 * node?
   1397 	 */
   1398 	if ((t = strpbrk(nname, "./")) == NULL)
   1399 		sep[0] = '.';
   1400 	else
   1401 		sep[0] = t[0];
   1402 	sep[1] = '\0';
   1403 
   1404 	/*
   1405 	 * put it all together, now.  t'ain't much, is it?
   1406 	 */
   1407 	node.sysctl_flags = SYSCTL_VERSION|flags|rw|type;
   1408 	node.sysctl_size = sz;
   1409 	t = strrchr(nname, sep[0]);
   1410 	if (t != NULL)
   1411 		strlcpy(node.sysctl_name, t + 1, sizeof(node.sysctl_name));
   1412 	else
   1413 		strlcpy(node.sysctl_name, nname, sizeof(node.sysctl_name));
   1414 	if (t == nname)
   1415 		t = NULL;
   1416 
   1417 	/*
   1418 	 * if this is a new top-level node, then we don't need to find
   1419 	 * the mib for its parent
   1420 	 */
   1421 	if (t == NULL) {
   1422 		namelen = 0;
   1423 		gsname[0] = '\0';
   1424 	}
   1425 
   1426 	/*
   1427 	 * on the other hand, if it's not a top-level node...
   1428 	 */
   1429 	else {
   1430 		namelen = sizeof(name) / sizeof(name[0]);
   1431 		sz = sizeof(gsname);
   1432 		*t = '\0';
   1433 		rc = sysctlgetmibinfo(nname, &name[0], &namelen,
   1434 				      gsname, &sz, NULL, SYSCTL_VERSION);
   1435 		*t = sep[0];
   1436 		if (rc == -1) {
   1437 			sysctlparseerror(namelen, nname);
   1438 			EXIT(EXIT_FAILURE);
   1439 		}
   1440 	}
   1441 
   1442 	/*
   1443 	 * yes, a new node is being created
   1444 	 */
   1445 	if (method != 0)
   1446 		name[namelen++] = method;
   1447 	else
   1448 		name[namelen++] = CTL_CREATE;
   1449 
   1450 	sz = sizeof(node);
   1451 	rc = prog_sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node));
   1452 
   1453 	if (rc == -1) {
   1454 		sysctlperror("%s: CTL_CREATE failed: %s\n",
   1455 			     nname, strerror(errno));
   1456 		EXIT(EXIT_FAILURE);
   1457 	}
   1458 	else {
   1459 		if (!qflag && !nflag)
   1460 			printf("%s(%s): (created)\n", nname, st(type));
   1461 		stale = 1;
   1462 	}
   1463 }
   1464 
   1465 static void
   1466 parse_destroy(char *l)
   1467 {
   1468 	struct sysctlnode node;
   1469 	size_t sz;
   1470 	int name[CTL_MAXNAME], rc;
   1471 	u_int namelen;
   1472 
   1473 	if (!wflag) {
   1474 		sysctlperror("Must specify -w to destroy nodes\n");
   1475 		exit(EXIT_FAILURE);
   1476 	}
   1477 
   1478 	memset(name, 0, sizeof(name));
   1479 	namelen = sizeof(name) / sizeof(name[0]);
   1480 	sz = sizeof(gsname);
   1481 	rc = sysctlgetmibinfo(l, &name[0], &namelen, gsname, &sz, NULL,
   1482 			      SYSCTL_VERSION);
   1483 	if (rc == -1) {
   1484 		sysctlparseerror(namelen, l);
   1485 		EXIT(EXIT_FAILURE);
   1486 	}
   1487 
   1488 	memset(&node, 0, sizeof(node));
   1489 	node.sysctl_flags = SYSCTL_VERSION;
   1490 	node.sysctl_num = name[namelen - 1];
   1491 	name[namelen - 1] = CTL_DESTROY;
   1492 
   1493 	sz = sizeof(node);
   1494 	rc = prog_sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node));
   1495 
   1496 	if (rc == -1) {
   1497 		sysctlperror("%s: CTL_DESTROY failed: %s\n",
   1498 			     l, strerror(errno));
   1499 		EXIT(EXIT_FAILURE);
   1500 	}
   1501 	else {
   1502 		if (!qflag && !nflag)
   1503 			printf("%s(%s): (destroyed)\n", gsname,
   1504 			       st(SYSCTL_TYPE(node.sysctl_flags)));
   1505 		stale = 1;
   1506 	}
   1507 }
   1508 
   1509 static void
   1510 parse_describe(char *l)
   1511 {
   1512 	struct sysctlnode newdesc;
   1513 	char buf[1024], *value;
   1514 	struct sysctldesc *d = (void*)&buf[0];
   1515 	int name[CTL_MAXNAME], rc;
   1516 	u_int namelen;
   1517 	size_t sz;
   1518 
   1519 	if (!wflag) {
   1520 		sysctlperror("Must specify -w to set descriptions\n");
   1521 		exit(EXIT_FAILURE);
   1522 	}
   1523 
   1524 	value = strchr(l, '=');
   1525 	*value++ = '\0';
   1526 
   1527 	memset(name, 0, sizeof(name));
   1528 	namelen = sizeof(name) / sizeof(name[0]);
   1529 	sz = sizeof(gsname);
   1530 	rc = sysctlgetmibinfo(l, &name[0], &namelen, gsname, &sz, NULL,
   1531 			      SYSCTL_VERSION);
   1532 	if (rc == -1) {
   1533 		sysctlparseerror(namelen, l);
   1534 		EXIT(EXIT_FAILURE);
   1535 	}
   1536 
   1537 	sz = sizeof(buf);
   1538 	memset(&newdesc, 0, sizeof(newdesc));
   1539 	newdesc.sysctl_flags = SYSCTL_VERSION|CTLFLAG_OWNDESC;
   1540 	newdesc.sysctl_num = name[namelen - 1];
   1541 	newdesc.sysctl_desc = value;
   1542 	name[namelen - 1] = CTL_DESCRIBE;
   1543 	rc = prog_sysctl(name, namelen, d, &sz, &newdesc, sizeof(newdesc));
   1544 	if (rc == -1)
   1545 		sysctlperror("%s: CTL_DESCRIBE failed: %s\n",
   1546 			     gsname, strerror(errno));
   1547 	else if (d->descr_len == 1)
   1548 		sysctlperror("%s: description not set\n", gsname);
   1549 	else if (!qflag && !nflag)
   1550 		printf("%s: %s\n", gsname, d->descr_str);
   1551 }
   1552 
   1553 /*
   1554  * ********************************************************************
   1555  * when things go wrong...
   1556  * ********************************************************************
   1557  */
   1558 static void
   1559 usage(void)
   1560 {
   1561 	const char *progname = getprogname();
   1562 
   1563 	(void)fprintf(stderr,
   1564 		      "usage:\t%s %s\n"
   1565 		      "\t%s %s\n"
   1566 		      "\t%s %s\n"
   1567 		      "\t%s %s\n"
   1568 		      "\t%s %s\n"
   1569 		      "\t%s %s\n",
   1570 		      progname, "[-dneq] [-x[x]|-r] variable ...",
   1571 		      progname, "[-ne] [-q] -w variable=value ...",
   1572 		      progname, "[-dne] -a",
   1573 		      progname, "[-dne] -A",
   1574 		      progname, "[-ne] -M",
   1575 		      progname, "[-dne] [-q] -f file");
   1576 	exit(EXIT_FAILURE);
   1577 }
   1578 
   1579 static void
   1580 getdesc1(int *name, u_int namelen, struct sysctlnode *pnode)
   1581 {
   1582 	struct sysctlnode node;
   1583 	char buf[1024], *desc;
   1584 	struct sysctldesc *d = (void*)buf;
   1585 	size_t sz = sizeof(buf);
   1586 	int rc;
   1587 
   1588 	memset(&node, 0, sizeof(node));
   1589 	node.sysctl_flags = SYSCTL_VERSION;
   1590 	node.sysctl_num = name[namelen - 1];
   1591 	name[namelen - 1] = CTL_DESCRIBE;
   1592 	rc = prog_sysctl(name, namelen, d, &sz, &node, sizeof(node));
   1593 
   1594 	if (rc == -1 ||
   1595 	    d->descr_len == 1 ||
   1596 	    d->descr_num != pnode->sysctl_num ||
   1597 	    d->descr_ver != pnode->sysctl_ver)
   1598 		desc = (char *)-1;
   1599 	else
   1600 		desc = malloc(d->descr_len);
   1601 
   1602 	if (desc == NULL)
   1603 		desc = (char *)-1;
   1604 	if (desc != (char *)-1)
   1605 		memcpy(desc, &d->descr_str[0], d->descr_len);
   1606 	name[namelen - 1] = node.sysctl_num;
   1607 	if (pnode->sysctl_desc != NULL &&
   1608 	    pnode->sysctl_desc != (const char *)-1)
   1609 		free(__UNCONST(pnode->sysctl_desc));
   1610 	pnode->sysctl_desc = desc;
   1611 }
   1612 
   1613 static void
   1614 getdesc(int *name, u_int namelen, struct sysctlnode *pnode)
   1615 {
   1616 	struct sysctlnode *node = pnode->sysctl_child;
   1617 	struct sysctldesc *d, *p, *plim;
   1618 	char *desc;
   1619 	size_t i, sz, child_cnt;
   1620 	int rc;
   1621 
   1622 	sz = 128 * pnode->sysctl_clen;
   1623 	name[namelen] = CTL_DESCRIBE;
   1624 
   1625 	/*
   1626 	 * attempt *twice* to get the description chunk.  if two tries
   1627 	 * doesn't work, give up.
   1628 	 */
   1629 	i = 0;
   1630 	do {
   1631 		d = malloc(sz);
   1632 		if (d == NULL)
   1633 			return;
   1634 		rc = prog_sysctl(name, namelen + 1, d, &sz, NULL, 0);
   1635 		if (rc == -1) {
   1636 			free(d);
   1637 			d = NULL;
   1638 			if (i == 0 && errno == ENOMEM)
   1639 				i = 1;
   1640 			else
   1641 				return;
   1642 		}
   1643 	} while (d == NULL);
   1644 
   1645 	/*
   1646 	 * hokey nested loop here, giving O(n**2) behavior, but should
   1647 	 * suffice for now
   1648 	 */
   1649 	plim = /*LINTED ptr cast*/(struct sysctldesc *)((char*)d + sz);
   1650 	child_cnt = (pnode->sysctl_flags & CTLTYPE_NODE) ? pnode->sysctl_clen
   1651 	    : 0;
   1652 	for (i = 0; i < child_cnt; i++) {
   1653 		node = &pnode->sysctl_child[i];
   1654 		for (p = d; p < plim; p = NEXT_DESCR(p))
   1655 			if (node->sysctl_num == p->descr_num)
   1656 				break;
   1657 		if (p < plim && node->sysctl_ver == p->descr_ver) {
   1658 			/*
   1659 			 * match found, attempt to attach description
   1660 			 */
   1661 			if (p->descr_len == 1)
   1662 				desc = NULL;
   1663 			else
   1664 				desc = malloc(p->descr_len);
   1665 			if (desc == NULL)
   1666 				desc = (char *)-1;
   1667 			else
   1668 				memcpy(desc, &p->descr_str[0], p->descr_len);
   1669 			node->sysctl_desc = desc;
   1670 		}
   1671 	}
   1672 
   1673 	free(d);
   1674 }
   1675 
   1676 static void
   1677 trim_whitespace(char *s, int dir)
   1678 {
   1679 	char *i, *o;
   1680 
   1681 	i = o = s;
   1682 	if (dir & 1)
   1683 		while (isspace((unsigned char)*i))
   1684 			i++;
   1685 	while ((*o++ = *i++) != '\0');
   1686 	o -= 2; /* already past nul, skip back to before it */
   1687 	if (dir & 2)
   1688 		while (o > s && isspace((unsigned char)*o))
   1689 			*o-- = '\0';
   1690 }
   1691 
   1692 void
   1693 sysctlerror(int soft)
   1694 {
   1695 	if (soft) {
   1696 		switch (errno) {
   1697 		case ENOENT:
   1698 		case ENOPROTOOPT:
   1699 		case ENOTDIR:
   1700 		case EINVAL:
   1701 		case EOPNOTSUPP:
   1702 		case EPROTONOSUPPORT:
   1703 			if (Aflag || req)
   1704 				sysctlperror("%s: the value is not available\n",
   1705 					     gsname);
   1706 			return;
   1707 		}
   1708 	}
   1709 
   1710 	if (Aflag || req)
   1711 		sysctlperror("%s: %s\n", gsname, strerror(errno));
   1712 	if (!soft)
   1713 		EXIT(EXIT_FAILURE);
   1714 }
   1715 
   1716 void
   1717 sysctlparseerror(u_int namelen, const char *pname)
   1718 {
   1719 
   1720 	if (qflag) {
   1721 		errs++;
   1722 		return;
   1723 	}
   1724 	sysctlperror("%s level name '%s' in '%s' is invalid\n",
   1725 		     lname[namelen], gsname, pname);
   1726 }
   1727 
   1728 static void
   1729 sysctlperror(const char *fmt, ...)
   1730 {
   1731 	va_list ap;
   1732 
   1733 	(void)fprintf(warnfp, "%s: ", getprogname());
   1734 	if (fn)
   1735 		(void)fprintf(warnfp, "%s#%zu: ", fn, nr);
   1736 	va_start(ap, fmt);
   1737 	(void)vfprintf(warnfp, fmt, ap);
   1738 	va_end(ap);
   1739 	errs++;
   1740 }
   1741 
   1742 
   1743 /*
   1744  * ********************************************************************
   1745  * how to write to a "simple" node
   1746  * ********************************************************************
   1747  */
   1748 static void
   1749 write_number(int *name, u_int namelen, struct sysctlnode *node, char *value)
   1750 {
   1751 	u_int ii, io;
   1752 	u_quad_t qi, qo;
   1753 	size_t si, so;
   1754 	bool bi, bo;
   1755 	int rc;
   1756 	void *i, *o;
   1757 	char *t;
   1758 
   1759 	if (fn)
   1760 		trim_whitespace(value, 3);
   1761 
   1762 	si = so = 0;
   1763 	i = o = NULL;
   1764 	bi = bo = false;
   1765 	errno = 0;
   1766 	qi = strtouq(value, &t, 0);
   1767 	if (qi == UQUAD_MAX && errno == ERANGE) {
   1768 		sysctlperror("%s: %s\n", value, strerror(errno));
   1769 		EXIT(EXIT_FAILURE);
   1770 	}
   1771 	if (t == value || *t != '\0') {
   1772 		sysctlperror("%s: not a number\n", value);
   1773 		EXIT(EXIT_FAILURE);
   1774 	}
   1775 
   1776 	switch (SYSCTL_TYPE(node->sysctl_flags)) {
   1777 	case CTLTYPE_INT:
   1778 		ii = (u_int)qi;
   1779 		io = (u_int)(qi >> 32);
   1780 		if (io != (u_int)-1 && io != 0) {
   1781 			sysctlperror("%s: %s\n", value, strerror(ERANGE));
   1782 			EXIT(EXIT_FAILURE);
   1783 		}
   1784 		o = &io;
   1785 		so = sizeof(io);
   1786 		i = &ii;
   1787 		si = sizeof(ii);
   1788 		break;
   1789 	case CTLTYPE_BOOL:
   1790 		bi = (bool)qi;
   1791 		o = &bo;
   1792 		so = sizeof(bo);
   1793 		i = &bi;
   1794 		si = sizeof(bi);
   1795 		break;
   1796 	case CTLTYPE_QUAD:
   1797 		o = &qo;
   1798 		so = sizeof(qo);
   1799 		i = &qi;
   1800 		si = sizeof(qi);
   1801 		break;
   1802 	}
   1803 
   1804 	rc = prog_sysctl(name, namelen, o, &so, i, si);
   1805 	if (rc == -1) {
   1806 		sysctlerror(0);
   1807 		return;
   1808 	}
   1809 
   1810 	switch (SYSCTL_TYPE(node->sysctl_flags)) {
   1811 	case CTLTYPE_INT:
   1812 		display_number(node, gsname, &io, sizeof(io), DISPLAY_OLD);
   1813 		display_number(node, gsname, &ii, sizeof(ii), DISPLAY_NEW);
   1814 		break;
   1815 	case CTLTYPE_BOOL:
   1816 		display_number(node, gsname, &bo, sizeof(bo), DISPLAY_OLD);
   1817 		display_number(node, gsname, &bi, sizeof(bi), DISPLAY_NEW);
   1818 		break;
   1819 	case CTLTYPE_QUAD:
   1820 		display_number(node, gsname, &qo, sizeof(qo), DISPLAY_OLD);
   1821 		display_number(node, gsname, &qi, sizeof(qi), DISPLAY_NEW);
   1822 		break;
   1823 	}
   1824 }
   1825 
   1826 static void
   1827 write_string(int *name, u_int namelen, struct sysctlnode *node, char *value)
   1828 {
   1829 	char *i, *o;
   1830 	size_t si, so;
   1831 	int rc;
   1832 
   1833 	i = value;
   1834 	si = strlen(i) + 1;
   1835 	so = node->sysctl_size;
   1836 	if (si > so && so != 0) {
   1837 		sysctlperror("%s: string too long\n", value);
   1838 		EXIT(EXIT_FAILURE);
   1839 	}
   1840 	o = malloc(so);
   1841 	if (o == NULL) {
   1842 		sysctlperror("%s: !malloc failed!\n", gsname);
   1843 		exit(EXIT_FAILURE);
   1844 	}
   1845 
   1846 	rc = prog_sysctl(name, namelen, o, &so, i, si);
   1847 	if (rc == -1) {
   1848 		sysctlerror(0);
   1849 		return;
   1850 	}
   1851 
   1852 	display_string(node, gsname, o, so, DISPLAY_OLD);
   1853 	display_string(node, gsname, i, si, DISPLAY_NEW);
   1854 	free(o);
   1855 }
   1856 
   1857 /*
   1858  * ********************************************************************
   1859  * simple ways to print stuff consistently
   1860  * ********************************************************************
   1861  */
   1862 static void
   1863 display_number(const struct sysctlnode *node, const char *name,
   1864 	       const void *data, size_t sz, int n)
   1865 {
   1866 	u_quad_t q;
   1867 	bool b;
   1868 	int i;
   1869 
   1870 	if (qflag)
   1871 		return;
   1872 	if ((nflag || rflag) && (n == DISPLAY_OLD))
   1873 		return;
   1874 
   1875 	if (rflag && n != DISPLAY_OLD) {
   1876 		fwrite(data, sz, 1, stdout);
   1877 		return;
   1878 	}
   1879 
   1880 	if (!nflag) {
   1881 		if (n == DISPLAY_VALUE)
   1882 			printf("%s%s", name, eq);
   1883 		else if (n == DISPLAY_OLD)
   1884 			printf("%s: ", name);
   1885 	}
   1886 
   1887 	if (xflag > 1) {
   1888 		if (n != DISPLAY_NEW)
   1889 			printf("\n");
   1890 		hex_dump(data, sz);
   1891 		return;
   1892 	}
   1893 
   1894 	switch (SYSCTL_TYPE(node->sysctl_flags)) {
   1895 	case CTLTYPE_INT:
   1896 		memcpy(&i, data, sz);
   1897 		if (xflag)
   1898 			printf("0x%0*x", (int)sz * 2, i);
   1899 		else if (node->sysctl_flags & CTLFLAG_HEX)
   1900 			printf("%#x", i);
   1901 		else if (node->sysctl_flags & CTLFLAG_UNSIGNED)
   1902 			printf("%u", i);
   1903 		else
   1904 			printf("%d", i);
   1905 		break;
   1906 	case CTLTYPE_BOOL:
   1907 		memcpy(&b, data, sz);
   1908 		if (xflag)
   1909 			printf("0x%0*x", (int)sz * 2, b);
   1910 		else if (node->sysctl_flags & CTLFLAG_HEX)
   1911 			printf("%#x", b);
   1912 		else
   1913 			printf("%d", b);
   1914 		break;
   1915 	case CTLTYPE_QUAD:
   1916 		memcpy(&q, data, sz);
   1917 		if (xflag)
   1918 			printf("0x%0*" PRIx64, (int)sz * 2, q);
   1919 		else if (node->sysctl_flags & CTLFLAG_HEX)
   1920 			printf("%#" PRIx64, q);
   1921 		else if (node->sysctl_flags & CTLFLAG_UNSIGNED)
   1922 			printf("%" PRIu64, q);
   1923 		else
   1924 			printf("%" PRIu64, q);
   1925 		break;
   1926 	}
   1927 
   1928 	if (n == DISPLAY_OLD)
   1929 		printf(" -> ");
   1930 	else
   1931 		printf("\n");
   1932 }
   1933 
   1934 static void
   1935 display_string(const struct sysctlnode *node, const char *name,
   1936 	       const void *data, size_t sz, int n)
   1937 {
   1938 	const unsigned char *buf = data;
   1939 	int ni;
   1940 
   1941 	if (qflag)
   1942 		return;
   1943 	if ((nflag || rflag) && (n == DISPLAY_OLD))
   1944 		return;
   1945 
   1946 	if (rflag && n != DISPLAY_OLD) {
   1947 		fwrite(data, sz, 1, stdout);
   1948 		return;
   1949 	}
   1950 
   1951 	if (!nflag) {
   1952 		if (n == DISPLAY_VALUE)
   1953 			printf("%s%s", name, eq);
   1954 		else if (n == DISPLAY_OLD)
   1955 			printf("%s: ", name);
   1956 	}
   1957 
   1958 	if (xflag > 1) {
   1959 		if (n != DISPLAY_NEW)
   1960 			printf("\n");
   1961 		hex_dump(data, sz);
   1962 		return;
   1963 	}
   1964 
   1965 	if (xflag || node->sysctl_flags & CTLFLAG_HEX) {
   1966 		for (ni = 0; ni < (int)sz; ni++) {
   1967 			if (xflag)
   1968 				printf("%02x", buf[ni]);
   1969 			if (buf[ni] == '\0')
   1970 				break;
   1971 			if (!xflag)
   1972 				printf("\\x%2.2x", buf[ni]);
   1973 		}
   1974 	}
   1975 	else
   1976 		printf("%.*s", (int)sz, buf);
   1977 
   1978 	if (n == DISPLAY_OLD)
   1979 		printf(" -> ");
   1980 	else
   1981 		printf("\n");
   1982 }
   1983 
   1984 /*ARGSUSED*/
   1985 static void
   1986 display_struct(const struct sysctlnode *node, const char *name,
   1987 	       const void *data, size_t sz, int n)
   1988 {
   1989 	const unsigned char *buf = data;
   1990 	int ni;
   1991 	size_t more;
   1992 
   1993 	if (qflag)
   1994 		return;
   1995 	if (!(xflag || rflag)) {
   1996 		if (Aflag || req)
   1997 			sysctlperror(
   1998 			    "%s: this type is unknown to this program\n",
   1999 			    gsname);
   2000 		return;
   2001 	}
   2002 	if ((nflag || rflag) && (n == DISPLAY_OLD))
   2003 		return;
   2004 
   2005 	if (rflag && n != DISPLAY_OLD) {
   2006 		fwrite(data, sz, 1, stdout);
   2007 		return;
   2008 	}
   2009 
   2010         if (!nflag) {
   2011                 if (n == DISPLAY_VALUE)
   2012                         printf("%s%s", name, eq);
   2013                 else if (n == DISPLAY_OLD)
   2014                         printf("%s: ", name);
   2015         }
   2016 
   2017 	if (xflag > 1) {
   2018 		if (n != DISPLAY_NEW)
   2019 			printf("\n");
   2020 		hex_dump(data, sz);
   2021 		return;
   2022 	}
   2023 
   2024 	if (sz > 16) {
   2025 		more = sz - 16;
   2026 		sz = 16;
   2027 	}
   2028 	else
   2029 		more = 0;
   2030 	for (ni = 0; ni < (int)sz; ni++)
   2031 		printf("%02x", buf[ni]);
   2032 	if (more)
   2033 		printf("...(%zu more bytes)", more);
   2034 	printf("\n");
   2035 }
   2036 
   2037 static void
   2038 hex_dump(const unsigned char *buf, size_t len)
   2039 {
   2040 	unsigned int i;
   2041 	int j;
   2042 	char line[80], tmp[12];
   2043 
   2044 	memset(line, ' ', sizeof(line));
   2045 	for (i = 0, j = 15; i < len; i++) {
   2046 		j = i % 16;
   2047 		/* reset line */
   2048 		if (j == 0) {
   2049 			line[58] = '|';
   2050 			line[77] = '|';
   2051 			line[78] = 0;
   2052 			snprintf(tmp, sizeof(tmp), "%07x", i);
   2053 			memcpy(&line[0], tmp, 7);
   2054 		}
   2055 		/* copy out hex version of byte */
   2056 		snprintf(tmp, sizeof(tmp), "%02x", buf[i]);
   2057 		memcpy(&line[9 + j * 3], tmp, 2);
   2058 		/* copy out plain version of byte */
   2059 		line[60 + j] = (isprint(buf[i])) ? buf[i] : '.';
   2060 		/* print a full line and erase it */
   2061 		if (j == 15) {
   2062 			printf("%s\n", line);
   2063 			memset(line, ' ', sizeof(line));
   2064 		}
   2065 	}
   2066 	if (line[0] != ' ')
   2067 		printf("%s\n", line);
   2068 	printf("%07zu bytes\n", len);
   2069 }
   2070 
   2071 /*
   2072  * ********************************************************************
   2073  * functions that handle particular nodes
   2074  * ********************************************************************
   2075  */
   2076 /*ARGSUSED*/
   2077 static void
   2078 printother(HANDLER_ARGS)
   2079 {
   2080 	int rc;
   2081 	void *p;
   2082 	size_t sz1, sz2;
   2083 
   2084 	if (!(Aflag || req) || Mflag)
   2085 		return;
   2086 
   2087 	/*
   2088 	 * okay...you asked for it, so let's give it a go
   2089 	 */
   2090 	while (type != CTLTYPE_NODE && (xflag || rflag)) {
   2091 		rc = prog_sysctl(name, namelen, NULL, &sz1, NULL, 0);
   2092 		if (rc == -1 || sz1 == 0)
   2093 			break;
   2094 		p = malloc(sz1);
   2095 		if (p == NULL)
   2096 			break;
   2097 		sz2 = sz1;
   2098 		rc = prog_sysctl(name, namelen, p, &sz2, NULL, 0);
   2099 		if (rc == -1 || sz1 != sz2) {
   2100 			free(p);
   2101 			break;
   2102 		}
   2103 		display_struct(pnode, gsname, p, sz1, DISPLAY_VALUE);
   2104 		free(p);
   2105 		return;
   2106 	}
   2107 
   2108 	/*
   2109 	 * that didn't work...do we have a specific message for this
   2110 	 * thing?
   2111 	 */
   2112 	if (v != NULL) {
   2113 		sysctlperror("%s: use '%s' to view this information\n",
   2114 			     gsname, (const char *)v);
   2115 		return;
   2116 	}
   2117 
   2118 	/*
   2119 	 * hmm...i wonder if we have any generic hints?
   2120 	 */
   2121 	switch (name[0]) {
   2122 	case CTL_NET:
   2123 		sysctlperror("%s: use 'netstat' to view this information\n",
   2124 			     sname);
   2125 		break;
   2126 	case CTL_DEBUG:
   2127 		sysctlperror("%s: missing 'options DEBUG' from kernel?\n",
   2128 			     sname);
   2129 		break;
   2130 	case CTL_DDB:
   2131 		sysctlperror("%s: missing 'options DDB' from kernel?\n",
   2132 			     sname);
   2133 		break;
   2134 	case CTL_VENDOR:
   2135 		sysctlperror("%s: no vendor extensions installed\n",
   2136 			     sname);
   2137 		break;
   2138 	}
   2139 }
   2140 
   2141 /*ARGSUSED*/
   2142 static void
   2143 kern_clockrate(HANDLER_ARGS)
   2144 {
   2145 	struct clockinfo clkinfo;
   2146 	size_t sz;
   2147 	int rc;
   2148 
   2149 	sz = sizeof(clkinfo);
   2150 	rc = prog_sysctl(name, namelen, &clkinfo, &sz, NULL, 0);
   2151 	if (rc == -1) {
   2152 		sysctlerror(1);
   2153 		return;
   2154 	}
   2155 	if (sz != sizeof(clkinfo))
   2156 		errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
   2157 
   2158 	if (xflag || rflag) {
   2159 		display_struct(pnode, sname, &clkinfo, sz,
   2160 			       DISPLAY_VALUE);
   2161 		return;
   2162 	}
   2163 	else if (!nflag)
   2164 		printf("%s: ", sname);
   2165 	printf("tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n",
   2166 	       clkinfo.tick, clkinfo.tickadj,
   2167 	       clkinfo.hz, clkinfo.profhz, clkinfo.stathz);
   2168 }
   2169 
   2170 /*ARGSUSED*/
   2171 static void
   2172 kern_boottime(HANDLER_ARGS)
   2173 {
   2174 	struct timeval timeval;
   2175 	time_t boottime;
   2176 	size_t sz;
   2177 	int rc;
   2178 
   2179 	sz = sizeof(timeval);
   2180 	rc = prog_sysctl(name, namelen, &timeval, &sz, NULL, 0);
   2181 	if (rc == -1) {
   2182 		sysctlerror(1);
   2183 		return;
   2184 	}
   2185 	if (sz != sizeof(timeval))
   2186 		errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
   2187 
   2188 	boottime = timeval.tv_sec;
   2189 	if (xflag || rflag)
   2190 		display_struct(pnode, sname, &timeval, sz,
   2191 			       DISPLAY_VALUE);
   2192 	else if (!nflag)
   2193 		/* ctime() provides the \n */
   2194 		printf("%s%s%s", sname, eq, ctime(&boottime));
   2195 	else if (nflag == 1)
   2196 		printf("%ld\n", (long)boottime);
   2197 	else
   2198 		printf("%ld.%06ld\n", (long)timeval.tv_sec,
   2199 		       (long)timeval.tv_usec);
   2200 }
   2201 
   2202 /*ARGSUSED*/
   2203 static void
   2204 kern_consdev(HANDLER_ARGS)
   2205 {
   2206 	dev_t cons;
   2207 	size_t sz;
   2208 	int rc;
   2209 
   2210 	sz = sizeof(cons);
   2211 	rc = prog_sysctl(name, namelen, &cons, &sz, NULL, 0);
   2212 	if (rc == -1) {
   2213 		sysctlerror(1);
   2214 		return;
   2215 	}
   2216 	if (sz != sizeof(cons))
   2217 		errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
   2218 
   2219 	if (xflag || rflag)
   2220 		display_struct(pnode, sname, &cons, sz,
   2221 			       DISPLAY_VALUE);
   2222 	else {
   2223 		if (!nflag)
   2224 			printf("%s%s", sname, eq);
   2225 		if (nflag < 2 && (sname = devname(cons, S_IFCHR)) != NULL)
   2226 			printf("%s\n", sname);
   2227 		else
   2228 			printf("0x%llx\n", (unsigned long long)cons);
   2229 	}
   2230 }
   2231 
   2232 /*ARGSUSED*/
   2233 static void
   2234 kern_cp_time(HANDLER_ARGS)
   2235 {
   2236 	u_int64_t *cp_time;
   2237 	size_t sz, osz;
   2238 	int rc, i, n;
   2239 	char s[sizeof("kern.cp_time.nnnnnn")];
   2240 	const char *tname;
   2241 
   2242 	/*
   2243 	 * three things to do here.
   2244 	 * case 1: get sum (no Aflag and namelen == 2)
   2245 	 * case 2: get specific processor (namelen == 3)
   2246 	 * case 3: get all processors (Aflag and namelen == 2)
   2247 	 */
   2248 
   2249 	if (namelen == 2 && Aflag) {
   2250 		sz = sizeof(n);
   2251 		rc = sysctlbyname("hw.ncpu", &n, &sz, NULL, 0);
   2252 		if (rc != 0)
   2253 			return; /* XXX print an error, eh? */
   2254 		n++; /* Add on space for the sum. */
   2255 		sz = n * sizeof(u_int64_t) * CPUSTATES;
   2256 	}
   2257 	else {
   2258 		n = -1; /* Just print one data set. */
   2259 		sz = sizeof(u_int64_t) * CPUSTATES;
   2260 	}
   2261 
   2262 	cp_time = malloc(sz);
   2263 	if (cp_time == NULL) {
   2264 		sysctlerror(1);
   2265 		return;
   2266 	}
   2267 
   2268 	osz = sz;
   2269 	rc = prog_sysctl(name, namelen, cp_time + (n != -1) * CPUSTATES, &osz,
   2270 		    NULL, 0);
   2271 
   2272 	if (rc == -1) {
   2273 		sysctlerror(1);
   2274 		free(cp_time);
   2275 		return;
   2276 	}
   2277 
   2278 	/*
   2279 	 * Check, but account for space we'll occupy with the sum.
   2280 	 */
   2281 	if (osz != sz - (n != -1) * CPUSTATES * sizeof(u_int64_t))
   2282 		errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
   2283 
   2284 	/*
   2285 	 * Compute the actual sum.  Two calls would be easier (we
   2286 	 * could just call ourselves recursively above), but the
   2287 	 * numbers wouldn't add up.
   2288 	 */
   2289 	if (n != -1) {
   2290 		memset(cp_time, 0, sizeof(u_int64_t) * CPUSTATES);
   2291 		for (i = 1; i < n; i++) {
   2292 			cp_time[CP_USER] += cp_time[i * CPUSTATES + CP_USER];
   2293                         cp_time[CP_NICE] += cp_time[i * CPUSTATES + CP_NICE];
   2294                         cp_time[CP_SYS] += cp_time[i * CPUSTATES + CP_SYS];
   2295                         cp_time[CP_INTR] += cp_time[i * CPUSTATES + CP_INTR];
   2296                         cp_time[CP_IDLE] += cp_time[i * CPUSTATES + CP_IDLE];
   2297 		}
   2298 	}
   2299 
   2300 	tname = sname;
   2301 	for (i = 0; n == -1 || i < n; i++) {
   2302 		if (i > 0) {
   2303 			(void)snprintf(s, sizeof(s), "%s%s%d", sname, sep,
   2304 				       i - 1);
   2305 			tname = s;
   2306 		}
   2307 		if (xflag || rflag)
   2308 			display_struct(pnode, tname, cp_time + (i * CPUSTATES),
   2309 				       sizeof(u_int64_t) * CPUSTATES,
   2310 				       DISPLAY_VALUE);
   2311 		else {
   2312 			if (!nflag)
   2313 				printf("%s: ", tname);
   2314 			printf("user = %" PRIu64
   2315 			       ", nice = %" PRIu64
   2316 			       ", sys = %" PRIu64
   2317 			       ", intr = %" PRIu64
   2318 			       ", idle = %" PRIu64
   2319 			       "\n",
   2320 			       cp_time[i * CPUSTATES + CP_USER],
   2321 			       cp_time[i * CPUSTATES + CP_NICE],
   2322 			       cp_time[i * CPUSTATES + CP_SYS],
   2323 			       cp_time[i * CPUSTATES + CP_INTR],
   2324 			       cp_time[i * CPUSTATES + CP_IDLE]);
   2325 		}
   2326 		/*
   2327 		 * Just printing the one node.
   2328 		 */
   2329 		if (n == -1)
   2330 			break;
   2331 	}
   2332 
   2333 	free(cp_time);
   2334 }
   2335 
   2336 /*ARGSUSED*/
   2337 static void
   2338 kern_drivers(HANDLER_ARGS)
   2339 {
   2340 	struct kinfo_drivers *kd;
   2341 	size_t sz, i;
   2342 	int rc;
   2343 	const char *comma;
   2344 
   2345 	rc = prog_sysctl(name, namelen, NULL, &sz, NULL, 0);
   2346 	if (rc == -1) {
   2347 		sysctlerror(1);
   2348 		return;
   2349 	}
   2350 
   2351 	if (sz % sizeof(*kd))
   2352 		err(EXIT_FAILURE, "bad size %zu for kern.drivers", sz);
   2353 
   2354 	kd = malloc(sz);
   2355 	if (kd == NULL) {
   2356 		sysctlerror(1);
   2357 		return;
   2358 	}
   2359 
   2360 	rc = prog_sysctl(name, namelen, kd, &sz, NULL, 0);
   2361 	if (rc == -1) {
   2362 		sysctlerror(1);
   2363 		free(kd);
   2364 		return;
   2365 	}
   2366 
   2367 	comma = "";
   2368 	if (!nflag)
   2369 		printf("%s%s", sname, eq);
   2370 	for (i = 0, sz /= sizeof(*kd); i < sz; i++) {
   2371 		(void)printf("%s[%d %d %s]", comma, kd[i].d_cmajor,
   2372 		    kd[i].d_bmajor, kd[i].d_name);
   2373 		comma = ", ";
   2374 	}
   2375 	(void)printf("\n");
   2376 	free(kd);
   2377 }
   2378 
   2379 /*ARGSUSED*/
   2380 static void
   2381 kern_cp_id(HANDLER_ARGS)
   2382 {
   2383 	u_int64_t *cp_id;
   2384 	size_t sz, osz;
   2385 	int rc, i, n;
   2386 	char s[sizeof("kern.cp_id.nnnnnn")];
   2387 	const char *tname;
   2388 	struct sysctlnode node = *pnode;
   2389 
   2390 	/*
   2391 	 * three things to do here.
   2392 	 * case 1: print a specific cpu id (namelen == 3)
   2393 	 * case 2: print all cpu ids separately (Aflag set)
   2394 	 * case 3: print all cpu ids on one line
   2395 	 */
   2396 
   2397 	if (namelen == 2) {
   2398 		sz = sizeof(n);
   2399 		rc = sysctlbyname("hw.ncpu", &n, &sz, NULL, 0);
   2400 		if (rc != 0)
   2401 			return; /* XXX print an error, eh? */
   2402 		sz = n * sizeof(u_int64_t);
   2403 	}
   2404 	else {
   2405 		n = -1; /* Just print one cpu id. */
   2406 		sz = sizeof(u_int64_t);
   2407 	}
   2408 
   2409 	cp_id = malloc(sz);
   2410 	if (cp_id == NULL) {
   2411 		sysctlerror(1);
   2412 		return;
   2413 	}
   2414 
   2415 	osz = sz;
   2416 	rc = prog_sysctl(name, namelen, cp_id, &osz, NULL, 0);
   2417 	if (rc == -1) {
   2418 		sysctlerror(1);
   2419 		free(cp_id);
   2420 		return;
   2421 	}
   2422 
   2423 	/*
   2424 	 * Check that we got back what we asked for.
   2425 	 */
   2426 	if (osz != sz)
   2427 		errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
   2428 
   2429 	/* pretend for output purposes */
   2430 	node.sysctl_flags = SYSCTL_FLAGS(pnode->sysctl_flags) |
   2431 		SYSCTL_TYPE(CTLTYPE_QUAD);
   2432 
   2433 	tname = sname;
   2434 	if (namelen == 3)
   2435 		display_number(&node, tname, cp_id,
   2436 			       sizeof(u_int64_t),
   2437 			       DISPLAY_VALUE);
   2438 	else if (Aflag) {
   2439 		for (i = 0; i < n; i++)
   2440 			(void)snprintf(s, sizeof(s), "%s%s%d", sname, sep, i);
   2441 			tname = s;
   2442 			display_number(&node, tname, &cp_id[i],
   2443 				       sizeof(u_int64_t),
   2444 				       DISPLAY_VALUE);
   2445 	}
   2446 	else {
   2447 		if (xflag || rflag)
   2448 			display_struct(pnode, tname, cp_id, sz, DISPLAY_VALUE);
   2449 		else {
   2450 			if (!nflag)
   2451 				printf("%s: ", tname);
   2452 			for (i = 0; i < n; i++) {
   2453 				if (i)
   2454 					printf(", ");
   2455 				printf("%d = %" PRIu64, i, cp_id[i]);
   2456 			}
   2457 			printf("\n");
   2458 		}
   2459 	}
   2460 
   2461 	free(cp_id);
   2462 }
   2463 
   2464 /*ARGSUSED*/
   2465 static void
   2466 vm_loadavg(HANDLER_ARGS)
   2467 {
   2468 	struct loadavg loadavg;
   2469 	size_t sz;
   2470 	int rc;
   2471 
   2472 	sz = sizeof(loadavg);
   2473 	rc = prog_sysctl(name, namelen, &loadavg, &sz, NULL, 0);
   2474 	if (rc == -1) {
   2475 		sysctlerror(1);
   2476 		return;
   2477 	}
   2478 	if (sz != sizeof(loadavg))
   2479 		errx(EXIT_FAILURE, "%s: !returned size wrong!", sname);
   2480 
   2481 	if (xflag || rflag) {
   2482 		display_struct(pnode, sname, &loadavg, sz,
   2483 			       DISPLAY_VALUE);
   2484 		return;
   2485 	}
   2486 	if (!nflag)
   2487 		printf("%s: ", sname);
   2488 	printf("%.2f %.2f %.2f\n",
   2489 	       (double) loadavg.ldavg[0] / loadavg.fscale,
   2490 	       (double) loadavg.ldavg[1] / loadavg.fscale,
   2491 	       (double) loadavg.ldavg[2] / loadavg.fscale);
   2492 }
   2493 
   2494 /*ARGSUSED*/
   2495 static void
   2496 proc_limit(HANDLER_ARGS)
   2497 {
   2498 	u_quad_t olim, *newp, nlim;
   2499 	size_t osz, nsz;
   2500 	char *t;
   2501 	int rc;
   2502 
   2503 	if (fn)
   2504 		trim_whitespace(value, 3);
   2505 
   2506 	osz = sizeof(olim);
   2507 	if (value != NULL) {
   2508 		nsz = sizeof(nlim);
   2509 		newp = &nlim;
   2510 		if (strcmp(value, "unlimited") == 0)
   2511 			nlim = RLIM_INFINITY;
   2512 		else {
   2513 			errno = 0;
   2514 			nlim = strtouq(value, &t, 0);
   2515 			if (t == value || *t != '\0' || errno != 0) {
   2516 				sysctlperror("%s: '%s' is not a valid limit\n",
   2517 					     sname, value);
   2518 				EXIT(EXIT_FAILURE);
   2519 			}
   2520 		}
   2521 	}
   2522 	else {
   2523 		nsz = 0;
   2524 		newp = NULL;
   2525 	}
   2526 
   2527 	rc = prog_sysctl(name, namelen, &olim, &osz, newp, nsz);
   2528 	if (rc == -1) {
   2529 		sysctlerror(newp == NULL);
   2530 		return;
   2531 	}
   2532 
   2533 	if (newp && qflag)
   2534 		return;
   2535 
   2536 	if (rflag || xflag || olim != RLIM_INFINITY)
   2537 		display_number(pnode, sname, &olim, sizeof(olim),
   2538 			       newp ? DISPLAY_OLD : DISPLAY_VALUE);
   2539 	else
   2540 		display_string(pnode, sname, "unlimited", 10,
   2541 			       newp ? DISPLAY_OLD : DISPLAY_VALUE);
   2542 
   2543 	if (newp) {
   2544 		if (rflag || xflag || nlim != RLIM_INFINITY)
   2545 			display_number(pnode, sname, &nlim, sizeof(nlim),
   2546 				       DISPLAY_NEW);
   2547 		else
   2548 			display_string(pnode, sname, "unlimited", 10,
   2549 				       DISPLAY_NEW);
   2550 	}
   2551 }
   2552 
   2553 #ifdef CPU_DISKINFO
   2554 /*ARGSUSED*/
   2555 static void
   2556 machdep_diskinfo(HANDLER_ARGS)
   2557 {
   2558 	struct disklist *dl;
   2559 	struct biosdisk_info *bi;
   2560 	struct nativedisk_info *ni;
   2561 	int rc;
   2562 	size_t sz;
   2563 	uint i, b, lim;
   2564 
   2565 	rc = prog_sysctl(name, namelen, NULL, &sz, NULL, 0);
   2566 	if (rc == -1) {
   2567 		sysctlerror(1);
   2568 		return;
   2569 	}
   2570 	dl = malloc(sz);
   2571 	if (dl == NULL) {
   2572 		sysctlerror(1);
   2573 		return;
   2574 	}
   2575 	rc = prog_sysctl(name, namelen, dl, &sz, NULL, 0);
   2576 	if (rc == -1) {
   2577 		sysctlerror(1);
   2578 		return;
   2579 	}
   2580 
   2581 	if (!nflag)
   2582 		printf("%s: ", sname);
   2583 	lim = dl->dl_nbiosdisks;
   2584 	if (lim > MAX_BIOSDISKS)
   2585 		lim = MAX_BIOSDISKS;
   2586 	for (bi = dl->dl_biosdisks, i = 0; i < lim; bi++, i++)
   2587 		printf("%x:%" PRIu64 "(%d/%d/%d),%x ",
   2588 		       bi->bi_dev, bi->bi_lbasecs,
   2589 		       bi->bi_cyl, bi->bi_head, bi->bi_sec,
   2590 		       bi->bi_flags);
   2591 	lim = dl->dl_nnativedisks;
   2592 	ni = dl->dl_nativedisks;
   2593 	bi = dl->dl_biosdisks;
   2594 	/* LINTED -- pointer casts are tedious */
   2595 	if ((char *)&ni[lim] != (char *)dl + sz) {
   2596 		sysctlperror("%s: size mismatch\n", gsname);
   2597 		return;
   2598 	}
   2599 	for (i = 0; i < lim; ni++, i++) {
   2600 		char t = ':';
   2601 		printf(" %.*s", (int)sizeof ni->ni_devname,
   2602 		       ni->ni_devname);
   2603 		for (b = 0; b < (unsigned int)ni->ni_nmatches; t = ',', b++)
   2604 			printf("%c%x", t,
   2605 			       bi[ni->ni_biosmatches[b]].bi_dev);
   2606 	}
   2607 	printf("\n");
   2608 	free(dl);
   2609 }
   2610 #endif /* CPU_DISKINFO */
   2611 
   2612 /*ARGSUSED*/
   2613 static void
   2614 mode_bits(HANDLER_ARGS)
   2615 {
   2616 	char buf[12], outbuf[100];
   2617 	int o, m, *newp, rc;
   2618 	size_t osz, nsz;
   2619 	mode_t om, mm;
   2620 
   2621 	if (fn)
   2622 		trim_whitespace(value, 3);
   2623 
   2624 	newp = NULL;
   2625 	osz = sizeof(o);
   2626 	if (value != NULL) {
   2627 		void *foo;
   2628 		int tt;
   2629 		size_t ttsz = sizeof(tt);
   2630 		mode_t old_umask;
   2631 
   2632 		nsz = sizeof(m);
   2633 		newp = &m;
   2634 		errno = 0;
   2635 		rc = prog_sysctl(name, namelen, &tt, &ttsz, NULL, 0);
   2636 		if (rc == -1) {
   2637 			sysctlperror("%s: failed query\n", sname);
   2638 			return;
   2639 		}
   2640 
   2641 		old_umask = umask(0);
   2642 		foo = setmode(value);
   2643 		umask(old_umask);
   2644 		if (foo == NULL) {
   2645 			sysctlperror("%s: '%s' is an invalid mode\n", sname,
   2646 				     value);
   2647 			EXIT(EXIT_FAILURE);
   2648 		}
   2649 		old_umask = umask(0);
   2650 		m = getmode(foo, (mode_t)tt);
   2651 		umask(old_umask);
   2652 		if (errno) {
   2653 			sysctlperror("%s: '%s' is an invalid mode\n", sname,
   2654 				     value);
   2655 			EXIT(EXIT_FAILURE);
   2656 		}
   2657 	}
   2658 	else {
   2659 		nsz = 0;
   2660 		newp = NULL;
   2661 	}
   2662 
   2663 	rc = prog_sysctl(name, namelen, &o, &osz, newp, nsz);
   2664 	if (rc == -1) {
   2665 		sysctlerror(newp == NULL);
   2666 		return;
   2667 	}
   2668 
   2669 	if (newp && qflag)
   2670 		return;
   2671 
   2672 	om = (mode_t)o;
   2673 	mm = (mode_t)m;
   2674 
   2675 	if (rflag || xflag)
   2676 		display_number(pnode, sname, &o, sizeof(o),
   2677 			       newp ? DISPLAY_OLD : DISPLAY_VALUE);
   2678 	else {
   2679 		memset(buf, 0, sizeof(buf));
   2680 		strmode(om, buf);
   2681 		rc = snprintf(outbuf, sizeof(outbuf), "%04o (%s)", om, buf + 1);
   2682 		display_string(pnode, sname, outbuf, rc, newp ? DISPLAY_OLD : DISPLAY_VALUE);
   2683 	}
   2684 
   2685 	if (newp) {
   2686 		if (rflag || xflag)
   2687 			display_number(pnode, sname, &m, sizeof(m),
   2688 				       DISPLAY_NEW);
   2689 		else {
   2690 			memset(buf, 0, sizeof(buf));
   2691 			strmode(mm, buf);
   2692 			rc = snprintf(outbuf, sizeof(outbuf), "%04o (%s)", mm, buf + 1);
   2693 			display_string(pnode, sname, outbuf, rc, DISPLAY_NEW);
   2694 		}
   2695 	}
   2696 }
   2697 
   2698 typedef __BITMAP_TYPE(, uint32_t, 0x10000) bitmap;
   2699 
   2700 static char *
   2701 bitmask_print(const bitmap *o)
   2702 {
   2703 	char *s, *os;
   2704 
   2705 	s = os = NULL;
   2706 	for (size_t i = 0; i < MAXPORTS; i++)
   2707 		if (__BITMAP_ISSET(i, o)) {
   2708 			int rv;
   2709 
   2710 			if (os)
   2711 			    	rv = asprintf(&s, "%s,%zu", os, i);
   2712 			else
   2713 			    	rv = asprintf(&s, "%zu", i);
   2714 			if (rv == -1)
   2715 				err(EXIT_FAILURE, "%s 1", __func__);
   2716 			free(os);
   2717 			os = s;
   2718 		}
   2719 	if (s == NULL && (s = strdup("")) == NULL)
   2720 		err(EXIT_FAILURE, "%s 2", __func__);
   2721 	return s;
   2722 }
   2723 
   2724 static void
   2725 bitmask_scan(const void *v, bitmap *o)
   2726 {
   2727 	char *s = strdup(v);
   2728 	if (s == NULL)
   2729 		err(EXIT_FAILURE, "%s", __func__);
   2730 
   2731 	__BITMAP_ZERO(o);
   2732 	for (s = strtok(s, ","); s; s = strtok(NULL, ",")) {
   2733 		char *e;
   2734 		errno = 0;
   2735 		unsigned long l = strtoul(s, &e, 0);
   2736 		if ((l == ULONG_MAX && errno == ERANGE) || s == e || *e)
   2737 			errx(EXIT_FAILURE, "Invalid port: %s", s);
   2738 		if (l >= MAXPORTS)
   2739 			errx(EXIT_FAILURE, "Port out of range: %s", s);
   2740 		__BITMAP_SET(l, o);
   2741 	}
   2742 }
   2743 
   2744 
   2745 static void
   2746 reserve(HANDLER_ARGS)
   2747 {
   2748 	int rc;
   2749 	size_t osz, nsz;
   2750 	bitmap o, n;
   2751 
   2752 	if (fn)
   2753 		trim_whitespace(value, 3);
   2754 
   2755 	osz = sizeof(o);
   2756 	if (value) {
   2757 		bitmask_scan(value, &n);
   2758 		value = (char *)&n;
   2759 		nsz = sizeof(n);
   2760 	} else
   2761 		nsz = 0;
   2762 
   2763 	rc = prog_sysctl(name, namelen, &o, &osz, value, nsz);
   2764 	if (rc == -1) {
   2765 		sysctlerror(value == NULL);
   2766 		return;
   2767 	}
   2768 
   2769 	if (value && qflag)
   2770 		return;
   2771 
   2772 	if (rflag || xflag)
   2773 		display_struct(pnode, sname, &o, sizeof(o),
   2774 		    value ? DISPLAY_OLD : DISPLAY_VALUE);
   2775 	else {
   2776 		char *s = bitmask_print(&o);
   2777 		display_string(pnode, sname, s, strlen(s),
   2778 		    value ? DISPLAY_OLD : DISPLAY_VALUE);
   2779 		free(s);
   2780 	}
   2781 
   2782 	if (value) {
   2783 		if (rflag || xflag)
   2784 			display_struct(pnode, sname, &n, sizeof(n),
   2785 			    DISPLAY_NEW);
   2786 		else {
   2787 			char *s = bitmask_print(&n);
   2788 			display_string(pnode, sname, s, strlen(s), DISPLAY_NEW);
   2789 			free(s);
   2790 		}
   2791 	}
   2792 }
   2793