pstat.c revision 1.120
1/*	$OpenBSD: pstat.c,v 1.120 2018/09/10 06:40:06 yasuoka Exp $	*/
2/*	$NetBSD: pstat.c,v 1.27 1996/10/23 22:50:06 cgd Exp $	*/
3
4/*-
5 * Copyright (c) 1980, 1991, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/param.h>	/* MAXCOMLEN DEV_BSIZE */
34#include <sys/types.h>
35#include <sys/proc.h>
36#include <sys/time.h>
37#include <sys/vnode.h>
38#include <sys/ucred.h>
39#include <sys/stat.h>
40#define _KERNEL
41#include <sys/file.h>
42#include <ufs/ufs/quota.h>
43#include <ufs/ufs/inode.h>
44#include <sys/mount.h>
45#undef _KERNEL
46#include <nfs/nfsproto.h>
47#include <nfs/rpcv2.h>
48#include <nfs/nfsnode.h>
49#include <sys/ioctl.h>
50#include <sys/tty.h>
51#include <sys/conf.h>
52#include <sys/device.h>
53#include <sys/swap.h>
54
55#include <sys/sysctl.h>
56
57#include <stdint.h>
58#include <endian.h>
59#include <err.h>
60#include <fcntl.h>
61#include <kvm.h>
62#include <limits.h>
63#include <nlist.h>
64#include <paths.h>
65#include <stdio.h>
66#include <stdlib.h>
67#include <string.h>
68#include <unistd.h>
69
70struct nlist vnodenl[] = {
71#define	FNL_NFILE	0		/* sysctl */
72	{"_numfiles"},
73#define FNL_MAXFILE	1		/* sysctl */
74	{"_maxfiles"},
75#define TTY_NTTY	2		/* sysctl */
76	{"_tty_count"},
77#define V_NUMV		3		/* sysctl */
78	{ "_numvnodes" },
79#define TTY_TTYLIST	4		/* sysctl */
80	{"_ttylist"},
81#define	V_MOUNTLIST	5		/* no sysctl */
82	{ "_mountlist" },
83	{ NULL }
84};
85
86struct itty *globalitp;
87struct kinfo_file *kf;
88struct nlist *globalnl;
89
90struct e_vnode {
91	struct vnode *vptr;
92	struct vnode vnode;
93};
94
95int	kflag;
96int	totalflag;
97int	usenumflag;
98int	hideroot;
99int	maxfile;
100int	need_nlist;
101int	nfile;
102int	ntty;
103int	numvnodes;
104char	*nlistf	= NULL;
105char	*memf	= NULL;
106kvm_t	*kd = NULL;
107
108#define	SVAR(var) __STRING(var)	/* to force expansion */
109#define	KGET(idx, var)							\
110	KGET1(idx, &var, sizeof(var), SVAR(var))
111#define	KGET1(idx, p, s, msg)						\
112	KGET2(globalnl[idx].n_value, p, s, msg)
113#define	KGET2(addr, p, s, msg)						\
114	if (kvm_read(kd, (u_long)(addr), p, s) != s)			\
115		warnx("cannot read %s: %s", msg, kvm_geterr(kd))
116#define	KGETRET(addr, p, s, msg)					\
117	if (kvm_read(kd, (u_long)(addr), p, s) != s) {			\
118		warnx("cannot read %s: %s", msg, kvm_geterr(kd));	\
119		return (0);						\
120	}
121
122void	filemode(void);
123void	filemodeprep(void);
124struct mount *
125	getmnt(struct mount *);
126struct e_vnode *
127	kinfo_vnodes(void);
128void	mount_print(struct mount *);
129void	nfs_header(void);
130int	nfs_print(struct vnode *);
131void	swapmode(void);
132void	ttymode(void);
133void	ttymodeprep(void);
134void	ttyprt(struct itty *);
135void	tty2itty(struct tty *tp, struct itty *itp);
136void	ufs_header(void);
137int	ufs_print(struct vnode *);
138void	ext2fs_header(void);
139int	ext2fs_print(struct vnode *);
140static void __dead	usage(void);
141void	vnode_header(void);
142void	vnode_print(struct vnode *, struct vnode *);
143void	vnodemode(void);
144void	vnodemodeprep(void);
145
146
147int
148main(int argc, char *argv[])
149{
150	int fileflag = 0, swapflag = 0, ttyflag = 0, vnodeflag = 0, ch;
151	char buf[_POSIX2_LINE_MAX];
152	const char *dformat = NULL;
153	extern char *optarg;
154	extern int optind;
155	int i;
156
157	hideroot = getuid();
158
159	while ((ch = getopt(argc, argv, "d:TM:N:fiknstv")) != -1)
160		switch (ch) {
161		case 'd':
162			dformat = optarg;
163			break;
164		case 'f':
165			fileflag = 1;
166			break;
167		case 'M':
168			memf = optarg;
169			break;
170		case 'N':
171			nlistf = optarg;
172			break;
173		case 'n':
174			usenumflag = 1;
175			break;
176		case 's':
177			swapflag = 1;
178			break;
179		case 'T':
180			totalflag = 1;
181			break;
182		case 't':
183			ttyflag = 1;
184			break;
185		case 'k':
186			kflag = 1;
187			break;
188		case 'v':
189		case 'i':		/* Backward compatibility. */
190			vnodeflag = 1;
191			break;
192		default:
193			usage();
194		}
195	argc -= optind;
196	argv += optind;
197
198	if (dformat && getuid())
199		errx(1, "Only root can use -d");
200
201	if ((dformat == NULL && argc > 0) || (dformat && argc == 0))
202		usage();
203
204	need_nlist = dformat || vnodeflag;
205
206	if (nlistf != NULL || memf != NULL) {
207		if (fileflag || totalflag)
208			need_nlist = 1;
209	}
210
211	if (vnodeflag || fileflag || dformat || need_nlist)
212		if ((kd = kvm_openfiles(nlistf, memf, NULL,
213		    O_RDONLY | (need_nlist ? 0 : KVM_NO_FILES), buf)) == 0)
214			errx(1, "kvm_openfiles: %s", buf);
215
216	if (need_nlist)
217		if (kvm_nlist(kd, vnodenl) == -1)
218			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
219
220	if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag || dformat))
221		usage();
222
223	if(!dformat) {
224		if (fileflag || totalflag)
225			filemodeprep();
226		if (vnodeflag || totalflag)
227			vnodemodeprep();
228		if (ttyflag)
229			ttymodeprep();
230	}
231
232	if (unveil(_PATH_DEVDB, "r") == -1)
233		err(1, "unveil");
234	if (pledge("stdio rpath vminfo", NULL) == -1)
235		err(1, "pledge");
236
237	if (dformat) {
238		struct nlist *nl;
239		int longformat = 0, stringformat = 0, error = 0, n;
240		uint32_t mask = ~0;
241		char format[10], buf[1024];
242
243		n = strlen(dformat);
244		if (n == 0)
245			errx(1, "illegal format");
246
247		/*
248		 * Support p, c, s, and {l, ll, h, hh, j, t, z, }[diouxX]
249		 */
250		if (strcmp(dformat, "p") == 0)
251			longformat = sizeof(long) == 8;
252		else if (strcmp(dformat, "c") == 0)
253			mask = 0xff;
254		else if (strcmp(dformat, "s") == 0)
255			stringformat = 1;
256		else if (strchr("diouxX", dformat[n - 1])) {
257			char *ptbl[]= {"l", "ll", "h", "hh", "j", "t", "z", ""};
258			int i;
259
260			char *mod;
261			for (i = 0; i < sizeof(ptbl)/sizeof(ptbl[0]); i++) {
262				mod = ptbl[i];
263				if (strlen(mod) == n - 1 &&
264				    strncmp(mod, dformat, strlen(mod)) == 0)
265					break;
266			}
267			if (i == sizeof(ptbl)/sizeof(ptbl[0])
268			    && dformat[1] != '\0')
269				errx(1, "illegal format");
270			if (strcmp(mod, "l") == 0)
271				longformat = sizeof(long) == sizeof(long long);
272			else if (strcmp(mod, "h") == 0)
273				mask = 0xffff;
274			else if (strcmp(mod, "hh") == 0)
275				mask = 0xff;
276			else if (strcmp(mod, "") != 0)
277				longformat = 1;
278
279		} else
280			errx(1, "illegal format");
281
282		if (*dformat == 's') {
283			stringformat = 1;
284			snprintf(format, sizeof(format), "%%.%zus",
285			    sizeof buf);
286		} else
287			snprintf(format, sizeof(format), "%%%s", dformat);
288
289		nl = calloc(argc + 1, sizeof *nl);
290		if (!nl)
291			err(1, "calloc nl: ");
292		for (i = 0; i < argc; i++) {
293			if (asprintf(&nl[i].n_name, "_%s",
294			    argv[i]) == -1)
295				warn("asprintf");
296		}
297		kvm_nlist(kd, nl);
298		globalnl = nl;
299		for (i = 0; i < argc; i++) {
300			uint64_t v;
301
302			printf("%s ", argv[i]);
303			if (!nl[i].n_value && argv[i][0] == '0') {
304				nl[i].n_value = strtoul(argv[i], NULL, 16);
305				nl[i].n_type = N_DATA;
306			}
307			if (!nl[i].n_value) {
308				printf("not found\n");
309				error++;
310				continue;
311			}
312
313			printf("at %p: ", (void *)nl[i].n_value);
314			if ((nl[i].n_type & N_TYPE) == N_DATA ||
315			    (nl[i].n_type & N_TYPE) == N_COMM) {
316				if (stringformat) {
317					KGET1(i, &buf, sizeof(buf), argv[i]);
318					buf[sizeof(buf) - 1] = '\0';
319				} else
320					KGET1(i, &v, sizeof(v), argv[i]);
321				if (stringformat)
322					printf(format, &buf);
323				else if (longformat)
324					printf(format, v);
325				else {
326#if BYTE_ORDER == BIG_ENDIAN
327					switch (mask) {
328					case 0xff:
329						v >>= 8;
330						/* FALLTHROUGH */
331					case 0xffff:
332						v >>= 16;
333						/* FALLTHROUGH */
334					case 0xffffffff:
335						v >>= 32;
336						break;
337					}
338#endif
339					printf(format, ((uint32_t)v) & mask);
340				}
341			}
342			printf("\n");
343		}
344		for (i = 0; i < argc; i++)
345			free(nl[i].n_name);
346		free(nl);
347		return error;
348	}
349
350	if (fileflag || totalflag)
351		filemode();
352	if (vnodeflag || totalflag)
353		vnodemode();
354	if (ttyflag)
355		ttymode();
356	if (swapflag || totalflag)
357		swapmode();
358	return 0;
359}
360
361void
362vnodemode(void)
363{
364	struct e_vnode *e_vnodebase, *endvnode, *evp;
365	struct vnode *vp;
366	struct mount *maddr, *mp = NULL;
367
368	globalnl = vnodenl;
369
370	e_vnodebase = kinfo_vnodes();
371	if (totalflag) {
372		(void)printf("%7d vnodes\n", numvnodes);
373		return;
374	}
375	if (!e_vnodebase)
376		return;
377	endvnode = e_vnodebase + numvnodes;
378	(void)printf("%d active vnodes\n", numvnodes);
379
380	maddr = NULL;
381	for (evp = e_vnodebase; evp < endvnode; evp++) {
382		vp = &evp->vnode;
383		if (vp->v_mount != maddr) {
384			/*
385			 * New filesystem
386			 */
387			if ((mp = getmnt(vp->v_mount)) == NULL)
388				continue;
389			maddr = vp->v_mount;
390			mount_print(mp);
391			vnode_header();
392			if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_FFS, MFSNAMELEN) ||
393			    !strncmp(mp->mnt_stat.f_fstypename, MOUNT_MFS, MFSNAMELEN)) {
394				ufs_header();
395			} else if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_NFS,
396			    MFSNAMELEN)) {
397				nfs_header();
398			} else if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_EXT2FS,
399			    MFSNAMELEN)) {
400				ext2fs_header();
401			}
402			(void)printf("\n");
403		}
404		vnode_print(evp->vptr, vp);
405
406		/* Syncer vnodes have no associated fs-specific data */
407		if (vp->v_data == NULL) {
408			printf(" %6c %5c %7c\n", '-', '-', '-');
409			continue;
410		}
411
412		if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_FFS, MFSNAMELEN) ||
413		    !strncmp(mp->mnt_stat.f_fstypename, MOUNT_MFS, MFSNAMELEN)) {
414			ufs_print(vp);
415		} else if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_NFS, MFSNAMELEN)) {
416			nfs_print(vp);
417		} else if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_EXT2FS,
418		    MFSNAMELEN)) {
419			ext2fs_print(vp);
420		}
421		(void)printf("\n");
422	}
423	free(e_vnodebase);
424}
425
426void
427vnodemodeprep(void)
428{
429	int mib[2];
430	size_t num;
431
432	if (kd == NULL) {
433		mib[0] = CTL_KERN;
434		mib[1] = KERN_NUMVNODES;
435		num = sizeof(numvnodes);
436		if (sysctl(mib, 2, &numvnodes, &num, NULL, 0) < 0)
437			err(1, "sysctl(KERN_NUMVNODES) failed");
438	}
439}
440
441void
442vnode_header(void)
443{
444	(void)printf("%*s TYP VFLAG  USE HOLD", 2 * (int)sizeof(long), "ADDR");
445}
446
447void
448vnode_print(struct vnode *avnode, struct vnode *vp)
449{
450	char *type, flags[16];
451	char *fp;
452	int flag;
453
454	/*
455	 * set type
456	 */
457	switch (vp->v_type) {
458	case VNON:
459		type = "non"; break;
460	case VREG:
461		type = "reg"; break;
462	case VDIR:
463		type = "dir"; break;
464	case VBLK:
465		type = "blk"; break;
466	case VCHR:
467		type = "chr"; break;
468	case VLNK:
469		type = "lnk"; break;
470	case VSOCK:
471		type = "soc"; break;
472	case VFIFO:
473		type = "fif"; break;
474	case VBAD:
475		type = "bad"; break;
476	default:
477		type = "unk"; break;
478	}
479	/*
480	 * gather flags
481	 */
482	fp = flags;
483	flag = vp->v_flag;
484	if (flag & VROOT)
485		*fp++ = 'R';
486	if (flag & VTEXT)
487		*fp++ = 'T';
488	if (flag & VSYSTEM)
489		*fp++ = 'S';
490	if (flag & VISTTY)
491		*fp++ = 'I';
492	if (flag & VXLOCK)
493		*fp++ = 'L';
494	if (flag & VXWANT)
495		*fp++ = 'W';
496	if (vp->v_bioflag & VBIOWAIT)
497		*fp++ = 'B';
498	if (flag & VALIASED)
499		*fp++ = 'A';
500	if (vp->v_bioflag & VBIOONFREELIST)
501		*fp++ = 'F';
502	if (flag & VLOCKSWORK)
503		*fp++ = 'l';
504	if (vp->v_bioflag & VBIOONSYNCLIST)
505		*fp++ = 's';
506	if (fp == flags)
507		*fp++ = '-';
508	*fp = '\0';
509	(void)printf("%0*lx %s %5s %4d %4u",
510	    2 * (int)sizeof(long), hideroot ? 0L : (long)avnode,
511	    type, flags, vp->v_usecount, vp->v_holdcnt);
512}
513
514void
515ufs_header(void)
516{
517	(void)printf(" FILEID IFLAG RDEV|SZ");
518}
519
520int
521ufs_print(struct vnode *vp)
522{
523	int flag;
524	struct inode inode, *ip = &inode;
525	struct ufs1_dinode di1;
526	char flagbuf[16], *flags = flagbuf;
527	char *name;
528	mode_t type;
529
530	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
531	KGETRET(inode.i_din1, &di1, sizeof(struct ufs1_dinode),
532	    "vnode's dinode");
533
534	inode.i_din1 = &di1;
535	flag = ip->i_flag;
536#if 0
537	if (flag & IN_LOCKED)
538		*flags++ = 'L';
539	if (flag & IN_WANTED)
540		*flags++ = 'W';
541	if (flag & IN_LWAIT)
542		*flags++ = 'Z';
543#endif
544	if (flag & IN_ACCESS)
545		*flags++ = 'A';
546	if (flag & IN_CHANGE)
547		*flags++ = 'C';
548	if (flag & IN_UPDATE)
549		*flags++ = 'U';
550	if (flag & IN_MODIFIED)
551		*flags++ = 'M';
552	if (flag & IN_LAZYMOD)
553		*flags++ = 'm';
554	if (flag & IN_RENAME)
555		*flags++ = 'R';
556	if (flag & IN_SHLOCK)
557		*flags++ = 'S';
558	if (flag & IN_EXLOCK)
559		*flags++ = 'E';
560	if (flag == 0)
561		*flags++ = '-';
562	*flags = '\0';
563
564	(void)printf(" %6d %5s", ip->i_number, flagbuf);
565	type = ip->i_ffs1_mode & S_IFMT;
566	if (S_ISCHR(ip->i_ffs1_mode) || S_ISBLK(ip->i_ffs1_mode))
567		if (usenumflag ||
568		    ((name = devname(ip->i_ffs1_rdev, type)) == NULL))
569			(void)printf("   %2d,%-2d",
570			    major(ip->i_ffs1_rdev), minor(ip->i_ffs1_rdev));
571		else
572			(void)printf(" %7s", name);
573	else
574		(void)printf(" %7lld", (long long)ip->i_ffs1_size);
575	return (0);
576}
577
578void
579ext2fs_header(void)
580{
581	(void)printf(" FILEID IFLAG SZ");
582}
583
584int
585ext2fs_print(struct vnode *vp)
586{
587	int flag;
588	struct inode inode, *ip = &inode;
589	struct ext2fs_dinode di;
590	char flagbuf[16], *flags = flagbuf;
591
592	KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode");
593	KGETRET(inode.i_e2din, &di, sizeof(struct ext2fs_dinode),
594	    "vnode's dinode");
595
596	inode.i_e2din = &di;
597	flag = ip->i_flag;
598
599#if 0
600	if (flag & IN_LOCKED)
601		*flags++ = 'L';
602	if (flag & IN_WANTED)
603		*flags++ = 'W';
604	if (flag & IN_LWAIT)
605		*flags++ = 'Z';
606#endif
607	if (flag & IN_ACCESS)
608		*flags++ = 'A';
609	if (flag & IN_CHANGE)
610		*flags++ = 'C';
611	if (flag & IN_UPDATE)
612		*flags++ = 'U';
613	if (flag & IN_MODIFIED)
614		*flags++ = 'M';
615	if (flag & IN_RENAME)
616		*flags++ = 'R';
617	if (flag & IN_SHLOCK)
618		*flags++ = 'S';
619	if (flag & IN_EXLOCK)
620		*flags++ = 'E';
621	if (flag == 0)
622		*flags++ = '-';
623	*flags = '\0';
624
625	(void)printf(" %6d %5s %2d", ip->i_number, flagbuf, ip->i_e2fs_size);
626	return (0);
627}
628
629void
630nfs_header(void)
631{
632	(void)printf(" FILEID NFLAG RDEV|SZ");
633}
634
635int
636nfs_print(struct vnode *vp)
637{
638	struct nfsnode nfsnode, *np = &nfsnode;
639	char flagbuf[16], *flags = flagbuf;
640	int flag;
641	char *name;
642	mode_t type;
643
644	KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode");
645	flag = np->n_flag;
646	if (flag & NFLUSHWANT)
647		*flags++ = 'W';
648	if (flag & NFLUSHINPROG)
649		*flags++ = 'P';
650	if (flag & NMODIFIED)
651		*flags++ = 'M';
652	if (flag & NWRITEERR)
653		*flags++ = 'E';
654	if (flag & NACC)
655		*flags++ = 'A';
656	if (flag & NUPD)
657		*flags++ = 'U';
658	if (flag & NCHG)
659		*flags++ = 'C';
660	if (flag == 0)
661		*flags++ = '-';
662	*flags = '\0';
663
664	(void)printf(" %6lld %5s", (long long)np->n_vattr.va_fileid, flagbuf);
665	type = np->n_vattr.va_mode & S_IFMT;
666	if (S_ISCHR(np->n_vattr.va_mode) || S_ISBLK(np->n_vattr.va_mode))
667		if (usenumflag ||
668		    ((name = devname(np->n_vattr.va_rdev, type)) == NULL))
669			(void)printf("   %2d,%-2d", major(np->n_vattr.va_rdev),
670			    minor(np->n_vattr.va_rdev));
671		else
672			(void)printf(" %7s", name);
673	else
674		(void)printf(" %7lld", (long long)np->n_size);
675	return (0);
676}
677
678/*
679 * Given a pointer to a mount structure in kernel space,
680 * read it in and return a usable pointer to it.
681 */
682struct mount *
683getmnt(struct mount *maddr)
684{
685	static struct mtab {
686		struct mtab *next;
687		struct mount *maddr;
688		struct mount mount;
689	} *mhead = NULL;
690	struct mtab *mt;
691
692	for (mt = mhead; mt != NULL; mt = mt->next)
693		if (maddr == mt->maddr)
694			return (&mt->mount);
695	if ((mt = malloc(sizeof(struct mtab))) == NULL)
696		err(1, "malloc: mount table");
697	KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table");
698	mt->maddr = maddr;
699	mt->next = mhead;
700	mhead = mt;
701	return (&mt->mount);
702}
703
704void
705mount_print(struct mount *mp)
706{
707	int flags;
708
709	(void)printf("*** MOUNT ");
710	(void)printf("%.*s %s on %s", MFSNAMELEN,
711	    mp->mnt_stat.f_fstypename, mp->mnt_stat.f_mntfromname,
712	    mp->mnt_stat.f_mntonname);
713	if ((flags = mp->mnt_flag)) {
714		char *comma = "(";
715
716		putchar(' ');
717		/* user visible flags */
718		if (flags & MNT_RDONLY) {
719			(void)printf("%srdonly", comma);
720			flags &= ~MNT_RDONLY;
721			comma = ",";
722		}
723		if (flags & MNT_SYNCHRONOUS) {
724			(void)printf("%ssynchronous", comma);
725			flags &= ~MNT_SYNCHRONOUS;
726			comma = ",";
727		}
728		if (flags & MNT_NOEXEC) {
729			(void)printf("%snoexec", comma);
730			flags &= ~MNT_NOEXEC;
731			comma = ",";
732		}
733		if (flags & MNT_NOSUID) {
734			(void)printf("%snosuid", comma);
735			flags &= ~MNT_NOSUID;
736			comma = ",";
737		}
738		if (flags & MNT_NODEV) {
739			(void)printf("%snodev", comma);
740			flags &= ~MNT_NODEV;
741			comma = ",";
742		}
743		if (flags & MNT_ASYNC) {
744			(void)printf("%sasync", comma);
745			flags &= ~MNT_ASYNC;
746			comma = ",";
747		}
748		if (flags & MNT_EXRDONLY) {
749			(void)printf("%sexrdonly", comma);
750			flags &= ~MNT_EXRDONLY;
751			comma = ",";
752		}
753		if (flags & MNT_EXPORTED) {
754			(void)printf("%sexport", comma);
755			flags &= ~MNT_EXPORTED;
756			comma = ",";
757		}
758		if (flags & MNT_DEFEXPORTED) {
759			(void)printf("%sdefdexported", comma);
760			flags &= ~MNT_DEFEXPORTED;
761			comma = ",";
762		}
763		if (flags & MNT_EXPORTANON) {
764			(void)printf("%sexportanon", comma);
765			flags &= ~MNT_EXPORTANON;
766			comma = ",";
767		}
768		if (flags & MNT_WXALLOWED) {
769			(void)printf("%swxallowed", comma);
770			flags &= ~MNT_WXALLOWED;
771			comma = ",";
772		}
773		if (flags & MNT_LOCAL) {
774			(void)printf("%slocal", comma);
775			flags &= ~MNT_LOCAL;
776			comma = ",";
777		}
778		if (flags & MNT_QUOTA) {
779			(void)printf("%squota", comma);
780			flags &= ~MNT_QUOTA;
781			comma = ",";
782		}
783		if (flags & MNT_ROOTFS) {
784			(void)printf("%srootfs", comma);
785			flags &= ~MNT_ROOTFS;
786			comma = ",";
787		}
788		if (flags & MNT_NOATIME) {
789			(void)printf("%snoatime", comma);
790			flags &= ~MNT_NOATIME;
791			comma = ",";
792		}
793		/* filesystem control flags */
794		if (flags & MNT_UPDATE) {
795			(void)printf("%supdate", comma);
796			flags &= ~MNT_UPDATE;
797			comma = ",";
798		}
799		if (flags & MNT_DELEXPORT) {
800			(void)printf("%sdelexport", comma);
801			flags &= ~MNT_DELEXPORT;
802			comma = ",";
803		}
804		if (flags & MNT_RELOAD) {
805			(void)printf("%sreload", comma);
806			flags &= ~MNT_RELOAD;
807			comma = ",";
808		}
809		if (flags & MNT_FORCE) {
810			(void)printf("%sforce", comma);
811			flags &= ~MNT_FORCE;
812			comma = ",";
813		}
814		if (flags & MNT_WANTRDWR) {
815			(void)printf("%swantrdwr", comma);
816			flags &= ~MNT_WANTRDWR;
817			comma = ",";
818		}
819		if (flags & MNT_SOFTDEP) {
820			(void)printf("%ssoftdep", comma);
821			flags &= ~MNT_SOFTDEP;
822			comma = ",";
823		}
824		if (flags)
825			(void)printf("%sunknown_flags:%x", comma, flags);
826		(void)printf(")");
827	}
828	(void)printf("\n");
829}
830
831/*
832 * simulate what a running kernel does in kinfo_vnode
833 */
834struct e_vnode *
835kinfo_vnodes(void)
836{
837	struct mntlist kvm_mountlist;
838	struct mount *mp, mount;
839	struct vnode *vp, vnode;
840	char *vbuf, *evbuf, *bp;
841	size_t num;
842
843	if (kd != NULL)
844		KGET(V_NUMV, numvnodes);
845	if (totalflag)
846		return NULL;
847	if ((vbuf = calloc(numvnodes + 20,
848	    sizeof(struct vnode *) + sizeof(struct vnode))) == NULL)
849		err(1, "malloc: vnode buffer");
850	bp = vbuf;
851	evbuf = vbuf + (numvnodes + 20) *
852	    (sizeof(struct vnode *) + sizeof(struct vnode));
853	KGET(V_MOUNTLIST, kvm_mountlist);
854	num = 0;
855	for (mp = TAILQ_FIRST(&kvm_mountlist); mp != NULL;
856	    mp = TAILQ_NEXT(&mount, mnt_list)) {
857		KGETRET(mp, &mount, sizeof(mount), "mount entry");
858		for (vp = LIST_FIRST(&mount.mnt_vnodelist);
859		    vp != NULL; vp = LIST_NEXT(&vnode, v_mntvnodes)) {
860			KGETRET(vp, &vnode, sizeof(vnode), "vnode");
861			if ((bp + sizeof(struct vnode *) +
862			    sizeof(struct vnode)) > evbuf)
863				/* XXX - should realloc */
864				errx(1, "no more room for vnodes");
865			memmove(bp, &vp, sizeof(struct vnode *));
866			bp += sizeof(struct vnode *);
867			memmove(bp, &vnode, sizeof(struct vnode));
868			bp += sizeof(struct vnode);
869			num++;
870		}
871	}
872	numvnodes = num;
873	return ((struct e_vnode *)vbuf);
874}
875
876const char hdr[] =
877"   LINE RAW  CAN  OUT  HWT LWT    COL STATE      SESS  PGID DISC\n";
878
879void
880tty2itty(struct tty *tp, struct itty *itp)
881{
882	itp->t_dev = tp->t_dev;
883	itp->t_rawq_c_cc = tp->t_rawq.c_cc;
884	itp->t_canq_c_cc = tp->t_canq.c_cc;
885	itp->t_outq_c_cc = tp->t_outq.c_cc;
886	itp->t_hiwat = tp->t_hiwat;
887	itp->t_lowat = tp->t_lowat;
888	itp->t_column = tp->t_column;
889	itp->t_state = tp->t_state;
890	itp->t_session = tp->t_session;
891	if (tp->t_pgrp != NULL)
892		KGET2(&tp->t_pgrp->pg_id, &itp->t_pgrp_pg_id, sizeof(pid_t), "pgid");
893	itp->t_line = tp->t_line;
894}
895
896void
897ttymode(void)
898{
899	struct ttylist_head tty_head;
900	struct tty *tp, tty;
901	int i;
902	struct itty itty;
903
904	if (need_nlist)
905		KGET(TTY_NTTY, ntty);
906	(void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s");
907	(void)printf("%s", hdr);
908	if (!need_nlist) {
909		for (i = 0; i < ntty; i++)
910			ttyprt(&globalitp[i]);
911		free(globalitp);
912	} else {
913		KGET(TTY_TTYLIST, tty_head);
914		for (tp = TAILQ_FIRST(&tty_head); tp;
915		    tp = TAILQ_NEXT(&tty, tty_link)) {
916			KGET2(tp, &tty, sizeof tty, "tty struct");
917			tty2itty(&tty, &itty);
918			ttyprt(&itty);
919		}
920	}
921}
922
923void
924ttymodeprep(void)
925{
926	int mib[3];
927	size_t nlen;
928
929	if (!need_nlist) {
930		mib[0] = CTL_KERN;
931		mib[1] = KERN_TTYCOUNT;
932		nlen = sizeof(ntty);
933		if (sysctl(mib, 2, &ntty, &nlen, NULL, 0) < 0)
934			err(1, "sysctl(KERN_TTYCOUNT) failed");
935
936		mib[0] = CTL_KERN;
937		mib[1] = KERN_TTY;
938		mib[2] = KERN_TTY_INFO;
939		if ((globalitp = reallocarray(NULL, ntty, sizeof(struct itty))) == NULL)
940			err(1, "malloc");
941		nlen = ntty * sizeof(struct itty);
942		if (sysctl(mib, 3, globalitp, &nlen, NULL, 0) < 0)
943			err(1, "sysctl(KERN_TTY_INFO) failed");
944	}
945}
946
947struct {
948	int flag;
949	char val;
950} ttystates[] = {
951	{ TS_WOPEN,	'W'},
952	{ TS_ISOPEN,	'O'},
953	{ TS_CARR_ON,	'C'},
954	{ TS_TIMEOUT,	'T'},
955	{ TS_FLUSH,	'F'},
956	{ TS_BUSY,	'B'},
957	{ TS_ASLEEP,	'A'},
958	{ TS_XCLUDE,	'X'},
959	{ TS_TTSTOP,	'S'},
960	{ TS_TBLOCK,	'K'},
961	{ TS_ASYNC,	'Y'},
962	{ TS_BKSL,	'D'},
963	{ TS_ERASE,	'E'},
964	{ TS_LNCH,	'L'},
965	{ TS_TYPEN,	'P'},
966	{ TS_CNTTB,	'N'},
967	{ 0,		'\0'},
968};
969
970void
971ttyprt(struct itty *tp)
972{
973	char *name, state[20];
974	int i, j;
975
976	if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL)
977		(void)printf("%2d,%-3d   ", major(tp->t_dev), minor(tp->t_dev));
978	else
979		(void)printf("%7s ", name);
980	(void)printf("%3d %4d ", tp->t_rawq_c_cc, tp->t_canq_c_cc);
981	(void)printf("%4d %4d %3d %6d ", tp->t_outq_c_cc,
982		tp->t_hiwat, tp->t_lowat, tp->t_column);
983	for (i = j = 0; ttystates[i].flag; i++)
984		if (tp->t_state&ttystates[i].flag)
985			state[j++] = ttystates[i].val;
986	if (j == 0)
987		state[j++] = '-';
988	state[j] = '\0';
989	(void)printf("%-6s %8lx", state,
990		hideroot ? 0 : (u_long)tp->t_session & 0xffffffff);
991	(void)printf("%6d ", tp->t_pgrp_pg_id);
992	switch (tp->t_line) {
993	case TTYDISC:
994		(void)printf("term\n");
995		break;
996	case PPPDISC:
997		(void)printf("ppp\n");
998		break;
999	case NMEADISC:
1000		(void)printf("nmea\n");
1001		break;
1002	default:
1003		(void)printf("%d\n", tp->t_line);
1004		break;
1005	}
1006}
1007
1008void
1009filemode(void)
1010{
1011	char flagbuf[16], *fbp;
1012	static char *dtypes[] = { "???", "inode", "socket", "pipe", "kqueue", "???", "???" };
1013
1014	globalnl = vnodenl;
1015
1016	if (nlistf != NULL || memf != NULL) {
1017		KGET(FNL_MAXFILE, maxfile);
1018		if (totalflag) {
1019			KGET(FNL_NFILE, nfile);
1020			(void)printf("%3d/%3d files\n", nfile, maxfile);
1021			return;
1022		}
1023	}
1024
1025	(void)printf("%d/%d open files\n", nfile, maxfile);
1026	if (totalflag)
1027		return;
1028
1029	(void)printf("%*s TYPE       FLG  CNT  MSG  %*s  OFFSET\n",
1030	    2 * (int)sizeof(long), "LOC", 2 * (int)sizeof(long), "DATA");
1031	for (; nfile-- > 0; kf++) {
1032		(void)printf("%0*llx ", 2 * (int)sizeof(long),
1033		    hideroot ? 0LL : kf->f_fileaddr);
1034		(void)printf("%-8.8s", dtypes[
1035		    (kf->f_type >= (sizeof(dtypes)/sizeof(dtypes[0])))
1036		    ? 0 : kf->f_type]);
1037		fbp = flagbuf;
1038		if (kf->f_flag & FREAD)
1039			*fbp++ = 'R';
1040		if (kf->f_flag & FWRITE)
1041			*fbp++ = 'W';
1042		if (kf->f_flag & FAPPEND)
1043			*fbp++ = 'A';
1044		if (kf->f_flag & FASYNC)
1045			*fbp++ = 'I';
1046
1047		if (kf->f_iflags & FIF_HASLOCK)
1048			*fbp++ = 'L';
1049
1050		*fbp = '\0';
1051		(void)printf("%6s  %3ld", flagbuf, (long)kf->f_count);
1052		(void)printf("  %3ld", (long)kf->f_msgcount);
1053		(void)printf("  %0*lx", 2 * (int)sizeof(long),
1054		    hideroot ? 0L : (long)kf->f_data);
1055
1056		if (kf->f_offset == (uint64_t)-1)
1057			(void)printf("  *\n");
1058		else if (kf->f_offset > INT64_MAX) {
1059			/* would have been negative */
1060			(void)printf("  %llx\n",
1061			    hideroot ? 0LL : (long long)kf->f_offset);
1062		} else
1063			(void)printf("  %lld\n",
1064			    hideroot ? 0LL : (long long)kf->f_offset);
1065	}
1066}
1067
1068void
1069filemodeprep(void)
1070{
1071	int mib[2];
1072	size_t len;
1073
1074	if (nlistf == NULL && memf == NULL) {
1075		mib[0] = CTL_KERN;
1076		mib[1] = KERN_MAXFILES;
1077		len = sizeof(maxfile);
1078		if (sysctl(mib, 2, &maxfile, &len, NULL, 0) < 0)
1079			err(1, "sysctl(KERN_MAXFILES) failed");
1080		if (totalflag) {
1081			mib[0] = CTL_KERN;
1082			mib[1] = KERN_NFILES;
1083			len = sizeof(nfile);
1084			if (sysctl(mib, 2, &nfile, &len, NULL, 0) < 0)
1085				err(1, "sysctl(KERN_NFILES) failed");
1086		}
1087	}
1088
1089	if (!totalflag) {
1090		kf = kvm_getfiles(kd, KERN_FILE_BYFILE, 0, sizeof *kf, &nfile);
1091		if (kf == NULL) {
1092			warnx("kvm_getfiles: %s", kvm_geterr(kd));
1093			return;
1094		}
1095	}
1096}
1097
1098/*
1099 * swapmode is based on a program called swapinfo written
1100 * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
1101 */
1102void
1103swapmode(void)
1104{
1105	char *header;
1106	int hlen = 10, nswap;
1107	int bdiv, i, avail, nfree, npfree, used;
1108	long blocksize;
1109	struct swapent *swdev;
1110
1111	if (kflag) {
1112		header = "1K-blocks";
1113		blocksize = 1024;
1114		hlen = strlen(header);
1115	} else
1116		header = getbsize(&hlen, &blocksize);
1117
1118	nswap = swapctl(SWAP_NSWAP, 0, 0);
1119	if (nswap == 0) {
1120		if (!totalflag)
1121			(void)printf("%-11s %*s %8s %8s %8s  %s\n",
1122			    "Device", hlen, header,
1123			    "Used", "Avail", "Capacity", "Priority");
1124		(void)printf("%-11s %*d %8d %8d %5.0f%%\n",
1125		    "Total", hlen, 0, 0, 0, 0.0);
1126		return;
1127	}
1128	if ((swdev = calloc(nswap, sizeof(*swdev))) == NULL)
1129		err(1, "malloc");
1130	if (swapctl(SWAP_STATS, swdev, nswap) == -1)
1131		err(1, "swapctl");
1132
1133	if (!totalflag)
1134		(void)printf("%-11s %*s %8s %8s %8s  %s\n",
1135		    "Device", hlen, header,
1136		    "Used", "Avail", "Capacity", "Priority");
1137
1138	/* Run through swap list, doing the funky monkey. */
1139	bdiv = blocksize / DEV_BSIZE;
1140	avail = nfree = npfree = 0;
1141	for (i = 0; i < nswap; i++) {
1142		int xsize, xfree;
1143
1144		if (!(swdev[i].se_flags & SWF_ENABLE))
1145			continue;
1146
1147		if (!totalflag) {
1148			if (usenumflag)
1149				(void)printf("%2d,%-2d       %*d ",
1150				    major(swdev[i].se_dev),
1151				    minor(swdev[i].se_dev),
1152				    hlen, swdev[i].se_nblks / bdiv);
1153			else
1154				(void)printf("%-11s %*d ", swdev[i].se_path,
1155				    hlen, swdev[i].se_nblks / bdiv);
1156		}
1157
1158		xsize = swdev[i].se_nblks;
1159		used = swdev[i].se_inuse;
1160		xfree = xsize - used;
1161		nfree += (xsize - used);
1162		npfree++;
1163		avail += xsize;
1164		if (totalflag)
1165			continue;
1166		(void)printf("%8d %8d %5.0f%%    %d\n",
1167		    used / bdiv, xfree / bdiv,
1168		    (double)used / (double)xsize * 100.0,
1169		    swdev[i].se_priority);
1170	}
1171	free(swdev);
1172
1173	/*
1174	 * If only one partition has been set up via swapon(8), we don't
1175	 * need to bother with totals.
1176	 */
1177	used = avail - nfree;
1178	if (totalflag) {
1179		(void)printf("%dM/%dM swap space\n",
1180		    used / (1048576 / DEV_BSIZE),
1181		    avail / (1048576 / DEV_BSIZE));
1182		return;
1183	}
1184	if (npfree > 1) {
1185		(void)printf("%-11s %*d %8d %8d %5.0f%%\n",
1186		    "Total", hlen, avail / bdiv, used / bdiv, nfree / bdiv,
1187		    (double)used / (double)avail * 100.0);
1188	}
1189}
1190
1191static void __dead
1192usage(void)
1193{
1194	(void)fprintf(stderr, "usage: "
1195	    "pstat [-fknsTtv] [-M core] [-N system] [-d format symbol ...]\n");
1196	exit(1);
1197}
1198