Home | History | Annotate | Line # | Download | only in pstat
pstat.c revision 1.30
      1 /*	$NetBSD: pstat.c,v 1.30 1997/06/12 07:45:35 bouyer 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.30 1997/06/12 07:45:35 bouyer 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 struct nlist nl[] = {
     84 #define VM_SWAPMAP	0
     85 	{ "_swapmap" },	/* list of free swap areas */
     86 #define VM_NSWAPMAP	1
     87 	{ "_nswapmap" },/* size of the swap map */
     88 #define VM_SWDEVT	2
     89 	{ "_swdevt" },	/* list of swap devices and sizes */
     90 #define VM_NSWAP	3
     91 	{ "_nswap" },	/* size of largest swap device */
     92 #define VM_NSWDEV	4
     93 	{ "_nswdev" },	/* number of swap devices */
     94 #define VM_DMMAX	5
     95 	{ "_dmmax" },	/* maximum size of a swap block */
     96 #define	V_MOUNTLIST	6
     97 	{ "_mountlist" },	/* address of head of mount list. */
     98 #define V_NUMV		7
     99 	{ "_numvnodes" },
    100 #define	FNL_NFILE	8
    101 	{"_nfiles"},
    102 #define FNL_MAXFILE	9
    103 	{"_maxfiles"},
    104 #define TTY_NTTY	10
    105 	{"_tty_count"},
    106 #define TTY_TTYLIST	11
    107 	{"_ttylist"},
    108 #define NLMANDATORY TTY_TTYLIST	/* names up to here are mandatory */
    109 #define VM_NISWAP	NLMANDATORY + 1
    110 	{ "_niswap" },
    111 #define VM_NISWDEV	NLMANDATORY + 2
    112 	{ "_niswdev" },
    113 	{ "" }
    114 };
    115 
    116 int	usenumflag;
    117 int	totalflag;
    118 int	kflag;
    119 char	*nlistf	= NULL;
    120 char	*memf	= NULL;
    121 kvm_t	*kd;
    122 
    123 #define	SVAR(var) __STRING(var)	/* to force expansion */
    124 #define	KGET(idx, var)							\
    125 	KGET1(idx, &var, sizeof(var), SVAR(var))
    126 #define	KGET1(idx, p, s, msg)						\
    127 	KGET2(nl[idx].n_value, p, s, msg)
    128 #define	KGET2(addr, p, s, msg)						\
    129 	if (kvm_read(kd, (u_long)(addr), p, s) != s)			\
    130 		warnx("cannot read %s: %s", msg, kvm_geterr(kd))
    131 #define	KGETRET(addr, p, s, msg)					\
    132 	if (kvm_read(kd, (u_long)(addr), p, s) != s) {			\
    133 		warnx("cannot read %s: %s", msg, kvm_geterr(kd));	\
    134 		return (0);						\
    135 	}
    136 
    137 void	filemode __P((void));
    138 int	getfiles __P((char **, int *));
    139 struct mount *
    140 	getmnt __P((struct mount *));
    141 struct e_vnode *
    142 	kinfo_vnodes __P((int *));
    143 struct e_vnode *
    144 	loadvnodes __P((int *));
    145 void	mount_print __P((struct mount *));
    146 void	nfs_header __P((void));
    147 int	nfs_print __P((struct vnode *));
    148 void	swapmode __P((void));
    149 void	ttymode __P((void));
    150 void	ttyprt __P((struct tty *));
    151 void	ufs_getflags __P((struct vnode *, struct inode *, char *));
    152 void	ufs_header __P((void));
    153 int	ufs_print __P((struct vnode *));
    154 int		ext2fs_print __P((struct vnode *));
    155 void	usage __P((void));
    156 void	vnode_header __P((void));
    157 void	vnode_print __P((struct vnode *, struct vnode *));
    158 void	vnodemode __P((void));
    159 
    160 int
    161 main(argc, argv)
    162 	int argc;
    163 	char *argv[];
    164 {
    165 	extern char *optarg;
    166 	extern int optind;
    167 	int ch, i, quit, ret;
    168 	int fileflag, swapflag, ttyflag, vnodeflag;
    169 	char buf[_POSIX2_LINE_MAX];
    170 
    171 	fileflag = swapflag = ttyflag = vnodeflag = 0;
    172 	while ((ch = getopt(argc, argv, "TM:N:fiknstv")) != -1)
    173 		switch (ch) {
    174 		case 'f':
    175 			fileflag = 1;
    176 			break;
    177 		case 'M':
    178 			memf = optarg;
    179 			break;
    180 		case 'N':
    181 			nlistf = optarg;
    182 			break;
    183 		case 'n':
    184 			usenumflag = 1;
    185 			break;
    186 		case 's':
    187 			swapflag = 1;
    188 			break;
    189 		case 'T':
    190 			totalflag = 1;
    191 			break;
    192 		case 't':
    193 			ttyflag = 1;
    194 			break;
    195 		case 'k':
    196 			kflag = 1;
    197 			break;
    198 		case 'v':
    199 		case 'i':		/* Backward compatibility. */
    200 			vnodeflag = 1;
    201 			break;
    202 		default:
    203 			usage();
    204 		}
    205 	argc -= optind;
    206 	argv += optind;
    207 
    208 	/*
    209 	 * Discard setgid privileges if not the running kernel so that bad
    210 	 * guys can't print interesting stuff from kernel memory.
    211 	 */
    212 	if (nlistf != NULL || memf != NULL)
    213 		(void)setgid(getgid());
    214 
    215 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
    216 		errx(1, "kvm_openfiles: %s", buf);
    217 	if ((ret = kvm_nlist(kd, nl)) != 0) {
    218 		if (ret == -1)
    219 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
    220 		for (i = quit = 0; i <= NLMANDATORY; i++)
    221 			if (!nl[i].n_value) {
    222 				quit = 1;
    223 				warnx("undefined symbol: %s", nl[i].n_name);
    224 			}
    225 		if (quit)
    226 			exit(1);
    227 	}
    228 	if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
    229 		usage();
    230 	if (fileflag || totalflag)
    231 		filemode();
    232 	if (vnodeflag || totalflag)
    233 		vnodemode();
    234 	if (ttyflag)
    235 		ttymode();
    236 	if (swapflag || totalflag)
    237 		swapmode();
    238 	exit (0);
    239 }
    240 
    241 struct e_vnode {
    242 	struct vnode *avnode;
    243 	struct vnode vnode;
    244 };
    245 
    246 void
    247 vnodemode()
    248 {
    249 	register struct e_vnode *e_vnodebase, *endvnode, *evp;
    250 	register struct vnode *vp;
    251 	register struct mount *maddr, *mp;
    252 	int numvnodes;
    253 
    254 	e_vnodebase = loadvnodes(&numvnodes);
    255 	if (totalflag) {
    256 		(void)printf("%7d vnodes\n", numvnodes);
    257 		return;
    258 	}
    259 	endvnode = e_vnodebase + numvnodes;
    260 	(void)printf("%d active vnodes\n", numvnodes);
    261 
    262 
    263 #define ST	mp->mnt_stat
    264 	maddr = NULL;
    265 	for (evp = e_vnodebase; evp < endvnode; evp++) {
    266 		vp = &evp->vnode;
    267 		if (vp->v_mount != maddr) {
    268 			/*
    269 			 * New filesystem
    270 			 */
    271 			if ((mp = getmnt(vp->v_mount)) == NULL)
    272 				continue;
    273 			maddr = vp->v_mount;
    274 			mount_print(mp);
    275 			vnode_header();
    276 			if (!strncmp(ST.f_fstypename, MOUNT_FFS, MFSNAMELEN) ||
    277 			    !strncmp(ST.f_fstypename, MOUNT_MFS, MFSNAMELEN)) {
    278 				ufs_header();
    279 			} else if (!strncmp(ST.f_fstypename, MOUNT_NFS,
    280 			    MFSNAMELEN)) {
    281 				nfs_header();
    282 			} else if (!strncmp(ST.f_fstypename, MOUNT_EXT2FS,
    283 				MFSNAMELEN)) {
    284 				ufs_header();
    285 			}
    286 			(void)printf("\n");
    287 		}
    288 		vnode_print(evp->avnode, vp);
    289 		if (!strncmp(ST.f_fstypename, MOUNT_FFS, MFSNAMELEN) ||
    290 		    !strncmp(ST.f_fstypename, MOUNT_MFS, MFSNAMELEN)) {
    291 			ufs_print(vp);
    292 		} else if (!strncmp(ST.f_fstypename, MOUNT_NFS, MFSNAMELEN)) {
    293 			nfs_print(vp);
    294 		} else if (!strncmp(ST.f_fstypename, MOUNT_EXT2FS, MFSNAMELEN)) {
    295 			ext2fs_print(vp);
    296 		}
    297 		(void)printf("\n");
    298 	}
    299 	free(e_vnodebase);
    300 }
    301 
    302 void
    303 vnode_header()
    304 {
    305 	(void)printf("ADDR     TYP VFLAG  USE HOLD");
    306 }
    307 
    308 void
    309 vnode_print(avnode, vp)
    310 	struct vnode *avnode;
    311 	struct vnode *vp;
    312 {
    313 	char *type, flags[16];
    314 	char *fp = flags;
    315 	register int flag;
    316 
    317 	/*
    318 	 * set type
    319 	 */
    320 	switch(vp->v_type) {
    321 	case VNON:
    322 		type = "non"; break;
    323 	case VREG:
    324 		type = "reg"; break;
    325 	case VDIR:
    326 		type = "dir"; break;
    327 	case VBLK:
    328 		type = "blk"; break;
    329 	case VCHR:
    330 		type = "chr"; break;
    331 	case VLNK:
    332 		type = "lnk"; break;
    333 	case VSOCK:
    334 		type = "soc"; break;
    335 	case VFIFO:
    336 		type = "fif"; break;
    337 	case VBAD:
    338 		type = "bad"; break;
    339 	default:
    340 		type = "unk"; break;
    341 	}
    342 	/*
    343 	 * gather flags
    344 	 */
    345 	flag = vp->v_flag;
    346 	if (flag & VROOT)
    347 		*fp++ = 'R';
    348 	if (flag & VTEXT)
    349 		*fp++ = 'T';
    350 	if (flag & VSYSTEM)
    351 		*fp++ = 'S';
    352 	if (flag & VISTTY)
    353 		*fp++ = 'I';
    354 	if (flag & VXLOCK)
    355 		*fp++ = 'L';
    356 	if (flag & VXWANT)
    357 		*fp++ = 'W';
    358 	if (flag & VBWAIT)
    359 		*fp++ = 'B';
    360 	if (flag & VALIASED)
    361 		*fp++ = 'A';
    362 	if (flag & VDIROP)
    363 		*fp++ = 'D';
    364 	if (flag == 0)
    365 		*fp++ = '-';
    366 	*fp = '\0';
    367 	(void)printf("%8x %s %5s %4d %4d",
    368 	    avnode, type, flags, vp->v_usecount, vp->v_holdcnt);
    369 }
    370 
    371 void
    372 ufs_getflags(vp, ip, flags)
    373 	struct vnode *vp;
    374 	struct inode *ip;
    375 	char *flags;
    376 {
    377 	register int flag;
    378 
    379 	flag = ip->i_flag;
    380 	if (flag & IN_LOCKED)
    381 		*flags++ = 'L';
    382 	if (flag & IN_WANTED)
    383 		*flags++ = 'W';
    384 	if (flag & IN_RENAME)
    385 		*flags++ = 'R';
    386 	if (flag & IN_UPDATE)
    387 		*flags++ = 'U';
    388 	if (flag & IN_ACCESS)
    389 		*flags++ = 'A';
    390 	if (flag & IN_CHANGE)
    391 		*flags++ = 'C';
    392 	if (flag & IN_MODIFIED)
    393 		*flags++ = 'M';
    394 	if (flag & IN_SHLOCK)
    395 		*flags++ = 'S';
    396 	if (flag & IN_EXLOCK)
    397 		*flags++ = 'E';
    398 	if (flag & IN_LWAIT)
    399 		*flags++ = 'Z';
    400 	if (flag == 0)
    401 		*flags++ = '-';
    402 	*flags = '\0';
    403 
    404 }
    405 
    406 void
    407 ufs_header()
    408 {
    409 	(void)printf(" FILEID IFLAG RDEV|SZ");
    410 }
    411 
    412 int
    413 ufs_print(vp)
    414 	struct vnode *vp;
    415 {
    416 	struct inode inode, *ip = &inode;
    417 	char flagbuf[16];
    418 	char *name;
    419 	mode_t type;
    420 
    421 	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
    422 	ufs_getflags(vp, ip, flagbuf);
    423 	(void)printf(" %6d %5s", ip->i_number, flagbuf);
    424 	type = ip->i_ffs_mode & S_IFMT;
    425 	if (S_ISCHR(ip->i_ffs_mode) || S_ISBLK(ip->i_ffs_mode))
    426 		if (usenumflag || ((name = devname(ip->i_ffs_rdev, type)) == NULL))
    427 			(void)printf("   %2d,%-2d",
    428 			    major(ip->i_ffs_rdev), minor(ip->i_ffs_rdev));
    429 		else
    430 			(void)printf(" %7s", name);
    431 	else
    432 		(void)printf(" %7qd", ip->i_ffs_size);
    433 	return (0);
    434 }
    435 
    436 int
    437 ext2fs_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_e2fs_mode & S_IFMT;
    449 	if (S_ISCHR(ip->i_e2fs_mode) || S_ISBLK(ip->i_e2fs_mode))
    450 		if (usenumflag || ((name = devname(ip->i_din.e2fs_din.e2di_rdev,
    451 			type)) == NULL))
    452 			(void)printf("   %2d,%-2d",
    453 			    major(ip->i_din.e2fs_din.e2di_rdev),
    454 						minor(ip->i_din.e2fs_din.e2di_rdev));
    455 		else
    456 			(void)printf(" %7s", name);
    457 	else
    458 		(void)printf(" %7u", (u_int)ip->i_e2fs_size);
    459 	return (0);
    460 }
    461 
    462 void
    463 nfs_header()
    464 {
    465 	(void)printf(" FILEID NFLAG RDEV|SZ");
    466 }
    467 
    468 int
    469 nfs_print(vp)
    470 	struct vnode *vp;
    471 {
    472 	struct nfsnode nfsnode, *np = &nfsnode;
    473 	char flagbuf[16], *flags = flagbuf;
    474 	register int flag;
    475 	char *name;
    476 	mode_t type;
    477 
    478 	KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
    479 	flag = np->n_flag;
    480 	if (flag & NFLUSHWANT)
    481 		*flags++ = 'W';
    482 	if (flag & NFLUSHINPROG)
    483 		*flags++ = 'P';
    484 	if (flag & NMODIFIED)
    485 		*flags++ = 'M';
    486 	if (flag & NWRITEERR)
    487 		*flags++ = 'E';
    488 	if (flag & NQNFSNONCACHE)
    489 		*flags++ = 'X';
    490 	if (flag & NQNFSWRITE)
    491 		*flags++ = 'O';
    492 	if (flag & NQNFSEVICTED)
    493 		*flags++ = 'G';
    494 	if (flag == 0)
    495 		*flags++ = '-';
    496 	*flags = '\0';
    497 
    498 #define VT	np->n_vattr
    499 	(void)printf(" %6d %5s", VT.va_fileid, flagbuf);
    500 	type = VT.va_mode & S_IFMT;
    501 	if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode))
    502 		if (usenumflag || ((name = devname(VT.va_rdev, type)) == NULL))
    503 			(void)printf("   %2d,%-2d",
    504 			    major(VT.va_rdev), minor(VT.va_rdev));
    505 		else
    506 			(void)printf(" %7s", name);
    507 	else
    508 		(void)printf(" %7qd", np->n_size);
    509 	return (0);
    510 }
    511 
    512 /*
    513  * Given a pointer to a mount structure in kernel space,
    514  * read it in and return a usable pointer to it.
    515  */
    516 struct mount *
    517 getmnt(maddr)
    518 	struct mount *maddr;
    519 {
    520 	static struct mtab {
    521 		struct mtab *next;
    522 		struct mount *maddr;
    523 		struct mount mount;
    524 	} *mhead = NULL;
    525 	register struct mtab *mt;
    526 
    527 	for (mt = mhead; mt != NULL; mt = mt->next)
    528 		if (maddr == mt->maddr)
    529 			return (&mt->mount);
    530 	if ((mt = malloc(sizeof(struct mtab))) == NULL)
    531 		err(1, NULL);
    532 	KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
    533 	mt->maddr = maddr;
    534 	mt->next = mhead;
    535 	mhead = mt;
    536 	return (&mt->mount);
    537 }
    538 
    539 void
    540 mount_print(mp)
    541 	struct mount *mp;
    542 {
    543 	register int flags;
    544 	char *type;
    545 
    546 #define ST	mp->mnt_stat
    547 	(void)printf("*** MOUNT ");
    548 	(void)printf("%.*s %s on %s", MFSNAMELEN, ST.f_fstypename,
    549 	    ST.f_mntfromname, ST.f_mntonname);
    550 	if (flags = mp->mnt_flag) {
    551 		char *comma = "(";
    552 
    553 		putchar(' ');
    554 		/* user visable flags */
    555 		if (flags & MNT_RDONLY) {
    556 			(void)printf("%srdonly", comma);
    557 			flags &= ~MNT_RDONLY;
    558 			comma = ",";
    559 		}
    560 		if (flags & MNT_SYNCHRONOUS) {
    561 			(void)printf("%ssynchronous", comma);
    562 			flags &= ~MNT_SYNCHRONOUS;
    563 			comma = ",";
    564 		}
    565 		if (flags & MNT_NOEXEC) {
    566 			(void)printf("%snoexec", comma);
    567 			flags &= ~MNT_NOEXEC;
    568 			comma = ",";
    569 		}
    570 		if (flags & MNT_NOSUID) {
    571 			(void)printf("%snosuid", comma);
    572 			flags &= ~MNT_NOSUID;
    573 			comma = ",";
    574 		}
    575 		if (flags & MNT_NODEV) {
    576 			(void)printf("%snodev", comma);
    577 			flags &= ~MNT_NODEV;
    578 			comma = ",";
    579 		}
    580 		if (flags & MNT_UNION) {
    581 			(void)printf("%sunion", comma);
    582 			flags &= ~MNT_UNION;
    583 			comma = ",";
    584 		}
    585 		if (flags & MNT_ASYNC) {
    586 			(void)printf("%sasync", comma);
    587 			flags &= ~MNT_ASYNC;
    588 			comma = ",";
    589 		}
    590 		if (flags & MNT_NOCOREDUMP) {
    591 			(void)printf("%snocoredump", comma);
    592 			flags &= ~MNT_NOCOREDUMP;
    593 			comma = ",";
    594 		}
    595 		if (flags & MNT_EXRDONLY) {
    596 			(void)printf("%sexrdonly", comma);
    597 			flags &= ~MNT_EXRDONLY;
    598 			comma = ",";
    599 		}
    600 		if (flags & MNT_EXPORTED) {
    601 			(void)printf("%sexport", comma);
    602 			flags &= ~MNT_EXPORTED;
    603 			comma = ",";
    604 		}
    605 		if (flags & MNT_DEFEXPORTED) {
    606 			(void)printf("%sdefdexported", comma);
    607 			flags &= ~MNT_DEFEXPORTED;
    608 			comma = ",";
    609 		}
    610 		if (flags & MNT_EXPORTANON) {
    611 			(void)printf("%sexportanon", comma);
    612 			flags &= ~MNT_EXPORTANON;
    613 			comma = ",";
    614 		}
    615 		if (flags & MNT_EXKERB) {
    616 			(void)printf("%sexkerb", comma);
    617 			flags &= ~MNT_EXKERB;
    618 			comma = ",";
    619 		}
    620 		if (flags & MNT_LOCAL) {
    621 			(void)printf("%slocal", comma);
    622 			flags &= ~MNT_LOCAL;
    623 			comma = ",";
    624 		}
    625 		if (flags & MNT_QUOTA) {
    626 			(void)printf("%squota", comma);
    627 			flags &= ~MNT_QUOTA;
    628 			comma = ",";
    629 		}
    630 		if (flags & MNT_ROOTFS) {
    631 			(void)printf("%srootfs", comma);
    632 			flags &= ~MNT_ROOTFS;
    633 			comma = ",";
    634 		}
    635 		/* filesystem control flags */
    636 		if (flags & MNT_UPDATE) {
    637 			(void)printf("%supdate", comma);
    638 			flags &= ~MNT_UPDATE;
    639 			comma = ",";
    640 		}
    641 		if (flags & MNT_MLOCK) {
    642 			(void)printf("%slock", comma);
    643 			flags &= ~MNT_MLOCK;
    644 			comma = ",";
    645 		}
    646 		if (flags & MNT_MWAIT) {
    647 			(void)printf("%swait", comma);
    648 			flags &= ~MNT_MWAIT;
    649 			comma = ",";
    650 		}
    651 		if (flags & MNT_MPBUSY) {
    652 			(void)printf("%sbusy", comma);
    653 			flags &= ~MNT_MPBUSY;
    654 			comma = ",";
    655 		}
    656 		if (flags & MNT_MPWANT) {
    657 			(void)printf("%swant", comma);
    658 			flags &= ~MNT_MPWANT;
    659 			comma = ",";
    660 		}
    661 		if (flags & MNT_UNMOUNT) {
    662 			(void)printf("%sunmount", comma);
    663 			flags &= ~MNT_UNMOUNT;
    664 			comma = ",";
    665 		}
    666 		if (flags)
    667 			(void)printf("%sunknown_flags:%x", comma, flags);
    668 		(void)printf(")");
    669 	}
    670 	(void)printf("\n");
    671 #undef ST
    672 }
    673 
    674 struct e_vnode *
    675 loadvnodes(avnodes)
    676 	int *avnodes;
    677 {
    678 	int mib[2];
    679 	size_t copysize;
    680 	struct e_vnode *vnodebase;
    681 
    682 	if (memf != NULL) {
    683 		/*
    684 		 * do it by hand
    685 		 */
    686 		return (kinfo_vnodes(avnodes));
    687 	}
    688 	mib[0] = CTL_KERN;
    689 	mib[1] = KERN_VNODE;
    690 	if (sysctl(mib, 2, NULL, &copysize, NULL, 0) == -1)
    691 		err(1, "sysctl: KERN_VNODE");
    692 	if ((vnodebase = malloc(copysize)) == NULL)
    693 		err(1, NULL);
    694 	if (sysctl(mib, 2, vnodebase, &copysize, NULL, 0) == -1)
    695 		err(1, "sysctl: KERN_VNODE");
    696 	if (copysize % sizeof(struct e_vnode))
    697 		errx(1, "vnode size mismatch");
    698 	*avnodes = copysize / sizeof(struct e_vnode);
    699 
    700 	return (vnodebase);
    701 }
    702 
    703 /*
    704  * simulate what a running kernel does in in kinfo_vnode
    705  */
    706 struct e_vnode *
    707 kinfo_vnodes(avnodes)
    708 	int *avnodes;
    709 {
    710 	struct mntlist mountlist;
    711 	struct mount *mp, mount;
    712 	struct vnode *vp, vnode;
    713 	char *vbuf, *evbuf, *bp;
    714 	int num, numvnodes;
    715 
    716 #define VPTRSZ  sizeof(struct vnode *)
    717 #define VNODESZ sizeof(struct vnode)
    718 
    719 	KGET(V_NUMV, numvnodes);
    720 	if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
    721 		err(1, NULL);
    722 	bp = vbuf;
    723 	evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
    724 	KGET(V_MOUNTLIST, mountlist);
    725 	for (num = 0, mp = mountlist.cqh_first; ; mp = mount.mnt_list.cqe_next) {
    726 		KGET2(mp, &mount, sizeof(mount), "mount entry");
    727 		for (vp = mount.mnt_vnodelist.lh_first;
    728 		    vp != NULL; vp = vnode.v_mntvnodes.le_next) {
    729 			KGET2(vp, &vnode, sizeof(vnode), "vnode");
    730 			if ((bp + VPTRSZ + VNODESZ) > evbuf)
    731 				/* XXX - should realloc */
    732 				errx(1, "no more room for vnodes");
    733 			memmove(bp, &vp, VPTRSZ);
    734 			bp += VPTRSZ;
    735 			memmove(bp, &vnode, VNODESZ);
    736 			bp += VNODESZ;
    737 			num++;
    738 		}
    739 		if (mp == mountlist.cqh_last)
    740 			break;
    741 	}
    742 	*avnodes = num;
    743 	return ((struct e_vnode *)vbuf);
    744 }
    745 
    746 char hdr[]="  LINE  RAW  CAN  OUT  HWT LWT    COL STATE      SESS  PGID DISC\n";
    747 
    748 void
    749 ttymode()
    750 {
    751 	int ntty, i;
    752 	struct ttylist_head tty_head;
    753 	struct tty *tp, tty;
    754 
    755 	KGET(TTY_NTTY, ntty);
    756 	(void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s");
    757 	KGET(TTY_TTYLIST, tty_head);
    758 	(void)printf(hdr);
    759 	for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) {
    760 		KGET2(tp, &tty, sizeof tty, "tty struct");
    761 		ttyprt(&tty);
    762 	}
    763 }
    764 
    765 struct {
    766 	int flag;
    767 	char val;
    768 } ttystates[] = {
    769 	{ TS_WOPEN,	'W'},
    770 	{ TS_ISOPEN,	'O'},
    771 	{ TS_CARR_ON,	'C'},
    772 	{ TS_TIMEOUT,	'T'},
    773 	{ TS_FLUSH,	'F'},
    774 	{ TS_BUSY,	'B'},
    775 	{ TS_ASLEEP,	'A'},
    776 	{ TS_XCLUDE,	'X'},
    777 	{ TS_TTSTOP,	'S'},
    778 	{ TS_TBLOCK,	'K'},
    779 	{ TS_ASYNC,	'Y'},
    780 	{ TS_BKSL,	'D'},
    781 	{ TS_ERASE,	'E'},
    782 	{ TS_LNCH,	'L'},
    783 	{ TS_TYPEN,	'P'},
    784 	{ TS_CNTTB,	'N'},
    785 	{ 0,	       '\0'},
    786 };
    787 
    788 void
    789 ttyprt(tp)
    790 	register struct tty *tp;
    791 {
    792 	register int i, j;
    793 	pid_t pgid;
    794 	char *name, state[20];
    795 
    796 	if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL)
    797 		(void)printf("0x%3x:%1x ", major(tp->t_dev), minor(tp->t_dev));
    798 	else
    799 		(void)printf("%-7s ", name);
    800 	(void)printf("%3d %4d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
    801 	(void)printf("%4d %4d %3d %6d ", tp->t_outq.c_cc,
    802 		tp->t_hiwat, tp->t_lowat, tp->t_column);
    803 	for (i = j = 0; ttystates[i].flag; i++)
    804 		if (tp->t_state&ttystates[i].flag)
    805 			state[j++] = ttystates[i].val;
    806 	if (j == 0)
    807 		state[j++] = '-';
    808 	state[j] = '\0';
    809 	(void)printf("%-6s %8x", state, (u_long)tp->t_session & ~KERNBASE);
    810 	pgid = 0;
    811 	if (tp->t_pgrp != NULL)
    812 		KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
    813 	(void)printf("%6d ", pgid);
    814 	switch (tp->t_line) {
    815 	case TTYDISC:
    816 		(void)printf("term\n");
    817 		break;
    818 	case TABLDISC:
    819 		(void)printf("tab\n");
    820 		break;
    821 	case SLIPDISC:
    822 		(void)printf("slip\n");
    823 		break;
    824 	case PPPDISC:
    825 		(void)printf("ppp\n");
    826 		break;
    827 	case STRIPDISC:
    828 		(void)printf("strip\n");
    829 		break;
    830 	default:
    831 		(void)printf("%d\n", tp->t_line);
    832 		break;
    833 	}
    834 }
    835 
    836 void
    837 filemode()
    838 {
    839 	register struct file *fp;
    840 	struct file *addr;
    841 	char *buf, flagbuf[16], *fbp;
    842 	int len, maxfile, nfile;
    843 	static char *dtypes[] = { "???", "inode", "socket" };
    844 
    845 	KGET(FNL_MAXFILE, maxfile);
    846 	if (totalflag) {
    847 		KGET(FNL_NFILE, nfile);
    848 		(void)printf("%3d/%3d files\n", nfile, maxfile);
    849 		return;
    850 	}
    851 	if (getfiles(&buf, &len) == -1)
    852 		return;
    853 	/*
    854 	 * Getfiles returns in malloc'd memory a pointer to the first file
    855 	 * structure, and then an array of file structs (whose addresses are
    856 	 * derivable from the previous entry).
    857 	 */
    858 	addr = ((struct filelist *)buf)->lh_first;
    859 	fp = (struct file *)(buf + sizeof(struct filelist));
    860 	nfile = (len - sizeof(struct filelist)) / sizeof(struct file);
    861 
    862 	(void)printf("%d/%d open files\n", nfile, maxfile);
    863 	(void)printf("   LOC   TYPE    FLG     CNT  MSG    DATA    OFFSET\n");
    864 	for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) {
    865 		if ((unsigned)fp->f_type > DTYPE_SOCKET)
    866 			continue;
    867 		(void)printf("%x ", addr);
    868 		(void)printf("%-8.8s", dtypes[fp->f_type]);
    869 		fbp = flagbuf;
    870 		if (fp->f_flag & FREAD)
    871 			*fbp++ = 'R';
    872 		if (fp->f_flag & FWRITE)
    873 			*fbp++ = 'W';
    874 		if (fp->f_flag & FAPPEND)
    875 			*fbp++ = 'A';
    876 #ifdef FSHLOCK	/* currently gone */
    877 		if (fp->f_flag & FSHLOCK)
    878 			*fbp++ = 'S';
    879 		if (fp->f_flag & FEXLOCK)
    880 			*fbp++ = 'X';
    881 #endif
    882 		if (fp->f_flag & FASYNC)
    883 			*fbp++ = 'I';
    884 		*fbp = '\0';
    885 		(void)printf("%6s  %3d", flagbuf, fp->f_count);
    886 		(void)printf("  %3d", fp->f_msgcount);
    887 		(void)printf("  %8.1x", fp->f_data);
    888 		if (fp->f_offset < 0)
    889 			(void)printf("  %qx\n", fp->f_offset);
    890 		else
    891 			(void)printf("  %qd\n", fp->f_offset);
    892 	}
    893 	free(buf);
    894 }
    895 
    896 int
    897 getfiles(abuf, alen)
    898 	char **abuf;
    899 	int *alen;
    900 {
    901 	size_t len;
    902 	int mib[2];
    903 	char *buf;
    904 
    905 	/*
    906 	 * XXX
    907 	 * Add emulation of KINFO_FILE here.
    908 	 */
    909 	if (memf != NULL)
    910 		errx(1, "files on dead kernel, not implemented\n");
    911 
    912 	mib[0] = CTL_KERN;
    913 	mib[1] = KERN_FILE;
    914 	if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
    915 		warn("sysctl: KERN_FILE");
    916 		return (-1);
    917 	}
    918 	if ((buf = malloc(len)) == NULL)
    919 		err(1, NULL);
    920 	if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
    921 		warn("sysctl: KERN_FILE");
    922 		return (-1);
    923 	}
    924 	*abuf = buf;
    925 	*alen = len;
    926 	return (0);
    927 }
    928 
    929 /*
    930  * swapmode is based on a program called swapinfo written
    931  * by Kevin Lahey <kml (at) rokkaku.atl.ga.us>.
    932  */
    933 void
    934 swapmode()
    935 {
    936 	char *header;
    937 	int hlen, nswap, nswdev, dmmax, nswapmap, niswap, niswdev;
    938 	int s, e, div, i, l, avail, nfree, npfree, used;
    939 	struct swdevt *sw;
    940 	long blocksize, *perdev;
    941 	struct map *swapmap, *kswapmap;
    942 	struct mapent *mp;
    943 
    944 	KGET(VM_NSWAP, nswap);
    945 	KGET(VM_NSWDEV, nswdev);
    946 	KGET(VM_DMMAX, dmmax);
    947 	KGET(VM_NSWAPMAP, nswapmap);
    948 	KGET(VM_SWAPMAP, kswapmap);	/* kernel `swapmap' is a pointer */
    949 	if ((sw = malloc(nswdev * sizeof(*sw))) == NULL ||
    950 	    (perdev = malloc(nswdev * sizeof(*perdev))) == NULL ||
    951 	    (mp = malloc(nswapmap * sizeof(*mp))) == NULL)
    952 		err(1, "malloc");
    953 	KGET1(VM_SWDEVT, sw, nswdev * sizeof(*sw), "swdevt");
    954 	KGET2((long)kswapmap, mp, nswapmap * sizeof(*mp), "swapmap");
    955 
    956 	/* Supports sequential swap */
    957 	if (nl[VM_NISWAP].n_value != 0) {
    958 		KGET(VM_NISWAP, niswap);
    959 		KGET(VM_NISWDEV, niswdev);
    960 	} else {
    961 		niswap = nswap;
    962 		niswdev = nswdev;
    963 	}
    964 
    965 	/* First entry in map is `struct map'; rest are mapent's. */
    966 	swapmap = (struct map *)mp;
    967 	if (nswapmap != swapmap->m_limit - (struct mapent *)kswapmap)
    968 		errx(1, "panic: nswapmap goof");
    969 
    970 	/* Count up swap space. */
    971 	nfree = 0;
    972 	memset(perdev, 0, nswdev * sizeof(*perdev));
    973 	for (mp++; mp->m_addr != 0; mp++) {
    974 		s = mp->m_addr;			/* start of swap region */
    975 		e = mp->m_addr + mp->m_size;	/* end of region */
    976 		nfree += mp->m_size;
    977 
    978 		/*
    979 		 * Swap space is split up among the configured disks.
    980 		 *
    981 		 * For interleaved swap devices, the first dmmax blocks
    982 		 * of swap space some from the first disk, the next dmmax
    983 		 * blocks from the next, and so on up to niswap blocks.
    984 		 *
    985 		 * Sequential swap devices follow the interleaved devices
    986 		 * (i.e. blocks starting at niswap) in the order in which
    987 		 * they appear in the swdev table.  The size of each device
    988 		 * will be a multiple of dmmax.
    989 		 *
    990 		 * The list of free space joins adjacent free blocks,
    991 		 * ignoring device boundries.  If we want to keep track
    992 		 * of this information per device, we'll just have to
    993 		 * extract it ourselves.  We know that dmmax-sized chunks
    994 		 * cannot span device boundaries (interleaved or sequential)
    995 		 * so we loop over such chunks assigning them to devices.
    996 		 */
    997 		i = -1;
    998 		while (s < e) {		/* XXX this is inefficient */
    999 			int bound = roundup(s+1, dmmax);
   1000 
   1001 			if (bound > e)
   1002 				bound = e;
   1003 			if (bound <= niswap) {
   1004 				/* Interleaved swap chunk. */
   1005 				if (i == -1)
   1006 					i = (s / dmmax) % niswdev;
   1007 				perdev[i] += bound - s;
   1008 				if (++i >= niswdev)
   1009 					i = 0;
   1010 			} else {
   1011 				/* Sequential swap chunk. */
   1012 				if (i < niswdev) {
   1013 					i = niswdev;
   1014 					l = niswap + sw[i].sw_nblks;
   1015 				}
   1016 				while (s >= l) {
   1017 					/* XXX don't die on bogus blocks */
   1018 					if (i == nswdev-1)
   1019 						break;
   1020 					l += sw[++i].sw_nblks;
   1021 				}
   1022 				perdev[i] += bound - s;
   1023 			}
   1024 			s = bound;
   1025 		}
   1026 	}
   1027 
   1028 	if (kflag) {
   1029 		header = "1K-blocks";
   1030 		blocksize = 1024;
   1031 		hlen = strlen(header);
   1032 	} else
   1033 		header = getbsize(&hlen, &blocksize);
   1034 	if (!totalflag)
   1035 		(void)printf("%-11s %*s %8s %8s %8s  %s\n",
   1036 		    "Device", hlen, header,
   1037 		    "Used", "Avail", "Capacity", "Type");
   1038 	div = blocksize / 512;
   1039 	avail = npfree = 0;
   1040 	for (i = 0; i < nswdev; i++) {
   1041 		int xsize, xfree;
   1042 
   1043 		/*
   1044 		 * Don't report statistics for partitions which have not
   1045 		 * yet been activated via swapon(8).
   1046 		 */
   1047 		if (!(sw[i].sw_flags & SW_FREED))
   1048 			continue;
   1049 
   1050 		if (!totalflag)
   1051 			(void)printf("/dev/%-6s %*d ",
   1052 			    devname(sw[i].sw_dev, S_IFBLK),
   1053 			    hlen, sw[i].sw_nblks / div);
   1054 
   1055 		xsize = sw[i].sw_nblks;
   1056 		xfree = perdev[i];
   1057 		used = xsize - xfree;
   1058 		npfree++;
   1059 		avail += xsize;
   1060 		if (totalflag)
   1061 			continue;
   1062 		(void)printf("%8d %8d %5.0f%%    %s\n",
   1063 		    used / div, xfree / div,
   1064 		    (double)used / (double)xsize * 100.0,
   1065 		    (sw[i].sw_flags & SW_SEQUENTIAL) ?
   1066 			     "Sequential" : "Interleaved");
   1067 	}
   1068 
   1069 	/*
   1070 	 * If only one partition has been set up via swapon(8), we don't
   1071 	 * need to bother with totals.
   1072 	 */
   1073 	used = avail - nfree;
   1074 	if (totalflag) {
   1075 		(void)printf("%dM/%dM swap space\n", used / 2048, avail / 2048);
   1076 		return;
   1077 	}
   1078 	if (npfree > 1) {
   1079 		(void)printf("%-11s %*d %8d %8d %5.0f%%\n",
   1080 		    "Total", hlen, avail / div, used / div, nfree / div,
   1081 		    (double)used / (double)avail * 100.0);
   1082 	}
   1083 }
   1084 
   1085 void
   1086 usage()
   1087 {
   1088 	(void)fprintf(stderr,
   1089 	    "usage: pstat [-T|-f|-s|-t|-v] [-kn] [-M core] [-N system]\n");
   1090 	exit(1);
   1091 }
   1092