1dea673e9SRodney W. Grimes /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 49c5cdfe0SPeter Wemm * Copyright (c) 1980, 1991, 1993, 1994 5dea673e9SRodney W. Grimes * The Regents of the University of California. All rights reserved. 65f9ab4b0SDag-Erling Smørgrav * Copyright (c) 2002 Networks Associates Technologies, Inc. 75f9ab4b0SDag-Erling Smørgrav * All rights reserved. 85f9ab4b0SDag-Erling Smørgrav * 95f9ab4b0SDag-Erling Smørgrav * Portions of this software were developed for the FreeBSD Project by 105f9ab4b0SDag-Erling Smørgrav * ThinkSec AS and NAI Labs, the Security Research Division of Network 115f9ab4b0SDag-Erling Smørgrav * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 125f9ab4b0SDag-Erling Smørgrav * ("CBOSS"), as part of the DARPA CHATS research program. 13dea673e9SRodney W. Grimes * 14dea673e9SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 15dea673e9SRodney W. Grimes * modification, are permitted provided that the following conditions 16dea673e9SRodney W. Grimes * are met: 17dea673e9SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 18dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 19dea673e9SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 20dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 21dea673e9SRodney W. Grimes * documentation and/or other materials provided with the distribution. 22fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 23dea673e9SRodney W. Grimes * may be used to endorse or promote products derived from this software 24dea673e9SRodney W. Grimes * without specific prior written permission. 25dea673e9SRodney W. Grimes * 26dea673e9SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27dea673e9SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28dea673e9SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29dea673e9SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30dea673e9SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31dea673e9SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32dea673e9SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33dea673e9SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34dea673e9SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35dea673e9SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36dea673e9SRodney W. Grimes * SUCH DAMAGE. 37dea673e9SRodney W. Grimes */ 38dea673e9SRodney W. Grimes 39b728350eSDavid E. O'Brien #if 0 40dea673e9SRodney W. Grimes #ifndef lint 41d9961cfdSPhilippe Charnier static const char copyright[] = 429c5cdfe0SPeter Wemm "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 43dea673e9SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 44dea673e9SRodney W. Grimes #endif /* not lint */ 45dea673e9SRodney W. Grimes 46dea673e9SRodney W. Grimes #ifndef lint 479c5cdfe0SPeter Wemm static char sccsid[] = "@(#)pstat.c 8.16 (Berkeley) 5/9/95"; 48dea673e9SRodney W. Grimes #endif /* not lint */ 49b728350eSDavid E. O'Brien #endif 50b728350eSDavid E. O'Brien #include <sys/cdefs.h> 51b728350eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 52dea673e9SRodney W. Grimes 53dea673e9SRodney W. Grimes #include <sys/param.h> 54dea673e9SRodney W. Grimes #include <sys/time.h> 55dea673e9SRodney W. Grimes #include <sys/file.h> 56dea673e9SRodney W. Grimes #include <sys/stat.h> 574418dbbdSDag-Erling Smørgrav #include <sys/stdint.h> 58dea673e9SRodney W. Grimes #include <sys/ioctl.h> 59dea673e9SRodney W. Grimes #include <sys/tty.h> 60eedc3436SMatthew Dillon #include <sys/blist.h> 61dea673e9SRodney W. Grimes 62dea673e9SRodney W. Grimes #include <sys/sysctl.h> 636004362eSDavid Schultz #include <vm/vm_param.h> 64dea673e9SRodney W. Grimes 65dea673e9SRodney W. Grimes #include <err.h> 666004362eSDavid Schultz #include <errno.h> 675d98ce75SBruce Evans #include <fcntl.h> 68dea673e9SRodney W. Grimes #include <kvm.h> 697257230fSGiorgos Keramidas #include <libutil.h> 70dea673e9SRodney W. Grimes #include <limits.h> 71dea673e9SRodney W. Grimes #include <nlist.h> 72dea673e9SRodney W. Grimes #include <stdio.h> 73dea673e9SRodney W. Grimes #include <stdlib.h> 74dea673e9SRodney W. Grimes #include <string.h> 75dea673e9SRodney W. Grimes #include <unistd.h> 76dea673e9SRodney W. Grimes 77182a90e4SDag-Erling Smørgrav enum { 78182a90e4SDag-Erling Smørgrav NL_CONSTTY, 79182a90e4SDag-Erling Smørgrav NL_MAXFILES, 80182a90e4SDag-Erling Smørgrav NL_NFILES, 8152074d38SStanislav Sedov NL_TTY_LIST, 8252074d38SStanislav Sedov NL_MARKER 83182a90e4SDag-Erling Smørgrav }; 84182a90e4SDag-Erling Smørgrav 8552074d38SStanislav Sedov static struct { 8652074d38SStanislav Sedov int order; 8752074d38SStanislav Sedov const char *name; 8852074d38SStanislav Sedov } namelist[] = { 8952074d38SStanislav Sedov { NL_CONSTTY, "_constty" }, 9052074d38SStanislav Sedov { NL_MAXFILES, "_maxfiles" }, 9152074d38SStanislav Sedov { NL_NFILES, "_openfiles" }, 9252074d38SStanislav Sedov { NL_TTY_LIST, "_tty_list" }, 9352074d38SStanislav Sedov { NL_MARKER, "" }, 94dea673e9SRodney W. Grimes }; 9552074d38SStanislav Sedov #define NNAMES (sizeof(namelist) / sizeof(*namelist)) 9652074d38SStanislav Sedov static struct nlist nl[NNAMES]; 97dea673e9SRodney W. Grimes 98*9a990500SChristian S.J. Peron #define SIZEHDR "Size" 99*9a990500SChristian S.J. Peron 1007257230fSGiorgos Keramidas static int humanflag; 1011dcc9c32SDag-Erling Smørgrav static int usenumflag; 1021dcc9c32SDag-Erling Smørgrav static int totalflag; 1031dcc9c32SDag-Erling Smørgrav static int swapflag; 104d88b2458SDag-Erling Smørgrav static char *nlistf; 105d88b2458SDag-Erling Smørgrav static char *memf; 1061dcc9c32SDag-Erling Smørgrav static kvm_t *kd; 107dea673e9SRodney W. Grimes 108777e045cSEd Schouten static const char *usagestr; 10959392fe2SPoul-Henning Kamp 110c9624363SDag-Erling Smørgrav static void filemode(void); 11152074d38SStanislav Sedov static int getfiles(struct xfile **, size_t *); 112c9624363SDag-Erling Smørgrav static void swapmode(void); 113c9624363SDag-Erling Smørgrav static void ttymode(void); 114333eaaedSDag-Erling Smørgrav static void ttyprt(struct xtty *); 115c9624363SDag-Erling Smørgrav static void usage(void); 116dea673e9SRodney W. Grimes 117dea673e9SRodney W. Grimes int 118c9624363SDag-Erling Smørgrav main(int argc, char *argv[]) 119dea673e9SRodney W. Grimes { 12052074d38SStanislav Sedov int ch, quit, ret; 1210cbfd1a5SDag-Erling Smørgrav int fileflag, ttyflag; 12252074d38SStanislav Sedov unsigned int i; 123777e045cSEd Schouten char buf[_POSIX2_LINE_MAX]; 124777e045cSEd Schouten const char *opts; 125dea673e9SRodney W. Grimes 1260cbfd1a5SDag-Erling Smørgrav fileflag = swapflag = ttyflag = 0; 12759392fe2SPoul-Henning Kamp 12859392fe2SPoul-Henning Kamp /* We will behave like good old swapinfo if thus invoked */ 12959392fe2SPoul-Henning Kamp opts = strrchr(argv[0], '/'); 13059392fe2SPoul-Henning Kamp if (opts) 13159392fe2SPoul-Henning Kamp opts++; 13259392fe2SPoul-Henning Kamp else 13359392fe2SPoul-Henning Kamp opts = argv[0]; 13459392fe2SPoul-Henning Kamp if (!strcmp(opts, "swapinfo")) { 13559392fe2SPoul-Henning Kamp swapflag = 1; 1367e8409a7SMark Murray opts = "ghkmM:N:"; 1377e8409a7SMark Murray usagestr = "swapinfo [-ghkm] [-M core [-N system]]"; 13859392fe2SPoul-Henning Kamp } else { 139ae35e8adSRuslan Ermilov opts = "TM:N:fghkmnst"; 140ae35e8adSRuslan Ermilov usagestr = "pstat [-Tfghkmnst] [-M core [-N system]]"; 14159392fe2SPoul-Henning Kamp } 14259392fe2SPoul-Henning Kamp 1436c3f552aSWarner Losh while ((ch = getopt(argc, argv, opts)) != -1) 144dea673e9SRodney W. Grimes switch (ch) { 145dea673e9SRodney W. Grimes case 'f': 146dea673e9SRodney W. Grimes fileflag = 1; 147dea673e9SRodney W. Grimes break; 1487e8409a7SMark Murray case 'g': 1492966d28cSSean Farley setenv("BLOCKSIZE", "1G", 1); 1507e8409a7SMark Murray break; 1517257230fSGiorgos Keramidas case 'h': 1527257230fSGiorgos Keramidas humanflag = 1; 1537257230fSGiorgos Keramidas break; 15459392fe2SPoul-Henning Kamp case 'k': 1552966d28cSSean Farley setenv("BLOCKSIZE", "1K", 1); 15659392fe2SPoul-Henning Kamp break; 1577e8409a7SMark Murray case 'm': 1582966d28cSSean Farley setenv("BLOCKSIZE", "1M", 1); 1597e8409a7SMark Murray break; 160dea673e9SRodney W. Grimes case 'M': 161dea673e9SRodney W. Grimes memf = optarg; 162dea673e9SRodney W. Grimes break; 163dea673e9SRodney W. Grimes case 'N': 164dea673e9SRodney W. Grimes nlistf = optarg; 165dea673e9SRodney W. Grimes break; 166dea673e9SRodney W. Grimes case 'n': 167dea673e9SRodney W. Grimes usenumflag = 1; 168dea673e9SRodney W. Grimes break; 169dea673e9SRodney W. Grimes case 's': 170eedc3436SMatthew Dillon ++swapflag; 171dea673e9SRodney W. Grimes break; 172dea673e9SRodney W. Grimes case 'T': 173dea673e9SRodney W. Grimes totalflag = 1; 174dea673e9SRodney W. Grimes break; 175dea673e9SRodney W. Grimes case 't': 176dea673e9SRodney W. Grimes ttyflag = 1; 177dea673e9SRodney W. Grimes break; 178dea673e9SRodney W. Grimes default: 179d9961cfdSPhilippe Charnier usage(); 180dea673e9SRodney W. Grimes } 181dea673e9SRodney W. Grimes 18252074d38SStanislav Sedov /* 18352074d38SStanislav Sedov * Initialize symbol names list. 18452074d38SStanislav Sedov */ 18552074d38SStanislav Sedov for (i = 0; i < NNAMES; i++) 18652074d38SStanislav Sedov nl[namelist[i].order].n_name = strdup(namelist[i].name); 18752074d38SStanislav Sedov 1884418dbbdSDag-Erling Smørgrav if (memf != NULL) { 1894418dbbdSDag-Erling Smørgrav kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf); 1904418dbbdSDag-Erling Smørgrav if (kd == NULL) 191dea673e9SRodney W. Grimes errx(1, "kvm_openfiles: %s", buf); 192dea673e9SRodney W. Grimes if ((ret = kvm_nlist(kd, nl)) != 0) { 193dea673e9SRodney W. Grimes if (ret == -1) 194dea673e9SRodney W. Grimes errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 1954418dbbdSDag-Erling Smørgrav quit = 0; 196182a90e4SDag-Erling Smørgrav for (i = 0; nl[i].n_name[0] != '\0'; ++i) 197182a90e4SDag-Erling Smørgrav if (nl[i].n_value == 0) { 198dea673e9SRodney W. Grimes quit = 1; 1994418dbbdSDag-Erling Smørgrav warnx("undefined symbol: %s", 2004418dbbdSDag-Erling Smørgrav nl[i].n_name); 201dea673e9SRodney W. Grimes } 202dea673e9SRodney W. Grimes if (quit) 203dea673e9SRodney W. Grimes exit(1); 204dea673e9SRodney W. Grimes } 2054418dbbdSDag-Erling Smørgrav } 2060cbfd1a5SDag-Erling Smørgrav if (!(fileflag | ttyflag | swapflag | totalflag)) 207d9961cfdSPhilippe Charnier usage(); 208dea673e9SRodney W. Grimes if (fileflag || totalflag) 209dea673e9SRodney W. Grimes filemode(); 210dea673e9SRodney W. Grimes if (ttyflag) 211dea673e9SRodney W. Grimes ttymode(); 212dea673e9SRodney W. Grimes if (swapflag || totalflag) 213dea673e9SRodney W. Grimes swapmode(); 214dea673e9SRodney W. Grimes exit (0); 215dea673e9SRodney W. Grimes } 216dea673e9SRodney W. Grimes 217d9961cfdSPhilippe Charnier static void 218c9624363SDag-Erling Smørgrav usage(void) 219d9961cfdSPhilippe Charnier { 220d9961cfdSPhilippe Charnier fprintf(stderr, "usage: %s\n", usagestr); 221d9961cfdSPhilippe Charnier exit (1); 222d9961cfdSPhilippe Charnier } 223d9961cfdSPhilippe Charnier 22446f7e0d2SRobert Drehmel static const char fhdr32[] = 22546f7e0d2SRobert Drehmel " LOC TYPE FLG CNT MSG DATA OFFSET\n"; 22646f7e0d2SRobert Drehmel /* c0000000 ------ RWAI 123 123 c0000000 1000000000000000 */ 22746f7e0d2SRobert Drehmel 22846f7e0d2SRobert Drehmel static const char fhdr64[] = 22946f7e0d2SRobert Drehmel " LOC TYPE FLG CNT MSG DATA OFFSET\n"; 23046f7e0d2SRobert Drehmel /* c000000000000000 ------ RWAI 123 123 c000000000000000 1000000000000000 */ 23146f7e0d2SRobert Drehmel 2321dcc9c32SDag-Erling Smørgrav static const char hdr[] = 233bc093719SEd Schouten " LINE INQ CAN LIN LOW OUTQ USE LOW COL SESS PGID STATE\n"; 234dea673e9SRodney W. Grimes 2351dcc9c32SDag-Erling Smørgrav static void 236182a90e4SDag-Erling Smørgrav ttymode_kvm(void) 237182a90e4SDag-Erling Smørgrav { 238203bcaf5SPoul-Henning Kamp TAILQ_HEAD(, tty) tl; 239182a90e4SDag-Erling Smørgrav struct tty *tp, tty; 240182a90e4SDag-Erling Smørgrav struct xtty xt; 241182a90e4SDag-Erling Smørgrav 242182a90e4SDag-Erling Smørgrav (void)printf("%s", hdr); 243182a90e4SDag-Erling Smørgrav bzero(&xt, sizeof xt); 244182a90e4SDag-Erling Smørgrav xt.xt_size = sizeof xt; 245182a90e4SDag-Erling Smørgrav if (kvm_read(kd, nl[NL_TTY_LIST].n_value, &tl, sizeof tl) != sizeof tl) 246182a90e4SDag-Erling Smørgrav errx(1, "kvm_read(): %s", kvm_geterr(kd)); 247203bcaf5SPoul-Henning Kamp tp = TAILQ_FIRST(&tl); 248182a90e4SDag-Erling Smørgrav while (tp != NULL) { 249182a90e4SDag-Erling Smørgrav if (kvm_read(kd, (u_long)tp, &tty, sizeof tty) != sizeof tty) 250182a90e4SDag-Erling Smørgrav errx(1, "kvm_read(): %s", kvm_geterr(kd)); 251bc093719SEd Schouten xt.xt_insize = tty.t_inq.ti_nblocks * TTYINQ_DATASIZE; 252bc093719SEd Schouten xt.xt_incc = tty.t_inq.ti_linestart - tty.t_inq.ti_begin; 253bc093719SEd Schouten xt.xt_inlc = tty.t_inq.ti_end - tty.t_inq.ti_linestart; 254bc093719SEd Schouten xt.xt_inlow = tty.t_inlow; 255bc093719SEd Schouten xt.xt_outsize = tty.t_outq.to_nblocks * TTYOUTQ_DATASIZE; 256bc093719SEd Schouten xt.xt_outcc = tty.t_outq.to_end - tty.t_outq.to_begin; 257bc093719SEd Schouten xt.xt_outlow = tty.t_outlow; 258bc093719SEd Schouten xt.xt_column = tty.t_column; 259bc093719SEd Schouten /* xt.xt_pgid = ... */ 260bc093719SEd Schouten /* xt.xt_sid = ... */ 261bc093719SEd Schouten xt.xt_flags = tty.t_flags; 26269921123SKonstantin Belousov xt.xt_dev = (uint32_t)NODEV; 263182a90e4SDag-Erling Smørgrav ttyprt(&xt); 264bc42e6c4SRemko Lodder tp = TAILQ_NEXT(&tty, t_list); 265182a90e4SDag-Erling Smørgrav } 266182a90e4SDag-Erling Smørgrav } 267182a90e4SDag-Erling Smørgrav 268182a90e4SDag-Erling Smørgrav static void 269182a90e4SDag-Erling Smørgrav ttymode_sysctl(void) 270dea673e9SRodney W. Grimes { 27152074d38SStanislav Sedov struct xtty *xttys; 272333eaaedSDag-Erling Smørgrav size_t len; 27352074d38SStanislav Sedov unsigned int i, n; 274dea673e9SRodney W. Grimes 275c01c5d5cSDima Dorfman (void)printf("%s", hdr); 27652074d38SStanislav Sedov if ((xttys = malloc(len = sizeof(*xttys))) == NULL) 277333eaaedSDag-Erling Smørgrav err(1, "malloc()"); 278333eaaedSDag-Erling Smørgrav while (sysctlbyname("kern.ttys", xttys, &len, 0, 0) == -1) { 279333eaaedSDag-Erling Smørgrav if (errno != ENOMEM) 280333eaaedSDag-Erling Smørgrav err(1, "sysctlbyname()"); 281333eaaedSDag-Erling Smørgrav len *= 2; 282333eaaedSDag-Erling Smørgrav if ((xttys = realloc(xttys, len)) == NULL) 283333eaaedSDag-Erling Smørgrav err(1, "realloc()"); 2841bc52887SPoul-Henning Kamp } 28552074d38SStanislav Sedov n = len / sizeof(*xttys); 28652074d38SStanislav Sedov for (i = 0; i < n; i++) 28752074d38SStanislav Sedov ttyprt(&xttys[i]); 288dea673e9SRodney W. Grimes } 289dea673e9SRodney W. Grimes 2901dcc9c32SDag-Erling Smørgrav static void 291182a90e4SDag-Erling Smørgrav ttymode(void) 292dea673e9SRodney W. Grimes { 293dea673e9SRodney W. Grimes 294182a90e4SDag-Erling Smørgrav if (kd != NULL) 295182a90e4SDag-Erling Smørgrav ttymode_kvm(); 296182a90e4SDag-Erling Smørgrav else 297182a90e4SDag-Erling Smørgrav ttymode_sysctl(); 298dea673e9SRodney W. Grimes } 299dea673e9SRodney W. Grimes 3001dcc9c32SDag-Erling Smørgrav static struct { 301dea673e9SRodney W. Grimes int flag; 302dea673e9SRodney W. Grimes char val; 303dea673e9SRodney W. Grimes } ttystates[] = { 304bc093719SEd Schouten #if 0 305bc093719SEd Schouten { TF_NOPREFIX, 'N' }, 30624f769b0SBruce Evans #endif 307bc093719SEd Schouten { TF_INITLOCK, 'I' }, 308bc093719SEd Schouten { TF_CALLOUT, 'C' }, 309bc093719SEd Schouten 310bc093719SEd Schouten /* Keep these together -> 'Oi' and 'Oo'. */ 311bc093719SEd Schouten { TF_OPENED, 'O' }, 312bc093719SEd Schouten { TF_OPENED_IN, 'i' }, 313bc093719SEd Schouten { TF_OPENED_OUT, 'o' }, 314c3328b2aSEd Schouten { TF_OPENED_CONS, 'c' }, 315bc093719SEd Schouten 316bc093719SEd Schouten { TF_GONE, 'G' }, 317bc093719SEd Schouten { TF_OPENCLOSE, 'B' }, 318bc093719SEd Schouten { TF_ASYNC, 'Y' }, 319bc093719SEd Schouten { TF_LITERAL, 'L' }, 320bc093719SEd Schouten 321bc093719SEd Schouten /* Keep these together -> 'Hi' and 'Ho'. */ 322bc093719SEd Schouten { TF_HIWAT, 'H' }, 323bc093719SEd Schouten { TF_HIWAT_IN, 'i' }, 324bc093719SEd Schouten { TF_HIWAT_OUT, 'o' }, 325bc093719SEd Schouten 326bc093719SEd Schouten { TF_STOPPED, 'S' }, 327bc093719SEd Schouten { TF_EXCLUDE, 'X' }, 328bc093719SEd Schouten { TF_BYPASS, 'l' }, 329bc093719SEd Schouten { TF_ZOMBIE, 'Z' }, 330a1215e37SEd Schouten { TF_HOOK, 's' }, 331bc093719SEd Schouten 332c0086bf2SEd Schouten /* Keep these together -> 'bi' and 'bo'. */ 333c0086bf2SEd Schouten { TF_BUSY, 'b' }, 334c0086bf2SEd Schouten { TF_BUSY_IN, 'i' }, 335c0086bf2SEd Schouten { TF_BUSY_OUT, 'o' }, 336c0086bf2SEd Schouten 337dea673e9SRodney W. Grimes { 0, '\0'}, 338dea673e9SRodney W. Grimes }; 339dea673e9SRodney W. Grimes 3401dcc9c32SDag-Erling Smørgrav static void 341333eaaedSDag-Erling Smørgrav ttyprt(struct xtty *xt) 342dea673e9SRodney W. Grimes { 3439c5cdfe0SPeter Wemm int i, j; 344ed09c2c1SXin LI const char *name; 345dea673e9SRodney W. Grimes 346333eaaedSDag-Erling Smørgrav if (xt->xt_size != sizeof *xt) 347333eaaedSDag-Erling Smørgrav errx(1, "struct xtty size mismatch"); 348333eaaedSDag-Erling Smørgrav if (usenumflag || xt->xt_dev == 0 || 349333eaaedSDag-Erling Smørgrav (name = devname(xt->xt_dev, S_IFCHR)) == NULL) 3509f365aa1SEd Schouten printf("%#10jx ", (uintmax_t)xt->xt_dev); 351dea673e9SRodney W. Grimes else 352bc093719SEd Schouten printf("%10s ", name); 353bc093719SEd Schouten printf("%5zu %4zu %4zu %4zu %5zu %4zu %4zu %5u %5d %5d ", 354bc093719SEd Schouten xt->xt_insize, xt->xt_incc, xt->xt_inlc, 355bc093719SEd Schouten (xt->xt_insize - xt->xt_inlow), xt->xt_outsize, 356bc093719SEd Schouten xt->xt_outcc, (xt->xt_outsize - xt->xt_outlow), 35737a9f582SEd Schouten MIN(xt->xt_column, 99999), xt->xt_sid, xt->xt_pgid); 358dea673e9SRodney W. Grimes for (i = j = 0; ttystates[i].flag; i++) 359bc093719SEd Schouten if (xt->xt_flags & ttystates[i].flag) { 360bc093719SEd Schouten putchar(ttystates[i].val); 361bc093719SEd Schouten j++; 362dea673e9SRodney W. Grimes } 363bc093719SEd Schouten if (j == 0) 364bc093719SEd Schouten putchar('-'); 365bc093719SEd Schouten putchar('\n'); 366dea673e9SRodney W. Grimes } 367dea673e9SRodney W. Grimes 3681dcc9c32SDag-Erling Smørgrav static void 369c9624363SDag-Erling Smørgrav filemode(void) 370dea673e9SRodney W. Grimes { 37152074d38SStanislav Sedov struct xfile *fp, *buf; 37252074d38SStanislav Sedov char flagbuf[16], *fbp; 3735f9ab4b0SDag-Erling Smørgrav int maxf, openf; 3744418dbbdSDag-Erling Smørgrav size_t len; 375493f0f17SEd Schouten static char const * const dtypes[] = { "???", "inode", "socket", 376777e045cSEd Schouten "pipe", "fifo", "kqueue", "crypto" }; 377eeebf53eSDag-Erling Smørgrav int i; 37846f7e0d2SRobert Drehmel int wid; 379dea673e9SRodney W. Grimes 3804418dbbdSDag-Erling Smørgrav if (kd != NULL) { 3815f9ab4b0SDag-Erling Smørgrav if (kvm_read(kd, nl[NL_MAXFILES].n_value, 3825f9ab4b0SDag-Erling Smørgrav &maxf, sizeof maxf) != sizeof maxf || 3835f9ab4b0SDag-Erling Smørgrav kvm_read(kd, nl[NL_NFILES].n_value, 3845f9ab4b0SDag-Erling Smørgrav &openf, sizeof openf) != sizeof openf) 3855f9ab4b0SDag-Erling Smørgrav errx(1, "kvm_read(): %s", kvm_geterr(kd)); 3864418dbbdSDag-Erling Smørgrav } else { 3875f9ab4b0SDag-Erling Smørgrav len = sizeof(int); 3885f9ab4b0SDag-Erling Smørgrav if (sysctlbyname("kern.maxfiles", &maxf, &len, 0, 0) == -1 || 3895f9ab4b0SDag-Erling Smørgrav sysctlbyname("kern.openfiles", &openf, &len, 0, 0) == -1) 3904418dbbdSDag-Erling Smørgrav err(1, "sysctlbyname()"); 3914418dbbdSDag-Erling Smørgrav } 3924418dbbdSDag-Erling Smørgrav 393dea673e9SRodney W. Grimes if (totalflag) { 3945f9ab4b0SDag-Erling Smørgrav (void)printf("%3d/%3d files\n", openf, maxf); 395dea673e9SRodney W. Grimes return; 396dea673e9SRodney W. Grimes } 397dea673e9SRodney W. Grimes if (getfiles(&buf, &len) == -1) 398dea673e9SRodney W. Grimes return; 399eeebf53eSDag-Erling Smørgrav openf = len / sizeof *fp; 40046f7e0d2SRobert Drehmel 4015f9ab4b0SDag-Erling Smørgrav (void)printf("%d/%d open files\n", openf, maxf); 40246f7e0d2SRobert Drehmel printf(sizeof(uintptr_t) == 4 ? fhdr32 : fhdr64); 40346f7e0d2SRobert Drehmel wid = (int)sizeof(uintptr_t) * 2; 404eeebf53eSDag-Erling Smørgrav for (fp = (struct xfile *)buf, i = 0; i < openf; ++fp, ++i) { 40546f7e0d2SRobert Drehmel if ((size_t)fp->xf_type >= sizeof(dtypes) / sizeof(dtypes[0])) 406dea673e9SRodney W. Grimes continue; 40746f7e0d2SRobert Drehmel (void)printf("%*jx", wid, (uintmax_t)(uintptr_t)fp->xf_file); 40846f7e0d2SRobert Drehmel (void)printf(" %-6.6s", dtypes[fp->xf_type]); 409dea673e9SRodney W. Grimes fbp = flagbuf; 410eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FREAD) 411dea673e9SRodney W. Grimes *fbp++ = 'R'; 412eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FWRITE) 413dea673e9SRodney W. Grimes *fbp++ = 'W'; 414eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FAPPEND) 415dea673e9SRodney W. Grimes *fbp++ = 'A'; 416eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FASYNC) 417dea673e9SRodney W. Grimes *fbp++ = 'I'; 418dea673e9SRodney W. Grimes *fbp = '\0'; 41946f7e0d2SRobert Drehmel (void)printf(" %4s %3d", flagbuf, fp->xf_count); 420eeebf53eSDag-Erling Smørgrav (void)printf(" %3d", fp->xf_msgcount); 42146f7e0d2SRobert Drehmel (void)printf(" %*jx", wid, (uintmax_t)(uintptr_t)fp->xf_data); 42246f7e0d2SRobert Drehmel (void)printf(" %*jx\n", (int)sizeof(fp->xf_offset) * 2, 42346f7e0d2SRobert Drehmel (uintmax_t)fp->xf_offset); 424dea673e9SRodney W. Grimes } 425dea673e9SRodney W. Grimes free(buf); 426dea673e9SRodney W. Grimes } 427dea673e9SRodney W. Grimes 4281dcc9c32SDag-Erling Smørgrav static int 42952074d38SStanislav Sedov getfiles(struct xfile **abuf, size_t *alen) 430dea673e9SRodney W. Grimes { 43152074d38SStanislav Sedov struct xfile *buf; 432dea673e9SRodney W. Grimes size_t len; 433dea673e9SRodney W. Grimes int mib[2]; 434dea673e9SRodney W. Grimes 435dea673e9SRodney W. Grimes /* 436dea673e9SRodney W. Grimes * XXX 437dea673e9SRodney W. Grimes * Add emulation of KINFO_FILE here. 438dea673e9SRodney W. Grimes */ 4394418dbbdSDag-Erling Smørgrav if (kd != NULL) 440d9961cfdSPhilippe Charnier errx(1, "files on dead kernel, not implemented"); 441dea673e9SRodney W. Grimes 442dea673e9SRodney W. Grimes mib[0] = CTL_KERN; 443dea673e9SRodney W. Grimes mib[1] = KERN_FILE; 444dea673e9SRodney W. Grimes if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 445dea673e9SRodney W. Grimes warn("sysctl: KERN_FILE"); 446dea673e9SRodney W. Grimes return (-1); 447dea673e9SRodney W. Grimes } 448dea673e9SRodney W. Grimes if ((buf = malloc(len)) == NULL) 449d9961cfdSPhilippe Charnier errx(1, "malloc"); 450dea673e9SRodney W. Grimes if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 451dea673e9SRodney W. Grimes warn("sysctl: KERN_FILE"); 452dea673e9SRodney W. Grimes return (-1); 453dea673e9SRodney W. Grimes } 454dea673e9SRodney W. Grimes *abuf = buf; 455dea673e9SRodney W. Grimes *alen = len; 456dea673e9SRodney W. Grimes return (0); 457dea673e9SRodney W. Grimes } 458dea673e9SRodney W. Grimes 459dea673e9SRodney W. Grimes /* 460dea673e9SRodney W. Grimes * swapmode is based on a program called swapinfo written 461dea673e9SRodney W. Grimes * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 462dea673e9SRodney W. Grimes */ 4634418dbbdSDag-Erling Smørgrav 4647257230fSGiorgos Keramidas #define CONVERT(v) ((int64_t)(v) * pagesize / blocksize) 46567c601d5SStanislav Sedov #define CONVERT_BLOCKS(v) ((int64_t)(v) * pagesize) 4664418dbbdSDag-Erling Smørgrav static struct kvm_swap swtot; 4674418dbbdSDag-Erling Smørgrav static int nswdev; 4684418dbbdSDag-Erling Smørgrav 4691dcc9c32SDag-Erling Smørgrav static void 4704418dbbdSDag-Erling Smørgrav print_swap_header(void) 471dea673e9SRodney W. Grimes { 472c48205f3SMike Barcroft int hlen; 473782f8687SMatthew Dillon long blocksize; 4744418dbbdSDag-Erling Smørgrav const char *header; 475dea673e9SRodney W. Grimes 476*9a990500SChristian S.J. Peron if (humanflag) { 477*9a990500SChristian S.J. Peron header = SIZEHDR; 478*9a990500SChristian S.J. Peron hlen = sizeof(SIZEHDR); 479*9a990500SChristian S.J. Peron } else { 480dea673e9SRodney W. Grimes header = getbsize(&hlen, &blocksize); 481*9a990500SChristian S.J. Peron } 4824418dbbdSDag-Erling Smørgrav if (totalflag == 0) 48346637267SPoul-Henning Kamp (void)printf("%-15s %*s %8s %8s %8s\n", 484c48205f3SMike Barcroft "Device", hlen, header, 48546637267SPoul-Henning Kamp "Used", "Avail", "Capacity"); 4864418dbbdSDag-Erling Smørgrav } 487eedc3436SMatthew Dillon 4884418dbbdSDag-Erling Smørgrav static void 489777e045cSEd Schouten print_swap_line(const char *swdevname, intmax_t nblks, intmax_t bused, 4907257230fSGiorgos Keramidas intmax_t bavail, float bpercent) 4914418dbbdSDag-Erling Smørgrav { 4927257230fSGiorgos Keramidas char usedbuf[5]; 4937257230fSGiorgos Keramidas char availbuf[5]; 494*9a990500SChristian S.J. Peron char sizebuf[5]; 495c48205f3SMike Barcroft int hlen, pagesize; 4964418dbbdSDag-Erling Smørgrav long blocksize; 4974418dbbdSDag-Erling Smørgrav 4984418dbbdSDag-Erling Smørgrav pagesize = getpagesize(); 4994418dbbdSDag-Erling Smørgrav getbsize(&hlen, &blocksize); 5007257230fSGiorgos Keramidas 501*9a990500SChristian S.J. Peron printf("%-15s ", swdevname); 5027257230fSGiorgos Keramidas if (humanflag) { 503*9a990500SChristian S.J. Peron humanize_number(sizebuf, sizeof(sizebuf), 504*9a990500SChristian S.J. Peron CONVERT_BLOCKS(nblks), "", 505*9a990500SChristian S.J. Peron HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 5067257230fSGiorgos Keramidas humanize_number(usedbuf, sizeof(usedbuf), 50767c601d5SStanislav Sedov CONVERT_BLOCKS(bused), "", 5087257230fSGiorgos Keramidas HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 5097257230fSGiorgos Keramidas humanize_number(availbuf, sizeof(availbuf), 51067c601d5SStanislav Sedov CONVERT_BLOCKS(bavail), "", 5117257230fSGiorgos Keramidas HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 512*9a990500SChristian S.J. Peron printf("%8s %8s %8s %5.0f%%\n", sizebuf, 513*9a990500SChristian S.J. Peron usedbuf, availbuf, bpercent); 5147257230fSGiorgos Keramidas } else { 515*9a990500SChristian S.J. Peron printf("%*jd %8jd %8jd %5.0f%%\n", hlen, 516*9a990500SChristian S.J. Peron (intmax_t)CONVERT(nblks), 517*9a990500SChristian S.J. Peron (intmax_t)CONVERT(bused), 5187257230fSGiorgos Keramidas (intmax_t)CONVERT(bavail), bpercent); 5197257230fSGiorgos Keramidas } 5207257230fSGiorgos Keramidas } 5217257230fSGiorgos Keramidas 5227257230fSGiorgos Keramidas static void 5237257230fSGiorgos Keramidas print_swap(struct kvm_swap *ksw) 5247257230fSGiorgos Keramidas { 5257257230fSGiorgos Keramidas 5264418dbbdSDag-Erling Smørgrav swtot.ksw_total += ksw->ksw_total; 5274418dbbdSDag-Erling Smørgrav swtot.ksw_used += ksw->ksw_used; 5284418dbbdSDag-Erling Smørgrav ++nswdev; 5297257230fSGiorgos Keramidas if (totalflag == 0) 5307257230fSGiorgos Keramidas print_swap_line(ksw->ksw_devname, ksw->ksw_total, 531a2e59d80SRobert Watson ksw->ksw_used, ksw->ksw_total - ksw->ksw_used, 53246637267SPoul-Henning Kamp (ksw->ksw_used * 100.0) / ksw->ksw_total); 533eedc3436SMatthew Dillon } 534eedc3436SMatthew Dillon 5354418dbbdSDag-Erling Smørgrav static void 5364418dbbdSDag-Erling Smørgrav print_swap_total(void) 5374418dbbdSDag-Erling Smørgrav { 538c48205f3SMike Barcroft int hlen, pagesize; 5394418dbbdSDag-Erling Smørgrav long blocksize; 5404418dbbdSDag-Erling Smørgrav 5414418dbbdSDag-Erling Smørgrav pagesize = getpagesize(); 5424418dbbdSDag-Erling Smørgrav getbsize(&hlen, &blocksize); 543dea673e9SRodney W. Grimes if (totalflag) { 544782f8687SMatthew Dillon blocksize = 1024 * 1024; 5457257230fSGiorgos Keramidas (void)printf("%jdM/%jdM swap space\n", 5464418dbbdSDag-Erling Smørgrav CONVERT(swtot.ksw_used), CONVERT(swtot.ksw_total)); 5474418dbbdSDag-Erling Smørgrav } else if (nswdev > 1) { 5487257230fSGiorgos Keramidas print_swap_line("Total", swtot.ksw_total, swtot.ksw_used, 5497257230fSGiorgos Keramidas swtot.ksw_total - swtot.ksw_used, 5504418dbbdSDag-Erling Smørgrav (swtot.ksw_used * 100.0) / swtot.ksw_total); 551dea673e9SRodney W. Grimes } 552dea673e9SRodney W. Grimes } 5534418dbbdSDag-Erling Smørgrav 5544418dbbdSDag-Erling Smørgrav static void 5554418dbbdSDag-Erling Smørgrav swapmode_kvm(void) 5564418dbbdSDag-Erling Smørgrav { 5574418dbbdSDag-Erling Smørgrav struct kvm_swap kswap[16]; 5584418dbbdSDag-Erling Smørgrav int i, n; 5594418dbbdSDag-Erling Smørgrav 5604418dbbdSDag-Erling Smørgrav n = kvm_getswapinfo(kd, kswap, sizeof kswap / sizeof kswap[0], 561799aba96SPoul-Henning Kamp SWIF_DEV_PREFIX); 5624418dbbdSDag-Erling Smørgrav 5634418dbbdSDag-Erling Smørgrav print_swap_header(); 5644418dbbdSDag-Erling Smørgrav for (i = 0; i < n; ++i) 5654418dbbdSDag-Erling Smørgrav print_swap(&kswap[i]); 5664418dbbdSDag-Erling Smørgrav print_swap_total(); 5674418dbbdSDag-Erling Smørgrav } 5684418dbbdSDag-Erling Smørgrav 5694418dbbdSDag-Erling Smørgrav static void 5704418dbbdSDag-Erling Smørgrav swapmode_sysctl(void) 5714418dbbdSDag-Erling Smørgrav { 5724418dbbdSDag-Erling Smørgrav struct kvm_swap ksw; 5734418dbbdSDag-Erling Smørgrav struct xswdev xsw; 5744418dbbdSDag-Erling Smørgrav size_t mibsize, size; 5754418dbbdSDag-Erling Smørgrav int mib[16], n; 5764418dbbdSDag-Erling Smørgrav 5774418dbbdSDag-Erling Smørgrav print_swap_header(); 5784418dbbdSDag-Erling Smørgrav mibsize = sizeof mib / sizeof mib[0]; 5794418dbbdSDag-Erling Smørgrav if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1) 5804418dbbdSDag-Erling Smørgrav err(1, "sysctlnametomib()"); 5814418dbbdSDag-Erling Smørgrav for (n = 0; ; ++n) { 5824418dbbdSDag-Erling Smørgrav mib[mibsize] = n; 5834418dbbdSDag-Erling Smørgrav size = sizeof xsw; 58416fc3635SMark Murray if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1) 5854418dbbdSDag-Erling Smørgrav break; 5864418dbbdSDag-Erling Smørgrav if (xsw.xsw_version != XSWDEV_VERSION) 5874418dbbdSDag-Erling Smørgrav errx(1, "xswdev version mismatch"); 5881afc333eSPoul-Henning Kamp if (xsw.xsw_dev == NODEV) 5891afc333eSPoul-Henning Kamp snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname, 5901afc333eSPoul-Henning Kamp "<NFSfile>"); 5911afc333eSPoul-Henning Kamp else 5924418dbbdSDag-Erling Smørgrav snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname, 5934418dbbdSDag-Erling Smørgrav "/dev/%s", devname(xsw.xsw_dev, S_IFCHR)); 5944418dbbdSDag-Erling Smørgrav ksw.ksw_used = xsw.xsw_used; 5954418dbbdSDag-Erling Smørgrav ksw.ksw_total = xsw.xsw_nblks; 5964418dbbdSDag-Erling Smørgrav ksw.ksw_flags = xsw.xsw_flags; 5974418dbbdSDag-Erling Smørgrav print_swap(&ksw); 5984418dbbdSDag-Erling Smørgrav } 5994418dbbdSDag-Erling Smørgrav if (errno != ENOENT) 6004418dbbdSDag-Erling Smørgrav err(1, "sysctl()"); 6014418dbbdSDag-Erling Smørgrav print_swap_total(); 6024418dbbdSDag-Erling Smørgrav } 6034418dbbdSDag-Erling Smørgrav 6044418dbbdSDag-Erling Smørgrav static void 6054418dbbdSDag-Erling Smørgrav swapmode(void) 6064418dbbdSDag-Erling Smørgrav { 6074418dbbdSDag-Erling Smørgrav if (kd != NULL) 6084418dbbdSDag-Erling Smørgrav swapmode_kvm(); 6094418dbbdSDag-Erling Smørgrav else 6104418dbbdSDag-Erling Smørgrav swapmode_sysctl(); 6114418dbbdSDag-Erling Smørgrav } 612