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