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