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