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