Home | History | Annotate | Line # | Download | only in sysctl
sysctl.c revision 1.90
      1 /*	$NetBSD: sysctl.c,v 1.90 2004/04/08 06:49:03 atatat 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.90 2004/04/08 06:49:03 atatat 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 <stdarg.h>
    105 #include <stdio.h>
    106 #include <stdlib.h>
    107 #include <string.h>
    108 #include <time.h>
    109 #include <unistd.h>
    110 
    111 /*
    112  * this needs to be able to do the printing and the setting
    113  */
    114 #define HANDLER_PROTO const char *, const char *, char *, \
    115 	int *, u_int, const struct sysctlnode *, \
    116 	u_int, void *
    117 #define HANDLER_ARGS const char *sname, const char *dname, char *value, \
    118 	int *name, u_int namelen, const struct sysctlnode *pnode, \
    119 	u_int type, void *v
    120 #define DISPLAY_VALUE	0
    121 #define DISPLAY_OLD	1
    122 #define DISPLAY_NEW	2
    123 
    124 /*
    125  * generic routines
    126  */
    127 static struct handlespec *findprinter(const int *, u_int);
    128 static struct handlespec *findwriter(const int *, u_int);
    129 static void print_tree(int *, u_int, struct sysctlnode *, u_int, int);
    130 static void write_number(int *, u_int, struct sysctlnode *, char *);
    131 static void write_string(int *, u_int, struct sysctlnode *, char *);
    132 static void display_number(const struct sysctlnode *, const char *,
    133 			   const void *, size_t, int);
    134 static void display_string(const struct sysctlnode *, const char *,
    135 			   const void *, size_t, int);
    136 static void display_struct(const struct sysctlnode *, const char *,
    137 			   const void *, size_t, int);
    138 static void hex_dump(const unsigned char *, size_t);
    139 static void usage(void);
    140 static void parse(char *);
    141 static void parse_create(char *);
    142 static void parse_destroy(char *);
    143 static void parse_describe(char *);
    144 static void getdesc1(int *, u_int, struct sysctlnode *);
    145 static void getdesc(int *, u_int, struct sysctlnode *);
    146 static void sysctlerror(int);
    147 
    148 /*
    149  * "borrowed" from libc:sysctlgetmibinfo.c
    150  */
    151 int __learn_tree(int *, u_int, struct sysctlnode *);
    152 
    153 /*
    154  * "handlers"
    155  */
    156 static void printother(HANDLER_PROTO);
    157 static void kern_clockrate(HANDLER_PROTO);
    158 static void kern_boottime(HANDLER_PROTO);
    159 static void kern_consdev(HANDLER_PROTO);
    160 static void kern_cp_time(HANDLER_PROTO);
    161 static void vm_loadavg(HANDLER_PROTO);
    162 static void proc_limit(HANDLER_PROTO);
    163 #ifdef CPU_DISKINFO
    164 static void machdep_diskinfo(HANDLER_PROTO);
    165 #endif /* CPU_DISKINFO */
    166 
    167 struct handlespec {
    168 	int ps_name[CTL_MAXNAME];
    169 	void (*ps_p)(HANDLER_PROTO);
    170 	void (*ps_w)(HANDLER_PROTO);
    171 	void *ps_d;
    172 } handlers[] = {
    173 	{ { CTL_KERN, KERN_CLOCKRATE },	kern_clockrate },
    174 	{ { CTL_KERN, KERN_VNODE },	printother,	NULL,	"pstat" },
    175 	{ { CTL_KERN, KERN_PROC },	printother,	NULL,	"ps" },
    176 	{ { CTL_KERN, KERN_PROC2 },	printother,	NULL,	"ps" },
    177 	{ { CTL_KERN, KERN_PROC_ARGS },	printother,	NULL,	"ps" },
    178 	{ { CTL_KERN, KERN_FILE },	printother,	NULL,	"pstat" },
    179 	{ { CTL_KERN, KERN_NTPTIME },	printother,	NULL,
    180 	  "ntpdc -c kerninfo" },
    181 	{ { CTL_KERN, KERN_MSGBUF },	printother,	NULL,	"dmesg" },
    182 	{ { CTL_KERN, KERN_BOOTTIME },	kern_boottime },
    183 	{ { CTL_KERN, KERN_CONSDEV },	kern_consdev },
    184 	{ { CTL_KERN, KERN_CP_TIME, -1 }, kern_cp_time },
    185 	{ { CTL_KERN, KERN_CP_TIME },	kern_cp_time },
    186 	{ { CTL_KERN, KERN_SYSVIPC_INFO }, printother,	NULL,	"ipcs" },
    187 	{ { CTL_VM,   VM_METER },	printother,	NULL,
    188 	  "vmstat' or 'systat" },
    189 	{ { CTL_VM,   VM_LOADAVG },	vm_loadavg },
    190 	{ { CTL_VM,   VM_UVMEXP },	printother,	NULL,
    191 	  "vmstat' or 'systat" },
    192 	{ { CTL_VM,   VM_UVMEXP2 },	printother,	NULL,
    193 	  "vmstat' or 'systat" },
    194 	{ { CTL_VFS,  2 /* NFS */, NFS_NFSSTATS },
    195 					printother,	NULL,	"nfsstat" },
    196 	{ { CTL_NET },			printother,	NULL,	NULL },
    197 	{ { CTL_NET, PF_LOCAL },	printother,	NULL,	NULL },
    198 	{ { CTL_NET, PF_INET, IPPROTO_TCP, TCPCTL_IDENT },
    199 					printother,	NULL,	"identd" },
    200 	{ { CTL_NET, PF_INET6, IPPROTO_TCP, TCPCTL_IDENT },
    201 					printother,	NULL,	"identd" },
    202 
    203 	{ { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_DRLIST },
    204 					printother,	NULL,	"ndp" },
    205 	{ { CTL_NET, PF_INET6, IPPROTO_ICMPV6, ICMPV6CTL_ND6_PRLIST },
    206 					printother,	NULL,	"ndp" },
    207 
    208 
    209 	{ { CTL_NET, PF_KEY, KEYCTL_DUMPSA },
    210 					printother,	NULL,	"setkey" },
    211 	{ { CTL_NET, PF_KEY, KEYCTL_DUMPSP },
    212 					printother,	NULL,	"setkey" },
    213 	/* { { CTL_DEBUG },		printother,	NULL,	NULL }, */
    214 	{ { CTL_HW,   HW_DISKSTATS },	printother,	NULL,	"iostat" },
    215 #ifdef CPU_CONSDEV
    216 	{ { CTL_MACHDEP, CPU_CONSDEV },	kern_consdev },
    217 #endif /* CPU_CONSDEV */
    218 #ifdef CPU_DISKINFO
    219 	{ { CTL_MACHDEP, CPU_DISKINFO },machdep_diskinfo },
    220 #endif /* CPU_CONSDEV */
    221 	{ { CTL_DDB },			printother,	NULL,	NULL },
    222 	{ { CTL_PROC, -1, PROC_PID_LIMIT, -1, -1 }, proc_limit, proc_limit },
    223 	{ { CTL_UNSPEC }, },
    224 };
    225 
    226 struct sysctlnode my_root = {
    227 #if defined(lint)
    228 	0
    229 #else /* defined(lint) */
    230 	.sysctl_flags = SYSCTL_VERSION|CTLFLAG_ROOT|CTLTYPE_NODE,
    231 	sysc_init_field(_sysctl_size, sizeof(struct sysctlnode)),
    232 	.sysctl_num = 0,
    233 	.sysctl_name = "(prog_root)",
    234 #endif /* defined(lint) */
    235 };
    236 
    237 int	Aflag, aflag, dflag, Mflag, nflag, qflag, rflag, wflag, xflag;
    238 int	req;
    239 FILE	*warnfp = stderr;
    240 
    241 /*
    242  * vah-riables n stuff
    243  */
    244 char gsname[SYSCTL_NAMELEN * CTL_MAXNAME + CTL_MAXNAME],
    245 	gdname[10 * CTL_MAXNAME + CTL_MAXNAME];
    246 char sep[2] = ".", *eq = " = ";
    247 const char *lname[] = {
    248 	"top", "second", "third", "fourth", "fifth", "sixth",
    249 	"seventh", "eighth", "ninth", "tenth", "eleventh", "twelfth"
    250 };
    251 
    252 /*
    253  * you've heard of main, haven't you?
    254  */
    255 int
    256 main(int argc, char *argv[])
    257 {
    258 	char *fn = NULL;
    259 	int name[CTL_MAXNAME];
    260 	int ch;
    261 
    262 	while ((ch = getopt(argc, argv, "Aabdef:Mnqrwx")) != -1) {
    263 		switch (ch) {
    264 		case 'A':
    265 			Aflag++;
    266 			break;
    267 		case 'a':
    268 			aflag++;
    269 			break;
    270 		case 'd':
    271 			dflag++;
    272 			break;
    273 		case 'e':
    274 			eq = "=";
    275 			break;
    276 		case 'f':
    277 			fn = optarg;
    278 			wflag++;
    279 			break;
    280 		case 'M':
    281 			Mflag++;
    282 			break;
    283 		case 'n':
    284 			nflag++;
    285 			break;
    286 		case 'q':
    287 			qflag++;
    288 			break;
    289 		case 'b':	/* FreeBSD compat */
    290 		case 'r':
    291 			rflag++;
    292 			break;
    293 		case 'w':
    294 			wflag++;
    295 			break;
    296 		case 'x':
    297 			xflag++;
    298 			break;
    299 		default:
    300 			usage();
    301 		}
    302 	}
    303 
    304 	argc -= optind;
    305 	argv += optind;
    306 
    307 	if (qflag && !wflag)
    308 		usage();
    309 	if (xflag && rflag)
    310 		usage();
    311 	/* if ((xflag || rflag) && wflag)
    312 		usage(); */
    313 	/* if (aflag && Mflag)
    314 		usage(); */
    315 	if ((Aflag || Mflag || dflag) && argc == 0 && fn == NULL)
    316 		aflag = 1;
    317 
    318 	if (Aflag)
    319 		warnfp = stdout;
    320 	req = 0;
    321 
    322 	if (aflag) {
    323 		print_tree(&name[0], 0, NULL, CTLTYPE_NODE, 1);
    324 		/* if (argc == 0) */
    325 		return (0);
    326 	}
    327 
    328 	if (fn) {
    329 		FILE *fp;
    330 		char *l;
    331 
    332 		fp = fopen(fn, "r");
    333 		if (fp == NULL) {
    334 			err(1, "%s", fn);
    335 		} else {
    336 			while ((l = fparseln(fp, NULL, NULL, NULL, 0)) != NULL)
    337 			{
    338 				if (*l) {
    339 					parse(l);
    340 					free(l);
    341 				}
    342 			}
    343 			fclose(fp);
    344 		}
    345 		return (0);
    346 	}
    347 
    348 	if (argc == 0)
    349 		usage();
    350 
    351 	while (argc-- > 0)
    352 		parse(*argv++);
    353 
    354 	return (0);
    355 }
    356 
    357 /*
    358  * ********************************************************************
    359  * how to find someone special to handle the reading (or maybe even
    360  * writing) of a particular node
    361  * ********************************************************************
    362  */
    363 static struct handlespec *
    364 findprinter(const int *name, u_int namelen)
    365 {
    366 	struct handlespec *p;
    367 	int i, j;
    368 
    369 	if (namelen < 1)
    370 		return (NULL);
    371 
    372 	p = &handlers[0];
    373 	for (i = 0; p[i].ps_name[0] != CTL_UNSPEC; i++) {
    374 		for (j = 0; j < namelen; j++)
    375 			if (p[i].ps_name[j] != name[j] &&
    376 			    p[i].ps_name[j] != -1)
    377 				break;
    378 		if (j == namelen && p[i].ps_p != NULL)
    379 			return (&p[i]);
    380 	}
    381 
    382 	return (NULL);
    383 }
    384 
    385 static struct handlespec *
    386 findwriter(const int *name, u_int namelen)
    387 {
    388 	struct handlespec *p;
    389 	int i, j;
    390 
    391         if (namelen < 1)
    392                 return (NULL);
    393 
    394 	p = &handlers[0];
    395 	for (i = 0; p[i].ps_name[0] != CTL_UNSPEC; i++) {
    396 		for (j = 0; j < namelen; j++)
    397 			if (p[i].ps_name[j] != name[j] &&
    398 			    p[i].ps_name[j] != -1)
    399 				break;
    400 		if (j == namelen && p[i].ps_w != NULL)
    401 			return (&p[i]);
    402 	}
    403 
    404 	return (NULL);
    405 }
    406 
    407 /*
    408  * ********************************************************************
    409  * convert this special number to a special string so we can print the
    410  * mib
    411  * ********************************************************************
    412  */
    413 static const char *
    414 sf(u_int f)
    415 {
    416 	static char s[256];
    417 	char *c;
    418 
    419 	s[0] = '\0';
    420 	c = "";
    421 
    422 #define print_flag(_f, _s, _c, _q, _x) \
    423 	if (((_f) & (__CONCAT(CTLFLAG_,_x))) == (__CONCAT(CTLFLAG_,_q))) { \
    424 		strlcat((_s), (_c), sizeof(_s)); \
    425 		strlcat((_s), __STRING(_q), sizeof(_s)); \
    426 		(_c) = ","; \
    427 		(_f) &= ~(__CONCAT(CTLFLAG_,_x)); \
    428 	}
    429 	print_flag(f, s, c, READONLY,  READWRITE);
    430 	print_flag(f, s, c, READONLY1, READWRITE);
    431 	print_flag(f, s, c, READONLY2, READWRITE);
    432 	print_flag(f, s, c, READWRITE, READWRITE);
    433 	print_flag(f, s, c, ANYWRITE,  ANYWRITE);
    434 	print_flag(f, s, c, PRIVATE,   PRIVATE);
    435 	print_flag(f, s, c, PERMANENT, PERMANENT);
    436 	print_flag(f, s, c, OWNDATA,   OWNDATA);
    437 	print_flag(f, s, c, IMMEDIATE, IMMEDIATE);
    438 	print_flag(f, s, c, HEX,       HEX);
    439 	print_flag(f, s, c, ROOT,      ROOT);
    440 	print_flag(f, s, c, ANYNUMBER, ANYNUMBER);
    441 	print_flag(f, s, c, HIDDEN,    HIDDEN);
    442 	print_flag(f, s, c, ALIAS,     ALIAS);
    443 #undef print_flag
    444 
    445 	if (f) {
    446 		char foo[9];
    447 		snprintf(foo, sizeof(foo), "%x", f);
    448 		strlcat(s, c, sizeof(s));
    449 		strlcat(s, foo, sizeof(s));
    450 	}
    451 
    452 	return (s);
    453 }
    454 
    455 static const char *
    456 st(u_int t)
    457 {
    458 
    459 	switch (t) {
    460 	case CTLTYPE_NODE:
    461 		return "NODE";
    462 	case CTLTYPE_INT:
    463 		return "INT";
    464 	case CTLTYPE_STRING:
    465 		return "STRING";
    466 	case CTLTYPE_QUAD:
    467                 return "QUAD";
    468 	case CTLTYPE_STRUCT:
    469 		return "STRUCT";
    470 	}
    471 
    472 	return "???";
    473 }
    474 
    475 /*
    476  * ********************************************************************
    477  * print this node and any others underneath it
    478  * ********************************************************************
    479  */
    480 static void
    481 print_tree(int *name, u_int namelen, struct sysctlnode *pnode, u_int type,
    482 	   int add)
    483 {
    484 	struct sysctlnode *node;
    485 	int rc, ni;
    486 	size_t sz;
    487 	char *sp, *dp, n[20];
    488 	struct handlespec *p;
    489 
    490 	sp = &gsname[strlen(gsname)];
    491 	dp = &gdname[strlen(gdname)];
    492 
    493 	if (sp != &gsname[0] && dp == &gdname[0]) {
    494 		/*
    495 		 * aw...shucks.  now we must play catch up
    496 		 */
    497 		for (ni = 0; ni < namelen; ni++) {
    498 			(void)snprintf(n, sizeof(n), "%d", name[ni]);
    499 			if (ni > 0)
    500 				strncat(gdname, ".", sizeof(gdname));
    501 			strncat(gdname, n, sizeof(gdname));
    502 		}
    503 	}
    504 
    505 	if (pnode == NULL)
    506 		pnode = &my_root;
    507 	else if (add) {
    508 		snprintf(n, sizeof(n), "%d", pnode->sysctl_num);
    509 		if (namelen > 1) {
    510 			strncat(gsname, sep, sizeof(gsname));
    511 			strncat(gdname, ".", sizeof(gdname));
    512 		}
    513 		strncat(gsname, pnode->sysctl_name, sizeof(gsname));
    514 		strncat(gdname, n, sizeof(gdname));
    515 	}
    516 
    517 	if (Mflag && pnode != &my_root) {
    518 		if (nflag)
    519 			printf("%s: ", gdname);
    520 		else
    521 			printf("%s (%s): ", gsname, gdname);
    522 		printf("CTLTYPE_%s", st(type));
    523 		if (type == CTLTYPE_NODE) {
    524 			if (SYSCTL_FLAGS(pnode->sysctl_flags) & CTLFLAG_ALIAS)
    525 				printf(", alias %d",
    526 				       pnode->sysctl_alias);
    527 			else
    528 				printf(", children %d/%d",
    529 				       pnode->sysctl_clen,
    530 				       pnode->sysctl_csize);
    531 		}
    532 		printf(", size %zu", pnode->sysctl_size);
    533 		printf(", flags 0x%x<%s>",
    534 		       SYSCTL_FLAGS(pnode->sysctl_flags),
    535 		       sf(SYSCTL_FLAGS(pnode->sysctl_flags)));
    536 		if (pnode->sysctl_func)
    537 			printf(", func=%p", pnode->sysctl_func);
    538 		printf(", ver=%d", pnode->sysctl_ver);
    539 		printf("\n");
    540 		if (type != CTLTYPE_NODE) {
    541 			*sp = *dp = '\0';
    542 			return;
    543 		}
    544 	}
    545 
    546 	if (dflag && pnode != &my_root) {
    547 		if (Aflag || type != CTLTYPE_NODE) {
    548 			if (pnode->sysctl_desc == NULL)
    549 				getdesc1(name, namelen, pnode);
    550 			if (Aflag ||
    551 			    (pnode->sysctl_desc != NULL &&
    552 			     pnode->sysctl_desc != (const char*)-1)) {
    553 				if (!nflag)
    554 					printf("%s: ", gsname);
    555 				if (pnode->sysctl_desc == NULL ||
    556 				    pnode->sysctl_desc == (const char*)-1)
    557 					printf("(no description)\n");
    558 				else
    559 					printf("%s\n", pnode->sysctl_desc);
    560 			}
    561 		}
    562 
    563 		if (type != CTLTYPE_NODE) {
    564 			*sp = *dp = '\0';
    565 			return;
    566 		}
    567 	}
    568 
    569 	/*
    570 	 * if this is an alias and we added our name, that means we
    571 	 * got here by recursing down into the tree, so skip it.  The
    572 	 * only way to print an aliased node is with either -M or by
    573 	 * name specifically.
    574 	 */
    575 	if (SYSCTL_FLAGS(pnode->sysctl_flags) & CTLFLAG_ALIAS && add) {
    576 		*sp = *dp = '\0';
    577 		return;
    578 	}
    579 
    580 	p = findprinter(name, namelen);
    581 	if (type != CTLTYPE_NODE && p != NULL) {
    582 		(*p->ps_p)(gsname, gdname, NULL, name, namelen, pnode, type,
    583 			   p->ps_d);
    584 		*sp = *dp = '\0';
    585 		return;
    586 	}
    587 
    588 	if (type != CTLTYPE_NODE && pnode->sysctl_size == 0) {
    589 		rc = sysctl(&name[0], namelen, NULL, &sz, NULL, 0);
    590 		if (rc == -1) {
    591 			sysctlerror(1);
    592 			*sp = *dp = '\0';
    593 			return;
    594 		}
    595 		if (sz == 0) {
    596 			if ((Aflag || req) && !Mflag)
    597 				printf("%s: node contains no data\n", gsname);
    598 			*sp = *dp = '\0';
    599 			return;
    600 		}
    601 	}
    602 	else
    603 		sz = pnode->sysctl_size;
    604 
    605 	switch (type) {
    606 	case CTLTYPE_NODE: {
    607 		__learn_tree(name, namelen, pnode);
    608 		node = pnode->sysctl_child;
    609 		if (node == NULL) {
    610 			if (dflag)
    611 				/* do nothing */;
    612 			else if (p != NULL)
    613 				(*p->ps_p)(gsname, gdname, NULL, name, namelen,
    614 					   pnode, type, p->ps_d);
    615 			else if ((Aflag || req) && !Mflag)
    616 				printf("%s: no children\n", gsname);
    617 		}
    618 		else {
    619 			if (dflag)
    620 				/*
    621 				 * get all descriptions for next level
    622 				 * in one chunk
    623 				 */
    624 				getdesc(name, namelen, pnode);
    625 			req = 0;
    626 			for (ni = 0; ni < pnode->sysctl_clen; ni++) {
    627 				name[namelen] = node[ni].sysctl_num;
    628 				if ((node[ni].sysctl_flags & CTLFLAG_HIDDEN) &&
    629 				    !(Aflag || req))
    630 					continue;
    631 				print_tree(name, namelen + 1, &node[ni],
    632 					   SYSCTL_TYPE(node[ni].sysctl_flags),
    633 					   1);
    634 			}
    635 		}
    636 		break;
    637 	}
    638 	case CTLTYPE_INT: {
    639 		int i;
    640 		rc = sysctl(name, namelen, &i, &sz, NULL, 0);
    641 		if (rc == -1) {
    642 			sysctlerror(1);
    643 			break;
    644 		}
    645 		display_number(pnode, gsname, &i, sizeof(i), DISPLAY_VALUE);
    646 		break;
    647 	}
    648 	case CTLTYPE_STRING: {
    649 		unsigned char buf[1024], *tbuf;
    650 		tbuf = buf;
    651 		sz = sizeof(buf);
    652 		rc = sysctl(&name[0], namelen, tbuf, &sz, NULL, 0);
    653 		if (rc == -1 && errno == ENOMEM) {
    654 			tbuf = malloc(sz);
    655 			if (tbuf == NULL) {
    656 				sysctlerror(1);
    657 				break;
    658 			}
    659 			rc = sysctl(&name[0], namelen, tbuf, &sz, NULL, 0);
    660 		}
    661 		if (rc == -1)
    662 			sysctlerror(1);
    663 		else
    664 			display_string(pnode, gsname, buf, sz, DISPLAY_VALUE);
    665 		if (tbuf != buf)
    666 			free(tbuf);
    667 		break;
    668 	}
    669 	case CTLTYPE_QUAD: {
    670 		u_quad_t q;
    671 		sz = sizeof(q);
    672 		rc = sysctl(&name[0], namelen, &q, &sz, NULL, 0);
    673 		if (rc == -1) {
    674 			sysctlerror(1);
    675 			break;
    676 		}
    677 		display_number(pnode, gsname, &q, sizeof(q), DISPLAY_VALUE);
    678 		break;
    679 	}
    680 	case CTLTYPE_STRUCT: {
    681 		/*
    682 		 * we shouldn't actually get here, but if we
    683 		 * do, would it be nice to have *something* to
    684 		 * do other than completely ignore the
    685 		 * request.
    686 		 */
    687 		unsigned char *d;
    688 		if ((d = malloc(sz)) == NULL) {
    689 			fprintf(warnfp, "%s: !malloc failed!\n", gsname);
    690 			break;
    691 		}
    692 		rc = sysctl(&name[0], namelen, d, &sz, NULL, 0);
    693 		if (rc == -1) {
    694 			sysctlerror(1);
    695 			break;
    696 		}
    697 		display_struct(pnode, gsname, d, sz, DISPLAY_VALUE);
    698 		free(d);
    699 		break;
    700 	}
    701 	default:
    702 		/* should i print an error here? */
    703 		break;
    704 	}
    705 
    706 	*sp = *dp = '\0';
    707 }
    708 
    709 /*
    710  * ********************************************************************
    711  * parse a request, possibly determining that it's a create or destroy
    712  * request
    713  * ********************************************************************
    714  */
    715 static void
    716 parse(char *l)
    717 {
    718 	struct sysctlnode *node;
    719 	struct handlespec *w;
    720 	int name[CTL_MAXNAME], dodesc = 0;
    721 	u_int namelen, type;
    722 	char *key, *value, *dot;
    723 	size_t sz;
    724 
    725 	req = 1;
    726 	key = l;
    727 	value = strchr(l, '=');
    728 	if (value != NULL)
    729 		*value++ = '\0';
    730 
    731 	if ((dot = strpbrk(key, "./")) == NULL)
    732 		sep[0] = '.';
    733 	else
    734 		sep[0] = dot[0];
    735 	sep[1] = '\0';
    736 
    737 	while (key[0] == sep[0] && key[1] == sep[0]) {
    738 		if (value != NULL)
    739 			value[-1] = '=';
    740 		if (strncmp(key + 2, "create", 6) == 0 &&
    741 		    (key[8] == '=' || key[8] == sep[0]))
    742 			parse_create(key + 8 + (key[8] == '='));
    743 		else if (strncmp(key + 2, "destroy", 7) == 0 &&
    744 			 (key[9] == '=' || key[9] == sep[0]))
    745 			parse_destroy(key + 9 + (key[9] == '='));
    746 		else if (strncmp(key + 2, "describe", 8) == 0 &&
    747 			 (key[10] == '=' || key[10] == sep[0])) {
    748 			key += 10 + (key[10] == '=');
    749 			if ((value = strchr(key, '=')) != NULL)
    750 				parse_describe(key);
    751 			else {
    752 				if (!dflag)
    753 					dodesc = 1;
    754 				break;
    755 			}
    756 		}
    757 		else
    758 			fprintf(warnfp, "%s: unable to parse '%s'\n",
    759 				getprogname(), key);
    760 		return;
    761 	}
    762 
    763 	node = &my_root;
    764 	namelen = CTL_MAXNAME;
    765 	sz = sizeof(gsname);
    766 
    767 	if (sysctlgetmibinfo(key, &name[0], &namelen, gsname, &sz, &node,
    768 			     SYSCTL_VERSION) == -1) {
    769 		fprintf(warnfp, "%s: %s level name '%s' in '%s' is invalid\n",
    770 			getprogname(), lname[namelen], gsname, l);
    771 		exit(1);
    772 	}
    773 
    774 	type = SYSCTL_TYPE(node->sysctl_flags);
    775 
    776 	if (value == NULL) {
    777 		if (dodesc)
    778 			dflag = 1;
    779 		print_tree(&name[0], namelen, node, type, 0);
    780 		if (dodesc)
    781 			dflag = 0;
    782 		gsname[0] = '\0';
    783 		return;
    784 	}
    785 
    786 	if (!wflag) {
    787 		fprintf(warnfp, "%s: Must specify -w to set variables\n",
    788 			getprogname());
    789 		exit(1);
    790 	}
    791 
    792 	if (type != CTLTYPE_NODE && (w = findwriter(name, namelen)) != NULL) {
    793 		(*w->ps_w)(gsname, gdname, value, name, namelen, node, type,
    794 			   w->ps_d);
    795 		gsname[0] = '\0';
    796 		return;
    797 	}
    798 
    799 	switch (type) {
    800 	case CTLTYPE_NODE:
    801 		/*
    802 		 * XXX old behavior is to print.  should we error instead?
    803 		 */
    804 		print_tree(&name[0], namelen, node, CTLTYPE_NODE, 1);
    805 		break;
    806 	case CTLTYPE_INT:
    807 		write_number(&name[0], namelen, node, value);
    808 		break;
    809 	case CTLTYPE_STRING:
    810 		write_string(&name[0], namelen, node, value);
    811 		break;
    812 	case CTLTYPE_QUAD:
    813 		write_number(&name[0], namelen, node, value);
    814 		break;
    815 	case CTLTYPE_STRUCT:
    816 		/*
    817 		 * XXX old behavior is to print.  should we error instead?
    818 		 */
    819 		/* fprintf(warnfp, "you can't write to %s\n", gsname); */
    820 		print_tree(&name[0], namelen, node, type, 0);
    821 		break;
    822 	}
    823 }
    824 
    825 /*
    826 
    827   //create=foo.bar.whatever...,
    828   [type=(int|quad|string|struct|node),]
    829   [size=###,]
    830   [n=###,]
    831   [flags=(iohxparw12),]
    832   [addr=0x####,|symbol=...|value=...]
    833 
    834   size is optional for some types.  type must be set before anything
    835   else.  nodes can have [r12whp], but nothing else applies.  if no
    836   size or type is given, node is asserted.  writeable is the default,
    837   with [r12w] being read-only, writeable below securelevel 1,
    838   writeable below securelevel 2, and unconditionally writeable
    839   respectively.  if you specify addr, it is assumed to be the name of
    840   a kernel symbol, if value, CTLFLAG_OWNDATA will be asserted for
    841   strings, CTLFLAG_IMMEDIATE for ints and u_quad_ts.  you cannot
    842   specify both value and addr.
    843 
    844 */
    845 
    846 static void
    847 parse_create(char *l)
    848 {
    849 	struct sysctlnode node;
    850 	size_t sz;
    851 	char *nname, *key, *value, *data, *addr, *c, *t;
    852 	int name[CTL_MAXNAME], i, rc, method, flags, rw;
    853 	u_int namelen, type;
    854 	u_quad_t q;
    855 	long li, lo;
    856 
    857 	if (!wflag) {
    858 		fprintf(warnfp, "%s: Must specify -w to create nodes\n",
    859 			getprogname());
    860 		exit(1);
    861 	}
    862 
    863 	/*
    864 	 * these are the pieces that make up the description of a new
    865 	 * node
    866 	 */
    867 	memset(&node, 0, sizeof(node));
    868 	node.sysctl_num = CTL_CREATE;  /* any number is fine */
    869 	flags = 0;
    870 	rw = -1;
    871 	type = 0;
    872 	sz = 0;
    873 	data = addr = NULL;
    874 	memset(name, 0, sizeof(name));
    875 	namelen = 0;
    876 	method = 0;
    877 
    878 	/*
    879 	 * misc stuff used when constructing
    880 	 */
    881 	i = 0;
    882 	q = 0;
    883 	key = NULL;
    884 	value = NULL;
    885 
    886 	/*
    887 	 * the name of the thing we're trying to create is first, so
    888 	 * pick it off.
    889 	 */
    890 	nname = l;
    891 	if ((c = strchr(nname, ',')) != NULL)
    892 		*c++ = '\0';
    893 
    894 	while (c != NULL) {
    895 
    896 		/*
    897 		 * pull off the next "key=value" pair
    898 		 */
    899 		key = c;
    900 		if ((t = strchr(key, '=')) != NULL) {
    901 			*t++ = '\0';
    902 			value = t;
    903 		}
    904 		else
    905 			value = NULL;
    906 
    907 		/*
    908 		 * if the "key" is "value", then that eats the rest of
    909 		 * the string, so we're done, otherwise bite it off at
    910 		 * the next comma.
    911 		 */
    912 		if (strcmp(key, "value") == 0) {
    913 			c = NULL;
    914 			data = value;
    915 			break;
    916 		}
    917 		else {
    918 			if ((c = strchr(value, ',')) != NULL)
    919 				*c++ = '\0';
    920 		}
    921 
    922 		/*
    923 		 * note that we (mostly) let the invoker of sysctl(8)
    924 		 * play rampant here and depend on the kernel to tell
    925 		 * them that they were wrong.  well...within reason.
    926 		 * we later check the various parameters against each
    927 		 * other to make sure it makes some sort of sense.
    928 		 */
    929 		if (strcmp(key, "addr") == 0) {
    930 			/*
    931 			 * we can't check these two.  only the kernel
    932 			 * can tell us when it fails to find the name
    933 			 * (or if the address is invalid).
    934 			 */
    935 			if (method != 0) {
    936 				fprintf(warnfp,
    937 				    "%s: %s: already have %s for new node\n",
    938 				    getprogname(), nname,
    939 				    method == CTL_CREATE ? "addr" : "symbol");
    940 				exit(1);
    941 			}
    942 			errno = 0;
    943 			addr = (void*)strtoul(value, &t, 0);
    944 			if (*t != '\0' || errno != 0) {
    945 				fprintf(warnfp,
    946 				    "%s: %s: '%s' is not a valid address\n",
    947 				    getprogname(), nname, value);
    948 				exit(1);
    949 			}
    950 			method = CTL_CREATE;
    951 		}
    952 		else if (strcmp(key, "symbol") == 0) {
    953 			if (method != 0) {
    954 				fprintf(warnfp,
    955 				    "%s: %s: already have %s for new node\n",
    956 				    getprogname(), nname,
    957 				    method == CTL_CREATE ? "addr" : "symbol");
    958 				exit(1);
    959 			}
    960 			addr = value;
    961 			method = CTL_CREATESYM;
    962 		}
    963 		else if (strcmp(key, "type") == 0) {
    964 			if (strcmp(value, "node") == 0)
    965 				type = CTLTYPE_NODE;
    966 			else if (strcmp(value, "int") == 0) {
    967 				sz = sizeof(int);
    968 				type = CTLTYPE_INT;
    969 			}
    970 			else if (strcmp(value, "string") == 0)
    971 				type = CTLTYPE_STRING;
    972 			else if (strcmp(value, "quad") == 0) {
    973 				sz = sizeof(u_quad_t);
    974 				type = CTLTYPE_QUAD;
    975 			}
    976 			else if (strcmp(value, "struct") == 0)
    977 				type = CTLTYPE_STRUCT;
    978 			else {
    979 				fprintf(warnfp,
    980 					"%s: %s: '%s' is not a valid type\n",
    981 					getprogname(), nname, value);
    982 				exit(1);
    983 			}
    984 		}
    985 		else if (strcmp(key, "size") == 0) {
    986 			errno = 0;
    987 			/*
    988 			 * yes, i know size_t is not an unsigned long,
    989 			 * but we can all agree that it ought to be,
    990 			 * right?
    991 			 */
    992 			sz = strtoul(value, &t, 0);
    993 			if (*t != '\0' || errno != 0) {
    994 				fprintf(warnfp,
    995 					"%s: %s: '%s' is not a valid size\n",
    996 					getprogname(), nname, value);
    997 				exit(1);
    998 			}
    999 		}
   1000 		else if (strcmp(key, "n") == 0) {
   1001 			errno = 0;
   1002 			li = strtol(value, &t, 0);
   1003 			node.sysctl_num = li;
   1004 			lo = node.sysctl_num;
   1005 			if (*t != '\0' || errno != 0 || li != lo || lo < 0) {
   1006 				fprintf(warnfp,
   1007 				    "%s: %s: '%s' is not a valid mib number\n",
   1008 				    getprogname(), nname, value);
   1009 				exit(1);
   1010 			}
   1011 		}
   1012 		else if (strcmp(key, "flags") == 0) {
   1013 			t = value;
   1014 			while (*t != '\0') {
   1015 				switch (*t) {
   1016 				case 'a':
   1017 					flags |= CTLFLAG_ANYWRITE;
   1018 					break;
   1019 				case 'h':
   1020 					flags |= CTLFLAG_HIDDEN;
   1021 					break;
   1022 				case 'i':
   1023 					flags |= CTLFLAG_IMMEDIATE;
   1024 					break;
   1025 				case 'o':
   1026 					flags |= CTLFLAG_OWNDATA;
   1027 					break;
   1028 				case 'p':
   1029 					flags |= CTLFLAG_PRIVATE;
   1030 					break;
   1031 				case 'x':
   1032 					flags |= CTLFLAG_HEX;
   1033 					break;
   1034 
   1035 				case 'r':
   1036 					rw = CTLFLAG_READONLY;
   1037 					break;
   1038 				case '1':
   1039 					rw = CTLFLAG_READONLY1;
   1040 					break;
   1041 				case '2':
   1042 					rw = CTLFLAG_READONLY2;
   1043 					break;
   1044 				case 'w':
   1045 					rw = CTLFLAG_READWRITE;
   1046 					break;
   1047 				default:
   1048 					fprintf(warnfp,
   1049 					   "%s: %s: '%c' is not a valid flag\n",
   1050 					    getprogname(), nname, *t);
   1051 					exit(1);
   1052 				}
   1053 				t++;
   1054 			}
   1055 		}
   1056 		else {
   1057 			fprintf(warnfp, "%s: %s: unrecognized keyword '%s'\n",
   1058 				getprogname(), nname, key);
   1059 			exit(1);
   1060 		}
   1061 	}
   1062 
   1063 	/*
   1064 	 * now that we've finished parsing the given string, fill in
   1065 	 * anything they didn't specify
   1066 	 */
   1067 	if (type == 0)
   1068 		type = CTLTYPE_NODE;
   1069 
   1070 	/*
   1071 	 * the "data" can be interpreted various ways depending on the
   1072 	 * type of node we're creating, as can the size
   1073 	 */
   1074 	if (data != NULL) {
   1075 		if (addr != NULL) {
   1076 			fprintf(warnfp,
   1077 				"%s: %s: cannot specify both value and "
   1078 				"address\n", getprogname(), nname);
   1079 			exit(1);
   1080 		}
   1081 
   1082 		switch (type) {
   1083 		case CTLTYPE_INT:
   1084 			errno = 0;
   1085 			li = strtol(data, &t, 0);
   1086 			i = li;
   1087 			lo = i;
   1088 			if (*t != '\0' || errno != 0 || li != lo || lo < 0) {
   1089 				fprintf(warnfp,
   1090 					"%s: %s: '%s' is not a valid integer\n",
   1091 					getprogname(), nname, value);
   1092 				exit(1);
   1093 			}
   1094 			if (!(flags & CTLFLAG_OWNDATA)) {
   1095 				flags |= CTLFLAG_IMMEDIATE;
   1096 				node.sysctl_idata = i;
   1097 			}
   1098 			else
   1099 				node.sysctl_data = &i;
   1100 			if (sz == 0)
   1101 				sz = sizeof(int);
   1102 			break;
   1103 		case CTLTYPE_STRING:
   1104 			flags |= CTLFLAG_OWNDATA;
   1105 			node.sysctl_data = data;
   1106 			if (sz == 0)
   1107 				sz = strlen(data) + 1;
   1108 			else if (sz < strlen(data) + 1) {
   1109 				fprintf(warnfp, "%s: %s: ignoring size=%zu for "
   1110 					"string node, too small for given "
   1111 					"value\n", getprogname(), nname, sz);
   1112 				sz = strlen(data) + 1;
   1113 			}
   1114 			break;
   1115 		case CTLTYPE_QUAD:
   1116 			errno = 0;
   1117 			q = strtouq(data, &t, 0);
   1118 			if (*t != '\0' || errno != 0) {
   1119 				fprintf(warnfp,
   1120 					"%s: %s: '%s' is not a valid quad\n",
   1121 					getprogname(), nname, value);
   1122 				exit(1);
   1123 			}
   1124 			if (!(flags & CTLFLAG_OWNDATA)) {
   1125 				flags |= CTLFLAG_IMMEDIATE;
   1126 				node.sysctl_qdata = q;
   1127 			}
   1128 			else
   1129 				node.sysctl_data = &q;
   1130 			if (sz == 0)
   1131 				sz = sizeof(u_quad_t);
   1132 			break;
   1133 		case CTLTYPE_STRUCT:
   1134 			fprintf(warnfp,
   1135 				"%s: %s: struct not initializable\n",
   1136 				getprogname(), nname);
   1137 			exit(1);
   1138 		}
   1139 
   1140 		/*
   1141 		 * these methods have all provided local starting
   1142 		 * values that the kernel must copy in
   1143 		 */
   1144 	}
   1145 
   1146 	/*
   1147 	 * hmm...no data, but we have an address of data.  that's
   1148 	 * fine.
   1149 	 */
   1150 	else if (addr != 0)
   1151 		node.sysctl_data = (void*)addr;
   1152 
   1153 	/*
   1154 	 * no data and no address?  well...okay.  we might be able to
   1155 	 * manage that.
   1156 	 */
   1157 	else if (type != CTLTYPE_NODE) {
   1158 		if (sz == 0) {
   1159 			fprintf(warnfp,
   1160 				"%s: %s: need a size or a starting value\n",
   1161 				getprogname(), nname);
   1162                         exit(1);
   1163                 }
   1164 		if (!(flags & CTLFLAG_IMMEDIATE))
   1165 			flags |= CTLFLAG_OWNDATA;
   1166 	}
   1167 
   1168 	/*
   1169 	 * now we do a few sanity checks on the description we've
   1170 	 * assembled
   1171 	 */
   1172 	if ((flags & CTLFLAG_IMMEDIATE) &&
   1173 	    (type == CTLTYPE_STRING || type == CTLTYPE_STRUCT)) {
   1174 		fprintf(warnfp,
   1175 			"%s: %s: cannot make an immediate %s\n",
   1176 			getprogname(), nname,
   1177 			(type == CTLTYPE_STRING) ? "string" : "struct");
   1178 		exit(1);
   1179 	}
   1180 	if (type == CTLTYPE_NODE && node.sysctl_data != NULL) {
   1181 		fprintf(warnfp, "%s: %s: nodes do not have data\n",
   1182 			getprogname(), nname);
   1183 		exit(1);
   1184 	}
   1185 
   1186 	/*
   1187 	 * some types must have a particular size
   1188 	 */
   1189 	if (sz != 0) {
   1190 		if ((type == CTLTYPE_INT && sz != sizeof(int)) ||
   1191 		    (type == CTLTYPE_QUAD && sz != sizeof(u_quad_t)) ||
   1192 		    (type == CTLTYPE_NODE && sz != 0)) {
   1193 			fprintf(warnfp, "%s: %s: wrong size for type\n",
   1194 				getprogname(), nname);
   1195 			exit(1);
   1196 		}
   1197 	}
   1198 	else if (type == CTLTYPE_STRUCT) {
   1199 		fprintf(warnfp, "%s: %s: struct must have size\n",
   1200 			getprogname(), nname);
   1201 		exit(1);
   1202 	}
   1203 
   1204 	/*
   1205 	 * now...if no one said anything yet, we default nodes or
   1206 	 * any type that owns data being writeable, and everything
   1207 	 * else being readonly.
   1208 	 */
   1209 	if (rw == -1) {
   1210 		if (type == CTLTYPE_NODE ||
   1211 		    (flags & (CTLFLAG_OWNDATA|CTLFLAG_IMMEDIATE)))
   1212 			rw = CTLFLAG_READWRITE;
   1213 		else
   1214 			rw = CTLFLAG_READONLY;
   1215 	}
   1216 
   1217 	/*
   1218 	 * if a kernel address was specified, that can't be made
   1219 	 * writeable by us.
   1220 	if (rw != CTLFLAG_READONLY && addr) {
   1221 		fprintf(warnfp, "%s: %s: kernel data can only be readable\n",
   1222 			getprogname(), nname);
   1223 		exit(1);
   1224 	}
   1225 	 */
   1226 
   1227 	/*
   1228 	 * what separator were they using in the full name of the new
   1229 	 * node?
   1230 	 */
   1231 	if ((t = strpbrk(nname, "./")) == NULL)
   1232 		sep[0] = '.';
   1233 	else
   1234 		sep[0] = t[0];
   1235 	sep[1] = '\0';
   1236 
   1237 	/*
   1238 	 * put it all together, now.  t'ain't much, is it?
   1239 	 */
   1240 	node.sysctl_flags = SYSCTL_VERSION|flags|rw|type;
   1241 	node.sysctl_size = sz;
   1242 	t = strrchr(nname, sep[0]);
   1243 	if (t != NULL)
   1244 		strlcpy(node.sysctl_name, t + 1, sizeof(node.sysctl_name));
   1245 	else
   1246 		strlcpy(node.sysctl_name, nname, sizeof(node.sysctl_name));
   1247 	if (t == nname)
   1248 		t = NULL;
   1249 
   1250 	/*
   1251 	 * if this is a new top-level node, then we don't need to find
   1252 	 * the mib for its parent
   1253 	 */
   1254 	if (t == NULL) {
   1255 		namelen = 0;
   1256 		gsname[0] = '\0';
   1257 	}
   1258 
   1259 	/*
   1260 	 * on the other hand, if it's not a top-level node...
   1261 	 */
   1262 	else {
   1263 		namelen = sizeof(name) / sizeof(name[0]);
   1264 		sz = sizeof(gsname);
   1265 		*t = '\0';
   1266 		rc = sysctlgetmibinfo(nname, &name[0], &namelen,
   1267 				      gsname, &sz, NULL, SYSCTL_VERSION);
   1268 		*t = sep[0];
   1269 		if (rc == -1) {
   1270 			fprintf(warnfp,
   1271 				"%s: %s level name '%s' in '%s' is invalid\n",
   1272 				getprogname(), lname[namelen], gsname, nname);
   1273 			exit(1);
   1274 		}
   1275 	}
   1276 
   1277 	/*
   1278 	 * yes, a new node is being created
   1279 	 */
   1280 	if (method != 0)
   1281 		name[namelen++] = method;
   1282 	else
   1283 		name[namelen++] = CTL_CREATE;
   1284 
   1285 	sz = sizeof(node);
   1286 	rc = sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node));
   1287 
   1288 	if (rc == -1) {
   1289 		fprintf(warnfp,
   1290 			"%s: %s: CTL_CREATE failed: %s\n",
   1291 			getprogname(), nname, strerror(errno));
   1292 		exit(1);
   1293 	}
   1294 	else if (!qflag && !nflag)
   1295 		printf("%s(%s): (created)\n", nname, st(type));
   1296 }
   1297 
   1298 static void
   1299 parse_destroy(char *l)
   1300 {
   1301 	struct sysctlnode node;
   1302 	size_t sz;
   1303 	int name[CTL_MAXNAME], rc;
   1304 	u_int namelen;
   1305 
   1306 	if (!wflag) {
   1307 		fprintf(warnfp, "%s: Must specify -w to destroy nodes\n",
   1308 			getprogname());
   1309 		exit(1);
   1310 	}
   1311 
   1312 	memset(name, 0, sizeof(name));
   1313 	namelen = sizeof(name) / sizeof(name[0]);
   1314 	sz = sizeof(gsname);
   1315 	rc = sysctlgetmibinfo(l, &name[0], &namelen, gsname, &sz, NULL,
   1316 			      SYSCTL_VERSION);
   1317 	if (rc == -1) {
   1318 		fprintf(warnfp,
   1319 			"%s: %s level name '%s' in '%s' is invalid\n",
   1320 			getprogname(), lname[namelen], gsname, l);
   1321 		exit(1);
   1322 	}
   1323 
   1324 	memset(&node, 0, sizeof(node));
   1325 	node.sysctl_flags = SYSCTL_VERSION;
   1326 	node.sysctl_num = name[namelen - 1];
   1327 	name[namelen - 1] = CTL_DESTROY;
   1328 
   1329 	sz = sizeof(node);
   1330 	rc = sysctl(&name[0], namelen, &node, &sz, &node, sizeof(node));
   1331 
   1332 	if (rc == -1) {
   1333 		fprintf(warnfp,
   1334 			"%s: %s: CTL_DESTROY failed: %s\n",
   1335 			getprogname(), l, strerror(errno));
   1336 		exit(1);
   1337 	}
   1338 	else if (!qflag && !nflag)
   1339 		printf("%s(%s): (destroyed)\n", gsname,
   1340 		       st(SYSCTL_TYPE(node.sysctl_flags)));
   1341 }
   1342 
   1343 static void
   1344 parse_describe(char *l)
   1345 {
   1346 	struct sysctlnode newdesc;
   1347 	char buf[1024], *value;
   1348 	struct sysctldesc *d = (void*)&buf[0];
   1349 	int name[CTL_MAXNAME], rc;
   1350 	u_int namelen;
   1351 	size_t sz;
   1352 
   1353 	if (!wflag) {
   1354 		fprintf(warnfp,
   1355 			"%s: Must specify -w to set descriptions\n",
   1356 			getprogname());
   1357 		exit(1);
   1358 	}
   1359 
   1360 	value = strchr(l, '=');
   1361 	*value++ = '\0';
   1362 
   1363 	memset(name, 0, sizeof(name));
   1364 	namelen = sizeof(name) / sizeof(name[0]);
   1365 	sz = sizeof(gsname);
   1366 	rc = sysctlgetmibinfo(l, &name[0], &namelen, gsname, &sz, NULL,
   1367 			      SYSCTL_VERSION);
   1368 	if (rc == -1) {
   1369 		fprintf(warnfp,
   1370 			"%s: %s level name '%s' in '%s' is invalid\n",
   1371 			getprogname(), lname[namelen], gsname, l);
   1372 		exit(1);
   1373 	}
   1374 
   1375 	sz = sizeof(buf);
   1376 	memset(&newdesc, 0, sizeof(newdesc));
   1377 	newdesc.sysctl_flags = SYSCTL_VERSION|CTLFLAG_OWNDESC;
   1378 	newdesc.sysctl_num = name[namelen - 1];
   1379 	newdesc.sysctl_desc = value;
   1380 	name[namelen - 1] = CTL_DESCRIBE;
   1381 	rc = sysctl(name, namelen, d, &sz, &newdesc, sizeof(newdesc));
   1382 	if (rc == -1)
   1383 		fprintf(warnfp, "%s: %s: CTL_DESCRIBE failed: %s\n",
   1384 			getprogname(), gsname, strerror(errno));
   1385 	else if (d->descr_len == 1)
   1386 		fprintf(warnfp, "%s: %s: description not set\n",
   1387 			getprogname(), gsname);
   1388 	else if (!qflag && !nflag)
   1389 		printf("%s: %s\n", gsname, d->descr_str);
   1390 }
   1391 
   1392 /*
   1393  * ********************************************************************
   1394  * when things go wrong...
   1395  * ********************************************************************
   1396  */
   1397 static void
   1398 usage(void)
   1399 {
   1400 	const char *progname = getprogname();
   1401 
   1402 	(void)fprintf(stderr,
   1403 		      "usage:\t%s %s\n"
   1404 		      "\t%s %s\n"
   1405 		      "\t%s %s\n"
   1406 		      "\t%s %s\n"
   1407 		      "\t%s %s\n"
   1408 		      "\t%s %s\n",
   1409 		      progname, "[-dne] [-x[x]|-r] variable ...",
   1410 		      progname, "[-ne] [-q] -w variable=value ...",
   1411 		      progname, "[-dne] -a",
   1412 		      progname, "[-dne] -A",
   1413 		      progname, "[-ne] -M",
   1414 		      progname, "[-dne] [-q] -f file");
   1415 	exit(1);
   1416 }
   1417 
   1418 static void
   1419 getdesc1(int *name, u_int namelen, struct sysctlnode *pnode)
   1420 {
   1421 	struct sysctlnode node;
   1422 	char buf[1024], *desc;
   1423 	struct sysctldesc *d = (void*)buf;
   1424 	size_t sz = sizeof(buf);
   1425 	int rc;
   1426 
   1427 	memset(&node, 0, sizeof(node));
   1428 	node.sysctl_flags = SYSCTL_VERSION;
   1429 	node.sysctl_num = name[namelen - 1];
   1430 	name[namelen - 1] = CTL_DESCRIBE;
   1431 	rc = sysctl(name, namelen, d, &sz, &node, sizeof(node));
   1432 
   1433 	if (rc == -1 ||
   1434 	    d->descr_len == 1 ||
   1435 	    d->descr_num != pnode->sysctl_num ||
   1436 	    d->descr_ver != pnode->sysctl_ver)
   1437 		desc = (char *)-1;
   1438 	else
   1439 		desc = malloc(d->descr_len);
   1440 
   1441 	if (desc == NULL)
   1442 		desc = (char *)-1;
   1443 	if (desc != (char *)-1)
   1444 		memcpy(desc, &d->descr_str[0], d->descr_len);
   1445 	name[namelen - 1] = node.sysctl_num;
   1446 	if (pnode->sysctl_desc != NULL &&
   1447 	    pnode->sysctl_desc != (const char *)-1)
   1448 		free((void*)pnode->sysctl_desc);
   1449 	pnode->sysctl_desc = desc;
   1450 }
   1451 
   1452 static void
   1453 getdesc(int *name, u_int namelen, struct sysctlnode *pnode)
   1454 {
   1455 	struct sysctlnode *node = pnode->sysctl_child;
   1456 	struct sysctldesc *d, *p;
   1457 	char *desc;
   1458 	size_t sz;
   1459 	int rc, i;
   1460 
   1461 	sz = 128 * pnode->sysctl_clen;
   1462 	name[namelen] = CTL_DESCRIBE;
   1463 
   1464 	/*
   1465 	 * attempt *twice* to get the description chunk.  if two tries
   1466 	 * doesn't work, give up.
   1467 	 */
   1468 	i = 0;
   1469 	do {
   1470 		d = malloc(128 * pnode->sysctl_clen);
   1471 		if (d == NULL)
   1472 			return;
   1473 		rc = sysctl(name, namelen + 1, d, &sz, NULL, 0);
   1474 		if (rc == -1) {
   1475 			free(d);
   1476 			d = NULL;
   1477 			if (i == 0 && errno == ENOMEM)
   1478 				i = 1;
   1479 			else
   1480 				return;
   1481 		}
   1482 	} while (d == NULL);
   1483 
   1484 	/*
   1485 	 * hokey nested loop here, giving O(n**2) behavior, but should
   1486 	 * suffice for now
   1487 	 */
   1488 	for (i = 0; i < pnode->sysctl_clen; i++) {
   1489 		node = &pnode->sysctl_child[i];
   1490 		for (p = d; (char *)p < (char *)d + sz; p = NEXT_DESCR(p))
   1491 			if (node->sysctl_num == p->descr_num)
   1492 				break;
   1493 		if ((char *)p < (char *)d + sz &&
   1494 		    node[i].sysctl_ver == p->descr_ver) {
   1495 			/*
   1496 			 * match found, attempt to attach description
   1497 			 */
   1498 			if (p->descr_len == 1)
   1499 				desc = NULL;
   1500 			else
   1501 				desc = malloc(p->descr_len);
   1502 			if (desc == NULL)
   1503 				desc = (char *)-1;
   1504 			else
   1505 				memcpy(desc, &p->descr_str[0], p->descr_len);
   1506 			node->sysctl_desc = desc;
   1507 		}
   1508 	}
   1509 
   1510 	free(d);
   1511 }
   1512 
   1513 void
   1514 sysctlerror(int soft)
   1515 {
   1516 	if (soft) {
   1517 		switch (errno) {
   1518 		case ENOENT:
   1519 		case ENOPROTOOPT:
   1520 		case ENOTDIR:
   1521 		case EOPNOTSUPP:
   1522 		case EPROTONOSUPPORT:
   1523 			if (Aflag || req)
   1524 				fprintf(warnfp,
   1525 					"%s: the value is not available\n",
   1526 					gsname);
   1527 			return;
   1528 		}
   1529 	}
   1530 
   1531 	fprintf(warnfp, "%s: sysctl() failed with %s\n",
   1532 		gsname, strerror(errno));
   1533 	if (!soft)
   1534 		exit(1);
   1535 }
   1536 
   1537 /*
   1538  * ********************************************************************
   1539  * how to write to a "simple" node
   1540  * ********************************************************************
   1541  */
   1542 static void
   1543 write_number(int *name, u_int namelen, struct sysctlnode *node, char *value)
   1544 {
   1545 	int ii, io;
   1546 	u_quad_t qi, qo;
   1547 	size_t si, so;
   1548 	int rc;
   1549 	void *i, *o;
   1550 	char *t;
   1551 
   1552 	si = so = 0;
   1553 	i = o = NULL;
   1554 	errno = 0;
   1555 	qi = strtouq(value, &t, 0);
   1556 	if (errno != 0) {
   1557 		fprintf(warnfp, "%s: value too large\n", value);
   1558 		exit(1);
   1559 	}
   1560 	if (*t != '\0') {
   1561 		fprintf(warnfp, "%s: not a number\n", value);
   1562 		exit(1);
   1563 	}
   1564 
   1565 	switch (SYSCTL_TYPE(node->sysctl_flags)) {
   1566 	    case CTLTYPE_INT:
   1567 		ii = (int)qi;
   1568 		qo = ii;
   1569 		if (qo != qi) {
   1570 			fprintf(warnfp, "%s: value too large\n", value);
   1571 			exit(1);
   1572 		}
   1573 		o = &io;
   1574 		so = sizeof(io);
   1575 		i = &ii;
   1576 		si = sizeof(ii);
   1577 		break;
   1578 	case CTLTYPE_QUAD:
   1579 		o = &qo;
   1580 		so = sizeof(qo);
   1581 		i = &qi;
   1582 		si = sizeof(qi);
   1583 		break;
   1584 	}
   1585 
   1586 	rc = sysctl(name, namelen, o, &so, i, si);
   1587 	if (rc == -1)
   1588 		sysctlerror(0);
   1589 
   1590 	switch (SYSCTL_TYPE(node->sysctl_flags)) {
   1591 	case CTLTYPE_INT:
   1592 		display_number(node, gsname, &io, sizeof(io), DISPLAY_OLD);
   1593 		display_number(node, gsname, &ii, sizeof(ii), DISPLAY_NEW);
   1594 		break;
   1595 	case CTLTYPE_QUAD:
   1596 		display_number(node, gsname, &qo, sizeof(qo), DISPLAY_OLD);
   1597 		display_number(node, gsname, &qi, sizeof(qi), DISPLAY_NEW);
   1598 		break;
   1599 	}
   1600 }
   1601 
   1602 static void
   1603 write_string(int *name, u_int namelen, struct sysctlnode *node, char *value)
   1604 {
   1605 	char *i, *o;
   1606 	size_t si, so;
   1607 	int rc;
   1608 
   1609 	i = value;
   1610 	si = strlen(i) + 1;
   1611 	so = node->sysctl_size;
   1612 	if (si > so && so != 0) {
   1613 		fprintf(warnfp, "%s: string too long\n", value);
   1614 		exit(1);
   1615 	}
   1616 	o = malloc(so);
   1617 	if (o == NULL) {
   1618 		fprintf(warnfp, "%s: !malloc failed!\n", gsname);
   1619 		exit(1);
   1620 	}
   1621 
   1622 	rc = sysctl(name, namelen, o, &so, i, si);
   1623 	if (rc == -1)
   1624 		sysctlerror(0);
   1625 
   1626 	display_string(node, gsname, o, so, DISPLAY_OLD);
   1627 	display_string(node, gsname, i, si, DISPLAY_NEW);
   1628 	free(o);
   1629 }
   1630 
   1631 /*
   1632  * ********************************************************************
   1633  * simple ways to print stuff consistently
   1634  * ********************************************************************
   1635  */
   1636 static void
   1637 display_number(const struct sysctlnode *node, const char *name,
   1638 	       const void *data, size_t sz, int n)
   1639 {
   1640 	u_quad_t q;
   1641 	int i;
   1642 
   1643 	if (qflag)
   1644 		return;
   1645 	if ((nflag || rflag) && (n == DISPLAY_OLD))
   1646 		return;
   1647 
   1648 	if (rflag && n != DISPLAY_OLD) {
   1649 		fwrite(data, sz, 1, stdout);
   1650 		return;
   1651 	}
   1652 
   1653 	if (!nflag) {
   1654 		if (n == DISPLAY_VALUE)
   1655 			printf("%s%s", name, eq);
   1656 		else if (n == DISPLAY_OLD)
   1657 			printf("%s: ", name);
   1658 	}
   1659 
   1660 	if (xflag > 1) {
   1661 		if (n != DISPLAY_NEW)
   1662 			printf("\n");
   1663 		hex_dump(data, sz);
   1664 		return;
   1665 	}
   1666 
   1667 	switch (SYSCTL_TYPE(node->sysctl_flags)) {
   1668 	case CTLTYPE_INT:
   1669 		memcpy(&i, data, sz);
   1670 		if (xflag)
   1671 			printf("0x%0*x", (int)sz * 2, i);
   1672 		else if (node->sysctl_flags & CTLFLAG_HEX)
   1673 			printf("%#x", i);
   1674 		else
   1675 			printf("%d", i);
   1676 		break;
   1677 	case CTLTYPE_QUAD:
   1678 		memcpy(&q, data, sz);
   1679 		if (xflag)
   1680 			printf("0x%0*" PRIx64, (int)sz * 2, q);
   1681 		else if (node->sysctl_flags & CTLFLAG_HEX)
   1682 			printf("%#" PRIx64, q);
   1683 		else
   1684 			printf("%" PRIu64, q);
   1685 		break;
   1686 	}
   1687 
   1688 	if (n == DISPLAY_OLD)
   1689 		printf(" -> ");
   1690 	else
   1691 		printf("\n");
   1692 }
   1693 
   1694 static void
   1695 display_string(const struct sysctlnode *node, const char *name,
   1696 	       const void *data, size_t sz, int n)
   1697 {
   1698 	const unsigned char *buf = data;
   1699 	int ni;
   1700 
   1701 	if (qflag)
   1702 		return;
   1703 	if ((nflag || rflag) && (n == DISPLAY_OLD))
   1704 		return;
   1705 
   1706 	if (rflag && n != DISPLAY_OLD) {
   1707 		fwrite(data, sz, 1, stdout);
   1708 		return;
   1709 	}
   1710 
   1711 	if (!nflag) {
   1712 		if (n == DISPLAY_VALUE)
   1713 			printf("%s%s", name, eq);
   1714 		else if (n == DISPLAY_OLD)
   1715 			printf("%s: ", name);
   1716 	}
   1717 
   1718 	if (xflag > 1) {
   1719 		if (n != DISPLAY_NEW)
   1720 			printf("\n");
   1721 		hex_dump(data, sz);
   1722 		return;
   1723 	}
   1724 
   1725 	if (xflag || node->sysctl_flags & CTLFLAG_HEX) {
   1726 		for (ni = 0; ni < (int)sz; ni++) {
   1727 			if (xflag)
   1728 				printf("%02x", buf[ni]);
   1729 			if (buf[ni] == '\0')
   1730 				break;
   1731 			if (!xflag)
   1732 				printf("\\x%2.2x", buf[ni]);
   1733 		}
   1734 	}
   1735 	else
   1736 		printf("%.*s", (int)sz, buf);
   1737 
   1738 	if (n == DISPLAY_OLD)
   1739 		printf(" -> ");
   1740 	else
   1741 		printf("\n");
   1742 }
   1743 
   1744 /*ARGSUSED*/
   1745 static void
   1746 display_struct(const struct sysctlnode *node, const char *name,
   1747 	       const void *data, size_t sz, int n)
   1748 {
   1749 	const unsigned char *buf = data;
   1750 	int ni;
   1751 	size_t more;
   1752 
   1753 	if (qflag)
   1754 		return;
   1755 	if (!(xflag || rflag)) {
   1756 		if (Aflag || req)
   1757 			fprintf(warnfp,
   1758 				"%s: this type is unknown to this program\n",
   1759 				gsname);
   1760 		return;
   1761 	}
   1762 	if ((nflag || rflag) && (n == DISPLAY_OLD))
   1763 		return;
   1764 
   1765 	if (rflag && n != DISPLAY_OLD) {
   1766 		fwrite(data, sz, 1, stdout);
   1767 		return;
   1768 	}
   1769 
   1770         if (!nflag) {
   1771                 if (n == DISPLAY_VALUE)
   1772                         printf("%s%s", name, eq);
   1773                 else if (n == DISPLAY_OLD)
   1774                         printf("%s: ", name);
   1775         }
   1776 
   1777 	if (xflag > 1) {
   1778 		if (n != DISPLAY_NEW)
   1779 			printf("\n");
   1780 		hex_dump(data, sz);
   1781 		return;
   1782 	}
   1783 
   1784 	if (sz > 16) {
   1785 		more = sz - 16;
   1786 		sz = 16;
   1787 	}
   1788 	else
   1789 		more = 0;
   1790 	for (ni = 0; ni < (int)sz; ni++)
   1791 		printf("%02x", buf[ni]);
   1792 	if (more)
   1793 		printf("...(%zu more bytes)", more);
   1794 	printf("\n");
   1795 }
   1796 
   1797 static void
   1798 hex_dump(const unsigned char *buf, size_t len)
   1799 {
   1800 	int i, j;
   1801 	char line[80], tmp[12];
   1802 
   1803 	memset(line, ' ', sizeof(line));
   1804 	for (i = 0, j = 15; i < len; i++) {
   1805 		j = i % 16;
   1806 		/* reset line */
   1807 		if (j == 0) {
   1808 			line[58] = '|';
   1809 			line[77] = '|';
   1810 			line[78] = 0;
   1811 			snprintf(tmp, sizeof(tmp), "%07d", i);
   1812 			memcpy(&line[0], tmp, 7);
   1813 		}
   1814 		/* copy out hex version of byte */
   1815 		snprintf(tmp, sizeof(tmp), "%02x", buf[i]);
   1816 		memcpy(&line[9 + j * 3], tmp, 2);
   1817 		/* copy out plain version of byte */
   1818 		line[60 + j] = (isprint(buf[i])) ? buf[i] : '.';
   1819 		/* print a full line and erase it */
   1820 		if (j == 15) {
   1821 			printf("%s\n", line);
   1822 			memset(line, ' ', sizeof(line));
   1823 		}
   1824 	}
   1825 	if (line[0] != ' ')
   1826 		printf("%s\n", line);
   1827 	printf("%07zu bytes\n", len);
   1828 }
   1829 
   1830 /*
   1831  * ********************************************************************
   1832  * functions that handle particular nodes
   1833  * ********************************************************************
   1834  */
   1835 /*ARGSUSED*/
   1836 static void
   1837 printother(HANDLER_ARGS)
   1838 {
   1839 	int rc;
   1840 	void *p;
   1841 	size_t sz1, sz2;
   1842 
   1843 	if (!(Aflag || req) || Mflag)
   1844 		return;
   1845 
   1846 	/*
   1847 	 * okay...you asked for it, so let's give it a go
   1848 	 */
   1849 	while (type != CTLTYPE_NODE && (xflag || rflag)) {
   1850 		rc = sysctl(name, namelen, NULL, &sz1, NULL, 0);
   1851 		if (rc == -1 || sz1 == 0)
   1852 			break;
   1853 		p = malloc(sz1);
   1854 		if (p == NULL)
   1855 			break;
   1856 		sz2 = sz1;
   1857 		rc = sysctl(name, namelen, p, &sz2, NULL, 0);
   1858 		if (rc == -1 || sz1 != sz2) {
   1859 			free(p);
   1860 			break;
   1861 		}
   1862 		display_struct(pnode, gsname, p, sz1, DISPLAY_VALUE);
   1863 		free(p);
   1864 		return;
   1865 	}
   1866 
   1867 	/*
   1868 	 * that didn't work...do we have a specific message for this
   1869 	 * thing?
   1870 	 */
   1871 	if (v != NULL) {
   1872 		fprintf(warnfp, "%s: use '%s' to view this information\n",
   1873 			gsname, (const char *)v);
   1874 		return;
   1875 	}
   1876 
   1877 	/*
   1878 	 * hmm...i wonder if we have any generic hints?
   1879 	 */
   1880 	switch (name[0]) {
   1881 	case CTL_NET:
   1882 		fprintf(warnfp, "%s: use 'netstat' to view this information\n",
   1883 			sname);
   1884 		break;
   1885 	case CTL_DEBUG:
   1886 		fprintf(warnfp, "%s: missing 'options DEBUG' from kernel?\n",
   1887 			sname);
   1888 		break;
   1889 	case CTL_DDB:
   1890 		fprintf(warnfp, "%s: missing 'options DDB' from kernel?\n",
   1891 			sname);
   1892 		break;
   1893 	}
   1894 }
   1895 
   1896 /*ARGSUSED*/
   1897 static void
   1898 kern_clockrate(HANDLER_ARGS)
   1899 {
   1900 	struct clockinfo clkinfo;
   1901 	size_t sz;
   1902 	int rc;
   1903 
   1904 	sz = sizeof(clkinfo);
   1905 	rc = sysctl(name, namelen, &clkinfo, &sz, NULL, 0);
   1906 	if (rc == -1) {
   1907 		sysctlerror(1);
   1908 		return;
   1909 	}
   1910 	if (sz != sizeof(clkinfo))
   1911 		errx(1, "%s: !returned size wrong!", sname);
   1912 
   1913 	if (xflag || rflag) {
   1914 		display_struct(pnode, sname, &clkinfo, sz,
   1915 			       DISPLAY_VALUE);
   1916 		return;
   1917 	}
   1918 	else if (!nflag)
   1919 		printf("%s: ", sname);
   1920 	printf("tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n",
   1921 	       clkinfo.tick, clkinfo.tickadj,
   1922 	       clkinfo.hz, clkinfo.profhz, clkinfo.stathz);
   1923 }
   1924 
   1925 /*ARGSUSED*/
   1926 static void
   1927 kern_boottime(HANDLER_ARGS)
   1928 {
   1929 	struct timeval timeval;
   1930 	time_t boottime;
   1931 	size_t sz;
   1932 	int rc;
   1933 
   1934 	sz = sizeof(timeval);
   1935 	rc = sysctl(name, namelen, &timeval, &sz, NULL, 0);
   1936 	if (rc == -1) {
   1937 		sysctlerror(1);
   1938 		return;
   1939 	}
   1940 	if (sz != sizeof(timeval))
   1941 		errx(1, "%s: !returned size wrong!", sname);
   1942 
   1943 	boottime = timeval.tv_sec;
   1944 	if (xflag || rflag)
   1945 		display_struct(pnode, sname, &timeval, sz,
   1946 			       DISPLAY_VALUE);
   1947 	else if (!nflag)
   1948 		/* ctime() provides the \n */
   1949 		printf("%s%s%s", sname, eq, ctime(&boottime));
   1950 	else if (nflag == 1)
   1951 		printf("%ld\n", (long)boottime);
   1952 	else
   1953 		printf("%ld.%06ld\n", (long)timeval.tv_sec,
   1954 		       (long)timeval.tv_usec);
   1955 }
   1956 
   1957 /*ARGSUSED*/
   1958 static void
   1959 kern_consdev(HANDLER_ARGS)
   1960 {
   1961 	dev_t cons;
   1962 	size_t sz;
   1963 	int rc;
   1964 
   1965 	sz = sizeof(cons);
   1966 	rc = sysctl(name, namelen, &cons, &sz, NULL, 0);
   1967 	if (rc == -1) {
   1968 		sysctlerror(1);
   1969 		return;
   1970 	}
   1971 	if (sz != sizeof(cons))
   1972 		errx(1, "%s: !returned size wrong!", sname);
   1973 
   1974 	if (xflag || rflag)
   1975 		display_struct(pnode, sname, &cons, sz,
   1976 			       DISPLAY_VALUE);
   1977 	else if (!nflag)
   1978 		printf("%s%s%s\n", sname, eq, devname(cons, S_IFCHR));
   1979 	else
   1980 		printf("0x%x\n", cons);
   1981 }
   1982 
   1983 /*ARGSUSED*/
   1984 static void
   1985 kern_cp_time(HANDLER_ARGS)
   1986 {
   1987 	u_int64_t *cp_time;
   1988 	size_t sz, osz;
   1989 	int rc, i, n;
   1990 	char s[sizeof("kern.cp_time.nnnnnn")];
   1991 	const char *tname;
   1992 
   1993 	/*
   1994 	 * three things to do here.
   1995 	 * case 1: get sum (no Aflag and namelen == 2)
   1996 	 * case 2: get specific processor (namelen == 3)
   1997 	 * case 3: get all processors (Aflag and namelen == 2)
   1998 	 */
   1999 
   2000 	if (namelen == 2 && Aflag) {
   2001 		sz = sizeof(n);
   2002 		rc = sysctlbyname("hw.ncpu", &n, &sz, NULL, 0);
   2003 		if (rc != 0)
   2004 			return; /* XXX print an error, eh? */
   2005 		n++; /* Add on space for the sum. */
   2006 		sz = n * sizeof(u_int64_t) * CPUSTATES;
   2007 	}
   2008 	else {
   2009 		n = -1; /* Just print one data set. */
   2010 		sz = sizeof(u_int64_t) * CPUSTATES;
   2011 	}
   2012 
   2013 	cp_time = malloc(sz);
   2014 	if (cp_time == NULL) {
   2015 		sysctlerror(1);
   2016 		return;
   2017 	}
   2018 
   2019 	osz = sz;
   2020 	rc = sysctl(name, namelen, cp_time + (n != -1) * CPUSTATES, &osz,
   2021 		    NULL, 0);
   2022 
   2023 	if (rc == -1) {
   2024 		sysctlerror(1);
   2025 		free(cp_time);
   2026 		return;
   2027 	}
   2028 
   2029 	/*
   2030 	 * Check, but account for space we'll occupy with the sum.
   2031 	 */
   2032 	if (osz != sz - (n != -1) * CPUSTATES * sizeof(u_int64_t))
   2033 		errx(1, "%s: !returned size wrong!", sname);
   2034 
   2035 	/*
   2036 	 * Compute the actual sum.  Two calls would be easier (we
   2037 	 * could just call ourselves recursively above), but the
   2038 	 * numbers wouldn't add up.
   2039 	 */
   2040 	if (n != -1) {
   2041 		memset(cp_time, 0, sizeof(u_int64_t) * CPUSTATES);
   2042 		for (i = 1; i < n; i++) {
   2043 			cp_time[CP_USER] += cp_time[i * CPUSTATES + CP_USER];
   2044                         cp_time[CP_NICE] += cp_time[i * CPUSTATES + CP_NICE];
   2045                         cp_time[CP_SYS] += cp_time[i * CPUSTATES + CP_SYS];
   2046                         cp_time[CP_INTR] += cp_time[i * CPUSTATES + CP_INTR];
   2047                         cp_time[CP_IDLE] += cp_time[i * CPUSTATES + CP_IDLE];
   2048 		}
   2049 	}
   2050 
   2051 	tname = sname;
   2052 	for (i = 0; n == -1 || i < n; i++) {
   2053 		if (i > 0) {
   2054 			(void)snprintf(s, sizeof(s), "%s%s%d", sname, sep,
   2055 				       i - 1);
   2056 			tname = s;
   2057 		}
   2058 		if (xflag || rflag)
   2059 			display_struct(pnode, tname, cp_time + (i * CPUSTATES),
   2060 				       sizeof(u_int64_t) * CPUSTATES,
   2061 				       DISPLAY_VALUE);
   2062 		else {
   2063 			if (!nflag)
   2064 				printf("%s: ", tname);
   2065 			printf("user = %" PRIu64
   2066 			       ", nice = %" PRIu64
   2067 			       ", sys = %" PRIu64
   2068 			       ", intr = %" PRIu64
   2069 			       ", idle = %" PRIu64
   2070 			       "\n",
   2071 			       cp_time[i * CPUSTATES + CP_USER],
   2072 			       cp_time[i * CPUSTATES + CP_NICE],
   2073 			       cp_time[i * CPUSTATES + CP_SYS],
   2074 			       cp_time[i * CPUSTATES + CP_INTR],
   2075 			       cp_time[i * CPUSTATES + CP_IDLE]);
   2076 		}
   2077 		/*
   2078 		 * Just printing the one node.
   2079 		 */
   2080 		if (n == -1)
   2081 			break;
   2082 	}
   2083 
   2084 	free(cp_time);
   2085 }
   2086 
   2087 /*ARGSUSED*/
   2088 static void
   2089 vm_loadavg(HANDLER_ARGS)
   2090 {
   2091 	struct loadavg loadavg;
   2092 	size_t sz;
   2093 	int rc;
   2094 
   2095 	sz = sizeof(loadavg);
   2096 	rc = sysctl(name, namelen, &loadavg, &sz, NULL, 0);
   2097 	if (rc == -1) {
   2098 		sysctlerror(1);
   2099 		return;
   2100 	}
   2101 	if (sz != sizeof(loadavg))
   2102 		errx(1, "%s: !returned size wrong!", sname);
   2103 
   2104 	if (xflag || rflag) {
   2105 		display_struct(pnode, sname, &loadavg, sz,
   2106 			       DISPLAY_VALUE);
   2107 		return;
   2108 	}
   2109 	if (!nflag)
   2110 		printf("%s: ", sname);
   2111 	printf("%.2f %.2f %.2f\n",
   2112 	       (double) loadavg.ldavg[0] / loadavg.fscale,
   2113 	       (double) loadavg.ldavg[1] / loadavg.fscale,
   2114 	       (double) loadavg.ldavg[2] / loadavg.fscale);
   2115 }
   2116 
   2117 /*ARGSUSED*/
   2118 static void
   2119 proc_limit(HANDLER_ARGS)
   2120 {
   2121 	u_quad_t olim, *newp, nlim;
   2122 	size_t osz, nsz;
   2123 	char *t;
   2124 	int rc;
   2125 
   2126 	osz = sizeof(olim);
   2127 	if (value != NULL) {
   2128 		nsz = sizeof(nlim);
   2129 		newp = &nlim;
   2130 		if (strcmp(value, "unlimited") == 0)
   2131 			nlim = RLIM_INFINITY;
   2132 		else {
   2133 			errno = 0;
   2134 			nlim = strtouq(value, &t, 0);
   2135 			if (*t != '\0' || errno != 0) {
   2136 				fprintf(warnfp,
   2137 					"%s: %s: '%s' is not a valid limit\n",
   2138 					getprogname(), sname, value);
   2139 				exit(1);
   2140 			}
   2141 		}
   2142 	}
   2143 	else {
   2144 		nsz = 0;
   2145 		newp = NULL;
   2146 	}
   2147 
   2148 	rc = sysctl(name, namelen, &olim, &osz, newp, nsz);
   2149 	if (rc == -1) {
   2150 		sysctlerror(newp == NULL);
   2151 		return;
   2152 	}
   2153 
   2154 	if (newp && qflag)
   2155 		return;
   2156 
   2157 	if (rflag || xflag || olim != RLIM_INFINITY)
   2158 		display_number(pnode, sname, &olim, sizeof(olim),
   2159 			       newp ? DISPLAY_OLD : DISPLAY_VALUE);
   2160 	else
   2161 		display_string(pnode, sname, "unlimited", 10,
   2162 			       newp ? DISPLAY_OLD : DISPLAY_VALUE);
   2163 
   2164 	if (newp) {
   2165 		if (rflag || xflag || nlim != RLIM_INFINITY)
   2166 			display_number(pnode, sname, &nlim, sizeof(nlim),
   2167 				       DISPLAY_NEW);
   2168 		else
   2169 			display_string(pnode, sname, "unlimited", 10,
   2170 				       DISPLAY_NEW);
   2171 	}
   2172 }
   2173 
   2174 #ifdef CPU_DISKINFO
   2175 /*ARGSUSED*/
   2176 static void
   2177 machdep_diskinfo(HANDLER_ARGS)
   2178 {
   2179 	struct disklist *dl;
   2180 	struct biosdisk_info *bi;
   2181 	struct nativedisk_info *ni;
   2182 	int rc;
   2183 	size_t sz;
   2184 	uint i, b, lim;
   2185 
   2186 	rc = sysctl(name, namelen, NULL, &sz, NULL, 0);
   2187 	if (rc == -1) {
   2188 		sysctlerror(1);
   2189 		return;
   2190 	}
   2191 	dl = malloc(sz);
   2192 	if (dl == NULL) {
   2193 		sysctlerror(1);
   2194 		return;
   2195 	}
   2196 	rc = sysctl(name, namelen, dl, &sz, NULL, 0);
   2197 	if (rc == -1) {
   2198 		sysctlerror(1);
   2199 		return;
   2200 	}
   2201 
   2202 	if (!nflag)
   2203 		printf("%s: ", sname);
   2204 	lim = dl->dl_nbiosdisks;
   2205 	if (lim > MAX_BIOSDISKS)
   2206 		lim = MAX_BIOSDISKS;
   2207 	for (bi = dl->dl_biosdisks, i = 0; i < lim; bi++, i++)
   2208 		printf("%x:%" PRIu64 "(%d/%d/%d),%x ",
   2209 		       bi->bi_dev, bi->bi_lbasecs,
   2210 		       bi->bi_cyl, bi->bi_head, bi->bi_sec,
   2211 		       bi->bi_flags);
   2212 	lim = dl->dl_nnativedisks;
   2213 	ni = dl->dl_nativedisks;
   2214 	bi = dl->dl_biosdisks;
   2215 	/* LINTED -- pointer casts are tedious */
   2216 	if ((char *)&ni[lim] != (char *)dl + sz) {
   2217 		fprintf(warnfp, "size mismatch\n");
   2218 		return;
   2219 	}
   2220 	for (i = 0; i < lim; ni++, i++) {
   2221 		char t = ':';
   2222 		printf(" %.*s", (int)sizeof ni->ni_devname,
   2223 		       ni->ni_devname);
   2224 		for (b = 0; b < ni->ni_nmatches; t = ',', b++)
   2225 			printf("%c%x", t,
   2226 			       bi[ni->ni_biosmatches[b]].bi_dev);
   2227 	}
   2228 	printf("\n");
   2229 }
   2230 #endif /* CPU_DISKINFO */
   2231