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, 79182a90e4SDag-Erling Smørgrav NL_TTY_LIST 80182a90e4SDag-Erling Smørgrav }; 81182a90e4SDag-Erling Smørgrav 821dcc9c32SDag-Erling Smørgrav static struct nlist nl[] = { 8300191f86SGiorgos Keramidas { .n_name = "_constty" }, 8400191f86SGiorgos Keramidas { .n_name = "_maxfiles" }, 85897d31beSJohn Baldwin { .n_name = "_openfiles" }, 8600191f86SGiorgos Keramidas { .n_name = "_tty_list" }, 8700191f86SGiorgos Keramidas { .n_name = "" } 88dea673e9SRodney W. Grimes }; 89dea673e9SRodney W. Grimes 907257230fSGiorgos Keramidas static int humanflag; 911dcc9c32SDag-Erling Smørgrav static int usenumflag; 921dcc9c32SDag-Erling Smørgrav static int totalflag; 931dcc9c32SDag-Erling Smørgrav static int swapflag; 94d88b2458SDag-Erling Smørgrav static char *nlistf; 95d88b2458SDag-Erling Smørgrav static char *memf; 961dcc9c32SDag-Erling Smørgrav static kvm_t *kd; 97dea673e9SRodney W. Grimes 981dcc9c32SDag-Erling Smørgrav static char *usagestr; 9959392fe2SPoul-Henning Kamp 100c9624363SDag-Erling Smørgrav static void filemode(void); 101333eaaedSDag-Erling Smørgrav static int getfiles(char **, size_t *); 102c9624363SDag-Erling Smørgrav static void swapmode(void); 103c9624363SDag-Erling Smørgrav static void ttymode(void); 104333eaaedSDag-Erling Smørgrav static void ttyprt(struct xtty *); 105c9624363SDag-Erling Smørgrav static void usage(void); 106dea673e9SRodney W. Grimes 107dea673e9SRodney W. Grimes int 108c9624363SDag-Erling Smørgrav main(int argc, char *argv[]) 109dea673e9SRodney W. Grimes { 110dea673e9SRodney W. Grimes int ch, i, quit, ret; 1110cbfd1a5SDag-Erling Smørgrav int fileflag, ttyflag; 11259392fe2SPoul-Henning Kamp char buf[_POSIX2_LINE_MAX],*opts; 113dea673e9SRodney W. Grimes 1140cbfd1a5SDag-Erling Smørgrav fileflag = swapflag = ttyflag = 0; 11559392fe2SPoul-Henning Kamp 11659392fe2SPoul-Henning Kamp /* We will behave like good old swapinfo if thus invoked */ 11759392fe2SPoul-Henning Kamp opts = strrchr(argv[0], '/'); 11859392fe2SPoul-Henning Kamp if (opts) 11959392fe2SPoul-Henning Kamp opts++; 12059392fe2SPoul-Henning Kamp else 12159392fe2SPoul-Henning Kamp opts = argv[0]; 12259392fe2SPoul-Henning Kamp if (!strcmp(opts, "swapinfo")) { 12359392fe2SPoul-Henning Kamp swapflag = 1; 1247e8409a7SMark Murray opts = "ghkmM:N:"; 1257e8409a7SMark Murray usagestr = "swapinfo [-ghkm] [-M core [-N system]]"; 12659392fe2SPoul-Henning Kamp } else { 127ae35e8adSRuslan Ermilov opts = "TM:N:fghkmnst"; 128ae35e8adSRuslan Ermilov usagestr = "pstat [-Tfghkmnst] [-M core [-N system]]"; 12959392fe2SPoul-Henning Kamp } 13059392fe2SPoul-Henning Kamp 1316c3f552aSWarner Losh while ((ch = getopt(argc, argv, opts)) != -1) 132dea673e9SRodney W. Grimes switch (ch) { 133dea673e9SRodney W. Grimes case 'f': 134dea673e9SRodney W. Grimes fileflag = 1; 135dea673e9SRodney W. Grimes break; 1367e8409a7SMark Murray case 'g': 1372966d28cSSean Farley setenv("BLOCKSIZE", "1G", 1); 1387e8409a7SMark Murray break; 1397257230fSGiorgos Keramidas case 'h': 1407257230fSGiorgos Keramidas humanflag = 1; 1417257230fSGiorgos Keramidas break; 14259392fe2SPoul-Henning Kamp case 'k': 1432966d28cSSean Farley setenv("BLOCKSIZE", "1K", 1); 14459392fe2SPoul-Henning Kamp break; 1457e8409a7SMark Murray case 'm': 1462966d28cSSean Farley setenv("BLOCKSIZE", "1M", 1); 1477e8409a7SMark Murray break; 148dea673e9SRodney W. Grimes case 'M': 149dea673e9SRodney W. Grimes memf = optarg; 150dea673e9SRodney W. Grimes break; 151dea673e9SRodney W. Grimes case 'N': 152dea673e9SRodney W. Grimes nlistf = optarg; 153dea673e9SRodney W. Grimes break; 154dea673e9SRodney W. Grimes case 'n': 155dea673e9SRodney W. Grimes usenumflag = 1; 156dea673e9SRodney W. Grimes break; 157dea673e9SRodney W. Grimes case 's': 158eedc3436SMatthew Dillon ++swapflag; 159dea673e9SRodney W. Grimes break; 160dea673e9SRodney W. Grimes case 'T': 161dea673e9SRodney W. Grimes totalflag = 1; 162dea673e9SRodney W. Grimes break; 163dea673e9SRodney W. Grimes case 't': 164dea673e9SRodney W. Grimes ttyflag = 1; 165dea673e9SRodney W. Grimes break; 166dea673e9SRodney W. Grimes default: 167d9961cfdSPhilippe Charnier usage(); 168dea673e9SRodney W. Grimes } 169dea673e9SRodney W. Grimes argc -= optind; 170dea673e9SRodney W. Grimes argv += optind; 171dea673e9SRodney W. Grimes 1724418dbbdSDag-Erling Smørgrav if (memf != NULL) { 1734418dbbdSDag-Erling Smørgrav kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf); 1744418dbbdSDag-Erling Smørgrav if (kd == NULL) 175dea673e9SRodney W. Grimes errx(1, "kvm_openfiles: %s", buf); 176dea673e9SRodney W. Grimes if ((ret = kvm_nlist(kd, nl)) != 0) { 177dea673e9SRodney W. Grimes if (ret == -1) 178dea673e9SRodney W. Grimes errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 1794418dbbdSDag-Erling Smørgrav quit = 0; 180182a90e4SDag-Erling Smørgrav for (i = 0; nl[i].n_name[0] != '\0'; ++i) 181182a90e4SDag-Erling Smørgrav if (nl[i].n_value == 0) { 182dea673e9SRodney W. Grimes quit = 1; 1834418dbbdSDag-Erling Smørgrav warnx("undefined symbol: %s", 1844418dbbdSDag-Erling Smørgrav nl[i].n_name); 185dea673e9SRodney W. Grimes } 186dea673e9SRodney W. Grimes if (quit) 187dea673e9SRodney W. Grimes exit(1); 188dea673e9SRodney W. Grimes } 1894418dbbdSDag-Erling Smørgrav } 1900cbfd1a5SDag-Erling Smørgrav if (!(fileflag | ttyflag | swapflag | totalflag)) 191d9961cfdSPhilippe Charnier usage(); 192dea673e9SRodney W. Grimes if (fileflag || totalflag) 193dea673e9SRodney W. Grimes filemode(); 194dea673e9SRodney W. Grimes if (ttyflag) 195dea673e9SRodney W. Grimes ttymode(); 196dea673e9SRodney W. Grimes if (swapflag || totalflag) 197dea673e9SRodney W. Grimes swapmode(); 198dea673e9SRodney W. Grimes exit (0); 199dea673e9SRodney W. Grimes } 200dea673e9SRodney W. Grimes 201d9961cfdSPhilippe Charnier static void 202c9624363SDag-Erling Smørgrav usage(void) 203d9961cfdSPhilippe Charnier { 204d9961cfdSPhilippe Charnier fprintf(stderr, "usage: %s\n", usagestr); 205d9961cfdSPhilippe Charnier exit (1); 206d9961cfdSPhilippe Charnier } 207d9961cfdSPhilippe Charnier 20846f7e0d2SRobert Drehmel static const char fhdr32[] = 20946f7e0d2SRobert Drehmel " LOC TYPE FLG CNT MSG DATA OFFSET\n"; 21046f7e0d2SRobert Drehmel /* c0000000 ------ RWAI 123 123 c0000000 1000000000000000 */ 21146f7e0d2SRobert Drehmel 21246f7e0d2SRobert Drehmel static const char fhdr64[] = 21346f7e0d2SRobert Drehmel " LOC TYPE FLG CNT MSG DATA OFFSET\n"; 21446f7e0d2SRobert Drehmel /* c000000000000000 ------ RWAI 123 123 c000000000000000 1000000000000000 */ 21546f7e0d2SRobert Drehmel 2161dcc9c32SDag-Erling Smørgrav static const char hdr[] = 217bc093719SEd Schouten " LINE INQ CAN LIN LOW OUTQ USE LOW COL SESS PGID STATE\n"; 218dea673e9SRodney W. Grimes 2191dcc9c32SDag-Erling Smørgrav static void 220182a90e4SDag-Erling Smørgrav ttymode_kvm(void) 221182a90e4SDag-Erling Smørgrav { 222203bcaf5SPoul-Henning Kamp TAILQ_HEAD(, tty) tl; 223182a90e4SDag-Erling Smørgrav struct tty *tp, tty; 224182a90e4SDag-Erling Smørgrav struct xtty xt; 225182a90e4SDag-Erling Smørgrav 226182a90e4SDag-Erling Smørgrav (void)printf("%s", hdr); 227182a90e4SDag-Erling Smørgrav bzero(&xt, sizeof xt); 228182a90e4SDag-Erling Smørgrav xt.xt_size = sizeof xt; 229182a90e4SDag-Erling Smørgrav if (kvm_read(kd, nl[NL_TTY_LIST].n_value, &tl, sizeof tl) != sizeof tl) 230182a90e4SDag-Erling Smørgrav errx(1, "kvm_read(): %s", kvm_geterr(kd)); 231203bcaf5SPoul-Henning Kamp tp = TAILQ_FIRST(&tl); 232182a90e4SDag-Erling Smørgrav while (tp != NULL) { 233182a90e4SDag-Erling Smørgrav if (kvm_read(kd, (u_long)tp, &tty, sizeof tty) != sizeof tty) 234182a90e4SDag-Erling Smørgrav errx(1, "kvm_read(): %s", kvm_geterr(kd)); 235bc093719SEd Schouten xt.xt_insize = tty.t_inq.ti_nblocks * TTYINQ_DATASIZE; 236bc093719SEd Schouten xt.xt_incc = tty.t_inq.ti_linestart - tty.t_inq.ti_begin; 237bc093719SEd Schouten xt.xt_inlc = tty.t_inq.ti_end - tty.t_inq.ti_linestart; 238bc093719SEd Schouten xt.xt_inlow = tty.t_inlow; 239bc093719SEd Schouten xt.xt_outsize = tty.t_outq.to_nblocks * TTYOUTQ_DATASIZE; 240bc093719SEd Schouten xt.xt_outcc = tty.t_outq.to_end - tty.t_outq.to_begin; 241bc093719SEd Schouten xt.xt_outlow = tty.t_outlow; 242bc093719SEd Schouten xt.xt_column = tty.t_column; 243bc093719SEd Schouten /* xt.xt_pgid = ... */ 244bc093719SEd Schouten /* xt.xt_sid = ... */ 245bc093719SEd Schouten xt.xt_flags = tty.t_flags; 246bc093719SEd Schouten xt.xt_dev = NODEV; 247182a90e4SDag-Erling Smørgrav ttyprt(&xt); 248bc42e6c4SRemko Lodder tp = TAILQ_NEXT(&tty, t_list); 249182a90e4SDag-Erling Smørgrav } 250182a90e4SDag-Erling Smørgrav } 251182a90e4SDag-Erling Smørgrav 252182a90e4SDag-Erling Smørgrav static void 253182a90e4SDag-Erling Smørgrav ttymode_sysctl(void) 254dea673e9SRodney W. Grimes { 255333eaaedSDag-Erling Smørgrav struct xtty *xt, *end; 256333eaaedSDag-Erling Smørgrav void *xttys; 257333eaaedSDag-Erling Smørgrav size_t len; 258dea673e9SRodney W. Grimes 259c01c5d5cSDima Dorfman (void)printf("%s", hdr); 260333eaaedSDag-Erling Smørgrav if ((xttys = malloc(len = sizeof *xt)) == NULL) 261333eaaedSDag-Erling Smørgrav err(1, "malloc()"); 262333eaaedSDag-Erling Smørgrav while (sysctlbyname("kern.ttys", xttys, &len, 0, 0) == -1) { 263333eaaedSDag-Erling Smørgrav if (errno != ENOMEM) 264333eaaedSDag-Erling Smørgrav err(1, "sysctlbyname()"); 265333eaaedSDag-Erling Smørgrav len *= 2; 266333eaaedSDag-Erling Smørgrav if ((xttys = realloc(xttys, len)) == NULL) 267333eaaedSDag-Erling Smørgrav err(1, "realloc()"); 2681bc52887SPoul-Henning Kamp } 269333eaaedSDag-Erling Smørgrav if (len > 0) { 270333eaaedSDag-Erling Smørgrav end = (struct xtty *)((char *)xttys + len); 271333eaaedSDag-Erling Smørgrav for (xt = xttys; xt < end; xt++) 272333eaaedSDag-Erling Smørgrav ttyprt(xt); 2731bc52887SPoul-Henning Kamp } 274dea673e9SRodney W. Grimes } 275dea673e9SRodney W. Grimes 2761dcc9c32SDag-Erling Smørgrav static void 277182a90e4SDag-Erling Smørgrav ttymode(void) 278dea673e9SRodney W. Grimes { 279dea673e9SRodney W. Grimes 280182a90e4SDag-Erling Smørgrav if (kd != NULL) 281182a90e4SDag-Erling Smørgrav ttymode_kvm(); 282182a90e4SDag-Erling Smørgrav else 283182a90e4SDag-Erling Smørgrav ttymode_sysctl(); 284dea673e9SRodney W. Grimes } 285dea673e9SRodney W. Grimes 2861dcc9c32SDag-Erling Smørgrav static struct { 287dea673e9SRodney W. Grimes int flag; 288dea673e9SRodney W. Grimes char val; 289dea673e9SRodney W. Grimes } ttystates[] = { 290bc093719SEd Schouten #if 0 291bc093719SEd Schouten { TF_NOPREFIX, 'N' }, 29224f769b0SBruce Evans #endif 293bc093719SEd Schouten { TF_INITLOCK, 'I' }, 294bc093719SEd Schouten { TF_CALLOUT, 'C' }, 295bc093719SEd Schouten 296bc093719SEd Schouten /* Keep these together -> 'Oi' and 'Oo'. */ 297bc093719SEd Schouten { TF_OPENED, 'O' }, 298bc093719SEd Schouten { TF_OPENED_IN, 'i' }, 299bc093719SEd Schouten { TF_OPENED_OUT, 'o' }, 300c3328b2aSEd Schouten { TF_OPENED_CONS, 'c' }, 301bc093719SEd Schouten 302bc093719SEd Schouten { TF_GONE, 'G' }, 303bc093719SEd Schouten { TF_OPENCLOSE, 'B' }, 304bc093719SEd Schouten { TF_ASYNC, 'Y' }, 305bc093719SEd Schouten { TF_LITERAL, 'L' }, 306bc093719SEd Schouten 307bc093719SEd Schouten /* Keep these together -> 'Hi' and 'Ho'. */ 308bc093719SEd Schouten { TF_HIWAT, 'H' }, 309bc093719SEd Schouten { TF_HIWAT_IN, 'i' }, 310bc093719SEd Schouten { TF_HIWAT_OUT, 'o' }, 311bc093719SEd Schouten 312bc093719SEd Schouten { TF_STOPPED, 'S' }, 313bc093719SEd Schouten { TF_EXCLUDE, 'X' }, 314bc093719SEd Schouten { TF_BYPASS, 'l' }, 315bc093719SEd Schouten { TF_ZOMBIE, 'Z' }, 316a1215e37SEd Schouten { TF_HOOK, 's' }, 317bc093719SEd Schouten 318c0086bf2SEd Schouten /* Keep these together -> 'bi' and 'bo'. */ 319c0086bf2SEd Schouten { TF_BUSY, 'b' }, 320c0086bf2SEd Schouten { TF_BUSY_IN, 'i' }, 321c0086bf2SEd Schouten { TF_BUSY_OUT, 'o' }, 322c0086bf2SEd Schouten 323dea673e9SRodney W. Grimes { 0, '\0'}, 324dea673e9SRodney W. Grimes }; 325dea673e9SRodney W. Grimes 3261dcc9c32SDag-Erling Smørgrav static void 327333eaaedSDag-Erling Smørgrav ttyprt(struct xtty *xt) 328dea673e9SRodney W. Grimes { 3299c5cdfe0SPeter Wemm int i, j; 330bc093719SEd Schouten char *name; 331dea673e9SRodney W. Grimes 332333eaaedSDag-Erling Smørgrav if (xt->xt_size != sizeof *xt) 333333eaaedSDag-Erling Smørgrav errx(1, "struct xtty size mismatch"); 334333eaaedSDag-Erling Smørgrav if (usenumflag || xt->xt_dev == 0 || 335333eaaedSDag-Erling Smørgrav (name = devname(xt->xt_dev, S_IFCHR)) == NULL) 336bc093719SEd Schouten printf("%5d,%4d ", major(xt->xt_dev), minor(xt->xt_dev)); 337dea673e9SRodney W. Grimes else 338bc093719SEd Schouten printf("%10s ", name); 339bc093719SEd Schouten printf("%5zu %4zu %4zu %4zu %5zu %4zu %4zu %5u %5d %5d ", 340bc093719SEd Schouten xt->xt_insize, xt->xt_incc, xt->xt_inlc, 341bc093719SEd Schouten (xt->xt_insize - xt->xt_inlow), xt->xt_outsize, 342bc093719SEd Schouten xt->xt_outcc, (xt->xt_outsize - xt->xt_outlow), 34337a9f582SEd Schouten MIN(xt->xt_column, 99999), xt->xt_sid, xt->xt_pgid); 344dea673e9SRodney W. Grimes for (i = j = 0; ttystates[i].flag; i++) 345bc093719SEd Schouten if (xt->xt_flags & ttystates[i].flag) { 346bc093719SEd Schouten putchar(ttystates[i].val); 347bc093719SEd Schouten j++; 348dea673e9SRodney W. Grimes } 349bc093719SEd Schouten if (j == 0) 350bc093719SEd Schouten putchar('-'); 351bc093719SEd Schouten putchar('\n'); 352dea673e9SRodney W. Grimes } 353dea673e9SRodney W. Grimes 3541dcc9c32SDag-Erling Smørgrav static void 355c9624363SDag-Erling Smørgrav filemode(void) 356dea673e9SRodney W. Grimes { 357eeebf53eSDag-Erling Smørgrav struct xfile *fp; 358dea673e9SRodney W. Grimes char *buf, flagbuf[16], *fbp; 3595f9ab4b0SDag-Erling Smørgrav int maxf, openf; 3604418dbbdSDag-Erling Smørgrav size_t len; 36146f7e0d2SRobert Drehmel static char *dtypes[] = { "???", "inode", "socket", "pipe", 36246f7e0d2SRobert Drehmel "fifo", "kqueue", "crypto" }; 363eeebf53eSDag-Erling Smørgrav int i; 36446f7e0d2SRobert Drehmel int wid; 365dea673e9SRodney W. Grimes 3664418dbbdSDag-Erling Smørgrav if (kd != NULL) { 3675f9ab4b0SDag-Erling Smørgrav if (kvm_read(kd, nl[NL_MAXFILES].n_value, 3685f9ab4b0SDag-Erling Smørgrav &maxf, sizeof maxf) != sizeof maxf || 3695f9ab4b0SDag-Erling Smørgrav kvm_read(kd, nl[NL_NFILES].n_value, 3705f9ab4b0SDag-Erling Smørgrav &openf, sizeof openf) != sizeof openf) 3715f9ab4b0SDag-Erling Smørgrav errx(1, "kvm_read(): %s", kvm_geterr(kd)); 3724418dbbdSDag-Erling Smørgrav } else { 3735f9ab4b0SDag-Erling Smørgrav len = sizeof(int); 3745f9ab4b0SDag-Erling Smørgrav if (sysctlbyname("kern.maxfiles", &maxf, &len, 0, 0) == -1 || 3755f9ab4b0SDag-Erling Smørgrav sysctlbyname("kern.openfiles", &openf, &len, 0, 0) == -1) 3764418dbbdSDag-Erling Smørgrav err(1, "sysctlbyname()"); 3774418dbbdSDag-Erling Smørgrav } 3784418dbbdSDag-Erling Smørgrav 379dea673e9SRodney W. Grimes if (totalflag) { 3805f9ab4b0SDag-Erling Smørgrav (void)printf("%3d/%3d files\n", openf, maxf); 381dea673e9SRodney W. Grimes return; 382dea673e9SRodney W. Grimes } 383dea673e9SRodney W. Grimes if (getfiles(&buf, &len) == -1) 384dea673e9SRodney W. Grimes return; 385eeebf53eSDag-Erling Smørgrav openf = len / sizeof *fp; 38646f7e0d2SRobert Drehmel 3875f9ab4b0SDag-Erling Smørgrav (void)printf("%d/%d open files\n", openf, maxf); 38846f7e0d2SRobert Drehmel printf(sizeof(uintptr_t) == 4 ? fhdr32 : fhdr64); 38946f7e0d2SRobert Drehmel wid = (int)sizeof(uintptr_t) * 2; 390eeebf53eSDag-Erling Smørgrav for (fp = (struct xfile *)buf, i = 0; i < openf; ++fp, ++i) { 39146f7e0d2SRobert Drehmel if ((size_t)fp->xf_type >= sizeof(dtypes) / sizeof(dtypes[0])) 392dea673e9SRodney W. Grimes continue; 39346f7e0d2SRobert Drehmel (void)printf("%*jx", wid, (uintmax_t)(uintptr_t)fp->xf_file); 39446f7e0d2SRobert Drehmel (void)printf(" %-6.6s", dtypes[fp->xf_type]); 395dea673e9SRodney W. Grimes fbp = flagbuf; 396eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FREAD) 397dea673e9SRodney W. Grimes *fbp++ = 'R'; 398eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FWRITE) 399dea673e9SRodney W. Grimes *fbp++ = 'W'; 400eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FAPPEND) 401dea673e9SRodney W. Grimes *fbp++ = 'A'; 402eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FASYNC) 403dea673e9SRodney W. Grimes *fbp++ = 'I'; 404dea673e9SRodney W. Grimes *fbp = '\0'; 40546f7e0d2SRobert Drehmel (void)printf(" %4s %3d", flagbuf, fp->xf_count); 406eeebf53eSDag-Erling Smørgrav (void)printf(" %3d", fp->xf_msgcount); 40746f7e0d2SRobert Drehmel (void)printf(" %*jx", wid, (uintmax_t)(uintptr_t)fp->xf_data); 40846f7e0d2SRobert Drehmel (void)printf(" %*jx\n", (int)sizeof(fp->xf_offset) * 2, 40946f7e0d2SRobert Drehmel (uintmax_t)fp->xf_offset); 410dea673e9SRodney W. Grimes } 411dea673e9SRodney W. Grimes free(buf); 412dea673e9SRodney W. Grimes } 413dea673e9SRodney W. Grimes 4141dcc9c32SDag-Erling Smørgrav static int 415333eaaedSDag-Erling Smørgrav getfiles(char **abuf, size_t *alen) 416dea673e9SRodney W. Grimes { 417dea673e9SRodney W. Grimes size_t len; 418dea673e9SRodney W. Grimes int mib[2]; 419dea673e9SRodney W. Grimes char *buf; 420dea673e9SRodney W. Grimes 421dea673e9SRodney W. Grimes /* 422dea673e9SRodney W. Grimes * XXX 423dea673e9SRodney W. Grimes * Add emulation of KINFO_FILE here. 424dea673e9SRodney W. Grimes */ 4254418dbbdSDag-Erling Smørgrav if (kd != NULL) 426d9961cfdSPhilippe Charnier errx(1, "files on dead kernel, not implemented"); 427dea673e9SRodney W. Grimes 428dea673e9SRodney W. Grimes mib[0] = CTL_KERN; 429dea673e9SRodney W. Grimes mib[1] = KERN_FILE; 430dea673e9SRodney W. Grimes if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 431dea673e9SRodney W. Grimes warn("sysctl: KERN_FILE"); 432dea673e9SRodney W. Grimes return (-1); 433dea673e9SRodney W. Grimes } 434dea673e9SRodney W. Grimes if ((buf = malloc(len)) == NULL) 435d9961cfdSPhilippe Charnier errx(1, "malloc"); 436dea673e9SRodney W. Grimes if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 437dea673e9SRodney W. Grimes warn("sysctl: KERN_FILE"); 438dea673e9SRodney W. Grimes return (-1); 439dea673e9SRodney W. Grimes } 440dea673e9SRodney W. Grimes *abuf = buf; 441dea673e9SRodney W. Grimes *alen = len; 442dea673e9SRodney W. Grimes return (0); 443dea673e9SRodney W. Grimes } 444dea673e9SRodney W. Grimes 445dea673e9SRodney W. Grimes /* 446dea673e9SRodney W. Grimes * swapmode is based on a program called swapinfo written 447dea673e9SRodney W. Grimes * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 448dea673e9SRodney W. Grimes */ 4494418dbbdSDag-Erling Smørgrav 4507257230fSGiorgos Keramidas #define CONVERT(v) ((int64_t)(v) * pagesize / blocksize) 4514418dbbdSDag-Erling Smørgrav static struct kvm_swap swtot; 4524418dbbdSDag-Erling Smørgrav static int nswdev; 4534418dbbdSDag-Erling Smørgrav 4541dcc9c32SDag-Erling Smørgrav static void 4554418dbbdSDag-Erling Smørgrav print_swap_header(void) 456dea673e9SRodney W. Grimes { 457c48205f3SMike Barcroft int hlen; 458782f8687SMatthew Dillon long blocksize; 4594418dbbdSDag-Erling Smørgrav const char *header; 460dea673e9SRodney W. Grimes 461dea673e9SRodney W. Grimes header = getbsize(&hlen, &blocksize); 4624418dbbdSDag-Erling Smørgrav if (totalflag == 0) 46346637267SPoul-Henning Kamp (void)printf("%-15s %*s %8s %8s %8s\n", 464c48205f3SMike Barcroft "Device", hlen, header, 46546637267SPoul-Henning Kamp "Used", "Avail", "Capacity"); 4664418dbbdSDag-Erling Smørgrav } 467eedc3436SMatthew Dillon 4684418dbbdSDag-Erling Smørgrav static void 4697257230fSGiorgos Keramidas print_swap_line(const char *devname, intmax_t nblks, intmax_t bused, 4707257230fSGiorgos Keramidas intmax_t bavail, float bpercent) 4714418dbbdSDag-Erling Smørgrav { 4727257230fSGiorgos Keramidas char usedbuf[5]; 4737257230fSGiorgos Keramidas char availbuf[5]; 474c48205f3SMike Barcroft int hlen, pagesize; 4754418dbbdSDag-Erling Smørgrav long blocksize; 4764418dbbdSDag-Erling Smørgrav 4774418dbbdSDag-Erling Smørgrav pagesize = getpagesize(); 4784418dbbdSDag-Erling Smørgrav getbsize(&hlen, &blocksize); 4797257230fSGiorgos Keramidas 4807257230fSGiorgos Keramidas printf("%-15s %*jd ", devname, hlen, CONVERT(nblks)); 4817257230fSGiorgos Keramidas if (humanflag) { 4827257230fSGiorgos Keramidas humanize_number(usedbuf, sizeof(usedbuf), 4837257230fSGiorgos Keramidas CONVERT(blocksize * bused), "", 4847257230fSGiorgos Keramidas HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 4857257230fSGiorgos Keramidas humanize_number(availbuf, sizeof(availbuf), 4867257230fSGiorgos Keramidas CONVERT(blocksize * bavail), "", 4877257230fSGiorgos Keramidas HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 4887257230fSGiorgos Keramidas printf("%8s %8s %5.0f%%\n", usedbuf, availbuf, bpercent); 4897257230fSGiorgos Keramidas } else { 4907257230fSGiorgos Keramidas printf("%8jd %8jd %5.0f%%\n", (intmax_t)CONVERT(bused), 4917257230fSGiorgos Keramidas (intmax_t)CONVERT(bavail), bpercent); 4927257230fSGiorgos Keramidas } 4937257230fSGiorgos Keramidas } 4947257230fSGiorgos Keramidas 4957257230fSGiorgos Keramidas static void 4967257230fSGiorgos Keramidas print_swap(struct kvm_swap *ksw) 4977257230fSGiorgos Keramidas { 4987257230fSGiorgos Keramidas 4994418dbbdSDag-Erling Smørgrav swtot.ksw_total += ksw->ksw_total; 5004418dbbdSDag-Erling Smørgrav swtot.ksw_used += ksw->ksw_used; 5014418dbbdSDag-Erling Smørgrav ++nswdev; 5027257230fSGiorgos Keramidas if (totalflag == 0) 5037257230fSGiorgos Keramidas print_swap_line(ksw->ksw_devname, ksw->ksw_total, 504a2e59d80SRobert Watson ksw->ksw_used, ksw->ksw_total - ksw->ksw_used, 50546637267SPoul-Henning Kamp (ksw->ksw_used * 100.0) / ksw->ksw_total); 506eedc3436SMatthew Dillon } 507eedc3436SMatthew Dillon 5084418dbbdSDag-Erling Smørgrav static void 5094418dbbdSDag-Erling Smørgrav print_swap_total(void) 5104418dbbdSDag-Erling Smørgrav { 511c48205f3SMike Barcroft int hlen, pagesize; 5124418dbbdSDag-Erling Smørgrav long blocksize; 5134418dbbdSDag-Erling Smørgrav 5144418dbbdSDag-Erling Smørgrav pagesize = getpagesize(); 5154418dbbdSDag-Erling Smørgrav getbsize(&hlen, &blocksize); 516dea673e9SRodney W. Grimes if (totalflag) { 517782f8687SMatthew Dillon blocksize = 1024 * 1024; 5187257230fSGiorgos Keramidas (void)printf("%jdM/%jdM swap space\n", 5194418dbbdSDag-Erling Smørgrav CONVERT(swtot.ksw_used), CONVERT(swtot.ksw_total)); 5204418dbbdSDag-Erling Smørgrav } else if (nswdev > 1) { 5217257230fSGiorgos Keramidas print_swap_line("Total", swtot.ksw_total, swtot.ksw_used, 5227257230fSGiorgos Keramidas swtot.ksw_total - swtot.ksw_used, 5234418dbbdSDag-Erling Smørgrav (swtot.ksw_used * 100.0) / swtot.ksw_total); 524dea673e9SRodney W. Grimes } 525dea673e9SRodney W. Grimes } 5264418dbbdSDag-Erling Smørgrav 5274418dbbdSDag-Erling Smørgrav static void 5284418dbbdSDag-Erling Smørgrav swapmode_kvm(void) 5294418dbbdSDag-Erling Smørgrav { 5304418dbbdSDag-Erling Smørgrav struct kvm_swap kswap[16]; 5314418dbbdSDag-Erling Smørgrav int i, n; 5324418dbbdSDag-Erling Smørgrav 5334418dbbdSDag-Erling Smørgrav n = kvm_getswapinfo(kd, kswap, sizeof kswap / sizeof kswap[0], 534799aba96SPoul-Henning Kamp SWIF_DEV_PREFIX); 5354418dbbdSDag-Erling Smørgrav 5364418dbbdSDag-Erling Smørgrav print_swap_header(); 5374418dbbdSDag-Erling Smørgrav for (i = 0; i < n; ++i) 5384418dbbdSDag-Erling Smørgrav print_swap(&kswap[i]); 5394418dbbdSDag-Erling Smørgrav print_swap_total(); 5404418dbbdSDag-Erling Smørgrav } 5414418dbbdSDag-Erling Smørgrav 5424418dbbdSDag-Erling Smørgrav static void 5434418dbbdSDag-Erling Smørgrav swapmode_sysctl(void) 5444418dbbdSDag-Erling Smørgrav { 5454418dbbdSDag-Erling Smørgrav struct kvm_swap ksw; 5464418dbbdSDag-Erling Smørgrav struct xswdev xsw; 5474418dbbdSDag-Erling Smørgrav size_t mibsize, size; 5484418dbbdSDag-Erling Smørgrav int mib[16], n; 5494418dbbdSDag-Erling Smørgrav 5504418dbbdSDag-Erling Smørgrav print_swap_header(); 5514418dbbdSDag-Erling Smørgrav mibsize = sizeof mib / sizeof mib[0]; 5524418dbbdSDag-Erling Smørgrav if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1) 5534418dbbdSDag-Erling Smørgrav err(1, "sysctlnametomib()"); 5544418dbbdSDag-Erling Smørgrav for (n = 0; ; ++n) { 5554418dbbdSDag-Erling Smørgrav mib[mibsize] = n; 5564418dbbdSDag-Erling Smørgrav size = sizeof xsw; 55716fc3635SMark Murray if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1) 5584418dbbdSDag-Erling Smørgrav break; 5594418dbbdSDag-Erling Smørgrav if (xsw.xsw_version != XSWDEV_VERSION) 5604418dbbdSDag-Erling Smørgrav errx(1, "xswdev version mismatch"); 5611afc333eSPoul-Henning Kamp if (xsw.xsw_dev == NODEV) 5621afc333eSPoul-Henning Kamp snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname, 5631afc333eSPoul-Henning Kamp "<NFSfile>"); 5641afc333eSPoul-Henning Kamp else 5654418dbbdSDag-Erling Smørgrav snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname, 5664418dbbdSDag-Erling Smørgrav "/dev/%s", devname(xsw.xsw_dev, S_IFCHR)); 5674418dbbdSDag-Erling Smørgrav ksw.ksw_used = xsw.xsw_used; 5684418dbbdSDag-Erling Smørgrav ksw.ksw_total = xsw.xsw_nblks; 5694418dbbdSDag-Erling Smørgrav ksw.ksw_flags = xsw.xsw_flags; 5704418dbbdSDag-Erling Smørgrav print_swap(&ksw); 5714418dbbdSDag-Erling Smørgrav } 5724418dbbdSDag-Erling Smørgrav if (errno != ENOENT) 5734418dbbdSDag-Erling Smørgrav err(1, "sysctl()"); 5744418dbbdSDag-Erling Smørgrav print_swap_total(); 5754418dbbdSDag-Erling Smørgrav } 5764418dbbdSDag-Erling Smørgrav 5774418dbbdSDag-Erling Smørgrav static void 5784418dbbdSDag-Erling Smørgrav swapmode(void) 5794418dbbdSDag-Erling Smørgrav { 5804418dbbdSDag-Erling Smørgrav if (kd != NULL) 5814418dbbdSDag-Erling Smørgrav swapmode_kvm(); 5824418dbbdSDag-Erling Smørgrav else 5834418dbbdSDag-Erling Smørgrav swapmode_sysctl(); 5844418dbbdSDag-Erling Smørgrav } 585