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