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