Home | History | Annotate | Line # | Download | only in sysctl
sysctl.c revision 1.25
      1 /*	$NetBSD: sysctl.c,v 1.25 2000/02/12 18:00:58 thorpej 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.25 2000/02/12 18:00:58 thorpej 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 (mib[1] == VM_NKMEMPAGES) {
    378 			size_t nkmempages_len;
    379 			int nkmempages;
    380 
    381 			nkmempages_len = sizeof(nkmempages);
    382 
    383 			if (sysctl(mib, 2, &nkmempages, &nkmempages_len,
    384 			    NULL, 0)) {
    385 				warn("unable to get %s", string);
    386 				return;
    387 			}
    388 			if (!nflag)
    389 				fprintf(stdout, "%s: ", string);
    390 			fprintf(stdout, "%d\n", nkmempages);
    391 		}
    392 		if (flags == 0)
    393 			return;
    394 		warnx("Use vmstat or systat to view %s information", string);
    395 		return;
    396 
    397 	case CTL_NET:
    398 		if (mib[1] == PF_INET) {
    399 			len = sysctl_inet(string, &bufp, mib, flags, &type);
    400 			if (len >= 0)
    401 				break;
    402 			return;
    403 		}
    404 #ifdef INET6
    405 		else if (mib[1] == PF_INET6) {
    406 			len = sysctl_inet6(string, &bufp, mib, flags, &type);
    407 			if (len >= 0)
    408 				break;
    409 			return;
    410 		}
    411 #endif /* INET6 */
    412 #ifdef IPSEC
    413 		else if (mib[1] == PF_KEY) {
    414 			len = sysctl_key(string, &bufp, mib, flags, &type);
    415 			if (len >= 0)
    416 				break;
    417 			return;
    418 		}
    419 #endif /* IPSEC */
    420 		if (flags == 0)
    421 			return;
    422 		warnx("Use netstat to view %s information", string);
    423 		return;
    424 
    425 	case CTL_DEBUG:
    426 		mib[2] = CTL_DEBUG_VALUE;
    427 		len = 3;
    428 		break;
    429 
    430 	case CTL_MACHDEP:
    431 #ifdef CPU_CONSDEV
    432 		if (mib[1] == CPU_CONSDEV)
    433 			special |= CONSDEV;
    434 #endif
    435 		break;
    436 
    437 	case CTL_VFS:
    438 		if (mib[1] == VFS_GENERIC)
    439 			len = sysctl_vfsgen(string, &bufp, mib, flags, &type);
    440 		else
    441 			len = sysctl_vfs(string, &bufp, mib, flags, &type);
    442 		if (len < 0)
    443 			return;
    444 
    445 		/* XXX Special-case for NFS stats. */
    446 		if (mib[1] == 2 && mib[2] == NFS_NFSSTATS) {
    447 			if (flags == 0)
    448 				return;
    449 			warnx("Use nfsstat to view %s information", string);
    450 			return;
    451 		}
    452 		break;
    453 
    454 	case CTL_USER:
    455 	case CTL_DDB:
    456 		break;
    457 	case CTL_PROC:
    458 		len = sysctl_proc(string, &bufp, mib, flags, &type);
    459 		if (len < 0)
    460 			return;
    461 		break;
    462 	default:
    463 		warnx("Illegal top level value: %d", mib[0]);
    464 		return;
    465 
    466 	}
    467 	if (bufp) {
    468 		warnx("Name %s in %s is unknown", bufp, string);
    469 		return;
    470 	}
    471 	if (newsize > 0) {
    472 		switch (type) {
    473 		case CTLTYPE_INT:
    474 			intval = atoi(newval);
    475 			newval = &intval;
    476 			newsize = sizeof intval;
    477 			break;
    478 
    479 		case CTLTYPE_LIMIT:
    480 			if (strcmp(newval, "unlimited") == 0) {
    481 				quadval = RLIM_INFINITY;
    482 				newval = &quadval;
    483 				newsize = sizeof quadval;
    484 				break;
    485 			}
    486 			/* FALLTHROUGH */
    487 		case CTLTYPE_QUAD:
    488 			sscanf(newval, "%qd", (long long *)&quadval);
    489 			newval = &quadval;
    490 			newsize = sizeof quadval;
    491 			break;
    492 		}
    493 	}
    494 	size = BUFSIZ;
    495 	if (sysctl(mib, len, buf, &size, newsize ? newval : 0, newsize) == -1) {
    496 		if (flags == 0)
    497 			return;
    498 		switch (errno) {
    499 		case EOPNOTSUPP:
    500 			warnx("The value of %s is not available", string);
    501 			return;
    502 		case ENOTDIR:
    503 			warnx("The specification of %s is incomplete",
    504 			    string);
    505 			return;
    506 		case ENOMEM:
    507 			warnx("The type %s is unknown to this program",
    508 			    string);
    509 			return;
    510 		default:
    511 			warn("sysctl() for %s failed", string);
    512 			return;
    513 		}
    514 	}
    515 	if (special & CLOCK) {
    516 		struct clockinfo *clkp = (struct clockinfo *)buf;
    517 
    518 		if (!nflag)
    519 			fprintf(stdout, "%s: ", string);
    520 		fprintf(stdout,
    521 		    "tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n",
    522 		    clkp->tick, clkp->tickadj, clkp->hz, clkp->profhz, clkp->stathz);
    523 		return;
    524 	}
    525 	if (special & BOOTTIME) {
    526 		struct timeval *btp = (struct timeval *)buf;
    527 		time_t boottime;
    528 
    529 		if (!nflag) {
    530 			boottime = btp->tv_sec;
    531 			fprintf(stdout, "%s = %s\n", string, ctime(&boottime));
    532 		} else
    533 			fprintf(stdout, "%ld\n", (long) btp->tv_sec);
    534 		return;
    535 	}
    536 	if (special & CONSDEV) {
    537 		dev_t dev = *(dev_t *)buf;
    538 
    539 		if (!nflag)
    540 			fprintf(stdout, "%s = %s\n", string,
    541 			    devname(dev, S_IFCHR));
    542 		else
    543 			fprintf(stdout, "0x%x\n", dev);
    544 		return;
    545 	}
    546 	switch (type) {
    547 	case CTLTYPE_INT:
    548 		if (newsize == 0) {
    549 			if (!nflag)
    550 				fprintf(stdout, "%s = ", string);
    551 			fprintf(stdout, "%d\n", *(int *)buf);
    552 		} else {
    553 			if (!nflag)
    554 				fprintf(stdout, "%s: %d -> ", string,
    555 				    *(int *)buf);
    556 			fprintf(stdout, "%d\n", *(int *)newval);
    557 		}
    558 		return;
    559 
    560 	case CTLTYPE_STRING:
    561 		if (newsize == 0) {
    562 			if (!nflag)
    563 				fprintf(stdout, "%s = ", string);
    564 			fprintf(stdout, "%s\n", buf);
    565 		} else {
    566 			if (!nflag)
    567 				fprintf(stdout, "%s: %s -> ", string, buf);
    568 			fprintf(stdout, "%s\n", (char *) newval);
    569 		}
    570 		return;
    571 
    572 	case CTLTYPE_LIMIT:
    573 #define PRINTF_LIMIT(lim) { \
    574 if ((lim) == RLIM_INFINITY) \
    575 	fprintf(stdout, "unlimited");\
    576 else \
    577 	fprintf(stdout, "%qd", (lim)); \
    578 }
    579 
    580 		if (newsize == 0) {
    581 			if (!nflag)
    582 				fprintf(stdout, "%s = ", string);
    583 			PRINTF_LIMIT((long long)(*(quad_t *)buf));
    584 		} else {
    585 			if (!nflag) {
    586 				fprintf(stdout, "%s: ", string);
    587 				PRINTF_LIMIT((long long)(*(quad_t *)buf));
    588 				fprintf(stdout, " -> ");
    589 			}
    590 			PRINTF_LIMIT((long long)(*(quad_t *)newval));
    591 		}
    592 		fprintf(stdout, "\n");
    593 		return;
    594 #undef PRINTF_LIMIT
    595 
    596 	case CTLTYPE_QUAD:
    597 		if (newsize == 0) {
    598 			if (!nflag)
    599 				fprintf(stdout, "%s = ", string);
    600 			fprintf(stdout, "%qd\n", (long long)(*(quad_t *)buf));
    601 		} else {
    602 			if (!nflag)
    603 				fprintf(stdout, "%s: %qd -> ", string,
    604 				    (long long)(*(quad_t *)buf));
    605 			fprintf(stdout, "%qd\n",
    606 			    (long long)(*(quad_t *)newval));
    607 		}
    608 		return;
    609 
    610 	case CTLTYPE_STRUCT:
    611 		warnx("%s: unknown structure returned", string);
    612 		return;
    613 
    614 	default:
    615 	case CTLTYPE_NODE:
    616 		warnx("%s: unknown type returned", string);
    617 		return;
    618 	}
    619 }
    620 
    621 /*
    622  * Initialize the set of debugging names
    623  */
    624 static void
    625 debuginit()
    626 {
    627 	int mib[3], loc, i;
    628 	size_t size;
    629 
    630 	if (secondlevel[CTL_DEBUG].list != 0)
    631 		return;
    632 	secondlevel[CTL_DEBUG].list = debugname;
    633 	mib[0] = CTL_DEBUG;
    634 	mib[2] = CTL_DEBUG_NAME;
    635 	for (loc = 0, i = 0; i < CTL_DEBUG_MAXID; i++) {
    636 		mib[1] = i;
    637 		size = BUFSIZ - loc;
    638 		if (sysctl(mib, 3, &names[loc], &size, NULL, 0) == -1)
    639 			continue;
    640 		debugname[i].ctl_name = &names[loc];
    641 		debugname[i].ctl_type = CTLTYPE_INT;
    642 		loc += size;
    643 	}
    644 }
    645 
    646 struct ctlname inetname[] = CTL_IPPROTO_NAMES;
    647 struct ctlname ipname[] = IPCTL_NAMES;
    648 struct ctlname icmpname[] = ICMPCTL_NAMES;
    649 struct ctlname tcpname[] = TCPCTL_NAMES;
    650 struct ctlname udpname[] = UDPCTL_NAMES;
    651 #ifdef IPSEC
    652 struct ctlname ipsecname[] = IPSECCTL_NAMES;
    653 #endif
    654 struct list inetlist = { inetname, IPPROTO_MAXID };
    655 struct list inetvars[] = {
    656 /*0*/	{ ipname, IPCTL_MAXID },	/* ip */
    657 	{ icmpname, ICMPCTL_MAXID },	/* icmp */
    658 	{ 0, 0 },			/* igmp */
    659 	{ 0, 0 },			/* ggmp */
    660 	{ 0, 0 },
    661 	{ 0, 0 },
    662 	{ tcpname, TCPCTL_MAXID },	/* tcp */
    663 	{ 0, 0 },
    664 	{ 0, 0 },			/* egp */
    665 	{ 0, 0 },
    666 /*10*/	{ 0, 0 },
    667 	{ 0, 0 },
    668 	{ 0, 0 },			/* pup */
    669 	{ 0, 0 },
    670 	{ 0, 0 },
    671 	{ 0, 0 },
    672 	{ 0, 0 },
    673 	{ udpname, UDPCTL_MAXID },	/* udp */
    674 	{ 0, 0 },
    675 	{ 0, 0 },
    676 /*20*/	{ 0, 0 },
    677 	{ 0, 0 },
    678 	{ 0, 0 },			/* idp */
    679 	{ 0, 0 },
    680 	{ 0, 0 },
    681 	{ 0, 0 },
    682 	{ 0, 0 },
    683 	{ 0, 0 },
    684 	{ 0, 0 },
    685 	{ 0, 0 },
    686 /*30*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    687 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    688 /*40*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    689 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    690 #ifdef IPSEC
    691 	{ ipsecname, IPSECCTL_MAXID },	/* esp - for backward compatibility */
    692 	{ ipsecname, IPSECCTL_MAXID },	/* ah */
    693 #else
    694 	{ 0, 0 },
    695 	{ 0, 0 },
    696 #endif
    697 };
    698 
    699 /*
    700  * handle internet requests
    701  */
    702 static int
    703 sysctl_inet(string, bufpp, mib, flags, typep)
    704 	char *string;
    705 	char **bufpp;
    706 	int mib[];
    707 	int flags;
    708 	int *typep;
    709 {
    710 	struct list *lp;
    711 	int indx;
    712 
    713 	if (*bufpp == NULL) {
    714 		listall(string, &inetlist);
    715 		return (-1);
    716 	}
    717 	if ((indx = findname(string, "third", bufpp, &inetlist)) == -1)
    718 		return (-1);
    719 	mib[2] = indx;
    720 	if (indx <= IPPROTO_MAXID && inetvars[indx].list != NULL)
    721 		lp = &inetvars[indx];
    722 	else if (!flags)
    723 		return (-1);
    724 	else {
    725 		warnx("No variables defined for protocol %s", string);
    726 		return (-1);
    727 	}
    728 	if (*bufpp == NULL) {
    729 		listall(string, lp);
    730 		return (-1);
    731 	}
    732 	if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
    733 		return (-1);
    734 	mib[3] = indx;
    735 	*typep = lp->list[indx].ctl_type;
    736 	return (4);
    737 }
    738 
    739 #ifdef INET6
    740 struct ctlname inet6name[] = CTL_IPV6PROTO_NAMES;
    741 struct ctlname ip6name[] = IPV6CTL_NAMES;
    742 struct ctlname icmp6name[] = ICMPV6CTL_NAMES;
    743 #ifdef TCP6
    744 struct ctlname tcp6name[] = TCP6CTL_NAMES;
    745 #endif
    746 struct ctlname udp6name[] = UDP6CTL_NAMES;
    747 struct ctlname pim6name[] = PIMCTL_NAMES;
    748 struct ctlname ipsec6name[] = IPSEC6CTL_NAMES;
    749 struct list inet6list = { inet6name, IPV6PROTO_MAXID };
    750 struct list inet6vars[] = {
    751 /*0*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    752 	{ 0, 0 },
    753 #ifdef TCP6
    754 	{ tcp6name, TCP6CTL_MAXID },	/* tcp6 */
    755 #else
    756 	{ 0, 0 },
    757 #endif
    758 	{ 0, 0 },
    759 	{ 0, 0 },
    760 	{ 0, 0 },
    761 /*10*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    762 	{ 0, 0 },
    763 	{ 0, 0 },
    764 	{ udp6name, UDP6CTL_MAXID },	/* udp6 */
    765 	{ 0, 0 },
    766 	{ 0, 0 },
    767 /*20*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    768 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    769 /*30*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    770 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    771 /*40*/	{ 0, 0 },
    772 	{ ip6name, IPV6CTL_MAXID },	/* ipv6 */
    773 	{ 0, 0 },
    774 	{ 0, 0 },
    775 	{ 0, 0 },
    776 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    777 #ifdef IPSEC
    778 /*50*/	{ ipsec6name, IPSECCTL_MAXID },	/* esp6 - for backward compatibility */
    779 	{ ipsec6name, IPSECCTL_MAXID },	/* ah6 */
    780 #else
    781 	{ 0, 0 },
    782 	{ 0, 0 },
    783 #endif
    784 	{ 0, 0 },
    785 	{ 0, 0 },
    786 	{ 0, 0 },
    787 	{ 0, 0 },
    788 	{ 0, 0 },
    789 	{ 0, 0 },
    790 	{ icmp6name, ICMPV6CTL_MAXID },	/* icmp6 */
    791 	{ 0, 0 },
    792 /*60*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    793 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    794 /*70*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    795 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    796 /*80*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    797 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    798 /*90*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    799 	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
    800 /*100*/	{ 0, 0 },
    801 	{ 0, 0 },
    802 	{ 0, 0 },
    803 	{ pim6name, PIMCTL_MAXID },	/* pim6 */
    804 };
    805 
    806 /*
    807  * handle internet6 requests
    808  */
    809 static int
    810 sysctl_inet6(string, bufpp, mib, flags, typep)
    811 	char *string;
    812 	char **bufpp;
    813 	int mib[];
    814 	int flags;
    815 	int *typep;
    816 {
    817 	struct list *lp;
    818 	int indx;
    819 
    820 	if (*bufpp == NULL) {
    821 		listall(string, &inet6list);
    822 		return (-1);
    823 	}
    824 	if ((indx = findname(string, "third", bufpp, &inet6list)) == -1)
    825 		return (-1);
    826 	mib[2] = indx;
    827 	if (indx <= sizeof(inet6vars)/sizeof(inet6vars[0])
    828 	 && inet6vars[indx].list != NULL) {
    829 		lp = &inet6vars[indx];
    830 	} else if (!flags) {
    831 		return (-1);
    832 	} else {
    833 		fprintf(stderr, "%s: no variables defined for this protocol\n",
    834 		    string);
    835 		return (-1);
    836 	}
    837 	if (*bufpp == NULL) {
    838 		listall(string, lp);
    839 		return (-1);
    840 	}
    841 	if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
    842 		return (-1);
    843 	mib[3] = indx;
    844 	*typep = lp->list[indx].ctl_type;
    845 	return (4);
    846 }
    847 #endif /* INET6 */
    848 
    849 #ifdef IPSEC
    850 struct ctlname keynames[] = KEYCTL_NAMES;
    851 struct list keylist = { keynames, KEYCTL_MAXID };
    852 
    853 /*
    854  * handle key requests
    855  */
    856 static int
    857 sysctl_key(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, &keylist);
    869 		return (-1);
    870 	}
    871 	if ((indx = findname(string, "third", bufpp, &keylist)) == -1)
    872 		return (-1);
    873 	mib[2] = indx;
    874 	lp = &keylist;
    875 	*typep = lp->list[indx].ctl_type;
    876 	return 3;
    877 }
    878 #endif /*IPSEC*/
    879 
    880 struct ctlname ffsname[] = FFS_NAMES;
    881 struct ctlname nfsname[] = NFS_NAMES;
    882 struct list vfsvars[] = {
    883 	{ 0, 0 },			/* generic */
    884 	{ ffsname, FFS_MAXID },		/* FFS */
    885 	{ nfsname, NFS_MAXID },		/* NFS */
    886 	{ 0, 0 },			/* MFS */
    887 	{ 0, 0 },			/* MSDOS */
    888 	{ 0, 0 },			/* LFS */
    889 	{ 0, 0 },			/* old LOFS */
    890 	{ 0, 0 },			/* FDESC */
    891 	{ 0, 0 },			/* PORTAL */
    892 	{ 0, 0 },			/* NULL */
    893 	{ 0, 0 },			/* UMAP */
    894 	{ 0, 0 },			/* KERNFS */
    895 	{ 0, 0 },			/* PROCFS */
    896 	{ 0, 0 },			/* AFS */
    897 	{ 0, 0 },			/* CD9660 */
    898 	{ 0, 0 },			/* UNION */
    899 	{ 0, 0 },			/* ADOSFS */
    900 	{ 0, 0 },			/* EXT2FS */
    901 	{ 0, 0 },			/* CODA */
    902 	{ 0, 0 },			/* FILECORE */
    903 };
    904 
    905 /*
    906  * handle vfs requests
    907  */
    908 static int
    909 sysctl_vfs(string, bufpp, mib, flags, typep)
    910 	char *string;
    911 	char **bufpp;
    912 	int mib[];
    913 	int flags;
    914 	int *typep;
    915 {
    916 	struct list *lp = &vfsvars[mib[1]];
    917 	int indx;
    918 
    919 	if (lp->list == NULL) {
    920 		if (flags)
    921 			warnx("No variables defined for file system %s",
    922 			    string);
    923 		return (-1);
    924 	}
    925 	if (*bufpp == NULL) {
    926 		listall(string, lp);
    927 		return (-1);
    928 	}
    929 	if ((indx = findname(string, "third", bufpp, lp)) == -1)
    930 		return (-1);
    931 	mib[2] = indx;
    932 	*typep = lp->list[indx].ctl_type;
    933 	return (3);
    934 }
    935 
    936 struct ctlname vfsgenname[] = CTL_VFSGENCTL_NAMES;
    937 struct list vfsgenvars = { vfsgenname, VFSGEN_MAXID };
    938 
    939 /*
    940  * handle vfs.generic requests
    941  */
    942 static int
    943 sysctl_vfsgen(string, bufpp, mib, flags, typep)
    944 	char *string;
    945 	char **bufpp;
    946 	int mib[];
    947 	int flags;
    948 	int *typep;
    949 {
    950 	struct list *lp = &vfsgenvars;
    951 	int indx;
    952 
    953 	if (*bufpp == NULL) {
    954 		listall(string, lp);
    955 		return (-1);
    956 	}
    957 	if ((indx = findname(string, "third", bufpp, lp)) == -1)
    958 		return (-1);
    959 	/* Don't bother with VFS_CONF. */
    960 	if (indx == VFS_CONF)
    961 		return (-1);
    962 	mib[2] = indx;
    963 	*typep = lp->list[indx].ctl_type;
    964 	return (3);
    965 }
    966 
    967 struct ctlname procnames[] = PROC_PID_NAMES;
    968 struct list procvars = {procnames, PROC_PID_MAXID};
    969 struct ctlname proclimitnames[] = PROC_PID_LIMIT_NAMES;
    970 struct list proclimitvars = {proclimitnames, PROC_PID_LIMIT_MAXID};
    971 struct ctlname proclimittypenames[] = PROC_PID_LIMIT_TYPE_NAMES;
    972 struct list proclimittypevars = {proclimittypenames,
    973     PROC_PID_LIMIT_TYPE_MAXID};
    974 /*
    975  * handle kern.proc requests
    976  */
    977 static int
    978 sysctl_proc(string, bufpp, mib, flags, typep)
    979 	char *string;
    980 	char **bufpp;
    981 	int mib[];
    982 	int flags;
    983 	int *typep;
    984 {
    985 	char *cp, name[BUFSIZ];
    986 	struct list *lp;
    987 	int indx;
    988 
    989 	if (*bufpp == NULL) {
    990 		strcpy(name, string);
    991 		cp = &name[strlen(name)];
    992 		*cp++ = '.';
    993 		strcpy(cp, "curproc");
    994 		parse (name, Aflag);
    995 		return (-1);
    996 	}
    997 	cp = strsep(bufpp, ".");
    998 	if (cp == NULL) {
    999 		warnx("%s: incomplete specification", string);
   1000 		return (-1);
   1001 	}
   1002 	if (strcmp(cp, "curproc") == 0) {
   1003 		mib[1] = PROC_CURPROC;
   1004 	} else {
   1005 		mib[1] = atoi(cp);
   1006 		if (mib[1] == 0) {
   1007 			warnx("second level name %s in %s is invalid", cp,
   1008 			    string);
   1009 			return (-1);
   1010 		}
   1011 	}
   1012 	*typep = CTLTYPE_NODE;
   1013 	lp = &procvars;
   1014 	if (*bufpp == NULL) {
   1015 		listall(string, lp);
   1016 		return (-1);
   1017 	}
   1018 	if ((indx = findname(string, "third", bufpp, lp)) == -1)
   1019 		return (-1);
   1020 	mib[2] = indx;
   1021 	*typep = lp->list[indx].ctl_type;
   1022 	if (*typep != CTLTYPE_NODE)
   1023 		return(3);
   1024 	lp = &proclimitvars;
   1025 	if (*bufpp == NULL) {
   1026 		listall(string, lp);
   1027 		return (-1);
   1028 	}
   1029 	if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
   1030 		return (-1);
   1031 	mib[3] = indx;
   1032 	lp = &proclimittypevars;
   1033 	if (*bufpp == NULL) {
   1034 		listall(string, lp);
   1035 		return (-1);
   1036 	}
   1037 	if ((indx = findname(string, "fifth", bufpp, lp)) == -1)
   1038 		return (-1);
   1039 	mib[4] = indx;
   1040 	*typep = CTLTYPE_LIMIT;
   1041 	return(5);
   1042 }
   1043 
   1044 struct ctlname mbufnames[] = CTL_MBUF_NAMES;
   1045 struct list mbufvars = { mbufnames, MBUF_MAXID };
   1046 /*
   1047  * handle kern.mbuf requests
   1048  */
   1049 static int
   1050 sysctl_mbuf(string, bufpp, mib, flags, typep)
   1051 	char *string;
   1052 	char **bufpp;
   1053 	int mib[];
   1054 	int flags;
   1055 	int *typep;
   1056 {
   1057 	struct list *lp = &mbufvars;
   1058 	int indx;
   1059 
   1060 	if (*bufpp == NULL) {
   1061 		listall(string, lp);
   1062 		return (-1);
   1063 	}
   1064 	if ((indx = findname(string, "third", bufpp, lp)) == -1)
   1065 		return (-1);
   1066 	mib[2] = indx;
   1067 	*typep = lp->list[indx].ctl_type;
   1068 	return (3);
   1069 }
   1070 
   1071 /*
   1072  * Scan a list of names searching for a particular name.
   1073  */
   1074 static int
   1075 findname(string, level, bufp, namelist)
   1076 	char *string;
   1077 	char *level;
   1078 	char **bufp;
   1079 	struct list *namelist;
   1080 {
   1081 	char *name;
   1082 	int i;
   1083 
   1084 	if (namelist->list == 0 || (name = strsep(bufp, ".")) == NULL) {
   1085 		warnx("%s: incomplete specification", string);
   1086 		return (-1);
   1087 	}
   1088 	for (i = 0; i < namelist->size; i++)
   1089 		if (namelist->list[i].ctl_name != NULL &&
   1090 		    strcmp(name, namelist->list[i].ctl_name) == 0)
   1091 			break;
   1092 	if (i == namelist->size) {
   1093 		warnx("%s level name %s in %s is invalid",
   1094 		    level, name, string);
   1095 		return (-1);
   1096 	}
   1097 	return (i);
   1098 }
   1099 
   1100 static void
   1101 usage()
   1102 {
   1103 	extern char *__progname;
   1104 
   1105 	(void)fprintf(stderr, "Usage:\t%s %s\n\t%s %s\n\t%s %s\n\t%s %s\n",
   1106 	    __progname, "[-n] variable ...",
   1107 	    __progname, "[-n] -w variable=value ...",
   1108 	    __progname, "[-n] -a",
   1109 	    __progname, "[-n] -A");
   1110 	exit(1);
   1111 }
   1112