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