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' }, 315a1215e37SEd Schouten { TF_HOOK, 's' }, 316bc093719SEd Schouten 317dea673e9SRodney W. Grimes { 0, '\0' }, 318dea673e9SRodney W. Grimes }; 319dea673e9SRodney W. Grimes 3201dcc9c32SDag-Erling Smørgrav static void 321333eaaedSDag-Erling Smørgrav ttyprt(struct xtty *xt) 322dea673e9SRodney W. Grimes { 3239c5cdfe0SPeter Wemm int i, j; 324bc093719SEd Schouten char *name; 325dea673e9SRodney W. Grimes 326333eaaedSDag-Erling Smørgrav if (xt->xt_size != sizeof *xt) 327333eaaedSDag-Erling Smørgrav errx(1, "struct xtty size mismatch"); 328333eaaedSDag-Erling Smørgrav if (usenumflag || xt->xt_dev == 0 || 329333eaaedSDag-Erling Smørgrav (name = devname(xt->xt_dev, S_IFCHR)) == NULL) 330bc093719SEd Schouten printf("%5d,%4d ", major(xt->xt_dev), minor(xt->xt_dev)); 331dea673e9SRodney W. Grimes else 332bc093719SEd Schouten printf("%10s ", name); 333bc093719SEd Schouten printf("%5zu %4zu %4zu %4zu %5zu %4zu %4zu %5u %5d %5d ", 334bc093719SEd Schouten xt->xt_insize, xt->xt_incc, xt->xt_inlc, 335bc093719SEd Schouten (xt->xt_insize - xt->xt_inlow), xt->xt_outsize, 336bc093719SEd Schouten xt->xt_outcc, (xt->xt_outsize - xt->xt_outlow), 337bc093719SEd Schouten xt->xt_column, xt->xt_sid, xt->xt_pgid); 338dea673e9SRodney W. Grimes for (i = j = 0; ttystates[i].flag; i++) 339bc093719SEd Schouten if (xt->xt_flags & ttystates[i].flag) { 340bc093719SEd Schouten putchar(ttystates[i].val); 341bc093719SEd Schouten j++; 342dea673e9SRodney W. Grimes } 343bc093719SEd Schouten if (j == 0) 344bc093719SEd Schouten putchar('-'); 345bc093719SEd Schouten putchar('\n'); 346dea673e9SRodney W. Grimes } 347dea673e9SRodney W. Grimes 3481dcc9c32SDag-Erling Smørgrav static void 349c9624363SDag-Erling Smørgrav filemode(void) 350dea673e9SRodney W. Grimes { 351eeebf53eSDag-Erling Smørgrav struct xfile *fp; 352dea673e9SRodney W. Grimes char *buf, flagbuf[16], *fbp; 3535f9ab4b0SDag-Erling Smørgrav int maxf, openf; 3544418dbbdSDag-Erling Smørgrav size_t len; 35546f7e0d2SRobert Drehmel static char *dtypes[] = { "???", "inode", "socket", "pipe", 35646f7e0d2SRobert Drehmel "fifo", "kqueue", "crypto" }; 357eeebf53eSDag-Erling Smørgrav int i; 35846f7e0d2SRobert Drehmel int wid; 359dea673e9SRodney W. Grimes 3604418dbbdSDag-Erling Smørgrav if (kd != NULL) { 3615f9ab4b0SDag-Erling Smørgrav if (kvm_read(kd, nl[NL_MAXFILES].n_value, 3625f9ab4b0SDag-Erling Smørgrav &maxf, sizeof maxf) != sizeof maxf || 3635f9ab4b0SDag-Erling Smørgrav kvm_read(kd, nl[NL_NFILES].n_value, 3645f9ab4b0SDag-Erling Smørgrav &openf, sizeof openf) != sizeof openf) 3655f9ab4b0SDag-Erling Smørgrav errx(1, "kvm_read(): %s", kvm_geterr(kd)); 3664418dbbdSDag-Erling Smørgrav } else { 3675f9ab4b0SDag-Erling Smørgrav len = sizeof(int); 3685f9ab4b0SDag-Erling Smørgrav if (sysctlbyname("kern.maxfiles", &maxf, &len, 0, 0) == -1 || 3695f9ab4b0SDag-Erling Smørgrav sysctlbyname("kern.openfiles", &openf, &len, 0, 0) == -1) 3704418dbbdSDag-Erling Smørgrav err(1, "sysctlbyname()"); 3714418dbbdSDag-Erling Smørgrav } 3724418dbbdSDag-Erling Smørgrav 373dea673e9SRodney W. Grimes if (totalflag) { 3745f9ab4b0SDag-Erling Smørgrav (void)printf("%3d/%3d files\n", openf, maxf); 375dea673e9SRodney W. Grimes return; 376dea673e9SRodney W. Grimes } 377dea673e9SRodney W. Grimes if (getfiles(&buf, &len) == -1) 378dea673e9SRodney W. Grimes return; 379eeebf53eSDag-Erling Smørgrav openf = len / sizeof *fp; 38046f7e0d2SRobert Drehmel 3815f9ab4b0SDag-Erling Smørgrav (void)printf("%d/%d open files\n", openf, maxf); 38246f7e0d2SRobert Drehmel printf(sizeof(uintptr_t) == 4 ? fhdr32 : fhdr64); 38346f7e0d2SRobert Drehmel wid = (int)sizeof(uintptr_t) * 2; 384eeebf53eSDag-Erling Smørgrav for (fp = (struct xfile *)buf, i = 0; i < openf; ++fp, ++i) { 38546f7e0d2SRobert Drehmel if ((size_t)fp->xf_type >= sizeof(dtypes) / sizeof(dtypes[0])) 386dea673e9SRodney W. Grimes continue; 38746f7e0d2SRobert Drehmel (void)printf("%*jx", wid, (uintmax_t)(uintptr_t)fp->xf_file); 38846f7e0d2SRobert Drehmel (void)printf(" %-6.6s", dtypes[fp->xf_type]); 389dea673e9SRodney W. Grimes fbp = flagbuf; 390eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FREAD) 391dea673e9SRodney W. Grimes *fbp++ = 'R'; 392eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FWRITE) 393dea673e9SRodney W. Grimes *fbp++ = 'W'; 394eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FAPPEND) 395dea673e9SRodney W. Grimes *fbp++ = 'A'; 396eeebf53eSDag-Erling Smørgrav if (fp->xf_flag & FASYNC) 397dea673e9SRodney W. Grimes *fbp++ = 'I'; 398dea673e9SRodney W. Grimes *fbp = '\0'; 39946f7e0d2SRobert Drehmel (void)printf(" %4s %3d", flagbuf, fp->xf_count); 400eeebf53eSDag-Erling Smørgrav (void)printf(" %3d", fp->xf_msgcount); 40146f7e0d2SRobert Drehmel (void)printf(" %*jx", wid, (uintmax_t)(uintptr_t)fp->xf_data); 40246f7e0d2SRobert Drehmel (void)printf(" %*jx\n", (int)sizeof(fp->xf_offset) * 2, 40346f7e0d2SRobert Drehmel (uintmax_t)fp->xf_offset); 404dea673e9SRodney W. Grimes } 405dea673e9SRodney W. Grimes free(buf); 406dea673e9SRodney W. Grimes } 407dea673e9SRodney W. Grimes 4081dcc9c32SDag-Erling Smørgrav static int 409333eaaedSDag-Erling Smørgrav getfiles(char **abuf, size_t *alen) 410dea673e9SRodney W. Grimes { 411dea673e9SRodney W. Grimes size_t len; 412dea673e9SRodney W. Grimes int mib[2]; 413dea673e9SRodney W. Grimes char *buf; 414dea673e9SRodney W. Grimes 415dea673e9SRodney W. Grimes /* 416dea673e9SRodney W. Grimes * XXX 417dea673e9SRodney W. Grimes * Add emulation of KINFO_FILE here. 418dea673e9SRodney W. Grimes */ 4194418dbbdSDag-Erling Smørgrav if (kd != NULL) 420d9961cfdSPhilippe Charnier errx(1, "files on dead kernel, not implemented"); 421dea673e9SRodney W. Grimes 422dea673e9SRodney W. Grimes mib[0] = CTL_KERN; 423dea673e9SRodney W. Grimes mib[1] = KERN_FILE; 424dea673e9SRodney W. Grimes if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 425dea673e9SRodney W. Grimes warn("sysctl: KERN_FILE"); 426dea673e9SRodney W. Grimes return (-1); 427dea673e9SRodney W. Grimes } 428dea673e9SRodney W. Grimes if ((buf = malloc(len)) == NULL) 429d9961cfdSPhilippe Charnier errx(1, "malloc"); 430dea673e9SRodney W. Grimes if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 431dea673e9SRodney W. Grimes warn("sysctl: KERN_FILE"); 432dea673e9SRodney W. Grimes return (-1); 433dea673e9SRodney W. Grimes } 434dea673e9SRodney W. Grimes *abuf = buf; 435dea673e9SRodney W. Grimes *alen = len; 436dea673e9SRodney W. Grimes return (0); 437dea673e9SRodney W. Grimes } 438dea673e9SRodney W. Grimes 439dea673e9SRodney W. Grimes /* 440dea673e9SRodney W. Grimes * swapmode is based on a program called swapinfo written 441dea673e9SRodney W. Grimes * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 442dea673e9SRodney W. Grimes */ 4434418dbbdSDag-Erling Smørgrav 4447257230fSGiorgos Keramidas #define CONVERT(v) ((int64_t)(v) * pagesize / blocksize) 4454418dbbdSDag-Erling Smørgrav static struct kvm_swap swtot; 4464418dbbdSDag-Erling Smørgrav static int nswdev; 4474418dbbdSDag-Erling Smørgrav 4481dcc9c32SDag-Erling Smørgrav static void 4494418dbbdSDag-Erling Smørgrav print_swap_header(void) 450dea673e9SRodney W. Grimes { 451c48205f3SMike Barcroft int hlen; 452782f8687SMatthew Dillon long blocksize; 4534418dbbdSDag-Erling Smørgrav const char *header; 454dea673e9SRodney W. Grimes 455dea673e9SRodney W. Grimes header = getbsize(&hlen, &blocksize); 4564418dbbdSDag-Erling Smørgrav if (totalflag == 0) 45746637267SPoul-Henning Kamp (void)printf("%-15s %*s %8s %8s %8s\n", 458c48205f3SMike Barcroft "Device", hlen, header, 45946637267SPoul-Henning Kamp "Used", "Avail", "Capacity"); 4604418dbbdSDag-Erling Smørgrav } 461eedc3436SMatthew Dillon 4624418dbbdSDag-Erling Smørgrav static void 4637257230fSGiorgos Keramidas print_swap_line(const char *devname, intmax_t nblks, intmax_t bused, 4647257230fSGiorgos Keramidas intmax_t bavail, float bpercent) 4654418dbbdSDag-Erling Smørgrav { 4667257230fSGiorgos Keramidas char usedbuf[5]; 4677257230fSGiorgos Keramidas char availbuf[5]; 468c48205f3SMike Barcroft int hlen, pagesize; 4694418dbbdSDag-Erling Smørgrav long blocksize; 4704418dbbdSDag-Erling Smørgrav 4714418dbbdSDag-Erling Smørgrav pagesize = getpagesize(); 4724418dbbdSDag-Erling Smørgrav getbsize(&hlen, &blocksize); 4737257230fSGiorgos Keramidas 4747257230fSGiorgos Keramidas printf("%-15s %*jd ", devname, hlen, CONVERT(nblks)); 4757257230fSGiorgos Keramidas if (humanflag) { 4767257230fSGiorgos Keramidas humanize_number(usedbuf, sizeof(usedbuf), 4777257230fSGiorgos Keramidas CONVERT(blocksize * bused), "", 4787257230fSGiorgos Keramidas HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 4797257230fSGiorgos Keramidas humanize_number(availbuf, sizeof(availbuf), 4807257230fSGiorgos Keramidas CONVERT(blocksize * bavail), "", 4817257230fSGiorgos Keramidas HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 4827257230fSGiorgos Keramidas printf("%8s %8s %5.0f%%\n", usedbuf, availbuf, bpercent); 4837257230fSGiorgos Keramidas } else { 4847257230fSGiorgos Keramidas printf("%8jd %8jd %5.0f%%\n", (intmax_t)CONVERT(bused), 4857257230fSGiorgos Keramidas (intmax_t)CONVERT(bavail), bpercent); 4867257230fSGiorgos Keramidas } 4877257230fSGiorgos Keramidas } 4887257230fSGiorgos Keramidas 4897257230fSGiorgos Keramidas static void 4907257230fSGiorgos Keramidas print_swap(struct kvm_swap *ksw) 4917257230fSGiorgos Keramidas { 4927257230fSGiorgos Keramidas 4934418dbbdSDag-Erling Smørgrav swtot.ksw_total += ksw->ksw_total; 4944418dbbdSDag-Erling Smørgrav swtot.ksw_used += ksw->ksw_used; 4954418dbbdSDag-Erling Smørgrav ++nswdev; 4967257230fSGiorgos Keramidas if (totalflag == 0) 4977257230fSGiorgos Keramidas print_swap_line(ksw->ksw_devname, ksw->ksw_total, 498a2e59d80SRobert Watson ksw->ksw_used, ksw->ksw_total - ksw->ksw_used, 49946637267SPoul-Henning Kamp (ksw->ksw_used * 100.0) / ksw->ksw_total); 500eedc3436SMatthew Dillon } 501eedc3436SMatthew Dillon 5024418dbbdSDag-Erling Smørgrav static void 5034418dbbdSDag-Erling Smørgrav print_swap_total(void) 5044418dbbdSDag-Erling Smørgrav { 505c48205f3SMike Barcroft int hlen, pagesize; 5064418dbbdSDag-Erling Smørgrav long blocksize; 5074418dbbdSDag-Erling Smørgrav 5084418dbbdSDag-Erling Smørgrav pagesize = getpagesize(); 5094418dbbdSDag-Erling Smørgrav getbsize(&hlen, &blocksize); 510dea673e9SRodney W. Grimes if (totalflag) { 511782f8687SMatthew Dillon blocksize = 1024 * 1024; 5127257230fSGiorgos Keramidas (void)printf("%jdM/%jdM swap space\n", 5134418dbbdSDag-Erling Smørgrav CONVERT(swtot.ksw_used), CONVERT(swtot.ksw_total)); 5144418dbbdSDag-Erling Smørgrav } else if (nswdev > 1) { 5157257230fSGiorgos Keramidas print_swap_line("Total", swtot.ksw_total, swtot.ksw_used, 5167257230fSGiorgos Keramidas swtot.ksw_total - swtot.ksw_used, 5174418dbbdSDag-Erling Smørgrav (swtot.ksw_used * 100.0) / swtot.ksw_total); 518dea673e9SRodney W. Grimes } 519dea673e9SRodney W. Grimes } 5204418dbbdSDag-Erling Smørgrav 5214418dbbdSDag-Erling Smørgrav static void 5224418dbbdSDag-Erling Smørgrav swapmode_kvm(void) 5234418dbbdSDag-Erling Smørgrav { 5244418dbbdSDag-Erling Smørgrav struct kvm_swap kswap[16]; 5254418dbbdSDag-Erling Smørgrav int i, n; 5264418dbbdSDag-Erling Smørgrav 5274418dbbdSDag-Erling Smørgrav n = kvm_getswapinfo(kd, kswap, sizeof kswap / sizeof kswap[0], 528799aba96SPoul-Henning Kamp SWIF_DEV_PREFIX); 5294418dbbdSDag-Erling Smørgrav 5304418dbbdSDag-Erling Smørgrav print_swap_header(); 5314418dbbdSDag-Erling Smørgrav for (i = 0; i < n; ++i) 5324418dbbdSDag-Erling Smørgrav print_swap(&kswap[i]); 5334418dbbdSDag-Erling Smørgrav print_swap_total(); 5344418dbbdSDag-Erling Smørgrav } 5354418dbbdSDag-Erling Smørgrav 5364418dbbdSDag-Erling Smørgrav static void 5374418dbbdSDag-Erling Smørgrav swapmode_sysctl(void) 5384418dbbdSDag-Erling Smørgrav { 5394418dbbdSDag-Erling Smørgrav struct kvm_swap ksw; 5404418dbbdSDag-Erling Smørgrav struct xswdev xsw; 5414418dbbdSDag-Erling Smørgrav size_t mibsize, size; 5424418dbbdSDag-Erling Smørgrav int mib[16], n; 5434418dbbdSDag-Erling Smørgrav 5444418dbbdSDag-Erling Smørgrav print_swap_header(); 5454418dbbdSDag-Erling Smørgrav mibsize = sizeof mib / sizeof mib[0]; 5464418dbbdSDag-Erling Smørgrav if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1) 5474418dbbdSDag-Erling Smørgrav err(1, "sysctlnametomib()"); 5484418dbbdSDag-Erling Smørgrav for (n = 0; ; ++n) { 5494418dbbdSDag-Erling Smørgrav mib[mibsize] = n; 5504418dbbdSDag-Erling Smørgrav size = sizeof xsw; 55116fc3635SMark Murray if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1) 5524418dbbdSDag-Erling Smørgrav break; 5534418dbbdSDag-Erling Smørgrav if (xsw.xsw_version != XSWDEV_VERSION) 5544418dbbdSDag-Erling Smørgrav errx(1, "xswdev version mismatch"); 5551afc333eSPoul-Henning Kamp if (xsw.xsw_dev == NODEV) 5561afc333eSPoul-Henning Kamp snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname, 5571afc333eSPoul-Henning Kamp "<NFSfile>"); 5581afc333eSPoul-Henning Kamp else 5594418dbbdSDag-Erling Smørgrav snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname, 5604418dbbdSDag-Erling Smørgrav "/dev/%s", devname(xsw.xsw_dev, S_IFCHR)); 5614418dbbdSDag-Erling Smørgrav ksw.ksw_used = xsw.xsw_used; 5624418dbbdSDag-Erling Smørgrav ksw.ksw_total = xsw.xsw_nblks; 5634418dbbdSDag-Erling Smørgrav ksw.ksw_flags = xsw.xsw_flags; 5644418dbbdSDag-Erling Smørgrav print_swap(&ksw); 5654418dbbdSDag-Erling Smørgrav } 5664418dbbdSDag-Erling Smørgrav if (errno != ENOENT) 5674418dbbdSDag-Erling Smørgrav err(1, "sysctl()"); 5684418dbbdSDag-Erling Smørgrav print_swap_total(); 5694418dbbdSDag-Erling Smørgrav } 5704418dbbdSDag-Erling Smørgrav 5714418dbbdSDag-Erling Smørgrav static void 5724418dbbdSDag-Erling Smørgrav swapmode(void) 5734418dbbdSDag-Erling Smørgrav { 5744418dbbdSDag-Erling Smørgrav if (kd != NULL) 5754418dbbdSDag-Erling Smørgrav swapmode_kvm(); 5764418dbbdSDag-Erling Smørgrav else 5774418dbbdSDag-Erling Smørgrav swapmode_sysctl(); 5784418dbbdSDag-Erling Smørgrav } 579