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