Home | History | Annotate | Line # | Download | only in pstat
pstat.c revision 1.55
      1 /*	$NetBSD: pstat.c,v 1.55 2000/12/13 05:34:55 enami 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.55 2000/12/13 05:34:55 enami 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_ACCESS)
    431 		*flags++ = 'A';
    432 	if (flag & IN_CHANGE)
    433 		*flags++ = 'C';
    434 	if (flag & IN_UPDATE)
    435 		*flags++ = 'U';
    436 	if (flag & IN_MODIFIED)
    437 		*flags++ = 'M';
    438 	if (flag & IN_ACCESSED)
    439 		*flags++ = 'a';
    440 	if (flag & IN_RENAME)
    441 		*flags++ = 'R';
    442 	if (flag & IN_SHLOCK)
    443 		*flags++ = 'S';
    444 	if (flag & IN_EXLOCK)
    445 		*flags++ = 'E';
    446 	if (flag & IN_CLEANING)
    447 		*flags++ = 'c';
    448 	if (flag & IN_ADIROP)
    449 		*flags++ = 'a';
    450 	if (flag == 0)
    451 		*flags++ = '-';
    452 	*flags = '\0';
    453 
    454 }
    455 
    456 void
    457 ufs_header()
    458 {
    459 
    460 	(void)printf(" FILEID IFLAG RDEV|SZ");
    461 }
    462 
    463 int
    464 ufs_print(vp)
    465 	struct vnode *vp;
    466 {
    467 	struct inode inode, *ip = &inode;
    468 	char flagbuf[16];
    469 	char *name;
    470 	mode_t type;
    471 
    472 	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
    473 	ufs_getflags(vp, ip, flagbuf);
    474 	(void)printf(" %6d %5s", ip->i_number, flagbuf);
    475 	type = ip->i_ffs_mode & S_IFMT;
    476 	if (S_ISCHR(ip->i_ffs_mode) || S_ISBLK(ip->i_ffs_mode))
    477 		if (usenumflag ||
    478 		    ((name = devname(ip->i_ffs_rdev, type)) == NULL))
    479 			(void)printf("   %2d,%-2d",
    480 			    major(ip->i_ffs_rdev), minor(ip->i_ffs_rdev));
    481 		else
    482 			(void)printf(" %7s", name);
    483 	else
    484 		(void)printf(" %7qd", (long long)ip->i_ffs_size);
    485 	return (0);
    486 }
    487 
    488 int
    489 ext2fs_print(vp)
    490 	struct vnode *vp;
    491 {
    492 	struct inode inode, *ip = &inode;
    493 	char flagbuf[16];
    494 	char *name;
    495 	mode_t type;
    496 
    497 	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
    498 	ufs_getflags(vp, ip, flagbuf);
    499 	(void)printf(" %6d %5s", ip->i_number, flagbuf);
    500 	type = ip->i_e2fs_mode & S_IFMT;
    501 	if (S_ISCHR(ip->i_e2fs_mode) || S_ISBLK(ip->i_e2fs_mode))
    502 		if (usenumflag ||
    503 		    ((name = devname(ip->i_e2fs_rdev, type)) == NULL))
    504 			(void)printf("   %2d,%-2d",
    505 			    major(ip->i_e2fs_rdev), minor(ip->i_e2fs_rdev));
    506 		else
    507 			(void)printf(" %7s", name);
    508 	else
    509 		(void)printf(" %7u", (u_int)ip->i_e2fs_size);
    510 	return (0);
    511 }
    512 
    513 void
    514 nfs_header()
    515 {
    516 
    517 	(void)printf(" FILEID NFLAG RDEV|SZ");
    518 }
    519 
    520 int
    521 nfs_print(vp)
    522 	struct vnode *vp;
    523 {
    524 	struct nfsnode nfsnode, *np = &nfsnode;
    525 	char flagbuf[16], *flags = flagbuf;
    526 	int flag;
    527 	struct vattr va;
    528 	char *name;
    529 	mode_t type;
    530 
    531 	KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
    532 	flag = np->n_flag;
    533 	if (flag & NFLUSHWANT)
    534 		*flags++ = 'W';
    535 	if (flag & NFLUSHINPROG)
    536 		*flags++ = 'P';
    537 	if (flag & NMODIFIED)
    538 		*flags++ = 'M';
    539 	if (flag & NWRITEERR)
    540 		*flags++ = 'E';
    541 	if (flag & NQNFSNONCACHE)
    542 		*flags++ = 'X';
    543 	if (flag & NQNFSWRITE)
    544 		*flags++ = 'O';
    545 	if (flag & NQNFSEVICTED)
    546 		*flags++ = 'G';
    547 	if (flag & NACC)
    548 		*flags++ = 'A';
    549 	if (flag & NUPD)
    550 		*flags++ = 'U';
    551 	if (flag & NCHG)
    552 		*flags++ = 'C';
    553 	if (flag == 0)
    554 		*flags++ = '-';
    555 	*flags = '\0';
    556 
    557 	KGETRET(np->n_vattr, &va, sizeof(va), "vnode attr");
    558 	(void)printf(" %6ld %5s", (long)va.va_fileid, flagbuf);
    559 	switch (va.va_type) {
    560 	case VCHR:
    561 		type = S_IFCHR;
    562 		goto device;
    563 
    564 	case VBLK:
    565 		type = S_IFBLK;
    566 	device:
    567 		if (usenumflag || ((name = devname(va.va_rdev, type)) == NULL))
    568 			(void)printf("   %2d,%-2d",
    569 			    major(va.va_rdev), minor(va.va_rdev));
    570 		else
    571 			(void)printf(" %7s", name);
    572 		break;
    573 	default:
    574 		(void)printf(" %7qd", (long long)np->n_size);
    575 		break;
    576 	}
    577 	return (0);
    578 }
    579 
    580 void
    581 union_header()
    582 {
    583 
    584 	(void)printf("    UPPER    LOWER");
    585 }
    586 
    587 int
    588 union_print(vp)
    589 	struct vnode *vp;
    590 {
    591 	struct union_node unode, *up = &unode;
    592 
    593 	KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode");
    594 
    595 	(void)printf(" %8lx %8lx", (long)up->un_uppervp, (long)up->un_lowervp);
    596 	return (0);
    597 }
    598 
    599 /*
    600  * Given a pointer to a mount structure in kernel space,
    601  * read it in and return a usable pointer to it.
    602  */
    603 struct mount *
    604 getmnt(maddr)
    605 	struct mount *maddr;
    606 {
    607 	static struct mtab {
    608 		struct mtab *next;
    609 		struct mount *maddr;
    610 		struct mount mount;
    611 	} *mhead = NULL;
    612 	struct mtab *mt;
    613 
    614 	for (mt = mhead; mt != NULL; mt = mt->next)
    615 		if (maddr == mt->maddr)
    616 			return (&mt->mount);
    617 	if ((mt = malloc(sizeof(struct mtab))) == NULL)
    618 		err(1, "malloc");
    619 	KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
    620 	mt->maddr = maddr;
    621 	mt->next = mhead;
    622 	mhead = mt;
    623 	return (&mt->mount);
    624 }
    625 
    626 void
    627 mount_print(mp)
    628 	struct mount *mp;
    629 {
    630 	int flags;
    631 
    632 	(void)printf("*** MOUNT %s %s on %s", ST.f_fstypename,
    633 	    ST.f_mntfromname, ST.f_mntonname);
    634 	if ((flags = mp->mnt_flag) != 0) {
    635 		int i;
    636 		const char *sep = " (";
    637 
    638 		for (i = 0; mnt_flags[i].m_flag; i++) {
    639 			if (flags & mnt_flags[i].m_flag) {
    640 				(void)printf("%s%s", sep, mnt_flags[i].m_name);
    641 				flags &= ~mnt_flags[i].m_flag;
    642 				sep = ",";
    643 			}
    644 		}
    645 		if (flags)
    646 			(void)printf("%sunknown_flags:%x", sep, flags);
    647 		(void)printf(")");
    648 	}
    649 	(void)printf("\n");
    650 }
    651 
    652 struct e_vnode *
    653 loadvnodes(avnodes)
    654 	int *avnodes;
    655 {
    656 	int mib[2];
    657 	size_t copysize;
    658 	struct e_vnode *vnodebase;
    659 
    660 	if (memf != NULL) {
    661 		/*
    662 		 * do it by hand
    663 		 */
    664 		return (kinfo_vnodes(avnodes));
    665 	}
    666 	mib[0] = CTL_KERN;
    667 	mib[1] = KERN_VNODE;
    668 	if (sysctl(mib, 2, NULL, &copysize, NULL, 0) == -1)
    669 		err(1, "sysctl: KERN_VNODE");
    670 	if ((vnodebase = malloc(copysize)) == NULL)
    671 		err(1, "malloc");
    672 	if (sysctl(mib, 2, vnodebase, &copysize, NULL, 0) == -1)
    673 		err(1, "sysctl: KERN_VNODE");
    674 	if (copysize % sizeof(struct e_vnode))
    675 		errx(1, "vnode size mismatch");
    676 	*avnodes = copysize / sizeof(struct e_vnode);
    677 
    678 	return (vnodebase);
    679 }
    680 
    681 /*
    682  * simulate what a running kernel does in in kinfo_vnode
    683  */
    684 struct e_vnode *
    685 kinfo_vnodes(avnodes)
    686 	int *avnodes;
    687 {
    688 	struct mntlist mountlist;
    689 	struct mount *mp, mount;
    690 	struct vnode *vp, vnode;
    691 	char *vbuf, *evbuf, *bp;
    692 	int num, numvnodes;
    693 
    694 #define VPTRSZ  sizeof(struct vnode *)
    695 #define VNODESZ sizeof(struct vnode)
    696 
    697 	KGET(V_NUMV, numvnodes);
    698 	if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
    699 		err(1, "malloc");
    700 	bp = vbuf;
    701 	evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
    702 	KGET(V_MOUNTLIST, mountlist);
    703 	for (num = 0, mp = mountlist.cqh_first;;
    704 	    mp = mount.mnt_list.cqe_next) {
    705 		KGET2(mp, &mount, sizeof(mount), "mount entry");
    706 		for (vp = mount.mnt_vnodelist.lh_first;
    707 		    vp != NULL; vp = vnode.v_mntvnodes.le_next) {
    708 			KGET2(vp, &vnode, sizeof(vnode), "vnode");
    709 			if ((bp + VPTRSZ + VNODESZ) > evbuf)
    710 				/* XXX - should realloc */
    711 				errx(1, "no more room for vnodes");
    712 			memmove(bp, &vp, VPTRSZ);
    713 			bp += VPTRSZ;
    714 			memmove(bp, &vnode, VNODESZ);
    715 			bp += VNODESZ;
    716 			num++;
    717 		}
    718 		if (mp == mountlist.cqh_last)
    719 			break;
    720 	}
    721 	*avnodes = num;
    722 	return ((struct e_vnode *)vbuf);
    723 }
    724 
    725 const char hdr[]="  LINE RAW CAN OUT  HWT LWT     COL STATE  SESS      PGID DISC\n";
    726 int ttyspace = 128;
    727 
    728 void
    729 ttymode()
    730 {
    731 	int ntty;
    732 	struct ttylist_head tty_head;
    733 	struct tty *tp, tty;
    734 
    735 	KGET(TTY_NTTY, ntty);
    736 	(void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s");
    737 	KGET(TTY_TTYLIST, tty_head);
    738 	(void)printf(hdr);
    739 	for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) {
    740 		KGET2(tp, &tty, sizeof tty, "tty struct");
    741 		ttyprt(&tty);
    742 	}
    743 }
    744 
    745 struct {
    746 	int flag;
    747 	char val;
    748 } ttystates[] = {
    749 	{ TS_ISOPEN,	'O'},
    750 	{ TS_DIALOUT,	'>'},
    751 	{ TS_CARR_ON,	'C'},
    752 	{ TS_TIMEOUT,	'T'},
    753 	{ TS_FLUSH,	'F'},
    754 	{ TS_BUSY,	'B'},
    755 	{ TS_ASLEEP,	'A'},
    756 	{ TS_XCLUDE,	'X'},
    757 	{ TS_TTSTOP,	'S'},
    758 	{ TS_TBLOCK,	'K'},
    759 	{ TS_ASYNC,	'Y'},
    760 	{ TS_BKSL,	'D'},
    761 	{ TS_ERASE,	'E'},
    762 	{ TS_LNCH,	'L'},
    763 	{ TS_TYPEN,	'P'},
    764 	{ TS_CNTTB,	'N'},
    765 	{ 0,	       '\0'},
    766 };
    767 
    768 void
    769 ttyprt(tp)
    770 	struct tty *tp;
    771 {
    772 	int i, j;
    773 	pid_t pgid;
    774 	char *name, state[20], buffer;
    775 	struct linesw t_linesw;
    776 
    777 	if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL)
    778 		(void)printf("0x%3x:%1x ", major(tp->t_dev), minor(tp->t_dev));
    779 	else
    780 		(void)printf("%-7s ", name);
    781 	(void)printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
    782 	(void)printf("%3d %4d %3d %7d ", tp->t_outq.c_cc,
    783 		tp->t_hiwat, tp->t_lowat, tp->t_column);
    784 	for (i = j = 0; ttystates[i].flag; i++)
    785 		if (tp->t_state&ttystates[i].flag)
    786 			state[j++] = ttystates[i].val;
    787 	if (tp->t_wopen)
    788 		state[j++] = 'W';
    789 	if (j == 0)
    790 		state[j++] = '-';
    791 	state[j] = '\0';
    792 	(void)printf("%-6s %8lX", state, (u_long)tp->t_session);
    793 	pgid = 0;
    794 	if (tp->t_pgrp != NULL)
    795 		KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
    796 	(void)printf("%6d ", pgid);
    797 	KGET2(tp->t_linesw, &t_linesw, sizeof(t_linesw),
    798 		"line discipline switch table");
    799 	name = t_linesw.l_name;
    800 	for (;;) {
    801 		KGET2(name, &buffer, sizeof(buffer), "line discipline name");
    802 		if (buffer == '\0')
    803 			break;
    804 		(void)putchar(buffer);
    805 		name++;
    806 	}
    807 	(void)putchar('\n');
    808 }
    809 
    810 void
    811 filemode()
    812 {
    813 	struct file *fp;
    814 	struct file *addr;
    815 	char *buf, flagbuf[16], *fbp;
    816 	int len, maxfile, nfile;
    817 	static char *dtypes[] = { "???", "inode", "socket" };
    818 
    819 	KGET(FNL_MAXFILE, maxfile);
    820 	if (totalflag) {
    821 		KGET(FNL_NFILE, nfile);
    822 		(void)printf("%3d/%3d files\n", nfile, maxfile);
    823 		return;
    824 	}
    825 	if (getfiles(&buf, &len) == -1)
    826 		return;
    827 	/*
    828 	 * Getfiles returns in malloc'd memory a pointer to the first file
    829 	 * structure, and then an array of file structs (whose addresses are
    830 	 * derivable from the previous entry).
    831 	 */
    832 	addr = ((struct filelist *)buf)->lh_first;
    833 	fp = (struct file *)(buf + sizeof(struct filelist));
    834 	nfile = (len - sizeof(struct filelist)) / sizeof(struct file);
    835 
    836 	(void)printf("%d/%d open files\n", nfile, maxfile);
    837 	(void)printf("   LOC   TYPE    FLG     CNT  MSG    DATA    OFFSET\n");
    838 	for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) {
    839 		if ((unsigned)fp->f_type > DTYPE_SOCKET)
    840 			continue;
    841 		(void)printf("%lx ", (long)addr);
    842 		(void)printf("%-8.8s", dtypes[fp->f_type]);
    843 		fbp = flagbuf;
    844 		if (fp->f_flag & FREAD)
    845 			*fbp++ = 'R';
    846 		if (fp->f_flag & FWRITE)
    847 			*fbp++ = 'W';
    848 		if (fp->f_flag & FAPPEND)
    849 			*fbp++ = 'A';
    850 #ifdef FSHLOCK	/* currently gone */
    851 		if (fp->f_flag & FSHLOCK)
    852 			*fbp++ = 'S';
    853 		if (fp->f_flag & FEXLOCK)
    854 			*fbp++ = 'X';
    855 #endif
    856 		if (fp->f_flag & FASYNC)
    857 			*fbp++ = 'I';
    858 		*fbp = '\0';
    859 		(void)printf("%6s  %3d", flagbuf, fp->f_count);
    860 		(void)printf("  %3d", fp->f_msgcount);
    861 		(void)printf("  %8.1lx", (long)fp->f_data);
    862 		if (fp->f_offset < 0)
    863 			(void)printf("  %qx\n", (long long)fp->f_offset);
    864 		else
    865 			(void)printf("  %qd\n", (long long)fp->f_offset);
    866 	}
    867 	free(buf);
    868 }
    869 
    870 int
    871 getfiles(abuf, alen)
    872 	char **abuf;
    873 	int *alen;
    874 {
    875 	size_t len;
    876 	int mib[2];
    877 	char *buf;
    878 
    879 	/*
    880 	 * XXX
    881 	 * Add emulation of KINFO_FILE here.
    882 	 */
    883 	if (memf != NULL)
    884 		errx(1, "files on dead kernel, not implemented\n");
    885 
    886 	mib[0] = CTL_KERN;
    887 	mib[1] = KERN_FILE;
    888 	if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
    889 		warn("sysctl: KERN_FILE");
    890 		return (-1);
    891 	}
    892 	if ((buf = malloc(len)) == NULL)
    893 		err(1, "malloc");
    894 	if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
    895 		warn("sysctl: KERN_FILE");
    896 		return (-1);
    897 	}
    898 	*abuf = buf;
    899 	*alen = len;
    900 	return (0);
    901 }
    902 
    903 void
    904 usage()
    905 {
    906 
    907 	(void)fprintf(stderr,
    908 	    "usage: pstat [-T|-f|-s|-t|-v] [-kn] [-M core] [-N system]\n");
    909 	exit(1);
    910 }
    911