xref: /dflybsd-src/usr.sbin/pstat/pstat.c (revision ff837cd5d97df9cfc8d66fc9ebc73557657d023e)
1 /*-
2  * Copyright (c) 1980, 1991, 1993, 1994
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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#) Copyright (c) 1980, 1991, 1993, 1994 The Regents of the University of California.  All rights reserved.
30  * @(#)pstat.c	8.16 (Berkeley) 5/9/95
31  * $FreeBSD: src/usr.sbin/pstat/pstat.c,v 1.49.2.5 2002/07/12 09:12:49 des Exp $
32  */
33 
34 #include <sys/user.h>
35 #include <sys/kinfo.h>
36 #include <sys/param.h>
37 #include <sys/time.h>
38 #include <sys/vnode.h>
39 #include <sys/ucred.h>
40 #include <sys/file.h>
41 #include <vfs/ufs/quota.h>
42 #include <vfs/ufs/inode.h>
43 #include <sys/mount.h>
44 #include <sys/uio.h>
45 #include <sys/namei.h>
46 #include <vfs/union/union.h>
47 #include <sys/stat.h>
48 #include <nfs/rpcv2.h>
49 #include <nfs/nfsproto.h>
50 #include <nfs/nfs.h>
51 #include <nfs/nfsnode.h>
52 #include <sys/ioctl.h>
53 #include <sys/ioctl_compat.h>	/* XXX NTTYDISC is too well hidden */
54 #include <sys/tty.h>
55 #include <sys/conf.h>
56 #include <sys/blist.h>
57 
58 #include <sys/sysctl.h>
59 
60 #include <err.h>
61 #include <fcntl.h>
62 #include <kvm.h>
63 #include <limits.h>
64 #include <nlist.h>
65 #include <stdio.h>
66 #include <stdlib.h>
67 #include <string.h>
68 #include <unistd.h>
69 
70 #ifdef USE_KCORE
71 #  define KCORE_KINFO_WRAPPER
72 #  include <kcore.h>
73 #else
74 #  include <kinfo.h>
75 #endif
76 
77 enum {
78 	NL_MOUNTLIST,
79 	NL_NUMVNODES,
80 	NL_RC_TTY,
81 	NL_NRC_TTY,
82 	NL_CY_TTY,
83 	NL_NCY_TTY,
84 	NL_SI_TTY,
85 	NL_SI_NPORTS,
86 };
87 
88 const size_t NL_LAST_MANDATORY = NL_NUMVNODES;
89 
90 struct nlist nl[] = {
91 	{ "_mountlist", 0, 0, 0, 0 },	/* address of head of mount list. */
92 	{ "_numvnodes", 0, 0, 0, 0 },
93 	{ "_rc_tty", 0, 0, 0, 0 },
94 	{ "_nrc_tty", 0, 0, 0, 0 },
95 	{ "_cy_tty", 0, 0, 0, 0 },
96 	{ "_ncy_tty", 0, 0, 0, 0 },
97 	{ "_si__tty", 0, 0, 0, 0 },
98 	{ "_si_Nports", 0, 0, 0, 0 },
99 	{ "", 0, 0, 0, 0 }
100 };
101 
102 int	usenumflag;
103 int	totalflag;
104 int	swapflag;
105 char	*nlistf	= NULL;
106 char	*memf	= NULL;
107 kvm_t	*kd;
108 
109 const char	*usagestr;
110 
111 struct {
112 	int m_flag;
113 	const char *m_name;
114 } mnt_flags[] = {
115 	{ MNT_RDONLY, "rdonly" },
116 	{ MNT_SYNCHRONOUS, "sync" },
117 	{ MNT_NOEXEC, "noexec" },
118 	{ MNT_NOSUID, "nosuid" },
119 	{ MNT_NODEV, "nodev" },
120 	{ MNT_UNION, "union" },
121 	{ MNT_ASYNC, "async" },
122 	{ MNT_SUIDDIR, "suiddir" },
123 	{ MNT_SOFTDEP, "softdep" },
124 	{ MNT_NOSYMFOLLOW, "nosymfollow" },
125 	{ MNT_NOATIME, "noatime" },
126 	{ MNT_NOCLUSTERR, "noclusterread" },
127 	{ MNT_NOCLUSTERW, "noclusterwrite" },
128 	{ MNT_EXRDONLY, "exrdonly" },
129 	{ MNT_EXPORTED, "exported" },
130 	{ MNT_DEFEXPORTED, "defexported" },
131 	{ MNT_EXPORTANON, "exportanon" },
132 	{ MNT_EXKERB, "exkerb" },
133 	{ MNT_EXPUBLIC, "public" },
134 	{ MNT_LOCAL, "local" },
135 	{ MNT_QUOTA, "quota" },
136 	{ MNT_ROOTFS, "rootfs" },
137 	{ MNT_USER, "user" },
138 	{ MNT_IGNORE, "ignore" },
139 	{ MNT_UPDATE, "update" },
140 	{ MNT_DELEXPORT, "delexport" },
141 	{ MNT_RELOAD, "reload" },
142 	{ MNT_FORCE, "force" },
143 	{ 0, NULL }
144 };
145 
146 
147 #define	SVAR(var) __STRING(var)	/* to force expansion */
148 #define	KGET(idx, var)							\
149 	KGET1(idx, &var, sizeof(var), SVAR(var))
150 #define	KGET1(idx, p, s, msg)						\
151 	KGET2(nl[idx].n_value, p, s, msg)
152 #define	KGET2(addr, p, s, msg)						\
153 	if (kvm_read(kd, (u_long)(addr), p, s) != s)			\
154 		warnx("cannot read %s: %s", msg, kvm_geterr(kd))
155 #define	KGETN(idx, var)							\
156 	KGET1N(idx, &var, sizeof(var), SVAR(var))
157 #define	KGET1N(idx, p, s, msg)						\
158 	KGET2N(nl[idx].n_value, p, s, msg)
159 #define	KGET2N(addr, p, s, msg)						\
160 	((kvm_read(kd, (u_long)(addr), p, s) == s) ? 1 : 0)
161 #define	KGETRET(addr, p, s, msg)					\
162 	if (kvm_read(kd, (u_long)(addr), p, s) != s) {			\
163 		warnx("cannot read %s: %s", msg, kvm_geterr(kd));	\
164 		return (0);						\
165 	}
166 
167 void	filemode(void);
168 int	getfiles(char **, int *);
169 struct mount *
170 	getmnt(struct mount *);
171 struct e_vnode *
172 	kinfo_vnodes(int *);
173 struct e_vnode *
174 	loadvnodes(int *);
175 void	mount_print(struct mount *);
176 void	nfs_header(void);
177 int	nfs_print(struct vnode *);
178 void	swapmode(void);
179 void	ttymode(void);
180 void	ttyprt(struct tty *, int);
181 void	ttytype(struct tty *, const char *, int, int, int);
182 void	ufs_header(void);
183 int	ufs_print(struct vnode *);
184 void	union_header(void);
185 int	union_print(struct vnode *);
186 static void usage(void);
187 void	vnode_header(void);
188 void	vnode_print(struct vnode *, struct vnode *);
189 void	vnodemode(void);
190 
191 int
192 main(int argc, char **argv)
193 {
194 	int ch, ret;
195 	int fileflag, ttyflag, vnodeflag;
196 	char buf[_POSIX2_LINE_MAX];
197 	const char *opts;
198 
199 	fileflag = swapflag = ttyflag = vnodeflag = 0;
200 
201 	/* We will behave like good old swapinfo if thus invoked */
202 	opts = strrchr(argv[0],'/');
203 	if (opts)
204 		opts++;
205 	else
206 		opts = argv[0];
207 	if (!strcmp(opts,"swapinfo")) {
208 		swapflag = 1;
209 		opts = "kM:N:";
210 		usagestr = "swapinfo [-k] [-M core] [-N system]";
211 	} else {
212 		opts = "TM:N:fiknstv";
213 		usagestr = "pstat [-Tfknst] [-M core] [-N system]";
214 	}
215 
216 	while ((ch = getopt(argc, argv, opts)) != -1)
217 		switch (ch) {
218 		case 'f':
219 			fileflag = 1;
220 			break;
221 		case 'k':
222 			if (setenv("BLOCKSIZE", "1K", 1) == -1)
223 				warn("setenv: cannot set BLOCKSIZE=1K");
224 			break;
225 		case 'M':
226 			memf = optarg;
227 			break;
228 		case 'N':
229 			nlistf = optarg;
230 			break;
231 		case 'n':
232 			usenumflag = 1;
233 			break;
234 		case 's':
235 			++swapflag;
236 			break;
237 		case 'T':
238 			totalflag = 1;
239 			break;
240 		case 't':
241 			ttyflag = 1;
242 			break;
243 		case 'v':
244 		case 'i':		/* Backward compatibility. */
245 			errx(1, "vnode mode not supported");
246 #if 0
247 			vnodeflag = 1;
248 			break;
249 #endif
250 		default:
251 			usage();
252 		}
253 	argc -= optind;
254 	argv += optind;
255 
256 	/*
257 	 * Discard setgid privileges if not the running kernel so that bad
258 	 * guys can't print interesting stuff from kernel memory.
259 	 */
260 	if (nlistf != NULL || memf != NULL)
261 		setgid(getgid());
262 
263 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
264 		errx(1, "kvm_openfiles: %s", buf);
265 #ifdef USE_KCORE
266 	if (kcore_wrapper_open(nlistf, memf, buf))
267 		errx(1, "kcore_open: %s", buf);
268 #endif
269 	if ((ret = kvm_nlist(kd, nl)) != 0) {
270 		size_t i;
271 		int quit = 0;
272 
273 		if (ret == -1)
274 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
275 		for (i = 0; i < NL_LAST_MANDATORY; i++) {
276 			if (!nl[i].n_value) {
277 				quit = 1;
278 				warnx("undefined symbol: %s", nl[i].n_name);
279 			}
280 		}
281 		if (quit)
282 			exit(1);
283 	}
284 	if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag))
285 		usage();
286 	if (fileflag || totalflag)
287 		filemode();
288 	if (vnodeflag)
289 		vnodemode();
290 	if (ttyflag)
291 		ttymode();
292 	if (swapflag || totalflag)
293 		swapmode();
294 	exit (0);
295 }
296 
297 static void
298 usage(void)
299 {
300 	fprintf(stderr, "usage: %s\n", usagestr);
301 	exit (1);
302 }
303 
304 struct e_vnode {
305 	struct vnode *avnode;
306 	struct vnode vnode;
307 };
308 
309 void
310 vnodemode(void)
311 {
312 	struct e_vnode *e_vnodebase, *endvnode, *evp;
313 	struct vnode *vp;
314 	struct mount *maddr, *mp = NULL;
315 	int numvnodes;
316 
317 	e_vnodebase = loadvnodes(&numvnodes);
318 	if (totalflag) {
319 		printf("%7d vnodes\n", numvnodes);
320 		return;
321 	}
322 	endvnode = e_vnodebase + numvnodes;
323 	printf("%d active vnodes\n", numvnodes);
324 
325 
326 #define ST	mp->mnt_stat
327 	maddr = NULL;
328 	for (evp = e_vnodebase; evp < endvnode; evp++) {
329 		vp = &evp->vnode;
330 		if (vp->v_mount != maddr) {
331 			/*
332 			 * New filesystem
333 			 */
334 			if ((mp = getmnt(vp->v_mount)) == NULL)
335 				continue;
336 			maddr = vp->v_mount;
337 			mount_print(mp);
338 			vnode_header();
339 			if (!strcmp(ST.f_fstypename, "ufs") ||
340 			    !strcmp(ST.f_fstypename, "mfs"))
341 				ufs_header();
342 			else if (!strcmp(ST.f_fstypename, "nfs"))
343 				nfs_header();
344 			else if (!strcmp(ST.f_fstypename, "union"))
345 				union_header();
346 			printf("\n");
347 		}
348 		vnode_print(evp->avnode, vp);
349 		if (!strcmp(ST.f_fstypename, "ufs") ||
350 		    !strcmp(ST.f_fstypename, "mfs"))
351 			ufs_print(vp);
352 		else if (!strcmp(ST.f_fstypename, "nfs"))
353 			nfs_print(vp);
354 		else if (!strcmp(ST.f_fstypename, "union"))
355 			union_print(vp);
356 		printf("\n");
357 	}
358 	free(e_vnodebase);
359 }
360 
361 void
362 vnode_header(void)
363 {
364 	printf("ADDR     TYP VFLAG  USE HOLD");
365 }
366 
367 void
368 vnode_print(struct vnode *avnode, struct vnode *vp)
369 {
370 	const char *type;
371 	char flags[32];
372 	char *fp = flags;
373 	int flag;
374 	int refs;
375 
376 	/*
377 	 * set type
378 	 */
379 	switch (vp->v_type) {
380 	case VNON:
381 		type = "non"; break;
382 	case VREG:
383 		type = "reg"; break;
384 	case VDIR:
385 		type = "dir"; break;
386 	case VBLK:
387 		type = "blk"; break;
388 	case VCHR:
389 		type = "chr"; break;
390 	case VLNK:
391 		type = "lnk"; break;
392 	case VSOCK:
393 		type = "soc"; break;
394 	case VFIFO:
395 		type = "fif"; break;
396 	case VBAD:
397 		type = "bad"; break;
398 	default:
399 		type = "unk"; break;
400 	}
401 	/*
402 	 * gather flags
403 	 */
404 	flag = vp->v_flag;
405 	if (flag & VROOT)
406 		*fp++ = 'R';
407 	if (flag & VTEXT)
408 		*fp++ = 'T';
409 	if (flag & VSYSTEM)
410 		*fp++ = 'S';
411 	if (flag & VISTTY)
412 		*fp++ = 't';
413 #ifdef VXLOCK
414 	if (flag & VXLOCK)
415 		*fp++ = 'L';
416 	if (flag & VXWANT)
417 		*fp++ = 'W';
418 #endif
419 	if (flag & VOBJBUF)
420 		*fp++ = 'V';
421 	if (flag & (VAGE0 | VAGE1))
422 		*fp++ = 'a';
423 	if (flag & VOLOCK)
424 		*fp++ = 'l';
425 	if (flag & VOWANT)
426 		*fp++ = 'w';
427 #ifdef VDOOMED
428 	if (flag & VDOOMED)
429 		*fp++ = 'D';
430 #endif
431 	if (flag & VONWORKLST)
432 		*fp++ = 'O';
433 #ifdef VRECLAIMED
434 	if (flag & VINACTIVE)
435 		*fp++ = 'I';
436 	if (flag & VRECLAIMED)
437 		*fp++ = 'X';
438 #endif
439 
440 	if (flag == 0)
441 		*fp++ = '-';
442 	*fp = '\0';
443 
444 	/*
445 	 * Convert SYSREF ref counts into something more
446 	 * human readable for display.
447 	 */
448 	refs = vp->v_refcnt;
449 	printf("%8lx %s %5s %08x %4d",
450 	    (u_long)(void *)avnode, type, flags, refs, vp->v_auxrefs);
451 }
452 
453 void
454 ufs_header(void)
455 {
456 	printf(" FILEID IFLAG RDEV|SZ");
457 }
458 
459 int
460 ufs_print(struct vnode *vp)
461 {
462 	int flag;
463 	struct inode inode, *ip = &inode;
464 	char flagbuf[16], *flags = flagbuf;
465 	char *name;
466 	mode_t type;
467 
468 	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
469 	flag = ip->i_flag;
470 	if (flag & IN_ACCESS)
471 		*flags++ = 'A';
472 	if (flag & IN_CHANGE)
473 		*flags++ = 'C';
474 	if (flag & IN_UPDATE)
475 		*flags++ = 'U';
476 	if (flag & IN_MODIFIED)
477 		*flags++ = 'M';
478 	if (flag & IN_RENAME)
479 		*flags++ = 'R';
480 	if (flag & IN_SHLOCK)
481 		*flags++ = 'S';
482 	if (flag & IN_EXLOCK)
483 		*flags++ = 'E';
484 	if (flag & IN_HASHED)
485 		*flags++ = 'H';
486 	if (flag & IN_LAZYMOD)
487 		*flags++ = 'L';
488 	if (flag == 0)
489 		*flags++ = '-';
490 	*flags = '\0';
491 
492 	printf(" %6ju %5s", (uintmax_t)ip->i_number, flagbuf);
493 	type = ip->i_mode & S_IFMT;
494 	if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode))
495 		if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL))
496 			printf("   %2d,%-2d",
497 			    major(ip->i_rdev), minor(ip->i_rdev));
498 		else
499 			printf(" %7s", name);
500 	else
501 		printf(" %7qd", ip->i_size);
502 	return (0);
503 }
504 
505 void
506 nfs_header(void)
507 {
508 	printf(" FILEID NFLAG RDEV|SZ");
509 }
510 
511 int
512 nfs_print(struct vnode *vp)
513 {
514 	struct nfsnode nfsnode, *np = &nfsnode;
515 	char flagbuf[16], *flags = flagbuf;
516 	int flag;
517 	char *name;
518 	mode_t type;
519 
520 	KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
521 	flag = np->n_flag;
522 	if (flag & NFLUSHWANT)
523 		*flags++ = 'W';
524 	if (flag & NFLUSHINPROG)
525 		*flags++ = 'P';
526 	if (flag & NLMODIFIED)
527 		*flags++ = 'M';
528 	if (flag & NRMODIFIED)
529 		*flags++ = 'R';
530 	if (flag & NWRITEERR)
531 		*flags++ = 'E';
532 	if (flag & NQNFSEVICTED)
533 		*flags++ = 'G';
534 	if (flag & NACC)
535 		*flags++ = 'A';
536 	if (flag & NUPD)
537 		*flags++ = 'U';
538 	if (flag & NCHG)
539 		*flags++ = 'C';
540 	if (flag & NLOCKED)
541 		*flags++ = 'L';
542 	if (flag & NWANTED)
543 		*flags++ = 'w';
544 	if (flag == 0)
545 		*flags++ = '-';
546 	*flags = '\0';
547 
548 #define VT	np->n_vattr
549 	printf(" %6ju %5s", (uintmax_t)VT.va_fileid, flagbuf);
550 	type = VT.va_mode & S_IFMT;
551 	if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode))
552 		if (usenumflag || ((name = devname((VT.va_rmajor << 8) | VT.va_rminor, type)) == NULL))
553 			printf("   %2d,%-2d",
554 			    VT.va_rmajor, VT.va_rminor);
555 		else
556 			printf(" %7s", name);
557 	else
558 		printf(" %7qd", np->n_size);
559 	return (0);
560 }
561 
562 void
563 union_header(void)
564 {
565 	printf("    UPPER    LOWER");
566 }
567 
568 int
569 union_print(struct vnode *vp)
570 {
571 	struct union_node unode, *up = &unode;
572 
573 	KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode");
574 
575 	printf(" %8lx %8lx", (u_long)(void *)up->un_uppervp,
576 	    (u_long)(void *)up->un_lowervp);
577 	return (0);
578 }
579 
580 /*
581  * Given a pointer to a mount structure in kernel space,
582  * read it in and return a usable pointer to it.
583  */
584 struct mount *
585 getmnt(struct mount *maddr)
586 {
587 	static struct mtab {
588 		struct mtab *next;
589 		struct mount *maddr;
590 		struct mount mount;
591 	} *mhead = NULL;
592 	struct mtab *mt;
593 
594 	for (mt = mhead; mt != NULL; mt = mt->next)
595 		if (maddr == mt->maddr)
596 			return (&mt->mount);
597 	if ((mt = malloc(sizeof(struct mtab))) == NULL)
598 		errx(1, "malloc");
599 	KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
600 	mt->maddr = maddr;
601 	mt->next = mhead;
602 	mhead = mt;
603 	return (&mt->mount);
604 }
605 
606 void
607 mount_print(struct mount *mp)
608 {
609 	int flags;
610 
611 #define ST	mp->mnt_stat
612 	printf("*** MOUNT %s %s on %s", ST.f_fstypename,
613 	    ST.f_mntfromname, ST.f_mntonname);
614 	if ((flags = mp->mnt_flag)) {
615 		int i;
616 		const char *sep = " (";
617 
618 		for (i = 0; mnt_flags[i].m_flag; i++) {
619 			if (flags & mnt_flags[i].m_flag) {
620 				printf("%s%s", sep, mnt_flags[i].m_name);
621 				flags &= ~mnt_flags[i].m_flag;
622 				sep = ",";
623 			}
624 		}
625 		if (flags)
626 			printf("%sunknown_flags:%x", sep, flags);
627 		printf(")");
628 	}
629 	printf("\n");
630 #undef ST
631 }
632 
633 struct e_vnode *
634 loadvnodes(int *avnodes)
635 {
636 	int mib[2];
637 	size_t copysize;
638 	struct e_vnode *vnodebase;
639 
640 	if (memf != NULL) {
641 		/*
642 		 * do it by hand
643 		 */
644 		return (kinfo_vnodes(avnodes));
645 	}
646 	mib[0] = CTL_KERN;
647 	mib[1] = KERN_VNODE;
648 	if (sysctl(mib, 2, NULL, &copysize, NULL, 0) == -1)
649 		err(1, "sysctl: KERN_VNODE");
650 	if ((vnodebase = malloc(copysize)) == NULL)
651 		errx(1, "malloc");
652 	if (sysctl(mib, 2, vnodebase, &copysize, NULL, 0) == -1)
653 		err(1, "sysctl: KERN_VNODE");
654 	if (copysize % sizeof(struct e_vnode))
655 		errx(1, "vnode size mismatch");
656 	*avnodes = copysize / sizeof(struct e_vnode);
657 
658 	return (vnodebase);
659 }
660 
661 /*
662  * simulate what a running kernel does in in kinfo_vnode
663  */
664 struct e_vnode *
665 kinfo_vnodes(int *avnodes)
666 {
667 	struct mntlist mountlist;
668 	struct mount *mp, mounth, *mp_next;
669 	struct vnode *vp, vnode, *vp_next;
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(NL_NUMVNODES, numvnodes);
677 	if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL)
678 		errx(1, "malloc");
679 	bp = vbuf;
680 	evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ);
681 	KGET(NL_MOUNTLIST, mountlist);
682 	for (num = 0, mp = TAILQ_FIRST(&mountlist); ; mp = mp_next) {
683 		KGET2(mp, &mounth, sizeof(mounth), "mount entry");
684 		mp_next = TAILQ_NEXT(&mounth, mnt_list);
685 		for (vp = TAILQ_FIRST(&mounth.mnt_nvnodelist);
686 		    vp != NULL; vp = vp_next) {
687 			KGET2(vp, &vnode, sizeof(vnode), "vnode");
688 			vp_next = TAILQ_NEXT(&vnode, v_nmntvnodes);
689 			if ((bp + VPTRSZ + VNODESZ) > evbuf)
690 				/* XXX - should realloc */
691 				errx(1, "no more room for vnodes");
692 			memmove(bp, &vp, VPTRSZ);
693 			bp += VPTRSZ;
694 			memmove(bp, &vnode, VNODESZ);
695 			bp += VNODESZ;
696 			num++;
697 		}
698 		if (mp == TAILQ_LAST(&mountlist, mntlist))
699 			break;
700 	}
701 	*avnodes = num;
702 	return ((struct e_vnode *)vbuf);
703 }
704 
705 char hdr[] =
706 "  LINE RAW CAN OUT IHIWT ILOWT OHWT LWT     COL STATE  SESS      PGID DISC\n";
707 int ttyspace = 128;
708 
709 void
710 ttymode(void)
711 {
712 	struct tty *tty;
713 	struct tty ttyb[1000];
714 	int error;
715 	size_t len, i;
716 
717 	printf(hdr);
718 	len = sizeof(ttyb);
719 	error = sysctlbyname("kern.ttys", &ttyb, &len, 0, 0);
720 	if (!error) {
721 		len /= sizeof(ttyb[0]);
722 		for (i = 0; i < len; i++) {
723 			ttyprt(&ttyb[i], 0);
724 		}
725 	}
726 	if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL)
727 		errx(1, "malloc");
728 	if (nl[NL_NRC_TTY].n_type != 0)
729 		ttytype(tty, "rc", NL_RC_TTY, NL_NRC_TTY, 0);
730 	if (nl[NL_NCY_TTY].n_type != 0)
731 		ttytype(tty, "cy", NL_CY_TTY, NL_NCY_TTY, 0);
732 	if (nl[NL_SI_NPORTS].n_type != 0)
733 		ttytype(tty, "si", NL_SI_TTY, NL_SI_NPORTS, 1);
734 	free(tty);
735 }
736 
737 void
738 ttytype(struct tty *tty, const char *name, int type, int number, int indir)
739 {
740 	struct tty *tp;
741 	int ntty;
742 	struct tty **ttyaddr;
743 
744 	if (tty == NULL)
745 		return;
746 	KGET(number, ntty);
747 	printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines");
748 	if (ntty > ttyspace) {
749 		ttyspace = ntty;
750 		if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == NULL)
751 			errx(1, "realloc");
752 	}
753 	if (indir) {
754 		KGET(type, ttyaddr);
755 		KGET2(ttyaddr, tty, (ssize_t)(ntty * sizeof(struct tty)),
756 		      "tty structs");
757 	} else {
758 		KGET1(type, tty, (ssize_t)(ntty * sizeof(struct tty)),
759 		      "tty structs");
760 	}
761 	printf(hdr);
762 	for (tp = tty; tp < &tty[ntty]; tp++)
763 		ttyprt(tp, tp - tty);
764 }
765 
766 struct {
767 	int flag;
768 	char val;
769 } ttystates[] = {
770 #ifdef TS_WOPEN
771 	{ TS_WOPEN,	'W'},
772 #endif
773 	{ TS_ISOPEN,	'O'},
774 	{ TS_CARR_ON,	'C'},
775 #ifdef TS_CONNECTED
776 	{ TS_CONNECTED,	'c'},
777 #endif
778 	{ TS_TIMEOUT,	'T'},
779 	{ TS_FLUSH,	'F'},
780 	{ TS_BUSY,	'B'},
781 #ifdef TS_ASLEEP
782 	{ TS_ASLEEP,	'A'},
783 #endif
784 #ifdef TS_SO_OLOWAT
785 	{ TS_SO_OLOWAT,	'A'},
786 #endif
787 #ifdef TS_SO_OCOMPLETE
788 	{ TS_SO_OCOMPLETE, 'a'},
789 #endif
790 	{ TS_XCLUDE,	'X'},
791 	{ TS_TTSTOP,	'S'},
792 #ifdef TS_CAR_OFLOW
793 	{ TS_CAR_OFLOW,	'm'},
794 #endif
795 #ifdef TS_CTS_OFLOW
796 	{ TS_CTS_OFLOW,	'o'},
797 #endif
798 #ifdef TS_DSR_OFLOW
799 	{ TS_DSR_OFLOW,	'd'},
800 #endif
801 	{ TS_TBLOCK,	'K'},
802 	{ TS_ASYNC,	'Y'},
803 	{ TS_BKSL,	'D'},
804 	{ TS_ERASE,	'E'},
805 	{ TS_LNCH,	'L'},
806 	{ TS_TYPEN,	'P'},
807 	{ TS_CNTTB,	'N'},
808 #ifdef TS_CAN_BYPASS_L_RINT
809 	{ TS_CAN_BYPASS_L_RINT, 'l'},
810 #endif
811 #ifdef TS_SNOOP
812 	{ TS_SNOOP,     's'},
813 #endif
814 #ifdef TS_ZOMBIE
815 	{ TS_ZOMBIE,	'Z'},
816 #endif
817 	{ 0,	       '\0'},
818 };
819 
820 void
821 ttyprt(struct tty *tp, int line)
822 {
823 	int i, j;
824 	pid_t pgid;
825 	char *name, state[20];
826 
827 	if (usenumflag || tp->t_dev == 0 ||
828 	   (name = devname(tp->t_dev, S_IFCHR)) == NULL)
829 		printf("%7d ", line);
830 	else
831 		printf("%7s ", name);
832 	printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
833 	printf("%3d %5d %5d %4d %3d %7d ", tp->t_outq.c_cc,
834 		tp->t_ihiwat, tp->t_ilowat, tp->t_ohiwat, tp->t_olowat,
835 		tp->t_column);
836 	for (i = j = 0; ttystates[i].flag; i++)
837 		if (tp->t_state&ttystates[i].flag)
838 			state[j++] = ttystates[i].val;
839 	if (j == 0)
840 		state[j++] = '-';
841 	state[j] = '\0';
842 	printf("%-6s %8lx", state, (u_long)(void *)tp->t_session);
843 	pgid = 0;
844 	if (tp->t_pgrp != NULL)
845 		KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
846 	printf("%6d ", pgid);
847 	switch (tp->t_line) {
848 	case TTYDISC:
849 		printf("term\n");
850 		break;
851 	case NTTYDISC:
852 		printf("ntty\n");
853 		break;
854 	case SLIPDISC:
855 		printf("slip\n");
856 		break;
857 	case PPPDISC:
858 		printf("ppp\n");
859 		break;
860 	default:
861 		printf("%d\n", tp->t_line);
862 		break;
863 	}
864 }
865 
866 void
867 filemode(void)
868 {
869 	struct kinfo_file *fp, *ofp;
870 	size_t len;
871 	char flagbuf[16], *fbp;
872 	int maxfile, nfile;
873 	static const char *dtypes[] = { "???", "inode", "socket" };
874 
875 	if (kinfo_get_maxfiles(&maxfile))
876 		err(1, "kinfo_get_maxfiles");
877 	if (totalflag) {
878 		if (kinfo_get_openfiles(&nfile))
879 			err(1, "kinfo_get_openfiles");
880 		printf("%3d/%3d files\n", nfile, maxfile);
881 		return;
882 	}
883 	if (kinfo_get_files(&fp, &len))
884 		err(1, "kinfo_get_files");
885 	ofp = fp;
886 
887 	printf("%d/%d open files\n", len, maxfile);
888 	printf("   LOC   TYPE    FLG     CNT  MSG    DATA    OFFSET\n");
889 	for (; len-- > 0; fp++) {
890 		if ((unsigned)fp->f_type > DTYPE_SOCKET)
891 			continue;
892 		printf("%p ", fp->f_file);
893 		printf("%-8.8s", dtypes[fp->f_type]);
894 		fbp = flagbuf;
895 		if (fp->f_flag & FREAD)
896 			*fbp++ = 'R';
897 		if (fp->f_flag & FWRITE)
898 			*fbp++ = 'W';
899 		if (fp->f_flag & FAPPEND)
900 			*fbp++ = 'A';
901 #ifdef FSHLOCK	/* currently gone */
902 		if (fp->f_flag & FSHLOCK)
903 			*fbp++ = 'S';
904 		if (fp->f_flag & FEXLOCK)
905 			*fbp++ = 'X';
906 #endif
907 		if (fp->f_flag & FASYNC)
908 			*fbp++ = 'I';
909 		*fbp = '\0';
910 		printf("%6s  %3d", flagbuf, fp->f_count);
911 		printf("  %3d", fp->f_msgcount);
912 		printf("  %8lx", (u_long)(void *)fp->f_data);
913 		if (fp->f_offset < 0)
914 			printf("  %qx\n", fp->f_offset);
915 		else
916 			printf("  %qd\n", fp->f_offset);
917 	}
918 	free(ofp);
919 }
920 
921 /*
922  * swapmode is based on a program called swapinfo written
923  * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
924  */
925 void
926 swapmode(void)
927 {
928 	struct kvm_swap kswap[16];
929 	int i;
930 	int n;
931 	int pagesize = getpagesize();
932 	const char *header;
933 	int hlen;
934 	long blocksize;
935 
936 	n = kvm_getswapinfo(
937 	    kd,
938 	    kswap,
939 	    sizeof(kswap)/sizeof(kswap[0]),
940 	    ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX
941 	);
942 
943 #define CONVERT(v)	((int)((quad_t)(v) * pagesize / blocksize))
944 
945 	header = getbsize(&hlen, &blocksize);
946 	if (totalflag == 0) {
947 		printf("%-15s %*s %8s %8s %8s  %s\n",
948 		    "Device", hlen, header,
949 		    "Used", "Avail", "Capacity", "Type");
950 
951 		for (i = 0; i < n; ++i) {
952 			printf(
953 			    "%-15s %*d ",
954 			    kswap[i].ksw_devname,
955 			    hlen,
956 			    CONVERT(kswap[i].ksw_total)
957 			);
958 			printf(
959 			    "%8d %8d %5.0f%%    %s\n",
960 			    CONVERT(kswap[i].ksw_used),
961 			    CONVERT(kswap[i].ksw_total - kswap[i].ksw_used),
962 			    (double)kswap[i].ksw_used * 100.0 /
963 				(double)kswap[i].ksw_total,
964 			    (kswap[i].ksw_flags & SW_SEQUENTIAL) ?
965 				"Sequential" : "Interleaved"
966 			);
967 		}
968 	}
969 
970 	if (totalflag) {
971 		blocksize = 1024 * 1024;
972 
973 		printf(
974 		    "%dM/%dM swap space\n",
975 		    CONVERT(kswap[n].ksw_used),
976 		    CONVERT(kswap[n].ksw_total)
977 		);
978 	} else if (n > 1) {
979 		printf(
980 		    "%-15s %*d %8d %8d %5.0f%%\n",
981 		    "Total",
982 		    hlen,
983 		    CONVERT(kswap[n].ksw_total),
984 		    CONVERT(kswap[n].ksw_used),
985 		    CONVERT(kswap[n].ksw_total - kswap[n].ksw_used),
986 		    (double)kswap[n].ksw_used * 100.0 /
987 			(double)kswap[n].ksw_total
988 		);
989 	}
990 }
991