pstat.c revision 1.4
1/*	$OpenBSD: pstat.c,v 1.4 1996/05/05 16:15:48 deraadt Exp $	*/
2/*	$NetBSD: pstat.c,v 1.19 1996/05/02 00:13:19 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";
46from: static char *rcsid = "$NetBSD: pstat.c,v 1.19 1996/05/02 00:13:19 cgd Exp $";
47#else
48static char *rcsid = "$OpenBSD: pstat.c,v 1.4 1996/05/05 16:15:48 deraadt Exp $";
49#endif
50#endif /* not lint */
51
52#include <sys/param.h>
53#include <sys/time.h>
54#include <sys/vnode.h>
55#include <sys/map.h>
56#include <sys/ucred.h>
57#define _KERNEL
58#include <sys/file.h>
59#include <ufs/ufs/quota.h>
60#include <ufs/ufs/inode.h>
61#define NFS
62#include <sys/mount.h>
63#undef NFS
64#undef _KERNEL
65#include <sys/stat.h>
66#include <nfs/nfsproto.h>
67#include <nfs/rpcv2.h>
68#include <nfs/nfsnode.h>
69#include <sys/ioctl.h>
70#include <sys/tty.h>
71#include <sys/conf.h>
72#include <sys/device.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 <stdio.h>
81#include <stdlib.h>
82#include <string.h>
83#include <unistd.h>
84
85struct nlist nl[] = {
86#define VM_SWAPMAP	0
87	{ "_swapmap" },	/* list of free swap areas */
88#define VM_NSWAPMAP	1
89	{ "_nswapmap" },/* size of the swap map */
90#define VM_SWDEVT	2
91	{ "_swdevt" },	/* list of swap devices and sizes */
92#define VM_NSWAP	3
93	{ "_nswap" },	/* size of largest swap device */
94#define VM_NSWDEV	4
95	{ "_nswdev" },	/* number of swap devices */
96#define VM_DMMAX	5
97	{ "_dmmax" },	/* maximum size of a swap block */
98#define	V_MOUNTLIST	6
99	{ "_mountlist" },	/* address of head of mount list. */
100#define V_NUMV		7
101	{ "_numvnodes" },
102#define	FNL_NFILE	8
103	{"_nfiles"},
104#define FNL_MAXFILE	9
105	{"_maxfiles"},
106#define NLMANDATORY FNL_MAXFILE	/* names up to here are mandatory */
107#define VM_NISWAP	NLMANDATORY + 1
108	{ "_niswap" },
109#define VM_NISWDEV	NLMANDATORY + 2
110	{ "_niswdev" },
111#define	SCONS		NLMANDATORY + 3
112	{ "_constty" },
113#define	SPTY		NLMANDATORY + 4
114	{ "_pt_tty" },
115#define	SNPTY		NLMANDATORY + 5
116	{ "_npty" },
117
118#ifdef sparc
119#define SZS	(SNPTY+1)
120	{ "_zs_tty" },
121#define SCZS	(SNPTY+2)
122	{ "_zscd" },
123#endif
124
125#ifdef hp300
126#define	SDCA	(SNPTY+1)
127	{ "_dca_tty" },
128#define	SNDCA	(SNPTY+2)
129	{ "_ndca" },
130#define	SDCM	(SNPTY+3)
131	{ "_dcm_tty" },
132#define	SNDCM	(SNPTY+4)
133	{ "_ndcm" },
134#define	SDCL	(SNPTY+5)
135	{ "_dcl_tty" },
136#define	SNDCL	(SNPTY+6)
137	{ "_ndcl" },
138#define	SITE	(SNPTY+7)
139	{ "_ite_tty" },
140#define	SNITE	(SNPTY+8)
141	{ "_nite" },
142#endif
143
144#ifdef mips
145#define SDC	(SNPTY+1)
146	{ "_dc_tty" },
147#define SNDC	(SNPTY+2)
148	{ "_dc_cnt" },
149#endif
150
151#ifdef i386
152#define SPC	(SNPTY+1)
153	{ "_pc_tty" },
154#define SCPC	(SNPTY+2)
155	{ "_pccd" },
156#define SCOM	(SNPTY+3)
157	{ "_com_tty" },
158#define SCCOM	(SNPTY+4)
159	{ "_comcd" },
160#endif
161#ifdef amiga
162#define SSER	(SNPTY + 1)
163	{ "_ser_tty" },
164#define SCSER	(SNPTY + 2)
165	{ "_sercd" },
166#define SITE	(SNPTY + 3)
167	{ "_ite_tty" },
168#define SCITE	(SNPTY + 4)
169	{ "_itecd" },
170#define SMFCS	(SNPTY + 5)
171	{ "_mfcs_tty" },
172#define SCMFCS	(SNPTY + 6)
173	{ "_mfcscd" },
174#endif
175
176	{ "" }
177};
178
179int	usenumflag;
180int	totalflag;
181int	kflag;
182char	*nlistf	= NULL;
183char	*memf	= NULL;
184kvm_t	*kd;
185
186#define	SVAR(var) __STRING(var)	/* to force expansion */
187#define	KGET(idx, var)							\
188	KGET1(idx, &var, sizeof(var), SVAR(var))
189#define	KGET1(idx, p, s, msg)						\
190	KGET2(nl[idx].n_value, p, s, msg)
191#define	KGET2(addr, p, s, msg)						\
192	if (kvm_read(kd, (u_long)(addr), p, s) != s)			\
193		warnx("cannot read %s: %s", msg, kvm_geterr(kd))
194#define	KGETRET(addr, p, s, msg)					\
195	if (kvm_read(kd, (u_long)(addr), p, s) != s) {			\
196		warnx("cannot read %s: %s", msg, kvm_geterr(kd));	\
197		return (0);						\
198	}
199
200void	filemode __P((void));
201int	getfiles __P((char **, int *));
202struct mount *
203	getmnt __P((struct mount *));
204struct e_vnode *
205	kinfo_vnodes __P((int *));
206struct e_vnode *
207	loadvnodes __P((int *));
208void	mount_print __P((struct mount *));
209void	nfs_header __P((void));
210int	nfs_print __P((struct vnode *));
211void	swapmode __P((void));
212void	ttymode __P((void));
213void	ttyprt __P((struct tty *, int));
214void	ttytype __P((char *, int, int));
215void	ttytype_newcf __P((char *, int, int));
216void	ttytype_oldcf __P((char *, int, int));
217void	ufs_header __P((void));
218int	ufs_print __P((struct vnode *));
219void	usage __P((void));
220void	vnode_header __P((void));
221void	vnode_print __P((struct vnode *, struct vnode *));
222void	vnodemode __P((void));
223
224int
225main(argc, argv)
226	int argc;
227	char *argv[];
228{
229	extern char *optarg;
230	extern int optind;
231	int ch, i, quit, ret;
232	int fileflag, swapflag, ttyflag, vnodeflag;
233	char buf[_POSIX2_LINE_MAX];
234
235	fileflag = swapflag = ttyflag = vnodeflag = 0;
236	while ((ch = getopt(argc, argv, "TM:N:fiknstv")) != -1)
237		switch (ch) {
238		case 'f':
239			fileflag = 1;
240			break;
241		case 'M':
242			memf = optarg;
243			break;
244		case 'N':
245			nlistf = optarg;
246			break;
247		case 'n':
248			usenumflag = 1;
249			break;
250		case 's':
251			swapflag = 1;
252			break;
253		case 'T':
254			totalflag = 1;
255			break;
256		case 't':
257			ttyflag = 1;
258			break;
259		case 'k':
260			kflag = 1;
261			break;
262		case 'v':
263		case 'i':		/* Backward compatibility. */
264			vnodeflag = 1;
265			break;
266		default:
267			usage();
268		}
269	argc -= optind;
270	argv += optind;
271
272	/*
273	 * Discard setgid privileges if not the running kernel so that bad
274	 * guys can't print interesting stuff from kernel memory.
275	 */
276	if (nlistf != NULL || memf != NULL)
277		(void)setgid(getgid());
278
279	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
280		errx(1, "kvm_openfiles: %s", buf);
281	if ((ret = kvm_nlist(kd, nl)) != 0) {
282		if (ret == -1)
283			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
284		for (i = quit = 0; i <= NLMANDATORY; i++)
285			if (!nl[i].n_value) {
286				quit = 1;
287				warnx("undefined symbol: %s\n", nl[i].n_name);
288			}
289		if (quit)
290			exit(1);
291	}
292	if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
293		usage();
294	if (fileflag || totalflag)
295		filemode();
296	if (vnodeflag || totalflag)
297		vnodemode();
298	if (ttyflag)
299		ttymode();
300	if (swapflag || totalflag)
301		swapmode();
302	exit (0);
303}
304
305struct e_vnode {
306	struct vnode *avnode;
307	struct vnode vnode;
308};
309
310void
311vnodemode()
312{
313	register struct e_vnode *e_vnodebase, *endvnode, *evp;
314	register struct vnode *vp;
315	register struct mount *maddr, *mp;
316	int numvnodes;
317
318	e_vnodebase = loadvnodes(&numvnodes);
319	if (totalflag) {
320		(void)printf("%7d vnodes\n", numvnodes);
321		return;
322	}
323	endvnode = e_vnodebase + numvnodes;
324	(void)printf("%d active vnodes\n", numvnodes);
325
326
327#define ST	mp->mnt_stat
328	maddr = NULL;
329	for (evp = e_vnodebase; evp < endvnode; evp++) {
330		vp = &evp->vnode;
331		if (vp->v_mount != maddr) {
332			/*
333			 * New filesystem
334			 */
335			if ((mp = getmnt(vp->v_mount)) == NULL)
336				continue;
337			maddr = vp->v_mount;
338			mount_print(mp);
339			vnode_header();
340			if (!strncmp(ST.f_fstypename, MOUNT_FFS, MFSNAMELEN) ||
341			    !strncmp(ST.f_fstypename, MOUNT_MFS, MFSNAMELEN)) {
342				ufs_header();
343			} else if (!strncmp(ST.f_fstypename, MOUNT_NFS,
344			    MFSNAMELEN)) {
345				nfs_header();
346			}
347			(void)printf("\n");
348		}
349		vnode_print(evp->avnode, vp);
350		if (!strncmp(ST.f_fstypename, MOUNT_FFS, MFSNAMELEN) ||
351		    !strncmp(ST.f_fstypename, MOUNT_MFS, MFSNAMELEN)) {
352			ufs_print(vp);
353		} else if (!strncmp(ST.f_fstypename, MOUNT_NFS, MFSNAMELEN)) {
354			nfs_print(vp);
355		}
356		(void)printf("\n");
357	}
358	free(e_vnodebase);
359}
360
361void
362vnode_header()
363{
364	(void)printf("ADDR     TYP VFLAG  USE HOLD");
365}
366
367void
368vnode_print(avnode, vp)
369	struct vnode *avnode;
370	struct vnode *vp;
371{
372	char *type, flags[16];
373	char *fp = flags;
374	register int flag;
375
376	/*
377	 * set type
378	 */
379	switch(vp->v_type) {
380	case VNON:
381		type = "non"; break;
382	case VREG:
383		type = "reg"; break;
384	case VDIR:
385		type = "dir"; break;
386	case VBLK:
387		type = "blk"; break;
388	case VCHR:
389		type = "chr"; break;
390	case VLNK:
391		type = "lnk"; break;
392	case VSOCK:
393		type = "soc"; break;
394	case VFIFO:
395		type = "fif"; break;
396	case VBAD:
397		type = "bad"; break;
398	default:
399		type = "unk"; break;
400	}
401	/*
402	 * gather flags
403	 */
404	flag = vp->v_flag;
405	if (flag & VROOT)
406		*fp++ = 'R';
407	if (flag & VTEXT)
408		*fp++ = 'T';
409	if (flag & VSYSTEM)
410		*fp++ = 'S';
411	if (flag & VISTTY)
412		*fp++ = 'I';
413	if (flag & VXLOCK)
414		*fp++ = 'L';
415	if (flag & VXWANT)
416		*fp++ = 'W';
417	if (flag & VBWAIT)
418		*fp++ = 'B';
419	if (flag & VALIASED)
420		*fp++ = 'A';
421	if (flag == 0)
422		*fp++ = '-';
423	*fp = '\0';
424	(void)printf("%8x %s %5s %4d %4d",
425	    avnode, type, flags, vp->v_usecount, vp->v_holdcnt);
426}
427
428void
429ufs_header()
430{
431	(void)printf(" FILEID IFLAG RDEV|SZ");
432}
433
434int
435ufs_print(vp)
436	struct vnode *vp;
437{
438	register int flag;
439	struct inode inode, *ip = &inode;
440	char flagbuf[16], *flags = flagbuf;
441	char *name;
442	mode_t type;
443
444	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
445	flag = ip->i_flag;
446	if (flag & IN_LOCKED)
447		*flags++ = 'L';
448	if (flag & IN_WANTED)
449		*flags++ = 'W';
450	if (flag & IN_RENAME)
451		*flags++ = 'R';
452	if (flag & IN_UPDATE)
453		*flags++ = 'U';
454	if (flag & IN_ACCESS)
455		*flags++ = 'A';
456	if (flag & IN_CHANGE)
457		*flags++ = 'C';
458	if (flag & IN_MODIFIED)
459		*flags++ = 'M';
460	if (flag & IN_SHLOCK)
461		*flags++ = 'S';
462	if (flag & IN_EXLOCK)
463		*flags++ = 'E';
464	if (flag & IN_LWAIT)
465		*flags++ = 'Z';
466	if (flag == 0)
467		*flags++ = '-';
468	*flags = '\0';
469
470	(void)printf(" %6d %5s", ip->i_number, flagbuf);
471	type = ip->i_mode & S_IFMT;
472	if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
473		if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL))
474			(void)printf("   %2d,%-2d",
475			    major(ip->i_rdev), minor(ip->i_rdev));
476		else
477			(void)printf(" %7s", name);
478	else
479		(void)printf(" %7qd", ip->i_size);
480	return (0);
481}
482
483void
484nfs_header()
485{
486	(void)printf(" FILEID NFLAG RDEV|SZ");
487}
488
489int
490nfs_print(vp)
491	struct vnode *vp;
492{
493	struct nfsnode nfsnode, *np = &nfsnode;
494	char flagbuf[16], *flags = flagbuf;
495	register int flag;
496	char *name;
497	mode_t type;
498
499	KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
500	flag = np->n_flag;
501	if (flag & NFLUSHWANT)
502		*flags++ = 'W';
503	if (flag & NFLUSHINPROG)
504		*flags++ = 'P';
505	if (flag & NMODIFIED)
506		*flags++ = 'M';
507	if (flag & NWRITEERR)
508		*flags++ = 'E';
509	if (flag & NQNFSNONCACHE)
510		*flags++ = 'X';
511	if (flag & NQNFSWRITE)
512		*flags++ = 'O';
513	if (flag & NQNFSEVICTED)
514		*flags++ = 'G';
515	if (flag == 0)
516		*flags++ = '-';
517	*flags = '\0';
518
519#define VT	np->n_vattr
520	(void)printf(" %6d %5s", VT.va_fileid, flagbuf);
521	type = VT.va_mode & S_IFMT;
522	if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode))
523		if (usenumflag || ((name = devname(VT.va_rdev, type)) == NULL))
524			(void)printf("   %2d,%-2d",
525			    major(VT.va_rdev), minor(VT.va_rdev));
526		else
527			(void)printf(" %7s", name);
528	else
529		(void)printf(" %7qd", np->n_size);
530	return (0);
531}
532
533/*
534 * Given a pointer to a mount structure in kernel space,
535 * read it in and return a usable pointer to it.
536 */
537struct mount *
538getmnt(maddr)
539	struct mount *maddr;
540{
541	static struct mtab {
542		struct mtab *next;
543		struct mount *maddr;
544		struct mount mount;
545	} *mhead = NULL;
546	register struct mtab *mt;
547
548	for (mt = mhead; mt != NULL; mt = mt->next)
549		if (maddr == mt->maddr)
550			return (&mt->mount);
551	if ((mt = malloc(sizeof(struct mtab))) == NULL)
552		err(1, NULL);
553	KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
554	mt->maddr = maddr;
555	mt->next = mhead;
556	mhead = mt;
557	return (&mt->mount);
558}
559
560void
561mount_print(mp)
562	struct mount *mp;
563{
564	register int flags;
565	char *type;
566
567#define ST	mp->mnt_stat
568	(void)printf("*** MOUNT ");
569	(void)printf("%.*s %s on %s", MFSNAMELEN, ST.f_fstypename,
570	    ST.f_mntfromname, ST.f_mntonname);
571	if (flags = mp->mnt_flag) {
572		char *comma = "(";
573
574		putchar(' ');
575		/* user visable flags */
576		if (flags & MNT_RDONLY) {
577			(void)printf("%srdonly", comma);
578			flags &= ~MNT_RDONLY;
579			comma = ",";
580		}
581		if (flags & MNT_SYNCHRONOUS) {
582			(void)printf("%ssynchronous", comma);
583			flags &= ~MNT_SYNCHRONOUS;
584			comma = ",";
585		}
586		if (flags & MNT_NOEXEC) {
587			(void)printf("%snoexec", comma);
588			flags &= ~MNT_NOEXEC;
589			comma = ",";
590		}
591		if (flags & MNT_NOSUID) {
592			(void)printf("%snosuid", comma);
593			flags &= ~MNT_NOSUID;
594			comma = ",";
595		}
596		if (flags & MNT_NODEV) {
597			(void)printf("%snodev", comma);
598			flags &= ~MNT_NODEV;
599			comma = ",";
600		}
601		if (flags & MNT_EXPORTED) {
602			(void)printf("%sexport", comma);
603			flags &= ~MNT_EXPORTED;
604			comma = ",";
605		}
606		if (flags & MNT_EXRDONLY) {
607			(void)printf("%sexrdonly", comma);
608			flags &= ~MNT_EXRDONLY;
609			comma = ",";
610		}
611		if (flags & MNT_LOCAL) {
612			(void)printf("%slocal", comma);
613			flags &= ~MNT_LOCAL;
614			comma = ",";
615		}
616		if (flags & MNT_QUOTA) {
617			(void)printf("%squota", comma);
618			flags &= ~MNT_QUOTA;
619			comma = ",";
620		}
621		if (flags & MNT_ROOTFS) {
622			(void)printf("%srootfs", comma);
623			flags &= ~MNT_ROOTFS;
624			comma = ",";
625		}
626		/* filesystem control flags */
627		if (flags & MNT_UPDATE) {
628			(void)printf("%supdate", comma);
629			flags &= ~MNT_UPDATE;
630			comma = ",";
631		}
632		if (flags & MNT_MLOCK) {
633			(void)printf("%slock", comma);
634			flags &= ~MNT_MLOCK;
635			comma = ",";
636		}
637		if (flags & MNT_MWAIT) {
638			(void)printf("%swait", comma);
639			flags &= ~MNT_MWAIT;
640			comma = ",";
641		}
642		if (flags & MNT_MPBUSY) {
643			(void)printf("%sbusy", comma);
644			flags &= ~MNT_MPBUSY;
645			comma = ",";
646		}
647		if (flags & MNT_MPWANT) {
648			(void)printf("%swant", comma);
649			flags &= ~MNT_MPWANT;
650			comma = ",";
651		}
652		if (flags & MNT_UNMOUNT) {
653			(void)printf("%sunmount", comma);
654			flags &= ~MNT_UNMOUNT;
655			comma = ",";
656		}
657		if (flags)
658			(void)printf("%sunknown_flags:%x", comma, flags);
659		(void)printf(")");
660	}
661	(void)printf("\n");
662#undef ST
663}
664
665struct e_vnode *
666loadvnodes(avnodes)
667	int *avnodes;
668{
669	int mib[2];
670	size_t copysize;
671	struct e_vnode *vnodebase;
672
673	if (memf != NULL) {
674		/*
675		 * do it by hand
676		 */
677		return (kinfo_vnodes(avnodes));
678	}
679	mib[0] = CTL_KERN;
680	mib[1] = KERN_VNODE;
681	if (sysctl(mib, 2, NULL, &copysize, NULL, 0) == -1)
682		err(1, "sysctl: KERN_VNODE");
683	if ((vnodebase = malloc(copysize)) == NULL)
684		err(1, NULL);
685	if (sysctl(mib, 2, vnodebase, &copysize, NULL, 0) == -1)
686		err(1, "sysctl: KERN_VNODE");
687	if (copysize % sizeof(struct e_vnode))
688		errx(1, "vnode size mismatch");
689	*avnodes = copysize / sizeof(struct e_vnode);
690
691	return (vnodebase);
692}
693
694/*
695 * simulate what a running kernel does in in kinfo_vnode
696 */
697struct e_vnode *
698kinfo_vnodes(avnodes)
699	int *avnodes;
700{
701	struct mntlist mountlist;
702	struct mount *mp, mount;
703	struct vnode *vp, vnode;
704	char *vbuf, *evbuf, *bp;
705	int num, numvnodes;
706
707#define VPTRSZ  sizeof(struct vnode *)
708#define VNODESZ sizeof(struct vnode)
709
710	KGET(V_NUMV, numvnodes);
711	if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
712		err(1, NULL);
713	bp = vbuf;
714	evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
715	KGET(V_MOUNTLIST, mountlist);
716	for (num = 0, mp = mountlist.cqh_first; ; mp = mount.mnt_list.cqe_next) {
717		KGET2(mp, &mount, sizeof(mount), "mount entry");
718		for (vp = mount.mnt_vnodelist.lh_first;
719		    vp != NULL; vp = vnode.v_mntvnodes.le_next) {
720			KGET2(vp, &vnode, sizeof(vnode), "vnode");
721			if ((bp + VPTRSZ + VNODESZ) > evbuf)
722				/* XXX - should realloc */
723				errx(1, "no more room for vnodes");
724			memmove(bp, &vp, VPTRSZ);
725			bp += VPTRSZ;
726			memmove(bp, &vnode, VNODESZ);
727			bp += VNODESZ;
728			num++;
729		}
730		if (mp == mountlist.cqh_last)
731			break;
732	}
733	*avnodes = num;
734	return ((struct e_vnode *)vbuf);
735}
736
737char hdr[]="  LINE  RAW  CAN  OUT  HWT LWT    COL STATE    SESS  PGID DISC\n";
738
739void
740ttymode()
741{
742
743#ifdef sparc
744	ttytype("console", SCONS, 1);
745	ttytype_newcf("zs", SZS, SCZS);
746#endif
747
748#ifdef vax
749	/* May fill in this later */
750#endif
751#ifdef tahoe
752	if (nl[SNVX].n_type != 0)
753		ttytype_oldcf("vx", SVX, SNVX);
754	if (nl[SNMP].n_type != 0)
755		ttytype_oldcf("mp", SMP, SNMP);
756#endif
757#ifdef hp300
758	if (nl[SNITE].n_type != 0)
759		ttytype_oldcf("ite", SITE, SNITE);
760	if (nl[SNDCA].n_type != 0)
761		ttytype_oldcf("dca", SDCA, SNDCA);
762	if (nl[SNDCM].n_type != 0)
763		ttytype_oldcf("dcm", SDCM, SNDCM);
764	if (nl[SNDCL].n_type != 0)
765		ttytype_oldcf("dcl", SDCL, SNDCL);
766#endif
767#ifdef mips
768	if (nl[SNDC].n_type != 0)
769		ttytype_oldcf("dc", SDC, SNDC);
770#endif
771#ifdef i386
772	if (nl[SCPC].n_type != 0)
773		ttytype_newcf("pc", SPC, SCPC);
774	if (nl[SCCOM].n_type != 0)
775		ttytype_newcf("com", SCOM, SCCOM);
776#endif
777#ifdef amiga
778	if (nl[SCSER].n_type != 0)
779		ttytype_newcf("ser", SSER, SCSER);
780	if (nl[SCITE].n_type != 0)
781		ttytype_newcf("ite", SITE, SCITE);
782	if (nl[SCMFCS].n_type != 0)
783		ttytype_newcf("mfcs", SMFCS, SCMFCS);
784#endif
785	if (nl[SNPTY].n_type != 0)
786		ttytype_oldcf("pty", SPTY, SNPTY);
787}
788
789void
790ttytype_oldcf(name, type, number)
791	char *name;
792	int type, number;
793{
794	int ntty;
795
796	KGET(number, ntty);
797	ttytype(name, type, ntty);
798}
799
800void
801ttytype_newcf(name, type, config)
802	char *name;
803	int type, config;
804{
805	struct cfdriver cf;
806	void **cd;
807	int i;
808
809	KGET(config, cf);
810	cd = malloc(cf.cd_ndevs * sizeof(void *));
811	if (!cd)
812		return;
813	KGET2(cf.cd_devs, cd, cf.cd_ndevs * sizeof(void *), "cfdevicep");
814	for (i = cf.cd_ndevs - 1; i >= 0; --i)
815		if (cd[i])
816			break;
817	free(cd);
818	ttytype(name, type, i + 1);
819}
820
821void
822ttytype(name, type, number)
823	char *name;
824	int type, number;
825{
826	static struct tty **ttyp;
827	static int nttyp;
828	static struct tty tty;
829	int ntty = number, i;
830
831	(void)printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines");
832	if (ntty > nttyp) {
833		nttyp = ntty;
834		if ((ttyp = realloc(ttyp, nttyp * sizeof(*ttyp))) == 0)
835			err(1, NULL);
836	}
837	KGET1(type, ttyp, nttyp * sizeof(*ttyp), "tty pointers");
838	(void)printf(hdr);
839	for (i = 0; i < ntty; i++) {
840		if (ttyp[i] == NULL)
841			continue;
842		KGET2(ttyp[i], &tty, sizeof(struct tty), "tty struct");
843		ttyprt(&tty, i);
844	}
845}
846
847struct {
848	int flag;
849	char val;
850} ttystates[] = {
851	{ TS_WOPEN,	'W'},
852	{ TS_ISOPEN,	'O'},
853	{ TS_CARR_ON,	'C'},
854	{ TS_TIMEOUT,	'T'},
855	{ TS_FLUSH,	'F'},
856	{ TS_BUSY,	'B'},
857	{ TS_ASLEEP,	'A'},
858	{ TS_XCLUDE,	'X'},
859	{ TS_TTSTOP,	'S'},
860	{ TS_TBLOCK,	'K'},
861	{ TS_ASYNC,	'Y'},
862	{ TS_BKSL,	'D'},
863	{ TS_ERASE,	'E'},
864	{ TS_LNCH,	'L'},
865	{ TS_TYPEN,	'P'},
866	{ TS_CNTTB,	'N'},
867	{ 0,	       '\0'},
868};
869
870void
871ttyprt(tp, line)
872	register struct tty *tp;
873	int line;
874{
875	register int i, j;
876	pid_t pgid;
877	char *name, state[20];
878
879	if (usenumflag || tp->t_dev == 0 ||
880	   (name = devname(tp->t_dev, S_IFCHR)) == NULL)
881		(void)printf("%7d ", line);
882	else
883		(void)printf("%-7s ", name);
884	(void)printf("%3d %4d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
885	(void)printf("%4d %4d %3d %6d ", tp->t_outq.c_cc,
886		tp->t_hiwat, tp->t_lowat, tp->t_column);
887	for (i = j = 0; ttystates[i].flag; i++)
888		if (tp->t_state&ttystates[i].flag)
889			state[j++] = ttystates[i].val;
890	if (j == 0)
891		state[j++] = '-';
892	state[j] = '\0';
893	(void)printf("%-6s %6x", state, (u_long)tp->t_session & ~KERNBASE);
894	pgid = 0;
895	if (tp->t_pgrp != NULL)
896		KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
897	(void)printf("%6d ", pgid);
898	switch (tp->t_line) {
899	case TTYDISC:
900		(void)printf("term\n");
901		break;
902	case TABLDISC:
903		(void)printf("tab\n");
904		break;
905	case SLIPDISC:
906		(void)printf("slip\n");
907		break;
908	case PPPDISC:
909		(void)printf("ppp\n");
910		break;
911	default:
912		(void)printf("%d\n", tp->t_line);
913		break;
914	}
915}
916
917void
918filemode()
919{
920	register struct file *fp;
921	struct file *addr;
922	char *buf, flagbuf[16], *fbp;
923	int len, maxfile, nfile;
924	static char *dtypes[] = { "???", "inode", "socket" };
925
926	KGET(FNL_MAXFILE, maxfile);
927	if (totalflag) {
928		KGET(FNL_NFILE, nfile);
929		(void)printf("%3d/%3d files\n", nfile, maxfile);
930		return;
931	}
932	if (getfiles(&buf, &len) == -1)
933		return;
934	/*
935	 * Getfiles returns in malloc'd memory a pointer to the first file
936	 * structure, and then an array of file structs (whose addresses are
937	 * derivable from the previous entry).
938	 */
939	addr = ((struct filelist *)buf)->lh_first;
940	fp = (struct file *)(buf + sizeof(struct filelist));
941	nfile = (len - sizeof(struct filelist)) / sizeof(struct file);
942
943	(void)printf("%d/%d open files\n", nfile, maxfile);
944	(void)printf("   LOC   TYPE    FLG     CNT  MSG    DATA    OFFSET\n");
945	for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) {
946		if ((unsigned)fp->f_type > DTYPE_SOCKET)
947			continue;
948		(void)printf("%x ", addr);
949		(void)printf("%-8.8s", dtypes[fp->f_type]);
950		fbp = flagbuf;
951		if (fp->f_flag & FREAD)
952			*fbp++ = 'R';
953		if (fp->f_flag & FWRITE)
954			*fbp++ = 'W';
955		if (fp->f_flag & FAPPEND)
956			*fbp++ = 'A';
957#ifdef FSHLOCK	/* currently gone */
958		if (fp->f_flag & FSHLOCK)
959			*fbp++ = 'S';
960		if (fp->f_flag & FEXLOCK)
961			*fbp++ = 'X';
962#endif
963		if (fp->f_flag & FASYNC)
964			*fbp++ = 'I';
965		*fbp = '\0';
966		(void)printf("%6s  %3d", flagbuf, fp->f_count);
967		(void)printf("  %3d", fp->f_msgcount);
968		(void)printf("  %8.1x", fp->f_data);
969		if (fp->f_offset < 0)
970			(void)printf("  %qx\n", fp->f_offset);
971		else
972			(void)printf("  %qd\n", fp->f_offset);
973	}
974	free(buf);
975}
976
977int
978getfiles(abuf, alen)
979	char **abuf;
980	int *alen;
981{
982	size_t len;
983	int mib[2];
984	char *buf;
985
986	/*
987	 * XXX
988	 * Add emulation of KINFO_FILE here.
989	 */
990	if (memf != NULL)
991		errx(1, "files on dead kernel, not implemented\n");
992
993	mib[0] = CTL_KERN;
994	mib[1] = KERN_FILE;
995	if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
996		warn("sysctl: KERN_FILE");
997		return (-1);
998	}
999	if ((buf = malloc(len)) == NULL)
1000		err(1, NULL);
1001	if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
1002		warn("sysctl: KERN_FILE");
1003		return (-1);
1004	}
1005	*abuf = buf;
1006	*alen = len;
1007	return (0);
1008}
1009
1010/*
1011 * swapmode is based on a program called swapinfo written
1012 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
1013 */
1014void
1015swapmode()
1016{
1017	char *header;
1018	int hlen, nswap, nswdev, dmmax, nswapmap, niswap, niswdev;
1019	int s, e, div, i, l, avail, nfree, npfree, used;
1020	struct swdevt *sw;
1021	long blocksize, *perdev;
1022	struct map *swapmap, *kswapmap;
1023	struct mapent *mp;
1024
1025	KGET(VM_NSWAP, nswap);
1026	KGET(VM_NSWDEV, nswdev);
1027	KGET(VM_DMMAX, dmmax);
1028	KGET(VM_NSWAPMAP, nswapmap);
1029	KGET(VM_SWAPMAP, kswapmap);	/* kernel `swapmap' is a pointer */
1030	if ((sw = malloc(nswdev * sizeof(*sw))) == NULL ||
1031	    (perdev = malloc(nswdev * sizeof(*perdev))) == NULL ||
1032	    (mp = malloc(nswapmap * sizeof(*mp))) == NULL)
1033		err(1, "malloc");
1034	KGET1(VM_SWDEVT, sw, nswdev * sizeof(*sw), "swdevt");
1035	KGET2((long)kswapmap, mp, nswapmap * sizeof(*mp), "swapmap");
1036
1037	/* Supports sequential swap */
1038	if (nl[VM_NISWAP].n_value != 0) {
1039		KGET(VM_NISWAP, niswap);
1040		KGET(VM_NISWDEV, niswdev);
1041	} else {
1042		niswap = nswap;
1043		niswdev = nswdev;
1044	}
1045
1046	/* First entry in map is `struct map'; rest are mapent's. */
1047	swapmap = (struct map *)mp;
1048	if (nswapmap != swapmap->m_limit - (struct mapent *)kswapmap)
1049		errx(1, "panic: nswapmap goof");
1050
1051	/* Count up swap space. */
1052	nfree = 0;
1053	memset(perdev, 0, nswdev * sizeof(*perdev));
1054	for (mp++; mp->m_addr != 0; mp++) {
1055		s = mp->m_addr;			/* start of swap region */
1056		e = mp->m_addr + mp->m_size;	/* end of region */
1057		nfree += mp->m_size;
1058
1059		/*
1060		 * Swap space is split up among the configured disks.
1061		 *
1062		 * For interleaved swap devices, the first dmmax blocks
1063		 * of swap space some from the first disk, the next dmmax
1064		 * blocks from the next, and so on up to niswap blocks.
1065		 *
1066		 * Sequential swap devices follow the interleaved devices
1067		 * (i.e. blocks starting at niswap) in the order in which
1068		 * they appear in the swdev table.  The size of each device
1069		 * will be a multiple of dmmax.
1070		 *
1071		 * The list of free space joins adjacent free blocks,
1072		 * ignoring device boundries.  If we want to keep track
1073		 * of this information per device, we'll just have to
1074		 * extract it ourselves.  We know that dmmax-sized chunks
1075		 * cannot span device boundaries (interleaved or sequential)
1076		 * so we loop over such chunks assigning them to devices.
1077		 */
1078		i = -1;
1079		while (s < e) {		/* XXX this is inefficient */
1080			int bound = roundup(s+1, dmmax);
1081
1082			if (bound > e)
1083				bound = e;
1084			if (bound <= niswap) {
1085				/* Interleaved swap chunk. */
1086				if (i == -1)
1087					i = (s / dmmax) % niswdev;
1088				perdev[i] += bound - s;
1089				if (++i >= niswdev)
1090					i = 0;
1091			} else {
1092				/* Sequential swap chunk. */
1093				if (i < niswdev) {
1094					i = niswdev;
1095					l = niswap + sw[i].sw_nblks;
1096				}
1097				while (s >= l) {
1098					/* XXX don't die on bogus blocks */
1099					if (i == nswdev-1)
1100						break;
1101					l += sw[++i].sw_nblks;
1102				}
1103				perdev[i] += bound - s;
1104			}
1105			s = bound;
1106		}
1107	}
1108
1109	if (kflag) {
1110		header = "1K-blocks";
1111		blocksize = 1024;
1112		hlen = strlen(header);
1113	} else
1114		header = getbsize(&hlen, &blocksize);
1115	if (!totalflag)
1116		(void)printf("%-11s %*s %8s %8s %8s  %s\n",
1117		    "Device", hlen, header,
1118		    "Used", "Avail", "Capacity", "Type");
1119	div = blocksize / 512;
1120	avail = npfree = 0;
1121	for (i = 0; i < nswdev; i++) {
1122		int xsize, xfree;
1123
1124		if (!totalflag)
1125			(void)printf("/dev/%-6s %*d ",
1126			    devname(sw[i].sw_dev, S_IFBLK),
1127			    hlen, sw[i].sw_nblks / div);
1128
1129		/*
1130		 * Don't report statistics for partitions which have not
1131		 * yet been activated via swapon(8).
1132		 */
1133		if (!(sw[i].sw_flags & SW_FREED)) {
1134			if (totalflag)
1135				continue;
1136			(void)printf(" *** not available for swapping ***\n");
1137			continue;
1138		}
1139		xsize = sw[i].sw_nblks;
1140		xfree = perdev[i];
1141		used = xsize - xfree;
1142		npfree++;
1143		avail += xsize;
1144		if (totalflag)
1145			continue;
1146		(void)printf("%8d %8d %5.0f%%    %s\n",
1147		    used / div, xfree / div,
1148		    (double)used / (double)xsize * 100.0,
1149		    (sw[i].sw_flags & SW_SEQUENTIAL) ?
1150			     "Sequential" : "Interleaved");
1151	}
1152
1153	/*
1154	 * If only one partition has been set up via swapon(8), we don't
1155	 * need to bother with totals.
1156	 */
1157	used = avail - nfree;
1158	if (totalflag) {
1159		(void)printf("%dM/%dM swap space\n", used / 2048, avail / 2048);
1160		return;
1161	}
1162	if (npfree > 1) {
1163		(void)printf("%-11s %*d %8d %8d %5.0f%%\n",
1164		    "Total", hlen, avail / div, used / div, nfree / div,
1165		    (double)used / (double)avail * 100.0);
1166	}
1167}
1168
1169void
1170usage()
1171{
1172	(void)fprintf(stderr,
1173	    "usage: pstat [-Tfknstv] [-M core] [-N system]\n");
1174	exit(1);
1175}
1176