sysctl.c revision 1.122
1/*	$OpenBSD: sysctl.c,v 1.122 2005/05/24 04:20:26 markus 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.122 2005/05/24 04:20:26 markus 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/ip_var.h>
76#include <netinet/udp.h>
77#include <netinet/udp_var.h>
78#include <netinet/tcp.h>
79#include <netinet/tcp_timer.h>
80#include <netinet/tcp_var.h>
81#include <netinet/ip_gre.h>
82#include <netinet/ip_ipcomp.h>
83#include <netinet/ip_carp.h>
84
85#ifdef INET6
86#include <netinet/ip6.h>
87#include <netinet/icmp6.h>
88#include <netinet6/ip6_var.h>
89#include <netinet6/pim6_var.h>
90#endif
91
92#include <uvm/uvm_swap_encrypt.h>
93
94#include <ufs/ufs/quota.h>
95#include <ufs/ufs/inode.h>
96#include <ufs/ffs/fs.h>
97#include <ufs/ffs/ffs_extern.h>
98
99#include <nfs/rpcv2.h>
100#include <nfs/nfsproto.h>
101#include <nfs/nfs.h>
102
103#include <netipx/ipx.h>
104#include <netipx/ipx_var.h>
105#include <netipx/spx_var.h>
106#include <ddb/db_var.h>
107#include <dev/rndvar.h>
108
109#include <err.h>
110#include <errno.h>
111#include <stdio.h>
112#include <stdlib.h>
113#include <string.h>
114#include <ctype.h>
115
116#ifdef CPU_BIOS
117#include <machine/biosvar.h>
118#endif
119
120struct ctlname topname[] = CTL_NAMES;
121struct ctlname kernname[] = CTL_KERN_NAMES;
122struct ctlname vmname[] = CTL_VM_NAMES;
123struct ctlname fsname[] = CTL_FS_NAMES;
124struct ctlname netname[] = CTL_NET_NAMES;
125struct ctlname hwname[] = CTL_HW_NAMES;
126struct ctlname username[] = CTL_USER_NAMES;
127struct ctlname debugname[CTL_DEBUG_MAXID];
128struct ctlname kernmallocname[] = CTL_KERN_MALLOC_NAMES;
129struct ctlname forkstatname[] = CTL_KERN_FORKSTAT_NAMES;
130struct ctlname nchstatsname[] = CTL_KERN_NCHSTATS_NAMES;
131struct ctlname ttysname[] = CTL_KERN_TTY_NAMES;
132struct ctlname semname[] = CTL_KERN_SEMINFO_NAMES;
133struct ctlname shmname[] = CTL_KERN_SHMINFO_NAMES;
134struct ctlname watchdogname[] = CTL_KERN_WATCHDOG_NAMES;
135struct ctlname tcname[] = CTL_KERN_TIMECOUNTER_NAMES;
136struct ctlname *vfsname;
137#ifdef CTL_MACHDEP_NAMES
138struct ctlname machdepname[] = CTL_MACHDEP_NAMES;
139#endif
140struct ctlname ddbname[] = CTL_DDB_NAMES;
141char names[BUFSIZ];
142int lastused;
143
144struct list {
145	struct	ctlname *list;
146	int	size;
147};
148struct list toplist = { topname, CTL_MAXID };
149struct list secondlevel[] = {
150	{ 0, 0 },			/* CTL_UNSPEC */
151	{ kernname, KERN_MAXID },	/* CTL_KERN */
152	{ vmname, VM_MAXID },		/* CTL_VM */
153	{ fsname, FS_MAXID },		/* CTL_FS */
154	{ netname, NET_MAXID },		/* CTL_NET */
155	{ 0, CTL_DEBUG_MAXID },		/* CTL_DEBUG */
156	{ hwname, HW_MAXID },		/* CTL_HW */
157#ifdef CTL_MACHDEP_NAMES
158	{ machdepname, CPU_MAXID },	/* CTL_MACHDEP */
159#else
160	{ 0, 0 },			/* CTL_MACHDEP */
161#endif
162	{ username, USER_MAXID },	/* CTL_USER_NAMES */
163	{ ddbname, DBCTL_MAXID },	/* CTL_DDB_NAMES */
164	{ 0, 0 },			/* CTL_VFS */
165};
166
167int	Aflag, aflag, nflag, qflag;
168
169/*
170 * Variables requiring special processing.
171 */
172#define	CLOCK		0x00000001
173#define	BOOTTIME	0x00000002
174#define	CHRDEV		0x00000004
175#define	BLKDEV		0x00000008
176#define	RNDSTATS	0x00000010
177#define	BADDYNAMIC	0x00000020
178#define	BIOSGEO		0x00000040
179#define	BIOSDEV		0x00000080
180#define	MAJ2DEV		0x00000100
181#define	UNSIGNED	0x00000200
182#define	KMEMBUCKETS	0x00000400
183#define	LONGARRAY	0x00000800
184#define	KMEMSTATS	0x00001000
185#define	SENSORS		0x00002000
186#define	ZTSSCALE	0x00004000
187
188/* prototypes */
189void debuginit(void);
190void listall(char *, struct list *);
191void parse(char *, int);
192void parse_baddynamic(int *, size_t, char *, void **, size_t *, int, int);
193void usage(void);
194int findname(char *, char *, char **, struct list *);
195int sysctl_inet(char *, char **, int *, int, int *);
196#ifdef INET6
197int sysctl_inet6(char *, char **, int *, int, int *);
198#endif
199int sysctl_bpf(char *, char **, int *, int, int *);
200int sysctl_ipx(char *, char **, int *, int, int *);
201int sysctl_fs(char *, char **, int *, int, int *);
202static int sysctl_vfs(char *, char **, int[], int, int *);
203static int sysctl_vfsgen(char *, char **, int[], int, int *);
204int sysctl_bios(char *, char **, int *, int, int *);
205int sysctl_swpenc(char *, char **, int *, int, int *);
206int sysctl_forkstat(char *, char **, int *, int, int *);
207int sysctl_tty(char *, char **, int *, int, int *);
208int sysctl_nchstats(char *, char **, int *, int, int *);
209int sysctl_malloc(char *, char **, int *, int, int *);
210int sysctl_seminfo(char *, char **, int *, int, int *);
211int sysctl_shminfo(char *, char **, int *, int, int *);
212int sysctl_watchdog(char *, char **, int *, int, int *);
213int sysctl_tc(char *, char **, int *, int, int *);
214int sysctl_sensors(char *, char **, int *, int, int *);
215int sysctl_emul(char *, char *, int);
216#ifdef CPU_CHIPSET
217int sysctl_chipset(char *, char **, int *, int, int *);
218#endif
219void vfsinit(void);
220
221char *equ = "=";
222
223int
224main(int argc, char *argv[])
225{
226	int ch, lvl1;
227
228	while ((ch = getopt(argc, argv, "Aanqw")) != -1) {
229		switch (ch) {
230
231		case 'A':
232			Aflag = 1;
233			break;
234
235		case 'a':
236			aflag = 1;
237			break;
238
239		case 'n':
240			nflag = 1;
241			break;
242
243		case 'q':
244			qflag = 1;
245			break;
246
247		case 'w':
248			/* flag no longer needed; var=value implies write */
249			break;
250
251		default:
252			usage();
253		}
254	}
255	argc -= optind;
256	argv += optind;
257
258	if (argc == 0 && (Aflag || aflag)) {
259		debuginit();
260		vfsinit();
261		for (lvl1 = 1; lvl1 < CTL_MAXID; lvl1++)
262			listall(topname[lvl1].ctl_name, &secondlevel[lvl1]);
263		return (0);
264	}
265	if (argc == 0)
266		usage();
267	for (; *argv != NULL; ++argv)
268		parse(*argv, 1);
269	return (0);
270}
271
272/*
273 * List all variables known to the system.
274 */
275void
276listall(char *prefix, struct list *lp)
277{
278	char *cp, name[BUFSIZ];
279	int lvl2, len;
280
281	if (lp->list == NULL)
282		return;
283	if ((len = strlcpy(name, prefix, sizeof(name))) >= sizeof(name))
284		errx(1, "%s: name too long", prefix);
285	cp = name + len++;
286	*cp++ = '.';
287	for (lvl2 = 0; lvl2 < lp->size; lvl2++) {
288		if (lp->list[lvl2].ctl_name == NULL)
289			continue;
290		if (strlcpy(cp, lp->list[lvl2].ctl_name,
291		    sizeof(name) - len) >= sizeof(name) - len)
292			warn("%s: name too long", lp->list[lvl2].ctl_name);
293		parse(name, Aflag);
294	}
295}
296
297/*
298 * Parse a name into a MIB entry.
299 * Lookup and print out the MIB entry if it exists.
300 * Set a new value if requested.
301 */
302void
303parse(char *string, int flags)
304{
305	int indx, type, state, intval, len;
306	size_t size, newsize = 0;
307	int lal = 0, special = 0;
308	void *newval = 0;
309	int64_t quadval;
310	struct list *lp;
311	int mib[CTL_MAXNAME];
312	char *cp, *bufp, buf[BUFSIZ];
313#ifdef CPU_ZTSSCALE
314	struct ztsscale {
315		int ts_minx;
316		int ts_maxx;
317		int ts_miny;
318		int ts_maxy;
319	} tsbuf;
320#endif
321
322	(void)strlcpy(buf, string, sizeof(buf));
323	bufp = buf;
324	if ((cp = strchr(string, '=')) != NULL) {
325		*strchr(buf, '=') = '\0';
326		*cp++ = '\0';
327		while (isspace(*cp))
328			cp++;
329		newval = cp;
330		newsize = strlen(cp);
331	}
332	if ((indx = findname(string, "top", &bufp, &toplist)) == -1)
333		return;
334	mib[0] = indx;
335	if (indx == CTL_VFS)
336		vfsinit();
337	if (indx == CTL_DEBUG)
338		debuginit();
339	lp = &secondlevel[indx];
340	if (lp->list == 0) {
341		warnx("%s: class is not implemented", topname[indx].ctl_name);
342		return;
343	}
344	if (bufp == NULL) {
345		listall(topname[indx].ctl_name, lp);
346		return;
347	}
348	if ((indx = findname(string, "second", &bufp, lp)) == -1)
349		return;
350	mib[1] = indx;
351	type = lp->list[indx].ctl_type;
352	len = 2;
353	switch (mib[0]) {
354
355	case CTL_KERN:
356		switch (mib[1]) {
357		case KERN_PROF:
358			mib[2] = GPROF_STATE;
359			size = sizeof(state);
360			if (sysctl(mib, 3, &state, &size, NULL, 0) == -1) {
361				if (flags == 0)
362					return;
363				if (!nflag)
364					(void)printf("%s: ", string);
365				(void)puts("kernel is not compiled for profiling");
366				return;
367			}
368			if (!nflag)
369				(void)printf("%s = %s\n", string,
370				    state == GMON_PROF_OFF ? "off" : "running");
371			return;
372		case KERN_FORKSTAT:
373			sysctl_forkstat(string, &bufp, mib, flags, &type);
374			return;
375		case KERN_TTY:
376			len = sysctl_tty(string, &bufp, mib, flags, &type);
377			if (len < 0)
378				return;
379			break;
380		case KERN_NCHSTATS:
381			sysctl_nchstats(string, &bufp, mib, flags, &type);
382			return;
383		case KERN_MALLOCSTATS:
384			len = sysctl_malloc(string, &bufp, mib, flags, &type);
385			if (len < 0)
386				return;
387			if (mib[2] == KERN_MALLOC_BUCKET)
388				special |= KMEMBUCKETS;
389			if (mib[2] == KERN_MALLOC_KMEMSTATS)
390				special |= KMEMSTATS;
391			newsize = 0;
392			break;
393		case KERN_MBSTAT:
394			if (flags == 0)
395				return;
396			warnx("use netstat to view %s", string);
397			return;
398		case KERN_MSGBUF:
399			if (flags == 0)
400				return;
401			warnx("use dmesg to view %s", string);
402			return;
403		case KERN_VNODE:
404		case KERN_FILE:
405			if (flags == 0)
406				return;
407			warnx("use pstat to view %s information", string);
408			return;
409		case KERN_PROC:
410		case KERN_PROC2:
411			if (flags == 0)
412				return;
413			warnx("use ps to view %s information", string);
414			return;
415		case KERN_CLOCKRATE:
416			special |= CLOCK;
417			break;
418		case KERN_BOOTTIME:
419			special |= BOOTTIME;
420			break;
421		case KERN_RND:
422			special |= RNDSTATS;
423			break;
424		case KERN_HOSTID:
425		case KERN_ARND:
426			special |= UNSIGNED;
427			break;
428		case KERN_CPTIME:
429			special |= LONGARRAY;
430			lal = CPUSTATES;
431			break;
432		case KERN_SEMINFO:
433			len = sysctl_seminfo(string, &bufp, mib, flags, &type);
434			if (len < 0)
435				return;
436			break;
437		case KERN_SHMINFO:
438			len = sysctl_shminfo(string, &bufp, mib, flags, &type);
439			if (len < 0)
440				return;
441			break;
442		case KERN_WATCHDOG:
443			len = sysctl_watchdog(string, &bufp, mib, flags,
444			    &type);
445			if (len < 0)
446				return;
447			break;
448		case KERN_TIMECOUNTER:
449			len = sysctl_tc(string, &bufp, mib, flags,
450			    &type);
451			if (len < 0)
452				return;
453			break;
454		case KERN_EMUL:
455			sysctl_emul(string, newval, flags);
456			return;
457		}
458		break;
459
460	case CTL_HW:
461		switch (mib[1]) {
462		case HW_DISKSTATS:
463			/*
464			 * Only complain if someone asks explicitly for this,
465			 * otherwise "fail" silently.
466			 */
467			if (flags)
468				warnx("use vmstat to view %s information",
469				    string);
470			return;
471		case HW_SENSORS:
472			special |= SENSORS;
473			len = sysctl_sensors(string, &bufp, mib, flags, &type);
474			if (len < 0)
475				return;
476			break;
477		}
478		break;
479
480	case CTL_VM:
481		if (mib[1] == VM_LOADAVG) {
482			double loads[3];
483
484			getloadavg(loads, 3);
485			if (!nflag)
486				(void)printf("%s%s", string, equ);
487			(void)printf("%.2f %.2f %.2f\n", loads[0],
488			    loads[1], loads[2]);
489			return;
490		} else if (mib[1] == VM_PSSTRINGS) {
491			struct _ps_strings _ps;
492
493			size = sizeof(_ps);
494			if (sysctl(mib, 2, &_ps, &size, NULL, 0) == -1) {
495				if (flags == 0)
496					return;
497				if (!nflag)
498					(void)printf("%s: ", string);
499				(void)puts("can't find ps strings");
500				return;
501			}
502			if (!nflag)
503				(void)printf("%s%s", string, equ);
504			(void)printf("%p\n", _ps.val);
505			return;
506		} else if (mib[1] == VM_SWAPENCRYPT) {
507			len = sysctl_swpenc(string, &bufp, mib, flags, &type);
508			if (len < 0)
509				return;
510
511			break;
512		} else if (mib[1] == VM_NKMEMPAGES ||
513		    mib[1] == VM_ANONMIN ||
514		    mib[1] == VM_VTEXTMIN ||
515		    mib[1] == VM_VNODEMIN) {
516			break;
517		}
518		if (flags == 0)
519			return;
520		warnx("use vmstat or systat to view %s information", string);
521		return;
522
523		break;
524
525	case CTL_NET:
526		if (mib[1] == PF_INET) {
527			len = sysctl_inet(string, &bufp, mib, flags, &type);
528			if (len < 0)
529				return;
530
531			if ((mib[2] == IPPROTO_TCP &&
532			    mib[3] == TCPCTL_BADDYNAMIC) ||
533			    (mib[2] == IPPROTO_UDP &&
534			    mib[3] == UDPCTL_BADDYNAMIC)) {
535
536				special |= BADDYNAMIC;
537
538				if (newval != NULL)
539					parse_baddynamic(mib, len, string,
540					    &newval, &newsize, flags, nflag);
541			}
542			break;
543		}
544#ifdef INET6
545		if (mib[1] == PF_INET6) {
546			len = sysctl_inet6(string, &bufp, mib, flags, &type);
547			if (len < 0)
548				return;
549
550			break;
551		}
552#endif
553		if (mib[1] == PF_IPX) {
554			len = sysctl_ipx(string, &bufp, mib, flags, &type);
555			if (len >= 0)
556				break;
557			return;
558		}
559		if (mib[1] == PF_BPF) {
560			len = sysctl_bpf(string, &bufp, mib, flags, &type);
561			if (len < 0)
562				return;
563			break;
564		}
565		if (flags == 0)
566			return;
567		warnx("use netstat to view %s information", string);
568		return;
569
570	case CTL_DEBUG:
571		mib[2] = CTL_DEBUG_VALUE;
572		len = 3;
573		break;
574
575	case CTL_MACHDEP:
576#ifdef CPU_CONSDEV
577		if (mib[1] == CPU_CONSDEV)
578			special |= CHRDEV;
579#endif
580#ifdef CPU_BLK2CHR
581		if (mib[1] == CPU_BLK2CHR) {
582			if (bufp == NULL)
583				return;
584			mib[2] = makedev(atoi(bufp),0);
585			bufp = NULL;
586			len = 3;
587			special |= CHRDEV;
588			break;
589		}
590#endif
591#ifdef CPU_CHR2BLK
592		if (mib[1] == CPU_CHR2BLK) {
593			if (bufp == NULL)
594				return;
595			mib[2] = makedev(atoi(bufp),0);
596			bufp = NULL;
597			len = 3;
598			special |= BLKDEV;
599			break;
600		}
601#endif
602#ifdef CPU_BIOS
603		if (mib[1] == CPU_BIOS) {
604			len = sysctl_bios(string, &bufp, mib, flags, &type);
605			if (len < 0)
606				return;
607			if (mib[2] == BIOS_DEV)
608				special |= BIOSDEV;
609			if (mib[2] == BIOS_DISKINFO)
610				special |= BIOSGEO;
611			break;
612		}
613#endif
614#ifdef CPU_CHIPSET
615		if (mib[1] == CPU_CHIPSET) {
616			len = sysctl_chipset(string, &bufp, mib, flags, &type);
617			if (len < 0)
618				return;
619			break;
620		}
621#endif
622#ifdef CPU_ZTSSCALE
623		if (mib[1] == CPU_ZTSSCALE) {
624			special |= ZTSSCALE;
625			if (newsize > 0) {
626				const char *errstr = 0;
627
628				/* Unspecified values default to 0. */
629				bzero(&tsbuf, sizeof tsbuf);
630				newval = (void *)strtok(newval, ",");
631				if (newval != NULL) {
632					tsbuf.ts_minx = (int)strtonum(newval,
633					    0, 32768, &errstr);
634					newval = (void *)strtok(NULL, ",");
635				}
636				if (!errstr && newval != NULL) {
637					tsbuf.ts_maxx = (int)strtonum(newval,
638					    0, 32768, &errstr);
639					newval = (void *)strtok(NULL, ",");
640				}
641				if (!errstr && newval != NULL) {
642					tsbuf.ts_miny = (int)strtonum(newval,
643					    0, 32768, &errstr);
644					newval = (void *)strtok(NULL, ",");
645				}
646				if (!errstr && newval != NULL) {
647					tsbuf.ts_maxy = (int)strtonum(newval,
648					    0, 32768, &errstr);
649					newval = (void *)strtok(NULL, ",");
650				}
651				if (errstr)
652					errx(1, "calibration value is %s",
653					    errstr);
654				if (newval != NULL)
655					errx(1, "too many calibration values");
656				newval = &tsbuf;
657				newsize = sizeof(tsbuf);
658			}
659			break;
660		}
661#endif
662		break;
663
664	case CTL_FS:
665		len = sysctl_fs(string, &bufp, mib, flags, &type);
666		if (len >= 0)
667			break;
668		return;
669
670	case CTL_VFS:
671		if (mib[1])
672			len = sysctl_vfs(string, &bufp, mib, flags, &type);
673		else
674			len = sysctl_vfsgen(string, &bufp, mib, flags, &type);
675		if (len >= 0) {
676			if (type == CTLTYPE_STRUCT) {
677				if (flags)
678					warnx("use nfsstat to view %s information",
679					    MOUNT_NFS);
680				return;
681			} else
682				break;
683		}
684		return;
685
686	case CTL_USER:
687	case CTL_DDB:
688		break;
689
690	default:
691		warnx("illegal top level value: %d", mib[0]);
692		return;
693
694	}
695	if (bufp) {
696		warnx("name %s in %s is unknown", bufp, string);
697		return;
698	}
699	if (newsize > 0) {
700		switch (type) {
701		case CTLTYPE_INT:
702			errno = 0;
703			if (special & UNSIGNED)
704				intval = strtoul(newval, &cp, 10);
705			else
706				intval = strtol(newval, &cp, 10);
707			if (*cp != '\0') {
708				warnx("%s: illegal value: %s", string,
709				    (char *)newval);
710				return;
711			}
712			if (errno == ERANGE) {
713				warnx("%s: value %s out of range", string,
714				    (char *)newval);
715				return;
716			}
717			newval = &intval;
718			newsize = sizeof(intval);
719			break;
720
721		case CTLTYPE_QUAD:
722			/* XXX - assumes sizeof(long long) == sizeof(quad_t) */
723			(void)sscanf(newval, "%lld", (long long *)&quadval);
724			newval = &quadval;
725			newsize = sizeof(quadval);
726			break;
727		}
728	}
729	size = BUFSIZ;
730	if (sysctl(mib, len, buf, &size, newval, newsize) == -1) {
731		if (flags == 0)
732			return;
733		switch (errno) {
734		case EOPNOTSUPP:
735			warnx("%s: value is not available", string);
736			return;
737		case ENOTDIR:
738			warnx("%s: specification is incomplete", string);
739			return;
740		case ENOMEM:
741			warnx("%s: type is unknown to this program", string);
742			return;
743		case ENXIO:
744			if (special & BIOSGEO)
745				return;
746		default:
747			warn("%s", string);
748			return;
749		}
750	}
751	if (special & KMEMBUCKETS) {
752		struct kmembuckets *kb = (struct kmembuckets *)buf;
753		if (!nflag)
754			(void)printf("%s%s", string, equ);
755		printf("(");
756		printf("calls = %llu ", (long long)kb->kb_calls);
757		printf("total_allocated = %llu ", (long long)kb->kb_total);
758		printf("total_free = %lld ", (long long)kb->kb_totalfree);
759		printf("elements = %lld ", (long long)kb->kb_elmpercl);
760		printf("high watermark = %lld ", (long long)kb->kb_highwat);
761		printf("could_free = %lld", (long long)kb->kb_couldfree);
762		printf(")\n");
763		return;
764	}
765	if (special & KMEMSTATS) {
766		struct kmemstats *km = (struct kmemstats *)buf;
767		int j, first = 1;
768
769		if (!nflag)
770			(void)printf("%s%s", string, equ);
771		(void)printf("(inuse = %ld, calls = %ld, memuse = %ldK, "
772		    "limblocks = %d, mapblocks = %d, maxused = %ldK, "
773		    "limit = %ldK, spare = %ld, sizes = (",
774		    km->ks_inuse, km->ks_calls,
775		    (km->ks_memuse + 1023) / 1024, km->ks_limblocks,
776		    km->ks_mapblocks, (km->ks_maxused + 1023) / 1024,
777		    (km->ks_limit + 1023) / 1024, km->ks_spare);
778		for (j = 1 << MINBUCKET; j < 1 << (MINBUCKET + 16); j <<= 1) {
779			if ((km->ks_size & j ) == 0)
780				continue;
781			if (first)
782				(void)printf("%d", j);
783			else
784				(void)printf(",%d", j);
785			first = 0;
786		}
787		if (first)
788			(void)printf("none");
789		(void)printf("))\n");
790		return;
791	}
792	if (special & CLOCK) {
793		struct clockinfo *clkp = (struct clockinfo *)buf;
794
795		if (!nflag)
796			(void)printf("%s%s", string, equ);
797		(void)printf(
798		    "tick = %d, tickadj = %d, hz = %d, profhz = %d, stathz = %d\n",
799		    clkp->tick, clkp->tickadj, clkp->hz, clkp->profhz, clkp->stathz);
800		return;
801	}
802	if (special & BOOTTIME) {
803		struct timeval *btp = (struct timeval *)buf;
804		time_t boottime;
805
806		if (!nflag) {
807			boottime = btp->tv_sec;
808			(void)printf("%s%s%s", string, equ, ctime(&boottime));
809		} else
810			(void)printf("%ld\n", btp->tv_sec);
811		return;
812	}
813	if (special & BLKDEV) {
814		dev_t dev = *(dev_t *)buf;
815
816		if (!nflag)
817			(void)printf("%s%s%s\n", string, equ,
818			    devname(dev, S_IFBLK));
819		else
820			(void)printf("0x%x\n", dev);
821		return;
822	}
823	if (special & CHRDEV) {
824		dev_t dev = *(dev_t *)buf;
825
826		if (!nflag)
827			(void)printf("%s%s%s\n", string, equ,
828			    devname(dev, S_IFCHR));
829		else
830			(void)printf("0x%x\n", dev);
831		return;
832	}
833#ifdef CPU_BIOS
834	if (special & BIOSGEO) {
835		bios_diskinfo_t *pdi = (bios_diskinfo_t *)buf;
836
837		if (!nflag)
838			(void)printf("%s%s", string, equ);
839		(void)printf("bootdev = 0x%x, "
840		    "cylinders = %u, heads = %u, sectors = %u\n",
841		    pdi->bsd_dev, pdi->bios_cylinders,
842		    pdi->bios_heads, pdi->bios_sectors);
843		return;
844	}
845	if (special & BIOSDEV) {
846		int dev = *(int*)buf;
847
848		if (!nflag)
849			(void)printf("%s%s", string, equ);
850		(void) printf("0x%02x\n", dev);
851		return;
852	}
853#endif
854	if (special & UNSIGNED) {
855		if (newsize == 0) {
856			if (!nflag)
857				(void)printf("%s%s", string, equ);
858			(void)printf("%u\n", *(u_int *)buf);
859		} else {
860			if (!qflag) {
861				if (!nflag)
862					(void)printf("%s: %u -> ", string,
863					    *(u_int *)buf);
864				(void)printf("%u\n", *(u_int *)newval);
865			}
866		}
867		return;
868	}
869	if (special & RNDSTATS) {
870		struct rndstats *rndstats = (struct rndstats *)buf;
871		int i;
872
873		if (!nflag)
874			(void)printf("%s%s", string, equ);
875		(void)printf(
876		"%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
877		    (unsigned long long)rndstats->rnd_total,
878		    (unsigned long long)rndstats->rnd_used,
879		    (unsigned long long)rndstats->rnd_reads,
880		    (unsigned long long)rndstats->arc4_reads,
881		    (unsigned long long)rndstats->arc4_nstirs,
882		    (unsigned long long)rndstats->arc4_stirs,
883		    (unsigned long long)rndstats->rnd_pad[0],
884		    (unsigned long long)rndstats->rnd_pad[1],
885		    (unsigned long long)rndstats->rnd_pad[2],
886		    (unsigned long long)rndstats->rnd_pad[3],
887		    (unsigned long long)rndstats->rnd_pad[4],
888		    (unsigned long long)rndstats->rnd_waits,
889		    (unsigned long long)rndstats->rnd_enqs,
890		    (unsigned long long)rndstats->rnd_deqs,
891		    (unsigned long long)rndstats->rnd_drops,
892		    (unsigned long long)rndstats->rnd_drople);
893		for (i = 0; i < sizeof(rndstats->rnd_ed)/sizeof(rndstats->rnd_ed[0]);
894		    i++)
895			(void)printf(" %llu", (unsigned long long)rndstats->rnd_ed[i]);
896		for (i = 0; i < sizeof(rndstats->rnd_sc)/sizeof(rndstats->rnd_sc[0]);
897		    i++)
898			(void)printf(" %llu", (unsigned long long)rndstats->rnd_sc[i]);
899		for (i = 0; i < sizeof(rndstats->rnd_sb)/sizeof(rndstats->rnd_sb[0]);
900		    i++)
901			(void)printf(" %llu", (unsigned long long)rndstats->rnd_sb[i]);
902		printf("\n");
903		return;
904	}
905	if (special & BADDYNAMIC) {
906		in_port_t port, lastport;
907		u_int32_t *baddynamic = (u_int32_t *)buf;
908
909		if (!qflag) {
910			if (!nflag)
911				(void)printf("%s%s", string,
912				    newsize ? ": " : equ);
913			lastport = 0;
914			for (port = IPPORT_RESERVED/2; port < IPPORT_RESERVED;
915			    port++)
916				if (DP_ISSET(baddynamic, port)) {
917					(void)printf("%s%hd",
918					    lastport ? "," : "", port);
919					lastport = port;
920				}
921			if (newsize != 0) {
922				if (!nflag)
923					fputs(" -> ", stdout);
924				baddynamic = (u_int32_t *)newval;
925				lastport = 0;
926				for (port = IPPORT_RESERVED/2;
927				    port < IPPORT_RESERVED; port++)
928					if (DP_ISSET(baddynamic, port)) {
929						(void)printf("%s%hd",
930						    lastport ? "," : "", port);
931						lastport = port;
932					}
933			}
934			(void)putchar('\n');
935		}
936		return;
937	}
938	if (special & LONGARRAY) {
939		long *la = (long *)buf;
940		if (!nflag)
941			printf("%s%s", string, equ);
942		while (lal--)
943			printf("%ld%s", *la++, lal? ",":"");
944		putchar('\n');
945		return;
946	}
947	if (special & SENSORS) {
948		struct sensor *s = (struct sensor *)buf;
949
950		if (size > 0 && (s->flags & SENSOR_FINVALID) == 0) {
951			if (!nflag)
952				printf("%s%s", string, equ);
953			printf("%s, %s, ", s->device, s->desc);
954			switch (s->type) {
955			case SENSOR_TEMP:
956				printf("temp, %.2f degC / %.2f degF",
957				    (s->value - 273150000) / 1000000.0,
958				    (s->value - 273150000) / 1000000.0 * 9 / 5 +
959				    32);
960				break;
961			case SENSOR_FANRPM:
962				printf("fanrpm, %lld RPM", s->value);
963				break;
964			case SENSOR_VOLTS_DC:
965				printf("volts_dc, %.2f V",
966				    s->value / 1000000.0);
967				break;
968			default:
969				printf("unknown");
970			}
971			printf("\n");
972		}
973		return;
974	}
975#ifdef CPU_ZTSSCALE
976	if (special & ZTSSCALE) {
977		struct ztsscale *tsp;
978
979		if (newsize == 0) {
980			if (!nflag)
981				(void)printf("%s%s", string, equ);
982			tsp = (struct ztsscale *)buf;
983			(void)printf("%d,%d,%d,%d\n", tsp->ts_minx,
984			    tsp->ts_maxx, tsp->ts_miny, tsp->ts_maxy);
985		} else {
986			if (!qflag) {
987				if (!nflag) {
988					tsp = (struct ztsscale *)buf;
989					(void)printf("%s: %d,%d,%d,%d -> ",
990					    string, tsp->ts_minx, tsp->ts_maxx,
991					    tsp->ts_miny, tsp->ts_maxy);
992				}
993				tsp = (struct ztsscale *)newval;
994				(void)printf("%d,%d,%d,%d\n", tsp->ts_minx,
995				    tsp->ts_maxx, tsp->ts_miny, tsp->ts_maxy);
996			}
997		}
998		return;
999	}
1000#endif
1001	switch (type) {
1002	case CTLTYPE_INT:
1003		if (newsize == 0) {
1004			if (!nflag)
1005				(void)printf("%s%s", string, equ);
1006			(void)printf("%d\n", *(int *)buf);
1007		} else {
1008			if (!qflag) {
1009				if (!nflag)
1010					(void)printf("%s: %d -> ", string,
1011					    *(int *)buf);
1012				(void)printf("%d\n", *(int *)newval);
1013			}
1014		}
1015		return;
1016
1017	case CTLTYPE_STRING:
1018		if (newval == NULL) {
1019			if (!nflag)
1020				(void)printf("%s%s", string, equ);
1021			(void)puts(buf);
1022		} else {
1023			if (!qflag) {
1024				if (!nflag)
1025					(void)printf("%s: %s -> ", string, buf);
1026				(void)puts((char *)newval);
1027			}
1028		}
1029		return;
1030
1031	case CTLTYPE_QUAD:
1032		if (newsize == 0) {
1033			long long tmp = *(quad_t *)buf;
1034
1035			if (!nflag)
1036				(void)printf("%s%s", string, equ);
1037			(void)printf("%lld\n", tmp);
1038		} else {
1039			long long tmp = *(quad_t *)buf;
1040
1041			if (!qflag) {
1042				if (!nflag)
1043					(void)printf("%s: %lld -> ",
1044					    string, tmp);
1045				tmp = *(quad_t *)newval;
1046				(void)printf("%qd\n", tmp);
1047			}
1048		}
1049		return;
1050
1051	case CTLTYPE_STRUCT:
1052		warnx("%s: unknown structure returned", string);
1053		return;
1054
1055	default:
1056	case CTLTYPE_NODE:
1057		warnx("%s: unknown type returned", string);
1058		return;
1059	}
1060}
1061
1062void
1063parse_baddynamic(int mib[], size_t len, char *string, void **newvalp,
1064    size_t *newsizep, int flags, int nflag)
1065{
1066	static u_int32_t newbaddynamic[DP_MAPSIZE];
1067	in_port_t port;
1068	size_t size;
1069	char action, *cp;
1070
1071	if (strchr((char *)*newvalp, '+') || strchr((char *)*newvalp, '-')) {
1072		size = sizeof(newbaddynamic);
1073		if (sysctl(mib, len, newbaddynamic, &size, 0, 0) == -1) {
1074			if (flags == 0)
1075				return;
1076			if (!nflag)
1077				(void)printf("%s: ", string);
1078			(void)puts("kernel does contain bad dynamic port tables");
1079			return;
1080		}
1081
1082		while (*newvalp && (cp = strsep((char **)newvalp, ", \t")) && *cp) {
1083			if (*cp != '+' && *cp != '-')
1084				errx(1, "cannot mix +/- with full list");
1085			action = *cp++;
1086			port = atoi(cp);
1087			if (port < IPPORT_RESERVED/2 || port >= IPPORT_RESERVED)
1088				errx(1, "invalid port, range is %d to %d",
1089				    IPPORT_RESERVED/2, IPPORT_RESERVED-1);
1090			if (action == '+')
1091				DP_SET(newbaddynamic, port);
1092			else
1093				DP_CLR(newbaddynamic, port);
1094		}
1095	} else {
1096		(void)memset((void *)newbaddynamic, 0, sizeof(newbaddynamic));
1097		while (*newvalp && (cp = strsep((char **)newvalp, ", \t")) && *cp) {
1098			port = atoi(cp);
1099			if (port < IPPORT_RESERVED/2 || port >= IPPORT_RESERVED)
1100				errx(1, "invalid port, range is %d to %d",
1101				    IPPORT_RESERVED/2, IPPORT_RESERVED-1);
1102			DP_SET(newbaddynamic, port);
1103		}
1104	}
1105
1106	*newvalp = (void *)newbaddynamic;
1107	*newsizep = sizeof(newbaddynamic);
1108}
1109
1110/*
1111 * Initialize the set of debugging names
1112 */
1113void
1114debuginit(void)
1115{
1116	int mib[3], loc, i;
1117	size_t size;
1118
1119	if (secondlevel[CTL_DEBUG].list != 0)
1120		return;
1121	secondlevel[CTL_DEBUG].list = debugname;
1122	mib[0] = CTL_DEBUG;
1123	mib[2] = CTL_DEBUG_NAME;
1124	for (loc = lastused, i = 0; i < CTL_DEBUG_MAXID; i++) {
1125		mib[1] = i;
1126		size = BUFSIZ - loc;
1127		if (sysctl(mib, 3, &names[loc], &size, NULL, 0) == -1)
1128			continue;
1129		debugname[i].ctl_name = &names[loc];
1130		debugname[i].ctl_type = CTLTYPE_INT;
1131		loc += size;
1132	}
1133	lastused = loc;
1134}
1135
1136struct ctlname vfsgennames[] = CTL_VFSGENCTL_NAMES;
1137struct ctlname ffsname[] = FFS_NAMES;
1138struct ctlname nfsname[] = FS_NFS_NAMES;
1139struct list *vfsvars;
1140int *vfs_typenums;
1141
1142/*
1143 * Initialize the set of filesystem names
1144 */
1145void
1146vfsinit(void)
1147{
1148	int mib[4], maxtypenum, cnt, loc, size;
1149	struct vfsconf vfc;
1150	size_t buflen;
1151
1152	if (secondlevel[CTL_VFS].list != 0)
1153		return;
1154	mib[0] = CTL_VFS;
1155	mib[1] = VFS_GENERIC;
1156	mib[2] = VFS_MAXTYPENUM;
1157	buflen = 4;
1158	if (sysctl(mib, 3, &maxtypenum, &buflen, (void *)0, (size_t)0) < 0)
1159		return;
1160	maxtypenum++;	/* + generic */
1161	if ((vfs_typenums = malloc(maxtypenum * sizeof(int))) == NULL)
1162		return;
1163	memset(vfs_typenums, 0, maxtypenum * sizeof(int));
1164	if ((vfsvars = malloc(maxtypenum * sizeof(*vfsvars))) == NULL) {
1165		free(vfs_typenums);
1166		return;
1167	}
1168	memset(vfsvars, 0, maxtypenum * sizeof(*vfsvars));
1169	if ((vfsname = malloc(maxtypenum * sizeof(*vfsname))) == NULL) {
1170		free(vfs_typenums);
1171		free(vfsvars);
1172		return;
1173	}
1174	memset(vfsname, 0, maxtypenum * sizeof(*vfsname));
1175	mib[2] = VFS_CONF;
1176	buflen = sizeof vfc;
1177	for (loc = lastused, cnt = 1; cnt < maxtypenum; cnt++) {
1178		mib[3] = cnt - 1;
1179		if (sysctl(mib, 4, &vfc, &buflen, (void *)0, (size_t)0) < 0) {
1180			if (errno == EOPNOTSUPP)
1181				continue;
1182			warn("vfsinit");
1183			free(vfsname);
1184			return;
1185		}
1186		if (!strcmp(vfc.vfc_name, MOUNT_FFS)) {
1187			vfsvars[cnt].list = ffsname;
1188			vfsvars[cnt].size = FFS_MAXID;
1189		}
1190		if (!strcmp(vfc.vfc_name, MOUNT_NFS)) {
1191			vfsvars[cnt].list = nfsname;
1192			vfsvars[cnt].size = NFS_MAXID;
1193		}
1194		vfs_typenums[cnt] = vfc.vfc_typenum;
1195		strlcat(&names[loc], vfc.vfc_name, sizeof names - loc);
1196		vfsname[cnt].ctl_name = &names[loc];
1197		vfsname[cnt].ctl_type = CTLTYPE_NODE;
1198		size = strlen(vfc.vfc_name) + 1;
1199		loc += size;
1200	}
1201	lastused = loc;
1202
1203	vfsname[0].ctl_name = "mounts";
1204	vfsname[0].ctl_type = CTLTYPE_NODE;
1205	vfsvars[0].list = vfsname + 1;
1206	vfsvars[0].size = maxtypenum - 1;
1207
1208	secondlevel[CTL_VFS].list = vfsname;
1209	secondlevel[CTL_VFS].size = maxtypenum;
1210	return;
1211}
1212
1213int
1214sysctl_vfsgen(char *string, char **bufpp, int mib[], int flags, int *typep)
1215{
1216	int indx;
1217	size_t size;
1218	struct vfsconf vfc;
1219
1220	if (*bufpp == NULL) {
1221		listall(string, vfsvars);
1222		return (-1);
1223	}
1224
1225	if ((indx = findname(string, "third", bufpp, vfsvars)) == -1)
1226		return (-1);
1227
1228	mib[1] = VFS_GENERIC;
1229	mib[2] = VFS_CONF;
1230	mib[3] = indx;
1231	size = sizeof vfc;
1232	if (sysctl(mib, 4, &vfc, &size, (void *)0, (size_t)0) < 0) {
1233		if (errno != EOPNOTSUPP)
1234			warn("vfs print");
1235		return -1;
1236	}
1237	if (flags == 0 && vfc.vfc_refcount == 0)
1238		return -1;
1239	if (!nflag)
1240		fprintf(stdout, "%s has %d mounted instance%s\n",
1241		    string, vfc.vfc_refcount,
1242		    vfc.vfc_refcount != 1 ? "s" : "");
1243	else
1244		fprintf(stdout, "%d\n", vfc.vfc_refcount);
1245
1246	return -1;
1247}
1248
1249int
1250sysctl_vfs(char *string, char **bufpp, int mib[], int flags, int *typep)
1251{
1252	struct list *lp = &vfsvars[mib[1]];
1253	int indx;
1254
1255	if (lp->list == NULL) {
1256		if (flags)
1257			warnx("No variables defined for file system %s", string);
1258		return (-1);
1259	}
1260	if (*bufpp == NULL) {
1261		listall(string, lp);
1262		return (-1);
1263	}
1264	if ((indx = findname(string, "third", bufpp, lp)) == -1)
1265		return (-1);
1266
1267	mib[1] = vfs_typenums[mib[1]];
1268	mib[2] = indx;
1269	*typep = lp->list[indx].ctl_type;
1270	return (3);
1271}
1272
1273struct ctlname posixname[] = CTL_FS_POSIX_NAMES;
1274struct list fslist = { posixname, FS_POSIX_MAXID };
1275
1276/*
1277 * handle file system requests
1278 */
1279int
1280sysctl_fs(char *string, char **bufpp, int mib[], int flags, int *typep)
1281{
1282	int indx;
1283
1284	if (*bufpp == NULL) {
1285		listall(string, &fslist);
1286		return (-1);
1287	}
1288	if ((indx = findname(string, "third", bufpp, &fslist)) == -1)
1289		return (-1);
1290	mib[2] = indx;
1291	*typep = fslist.list[indx].ctl_type;
1292	return (3);
1293}
1294
1295#ifdef CPU_BIOS
1296struct ctlname biosname[] = CTL_BIOS_NAMES;
1297struct list bioslist = { biosname, BIOS_MAXID };
1298
1299/*
1300 * handle BIOS requests
1301 */
1302int
1303sysctl_bios(char *string, char **bufpp, int mib[], int flags, int *typep)
1304{
1305	char *name;
1306	int indx;
1307
1308	if (*bufpp == NULL) {
1309		listall(string, &bioslist);
1310		return (-1);
1311	}
1312	if ((indx = findname(string, "third", bufpp, &bioslist)) == -1)
1313		return (-1);
1314	mib[2] = indx;
1315	if (indx == BIOS_DISKINFO) {
1316		if (*bufpp == NULL) {
1317			char name[BUFSIZ];
1318
1319			/* scan all the bios devices */
1320			for (indx = 0; indx < 256; indx++) {
1321				snprintf(name, sizeof(name), "%s.%u",
1322				    string, indx);
1323				parse(name, 1);
1324			}
1325			return (-1);
1326		}
1327		if ((name = strsep(bufpp, ".")) == NULL) {
1328			warnx("%s: incomplete specification", string);
1329			return (-1);
1330		}
1331		mib[3] = atoi(name);
1332		*typep = CTLTYPE_STRUCT;
1333		return (4);
1334	} else {
1335		*typep = bioslist.list[indx].ctl_type;
1336		return (3);
1337	}
1338}
1339#endif
1340
1341struct ctlname swpencname[] = CTL_SWPENC_NAMES;
1342struct list swpenclist = { swpencname, SWPENC_MAXID };
1343
1344/*
1345 * handle swap encrypt requests
1346 */
1347int
1348sysctl_swpenc(char *string, char **bufpp, int mib[], int flags, int *typep)
1349{
1350	int indx;
1351
1352	if (*bufpp == NULL) {
1353		listall(string, &swpenclist);
1354		return (-1);
1355	}
1356	if ((indx = findname(string, "third", bufpp, &swpenclist)) == -1)
1357		return (-1);
1358	mib[2] = indx;
1359	*typep = swpenclist.list[indx].ctl_type;
1360	return (3);
1361}
1362
1363struct ctlname inetname[] = CTL_IPPROTO_NAMES;
1364struct ctlname ipname[] = IPCTL_NAMES;
1365struct ctlname icmpname[] = ICMPCTL_NAMES;
1366struct ctlname ipipname[] = IPIPCTL_NAMES;
1367struct ctlname tcpname[] = TCPCTL_NAMES;
1368struct ctlname udpname[] = UDPCTL_NAMES;
1369struct ctlname espname[] = ESPCTL_NAMES;
1370struct ctlname ahname[] = AHCTL_NAMES;
1371struct ctlname etheripname[] = ETHERIPCTL_NAMES;
1372struct ctlname grename[] = GRECTL_NAMES;
1373struct ctlname mobileipname[] = MOBILEIPCTL_NAMES;
1374struct ctlname ipcompname[] = IPCOMPCTL_NAMES;
1375struct ctlname carpname[] = CARPCTL_NAMES;
1376struct ctlname bpfname[] = CTL_NET_BPF_NAMES;
1377struct ctlname ifqname[] = CTL_IFQ_NAMES;
1378struct list inetlist = { inetname, IPPROTO_MAXID };
1379struct list inetvars[] = {
1380	{ ipname, IPCTL_MAXID },	/* ip */
1381	{ icmpname, ICMPCTL_MAXID },	/* icmp */
1382	{ 0, 0 },			/* igmp */
1383	{ 0, 0 },			/* ggmp */
1384	{ ipipname, IPIPCTL_MAXID },	/* ipencap */
1385	{ 0, 0 },
1386	{ tcpname, TCPCTL_MAXID },	/* tcp */
1387	{ 0, 0 },
1388	{ 0, 0 },			/* egp */
1389	{ 0, 0 },
1390	{ 0, 0 },
1391	{ 0, 0 },
1392	{ 0, 0 },			/* pup */
1393	{ 0, 0 },
1394	{ 0, 0 },
1395	{ 0, 0 },
1396	{ 0, 0 },
1397	{ udpname, UDPCTL_MAXID },	/* udp */
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	{ grename, GRECTL_MAXID }, /* GRE */
1428	{ 0, 0 },
1429	{ 0, 0 },
1430	{ espname, ESPCTL_MAXID },	/* esp */
1431	{ ahname, AHCTL_MAXID },	/* ah */
1432	{ 0, 0 },
1433	{ 0, 0 },
1434	{ 0, 0 },
1435	{ mobileipname, MOBILEIPCTL_MAXID }, /* mobileip */
1436	{ 0, 0 },
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	{ etheripname, ETHERIPCTL_MAXID },
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	{ ipcompname, IPCOMPCTL_MAXID },
1489	{ 0, 0 },
1490	{ 0, 0 },
1491	{ 0, 0 },
1492	{ carpname, CARPCTL_MAXID },
1493};
1494struct list bpflist = { bpfname, NET_BPF_MAXID };
1495struct list ifqlist = { ifqname, IFQCTL_MAXID };
1496
1497struct list kernmalloclist = { kernmallocname, KERN_MALLOC_MAXID };
1498struct list forkstatlist = { forkstatname, KERN_FORKSTAT_MAXID };
1499struct list nchstatslist = { nchstatsname, KERN_NCHSTATS_MAXID };
1500struct list ttylist = { ttysname, KERN_TTY_MAXID };
1501struct list semlist = { semname, KERN_SEMINFO_MAXID };
1502struct list shmlist = { shmname, KERN_SHMINFO_MAXID };
1503struct list watchdoglist = { watchdogname, KERN_WATCHDOG_MAXID };
1504struct list tclist = { tcname, KERN_TIMECOUNTER_MAXID };
1505
1506/*
1507 * handle vfs namei cache statistics
1508 */
1509int
1510sysctl_nchstats(char *string, char **bufpp, int mib[], int flags, int *typep)
1511{
1512	static struct nchstats nch;
1513	int indx;
1514	size_t size;
1515	static int keepvalue = 0;
1516
1517	if (*bufpp == NULL) {
1518		bzero(&nch, sizeof(struct nchstats));
1519		listall(string, &nchstatslist);
1520		return (-1);
1521	}
1522	if ((indx = findname(string, "third", bufpp, &nchstatslist)) == -1)
1523		return (-1);
1524	mib[2] = indx;
1525	if (*bufpp != NULL) {
1526		warnx("fourth level name in %s is invalid", string);
1527		return (-1);
1528	}
1529	if (keepvalue == 0) {
1530		size = sizeof(struct nchstats);
1531		if (sysctl(mib, 2, &nch, &size, NULL, 0) < 0)
1532			return (-1);
1533		keepvalue = 1;
1534	}
1535	if (!nflag)
1536		(void)printf("%s%s", string, equ);
1537	switch (indx) {
1538	case KERN_NCHSTATS_GOODHITS:
1539		(void)printf("%ld\n", nch.ncs_goodhits);
1540		break;
1541	case KERN_NCHSTATS_NEGHITS:
1542		(void)printf("%ld\n", nch.ncs_neghits);
1543		break;
1544	case KERN_NCHSTATS_BADHITS:
1545		(void)printf("%ld\n", nch.ncs_badhits);
1546		break;
1547	case KERN_NCHSTATS_FALSEHITS:
1548		(void)printf("%ld\n", nch.ncs_falsehits);
1549		break;
1550	case KERN_NCHSTATS_MISS:
1551		(void)printf("%ld\n", nch.ncs_miss);
1552		break;
1553	case KERN_NCHSTATS_LONG:
1554		(void)printf("%ld\n", nch.ncs_long);
1555		break;
1556	case KERN_NCHSTATS_PASS2:
1557		(void)printf("%ld\n", nch.ncs_pass2);
1558		break;
1559	case KERN_NCHSTATS_2PASSES:
1560		(void)printf("%ld\n", nch.ncs_2passes);
1561		break;
1562	}
1563	return (-1);
1564}
1565
1566/*
1567 * handle tty statistics
1568 */
1569int
1570sysctl_tty(char *string, char **bufpp, int mib[], int flags, int *typep)
1571{
1572	int indx;
1573
1574	if (*bufpp == NULL) {
1575		listall(string, &ttylist);
1576		return (-1);
1577	}
1578	if ((indx = findname(string, "third", bufpp, &ttylist)) == -1)
1579		return (-1);
1580	mib[2] = indx;
1581
1582	if ((*typep = ttylist.list[indx].ctl_type) == CTLTYPE_STRUCT) {
1583		if (flags)
1584			warnx("use pstat -t to view %s information",
1585			    string);
1586		return (-1);
1587	}
1588	return (3);
1589}
1590
1591/*
1592 * handle fork statistics
1593 */
1594int
1595sysctl_forkstat(char *string, char **bufpp, int mib[], int flags, int *typep)
1596{
1597	static struct forkstat fks;
1598	static int keepvalue = 0;
1599	int indx;
1600	size_t size;
1601
1602	if (*bufpp == NULL) {
1603		bzero(&fks, sizeof(struct forkstat));
1604		listall(string, &forkstatlist);
1605		return (-1);
1606	}
1607	if ((indx = findname(string, "third", bufpp, &forkstatlist)) == -1)
1608		return (-1);
1609	if (*bufpp != NULL) {
1610		warnx("fourth level name in %s is invalid", string);
1611		return (-1);
1612	}
1613	if (keepvalue == 0) {
1614		size = sizeof(struct forkstat);
1615		if (sysctl(mib, 2, &fks, &size, NULL, 0) < 0)
1616			return (-1);
1617		keepvalue = 1;
1618	}
1619	if (!nflag)
1620		(void)printf("%s%s", string, equ);
1621	switch (indx)	{
1622	case KERN_FORKSTAT_FORK:
1623		(void)printf("%d\n", fks.cntfork);
1624		break;
1625	case KERN_FORKSTAT_VFORK:
1626		(void)printf("%d\n", fks.cntvfork);
1627		break;
1628	case KERN_FORKSTAT_RFORK:
1629		(void)printf("%d\n", fks.cntrfork);
1630		break;
1631	case KERN_FORKSTAT_KTHREAD:
1632		(void)printf("%d\n", fks.cntkthread);
1633		break;
1634	case KERN_FORKSTAT_SIZFORK:
1635		(void)printf("%d\n", fks.sizfork);
1636		break;
1637	case KERN_FORKSTAT_SIZVFORK:
1638		(void)printf("%d\n", fks.sizvfork);
1639		break;
1640	case KERN_FORKSTAT_SIZRFORK:
1641		(void)printf("%d\n", fks.sizrfork);
1642		break;
1643	case KERN_FORKSTAT_SIZKTHREAD:
1644		(void)printf("%d\n", fks.sizkthread);
1645		break;
1646	}
1647	return (-1);
1648}
1649
1650/*
1651 * handle malloc statistics
1652 */
1653int
1654sysctl_malloc(char *string, char **bufpp, int mib[], int flags, int *typep)
1655{
1656	int indx, stor, i;
1657	char *name, bufp[BUFSIZ], *buf, *ptr;
1658	struct list lp;
1659	size_t size;
1660
1661	if (*bufpp == NULL) {
1662		listall(string, &kernmalloclist);
1663		return (-1);
1664	}
1665	if ((indx = findname(string, "third", bufpp, &kernmalloclist)) == -1)
1666		return (-1);
1667	mib[2] = indx;
1668	if (mib[2] == KERN_MALLOC_BUCKET) {
1669		if ((name = strsep(bufpp, ".")) == NULL) {
1670			size = BUFSIZ;
1671			stor = mib[2];
1672			mib[2] = KERN_MALLOC_BUCKETS;
1673			buf = bufp;
1674			if (sysctl(mib, 3, buf, &size, NULL, 0) < 0)
1675				return (-1);
1676			mib[2] = stor;
1677			for (stor = 0, i = 0; i < size; i++)
1678				if (buf[i] == ',')
1679					stor++;
1680			lp.list = calloc(stor + 2, sizeof(struct ctlname));
1681			if (lp.list == NULL)
1682				return (-1);
1683			lp.size = stor + 2;
1684			for (i = 1;
1685			    (lp.list[i].ctl_name = strsep(&buf, ",")) != NULL;
1686			    i++) {
1687				lp.list[i].ctl_type = CTLTYPE_STRUCT;
1688			}
1689			lp.list[i].ctl_name = buf;
1690			lp.list[i].ctl_type = CTLTYPE_STRUCT;
1691			listall(string, &lp);
1692			free(lp.list);
1693			return (-1);
1694		}
1695		mib[3] = atoi(name);
1696		return (4);
1697	} else if (mib[2] == KERN_MALLOC_BUCKETS) {
1698		*typep = CTLTYPE_STRING;
1699		return (3);
1700	} else if (mib[2] == KERN_MALLOC_KMEMSTATS) {
1701		size = BUFSIZ;
1702		stor = mib[2];
1703		mib[2] = KERN_MALLOC_KMEMNAMES;
1704		buf = bufp;
1705		if (sysctl(mib, 3, buf, &size, NULL, 0) < 0)
1706			return (-1);
1707		mib[2] = stor;
1708		if ((name = strsep(bufpp, ".")) == NULL) {
1709			for (stor = 0, i = 0; i < size; i++)
1710				if (buf[i] == ',')
1711					stor++;
1712			lp.list = calloc(stor + 2, sizeof(struct ctlname));
1713			if (lp.list == NULL)
1714				return (-1);
1715			lp.size = stor + 2;
1716			for (i = 1;
1717			    (lp.list[i].ctl_name = strsep(&buf, ",")) != NULL; i++) {
1718				if (lp.list[i].ctl_name[0] == '\0') {
1719					i--;
1720					continue;
1721				}
1722				lp.list[i].ctl_type = CTLTYPE_STRUCT;
1723			}
1724			lp.list[i].ctl_name = buf;
1725			lp.list[i].ctl_type = CTLTYPE_STRUCT;
1726			listall(string, &lp);
1727			free(lp.list);
1728			return (-1);
1729		}
1730		ptr = strstr(buf, name);
1731 tryagain:
1732		if (ptr == NULL) {
1733			warnx("fourth level name %s in %s is invalid", name,
1734			    string);
1735			return (-1);
1736		}
1737		if ((*(ptr + strlen(name)) != ',') &&
1738		    (*(ptr + strlen(name)) != '\0')) {
1739			ptr = strstr(ptr + 1, name); /* retry */
1740			goto tryagain;
1741		}
1742		if ((ptr != buf) && (*(ptr - 1) != ',')) {
1743			ptr = strstr(ptr + 1, name); /* retry */
1744			goto tryagain;
1745		}
1746		for (i = 0, stor = 0; buf + i < ptr; i++)
1747			if (buf[i] == ',')
1748				stor++;
1749		mib[3] = stor;
1750		return (4);
1751	} else if (mib[2] == KERN_MALLOC_KMEMNAMES) {
1752		*typep = CTLTYPE_STRING;
1753		return (3);
1754	}
1755	return (-1);
1756}
1757
1758#ifdef CPU_CHIPSET
1759/*
1760 * handle machdep.chipset requests
1761 */
1762struct ctlname chipsetname[] = CTL_CHIPSET_NAMES;
1763struct list chipsetlist = { chipsetname, CPU_CHIPSET_MAXID };
1764
1765int
1766sysctl_chipset(char *string, char **bufpp, int mib[], int flags, int *typep)
1767{
1768	int indx, bwx;
1769	static void *q;
1770	size_t len;
1771	char *p;
1772
1773	if (*bufpp == NULL) {
1774		listall(string, &chipsetlist);
1775		return (-1);
1776	}
1777	if ((indx = findname(string, "third", bufpp, &chipsetlist)) == -1)
1778		return (-1);
1779	mib[2] = indx;
1780	if (!nflag)
1781		printf("%s%s", string, equ);
1782	switch(mib[2]) {
1783	case CPU_CHIPSET_MEM:
1784	case CPU_CHIPSET_DENSE:
1785	case CPU_CHIPSET_PORTS:
1786	case CPU_CHIPSET_HAE_MASK:
1787		len = sizeof(void *);
1788		if (sysctl(mib, 3, &q, &len, NULL, 0) < 0)
1789			return (-1);
1790		printf("%p\n", q);
1791		break;
1792	case CPU_CHIPSET_BWX:
1793		len = sizeof(int);
1794		if (sysctl(mib, 3, &bwx, &len, NULL, 0) < 0)
1795			return (-1);
1796		printf("%d\n", bwx);
1797		break;
1798	case CPU_CHIPSET_TYPE:
1799		if (sysctl(mib, 3, NULL, &len, NULL, 0) < 0)
1800			return (-1);
1801		p = malloc(len + 1);
1802		if (p == NULL)
1803			return (-1);
1804		if (sysctl(mib, 3, p, &len, NULL, 0) < 0)
1805			return (-1);
1806		p[len] = '\0';
1807		printf("%s\n", p);
1808		break;
1809	}
1810	return (-1);
1811}
1812#endif
1813/*
1814 * handle internet requests
1815 */
1816int
1817sysctl_inet(char *string, char **bufpp, int mib[], int flags, int *typep)
1818{
1819	struct list *lp;
1820	int indx;
1821
1822	if (*bufpp == NULL) {
1823		listall(string, &inetlist);
1824		return (-1);
1825	}
1826	if ((indx = findname(string, "third", bufpp, &inetlist)) == -1)
1827		return (-1);
1828	mib[2] = indx;
1829	if (indx < IPPROTO_MAXID && inetvars[indx].list != NULL)
1830		lp = &inetvars[indx];
1831	else if (!flags)
1832		return (-1);
1833	else {
1834		warnx("%s: no variables defined for this protocol", string);
1835		return (-1);
1836	}
1837	if (*bufpp == NULL) {
1838		listall(string, lp);
1839		return (-1);
1840	}
1841	if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
1842		return (-1);
1843	mib[3] = indx;
1844	*typep = lp->list[indx].ctl_type;
1845	if (*typep == CTLTYPE_NODE) {
1846		int tindx;
1847
1848		if (*bufpp == 0) {
1849			listall(string, &ifqlist);
1850			return(-1);
1851		}
1852		lp = &ifqlist;
1853		if ((tindx = findname(string, "fifth", bufpp, lp)) == -1)
1854			return (-1);
1855		mib[4] = tindx;
1856		*typep = lp->list[tindx].ctl_type;
1857		return(5);
1858	}
1859	return (4);
1860}
1861
1862#ifdef INET6
1863struct ctlname inet6name[] = CTL_IPV6PROTO_NAMES;
1864struct ctlname ip6name[] = IPV6CTL_NAMES;
1865struct ctlname icmp6name[] = ICMPV6CTL_NAMES;
1866struct ctlname pim6name[] = PIM6CTL_NAMES;
1867struct list inet6list = { inet6name, IPV6PROTO_MAXID };
1868struct list inet6vars[] = {
1869/*0*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1870	{ 0, 0 },
1871	{ 0, 0 },
1872	{ 0, 0 },
1873	{ 0, 0 },
1874	{ 0, 0 },
1875/*10*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1876	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1877/*20*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1878	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1879/*30*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1880	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1881/*40*/	{ 0, 0 },
1882	{ ip6name, IPV6CTL_MAXID },	/* ipv6 */
1883	{ 0, 0 },
1884	{ 0, 0 },
1885	{ 0, 0 },
1886	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1887/*50*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1888	{ 0, 0 },
1889	{ 0, 0 },
1890	{ 0, 0 },
1891	{ icmp6name, ICMPV6CTL_MAXID },	/* icmp6 */
1892	{ 0, 0 },
1893/*60*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1894	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1895/*70*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1896	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1897/*80*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1898	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1899/*90*/	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1900	{ 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 }, { 0, 0 },
1901/*100*/	{ 0, 0 },
1902	{ 0, 0 },
1903	{ 0, 0 },
1904	{ pim6name, PIM6CTL_MAXID },	/* pim6 */
1905};
1906
1907/*
1908 * handle internet6 requests
1909 */
1910int
1911sysctl_inet6(char *string, char **bufpp, int mib[], int flags, int *typep)
1912{
1913	struct list *lp;
1914	int indx;
1915
1916	if (*bufpp == NULL) {
1917		listall(string, &inet6list);
1918		return (-1);
1919	}
1920	if ((indx = findname(string, "third", bufpp, &inet6list)) == -1)
1921		return (-1);
1922	mib[2] = indx;
1923	if (indx < IPV6PROTO_MAXID && inet6vars[indx].list != NULL)
1924		lp = &inet6vars[indx];
1925	else if (!flags)
1926		return (-1);
1927	else {
1928		warnx("%s: no variables defined for this protocol", string);
1929		return (-1);
1930	}
1931	if (*bufpp == NULL) {
1932		listall(string, lp);
1933		return (-1);
1934	}
1935	if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
1936		return (-1);
1937	mib[3] = indx;
1938	*typep = lp->list[indx].ctl_type;
1939	return (4);
1940}
1941#endif
1942
1943struct ctlname ipxname[] = CTL_IPXPROTO_NAMES;
1944struct ctlname ipxpname[] = IPXCTL_NAMES;
1945struct ctlname spxpname[] = SPXCTL_NAMES;
1946struct list ipxlist = { ipxname, IPXCTL_MAXID };
1947struct list ipxvars[] = {
1948	{ ipxpname, IPXCTL_MAXID },	/* ipx */
1949	{ 0, 0 },
1950	{ 0, 0 },
1951	{ 0, 0 },
1952	{ 0, 0 },
1953	{ spxpname, SPXCTL_MAXID },
1954};
1955
1956/*
1957 * Handle internet requests
1958 */
1959int
1960sysctl_ipx(char *string, char **bufpp, int mib[], int flags, int *typep)
1961{
1962	struct list *lp;
1963	int indx;
1964
1965	if (*bufpp == NULL) {
1966		listall(string, &ipxlist);
1967		return (-1);
1968	}
1969	if ((indx = findname(string, "third", bufpp, &ipxlist)) == -1)
1970		return (-1);
1971	mib[2] = indx;
1972	if (indx <= IPXPROTO_SPX && ipxvars[indx].list != NULL)
1973		lp = &ipxvars[indx];
1974	else if (!flags)
1975		return (-1);
1976	else {
1977		warnx("%s: no variables defined for this protocol", string);
1978		return (-1);
1979	}
1980	if (*bufpp == NULL) {
1981		listall(string, lp);
1982		return (-1);
1983	}
1984	if ((indx = findname(string, "fourth", bufpp, lp)) == -1)
1985		return (-1);
1986	mib[3] = indx;
1987	*typep = lp->list[indx].ctl_type;
1988	return (4);
1989}
1990
1991/* handle bpf requests */
1992int
1993sysctl_bpf(char *string, char **bufpp, int mib[], int flags, int *typep)
1994{
1995	int indx;
1996
1997	if (*bufpp == NULL) {
1998		listall(string, &bpflist);
1999		return (-1);
2000	}
2001	if ((indx = findname(string, "third", bufpp, &bpflist)) == -1)
2002		return (-1);
2003	mib[2] = indx;
2004	*typep = CTLTYPE_INT;
2005	return (3);
2006}
2007
2008/*
2009 * Handle SysV semaphore info requests
2010 */
2011int
2012sysctl_seminfo(string, bufpp, mib, flags, typep)
2013	char *string;
2014	char **bufpp;
2015	int mib[];
2016	int flags;
2017	int *typep;
2018{
2019	int indx;
2020
2021	if (*bufpp == NULL) {
2022		listall(string, &semlist);
2023		return (-1);
2024	}
2025	if ((indx = findname(string, "third", bufpp, &semlist)) == -1)
2026		return (-1);
2027	mib[2] = indx;
2028	*typep = CTLTYPE_INT;
2029	return (3);
2030}
2031
2032/*
2033 * Handle SysV shared memory info requests
2034 */
2035int
2036sysctl_shminfo(string, bufpp, mib, flags, typep)
2037	char *string;
2038	char **bufpp;
2039	int mib[];
2040	int flags;
2041	int *typep;
2042{
2043	int indx;
2044
2045	if (*bufpp == NULL) {
2046		listall(string, &shmlist);
2047		return (-1);
2048	}
2049	if ((indx = findname(string, "third", bufpp, &shmlist)) == -1)
2050		return (-1);
2051	mib[2] = indx;
2052	*typep = CTLTYPE_INT;
2053	return (3);
2054}
2055
2056/*
2057 * Handle watchdog support
2058 */
2059int
2060sysctl_watchdog(char *string, char **bufpp, int mib[], int flags,
2061    int *typep)
2062{
2063	int indx;
2064
2065	if (*bufpp == NULL) {
2066		listall(string, &watchdoglist);
2067		return (-1);
2068	}
2069	if ((indx = findname(string, "third", bufpp, &watchdoglist)) == -1)
2070		return (-1);
2071	mib[2] = indx;
2072	*typep = watchdoglist.list[indx].ctl_type;
2073	return (3);
2074}
2075
2076/*
2077 * Handle timecounter support
2078 */
2079int
2080sysctl_tc(char *string, char **bufpp, int mib[], int flags,
2081    int *typep)
2082{
2083	int indx;
2084
2085	if (*bufpp == NULL) {
2086		listall(string, &tclist);
2087		return (-1);
2088	}
2089	if ((indx = findname(string, "third", bufpp, &tclist)) == -1)
2090		return (-1);
2091	mib[2] = indx;
2092	*typep = tclist.list[indx].ctl_type;
2093	return (3);
2094}
2095
2096/*
2097 * Handle hardware monitoring sensors support
2098 */
2099int
2100sysctl_sensors(char *string, char **bufpp, int mib[], int flags, int *typep)
2101{
2102	char *name;
2103	int indx;
2104
2105	if (*bufpp == NULL) {
2106		char buf[BUFSIZ];
2107
2108		/* scan all sensors */
2109		for (indx = 0; indx < 256; indx++) {
2110			snprintf(buf, sizeof(buf), "%s.%u", string, indx);
2111			parse(buf, 0);
2112		}
2113		return (-1);
2114	}
2115	if ((name = strsep(bufpp, ".")) == NULL) {
2116		warnx("%s: incomplete specification", string);
2117		return (-1);
2118	}
2119	mib[2] = atoi(name);
2120	*typep = CTLTYPE_STRUCT;
2121	return (3);
2122}
2123
2124struct emulname {
2125	char *name;
2126	int index;
2127} *emul_names;
2128int	emul_num, nemuls;
2129int	emul_init(void);
2130
2131int
2132sysctl_emul(char *string, char *newval, int flags)
2133{
2134	int mib[4], enabled, i, old, print, found = 0;
2135	char *head, *target;
2136	size_t len;
2137
2138	if (emul_init() == -1) {
2139		warnx("emul_init: out of memory");
2140		return (1);
2141	}
2142
2143	mib[0] = CTL_KERN;
2144	mib[1] = KERN_EMUL;
2145	mib[3] = KERN_EMUL_ENABLED;
2146	head = "kern.emul.";
2147
2148	if (aflag || strcmp(string, "kern.emul") == 0) {
2149		if (newval) {
2150			warnx("%s: specification is incomplete", string);
2151			return (1);
2152		}
2153		if (nflag)
2154			printf("%d\n", nemuls);
2155		else
2156			printf("%snemuls%s%d\n", head, equ, nemuls);
2157		for (i = 0; i < emul_num; i++) {
2158			if (emul_names[i].name == NULL)
2159				break;
2160			if (i > 0 && strcmp(emul_names[i].name,
2161			    emul_names[i-1].name) == 0)
2162				continue;
2163			mib[2] = emul_names[i].index;
2164			len = sizeof(int);
2165			if (sysctl(mib, 4, &enabled, &len, NULL, 0) == -1) {
2166				warn("%s", string);
2167				continue;
2168			}
2169			if (nflag)
2170				printf("%d\n", enabled);
2171			else
2172				printf("%s%s%s%d\n", head, emul_names[i].name,
2173				    equ, enabled);
2174		}
2175		return (0);
2176	}
2177	/* User specified a third level name */
2178	target = strrchr(string, '.');
2179	target++;
2180	if (strcmp(target, "nemuls") == 0) {
2181		if (newval) {
2182			warnx("Operation not permitted");
2183			return (1);
2184		}
2185		if (nflag)
2186			printf("%d\n", nemuls);
2187		else
2188			printf("%snemuls = %d\n", head, nemuls);
2189		return (0);
2190	}
2191	print = 1;
2192	for (i = 0; i < emul_num; i++) {
2193		if (!emul_names[i].name || (strcmp(target, emul_names[i].name)))
2194			continue;
2195		found = 1;
2196		mib[2] = emul_names[i].index;
2197		len = sizeof(int);
2198		if (newval) {
2199			enabled = atoi(newval);
2200			if (sysctl(mib, 4, &old, &len, &enabled, len) == -1) {
2201				warn("%s", string);
2202				print = 0;
2203				continue;
2204			}
2205			if (print) {
2206				if (nflag)
2207					printf("%d\n", enabled);
2208				else
2209					printf("%s%s: %d -> %d\n", head,
2210					    target, old, enabled);
2211			}
2212		} else {
2213			if (sysctl(mib, 4, &enabled, &len, NULL, 0) == -1) {
2214				warn("%s", string);
2215				continue;
2216			}
2217			if (print) {
2218				if (nflag)
2219					printf("%d\n", enabled);
2220				else
2221					printf("%s%s = %d\n", head, target,
2222					    enabled);
2223			}
2224		}
2225		print = 0;
2226	}
2227	if (!found)
2228		warnx("third level name %s in kern.emul is invalid",
2229		    string);
2230	return (0);
2231
2232
2233}
2234
2235int
2236emulcmp(const void *m, const void *n)
2237{
2238	const struct emulname *a = m, *b = n;
2239
2240	if (!a || !a->name)
2241		return 1;
2242	if (!b || !b->name)
2243		return -1;
2244	return (strcmp(a->name, b->name));
2245}
2246
2247int
2248emul_init(void)
2249{
2250	static int done;
2251	char string[16];
2252	int mib[4], i;
2253	size_t len;
2254
2255	if (done)
2256		return (0);
2257	done = 1;
2258
2259	mib[0] = CTL_KERN;
2260	mib[1] = KERN_EMUL;
2261	mib[2] = KERN_EMUL_NUM;
2262	len = sizeof(int);
2263	if (sysctl(mib, 3, &emul_num, &len, NULL, 0) == -1)
2264		return (-1);
2265
2266	emul_names = calloc(emul_num, sizeof(*emul_names));
2267	if (emul_names == NULL)
2268		return (-1);
2269
2270	nemuls = emul_num;
2271	for (i = 0; i < emul_num; i++) {
2272		emul_names[i].index = mib[2] = i + 1;
2273		mib[3] = KERN_EMUL_NAME;
2274		len = sizeof(string);
2275		if (sysctl(mib, 4, string, &len, NULL, 0) == -1)
2276			continue;
2277		if (strcmp(string, "native") == 0)
2278			continue;
2279		emul_names[i].name = strdup(string);
2280		if (emul_names[i].name == NULL) {
2281			free(emul_names);
2282			return (-1);
2283		}
2284	}
2285	qsort(emul_names, nemuls, sizeof(*emul_names), emulcmp);
2286	for (i = 0; i < emul_num; i++) {
2287		if (!emul_names[i].name || (i > 0 &&
2288		    strcmp(emul_names[i].name, emul_names[i - 1].name) == 0)) {
2289
2290			nemuls--;
2291		}
2292	}
2293	return (0);
2294}
2295
2296/*
2297 * Scan a list of names searching for a particular name.
2298 */
2299int
2300findname(char *string, char *level, char **bufp, struct list *namelist)
2301{
2302	char *name;
2303	int i;
2304
2305	if (namelist->list == 0 || (name = strsep(bufp, ".")) == NULL) {
2306		warnx("%s: incomplete specification", string);
2307		return (-1);
2308	}
2309	for (i = 0; i < namelist->size; i++)
2310		if (namelist->list[i].ctl_name != NULL &&
2311		    strcmp(name, namelist->list[i].ctl_name) == 0)
2312			break;
2313	if (i == namelist->size) {
2314		warnx("%s level name %s in %s is invalid", level, name, string);
2315		return (-1);
2316	}
2317	return (i);
2318}
2319
2320void
2321usage(void)
2322{
2323
2324	(void)fprintf(stderr, "usage:\t%s\n\t%s\n\t%s\n",
2325	    "sysctl [-n] variable ...", "sysctl [-nqw] variable=value ...",
2326	    "sysctl [-n] -Aa");
2327	exit(1);
2328}
2329