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. 20*fbbd9655SWarner Losh * 3. 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 17852074d38SStanislav Sedov /* 17952074d38SStanislav Sedov * Initialize symbol names list. 18052074d38SStanislav Sedov */ 18152074d38SStanislav Sedov for (i = 0; i < NNAMES; i++) 18252074d38SStanislav Sedov nl[namelist[i].order].n_name = strdup(namelist[i].name); 18352074d38SStanislav Sedov 1844418dbbdSDag-Erling Smørgrav if (memf != NULL) { 1854418dbbdSDag-Erling Smørgrav kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf); 1864418dbbdSDag-Erling Smørgrav if (kd == NULL) 187dea673e9SRodney W. Grimes errx(1, "kvm_openfiles: %s", buf); 188dea673e9SRodney W. Grimes if ((ret = kvm_nlist(kd, nl)) != 0) { 189dea673e9SRodney W. Grimes if (ret == -1) 190dea673e9SRodney W. Grimes errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 1914418dbbdSDag-Erling Smørgrav quit = 0; 192182a90e4SDag-Erling Smørgrav for (i = 0; nl[i].n_name[0] != '\0'; ++i) 193182a90e4SDag-Erling Smørgrav if (nl[i].n_value == 0) { 194dea673e9SRodney W. Grimes quit = 1; 1954418dbbdSDag-Erling Smørgrav warnx("undefined symbol: %s", 1964418dbbdSDag-Erling Smørgrav nl[i].n_name); 197dea673e9SRodney W. Grimes } 198dea673e9SRodney W. Grimes if (quit) 199dea673e9SRodney W. Grimes exit(1); 200dea673e9SRodney W. Grimes } 2014418dbbdSDag-Erling Smørgrav } 2020cbfd1a5SDag-Erling Smørgrav if (!(fileflag | ttyflag | swapflag | totalflag)) 203d9961cfdSPhilippe Charnier usage(); 204dea673e9SRodney W. Grimes if (fileflag || totalflag) 205dea673e9SRodney W. Grimes filemode(); 206dea673e9SRodney W. Grimes if (ttyflag) 207dea673e9SRodney W. Grimes ttymode(); 208dea673e9SRodney W. Grimes if (swapflag || totalflag) 209dea673e9SRodney W. Grimes swapmode(); 210dea673e9SRodney W. Grimes exit (0); 211dea673e9SRodney W. Grimes } 212dea673e9SRodney W. Grimes 213d9961cfdSPhilippe Charnier static void 214c9624363SDag-Erling Smørgrav usage(void) 215d9961cfdSPhilippe Charnier { 216d9961cfdSPhilippe Charnier fprintf(stderr, "usage: %s\n", usagestr); 217d9961cfdSPhilippe Charnier exit (1); 218d9961cfdSPhilippe Charnier } 219d9961cfdSPhilippe Charnier 22046f7e0d2SRobert Drehmel static const char fhdr32[] = 22146f7e0d2SRobert Drehmel " LOC TYPE FLG CNT MSG DATA OFFSET\n"; 22246f7e0d2SRobert Drehmel /* c0000000 ------ RWAI 123 123 c0000000 1000000000000000 */ 22346f7e0d2SRobert Drehmel 22446f7e0d2SRobert Drehmel static const char fhdr64[] = 22546f7e0d2SRobert Drehmel " LOC TYPE FLG CNT MSG DATA OFFSET\n"; 22646f7e0d2SRobert Drehmel /* c000000000000000 ------ RWAI 123 123 c000000000000000 1000000000000000 */ 22746f7e0d2SRobert Drehmel 2281dcc9c32SDag-Erling Smørgrav static const char hdr[] = 229bc093719SEd Schouten " LINE INQ CAN LIN LOW OUTQ USE LOW COL SESS PGID STATE\n"; 230dea673e9SRodney W. Grimes 2311dcc9c32SDag-Erling Smørgrav static void 232182a90e4SDag-Erling Smørgrav ttymode_kvm(void) 233182a90e4SDag-Erling Smørgrav { 234203bcaf5SPoul-Henning Kamp TAILQ_HEAD(, tty) tl; 235182a90e4SDag-Erling Smørgrav struct tty *tp, tty; 236182a90e4SDag-Erling Smørgrav struct xtty xt; 237182a90e4SDag-Erling Smørgrav 238182a90e4SDag-Erling Smørgrav (void)printf("%s", hdr); 239182a90e4SDag-Erling Smørgrav bzero(&xt, sizeof xt); 240182a90e4SDag-Erling Smørgrav xt.xt_size = sizeof xt; 241182a90e4SDag-Erling Smørgrav if (kvm_read(kd, nl[NL_TTY_LIST].n_value, &tl, sizeof tl) != sizeof tl) 242182a90e4SDag-Erling Smørgrav errx(1, "kvm_read(): %s", kvm_geterr(kd)); 243203bcaf5SPoul-Henning Kamp tp = TAILQ_FIRST(&tl); 244182a90e4SDag-Erling Smørgrav while (tp != NULL) { 245182a90e4SDag-Erling Smørgrav if (kvm_read(kd, (u_long)tp, &tty, sizeof tty) != sizeof tty) 246182a90e4SDag-Erling Smørgrav errx(1, "kvm_read(): %s", kvm_geterr(kd)); 247bc093719SEd Schouten xt.xt_insize = tty.t_inq.ti_nblocks * TTYINQ_DATASIZE; 248bc093719SEd Schouten xt.xt_incc = tty.t_inq.ti_linestart - tty.t_inq.ti_begin; 249bc093719SEd Schouten xt.xt_inlc = tty.t_inq.ti_end - tty.t_inq.ti_linestart; 250bc093719SEd Schouten xt.xt_inlow = tty.t_inlow; 251bc093719SEd Schouten xt.xt_outsize = tty.t_outq.to_nblocks * TTYOUTQ_DATASIZE; 252bc093719SEd Schouten xt.xt_outcc = tty.t_outq.to_end - tty.t_outq.to_begin; 253bc093719SEd Schouten xt.xt_outlow = tty.t_outlow; 254bc093719SEd Schouten xt.xt_column = tty.t_column; 255bc093719SEd Schouten /* xt.xt_pgid = ... */ 256bc093719SEd Schouten /* xt.xt_sid = ... */ 257bc093719SEd Schouten xt.xt_flags = tty.t_flags; 258bc093719SEd Schouten xt.xt_dev = NODEV; 259182a90e4SDag-Erling Smørgrav ttyprt(&xt); 260bc42e6c4SRemko Lodder tp = TAILQ_NEXT(&tty, t_list); 261182a90e4SDag-Erling Smørgrav } 262182a90e4SDag-Erling Smørgrav } 263182a90e4SDag-Erling Smørgrav 264182a90e4SDag-Erling Smørgrav static void 265182a90e4SDag-Erling Smørgrav ttymode_sysctl(void) 266dea673e9SRodney W. Grimes { 26752074d38SStanislav Sedov struct xtty *xttys; 268333eaaedSDag-Erling Smørgrav size_t len; 26952074d38SStanislav Sedov unsigned int i, n; 270dea673e9SRodney W. Grimes 271c01c5d5cSDima Dorfman (void)printf("%s", hdr); 27252074d38SStanislav Sedov if ((xttys = malloc(len = sizeof(*xttys))) == NULL) 273333eaaedSDag-Erling Smørgrav err(1, "malloc()"); 274333eaaedSDag-Erling Smørgrav while (sysctlbyname("kern.ttys", xttys, &len, 0, 0) == -1) { 275333eaaedSDag-Erling Smørgrav if (errno != ENOMEM) 276333eaaedSDag-Erling Smørgrav err(1, "sysctlbyname()"); 277333eaaedSDag-Erling Smørgrav len *= 2; 278333eaaedSDag-Erling Smørgrav if ((xttys = realloc(xttys, len)) == NULL) 279333eaaedSDag-Erling Smørgrav err(1, "realloc()"); 2801bc52887SPoul-Henning Kamp } 28152074d38SStanislav Sedov n = len / sizeof(*xttys); 28252074d38SStanislav Sedov for (i = 0; i < n; i++) 28352074d38SStanislav Sedov ttyprt(&xttys[i]); 284dea673e9SRodney W. Grimes } 285dea673e9SRodney W. Grimes 2861dcc9c32SDag-Erling Smørgrav static void 287182a90e4SDag-Erling Smørgrav ttymode(void) 288dea673e9SRodney W. Grimes { 289dea673e9SRodney W. Grimes 290182a90e4SDag-Erling Smørgrav if (kd != NULL) 291182a90e4SDag-Erling Smørgrav ttymode_kvm(); 292182a90e4SDag-Erling Smørgrav else 293182a90e4SDag-Erling Smørgrav ttymode_sysctl(); 294dea673e9SRodney W. Grimes } 295dea673e9SRodney W. Grimes 2961dcc9c32SDag-Erling Smørgrav static struct { 297dea673e9SRodney W. Grimes int flag; 298dea673e9SRodney W. Grimes char val; 299dea673e9SRodney W. Grimes } ttystates[] = { 300bc093719SEd Schouten #if 0 301bc093719SEd Schouten { TF_NOPREFIX, 'N' }, 30224f769b0SBruce Evans #endif 303bc093719SEd Schouten { TF_INITLOCK, 'I' }, 304bc093719SEd Schouten { TF_CALLOUT, 'C' }, 305bc093719SEd Schouten 306bc093719SEd Schouten /* Keep these together -> 'Oi' and 'Oo'. */ 307bc093719SEd Schouten { TF_OPENED, 'O' }, 308bc093719SEd Schouten { TF_OPENED_IN, 'i' }, 309bc093719SEd Schouten { TF_OPENED_OUT, 'o' }, 310c3328b2aSEd Schouten { TF_OPENED_CONS, 'c' }, 311bc093719SEd Schouten 312bc093719SEd Schouten { TF_GONE, 'G' }, 313bc093719SEd Schouten { TF_OPENCLOSE, 'B' }, 314bc093719SEd Schouten { TF_ASYNC, 'Y' }, 315bc093719SEd Schouten { TF_LITERAL, 'L' }, 316bc093719SEd Schouten 317bc093719SEd Schouten /* Keep these together -> 'Hi' and 'Ho'. */ 318bc093719SEd Schouten { TF_HIWAT, 'H' }, 319bc093719SEd Schouten { TF_HIWAT_IN, 'i' }, 320bc093719SEd Schouten { TF_HIWAT_OUT, 'o' }, 321bc093719SEd Schouten 322bc093719SEd Schouten { TF_STOPPED, 'S' }, 323bc093719SEd Schouten { TF_EXCLUDE, 'X' }, 324bc093719SEd Schouten { TF_BYPASS, 'l' }, 325bc093719SEd Schouten { TF_ZOMBIE, 'Z' }, 326a1215e37SEd Schouten { TF_HOOK, 's' }, 327bc093719SEd Schouten 328c0086bf2SEd Schouten /* Keep these together -> 'bi' and 'bo'. */ 329c0086bf2SEd Schouten { TF_BUSY, 'b' }, 330c0086bf2SEd Schouten { TF_BUSY_IN, 'i' }, 331c0086bf2SEd Schouten { TF_BUSY_OUT, 'o' }, 332c0086bf2SEd Schouten 333dea673e9SRodney W. Grimes { 0, '\0'}, 334dea673e9SRodney W. Grimes }; 335dea673e9SRodney W. Grimes 3361dcc9c32SDag-Erling Smørgrav static void 337333eaaedSDag-Erling Smørgrav ttyprt(struct xtty *xt) 338dea673e9SRodney W. Grimes { 3399c5cdfe0SPeter Wemm int i, j; 340ed09c2c1SXin LI const char *name; 341dea673e9SRodney W. Grimes 342333eaaedSDag-Erling Smørgrav if (xt->xt_size != sizeof *xt) 343333eaaedSDag-Erling Smørgrav errx(1, "struct xtty size mismatch"); 344333eaaedSDag-Erling Smørgrav if (usenumflag || xt->xt_dev == 0 || 345333eaaedSDag-Erling Smørgrav (name = devname(xt->xt_dev, S_IFCHR)) == NULL) 3469f365aa1SEd Schouten printf("%#10jx ", (uintmax_t)xt->xt_dev); 347dea673e9SRodney W. Grimes else 348bc093719SEd Schouten printf("%10s ", name); 349bc093719SEd Schouten printf("%5zu %4zu %4zu %4zu %5zu %4zu %4zu %5u %5d %5d ", 350bc093719SEd Schouten xt->xt_insize, xt->xt_incc, xt->xt_inlc, 351bc093719SEd Schouten (xt->xt_insize - xt->xt_inlow), xt->xt_outsize, 352bc093719SEd Schouten xt->xt_outcc, (xt->xt_outsize - xt->xt_outlow), 35337a9f582SEd Schouten MIN(xt->xt_column, 99999), xt->xt_sid, xt->xt_pgid); 354dea673e9SRodney W. Grimes for (i = j = 0; ttystates[i].flag; i++) 355bc093719SEd Schouten if (xt->xt_flags & ttystates[i].flag) { 356bc093719SEd Schouten putchar(ttystates[i].val); 357bc093719SEd Schouten j++; 358dea673e9SRodney W. Grimes } 359bc093719SEd Schouten if (j == 0) 360bc093719SEd Schouten putchar('-'); 361bc093719SEd Schouten putchar('\n'); 362dea673e9SRodney W. Grimes } 363dea673e9SRodney W. Grimes 3641dcc9c32SDag-Erling Smørgrav static void 365c9624363SDag-Erling Smørgrav filemode(void) 366dea673e9SRodney W. Grimes { 36752074d38SStanislav Sedov struct xfile *fp, *buf; 36852074d38SStanislav Sedov char flagbuf[16], *fbp; 3695f9ab4b0SDag-Erling Smørgrav int maxf, openf; 3704418dbbdSDag-Erling Smørgrav size_t len; 371493f0f17SEd Schouten static char const * const dtypes[] = { "???", "inode", "socket", 372777e045cSEd Schouten "pipe", "fifo", "kqueue", "crypto" }; 373eeebf53eSDag-Erling Smørgrav int i; 37446f7e0d2SRobert Drehmel int wid; 375dea673e9SRodney W. Grimes 3764418dbbdSDag-Erling Smørgrav if (kd != NULL) { 3775f9ab4b0SDag-Erling Smørgrav if (kvm_read(kd, nl[NL_MAXFILES].n_value, 3785f9ab4b0SDag-Erling Smørgrav &maxf, sizeof maxf) != sizeof maxf || 3795f9ab4b0SDag-Erling Smørgrav kvm_read(kd, nl[NL_NFILES].n_value, 3805f9ab4b0SDag-Erling Smørgrav &openf, sizeof openf) != sizeof openf) 3815f9ab4b0SDag-Erling Smørgrav errx(1, "kvm_read(): %s", kvm_geterr(kd)); 3824418dbbdSDag-Erling Smørgrav } else { 3835f9ab4b0SDag-Erling Smørgrav len = sizeof(int); 3845f9ab4b0SDag-Erling Smørgrav if (sysctlbyname("kern.maxfiles", &maxf, &len, 0, 0) == -1 || 3855f9ab4b0SDag-Erling Smørgrav sysctlbyname("kern.openfiles", &openf, &len, 0, 0) == -1) 3864418dbbdSDag-Erling Smørgrav err(1, "sysctlbyname()"); 3874418dbbdSDag-Erling Smørgrav } 3884418dbbdSDag-Erling Smørgrav 389dea673e9SRodney W. Grimes if (totalflag) { 3905f9ab4b0SDag-Erling Smørgrav (void)printf("%3d/%3d files\n", openf, maxf); 391dea673e9SRodney W. Grimes return; 392dea673e9SRodney W. Grimes } 393dea673e9SRodney W. Grimes if (getfiles(&buf, &len) == -1) 394dea673e9SRodney W. Grimes return; 395eeebf53eSDag-Erling Smørgrav openf = len / sizeof *fp; 39646f7e0d2SRobert Drehmel 3975f9ab4b0SDag-Erling Smørgrav (void)printf("%d/%d open files\n", openf, maxf); 39846f7e0d2SRobert Drehmel printf(sizeof(uintptr_t) == 4 ? fhdr32 : fhdr64); 39946f7e0d2SRobert Drehmel wid = (int)sizeof(uintptr_t) * 2; 400eeebf53eSDag-Erling Smørgrav for (fp = (struct xfile *)buf, i = 0; i < openf; ++fp, ++i) { 40146f7e0d2SRobert Drehmel if ((size_t)fp->xf_type >= sizeof(dtypes) / sizeof(dtypes[0])) 402dea673e9SRodney W. Grimes continue; 40346f7e0d2SRobert Drehmel (void)printf("%*jx", wid, (uintmax_t)(uintptr_t)fp->xf_file); 40446f7e0d2SRobert Drehmel (void)printf(" %-6.6s", dtypes[fp->xf_type]); 405dea673e9SRodney W. Grimes fbp = flagbuf; 406eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FREAD) 407dea673e9SRodney W. Grimes *fbp++ = 'R'; 408eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FWRITE) 409dea673e9SRodney W. Grimes *fbp++ = 'W'; 410eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FAPPEND) 411dea673e9SRodney W. Grimes *fbp++ = 'A'; 412eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FASYNC) 413dea673e9SRodney W. Grimes *fbp++ = 'I'; 414dea673e9SRodney W. Grimes *fbp = '\0'; 41546f7e0d2SRobert Drehmel (void)printf(" %4s %3d", flagbuf, fp->xf_count); 416eeebf53eSDag-Erling Smørgrav (void)printf(" %3d", fp->xf_msgcount); 41746f7e0d2SRobert Drehmel (void)printf(" %*jx", wid, (uintmax_t)(uintptr_t)fp->xf_data); 41846f7e0d2SRobert Drehmel (void)printf(" %*jx\n", (int)sizeof(fp->xf_offset) * 2, 41946f7e0d2SRobert Drehmel (uintmax_t)fp->xf_offset); 420dea673e9SRodney W. Grimes } 421dea673e9SRodney W. Grimes free(buf); 422dea673e9SRodney W. Grimes } 423dea673e9SRodney W. Grimes 4241dcc9c32SDag-Erling Smørgrav static int 42552074d38SStanislav Sedov getfiles(struct xfile **abuf, size_t *alen) 426dea673e9SRodney W. Grimes { 42752074d38SStanislav Sedov struct xfile *buf; 428dea673e9SRodney W. Grimes size_t len; 429dea673e9SRodney W. Grimes int mib[2]; 430dea673e9SRodney W. Grimes 431dea673e9SRodney W. Grimes /* 432dea673e9SRodney W. Grimes * XXX 433dea673e9SRodney W. Grimes * Add emulation of KINFO_FILE here. 434dea673e9SRodney W. Grimes */ 4354418dbbdSDag-Erling Smørgrav if (kd != NULL) 436d9961cfdSPhilippe Charnier errx(1, "files on dead kernel, not implemented"); 437dea673e9SRodney W. Grimes 438dea673e9SRodney W. Grimes mib[0] = CTL_KERN; 439dea673e9SRodney W. Grimes mib[1] = KERN_FILE; 440dea673e9SRodney W. Grimes if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 441dea673e9SRodney W. Grimes warn("sysctl: KERN_FILE"); 442dea673e9SRodney W. Grimes return (-1); 443dea673e9SRodney W. Grimes } 444dea673e9SRodney W. Grimes if ((buf = malloc(len)) == NULL) 445d9961cfdSPhilippe Charnier errx(1, "malloc"); 446dea673e9SRodney W. Grimes if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 447dea673e9SRodney W. Grimes warn("sysctl: KERN_FILE"); 448dea673e9SRodney W. Grimes return (-1); 449dea673e9SRodney W. Grimes } 450dea673e9SRodney W. Grimes *abuf = buf; 451dea673e9SRodney W. Grimes *alen = len; 452dea673e9SRodney W. Grimes return (0); 453dea673e9SRodney W. Grimes } 454dea673e9SRodney W. Grimes 455dea673e9SRodney W. Grimes /* 456dea673e9SRodney W. Grimes * swapmode is based on a program called swapinfo written 457dea673e9SRodney W. Grimes * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 458dea673e9SRodney W. Grimes */ 4594418dbbdSDag-Erling Smørgrav 4607257230fSGiorgos Keramidas #define CONVERT(v) ((int64_t)(v) * pagesize / blocksize) 46167c601d5SStanislav Sedov #define CONVERT_BLOCKS(v) ((int64_t)(v) * pagesize) 4624418dbbdSDag-Erling Smørgrav static struct kvm_swap swtot; 4634418dbbdSDag-Erling Smørgrav static int nswdev; 4644418dbbdSDag-Erling Smørgrav 4651dcc9c32SDag-Erling Smørgrav static void 4664418dbbdSDag-Erling Smørgrav print_swap_header(void) 467dea673e9SRodney W. Grimes { 468c48205f3SMike Barcroft int hlen; 469782f8687SMatthew Dillon long blocksize; 4704418dbbdSDag-Erling Smørgrav const char *header; 471dea673e9SRodney W. Grimes 472dea673e9SRodney W. Grimes header = getbsize(&hlen, &blocksize); 4734418dbbdSDag-Erling Smørgrav if (totalflag == 0) 47446637267SPoul-Henning Kamp (void)printf("%-15s %*s %8s %8s %8s\n", 475c48205f3SMike Barcroft "Device", hlen, header, 47646637267SPoul-Henning Kamp "Used", "Avail", "Capacity"); 4774418dbbdSDag-Erling Smørgrav } 478eedc3436SMatthew Dillon 4794418dbbdSDag-Erling Smørgrav static void 480777e045cSEd Schouten print_swap_line(const char *swdevname, intmax_t nblks, intmax_t bused, 4817257230fSGiorgos Keramidas intmax_t bavail, float bpercent) 4824418dbbdSDag-Erling Smørgrav { 4837257230fSGiorgos Keramidas char usedbuf[5]; 4847257230fSGiorgos Keramidas char availbuf[5]; 485c48205f3SMike Barcroft int hlen, pagesize; 4864418dbbdSDag-Erling Smørgrav long blocksize; 4874418dbbdSDag-Erling Smørgrav 4884418dbbdSDag-Erling Smørgrav pagesize = getpagesize(); 4894418dbbdSDag-Erling Smørgrav getbsize(&hlen, &blocksize); 4907257230fSGiorgos Keramidas 491777e045cSEd Schouten printf("%-15s %*jd ", swdevname, hlen, CONVERT(nblks)); 4927257230fSGiorgos Keramidas if (humanflag) { 4937257230fSGiorgos Keramidas humanize_number(usedbuf, sizeof(usedbuf), 49467c601d5SStanislav Sedov CONVERT_BLOCKS(bused), "", 4957257230fSGiorgos Keramidas HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 4967257230fSGiorgos Keramidas humanize_number(availbuf, sizeof(availbuf), 49767c601d5SStanislav Sedov CONVERT_BLOCKS(bavail), "", 4987257230fSGiorgos Keramidas HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 4997257230fSGiorgos Keramidas printf("%8s %8s %5.0f%%\n", usedbuf, availbuf, bpercent); 5007257230fSGiorgos Keramidas } else { 5017257230fSGiorgos Keramidas printf("%8jd %8jd %5.0f%%\n", (intmax_t)CONVERT(bused), 5027257230fSGiorgos Keramidas (intmax_t)CONVERT(bavail), bpercent); 5037257230fSGiorgos Keramidas } 5047257230fSGiorgos Keramidas } 5057257230fSGiorgos Keramidas 5067257230fSGiorgos Keramidas static void 5077257230fSGiorgos Keramidas print_swap(struct kvm_swap *ksw) 5087257230fSGiorgos Keramidas { 5097257230fSGiorgos Keramidas 5104418dbbdSDag-Erling Smørgrav swtot.ksw_total += ksw->ksw_total; 5114418dbbdSDag-Erling Smørgrav swtot.ksw_used += ksw->ksw_used; 5124418dbbdSDag-Erling Smørgrav ++nswdev; 5137257230fSGiorgos Keramidas if (totalflag == 0) 5147257230fSGiorgos Keramidas print_swap_line(ksw->ksw_devname, ksw->ksw_total, 515a2e59d80SRobert Watson ksw->ksw_used, ksw->ksw_total - ksw->ksw_used, 51646637267SPoul-Henning Kamp (ksw->ksw_used * 100.0) / ksw->ksw_total); 517eedc3436SMatthew Dillon } 518eedc3436SMatthew Dillon 5194418dbbdSDag-Erling Smørgrav static void 5204418dbbdSDag-Erling Smørgrav print_swap_total(void) 5214418dbbdSDag-Erling Smørgrav { 522c48205f3SMike Barcroft int hlen, pagesize; 5234418dbbdSDag-Erling Smørgrav long blocksize; 5244418dbbdSDag-Erling Smørgrav 5254418dbbdSDag-Erling Smørgrav pagesize = getpagesize(); 5264418dbbdSDag-Erling Smørgrav getbsize(&hlen, &blocksize); 527dea673e9SRodney W. Grimes if (totalflag) { 528782f8687SMatthew Dillon blocksize = 1024 * 1024; 5297257230fSGiorgos Keramidas (void)printf("%jdM/%jdM swap space\n", 5304418dbbdSDag-Erling Smørgrav CONVERT(swtot.ksw_used), CONVERT(swtot.ksw_total)); 5314418dbbdSDag-Erling Smørgrav } else if (nswdev > 1) { 5327257230fSGiorgos Keramidas print_swap_line("Total", swtot.ksw_total, swtot.ksw_used, 5337257230fSGiorgos Keramidas swtot.ksw_total - swtot.ksw_used, 5344418dbbdSDag-Erling Smørgrav (swtot.ksw_used * 100.0) / swtot.ksw_total); 535dea673e9SRodney W. Grimes } 536dea673e9SRodney W. Grimes } 5374418dbbdSDag-Erling Smørgrav 5384418dbbdSDag-Erling Smørgrav static void 5394418dbbdSDag-Erling Smørgrav swapmode_kvm(void) 5404418dbbdSDag-Erling Smørgrav { 5414418dbbdSDag-Erling Smørgrav struct kvm_swap kswap[16]; 5424418dbbdSDag-Erling Smørgrav int i, n; 5434418dbbdSDag-Erling Smørgrav 5444418dbbdSDag-Erling Smørgrav n = kvm_getswapinfo(kd, kswap, sizeof kswap / sizeof kswap[0], 545799aba96SPoul-Henning Kamp SWIF_DEV_PREFIX); 5464418dbbdSDag-Erling Smørgrav 5474418dbbdSDag-Erling Smørgrav print_swap_header(); 5484418dbbdSDag-Erling Smørgrav for (i = 0; i < n; ++i) 5494418dbbdSDag-Erling Smørgrav print_swap(&kswap[i]); 5504418dbbdSDag-Erling Smørgrav print_swap_total(); 5514418dbbdSDag-Erling Smørgrav } 5524418dbbdSDag-Erling Smørgrav 5534418dbbdSDag-Erling Smørgrav static void 5544418dbbdSDag-Erling Smørgrav swapmode_sysctl(void) 5554418dbbdSDag-Erling Smørgrav { 5564418dbbdSDag-Erling Smørgrav struct kvm_swap ksw; 5574418dbbdSDag-Erling Smørgrav struct xswdev xsw; 5584418dbbdSDag-Erling Smørgrav size_t mibsize, size; 5594418dbbdSDag-Erling Smørgrav int mib[16], n; 5604418dbbdSDag-Erling Smørgrav 5614418dbbdSDag-Erling Smørgrav print_swap_header(); 5624418dbbdSDag-Erling Smørgrav mibsize = sizeof mib / sizeof mib[0]; 5634418dbbdSDag-Erling Smørgrav if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1) 5644418dbbdSDag-Erling Smørgrav err(1, "sysctlnametomib()"); 5654418dbbdSDag-Erling Smørgrav for (n = 0; ; ++n) { 5664418dbbdSDag-Erling Smørgrav mib[mibsize] = n; 5674418dbbdSDag-Erling Smørgrav size = sizeof xsw; 56816fc3635SMark Murray if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1) 5694418dbbdSDag-Erling Smørgrav break; 5704418dbbdSDag-Erling Smørgrav if (xsw.xsw_version != XSWDEV_VERSION) 5714418dbbdSDag-Erling Smørgrav errx(1, "xswdev version mismatch"); 5721afc333eSPoul-Henning Kamp if (xsw.xsw_dev == NODEV) 5731afc333eSPoul-Henning Kamp snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname, 5741afc333eSPoul-Henning Kamp "<NFSfile>"); 5751afc333eSPoul-Henning Kamp else 5764418dbbdSDag-Erling Smørgrav snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname, 5774418dbbdSDag-Erling Smørgrav "/dev/%s", devname(xsw.xsw_dev, S_IFCHR)); 5784418dbbdSDag-Erling Smørgrav ksw.ksw_used = xsw.xsw_used; 5794418dbbdSDag-Erling Smørgrav ksw.ksw_total = xsw.xsw_nblks; 5804418dbbdSDag-Erling Smørgrav ksw.ksw_flags = xsw.xsw_flags; 5814418dbbdSDag-Erling Smørgrav print_swap(&ksw); 5824418dbbdSDag-Erling Smørgrav } 5834418dbbdSDag-Erling Smørgrav if (errno != ENOENT) 5844418dbbdSDag-Erling Smørgrav err(1, "sysctl()"); 5854418dbbdSDag-Erling Smørgrav print_swap_total(); 5864418dbbdSDag-Erling Smørgrav } 5874418dbbdSDag-Erling Smørgrav 5884418dbbdSDag-Erling Smørgrav static void 5894418dbbdSDag-Erling Smørgrav swapmode(void) 5904418dbbdSDag-Erling Smørgrav { 5914418dbbdSDag-Erling Smørgrav if (kd != NULL) 5924418dbbdSDag-Erling Smørgrav swapmode_kvm(); 5934418dbbdSDag-Erling Smørgrav else 5944418dbbdSDag-Erling Smørgrav swapmode_sysctl(); 5954418dbbdSDag-Erling Smørgrav } 596