pstat.c revision 47018
1/*-
2 * Copyright (c) 1980, 1991, 1993, 1994
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static const char copyright[] =
36"@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
37	The Regents of the University of California.  All rights reserved.\n";
38#endif /* not lint */
39
40#ifndef lint
41#if 0
42static char sccsid[] = "@(#)pstat.c	8.16 (Berkeley) 5/9/95";
43#endif
44static const char rcsid[] =
45	"$Id: pstat.c,v 1.40 1999/01/22 10:57:22 dillon Exp $";
46#endif /* not lint */
47
48#include <sys/param.h>
49#include <sys/time.h>
50#include <sys/vnode.h>
51#include <sys/ucred.h>
52#define KERNEL
53#include <sys/file.h>
54#include <ufs/ufs/quota.h>
55#include <ufs/ufs/inode.h>
56#include <sys/mount.h>
57#include <sys/uio.h>
58#include <sys/namei.h>
59#include <miscfs/union/union.h>
60#undef KERNEL
61#include <sys/stat.h>
62#include <nfs/rpcv2.h>
63#include <nfs/nfsproto.h>
64#include <nfs/nfs.h>
65#include <nfs/nfsnode.h>
66#include <sys/ioctl.h>
67#include <sys/ioctl_compat.h>	/* XXX NTTYDISC is too well hidden */
68#include <sys/tty.h>
69#include <sys/conf.h>
70#include <sys/blist.h>
71
72#include <sys/user.h>
73#include <sys/sysctl.h>
74
75#include <err.h>
76#include <fcntl.h>
77#include <kvm.h>
78#include <limits.h>
79#include <nlist.h>
80#include <stdio.h>
81#include <stdlib.h>
82#include <string.h>
83#include <unistd.h>
84
85struct nlist nl[] = {
86#define NLMANDATORYBEG	0
87#define	V_MOUNTLIST	0
88	{ "_mountlist" },	/* address of head of mount list. */
89#define V_NUMV		1
90	{ "_numvnodes" },
91#define	FNL_NFILE	2
92	{"_nfiles"},
93#define FNL_MAXFILE	3
94	{"_maxfiles"},
95#define NLMANDATORYEND FNL_MAXFILE	/* names up to here are mandatory */
96#define	SCONS		NLMANDATORYEND + 1
97	{ "_cons" },
98#define	SPTY		NLMANDATORYEND + 2
99	{ "_pt_tty" },
100#define	SNPTY		NLMANDATORYEND + 3
101	{ "_npty" },
102
103#ifdef hp300
104#define	SDCA	(SNPTY+1)
105	{ "_dca_tty" },
106#define	SNDCA	(SNPTY+2)
107	{ "_ndca" },
108#define	SDCM	(SNPTY+3)
109	{ "_dcm_tty" },
110#define	SNDCM	(SNPTY+4)
111	{ "_ndcm" },
112#define	SDCL	(SNPTY+5)
113	{ "_dcl_tty" },
114#define	SNDCL	(SNPTY+6)
115	{ "_ndcl" },
116#define	SITE	(SNPTY+7)
117	{ "_ite_tty" },
118#define	SNITE	(SNPTY+8)
119	{ "_nite" },
120#endif
121
122#ifdef mips
123#define SDC	(SNPTY+1)
124	{ "_dc_tty" },
125#define SNDC	(SNPTY+2)
126	{ "_dc_cnt" },
127#endif
128
129#ifdef __FreeBSD__
130#define SCCONS	(SNPTY+1)
131	{ "_sccons" },
132#define NSCCONS	(SNPTY+2)
133	{ "_nsccons" },
134#define SIO  (SNPTY+3)
135	{ "_sio_tty" },
136#define NSIO (SNPTY+4)
137	{ "_nsio_tty" },
138#define RC  (SNPTY+5)
139	{ "_rc_tty" },
140#define NRC (SNPTY+6)
141	{ "_nrc_tty" },
142#define CY  (SNPTY+7)
143	{ "_cy_tty" },
144#define NCY (SNPTY+8)
145	{ "_ncy_tty" },
146#define SI  (SNPTY+9)
147	{ "_si_tty" },
148#define NSI (SNPTY+10)
149	{ "_si_Nports" },
150#endif
151	{ "" }
152};
153
154int	usenumflag;
155int	totalflag;
156int	swapflag;
157char	*nlistf	= NULL;
158char	*memf	= NULL;
159kvm_t	*kd;
160
161char	*usagestr;
162
163struct {
164	int m_flag;
165	const char *m_name;
166} mnt_flags[] = {
167	{ MNT_RDONLY, "rdonly" },
168	{ MNT_SYNCHRONOUS, "sync" },
169	{ MNT_NOEXEC, "noexec" },
170	{ MNT_NOSUID, "nosuid" },
171	{ MNT_NODEV, "nodev" },
172	{ MNT_UNION, "union" },
173	{ MNT_ASYNC, "async" },
174	{ MNT_NOATIME, "noatime" },
175	{ MNT_EXRDONLY, "exrdonly" },
176	{ MNT_EXPORTED, "exported" },
177	{ MNT_DEFEXPORTED, "defexported" },
178	{ MNT_EXPORTANON, "exportanon" },
179	{ MNT_EXKERB, "exkerb" },
180	{ MNT_LOCAL, "local" },
181	{ MNT_QUOTA, "quota" },
182	{ MNT_ROOTFS, "rootfs" },
183	{ MNT_USER, "user" },
184	{ MNT_UPDATE, "update" },
185	{ MNT_DELEXPORT },
186	{ MNT_UPDATE, "update" },
187	{ MNT_DELEXPORT, "delexport" },
188	{ MNT_RELOAD, "reload" },
189	{ MNT_FORCE, "force" },
190#if 0
191	{ MNT_UNMOUNT, "unmount" },
192	{ MNT_MWAIT, "mwait" },
193	{ MNT_WANTRDWR, "wantrdwr" },
194#endif
195	{ 0 }
196};
197
198
199#define	SVAR(var) __STRING(var)	/* to force expansion */
200#define	KGET(idx, var)							\
201	KGET1(idx, &var, sizeof(var), SVAR(var))
202#define	KGET1(idx, p, s, msg)						\
203	KGET2(nl[idx].n_value, p, s, msg)
204#define	KGET2(addr, p, s, msg)						\
205	if (kvm_read(kd, (u_long)(addr), p, s) != s)			\
206		warnx("cannot read %s: %s", msg, kvm_geterr(kd))
207#define	KGETN(idx, var)							\
208	KGET1N(idx, &var, sizeof(var), SVAR(var))
209#define	KGET1N(idx, p, s, msg)						\
210	KGET2N(nl[idx].n_value, p, s, msg)
211#define	KGET2N(addr, p, s, msg)						\
212	((kvm_read(kd, (u_long)(addr), p, s) == s) ? 1 : 0)
213#define	KGETRET(addr, p, s, msg)					\
214	if (kvm_read(kd, (u_long)(addr), p, s) != s) {			\
215		warnx("cannot read %s: %s", msg, kvm_geterr(kd));	\
216		return (0);						\
217	}
218
219void	filemode __P((void));
220int	getfiles __P((char **, int *));
221struct mount *
222	getmnt __P((struct mount *));
223struct e_vnode *
224	kinfo_vnodes __P((int *));
225struct e_vnode *
226	loadvnodes __P((int *));
227void	mount_print __P((struct mount *));
228void	nfs_header __P((void));
229int	nfs_print __P((struct vnode *));
230void	swapmode __P((void));
231void	ttymode __P((void));
232void	ttyprt __P((struct tty *, int));
233void	ttytype __P((struct tty *, char *, int, int, int));
234void	ufs_header __P((void));
235int	ufs_print __P((struct vnode *));
236void	union_header __P((void));
237int	union_print __P((struct vnode *));
238static void usage __P((void));
239void	vnode_header __P((void));
240void	vnode_print __P((struct vnode *, struct vnode *));
241void	vnodemode __P((void));
242
243int
244main(argc, argv)
245	int argc;
246	char *argv[];
247{
248	int ch, i, quit, ret;
249	int fileflag, ttyflag, vnodeflag;
250	char buf[_POSIX2_LINE_MAX],*opts;
251
252	fileflag = swapflag = ttyflag = vnodeflag = 0;
253
254	/* We will behave like good old swapinfo if thus invoked */
255	opts = strrchr(argv[0],'/');
256	if (opts)
257		opts++;
258	else
259		opts = argv[0];
260	if (!strcmp(opts,"swapinfo")) {
261		swapflag = 1;
262		opts = "kM:N:";
263		usagestr = "swapinfo [-k] [-M core] [-N system]";
264	} else {
265		opts = "TM:N:fiknstv";
266		usagestr = "pstat [-Tfknstv] [-M core] [-N system]";
267	}
268
269	while ((ch = getopt(argc, argv, opts)) != -1)
270		switch (ch) {
271		case 'f':
272			fileflag = 1;
273			break;
274		case 'k':
275			putenv("BLOCKSIZE=1K");
276			break;
277		case 'M':
278			memf = optarg;
279			break;
280		case 'N':
281			nlistf = optarg;
282			break;
283		case 'n':
284			usenumflag = 1;
285			break;
286		case 's':
287			++swapflag;
288			break;
289		case 'T':
290			totalflag = 1;
291			break;
292		case 't':
293			ttyflag = 1;
294			break;
295		case 'v':
296		case 'i':		/* Backward compatibility. */
297			fprintf(stderr, "vnode mode not supported\n");
298			exit(1);
299#if 0
300			vnodeflag = 1;
301			break;
302#endif
303		default:
304			usage();
305		}
306	argc -= optind;
307	argv += optind;
308
309	/*
310	 * Discard setgid privileges if not the running kernel so that bad
311	 * guys can't print interesting stuff from kernel memory.
312	 */
313	if (nlistf != NULL || memf != NULL)
314		(void)setgid(getgid());
315
316	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
317		errx(1, "kvm_openfiles: %s", buf);
318	if ((ret = kvm_nlist(kd, nl)) != 0) {
319		if (ret == -1)
320			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
321		for (i = NLMANDATORYBEG, quit = 0; i <= NLMANDATORYEND; i++)
322			if (!nl[i].n_value) {
323				quit = 1;
324				warnx("undefined symbol: %s", nl[i].n_name);
325			}
326		if (quit)
327			exit(1);
328	}
329	if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
330		usage();
331	if (fileflag || totalflag)
332		filemode();
333	if (vnodeflag)
334		vnodemode();
335	if (ttyflag)
336		ttymode();
337	if (swapflag || totalflag)
338		swapmode();
339	exit (0);
340}
341
342static void
343usage()
344{
345	fprintf(stderr, "usage: %s\n", usagestr);
346	exit (1);
347}
348
349struct e_vnode {
350	struct vnode *avnode;
351	struct vnode vnode;
352};
353
354void
355vnodemode()
356{
357	struct e_vnode *e_vnodebase, *endvnode, *evp;
358	struct vnode *vp;
359	struct mount *maddr, *mp;
360	int numvnodes;
361
362	e_vnodebase = loadvnodes(&numvnodes);
363	if (totalflag) {
364		(void)printf("%7d vnodes\n", numvnodes);
365		return;
366	}
367	endvnode = e_vnodebase + numvnodes;
368	(void)printf("%d active vnodes\n", numvnodes);
369
370
371#define ST	mp->mnt_stat
372	maddr = NULL;
373	for (evp = e_vnodebase; evp < endvnode; evp++) {
374		vp = &evp->vnode;
375		if (vp->v_mount != maddr) {
376			/*
377			 * New filesystem
378			 */
379			if ((mp = getmnt(vp->v_mount)) == NULL)
380				continue;
381			maddr = vp->v_mount;
382			mount_print(mp);
383			vnode_header();
384			if (!strcmp(ST.f_fstypename, "ufs") ||
385			    !strcmp(ST.f_fstypename, "mfs"))
386				ufs_header();
387			else if (!strcmp(ST.f_fstypename, "nfs"))
388				nfs_header();
389			else if (!strcmp(ST.f_fstypename, "union"))
390				union_header();
391			(void)printf("\n");
392		}
393		vnode_print(evp->avnode, vp);
394		if (!strcmp(ST.f_fstypename, "ufs") ||
395		    !strcmp(ST.f_fstypename, "mfs"))
396			ufs_print(vp);
397		else if (!strcmp(ST.f_fstypename, "nfs"))
398			nfs_print(vp);
399		else if (!strcmp(ST.f_fstypename, "union"))
400			union_print(vp);
401		(void)printf("\n");
402	}
403	free(e_vnodebase);
404}
405
406void
407vnode_header()
408{
409	(void)printf("ADDR     TYP VFLAG  USE HOLD");
410}
411
412void
413vnode_print(avnode, vp)
414	struct vnode *avnode;
415	struct vnode *vp;
416{
417	char *type, flags[16];
418	char *fp = flags;
419	int flag;
420
421	/*
422	 * set type
423	 */
424	switch (vp->v_type) {
425	case VNON:
426		type = "non"; break;
427	case VREG:
428		type = "reg"; break;
429	case VDIR:
430		type = "dir"; break;
431	case VBLK:
432		type = "blk"; break;
433	case VCHR:
434		type = "chr"; break;
435	case VLNK:
436		type = "lnk"; break;
437	case VSOCK:
438		type = "soc"; break;
439	case VFIFO:
440		type = "fif"; break;
441	case VBAD:
442		type = "bad"; break;
443	default:
444		type = "unk"; break;
445	}
446	/*
447	 * gather flags
448	 */
449	flag = vp->v_flag;
450	if (flag & VROOT)
451		*fp++ = 'R';
452	if (flag & VTEXT)
453		*fp++ = 'T';
454	if (flag & VSYSTEM)
455		*fp++ = 'S';
456	if (flag & VISTTY)
457		*fp++ = 't';
458	if (flag & VXLOCK)
459		*fp++ = 'L';
460	if (flag & VXWANT)
461		*fp++ = 'W';
462	if (flag & VBWAIT)
463		*fp++ = 'B';
464	if (flag & VALIASED)
465		*fp++ = 'A';
466	if (flag & VOBJBUF)
467		*fp++ = 'V';
468	if (flag & VAGE)
469		*fp++ = 'a';
470	if (flag & VOLOCK)
471		*fp++ = 'l';
472	if (flag & VOWANT)
473		*fp++ = 'w';
474	if (flag == 0)
475		*fp++ = '-';
476	*fp = '\0';
477	(void)printf("%8lx %s %5s %4d %4d",
478	    (u_long)(void *)avnode, type, flags, vp->v_usecount, vp->v_holdcnt);
479}
480
481void
482ufs_header()
483{
484	(void)printf(" FILEID IFLAG RDEV|SZ");
485}
486
487int
488ufs_print(vp)
489	struct vnode *vp;
490{
491	int flag;
492	struct inode inode, *ip = &inode;
493	char flagbuf[16], *flags = flagbuf;
494	char *name;
495	mode_t type;
496
497	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
498	flag = ip->i_flag;
499	if (flag & IN_RENAME)
500		*flags++ = 'R';
501	if (flag & IN_UPDATE)
502		*flags++ = 'U';
503	if (flag & IN_ACCESS)
504		*flags++ = 'A';
505	if (flag & IN_CHANGE)
506		*flags++ = 'C';
507	if (flag & IN_MODIFIED)
508		*flags++ = 'M';
509	if (flag & IN_SHLOCK)
510		*flags++ = 'S';
511	if (flag & IN_EXLOCK)
512		*flags++ = 'E';
513	if (flag == 0)
514		*flags++ = '-';
515	*flags = '\0';
516
517	(void)printf(" %6d %5s", ip->i_number, flagbuf);
518	type = ip->i_mode & S_IFMT;
519	if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
520		if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL))
521			(void)printf("   %2d,%-2d",
522			    major(ip->i_rdev), minor(ip->i_rdev));
523		else
524			(void)printf(" %7s", name);
525	else
526		(void)printf(" %7qd", ip->i_size);
527	return (0);
528}
529
530void
531nfs_header()
532{
533	(void)printf(" FILEID NFLAG RDEV|SZ");
534}
535
536int
537nfs_print(vp)
538	struct vnode *vp;
539{
540	struct nfsnode nfsnode, *np = &nfsnode;
541	char flagbuf[16], *flags = flagbuf;
542	int flag;
543	char *name;
544	mode_t type;
545
546	KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
547	flag = np->n_flag;
548	if (flag & NFLUSHWANT)
549		*flags++ = 'W';
550	if (flag & NFLUSHINPROG)
551		*flags++ = 'P';
552	if (flag & NMODIFIED)
553		*flags++ = 'M';
554	if (flag & NWRITEERR)
555		*flags++ = 'E';
556	if (flag & NQNFSNONCACHE)
557		*flags++ = 'X';
558	if (flag & NQNFSWRITE)
559		*flags++ = 'O';
560	if (flag & NQNFSEVICTED)
561		*flags++ = 'G';
562	if (flag == 0)
563		*flags++ = '-';
564	*flags = '\0';
565
566#define VT	np->n_vattr
567	(void)printf(" %6ld %5s", VT.va_fileid, flagbuf);
568	type = VT.va_mode & S_IFMT;
569	if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode))
570		if (usenumflag || ((name = devname(VT.va_rdev, type)) == NULL))
571			(void)printf("   %2d,%-2d",
572			    major(VT.va_rdev), minor(VT.va_rdev));
573		else
574			(void)printf(" %7s", name);
575	else
576		(void)printf(" %7qd", np->n_size);
577	return (0);
578}
579
580void
581union_header()
582{
583	(void)printf("    UPPER    LOWER");
584}
585
586int
587union_print(vp)
588	struct vnode *vp;
589{
590	struct union_node unode, *up = &unode;
591
592	KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode");
593
594	(void)printf(" %8lx %8lx", (u_long)(void *)up->un_uppervp,
595	    (u_long)(void *)up->un_lowervp);
596	return (0);
597}
598
599/*
600 * Given a pointer to a mount structure in kernel space,
601 * read it in and return a usable pointer to it.
602 */
603struct mount *
604getmnt(maddr)
605	struct mount *maddr;
606{
607	static struct mtab {
608		struct mtab *next;
609		struct mount *maddr;
610		struct mount mount;
611	} *mhead = NULL;
612	struct mtab *mt;
613
614	for (mt = mhead; mt != NULL; mt = mt->next)
615		if (maddr == mt->maddr)
616			return (&mt->mount);
617	if ((mt = malloc(sizeof(struct mtab))) == NULL)
618		errx(1, "malloc");
619	KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
620	mt->maddr = maddr;
621	mt->next = mhead;
622	mhead = mt;
623	return (&mt->mount);
624}
625
626void
627mount_print(mp)
628	struct mount *mp;
629{
630	int flags;
631	const char *type;
632
633#define ST	mp->mnt_stat
634	(void)printf("*** MOUNT %s %s on %s", ST.f_fstypename,
635	    ST.f_mntfromname, ST.f_mntonname);
636	if ((flags = mp->mnt_flag)) {
637		int i;
638		const char *sep = " (";
639
640		for (i = 0; mnt_flags[i].m_flag; i++) {
641			if (flags & mnt_flags[i].m_flag) {
642				(void)printf("%s%s", sep, mnt_flags[i].m_name);
643				flags &= ~mnt_flags[i].m_flag;
644				sep = ",";
645			}
646		}
647		if (flags)
648			(void)printf("%sunknown_flags:%x", sep, flags);
649		(void)printf(")");
650	}
651	(void)printf("\n");
652#undef ST
653}
654
655struct e_vnode *
656loadvnodes(avnodes)
657	int *avnodes;
658{
659	int mib[2];
660	size_t copysize;
661	struct e_vnode *vnodebase;
662
663	if (memf != NULL) {
664		/*
665		 * do it by hand
666		 */
667		return (kinfo_vnodes(avnodes));
668	}
669	mib[0] = CTL_KERN;
670	mib[1] = KERN_VNODE;
671	if (sysctl(mib, 2, NULL, &copysize, NULL, 0) == -1)
672		err(1, "sysctl: KERN_VNODE");
673	if ((vnodebase = malloc(copysize)) == NULL)
674		errx(1, "malloc");
675	if (sysctl(mib, 2, vnodebase, &copysize, NULL, 0) == -1)
676		err(1, "sysctl: KERN_VNODE");
677	if (copysize % sizeof(struct e_vnode))
678		errx(1, "vnode size mismatch");
679	*avnodes = copysize / sizeof(struct e_vnode);
680
681	return (vnodebase);
682}
683
684/*
685 * simulate what a running kernel does in in kinfo_vnode
686 */
687struct e_vnode *
688kinfo_vnodes(avnodes)
689	int *avnodes;
690{
691	struct mntlist mountlist;
692	struct mount *mp, mount, *mp_next;
693	struct vnode *vp, vnode, *vp_next;
694	char *vbuf, *evbuf, *bp;
695	int num, numvnodes;
696
697#define VPTRSZ  sizeof(struct vnode *)
698#define VNODESZ sizeof(struct vnode)
699
700	KGET(V_NUMV, numvnodes);
701	if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
702		errx(1, "malloc");
703	bp = vbuf;
704	evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
705	KGET(V_MOUNTLIST, mountlist);
706	for (num = 0, mp = mountlist.cqh_first; ; mp = mp_next) {
707		KGET2(mp, &mount, sizeof(mount), "mount entry");
708		mp_next = mount.mnt_list.cqe_next;
709		for (vp = mount.mnt_vnodelist.lh_first;
710		    vp != NULL; vp = vp_next) {
711			KGET2(vp, &vnode, sizeof(vnode), "vnode");
712			vp_next = vnode.v_mntvnodes.le_next;
713			if ((bp + VPTRSZ + VNODESZ) > evbuf)
714				/* XXX - should realloc */
715				errx(1, "no more room for vnodes");
716			memmove(bp, &vp, VPTRSZ);
717			bp += VPTRSZ;
718			memmove(bp, &vnode, VNODESZ);
719			bp += VNODESZ;
720			num++;
721		}
722		if (mp == mountlist.cqh_last)
723			break;
724	}
725	*avnodes = num;
726	return ((struct e_vnode *)vbuf);
727}
728
729char hdr[] =
730"  LINE RAW CAN OUT IHIWT ILOWT OHWT LWT     COL STATE  SESS      PGID DISC\n";
731int ttyspace = 128;
732
733void
734ttymode()
735{
736	struct tty *tty;
737
738	if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL)
739		errx(1, "malloc");
740#if !defined(hp300) && !defined(mips)
741	if (nl[SCONS].n_type != 0) {
742		(void)printf("1 console\n");
743		KGET(SCONS, *tty);
744		(void)printf(hdr);
745		ttyprt(&tty[0], 0);
746	}
747#endif
748#ifdef vax
749	if (nl[SNQD].n_type != 0)
750		qdss();
751	if (nl[SNDZ].n_type != 0)
752		ttytype(tty, "dz", SDZ, SNDZ, 0);
753	if (nl[SNDH].n_type != 0)
754		ttytype(tty, "dh", SDH, SNDH, 0);
755	if (nl[SNDMF].n_type != 0)
756		ttytype(tty, "dmf", SDMF, SNDMF, 0);
757	if (nl[SNDHU].n_type != 0)
758		ttytype(tty, "dhu", SDHU, SNDHU, 0);
759	if (nl[SNDMZ].n_type != 0)
760		ttytype(tty, "dmz", SDMZ, SNDMZ, 0);
761#endif
762#ifdef tahoe
763	if (nl[SNVX].n_type != 0)
764		ttytype(tty, "vx", SVX, SNVX, 0);
765	if (nl[SNMP].n_type != 0)
766		ttytype(tty, "mp", SMP, SNMP, 0);
767#endif
768#ifdef hp300
769	if (nl[SNITE].n_type != 0)
770		ttytype(tty, "ite", SITE, SNITE, 0);
771	if (nl[SNDCA].n_type != 0)
772		ttytype(tty, "dca", SDCA, SNDCA, 0);
773	if (nl[SNDCM].n_type != 0)
774		ttytype(tty, "dcm", SDCM, SNDCM, 0);
775	if (nl[SNDCL].n_type != 0)
776		ttytype(tty, "dcl", SDCL, SNDCL, 0);
777#endif
778#ifdef mips
779	if (nl[SNDC].n_type != 0)
780		ttytype(tty, "dc", SDC, SNDC, 0);
781#endif
782#ifdef __FreeBSD__
783	if (nl[NSCCONS].n_type != 0)
784		ttytype(tty, "vty", SCCONS, NSCCONS, 0);
785	if (nl[NSIO].n_type != 0)
786		ttytype(tty, "sio", SIO, NSIO, 0);
787	if (nl[NRC].n_type != 0)
788		ttytype(tty, "rc", RC, NRC, 0);
789	if (nl[NCY].n_type != 0)
790		ttytype(tty, "cy", CY, NCY, 0);
791	if (nl[NSI].n_type != 0)
792		ttytype(tty, "si", SI, NSI, 1);
793#endif
794	if (nl[SNPTY].n_type != 0)
795		ttytype(tty, "pty", SPTY, SNPTY, 0);
796}
797
798void
799ttytype(tty, name, type, number, indir)
800	struct tty *tty;
801	char *name;
802	int type, number, indir;
803{
804	struct tty *tp;
805	int ntty;
806	struct tty **ttyaddr;
807
808	if (tty == NULL)
809		return;
810	KGET(number, ntty);
811	(void)printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines");
812	if (ntty > ttyspace) {
813		ttyspace = ntty;
814		if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == 0)
815			errx(1, "realloc");
816	}
817	if (indir) {
818		KGET(type, ttyaddr);
819		KGET2(ttyaddr, tty, ntty * sizeof(struct tty), "tty structs");
820	} else {
821		KGET1(type, tty, ntty * sizeof(struct tty), "tty structs");
822	}
823	(void)printf(hdr);
824	for (tp = tty; tp < &tty[ntty]; tp++)
825		ttyprt(tp, tp - tty);
826}
827
828struct {
829	int flag;
830	char val;
831} ttystates[] = {
832#ifdef TS_WOPEN
833	{ TS_WOPEN,	'W'},
834#endif
835	{ TS_ISOPEN,	'O'},
836	{ TS_CARR_ON,	'C'},
837#ifdef TS_CONNECTED
838	{ TS_CONNECTED,	'c'},
839#endif
840	{ TS_TIMEOUT,	'T'},
841	{ TS_FLUSH,	'F'},
842	{ TS_BUSY,	'B'},
843#ifdef TS_ASLEEP
844	{ TS_ASLEEP,	'A'},
845#endif
846#ifdef TS_SO_OLOWAT
847	{ TS_SO_OLOWAT,	'A'},
848#endif
849#ifdef TS_SO_OCOMPLETE
850	{ TS_SO_OCOMPLETE, 'a'},
851#endif
852	{ TS_XCLUDE,	'X'},
853	{ TS_TTSTOP,	'S'},
854#ifdef TS_CAR_OFLOW
855	{ TS_CAR_OFLOW,	'm'},
856#endif
857#ifdef TS_CTS_OFLOW
858	{ TS_CTS_OFLOW,	'o'},
859#endif
860#ifdef TS_DSR_OFLOW
861	{ TS_DSR_OFLOW,	'd'},
862#endif
863	{ TS_TBLOCK,	'K'},
864	{ TS_ASYNC,	'Y'},
865	{ TS_BKSL,	'D'},
866	{ TS_ERASE,	'E'},
867	{ TS_LNCH,	'L'},
868	{ TS_TYPEN,	'P'},
869	{ TS_CNTTB,	'N'},
870#ifdef TS_CAN_BYPASS_L_RINT
871	{ TS_CAN_BYPASS_L_RINT, 'l'},
872#endif
873#ifdef TS_SNOOP
874	{ TS_SNOOP,     's'},
875#endif
876#ifdef TS_ZOMBIE
877	{ TS_ZOMBIE,	'Z'},
878#endif
879	{ 0,	       '\0'},
880};
881
882void
883ttyprt(tp, line)
884	struct tty *tp;
885	int line;
886{
887	int i, j;
888	pid_t pgid;
889	char *name, state[20];
890
891	if (usenumflag || tp->t_dev == 0 ||
892	   (name = devname(tp->t_dev, S_IFCHR)) == NULL)
893		(void)printf("%7d ", line);
894	else
895		(void)printf("%7s ", name);
896	(void)printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
897	(void)printf("%3d %5d %5d %4d %3d %7d ", tp->t_outq.c_cc,
898		tp->t_ihiwat, tp->t_ilowat, tp->t_ohiwat, tp->t_olowat,
899		tp->t_column);
900	for (i = j = 0; ttystates[i].flag; i++)
901		if (tp->t_state&ttystates[i].flag)
902			state[j++] = ttystates[i].val;
903	if (j == 0)
904		state[j++] = '-';
905	state[j] = '\0';
906	(void)printf("%-6s %8lx", state, (u_long)(void *)tp->t_session);
907	pgid = 0;
908	if (tp->t_pgrp != NULL)
909		KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
910	(void)printf("%6d ", pgid);
911	switch (tp->t_line) {
912	case TTYDISC:
913		(void)printf("term\n");
914		break;
915	case NTTYDISC:
916		(void)printf("ntty\n");
917		break;
918	case TABLDISC:
919		(void)printf("tab\n");
920		break;
921	case SLIPDISC:
922		(void)printf("slip\n");
923		break;
924	case PPPDISC:
925		(void)printf("ppp\n");
926		break;
927	default:
928		(void)printf("%d\n", tp->t_line);
929		break;
930	}
931}
932
933void
934filemode()
935{
936	struct file *fp;
937	struct file *addr;
938	char *buf, flagbuf[16], *fbp;
939	int len, maxfile, nfile;
940	static char *dtypes[] = { "???", "inode", "socket" };
941
942	KGET(FNL_MAXFILE, maxfile);
943	if (totalflag) {
944		KGET(FNL_NFILE, nfile);
945		(void)printf("%3d/%3d files\n", nfile, maxfile);
946		return;
947	}
948	if (getfiles(&buf, &len) == -1)
949		return;
950	/*
951	 * Getfiles returns in malloc'd memory a pointer to the first file
952	 * structure, and then an array of file structs (whose addresses are
953	 * derivable from the previous entry).
954	 */
955	addr = ((struct filelist *)buf)->lh_first;
956	fp = (struct file *)(buf + sizeof(struct filelist));
957	nfile = (len - sizeof(struct filelist)) / sizeof(struct file);
958
959	(void)printf("%d/%d open files\n", nfile, maxfile);
960	(void)printf("   LOC   TYPE    FLG     CNT  MSG    DATA    OFFSET\n");
961	for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) {
962		if ((unsigned)fp->f_type > DTYPE_SOCKET)
963			continue;
964		(void)printf("%8lx ", (u_long)(void *)addr);
965		(void)printf("%-8.8s", dtypes[fp->f_type]);
966		fbp = flagbuf;
967		if (fp->f_flag & FREAD)
968			*fbp++ = 'R';
969		if (fp->f_flag & FWRITE)
970			*fbp++ = 'W';
971		if (fp->f_flag & FAPPEND)
972			*fbp++ = 'A';
973#ifdef FSHLOCK	/* currently gone */
974		if (fp->f_flag & FSHLOCK)
975			*fbp++ = 'S';
976		if (fp->f_flag & FEXLOCK)
977			*fbp++ = 'X';
978#endif
979		if (fp->f_flag & FASYNC)
980			*fbp++ = 'I';
981		*fbp = '\0';
982		(void)printf("%6s  %3d", flagbuf, fp->f_count);
983		(void)printf("  %3d", fp->f_msgcount);
984		(void)printf("  %8lx", (u_long)(void *)fp->f_data);
985		if (fp->f_offset < 0)
986			(void)printf("  %qx\n", fp->f_offset);
987		else
988			(void)printf("  %qd\n", fp->f_offset);
989	}
990	free(buf);
991}
992
993int
994getfiles(abuf, alen)
995	char **abuf;
996	int *alen;
997{
998	size_t len;
999	int mib[2];
1000	char *buf;
1001
1002	/*
1003	 * XXX
1004	 * Add emulation of KINFO_FILE here.
1005	 */
1006	if (memf != NULL)
1007		errx(1, "files on dead kernel, not implemented");
1008
1009	mib[0] = CTL_KERN;
1010	mib[1] = KERN_FILE;
1011	if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
1012		warn("sysctl: KERN_FILE");
1013		return (-1);
1014	}
1015	if ((buf = malloc(len)) == NULL)
1016		errx(1, "malloc");
1017	if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
1018		warn("sysctl: KERN_FILE");
1019		return (-1);
1020	}
1021	*abuf = buf;
1022	*alen = len;
1023	return (0);
1024}
1025
1026/*
1027 * swapmode is based on a program called swapinfo written
1028 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
1029 */
1030void
1031swapmode(void)
1032{
1033	struct kvm_swap kswap[16];
1034	int i;
1035	int n;
1036	int pagesize = getpagesize();
1037	const char *header;
1038	int hlen;
1039	long blocksize;
1040
1041	n = kvm_getswapinfo(
1042	    kd,
1043	    kswap,
1044	    sizeof(kswap)/sizeof(kswap[0]),
1045	    ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX
1046	);
1047
1048#define CONVERT(v)	((int)((quad_t)(v) * pagesize / blocksize))
1049
1050	header = getbsize(&hlen, &blocksize);
1051	if (totalflag == 0) {
1052		(void)printf("%-11s %*s %8s %8s %8s  %s\n",
1053		    "Device", hlen, header,
1054		    "Used", "Avail", "Capacity", "Type");
1055
1056		for (i = 0; i < n; ++i) {
1057			(void)printf(
1058			    "%-11s %*d ",
1059			    kswap[i].ksw_devname,
1060			    hlen,
1061			    CONVERT(kswap[i].ksw_total)
1062			);
1063			(void)printf(
1064			    "%8d %8d %5.0f%%    %s\n",
1065			    CONVERT(kswap[i].ksw_used),
1066			    CONVERT(kswap[i].ksw_total - kswap[i].ksw_used),
1067			    (double)kswap[i].ksw_used * 100.0 /
1068				(double)kswap[i].ksw_total,
1069			    (kswap[i].ksw_flags & SW_SEQUENTIAL) ?
1070				"Sequential" : "Interleaved"
1071			);
1072		}
1073	}
1074
1075	if (totalflag) {
1076		blocksize = 1024 * 1024;
1077
1078		(void)printf(
1079		    "%dM/%dM swap space\n",
1080		    CONVERT(kswap[n].ksw_used),
1081		    CONVERT(kswap[n].ksw_total)
1082		);
1083	} else if (n > 1) {
1084		(void)printf(
1085		    "%-11s %*d %8d %8d %5.0f%%\n",
1086		    "Total",
1087		    hlen,
1088		    CONVERT(kswap[n].ksw_total),
1089		    CONVERT(kswap[n].ksw_used),
1090		    CONVERT(kswap[n].ksw_total - kswap[n].ksw_used),
1091		    (double)kswap[n].ksw_used * 100.0 /
1092			(double)kswap[n].ksw_total
1093		);
1094	}
1095}
1096