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