xref: /freebsd/usr.sbin/pstat/pstat.c (revision 0cbfd1a52526521694bf833c9ac84fc3cdee000d)
1dea673e9SRodney W. Grimes /*-
29c5cdfe0SPeter Wemm  * Copyright (c) 1980, 1991, 1993, 1994
3dea673e9SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4dea673e9SRodney W. Grimes  *
5dea673e9SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6dea673e9SRodney W. Grimes  * modification, are permitted provided that the following conditions
7dea673e9SRodney W. Grimes  * are met:
8dea673e9SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10dea673e9SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11dea673e9SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12dea673e9SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13dea673e9SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
14dea673e9SRodney W. Grimes  *    must display the following acknowledgement:
15dea673e9SRodney W. Grimes  *	This product includes software developed by the University of
16dea673e9SRodney W. Grimes  *	California, Berkeley and its contributors.
17dea673e9SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
18dea673e9SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
19dea673e9SRodney W. Grimes  *    without specific prior written permission.
20dea673e9SRodney W. Grimes  *
21dea673e9SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22dea673e9SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23dea673e9SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24dea673e9SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25dea673e9SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26dea673e9SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27dea673e9SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28dea673e9SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29dea673e9SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30dea673e9SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31dea673e9SRodney W. Grimes  * SUCH DAMAGE.
32dea673e9SRodney W. Grimes  */
33dea673e9SRodney W. Grimes 
34dea673e9SRodney W. Grimes #ifndef lint
35d9961cfdSPhilippe Charnier static const char copyright[] =
369c5cdfe0SPeter Wemm "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\
37dea673e9SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
38dea673e9SRodney W. Grimes #endif /* not lint */
39dea673e9SRodney W. Grimes 
40dea673e9SRodney W. Grimes #ifndef lint
41d9961cfdSPhilippe Charnier #if 0
429c5cdfe0SPeter Wemm static char sccsid[] = "@(#)pstat.c	8.16 (Berkeley) 5/9/95";
43d9961cfdSPhilippe Charnier #endif
44d9961cfdSPhilippe Charnier static const char rcsid[] =
4597d92980SPeter Wemm   "$FreeBSD$";
46dea673e9SRodney W. Grimes #endif /* not lint */
47dea673e9SRodney W. Grimes 
48dea673e9SRodney W. Grimes #include <sys/param.h>
49dea673e9SRodney W. Grimes #include <sys/time.h>
50c4473420SPeter Wemm #define _KERNEL
51dea673e9SRodney W. Grimes #include <sys/file.h>
529c5cdfe0SPeter Wemm #include <sys/uio.h>
53c4473420SPeter Wemm #undef _KERNEL
54dea673e9SRodney W. Grimes #include <sys/stat.h>
55dea673e9SRodney W. Grimes #include <sys/ioctl.h>
5690300419SAndrey A. Chernov #include <sys/ioctl_compat.h>	/* XXX NTTYDISC is too well hidden */
57dea673e9SRodney W. Grimes #include <sys/tty.h>
58dea673e9SRodney W. Grimes #include <sys/conf.h>
59eedc3436SMatthew Dillon #include <sys/blist.h>
60dea673e9SRodney W. Grimes 
613aab05cfSPoul-Henning Kamp #include <sys/user.h>
62dea673e9SRodney W. Grimes #include <sys/sysctl.h>
63dea673e9SRodney W. Grimes 
64dea673e9SRodney W. Grimes #include <err.h>
655d98ce75SBruce Evans #include <fcntl.h>
66dea673e9SRodney W. Grimes #include <kvm.h>
67dea673e9SRodney W. Grimes #include <limits.h>
68dea673e9SRodney W. Grimes #include <nlist.h>
69dea673e9SRodney W. Grimes #include <stdio.h>
70dea673e9SRodney W. Grimes #include <stdlib.h>
71dea673e9SRodney W. Grimes #include <string.h>
72dea673e9SRodney W. Grimes #include <unistd.h>
73dea673e9SRodney W. Grimes 
741dcc9c32SDag-Erling Smørgrav static struct nlist nl[] = {
75782f8687SMatthew Dillon #define NLMANDATORYBEG	0
76782f8687SMatthew Dillon #define	V_MOUNTLIST	0
77dea673e9SRodney W. Grimes 	{ "_mountlist" },	/* address of head of mount list. */
78782f8687SMatthew Dillon #define V_NUMV		1
79dea673e9SRodney W. Grimes 	{ "_numvnodes" },
80782f8687SMatthew Dillon #define	FNL_NFILE	2
81dea673e9SRodney W. Grimes 	{"_nfiles"},
82782f8687SMatthew Dillon #define FNL_MAXFILE	3
83dea673e9SRodney W. Grimes 	{"_maxfiles"},
84eedc3436SMatthew Dillon #define NLMANDATORYEND FNL_MAXFILE	/* names up to here are mandatory */
85eedc3436SMatthew Dillon #define	SCONS		NLMANDATORYEND + 1
8609941f75SDag-Erling Smørgrav 	{ "_constty" },
87eedc3436SMatthew Dillon #define	SPTY		NLMANDATORYEND + 2
88dea673e9SRodney W. Grimes 	{ "_pt_tty" },
89eedc3436SMatthew Dillon #define	SNPTY		NLMANDATORYEND + 3
90dea673e9SRodney W. Grimes 	{ "_npty" },
91dea673e9SRodney W. Grimes 
92dea673e9SRodney W. Grimes 
93dea673e9SRodney W. Grimes 
945a3391bfSAndrey A. Chernov #ifdef __FreeBSD__
95782f8687SMatthew Dillon #define SCCONS	(SNPTY+1)
96de4c8be3SSøren Schmidt 	{ "_sccons" },
97782f8687SMatthew Dillon #define NSCCONS	(SNPTY+2)
98de4c8be3SSøren Schmidt 	{ "_nsccons" },
99782f8687SMatthew Dillon #define SIO  (SNPTY+3)
10090300419SAndrey A. Chernov 	{ "_sio_tty" },
101782f8687SMatthew Dillon #define NSIO (SNPTY+4)
10290300419SAndrey A. Chernov 	{ "_nsio_tty" },
103782f8687SMatthew Dillon #define RC  (SNPTY+5)
10490300419SAndrey A. Chernov 	{ "_rc_tty" },
105782f8687SMatthew Dillon #define NRC (SNPTY+6)
10690300419SAndrey A. Chernov 	{ "_nrc_tty" },
107782f8687SMatthew Dillon #define CY  (SNPTY+7)
10824f769b0SBruce Evans 	{ "_cy_tty" },
109782f8687SMatthew Dillon #define NCY (SNPTY+8)
11024f769b0SBruce Evans 	{ "_ncy_tty" },
111782f8687SMatthew Dillon #define SI  (SNPTY+9)
112a9aa8c37SPeter Wemm 	{ "_si_tty" },
113782f8687SMatthew Dillon #define NSI (SNPTY+10)
114a9aa8c37SPeter Wemm 	{ "_si_Nports" },
1155a3391bfSAndrey A. Chernov #endif
116dea673e9SRodney W. Grimes 	{ "" }
117dea673e9SRodney W. Grimes };
118dea673e9SRodney W. Grimes 
1191dcc9c32SDag-Erling Smørgrav static int	usenumflag;
1201dcc9c32SDag-Erling Smørgrav static int	totalflag;
1211dcc9c32SDag-Erling Smørgrav static int	swapflag;
1221dcc9c32SDag-Erling Smørgrav static char	*nlistf	= NULL;
1231dcc9c32SDag-Erling Smørgrav static char	*memf	= NULL;
1241dcc9c32SDag-Erling Smørgrav static kvm_t	*kd;
125dea673e9SRodney W. Grimes 
1261dcc9c32SDag-Erling Smørgrav static char	*usagestr;
12759392fe2SPoul-Henning Kamp 
128dea673e9SRodney W. Grimes #define	SVAR(var) __STRING(var)	/* to force expansion */
129dea673e9SRodney W. Grimes #define	KGET(idx, var)							\
130dea673e9SRodney W. Grimes 	KGET1(idx, &var, sizeof(var), SVAR(var))
131dea673e9SRodney W. Grimes #define	KGET1(idx, p, s, msg)						\
132dea673e9SRodney W. Grimes 	KGET2(nl[idx].n_value, p, s, msg)
133dea673e9SRodney W. Grimes #define	KGET2(addr, p, s, msg)						\
134dea673e9SRodney W. Grimes 	if (kvm_read(kd, (u_long)(addr), p, s) != s)			\
135dea673e9SRodney W. Grimes 		warnx("cannot read %s: %s", msg, kvm_geterr(kd))
136eedc3436SMatthew Dillon #define	KGETN(idx, var)							\
137eedc3436SMatthew Dillon 	KGET1N(idx, &var, sizeof(var), SVAR(var))
138eedc3436SMatthew Dillon #define	KGET1N(idx, p, s, msg)						\
139eedc3436SMatthew Dillon 	KGET2N(nl[idx].n_value, p, s, msg)
140eedc3436SMatthew Dillon #define	KGET2N(addr, p, s, msg)						\
141eedc3436SMatthew Dillon 	((kvm_read(kd, (u_long)(addr), p, s) == s) ? 1 : 0)
142dea673e9SRodney W. Grimes #define	KGETRET(addr, p, s, msg)					\
143dea673e9SRodney W. Grimes 	if (kvm_read(kd, (u_long)(addr), p, s) != s) {			\
144dea673e9SRodney W. Grimes 		warnx("cannot read %s: %s", msg, kvm_geterr(kd));	\
145dea673e9SRodney W. Grimes 		return (0);						\
146dea673e9SRodney W. Grimes 	}
147dea673e9SRodney W. Grimes 
148c9624363SDag-Erling Smørgrav static void	filemode(void);
149c9624363SDag-Erling Smørgrav static int	getfiles(char **, int *);
150c9624363SDag-Erling Smørgrav static void	swapmode(void);
151c9624363SDag-Erling Smørgrav static void	ttymode(void);
152c9624363SDag-Erling Smørgrav static void	ttyprt(struct tty *, int);
153c9624363SDag-Erling Smørgrav static void	ttytype(struct tty *, char *, int, int, int);
154c9624363SDag-Erling Smørgrav static void	usage(void);
155dea673e9SRodney W. Grimes 
156dea673e9SRodney W. Grimes int
157c9624363SDag-Erling Smørgrav main(int argc, char *argv[])
158dea673e9SRodney W. Grimes {
159dea673e9SRodney W. Grimes 	int ch, i, quit, ret;
1600cbfd1a5SDag-Erling Smørgrav 	int fileflag, ttyflag;
16159392fe2SPoul-Henning Kamp 	char buf[_POSIX2_LINE_MAX],*opts;
162dea673e9SRodney W. Grimes 
1630cbfd1a5SDag-Erling Smørgrav 	fileflag = swapflag = ttyflag = 0;
16459392fe2SPoul-Henning Kamp 
16559392fe2SPoul-Henning Kamp 	/* We will behave like good old swapinfo if thus invoked */
16659392fe2SPoul-Henning Kamp 	opts = strrchr(argv[0],'/');
16759392fe2SPoul-Henning Kamp 	if (opts)
16859392fe2SPoul-Henning Kamp 		opts++;
16959392fe2SPoul-Henning Kamp 	else
17059392fe2SPoul-Henning Kamp 		opts = argv[0];
17159392fe2SPoul-Henning Kamp 	if (!strcmp(opts,"swapinfo")) {
17259392fe2SPoul-Henning Kamp 		swapflag = 1;
1732d8e94a4SSujal Patel 		opts = "kM:N:";
174d9961cfdSPhilippe Charnier 		usagestr = "swapinfo [-k] [-M core] [-N system]";
17559392fe2SPoul-Henning Kamp 	} else {
1760cbfd1a5SDag-Erling Smørgrav 		opts = "TM:N:fknst";
177d9961cfdSPhilippe Charnier 		usagestr = "pstat [-Tfknstv] [-M core] [-N system]";
17859392fe2SPoul-Henning Kamp 	}
17959392fe2SPoul-Henning Kamp 
1806c3f552aSWarner Losh 	while ((ch = getopt(argc, argv, opts)) != -1)
181dea673e9SRodney W. Grimes 		switch (ch) {
182dea673e9SRodney W. Grimes 		case 'f':
183dea673e9SRodney W. Grimes 			fileflag = 1;
184dea673e9SRodney W. Grimes 			break;
18559392fe2SPoul-Henning Kamp 		case 'k':
18659392fe2SPoul-Henning Kamp 			putenv("BLOCKSIZE=1K");
18759392fe2SPoul-Henning Kamp 			break;
188dea673e9SRodney W. Grimes 		case 'M':
189dea673e9SRodney W. Grimes 			memf = optarg;
190dea673e9SRodney W. Grimes 			break;
191dea673e9SRodney W. Grimes 		case 'N':
192dea673e9SRodney W. Grimes 			nlistf = optarg;
193dea673e9SRodney W. Grimes 			break;
194dea673e9SRodney W. Grimes 		case 'n':
195dea673e9SRodney W. Grimes 			usenumflag = 1;
196dea673e9SRodney W. Grimes 			break;
197dea673e9SRodney W. Grimes 		case 's':
198eedc3436SMatthew Dillon 			++swapflag;
199dea673e9SRodney W. Grimes 			break;
200dea673e9SRodney W. Grimes 		case 'T':
201dea673e9SRodney W. Grimes 			totalflag = 1;
202dea673e9SRodney W. Grimes 			break;
203dea673e9SRodney W. Grimes 		case 't':
204dea673e9SRodney W. Grimes 			ttyflag = 1;
205dea673e9SRodney W. Grimes 			break;
206dea673e9SRodney W. Grimes 		default:
207d9961cfdSPhilippe Charnier 			usage();
208dea673e9SRodney W. Grimes 		}
209dea673e9SRodney W. Grimes 	argc -= optind;
210dea673e9SRodney W. Grimes 	argv += optind;
211dea673e9SRodney W. Grimes 
212dea673e9SRodney W. Grimes 	/*
213dea673e9SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
214dea673e9SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
215dea673e9SRodney W. Grimes 	 */
216dea673e9SRodney W. Grimes 	if (nlistf != NULL || memf != NULL)
217dea673e9SRodney W. Grimes 		(void)setgid(getgid());
218dea673e9SRodney W. Grimes 
219dea673e9SRodney W. Grimes 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0)
220dea673e9SRodney W. Grimes 		errx(1, "kvm_openfiles: %s", buf);
2213b7e5cccSRuslan Ermilov 	(void)setgid(getgid());
222dea673e9SRodney W. Grimes 	if ((ret = kvm_nlist(kd, nl)) != 0) {
223dea673e9SRodney W. Grimes 		if (ret == -1)
224dea673e9SRodney W. Grimes 			errx(1, "kvm_nlist: %s", kvm_geterr(kd));
225eedc3436SMatthew Dillon 		for (i = NLMANDATORYBEG, quit = 0; i <= NLMANDATORYEND; i++)
226dea673e9SRodney W. Grimes 			if (!nl[i].n_value) {
227dea673e9SRodney W. Grimes 				quit = 1;
228d9961cfdSPhilippe Charnier 				warnx("undefined symbol: %s", nl[i].n_name);
229dea673e9SRodney W. Grimes 			}
230dea673e9SRodney W. Grimes 		if (quit)
231dea673e9SRodney W. Grimes 			exit(1);
232dea673e9SRodney W. Grimes 	}
2330cbfd1a5SDag-Erling Smørgrav 	if (!(fileflag | ttyflag | swapflag | totalflag))
234d9961cfdSPhilippe Charnier 		usage();
235dea673e9SRodney W. Grimes 	if (fileflag || totalflag)
236dea673e9SRodney W. Grimes 		filemode();
237dea673e9SRodney W. Grimes 	if (ttyflag)
238dea673e9SRodney W. Grimes 		ttymode();
239dea673e9SRodney W. Grimes 	if (swapflag || totalflag)
240dea673e9SRodney W. Grimes 		swapmode();
241dea673e9SRodney W. Grimes 	exit (0);
242dea673e9SRodney W. Grimes }
243dea673e9SRodney W. Grimes 
244d9961cfdSPhilippe Charnier static void
245c9624363SDag-Erling Smørgrav usage(void)
246d9961cfdSPhilippe Charnier {
247d9961cfdSPhilippe Charnier 	fprintf(stderr, "usage: %s\n", usagestr);
248d9961cfdSPhilippe Charnier 	exit (1);
249d9961cfdSPhilippe Charnier }
250d9961cfdSPhilippe Charnier 
2511dcc9c32SDag-Erling Smørgrav static const char hdr[] =
252f6f60773SBruce Evans "  LINE RAW CAN OUT IHIWT ILOWT OHWT LWT     COL STATE  SESS      PGID DISC\n";
253dea673e9SRodney W. Grimes int ttyspace = 128;
254dea673e9SRodney W. Grimes 
2551dcc9c32SDag-Erling Smørgrav static void
256c9624363SDag-Erling Smørgrav ttymode(void)
257dea673e9SRodney W. Grimes {
258dea673e9SRodney W. Grimes 	struct tty *tty;
2591bc52887SPoul-Henning Kamp 	struct tty ttyb[1000];
2606ed5918aSThomas Moestl 	int error;
2616ed5918aSThomas Moestl 	size_t len, i;
262dea673e9SRodney W. Grimes 
263c01c5d5cSDima Dorfman 	(void)printf("%s", hdr);
2641bc52887SPoul-Henning Kamp 	len = sizeof(ttyb);
2651bc52887SPoul-Henning Kamp 	error = sysctlbyname("kern.ttys", &ttyb, &len, 0, 0);
2661bc52887SPoul-Henning Kamp 	if (!error) {
2671bc52887SPoul-Henning Kamp 		len /= sizeof(ttyb[0]);
2681bc52887SPoul-Henning Kamp 		for (i = 0; i < len; i++) {
2691bc52887SPoul-Henning Kamp 			ttyprt(&ttyb[i], 0);
2701bc52887SPoul-Henning Kamp 		}
2711bc52887SPoul-Henning Kamp 	}
272dea673e9SRodney W. Grimes 	if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL)
273d9961cfdSPhilippe Charnier 		errx(1, "malloc");
2745a3391bfSAndrey A. Chernov 	if (nl[SCONS].n_type != 0) {
275dea673e9SRodney W. Grimes 		(void)printf("1 console\n");
276dea673e9SRodney W. Grimes 		KGET(SCONS, *tty);
277dea673e9SRodney W. Grimes 		ttyprt(&tty[0], 0);
2785a3391bfSAndrey A. Chernov 	}
2795a3391bfSAndrey A. Chernov #ifdef __FreeBSD__
280de4c8be3SSøren Schmidt 	if (nl[NSCCONS].n_type != 0)
281a9aa8c37SPeter Wemm 		ttytype(tty, "vty", SCCONS, NSCCONS, 0);
28290300419SAndrey A. Chernov 	if (nl[NSIO].n_type != 0)
283a9aa8c37SPeter Wemm 		ttytype(tty, "sio", SIO, NSIO, 0);
28490300419SAndrey A. Chernov 	if (nl[NRC].n_type != 0)
285a9aa8c37SPeter Wemm 		ttytype(tty, "rc", RC, NRC, 0);
28624f769b0SBruce Evans 	if (nl[NCY].n_type != 0)
287a9aa8c37SPeter Wemm 		ttytype(tty, "cy", CY, NCY, 0);
288a9aa8c37SPeter Wemm 	if (nl[NSI].n_type != 0)
289a9aa8c37SPeter Wemm 		ttytype(tty, "si", SI, NSI, 1);
2905a3391bfSAndrey A. Chernov #endif
291dea673e9SRodney W. Grimes 	if (nl[SNPTY].n_type != 0)
292a9aa8c37SPeter Wemm 		ttytype(tty, "pty", SPTY, SNPTY, 0);
293dea673e9SRodney W. Grimes }
294dea673e9SRodney W. Grimes 
2951dcc9c32SDag-Erling Smørgrav static void
296c9624363SDag-Erling Smørgrav ttytype(struct tty *tty, char *name, int type, int number, int indir)
297dea673e9SRodney W. Grimes {
2989c5cdfe0SPeter Wemm 	struct tty *tp;
299dea673e9SRodney W. Grimes 	int ntty;
300a9aa8c37SPeter Wemm 	struct tty **ttyaddr;
301dea673e9SRodney W. Grimes 
302dea673e9SRodney W. Grimes 	if (tty == NULL)
303dea673e9SRodney W. Grimes 		return;
304dea673e9SRodney W. Grimes 	KGET(number, ntty);
305dea673e9SRodney W. Grimes 	(void)printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines");
306dea673e9SRodney W. Grimes 	if (ntty > ttyspace) {
307dea673e9SRodney W. Grimes 		ttyspace = ntty;
308dea673e9SRodney W. Grimes 		if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == 0)
309d9961cfdSPhilippe Charnier 			errx(1, "realloc");
310dea673e9SRodney W. Grimes 	}
311a9aa8c37SPeter Wemm 	if (indir) {
312a9aa8c37SPeter Wemm 		KGET(type, ttyaddr);
313a9aa8c37SPeter Wemm 		KGET2(ttyaddr, tty, ntty * sizeof(struct tty), "tty structs");
314a9aa8c37SPeter Wemm 	} else {
315dea673e9SRodney W. Grimes 		KGET1(type, tty, ntty * sizeof(struct tty), "tty structs");
316a9aa8c37SPeter Wemm 	}
317c01c5d5cSDima Dorfman 	(void)printf("%s", hdr);
318dea673e9SRodney W. Grimes 	for (tp = tty; tp < &tty[ntty]; tp++)
319dea673e9SRodney W. Grimes 		ttyprt(tp, tp - tty);
320dea673e9SRodney W. Grimes }
321dea673e9SRodney W. Grimes 
3221dcc9c32SDag-Erling Smørgrav static struct {
323dea673e9SRodney W. Grimes 	int flag;
324dea673e9SRodney W. Grimes 	char val;
325dea673e9SRodney W. Grimes } ttystates[] = {
32624f769b0SBruce Evans #ifdef TS_WOPEN
327dea673e9SRodney W. Grimes 	{ TS_WOPEN,	'W'},
32824f769b0SBruce Evans #endif
329dea673e9SRodney W. Grimes 	{ TS_ISOPEN,	'O'},
330dea673e9SRodney W. Grimes 	{ TS_CARR_ON,	'C'},
33124f769b0SBruce Evans #ifdef TS_CONNECTED
33224f769b0SBruce Evans 	{ TS_CONNECTED,	'c'},
33324f769b0SBruce Evans #endif
334dea673e9SRodney W. Grimes 	{ TS_TIMEOUT,	'T'},
335dea673e9SRodney W. Grimes 	{ TS_FLUSH,	'F'},
336dea673e9SRodney W. Grimes 	{ TS_BUSY,	'B'},
33724f769b0SBruce Evans #ifdef TS_ASLEEP
338dea673e9SRodney W. Grimes 	{ TS_ASLEEP,	'A'},
33924f769b0SBruce Evans #endif
34024f769b0SBruce Evans #ifdef TS_SO_OLOWAT
34124f769b0SBruce Evans 	{ TS_SO_OLOWAT,	'A'},
34224f769b0SBruce Evans #endif
34324f769b0SBruce Evans #ifdef TS_SO_OCOMPLETE
34424f769b0SBruce Evans 	{ TS_SO_OCOMPLETE, 'a'},
34524f769b0SBruce Evans #endif
346dea673e9SRodney W. Grimes 	{ TS_XCLUDE,	'X'},
347dea673e9SRodney W. Grimes 	{ TS_TTSTOP,	'S'},
34824f769b0SBruce Evans #ifdef TS_CAR_OFLOW
34924f769b0SBruce Evans 	{ TS_CAR_OFLOW,	'm'},
35024f769b0SBruce Evans #endif
35124f769b0SBruce Evans #ifdef TS_CTS_OFLOW
35224f769b0SBruce Evans 	{ TS_CTS_OFLOW,	'o'},
35324f769b0SBruce Evans #endif
35424f769b0SBruce Evans #ifdef TS_DSR_OFLOW
35524f769b0SBruce Evans 	{ TS_DSR_OFLOW,	'd'},
35624f769b0SBruce Evans #endif
357dea673e9SRodney W. Grimes 	{ TS_TBLOCK,	'K'},
358dea673e9SRodney W. Grimes 	{ TS_ASYNC,	'Y'},
359dea673e9SRodney W. Grimes 	{ TS_BKSL,	'D'},
360dea673e9SRodney W. Grimes 	{ TS_ERASE,	'E'},
361dea673e9SRodney W. Grimes 	{ TS_LNCH,	'L'},
362dea673e9SRodney W. Grimes 	{ TS_TYPEN,	'P'},
363dea673e9SRodney W. Grimes 	{ TS_CNTTB,	'N'},
36424f769b0SBruce Evans #ifdef TS_CAN_BYPASS_L_RINT
365020531ceSAndrey A. Chernov 	{ TS_CAN_BYPASS_L_RINT, 'l'},
366020531ceSAndrey A. Chernov #endif
36724f769b0SBruce Evans #ifdef TS_SNOOP
36824f769b0SBruce Evans 	{ TS_SNOOP,     's'},
36924f769b0SBruce Evans #endif
37024f769b0SBruce Evans #ifdef TS_ZOMBIE
37124f769b0SBruce Evans 	{ TS_ZOMBIE,	'Z'},
37224f769b0SBruce Evans #endif
373dea673e9SRodney W. Grimes 	{ 0,	       '\0'},
374dea673e9SRodney W. Grimes };
375dea673e9SRodney W. Grimes 
3761dcc9c32SDag-Erling Smørgrav static void
377c9624363SDag-Erling Smørgrav ttyprt(struct tty *tp, int line)
378dea673e9SRodney W. Grimes {
3799c5cdfe0SPeter Wemm 	int i, j;
380dea673e9SRodney W. Grimes 	pid_t pgid;
381dea673e9SRodney W. Grimes 	char *name, state[20];
382dea673e9SRodney W. Grimes 
383dea673e9SRodney W. Grimes 	if (usenumflag || tp->t_dev == 0 ||
384dea673e9SRodney W. Grimes 	   (name = devname(tp->t_dev, S_IFCHR)) == NULL)
385a0bdb67eSDima Dorfman 		(void)printf("   %2d,%-2d", major(tp->t_dev), minor(tp->t_dev));
386dea673e9SRodney W. Grimes 	else
387dea673e9SRodney W. Grimes 		(void)printf("%7s ", name);
388dea673e9SRodney W. Grimes 	(void)printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc);
389f6f60773SBruce Evans 	(void)printf("%3d %5d %5d %4d %3d %7d ", tp->t_outq.c_cc,
3900b8a3ff7SBruce Evans 		tp->t_ihiwat, tp->t_ilowat, tp->t_ohiwat, tp->t_olowat,
3910b8a3ff7SBruce Evans 		tp->t_column);
392dea673e9SRodney W. Grimes 	for (i = j = 0; ttystates[i].flag; i++)
393dea673e9SRodney W. Grimes 		if (tp->t_state&ttystates[i].flag)
394dea673e9SRodney W. Grimes 			state[j++] = ttystates[i].val;
395dea673e9SRodney W. Grimes 	if (j == 0)
396dea673e9SRodney W. Grimes 		state[j++] = '-';
397dea673e9SRodney W. Grimes 	state[j] = '\0';
398f6f60773SBruce Evans 	(void)printf("%-6s %8lx", state, (u_long)(void *)tp->t_session);
399dea673e9SRodney W. Grimes 	pgid = 0;
400dea673e9SRodney W. Grimes 	if (tp->t_pgrp != NULL)
401dea673e9SRodney W. Grimes 		KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid");
402dea673e9SRodney W. Grimes 	(void)printf("%6d ", pgid);
403dea673e9SRodney W. Grimes 	switch (tp->t_line) {
404dea673e9SRodney W. Grimes 	case TTYDISC:
405dea673e9SRodney W. Grimes 		(void)printf("term\n");
406dea673e9SRodney W. Grimes 		break;
40790300419SAndrey A. Chernov 	case NTTYDISC:
40890300419SAndrey A. Chernov 		(void)printf("ntty\n");
40990300419SAndrey A. Chernov 		break;
410dea673e9SRodney W. Grimes 	case SLIPDISC:
411dea673e9SRodney W. Grimes 		(void)printf("slip\n");
412dea673e9SRodney W. Grimes 		break;
41390300419SAndrey A. Chernov 	case PPPDISC:
41490300419SAndrey A. Chernov 		(void)printf("ppp\n");
41590300419SAndrey A. Chernov 		break;
416dea673e9SRodney W. Grimes 	default:
417dea673e9SRodney W. Grimes 		(void)printf("%d\n", tp->t_line);
418dea673e9SRodney W. Grimes 		break;
419dea673e9SRodney W. Grimes 	}
420dea673e9SRodney W. Grimes }
421dea673e9SRodney W. Grimes 
4221dcc9c32SDag-Erling Smørgrav static void
423c9624363SDag-Erling Smørgrav filemode(void)
424dea673e9SRodney W. Grimes {
4259c5cdfe0SPeter Wemm 	struct file *fp;
426dea673e9SRodney W. Grimes 	struct file *addr;
427dea673e9SRodney W. Grimes 	char *buf, flagbuf[16], *fbp;
428dea673e9SRodney W. Grimes 	int len, maxfile, nfile;
429dea673e9SRodney W. Grimes 	static char *dtypes[] = { "???", "inode", "socket" };
430dea673e9SRodney W. Grimes 
431dea673e9SRodney W. Grimes 	KGET(FNL_MAXFILE, maxfile);
432dea673e9SRodney W. Grimes 	if (totalflag) {
433dea673e9SRodney W. Grimes 		KGET(FNL_NFILE, nfile);
434dea673e9SRodney W. Grimes 		(void)printf("%3d/%3d files\n", nfile, maxfile);
435dea673e9SRodney W. Grimes 		return;
436dea673e9SRodney W. Grimes 	}
437dea673e9SRodney W. Grimes 	if (getfiles(&buf, &len) == -1)
438dea673e9SRodney W. Grimes 		return;
439dea673e9SRodney W. Grimes 	/*
440dea673e9SRodney W. Grimes 	 * Getfiles returns in malloc'd memory a pointer to the first file
441dea673e9SRodney W. Grimes 	 * structure, and then an array of file structs (whose addresses are
442dea673e9SRodney W. Grimes 	 * derivable from the previous entry).
443dea673e9SRodney W. Grimes 	 */
44488f9a603SPoul-Henning Kamp 	addr = LIST_FIRST((struct filelist *)buf);
44598d69008SJeffrey Hsu 	fp = (struct file *)(buf + sizeof(struct filelist));
44698d69008SJeffrey Hsu 	nfile = (len - sizeof(struct filelist)) / sizeof(struct file);
447dea673e9SRodney W. Grimes 
448dea673e9SRodney W. Grimes 	(void)printf("%d/%d open files\n", nfile, maxfile);
449dea673e9SRodney W. Grimes 	(void)printf("   LOC   TYPE    FLG     CNT  MSG    DATA    OFFSET\n");
45088f9a603SPoul-Henning Kamp 	for (; (char *)fp < buf + len; addr = LIST_NEXT(fp, f_list), fp++) {
451dea673e9SRodney W. Grimes 		if ((unsigned)fp->f_type > DTYPE_SOCKET)
452dea673e9SRodney W. Grimes 			continue;
453f6f60773SBruce Evans 		(void)printf("%8lx ", (u_long)(void *)addr);
454dea673e9SRodney W. Grimes 		(void)printf("%-8.8s", dtypes[fp->f_type]);
455dea673e9SRodney W. Grimes 		fbp = flagbuf;
456dea673e9SRodney W. Grimes 		if (fp->f_flag & FREAD)
457dea673e9SRodney W. Grimes 			*fbp++ = 'R';
458dea673e9SRodney W. Grimes 		if (fp->f_flag & FWRITE)
459dea673e9SRodney W. Grimes 			*fbp++ = 'W';
460dea673e9SRodney W. Grimes 		if (fp->f_flag & FAPPEND)
461dea673e9SRodney W. Grimes 			*fbp++ = 'A';
462dea673e9SRodney W. Grimes 		if (fp->f_flag & FASYNC)
463dea673e9SRodney W. Grimes 			*fbp++ = 'I';
464dea673e9SRodney W. Grimes 		*fbp = '\0';
465dea673e9SRodney W. Grimes 		(void)printf("%6s  %3d", flagbuf, fp->f_count);
466dea673e9SRodney W. Grimes 		(void)printf("  %3d", fp->f_msgcount);
467f6f60773SBruce Evans 		(void)printf("  %8lx", (u_long)(void *)fp->f_data);
468dea673e9SRodney W. Grimes 		if (fp->f_offset < 0)
469dea673e9SRodney W. Grimes 			(void)printf("  %qx\n", fp->f_offset);
470dea673e9SRodney W. Grimes 		else
471dea673e9SRodney W. Grimes 			(void)printf("  %qd\n", fp->f_offset);
472dea673e9SRodney W. Grimes 	}
473dea673e9SRodney W. Grimes 	free(buf);
474dea673e9SRodney W. Grimes }
475dea673e9SRodney W. Grimes 
4761dcc9c32SDag-Erling Smørgrav static int
477c9624363SDag-Erling Smørgrav getfiles(char **abuf, int *alen)
478dea673e9SRodney W. Grimes {
479dea673e9SRodney W. Grimes 	size_t len;
480dea673e9SRodney W. Grimes 	int mib[2];
481dea673e9SRodney W. Grimes 	char *buf;
482dea673e9SRodney W. Grimes 
483dea673e9SRodney W. Grimes 	/*
484dea673e9SRodney W. Grimes 	 * XXX
485dea673e9SRodney W. Grimes 	 * Add emulation of KINFO_FILE here.
486dea673e9SRodney W. Grimes 	 */
487dea673e9SRodney W. Grimes 	if (memf != NULL)
488d9961cfdSPhilippe Charnier 		errx(1, "files on dead kernel, not implemented");
489dea673e9SRodney W. Grimes 
490dea673e9SRodney W. Grimes 	mib[0] = CTL_KERN;
491dea673e9SRodney W. Grimes 	mib[1] = KERN_FILE;
492dea673e9SRodney W. Grimes 	if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) {
493dea673e9SRodney W. Grimes 		warn("sysctl: KERN_FILE");
494dea673e9SRodney W. Grimes 		return (-1);
495dea673e9SRodney W. Grimes 	}
496dea673e9SRodney W. Grimes 	if ((buf = malloc(len)) == NULL)
497d9961cfdSPhilippe Charnier 		errx(1, "malloc");
498dea673e9SRodney W. Grimes 	if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) {
499dea673e9SRodney W. Grimes 		warn("sysctl: KERN_FILE");
500dea673e9SRodney W. Grimes 		return (-1);
501dea673e9SRodney W. Grimes 	}
502dea673e9SRodney W. Grimes 	*abuf = buf;
503dea673e9SRodney W. Grimes 	*alen = len;
504dea673e9SRodney W. Grimes 	return (0);
505dea673e9SRodney W. Grimes }
506dea673e9SRodney W. Grimes 
507dea673e9SRodney W. Grimes /*
508dea673e9SRodney W. Grimes  * swapmode is based on a program called swapinfo written
509dea673e9SRodney W. Grimes  * by Kevin Lahey <kml@rokkaku.atl.ga.us>.
510dea673e9SRodney W. Grimes  */
5111dcc9c32SDag-Erling Smørgrav static void
512eedc3436SMatthew Dillon swapmode(void)
513dea673e9SRodney W. Grimes {
514782f8687SMatthew Dillon 	struct kvm_swap kswap[16];
515782f8687SMatthew Dillon 	int i;
516782f8687SMatthew Dillon 	int n;
517782f8687SMatthew Dillon 	int pagesize = getpagesize();
518782f8687SMatthew Dillon 	const char *header;
519782f8687SMatthew Dillon 	int hlen;
520782f8687SMatthew Dillon 	long blocksize;
521dea673e9SRodney W. Grimes 
522782f8687SMatthew Dillon 	n = kvm_getswapinfo(
523782f8687SMatthew Dillon 	    kd,
524782f8687SMatthew Dillon 	    kswap,
525782f8687SMatthew Dillon 	    sizeof(kswap)/sizeof(kswap[0]),
526c67ba159SMatthew Dillon 	    ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX
527eedc3436SMatthew Dillon 	);
528eedc3436SMatthew Dillon 
529782f8687SMatthew Dillon #define CONVERT(v)	((int)((quad_t)(v) * pagesize / blocksize))
530dea673e9SRodney W. Grimes 
531dea673e9SRodney W. Grimes 	header = getbsize(&hlen, &blocksize);
532782f8687SMatthew Dillon 	if (totalflag == 0) {
53373f2a3c2SPhilippe Charnier 		(void)printf("%-15s %*s %8s %8s %8s  %s\n",
534dea673e9SRodney W. Grimes 		    "Device", hlen, header,
535dea673e9SRodney W. Grimes 		    "Used", "Avail", "Capacity", "Type");
536eedc3436SMatthew Dillon 
537782f8687SMatthew Dillon 		for (i = 0; i < n; ++i) {
538782f8687SMatthew Dillon 			(void)printf(
53973f2a3c2SPhilippe Charnier 			    "%-15s %*d ",
540782f8687SMatthew Dillon 			    kswap[i].ksw_devname,
541782f8687SMatthew Dillon 			    hlen,
542782f8687SMatthew Dillon 			    CONVERT(kswap[i].ksw_total)
543782f8687SMatthew Dillon 			);
544782f8687SMatthew Dillon 			(void)printf(
545782f8687SMatthew Dillon 			    "%8d %8d %5.0f%%    %s\n",
546782f8687SMatthew Dillon 			    CONVERT(kswap[i].ksw_used),
547782f8687SMatthew Dillon 			    CONVERT(kswap[i].ksw_total - kswap[i].ksw_used),
548782f8687SMatthew Dillon 			    (double)kswap[i].ksw_used * 100.0 /
549782f8687SMatthew Dillon 				(double)kswap[i].ksw_total,
550782f8687SMatthew Dillon 			    (kswap[i].ksw_flags & SW_SEQUENTIAL) ?
551782f8687SMatthew Dillon 				"Sequential" : "Interleaved"
552782f8687SMatthew Dillon 			);
553eedc3436SMatthew Dillon 		}
554eedc3436SMatthew Dillon 	}
555eedc3436SMatthew Dillon 
556dea673e9SRodney W. Grimes 	if (totalflag) {
557782f8687SMatthew Dillon 		blocksize = 1024 * 1024;
558782f8687SMatthew Dillon 
559782f8687SMatthew Dillon 		(void)printf(
560782f8687SMatthew Dillon 		    "%dM/%dM swap space\n",
561782f8687SMatthew Dillon 		    CONVERT(kswap[n].ksw_used),
562782f8687SMatthew Dillon 		    CONVERT(kswap[n].ksw_total)
563782f8687SMatthew Dillon 		);
564782f8687SMatthew Dillon 	} else if (n > 1) {
565782f8687SMatthew Dillon 		(void)printf(
56673f2a3c2SPhilippe Charnier 		    "%-15s %*d %8d %8d %5.0f%%\n",
567782f8687SMatthew Dillon 		    "Total",
568782f8687SMatthew Dillon 		    hlen,
569782f8687SMatthew Dillon 		    CONVERT(kswap[n].ksw_total),
570782f8687SMatthew Dillon 		    CONVERT(kswap[n].ksw_used),
571782f8687SMatthew Dillon 		    CONVERT(kswap[n].ksw_total - kswap[n].ksw_used),
572782f8687SMatthew Dillon 		    (double)kswap[n].ksw_used * 100.0 /
573782f8687SMatthew Dillon 			(double)kswap[n].ksw_total
574782f8687SMatthew Dillon 		);
575dea673e9SRodney W. Grimes 	}
576dea673e9SRodney W. Grimes }
577