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