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