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