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