1dea673e9SRodney W. Grimes /*- 29c5cdfe0SPeter Wemm * Copyright (c) 1980, 1991, 1993, 1994 3dea673e9SRodney W. Grimes * The Regents of the University of California. All rights reserved. 45f9ab4b0SDag-Erling Smørgrav * Copyright (c) 2002 Networks Associates Technologies, Inc. 55f9ab4b0SDag-Erling Smørgrav * All rights reserved. 65f9ab4b0SDag-Erling Smørgrav * 75f9ab4b0SDag-Erling Smørgrav * Portions of this software were developed for the FreeBSD Project by 85f9ab4b0SDag-Erling Smørgrav * ThinkSec AS and NAI Labs, the Security Research Division of Network 95f9ab4b0SDag-Erling Smørgrav * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 105f9ab4b0SDag-Erling Smørgrav * ("CBOSS"), as part of the DARPA CHATS research program. 11dea673e9SRodney W. Grimes * 12dea673e9SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 13dea673e9SRodney W. Grimes * modification, are permitted provided that the following conditions 14dea673e9SRodney W. Grimes * are met: 15dea673e9SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 16dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 17dea673e9SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 18dea673e9SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 19dea673e9SRodney W. Grimes * documentation and/or other materials provided with the distribution. 20dea673e9SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 21dea673e9SRodney W. Grimes * may be used to endorse or promote products derived from this software 22dea673e9SRodney W. Grimes * without specific prior written permission. 23dea673e9SRodney W. Grimes * 24dea673e9SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25dea673e9SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26dea673e9SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27dea673e9SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28dea673e9SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29dea673e9SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30dea673e9SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31dea673e9SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32dea673e9SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33dea673e9SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34dea673e9SRodney W. Grimes * SUCH DAMAGE. 35dea673e9SRodney W. Grimes */ 36dea673e9SRodney W. Grimes 37b728350eSDavid E. O'Brien #if 0 38dea673e9SRodney W. Grimes #ifndef lint 39d9961cfdSPhilippe Charnier static const char copyright[] = 409c5cdfe0SPeter Wemm "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 41dea673e9SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 42dea673e9SRodney W. Grimes #endif /* not lint */ 43dea673e9SRodney W. Grimes 44dea673e9SRodney W. Grimes #ifndef lint 459c5cdfe0SPeter Wemm static char sccsid[] = "@(#)pstat.c 8.16 (Berkeley) 5/9/95"; 46dea673e9SRodney W. Grimes #endif /* not lint */ 47b728350eSDavid E. O'Brien #endif 48b728350eSDavid E. O'Brien #include <sys/cdefs.h> 49b728350eSDavid E. O'Brien __FBSDID("$FreeBSD$"); 50dea673e9SRodney W. Grimes 51dea673e9SRodney W. Grimes #include <sys/param.h> 52dea673e9SRodney W. Grimes #include <sys/time.h> 53dea673e9SRodney W. Grimes #include <sys/file.h> 54dea673e9SRodney W. Grimes #include <sys/stat.h> 554418dbbdSDag-Erling Smørgrav #include <sys/stdint.h> 56dea673e9SRodney W. Grimes #include <sys/ioctl.h> 57dea673e9SRodney W. Grimes #include <sys/tty.h> 58eedc3436SMatthew Dillon #include <sys/blist.h> 59dea673e9SRodney W. Grimes 60dea673e9SRodney W. Grimes #include <sys/sysctl.h> 616004362eSDavid Schultz #include <vm/vm_param.h> 62dea673e9SRodney W. Grimes 63dea673e9SRodney W. Grimes #include <err.h> 646004362eSDavid Schultz #include <errno.h> 655d98ce75SBruce Evans #include <fcntl.h> 66dea673e9SRodney W. Grimes #include <kvm.h> 677257230fSGiorgos Keramidas #include <libutil.h> 68dea673e9SRodney W. Grimes #include <limits.h> 69dea673e9SRodney W. Grimes #include <nlist.h> 70dea673e9SRodney W. Grimes #include <stdio.h> 71dea673e9SRodney W. Grimes #include <stdlib.h> 72dea673e9SRodney W. Grimes #include <string.h> 73dea673e9SRodney W. Grimes #include <unistd.h> 74dea673e9SRodney W. Grimes 75182a90e4SDag-Erling Smørgrav enum { 76182a90e4SDag-Erling Smørgrav NL_CONSTTY, 77182a90e4SDag-Erling Smørgrav NL_MAXFILES, 78182a90e4SDag-Erling Smørgrav NL_NFILES, 7952074d38SStanislav Sedov NL_TTY_LIST, 8052074d38SStanislav Sedov NL_MARKER 81182a90e4SDag-Erling Smørgrav }; 82182a90e4SDag-Erling Smørgrav 8352074d38SStanislav Sedov static struct { 8452074d38SStanislav Sedov int order; 8552074d38SStanislav Sedov const char *name; 8652074d38SStanislav Sedov } namelist[] = { 8752074d38SStanislav Sedov { NL_CONSTTY, "_constty" }, 8852074d38SStanislav Sedov { NL_MAXFILES, "_maxfiles" }, 8952074d38SStanislav Sedov { NL_NFILES, "_openfiles" }, 9052074d38SStanislav Sedov { NL_TTY_LIST, "_tty_list" }, 9152074d38SStanislav Sedov { NL_MARKER, "" }, 92dea673e9SRodney W. Grimes }; 9352074d38SStanislav Sedov #define NNAMES (sizeof(namelist) / sizeof(*namelist)) 9452074d38SStanislav Sedov static struct nlist nl[NNAMES]; 95dea673e9SRodney W. Grimes 967257230fSGiorgos Keramidas static int humanflag; 971dcc9c32SDag-Erling Smørgrav static int usenumflag; 981dcc9c32SDag-Erling Smørgrav static int totalflag; 991dcc9c32SDag-Erling Smørgrav static int swapflag; 100d88b2458SDag-Erling Smørgrav static char *nlistf; 101d88b2458SDag-Erling Smørgrav static char *memf; 1021dcc9c32SDag-Erling Smørgrav static kvm_t *kd; 103dea673e9SRodney W. Grimes 104777e045cSEd Schouten static const char *usagestr; 10559392fe2SPoul-Henning Kamp 106c9624363SDag-Erling Smørgrav static void filemode(void); 10752074d38SStanislav Sedov static int getfiles(struct xfile **, size_t *); 108c9624363SDag-Erling Smørgrav static void swapmode(void); 109c9624363SDag-Erling Smørgrav static void ttymode(void); 110333eaaedSDag-Erling Smørgrav static void ttyprt(struct xtty *); 111c9624363SDag-Erling Smørgrav static void usage(void); 112dea673e9SRodney W. Grimes 113dea673e9SRodney W. Grimes int 114c9624363SDag-Erling Smørgrav main(int argc, char *argv[]) 115dea673e9SRodney W. Grimes { 11652074d38SStanislav Sedov int ch, quit, ret; 1170cbfd1a5SDag-Erling Smørgrav int fileflag, ttyflag; 11852074d38SStanislav Sedov unsigned int i; 119777e045cSEd Schouten char buf[_POSIX2_LINE_MAX]; 120777e045cSEd Schouten const char *opts; 121dea673e9SRodney W. Grimes 1220cbfd1a5SDag-Erling Smørgrav fileflag = swapflag = ttyflag = 0; 12359392fe2SPoul-Henning Kamp 12459392fe2SPoul-Henning Kamp /* We will behave like good old swapinfo if thus invoked */ 12559392fe2SPoul-Henning Kamp opts = strrchr(argv[0], '/'); 12659392fe2SPoul-Henning Kamp if (opts) 12759392fe2SPoul-Henning Kamp opts++; 12859392fe2SPoul-Henning Kamp else 12959392fe2SPoul-Henning Kamp opts = argv[0]; 13059392fe2SPoul-Henning Kamp if (!strcmp(opts, "swapinfo")) { 13159392fe2SPoul-Henning Kamp swapflag = 1; 1327e8409a7SMark Murray opts = "ghkmM:N:"; 1337e8409a7SMark Murray usagestr = "swapinfo [-ghkm] [-M core [-N system]]"; 13459392fe2SPoul-Henning Kamp } else { 135ae35e8adSRuslan Ermilov opts = "TM:N:fghkmnst"; 136ae35e8adSRuslan Ermilov usagestr = "pstat [-Tfghkmnst] [-M core [-N system]]"; 13759392fe2SPoul-Henning Kamp } 13859392fe2SPoul-Henning Kamp 1396c3f552aSWarner Losh while ((ch = getopt(argc, argv, opts)) != -1) 140dea673e9SRodney W. Grimes switch (ch) { 141dea673e9SRodney W. Grimes case 'f': 142dea673e9SRodney W. Grimes fileflag = 1; 143dea673e9SRodney W. Grimes break; 1447e8409a7SMark Murray case 'g': 1452966d28cSSean Farley setenv("BLOCKSIZE", "1G", 1); 1467e8409a7SMark Murray break; 1477257230fSGiorgos Keramidas case 'h': 1487257230fSGiorgos Keramidas humanflag = 1; 1497257230fSGiorgos Keramidas break; 15059392fe2SPoul-Henning Kamp case 'k': 1512966d28cSSean Farley setenv("BLOCKSIZE", "1K", 1); 15259392fe2SPoul-Henning Kamp break; 1537e8409a7SMark Murray case 'm': 1542966d28cSSean Farley setenv("BLOCKSIZE", "1M", 1); 1557e8409a7SMark Murray break; 156dea673e9SRodney W. Grimes case 'M': 157dea673e9SRodney W. Grimes memf = optarg; 158dea673e9SRodney W. Grimes break; 159dea673e9SRodney W. Grimes case 'N': 160dea673e9SRodney W. Grimes nlistf = optarg; 161dea673e9SRodney W. Grimes break; 162dea673e9SRodney W. Grimes case 'n': 163dea673e9SRodney W. Grimes usenumflag = 1; 164dea673e9SRodney W. Grimes break; 165dea673e9SRodney W. Grimes case 's': 166eedc3436SMatthew Dillon ++swapflag; 167dea673e9SRodney W. Grimes break; 168dea673e9SRodney W. Grimes case 'T': 169dea673e9SRodney W. Grimes totalflag = 1; 170dea673e9SRodney W. Grimes break; 171dea673e9SRodney W. Grimes case 't': 172dea673e9SRodney W. Grimes ttyflag = 1; 173dea673e9SRodney W. Grimes break; 174dea673e9SRodney W. Grimes default: 175d9961cfdSPhilippe Charnier usage(); 176dea673e9SRodney W. Grimes } 177dea673e9SRodney W. Grimes argc -= optind; 178dea673e9SRodney W. Grimes argv += optind; 179dea673e9SRodney W. Grimes 18052074d38SStanislav Sedov /* 18152074d38SStanislav Sedov * Initialize symbol names list. 18252074d38SStanislav Sedov */ 18352074d38SStanislav Sedov for (i = 0; i < NNAMES; i++) 18452074d38SStanislav Sedov nl[namelist[i].order].n_name = strdup(namelist[i].name); 18552074d38SStanislav Sedov 1864418dbbdSDag-Erling Smørgrav if (memf != NULL) { 1874418dbbdSDag-Erling Smørgrav kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf); 1884418dbbdSDag-Erling Smørgrav if (kd == NULL) 189dea673e9SRodney W. Grimes errx(1, "kvm_openfiles: %s", buf); 190dea673e9SRodney W. Grimes if ((ret = kvm_nlist(kd, nl)) != 0) { 191dea673e9SRodney W. Grimes if (ret == -1) 192dea673e9SRodney W. Grimes errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 1934418dbbdSDag-Erling Smørgrav quit = 0; 194182a90e4SDag-Erling Smørgrav for (i = 0; nl[i].n_name[0] != '\0'; ++i) 195182a90e4SDag-Erling Smørgrav if (nl[i].n_value == 0) { 196dea673e9SRodney W. Grimes quit = 1; 1974418dbbdSDag-Erling Smørgrav warnx("undefined symbol: %s", 1984418dbbdSDag-Erling Smørgrav nl[i].n_name); 199dea673e9SRodney W. Grimes } 200dea673e9SRodney W. Grimes if (quit) 201dea673e9SRodney W. Grimes exit(1); 202dea673e9SRodney W. Grimes } 2034418dbbdSDag-Erling Smørgrav } 2040cbfd1a5SDag-Erling Smørgrav if (!(fileflag | ttyflag | swapflag | totalflag)) 205d9961cfdSPhilippe Charnier usage(); 206dea673e9SRodney W. Grimes if (fileflag || totalflag) 207dea673e9SRodney W. Grimes filemode(); 208dea673e9SRodney W. Grimes if (ttyflag) 209dea673e9SRodney W. Grimes ttymode(); 210dea673e9SRodney W. Grimes if (swapflag || totalflag) 211dea673e9SRodney W. Grimes swapmode(); 212dea673e9SRodney W. Grimes exit (0); 213dea673e9SRodney W. Grimes } 214dea673e9SRodney W. Grimes 215d9961cfdSPhilippe Charnier static void 216c9624363SDag-Erling Smørgrav usage(void) 217d9961cfdSPhilippe Charnier { 218d9961cfdSPhilippe Charnier fprintf(stderr, "usage: %s\n", usagestr); 219d9961cfdSPhilippe Charnier exit (1); 220d9961cfdSPhilippe Charnier } 221d9961cfdSPhilippe Charnier 22246f7e0d2SRobert Drehmel static const char fhdr32[] = 22346f7e0d2SRobert Drehmel " LOC TYPE FLG CNT MSG DATA OFFSET\n"; 22446f7e0d2SRobert Drehmel /* c0000000 ------ RWAI 123 123 c0000000 1000000000000000 */ 22546f7e0d2SRobert Drehmel 22646f7e0d2SRobert Drehmel static const char fhdr64[] = 22746f7e0d2SRobert Drehmel " LOC TYPE FLG CNT MSG DATA OFFSET\n"; 22846f7e0d2SRobert Drehmel /* c000000000000000 ------ RWAI 123 123 c000000000000000 1000000000000000 */ 22946f7e0d2SRobert Drehmel 2301dcc9c32SDag-Erling Smørgrav static const char hdr[] = 231bc093719SEd Schouten " LINE INQ CAN LIN LOW OUTQ USE LOW COL SESS PGID STATE\n"; 232dea673e9SRodney W. Grimes 2331dcc9c32SDag-Erling Smørgrav static void 234182a90e4SDag-Erling Smørgrav ttymode_kvm(void) 235182a90e4SDag-Erling Smørgrav { 236203bcaf5SPoul-Henning Kamp TAILQ_HEAD(, tty) tl; 237182a90e4SDag-Erling Smørgrav struct tty *tp, tty; 238182a90e4SDag-Erling Smørgrav struct xtty xt; 239182a90e4SDag-Erling Smørgrav 240182a90e4SDag-Erling Smørgrav (void)printf("%s", hdr); 241182a90e4SDag-Erling Smørgrav bzero(&xt, sizeof xt); 242182a90e4SDag-Erling Smørgrav xt.xt_size = sizeof xt; 243182a90e4SDag-Erling Smørgrav if (kvm_read(kd, nl[NL_TTY_LIST].n_value, &tl, sizeof tl) != sizeof tl) 244182a90e4SDag-Erling Smørgrav errx(1, "kvm_read(): %s", kvm_geterr(kd)); 245203bcaf5SPoul-Henning Kamp tp = TAILQ_FIRST(&tl); 246182a90e4SDag-Erling Smørgrav while (tp != NULL) { 247182a90e4SDag-Erling Smørgrav if (kvm_read(kd, (u_long)tp, &tty, sizeof tty) != sizeof tty) 248182a90e4SDag-Erling Smørgrav errx(1, "kvm_read(): %s", kvm_geterr(kd)); 249bc093719SEd Schouten xt.xt_insize = tty.t_inq.ti_nblocks * TTYINQ_DATASIZE; 250bc093719SEd Schouten xt.xt_incc = tty.t_inq.ti_linestart - tty.t_inq.ti_begin; 251bc093719SEd Schouten xt.xt_inlc = tty.t_inq.ti_end - tty.t_inq.ti_linestart; 252bc093719SEd Schouten xt.xt_inlow = tty.t_inlow; 253bc093719SEd Schouten xt.xt_outsize = tty.t_outq.to_nblocks * TTYOUTQ_DATASIZE; 254bc093719SEd Schouten xt.xt_outcc = tty.t_outq.to_end - tty.t_outq.to_begin; 255bc093719SEd Schouten xt.xt_outlow = tty.t_outlow; 256bc093719SEd Schouten xt.xt_column = tty.t_column; 257bc093719SEd Schouten /* xt.xt_pgid = ... */ 258bc093719SEd Schouten /* xt.xt_sid = ... */ 259bc093719SEd Schouten xt.xt_flags = tty.t_flags; 260bc093719SEd Schouten xt.xt_dev = NODEV; 261182a90e4SDag-Erling Smørgrav ttyprt(&xt); 262bc42e6c4SRemko Lodder tp = TAILQ_NEXT(&tty, t_list); 263182a90e4SDag-Erling Smørgrav } 264182a90e4SDag-Erling Smørgrav } 265182a90e4SDag-Erling Smørgrav 266182a90e4SDag-Erling Smørgrav static void 267182a90e4SDag-Erling Smørgrav ttymode_sysctl(void) 268dea673e9SRodney W. Grimes { 26952074d38SStanislav Sedov struct xtty *xttys; 270333eaaedSDag-Erling Smørgrav size_t len; 27152074d38SStanislav Sedov unsigned int i, n; 272dea673e9SRodney W. Grimes 273c01c5d5cSDima Dorfman (void)printf("%s", hdr); 27452074d38SStanislav Sedov if ((xttys = malloc(len = sizeof(*xttys))) == NULL) 275333eaaedSDag-Erling Smørgrav err(1, "malloc()"); 276333eaaedSDag-Erling Smørgrav while (sysctlbyname("kern.ttys", xttys, &len, 0, 0) == -1) { 277333eaaedSDag-Erling Smørgrav if (errno != ENOMEM) 278333eaaedSDag-Erling Smørgrav err(1, "sysctlbyname()"); 279333eaaedSDag-Erling Smørgrav len *= 2; 280333eaaedSDag-Erling Smørgrav if ((xttys = realloc(xttys, len)) == NULL) 281333eaaedSDag-Erling Smørgrav err(1, "realloc()"); 2821bc52887SPoul-Henning Kamp } 28352074d38SStanislav Sedov n = len / sizeof(*xttys); 28452074d38SStanislav Sedov for (i = 0; i < n; i++) 28552074d38SStanislav Sedov ttyprt(&xttys[i]); 286dea673e9SRodney W. Grimes } 287dea673e9SRodney W. Grimes 2881dcc9c32SDag-Erling Smørgrav static void 289182a90e4SDag-Erling Smørgrav ttymode(void) 290dea673e9SRodney W. Grimes { 291dea673e9SRodney W. Grimes 292182a90e4SDag-Erling Smørgrav if (kd != NULL) 293182a90e4SDag-Erling Smørgrav ttymode_kvm(); 294182a90e4SDag-Erling Smørgrav else 295182a90e4SDag-Erling Smørgrav ttymode_sysctl(); 296dea673e9SRodney W. Grimes } 297dea673e9SRodney W. Grimes 2981dcc9c32SDag-Erling Smørgrav static struct { 299dea673e9SRodney W. Grimes int flag; 300dea673e9SRodney W. Grimes char val; 301dea673e9SRodney W. Grimes } ttystates[] = { 302bc093719SEd Schouten #if 0 303bc093719SEd Schouten { TF_NOPREFIX, 'N' }, 30424f769b0SBruce Evans #endif 305bc093719SEd Schouten { TF_INITLOCK, 'I' }, 306bc093719SEd Schouten { TF_CALLOUT, 'C' }, 307bc093719SEd Schouten 308bc093719SEd Schouten /* Keep these together -> 'Oi' and 'Oo'. */ 309bc093719SEd Schouten { TF_OPENED, 'O' }, 310bc093719SEd Schouten { TF_OPENED_IN, 'i' }, 311bc093719SEd Schouten { TF_OPENED_OUT, 'o' }, 312c3328b2aSEd Schouten { TF_OPENED_CONS, 'c' }, 313bc093719SEd Schouten 314bc093719SEd Schouten { TF_GONE, 'G' }, 315bc093719SEd Schouten { TF_OPENCLOSE, 'B' }, 316bc093719SEd Schouten { TF_ASYNC, 'Y' }, 317bc093719SEd Schouten { TF_LITERAL, 'L' }, 318bc093719SEd Schouten 319bc093719SEd Schouten /* Keep these together -> 'Hi' and 'Ho'. */ 320bc093719SEd Schouten { TF_HIWAT, 'H' }, 321bc093719SEd Schouten { TF_HIWAT_IN, 'i' }, 322bc093719SEd Schouten { TF_HIWAT_OUT, 'o' }, 323bc093719SEd Schouten 324bc093719SEd Schouten { TF_STOPPED, 'S' }, 325bc093719SEd Schouten { TF_EXCLUDE, 'X' }, 326bc093719SEd Schouten { TF_BYPASS, 'l' }, 327bc093719SEd Schouten { TF_ZOMBIE, 'Z' }, 328a1215e37SEd Schouten { TF_HOOK, 's' }, 329bc093719SEd Schouten 330c0086bf2SEd Schouten /* Keep these together -> 'bi' and 'bo'. */ 331c0086bf2SEd Schouten { TF_BUSY, 'b' }, 332c0086bf2SEd Schouten { TF_BUSY_IN, 'i' }, 333c0086bf2SEd Schouten { TF_BUSY_OUT, 'o' }, 334c0086bf2SEd Schouten 335dea673e9SRodney W. Grimes { 0, '\0'}, 336dea673e9SRodney W. Grimes }; 337dea673e9SRodney W. Grimes 3381dcc9c32SDag-Erling Smørgrav static void 339333eaaedSDag-Erling Smørgrav ttyprt(struct xtty *xt) 340dea673e9SRodney W. Grimes { 3419c5cdfe0SPeter Wemm int i, j; 342bc093719SEd Schouten char *name; 343dea673e9SRodney W. Grimes 344333eaaedSDag-Erling Smørgrav if (xt->xt_size != sizeof *xt) 345333eaaedSDag-Erling Smørgrav errx(1, "struct xtty size mismatch"); 346333eaaedSDag-Erling Smørgrav if (usenumflag || xt->xt_dev == 0 || 347333eaaedSDag-Erling Smørgrav (name = devname(xt->xt_dev, S_IFCHR)) == NULL) 348bc093719SEd Schouten printf("%5d,%4d ", major(xt->xt_dev), minor(xt->xt_dev)); 349dea673e9SRodney W. Grimes else 350bc093719SEd Schouten printf("%10s ", name); 351bc093719SEd Schouten printf("%5zu %4zu %4zu %4zu %5zu %4zu %4zu %5u %5d %5d ", 352bc093719SEd Schouten xt->xt_insize, xt->xt_incc, xt->xt_inlc, 353bc093719SEd Schouten (xt->xt_insize - xt->xt_inlow), xt->xt_outsize, 354bc093719SEd Schouten xt->xt_outcc, (xt->xt_outsize - xt->xt_outlow), 35537a9f582SEd Schouten MIN(xt->xt_column, 99999), xt->xt_sid, xt->xt_pgid); 356dea673e9SRodney W. Grimes for (i = j = 0; ttystates[i].flag; i++) 357bc093719SEd Schouten if (xt->xt_flags & ttystates[i].flag) { 358bc093719SEd Schouten putchar(ttystates[i].val); 359bc093719SEd Schouten j++; 360dea673e9SRodney W. Grimes } 361bc093719SEd Schouten if (j == 0) 362bc093719SEd Schouten putchar('-'); 363bc093719SEd Schouten putchar('\n'); 364dea673e9SRodney W. Grimes } 365dea673e9SRodney W. Grimes 3661dcc9c32SDag-Erling Smørgrav static void 367c9624363SDag-Erling Smørgrav filemode(void) 368dea673e9SRodney W. Grimes { 36952074d38SStanislav Sedov struct xfile *fp, *buf; 37052074d38SStanislav Sedov char flagbuf[16], *fbp; 3715f9ab4b0SDag-Erling Smørgrav int maxf, openf; 3724418dbbdSDag-Erling Smørgrav size_t len; 373493f0f17SEd Schouten static char const * const dtypes[] = { "???", "inode", "socket", 374777e045cSEd Schouten "pipe", "fifo", "kqueue", "crypto" }; 375eeebf53eSDag-Erling Smørgrav int i; 37646f7e0d2SRobert Drehmel int wid; 377dea673e9SRodney W. Grimes 3784418dbbdSDag-Erling Smørgrav if (kd != NULL) { 3795f9ab4b0SDag-Erling Smørgrav if (kvm_read(kd, nl[NL_MAXFILES].n_value, 3805f9ab4b0SDag-Erling Smørgrav &maxf, sizeof maxf) != sizeof maxf || 3815f9ab4b0SDag-Erling Smørgrav kvm_read(kd, nl[NL_NFILES].n_value, 3825f9ab4b0SDag-Erling Smørgrav &openf, sizeof openf) != sizeof openf) 3835f9ab4b0SDag-Erling Smørgrav errx(1, "kvm_read(): %s", kvm_geterr(kd)); 3844418dbbdSDag-Erling Smørgrav } else { 3855f9ab4b0SDag-Erling Smørgrav len = sizeof(int); 3865f9ab4b0SDag-Erling Smørgrav if (sysctlbyname("kern.maxfiles", &maxf, &len, 0, 0) == -1 || 3875f9ab4b0SDag-Erling Smørgrav sysctlbyname("kern.openfiles", &openf, &len, 0, 0) == -1) 3884418dbbdSDag-Erling Smørgrav err(1, "sysctlbyname()"); 3894418dbbdSDag-Erling Smørgrav } 3904418dbbdSDag-Erling Smørgrav 391dea673e9SRodney W. Grimes if (totalflag) { 3925f9ab4b0SDag-Erling Smørgrav (void)printf("%3d/%3d files\n", openf, maxf); 393dea673e9SRodney W. Grimes return; 394dea673e9SRodney W. Grimes } 395dea673e9SRodney W. Grimes if (getfiles(&buf, &len) == -1) 396dea673e9SRodney W. Grimes return; 397eeebf53eSDag-Erling Smørgrav openf = len / sizeof *fp; 39846f7e0d2SRobert Drehmel 3995f9ab4b0SDag-Erling Smørgrav (void)printf("%d/%d open files\n", openf, maxf); 40046f7e0d2SRobert Drehmel printf(sizeof(uintptr_t) == 4 ? fhdr32 : fhdr64); 40146f7e0d2SRobert Drehmel wid = (int)sizeof(uintptr_t) * 2; 402eeebf53eSDag-Erling Smørgrav for (fp = (struct xfile *)buf, i = 0; i < openf; ++fp, ++i) { 40346f7e0d2SRobert Drehmel if ((size_t)fp->xf_type >= sizeof(dtypes) / sizeof(dtypes[0])) 404dea673e9SRodney W. Grimes continue; 40546f7e0d2SRobert Drehmel (void)printf("%*jx", wid, (uintmax_t)(uintptr_t)fp->xf_file); 40646f7e0d2SRobert Drehmel (void)printf(" %-6.6s", dtypes[fp->xf_type]); 407dea673e9SRodney W. Grimes fbp = flagbuf; 408eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FREAD) 409dea673e9SRodney W. Grimes *fbp++ = 'R'; 410eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FWRITE) 411dea673e9SRodney W. Grimes *fbp++ = 'W'; 412eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FAPPEND) 413dea673e9SRodney W. Grimes *fbp++ = 'A'; 414eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FASYNC) 415dea673e9SRodney W. Grimes *fbp++ = 'I'; 416dea673e9SRodney W. Grimes *fbp = '\0'; 41746f7e0d2SRobert Drehmel (void)printf(" %4s %3d", flagbuf, fp->xf_count); 418eeebf53eSDag-Erling Smørgrav (void)printf(" %3d", fp->xf_msgcount); 41946f7e0d2SRobert Drehmel (void)printf(" %*jx", wid, (uintmax_t)(uintptr_t)fp->xf_data); 42046f7e0d2SRobert Drehmel (void)printf(" %*jx\n", (int)sizeof(fp->xf_offset) * 2, 42146f7e0d2SRobert Drehmel (uintmax_t)fp->xf_offset); 422dea673e9SRodney W. Grimes } 423dea673e9SRodney W. Grimes free(buf); 424dea673e9SRodney W. Grimes } 425dea673e9SRodney W. Grimes 4261dcc9c32SDag-Erling Smørgrav static int 42752074d38SStanislav Sedov getfiles(struct xfile **abuf, size_t *alen) 428dea673e9SRodney W. Grimes { 42952074d38SStanislav Sedov struct xfile *buf; 430dea673e9SRodney W. Grimes size_t len; 431dea673e9SRodney W. Grimes int mib[2]; 432dea673e9SRodney W. Grimes 433dea673e9SRodney W. Grimes /* 434dea673e9SRodney W. Grimes * XXX 435dea673e9SRodney W. Grimes * Add emulation of KINFO_FILE here. 436dea673e9SRodney W. Grimes */ 4374418dbbdSDag-Erling Smørgrav if (kd != NULL) 438d9961cfdSPhilippe Charnier errx(1, "files on dead kernel, not implemented"); 439dea673e9SRodney W. Grimes 440dea673e9SRodney W. Grimes mib[0] = CTL_KERN; 441dea673e9SRodney W. Grimes mib[1] = KERN_FILE; 442dea673e9SRodney W. Grimes if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 443dea673e9SRodney W. Grimes warn("sysctl: KERN_FILE"); 444dea673e9SRodney W. Grimes return (-1); 445dea673e9SRodney W. Grimes } 446dea673e9SRodney W. Grimes if ((buf = malloc(len)) == NULL) 447d9961cfdSPhilippe Charnier errx(1, "malloc"); 448dea673e9SRodney W. Grimes if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 449dea673e9SRodney W. Grimes warn("sysctl: KERN_FILE"); 450dea673e9SRodney W. Grimes return (-1); 451dea673e9SRodney W. Grimes } 452dea673e9SRodney W. Grimes *abuf = buf; 453dea673e9SRodney W. Grimes *alen = len; 454dea673e9SRodney W. Grimes return (0); 455dea673e9SRodney W. Grimes } 456dea673e9SRodney W. Grimes 457dea673e9SRodney W. Grimes /* 458dea673e9SRodney W. Grimes * swapmode is based on a program called swapinfo written 459dea673e9SRodney W. Grimes * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 460dea673e9SRodney W. Grimes */ 4614418dbbdSDag-Erling Smørgrav 4627257230fSGiorgos Keramidas #define CONVERT(v) ((int64_t)(v) * pagesize / blocksize) 46367c601d5SStanislav Sedov #define CONVERT_BLOCKS(v) ((int64_t)(v) * pagesize) 4644418dbbdSDag-Erling Smørgrav static struct kvm_swap swtot; 4654418dbbdSDag-Erling Smørgrav static int nswdev; 4664418dbbdSDag-Erling Smørgrav 4671dcc9c32SDag-Erling Smørgrav static void 4684418dbbdSDag-Erling Smørgrav print_swap_header(void) 469dea673e9SRodney W. Grimes { 470c48205f3SMike Barcroft int hlen; 471782f8687SMatthew Dillon long blocksize; 4724418dbbdSDag-Erling Smørgrav const char *header; 473dea673e9SRodney W. Grimes 474dea673e9SRodney W. Grimes header = getbsize(&hlen, &blocksize); 4754418dbbdSDag-Erling Smørgrav if (totalflag == 0) 47646637267SPoul-Henning Kamp (void)printf("%-15s %*s %8s %8s %8s\n", 477c48205f3SMike Barcroft "Device", hlen, header, 47846637267SPoul-Henning Kamp "Used", "Avail", "Capacity"); 4794418dbbdSDag-Erling Smørgrav } 480eedc3436SMatthew Dillon 4814418dbbdSDag-Erling Smørgrav static void 482777e045cSEd Schouten print_swap_line(const char *swdevname, intmax_t nblks, intmax_t bused, 4837257230fSGiorgos Keramidas intmax_t bavail, float bpercent) 4844418dbbdSDag-Erling Smørgrav { 4857257230fSGiorgos Keramidas char usedbuf[5]; 4867257230fSGiorgos Keramidas char availbuf[5]; 487c48205f3SMike Barcroft int hlen, pagesize; 4884418dbbdSDag-Erling Smørgrav long blocksize; 4894418dbbdSDag-Erling Smørgrav 4904418dbbdSDag-Erling Smørgrav pagesize = getpagesize(); 4914418dbbdSDag-Erling Smørgrav getbsize(&hlen, &blocksize); 4927257230fSGiorgos Keramidas 493777e045cSEd Schouten printf("%-15s %*jd ", swdevname, hlen, CONVERT(nblks)); 4947257230fSGiorgos Keramidas if (humanflag) { 4957257230fSGiorgos Keramidas humanize_number(usedbuf, sizeof(usedbuf), 49667c601d5SStanislav Sedov CONVERT_BLOCKS(bused), "", 4977257230fSGiorgos Keramidas HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 4987257230fSGiorgos Keramidas humanize_number(availbuf, sizeof(availbuf), 49967c601d5SStanislav Sedov CONVERT_BLOCKS(bavail), "", 5007257230fSGiorgos Keramidas HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 5017257230fSGiorgos Keramidas printf("%8s %8s %5.0f%%\n", usedbuf, availbuf, bpercent); 5027257230fSGiorgos Keramidas } else { 5037257230fSGiorgos Keramidas printf("%8jd %8jd %5.0f%%\n", (intmax_t)CONVERT(bused), 5047257230fSGiorgos Keramidas (intmax_t)CONVERT(bavail), bpercent); 5057257230fSGiorgos Keramidas } 5067257230fSGiorgos Keramidas } 5077257230fSGiorgos Keramidas 5087257230fSGiorgos Keramidas static void 5097257230fSGiorgos Keramidas print_swap(struct kvm_swap *ksw) 5107257230fSGiorgos Keramidas { 5117257230fSGiorgos Keramidas 5124418dbbdSDag-Erling Smørgrav swtot.ksw_total += ksw->ksw_total; 5134418dbbdSDag-Erling Smørgrav swtot.ksw_used += ksw->ksw_used; 5144418dbbdSDag-Erling Smørgrav ++nswdev; 5157257230fSGiorgos Keramidas if (totalflag == 0) 5167257230fSGiorgos Keramidas print_swap_line(ksw->ksw_devname, ksw->ksw_total, 517a2e59d80SRobert Watson ksw->ksw_used, ksw->ksw_total - ksw->ksw_used, 51846637267SPoul-Henning Kamp (ksw->ksw_used * 100.0) / ksw->ksw_total); 519eedc3436SMatthew Dillon } 520eedc3436SMatthew Dillon 5214418dbbdSDag-Erling Smørgrav static void 5224418dbbdSDag-Erling Smørgrav print_swap_total(void) 5234418dbbdSDag-Erling Smørgrav { 524c48205f3SMike Barcroft int hlen, pagesize; 5254418dbbdSDag-Erling Smørgrav long blocksize; 5264418dbbdSDag-Erling Smørgrav 5274418dbbdSDag-Erling Smørgrav pagesize = getpagesize(); 5284418dbbdSDag-Erling Smørgrav getbsize(&hlen, &blocksize); 529dea673e9SRodney W. Grimes if (totalflag) { 530782f8687SMatthew Dillon blocksize = 1024 * 1024; 5317257230fSGiorgos Keramidas (void)printf("%jdM/%jdM swap space\n", 5324418dbbdSDag-Erling Smørgrav CONVERT(swtot.ksw_used), CONVERT(swtot.ksw_total)); 5334418dbbdSDag-Erling Smørgrav } else if (nswdev > 1) { 5347257230fSGiorgos Keramidas print_swap_line("Total", swtot.ksw_total, swtot.ksw_used, 5357257230fSGiorgos Keramidas swtot.ksw_total - swtot.ksw_used, 5364418dbbdSDag-Erling Smørgrav (swtot.ksw_used * 100.0) / swtot.ksw_total); 537dea673e9SRodney W. Grimes } 538dea673e9SRodney W. Grimes } 5394418dbbdSDag-Erling Smørgrav 5404418dbbdSDag-Erling Smørgrav static void 5414418dbbdSDag-Erling Smørgrav swapmode_kvm(void) 5424418dbbdSDag-Erling Smørgrav { 5434418dbbdSDag-Erling Smørgrav struct kvm_swap kswap[16]; 5444418dbbdSDag-Erling Smørgrav int i, n; 5454418dbbdSDag-Erling Smørgrav 5464418dbbdSDag-Erling Smørgrav n = kvm_getswapinfo(kd, kswap, sizeof kswap / sizeof kswap[0], 547799aba96SPoul-Henning Kamp SWIF_DEV_PREFIX); 5484418dbbdSDag-Erling Smørgrav 5494418dbbdSDag-Erling Smørgrav print_swap_header(); 5504418dbbdSDag-Erling Smørgrav for (i = 0; i < n; ++i) 5514418dbbdSDag-Erling Smørgrav print_swap(&kswap[i]); 5524418dbbdSDag-Erling Smørgrav print_swap_total(); 5534418dbbdSDag-Erling Smørgrav } 5544418dbbdSDag-Erling Smørgrav 5554418dbbdSDag-Erling Smørgrav static void 5564418dbbdSDag-Erling Smørgrav swapmode_sysctl(void) 5574418dbbdSDag-Erling Smørgrav { 5584418dbbdSDag-Erling Smørgrav struct kvm_swap ksw; 5594418dbbdSDag-Erling Smørgrav struct xswdev xsw; 5604418dbbdSDag-Erling Smørgrav size_t mibsize, size; 5614418dbbdSDag-Erling Smørgrav int mib[16], n; 5624418dbbdSDag-Erling Smørgrav 5634418dbbdSDag-Erling Smørgrav print_swap_header(); 5644418dbbdSDag-Erling Smørgrav mibsize = sizeof mib / sizeof mib[0]; 5654418dbbdSDag-Erling Smørgrav if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1) 5664418dbbdSDag-Erling Smørgrav err(1, "sysctlnametomib()"); 5674418dbbdSDag-Erling Smørgrav for (n = 0; ; ++n) { 5684418dbbdSDag-Erling Smørgrav mib[mibsize] = n; 5694418dbbdSDag-Erling Smørgrav size = sizeof xsw; 57016fc3635SMark Murray if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1) 5714418dbbdSDag-Erling Smørgrav break; 5724418dbbdSDag-Erling Smørgrav if (xsw.xsw_version != XSWDEV_VERSION) 5734418dbbdSDag-Erling Smørgrav errx(1, "xswdev version mismatch"); 5741afc333eSPoul-Henning Kamp if (xsw.xsw_dev == NODEV) 5751afc333eSPoul-Henning Kamp snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname, 5761afc333eSPoul-Henning Kamp "<NFSfile>"); 5771afc333eSPoul-Henning Kamp else 5784418dbbdSDag-Erling Smørgrav snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname, 5794418dbbdSDag-Erling Smørgrav "/dev/%s", devname(xsw.xsw_dev, S_IFCHR)); 5804418dbbdSDag-Erling Smørgrav ksw.ksw_used = xsw.xsw_used; 5814418dbbdSDag-Erling Smørgrav ksw.ksw_total = xsw.xsw_nblks; 5824418dbbdSDag-Erling Smørgrav ksw.ksw_flags = xsw.xsw_flags; 5834418dbbdSDag-Erling Smørgrav print_swap(&ksw); 5844418dbbdSDag-Erling Smørgrav } 5854418dbbdSDag-Erling Smørgrav if (errno != ENOENT) 5864418dbbdSDag-Erling Smørgrav err(1, "sysctl()"); 5874418dbbdSDag-Erling Smørgrav print_swap_total(); 5884418dbbdSDag-Erling Smørgrav } 5894418dbbdSDag-Erling Smørgrav 5904418dbbdSDag-Erling Smørgrav static void 5914418dbbdSDag-Erling Smørgrav swapmode(void) 5924418dbbdSDag-Erling Smørgrav { 5934418dbbdSDag-Erling Smørgrav if (kd != NULL) 5944418dbbdSDag-Erling Smørgrav swapmode_kvm(); 5954418dbbdSDag-Erling Smørgrav else 5964418dbbdSDag-Erling Smørgrav swapmode_sysctl(); 5974418dbbdSDag-Erling Smørgrav } 598