xref: /netbsd-src/usr.sbin/pstat/pstat.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: pstat.c,v 1.31 1997/06/12 15:03:44 mrg Exp $	*/
2 
3 /*-
4  * Copyright (c) 1980, 1991, 1993
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 #ifndef lint
37 static char copyright[] =
38 "@(#) Copyright (c) 1980, 1991, 1993\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 from: static char sccsid[] = "@(#)pstat.c	8.9 (Berkeley) 2/16/94";
45 #else
46 static char *rcsid = "$NetBSD: pstat.c,v 1.31 1997/06/12 15:03:44 mrg 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 #include <sys/file.h>
57 #include <ufs/ufs/quota.h>
58 #include <ufs/ufs/inode.h>
59 #define NFS
60 #include <sys/mount.h>
61 #undef NFS
62 #undef _KERNEL
63 #include <sys/stat.h>
64 #include <nfs/nfsproto.h>
65 #include <nfs/rpcv2.h>
66 #include <nfs/nfsnode.h>
67 #include <sys/ioctl.h>
68 #include <sys/tty.h>
69 #include <sys/conf.h>
70 #include <sys/device.h>
71 
72 #include <sys/sysctl.h>
73 
74 #include <err.h>
75 #include <kvm.h>
76 #include <limits.h>
77 #include <nlist.h>
78 #include <stdio.h>
79 #include <stdlib.h>
80 #include <string.h>
81 #include <unistd.h>
82 
83 #include "swapctl.h"
84 
85 struct nlist nl[] = {
86 #define	V_MOUNTLIST	0
87 	{ "_mountlist" },	/* address of head of mount list. */
88 #define V_NUMV		1
89 	{ "_numvnodes" },
90 #define	FNL_NFILE	2
91 	{"_nfiles"},
92 #define FNL_MAXFILE	3
93 	{"_maxfiles"},
94 #define TTY_NTTY	4
95 	{"_tty_count"},
96 #define TTY_TTYLIST	5
97 	{"_ttylist"},
98 #define NLMANDATORY TTY_TTYLIST	/* names up to here are mandatory */
99 	{ "" }
100 };
101 
102 int	usenumflag;
103 int	totalflag;
104 int	kflag;
105 char	*nlistf	= NULL;
106 char	*memf	= NULL;
107 kvm_t	*kd;
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 
123 void	filemode __P((void));
124 int	getfiles __P((char **, int *));
125 struct mount *
126 	getmnt __P((struct mount *));
127 struct e_vnode *
128 	kinfo_vnodes __P((int *));
129 struct e_vnode *
130 	loadvnodes __P((int *));
131 void	mount_print __P((struct mount *));
132 void	nfs_header __P((void));
133 int	nfs_print __P((struct vnode *));
134 void	ttymode __P((void));
135 void	ttyprt __P((struct tty *));
136 void	ufs_getflags __P((struct vnode *, struct inode *, char *));
137 void	ufs_header __P((void));
138 int	ufs_print __P((struct vnode *));
139 int		ext2fs_print __P((struct vnode *));
140 void	usage __P((void));
141 void	vnode_header __P((void));
142 void	vnode_print __P((struct vnode *, struct vnode *));
143 void	vnodemode __P((void));
144 
145 int
146 main(argc, argv)
147 	int argc;
148 	char *argv[];
149 {
150 	extern char *optarg;
151 	extern int optind;
152 	int ch, i, quit, ret;
153 	int fileflag, swapflag, ttyflag, vnodeflag;
154 	char buf[_POSIX2_LINE_MAX];
155 
156 	fileflag = swapflag = ttyflag = vnodeflag = 0;
157 	while ((ch = getopt(argc, argv, "TM:N:fiknstv")) != -1)
158 		switch (ch) {
159 		case 'f':
160 			fileflag = 1;
161 			break;
162 		case 'M':
163 			memf = optarg;
164 			break;
165 		case 'N':
166 			nlistf = optarg;
167 			break;
168 		case 'n':
169 			usenumflag = 1;
170 			break;
171 		case 's':
172 			swapflag = 1;
173 			break;
174 		case 'T':
175 			totalflag = 1;
176 			break;
177 		case 't':
178 			ttyflag = 1;
179 			break;
180 		case 'k':
181 			kflag = 1;
182 			break;
183 		case 'v':
184 		case 'i':		/* Backward compatibility. */
185 			vnodeflag = 1;
186 			break;
187 		default:
188 			usage();
189 		}
190 	argc -= optind;
191 	argv += optind;
192 
193 	/*
194 	 * Discard setgid privileges if not the running kernel so that bad
195 	 * guys can't print interesting stuff from kernel memory.
196 	 */
197 	if (nlistf != NULL || memf != NULL)
198 		(void)setgid(getgid());
199 
200 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
201 		errx(1, "kvm_openfiles: %s", buf);
202 	if ((ret = kvm_nlist(kd, nl)) != 0) {
203 		if (ret == -1)
204 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
205 		for (i = quit = 0; i <= NLMANDATORY; i++)
206 			if (!nl[i].n_value) {
207 				quit = 1;
208 				warnx("undefined symbol: %s", nl[i].n_name);
209 			}
210 		if (quit)
211 			exit(1);
212 	}
213 	if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
214 		usage();
215 	if (fileflag || totalflag)
216 		filemode();
217 	if (vnodeflag || totalflag)
218 		vnodemode();
219 	if (ttyflag)
220 		ttymode();
221 	if (swapflag || totalflag)
222 		list_swap(0, 0, kflag, totalflag, 1);
223 	exit (0);
224 }
225 
226 struct e_vnode {
227 	struct vnode *avnode;
228 	struct vnode vnode;
229 };
230 
231 void
232 vnodemode()
233 {
234 	register struct e_vnode *e_vnodebase, *endvnode, *evp;
235 	register struct vnode *vp;
236 	register struct mount *maddr, *mp;
237 	int numvnodes;
238 
239 	e_vnodebase = loadvnodes(&numvnodes);
240 	if (totalflag) {
241 		(void)printf("%7d vnodes\n", numvnodes);
242 		return;
243 	}
244 	endvnode = e_vnodebase + numvnodes;
245 	(void)printf("%d active vnodes\n", numvnodes);
246 
247 
248 #define ST	mp->mnt_stat
249 	maddr = NULL;
250 	for (evp = e_vnodebase; evp < endvnode; evp++) {
251 		vp = &evp->vnode;
252 		if (vp->v_mount != maddr) {
253 			/*
254 			 * New filesystem
255 			 */
256 			if ((mp = getmnt(vp->v_mount)) == NULL)
257 				continue;
258 			maddr = vp->v_mount;
259 			mount_print(mp);
260 			vnode_header();
261 			if (!strncmp(ST.f_fstypename, MOUNT_FFS, MFSNAMELEN) ||
262 			    !strncmp(ST.f_fstypename, MOUNT_MFS, MFSNAMELEN)) {
263 				ufs_header();
264 			} else if (!strncmp(ST.f_fstypename, MOUNT_NFS,
265 			    MFSNAMELEN)) {
266 				nfs_header();
267 			} else if (!strncmp(ST.f_fstypename, MOUNT_EXT2FS,
268 				MFSNAMELEN)) {
269 				ufs_header();
270 			}
271 			(void)printf("\n");
272 		}
273 		vnode_print(evp->avnode, vp);
274 		if (!strncmp(ST.f_fstypename, MOUNT_FFS, MFSNAMELEN) ||
275 		    !strncmp(ST.f_fstypename, MOUNT_MFS, MFSNAMELEN)) {
276 			ufs_print(vp);
277 		} else if (!strncmp(ST.f_fstypename, MOUNT_NFS, MFSNAMELEN)) {
278 			nfs_print(vp);
279 		} else if (!strncmp(ST.f_fstypename, MOUNT_EXT2FS, MFSNAMELEN)) {
280 			ext2fs_print(vp);
281 		}
282 		(void)printf("\n");
283 	}
284 	free(e_vnodebase);
285 }
286 
287 void
288 vnode_header()
289 {
290 	(void)printf("ADDR     TYP VFLAG  USE HOLD");
291 }
292 
293 void
294 vnode_print(avnode, vp)
295 	struct vnode *avnode;
296 	struct vnode *vp;
297 {
298 	char *type, flags[16];
299 	char *fp = flags;
300 	register int flag;
301 
302 	/*
303 	 * set type
304 	 */
305 	switch(vp->v_type) {
306 	case VNON:
307 		type = "non"; break;
308 	case VREG:
309 		type = "reg"; break;
310 	case VDIR:
311 		type = "dir"; break;
312 	case VBLK:
313 		type = "blk"; break;
314 	case VCHR:
315 		type = "chr"; break;
316 	case VLNK:
317 		type = "lnk"; break;
318 	case VSOCK:
319 		type = "soc"; break;
320 	case VFIFO:
321 		type = "fif"; break;
322 	case VBAD:
323 		type = "bad"; break;
324 	default:
325 		type = "unk"; break;
326 	}
327 	/*
328 	 * gather flags
329 	 */
330 	flag = vp->v_flag;
331 	if (flag & VROOT)
332 		*fp++ = 'R';
333 	if (flag & VTEXT)
334 		*fp++ = 'T';
335 	if (flag & VSYSTEM)
336 		*fp++ = 'S';
337 	if (flag & VISTTY)
338 		*fp++ = 'I';
339 	if (flag & VXLOCK)
340 		*fp++ = 'L';
341 	if (flag & VXWANT)
342 		*fp++ = 'W';
343 	if (flag & VBWAIT)
344 		*fp++ = 'B';
345 	if (flag & VALIASED)
346 		*fp++ = 'A';
347 	if (flag & VDIROP)
348 		*fp++ = 'D';
349 	if (flag == 0)
350 		*fp++ = '-';
351 	*fp = '\0';
352 	(void)printf("%8x %s %5s %4d %4d",
353 	    avnode, type, flags, vp->v_usecount, vp->v_holdcnt);
354 }
355 
356 void
357 ufs_getflags(vp, ip, flags)
358 	struct vnode *vp;
359 	struct inode *ip;
360 	char *flags;
361 {
362 	register int flag;
363 
364 	flag = ip->i_flag;
365 	if (flag & IN_LOCKED)
366 		*flags++ = 'L';
367 	if (flag & IN_WANTED)
368 		*flags++ = 'W';
369 	if (flag & IN_RENAME)
370 		*flags++ = 'R';
371 	if (flag & IN_UPDATE)
372 		*flags++ = 'U';
373 	if (flag & IN_ACCESS)
374 		*flags++ = 'A';
375 	if (flag & IN_CHANGE)
376 		*flags++ = 'C';
377 	if (flag & IN_MODIFIED)
378 		*flags++ = 'M';
379 	if (flag & IN_SHLOCK)
380 		*flags++ = 'S';
381 	if (flag & IN_EXLOCK)
382 		*flags++ = 'E';
383 	if (flag & IN_LWAIT)
384 		*flags++ = 'Z';
385 	if (flag == 0)
386 		*flags++ = '-';
387 	*flags = '\0';
388 
389 }
390 
391 void
392 ufs_header()
393 {
394 	(void)printf(" FILEID IFLAG RDEV|SZ");
395 }
396 
397 int
398 ufs_print(vp)
399 	struct vnode *vp;
400 {
401 	struct inode inode, *ip = &inode;
402 	char flagbuf[16];
403 	char *name;
404 	mode_t type;
405 
406 	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
407 	ufs_getflags(vp, ip, flagbuf);
408 	(void)printf(" %6d %5s", ip->i_number, flagbuf);
409 	type = ip->i_ffs_mode & S_IFMT;
410 	if (S_ISCHR(ip->i_ffs_mode) || S_ISBLK(ip->i_ffs_mode))
411 		if (usenumflag || ((name = devname(ip->i_ffs_rdev, type)) == NULL))
412 			(void)printf("   %2d,%-2d",
413 			    major(ip->i_ffs_rdev), minor(ip->i_ffs_rdev));
414 		else
415 			(void)printf(" %7s", name);
416 	else
417 		(void)printf(" %7qd", ip->i_ffs_size);
418 	return (0);
419 }
420 
421 int
422 ext2fs_print(vp)
423 	struct vnode *vp;
424 {
425 	struct inode inode, *ip = &inode;
426 	char flagbuf[16];
427 	char *name;
428 	mode_t type;
429 
430 	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
431 	ufs_getflags(vp, ip, flagbuf);
432 	(void)printf(" %6d %5s", ip->i_number, flagbuf);
433 	type = ip->i_e2fs_mode & S_IFMT;
434 	if (S_ISCHR(ip->i_e2fs_mode) || S_ISBLK(ip->i_e2fs_mode))
435 		if (usenumflag || ((name = devname(ip->i_din.e2fs_din.e2di_rdev,
436 			type)) == NULL))
437 			(void)printf("   %2d,%-2d",
438 			    major(ip->i_din.e2fs_din.e2di_rdev),
439 						minor(ip->i_din.e2fs_din.e2di_rdev));
440 		else
441 			(void)printf(" %7s", name);
442 	else
443 		(void)printf(" %7u", (u_int)ip->i_e2fs_size);
444 	return (0);
445 }
446 
447 void
448 nfs_header()
449 {
450 	(void)printf(" FILEID NFLAG RDEV|SZ");
451 }
452 
453 int
454 nfs_print(vp)
455 	struct vnode *vp;
456 {
457 	struct nfsnode nfsnode, *np = &nfsnode;
458 	char flagbuf[16], *flags = flagbuf;
459 	register int flag;
460 	char *name;
461 	mode_t type;
462 
463 	KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
464 	flag = np->n_flag;
465 	if (flag & NFLUSHWANT)
466 		*flags++ = 'W';
467 	if (flag & NFLUSHINPROG)
468 		*flags++ = 'P';
469 	if (flag & NMODIFIED)
470 		*flags++ = 'M';
471 	if (flag & NWRITEERR)
472 		*flags++ = 'E';
473 	if (flag & NQNFSNONCACHE)
474 		*flags++ = 'X';
475 	if (flag & NQNFSWRITE)
476 		*flags++ = 'O';
477 	if (flag & NQNFSEVICTED)
478 		*flags++ = 'G';
479 	if (flag == 0)
480 		*flags++ = '-';
481 	*flags = '\0';
482 
483 #define VT	np->n_vattr
484 	(void)printf(" %6d %5s", VT.va_fileid, flagbuf);
485 	type = VT.va_mode & S_IFMT;
486 	if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode))
487 		if (usenumflag || ((name = devname(VT.va_rdev, type)) == NULL))
488 			(void)printf("   %2d,%-2d",
489 			    major(VT.va_rdev), minor(VT.va_rdev));
490 		else
491 			(void)printf(" %7s", name);
492 	else
493 		(void)printf(" %7qd", np->n_size);
494 	return (0);
495 }
496 
497 /*
498  * Given a pointer to a mount structure in kernel space,
499  * read it in and return a usable pointer to it.
500  */
501 struct mount *
502 getmnt(maddr)
503 	struct mount *maddr;
504 {
505 	static struct mtab {
506 		struct mtab *next;
507 		struct mount *maddr;
508 		struct mount mount;
509 	} *mhead = NULL;
510 	register struct mtab *mt;
511 
512 	for (mt = mhead; mt != NULL; mt = mt->next)
513 		if (maddr == mt->maddr)
514 			return (&mt->mount);
515 	if ((mt = malloc(sizeof(struct mtab))) == NULL)
516 		err(1, NULL);
517 	KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
518 	mt->maddr = maddr;
519 	mt->next = mhead;
520 	mhead = mt;
521 	return (&mt->mount);
522 }
523 
524 void
525 mount_print(mp)
526 	struct mount *mp;
527 {
528 	register int flags;
529 	char *type;
530 
531 #define ST	mp->mnt_stat
532 	(void)printf("*** MOUNT ");
533 	(void)printf("%.*s %s on %s", MFSNAMELEN, ST.f_fstypename,
534 	    ST.f_mntfromname, ST.f_mntonname);
535 	if (flags = mp->mnt_flag) {
536 		char *comma = "(";
537 
538 		putchar(' ');
539 		/* user visable flags */
540 		if (flags & MNT_RDONLY) {
541 			(void)printf("%srdonly", comma);
542 			flags &= ~MNT_RDONLY;
543 			comma = ",";
544 		}
545 		if (flags & MNT_SYNCHRONOUS) {
546 			(void)printf("%ssynchronous", comma);
547 			flags &= ~MNT_SYNCHRONOUS;
548 			comma = ",";
549 		}
550 		if (flags & MNT_NOEXEC) {
551 			(void)printf("%snoexec", comma);
552 			flags &= ~MNT_NOEXEC;
553 			comma = ",";
554 		}
555 		if (flags & MNT_NOSUID) {
556 			(void)printf("%snosuid", comma);
557 			flags &= ~MNT_NOSUID;
558 			comma = ",";
559 		}
560 		if (flags & MNT_NODEV) {
561 			(void)printf("%snodev", comma);
562 			flags &= ~MNT_NODEV;
563 			comma = ",";
564 		}
565 		if (flags & MNT_UNION) {
566 			(void)printf("%sunion", comma);
567 			flags &= ~MNT_UNION;
568 			comma = ",";
569 		}
570 		if (flags & MNT_ASYNC) {
571 			(void)printf("%sasync", comma);
572 			flags &= ~MNT_ASYNC;
573 			comma = ",";
574 		}
575 		if (flags & MNT_NOCOREDUMP) {
576 			(void)printf("%snocoredump", comma);
577 			flags &= ~MNT_NOCOREDUMP;
578 			comma = ",";
579 		}
580 		if (flags & MNT_EXRDONLY) {
581 			(void)printf("%sexrdonly", comma);
582 			flags &= ~MNT_EXRDONLY;
583 			comma = ",";
584 		}
585 		if (flags & MNT_EXPORTED) {
586 			(void)printf("%sexport", comma);
587 			flags &= ~MNT_EXPORTED;
588 			comma = ",";
589 		}
590 		if (flags & MNT_DEFEXPORTED) {
591 			(void)printf("%sdefdexported", comma);
592 			flags &= ~MNT_DEFEXPORTED;
593 			comma = ",";
594 		}
595 		if (flags & MNT_EXPORTANON) {
596 			(void)printf("%sexportanon", comma);
597 			flags &= ~MNT_EXPORTANON;
598 			comma = ",";
599 		}
600 		if (flags & MNT_EXKERB) {
601 			(void)printf("%sexkerb", comma);
602 			flags &= ~MNT_EXKERB;
603 			comma = ",";
604 		}
605 		if (flags & MNT_LOCAL) {
606 			(void)printf("%slocal", comma);
607 			flags &= ~MNT_LOCAL;
608 			comma = ",";
609 		}
610 		if (flags & MNT_QUOTA) {
611 			(void)printf("%squota", comma);
612 			flags &= ~MNT_QUOTA;
613 			comma = ",";
614 		}
615 		if (flags & MNT_ROOTFS) {
616 			(void)printf("%srootfs", comma);
617 			flags &= ~MNT_ROOTFS;
618 			comma = ",";
619 		}
620 		/* filesystem control flags */
621 		if (flags & MNT_UPDATE) {
622 			(void)printf("%supdate", comma);
623 			flags &= ~MNT_UPDATE;
624 			comma = ",";
625 		}
626 		if (flags & MNT_MLOCK) {
627 			(void)printf("%slock", comma);
628 			flags &= ~MNT_MLOCK;
629 			comma = ",";
630 		}
631 		if (flags & MNT_MWAIT) {
632 			(void)printf("%swait", comma);
633 			flags &= ~MNT_MWAIT;
634 			comma = ",";
635 		}
636 		if (flags & MNT_MPBUSY) {
637 			(void)printf("%sbusy", comma);
638 			flags &= ~MNT_MPBUSY;
639 			comma = ",";
640 		}
641 		if (flags & MNT_MPWANT) {
642 			(void)printf("%swant", comma);
643 			flags &= ~MNT_MPWANT;
644 			comma = ",";
645 		}
646 		if (flags & MNT_UNMOUNT) {
647 			(void)printf("%sunmount", comma);
648 			flags &= ~MNT_UNMOUNT;
649 			comma = ",";
650 		}
651 		if (flags)
652 			(void)printf("%sunknown_flags:%x", comma, flags);
653 		(void)printf(")");
654 	}
655 	(void)printf("\n");
656 #undef ST
657 }
658 
659 struct e_vnode *
660 loadvnodes(avnodes)
661 	int *avnodes;
662 {
663 	int mib[2];
664 	size_t copysize;
665 	struct e_vnode *vnodebase;
666 
667 	if (memf != NULL) {
668 		/*
669 		 * do it by hand
670 		 */
671 		return (kinfo_vnodes(avnodes));
672 	}
673 	mib[0] = CTL_KERN;
674 	mib[1] = KERN_VNODE;
675 	if (sysctl(mib, 2, NULL, &copysize, NULL, 0) == -1)
676 		err(1, "sysctl: KERN_VNODE");
677 	if ((vnodebase = malloc(copysize)) == NULL)
678 		err(1, NULL);
679 	if (sysctl(mib, 2, vnodebase, &copysize, NULL, 0) == -1)
680 		err(1, "sysctl: KERN_VNODE");
681 	if (copysize % sizeof(struct e_vnode))
682 		errx(1, "vnode size mismatch");
683 	*avnodes = copysize / sizeof(struct e_vnode);
684 
685 	return (vnodebase);
686 }
687 
688 /*
689  * simulate what a running kernel does in in kinfo_vnode
690  */
691 struct e_vnode *
692 kinfo_vnodes(avnodes)
693 	int *avnodes;
694 {
695 	struct mntlist mountlist;
696 	struct mount *mp, mount;
697 	struct vnode *vp, vnode;
698 	char *vbuf, *evbuf, *bp;
699 	int num, numvnodes;
700 
701 #define VPTRSZ  sizeof(struct vnode *)
702 #define VNODESZ sizeof(struct vnode)
703 
704 	KGET(V_NUMV, numvnodes);
705 	if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
706 		err(1, NULL);
707 	bp = vbuf;
708 	evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
709 	KGET(V_MOUNTLIST, mountlist);
710 	for (num = 0, mp = mountlist.cqh_first; ; mp = mount.mnt_list.cqe_next) {
711 		KGET2(mp, &mount, sizeof(mount), "mount entry");
712 		for (vp = mount.mnt_vnodelist.lh_first;
713 		    vp != NULL; vp = vnode.v_mntvnodes.le_next) {
714 			KGET2(vp, &vnode, sizeof(vnode), "vnode");
715 			if ((bp + VPTRSZ + VNODESZ) > evbuf)
716 				/* XXX - should realloc */
717 				errx(1, "no more room for vnodes");
718 			memmove(bp, &vp, VPTRSZ);
719 			bp += VPTRSZ;
720 			memmove(bp, &vnode, VNODESZ);
721 			bp += VNODESZ;
722 			num++;
723 		}
724 		if (mp == mountlist.cqh_last)
725 			break;
726 	}
727 	*avnodes = num;
728 	return ((struct e_vnode *)vbuf);
729 }
730 
731 char hdr[]="  LINE  RAW  CAN  OUT  HWT LWT    COL STATE      SESS  PGID DISC\n";
732 
733 void
734 ttymode()
735 {
736 	int ntty, i;
737 	struct ttylist_head tty_head;
738 	struct tty *tp, tty;
739 
740 	KGET(TTY_NTTY, ntty);
741 	(void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s");
742 	KGET(TTY_TTYLIST, tty_head);
743 	(void)printf(hdr);
744 	for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) {
745 		KGET2(tp, &tty, sizeof tty, "tty struct");
746 		ttyprt(&tty);
747 	}
748 }
749 
750 struct {
751 	int flag;
752 	char val;
753 } ttystates[] = {
754 	{ TS_WOPEN,	'W'},
755 	{ TS_ISOPEN,	'O'},
756 	{ TS_CARR_ON,	'C'},
757 	{ TS_TIMEOUT,	'T'},
758 	{ TS_FLUSH,	'F'},
759 	{ TS_BUSY,	'B'},
760 	{ TS_ASLEEP,	'A'},
761 	{ TS_XCLUDE,	'X'},
762 	{ TS_TTSTOP,	'S'},
763 	{ TS_TBLOCK,	'K'},
764 	{ TS_ASYNC,	'Y'},
765 	{ TS_BKSL,	'D'},
766 	{ TS_ERASE,	'E'},
767 	{ TS_LNCH,	'L'},
768 	{ TS_TYPEN,	'P'},
769 	{ TS_CNTTB,	'N'},
770 	{ 0,	       '\0'},
771 };
772 
773 void
774 ttyprt(tp)
775 	register struct tty *tp;
776 {
777 	register int i, j;
778 	pid_t pgid;
779 	char *name, state[20];
780 
781 	if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL)
782 		(void)printf("0x%3x:%1x ", major(tp->t_dev), minor(tp->t_dev));
783 	else
784 		(void)printf("%-7s ", name);
785 	(void)printf("%3d %4d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
786 	(void)printf("%4d %4d %3d %6d ", tp->t_outq.c_cc,
787 		tp->t_hiwat, tp->t_lowat, tp->t_column);
788 	for (i = j = 0; ttystates[i].flag; i++)
789 		if (tp->t_state&ttystates[i].flag)
790 			state[j++] = ttystates[i].val;
791 	if (j == 0)
792 		state[j++] = '-';
793 	state[j] = '\0';
794 	(void)printf("%-6s %8x", state, (u_long)tp->t_session & ~KERNBASE);
795 	pgid = 0;
796 	if (tp->t_pgrp != NULL)
797 		KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
798 	(void)printf("%6d ", pgid);
799 	switch (tp->t_line) {
800 	case TTYDISC:
801 		(void)printf("term\n");
802 		break;
803 	case TABLDISC:
804 		(void)printf("tab\n");
805 		break;
806 	case SLIPDISC:
807 		(void)printf("slip\n");
808 		break;
809 	case PPPDISC:
810 		(void)printf("ppp\n");
811 		break;
812 	case STRIPDISC:
813 		(void)printf("strip\n");
814 		break;
815 	default:
816 		(void)printf("%d\n", tp->t_line);
817 		break;
818 	}
819 }
820 
821 void
822 filemode()
823 {
824 	register struct file *fp;
825 	struct file *addr;
826 	char *buf, flagbuf[16], *fbp;
827 	int len, maxfile, nfile;
828 	static char *dtypes[] = { "???", "inode", "socket" };
829 
830 	KGET(FNL_MAXFILE, maxfile);
831 	if (totalflag) {
832 		KGET(FNL_NFILE, nfile);
833 		(void)printf("%3d/%3d files\n", nfile, maxfile);
834 		return;
835 	}
836 	if (getfiles(&buf, &len) == -1)
837 		return;
838 	/*
839 	 * Getfiles returns in malloc'd memory a pointer to the first file
840 	 * structure, and then an array of file structs (whose addresses are
841 	 * derivable from the previous entry).
842 	 */
843 	addr = ((struct filelist *)buf)->lh_first;
844 	fp = (struct file *)(buf + sizeof(struct filelist));
845 	nfile = (len - sizeof(struct filelist)) / sizeof(struct file);
846 
847 	(void)printf("%d/%d open files\n", nfile, maxfile);
848 	(void)printf("   LOC   TYPE    FLG     CNT  MSG    DATA    OFFSET\n");
849 	for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) {
850 		if ((unsigned)fp->f_type > DTYPE_SOCKET)
851 			continue;
852 		(void)printf("%x ", addr);
853 		(void)printf("%-8.8s", dtypes[fp->f_type]);
854 		fbp = flagbuf;
855 		if (fp->f_flag & FREAD)
856 			*fbp++ = 'R';
857 		if (fp->f_flag & FWRITE)
858 			*fbp++ = 'W';
859 		if (fp->f_flag & FAPPEND)
860 			*fbp++ = 'A';
861 #ifdef FSHLOCK	/* currently gone */
862 		if (fp->f_flag & FSHLOCK)
863 			*fbp++ = 'S';
864 		if (fp->f_flag & FEXLOCK)
865 			*fbp++ = 'X';
866 #endif
867 		if (fp->f_flag & FASYNC)
868 			*fbp++ = 'I';
869 		*fbp = '\0';
870 		(void)printf("%6s  %3d", flagbuf, fp->f_count);
871 		(void)printf("  %3d", fp->f_msgcount);
872 		(void)printf("  %8.1x", fp->f_data);
873 		if (fp->f_offset < 0)
874 			(void)printf("  %qx\n", fp->f_offset);
875 		else
876 			(void)printf("  %qd\n", fp->f_offset);
877 	}
878 	free(buf);
879 }
880 
881 int
882 getfiles(abuf, alen)
883 	char **abuf;
884 	int *alen;
885 {
886 	size_t len;
887 	int mib[2];
888 	char *buf;
889 
890 	/*
891 	 * XXX
892 	 * Add emulation of KINFO_FILE here.
893 	 */
894 	if (memf != NULL)
895 		errx(1, "files on dead kernel, not implemented\n");
896 
897 	mib[0] = CTL_KERN;
898 	mib[1] = KERN_FILE;
899 	if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
900 		warn("sysctl: KERN_FILE");
901 		return (-1);
902 	}
903 	if ((buf = malloc(len)) == NULL)
904 		err(1, NULL);
905 	if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
906 		warn("sysctl: KERN_FILE");
907 		return (-1);
908 	}
909 	*abuf = buf;
910 	*alen = len;
911 	return (0);
912 }
913 
914 void
915 usage()
916 {
917 	(void)fprintf(stderr,
918 	    "usage: pstat [-T|-f|-s|-t|-v] [-kn] [-M core] [-N system]\n");
919 	exit(1);
920 }
921