pstat.c revision 1.37
1/*	$OpenBSD: pstat.c,v 1.37 2002/07/12 05:32:51 art Exp $	*/
2/*	$NetBSD: pstat.c,v 1.27 1996/10/23 22:50:06 cgd Exp $	*/
3
4/*-
5 * Copyright (c) 1980, 1991, 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. All advertising materials mentioning features or use of this software
17 *    must display the following acknowledgement:
18 *	This product includes software developed by the University of
19 *	California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 *    may be used to endorse or promote products derived from this software
22 *    without specific prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef lint
38static char copyright[] =
39"@(#) Copyright (c) 1980, 1991, 1993\n\
40	The Regents of the University of California.  All rights reserved.\n";
41#endif /* not lint */
42
43#ifndef lint
44#if 0
45from: static char sccsid[] = "@(#)pstat.c	8.9 (Berkeley) 2/16/94";
46#else
47static char *rcsid = "$OpenBSD: pstat.c,v 1.37 2002/07/12 05:32:51 art Exp $";
48#endif
49#endif /* not lint */
50
51#include <sys/param.h>
52#include <sys/time.h>
53#include <sys/buf.h>
54#include <sys/vnode.h>
55#include <sys/ucred.h>
56#define _KERNEL
57#include <sys/file.h>
58#include <ufs/ufs/quota.h>
59#include <ufs/ufs/inode.h>
60#define NFS
61#include <sys/mount.h>
62#undef NFS
63#undef _KERNEL
64#include <sys/stat.h>
65#include <nfs/nfsproto.h>
66#include <nfs/rpcv2.h>
67#include <nfs/nfsnode.h>
68#include <sys/ioctl.h>
69#include <sys/tty.h>
70#include <sys/conf.h>
71#include <sys/device.h>
72#include <sys/swap.h>
73
74#include <sys/sysctl.h>
75
76#include <err.h>
77#include <kvm.h>
78#include <limits.h>
79#include <nlist.h>
80#include <paths.h>
81#include <stdio.h>
82#include <stdlib.h>
83#include <string.h>
84#include <unistd.h>
85
86struct nlist nl[] = {
87#define	FNL_NFILE	0		/* sysctl */
88	{"_nfiles"},
89#define FNL_MAXFILE	1		/* sysctl */
90	{"_maxfiles"},
91#define TTY_NTTY	2		/* sysctl */
92	{"_tty_count"},
93#define V_NUMV		3		/* sysctl */
94	{ "_numvnodes" },
95#define	V_MOUNTLIST	4		/* no sysctl */
96	{ "_mountlist" },
97#define TTY_TTYLIST	5		/* no sysctl */
98	{"_ttylist"},
99	{ "" }
100};
101
102int	usenumflag;
103int	totalflag;
104int	kflag;
105char	*nlistf	= NULL;
106char	*memf	= NULL;
107kvm_t	*kd = 0;
108
109#define	SVAR(var) __STRING(var)	/* to force expansion */
110#define	KGET(idx, var)							\
111	KGET1(idx, &var, sizeof(var), SVAR(var))
112#define	KGET1(idx, p, s, msg)						\
113	KGET2(nl[idx].n_value, p, s, msg)
114#define	KGET2(addr, p, s, msg)						\
115	if (kvm_read(kd, (u_long)(addr), p, s) != s)			\
116		warnx("cannot read %s: %s", msg, kvm_geterr(kd))
117#define	KGETRET(addr, p, s, msg)					\
118	if (kvm_read(kd, (u_long)(addr), p, s) != s) {			\
119		warnx("cannot read %s: %s", msg, kvm_geterr(kd));	\
120		return (0);						\
121	}
122
123void	filemode(void);
124int	getfiles(char **, size_t *);
125struct mount *
126	getmnt(struct mount *);
127struct e_vnode *
128	kinfo_vnodes(int *);
129struct e_vnode *
130	loadvnodes(int *);
131void	mount_print(struct mount *);
132void	nfs_header(void);
133int	nfs_print(struct vnode *);
134void	swapmode(void);
135void	ttymode(void);
136void	ttyprt(struct tty *);
137void	ufs_header(void);
138int	ufs_print(struct vnode *);
139void	ext2fs_header(void);
140int	ext2fs_print(struct vnode *);
141void	usage(void);
142void	vnode_header(void);
143void	vnode_print(struct vnode *, struct vnode *);
144void	vnodemode(void);
145
146int
147main(argc, argv)
148	int argc;
149	char *argv[];
150{
151	extern char *optarg;
152	extern int optind;
153	int ch, ret;
154	int fileflag, swapflag, ttyflag, vnodeflag;
155	char buf[_POSIX2_LINE_MAX];
156
157	fileflag = swapflag = ttyflag = vnodeflag = 0;
158	while ((ch = getopt(argc, argv, "TM:N:fiknstv")) != -1)
159		switch (ch) {
160		case 'f':
161			fileflag = 1;
162			break;
163		case 'M':
164			memf = optarg;
165			break;
166		case 'N':
167			nlistf = optarg;
168			break;
169		case 'n':
170			usenumflag = 1;
171			break;
172		case 's':
173			swapflag = 1;
174			break;
175		case 'T':
176			totalflag = 1;
177			break;
178		case 't':
179			ttyflag = 1;
180			break;
181		case 'k':
182			kflag = 1;
183			break;
184		case 'v':
185		case 'i':		/* Backward compatibility. */
186			vnodeflag = 1;
187			break;
188		default:
189			usage();
190		}
191	argc -= optind;
192	argv += optind;
193
194	/*
195	 * Discard setgid privileges if not the running kernel so that bad
196	 * guys can't print interesting stuff from kernel memory.
197	 */
198	if (nlistf != NULL || memf != NULL) {
199		(void)setegid(getgid());
200		(void)setgid(getgid());
201	}
202
203	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
204		errx(1, "kvm_openfiles: %s", buf);
205
206	(void)setegid(getgid());
207	(void)setgid(getgid());
208
209	if ((ret = kvm_nlist(kd, nl)) == -1)
210		errx(1, "kvm_nlist: %s", kvm_geterr(kd));
211	if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
212		usage();
213	if (fileflag || totalflag)
214		filemode();
215	if (vnodeflag || totalflag)
216		vnodemode();
217	if (ttyflag)
218		ttymode();
219	if (swapflag || totalflag)
220		swapmode();
221	exit (0);
222}
223
224void
225vnodemode()
226{
227	struct e_vnode *e_vnodebase, *endvnode, *evp;
228	struct vnode *vp;
229	struct mount *maddr, *mp = NULL;
230	int numvnodes;
231
232	e_vnodebase = loadvnodes(&numvnodes);
233	if (totalflag) {
234		(void)printf("%7d vnodes\n", numvnodes);
235		return;
236	}
237	endvnode = e_vnodebase + numvnodes;
238	(void)printf("%d active vnodes\n", numvnodes);
239
240
241#define ST	mp->mnt_stat
242	maddr = NULL;
243	for (evp = e_vnodebase; evp < endvnode; evp++) {
244		vp = &evp->vnode;
245		if (vp->v_mount != maddr) {
246			/*
247			 * New filesystem
248			 */
249			if ((mp = getmnt(vp->v_mount)) == NULL)
250				continue;
251			maddr = vp->v_mount;
252			mount_print(mp);
253			vnode_header();
254			if (!strncmp(ST.f_fstypename, MOUNT_FFS, MFSNAMELEN) ||
255			    !strncmp(ST.f_fstypename, MOUNT_MFS, MFSNAMELEN)) {
256				ufs_header();
257			} else if (!strncmp(ST.f_fstypename, MOUNT_NFS,
258			    MFSNAMELEN)) {
259				nfs_header();
260			} else if (!strncmp(ST.f_fstypename, MOUNT_EXT2FS,
261			    MFSNAMELEN)) {
262				ext2fs_header();
263			}
264			(void)printf("\n");
265		}
266		vnode_print(evp->vptr, vp);
267		if (!strncmp(ST.f_fstypename, MOUNT_FFS, MFSNAMELEN) ||
268		    !strncmp(ST.f_fstypename, MOUNT_MFS, MFSNAMELEN)) {
269			ufs_print(vp);
270		} else if (!strncmp(ST.f_fstypename, MOUNT_NFS, MFSNAMELEN)) {
271			nfs_print(vp);
272		} else if (!strncmp(ST.f_fstypename, MOUNT_EXT2FS,
273		    MFSNAMELEN)) {
274			ext2fs_print(vp);
275		}
276		(void)printf("\n");
277	}
278	free(e_vnodebase);
279}
280
281void
282vnode_header()
283{
284	(void)printf("ADDR     TYP VFLAG  USE HOLD");
285}
286
287void
288vnode_print(avnode, vp)
289	struct vnode *avnode;
290	struct vnode *vp;
291{
292	char *type, flags[16];
293	char *fp = flags;
294	int flag;
295
296	/*
297	 * set type
298	 */
299	switch (vp->v_type) {
300	case VNON:
301		type = "non"; break;
302	case VREG:
303		type = "reg"; break;
304	case VDIR:
305		type = "dir"; break;
306	case VBLK:
307		type = "blk"; break;
308	case VCHR:
309		type = "chr"; break;
310	case VLNK:
311		type = "lnk"; break;
312	case VSOCK:
313		type = "soc"; break;
314	case VFIFO:
315		type = "fif"; break;
316	case VBAD:
317		type = "bad"; break;
318	default:
319		type = "unk"; break;
320	}
321	/*
322	 * gather flags
323	 */
324	flag = vp->v_flag;
325	if (flag & VROOT)
326		*fp++ = 'R';
327	if (flag & VTEXT)
328		*fp++ = 'T';
329	if (flag & VSYSTEM)
330		*fp++ = 'S';
331	if (flag & VISTTY)
332		*fp++ = 'I';
333	if (flag & VXLOCK)
334		*fp++ = 'L';
335	if (flag & VXWANT)
336		*fp++ = 'W';
337	if (vp->v_bioflag & VBIOWAIT)
338		*fp++ = 'B';
339	if (flag & VALIASED)
340		*fp++ = 'A';
341	if (vp->v_bioflag & VBIOONFREELIST)
342		*fp++ = 'F';
343	if (flag & VLOCKSWORK)
344		*fp++ = 'l';
345	if (vp->v_bioflag & VBIOONSYNCLIST)
346		*fp++ = 's';
347	if (flag == 0)
348		*fp++ = '-';
349	*fp = '\0';
350	(void)printf("%8lx %s %5s %4d %4u",
351	    (long)avnode, type, flags, vp->v_usecount, vp->v_holdcnt);
352}
353
354void
355ufs_header()
356{
357	(void)printf(" FILEID IFLAG RDEV|SZ");
358}
359
360int
361ufs_print(vp)
362	struct vnode *vp;
363{
364	int flag;
365	struct inode inode, *ip = &inode;
366	char flagbuf[16], *flags = flagbuf;
367	char *name;
368	mode_t type;
369
370	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
371	flag = ip->i_flag;
372#if 0
373	if (flag & IN_LOCKED)
374		*flags++ = 'L';
375	if (flag & IN_WANTED)
376		*flags++ = 'W';
377	if (flag & IN_LWAIT)
378		*flags++ = 'Z';
379#endif
380	if (flag & IN_ACCESS)
381		*flags++ = 'A';
382	if (flag & IN_CHANGE)
383		*flags++ = 'C';
384	if (flag & IN_UPDATE)
385		*flags++ = 'U';
386	if (flag & IN_MODIFIED)
387		*flags++ = 'M';
388	if (flag & IN_RENAME)
389		*flags++ = 'R';
390	if (flag & IN_SHLOCK)
391		*flags++ = 'S';
392	if (flag & IN_EXLOCK)
393		*flags++ = 'E';
394	if (flag == 0)
395		*flags++ = '-';
396	*flags = '\0';
397
398	(void)printf(" %6d %5s", ip->i_number, flagbuf);
399	type = ip->i_ffs_mode & S_IFMT;
400	if (S_ISCHR(ip->i_ffs_mode) || S_ISBLK(ip->i_ffs_mode))
401		if (usenumflag ||
402		    ((name = devname(ip->i_ffs_rdev, type)) == NULL))
403			(void)printf("   %2d,%-2d",
404			    major(ip->i_ffs_rdev), minor(ip->i_ffs_rdev));
405		else
406			(void)printf(" %7s", name);
407	else
408		(void)printf(" %7qd", ip->i_ffs_size);
409	return (0);
410}
411
412void
413ext2fs_header()
414{
415	(void)printf(" FILEID IFLAG SZ");
416}
417
418int
419ext2fs_print(vp)
420	struct vnode *vp;
421{
422	int flag;
423	struct inode inode, *ip = &inode;
424	char flagbuf[16], *flags = flagbuf;
425
426	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
427	flag = ip->i_flag;
428#if 0
429	if (flag & IN_LOCKED)
430		*flags++ = 'L';
431	if (flag & IN_WANTED)
432		*flags++ = 'W';
433	if (flag & IN_LWAIT)
434		*flags++ = 'Z';
435#endif
436	if (flag & IN_ACCESS)
437		*flags++ = 'A';
438	if (flag & IN_CHANGE)
439		*flags++ = 'C';
440	if (flag & IN_UPDATE)
441		*flags++ = 'U';
442	if (flag & IN_MODIFIED)
443		*flags++ = 'M';
444	if (flag & IN_RENAME)
445		*flags++ = 'R';
446	if (flag & IN_SHLOCK)
447		*flags++ = 'S';
448	if (flag & IN_EXLOCK)
449		*flags++ = 'E';
450	if (flag == 0)
451		*flags++ = '-';
452	*flags = '\0';
453
454	(void)printf(" %6d %5s %2d", ip->i_number, flagbuf, ip->i_e2fs_size);
455	return (0);
456}
457
458void
459nfs_header()
460{
461	(void)printf(" FILEID NFLAG RDEV|SZ");
462}
463
464int
465nfs_print(vp)
466	struct vnode *vp;
467{
468	struct nfsnode nfsnode, *np = &nfsnode;
469	char flagbuf[16], *flags = flagbuf;
470	int flag;
471	char *name;
472	mode_t type;
473
474	KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
475	flag = np->n_flag;
476	if (flag & NFLUSHWANT)
477		*flags++ = 'W';
478	if (flag & NFLUSHINPROG)
479		*flags++ = 'P';
480	if (flag & NMODIFIED)
481		*flags++ = 'M';
482	if (flag & NWRITEERR)
483		*flags++ = 'E';
484	if (flag & NACC)
485		*flags++ = 'A';
486	if (flag & NUPD)
487		*flags++ = 'U';
488	if (flag & NCHG)
489		*flags++ = 'C';
490	if (flag == 0)
491		*flags++ = '-';
492	*flags = '\0';
493
494#define VT	np->n_vattr
495	(void)printf(" %6ld %5s", VT.va_fileid, flagbuf);
496	type = VT.va_mode & S_IFMT;
497	if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode))
498		if (usenumflag || ((name = devname(VT.va_rdev, type)) == NULL))
499			(void)printf("   %2d,%-2d",
500			    major(VT.va_rdev), minor(VT.va_rdev));
501		else
502			(void)printf(" %7s", name);
503	else
504		(void)printf(" %7qd", np->n_size);
505	return (0);
506}
507
508/*
509 * Given a pointer to a mount structure in kernel space,
510 * read it in and return a usable pointer to it.
511 */
512struct mount *
513getmnt(maddr)
514	struct mount *maddr;
515{
516	static struct mtab {
517		struct mtab *next;
518		struct mount *maddr;
519		struct mount mount;
520	} *mhead = NULL;
521	struct mtab *mt;
522
523	for (mt = mhead; mt != NULL; mt = mt->next)
524		if (maddr == mt->maddr)
525			return (&mt->mount);
526	if ((mt = malloc(sizeof(struct mtab))) == NULL)
527		err(1, "malloc: mount table");
528	KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
529	mt->maddr = maddr;
530	mt->next = mhead;
531	mhead = mt;
532	return (&mt->mount);
533}
534
535void
536mount_print(mp)
537	struct mount *mp;
538{
539	int flags;
540
541#define ST	mp->mnt_stat
542	(void)printf("*** MOUNT ");
543	(void)printf("%.*s %s on %s", MFSNAMELEN, ST.f_fstypename,
544	    ST.f_mntfromname, ST.f_mntonname);
545	if ((flags = mp->mnt_flag)) {
546		char *comma = "(";
547
548		putchar(' ');
549		/* user visable flags */
550		if (flags & MNT_RDONLY) {
551			(void)printf("%srdonly", comma);
552			flags &= ~MNT_RDONLY;
553			comma = ",";
554		}
555		if (flags & MNT_SYNCHRONOUS) {
556			(void)printf("%ssynchronous", comma);
557			flags &= ~MNT_SYNCHRONOUS;
558			comma = ",";
559		}
560		if (flags & MNT_NOEXEC) {
561			(void)printf("%snoexec", comma);
562			flags &= ~MNT_NOEXEC;
563			comma = ",";
564		}
565		if (flags & MNT_NOSUID) {
566			(void)printf("%snosuid", comma);
567			flags &= ~MNT_NOSUID;
568			comma = ",";
569		}
570		if (flags & MNT_NODEV) {
571			(void)printf("%snodev", comma);
572			flags &= ~MNT_NODEV;
573			comma = ",";
574		}
575		if (flags & MNT_UNION) {
576			(void)printf("%sunion", comma);
577			flags &= ~MNT_UNION;
578			comma = ",";
579		}
580		if (flags & MNT_ASYNC) {
581			(void)printf("%sasync", comma);
582			flags &= ~MNT_ASYNC;
583			comma = ",";
584		}
585		if (flags & MNT_EXRDONLY) {
586			(void)printf("%sexrdonly", comma);
587			flags &= ~MNT_EXRDONLY;
588			comma = ",";
589		}
590		if (flags & MNT_EXPORTED) {
591			(void)printf("%sexport", comma);
592			flags &= ~MNT_EXPORTED;
593			comma = ",";
594		}
595		if (flags & MNT_DEFEXPORTED) {
596			(void)printf("%sdefdexported", comma);
597			flags &= ~MNT_DEFEXPORTED;
598			comma = ",";
599		}
600		if (flags & MNT_EXPORTANON) {
601			(void)printf("%sexportanon", comma);
602			flags &= ~MNT_EXPORTANON;
603			comma = ",";
604		}
605		if (flags & MNT_EXKERB) {
606			(void)printf("%sexkerb", comma);
607			flags &= ~MNT_EXKERB;
608			comma = ",";
609		}
610		if (flags & MNT_LOCAL) {
611			(void)printf("%slocal", comma);
612			flags &= ~MNT_LOCAL;
613			comma = ",";
614		}
615		if (flags & MNT_QUOTA) {
616			(void)printf("%squota", comma);
617			flags &= ~MNT_QUOTA;
618			comma = ",";
619		}
620		if (flags & MNT_ROOTFS) {
621			(void)printf("%srootfs", comma);
622			flags &= ~MNT_ROOTFS;
623			comma = ",";
624		}
625		if (flags & MNT_NOATIME) {
626			(void)printf("%snoatime", comma);
627			flags &= ~MNT_NOATIME;
628			comma = ",";
629		}
630		/* filesystem control flags */
631		if (flags & MNT_UPDATE) {
632			(void)printf("%supdate", comma);
633			flags &= ~MNT_UPDATE;
634			comma = ",";
635		}
636		if (flags & MNT_DELEXPORT) {
637			(void)printf("%sdelexport", comma);
638			flags &= ~MNT_DELEXPORT;
639			comma = ",";
640		}
641		if (flags & MNT_RELOAD) {
642			(void)printf("%sreload", comma);
643			flags &= ~MNT_RELOAD;
644			comma = ",";
645		}
646		if (flags & MNT_FORCE) {
647			(void)printf("%sforce", comma);
648			flags &= ~MNT_FORCE;
649			comma = ",";
650		}
651		if (flags & MNT_WANTRDWR) {
652			(void)printf("%swantrdwr", comma);
653			flags &= ~MNT_WANTRDWR;
654			comma = ",";
655		}
656		if (flags & MNT_SOFTDEP) {
657			(void)printf("%ssoftdep", comma);
658			flags &= ~MNT_SOFTDEP;
659			comma = ",";
660		}
661		if (flags)
662			(void)printf("%sunknown_flags:%x", comma, flags);
663		(void)printf(")");
664	}
665	(void)printf("\n");
666#undef ST
667}
668
669struct e_vnode *
670loadvnodes(avnodes)
671	int *avnodes;
672{
673	int mib[2];
674	size_t copysize;
675	struct e_vnode *vnodebase;
676
677	if (memf != NULL) {
678		/*
679		 * do it by hand
680		 */
681		return (kinfo_vnodes(avnodes));
682	}
683	mib[0] = CTL_KERN;
684	mib[1] = KERN_VNODE;
685	if (sysctl(mib, 2, NULL, &copysize, NULL, 0) == -1)
686		err(1, "sysctl: KERN_VNODE");
687	if ((vnodebase = malloc(copysize)) == NULL)
688		err(1, "malloc: vnode table");
689	if (sysctl(mib, 2, vnodebase, &copysize, NULL, 0) == -1)
690		err(1, "sysctl: KERN_VNODE");
691	if (copysize % sizeof(struct e_vnode))
692		errx(1, "vnode size mismatch");
693	*avnodes = copysize / sizeof(struct e_vnode);
694
695	return (vnodebase);
696}
697
698/*
699 * simulate what a running kernel does in in kinfo_vnode
700 */
701struct e_vnode *
702kinfo_vnodes(avnodes)
703	int *avnodes;
704{
705	struct mntlist mountlist;
706	struct mount *mp, mount;
707	struct vnode *vp, vnode;
708	char *vbuf, *evbuf, *bp;
709	int mib[2], numvnodes;
710	size_t num;
711
712#define VPTRSZ  sizeof(struct vnode *)
713#define VNODESZ sizeof(struct vnode)
714
715	if (kd == 0) {
716		mib[0] = CTL_KERN;
717		mib[1] = KERN_NUMVNODES;
718		num = sizeof(numvnodes);
719		if (sysctl(mib, 2, &numvnodes, &num, NULL, 0) < 0)
720			err(1, "sysctl(KERN_NUMVNODES) failed");
721	} else
722		KGET(V_NUMV, numvnodes);
723	if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
724		err(1, "malloc: vnode buffer");
725	bp = vbuf;
726	evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
727	KGET(V_MOUNTLIST, mountlist);
728	for (num = 0, mp = mountlist.cqh_first; ; mp = mount.mnt_list.cqe_next) {
729		KGET2(mp, &mount, sizeof(mount), "mount entry");
730		for (vp = mount.mnt_vnodelist.lh_first;
731		    vp != NULL; vp = vnode.v_mntvnodes.le_next) {
732			KGET2(vp, &vnode, sizeof(vnode), "vnode");
733			if ((bp + VPTRSZ + VNODESZ) > evbuf)
734				/* XXX - should realloc */
735				errx(1, "no more room for vnodes");
736			memmove(bp, &vp, VPTRSZ);
737			bp += VPTRSZ;
738			memmove(bp, &vnode, VNODESZ);
739			bp += VNODESZ;
740			num++;
741		}
742		if (mp == mountlist.cqh_last)
743			break;
744	}
745	*avnodes = num;
746	return ((struct e_vnode *)vbuf);
747}
748
749char hdr[]="   LINE RAW  CAN  OUT  HWT LWT    COL STATE      SESS  PGID DISC\n";
750
751void
752ttymode()
753{
754	struct ttylist_head tty_head;
755	struct tty *tp, tty;
756	int mib[2], ntty;
757	size_t nlen;
758
759	if (kd == 0) {
760		mib[0] = CTL_KERN;
761		mib[1] = KERN_TTYCOUNT;
762		nlen = sizeof(ntty);
763		if (sysctl(mib, 2, &ntty, &nlen, NULL, 0) < 0)
764			err(1, "sysctl(KERN_TTYCOUNT) failed");
765	} else
766		KGET(TTY_NTTY, ntty);
767	(void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s");
768	KGET(TTY_TTYLIST, tty_head);
769	(void)printf(hdr);
770	for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) {
771		KGET2(tp, &tty, sizeof tty, "tty struct");
772		ttyprt(&tty);
773	}
774}
775
776struct {
777	int flag;
778	char val;
779} ttystates[] = {
780	{ TS_WOPEN,	'W'},
781	{ TS_ISOPEN,	'O'},
782	{ TS_CARR_ON,	'C'},
783	{ TS_TIMEOUT,	'T'},
784	{ TS_FLUSH,	'F'},
785	{ TS_BUSY,	'B'},
786	{ TS_ASLEEP,	'A'},
787	{ TS_XCLUDE,	'X'},
788	{ TS_TTSTOP,	'S'},
789	{ TS_TBLOCK,	'K'},
790	{ TS_ASYNC,	'Y'},
791	{ TS_BKSL,	'D'},
792	{ TS_ERASE,	'E'},
793	{ TS_LNCH,	'L'},
794	{ TS_TYPEN,	'P'},
795	{ TS_CNTTB,	'N'},
796	{ 0,	       '\0'},
797};
798
799void
800ttyprt(tp)
801	struct tty *tp;
802{
803	int i, j;
804	pid_t pgid;
805	char *name, state[20];
806
807	if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL)
808		(void)printf("%2d,%-3d   ", major(tp->t_dev), minor(tp->t_dev));
809	else
810		(void)printf("%7s ", name);
811	(void)printf("%3d %4d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
812	(void)printf("%4d %4d %3d %6d ", tp->t_outq.c_cc,
813		tp->t_hiwat, tp->t_lowat, tp->t_column);
814	for (i = j = 0; ttystates[i].flag; i++)
815		if (tp->t_state&ttystates[i].flag)
816			state[j++] = ttystates[i].val;
817	if (j == 0)
818		state[j++] = '-';
819	state[j] = '\0';
820	(void)printf("%-6s %8lx", state, (u_long)tp->t_session & ~KERNBASE);
821	pgid = 0;
822	if (tp->t_pgrp != NULL)
823		KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
824	(void)printf("%6d ", pgid);
825	switch (tp->t_line) {
826	case TTYDISC:
827		(void)printf("term\n");
828		break;
829	case TABLDISC:
830		(void)printf("tab\n");
831		break;
832	case SLIPDISC:
833		(void)printf("slip\n");
834		break;
835	case PPPDISC:
836		(void)printf("ppp\n");
837		break;
838	case STRIPDISC:
839		(void)printf("strip\n");
840		break;
841	default:
842		(void)printf("%d\n", tp->t_line);
843		break;
844	}
845}
846
847void
848filemode()
849{
850	struct file fp, *ffp;
851	struct file *addr;
852	char *buf, flagbuf[16], *fbp;
853	static char *dtypes[] = { "???", "inode", "socket" };
854	int mib[2], maxfile, nfile;
855	size_t len;
856
857	if (kd == 0) {
858		mib[0] = CTL_KERN;
859		mib[1] = KERN_MAXFILES;
860		len = sizeof(maxfile);
861		if (sysctl(mib, 2, &maxfile, &len, NULL, 0) < 0)
862			err(1, "sysctl(KERN_MAXFILES) failed");
863		if (totalflag) {
864			mib[0] = CTL_KERN;
865			mib[1] = KERN_NFILES;
866			len = sizeof(nfile);
867			if (sysctl(mib, 2, &nfile, &len, NULL, 0) < 0)
868				err(1, "sysctl(KERN_NFILES) failed");
869		}
870	} else {
871		KGET(FNL_MAXFILE, maxfile);
872		if (totalflag) {
873			KGET(FNL_NFILE, nfile);
874			(void)printf("%3d/%3d files\n", nfile, maxfile);
875			return;
876		}
877	}
878
879	if (getfiles(&buf, &len) == -1)
880		return;
881	/*
882	 * Getfiles returns in malloc'd memory a pointer to the first file
883	 * structure, and then an array of file structs (whose addresses are
884	 * derivable from the previous entry).
885	 */
886	addr = ((struct filelist *)buf)->lh_first;
887	ffp = (struct file *)(buf + sizeof(struct filelist));
888	nfile = (len - sizeof(struct filelist)) / sizeof(struct file);
889
890	(void)printf("%d/%d open files\n", nfile, maxfile);
891
892	(void)printf("%*s TYPE       FLG  CNT  MSG  %*s  OFFSET\n",
893	    8, "LOC", 8, "DATA");
894	for (; (char *)ffp < buf + len; addr = ffp->f_list.le_next, ffp++) {
895		memmove(&fp, ffp, sizeof fp);
896		if ((unsigned)fp.f_type > DTYPE_SOCKET)
897			continue;
898		(void)printf("%lx ", (long)addr);
899		(void)printf("%-8.8s", dtypes[fp.f_type]);
900		fbp = flagbuf;
901		if (fp.f_flag & FREAD)
902			*fbp++ = 'R';
903		if (fp.f_flag & FWRITE)
904			*fbp++ = 'W';
905		if (fp.f_flag & FAPPEND)
906			*fbp++ = 'A';
907#ifdef FSHLOCK	/* currently gone */
908		if (fp.f_flag & FSHLOCK)
909			*fbp++ = 'S';
910		if (fp.f_flag & FEXLOCK)
911			*fbp++ = 'X';
912#endif
913		if (fp.f_flag & FASYNC)
914			*fbp++ = 'I';
915		*fbp = '\0';
916		(void)printf("%6s  %3ld", flagbuf, fp.f_count);
917		(void)printf("  %3ld", fp.f_msgcount);
918		(void)printf("  %8.1lx", (long)fp.f_data);
919
920		if (fp.f_offset < 0)
921			(void)printf("  %llx\n", (long long)fp.f_offset);
922		else
923			(void)printf("  %lld\n", (long long)fp.f_offset);
924	}
925	free(buf);
926}
927
928int
929getfiles(abuf, alen)
930	char **abuf;
931	size_t *alen;
932{
933	size_t len;
934	int mib[2];
935	char *buf;
936
937	/*
938	 * XXX
939	 * Add emulation of KINFO_FILE here.
940	 */
941	if (memf != NULL)
942		errx(1, "files on dead kernel, not implemented");
943
944	mib[0] = CTL_KERN;
945	mib[1] = KERN_FILE;
946	if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
947		warn("sysctl: KERN_FILE");
948		return (-1);
949	}
950	if ((buf = malloc(len)) == NULL)
951		err(1, "malloc: KERN_FILE");
952	if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
953		warn("sysctl: KERN_FILE");
954		return (-1);
955	}
956	*abuf = buf;
957	*alen = len;
958	return (0);
959}
960
961/*
962 * swapmode is based on a program called swapinfo written
963 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
964 */
965void
966swapmode()
967{
968	char *header;
969	int hlen = 10, nswap, rnswap;
970	int div, i, avail, nfree, npfree, used;
971	long blocksize;
972	struct swapent *swdev;
973
974	if (kflag) {
975		header = "1K-blocks";
976		blocksize = 1024;
977		hlen = strlen(header);
978	} else
979		header = getbsize(&hlen, &blocksize);
980
981	nswap = swapctl(SWAP_NSWAP, 0, 0);
982	if (nswap == 0) {
983		if (!totalflag)
984			(void)printf("%-11s %*s %8s %8s %8s  %s\n",
985			    "Device", hlen, header,
986			    "Used", "Avail", "Capacity", "Priority");
987		(void)printf("%-11s %*d %8d %8d %5.0f%%\n",
988		    "Total", hlen, 0, 0, 0, 0.0);
989		return;
990	}
991	if ((swdev = malloc(nswap * sizeof(*swdev))) == NULL)
992		err(1, "malloc");
993	rnswap = swapctl(SWAP_STATS, swdev, nswap);
994
995	if (!totalflag)
996		(void)printf("%-11s %*s %8s %8s %8s  %s\n",
997		    "Device", hlen, header,
998		    "Used", "Avail", "Capacity", "Priority");
999
1000	/* Run through swap list, doing the funky monkey. */
1001	div = blocksize / DEV_BSIZE;
1002	avail = nfree = npfree = 0;
1003	for (i = 0; i < nswap; i++) {
1004		int xsize, xfree;
1005
1006		if (!(swdev[i].se_flags & SWF_ENABLE))
1007			continue;
1008
1009		if (!totalflag) {
1010			if (usenumflag)
1011				(void)printf("%2d,%-2d       %*d ",
1012				    major(swdev[i].se_dev),
1013				    minor(swdev[i].se_dev),
1014				    hlen, swdev[i].se_nblks / div);
1015			else
1016				(void)printf("%-11s %*d ", swdev[i].se_path,
1017				    hlen, swdev[i].se_nblks / div);
1018		}
1019
1020		xsize = swdev[i].se_nblks;
1021		used = swdev[i].se_inuse;
1022		xfree = xsize - used;
1023		nfree += (xsize - used);
1024		npfree++;
1025		avail += xsize;
1026		if (totalflag)
1027			continue;
1028		(void)printf("%8d %8d %5.0f%%    %d\n",
1029		    used / div, xfree / div,
1030		    (double)used / (double)xsize * 100.0,
1031		    swdev[i].se_priority);
1032	}
1033
1034	/*
1035	 * If only one partition has been set up via swapon(8), we don't
1036	 * need to bother with totals.
1037	 */
1038	used = avail - nfree;
1039	if (totalflag) {
1040		(void)printf("%dM/%dM swap space\n",
1041		    used / (1048576 / DEV_BSIZE),
1042		    avail / (1048576 / DEV_BSIZE));
1043		return;
1044	}
1045	if (npfree > 1) {
1046		(void)printf("%-11s %*d %8d %8d %5.0f%%\n",
1047		    "Total", hlen, avail / div, used / div, nfree / div,
1048		    (double)used / (double)avail * 100.0);
1049	}
1050
1051	free(swdev);
1052}
1053
1054void
1055usage()
1056{
1057	(void)fprintf(stderr,
1058	    "usage: pstat [-Tfknstv] [-M core] [-N system]\n");
1059	exit(1);
1060}
1061