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