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