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