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