1 /*- 2 * Copyright (c) 1980, 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 2002 Networks Associates Technologies, Inc. 5 * All rights reserved. 6 * 7 * Portions of this software were developed for the FreeBSD Project by 8 * ThinkSec AS and NAI Labs, the Security Research Division of Network 9 * Associates, Inc. under DARPA/SPAWAR contract N66001-01-C-8035 10 * ("CBOSS"), as part of the DARPA CHATS research program. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #if 0 38 #ifndef lint 39 static const char copyright[] = 40 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 41 The Regents of the University of California. All rights reserved.\n"; 42 #endif /* not lint */ 43 44 #ifndef lint 45 static char sccsid[] = "@(#)pstat.c 8.16 (Berkeley) 5/9/95"; 46 #endif /* not lint */ 47 #endif 48 #include <sys/cdefs.h> 49 __FBSDID("$FreeBSD$"); 50 51 #include <sys/param.h> 52 #include <sys/time.h> 53 #include <sys/file.h> 54 #include <sys/stat.h> 55 #include <sys/stdint.h> 56 #include <sys/ioctl.h> 57 #include <sys/tty.h> 58 #include <sys/blist.h> 59 60 #include <sys/sysctl.h> 61 #include <vm/vm_param.h> 62 63 #include <err.h> 64 #include <errno.h> 65 #include <fcntl.h> 66 #include <kvm.h> 67 #include <libutil.h> 68 #include <limits.h> 69 #include <nlist.h> 70 #include <stdio.h> 71 #include <stdlib.h> 72 #include <string.h> 73 #include <unistd.h> 74 75 enum { 76 NL_CONSTTY, 77 NL_MAXFILES, 78 NL_NFILES, 79 NL_TTY_LIST 80 }; 81 82 static struct nlist nl[] = { 83 { .n_name = "_constty" }, 84 { .n_name = "_maxfiles" }, 85 { .n_name = "_openfiles" }, 86 { .n_name = "_tty_list" }, 87 { .n_name = "" } 88 }; 89 90 static int humanflag; 91 static int usenumflag; 92 static int totalflag; 93 static int swapflag; 94 static char *nlistf; 95 static char *memf; 96 static kvm_t *kd; 97 98 static char *usagestr; 99 100 static void filemode(void); 101 static int getfiles(char **, size_t *); 102 static void swapmode(void); 103 static void ttymode(void); 104 static void ttyprt(struct xtty *); 105 static void usage(void); 106 107 int 108 main(int argc, char *argv[]) 109 { 110 int ch, i, quit, ret; 111 int fileflag, ttyflag; 112 char buf[_POSIX2_LINE_MAX],*opts; 113 114 fileflag = swapflag = ttyflag = 0; 115 116 /* We will behave like good old swapinfo if thus invoked */ 117 opts = strrchr(argv[0], '/'); 118 if (opts) 119 opts++; 120 else 121 opts = argv[0]; 122 if (!strcmp(opts, "swapinfo")) { 123 swapflag = 1; 124 opts = "ghkmM:N:"; 125 usagestr = "swapinfo [-ghkm] [-M core [-N system]]"; 126 } else { 127 opts = "TM:N:fghkmnst"; 128 usagestr = "pstat [-Tfghkmnst] [-M core [-N system]]"; 129 } 130 131 while ((ch = getopt(argc, argv, opts)) != -1) 132 switch (ch) { 133 case 'f': 134 fileflag = 1; 135 break; 136 case 'g': 137 setenv("BLOCKSIZE", "1G", 1); 138 break; 139 case 'h': 140 humanflag = 1; 141 break; 142 case 'k': 143 setenv("BLOCKSIZE", "1K", 1); 144 break; 145 case 'm': 146 setenv("BLOCKSIZE", "1M", 1); 147 break; 148 case 'M': 149 memf = optarg; 150 break; 151 case 'N': 152 nlistf = optarg; 153 break; 154 case 'n': 155 usenumflag = 1; 156 break; 157 case 's': 158 ++swapflag; 159 break; 160 case 'T': 161 totalflag = 1; 162 break; 163 case 't': 164 ttyflag = 1; 165 break; 166 default: 167 usage(); 168 } 169 argc -= optind; 170 argv += optind; 171 172 if (memf != NULL) { 173 kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf); 174 if (kd == NULL) 175 errx(1, "kvm_openfiles: %s", buf); 176 if ((ret = kvm_nlist(kd, nl)) != 0) { 177 if (ret == -1) 178 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 179 quit = 0; 180 for (i = 0; nl[i].n_name[0] != '\0'; ++i) 181 if (nl[i].n_value == 0) { 182 quit = 1; 183 warnx("undefined symbol: %s", 184 nl[i].n_name); 185 } 186 if (quit) 187 exit(1); 188 } 189 } 190 if (!(fileflag | ttyflag | swapflag | totalflag)) 191 usage(); 192 if (fileflag || totalflag) 193 filemode(); 194 if (ttyflag) 195 ttymode(); 196 if (swapflag || totalflag) 197 swapmode(); 198 exit (0); 199 } 200 201 static void 202 usage(void) 203 { 204 fprintf(stderr, "usage: %s\n", usagestr); 205 exit (1); 206 } 207 208 static const char fhdr32[] = 209 " LOC TYPE FLG CNT MSG DATA OFFSET\n"; 210 /* c0000000 ------ RWAI 123 123 c0000000 1000000000000000 */ 211 212 static const char fhdr64[] = 213 " LOC TYPE FLG CNT MSG DATA OFFSET\n"; 214 /* c000000000000000 ------ RWAI 123 123 c000000000000000 1000000000000000 */ 215 216 static const char hdr[] = 217 " LINE INQ CAN LIN LOW OUTQ USE LOW COL SESS PGID STATE\n"; 218 219 static void 220 ttymode_kvm(void) 221 { 222 TAILQ_HEAD(, tty) tl; 223 struct tty *tp, tty; 224 struct xtty xt; 225 226 (void)printf("%s", hdr); 227 bzero(&xt, sizeof xt); 228 xt.xt_size = sizeof xt; 229 if (kvm_read(kd, nl[NL_TTY_LIST].n_value, &tl, sizeof tl) != sizeof tl) 230 errx(1, "kvm_read(): %s", kvm_geterr(kd)); 231 tp = TAILQ_FIRST(&tl); 232 while (tp != NULL) { 233 if (kvm_read(kd, (u_long)tp, &tty, sizeof tty) != sizeof tty) 234 errx(1, "kvm_read(): %s", kvm_geterr(kd)); 235 xt.xt_insize = tty.t_inq.ti_nblocks * TTYINQ_DATASIZE; 236 xt.xt_incc = tty.t_inq.ti_linestart - tty.t_inq.ti_begin; 237 xt.xt_inlc = tty.t_inq.ti_end - tty.t_inq.ti_linestart; 238 xt.xt_inlow = tty.t_inlow; 239 xt.xt_outsize = tty.t_outq.to_nblocks * TTYOUTQ_DATASIZE; 240 xt.xt_outcc = tty.t_outq.to_end - tty.t_outq.to_begin; 241 xt.xt_outlow = tty.t_outlow; 242 xt.xt_column = tty.t_column; 243 /* xt.xt_pgid = ... */ 244 /* xt.xt_sid = ... */ 245 xt.xt_flags = tty.t_flags; 246 xt.xt_dev = NODEV; 247 ttyprt(&xt); 248 tp = TAILQ_NEXT(&tty, t_list); 249 } 250 } 251 252 static void 253 ttymode_sysctl(void) 254 { 255 struct xtty *xt, *end; 256 void *xttys; 257 size_t len; 258 259 (void)printf("%s", hdr); 260 if ((xttys = malloc(len = sizeof *xt)) == NULL) 261 err(1, "malloc()"); 262 while (sysctlbyname("kern.ttys", xttys, &len, 0, 0) == -1) { 263 if (errno != ENOMEM) 264 err(1, "sysctlbyname()"); 265 len *= 2; 266 if ((xttys = realloc(xttys, len)) == NULL) 267 err(1, "realloc()"); 268 } 269 if (len > 0) { 270 end = (struct xtty *)((char *)xttys + len); 271 for (xt = xttys; xt < end; xt++) 272 ttyprt(xt); 273 } 274 } 275 276 static void 277 ttymode(void) 278 { 279 280 if (kd != NULL) 281 ttymode_kvm(); 282 else 283 ttymode_sysctl(); 284 } 285 286 static struct { 287 int flag; 288 char val; 289 } ttystates[] = { 290 #if 0 291 { TF_NOPREFIX, 'N' }, 292 #endif 293 { TF_INITLOCK, 'I' }, 294 { TF_CALLOUT, 'C' }, 295 296 /* Keep these together -> 'Oi' and 'Oo'. */ 297 { TF_OPENED, 'O' }, 298 { TF_OPENED_IN, 'i' }, 299 { TF_OPENED_OUT,'o' }, 300 301 { TF_GONE, 'G' }, 302 { TF_OPENCLOSE, 'B' }, 303 { TF_ASYNC, 'Y' }, 304 { TF_LITERAL, 'L' }, 305 306 /* Keep these together -> 'Hi' and 'Ho'. */ 307 { TF_HIWAT, 'H' }, 308 { TF_HIWAT_IN, 'i' }, 309 { TF_HIWAT_OUT, 'o' }, 310 311 { TF_STOPPED, 'S' }, 312 { TF_EXCLUDE, 'X' }, 313 { TF_BYPASS, 'l' }, 314 { TF_ZOMBIE, 'Z' }, 315 316 { 0, '\0' }, 317 }; 318 319 static void 320 ttyprt(struct xtty *xt) 321 { 322 int i, j; 323 char *name; 324 325 if (xt->xt_size != sizeof *xt) 326 errx(1, "struct xtty size mismatch"); 327 if (usenumflag || xt->xt_dev == 0 || 328 (name = devname(xt->xt_dev, S_IFCHR)) == NULL) 329 printf("%5d,%4d ", major(xt->xt_dev), minor(xt->xt_dev)); 330 else 331 printf("%10s ", name); 332 printf("%5zu %4zu %4zu %4zu %5zu %4zu %4zu %5u %5d %5d ", 333 xt->xt_insize, xt->xt_incc, xt->xt_inlc, 334 (xt->xt_insize - xt->xt_inlow), xt->xt_outsize, 335 xt->xt_outcc, (xt->xt_outsize - xt->xt_outlow), 336 xt->xt_column, xt->xt_sid, xt->xt_pgid); 337 for (i = j = 0; ttystates[i].flag; i++) 338 if (xt->xt_flags & ttystates[i].flag) { 339 putchar(ttystates[i].val); 340 j++; 341 } 342 if (j == 0) 343 putchar('-'); 344 putchar('\n'); 345 } 346 347 static void 348 filemode(void) 349 { 350 struct xfile *fp; 351 char *buf, flagbuf[16], *fbp; 352 int maxf, openf; 353 size_t len; 354 static char *dtypes[] = { "???", "inode", "socket", "pipe", 355 "fifo", "kqueue", "crypto" }; 356 int i; 357 int wid; 358 359 if (kd != NULL) { 360 if (kvm_read(kd, nl[NL_MAXFILES].n_value, 361 &maxf, sizeof maxf) != sizeof maxf || 362 kvm_read(kd, nl[NL_NFILES].n_value, 363 &openf, sizeof openf) != sizeof openf) 364 errx(1, "kvm_read(): %s", kvm_geterr(kd)); 365 } else { 366 len = sizeof(int); 367 if (sysctlbyname("kern.maxfiles", &maxf, &len, 0, 0) == -1 || 368 sysctlbyname("kern.openfiles", &openf, &len, 0, 0) == -1) 369 err(1, "sysctlbyname()"); 370 } 371 372 if (totalflag) { 373 (void)printf("%3d/%3d files\n", openf, maxf); 374 return; 375 } 376 if (getfiles(&buf, &len) == -1) 377 return; 378 openf = len / sizeof *fp; 379 380 (void)printf("%d/%d open files\n", openf, maxf); 381 printf(sizeof(uintptr_t) == 4 ? fhdr32 : fhdr64); 382 wid = (int)sizeof(uintptr_t) * 2; 383 for (fp = (struct xfile *)buf, i = 0; i < openf; ++fp, ++i) { 384 if ((size_t)fp->xf_type >= sizeof(dtypes) / sizeof(dtypes[0])) 385 continue; 386 (void)printf("%*jx", wid, (uintmax_t)(uintptr_t)fp->xf_file); 387 (void)printf(" %-6.6s", dtypes[fp->xf_type]); 388 fbp = flagbuf; 389 if (fp->xf_flag & FREAD) 390 *fbp++ = 'R'; 391 if (fp->xf_flag & FWRITE) 392 *fbp++ = 'W'; 393 if (fp->xf_flag & FAPPEND) 394 *fbp++ = 'A'; 395 if (fp->xf_flag & FASYNC) 396 *fbp++ = 'I'; 397 *fbp = '\0'; 398 (void)printf(" %4s %3d", flagbuf, fp->xf_count); 399 (void)printf(" %3d", fp->xf_msgcount); 400 (void)printf(" %*jx", wid, (uintmax_t)(uintptr_t)fp->xf_data); 401 (void)printf(" %*jx\n", (int)sizeof(fp->xf_offset) * 2, 402 (uintmax_t)fp->xf_offset); 403 } 404 free(buf); 405 } 406 407 static int 408 getfiles(char **abuf, size_t *alen) 409 { 410 size_t len; 411 int mib[2]; 412 char *buf; 413 414 /* 415 * XXX 416 * Add emulation of KINFO_FILE here. 417 */ 418 if (kd != NULL) 419 errx(1, "files on dead kernel, not implemented"); 420 421 mib[0] = CTL_KERN; 422 mib[1] = KERN_FILE; 423 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 424 warn("sysctl: KERN_FILE"); 425 return (-1); 426 } 427 if ((buf = malloc(len)) == NULL) 428 errx(1, "malloc"); 429 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 430 warn("sysctl: KERN_FILE"); 431 return (-1); 432 } 433 *abuf = buf; 434 *alen = len; 435 return (0); 436 } 437 438 /* 439 * swapmode is based on a program called swapinfo written 440 * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 441 */ 442 443 #define CONVERT(v) ((int64_t)(v) * pagesize / blocksize) 444 static struct kvm_swap swtot; 445 static int nswdev; 446 447 static void 448 print_swap_header(void) 449 { 450 int hlen; 451 long blocksize; 452 const char *header; 453 454 header = getbsize(&hlen, &blocksize); 455 if (totalflag == 0) 456 (void)printf("%-15s %*s %8s %8s %8s\n", 457 "Device", hlen, header, 458 "Used", "Avail", "Capacity"); 459 } 460 461 static void 462 print_swap_line(const char *devname, intmax_t nblks, intmax_t bused, 463 intmax_t bavail, float bpercent) 464 { 465 char usedbuf[5]; 466 char availbuf[5]; 467 int hlen, pagesize; 468 long blocksize; 469 470 pagesize = getpagesize(); 471 getbsize(&hlen, &blocksize); 472 473 printf("%-15s %*jd ", devname, hlen, CONVERT(nblks)); 474 if (humanflag) { 475 humanize_number(usedbuf, sizeof(usedbuf), 476 CONVERT(blocksize * bused), "", 477 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 478 humanize_number(availbuf, sizeof(availbuf), 479 CONVERT(blocksize * bavail), "", 480 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 481 printf("%8s %8s %5.0f%%\n", usedbuf, availbuf, bpercent); 482 } else { 483 printf("%8jd %8jd %5.0f%%\n", (intmax_t)CONVERT(bused), 484 (intmax_t)CONVERT(bavail), bpercent); 485 } 486 } 487 488 static void 489 print_swap(struct kvm_swap *ksw) 490 { 491 492 swtot.ksw_total += ksw->ksw_total; 493 swtot.ksw_used += ksw->ksw_used; 494 ++nswdev; 495 if (totalflag == 0) 496 print_swap_line(ksw->ksw_devname, ksw->ksw_total, 497 ksw->ksw_used, ksw->ksw_total - ksw->ksw_used, 498 (ksw->ksw_used * 100.0) / ksw->ksw_total); 499 } 500 501 static void 502 print_swap_total(void) 503 { 504 int hlen, pagesize; 505 long blocksize; 506 507 pagesize = getpagesize(); 508 getbsize(&hlen, &blocksize); 509 if (totalflag) { 510 blocksize = 1024 * 1024; 511 (void)printf("%jdM/%jdM swap space\n", 512 CONVERT(swtot.ksw_used), CONVERT(swtot.ksw_total)); 513 } else if (nswdev > 1) { 514 print_swap_line("Total", swtot.ksw_total, swtot.ksw_used, 515 swtot.ksw_total - swtot.ksw_used, 516 (swtot.ksw_used * 100.0) / swtot.ksw_total); 517 } 518 } 519 520 static void 521 swapmode_kvm(void) 522 { 523 struct kvm_swap kswap[16]; 524 int i, n; 525 526 n = kvm_getswapinfo(kd, kswap, sizeof kswap / sizeof kswap[0], 527 SWIF_DEV_PREFIX); 528 529 print_swap_header(); 530 for (i = 0; i < n; ++i) 531 print_swap(&kswap[i]); 532 print_swap_total(); 533 } 534 535 static void 536 swapmode_sysctl(void) 537 { 538 struct kvm_swap ksw; 539 struct xswdev xsw; 540 size_t mibsize, size; 541 int mib[16], n; 542 543 print_swap_header(); 544 mibsize = sizeof mib / sizeof mib[0]; 545 if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1) 546 err(1, "sysctlnametomib()"); 547 for (n = 0; ; ++n) { 548 mib[mibsize] = n; 549 size = sizeof xsw; 550 if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1) 551 break; 552 if (xsw.xsw_version != XSWDEV_VERSION) 553 errx(1, "xswdev version mismatch"); 554 if (xsw.xsw_dev == NODEV) 555 snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname, 556 "<NFSfile>"); 557 else 558 snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname, 559 "/dev/%s", devname(xsw.xsw_dev, S_IFCHR)); 560 ksw.ksw_used = xsw.xsw_used; 561 ksw.ksw_total = xsw.xsw_nblks; 562 ksw.ksw_flags = xsw.xsw_flags; 563 print_swap(&ksw); 564 } 565 if (errno != ENOENT) 566 err(1, "sysctl()"); 567 print_swap_total(); 568 } 569 570 static void 571 swapmode(void) 572 { 573 if (kd != NULL) 574 swapmode_kvm(); 575 else 576 swapmode_sysctl(); 577 } 578