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