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' }, 300bc093719SEd Schouten 301bc093719SEd Schouten { TF_GONE, 'G' }, 302bc093719SEd Schouten { TF_OPENCLOSE, 'B' }, 303bc093719SEd Schouten { TF_ASYNC, 'Y' }, 304bc093719SEd Schouten { TF_LITERAL, 'L' }, 305bc093719SEd Schouten 306bc093719SEd Schouten /* Keep these together -> 'Hi' and 'Ho'. */ 307bc093719SEd Schouten { TF_HIWAT, 'H' }, 308bc093719SEd Schouten { TF_HIWAT_IN, 'i' }, 309bc093719SEd Schouten { TF_HIWAT_OUT, 'o' }, 310bc093719SEd Schouten 311bc093719SEd Schouten { TF_STOPPED, 'S' }, 312bc093719SEd Schouten { TF_EXCLUDE, 'X' }, 313bc093719SEd Schouten { TF_BYPASS, 'l' }, 314bc093719SEd Schouten { TF_ZOMBIE, 'Z' }, 315bc093719SEd Schouten 316dea673e9SRodney W. Grimes { 0, '\0' }, 317dea673e9SRodney W. Grimes }; 318dea673e9SRodney W. Grimes 3191dcc9c32SDag-Erling Smørgrav static void 320333eaaedSDag-Erling Smørgrav ttyprt(struct xtty *xt) 321dea673e9SRodney W. Grimes { 3229c5cdfe0SPeter Wemm int i, j; 323bc093719SEd Schouten char *name; 324dea673e9SRodney W. Grimes 325333eaaedSDag-Erling Smørgrav if (xt->xt_size != sizeof *xt) 326333eaaedSDag-Erling Smørgrav errx(1, "struct xtty size mismatch"); 327333eaaedSDag-Erling Smørgrav if (usenumflag || xt->xt_dev == 0 || 328333eaaedSDag-Erling Smørgrav (name = devname(xt->xt_dev, S_IFCHR)) == NULL) 329bc093719SEd Schouten printf("%5d,%4d ", major(xt->xt_dev), minor(xt->xt_dev)); 330dea673e9SRodney W. Grimes else 331bc093719SEd Schouten printf("%10s ", name); 332bc093719SEd Schouten printf("%5zu %4zu %4zu %4zu %5zu %4zu %4zu %5u %5d %5d ", 333bc093719SEd Schouten xt->xt_insize, xt->xt_incc, xt->xt_inlc, 334bc093719SEd Schouten (xt->xt_insize - xt->xt_inlow), xt->xt_outsize, 335bc093719SEd Schouten xt->xt_outcc, (xt->xt_outsize - xt->xt_outlow), 336bc093719SEd Schouten xt->xt_column, xt->xt_sid, xt->xt_pgid); 337dea673e9SRodney W. Grimes for (i = j = 0; ttystates[i].flag; i++) 338bc093719SEd Schouten if (xt->xt_flags & ttystates[i].flag) { 339bc093719SEd Schouten putchar(ttystates[i].val); 340bc093719SEd Schouten j++; 341dea673e9SRodney W. Grimes } 342bc093719SEd Schouten if (j == 0) 343bc093719SEd Schouten putchar('-'); 344bc093719SEd Schouten putchar('\n'); 345dea673e9SRodney W. Grimes } 346dea673e9SRodney W. Grimes 3471dcc9c32SDag-Erling Smørgrav static void 348c9624363SDag-Erling Smørgrav filemode(void) 349dea673e9SRodney W. Grimes { 350eeebf53eSDag-Erling Smørgrav struct xfile *fp; 351dea673e9SRodney W. Grimes char *buf, flagbuf[16], *fbp; 3525f9ab4b0SDag-Erling Smørgrav int maxf, openf; 3534418dbbdSDag-Erling Smørgrav size_t len; 35446f7e0d2SRobert Drehmel static char *dtypes[] = { "???", "inode", "socket", "pipe", 35546f7e0d2SRobert Drehmel "fifo", "kqueue", "crypto" }; 356eeebf53eSDag-Erling Smørgrav int i; 35746f7e0d2SRobert Drehmel int wid; 358dea673e9SRodney W. Grimes 3594418dbbdSDag-Erling Smørgrav if (kd != NULL) { 3605f9ab4b0SDag-Erling Smørgrav if (kvm_read(kd, nl[NL_MAXFILES].n_value, 3615f9ab4b0SDag-Erling Smørgrav &maxf, sizeof maxf) != sizeof maxf || 3625f9ab4b0SDag-Erling Smørgrav kvm_read(kd, nl[NL_NFILES].n_value, 3635f9ab4b0SDag-Erling Smørgrav &openf, sizeof openf) != sizeof openf) 3645f9ab4b0SDag-Erling Smørgrav errx(1, "kvm_read(): %s", kvm_geterr(kd)); 3654418dbbdSDag-Erling Smørgrav } else { 3665f9ab4b0SDag-Erling Smørgrav len = sizeof(int); 3675f9ab4b0SDag-Erling Smørgrav if (sysctlbyname("kern.maxfiles", &maxf, &len, 0, 0) == -1 || 3685f9ab4b0SDag-Erling Smørgrav sysctlbyname("kern.openfiles", &openf, &len, 0, 0) == -1) 3694418dbbdSDag-Erling Smørgrav err(1, "sysctlbyname()"); 3704418dbbdSDag-Erling Smørgrav } 3714418dbbdSDag-Erling Smørgrav 372dea673e9SRodney W. Grimes if (totalflag) { 3735f9ab4b0SDag-Erling Smørgrav (void)printf("%3d/%3d files\n", openf, maxf); 374dea673e9SRodney W. Grimes return; 375dea673e9SRodney W. Grimes } 376dea673e9SRodney W. Grimes if (getfiles(&buf, &len) == -1) 377dea673e9SRodney W. Grimes return; 378eeebf53eSDag-Erling Smørgrav openf = len / sizeof *fp; 37946f7e0d2SRobert Drehmel 3805f9ab4b0SDag-Erling Smørgrav (void)printf("%d/%d open files\n", openf, maxf); 38146f7e0d2SRobert Drehmel printf(sizeof(uintptr_t) == 4 ? fhdr32 : fhdr64); 38246f7e0d2SRobert Drehmel wid = (int)sizeof(uintptr_t) * 2; 383eeebf53eSDag-Erling Smørgrav for (fp = (struct xfile *)buf, i = 0; i < openf; ++fp, ++i) { 38446f7e0d2SRobert Drehmel if ((size_t)fp->xf_type >= sizeof(dtypes) / sizeof(dtypes[0])) 385dea673e9SRodney W. Grimes continue; 38646f7e0d2SRobert Drehmel (void)printf("%*jx", wid, (uintmax_t)(uintptr_t)fp->xf_file); 38746f7e0d2SRobert Drehmel (void)printf(" %-6.6s", dtypes[fp->xf_type]); 388dea673e9SRodney W. Grimes fbp = flagbuf; 389eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FREAD) 390dea673e9SRodney W. Grimes *fbp++ = 'R'; 391eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FWRITE) 392dea673e9SRodney W. Grimes *fbp++ = 'W'; 393eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FAPPEND) 394dea673e9SRodney W. Grimes *fbp++ = 'A'; 395eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FASYNC) 396dea673e9SRodney W. Grimes *fbp++ = 'I'; 397dea673e9SRodney W. Grimes *fbp = '\0'; 39846f7e0d2SRobert Drehmel (void)printf(" %4s %3d", flagbuf, fp->xf_count); 399eeebf53eSDag-Erling Smørgrav (void)printf(" %3d", fp->xf_msgcount); 40046f7e0d2SRobert Drehmel (void)printf(" %*jx", wid, (uintmax_t)(uintptr_t)fp->xf_data); 40146f7e0d2SRobert Drehmel (void)printf(" %*jx\n", (int)sizeof(fp->xf_offset) * 2, 40246f7e0d2SRobert Drehmel (uintmax_t)fp->xf_offset); 403dea673e9SRodney W. Grimes } 404dea673e9SRodney W. Grimes free(buf); 405dea673e9SRodney W. Grimes } 406dea673e9SRodney W. Grimes 4071dcc9c32SDag-Erling Smørgrav static int 408333eaaedSDag-Erling Smørgrav getfiles(char **abuf, size_t *alen) 409dea673e9SRodney W. Grimes { 410dea673e9SRodney W. Grimes size_t len; 411dea673e9SRodney W. Grimes int mib[2]; 412dea673e9SRodney W. Grimes char *buf; 413dea673e9SRodney W. Grimes 414dea673e9SRodney W. Grimes /* 415dea673e9SRodney W. Grimes * XXX 416dea673e9SRodney W. Grimes * Add emulation of KINFO_FILE here. 417dea673e9SRodney W. Grimes */ 4184418dbbdSDag-Erling Smørgrav if (kd != NULL) 419d9961cfdSPhilippe Charnier errx(1, "files on dead kernel, not implemented"); 420dea673e9SRodney W. Grimes 421dea673e9SRodney W. Grimes mib[0] = CTL_KERN; 422dea673e9SRodney W. Grimes mib[1] = KERN_FILE; 423dea673e9SRodney W. Grimes if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 424dea673e9SRodney W. Grimes warn("sysctl: KERN_FILE"); 425dea673e9SRodney W. Grimes return (-1); 426dea673e9SRodney W. Grimes } 427dea673e9SRodney W. Grimes if ((buf = malloc(len)) == NULL) 428d9961cfdSPhilippe Charnier errx(1, "malloc"); 429dea673e9SRodney W. Grimes if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 430dea673e9SRodney W. Grimes warn("sysctl: KERN_FILE"); 431dea673e9SRodney W. Grimes return (-1); 432dea673e9SRodney W. Grimes } 433dea673e9SRodney W. Grimes *abuf = buf; 434dea673e9SRodney W. Grimes *alen = len; 435dea673e9SRodney W. Grimes return (0); 436dea673e9SRodney W. Grimes } 437dea673e9SRodney W. Grimes 438dea673e9SRodney W. Grimes /* 439dea673e9SRodney W. Grimes * swapmode is based on a program called swapinfo written 440dea673e9SRodney W. Grimes * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 441dea673e9SRodney W. Grimes */ 4424418dbbdSDag-Erling Smørgrav 4437257230fSGiorgos Keramidas #define CONVERT(v) ((int64_t)(v) * pagesize / blocksize) 4444418dbbdSDag-Erling Smørgrav static struct kvm_swap swtot; 4454418dbbdSDag-Erling Smørgrav static int nswdev; 4464418dbbdSDag-Erling Smørgrav 4471dcc9c32SDag-Erling Smørgrav static void 4484418dbbdSDag-Erling Smørgrav print_swap_header(void) 449dea673e9SRodney W. Grimes { 450c48205f3SMike Barcroft int hlen; 451782f8687SMatthew Dillon long blocksize; 4524418dbbdSDag-Erling Smørgrav const char *header; 453dea673e9SRodney W. Grimes 454dea673e9SRodney W. Grimes header = getbsize(&hlen, &blocksize); 4554418dbbdSDag-Erling Smørgrav if (totalflag == 0) 45646637267SPoul-Henning Kamp (void)printf("%-15s %*s %8s %8s %8s\n", 457c48205f3SMike Barcroft "Device", hlen, header, 45846637267SPoul-Henning Kamp "Used", "Avail", "Capacity"); 4594418dbbdSDag-Erling Smørgrav } 460eedc3436SMatthew Dillon 4614418dbbdSDag-Erling Smørgrav static void 4627257230fSGiorgos Keramidas print_swap_line(const char *devname, intmax_t nblks, intmax_t bused, 4637257230fSGiorgos Keramidas intmax_t bavail, float bpercent) 4644418dbbdSDag-Erling Smørgrav { 4657257230fSGiorgos Keramidas char usedbuf[5]; 4667257230fSGiorgos Keramidas char availbuf[5]; 467c48205f3SMike Barcroft int hlen, pagesize; 4684418dbbdSDag-Erling Smørgrav long blocksize; 4694418dbbdSDag-Erling Smørgrav 4704418dbbdSDag-Erling Smørgrav pagesize = getpagesize(); 4714418dbbdSDag-Erling Smørgrav getbsize(&hlen, &blocksize); 4727257230fSGiorgos Keramidas 4737257230fSGiorgos Keramidas printf("%-15s %*jd ", devname, hlen, CONVERT(nblks)); 4747257230fSGiorgos Keramidas if (humanflag) { 4757257230fSGiorgos Keramidas humanize_number(usedbuf, sizeof(usedbuf), 4767257230fSGiorgos Keramidas CONVERT(blocksize * bused), "", 4777257230fSGiorgos Keramidas HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 4787257230fSGiorgos Keramidas humanize_number(availbuf, sizeof(availbuf), 4797257230fSGiorgos Keramidas CONVERT(blocksize * bavail), "", 4807257230fSGiorgos Keramidas HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 4817257230fSGiorgos Keramidas printf("%8s %8s %5.0f%%\n", usedbuf, availbuf, bpercent); 4827257230fSGiorgos Keramidas } else { 4837257230fSGiorgos Keramidas printf("%8jd %8jd %5.0f%%\n", (intmax_t)CONVERT(bused), 4847257230fSGiorgos Keramidas (intmax_t)CONVERT(bavail), bpercent); 4857257230fSGiorgos Keramidas } 4867257230fSGiorgos Keramidas } 4877257230fSGiorgos Keramidas 4887257230fSGiorgos Keramidas static void 4897257230fSGiorgos Keramidas print_swap(struct kvm_swap *ksw) 4907257230fSGiorgos Keramidas { 4917257230fSGiorgos Keramidas 4924418dbbdSDag-Erling Smørgrav swtot.ksw_total += ksw->ksw_total; 4934418dbbdSDag-Erling Smørgrav swtot.ksw_used += ksw->ksw_used; 4944418dbbdSDag-Erling Smørgrav ++nswdev; 4957257230fSGiorgos Keramidas if (totalflag == 0) 4967257230fSGiorgos Keramidas print_swap_line(ksw->ksw_devname, ksw->ksw_total, 497a2e59d80SRobert Watson ksw->ksw_used, ksw->ksw_total - ksw->ksw_used, 49846637267SPoul-Henning Kamp (ksw->ksw_used * 100.0) / ksw->ksw_total); 499eedc3436SMatthew Dillon } 500eedc3436SMatthew Dillon 5014418dbbdSDag-Erling Smørgrav static void 5024418dbbdSDag-Erling Smørgrav print_swap_total(void) 5034418dbbdSDag-Erling Smørgrav { 504c48205f3SMike Barcroft int hlen, pagesize; 5054418dbbdSDag-Erling Smørgrav long blocksize; 5064418dbbdSDag-Erling Smørgrav 5074418dbbdSDag-Erling Smørgrav pagesize = getpagesize(); 5084418dbbdSDag-Erling Smørgrav getbsize(&hlen, &blocksize); 509dea673e9SRodney W. Grimes if (totalflag) { 510782f8687SMatthew Dillon blocksize = 1024 * 1024; 5117257230fSGiorgos Keramidas (void)printf("%jdM/%jdM swap space\n", 5124418dbbdSDag-Erling Smørgrav CONVERT(swtot.ksw_used), CONVERT(swtot.ksw_total)); 5134418dbbdSDag-Erling Smørgrav } else if (nswdev > 1) { 5147257230fSGiorgos Keramidas print_swap_line("Total", swtot.ksw_total, swtot.ksw_used, 5157257230fSGiorgos Keramidas swtot.ksw_total - swtot.ksw_used, 5164418dbbdSDag-Erling Smørgrav (swtot.ksw_used * 100.0) / swtot.ksw_total); 517dea673e9SRodney W. Grimes } 518dea673e9SRodney W. Grimes } 5194418dbbdSDag-Erling Smørgrav 5204418dbbdSDag-Erling Smørgrav static void 5214418dbbdSDag-Erling Smørgrav swapmode_kvm(void) 5224418dbbdSDag-Erling Smørgrav { 5234418dbbdSDag-Erling Smørgrav struct kvm_swap kswap[16]; 5244418dbbdSDag-Erling Smørgrav int i, n; 5254418dbbdSDag-Erling Smørgrav 5264418dbbdSDag-Erling Smørgrav n = kvm_getswapinfo(kd, kswap, sizeof kswap / sizeof kswap[0], 527799aba96SPoul-Henning Kamp SWIF_DEV_PREFIX); 5284418dbbdSDag-Erling Smørgrav 5294418dbbdSDag-Erling Smørgrav print_swap_header(); 5304418dbbdSDag-Erling Smørgrav for (i = 0; i < n; ++i) 5314418dbbdSDag-Erling Smørgrav print_swap(&kswap[i]); 5324418dbbdSDag-Erling Smørgrav print_swap_total(); 5334418dbbdSDag-Erling Smørgrav } 5344418dbbdSDag-Erling Smørgrav 5354418dbbdSDag-Erling Smørgrav static void 5364418dbbdSDag-Erling Smørgrav swapmode_sysctl(void) 5374418dbbdSDag-Erling Smørgrav { 5384418dbbdSDag-Erling Smørgrav struct kvm_swap ksw; 5394418dbbdSDag-Erling Smørgrav struct xswdev xsw; 5404418dbbdSDag-Erling Smørgrav size_t mibsize, size; 5414418dbbdSDag-Erling Smørgrav int mib[16], n; 5424418dbbdSDag-Erling Smørgrav 5434418dbbdSDag-Erling Smørgrav print_swap_header(); 5444418dbbdSDag-Erling Smørgrav mibsize = sizeof mib / sizeof mib[0]; 5454418dbbdSDag-Erling Smørgrav if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1) 5464418dbbdSDag-Erling Smørgrav err(1, "sysctlnametomib()"); 5474418dbbdSDag-Erling Smørgrav for (n = 0; ; ++n) { 5484418dbbdSDag-Erling Smørgrav mib[mibsize] = n; 5494418dbbdSDag-Erling Smørgrav size = sizeof xsw; 55016fc3635SMark Murray if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1) 5514418dbbdSDag-Erling Smørgrav break; 5524418dbbdSDag-Erling Smørgrav if (xsw.xsw_version != XSWDEV_VERSION) 5534418dbbdSDag-Erling Smørgrav errx(1, "xswdev version mismatch"); 5541afc333eSPoul-Henning Kamp if (xsw.xsw_dev == NODEV) 5551afc333eSPoul-Henning Kamp snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname, 5561afc333eSPoul-Henning Kamp "<NFSfile>"); 5571afc333eSPoul-Henning Kamp else 5584418dbbdSDag-Erling Smørgrav snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname, 5594418dbbdSDag-Erling Smørgrav "/dev/%s", devname(xsw.xsw_dev, S_IFCHR)); 5604418dbbdSDag-Erling Smørgrav ksw.ksw_used = xsw.xsw_used; 5614418dbbdSDag-Erling Smørgrav ksw.ksw_total = xsw.xsw_nblks; 5624418dbbdSDag-Erling Smørgrav ksw.ksw_flags = xsw.xsw_flags; 5634418dbbdSDag-Erling Smørgrav print_swap(&ksw); 5644418dbbdSDag-Erling Smørgrav } 5654418dbbdSDag-Erling Smørgrav if (errno != ENOENT) 5664418dbbdSDag-Erling Smørgrav err(1, "sysctl()"); 5674418dbbdSDag-Erling Smørgrav print_swap_total(); 5684418dbbdSDag-Erling Smørgrav } 5694418dbbdSDag-Erling Smørgrav 5704418dbbdSDag-Erling Smørgrav static void 5714418dbbdSDag-Erling Smørgrav swapmode(void) 5724418dbbdSDag-Erling Smørgrav { 5734418dbbdSDag-Erling Smørgrav if (kd != NULL) 5744418dbbdSDag-Erling Smørgrav swapmode_kvm(); 5754418dbbdSDag-Erling Smørgrav else 5764418dbbdSDag-Erling Smørgrav swapmode_sysctl(); 5774418dbbdSDag-Erling Smørgrav } 578