sysctl.c revision 1.159
1/*	$OpenBSD: sysctl.c,v 1.159 2008/07/12 12:04:10 thib Exp $	*/
2/*	$NetBSD: sysctl.c,v 1.9 1995/09/30 07:12:50 thorpej Exp $	*/
3
4/*
5 * Copyright (c) 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#ifndef lint
34static const char copyright[] =
35"@(#) Copyright (c) 1993\n\
36	The Regents of the University of California.  All rights reserved.\n";
37#endif /* not lint */
38
39#ifndef lint
40#if 0
41static const char sccsid[] = "@(#)sysctl.c	8.5 (Berkeley) 5/9/95";
42#else
43static const char rcsid[] = "$OpenBSD: sysctl.c,v 1.159 2008/07/12 12:04:10 thib Exp $";
44#endif
45#endif /* not lint */
46
47#include <sys/param.h>
48#include <sys/gmon.h>
49#include <sys/mount.h>
50#include <sys/stat.h>
51#include <sys/sem.h>
52#include <sys/shm.h>
53#include <sys/sysctl.h>
54#include <sys/socket.h>
55#include <sys/malloc.h>
56#include <sys/dkstat.h>
57#include <sys/uio.h>
58#include <sys/tty.h>
59#include <sys/namei.h>
60#include <sys/sensors.h>
61#include <machine/cpu.h>
62#include <net/route.h>
63#include <net/if.h>
64
65#include <netinet/in.h>
66#include <netinet/in_systm.h>
67#include <netinet/ip.h>
68#include <netinet/in_pcb.h>
69#include <netinet/ip_icmp.h>
70#include <netinet/ip_ipip.h>
71#include <netinet/ip_ether.h>
72#include <netinet/ip_ah.h>
73#include <netinet/ip_esp.h>
74#include <netinet/icmp_var.h>
75#include <netinet/igmp_var.h>
76#include <netinet/ip_var.h>
77#include <netinet/udp.h>
78#include <netinet/udp_var.h>
79#include <netinet/tcp.h>
80#include <netinet/tcp_timer.h>
81#include <netinet/tcp_var.h>
82#include <netinet/ip_gre.h>
83#include <netinet/ip_ipcomp.h>
84#include <netinet/ip_carp.h>
85
86#include <net/pfvar.h>
87#include <net/if_pfsync.h>
88
89#ifdef INET6
90#include <netinet/ip6.h>
91#include <netinet/icmp6.h>
92#include <netinet6/ip6_var.h>
93#include <netinet6/pim6_var.h>
94#endif
95
96#include <netmpls/mpls.h>
97
98#include <uvm/uvm_swap_encrypt.h>
99
100#include <ufs/ufs/quota.h>
101#include <ufs/ufs/inode.h>
102#include <ufs/ffs/fs.h>
103#include <ufs/ffs/ffs_extern.h>
104
105#include <nfs/rpcv2.h>
106#include <nfs/nfsproto.h>
107#include <nfs/nfs.h>
108
109#include <ddb/db_var.h>
110#include <dev/rndvar.h>
111
112#include <err.h>
113#include <errno.h>
114#include <stdio.h>
115#include <stdlib.h>
116#include <string.h>
117#include <ctype.h>
118
119#ifdef CPU_BIOS
120#include <machine/biosvar.h>
121#endif
122
123struct ctlname topname[] = CTL_NAMES;
124struct ctlname kernname[] = CTL_KERN_NAMES;
125struct ctlname vmname[] = CTL_VM_NAMES;
126struct ctlname fsname[] = CTL_FS_NAMES;
127struct ctlname netname[] = CTL_NET_NAMES;
128struct ctlname hwname[] = CTL_HW_NAMES;
129struct ctlname username[] = CTL_USER_NAMES;
130struct ctlname debugname[CTL_DEBUG_MAXID];
131struct ctlname kernmallocname[] = CTL_KERN_MALLOC_NAMES;
132struct ctlname forkstatname[] = CTL_KERN_FORKSTAT_NAMES;
133struct ctlname nchstatsname[] = CTL_KERN_NCHSTATS_NAMES;
134struct ctlname ttysname[] = CTL_KERN_TTY_NAMES;
135struct ctlname semname[] = CTL_KERN_SEMINFO_NAMES;
136struct ctlname shmname[] = CTL_KERN_SHMINFO_NAMES;
137struct ctlname watchdogname[] = CTL_KERN_WATCHDOG_NAMES;
138struct ctlname tcname[] = CTL_KERN_TIMECOUNTER_NAMES;
139struct ctlname *vfsname;
140#ifdef CTL_MACHDEP_NAMES
141struct ctlname machdepname[] = CTL_MACHDEP_NAMES;
142#endif
143struct ctlname ddbname[] = CTL_DDB_NAMES;
144char names[BUFSIZ];
145int lastused;
146
147/* Maximum size object to expect from sysctl(3) */
148#define SYSCTL_BUFSIZ	8192
149
150struct list {
151	struct	ctlname *list;
152	int	size;
153};
154struct list toplist = { topname, CTL_MAXID };
155struct list secondlevel[] = {
156	{ 0, 0 },			/* CTL_UNSPEC */
157	{ kernname, KERN_MAXID },	/* CTL_KERN */
158	{ vmname, VM_MAXID },		/* CTL_VM */
159	{ fsname, FS_MAXID },		/* CTL_FS */
160	{ netname, NET_MAXID },		/* CTL_NET */
161	{ 0, CTL_DEBUG_MAXID },		/* CTL_DEBUG */
162	{ hwname, HW_MAXID },		/* CTL_HW */
163#ifdef CTL_MACHDEP_NAMES
164	{ machdepname, CPU_MAXID },	/* CTL_MACHDEP */
165#else
166	{ 0, 0 },			/* CTL_MACHDEP */
167#endif
168	{ username, USER_MAXID },	/* CTL_USER_NAMES */
169	{ ddbname, DBCTL_MAXID },	/* CTL_DDB_NAMES */
170	{ 0, 0 },			/* CTL_VFS */
171};
172
173int	Aflag, aflag, nflag, qflag;
174
175/*
176 * Variables requiring special processing.
177 */
178#define	CLOCK		0x00000001
179#define	BOOTTIME	0x00000002
180#define	CHRDEV		0x00000004
181#define	BLKDEV		0x00000008
182#define	RNDSTATS	0x00000010
183#define	BADDYNAMIC	0x00000020
184#define	BIOSGEO		0x00000040
185#define	BIOSDEV		0x00000080
186#define	MAJ2DEV		0x00000100
187#define	UNSIGNED	0x00000200
188#define	KMEMBUCKETS	0x00000400
189#define	LONGARRAY	0x00000800
190#define	KMEMSTATS	0x00001000
191#define	SENSORS		0x00002000
192
193/* prototypes */
194void debuginit(void);
195void listall(char *, struct list *);
196void parse(char *, int);
197void parse_baddynamic(int *, size_t, char *, void **, size_t *, int, int);
198void usage(void);
199int findname(char *, char *, char **, struct list *);
200int sysctl_inet(char *, char **, int *, int, int *);
201#ifdef INET6
202int sysctl_inet6(char *, char **, int *, int, int *);
203#endif
204int sysctl_bpf(char *, char **, int *, int, int *);
205int sysctl_mpls(char *, char **, int *, int, int *);
206int sysctl_fs(char *, char **, int *, int, int *);
207static int sysctl_vfs(char *, char **, int[], int, int *);
208static int sysctl_vfsgen(char *, char **, int[], int, int *);
209int sysctl_bios(char *, char **, int *, int, int *);
210int sysctl_swpenc(char *, char **, int *, int, int *);
211int sysctl_forkstat(char *, char **, int *, int, int *);
212int sysctl_tty(char *, char **, int *, int, int *);
213int sysctl_nchstats(char *, char **, int *, int, int *);
214int sysctl_malloc(char *, char **, int *, int, int *);
215int sysctl_seminfo(char *, char **, int *, int, int *);
216int sysctl_shminfo(char *, char **, int *, int, int *);
217int sysctl_watchdog(char *, char **, int *, int, int *);
218int sysctl_tc(char *, char **, int *, int, int *);
219int sysctl_sensors(char *, char **, int *, int, int *);
220void print_sensordev(char *, int *, u_int, struct sensordev *);
221void print_sensor(struct sensor *);
222int sysctl_emul(char *, char *, int);
223#ifdef CPU_CHIPSET
224int sysctl_chipset(char *, char **, int *, int, int *);
225#endif
226void vfsinit(void);
227
228char *equ = "=";
229
230int
231main(int argc, char *argv[])
232{
233	int ch, lvl1;
234
235	while ((ch = getopt(argc, argv, "Aanqw")) != -1) {
236		switch (ch) {
237
238		case 'A':
239			Aflag = 1;
240			break;
241
242		case 'a':
243			aflag = 1;
244			break;
245
246		case 'n':
247			nflag = 1;
248			break;
249
250		case 'q':
251			qflag = 1;
252			break;
253
254		case 'w':
255			/* flag no longer needed; var=value implies write */
256			break;
257
258		default:
259			usage();
260		}
261	}
262	argc -= optind;
263	argv += optind;
264
265	if (argc == 0 || (Aflag || aflag)) {
266		debuginit();
267		vfsinit();
268		for (lvl1 = 1; lvl1 < CTL_MAXID; lvl1++)
269			listall(topname[lvl1].ctl_name, &secondlevel[lvl1]);
270		return (0);
271	}
272	for (; *argv != NULL; ++argv)
273		parse(*argv, 1);
274	return (0);
275}
276
277/*
278 * List all variables known to the system.
279 */
280void
281listall(char *prefix, struct list *lp)
282{
283	char *cp, name[BUFSIZ];
284	int lvl2, len;
285
286	if (lp->list == NULL)
287		return;
288	if ((len = strlcpy(name, prefix, sizeof(name))) >= sizeof(name))
289		errx(1, "%s: name too long", prefix);
290	cp = name + len++;
291	*cp++ = '.';
292	for (lvl2 = 0; lvl2 < lp->size; lvl2++) {
293		if (lp->list[lvl2].ctl_name == NULL)
294			continue;
295		if (strlcpy(cp, lp->list[lvl2].ctl_name,
296		    sizeof(name) - len) >= sizeof(name) - len)
297			warn("%s: name too long", lp->list[lvl2].ctl_name);
298		parse(name, Aflag);
299	}
300}
301
302/*
303 * Parse a name into a MIB entry.
304 * Lookup and print out the MIB entry if it exists.
305 * Set a new value if requested.
306 */
307void
308parse(char *string, int flags)
309{
310	int indx, type, state, intval, len;
311	size_t size, newsize = 0;
312	int lal = 0, special = 0;
313	void *newval = NULL;
314	int64_t quadval;
315	struct list *lp;
316	int mib[CTL_MAXNAME];
317	char *cp, *bufp, buf[SYSCTL_BUFSIZ];
318
319	(void)strlcpy(buf, string, sizeof(buf));
320	bufp = buf;
321	if ((cp = strchr(string, '=')) != NULL) {
322		*strchr(buf, '=') = '\0';
323		*cp++ = '\0';
324		while (isspace(*cp))
325			cp++;
326		newval = cp;
327		newsize = strlen(cp);
328	}
329	if ((indx = findname(string, "top", &bufp, &toplist)) == -1)
330		return;
331	mib[0] = indx;
332	if (indx == CTL_VFS)
333		vfsinit();
334	if (indx == CTL_DEBUG)
335		debuginit();
336	lp = &secondlevel[indx];
337	if (lp->list == 0) {
338		warnx("%s: class is not implemented", topname[indx].ctl_name);
339		return;
340	}
341	if (bufp == NULL) {
342		listall(topname[indx].ctl_name, lp);
343		return;
344	}
345	if ((indx = findname(string, "second", &bufp, lp)) == -1)
346		return;
347	mib[1] = indx;
348	type = lp->list[indx].ctl_type;
349	len = 2;
350	switch (mib[0]) {
351
352	case CTL_KERN:
353		switch (mib[1]) {
354		case KERN_PROF:
355			mib[2] = GPROF_STATE;
356			size = sizeof(state);
357			if (sysctl(mib, 3, &state, &size, NULL, 0) == -1) {
358				if (flags == 0)
359					return;
360				if (!nflag)
361					(void)printf("%s: ", string);
362				(void)puts("kernel is not compiled for profiling");
363				return;
364			}
365			if (!nflag)
366				(void)printf("%s = %s\n", string,
367				    state == GMON_PROF_OFF ? "off" : "running");
368			return;
369		case KERN_FORKSTAT:
370			sysctl_forkstat(string, &bufp, mib, flags, &type);
371			return;
372		case KERN_TTY:
373			len = sysctl_tty(string, &bufp, mib, flags, &type);
374			if (len < 0)
375				return;
376			break;
377		case KERN_NCHSTATS:
378			sysctl_nchstats(string, &bufp, mib, flags, &type);
379			return;
380		case KERN_MALLOCSTATS:
381			len = sysctl_malloc(string, &bufp, mib, flags, &type);
382			if (len < 0)
383				return;
384			if (mib[2] == KERN_MALLOC_BUCKET)
385				special |= KMEMBUCKETS;
386			if (mib[2] == KERN_MALLOC_KMEMSTATS)
387				special |= KMEMSTATS;
388			newsize = 0;
389			break;
390		case KERN_MBSTAT:
391			if (flags == 0)
392				return;
393			warnx("use netstat to view %s", string);
394			return;
395		case KERN_MSGBUF:
396			if (flags == 0)
397				return;
398			warnx("use dmesg to view %s", string);
399			return;
400		case KERN_VNODE:
401		case KERN_FILE:
402			if (flags == 0)
403				return;
404			warnx("use pstat to view %s information", string);
405			return;
406		case KERN_PROC:
407		case KERN_PROC2:
408			if (flags == 0)
409				return;
410			warnx("use ps to view %s information", string);
411			return;
412		case KERN_CLOCKRATE:
413			special |= CLOCK;
414			break;
415		case KERN_BOOTTIME:
416			special |= BOOTTIME;
417			break;
418		case KERN_RND:
419			special |= RNDSTATS;
420			break;
421		case KERN_HOSTID:
422		case KERN_ARND:
423			special |= UNSIGNED;
424			break;
425		case KERN_CPTIME:
426			special |= LONGARRAY;
427			lal = CPUSTATES;
428			break;
429		case KERN_SEMINFO:
430			len = sysctl_seminfo(string, &bufp, mib, flags, &type);
431			if (len < 0)
432				return;
433			break;
434		case KERN_SHMINFO:
435			len = sysctl_shminfo(string, &bufp, mib, flags, &type);
436			if (len < 0)
437				return;
438			break;
439		case KERN_WATCHDOG:
440			len = sysctl_watchdog(string, &bufp, mib, flags,
441			    &type);
442			if (len < 0)
443				return;
444			break;
445		case KERN_TIMECOUNTER:
446			len = sysctl_tc(string, &bufp, mib, flags,
447			    &type);
448			if (len < 0)
449				return;
450			break;
451		case KERN_EMUL:
452			sysctl_emul(string, newval, flags);
453			return;
454		}
455		break;
456
457	case CTL_HW:
458		switch (mib[1]) {
459		case HW_DISKSTATS:
460			/*
461			 * Only complain if someone asks explicitly for this,
462			 * otherwise "fail" silently.
463			 */
464			if (flags)
465				warnx("use vmstat to view %s information",
466				    string);
467			return;
468		case HW_SENSORS:
469			special |= SENSORS;
470			len = sysctl_sensors(string, &bufp, mib, flags, &type);
471			if (len < 0)
472				return;
473			break;
474		case HW_PHYSMEM:
475		case HW_USERMEM:
476			/*
477			 * Don't print these; we'll print the 64-bit
478			 * variants instead.
479			 */
480			return;
481		}
482		break;
483
484	case CTL_VM:
485		if (mib[1] == VM_LOADAVG) {
486			double loads[3];
487
488			getloadavg(loads, 3);
489			if (!nflag)
490				(void)printf("%s%s", string, equ);
491			(void)printf("%.2f %.2f %.2f\n", loads[0],
492			    loads[1], loads[2]);
493			return;
494		} else if (mib[1] == VM_PSSTRINGS) {
495			struct _ps_strings _ps;
496
497			size = sizeof(_ps);
498			if (sysctl(mib, 2, &_ps, &size, NULL, 0) == -1) {
499				if (flags == 0)
500					return;
501				if (!nflag)
502					(void)printf("%s: ", string);
503				(void)puts("can't find ps strings");
504				return;
505			}
506			if (!nflag)
507				(void)printf("%s%s", string, equ);
508			(void)printf("%p\n", _ps.val);
509			return;
510		} else if (mib[1] == VM_SWAPENCRYPT) {
511			len = sysctl_swpenc(string, &bufp, mib, flags, &type);
512			if (len < 0)
513				return;
514
515			break;
516		} else if (mib[1] == VM_NKMEMPAGES ||
517		    mib[1] == VM_ANONMIN ||
518		    mib[1] == VM_VTEXTMIN ||
519		    mib[1] == VM_VNODEMIN) {
520			break;
521		}
522		if (flags == 0)
523			return;
524		warnx("use vmstat or systat to view %s information", string);
525		return;
526
527		break;
528
529	case CTL_NET:
530		if (mib[1] == PF_INET) {
531			len = sysctl_inet(string, &bufp, mib, flags, &type);
532			if (len < 0)
533				return;
534
535			if ((mib[2] == IPPROTO_IP && mib[3] == IPCTL_MRTSTATS) ||
536			    (mib[2] == IPPROTO_IP && mib[3] == IPCTL_STATS) ||
537			    (mib[2] == IPPROTO_TCP && mib[3] == TCPCTL_STATS) ||
538			    (mib[2] == IPPROTO_UDP && mib[3] == UDPCTL_STATS) ||
539			    (mib[2] == IPPROTO_ESP && mib[3] == ESPCTL_STATS) ||
540			    (mib[2] == IPPROTO_AH && mib[3] == AHCTL_STATS) ||
541			    (mib[2] == IPPROTO_IGMP && mib[3] == IGMPCTL_STATS) ||
542			    (mib[2] == IPPROTO_ETHERIP && mib[3] == ETHERIPCTL_STATS) ||
543			    (mib[2] == IPPROTO_IPIP && mib[3] == IPIPCTL_STATS) ||
544			    (mib[2] == IPPROTO_IPCOMP && mib[3] == IPCOMPCTL_STATS) ||
545			    (mib[2] == IPPROTO_ICMP && mib[3] == ICMPCTL_STATS) ||
546			    (mib[2] == IPPROTO_CARP && mib[3] == CARPCTL_STATS) ||
547			    (mib[2] == IPPROTO_PFSYNC && mib[3] == PFSYNCCTL_STATS)) {
548				if (flags == 0)
549					return;
550				warnx("use netstat to view %s information",
551				    string);
552				return;
553			} else if ((mib[2] == IPPROTO_TCP &&
554			    mib[3] == TCPCTL_BADDYNAMIC) ||
555			    (mib[2] == IPPROTO_UDP &&
556			    mib[3] == UDPCTL_BADDYNAMIC)) {
557
558				special |= BADDYNAMIC;
559
560				if (newval != NULL)
561					parse_baddynamic(mib, len, string,
562					    &newval, &newsize, flags, nflag);
563			}
564			break;
565		}
566#ifdef INET6
567		if (mib[1] == PF_INET6) {
568			len = sysctl_inet6(string, &bufp, mib, flags, &type);
569			if (len < 0)
570				return;
571
572			if ((mib[2] == IPPROTO_PIM && mib[3] == PIM6CTL_STATS)) {
573				if (flags == 0)
574					return;
575				warnx("use netstat to view %s information",
576				    string);
577				return;
578			}
579			break;
580		}
581#endif
582		if (mib[1] == PF_BPF) {
583			len = sysctl_bpf(string, &bufp, mib, flags, &type);
584			if (len < 0)
585				return;
586			break;
587		}
588		if (mib[1] == PF_MPLS) {
589			len = sysctl_mpls(string, &bufp, mib, flags, &type);
590			if (len < 0)
591				return;
592			break;
593		}
594		if (flags == 0)
595			return;
596		warnx("use netstat to view %s information", string);
597		return;
598
599	case CTL_DEBUG:
600		mib[2] = CTL_DEBUG_VALUE;
601		len = 3;
602		break;
603
604	case CTL_MACHDEP:
605#ifdef CPU_CONSDEV
606		if (mib[1] == CPU_CONSDEV)
607			special |= CHRDEV;
608#endif
609#ifdef CPU_BLK2CHR
610		if (mib[1] == CPU_BLK2CHR) {
611			if (bufp == NULL)
612				return;
613			mib[2] = makedev(atoi(bufp),0);
614			bufp = NULL;
615			len = 3;
616			special |= CHRDEV;
617			break;
618		}
619#endif
620#ifdef CPU_CHR2BLK
621		if (mib[1] == CPU_CHR2BLK) {
622			if (bufp == NULL)
623				return;
624			mib[2] = makedev(atoi(bufp),0);
625			bufp = NULL;
626			len = 3;
627			special |= BLKDEV;
628			break;
629		}
630#endif
631#ifdef CPU_BIOS
632		if (mib[1] == CPU_BIOS) {
633			len = sysctl_bios(string, &bufp, mib, flags, &type);
634			if (len < 0)
635				return;
636			if (mib[2] == BIOS_DEV)
637				special |= BIOSDEV;
638			if (mib[2] == BIOS_DISKINFO)
639				special |= BIOSGEO;
640			break;
641		}
642#endif
643#ifdef CPU_CHIPSET
644		if (mib[1] == CPU_CHIPSET) {
645			len = sysctl_chipset(string, &bufp, mib, flags, &type);
646			if (len < 0)
647				return;
648			break;
649		}
650#endif
651		break;
652
653	case CTL_FS:
654		len = sysctl_fs(string, &bufp, mib, flags, &type);
655		if (len >= 0)
656			break;
657		return;
658
659	case CTL_VFS:
660		if (mib[1])
661			len = sysctl_vfs(string, &bufp, mib, flags, &type);
662		else
663			len = sysctl_vfsgen(string, &bufp, mib, flags, &type);
664		if (len >= 0) {
665			if (type == CTLTYPE_STRUCT) {
666				if (flags)
667					warnx("use nfsstat to view %s information",
668					    MOUNT_NFS);
669				return;
670			} else
671				break;
672		}
673		return;
674
675	case CTL_USER:
676	case CTL_DDB:
677		break;
678
679	default:
680		warnx("illegal top level value: %d", mib[0]);
681		return;
682
683	}
684	if (bufp) {
685		warnx("name %s in %s is unknown", bufp, string);
686		return;
687	}
688	if (newsize > 0) {
689		switch (type) {
690		case CTLTYPE_INT:
691			errno = 0;
692			if (special & UNSIGNED)
693				intval = strtoul(newval, &cp, 10);
694			else
695				intval = strtol(newval, &cp, 10);
696			if (*cp != '\0') {
697				warnx("%s: illegal value: %s", string,
698				    (char *)newval);
699				return;
700			}
701			if (errno == ERANGE) {
702				warnx("%s: value %s out of range", string,
703				    (char *)newval);
704				return;
705			}
706			newval = &intval;
707			newsize = sizeof(intval);
708			break;
709
710		case CTLTYPE_QUAD:
711			/* XXX - assumes sizeof(long long) == sizeof(quad_t) */
712			(void)sscanf(newval, "%lld", (long long *)&quadval);
713			newval = &quadval;
714			newsize = sizeof(quadval);
715			break;
716		}
717	}
718	size = SYSCTL_BUFSIZ;
719	if (sysctl(mib, len, buf, &size, newval, newsize) == -1) {
720		if (flags == 0)
721			return;
722		switch (errno) {
723		case EOPNOTSUPP:
724			warnx("%s: value is not available", string);
725			return;
726		case ENOTDIR:
727			warnx("%s: specification is incomplete", string);
728			return;
729		case ENOMEM:
730			warnx("%s: type is unknown to this program", string);
731			return;
732		case ENXIO:
733			if (special & BIOSGEO)
734				return;
735		default:
736			warn("%s", string);
737			return;
738		}
739	}
740	if (special & KMEMBUCKETS) {
741		struct kmembuckets *kb = (struct kmembuckets *)buf;
742		if (!nflag)
743			(void)printf("%s%s", string, equ);
744		printf("(");
745		printf("calls = %llu ", (long long)kb->kb_calls);
746		printf("total_allocated = %llu ", (long long)kb->kb_total);
747		printf("total_free = %lld ", (long long)kb->kb_totalfree);
748		printf("elements = %lld ", (long long)kb->kb_elmpercl);
749		printf("high watermark = %lld ", (long long)kb->kb_highwat);
750		printf("could_free = %lld", (long long)kb->kb_couldfree);
751		printf(")\n");
752		return;
753	}
754	if (special & KMEMSTATS) {
755		struct kmemstats *km = (struct kmemstats *)buf;
756		int j, first = 1;
757
758		if (!nflag)
759			(void)printf("%s%s", string, equ);
760		(void)printf("(inuse = %ld, calls = %ld, memuse = %ldK, "
761		    "limblocks = %d, mapblocks = %d, maxused = %ldK, "
762		    "limit = %ldK, spare = %ld, sizes = (",
763		    km->ks_inuse, km->ks_calls,
764		    (km->ks_memuse + 1023) / 1024, km->ks_limblocks,
765		    km->ks_mapblocks, (km->ks_maxused + 1023) / 1024,
766		    (km->ks_limit + 1023) / 1024, km->ks_spare);
767		for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
768			if ((km->ks_size & j ) == 0)
769				continue;
770			if (first)
771				(void)printf("%d", j);
772			else
773				(void)printf(",%d", j);
774			first = 0;
775		}
776		if (first)
777			(void)printf("none");
778		(void)printf("))\n");
779		return;
780	}
781	if (special & CLOCK) {
782		struct clockinfo *clkp = (struct clockinfo *)buf;
783
784		if (!nflag)
785			(void)printf("%s%s", string, equ);
786		(void)printf(
787		    "tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n",
788		    clkp->tick, clkp->tickadj, clkp->hz, clkp->profhz, clkp->stathz);
789		return;
790	}
791	if (special & BOOTTIME) {
792		struct timeval *btp = (struct timeval *)buf;
793		time_t boottime;
794
795		if (!nflag) {
796			boottime = btp->tv_sec;
797			(void)printf("%s%s%s", string, equ, ctime(&boottime));
798		} else
799			(void)printf("%ld\n", btp->tv_sec);
800		return;
801	}
802	if (special & BLKDEV) {
803		dev_t dev = *(dev_t *)buf;
804
805		if (!nflag)
806			(void)printf("%s%s%s\n", string, equ,
807			    devname(dev, S_IFBLK));
808		else
809			(void)printf("0x%x\n", dev);
810		return;
811	}
812	if (special & CHRDEV) {
813		dev_t dev = *(dev_t *)buf;
814
815		if (!nflag)
816			(void)printf("%s%s%s\n", string, equ,
817			    devname(dev, S_IFCHR));
818		else
819			(void)printf("0x%x\n", dev);
820		return;
821	}
822#ifdef CPU_BIOS
823	if (special & BIOSGEO) {
824		bios_diskinfo_t *pdi = (bios_diskinfo_t *)buf;
825
826		if (!nflag)
827			(void)printf("%s%s", string, equ);
828		(void)printf("bootdev = 0x%x, "
829		    "cylinders = %u, heads = %u, sectors = %u\n",
830		    pdi->bsd_dev, pdi->bios_cylinders,
831		    pdi->bios_heads, pdi->bios_sectors);
832		return;
833	}
834	if (special & BIOSDEV) {
835		int dev = *(int*)buf;
836
837		if (!nflag)
838			(void)printf("%s%s", string, equ);
839		(void) printf("0x%02x\n", dev);
840		return;
841	}
842#endif
843	if (special & UNSIGNED) {
844		if (newsize == 0) {
845			if (!nflag)
846				(void)printf("%s%s", string, equ);
847			(void)printf("%u\n", *(u_int *)buf);
848		} else {
849			if (!qflag) {
850				if (!nflag)
851					(void)printf("%s: %u -> ", string,
852					    *(u_int *)buf);
853				(void)printf("%u\n", *(u_int *)newval);
854			}
855		}
856		return;
857	}
858	if (special & RNDSTATS) {
859		struct rndstats *rndstats = (struct rndstats *)buf;
860		int i;
861
862		if (!nflag)
863			(void)printf("%s%s", string, equ);
864		(void)printf(
865		"%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
866		    (unsigned long long)rndstats->rnd_total,
867		    (unsigned long long)rndstats->rnd_used,
868		    (unsigned long long)rndstats->rnd_reads,
869		    (unsigned long long)rndstats->arc4_reads,
870		    (unsigned long long)rndstats->arc4_nstirs,
871		    (unsigned long long)rndstats->arc4_stirs,
872		    (unsigned long long)rndstats->rnd_pad[0],
873		    (unsigned long long)rndstats->rnd_pad[1],
874		    (unsigned long long)rndstats->rnd_pad[2],
875		    (unsigned long long)rndstats->rnd_pad[3],
876		    (unsigned long long)rndstats->rnd_pad[4],
877		    (unsigned long long)rndstats->rnd_waits,
878		    (unsigned long long)rndstats->rnd_enqs,
879		    (unsigned long long)rndstats->rnd_deqs,
880		    (unsigned long long)rndstats->rnd_drops,
881		    (unsigned long long)rndstats->rnd_drople);
882		for (i = 0; i < sizeof(rndstats->rnd_ed)/sizeof(rndstats->rnd_ed[0]);
883		    i++)
884			(void)printf(" %llu", (unsigned long long)rndstats->rnd_ed[i]);
885		for (i = 0; i < sizeof(rndstats->rnd_sc)/sizeof(rndstats->rnd_sc[0]);
886		    i++)
887			(void)printf(" %llu", (unsigned long long)rndstats->rnd_sc[i]);
888		for (i = 0; i < sizeof(rndstats->rnd_sb)/sizeof(rndstats->rnd_sb[0]);
889		    i++)
890			(void)printf(" %llu", (unsigned long long)rndstats->rnd_sb[i]);
891		printf("\n");
892		return;
893	}
894	if (special & BADDYNAMIC) {
895		u_int port, lastport;
896		u_int32_t *baddynamic = (u_int32_t *)buf;
897
898		if (!qflag) {
899			if (!nflag)
900				(void)printf("%s%s", string,
901				    newsize ? ": " : equ);
902			lastport = 0;
903			for (port = 0; port < 65536; port++)
904				if (DP_ISSET(baddynamic, port)) {
905					(void)printf("%s%u",
906					    lastport ? "," : "", port);
907					lastport = port;
908				}
909			if (newsize != 0) {
910				if (!nflag)
911					fputs(" -> ", stdout);
912				baddynamic = (u_int32_t *)newval;
913				lastport = 0;
914				for (port = 0; port < 65536; port++)
915					if (DP_ISSET(baddynamic, port)) {
916						(void)printf("%s%u",
917						    lastport ? "," : "", port);
918						lastport = port;
919					}
920			}
921			(void)putchar('\n');
922		}
923		return;
924	}
925	if (special & LONGARRAY) {
926		long *la = (long *)buf;
927		if (!nflag)
928			printf("%s%s", string, equ);
929		while (lal--)
930			printf("%ld%s", *la++, lal? ",":"");
931		putchar('\n');
932		return;
933	}
934	if (special & SENSORS) {
935		struct sensor *s = (struct sensor *)buf;
936
937		if (size > 0 && (s->flags & SENSOR_FINVALID) == 0) {
938			if (!nflag)
939				printf("%s%s", string, equ);
940			print_sensor(s);
941			printf("\n");
942		}
943		return;
944	}
945	switch (type) {
946	case CTLTYPE_INT:
947		if (newsize == 0) {
948			if (!nflag)
949				(void)printf("%s%s", string, equ);
950			(void)printf("%d\n", *(int *)buf);
951		} else {
952			if (!qflag) {
953				if (!nflag)
954					(void)printf("%s: %d -> ", string,
955					    *(int *)buf);
956				(void)printf("%d\n", *(int *)newval);
957			}
958		}
959		return;
960
961	case CTLTYPE_STRING:
962		if (newval == NULL) {
963			if (!nflag)
964				(void)printf("%s%s", string, equ);
965			(void)puts(buf);
966		} else {
967			if (!qflag) {
968				if (!nflag)
969					(void)printf("%s: %s -> ", string, buf);
970				(void)puts((char *)newval);
971			}
972		}
973		return;
974
975	case CTLTYPE_QUAD:
976		if (newsize == 0) {
977			long long tmp = *(quad_t *)buf;
978
979			if (!nflag)
980				(void)printf("%s%s", string, equ);
981			(void)printf("%lld\n", tmp);
982		} else {
983			long long tmp = *(quad_t *)buf;
984
985			if (!qflag) {
986				if (!nflag)
987					(void)printf("%s: %lld -> ",
988					    string, tmp);
989				tmp = *(quad_t *)newval;
990				(void)printf("%qd\n", tmp);
991			}
992		}
993		return;
994
995	case CTLTYPE_STRUCT:
996		warnx("%s: unknown structure returned", string);
997		return;
998
999	default:
1000	case CTLTYPE_NODE:
1001		warnx("%s: unknown type returned", string);
1002		return;
1003	}
1004}
1005
1006void
1007parse_baddynamic(int mib[], size_t len, char *string, void **newvalp,
1008    size_t *newsizep, int flags, int nflag)
1009{
1010	static u_int32_t newbaddynamic[DP_MAPSIZE];
1011	in_port_t port;
1012	size_t size;
1013	char action, *cp;
1014	const char *errstr;
1015
1016	if (strchr((char *)*newvalp, '+') || strchr((char *)*newvalp, '-')) {
1017		size = sizeof(newbaddynamic);
1018		if (sysctl(mib, len, newbaddynamic, &size, 0, 0) == -1) {
1019			if (flags == 0)
1020				return;
1021			if (!nflag)
1022				(void)printf("%s: ", string);
1023			(void)puts("kernel does contain bad dynamic port tables");
1024			return;
1025		}
1026
1027		while (*newvalp && (cp = strsep((char **)newvalp, ", \t")) && *cp) {
1028			if (*cp != '+' && *cp != '-')
1029				errx(1, "cannot mix +/- with full list");
1030			action = *cp++;
1031			port = strtonum(cp, 0, 65535, &errstr);
1032			if (errstr != NULL)
1033				errx(1, "port is %s: %s", errstr, cp);
1034			if (action == '+')
1035				DP_SET(newbaddynamic, port);
1036			else
1037				DP_CLR(newbaddynamic, port);
1038		}
1039	} else {
1040		(void)memset((void *)newbaddynamic, 0, sizeof(newbaddynamic));
1041		while (*newvalp && (cp = strsep((char **)newvalp, ", \t")) && *cp) {
1042			port = strtonum(cp, 0, 65535, &errstr);
1043			if (errstr != NULL)
1044				errx(1, "port is %s: %s", errstr, cp);
1045			DP_SET(newbaddynamic, port);
1046		}
1047	}
1048
1049	*newvalp = (void *)newbaddynamic;
1050	*newsizep = sizeof(newbaddynamic);
1051}
1052
1053/*
1054 * Initialize the set of debugging names
1055 */
1056void
1057debuginit(void)
1058{
1059	int mib[3], loc, i;
1060	size_t size;
1061
1062	if (secondlevel[CTL_DEBUG].list != 0)
1063		return;
1064	secondlevel[CTL_DEBUG].list = debugname;
1065	mib[0] = CTL_DEBUG;
1066	mib[2] = CTL_DEBUG_NAME;
1067	for (loc = lastused, i = 0; i < CTL_DEBUG_MAXID; i++) {
1068		mib[1] = i;
1069		size = BUFSIZ - loc;
1070		if (sysctl(mib, 3, &names[loc], &size, NULL, 0) == -1)
1071			continue;
1072		debugname[i].ctl_name = &names[loc];
1073		debugname[i].ctl_type = CTLTYPE_INT;
1074		loc += size;
1075	}
1076	lastused = loc;
1077}
1078
1079struct ctlname vfsgennames[] = CTL_VFSGENCTL_NAMES;
1080struct ctlname ffsname[] = FFS_NAMES;
1081struct ctlname nfsname[] = FS_NFS_NAMES;
1082struct list *vfsvars;
1083int *vfs_typenums;
1084
1085/*
1086 * Initialize the set of filesystem names
1087 */
1088void
1089vfsinit(void)
1090{
1091	int mib[4], maxtypenum, cnt, loc, size;
1092	struct vfsconf vfc;
1093	size_t buflen;
1094
1095	if (secondlevel[CTL_VFS].list != 0)
1096		return;
1097	mib[0] = CTL_VFS;
1098	mib[1] = VFS_GENERIC;
1099	mib[2] = VFS_MAXTYPENUM;
1100	buflen = 4;
1101	if (sysctl(mib, 3, &maxtypenum, &buflen, (void *)0, (size_t)0) < 0)
1102		return;
1103	maxtypenum++;	/* + generic */
1104	if ((vfs_typenums = calloc(maxtypenum, sizeof(int))) == NULL)
1105		return;
1106	if ((vfsvars = calloc(maxtypenum, sizeof(*vfsvars))) == NULL) {
1107		free(vfs_typenums);
1108		return;
1109	}
1110	if ((vfsname = calloc(maxtypenum, sizeof(*vfsname))) == NULL) {
1111		free(vfs_typenums);
1112		free(vfsvars);
1113		return;
1114	}
1115	mib[2] = VFS_CONF;
1116	buflen = sizeof vfc;
1117	for (loc = lastused, cnt = 1; cnt < maxtypenum; cnt++) {
1118		mib[3] = cnt - 1;
1119		if (sysctl(mib, 4, &vfc, &buflen, (void *)0, (size_t)0) < 0) {
1120			if (errno == EOPNOTSUPP)
1121				continue;
1122			warn("vfsinit");
1123			free(vfsname);
1124			free(vfsvars);
1125			free(vfs_typenums);
1126			return;
1127		}
1128		if (!strcmp(vfc.vfc_name, MOUNT_FFS)) {
1129			vfsvars[cnt].list = ffsname;
1130			vfsvars[cnt].size = FFS_MAXID;
1131		}
1132		if (!strcmp(vfc.vfc_name, MOUNT_NFS)) {
1133			vfsvars[cnt].list = nfsname;
1134			vfsvars[cnt].size = NFS_MAXID;
1135		}
1136		vfs_typenums[cnt] = vfc.vfc_typenum;
1137		strlcat(&names[loc], vfc.vfc_name, sizeof names - loc);
1138		vfsname[cnt].ctl_name = &names[loc];
1139		vfsname[cnt].ctl_type = CTLTYPE_NODE;
1140		size = strlen(vfc.vfc_name) + 1;
1141		loc += size;
1142	}
1143	lastused = loc;
1144
1145	vfsname[0].ctl_name = "mounts";
1146	vfsname[0].ctl_type = CTLTYPE_NODE;
1147	vfsvars[0].list = vfsname + 1;
1148	vfsvars[0].size = maxtypenum - 1;
1149
1150	secondlevel[CTL_VFS].list = vfsname;
1151	secondlevel[CTL_VFS].size = maxtypenum;
1152	return;
1153}
1154
1155int
1156sysctl_vfsgen(char *string, char **bufpp, int mib[], int flags, int *typep)
1157{
1158	int indx;
1159	size_t size;
1160	struct vfsconf vfc;
1161
1162	if (*bufpp == NULL) {
1163		listall(string, vfsvars);
1164		return (-1);
1165	}
1166
1167	if ((indx = findname(string, "third", bufpp, vfsvars)) == -1)
1168		return (-1);
1169
1170	mib[1] = VFS_GENERIC;
1171	mib[2] = VFS_CONF;
1172	mib[3] = indx;
1173	size = sizeof vfc;
1174	if (sysctl(mib, 4, &vfc, &size, (void *)0, (size_t)0) < 0) {
1175		if (errno != EOPNOTSUPP)
1176			warn("vfs print");
1177		return -1;
1178	}
1179	if (flags == 0 && vfc.vfc_refcount == 0)
1180		return -1;
1181	if (!nflag)
1182		fprintf(stdout, "%s has %d mounted instance%s\n",
1183		    string, vfc.vfc_refcount,
1184		    vfc.vfc_refcount != 1 ? "s" : "");
1185	else
1186		fprintf(stdout, "%d\n", vfc.vfc_refcount);
1187
1188	return -1;
1189}
1190
1191int
1192sysctl_vfs(char *string, char **bufpp, int mib[], int flags, int *typep)
1193{
1194	struct list *lp = &vfsvars[mib[1]];
1195	int indx;
1196
1197	if (lp->list == NULL) {
1198		if (flags)
1199			warnx("No variables defined for file system %s", string);
1200		return (-1);
1201	}
1202	if (*bufpp == NULL) {
1203		listall(string, lp);
1204		return (-1);
1205	}
1206	if ((indx = findname(string, "third", bufpp, lp)) == -1)
1207		return (-1);
1208
1209	mib[1] = vfs_typenums[mib[1]];
1210	mib[2] = indx;
1211	*typep = lp->list[indx].ctl_type;
1212	return (3);
1213}
1214
1215struct ctlname posixname[] = CTL_FS_POSIX_NAMES;
1216struct list fslist = { posixname, FS_POSIX_MAXID };
1217
1218/*
1219 * handle file system requests
1220 */
1221int
1222sysctl_fs(char *string, char **bufpp, int mib[], int flags, int *typep)
1223{
1224	int indx;
1225
1226	if (*bufpp == NULL) {
1227		listall(string, &fslist);
1228		return (-1);
1229	}
1230	if ((indx = findname(string, "third", bufpp, &fslist)) == -1)
1231		return (-1);
1232	mib[2] = indx;
1233	*typep = fslist.list[indx].ctl_type;
1234	return (3);
1235}
1236
1237#ifdef CPU_BIOS
1238struct ctlname biosname[] = CTL_BIOS_NAMES;
1239struct list bioslist = { biosname, BIOS_MAXID };
1240
1241/*
1242 * handle BIOS requests
1243 */
1244int
1245sysctl_bios(char *string, char **bufpp, int mib[], int flags, int *typep)
1246{
1247	char *name;
1248	int indx;
1249
1250	if (*bufpp == NULL) {
1251		listall(string, &bioslist);
1252		return (-1);
1253	}
1254	if ((indx = findname(string, "third", bufpp, &bioslist)) == -1)
1255		return (-1);
1256	mib[2] = indx;
1257	if (indx == BIOS_DISKINFO) {
1258		if (*bufpp == NULL) {
1259			char name[BUFSIZ];
1260
1261			/* scan all the bios devices */
1262			for (indx = 0; indx < 256; indx++) {
1263				snprintf(name, sizeof(name), "%s.%u",
1264				    string, indx);
1265				parse(name, 1);
1266			}
1267			return (-1);
1268		}
1269		if ((name = strsep(bufpp, ".")) == NULL) {
1270			warnx("%s: incomplete specification", string);
1271			return (-1);
1272		}
1273		mib[3] = atoi(name);
1274		*typep = CTLTYPE_STRUCT;
1275		return (4);
1276	} else {
1277		*typep = bioslist.list[indx].ctl_type;
1278		return (3);
1279	}
1280}
1281#endif
1282
1283struct ctlname swpencname[] = CTL_SWPENC_NAMES;
1284struct list swpenclist = { swpencname, SWPENC_MAXID };
1285
1286/*
1287 * handle swap encrypt requests
1288 */
1289int
1290sysctl_swpenc(char *string, char **bufpp, int mib[], int flags, int *typep)
1291{
1292	int indx;
1293
1294	if (*bufpp == NULL) {
1295		listall(string, &swpenclist);
1296		return (-1);
1297	}
1298	if ((indx = findname(string, "third", bufpp, &swpenclist)) == -1)
1299		return (-1);
1300	mib[2] = indx;
1301	*typep = swpenclist.list[indx].ctl_type;
1302	return (3);
1303}
1304
1305struct ctlname inetname[] = CTL_IPPROTO_NAMES;
1306struct ctlname ipname[] = IPCTL_NAMES;
1307struct ctlname icmpname[] = ICMPCTL_NAMES;
1308struct ctlname igmpname[] = IGMPCTL_NAMES;
1309struct ctlname ipipname[] = IPIPCTL_NAMES;
1310struct ctlname tcpname[] = TCPCTL_NAMES;
1311struct ctlname udpname[] = UDPCTL_NAMES;
1312struct ctlname espname[] = ESPCTL_NAMES;
1313struct ctlname ahname[] = AHCTL_NAMES;
1314struct ctlname etheripname[] = ETHERIPCTL_NAMES;
1315struct ctlname grename[] = GRECTL_NAMES;
1316struct ctlname mobileipname[] = MOBILEIPCTL_NAMES;
1317struct ctlname ipcompname[] = IPCOMPCTL_NAMES;
1318struct ctlname carpname[] = CARPCTL_NAMES;
1319struct ctlname pfsyncname[] = PFSYNCCTL_NAMES;
1320struct ctlname bpfname[] = CTL_NET_BPF_NAMES;
1321struct ctlname ifqname[] = CTL_IFQ_NAMES;
1322struct list inetlist = { inetname, IPPROTO_MAXID };
1323struct list inetvars[] = {
1324	{ ipname, IPCTL_MAXID },	/* ip */
1325	{ icmpname, ICMPCTL_MAXID },	/* icmp */
1326	{ igmpname, IGMPCTL_MAXID },	/* igmp */
1327	{ 0, 0 },			/* ggmp */
1328	{ ipipname, IPIPCTL_MAXID },	/* ipencap */
1329	{ 0, 0 },
1330	{ tcpname, TCPCTL_MAXID },	/* tcp */
1331	{ 0, 0 },
1332	{ 0, 0 },			/* egp */
1333	{ 0, 0 },
1334	{ 0, 0 },
1335	{ 0, 0 },
1336	{ 0, 0 },			/* pup */
1337	{ 0, 0 },
1338	{ 0, 0 },
1339	{ 0, 0 },
1340	{ 0, 0 },
1341	{ udpname, UDPCTL_MAXID },	/* udp */
1342	{ 0, 0 },
1343	{ 0, 0 },
1344	{ 0, 0 },
1345	{ 0, 0 },
1346	{ 0, 0 },
1347	{ 0, 0 },
1348	{ 0, 0 },
1349	{ 0, 0 },
1350	{ 0, 0 },
1351	{ 0, 0 },
1352	{ 0, 0 },
1353	{ 0, 0 },
1354	{ 0, 0 },
1355	{ 0, 0 },
1356	{ 0, 0 },
1357	{ 0, 0 },
1358	{ 0, 0 },
1359	{ 0, 0 },
1360	{ 0, 0 },
1361	{ 0, 0 },
1362	{ 0, 0 },
1363	{ 0, 0 },
1364	{ 0, 0 },
1365	{ 0, 0 },
1366	{ 0, 0 },
1367	{ 0, 0 },
1368	{ 0, 0 },
1369	{ 0, 0 },
1370	{ 0, 0 },
1371	{ grename, GRECTL_MAXID },	/* gre */
1372	{ 0, 0 },
1373	{ 0, 0 },
1374	{ espname, ESPCTL_MAXID },	/* esp */
1375	{ ahname, AHCTL_MAXID },	/* ah */
1376	{ 0, 0 },
1377	{ 0, 0 },
1378	{ 0, 0 },
1379	{ mobileipname, MOBILEIPCTL_MAXID }, /* mobileip */
1380	{ 0, 0 },
1381	{ 0, 0 },
1382	{ 0, 0 },
1383	{ 0, 0 },
1384	{ 0, 0 },
1385	{ 0, 0 },
1386	{ 0, 0 },
1387	{ 0, 0 },
1388	{ 0, 0 },
1389	{ 0, 0 },
1390	{ 0, 0 },
1391	{ 0, 0 },
1392	{ 0, 0 },
1393	{ 0, 0 },
1394	{ 0, 0 },
1395	{ 0, 0 },
1396	{ 0, 0 },
1397	{ 0, 0 },
1398	{ 0, 0 },
1399	{ 0, 0 },
1400	{ 0, 0 },
1401	{ 0, 0 },
1402	{ 0, 0 },
1403	{ 0, 0 },
1404	{ 0, 0 },
1405	{ 0, 0 },
1406	{ 0, 0 },
1407	{ 0, 0 },
1408	{ 0, 0 },
1409	{ 0, 0 },
1410	{ 0, 0 },
1411	{ 0, 0 },
1412	{ 0, 0 },
1413	{ 0, 0 },
1414	{ 0, 0 },
1415	{ 0, 0 },
1416	{ 0, 0 },
1417	{ 0, 0 },
1418	{ 0, 0 },
1419	{ 0, 0 },
1420	{ 0, 0 },
1421	{ etheripname, ETHERIPCTL_MAXID },
1422	{ 0, 0 },
1423	{ 0, 0 },
1424	{ 0, 0 },
1425	{ 0, 0 },
1426	{ 0, 0 },
1427	{ 0, 0 },
1428	{ 0, 0 },
1429	{ 0, 0 },
1430	{ 0, 0 },
1431	{ 0, 0 },
1432	{ ipcompname, IPCOMPCTL_MAXID },
1433	{ 0, 0 },
1434	{ 0, 0 },
1435	{ 0, 0 },
1436	{ carpname, CARPCTL_MAXID },
1437	{ 0, 0 },
1438	{ 0, 0 },
1439	{ 0, 0 },
1440	{ 0, 0 },
1441	{ 0, 0 },
1442	{ 0, 0 },
1443	{ 0, 0 },
1444	{ 0, 0 },
1445	{ 0, 0 },
1446	{ 0, 0 },
1447	{ 0, 0 },
1448	{ 0, 0 },
1449	{ 0, 0 },
1450	{ 0, 0 },
1451	{ 0, 0 },
1452	{ 0, 0 },
1453	{ 0, 0 },
1454	{ 0, 0 },
1455	{ 0, 0 },
1456	{ 0, 0 },
1457	{ 0, 0 },
1458	{ 0, 0 },
1459	{ 0, 0 },
1460	{ 0, 0 },
1461	{ 0, 0 },
1462	{ 0, 0 },
1463	{ 0, 0 },
1464	{ 0, 0 },
1465	{ 0, 0 },
1466	{ 0, 0 },
1467	{ 0, 0 },
1468	{ 0, 0 },
1469	{ 0, 0 },
1470	{ 0, 0 },
1471	{ 0, 0 },
1472	{ 0, 0 },
1473	{ 0, 0 },
1474	{ 0, 0 },
1475	{ 0, 0 },
1476	{ 0, 0 },
1477	{ 0, 0 },
1478	{ 0, 0 },
1479	{ 0, 0 },
1480	{ 0, 0 },
1481	{ 0, 0 },
1482	{ 0, 0 },
1483	{ 0, 0 },
1484	{ 0, 0 },
1485	{ 0, 0 },
1486	{ 0, 0 },
1487	{ 0, 0 },
1488	{ 0, 0 },
1489	{ 0, 0 },
1490	{ 0, 0 },
1491	{ 0, 0 },
1492	{ 0, 0 },
1493	{ 0, 0 },
1494	{ 0, 0 },
1495	{ 0, 0 },
1496	{ 0, 0 },
1497	{ 0, 0 },
1498	{ 0, 0 },
1499	{ 0, 0 },
1500	{ 0, 0 },
1501	{ 0, 0 },
1502	{ 0, 0 },
1503	{ 0, 0 },
1504	{ 0, 0 },
1505	{ 0, 0 },
1506	{ 0, 0 },
1507	{ 0, 0 },
1508	{ 0, 0 },
1509	{ 0, 0 },
1510	{ 0, 0 },
1511	{ 0, 0 },
1512	{ 0, 0 },
1513	{ 0, 0 },
1514	{ 0, 0 },
1515	{ 0, 0 },
1516	{ 0, 0 },
1517	{ 0, 0 },
1518	{ 0, 0 },
1519	{ 0, 0 },
1520	{ 0, 0 },
1521	{ 0, 0 },
1522	{ 0, 0 },
1523	{ 0, 0 },
1524	{ 0, 0 },
1525	{ 0, 0 },
1526	{ 0, 0 },
1527	{ 0, 0 },
1528	{ 0, 0 },
1529	{ 0, 0 },
1530	{ 0, 0 },
1531	{ 0, 0 },
1532	{ 0, 0 },
1533	{ 0, 0 },
1534	{ 0, 0 },
1535	{ 0, 0 },
1536	{ 0, 0 },
1537	{ 0, 0 },
1538	{ 0, 0 },
1539	{ 0, 0 },
1540	{ 0, 0 },
1541	{ 0, 0 },
1542	{ 0, 0 },
1543	{ 0, 0 },
1544	{ 0, 0 },
1545	{ 0, 0 },
1546	{ 0, 0 },
1547	{ 0, 0 },
1548	{ 0, 0 },
1549	{ 0, 0 },
1550	{ 0, 0 },
1551	{ 0, 0 },
1552	{ 0, 0 },
1553	{ 0, 0 },
1554	{ 0, 0 },
1555	{ 0, 0 },
1556	{ 0, 0 },
1557	{ 0, 0 },
1558	{ 0, 0 },
1559	{ 0, 0 },
1560	{ 0, 0 },
1561	{ 0, 0 },
1562	{ 0, 0 },
1563	{ 0, 0 },
1564	{ 0, 0 },
1565	{ 0, 0 },
1566	{ 0, 0 },
1567	{ 0, 0 },
1568	{ 0, 0 },
1569	{ 0, 0 },
1570	{ 0, 0 },
1571	{ 0, 0 },
1572	{ 0, 0 },
1573	{ pfsyncname, PFSYNCCTL_MAXID },
1574};
1575struct list bpflist = { bpfname, NET_BPF_MAXID };
1576struct list ifqlist = { ifqname, IFQCTL_MAXID };
1577
1578struct list kernmalloclist = { kernmallocname, KERN_MALLOC_MAXID };
1579struct list forkstatlist = { forkstatname, KERN_FORKSTAT_MAXID };
1580struct list nchstatslist = { nchstatsname, KERN_NCHSTATS_MAXID };
1581struct list ttylist = { ttysname, KERN_TTY_MAXID };
1582struct list semlist = { semname, KERN_SEMINFO_MAXID };
1583struct list shmlist = { shmname, KERN_SHMINFO_MAXID };
1584struct list watchdoglist = { watchdogname, KERN_WATCHDOG_MAXID };
1585struct list tclist = { tcname, KERN_TIMECOUNTER_MAXID };
1586
1587/*
1588 * handle vfs namei cache statistics
1589 */
1590int
1591sysctl_nchstats(char *string, char **bufpp, int mib[], int flags, int *typep)
1592{
1593	static struct nchstats nch;
1594	int indx;
1595	size_t size;
1596	static int keepvalue = 0;
1597
1598	if (*bufpp == NULL) {
1599		bzero(&nch, sizeof(struct nchstats));
1600		listall(string, &nchstatslist);
1601		return (-1);
1602	}
1603	if ((indx = findname(string, "third", bufpp, &nchstatslist)) == -1)
1604		return (-1);
1605	mib[2] = indx;
1606	if (*bufpp != NULL) {
1607		warnx("fourth level name in %s is invalid", string);
1608		return (-1);
1609	}
1610	if (keepvalue == 0) {
1611		size = sizeof(struct nchstats);
1612		if (sysctl(mib, 2, &nch, &size, NULL, 0) < 0)
1613			return (-1);
1614		keepvalue = 1;
1615	}
1616	if (!nflag)
1617		(void)printf("%s%s", string, equ);
1618	switch (indx) {
1619	case KERN_NCHSTATS_GOODHITS:
1620		(void)printf("%llu\n", nch.ncs_goodhits);
1621		break;
1622	case KERN_NCHSTATS_NEGHITS:
1623		(void)printf("%llu\n", nch.ncs_neghits);
1624		break;
1625	case KERN_NCHSTATS_BADHITS:
1626		(void)printf("%llu\n", nch.ncs_badhits);
1627		break;
1628	case KERN_NCHSTATS_FALSEHITS:
1629		(void)printf("%llu\n", nch.ncs_falsehits);
1630		break;
1631	case KERN_NCHSTATS_MISS:
1632		(void)printf("%llu\n", nch.ncs_miss);
1633		break;
1634	case KERN_NCHSTATS_LONG:
1635		(void)printf("%llu\n", nch.ncs_long);
1636		break;
1637	case KERN_NCHSTATS_PASS2:
1638		(void)printf("%llu\n", nch.ncs_pass2);
1639		break;
1640	case KERN_NCHSTATS_2PASSES:
1641		(void)printf("%llu\n", nch.ncs_2passes);
1642		break;
1643	case KERN_NCHSTATS_REVHITS:
1644		(void)printf("%llu\n", nch.ncs_revhits);
1645		break;
1646	case KERN_NCHSTATS_REVMISS:
1647		(void)printf("%llu\n", nch.ncs_revmiss);
1648		break;
1649	case KERN_NCHSTATS_DOTHITS:
1650		(void)printf("%llu\n", nch.ncs_dothits);
1651		break;
1652	case KERN_NCHSTATS_DOTDOTHITS:
1653		(void)printf("%llu\n", nch.ncs_dotdothits);
1654		break;
1655	}
1656	return (-1);
1657}
1658
1659/*
1660 * handle tty statistics
1661 */
1662int
1663sysctl_tty(char *string, char **bufpp, int mib[], int flags, int *typep)
1664{
1665	int indx;
1666
1667	if (*bufpp == NULL) {
1668		listall(string, &ttylist);
1669		return (-1);
1670	}
1671	if ((indx = findname(string, "third", bufpp, &ttylist)) == -1)
1672		return (-1);
1673	mib[2] = indx;
1674
1675	if ((*typep = ttylist.list[indx].ctl_type) == CTLTYPE_STRUCT) {
1676		if (flags)
1677			warnx("use pstat -t to view %s information",
1678			    string);
1679		return (-1);
1680	}
1681	return (3);
1682}
1683
1684/*
1685 * handle fork statistics
1686 */
1687int
1688sysctl_forkstat(char *string, char **bufpp, int mib[], int flags, int *typep)
1689{
1690	static struct forkstat fks;
1691	static int keepvalue = 0;
1692	int indx;
1693	size_t size;
1694
1695	if (*bufpp == NULL) {
1696		bzero(&fks, sizeof(struct forkstat));
1697		listall(string, &forkstatlist);
1698		return (-1);
1699	}
1700	if ((indx = findname(string, "third", bufpp, &forkstatlist)) == -1)
1701		return (-1);
1702	if (*bufpp != NULL) {
1703		warnx("fourth level name in %s is invalid", string);
1704		return (-1);
1705	}
1706	if (keepvalue == 0) {
1707		size = sizeof(struct forkstat);
1708		if (sysctl(mib, 2, &fks, &size, NULL, 0) < 0)
1709			return (-1);
1710		keepvalue = 1;
1711	}
1712	if (!nflag)
1713		(void)printf("%s%s", string, equ);
1714	switch (indx)	{
1715	case KERN_FORKSTAT_FORK:
1716		(void)printf("%d\n", fks.cntfork);
1717		break;
1718	case KERN_FORKSTAT_VFORK:
1719		(void)printf("%d\n", fks.cntvfork);
1720		break;
1721	case KERN_FORKSTAT_RFORK:
1722		(void)printf("%d\n", fks.cntrfork);
1723		break;
1724	case KERN_FORKSTAT_KTHREAD:
1725		(void)printf("%d\n", fks.cntkthread);
1726		break;
1727	case KERN_FORKSTAT_SIZFORK:
1728		(void)printf("%d\n", fks.sizfork);
1729		break;
1730	case KERN_FORKSTAT_SIZVFORK:
1731		(void)printf("%d\n", fks.sizvfork);
1732		break;
1733	case KERN_FORKSTAT_SIZRFORK:
1734		(void)printf("%d\n", fks.sizrfork);
1735		break;
1736	case KERN_FORKSTAT_SIZKTHREAD:
1737		(void)printf("%d\n", fks.sizkthread);
1738		break;
1739	}
1740	return (-1);
1741}
1742
1743/*
1744 * handle malloc statistics
1745 */
1746int
1747sysctl_malloc(char *string, char **bufpp, int mib[], int flags, int *typep)
1748{
1749	int indx, stor, i;
1750	char *name, bufp[SYSCTL_BUFSIZ], *buf, *ptr;
1751	struct list lp;
1752	size_t size;
1753
1754	if (*bufpp == NULL) {
1755		listall(string, &kernmalloclist);
1756		return (-1);
1757	}
1758	if ((indx = findname(string, "third", bufpp, &kernmalloclist)) == -1)
1759		return (-1);
1760	mib[2] = indx;
1761	if (mib[2] == KERN_MALLOC_BUCKET) {
1762		if ((name = strsep(bufpp, ".")) == NULL) {
1763			size = SYSCTL_BUFSIZ;
1764			stor = mib[2];
1765			mib[2] = KERN_MALLOC_BUCKETS;
1766			buf = bufp;
1767			if (sysctl(mib, 3, buf, &size, NULL, 0) < 0)
1768				return (-1);
1769			mib[2] = stor;
1770			for (stor = 0, i = 0; i < size; i++)
1771				if (buf[i] == ',')
1772					stor++;
1773			lp.list = calloc(stor + 2, sizeof(struct ctlname));
1774			if (lp.list == NULL)
1775				return (-1);
1776			lp.size = stor + 2;
1777			for (i = 1;
1778			    (lp.list[i].ctl_name = strsep(&buf, ",")) != NULL;
1779			    i++) {
1780				lp.list[i].ctl_type = CTLTYPE_STRUCT;
1781			}
1782			lp.list[i].ctl_name = buf;
1783			lp.list[i].ctl_type = CTLTYPE_STRUCT;
1784			listall(string, &lp);
1785			free(lp.list);
1786			return (-1);
1787		}
1788		mib[3] = atoi(name);
1789		return (4);
1790	} else if (mib[2] == KERN_MALLOC_BUCKETS) {
1791		*typep = CTLTYPE_STRING;
1792		return (3);
1793	} else if (mib[2] == KERN_MALLOC_KMEMSTATS) {
1794		size = SYSCTL_BUFSIZ;
1795		stor = mib[2];
1796		mib[2] = KERN_MALLOC_KMEMNAMES;
1797		buf = bufp;
1798		if (sysctl(mib, 3, buf, &size, NULL, 0) < 0)
1799			return (-1);
1800		mib[2] = stor;
1801		if ((name = strsep(bufpp, ".")) == NULL) {
1802			for (stor = 0, i = 0; i < size; i++)
1803				if (buf[i] == ',')
1804					stor++;
1805			lp.list = calloc(stor + 2, sizeof(struct ctlname));
1806			if (lp.list == NULL)
1807				return (-1);
1808			lp.size = stor + 2;
1809			for (i = 1;
1810			    (lp.list[i].ctl_name = strsep(&buf, ",")) != NULL;
1811			    i++) {
1812				if (lp.list[i].ctl_name[0] == '\0') {
1813					i--;
1814					continue;
1815				}
1816				lp.list[i].ctl_type = CTLTYPE_STRUCT;
1817			}
1818			lp.list[i].ctl_name = buf;
1819			lp.list[i].ctl_type = CTLTYPE_STRUCT;
1820			listall(string, &lp);
1821			free(lp.list);
1822			return (-1);
1823		}
1824		ptr = strstr(buf, name);
1825 tryagain:
1826		if (ptr == NULL) {
1827			warnx("fourth level name %s in %s is invalid", name,
1828			    string);
1829			return (-1);
1830		}
1831		if ((*(ptr + strlen(name)) != ',') &&
1832		    (*(ptr + strlen(name)) != '\0')) {
1833			ptr = strstr(ptr + 1, name); /* retry */
1834			goto tryagain;
1835		}
1836		if ((ptr != buf) && (*(ptr - 1) != ',')) {
1837			ptr = strstr(ptr + 1, name); /* retry */
1838			goto tryagain;
1839		}
1840		for (i = 0, stor = 0; buf + i < ptr; i++)
1841			if (buf[i] == ',')
1842				stor++;
1843		mib[3] = stor;
1844		return (4);
1845	} else if (mib[2] == KERN_MALLOC_KMEMNAMES) {
1846		*typep = CTLTYPE_STRING;
1847		return (3);
1848	}
1849	return (-1);
1850}
1851
1852#ifdef CPU_CHIPSET
1853/*
1854 * handle machdep.chipset requests
1855 */
1856struct ctlname chipsetname[] = CTL_CHIPSET_NAMES;
1857struct list chipsetlist = { chipsetname, CPU_CHIPSET_MAXID };
1858
1859int
1860sysctl_chipset(char *string, char **bufpp, int mib[], int flags, int *typep)
1861{
1862	int indx, bwx;
1863	static void *q;
1864	size_t len;
1865	char *p;
1866
1867	if (*bufpp == NULL) {
1868		listall(string, &chipsetlist);
1869		return (-1);
1870	}
1871	if ((indx = findname(string, "third", bufpp, &chipsetlist)) == -1)
1872		return (-1);
1873	mib[2] = indx;
1874	if (!nflag)
1875		printf("%s%s", string, equ);
1876	switch(mib[2]) {
1877	case CPU_CHIPSET_MEM:
1878	case CPU_CHIPSET_DENSE:
1879	case CPU_CHIPSET_PORTS:
1880	case CPU_CHIPSET_HAE_MASK:
1881		len = sizeof(void *);
1882		if (sysctl(mib, 3, &q, &len, NULL, 0) < 0)
1883			return (-1);
1884		printf("%p\n", q);
1885		break;
1886	case CPU_CHIPSET_BWX:
1887		len = sizeof(int);
1888		if (sysctl(mib, 3, &bwx, &len, NULL, 0) < 0)
1889			return (-1);
1890		printf("%d\n", bwx);
1891		break;
1892	case CPU_CHIPSET_TYPE:
1893		if (sysctl(mib, 3, NULL, &len, NULL, 0) < 0)
1894			return (-1);
1895		p = malloc(len + 1);
1896		if (p == NULL)
1897			return (-1);
1898		if (sysctl(mib, 3, p, &len, NULL, 0) < 0) {
1899			free(p);
1900			return (-1);
1901		}
1902		p[len] = '\0';
1903		printf("%s\n", p);
1904		free(p);
1905		break;
1906	}
1907	return (-1);
1908}
1909#endif
1910/*
1911 * handle internet requests
1912 */
1913int
1914sysctl_inet(char *string, char **bufpp, int mib[], int flags, int *typep)
1915{
1916	struct list *lp;
1917	int indx;
1918
1919	if (*bufpp == NULL) {
1920		listall(string, &inetlist);
1921		return (-1);
1922	}
1923	if ((indx = findname(string, "third", bufpp, &inetlist)) == -1)
1924		return (-1);
1925	mib[2] = indx;
1926	if (indx < IPPROTO_MAXID && inetvars[indx].list != NULL)
1927		lp = &inetvars[indx];
1928	else if (!flags)
1929		return (-1);
1930	else {
1931		warnx("%s: no variables defined for this protocol", string);
1932		return (-1);
1933	}
1934	if (*bufpp == NULL) {
1935		listall(string, lp);
1936		return (-1);
1937	}
1938	if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
1939		return (-1);
1940	mib[3] = indx;
1941	*typep = lp->list[indx].ctl_type;
1942	if (*typep == CTLTYPE_NODE) {
1943		int tindx;
1944
1945		if (*bufpp == NULL) {
1946			listall(string, &ifqlist);
1947			return(-1);
1948		}
1949		lp = &ifqlist;
1950		if ((tindx = findname(string, "fifth", bufpp, lp)) == -1)
1951			return (-1);
1952		mib[4] = tindx;
1953		*typep = lp->list[tindx].ctl_type;
1954		return(5);
1955	}
1956	return (4);
1957}
1958
1959#ifdef INET6
1960struct ctlname inet6name[] = CTL_IPV6PROTO_NAMES;
1961struct ctlname ip6name[] = IPV6CTL_NAMES;
1962struct ctlname icmp6name[] = ICMPV6CTL_NAMES;
1963struct ctlname pim6name[] = PIM6CTL_NAMES;
1964struct list inet6list = { inet6name, IPV6PROTO_MAXID };
1965struct list inet6vars[] = {
1966/*0*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1967	{ 0, 0 },
1968	{ 0, 0 },
1969	{ 0, 0 },
1970	{ 0, 0 },
1971	{ 0, 0 },
1972/*10*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1973	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1974/*20*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1975	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1976/*30*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1977	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1978/*40*/	{ 0, 0 },
1979	{ ip6name, IPV6CTL_MAXID },	/* ipv6 */
1980	{ 0, 0 },
1981	{ 0, 0 },
1982	{ 0, 0 },
1983	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1984/*50*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1985	{ 0, 0 },
1986	{ 0, 0 },
1987	{ 0, 0 },
1988	{ icmp6name, ICMPV6CTL_MAXID },	/* icmp6 */
1989	{ 0, 0 },
1990/*60*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1991	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1992/*70*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1993	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1994/*80*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1995	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1996/*90*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1997	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1998/*100*/	{ 0, 0 },
1999	{ 0, 0 },
2000	{ 0, 0 },
2001	{ pim6name, PIM6CTL_MAXID },	/* pim6 */
2002};
2003
2004/*
2005 * handle internet6 requests
2006 */
2007int
2008sysctl_inet6(char *string, char **bufpp, int mib[], int flags, int *typep)
2009{
2010	struct list *lp;
2011	int indx;
2012
2013	if (*bufpp == NULL) {
2014		listall(string, &inet6list);
2015		return (-1);
2016	}
2017	if ((indx = findname(string, "third", bufpp, &inet6list)) == -1)
2018		return (-1);
2019	mib[2] = indx;
2020	if (indx < IPV6PROTO_MAXID && inet6vars[indx].list != NULL)
2021		lp = &inet6vars[indx];
2022	else if (!flags)
2023		return (-1);
2024	else {
2025		warnx("%s: no variables defined for this protocol", string);
2026		return (-1);
2027	}
2028	if (*bufpp == NULL) {
2029		listall(string, lp);
2030		return (-1);
2031	}
2032	if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
2033		return (-1);
2034	mib[3] = indx;
2035	*typep = lp->list[indx].ctl_type;
2036	return (4);
2037}
2038#endif
2039
2040/* handle bpf requests */
2041int
2042sysctl_bpf(char *string, char **bufpp, int mib[], int flags, int *typep)
2043{
2044	int indx;
2045
2046	if (*bufpp == NULL) {
2047		listall(string, &bpflist);
2048		return (-1);
2049	}
2050	if ((indx = findname(string, "third", bufpp, &bpflist)) == -1)
2051		return (-1);
2052	mib[2] = indx;
2053	*typep = CTLTYPE_INT;
2054	return (3);
2055}
2056
2057struct ctlname mplsname[] = MPLSCTL_NAMES;
2058struct list mplslist = { mplsname, MPLSCTL_MAXID };
2059
2060/* handle MPLS requests */
2061int
2062sysctl_mpls(char *string, char **bufpp, int mib[], int flags, int *typep)
2063{
2064	struct list *lp;
2065	int indx;
2066
2067	if (*bufpp == NULL) {
2068		listall(string, &mplslist);
2069		return (-1);
2070	}
2071	if ((indx = findname(string, "third", bufpp, &mplslist)) == -1)
2072		return (-1);
2073	mib[2] = indx;
2074	*typep = mplslist.list[indx].ctl_type;
2075	if (*typep == CTLTYPE_NODE) {
2076		int tindx;
2077
2078		if (*bufpp == NULL) {
2079			listall(string, &ifqlist);
2080			return(-1);
2081		}
2082		lp = &ifqlist;
2083		if ((tindx = findname(string, "fourth", bufpp, lp)) == -1)
2084			return (-1);
2085		mib[3] = tindx;
2086		*typep = lp->list[tindx].ctl_type;
2087		return(4);
2088	}
2089	return (3);
2090}
2091
2092/*
2093 * Handle SysV semaphore info requests
2094 */
2095int
2096sysctl_seminfo(string, bufpp, mib, flags, typep)
2097	char *string;
2098	char **bufpp;
2099	int mib[];
2100	int flags;
2101	int *typep;
2102{
2103	int indx;
2104
2105	if (*bufpp == NULL) {
2106		listall(string, &semlist);
2107		return (-1);
2108	}
2109	if ((indx = findname(string, "third", bufpp, &semlist)) == -1)
2110		return (-1);
2111	mib[2] = indx;
2112	*typep = CTLTYPE_INT;
2113	return (3);
2114}
2115
2116/*
2117 * Handle SysV shared memory info requests
2118 */
2119int
2120sysctl_shminfo(string, bufpp, mib, flags, typep)
2121	char *string;
2122	char **bufpp;
2123	int mib[];
2124	int flags;
2125	int *typep;
2126{
2127	int indx;
2128
2129	if (*bufpp == NULL) {
2130		listall(string, &shmlist);
2131		return (-1);
2132	}
2133	if ((indx = findname(string, "third", bufpp, &shmlist)) == -1)
2134		return (-1);
2135	mib[2] = indx;
2136	*typep = CTLTYPE_INT;
2137	return (3);
2138}
2139
2140/*
2141 * Handle watchdog support
2142 */
2143int
2144sysctl_watchdog(char *string, char **bufpp, int mib[], int flags,
2145    int *typep)
2146{
2147	int indx;
2148
2149	if (*bufpp == NULL) {
2150		listall(string, &watchdoglist);
2151		return (-1);
2152	}
2153	if ((indx = findname(string, "third", bufpp, &watchdoglist)) == -1)
2154		return (-1);
2155	mib[2] = indx;
2156	*typep = watchdoglist.list[indx].ctl_type;
2157	return (3);
2158}
2159
2160/*
2161 * Handle timecounter support
2162 */
2163int
2164sysctl_tc(char *string, char **bufpp, int mib[], int flags,
2165    int *typep)
2166{
2167	int indx;
2168
2169	if (*bufpp == NULL) {
2170		listall(string, &tclist);
2171		return (-1);
2172	}
2173	if ((indx = findname(string, "third", bufpp, &tclist)) == -1)
2174		return (-1);
2175	mib[2] = indx;
2176	*typep = tclist.list[indx].ctl_type;
2177	return (3);
2178}
2179
2180/*
2181 * Handle hardware monitoring sensors support
2182 */
2183int
2184sysctl_sensors(char *string, char **bufpp, int mib[], int flags, int *typep)
2185{
2186	char *devname, *typename;
2187	int dev, numt, i;
2188	enum sensor_type type;
2189	struct sensordev snsrdev;
2190	size_t sdlen = sizeof(snsrdev);
2191
2192	if (*bufpp == NULL) {
2193		char buf[SYSCTL_BUFSIZ];
2194
2195		/* scan all sensor devices */
2196		for (dev = 0; dev < MAXSENSORDEVICES; dev++) {
2197			mib[2] = dev;
2198			if (sysctl(mib, 3, &snsrdev, &sdlen, NULL, 0) == -1)
2199				continue;
2200			snprintf(buf, sizeof(buf), "%s.%s",
2201			    string, snsrdev.xname);
2202			print_sensordev(buf, mib, 3, &snsrdev);
2203		}
2204		return (-1);
2205	}
2206
2207	/*
2208	 * If we get this far, it means that some arguments were
2209	 * provided below hw.sensors tree.
2210	 * The first branch of hw.sensors tree is the device name.
2211	 */
2212	if ((devname = strsep(bufpp, ".")) == NULL) {
2213		warnx("%s: incomplete specification", string);
2214		return (-1);
2215	}
2216	/* convert sensor device string to an integer */
2217	for (dev = 0; dev < MAXSENSORDEVICES; dev++) {
2218		mib[2] = dev;
2219		if (sysctl(mib, 3, &snsrdev, &sdlen, NULL, 0) == -1)
2220			continue;
2221		if (strcmp(devname, snsrdev.xname) == 0)
2222			break;
2223	}
2224	if (strcmp(devname, snsrdev.xname) != 0) {
2225		warnx("%s: sensor device not found: %s", string, devname);
2226		return (-1);
2227	}
2228	if (*bufpp == NULL) {
2229		/* only device name was provided -- let's print all sensors
2230		 * that are attached to the specified device
2231		 */
2232		print_sensordev(string, mib, 3, &snsrdev);
2233		return (-1);
2234	}
2235
2236	/*
2237	 * At this point we have identified the sensor device,
2238	 * now let's go further and identify sensor type.
2239	 */
2240	if ((typename = strsep(bufpp, ".")) == NULL) {
2241		warnx("%s: incomplete specification", string);
2242		return (-1);
2243	}
2244	numt = -1;
2245	for (i = 0; typename[i] != '\0'; i++)
2246		if (isdigit(typename[i])) {
2247			numt = atoi(&typename[i]);
2248			typename[i] = '\0';
2249			break;
2250		}
2251	for (type = 0; type < SENSOR_MAX_TYPES; type++)
2252		if (strcmp(typename, sensor_type_s[type]) == 0)
2253			break;
2254	if (type == SENSOR_MAX_TYPES) {
2255		warnx("%s: sensor type not recognised: %s", string, typename);
2256		return (-1);
2257	}
2258	mib[3] = type;
2259
2260	/*
2261	 * If no integer was provided after sensor_type, let's
2262	 * print all sensors of the specified type.
2263	 */
2264	if (numt == -1) {
2265		print_sensordev(string, mib, 4, &snsrdev);
2266		return (-1);
2267	}
2268
2269	/*
2270	 * At this point we know that we have received a direct request
2271	 * via command-line for a specific sensor. Let's have the parse()
2272	 * function deal with it further, and report any errors if such
2273	 * sensor node does not exist.
2274	 */
2275	mib[4] = numt;
2276	*typep = CTLTYPE_STRUCT;
2277	return (5);
2278}
2279
2280/*
2281 * Print sensors from the specified device.
2282 */
2283
2284void
2285print_sensordev(char *string, int mib[], u_int mlen, struct sensordev *snsrdev)
2286{
2287	char buf[SYSCTL_BUFSIZ];
2288	enum sensor_type type;
2289
2290	if (mlen == 3) {
2291		for (type = 0; type < SENSOR_MAX_TYPES; type++) {
2292			mib[3] = type;
2293			snprintf(buf, sizeof(buf), "%s.%s",
2294			    string, sensor_type_s[type]);
2295			print_sensordev(buf, mib, mlen+1, snsrdev);
2296		}
2297		return;
2298	}
2299
2300	if (mlen == 4) {
2301		int numt;
2302
2303		type = mib[3];
2304		for (numt = 0; numt < snsrdev->maxnumt[type]; numt++) {
2305			mib[4] = numt;
2306			snprintf(buf, sizeof(buf), "%s%u", string, numt);
2307			print_sensordev(buf, mib, mlen+1, snsrdev);
2308		}
2309		return;
2310	}
2311
2312	if (mlen == 5) {
2313		struct sensor snsr;
2314		size_t slen = sizeof(snsr);
2315
2316		/* this function is only printing sensors in bulk, so we
2317		 * do not return any error messages if the requested sensor
2318		 * is not found by sysctl(3)
2319		 */
2320		if (sysctl(mib, 5, &snsr, &slen, NULL, 0) == -1)
2321			return;
2322
2323		if (slen > 0 && (snsr.flags & SENSOR_FINVALID) == 0) {
2324			if (!nflag)
2325				printf("%s%s", string, equ);
2326			print_sensor(&snsr);
2327			printf("\n");
2328		}
2329		return;
2330	}
2331}
2332
2333void
2334print_sensor(struct sensor *s)
2335{
2336	const char *name;
2337
2338	if (s->flags & SENSOR_FUNKNOWN)
2339		printf("unknown");
2340	else {
2341		switch (s->type) {
2342		case SENSOR_TEMP:
2343			printf("%.2f degC",
2344			    (s->value - 273150000) / 1000000.0);
2345			break;
2346		case SENSOR_FANRPM:
2347			printf("%lld RPM", s->value);
2348			break;
2349		case SENSOR_VOLTS_DC:
2350			printf("%.2f VDC", s->value / 1000000.0);
2351			break;
2352		case SENSOR_AMPS:
2353			printf("%.2f A", s->value / 1000000.0);
2354			break;
2355		case SENSOR_WATTHOUR:
2356			printf("%.2f Wh", s->value / 1000000.0);
2357			break;
2358		case SENSOR_AMPHOUR:
2359			printf("%.2f Ah", s->value / 1000000.0);
2360			break;
2361		case SENSOR_INDICATOR:
2362			printf("%s", s->value ? "On" : "Off");
2363			break;
2364		case SENSOR_INTEGER:
2365			printf("%lld", s->value);
2366			break;
2367		case SENSOR_PERCENT:
2368			printf("%.2f%%", s->value / 1000.0);
2369			break;
2370		case SENSOR_LUX:
2371			printf("%.2f lx", s->value / 1000000.0);
2372			break;
2373		case SENSOR_DRIVE:
2374			switch (s->value) {
2375			case SENSOR_DRIVE_EMPTY:
2376				name = "empty";
2377				break;
2378			case SENSOR_DRIVE_READY:
2379				name = "ready";
2380				break;
2381			case SENSOR_DRIVE_POWERUP:
2382				name = "powering up";
2383				break;
2384			case SENSOR_DRIVE_ONLINE:
2385				name = "online";
2386				break;
2387			case SENSOR_DRIVE_IDLE:
2388				name = "idle";
2389				break;
2390			case SENSOR_DRIVE_ACTIVE:
2391				name = "active";
2392				break;
2393			case SENSOR_DRIVE_REBUILD:
2394				name = "rebuilding";
2395				break;
2396			case SENSOR_DRIVE_POWERDOWN:
2397				name = "powering down";
2398				break;
2399			case SENSOR_DRIVE_FAIL:
2400				name = "failed";
2401				break;
2402			case SENSOR_DRIVE_PFAIL:
2403				name = "degraded";
2404				break;
2405			default:
2406				name = "unknown";
2407				break;
2408			}
2409			printf(name);
2410			break;
2411		case SENSOR_TIMEDELTA:
2412			printf("%.6f secs", s->value / 1000000000.0);
2413			break;
2414		default:
2415			printf("unknown");
2416		}
2417	}
2418
2419	if (s->desc[0] != '\0')
2420		printf(" (%s)", s->desc);
2421
2422	switch (s->status) {
2423	case SENSOR_S_UNSPEC:
2424		break;
2425	case SENSOR_S_OK:
2426		printf(", OK");
2427		break;
2428	case SENSOR_S_WARN:
2429		printf(", WARNING");
2430		break;
2431	case SENSOR_S_CRIT:
2432		printf(", CRITICAL");
2433		break;
2434	case SENSOR_S_UNKNOWN:
2435		printf(", UNKNOWN");
2436		break;
2437	}
2438
2439	if (s->tv.tv_sec) {
2440		time_t t = s->tv.tv_sec;
2441		char ct[26];
2442
2443		ctime_r(&t, ct);
2444		ct[19] = '\0';
2445		printf(", %s.%03ld", ct, s->tv.tv_usec / 1000);
2446	}
2447}
2448
2449struct emulname {
2450	char *name;
2451	int index;
2452} *emul_names;
2453int	emul_num, nemuls;
2454int	emul_init(void);
2455
2456int
2457sysctl_emul(char *string, char *newval, int flags)
2458{
2459	int mib[4], enabled, i, old, print, found = 0;
2460	char *head, *target;
2461	size_t len;
2462
2463	if (emul_init() == -1) {
2464		warnx("emul_init: out of memory");
2465		return (1);
2466	}
2467
2468	mib[0] = CTL_KERN;
2469	mib[1] = KERN_EMUL;
2470	mib[3] = KERN_EMUL_ENABLED;
2471	head = "kern.emul.";
2472
2473	if (aflag || strcmp(string, "kern.emul") == 0) {
2474		if (newval) {
2475			warnx("%s: specification is incomplete", string);
2476			return (1);
2477		}
2478		if (nflag)
2479			printf("%d\n", nemuls);
2480		else
2481			printf("%snemuls%s%d\n", head, equ, nemuls);
2482		for (i = 0; i < emul_num; i++) {
2483			if (emul_names[i].name == NULL)
2484				break;
2485			if (i > 0 && strcmp(emul_names[i].name,
2486			    emul_names[i-1].name) == 0)
2487				continue;
2488			mib[2] = emul_names[i].index;
2489			len = sizeof(int);
2490			if (sysctl(mib, 4, &enabled, &len, NULL, 0) == -1) {
2491				warn("%s", string);
2492				continue;
2493			}
2494			if (nflag)
2495				printf("%d\n", enabled);
2496			else
2497				printf("%s%s%s%d\n", head, emul_names[i].name,
2498				    equ, enabled);
2499		}
2500		return (0);
2501	}
2502	/* User specified a third level name */
2503	target = strrchr(string, '.');
2504	target++;
2505	if (strcmp(target, "nemuls") == 0) {
2506		if (newval) {
2507			warnx("Operation not permitted");
2508			return (1);
2509		}
2510		if (nflag)
2511			printf("%d\n", nemuls);
2512		else
2513			printf("%snemuls = %d\n", head, nemuls);
2514		return (0);
2515	}
2516	print = 1;
2517	for (i = 0; i < emul_num; i++) {
2518		if (!emul_names[i].name || (strcmp(target, emul_names[i].name)))
2519			continue;
2520		found = 1;
2521		mib[2] = emul_names[i].index;
2522		len = sizeof(int);
2523		if (newval) {
2524			enabled = atoi(newval);
2525			if (sysctl(mib, 4, &old, &len, &enabled, len) == -1) {
2526				warn("%s", string);
2527				print = 0;
2528				continue;
2529			}
2530			if (print) {
2531				if (nflag)
2532					printf("%d\n", enabled);
2533				else
2534					printf("%s%s: %d -> %d\n", head,
2535					    target, old, enabled);
2536			}
2537		} else {
2538			if (sysctl(mib, 4, &enabled, &len, NULL, 0) == -1) {
2539				warn("%s", string);
2540				continue;
2541			}
2542			if (print) {
2543				if (nflag)
2544					printf("%d\n", enabled);
2545				else
2546					printf("%s%s = %d\n", head, target,
2547					    enabled);
2548			}
2549		}
2550		print = 0;
2551	}
2552	if (!found)
2553		warnx("third level name %s in kern.emul is invalid",
2554		    string);
2555	return (0);
2556
2557
2558}
2559
2560int
2561emulcmp(const void *m, const void *n)
2562{
2563	const struct emulname *a = m, *b = n;
2564
2565	if (!a || !a->name)
2566		return 1;
2567	if (!b || !b->name)
2568		return -1;
2569	return (strcmp(a->name, b->name));
2570}
2571
2572int
2573emul_init(void)
2574{
2575	static int done;
2576	char string[16];
2577	int mib[4], i;
2578	size_t len;
2579
2580	if (done)
2581		return (0);
2582	done = 1;
2583
2584	mib[0] = CTL_KERN;
2585	mib[1] = KERN_EMUL;
2586	mib[2] = KERN_EMUL_NUM;
2587	len = sizeof(int);
2588	if (sysctl(mib, 3, &emul_num, &len, NULL, 0) == -1)
2589		return (-1);
2590
2591	emul_names = calloc(emul_num, sizeof(*emul_names));
2592	if (emul_names == NULL)
2593		return (-1);
2594
2595	nemuls = emul_num;
2596	for (i = 0; i < emul_num; i++) {
2597		emul_names[i].index = mib[2] = i + 1;
2598		mib[3] = KERN_EMUL_NAME;
2599		len = sizeof(string);
2600		if (sysctl(mib, 4, string, &len, NULL, 0) == -1)
2601			continue;
2602		if (strcmp(string, "native") == 0)
2603			continue;
2604		emul_names[i].name = strdup(string);
2605		if (emul_names[i].name == NULL) {
2606			free(emul_names);
2607			return (-1);
2608		}
2609	}
2610	qsort(emul_names, nemuls, sizeof(*emul_names), emulcmp);
2611	for (i = 0; i < emul_num; i++) {
2612		if (!emul_names[i].name || (i > 0 &&
2613		    strcmp(emul_names[i].name, emul_names[i - 1].name) == 0))
2614			nemuls--;
2615	}
2616	return (0);
2617}
2618
2619/*
2620 * Scan a list of names searching for a particular name.
2621 */
2622int
2623findname(char *string, char *level, char **bufp, struct list *namelist)
2624{
2625	char *name;
2626	int i;
2627
2628	if (namelist->list == 0 || (name = strsep(bufp, ".")) == NULL) {
2629		warnx("%s: incomplete specification", string);
2630		return (-1);
2631	}
2632	for (i = 0; i < namelist->size; i++)
2633		if (namelist->list[i].ctl_name != NULL &&
2634		    strcmp(name, namelist->list[i].ctl_name) == 0)
2635			break;
2636	if (i == namelist->size) {
2637		warnx("%s level name %s in %s is invalid", level, name, string);
2638		return (-1);
2639	}
2640	return (i);
2641}
2642
2643void
2644usage(void)
2645{
2646
2647	(void)fprintf(stderr,
2648	    "usage: sysctl [-Aan]\n"
2649	    "       sysctl [-n] name ...\n"
2650	    "       sysctl [-nq] name=value ...\n");
2651	exit(1);
2652}
2653