xref: /netbsd/usr.sbin/pstat/pstat.c (revision 69f9e170)
1 /*	$NetBSD: pstat.c,v 1.112 2009/03/08 12:52:08 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 1980, 1991, 1993, 1994
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. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\
35  The Regents of the University of California.  All rights reserved.");
36 #endif /* not lint */
37 
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)pstat.c	8.16 (Berkeley) 5/9/95";
41 #else
42 __RCSID("$NetBSD: pstat.c,v 1.112 2009/03/08 12:52:08 ad Exp $");
43 #endif
44 #endif /* not lint */
45 
46 #define _KERNEL
47 #include <sys/types.h>
48 #undef _KERNEL
49 #include <sys/param.h>
50 #include <sys/time.h>
51 #include <sys/vnode.h>
52 #include <sys/ucred.h>
53 #include <stdbool.h>
54 #define _KERNEL
55 #include <sys/file.h>
56 #include <ufs/ufs/inode.h>
57 #define NFS
58 #include <sys/mount.h>
59 #undef NFS
60 #include <sys/uio.h>
61 #include <miscfs/genfs/layer.h>
62 #undef _KERNEL
63 #include <sys/stat.h>
64 #include <nfs/nfsproto.h>
65 #include <nfs/rpcv2.h>
66 #include <nfs/nfs.h>
67 #include <nfs/nfsnode.h>
68 #include <sys/ioctl.h>
69 #include <sys/tty.h>
70 #include <sys/conf.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 #include "swapctl.h"
84 
85 struct nlist nl[] = {
86 #define	V_MOUNTLIST	0
87 	{ "_mountlist" },	/* address of head of mount list. */
88 #define	V_NUMV		1
89 	{ "_numvnodes" },
90 #define	FNL_NFILE	2
91 	{ "_nfiles" },
92 #define FNL_MAXFILE	3
93 	{ "_maxfiles" },
94 #define TTY_NTTY	4
95 	{ "_tty_count" },
96 #define TTY_TTYLIST	5
97 	{ "_ttylist" },
98 #define NLMANDATORY TTY_TTYLIST	/* names up to here are mandatory */
99 	{ "" }
100 };
101 
102 int	usenumflag;
103 int	totalflag;
104 int	kflag;
105 int	hflag;
106 char	*nlistf	= NULL;
107 char	*memf	= NULL;
108 kvm_t	*kd;
109 
110 static const char * const dtypes[] = { DTYPE_NAMES };
111 
112 
113 static const struct {
114 	u_int m_flag;
115 	u_int m_visible;
116 	const char *m_name;
117 } mnt_flags[] = {
118 	__MNT_FLAGS
119 };
120 
121 struct flagbit_desc {
122 	u_int fd_flags;
123 	char fd_mark;
124 };
125 
126 #define	SVAR(var) __STRING(var)	/* to force expansion */
127 #define	KGET(idx, var)							\
128 	KGET1(idx, &var, sizeof(var), SVAR(var))
129 #define	KGET1(idx, p, s, msg)						\
130 	KGET2(nl[idx].n_value, p, s, msg)
131 #define	KGET2(addr, p, s, msg) do {					\
132 	if (kvm_read(kd, (u_long)(addr), p, s) != s)			\
133 		warnx("cannot read %s: %s", msg, kvm_geterr(kd));	\
134 } while (/* CONSTCOND */0)
135 #define	KGETRET(addr, p, s, msg) do {					\
136 	if (kvm_read(kd, (u_long)(addr), p, s) != s) {			\
137 		warnx("cannot read %s: %s", msg, kvm_geterr(kd));	\
138 		return (0);						\
139 	}								\
140 } while (/* CONSTCOND */0)
141 
142 #if 1				/* This is copied from vmstat/vmstat.c */
143 /*
144  * Print single word.  `ovflow' is number of characters didn't fit
145  * on the last word.  `fmt' is a format string to print this word.
146  * It must contain asterisk for field width.  `width' is a width
147  * occupied by this word.  `fixed' is a number of constant chars in
148  * `fmt'.  `val' is a value to be printed using format string `fmt'.
149  */
150 #define	PRWORD(ovflw, fmt, width, fixed, val) do {	\
151 	(ovflw) += printf((fmt),			\
152 	    (width) - (fixed) - (ovflw) > 0 ?		\
153 	    (width) - (fixed) - (ovflw) : 0,		\
154 	    (val)) - (width);				\
155 	if ((ovflw) < 0)				\
156 		(ovflw) = 0;				\
157 } while (/* CONSTCOND */0)
158 #endif
159 
160 void	filemode(void);
161 int	getfiles(char **, int *, char **);
162 int	getflags(const struct flagbit_desc *, char *, u_int);
163 struct mount *
164 	getmnt(struct mount *);
165 char *	kinfo_vnodes(int *);
166 void	layer_header(void);
167 int	layer_print(struct vnode *, int);
168 char *	loadvnodes(int *);
169 int	main(int, char **);
170 void	mount_print(struct mount *);
171 void	nfs_header(void);
172 int	nfs_print(struct vnode *, int);
173 void	ttymode(void);
174 void	ttyprt(struct tty *);
175 void	ufs_header(void);
176 int	ufs_print(struct vnode *, int);
177 int	ext2fs_print(struct vnode *, int);
178 void	usage(void);
179 void	vnode_header(void);
180 int	vnode_print(struct vnode *, struct vnode *);
181 void	vnodemode(void);
182 
183 int
184 main(int argc, char *argv[])
185 {
186 	int ch, i, quit, ret, use_sysctl;
187 	int fileflag, swapflag, ttyflag, vnodeflag;
188 	gid_t egid = getegid();
189 	char buf[_POSIX2_LINE_MAX];
190 
191 	setegid(getgid());
192 	fileflag = swapflag = ttyflag = vnodeflag = 0;
193 	while ((ch = getopt(argc, argv, "TM:N:fghikmnstv")) != -1)
194 		switch (ch) {
195 		case 'f':
196 			fileflag = 1;
197 			break;
198 		case 'M':
199 			memf = optarg;
200 			break;
201 		case 'N':
202 			nlistf = optarg;
203 			break;
204 		case 'n':
205 			usenumflag = 1;
206 			break;
207 		case 's':
208 			swapflag = 1;
209 			break;
210 		case 'T':
211 			totalflag = 1;
212 			break;
213 		case 't':
214 			ttyflag = 1;
215 			break;
216 		case 'k':
217 			kflag = 1;
218 			break;
219 		case 'g':
220 			kflag = 3; /* 1k ^ 3 */
221 			break;
222 		case 'h':
223 			hflag = 1;
224 			break;
225 		case 'm':
226 			kflag = 2; /* 1k ^ 2 */
227 			break;
228 		case 'v':
229 		case 'i':		/* Backward compatibility. */
230 			vnodeflag = 1;
231 			break;
232 		default:
233 			usage();
234 		}
235 	argc -= optind;
236 	argv += optind;
237 
238 	/*
239 	 * Discard setgid privileges.  If not the running kernel, we toss
240 	 * them away totally so that bad guys can't print interesting stuff
241 	 * from kernel memory, otherwise switch back to kmem for the
242 	 * duration of the kvm_openfiles() call.
243 	 */
244 	if (nlistf != NULL || memf != NULL)
245 		(void)setgid(getgid());
246 	else
247 		(void)setegid(egid);
248 
249 	use_sysctl = (nlistf == NULL && memf == NULL);
250 
251 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
252 		errx(1, "kvm_openfiles: %s", buf);
253 
254 	/* get rid of it now anyway */
255 	if (nlistf == NULL && memf == NULL)
256 		(void)setgid(getgid());
257 	if ((ret = kvm_nlist(kd, nl)) != 0) {
258 		if (ret == -1)
259 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
260 		for (i = quit = 0; i <= NLMANDATORY; i++)
261 			if (!nl[i].n_value) {
262 				quit = 1;
263 				warnx("undefined symbol: %s", nl[i].n_name);
264 			}
265 		if (quit)
266 			exit(1);
267 	}
268 	if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
269 		usage();
270 	if (fileflag || totalflag)
271 		filemode();
272 	if (vnodeflag || totalflag)
273 		vnodemode();
274 	if (ttyflag)
275 		ttymode();
276 	if (swapflag || totalflag)
277 		if (use_sysctl)
278 			list_swap(0, kflag, 0, totalflag, 1, hflag);
279 	exit(0);
280 }
281 
282 #define	VPTRSZ  sizeof(struct vnode *)
283 #define	VNODESZ sizeof(struct vnode)
284 #define	PTRSTRWIDTH ((int)sizeof(void *) * 2) /* Width of resulting string
285 						 when pointer is printed
286 						 in hexadecimal. */
287 
288 static void
289 devprintf(char *buf, size_t buflen, dev_t dev)
290 {
291 	(void)snprintf(buf, buflen, "%llu,%llu",
292 	    (unsigned long long)major(dev),
293 	    (unsigned long long)minor(dev));
294 }
295 
296 void
297 vnodemode(void)
298 {
299 	char *e_vnodebase, *endvnode, *evp;
300 	struct vnode *vp;
301 	struct mount *maddr, *mp;
302 	int numvnodes, ovflw;
303 	int (*vnode_fsprint) (struct vnode *, int); /* per-fs data printer */
304 
305 	mp = NULL;
306 	e_vnodebase = loadvnodes(&numvnodes);
307 	if (totalflag) {
308 		(void)printf("%7d vnodes\n", numvnodes);
309 		goto out;
310 	}
311 	endvnode = e_vnodebase + numvnodes * (VPTRSZ + VNODESZ);
312 	(void)printf("%d active vnodes\n", numvnodes);
313 
314 #define	ST	mp->mnt_stat
315 #define	FSTYPE_IS(mp, name)						\
316 	(strncmp((mp)->mnt_stat.f_fstypename, (name), 			\
317 	sizeof((mp)->mnt_stat.f_fstypename)) == 0)
318 	maddr = NULL;
319 	vnode_fsprint = NULL;
320 	for (evp = e_vnodebase; evp < endvnode; evp += VPTRSZ + VNODESZ) {
321 		vp = (struct vnode *)(evp + VPTRSZ);
322 		if (vp->v_mount != maddr) {
323 			/*
324 			 * New filesystem
325 			 */
326 			if ((mp = getmnt(vp->v_mount)) == NULL)
327 				continue;
328 			maddr = vp->v_mount;
329 			mount_print(mp);
330 			vnode_header();
331 			if (FSTYPE_IS(mp, MOUNT_FFS) ||
332 			    FSTYPE_IS(mp, MOUNT_MFS)) {
333 				ufs_header();
334 				vnode_fsprint = ufs_print;
335 			} else if (FSTYPE_IS(mp, MOUNT_NFS)) {
336 				nfs_header();
337 				vnode_fsprint = nfs_print;
338 			} else if (FSTYPE_IS(mp, MOUNT_EXT2FS)) {
339 				ufs_header();
340 				vnode_fsprint = ext2fs_print;
341 			} else if (FSTYPE_IS(mp, MOUNT_NULL) ||
342 			    FSTYPE_IS(mp, MOUNT_OVERLAY) ||
343 			    FSTYPE_IS(mp, MOUNT_UMAP)) {
344 				layer_header();
345 				vnode_fsprint = layer_print;
346 			} else
347 				vnode_fsprint = NULL;
348 			(void)printf("\n");
349 		}
350 		ovflw = vnode_print(*(struct vnode **)evp, vp);
351 		if (VTOI(vp) != NULL && vnode_fsprint != NULL)
352 			(*vnode_fsprint)(vp, ovflw);
353 		(void)printf("\n");
354 	}
355 
356  out:
357 	if (e_vnodebase)
358 		free(e_vnodebase);
359 }
360 
361 int
362 getflags(const struct flagbit_desc *fd, char *p, u_int flags)
363 {
364 	char *q = p;
365 
366 	if (flags == 0) {
367 		*p++ = '-';
368 		*p = '\0';
369 		return (0);
370 	}
371 
372 	for (; fd->fd_flags != 0; fd++)
373 		if ((flags & fd->fd_flags) != 0)
374 			*p++ = fd->fd_mark;
375 	*p = '\0';
376 	return (p - q);
377 }
378 
379 const struct flagbit_desc vnode_flags[] = {
380 	{ VV_ROOT,	'R' },
381 	{ VI_TEXT,	'T' },
382 	{ VV_SYSTEM,	'S' },
383 	{ VV_ISTTY,	'I' },
384 	{ VI_EXECMAP,	'E' },
385 	{ VI_XLOCK,	'L' },
386 	{ VU_DIROP,	'D' },
387 	{ VI_LAYER,	'Y' },
388 	{ VI_ONWORKLST,	'O' },
389 	{ 0,		'\0' },
390 };
391 
392 void
393 vnode_header(void)
394 {
395 
396 	(void)printf("%-*s TYP VFLAG  USE HOLD TAG NPAGE",
397 	    PTRSTRWIDTH, "ADDR");
398 }
399 
400 int
401 vnode_print(struct vnode *avnode, struct vnode *vp)
402 {
403 	char *type, flags[sizeof(vnode_flags) / sizeof(vnode_flags[0])];
404 	int ovflw;
405 
406 	/*
407 	 * set type
408 	 */
409 	switch (vp->v_type) {
410 	case VNON:
411 		type = "non"; break;
412 	case VREG:
413 		type = "reg"; break;
414 	case VDIR:
415 		type = "dir"; break;
416 	case VBLK:
417 		type = "blk"; break;
418 	case VCHR:
419 		type = "chr"; break;
420 	case VLNK:
421 		type = "lnk"; break;
422 	case VSOCK:
423 		type = "soc"; break;
424 	case VFIFO:
425 		type = "fif"; break;
426 	case VBAD:
427 		type = "bad"; break;
428 	default:
429 		type = "unk"; break;
430 	}
431 	/*
432 	 * gather flags
433 	 */
434 	(void)getflags(vnode_flags, flags,
435 	    vp->v_uflag | vp->v_iflag | vp->v_vflag);
436 
437 	ovflw = 0;
438 	PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)avnode);
439 	PRWORD(ovflw, " %*s", 4, 1, type);
440 	PRWORD(ovflw, " %*s", 6, 1, flags);
441 	PRWORD(ovflw, " %*ld", 5, 1, (long)vp->v_usecount);
442 	PRWORD(ovflw, " %*ld", 5, 1, (long)vp->v_holdcnt);
443 	PRWORD(ovflw, " %*d", 4, 1, vp->v_tag);
444 	PRWORD(ovflw, " %*d", 6, 1, vp->v_uobj.uo_npages);
445 	return (ovflw);
446 }
447 
448 const struct flagbit_desc ufs_flags[] = {
449 	{ IN_ACCESS,	'A' },
450 	{ IN_CHANGE,	'C' },
451 	{ IN_UPDATE,	'U' },
452 	{ IN_MODIFIED,	'M' },
453 	{ IN_ACCESSED,	'a' },
454 	{ IN_RENAME,	'R' },
455 	{ IN_SHLOCK,	'S' },
456 	{ IN_EXLOCK,	'E' },
457 	{ IN_CLEANING,	'c' },
458 	{ IN_ADIROP,	'D' },
459 	{ IN_SPACECOUNTED, 's' },
460 	{ 0,		'\0' },
461 };
462 
463 void
464 ufs_header(void)
465 {
466 
467 	(void)printf(" FILEID IFLAG RDEV|SZ");
468 }
469 
470 int
471 ufs_print(struct vnode *vp, int ovflw)
472 {
473 	struct inode inode, *ip = &inode;
474 	union dinode {
475 		struct ufs1_dinode dp1;
476 		struct ufs2_dinode dp2;
477 	} dip;
478 	char flags[sizeof(ufs_flags) / sizeof(ufs_flags[0])];
479 	char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */
480 	char *name;
481 	mode_t type;
482 	dev_t rdev;
483 
484 	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
485 	KGETRET(ip->i_din.ffs1_din, &dip, sizeof (struct ufs1_dinode),
486 	    "inode's dinode");
487 
488 	if (ip->i_size == dip.dp1.di_size)
489 		rdev = dip.dp1.di_rdev;
490 	else {
491 		KGETRET(ip->i_din.ffs1_din, &dip, sizeof (struct ufs2_dinode),
492 		    "inode's UFS2 dinode");
493 		rdev = dip.dp2.di_rdev;
494 	}
495 
496 	/*
497 	 * XXX need to to locking state.
498 	 */
499 
500 	(void)getflags(ufs_flags, flags, ip->i_flag);
501 	PRWORD(ovflw, " %*llu", 7, 1, (unsigned long long)ip->i_number);
502 	PRWORD(ovflw, " %*s", 6, 1, flags);
503 	type = ip->i_mode & S_IFMT;
504 	if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) {
505 		if (usenumflag ||
506 		    (name = devname(rdev, type)) == NULL) {
507 			devprintf(dev, sizeof(dev), rdev);
508 			name = dev;
509 		}
510 		PRWORD(ovflw, " %*s", 8, 1, name);
511 	} else
512 		PRWORD(ovflw, " %*lld", 8, 1, (long long)ip->i_size);
513 	return 0;
514 }
515 
516 int
517 ext2fs_print(struct vnode *vp, int ovflw)
518 {
519 	struct inode inode, *ip = &inode;
520 	struct ext2fs_dinode dip;
521 	char flags[sizeof(ufs_flags) / sizeof(ufs_flags[0])];
522 	char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */
523 	char *name;
524 	mode_t type;
525 
526 	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
527 	KGETRET(ip->i_din.e2fs_din, &dip, sizeof (struct ext2fs_dinode),
528 	    "inode's dinode");
529 
530 	/*
531 	 * XXX need to to locking state.
532 	 */
533 
534 	(void)getflags(ufs_flags, flags, ip->i_flag);
535 	PRWORD(ovflw, " %*llu", 7, 1, (unsigned long long)ip->i_number);
536 	PRWORD(ovflw, " %*s", 6, 1, flags);
537 	type = dip.e2di_mode & S_IFMT;
538 	if (S_ISCHR(dip.e2di_mode) || S_ISBLK(dip.e2di_mode)) {
539 		if (usenumflag ||
540 		    (name = devname(dip.e2di_rdev, type)) == NULL) {
541 			devprintf(dev, sizeof(dev), dip.e2di_rdev);
542 			name = dev;
543 		}
544 		PRWORD(ovflw, " %*s", 8, 1, name);
545 	} else
546 		PRWORD(ovflw, " %*u", 8, 1, (u_int)dip.e2di_size);
547 	return (0);
548 }
549 
550 const struct flagbit_desc nfs_flags[] = {
551 	{ NFLUSHWANT,	'W' },
552 	{ NFLUSHINPROG,	'P' },
553 	{ NMODIFIED,	'M' },
554 	{ NWRITEERR,	'E' },
555 	{ NACC,		'A' },
556 	{ NUPD,		'U' },
557 	{ NCHG,		'C' },
558 	{ 0,		'\0' },
559 };
560 
561 void
562 nfs_header(void)
563 {
564 
565 	(void)printf(" FILEID NFLAG RDEV|SZ");
566 }
567 
568 int
569 nfs_print(struct vnode *vp, int ovflw)
570 {
571 	struct nfsnode nfsnode, *np = &nfsnode;
572 	char flags[sizeof(nfs_flags) / sizeof(nfs_flags[0])];
573 	char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */
574 	struct vattr va;
575 	char *name;
576 	mode_t type;
577 
578 	KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
579 	(void)getflags(nfs_flags, flags, np->n_flag);
580 
581 	KGETRET(np->n_vattr, &va, sizeof(va), "vnode attr");
582 	PRWORD(ovflw, " %*ld", 7, 1, (long)va.va_fileid);
583 	PRWORD(ovflw, " %*s", 6, 1, flags);
584 	switch (va.va_type) {
585 	case VCHR:
586 		type = S_IFCHR;
587 		goto device;
588 
589 	case VBLK:
590 		type = S_IFBLK;
591 	device:
592 		if (usenumflag || (name = devname(va.va_rdev, type)) == NULL) {
593 			devprintf(dev, sizeof(dev), va.va_rdev);
594 			name = dev;
595 		}
596 		PRWORD(ovflw, " %*s", 8, 1, name);
597 		break;
598 	default:
599 		PRWORD(ovflw, " %*lld", 8, 1, (long long)np->n_size);
600 		break;
601 	}
602 	return (0);
603 }
604 
605 void
606 layer_header(void)
607 {
608 
609 	(void)printf(" %*s", PTRSTRWIDTH, "LOWER");
610 }
611 
612 int
613 layer_print(struct vnode *vp, int ovflw)
614 {
615 	struct layer_node lnode, *lp = &lnode;
616 
617 	KGETRET(VTOLAYER(vp), &lnode, sizeof(lnode), "layer vnode");
618 
619 	PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)lp->layer_lowervp);
620 	return (0);
621 }
622 
623 /*
624  * Given a pointer to a mount structure in kernel space,
625  * read it in and return a usable pointer to it.
626  */
627 struct mount *
628 getmnt(struct mount *maddr)
629 {
630 	static struct mtab {
631 		struct mtab *next;
632 		struct mount *maddr;
633 		struct mount mount;
634 	} *mhead = NULL;
635 	struct mtab *mt;
636 	struct mount mb;
637 
638 	for (mt = mhead; mt != NULL; mt = mt->next)
639 		if (maddr == mt->maddr)
640 			return (&mt->mount);
641 	KGETRET(maddr, &mb, sizeof(struct mount), "mount table");
642 	if ((mt = malloc(sizeof(struct mtab))) == NULL)
643 		err(1, "malloc");
644 	mt->mount = mb;
645 	mt->maddr = maddr;
646 	mt->next = mhead;
647 	mhead = mt;
648 	return (&mt->mount);
649 }
650 
651 void
652 mount_print(struct mount *mp)
653 {
654 	int flags;
655 
656 	(void)printf("*** MOUNT %s %s on %s", ST.f_fstypename,
657 	    ST.f_mntfromname, ST.f_mntonname);
658 	if ((flags = mp->mnt_flag) != 0) {
659 		int i;
660 		const char *sep = " (";
661 
662 		for (i = 0; i < sizeof mnt_flags / sizeof mnt_flags[0]; i++) {
663 			if (flags & mnt_flags[i].m_flag) {
664 				(void)printf("%s%s", sep, mnt_flags[i].m_name);
665 				flags &= ~mnt_flags[i].m_flag;
666 				sep = ",";
667 			}
668 		}
669 		if (flags)
670 			(void)printf("%sunknown_flags:%x", sep, flags);
671 		(void)printf(")");
672 	}
673 	(void)printf("\n");
674 }
675 
676 char *
677 loadvnodes(int *avnodes)
678 {
679 	int mib[2];
680 	size_t copysize;
681 	char *vnodebase;
682 
683 	if (totalflag) {
684 		KGET(V_NUMV, *avnodes);
685 		return NULL;
686 	}
687 	if (memf != NULL) {
688 		/*
689 		 * do it by hand
690 		 */
691 		return (kinfo_vnodes(avnodes));
692 	}
693 	mib[0] = CTL_KERN;
694 	mib[1] = KERN_VNODE;
695 	if (sysctl(mib, 2, NULL, &copysize, NULL, 0) == -1)
696 		err(1, "sysctl: KERN_VNODE");
697 	if ((vnodebase = malloc(copysize)) == NULL)
698 		err(1, "malloc");
699 	if (sysctl(mib, 2, vnodebase, &copysize, NULL, 0) == -1)
700 		err(1, "sysctl: KERN_VNODE");
701 	if (copysize % (VPTRSZ + VNODESZ))
702 		errx(1, "vnode size mismatch");
703 	*avnodes = copysize / (VPTRSZ + VNODESZ);
704 
705 	return (vnodebase);
706 }
707 
708 /*
709  * simulate what a running kernel does in in kinfo_vnode
710  */
711 char *
712 kinfo_vnodes(int *avnodes)
713 {
714 	struct mntlist mountlist;
715 	struct mount *mp, mount;
716 	struct vnode *vp, vnode;
717 	char *beg, *bp, *ep;
718 	int numvnodes;
719 
720 	KGET(V_NUMV, numvnodes);
721 	if ((bp = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
722 		err(1, "malloc");
723 	beg = bp;
724 	ep = bp + (numvnodes + 20) * (VPTRSZ + VNODESZ);
725 	KGET(V_MOUNTLIST, mountlist);
726 	for (mp = mountlist.cqh_first;;
727 	    mp = mount.mnt_list.cqe_next) {
728 		KGET2(mp, &mount, sizeof(mount), "mount entry");
729 		TAILQ_FOREACH(vp, &mount.mnt_vnodelist, v_mntvnodes) {
730 			KGET2(vp, &vnode, sizeof(vnode), "vnode");
731 			if (bp + VPTRSZ + VNODESZ > ep)
732 				/* XXX - should realloc */
733 				errx(1, "no more room for vnodes");
734 			memmove(bp, &vp, VPTRSZ);
735 			bp += VPTRSZ;
736 			memmove(bp, &vnode, VNODESZ);
737 			bp += VNODESZ;
738 		}
739 		if (mp == mountlist.cqh_last)
740 			break;
741 	}
742 	*avnodes = (bp - beg) / (VPTRSZ + VNODESZ);
743 	return (beg);
744 }
745 
746 void
747 ttymode(void)
748 {
749 	int ntty;
750 	struct ttylist_head tty_head;
751 	struct tty *tp, tty;
752 
753 	KGET(TTY_NTTY, ntty);
754 	(void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s");
755 	KGET(TTY_TTYLIST, tty_head);
756 	(void)printf(
757 	    "  LINE RAW CAN OUT  HWT LWT     COL STATE  %-*s  PGID DISC\n",
758 	    PTRSTRWIDTH, "SESS");
759 	for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) {
760 		KGET2(tp, &tty, sizeof tty, "tty struct");
761 		ttyprt(&tty);
762 	}
763 }
764 
765 static const struct flagbit_desc ttystates[] = {
766 	{ TS_ISOPEN,	'O'},
767 	{ TS_DIALOUT,	'>'},
768 	{ TS_CARR_ON,	'C'},
769 	{ TS_TIMEOUT,	'T'},
770 	{ TS_FLUSH,	'F'},
771 	{ TS_BUSY,	'B'},
772 	{ TS_XCLUDE,	'X'},
773 	{ TS_TTSTOP,	'S'},
774 	{ TS_TBLOCK,	'K'},
775 	{ TS_ASYNC,	'Y'},
776 	{ TS_BKSL,	'D'},
777 	{ TS_ERASE,	'E'},
778 	{ TS_LNCH,	'L'},
779 	{ TS_TYPEN,	'P'},
780 	{ TS_CNTTB,	'N'},
781 	{ 0,		'\0'},
782 };
783 
784 void
785 ttyprt(struct tty *tp)
786 {
787 	char state[sizeof(ttystates) / sizeof(ttystates[0]) + 1];
788 	char dev[2 + 3 + 1 + 5 + 1]; /* 12bit major + 20bit minor */
789 	struct linesw t_linesw;
790 	const char *name;
791 	char buffer;
792 	pid_t pgid;
793 	int n, ovflw;
794 
795 	if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL) {
796 		devprintf(dev, sizeof(dev), tp->t_dev);
797 		name = dev;
798 	}
799 	ovflw = 0;
800 	PRWORD(ovflw, "%-*s", 7, 0, name);
801 	PRWORD(ovflw, " %*d", 3, 1, tp->t_rawq.c_cc);
802 	PRWORD(ovflw, " %*d", 4, 1, tp->t_canq.c_cc);
803 	PRWORD(ovflw, " %*d", 4, 1, tp->t_outq.c_cc);
804 	PRWORD(ovflw, " %*d", 5, 1, tp->t_hiwat);
805 	PRWORD(ovflw, " %*d", 4, 1, tp->t_lowat);
806 	PRWORD(ovflw, " %*d", 8, 1, tp->t_column);
807 	n = getflags(ttystates, state, tp->t_state);
808 	if (tp->t_wopen) {
809 		state[n++] = 'W';
810 		state[n] = '\0';
811 	}
812 	PRWORD(ovflw, " %-*s", 7, 1, state);
813 	PRWORD(ovflw, " %*lX", PTRSTRWIDTH + 1, 1, (u_long)tp->t_session);
814 	pgid = 0;
815 	if (tp->t_pgrp != NULL)
816 		KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
817 	PRWORD(ovflw, " %*d", 6, 1, pgid);
818 	KGET2(tp->t_linesw, &t_linesw, sizeof(t_linesw),
819 	    "line discipline switch table");
820 	name = t_linesw.l_name;
821 	(void)putchar(' ');
822 	for (;;) {
823 		KGET2(name, &buffer, sizeof(buffer), "line discipline name");
824 		if (buffer == '\0')
825 			break;
826 		(void)putchar(buffer);
827 		name++;
828 	}
829 	(void)putchar('\n');
830 }
831 
832 static const struct flagbit_desc filemode_flags[] = {
833 	{ FREAD,	'R' },
834 	{ FWRITE,	'W' },
835 	{ FAPPEND,	'A' },
836 #ifdef FSHLOCK	/* currently gone */
837 	{ FSHLOCK,	'S' },
838 	{ FEXLOCK,	'X' },
839 #endif
840 	{ FASYNC,	'I' },
841 	{ 0,		'\0' },
842 };
843 
844 void
845 filemode(void)
846 {
847 	struct file *fp;
848 	struct file *addr;
849 	char flags[sizeof(filemode_flags) / sizeof(filemode_flags[0])];
850 	char *buf, *offset;
851 	int len, maxfile, nfile, ovflw;
852 
853 	KGET(FNL_MAXFILE, maxfile);
854 	if (totalflag) {
855 		KGET(FNL_NFILE, nfile);
856 		(void)printf("%3d/%3d files\n", nfile, maxfile);
857 		return;
858 	}
859 	if (getfiles(&buf, &len, &offset) == -1)
860 		return;
861 	/*
862 	 * Getfiles returns in malloc'd memory a pointer to the first file
863 	 * structure, and then an array of file structs (whose addresses are
864 	 * derivable from the previous entry).
865 	 */
866 	addr = ((struct filelist *)offset)->lh_first;
867 	fp = (struct file *)(offset + sizeof(struct filelist));
868 	nfile = (len - sizeof(struct filelist)) / sizeof(struct file);
869 
870 	(void)printf("%d/%d open files\n", nfile, maxfile);
871 	(void)printf("%*s%s%*s TYPE    FLG     CNT  MSG  %*s%s%*s IFLG OFFSET\n",
872 	    (PTRSTRWIDTH - 4) / 2, "", " LOC", (PTRSTRWIDTH - 4) / 2, "",
873 	    (PTRSTRWIDTH - 4) / 2, "", "DATA", (PTRSTRWIDTH - 4) / 2, "");
874 	for (; (char *)fp < offset + len; addr = fp->f_list.le_next, fp++) {
875 		if ((unsigned)fp->f_type >= sizeof(dtypes) / sizeof(dtypes[0]))
876 			continue;
877 		ovflw = 0;
878 		(void)getflags(filemode_flags, flags, fp->f_flag);
879 		PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)addr);
880 		PRWORD(ovflw, " %-*s", 9, 1, dtypes[fp->f_type]);
881 		PRWORD(ovflw, " %*s", 6, 1, flags);
882 		PRWORD(ovflw, " %*d", 5, 1, fp->f_count);
883 		PRWORD(ovflw, " %*d", 5, 1, fp->f_msgcount);
884 		PRWORD(ovflw, "  %*lx", PTRSTRWIDTH + 1, 2, (long)fp->f_data);
885 		PRWORD(ovflw, " %*x", 5, 1, 0);
886 		if (fp->f_offset < 0)
887 			PRWORD(ovflw, "  %-*lld\n", PTRSTRWIDTH + 1, 2,
888 			    (long long)fp->f_offset);
889 		else
890 			PRWORD(ovflw, "  %-*lld\n", PTRSTRWIDTH + 1, 2,
891 			    (long long)fp->f_offset);
892 	}
893 	free(buf);
894 }
895 
896 int
897 getfiles(char **abuf, int *alen, char **aoffset)
898 {
899 	size_t len;
900 	int mib[2];
901 	char *buf;
902 	size_t offset;
903 
904 	/*
905 	 * XXX
906 	 * Add emulation of KINFO_FILE here.
907 	 */
908 	if (memf != NULL)
909 		errx(1, "files on dead kernel, not implemented");
910 
911 	mib[0] = CTL_KERN;
912 	mib[1] = KERN_FILE;
913 	if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
914 		warn("sysctl: KERN_FILE");
915 		return (-1);
916 	}
917 	/* We need to align (struct file *) in the buffer. */
918 	offset = len % sizeof(off_t);
919 	if ((buf = malloc(len + offset)) == NULL)
920 		err(1, "malloc");
921 	if (sysctl(mib, 2, buf + offset, &len, NULL, 0) == -1) {
922 		warn("sysctl: KERN_FILE");
923 		return (-1);
924 	}
925 	*abuf = buf;
926 	*alen = len;
927 	*aoffset = (buf + offset);
928 	return (0);
929 }
930 
931 void
932 usage(void)
933 {
934 
935 	(void)fprintf(stderr,
936 	    "usage: %s [-T|-f|-s|-t|-v] [-ghkmn] [-M core] [-N system]\n",
937 	    getprogname());
938 	exit(1);
939 }
940