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