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