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