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