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