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