xref: /netbsd-src/usr.sbin/pstat/pstat.c (revision 4472dbe5e3bd91ef2540bada7a7ca7384627ff9b)
1 /*	$NetBSD: pstat.c,v 1.51 2000/04/14 06:26:54 simonb Exp $	*/
2 
3 /*-
4  * Copyright (c) 1980, 1991, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
39 	The Regents of the University of California.  All rights reserved.\n");
40 #endif /* not lint */
41 
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)pstat.c	8.16 (Berkeley) 5/9/95";
45 #else
46 __RCSID("$NetBSD: pstat.c,v 1.51 2000/04/14 06:26:54 simonb Exp $");
47 #endif
48 #endif /* not lint */
49 
50 #include <sys/param.h>
51 #include <sys/time.h>
52 #include <sys/vnode.h>
53 #include <sys/map.h>
54 #include <sys/ucred.h>
55 #define _KERNEL
56 #define _LKM
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 #include <sys/uio.h>
64 #include <sys/namei.h>
65 #include <miscfs/union/union.h>
66 #undef _KERNEL
67 #include <sys/stat.h>
68 #include <nfs/nfsproto.h>
69 #include <nfs/rpcv2.h>
70 #include <nfs/nfs.h>
71 #include <nfs/nfsnode.h>
72 #include <sys/ioctl.h>
73 #include <sys/tty.h>
74 #include <sys/conf.h>
75 #include <sys/device.h>
76 
77 #include <sys/sysctl.h>
78 
79 #include <err.h>
80 #include <kvm.h>
81 #include <limits.h>
82 #include <nlist.h>
83 #include <stdio.h>
84 #include <stdlib.h>
85 #include <string.h>
86 #include <unistd.h>
87 
88 #include "swapctl.h"
89 
90 struct nlist nl[] = {
91 #define	V_MOUNTLIST	0
92 	{ "_mountlist" },	/* address of head of mount list. */
93 #define	V_NUMV		1
94 	{ "_numvnodes" },
95 #define	FNL_NFILE	2
96 	{ "_nfiles" },
97 #define FNL_MAXFILE	3
98 	{ "_maxfiles" },
99 #define TTY_NTTY	4
100 	{ "_tty_count" },
101 #define TTY_TTYLIST	5
102 	{ "_ttylist" },
103 #define NLMANDATORY TTY_TTYLIST	/* names up to here are mandatory */
104 	{ "" }
105 };
106 
107 int	usenumflag;
108 int	totalflag;
109 int	kflag;
110 char	*nlistf	= NULL;
111 char	*memf	= NULL;
112 kvm_t	*kd;
113 
114 struct {
115 	int m_flag;
116 	const char *m_name;
117 } mnt_flags[] = {
118 	{ MNT_RDONLY, "rdonly" },
119 	{ MNT_SYNCHRONOUS, "sync" },
120 	{ MNT_NOEXEC, "noexec" },
121 	{ MNT_NOSUID, "nosuid" },
122 	{ MNT_NODEV, "nodev" },
123 	{ MNT_UNION, "union" },
124 	{ MNT_ASYNC, "async" },
125 	{ MNT_NOCOREDUMP, "nocoredump" },
126 	{ MNT_NOATIME, "noatime" },
127 	{ MNT_SYMPERM, "symperm" },
128 	{ MNT_NODEVMTIME, "nodevmtime" },
129 	{ MNT_SOFTDEP, "softdep" },
130 	{ MNT_EXRDONLY, "exrdonly" },
131 	{ MNT_EXPORTED, "exported" },
132 	{ MNT_DEFEXPORTED, "defexported" },
133 	{ MNT_EXPORTANON, "exportanon" },
134 	{ MNT_EXKERB, "exkerb" },
135 	{ MNT_EXNORESPORT, "exnoresport" },
136 	{ MNT_EXPUBLIC, "expublic" },
137 	{ MNT_LOCAL, "local" },
138 	{ MNT_QUOTA, "quota" },
139 	{ MNT_ROOTFS, "rootfs" },
140 	{ MNT_UPDATE, "update" },
141 	{ MNT_DELEXPORT, "delexport" },
142 	{ MNT_RELOAD, "reload" },
143 	{ MNT_FORCE, "force" },
144 	{ MNT_GONE, "gone" },
145 	{ MNT_UNMOUNT, "unmount" },
146 	{ MNT_WANTRDWR, "wantrdwr" },
147 	{ 0 }
148 };
149 
150 #define	SVAR(var) __STRING(var)	/* to force expansion */
151 #define	KGET(idx, var)							\
152 	KGET1(idx, &var, sizeof(var), SVAR(var))
153 #define	KGET1(idx, p, s, msg)						\
154 	KGET2(nl[idx].n_value, p, s, msg)
155 #define	KGET2(addr, p, s, msg)						\
156 	if (kvm_read(kd, (u_long)(addr), p, s) != s)			\
157 		warnx("cannot read %s: %s", msg, kvm_geterr(kd))
158 #define	KGETRET(addr, p, s, msg)					\
159 	if (kvm_read(kd, (u_long)(addr), p, s) != s) {			\
160 		warnx("cannot read %s: %s", msg, kvm_geterr(kd));	\
161 		return (0);						\
162 	}
163 
164 void	filemode __P((void));
165 int	getfiles __P((char **, int *));
166 struct mount *
167 	getmnt __P((struct mount *));
168 struct e_vnode *
169 	kinfo_vnodes __P((int *));
170 struct e_vnode *
171 	loadvnodes __P((int *));
172 int	main __P((int, char **));
173 void	mount_print __P((struct mount *));
174 void	nfs_header __P((void));
175 int	nfs_print __P((struct vnode *));
176 void	ttymode __P((void));
177 void	ttyprt __P((struct tty *));
178 void	ufs_getflags __P((struct vnode *, struct inode *, char *));
179 void	ufs_header __P((void));
180 int	ufs_print __P((struct vnode *));
181 int	ext2fs_print __P((struct vnode *));
182 void	union_header __P((void));
183 int	union_print __P((struct vnode *));
184 void	usage __P((void));
185 void	vnode_header __P((void));
186 void	vnode_print __P((struct vnode *, struct vnode *));
187 void	vnodemode __P((void));
188 
189 int
190 main(argc, argv)
191 	int argc;
192 	char *argv[];
193 {
194 	int ch, i, quit, ret;
195 	int fileflag, swapflag, ttyflag, vnodeflag;
196 	gid_t egid = getegid();
197 	char buf[_POSIX2_LINE_MAX];
198 
199 	setegid(getgid());
200 	fileflag = swapflag = ttyflag = vnodeflag = 0;
201 	while ((ch = getopt(argc, argv, "TM:N:fiknstv")) != -1)
202 		switch (ch) {
203 		case 'f':
204 			fileflag = 1;
205 			break;
206 		case 'M':
207 			memf = optarg;
208 			break;
209 		case 'N':
210 			nlistf = optarg;
211 			break;
212 		case 'n':
213 			usenumflag = 1;
214 			break;
215 		case 's':
216 			swapflag = 1;
217 			break;
218 		case 'T':
219 			totalflag = 1;
220 			break;
221 		case 't':
222 			ttyflag = 1;
223 			break;
224 		case 'k':
225 			kflag = 1;
226 			break;
227 		case 'v':
228 		case 'i':		/* Backward compatibility. */
229 			vnodeflag = 1;
230 			break;
231 		default:
232 			usage();
233 		}
234 	argc -= optind;
235 	argv += optind;
236 
237 	/*
238 	 * Discard setgid privileges.  If not the running kernel, we toss
239 	 * them away totally so that bad guys can't print interesting stuff
240 	 * from kernel memory, otherwise switch back to kmem for the
241 	 * duration of the kvm_openfiles() call.
242 	 */
243 	if (nlistf != NULL || memf != NULL)
244 		(void)setgid(getgid());
245 	else
246 		(void)setegid(egid);
247 
248 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
249 		errx(1, "kvm_openfiles: %s", buf);
250 
251 	/* get rid of it now anyway */
252 	if (nlistf == NULL && memf == NULL)
253 		(void)setgid(getgid());
254 	if ((ret = kvm_nlist(kd, nl)) != 0) {
255 		if (ret == -1)
256 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
257 		for (i = quit = 0; i <= NLMANDATORY; i++)
258 			if (!nl[i].n_value) {
259 				quit = 1;
260 				warnx("undefined symbol: %s", nl[i].n_name);
261 			}
262 		if (quit)
263 			exit(1);
264 	}
265 	if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
266 		usage();
267 	if (fileflag || totalflag)
268 		filemode();
269 	if (vnodeflag || totalflag)
270 		vnodemode();
271 	if (ttyflag)
272 		ttymode();
273 	if (swapflag || totalflag)
274 		list_swap(0, kflag, 0, totalflag, 1);
275 	exit (0);
276 }
277 
278 struct e_vnode {
279 	struct vnode *avnode;
280 	struct vnode vnode;
281 };
282 
283 void
284 vnodemode()
285 {
286 	struct e_vnode *e_vnodebase, *endvnode, *evp;
287 	struct vnode *vp;
288 	struct mount *maddr, *mp;
289 	int numvnodes;
290 	int (*vnode_fsprint) __P((struct vnode *)); /* per-fs data printer */
291 
292 	mp = NULL;
293 	e_vnodebase = loadvnodes(&numvnodes);
294 	if (totalflag) {
295 		(void)printf("%7d vnodes\n", numvnodes);
296 		return;
297 	}
298 	endvnode = e_vnodebase + numvnodes;
299 	(void)printf("%d active vnodes\n", numvnodes);
300 
301 #define	ST	mp->mnt_stat
302 #define	FSTYPE_IS(mp, name)						\
303 	(strncmp((mp)->mnt_stat.f_fstypename, (name), MFSNAMELEN) == 0)
304 	maddr = NULL;
305 	vnode_fsprint = NULL;
306 	for (evp = e_vnodebase; evp < endvnode; evp++) {
307 		vp = &evp->vnode;
308 		if (vp->v_mount != maddr) {
309 			/*
310 			 * New filesystem
311 			 */
312 			if ((mp = getmnt(vp->v_mount)) == NULL)
313 				continue;
314 			maddr = vp->v_mount;
315 			mount_print(mp);
316 			vnode_header();
317 			if (FSTYPE_IS(mp, MOUNT_FFS) ||
318 			    FSTYPE_IS(mp, MOUNT_MFS)) {
319 				ufs_header();
320 				vnode_fsprint = ufs_print;
321 			} else if (FSTYPE_IS(mp, MOUNT_NFS)) {
322 				nfs_header();
323 				vnode_fsprint = nfs_print;
324 			} else if (FSTYPE_IS(mp, MOUNT_EXT2FS)) {
325 				ufs_header();
326 				vnode_fsprint = ext2fs_print;
327 			} else if (FSTYPE_IS(mp, MOUNT_UNION)) {
328 				union_header();
329 				vnode_fsprint = union_print;
330 			} else
331 				vnode_fsprint = NULL;
332 			(void)printf("\n");
333 		}
334 		vnode_print(evp->avnode, vp);
335 		if (VTOI(vp) != NULL && vnode_fsprint != NULL)
336 			(*vnode_fsprint)(vp);
337 		(void)printf("\n");
338 	}
339 	free(e_vnodebase);
340 }
341 
342 void
343 vnode_header()
344 {
345 
346 	(void)printf("ADDR     TYP VFLAG  USE HOLD TAG");
347 }
348 
349 void
350 vnode_print(avnode, vp)
351 	struct vnode *avnode;
352 	struct vnode *vp;
353 {
354 	char *type, flags[16];
355 	char *fp = flags;
356 	int flag;
357 
358 	/*
359 	 * set type
360 	 */
361 	switch (vp->v_type) {
362 	case VNON:
363 		type = "non"; break;
364 	case VREG:
365 		type = "reg"; break;
366 	case VDIR:
367 		type = "dir"; break;
368 	case VBLK:
369 		type = "blk"; break;
370 	case VCHR:
371 		type = "chr"; break;
372 	case VLNK:
373 		type = "lnk"; break;
374 	case VSOCK:
375 		type = "soc"; break;
376 	case VFIFO:
377 		type = "fif"; break;
378 	case VBAD:
379 		type = "bad"; break;
380 	default:
381 		type = "unk"; break;
382 	}
383 	/*
384 	 * gather flags
385 	 */
386 	flag = vp->v_flag;
387 	if (flag & VROOT)
388 		*fp++ = 'R';
389 	if (flag & VTEXT)
390 		*fp++ = 'T';
391 	if (flag & VSYSTEM)
392 		*fp++ = 'S';
393 	if (flag & VISTTY)
394 		*fp++ = 'I';
395 	if (flag & VXLOCK)
396 		*fp++ = 'L';
397 	if (flag & VXWANT)
398 		*fp++ = 'W';
399 	if (flag & VBWAIT)
400 		*fp++ = 'B';
401 	if (flag & VALIASED)
402 		*fp++ = 'A';
403 	if (flag & VDIROP)
404 		*fp++ = 'D';
405 	if (flag & VLAYER)
406 		*fp++ = 'Y';
407 	if (flag & VONWORKLST)
408 		*fp++ = 'O';
409 	if (flag == 0)
410 		*fp++ = '-';
411 	*fp = '\0';
412 	(void)printf("%8lx %s %5s %4ld %4ld %3d",
413 	    (long)avnode, type, flags, (long)vp->v_usecount,
414 	    (long)vp->v_holdcnt, vp->v_tag);
415 }
416 
417 void
418 ufs_getflags(vp, ip, flags)
419 	struct vnode *vp;
420 	struct inode *ip;
421 	char *flags;
422 {
423 	int flag;
424 
425 	/*
426 	 * XXX need to to locking state.
427 	 */
428 
429 	flag = ip->i_flag;
430 	if (flag & IN_RENAME)
431 		*flags++ = 'R';
432 	if (flag & IN_UPDATE)
433 		*flags++ = 'U';
434 	if (flag & IN_ACCESS)
435 		*flags++ = 'A';
436 	if (flag & IN_CHANGE)
437 		*flags++ = 'C';
438 	if (flag & IN_MODIFIED)
439 		*flags++ = 'M';
440 	if (flag & IN_SHLOCK)
441 		*flags++ = 'S';
442 	if (flag & IN_EXLOCK)
443 		*flags++ = 'E';
444 	if (flag == 0)
445 		*flags++ = '-';
446 	*flags = '\0';
447 
448 }
449 
450 void
451 ufs_header()
452 {
453 
454 	(void)printf(" FILEID IFLAG RDEV|SZ");
455 }
456 
457 int
458 ufs_print(vp)
459 	struct vnode *vp;
460 {
461 	struct inode inode, *ip = &inode;
462 	char flagbuf[16];
463 	char *name;
464 	mode_t type;
465 
466 	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
467 	ufs_getflags(vp, ip, flagbuf);
468 	(void)printf(" %6d %5s", ip->i_number, flagbuf);
469 	type = ip->i_ffs_mode & S_IFMT;
470 	if (S_ISCHR(ip->i_ffs_mode) || S_ISBLK(ip->i_ffs_mode))
471 		if (usenumflag ||
472 		    ((name = devname(ip->i_ffs_rdev, type)) == NULL))
473 			(void)printf("   %2d,%-2d",
474 			    major(ip->i_ffs_rdev), minor(ip->i_ffs_rdev));
475 		else
476 			(void)printf(" %7s", name);
477 	else
478 		(void)printf(" %7qd", (long long)ip->i_ffs_size);
479 	return (0);
480 }
481 
482 int
483 ext2fs_print(vp)
484 	struct vnode *vp;
485 {
486 	struct inode inode, *ip = &inode;
487 	char flagbuf[16];
488 	char *name;
489 	mode_t type;
490 
491 	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
492 	ufs_getflags(vp, ip, flagbuf);
493 	(void)printf(" %6d %5s", ip->i_number, flagbuf);
494 	type = ip->i_e2fs_mode & S_IFMT;
495 	if (S_ISCHR(ip->i_e2fs_mode) || S_ISBLK(ip->i_e2fs_mode))
496 		if (usenumflag ||
497 		    ((name = devname(ip->i_e2fs_rdev, type)) == NULL))
498 			(void)printf("   %2d,%-2d",
499 			    major(ip->i_e2fs_rdev), minor(ip->i_e2fs_rdev));
500 		else
501 			(void)printf(" %7s", name);
502 	else
503 		(void)printf(" %7u", (u_int)ip->i_e2fs_size);
504 	return (0);
505 }
506 
507 void
508 nfs_header()
509 {
510 
511 	(void)printf(" FILEID NFLAG RDEV|SZ");
512 }
513 
514 int
515 nfs_print(vp)
516 	struct vnode *vp;
517 {
518 	struct nfsnode nfsnode, *np = &nfsnode;
519 	char flagbuf[16], *flags = flagbuf;
520 	int flag;
521 	struct vattr va;
522 	char *name;
523 	mode_t type;
524 
525 	KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
526 	flag = np->n_flag;
527 	if (flag & NFLUSHWANT)
528 		*flags++ = 'W';
529 	if (flag & NFLUSHINPROG)
530 		*flags++ = 'P';
531 	if (flag & NMODIFIED)
532 		*flags++ = 'M';
533 	if (flag & NWRITEERR)
534 		*flags++ = 'E';
535 	if (flag & NQNFSNONCACHE)
536 		*flags++ = 'X';
537 	if (flag & NQNFSWRITE)
538 		*flags++ = 'O';
539 	if (flag & NQNFSEVICTED)
540 		*flags++ = 'G';
541 	if (flag == 0)
542 		*flags++ = '-';
543 	*flags = '\0';
544 
545 	KGETRET(np->n_vattr, &va, sizeof(va), "vnode attr");
546 	(void)printf(" %6ld %5s", (long)va.va_fileid, flagbuf);
547 	type = va.va_mode & S_IFMT;
548 	if (S_ISCHR(va.va_mode) || S_ISBLK(va.va_mode))
549 		if (usenumflag || ((name = devname(va.va_rdev, type)) == NULL))
550 			(void)printf("   %2d,%-2d",
551 			    major(va.va_rdev), minor(va.va_rdev));
552 		else
553 			(void)printf(" %7s", name);
554 	else
555 		(void)printf(" %7qd", (long long)np->n_size);
556 	return (0);
557 }
558 
559 void
560 union_header()
561 {
562 
563 	(void)printf("    UPPER    LOWER");
564 }
565 
566 int
567 union_print(vp)
568 	struct vnode *vp;
569 {
570 	struct union_node unode, *up = &unode;
571 
572 	KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode");
573 
574 	(void)printf(" %8lx %8lx", (long)up->un_uppervp, (long)up->un_lowervp);
575 	return (0);
576 }
577 
578 /*
579  * Given a pointer to a mount structure in kernel space,
580  * read it in and return a usable pointer to it.
581  */
582 struct mount *
583 getmnt(maddr)
584 	struct mount *maddr;
585 {
586 	static struct mtab {
587 		struct mtab *next;
588 		struct mount *maddr;
589 		struct mount mount;
590 	} *mhead = NULL;
591 	struct mtab *mt;
592 
593 	for (mt = mhead; mt != NULL; mt = mt->next)
594 		if (maddr == mt->maddr)
595 			return (&mt->mount);
596 	if ((mt = malloc(sizeof(struct mtab))) == NULL)
597 		err(1, "malloc");
598 	KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
599 	mt->maddr = maddr;
600 	mt->next = mhead;
601 	mhead = mt;
602 	return (&mt->mount);
603 }
604 
605 void
606 mount_print(mp)
607 	struct mount *mp;
608 {
609 	int flags;
610 
611 	(void)printf("*** MOUNT %s %s on %s", ST.f_fstypename,
612 	    ST.f_mntfromname, ST.f_mntonname);
613 	if ((flags = mp->mnt_flag) != 0) {
614 		int i;
615 		const char *sep = " (";
616 
617 		for (i = 0; mnt_flags[i].m_flag; i++) {
618 			if (flags & mnt_flags[i].m_flag) {
619 				(void)printf("%s%s", sep, mnt_flags[i].m_name);
620 				flags &= ~mnt_flags[i].m_flag;
621 				sep = ",";
622 			}
623 		}
624 		if (flags)
625 			(void)printf("%sunknown_flags:%x", sep, flags);
626 		(void)printf(")");
627 	}
628 	(void)printf("\n");
629 }
630 
631 struct e_vnode *
632 loadvnodes(avnodes)
633 	int *avnodes;
634 {
635 	int mib[2];
636 	size_t copysize;
637 	struct e_vnode *vnodebase;
638 
639 	if (memf != NULL) {
640 		/*
641 		 * do it by hand
642 		 */
643 		return (kinfo_vnodes(avnodes));
644 	}
645 	mib[0] = CTL_KERN;
646 	mib[1] = KERN_VNODE;
647 	if (sysctl(mib, 2, NULL, &copysize, NULL, 0) == -1)
648 		err(1, "sysctl: KERN_VNODE");
649 	if ((vnodebase = malloc(copysize)) == NULL)
650 		err(1, "malloc");
651 	if (sysctl(mib, 2, vnodebase, &copysize, NULL, 0) == -1)
652 		err(1, "sysctl: KERN_VNODE");
653 	if (copysize % sizeof(struct e_vnode))
654 		errx(1, "vnode size mismatch");
655 	*avnodes = copysize / sizeof(struct e_vnode);
656 
657 	return (vnodebase);
658 }
659 
660 /*
661  * simulate what a running kernel does in in kinfo_vnode
662  */
663 struct e_vnode *
664 kinfo_vnodes(avnodes)
665 	int *avnodes;
666 {
667 	struct mntlist mountlist;
668 	struct mount *mp, mount;
669 	struct vnode *vp, vnode;
670 	char *vbuf, *evbuf, *bp;
671 	int num, numvnodes;
672 
673 #define VPTRSZ  sizeof(struct vnode *)
674 #define VNODESZ sizeof(struct vnode)
675 
676 	KGET(V_NUMV, numvnodes);
677 	if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
678 		err(1, "malloc");
679 	bp = vbuf;
680 	evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
681 	KGET(V_MOUNTLIST, mountlist);
682 	for (num = 0, mp = mountlist.cqh_first;;
683 	    mp = mount.mnt_list.cqe_next) {
684 		KGET2(mp, &mount, sizeof(mount), "mount entry");
685 		for (vp = mount.mnt_vnodelist.lh_first;
686 		    vp != NULL; vp = vnode.v_mntvnodes.le_next) {
687 			KGET2(vp, &vnode, sizeof(vnode), "vnode");
688 			if ((bp + VPTRSZ + VNODESZ) > evbuf)
689 				/* XXX - should realloc */
690 				errx(1, "no more room for vnodes");
691 			memmove(bp, &vp, VPTRSZ);
692 			bp += VPTRSZ;
693 			memmove(bp, &vnode, VNODESZ);
694 			bp += VNODESZ;
695 			num++;
696 		}
697 		if (mp == mountlist.cqh_last)
698 			break;
699 	}
700 	*avnodes = num;
701 	return ((struct e_vnode *)vbuf);
702 }
703 
704 char hdr[]="  LINE RAW CAN OUT  HWT LWT     COL STATE  SESS      PGID DISC\n";
705 int ttyspace = 128;
706 
707 void
708 ttymode()
709 {
710 	int ntty;
711 	struct ttylist_head tty_head;
712 	struct tty *tp, tty;
713 
714 	KGET(TTY_NTTY, ntty);
715 	(void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s");
716 	KGET(TTY_TTYLIST, tty_head);
717 	(void)printf(hdr);
718 	for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) {
719 		KGET2(tp, &tty, sizeof tty, "tty struct");
720 		ttyprt(&tty);
721 	}
722 }
723 
724 struct {
725 	int flag;
726 	char val;
727 } ttystates[] = {
728 	{ TS_ISOPEN,	'O'},
729 	{ TS_DIALOUT,	'>'},
730 	{ TS_CARR_ON,	'C'},
731 	{ TS_TIMEOUT,	'T'},
732 	{ TS_FLUSH,	'F'},
733 	{ TS_BUSY,	'B'},
734 	{ TS_ASLEEP,	'A'},
735 	{ TS_XCLUDE,	'X'},
736 	{ TS_TTSTOP,	'S'},
737 	{ TS_TBLOCK,	'K'},
738 	{ TS_ASYNC,	'Y'},
739 	{ TS_BKSL,	'D'},
740 	{ TS_ERASE,	'E'},
741 	{ TS_LNCH,	'L'},
742 	{ TS_TYPEN,	'P'},
743 	{ TS_CNTTB,	'N'},
744 	{ 0,	       '\0'},
745 };
746 
747 void
748 ttyprt(tp)
749 	struct tty *tp;
750 {
751 	int i, j;
752 	pid_t pgid;
753 	char *name, state[20];
754 
755 	if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL)
756 		(void)printf("0x%3x:%1x ", major(tp->t_dev), minor(tp->t_dev));
757 	else
758 		(void)printf("%-7s ", name);
759 	(void)printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
760 	(void)printf("%3d %4d %3d %7d ", tp->t_outq.c_cc,
761 		tp->t_hiwat, tp->t_lowat, tp->t_column);
762 	for (i = j = 0; ttystates[i].flag; i++)
763 		if (tp->t_state&ttystates[i].flag)
764 			state[j++] = ttystates[i].val;
765 	if (tp->t_wopen)
766 		state[j++] = 'W';
767 	if (j == 0)
768 		state[j++] = '-';
769 	state[j] = '\0';
770 	(void)printf("%-6s %8lX", state, (u_long)tp->t_session);
771 	pgid = 0;
772 	if (tp->t_pgrp != NULL)
773 		KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
774 	(void)printf("%6d ", pgid);
775 	switch (tp->t_line) {
776 	case TTYDISC:
777 		(void)printf("term\n");
778 		break;
779 	case TABLDISC:
780 		(void)printf("tab\n");
781 		break;
782 	case SLIPDISC:
783 		(void)printf("slip\n");
784 		break;
785 	case PPPDISC:
786 		(void)printf("ppp\n");
787 		break;
788 	case STRIPDISC:
789 		(void)printf("strip\n");
790 		break;
791 	default:
792 		(void)printf("%d\n", tp->t_line);
793 		break;
794 	}
795 }
796 
797 void
798 filemode()
799 {
800 	struct file *fp;
801 	struct file *addr;
802 	char *buf, flagbuf[16], *fbp;
803 	int len, maxfile, nfile;
804 	static char *dtypes[] = { "???", "inode", "socket" };
805 
806 	KGET(FNL_MAXFILE, maxfile);
807 	if (totalflag) {
808 		KGET(FNL_NFILE, nfile);
809 		(void)printf("%3d/%3d files\n", nfile, maxfile);
810 		return;
811 	}
812 	if (getfiles(&buf, &len) == -1)
813 		return;
814 	/*
815 	 * Getfiles returns in malloc'd memory a pointer to the first file
816 	 * structure, and then an array of file structs (whose addresses are
817 	 * derivable from the previous entry).
818 	 */
819 	addr = ((struct filelist *)buf)->lh_first;
820 	fp = (struct file *)(buf + sizeof(struct filelist));
821 	nfile = (len - sizeof(struct filelist)) / sizeof(struct file);
822 
823 	(void)printf("%d/%d open files\n", nfile, maxfile);
824 	(void)printf("   LOC   TYPE    FLG     CNT  MSG    DATA    OFFSET\n");
825 	for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) {
826 		if ((unsigned)fp->f_type > DTYPE_SOCKET)
827 			continue;
828 		(void)printf("%lx ", (long)addr);
829 		(void)printf("%-8.8s", dtypes[fp->f_type]);
830 		fbp = flagbuf;
831 		if (fp->f_flag & FREAD)
832 			*fbp++ = 'R';
833 		if (fp->f_flag & FWRITE)
834 			*fbp++ = 'W';
835 		if (fp->f_flag & FAPPEND)
836 			*fbp++ = 'A';
837 #ifdef FSHLOCK	/* currently gone */
838 		if (fp->f_flag & FSHLOCK)
839 			*fbp++ = 'S';
840 		if (fp->f_flag & FEXLOCK)
841 			*fbp++ = 'X';
842 #endif
843 		if (fp->f_flag & FASYNC)
844 			*fbp++ = 'I';
845 		*fbp = '\0';
846 		(void)printf("%6s  %3d", flagbuf, fp->f_count);
847 		(void)printf("  %3d", fp->f_msgcount);
848 		(void)printf("  %8.1lx", (long)fp->f_data);
849 		if (fp->f_offset < 0)
850 			(void)printf("  %qx\n", (long long)fp->f_offset);
851 		else
852 			(void)printf("  %qd\n", (long long)fp->f_offset);
853 	}
854 	free(buf);
855 }
856 
857 int
858 getfiles(abuf, alen)
859 	char **abuf;
860 	int *alen;
861 {
862 	size_t len;
863 	int mib[2];
864 	char *buf;
865 
866 	/*
867 	 * XXX
868 	 * Add emulation of KINFO_FILE here.
869 	 */
870 	if (memf != NULL)
871 		errx(1, "files on dead kernel, not implemented\n");
872 
873 	mib[0] = CTL_KERN;
874 	mib[1] = KERN_FILE;
875 	if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
876 		warn("sysctl: KERN_FILE");
877 		return (-1);
878 	}
879 	if ((buf = malloc(len)) == NULL)
880 		err(1, "malloc");
881 	if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
882 		warn("sysctl: KERN_FILE");
883 		return (-1);
884 	}
885 	*abuf = buf;
886 	*alen = len;
887 	return (0);
888 }
889 
890 void
891 usage()
892 {
893 
894 	(void)fprintf(stderr,
895 	    "usage: pstat [-T|-f|-s|-t|-v] [-kn] [-M core] [-N system]\n");
896 	exit(1);
897 }
898