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