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