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 { TF_HOOK, 's' }, 316 317 { 0, '\0' }, 318 }; 319 320 static void 321 ttyprt(struct xtty *xt) 322 { 323 int i, j; 324 char *name; 325 326 if (xt->xt_size != sizeof *xt) 327 errx(1, "struct xtty size mismatch"); 328 if (usenumflag || xt->xt_dev == 0 || 329 (name = devname(xt->xt_dev, S_IFCHR)) == NULL) 330 printf("%5d,%4d ", major(xt->xt_dev), minor(xt->xt_dev)); 331 else 332 printf("%10s ", name); 333 printf("%5zu %4zu %4zu %4zu %5zu %4zu %4zu %5u %5d %5d ", 334 xt->xt_insize, xt->xt_incc, xt->xt_inlc, 335 (xt->xt_insize - xt->xt_inlow), xt->xt_outsize, 336 xt->xt_outcc, (xt->xt_outsize - xt->xt_outlow), 337 MIN(xt->xt_column, 99999), xt->xt_sid, xt->xt_pgid); 338 for (i = j = 0; ttystates[i].flag; i++) 339 if (xt->xt_flags & ttystates[i].flag) { 340 putchar(ttystates[i].val); 341 j++; 342 } 343 if (j == 0) 344 putchar('-'); 345 putchar('\n'); 346 } 347 348 static void 349 filemode(void) 350 { 351 struct xfile *fp; 352 char *buf, flagbuf[16], *fbp; 353 int maxf, openf; 354 size_t len; 355 static char *dtypes[] = { "???", "inode", "socket", "pipe", 356 "fifo", "kqueue", "crypto" }; 357 int i; 358 int wid; 359 360 if (kd != NULL) { 361 if (kvm_read(kd, nl[NL_MAXFILES].n_value, 362 &maxf, sizeof maxf) != sizeof maxf || 363 kvm_read(kd, nl[NL_NFILES].n_value, 364 &openf, sizeof openf) != sizeof openf) 365 errx(1, "kvm_read(): %s", kvm_geterr(kd)); 366 } else { 367 len = sizeof(int); 368 if (sysctlbyname("kern.maxfiles", &maxf, &len, 0, 0) == -1 || 369 sysctlbyname("kern.openfiles", &openf, &len, 0, 0) == -1) 370 err(1, "sysctlbyname()"); 371 } 372 373 if (totalflag) { 374 (void)printf("%3d/%3d files\n", openf, maxf); 375 return; 376 } 377 if (getfiles(&buf, &len) == -1) 378 return; 379 openf = len / sizeof *fp; 380 381 (void)printf("%d/%d open files\n", openf, maxf); 382 printf(sizeof(uintptr_t) == 4 ? fhdr32 : fhdr64); 383 wid = (int)sizeof(uintptr_t) * 2; 384 for (fp = (struct xfile *)buf, i = 0; i < openf; ++fp, ++i) { 385 if ((size_t)fp->xf_type >= sizeof(dtypes) / sizeof(dtypes[0])) 386 continue; 387 (void)printf("%*jx", wid, (uintmax_t)(uintptr_t)fp->xf_file); 388 (void)printf(" %-6.6s", dtypes[fp->xf_type]); 389 fbp = flagbuf; 390 if (fp->xf_flag & FREAD) 391 *fbp++ = 'R'; 392 if (fp->xf_flag & FWRITE) 393 *fbp++ = 'W'; 394 if (fp->xf_flag & FAPPEND) 395 *fbp++ = 'A'; 396 if (fp->xf_flag & FASYNC) 397 *fbp++ = 'I'; 398 *fbp = '\0'; 399 (void)printf(" %4s %3d", flagbuf, fp->xf_count); 400 (void)printf(" %3d", fp->xf_msgcount); 401 (void)printf(" %*jx", wid, (uintmax_t)(uintptr_t)fp->xf_data); 402 (void)printf(" %*jx\n", (int)sizeof(fp->xf_offset) * 2, 403 (uintmax_t)fp->xf_offset); 404 } 405 free(buf); 406 } 407 408 static int 409 getfiles(char **abuf, size_t *alen) 410 { 411 size_t len; 412 int mib[2]; 413 char *buf; 414 415 /* 416 * XXX 417 * Add emulation of KINFO_FILE here. 418 */ 419 if (kd != NULL) 420 errx(1, "files on dead kernel, not implemented"); 421 422 mib[0] = CTL_KERN; 423 mib[1] = KERN_FILE; 424 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 425 warn("sysctl: KERN_FILE"); 426 return (-1); 427 } 428 if ((buf = malloc(len)) == NULL) 429 errx(1, "malloc"); 430 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 431 warn("sysctl: KERN_FILE"); 432 return (-1); 433 } 434 *abuf = buf; 435 *alen = len; 436 return (0); 437 } 438 439 /* 440 * swapmode is based on a program called swapinfo written 441 * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 442 */ 443 444 #define CONVERT(v) ((int64_t)(v) * pagesize / blocksize) 445 static struct kvm_swap swtot; 446 static int nswdev; 447 448 static void 449 print_swap_header(void) 450 { 451 int hlen; 452 long blocksize; 453 const char *header; 454 455 header = getbsize(&hlen, &blocksize); 456 if (totalflag == 0) 457 (void)printf("%-15s %*s %8s %8s %8s\n", 458 "Device", hlen, header, 459 "Used", "Avail", "Capacity"); 460 } 461 462 static void 463 print_swap_line(const char *devname, intmax_t nblks, intmax_t bused, 464 intmax_t bavail, float bpercent) 465 { 466 char usedbuf[5]; 467 char availbuf[5]; 468 int hlen, pagesize; 469 long blocksize; 470 471 pagesize = getpagesize(); 472 getbsize(&hlen, &blocksize); 473 474 printf("%-15s %*jd ", devname, hlen, CONVERT(nblks)); 475 if (humanflag) { 476 humanize_number(usedbuf, sizeof(usedbuf), 477 CONVERT(blocksize * bused), "", 478 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 479 humanize_number(availbuf, sizeof(availbuf), 480 CONVERT(blocksize * bavail), "", 481 HN_AUTOSCALE, HN_B | HN_NOSPACE | HN_DECIMAL); 482 printf("%8s %8s %5.0f%%\n", usedbuf, availbuf, bpercent); 483 } else { 484 printf("%8jd %8jd %5.0f%%\n", (intmax_t)CONVERT(bused), 485 (intmax_t)CONVERT(bavail), bpercent); 486 } 487 } 488 489 static void 490 print_swap(struct kvm_swap *ksw) 491 { 492 493 swtot.ksw_total += ksw->ksw_total; 494 swtot.ksw_used += ksw->ksw_used; 495 ++nswdev; 496 if (totalflag == 0) 497 print_swap_line(ksw->ksw_devname, ksw->ksw_total, 498 ksw->ksw_used, ksw->ksw_total - ksw->ksw_used, 499 (ksw->ksw_used * 100.0) / ksw->ksw_total); 500 } 501 502 static void 503 print_swap_total(void) 504 { 505 int hlen, pagesize; 506 long blocksize; 507 508 pagesize = getpagesize(); 509 getbsize(&hlen, &blocksize); 510 if (totalflag) { 511 blocksize = 1024 * 1024; 512 (void)printf("%jdM/%jdM swap space\n", 513 CONVERT(swtot.ksw_used), CONVERT(swtot.ksw_total)); 514 } else if (nswdev > 1) { 515 print_swap_line("Total", swtot.ksw_total, swtot.ksw_used, 516 swtot.ksw_total - swtot.ksw_used, 517 (swtot.ksw_used * 100.0) / swtot.ksw_total); 518 } 519 } 520 521 static void 522 swapmode_kvm(void) 523 { 524 struct kvm_swap kswap[16]; 525 int i, n; 526 527 n = kvm_getswapinfo(kd, kswap, sizeof kswap / sizeof kswap[0], 528 SWIF_DEV_PREFIX); 529 530 print_swap_header(); 531 for (i = 0; i < n; ++i) 532 print_swap(&kswap[i]); 533 print_swap_total(); 534 } 535 536 static void 537 swapmode_sysctl(void) 538 { 539 struct kvm_swap ksw; 540 struct xswdev xsw; 541 size_t mibsize, size; 542 int mib[16], n; 543 544 print_swap_header(); 545 mibsize = sizeof mib / sizeof mib[0]; 546 if (sysctlnametomib("vm.swap_info", mib, &mibsize) == -1) 547 err(1, "sysctlnametomib()"); 548 for (n = 0; ; ++n) { 549 mib[mibsize] = n; 550 size = sizeof xsw; 551 if (sysctl(mib, mibsize + 1, &xsw, &size, NULL, 0) == -1) 552 break; 553 if (xsw.xsw_version != XSWDEV_VERSION) 554 errx(1, "xswdev version mismatch"); 555 if (xsw.xsw_dev == NODEV) 556 snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname, 557 "<NFSfile>"); 558 else 559 snprintf(ksw.ksw_devname, sizeof ksw.ksw_devname, 560 "/dev/%s", devname(xsw.xsw_dev, S_IFCHR)); 561 ksw.ksw_used = xsw.xsw_used; 562 ksw.ksw_total = xsw.xsw_nblks; 563 ksw.ksw_flags = xsw.xsw_flags; 564 print_swap(&ksw); 565 } 566 if (errno != ENOENT) 567 err(1, "sysctl()"); 568 print_swap_total(); 569 } 570 571 static void 572 swapmode(void) 573 { 574 if (kd != NULL) 575 swapmode_kvm(); 576 else 577 swapmode_sysctl(); 578 } 579