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