Home | History | Annotate | Line # | Download | only in sysctl
sysctl.c revision 1.72
      1 /*	$NetBSD: sysctl.c,v 1.72 2003/09/20 17:02:19 grant Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 __COPYRIGHT(
     35 "@(#) Copyright (c) 1993\n\
     36 	The Regents of the University of California.  All rights reserved.\n");
     37 #endif /* not lint */
     38 
     39 #ifndef lint
     40 #if 0
     41 static char sccsid[] = "@(#)sysctl.c	8.1 (Berkeley) 6/6/93";
     42 #else
     43 __RCSID("$NetBSD: sysctl.c,v 1.72 2003/09/20 17:02:19 grant Exp $");
     44 #endif
     45 #endif /* not lint */
     46 
     47 #include <sys/param.h>
     48 #include <sys/gmon.h>
     49 #include <sys/stat.h>
     50 #include <sys/sysctl.h>
     51 #include <sys/socket.h>
     52 #include <sys/mount.h>
     53 #include <sys/mbuf.h>
     54 #include <sys/resource.h>
     55 #include <uvm/uvm_param.h>
     56 #include <machine/cpu.h>
     57 
     58 #include <ufs/ufs/dinode.h>
     59 #include <ufs/ufs/dir.h>
     60 #include <ufs/ffs/fs.h>
     61 #include <ufs/ffs/ffs_extern.h>
     62 
     63 #include <nfs/rpcv2.h>
     64 #include <nfs/nfsproto.h>
     65 #include <nfs/nfs.h>
     66 
     67 #include <netinet/in.h>
     68 #include <netinet/in_systm.h>
     69 #include <netinet/ip.h>
     70 #include <netinet/ip_icmp.h>
     71 #include <netinet/icmp_var.h>
     72 #include <netinet/ip_var.h>
     73 #include <netinet/udp.h>
     74 #include <netinet/udp_var.h>
     75 #include <netinet/tcp.h>
     76 #include <netinet/tcp_timer.h>
     77 #include <netinet/tcp_var.h>
     78 
     79 #ifdef INET6
     80 #include <netinet/ip6.h>
     81 #include <netinet/icmp6.h>
     82 #include <netinet6/ip6_var.h>
     83 #include <netinet6/udp6.h>
     84 #include <netinet6/udp6_var.h>
     85 #ifdef TCP6
     86 #include <netinet6/tcp6.h>
     87 #include <netinet6/tcp6_timer.h>
     88 #include <netinet6/tcp6_var.h>
     89 #endif
     90 #include <netinet6/pim6_var.h>
     91 #endif /* INET6 */
     92 
     93 #include "../../sys/compat/linux/common/linux_exec.h"
     94 #include "../../sys/compat/irix/irix_sysctl.h"
     95 #include "../../sys/compat/darwin/darwin_sysctl.h"
     96 
     97 #ifdef IPSEC
     98 #include <net/route.h>
     99 #include <netinet6/ipsec.h>
    100 #include <netkey/key_var.h>
    101 #endif /* IPSEC */
    102 
    103 #include <sys/pipe.h>
    104 
    105 #include <err.h>
    106 #include <ctype.h>
    107 #include <errno.h>
    108 #include <stdio.h>
    109 #include <stdlib.h>
    110 #include <string.h>
    111 #include <unistd.h>
    112 
    113 struct ctlname topname[] = CTL_NAMES;
    114 struct ctlname kernname[] = CTL_KERN_NAMES;
    115 struct ctlname vmname[] = CTL_VM_NAMES;
    116 struct ctlname vfsname[] = CTL_VFS_NAMES;
    117 struct ctlname netname[] = CTL_NET_NAMES;
    118 struct ctlname hwname[] = CTL_HW_NAMES;
    119 struct ctlname username[] = CTL_USER_NAMES;
    120 struct ctlname ddbname[] = CTL_DDB_NAMES;
    121 struct ctlname debugname[CTL_DEBUG_MAXID];
    122 #ifdef CTL_MACHDEP_NAMES
    123 struct ctlname machdepname[] = CTL_MACHDEP_NAMES;
    124 #endif
    125 struct ctlname emulname[] = CTL_EMUL_NAMES;
    126 struct ctlname vendorname[] = { { 0, 0 } };
    127 
    128 /* this one is dummy, it's used only for '-a' or '-A' */
    129 struct ctlname procname[] = { {0, 0}, {"curproc", CTLTYPE_NODE} };
    130 
    131 char names[BUFSIZ];
    132 
    133 struct list {
    134 	struct	ctlname *list;
    135 	int	size;
    136 };
    137 struct list toplist = { topname, CTL_MAXID };
    138 struct list secondlevel[] = {
    139 	{ 0, 0 },			/* CTL_UNSPEC */
    140 	{ kernname, KERN_MAXID },	/* CTL_KERN */
    141 	{ vmname, VM_MAXID },		/* CTL_VM */
    142 	{ vfsname, VFS_MAXID },		/* CTL_VFS */
    143 	{ netname, NET_MAXID },		/* CTL_NET */
    144 	{ 0, CTL_DEBUG_MAXID },		/* CTL_DEBUG */
    145 	{ hwname, HW_MAXID },		/* CTL_HW */
    146 #ifdef CTL_MACHDEP_NAMES
    147 	{ machdepname, CPU_MAXID },	/* CTL_MACHDEP */
    148 #else
    149 	{ 0, 0 },			/* CTL_MACHDEP */
    150 #endif
    151 	{ username, USER_MAXID },	/* CTL_USER_NAMES */
    152 	{ ddbname, DDBCTL_MAXID },	/* CTL_DDB_NAMES */
    153 	{ procname, 2 },		/* dummy name */
    154 	{ vendorname, 0 },		/* CTL_VENDOR_NAMES */
    155 	{ emulname, EMUL_MAXID },	/* CTL_EMUL_NAMES */
    156 
    157 	{ 0, 0},
    158 };
    159 
    160 int	Aflag, aflag, eflag, nflag, qflag, wflag;
    161 FILE	*warnfp = stderr;
    162 
    163 /*
    164  * Variables requiring special processing.
    165  */
    166 #define	CLOCK		0x00000001
    167 #define	BOOTTIME	0x00000002
    168 #define	CONSDEV		0x00000003
    169 #define	DISKINFO	0x00000004
    170 #define	CPTIME		0x00000005
    171 #define	CNMAGIC		0x00000006
    172 
    173 /*
    174  * A dummy type for limits, which requires special parsing
    175  */
    176 #define CTLTYPE_LIMIT	((~0x1) << 31)
    177 
    178 int main(int, char *[]);
    179 
    180 static void listall(const char *, struct list *);
    181 static void parse(char *, int);
    182 static void debuginit(void);
    183 static int sysctl_inet(char *, char **, int[], int, int *);
    184 #ifdef INET6
    185 static int sysctl_inet6(char *, char **, int[], int, int *);
    186 #endif
    187 static int sysctl_vfs(char *, char **, int[], int, int *);
    188 static int sysctl_proc(char *, char **, int[], int, int *);
    189 static int sysctl_3rd(struct list *, char *, char **, int[], int, int *);
    190 
    191 #ifdef IPSEC
    192 struct ctlname keynames[] = KEYCTL_NAMES;
    193 struct list keyvars = { keynames, KEYCTL_MAXID };
    194 #endif /*IPSEC*/
    195 struct ctlname vfsgenname[] = CTL_VFSGENCTL_NAMES;
    196 struct list vfsgenvars = { vfsgenname, VFSGEN_MAXID };
    197 struct ctlname mbufnames[] = CTL_MBUF_NAMES;
    198 struct list mbufvars = { mbufnames, MBUF_MAXID };
    199 struct ctlname pipenames[] = CTL_PIPE_NAMES;
    200 struct list pipevars = { pipenames, KERN_PIPE_MAXID };
    201 struct ctlname tkstatnames[] = KERN_TKSTAT_NAMES;
    202 struct list tkstatvars = { tkstatnames, KERN_TKSTAT_MAXID };
    203 
    204 static int sysctl_linux(char *, char **, int[], int, int *);
    205 static int sysctl_irix(char *, char **, int[], int, int *);
    206 static int sysctl_darwin(char *, char **, int[], int, int *);
    207 static int findname(char *, char *, char **, struct list *);
    208 static void usage(void);
    209 
    210 #define USEAPP(s, a) \
    211     if (flags) fprintf(warnfp, "%s: use '%s' to view this information\n", s, a)
    212 
    213 
    214 int
    215 main(int argc, char *argv[])
    216 {
    217 	char *fn = NULL;
    218 	int ch, lvl1;
    219 
    220 	while ((ch = getopt(argc, argv, "Aaef:nqw")) != -1) {
    221 		switch (ch) {
    222 
    223 		case 'A':
    224 			Aflag = 1;
    225 			break;
    226 
    227 		case 'a':
    228 			aflag = 1;
    229 			break;
    230 
    231 		case 'e':
    232 			eflag = 1;
    233 			break;
    234 
    235 		case 'f':
    236 			fn = optarg;
    237 			wflag = 1;
    238 			break;
    239 
    240 		case 'n':
    241 			nflag = 1;
    242 			break;
    243 
    244 		case 'q':
    245 			qflag = 1;
    246 			break;
    247 
    248 		case 'w':
    249 			wflag = 1;
    250 			break;
    251 
    252 		default:
    253 			usage();
    254 		}
    255 	}
    256 
    257 	if (qflag && !wflag)
    258 		usage();
    259 
    260 	argc -= optind;
    261 	argv += optind;
    262 
    263 	if (Aflag || aflag) {
    264 		warnfp = stdout;
    265 		debuginit();
    266 		for (lvl1 = 1; lvl1 < CTL_MAXID; lvl1++)
    267 			listall(topname[lvl1].ctl_name, &secondlevel[lvl1]);
    268 		return 0;
    269 	}
    270 
    271 	if (fn) {
    272 		FILE *fp;
    273 		char *l;
    274 
    275 		fp = fopen(fn, "r");
    276 		if (fp == NULL) {
    277 			err(1, "%s", fn);
    278 		} else {
    279 			for (; (l = fparseln(fp, NULL, NULL, NULL, 0)) != NULL;
    280 			    free(l)) {
    281 				if (*l)
    282 					parse(l, 1);
    283 			}
    284 			fclose(fp);
    285 		}
    286 	} else {
    287 		if (argc == 0)
    288 			usage();
    289 		while (argc-- > 0)
    290 			parse(*argv++, 1);
    291 	}
    292 	return 0;
    293 }
    294 
    295 /*
    296  * List all variables known to the system.
    297  */
    298 static void
    299 listall(const char *prefix, struct list *lp)
    300 {
    301 	int lvl2;
    302 	char name[BUFSIZ], *cp;
    303 
    304 	if (lp->list == 0)
    305 		return;
    306 	strlcpy(name, prefix, sizeof(name));
    307 	strlcat(name, ".", sizeof(name));
    308 	cp = &name[strlen(name)];
    309 	for (lvl2 = 0; lvl2 < lp->size; lvl2++) {
    310 		if (lp->list[lvl2].ctl_name == 0)
    311 			continue;
    312 		strlcpy(cp, lp->list[lvl2].ctl_name,
    313 		    sizeof(name) - (cp - name));
    314 		parse(name, Aflag);
    315 	}
    316 }
    317 
    318 /*
    319  * Parse a name into a MIB entry.
    320  * Lookup and print out the MIB entry if it exists.
    321  * Set a new value if requested.
    322  */
    323 static void
    324 parse(char *string, int flags)
    325 {
    326 	int indx, type, state, len;
    327 	int special = 0;
    328 	void *newval = 0;
    329 	int intval, newsize = 0;
    330 	long long quadval;
    331 	size_t size;
    332 	struct list *lp;
    333 	int mib[CTL_MAXNAME];
    334 	char *cp, *bufp, buf[BUFSIZ], *eq;
    335 	double loads[3];
    336 
    337 	if (eflag)
    338 		eq = "=";
    339 	else
    340 		eq = " = ";
    341 
    342 	bufp = buf;
    343 	snprintf(buf, BUFSIZ, "%s", string);
    344 	if ((cp = strchr(string, '=')) != NULL) {
    345 		if (!wflag)
    346 			errx(2, "Must specify -w to set variables");
    347 		*strchr(buf, '=') = '\0';
    348 		*cp++ = '\0';
    349 		while (isspace((unsigned char) *cp))
    350 			cp++;
    351 		newval = cp;
    352 		newsize = strlen(cp);
    353 	}
    354 	if ((indx = findname(string, "top", &bufp, &toplist)) == -1)
    355 		return;
    356 	mib[0] = indx;
    357 	if (indx == CTL_DEBUG)
    358 		debuginit();
    359 	if (mib[0] == CTL_PROC) {
    360 		type = CTLTYPE_NODE;
    361 		len = 1;
    362 	} else {
    363 		lp = &secondlevel[indx];
    364 		if (lp->list == 0) {
    365 			warnx("Class `%s' is not implemented",
    366 			    topname[indx].ctl_name);
    367 			return;
    368 		}
    369 		if (bufp == NULL) {
    370 			listall(topname[indx].ctl_name, lp);
    371 			return;
    372 		}
    373 		if ((indx = findname(string, "second", &bufp, lp)) == -1)
    374 			return;
    375 		mib[1] = indx;
    376 		type = lp->list[indx].ctl_type;
    377 		len = 2;
    378 	}
    379 	switch (mib[0]) {
    380 
    381 	case CTL_KERN:
    382 		switch (mib[1]) {
    383 		case KERN_PROF:
    384 			mib[2] = GPROF_STATE;
    385 			size = sizeof state;
    386 			if (sysctl(mib, 3, &state, &size, NULL, 0) < 0) {
    387 				if (flags == 0)
    388 					return;
    389 				if (!nflag)
    390 					fprintf(warnfp, "%s: ", string);
    391 				fprintf(warnfp,
    392 				    "kernel is not compiled for profiling\n");
    393 				return;
    394 			}
    395 			if (!nflag)
    396 				printf("%s: %s\n", string,
    397 				    state == GMON_PROF_OFF ? "off" : "running");
    398 			return;
    399 		case KERN_VNODE:
    400 		case KERN_FILE:
    401 			USEAPP(string, "pstat");
    402 			return;
    403 		case KERN_PROC:
    404 		case KERN_PROC2:
    405 		case KERN_PROC_ARGS:
    406 			USEAPP(string, "ps");
    407 			return;
    408 		case KERN_CLOCKRATE:
    409 			special = CLOCK;
    410 			break;
    411 		case KERN_BOOTTIME:
    412 			special = BOOTTIME;
    413 			break;
    414 		case KERN_NTPTIME:
    415 			USEAPP(string, "ntpdc -c kerninfo");
    416 			return;
    417 		case KERN_MBUF:
    418 			len = sysctl_3rd(&mbufvars, string, &bufp, mib, flags,
    419 			    &type);
    420 			if (len < 0)
    421 				return;
    422 			break;
    423 		case KERN_CP_TIME:
    424 			special = CPTIME;
    425 			break;
    426 		case KERN_MSGBUF:
    427 			USEAPP(string, "dmesg");
    428 			return;
    429 		case KERN_CONSDEV:
    430 			special = CONSDEV;
    431 			break;
    432 		case KERN_PIPE:
    433 			len = sysctl_3rd(&pipevars, string, &bufp, mib, flags,
    434 			    &type);
    435 			if (len < 0)
    436 				return;
    437 			break;
    438 		case KERN_TKSTAT:
    439 			len = sysctl_3rd(&tkstatvars, string, &bufp, mib, flags,
    440 			    &type);
    441 			if (len < 0)
    442 				return;
    443 			break;
    444 		}
    445 		break;
    446 
    447 	case CTL_HW:
    448 		switch (mib[1]) {
    449 		case HW_DISKSTATS:
    450 			USEAPP(string, "iostat");
    451 			return;
    452 		case HW_CNMAGIC:
    453 			if (!nflag)
    454 				special = CNMAGIC;
    455 			break;
    456 		}
    457 		break;
    458 
    459 	case CTL_VM:
    460 		switch (mib[1]) {
    461 		case VM_LOADAVG:
    462 			getloadavg(loads, 3);
    463 			if (!nflag)
    464 				printf("%s: ", string);
    465 			printf("%.2f %.2f %.2f\n", loads[0], loads[1],
    466 			    loads[2]);
    467 			return;
    468 
    469 		case VM_METER:
    470 		case VM_UVMEXP:
    471 		case VM_UVMEXP2:
    472 			USEAPP(string, "vmstat' or 'systat");
    473 			return;
    474 		}
    475 		break;
    476 
    477 	case CTL_NET:
    478 		if (mib[1] == PF_INET) {
    479 			len = sysctl_inet(string, &bufp, mib, flags, &type);
    480 			if (len >= 0)
    481 				break;
    482 			return;
    483 		}
    484 #ifdef INET6
    485 		else if (mib[1] == PF_INET6) {
    486 			len = sysctl_inet6(string, &bufp, mib, flags, &type);
    487 			if (len >= 0)
    488 				break;
    489 			return;
    490 		}
    491 #endif /* INET6 */
    492 #ifdef IPSEC
    493 		else if (mib[1] == PF_KEY) {
    494 			len = sysctl_3rd(&keyvars, string, &bufp, mib, flags,
    495 			    &type);
    496 			if (len >= 0)
    497 				break;
    498 			return;
    499 		}
    500 #endif /* IPSEC */
    501 		if (flags == 0)
    502 			return;
    503 		USEAPP(string, "netstat");
    504 		return;
    505 
    506 	case CTL_DEBUG:
    507 		mib[2] = CTL_DEBUG_VALUE;
    508 		len = 3;
    509 		break;
    510 
    511 	case CTL_MACHDEP:
    512 #ifdef CPU_CONSDEV
    513 		if (mib[1] == CPU_CONSDEV)
    514 			special = CONSDEV;
    515 #endif
    516 #ifdef CPU_DISKINFO
    517 		if (mib[1] == CPU_DISKINFO)
    518 			special = DISKINFO;
    519 #endif
    520 		break;
    521 
    522 	case CTL_VFS:
    523 		if (mib[1] == VFS_GENERIC) {
    524 			len = sysctl_3rd(&vfsgenvars, string, &bufp, mib, flags,
    525 			    &type);
    526 			/* Don't bother with VFS_CONF. */
    527 			if (mib[2] == VFS_CONF)
    528 				len = -1;
    529 		} else
    530 			len = sysctl_vfs(string, &bufp, mib, flags, &type);
    531 		if (len < 0)
    532 			return;
    533 
    534 		/* XXX Special-case for NFS stats. */
    535 		if (mib[1] == 2 && mib[2] == NFS_NFSSTATS) {
    536 			USEAPP(string, "nfsstat");
    537 			return;
    538 		}
    539 		break;
    540 
    541 	case CTL_VENDOR:
    542 	case CTL_USER:
    543 	case CTL_DDB:
    544 		break;
    545 	case CTL_PROC:
    546 		len = sysctl_proc(string, &bufp, mib, flags, &type);
    547 		if (len < 0)
    548 			return;
    549 		break;
    550 	case CTL_EMUL:
    551 		switch (mib[1]) {
    552 		case EMUL_IRIX:
    553 		    len = sysctl_irix(string, &bufp, mib, flags, &type);
    554 		    break;
    555 
    556 		case EMUL_LINUX:
    557 		    len = sysctl_linux(string, &bufp, mib, flags, &type);
    558 		    break;
    559 
    560 		case EMUL_DARWIN:
    561 		    len = sysctl_darwin(string, &bufp, mib, flags, &type);
    562 		    break;
    563 
    564 		default:
    565 		    warnx("Illegal emul level value: %d", mib[0]);
    566 		    break;
    567 		}
    568 		if (len < 0)
    569 			return;
    570 		break;
    571 	default:
    572 		warnx("Illegal top level value: %d", mib[0]);
    573 		return;
    574 
    575 	}
    576 	if (bufp) {
    577 		warnx("Name %s in %s is unknown", bufp, string);
    578 		return;
    579 	}
    580 	if (newsize > 0) {
    581 		switch (type) {
    582 		case CTLTYPE_INT:
    583 			intval = atoi(newval);
    584 			newval = &intval;
    585 			newsize = sizeof intval;
    586 			break;
    587 
    588 		case CTLTYPE_LIMIT:
    589 			if (strcmp(newval, "unlimited") == 0) {
    590 				quadval = RLIM_INFINITY;
    591 				newval = &quadval;
    592 				newsize = sizeof quadval;
    593 				break;
    594 			}
    595 			/* FALLTHROUGH */
    596 		case CTLTYPE_QUAD:
    597 			sscanf(newval, "%lld", &quadval);
    598 			newval = &quadval;
    599 			newsize = sizeof quadval;
    600 			break;
    601 		}
    602 	}
    603 	size = BUFSIZ;
    604 	if (sysctl(mib, len, buf, &size, newsize ? newval : 0, newsize) == -1) {
    605 		if (flags == 0)
    606 			return;
    607 		switch (errno) {
    608 		case EOPNOTSUPP:
    609 			fprintf(warnfp,
    610 			    "%s: the value is not available\n", string);
    611 			return;
    612 		case ENOTDIR:
    613 			fprintf(warnfp,
    614 			    "%s: the specification is incomplete\n", string);
    615 			return;
    616 		case ENOMEM:
    617 			fprintf(warnfp,
    618 			    "%s: this type is unknown to this program\n",
    619 			    string);
    620 			return;
    621 		default:
    622 			fprintf(warnfp, "%s: sysctl() failed with %s\n",
    623 			    string, strerror(errno));
    624 			return;
    625 		}
    626 	}
    627 	if (qflag && (newsize > 0))
    628 		return;
    629 	if (special == CLOCK) {
    630 		struct clockinfo *clkp = (struct clockinfo *)buf;
    631 
    632 		if (!nflag)
    633 			printf("%s: ", string);
    634 		printf(
    635 		    "tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n",
    636 		    clkp->tick, clkp->tickadj, clkp->hz, clkp->profhz, clkp->stathz);
    637 		return;
    638 	}
    639 	if (special == BOOTTIME) {
    640 		struct timeval *btp = (struct timeval *)buf;
    641 		time_t boottime;
    642 
    643 		if (!nflag) {
    644 			boottime = btp->tv_sec;
    645 			/* ctime() provides the trailing newline */
    646 			printf("%s%s%s", string, eq, ctime(&boottime));
    647 		} else
    648 			printf("%ld\n", (long) btp->tv_sec);
    649 		return;
    650 	}
    651 	if (special == CONSDEV) {
    652 		dev_t dev = *(dev_t *)buf;
    653 
    654 		if (!nflag)
    655 			printf("%s%s%s\n", string, eq, devname(dev, S_IFCHR));
    656 		else
    657 			printf("0x%x\n", dev);
    658 		return;
    659 	}
    660 	if (special == DISKINFO) {
    661 #ifdef CPU_DISKINFO
    662 		/* Don't know a good way to deal with this i386 specific one */
    663 		/* OTOH this does pass the info out... */
    664 		struct disklist *dl = (void *)buf;
    665 		struct biosdisk_info *bi;
    666 		struct nativedisk_info *ni;
    667 		uint i, b, lim;
    668 		if (!nflag)
    669 			printf("%s: ", string);
    670 		lim = dl->dl_nbiosdisks;
    671 		if (lim > MAX_BIOSDISKS)
    672 			lim = MAX_BIOSDISKS;
    673 		for (bi = dl->dl_biosdisks, i = 0; i < lim; bi++, i++)
    674 			printf("%x:%lld(%d/%d/%d),%x ",
    675 				bi->bi_dev, (long long)bi->bi_lbasecs,
    676 				bi->bi_cyl, bi->bi_head, bi->bi_sec,
    677 				bi->bi_flags);
    678 		lim = dl->dl_nnativedisks;
    679 		ni = dl->dl_nativedisks;
    680 		bi = dl->dl_biosdisks;
    681 		if ((char *)&ni[lim] != (char *)buf + size) {
    682 			fprintf(warnfp, "size mismatch\n");
    683 			return;
    684 		}
    685 		for (i = 0; i < lim; ni++, i++) {
    686 			char sep = ':';
    687 			printf(" %.*s", (int)sizeof ni->ni_devname,
    688 				ni->ni_devname);
    689 			for (b = 0; b < ni->ni_nmatches; sep = ',', b++)
    690 				printf("%c%x", sep,
    691 					bi[ni->ni_biosmatches[b]].bi_dev);
    692 		}
    693 		printf("\n");
    694 #endif
    695 		return;
    696 	}
    697 	if (special == CPTIME) {
    698 		u_int64_t *cp_time = (u_int64_t *)buf;
    699 
    700 		if (!nflag)
    701 			printf("%s: ", string);
    702 		printf("user = %llu, nice = %llu, sys = %llu, intr = %llu, "
    703 		    "idle = %llu\n", (unsigned long long) cp_time[0],
    704 		    (unsigned long long) cp_time[1],
    705 		    (unsigned long long) cp_time[2],
    706 		    (unsigned long long) cp_time[3],
    707 		    (unsigned long long) cp_time[4]);
    708 		return;
    709 	}
    710 	if (special == CNMAGIC) {
    711 		int i;
    712 
    713 		if (!nflag)
    714 			printf("%s%s", string, eq);
    715 		for (i = 0; i < size - 1; i++)
    716 			printf("\\x%2.2x", buf[i]);
    717 		if (newsize != 0) {
    718 			printf(" -> ");
    719 			for (i = 0; i < newsize - 1; i++)
    720 				printf("\\x%2.2x", ((unsigned char *)newval)[i]);
    721 		}
    722 		printf("\n");
    723 		return;
    724 	}
    725 
    726 	switch (type) {
    727 	case CTLTYPE_INT:
    728 		if (newsize == 0) {
    729 			if (!nflag)
    730 				printf("%s%s", string, eq);
    731 			printf("%d\n", *(int *)buf);
    732 		} else {
    733 			if (!nflag)
    734 				printf("%s: %d -> ", string, *(int *)buf);
    735 			printf("%d\n", *(int *)newval);
    736 		}
    737 		return;
    738 
    739 	case CTLTYPE_STRING:
    740 		/* If sysctl() didn't return any data, don't print a string. */
    741 		if (size == 0)
    742 			buf[0] = '\0';
    743 		if (newsize == 0) {
    744 			if (!nflag)
    745 				printf("%s%s", string, eq);
    746 			printf("%s\n", buf);
    747 		} else {
    748 			if (!nflag)
    749 				printf("%s: %s -> ", string, buf);
    750 			printf("%s\n", (char *) newval);
    751 		}
    752 		return;
    753 
    754 	case CTLTYPE_LIMIT:
    755 #define PRINTF_LIMIT(lim) { \
    756 if ((lim) == RLIM_INFINITY) \
    757 	printf("unlimited");\
    758 else \
    759 	printf("%lld", (long long)(lim)); \
    760 }
    761 
    762 		if (newsize == 0) {
    763 			if (!nflag)
    764 				printf("%s%s", string, eq);
    765 			PRINTF_LIMIT((long long)(*(quad_t *)buf));
    766 		} else {
    767 			if (!nflag) {
    768 				printf("%s: ", string);
    769 				PRINTF_LIMIT((long long)(*(quad_t *)buf));
    770 				printf(" -> ");
    771 			}
    772 			PRINTF_LIMIT((long long)(*(quad_t *)newval));
    773 		}
    774 		printf("\n");
    775 		return;
    776 #undef PRINTF_LIMIT
    777 
    778 	case CTLTYPE_QUAD:
    779 		if (newsize == 0) {
    780 			if (!nflag)
    781 				printf("%s%s", string, eq);
    782 			printf("%lld\n", (long long)(*(quad_t *)buf));
    783 		} else {
    784 			if (!nflag)
    785 				printf("%s: %lld -> ", string,
    786 				    (long long)(*(quad_t *)buf));
    787 			printf("%lld\n", (long long)(*(quad_t *)newval));
    788 		}
    789 		return;
    790 
    791 	case CTLTYPE_STRUCT:
    792 		warnx("%s: unknown structure returned", string);
    793 		return;
    794 
    795 	default:
    796 	case CTLTYPE_NODE:
    797 		warnx("%s: unknown type returned", string);
    798 		return;
    799 	}
    800 }
    801 
    802 /*
    803  * Initialize the set of debugging names
    804  */
    805 static void
    806 debuginit(void)
    807 {
    808 	int mib[3], loc, i;
    809 	size_t size;
    810 
    811 	if (secondlevel[CTL_DEBUG].list != 0)
    812 		return;
    813 	secondlevel[CTL_DEBUG].list = debugname;
    814 	mib[0] = CTL_DEBUG;
    815 	mib[2] = CTL_DEBUG_NAME;
    816 	for (loc = 0, i = 0; i < CTL_DEBUG_MAXID; i++) {
    817 		mib[1] = i;
    818 		size = BUFSIZ - loc;
    819 		if (sysctl(mib, 3, &names[loc], &size, NULL, 0) == -1)
    820 			continue;
    821 		debugname[i].ctl_name = &names[loc];
    822 		debugname[i].ctl_type = CTLTYPE_INT;
    823 		loc += size;
    824 	}
    825 }
    826 
    827 struct ctlname inetname[] = CTL_IPPROTO_NAMES;
    828 struct ctlname ipname[] = IPCTL_NAMES;
    829 struct ctlname icmpname[] = ICMPCTL_NAMES;
    830 struct ctlname tcpname[] = TCPCTL_NAMES;
    831 struct ctlname udpname[] = UDPCTL_NAMES;
    832 #ifdef IPSEC
    833 struct ctlname ipsecname[] = IPSECCTL_NAMES;
    834 #endif
    835 struct list inetlist = { inetname, IPPROTO_MAXID };
    836 struct list inetvars[] = {
    837 /*0*/	{ ipname, IPCTL_MAXID },	/* ip */
    838 	{ icmpname, ICMPCTL_MAXID },	/* icmp */
    839 	{ 0, 0 },			/* igmp */
    840 	{ 0, 0 },			/* ggmp */
    841 	{ 0, 0 },
    842 	{ 0, 0 },
    843 	{ tcpname, TCPCTL_MAXID },	/* tcp */
    844 	{ 0, 0 },
    845 	{ 0, 0 },			/* egp */
    846 	{ 0, 0 },
    847 /*10*/	{ 0, 0 },
    848 	{ 0, 0 },
    849 	{ 0, 0 },			/* pup */
    850 	{ 0, 0 },
    851 	{ 0, 0 },
    852 	{ 0, 0 },
    853 	{ 0, 0 },
    854 	{ udpname, UDPCTL_MAXID },	/* udp */
    855 	{ 0, 0 },
    856 	{ 0, 0 },
    857 /*20*/	{ 0, 0 },
    858 	{ 0, 0 },
    859 	{ 0, 0 },			/* idp */
    860 	{ 0, 0 },
    861 	{ 0, 0 },
    862 	{ 0, 0 },
    863 	{ 0, 0 },
    864 	{ 0, 0 },
    865 	{ 0, 0 },
    866 	{ 0, 0 },
    867 /*30*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    868 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    869 /*40*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    870 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    871 #ifdef IPSEC
    872 	{ ipsecname, IPSECCTL_MAXID },	/* esp - for backward compatibility */
    873 	{ ipsecname, IPSECCTL_MAXID },	/* ah */
    874 #else
    875 	{ 0, 0 },
    876 	{ 0, 0 },
    877 #endif
    878 };
    879 
    880 /*
    881  * handle internet requests
    882  */
    883 static int
    884 sysctl_inet(char *string, char **bufpp, int mib[], int flags, int *typep)
    885 {
    886 	struct list *lp;
    887 	int indx;
    888 
    889 	if (*bufpp == NULL) {
    890 		listall(string, &inetlist);
    891 		return (-1);
    892 	}
    893 	if ((indx = findname(string, "third", bufpp, &inetlist)) == -1)
    894 		return (-1);
    895 	mib[2] = indx;
    896 	if (indx <= IPPROTO_MAXID && inetvars[indx].list != NULL)
    897 		lp = &inetvars[indx];
    898 	else if (!flags)
    899 		return (-1);
    900 	else {
    901 		fprintf(warnfp,
    902 		    "%s: no variables defined for protocol\n", string);
    903 		return (-1);
    904 	}
    905 	if (*bufpp == NULL) {
    906 		listall(string, lp);
    907 		return (-1);
    908 	}
    909 	if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
    910 		return (-1);
    911 	mib[3] = indx;
    912 	*typep = lp->list[indx].ctl_type;
    913 	return (4);
    914 }
    915 
    916 #ifdef INET6
    917 struct ctlname inet6name[] = CTL_IPV6PROTO_NAMES;
    918 struct ctlname ip6name[] = IPV6CTL_NAMES;
    919 struct ctlname icmp6name[] = ICMPV6CTL_NAMES;
    920 struct ctlname udp6name[] = UDP6CTL_NAMES;
    921 struct ctlname pim6name[] = PIM6CTL_NAMES;
    922 struct ctlname ipsec6name[] = IPSEC6CTL_NAMES;
    923 struct list inet6list = { inet6name, IPV6PROTO_MAXID };
    924 struct list inet6vars[] = {
    925 /*0*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    926 	{ 0, 0 },
    927 	{ tcpname, TCPCTL_MAXID },	/* tcp6 */
    928 	{ 0, 0 },
    929 	{ 0, 0 },
    930 	{ 0, 0 },
    931 /*10*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    932 	{ 0, 0 },
    933 	{ 0, 0 },
    934 	{ udp6name, UDP6CTL_MAXID },	/* udp6 */
    935 	{ 0, 0 },
    936 	{ 0, 0 },
    937 /*20*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    938 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    939 /*30*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    940 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    941 /*40*/	{ 0, 0 },
    942 	{ ip6name, IPV6CTL_MAXID },	/* ipv6 */
    943 	{ 0, 0 },
    944 	{ 0, 0 },
    945 	{ 0, 0 },
    946 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    947 #ifdef IPSEC
    948 /*50*/	{ ipsec6name, IPSECCTL_MAXID },	/* esp6 - for backward compatibility */
    949 	{ ipsec6name, IPSECCTL_MAXID },	/* ah6 */
    950 #else
    951 	{ 0, 0 },
    952 	{ 0, 0 },
    953 #endif
    954 	{ 0, 0 },
    955 	{ 0, 0 },
    956 	{ 0, 0 },
    957 	{ 0, 0 },
    958 	{ 0, 0 },
    959 	{ 0, 0 },
    960 	{ icmp6name, ICMPV6CTL_MAXID },	/* icmp6 */
    961 	{ 0, 0 },
    962 /*60*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    963 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    964 /*70*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    965 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    966 /*80*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    967 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    968 /*90*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    969 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    970 /*100*/	{ 0, 0 },
    971 	{ 0, 0 },
    972 	{ 0, 0 },
    973 	{ pim6name, PIM6CTL_MAXID },	/* pim6 */
    974 };
    975 
    976 /*
    977  * handle internet6 requests
    978  */
    979 static int
    980 sysctl_inet6(char *string, char **bufpp, int mib[], int flags, int *typep)
    981 {
    982 	struct list *lp;
    983 	int indx;
    984 
    985 	if (*bufpp == NULL) {
    986 		listall(string, &inet6list);
    987 		return (-1);
    988 	}
    989 	if ((indx = findname(string, "third", bufpp, &inet6list)) == -1)
    990 		return (-1);
    991 	mib[2] = indx;
    992 	if (indx <= sizeof(inet6vars)/sizeof(inet6vars[0])
    993 	 && inet6vars[indx].list != NULL) {
    994 		lp = &inet6vars[indx];
    995 	} else if (!flags) {
    996 		return (-1);
    997 	} else {
    998 		fprintf(stderr, "%s: no variables defined for this protocol\n",
    999 		    string);
   1000 		return (-1);
   1001 	}
   1002 	if (*bufpp == NULL) {
   1003 		listall(string, lp);
   1004 		return (-1);
   1005 	}
   1006 	if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
   1007 		return (-1);
   1008 	mib[3] = indx;
   1009 	*typep = lp->list[indx].ctl_type;
   1010 	return (4);
   1011 }
   1012 #endif /* INET6 */
   1013 
   1014 struct ctlname ffsname[] = FFS_NAMES;
   1015 struct ctlname nfsname[] = NFS_NAMES;
   1016 struct list vfsvars[] = {
   1017 	{ 0, 0 },			/* generic */
   1018 	{ ffsname, FFS_MAXID },		/* FFS */
   1019 	{ nfsname, NFS_MAXID },		/* NFS */
   1020 	{ 0, 0 },			/* MFS */
   1021 	{ 0, 0 },			/* MSDOS */
   1022 	{ 0, 0 },			/* LFS */
   1023 	{ 0, 0 },			/* old LOFS */
   1024 	{ 0, 0 },			/* FDESC */
   1025 	{ 0, 0 },			/* PORTAL */
   1026 	{ 0, 0 },			/* NULL */
   1027 	{ 0, 0 },			/* UMAP */
   1028 	{ 0, 0 },			/* KERNFS */
   1029 	{ 0, 0 },			/* PROCFS */
   1030 	{ 0, 0 },			/* AFS */
   1031 	{ 0, 0 },			/* CD9660 */
   1032 	{ 0, 0 },			/* UNION */
   1033 	{ 0, 0 },			/* ADOSFS */
   1034 	{ 0, 0 },			/* EXT2FS */
   1035 	{ 0, 0 },			/* CODA */
   1036 	{ 0, 0 },			/* FILECORE */
   1037 };
   1038 
   1039 /*
   1040  * handle vfs requests
   1041  */
   1042 static int
   1043 sysctl_vfs(char *string, char **bufpp, int mib[], int flags, int *typep)
   1044 {
   1045 	struct list *lp = &vfsvars[mib[1]];
   1046 	int indx;
   1047 
   1048 	if (lp->list == NULL) {
   1049 		if (flags)
   1050 			fprintf(warnfp,
   1051 			    "%s: no variables defined for file system\n",
   1052 			    string);
   1053 		return (-1);
   1054 	}
   1055 	if (*bufpp == NULL) {
   1056 		listall(string, lp);
   1057 		return (-1);
   1058 	}
   1059 	if ((indx = findname(string, "third", bufpp, lp)) == -1)
   1060 		return (-1);
   1061 	mib[2] = indx;
   1062 	*typep = lp->list[indx].ctl_type;
   1063 	return (3);
   1064 }
   1065 
   1066 /*
   1067  * handle 3rd level requests.
   1068  */
   1069 static int
   1070 sysctl_3rd(struct list *lp, char *string, char **bufpp, int mib[], int flags,
   1071     int *typep)
   1072 {
   1073 	int indx;
   1074 
   1075 	if (*bufpp == NULL) {
   1076 		listall(string, lp);
   1077 		return (-1);
   1078 	}
   1079 	if ((indx = findname(string, "third", bufpp, lp)) == -1)
   1080 		return (-1);
   1081 	mib[2] = indx;
   1082 	*typep = lp->list[indx].ctl_type;
   1083 	return (3);
   1084 }
   1085 
   1086 struct ctlname procnames[] = PROC_PID_NAMES;
   1087 struct list procvars = {procnames, PROC_PID_MAXID};
   1088 struct ctlname proclimitnames[] = PROC_PID_LIMIT_NAMES;
   1089 struct list proclimitvars = {proclimitnames, PROC_PID_LIMIT_MAXID};
   1090 struct ctlname proclimittypenames[] = PROC_PID_LIMIT_TYPE_NAMES;
   1091 struct list proclimittypevars = {proclimittypenames,
   1092     PROC_PID_LIMIT_TYPE_MAXID};
   1093 /*
   1094  * handle kern.proc requests
   1095  */
   1096 static int
   1097 sysctl_proc(char *string, char **bufpp, int mib[], int flags, int *typep)
   1098 {
   1099 	char *cp, name[BUFSIZ];
   1100 	struct list *lp;
   1101 	int indx;
   1102 
   1103 	if (*bufpp == NULL) {
   1104 		strlcpy(name, string, sizeof(name));
   1105 		strlcat(name, ".curproc", sizeof(name));
   1106 		parse(name, Aflag);
   1107 		return (-1);
   1108 	}
   1109 	cp = strsep(bufpp, ".");
   1110 	if (cp == NULL) {
   1111 		warnx("%s: incomplete specification", string);
   1112 		return (-1);
   1113 	}
   1114 	if (strcmp(cp, "curproc") == 0) {
   1115 		mib[1] = PROC_CURPROC;
   1116 	} else {
   1117 		mib[1] = atoi(cp);
   1118 		if (mib[1] == 0) {
   1119 			warnx("second level name %s in %s is invalid", cp,
   1120 			    string);
   1121 			return (-1);
   1122 		}
   1123 	}
   1124 	*typep = CTLTYPE_NODE;
   1125 	lp = &procvars;
   1126 	if (*bufpp == NULL) {
   1127 		listall(string, lp);
   1128 		return (-1);
   1129 	}
   1130 	if ((indx = findname(string, "third", bufpp, lp)) == -1)
   1131 		return (-1);
   1132 	mib[2] = indx;
   1133 	*typep = lp->list[indx].ctl_type;
   1134 	if (*typep != CTLTYPE_NODE)
   1135 		return(3);
   1136 	lp = &proclimitvars;
   1137 	if (*bufpp == NULL) {
   1138 		listall(string, lp);
   1139 		return (-1);
   1140 	}
   1141 	if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
   1142 		return (-1);
   1143 	mib[3] = indx;
   1144 	lp = &proclimittypevars;
   1145 	if (*bufpp == NULL) {
   1146 		listall(string, lp);
   1147 		return (-1);
   1148 	}
   1149 	if ((indx = findname(string, "fifth", bufpp, lp)) == -1)
   1150 		return (-1);
   1151 	mib[4] = indx;
   1152 	*typep = CTLTYPE_LIMIT;
   1153 	return(5);
   1154 }
   1155 
   1156 struct ctlname linuxnames[] = EMUL_LINUX_NAMES;
   1157 struct list linuxvars = { linuxnames, EMUL_LINUX_MAXID };
   1158 struct ctlname linuxkernnames[] = EMUL_LINUX_KERN_NAMES;
   1159 struct list linuxkernvars = { linuxkernnames, EMUL_LINUX_KERN_MAXID };
   1160 
   1161 static int
   1162 sysctl_linux(char *string, char **bufpp, int mib[], int flags, int *typep)
   1163 {
   1164 	struct list *lp = &linuxvars;
   1165 	int indx;
   1166 	char name[BUFSIZ];
   1167 
   1168 	if (*bufpp == NULL) {
   1169 		strlcpy(name, string, sizeof(name));
   1170 		strlcat(name, ".kern", sizeof(name));
   1171 		listall(name, &linuxkernvars);
   1172 		return (-1);
   1173 	}
   1174 	if ((indx = findname(string, "third", bufpp, lp)) == -1)
   1175 		return (-1);
   1176 	mib[2] = indx;
   1177 	lp = &linuxkernvars;
   1178 	*typep = lp->list[indx].ctl_type;
   1179 	if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
   1180 		return (-1);
   1181 	mib[3] = indx;
   1182 	*typep = lp->list[indx].ctl_type;
   1183 	return (4);
   1184 }
   1185 
   1186 struct ctlname irixnames[] = EMUL_IRIX_NAMES;
   1187 struct list irixvars = { irixnames, EMUL_IRIX_MAXID };
   1188 struct ctlname irixkernnames[] = EMUL_IRIX_KERN_NAMES;
   1189 struct list irixkernvars = { irixkernnames, EMUL_IRIX_KERN_MAXID };
   1190 
   1191 static int
   1192 sysctl_irix(char *string, char **bufpp, int mib[], int flags, int *typep)
   1193 {
   1194 	struct list *lp = &irixvars;
   1195 	int indx;
   1196 	char name[BUFSIZ];
   1197 
   1198 	if (*bufpp == NULL) {
   1199 		strlcpy(name, string, sizeof(name));
   1200 		strlcat(name, ".kern", sizeof(name));
   1201 		listall(name, &irixkernvars);
   1202 		return (-1);
   1203 	}
   1204 	if ((indx = findname(string, "third", bufpp, lp)) == -1)
   1205 		return (-1);
   1206 	mib[2] = indx;
   1207 	lp = &irixkernvars;
   1208 	*typep = lp->list[indx].ctl_type;
   1209 	if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
   1210 		return (-1);
   1211 	mib[3] = indx;
   1212 	*typep = lp->list[indx].ctl_type;
   1213 	return (4);
   1214 }
   1215 
   1216 struct ctlname darwinnames[] = EMUL_DARWIN_NAMES;
   1217 struct list darwinvars = { darwinnames, EMUL_DARWIN_MAXID };
   1218 
   1219 static int
   1220 sysctl_darwin(char *string, char **bufpp, int mib[], int flags, int *typep)
   1221 {
   1222 	struct list *lp = &darwinvars;
   1223 	int indx;
   1224 
   1225 	if (*bufpp == NULL) {
   1226 		listall(string, &darwinvars);
   1227 		return (-1);
   1228 	}
   1229 	if ((indx = findname(string, "third", bufpp, lp)) == -1)
   1230 		return (-1);
   1231 	mib[2] = indx;
   1232 	lp = &darwinvars;
   1233 	*typep = lp->list[indx].ctl_type;
   1234 	return (3);
   1235 }
   1236 
   1237 /*
   1238  * Scan a list of names searching for a particular name.
   1239  */
   1240 static int
   1241 findname(char *string, char *level, char **bufp, struct list *namelist)
   1242 {
   1243 	char *name;
   1244 	int i;
   1245 
   1246 	if (namelist->list == 0 || (name = strsep(bufp, ".")) == NULL) {
   1247 		warnx("%s: incomplete specification", string);
   1248 		return (-1);
   1249 	}
   1250 	for (i = 0; i < namelist->size; i++)
   1251 		if (namelist->list[i].ctl_name != NULL &&
   1252 		    strcmp(name, namelist->list[i].ctl_name) == 0)
   1253 			break;
   1254 	if (i == namelist->size) {
   1255 		warnx("%s level name %s in %s is invalid",
   1256 		    level, name, string);
   1257 		return (-1);
   1258 	}
   1259 	return (i);
   1260 }
   1261 
   1262 static void
   1263 usage(void)
   1264 {
   1265 	const char *progname = getprogname();
   1266 
   1267 	(void)fprintf(stderr,
   1268 	    "Usage:\t%s %s\n\t%s %s\n\t%s %s\n\t%s %s\n\t%s %s\n",
   1269 	    progname, "[-n] [-e] variable ...",
   1270 	    progname, "[-n] [-q] -w variable=value ...",
   1271 	    progname, "[-n] [-e] -a",
   1272 	    progname, "[-n] [-e] -A",
   1273 	    progname, "[-n] [-q] -f file");
   1274 	exit(1);
   1275 }
   1276