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