1 /*- 2 * Copyright (c) 1980, 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#) Copyright (c) 1980, 1991, 1993, 1994 The Regents of the University of California. All rights reserved. 30 * @(#)pstat.c 8.16 (Berkeley) 5/9/95 31 * $FreeBSD: src/usr.sbin/pstat/pstat.c,v 1.49.2.5 2002/07/12 09:12:49 des Exp $ 32 */ 33 34 #include <sys/user.h> 35 #include <sys/kinfo.h> 36 #include <sys/param.h> 37 #include <sys/time.h> 38 #include <sys/vnode.h> 39 #include <sys/ucred.h> 40 #include <sys/file.h> 41 #include <vfs/ufs/quota.h> 42 #include <vfs/ufs/inode.h> 43 #include <sys/mount.h> 44 #include <sys/uio.h> 45 #include <sys/namei.h> 46 #include <vfs/union/union.h> 47 #include <sys/stat.h> 48 #include <nfs/rpcv2.h> 49 #include <nfs/nfsproto.h> 50 #include <nfs/nfs.h> 51 #include <nfs/nfsnode.h> 52 #include <sys/ioctl.h> 53 #include <sys/ioctl_compat.h> /* XXX NTTYDISC is too well hidden */ 54 #include <sys/tty.h> 55 #include <sys/conf.h> 56 #include <sys/blist.h> 57 58 #include <sys/sysctl.h> 59 60 #include <err.h> 61 #include <fcntl.h> 62 #include <kvm.h> 63 #include <limits.h> 64 #include <nlist.h> 65 #include <stdio.h> 66 #include <stdlib.h> 67 #include <string.h> 68 #include <unistd.h> 69 70 #ifdef USE_KCORE 71 # define KCORE_KINFO_WRAPPER 72 # include <kcore.h> 73 #else 74 # include <kinfo.h> 75 #endif 76 77 enum { 78 NL_MOUNTLIST, 79 NL_NUMVNODES, 80 NL_RC_TTY, 81 NL_NRC_TTY, 82 NL_CY_TTY, 83 NL_NCY_TTY, 84 NL_SI_TTY, 85 NL_SI_NPORTS, 86 }; 87 88 const size_t NL_LAST_MANDATORY = NL_NUMVNODES; 89 90 struct nlist nl[] = { 91 { "_mountlist", 0, 0, 0, 0 }, /* address of head of mount list. */ 92 { "_numvnodes", 0, 0, 0, 0 }, 93 { "_rc_tty", 0, 0, 0, 0 }, 94 { "_nrc_tty", 0, 0, 0, 0 }, 95 { "_cy_tty", 0, 0, 0, 0 }, 96 { "_ncy_tty", 0, 0, 0, 0 }, 97 { "_si__tty", 0, 0, 0, 0 }, 98 { "_si_Nports", 0, 0, 0, 0 }, 99 { "", 0, 0, 0, 0 } 100 }; 101 102 int usenumflag; 103 int totalflag; 104 int swapflag; 105 char *nlistf = NULL; 106 char *memf = NULL; 107 kvm_t *kd; 108 109 const char *usagestr; 110 111 struct { 112 int m_flag; 113 const char *m_name; 114 } mnt_flags[] = { 115 { MNT_RDONLY, "rdonly" }, 116 { MNT_SYNCHRONOUS, "sync" }, 117 { MNT_NOEXEC, "noexec" }, 118 { MNT_NOSUID, "nosuid" }, 119 { MNT_NODEV, "nodev" }, 120 { MNT_UNION, "union" }, 121 { MNT_ASYNC, "async" }, 122 { MNT_SUIDDIR, "suiddir" }, 123 { MNT_SOFTDEP, "softdep" }, 124 { MNT_NOSYMFOLLOW, "nosymfollow" }, 125 { MNT_NOATIME, "noatime" }, 126 { MNT_NOCLUSTERR, "noclusterread" }, 127 { MNT_NOCLUSTERW, "noclusterwrite" }, 128 { MNT_EXRDONLY, "exrdonly" }, 129 { MNT_EXPORTED, "exported" }, 130 { MNT_DEFEXPORTED, "defexported" }, 131 { MNT_EXPORTANON, "exportanon" }, 132 { MNT_EXKERB, "exkerb" }, 133 { MNT_EXPUBLIC, "public" }, 134 { MNT_LOCAL, "local" }, 135 { MNT_QUOTA, "quota" }, 136 { MNT_ROOTFS, "rootfs" }, 137 { MNT_USER, "user" }, 138 { MNT_IGNORE, "ignore" }, 139 { MNT_UPDATE, "update" }, 140 { MNT_DELEXPORT, "delexport" }, 141 { MNT_RELOAD, "reload" }, 142 { MNT_FORCE, "force" }, 143 { 0, NULL } 144 }; 145 146 147 #define SVAR(var) __STRING(var) /* to force expansion */ 148 #define KGET(idx, var) \ 149 KGET1(idx, &var, sizeof(var), SVAR(var)) 150 #define KGET1(idx, p, s, msg) \ 151 KGET2(nl[idx].n_value, p, s, msg) 152 #define KGET2(addr, p, s, msg) \ 153 if (kvm_read(kd, (u_long)(addr), p, s) != s) \ 154 warnx("cannot read %s: %s", msg, kvm_geterr(kd)) 155 #define KGETN(idx, var) \ 156 KGET1N(idx, &var, sizeof(var), SVAR(var)) 157 #define KGET1N(idx, p, s, msg) \ 158 KGET2N(nl[idx].n_value, p, s, msg) 159 #define KGET2N(addr, p, s, msg) \ 160 ((kvm_read(kd, (u_long)(addr), p, s) == s) ? 1 : 0) 161 #define KGETRET(addr, p, s, msg) \ 162 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \ 163 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 164 return (0); \ 165 } 166 167 void filemode(void); 168 int getfiles(char **, int *); 169 struct mount * 170 getmnt(struct mount *); 171 struct e_vnode * 172 kinfo_vnodes(int *); 173 struct e_vnode * 174 loadvnodes(int *); 175 void mount_print(struct mount *); 176 void nfs_header(void); 177 int nfs_print(struct vnode *); 178 void swapmode(void); 179 void ttymode(void); 180 void ttyprt(struct tty *, int); 181 void ttytype(struct tty *, const char *, int, int, int); 182 void ufs_header(void); 183 int ufs_print(struct vnode *); 184 void union_header(void); 185 int union_print(struct vnode *); 186 static void usage(void); 187 void vnode_header(void); 188 void vnode_print(struct vnode *, struct vnode *); 189 void vnodemode(void); 190 191 int 192 main(int argc, char **argv) 193 { 194 int ch, ret; 195 int fileflag, ttyflag, vnodeflag; 196 char buf[_POSIX2_LINE_MAX]; 197 const char *opts; 198 199 fileflag = swapflag = ttyflag = vnodeflag = 0; 200 201 /* We will behave like good old swapinfo if thus invoked */ 202 opts = strrchr(argv[0],'/'); 203 if (opts) 204 opts++; 205 else 206 opts = argv[0]; 207 if (!strcmp(opts,"swapinfo")) { 208 swapflag = 1; 209 opts = "kM:N:"; 210 usagestr = "swapinfo [-k] [-M core] [-N system]"; 211 } else { 212 opts = "TM:N:fiknstv"; 213 usagestr = "pstat [-Tfknst] [-M core] [-N system]"; 214 } 215 216 while ((ch = getopt(argc, argv, opts)) != -1) 217 switch (ch) { 218 case 'f': 219 fileflag = 1; 220 break; 221 case 'k': 222 if (setenv("BLOCKSIZE", "1K", 1) == -1) 223 warn("setenv: cannot set BLOCKSIZE=1K"); 224 break; 225 case 'M': 226 memf = optarg; 227 break; 228 case 'N': 229 nlistf = optarg; 230 break; 231 case 'n': 232 usenumflag = 1; 233 break; 234 case 's': 235 ++swapflag; 236 break; 237 case 'T': 238 totalflag = 1; 239 break; 240 case 't': 241 ttyflag = 1; 242 break; 243 case 'v': 244 case 'i': /* Backward compatibility. */ 245 errx(1, "vnode mode not supported"); 246 #if 0 247 vnodeflag = 1; 248 break; 249 #endif 250 default: 251 usage(); 252 } 253 argc -= optind; 254 argv += optind; 255 256 /* 257 * Discard setgid privileges if not the running kernel so that bad 258 * guys can't print interesting stuff from kernel memory. 259 */ 260 if (nlistf != NULL || memf != NULL) 261 setgid(getgid()); 262 263 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL) 264 errx(1, "kvm_openfiles: %s", buf); 265 #ifdef USE_KCORE 266 if (kcore_wrapper_open(nlistf, memf, buf)) 267 errx(1, "kcore_open: %s", buf); 268 #endif 269 if ((ret = kvm_nlist(kd, nl)) != 0) { 270 size_t i; 271 int quit = 0; 272 273 if (ret == -1) 274 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 275 for (i = 0; i < NL_LAST_MANDATORY; i++) { 276 if (!nl[i].n_value) { 277 quit = 1; 278 warnx("undefined symbol: %s", nl[i].n_name); 279 } 280 } 281 if (quit) 282 exit(1); 283 } 284 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag)) 285 usage(); 286 if (fileflag || totalflag) 287 filemode(); 288 if (vnodeflag) 289 vnodemode(); 290 if (ttyflag) 291 ttymode(); 292 if (swapflag || totalflag) 293 swapmode(); 294 exit (0); 295 } 296 297 static void 298 usage(void) 299 { 300 fprintf(stderr, "usage: %s\n", usagestr); 301 exit (1); 302 } 303 304 struct e_vnode { 305 struct vnode *avnode; 306 struct vnode vnode; 307 }; 308 309 void 310 vnodemode(void) 311 { 312 struct e_vnode *e_vnodebase, *endvnode, *evp; 313 struct vnode *vp; 314 struct mount *maddr, *mp = NULL; 315 int numvnodes; 316 317 e_vnodebase = loadvnodes(&numvnodes); 318 if (totalflag) { 319 printf("%7d vnodes\n", numvnodes); 320 return; 321 } 322 endvnode = e_vnodebase + numvnodes; 323 printf("%d active vnodes\n", numvnodes); 324 325 326 #define ST mp->mnt_stat 327 maddr = NULL; 328 for (evp = e_vnodebase; evp < endvnode; evp++) { 329 vp = &evp->vnode; 330 if (vp->v_mount != maddr) { 331 /* 332 * New filesystem 333 */ 334 if ((mp = getmnt(vp->v_mount)) == NULL) 335 continue; 336 maddr = vp->v_mount; 337 mount_print(mp); 338 vnode_header(); 339 if (!strcmp(ST.f_fstypename, "ufs") || 340 !strcmp(ST.f_fstypename, "mfs")) 341 ufs_header(); 342 else if (!strcmp(ST.f_fstypename, "nfs")) 343 nfs_header(); 344 else if (!strcmp(ST.f_fstypename, "union")) 345 union_header(); 346 printf("\n"); 347 } 348 vnode_print(evp->avnode, vp); 349 if (!strcmp(ST.f_fstypename, "ufs") || 350 !strcmp(ST.f_fstypename, "mfs")) 351 ufs_print(vp); 352 else if (!strcmp(ST.f_fstypename, "nfs")) 353 nfs_print(vp); 354 else if (!strcmp(ST.f_fstypename, "union")) 355 union_print(vp); 356 printf("\n"); 357 } 358 free(e_vnodebase); 359 } 360 361 void 362 vnode_header(void) 363 { 364 printf("ADDR TYP VFLAG USE HOLD"); 365 } 366 367 void 368 vnode_print(struct vnode *avnode, struct vnode *vp) 369 { 370 const char *type; 371 char flags[32]; 372 char *fp = flags; 373 int flag; 374 int refs; 375 376 /* 377 * set type 378 */ 379 switch (vp->v_type) { 380 case VNON: 381 type = "non"; break; 382 case VREG: 383 type = "reg"; break; 384 case VDIR: 385 type = "dir"; break; 386 case VBLK: 387 type = "blk"; break; 388 case VCHR: 389 type = "chr"; break; 390 case VLNK: 391 type = "lnk"; break; 392 case VSOCK: 393 type = "soc"; break; 394 case VFIFO: 395 type = "fif"; break; 396 case VBAD: 397 type = "bad"; break; 398 default: 399 type = "unk"; break; 400 } 401 /* 402 * gather flags 403 */ 404 flag = vp->v_flag; 405 if (flag & VROOT) 406 *fp++ = 'R'; 407 if (flag & VTEXT) 408 *fp++ = 'T'; 409 if (flag & VSYSTEM) 410 *fp++ = 'S'; 411 if (flag & VISTTY) 412 *fp++ = 't'; 413 #ifdef VXLOCK 414 if (flag & VXLOCK) 415 *fp++ = 'L'; 416 if (flag & VXWANT) 417 *fp++ = 'W'; 418 #endif 419 if (flag & VOBJBUF) 420 *fp++ = 'V'; 421 if (flag & (VAGE0 | VAGE1)) 422 *fp++ = 'a'; 423 #ifdef VDOOMED 424 if (flag & VDOOMED) 425 *fp++ = 'D'; 426 #endif 427 if (flag & VONWORKLST) 428 *fp++ = 'O'; 429 #ifdef VRECLAIMED 430 if (flag & VINACTIVE) 431 *fp++ = 'I'; 432 if (flag & VRECLAIMED) 433 *fp++ = 'X'; 434 #endif 435 436 if (flag == 0) 437 *fp++ = '-'; 438 *fp = '\0'; 439 440 /* 441 * Convert SYSREF ref counts into something more 442 * human readable for display. 443 */ 444 refs = vp->v_refcnt; 445 printf("%8lx %s %5s %08x %4d", 446 (u_long)(void *)avnode, type, flags, refs, vp->v_auxrefs); 447 } 448 449 void 450 ufs_header(void) 451 { 452 printf(" FILEID IFLAG RDEV|SZ"); 453 } 454 455 int 456 ufs_print(struct vnode *vp) 457 { 458 int flag; 459 struct inode inode, *ip = &inode; 460 char flagbuf[16], *flags = flagbuf; 461 char *name; 462 mode_t type; 463 464 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 465 flag = ip->i_flag; 466 if (flag & IN_ACCESS) 467 *flags++ = 'A'; 468 if (flag & IN_CHANGE) 469 *flags++ = 'C'; 470 if (flag & IN_UPDATE) 471 *flags++ = 'U'; 472 if (flag & IN_MODIFIED) 473 *flags++ = 'M'; 474 if (flag & IN_RENAME) 475 *flags++ = 'R'; 476 if (flag & IN_SHLOCK) 477 *flags++ = 'S'; 478 if (flag & IN_EXLOCK) 479 *flags++ = 'E'; 480 if (flag & IN_HASHED) 481 *flags++ = 'H'; 482 if (flag & IN_LAZYMOD) 483 *flags++ = 'L'; 484 if (flag == 0) 485 *flags++ = '-'; 486 *flags = '\0'; 487 488 printf(" %6ju %5s", (uintmax_t)ip->i_number, flagbuf); 489 type = ip->i_mode & S_IFMT; 490 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) 491 if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL)) 492 printf(" %2d,%-2d", 493 major(ip->i_rdev), minor(ip->i_rdev)); 494 else 495 printf(" %7s", name); 496 else 497 printf(" %7qd", ip->i_size); 498 return (0); 499 } 500 501 void 502 nfs_header(void) 503 { 504 printf(" FILEID NFLAG RDEV|SZ"); 505 } 506 507 int 508 nfs_print(struct vnode *vp) 509 { 510 struct nfsnode nfsnode, *np = &nfsnode; 511 char flagbuf[16], *flags = flagbuf; 512 int flag; 513 char *name; 514 mode_t type; 515 516 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode"); 517 flag = np->n_flag; 518 if (flag & NFLUSHWANT) 519 *flags++ = 'W'; 520 if (flag & NFLUSHINPROG) 521 *flags++ = 'P'; 522 if (flag & NLMODIFIED) 523 *flags++ = 'M'; 524 if (flag & NRMODIFIED) 525 *flags++ = 'R'; 526 if (flag & NWRITEERR) 527 *flags++ = 'E'; 528 if (flag & NQNFSEVICTED) 529 *flags++ = 'G'; 530 if (flag & NACC) 531 *flags++ = 'A'; 532 if (flag & NUPD) 533 *flags++ = 'U'; 534 if (flag & NCHG) 535 *flags++ = 'C'; 536 if (flag & NLOCKED) 537 *flags++ = 'L'; 538 if (flag & NWANTED) 539 *flags++ = 'w'; 540 if (flag == 0) 541 *flags++ = '-'; 542 *flags = '\0'; 543 544 #define VT np->n_vattr 545 printf(" %6ju %5s", (uintmax_t)VT.va_fileid, flagbuf); 546 type = VT.va_mode & S_IFMT; 547 if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode)) 548 if (usenumflag || ((name = devname((VT.va_rmajor << 8) | VT.va_rminor, type)) == NULL)) 549 printf(" %2d,%-2d", 550 VT.va_rmajor, VT.va_rminor); 551 else 552 printf(" %7s", name); 553 else 554 printf(" %7qd", np->n_size); 555 return (0); 556 } 557 558 void 559 union_header(void) 560 { 561 printf(" UPPER LOWER"); 562 } 563 564 int 565 union_print(struct vnode *vp) 566 { 567 struct union_node unode, *up = &unode; 568 569 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode"); 570 571 printf(" %8lx %8lx", (u_long)(void *)up->un_uppervp, 572 (u_long)(void *)up->un_lowervp); 573 return (0); 574 } 575 576 /* 577 * Given a pointer to a mount structure in kernel space, 578 * read it in and return a usable pointer to it. 579 */ 580 struct mount * 581 getmnt(struct mount *maddr) 582 { 583 static struct mtab { 584 struct mtab *next; 585 struct mount *maddr; 586 struct mount mount; 587 } *mhead = NULL; 588 struct mtab *mt; 589 590 for (mt = mhead; mt != NULL; mt = mt->next) 591 if (maddr == mt->maddr) 592 return (&mt->mount); 593 if ((mt = malloc(sizeof(struct mtab))) == NULL) 594 errx(1, "malloc"); 595 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table"); 596 mt->maddr = maddr; 597 mt->next = mhead; 598 mhead = mt; 599 return (&mt->mount); 600 } 601 602 void 603 mount_print(struct mount *mp) 604 { 605 int flags; 606 607 #define ST mp->mnt_stat 608 printf("*** MOUNT %s %s on %s", ST.f_fstypename, 609 ST.f_mntfromname, ST.f_mntonname); 610 if ((flags = mp->mnt_flag)) { 611 int i; 612 const char *sep = " ("; 613 614 for (i = 0; mnt_flags[i].m_flag; i++) { 615 if (flags & mnt_flags[i].m_flag) { 616 printf("%s%s", sep, mnt_flags[i].m_name); 617 flags &= ~mnt_flags[i].m_flag; 618 sep = ","; 619 } 620 } 621 if (flags) 622 printf("%sunknown_flags:%x", sep, flags); 623 printf(")"); 624 } 625 printf("\n"); 626 #undef ST 627 } 628 629 struct e_vnode * 630 loadvnodes(int *avnodes) 631 { 632 int mib[2]; 633 size_t copysize; 634 struct e_vnode *vnodebase; 635 636 if (memf != NULL) { 637 /* 638 * do it by hand 639 */ 640 return (kinfo_vnodes(avnodes)); 641 } 642 mib[0] = CTL_KERN; 643 mib[1] = KERN_VNODE; 644 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1) 645 err(1, "sysctl: KERN_VNODE"); 646 if ((vnodebase = malloc(copysize)) == NULL) 647 errx(1, "malloc"); 648 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1) 649 err(1, "sysctl: KERN_VNODE"); 650 if (copysize % sizeof(struct e_vnode)) 651 errx(1, "vnode size mismatch"); 652 *avnodes = copysize / sizeof(struct e_vnode); 653 654 return (vnodebase); 655 } 656 657 /* 658 * simulate what a running kernel does in in kinfo_vnode 659 */ 660 struct e_vnode * 661 kinfo_vnodes(int *avnodes) 662 { 663 struct mntlist mountlist; 664 struct mount *mp, mounth, *mp_next; 665 struct vnode *vp, vnode, *vp_next; 666 char *vbuf, *evbuf, *bp; 667 int num, numvnodes; 668 669 #define VPTRSZ sizeof(struct vnode *) 670 #define VNODESZ sizeof(struct vnode) 671 672 KGET(NL_NUMVNODES, numvnodes); 673 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL) 674 errx(1, "malloc"); 675 bp = vbuf; 676 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ); 677 KGET(NL_MOUNTLIST, mountlist); 678 for (num = 0, mp = TAILQ_FIRST(&mountlist); ; mp = mp_next) { 679 KGET2(mp, &mounth, sizeof(mounth), "mount entry"); 680 mp_next = TAILQ_NEXT(&mounth, mnt_list); 681 for (vp = TAILQ_FIRST(&mounth.mnt_nvnodelist); 682 vp != NULL; vp = vp_next) { 683 KGET2(vp, &vnode, sizeof(vnode), "vnode"); 684 vp_next = TAILQ_NEXT(&vnode, v_nmntvnodes); 685 if ((bp + VPTRSZ + VNODESZ) > evbuf) 686 /* XXX - should realloc */ 687 errx(1, "no more room for vnodes"); 688 memmove(bp, &vp, VPTRSZ); 689 bp += VPTRSZ; 690 memmove(bp, &vnode, VNODESZ); 691 bp += VNODESZ; 692 num++; 693 } 694 if (mp == TAILQ_LAST(&mountlist, mntlist)) 695 break; 696 } 697 *avnodes = num; 698 return ((struct e_vnode *)vbuf); 699 } 700 701 char hdr[] = 702 " LINE RAW CAN OUT IHIWT ILOWT OHWT LWT COL STATE SESS PGID DISC\n"; 703 int ttyspace = 128; 704 705 void 706 ttymode(void) 707 { 708 struct tty *tty; 709 struct tty ttyb[1000]; 710 int error; 711 size_t len, i; 712 713 printf(hdr); 714 len = sizeof(ttyb); 715 error = sysctlbyname("kern.ttys", &ttyb, &len, 0, 0); 716 if (!error) { 717 len /= sizeof(ttyb[0]); 718 for (i = 0; i < len; i++) { 719 ttyprt(&ttyb[i], 0); 720 } 721 } 722 if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL) 723 errx(1, "malloc"); 724 if (nl[NL_NRC_TTY].n_type != 0) 725 ttytype(tty, "rc", NL_RC_TTY, NL_NRC_TTY, 0); 726 if (nl[NL_NCY_TTY].n_type != 0) 727 ttytype(tty, "cy", NL_CY_TTY, NL_NCY_TTY, 0); 728 if (nl[NL_SI_NPORTS].n_type != 0) 729 ttytype(tty, "si", NL_SI_TTY, NL_SI_NPORTS, 1); 730 free(tty); 731 } 732 733 void 734 ttytype(struct tty *tty, const char *name, int type, int number, int indir) 735 { 736 struct tty *tp; 737 int ntty; 738 struct tty **ttyaddr; 739 740 if (tty == NULL) 741 return; 742 KGET(number, ntty); 743 printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines"); 744 if (ntty > ttyspace) { 745 ttyspace = ntty; 746 if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == NULL) 747 errx(1, "realloc"); 748 } 749 if (indir) { 750 KGET(type, ttyaddr); 751 KGET2(ttyaddr, tty, (ssize_t)(ntty * sizeof(struct tty)), 752 "tty structs"); 753 } else { 754 KGET1(type, tty, (ssize_t)(ntty * sizeof(struct tty)), 755 "tty structs"); 756 } 757 printf(hdr); 758 for (tp = tty; tp < &tty[ntty]; tp++) 759 ttyprt(tp, tp - tty); 760 } 761 762 struct { 763 int flag; 764 char val; 765 } ttystates[] = { 766 #ifdef TS_WOPEN 767 { TS_WOPEN, 'W'}, 768 #endif 769 { TS_ISOPEN, 'O'}, 770 { TS_CARR_ON, 'C'}, 771 #ifdef TS_CONNECTED 772 { TS_CONNECTED, 'c'}, 773 #endif 774 { TS_TIMEOUT, 'T'}, 775 { TS_FLUSH, 'F'}, 776 { TS_BUSY, 'B'}, 777 #ifdef TS_ASLEEP 778 { TS_ASLEEP, 'A'}, 779 #endif 780 #ifdef TS_SO_OLOWAT 781 { TS_SO_OLOWAT, 'A'}, 782 #endif 783 #ifdef TS_SO_OCOMPLETE 784 { TS_SO_OCOMPLETE, 'a'}, 785 #endif 786 { TS_XCLUDE, 'X'}, 787 { TS_TTSTOP, 'S'}, 788 #ifdef TS_CAR_OFLOW 789 { TS_CAR_OFLOW, 'm'}, 790 #endif 791 #ifdef TS_CTS_OFLOW 792 { TS_CTS_OFLOW, 'o'}, 793 #endif 794 #ifdef TS_DSR_OFLOW 795 { TS_DSR_OFLOW, 'd'}, 796 #endif 797 { TS_TBLOCK, 'K'}, 798 { TS_ASYNC, 'Y'}, 799 { TS_BKSL, 'D'}, 800 { TS_ERASE, 'E'}, 801 { TS_LNCH, 'L'}, 802 { TS_TYPEN, 'P'}, 803 { TS_CNTTB, 'N'}, 804 #ifdef TS_CAN_BYPASS_L_RINT 805 { TS_CAN_BYPASS_L_RINT, 'l'}, 806 #endif 807 #ifdef TS_SNOOP 808 { TS_SNOOP, 's'}, 809 #endif 810 #ifdef TS_ZOMBIE 811 { TS_ZOMBIE, 'Z'}, 812 #endif 813 { 0, '\0'}, 814 }; 815 816 void 817 ttyprt(struct tty *tp, int line) 818 { 819 int i, j; 820 pid_t pgid; 821 char *name, state[20]; 822 823 if (usenumflag || tp->t_dev == 0 || 824 (name = devname(tp->t_dev, S_IFCHR)) == NULL) 825 printf("%7d ", line); 826 else 827 printf("%7s ", name); 828 printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc); 829 printf("%3d %5d %5d %4d %3d %7d ", tp->t_outq.c_cc, 830 tp->t_ihiwat, tp->t_ilowat, tp->t_ohiwat, tp->t_olowat, 831 tp->t_column); 832 for (i = j = 0; ttystates[i].flag; i++) 833 if (tp->t_state&ttystates[i].flag) 834 state[j++] = ttystates[i].val; 835 if (j == 0) 836 state[j++] = '-'; 837 state[j] = '\0'; 838 printf("%-6s %8lx", state, (u_long)(void *)tp->t_session); 839 pgid = 0; 840 if (tp->t_pgrp != NULL) 841 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid"); 842 printf("%6d ", pgid); 843 switch (tp->t_line) { 844 case TTYDISC: 845 printf("term\n"); 846 break; 847 case NTTYDISC: 848 printf("ntty\n"); 849 break; 850 case SLIPDISC: 851 printf("slip\n"); 852 break; 853 case PPPDISC: 854 printf("ppp\n"); 855 break; 856 default: 857 printf("%d\n", tp->t_line); 858 break; 859 } 860 } 861 862 void 863 filemode(void) 864 { 865 struct kinfo_file *fp, *ofp; 866 size_t len; 867 char flagbuf[16], *fbp; 868 int maxfile, nfile; 869 static const char *dtypes[] = { "???", "inode", "socket" }; 870 871 if (kinfo_get_maxfiles(&maxfile)) 872 err(1, "kinfo_get_maxfiles"); 873 if (totalflag) { 874 if (kinfo_get_openfiles(&nfile)) 875 err(1, "kinfo_get_openfiles"); 876 printf("%3d/%3d files\n", nfile, maxfile); 877 return; 878 } 879 if (kinfo_get_files(&fp, &len)) 880 err(1, "kinfo_get_files"); 881 ofp = fp; 882 883 printf("%d/%d open files\n", len, maxfile); 884 printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n"); 885 for (; len-- > 0; fp++) { 886 if ((unsigned)fp->f_type > DTYPE_SOCKET) 887 continue; 888 printf("%p ", fp->f_file); 889 printf("%-8.8s", dtypes[fp->f_type]); 890 fbp = flagbuf; 891 if (fp->f_flag & FREAD) 892 *fbp++ = 'R'; 893 if (fp->f_flag & FWRITE) 894 *fbp++ = 'W'; 895 if (fp->f_flag & FAPPEND) 896 *fbp++ = 'A'; 897 #ifdef FSHLOCK /* currently gone */ 898 if (fp->f_flag & FSHLOCK) 899 *fbp++ = 'S'; 900 if (fp->f_flag & FEXLOCK) 901 *fbp++ = 'X'; 902 #endif 903 if (fp->f_flag & FASYNC) 904 *fbp++ = 'I'; 905 *fbp = '\0'; 906 printf("%6s %3d", flagbuf, fp->f_count); 907 printf(" %3d", fp->f_msgcount); 908 printf(" %8lx", (u_long)(void *)fp->f_data); 909 if (fp->f_offset < 0) 910 printf(" %qx\n", fp->f_offset); 911 else 912 printf(" %qd\n", fp->f_offset); 913 } 914 free(ofp); 915 } 916 917 /* 918 * swapmode is based on a program called swapinfo written 919 * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 920 */ 921 void 922 swapmode(void) 923 { 924 struct kvm_swap kswap[16]; 925 int i; 926 int n; 927 int pagesize = getpagesize(); 928 const char *header; 929 int hlen; 930 long blocksize; 931 932 n = kvm_getswapinfo( 933 kd, 934 kswap, 935 sizeof(kswap)/sizeof(kswap[0]), 936 ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX 937 ); 938 939 #define CONVERT(v) ((int)((quad_t)(v) * pagesize / blocksize)) 940 941 header = getbsize(&hlen, &blocksize); 942 if (totalflag == 0) { 943 printf("%-15s %*s %8s %8s %8s %s\n", 944 "Device", hlen, header, 945 "Used", "Avail", "Capacity", "Type"); 946 947 for (i = 0; i < n; ++i) { 948 printf( 949 "%-15s %*d ", 950 kswap[i].ksw_devname, 951 hlen, 952 CONVERT(kswap[i].ksw_total) 953 ); 954 printf( 955 "%8d %8d %5.0f%% %s\n", 956 CONVERT(kswap[i].ksw_used), 957 CONVERT(kswap[i].ksw_total - kswap[i].ksw_used), 958 (double)kswap[i].ksw_used * 100.0 / 959 (double)kswap[i].ksw_total, 960 (kswap[i].ksw_flags & SW_SEQUENTIAL) ? 961 "Sequential" : "Interleaved" 962 ); 963 } 964 } 965 966 if (totalflag) { 967 blocksize = 1024 * 1024; 968 969 printf( 970 "%dM/%dM swap space\n", 971 CONVERT(kswap[n].ksw_used), 972 CONVERT(kswap[n].ksw_total) 973 ); 974 } else if (n > 1) { 975 printf( 976 "%-15s %*d %8d %8d %5.0f%%\n", 977 "Total", 978 hlen, 979 CONVERT(kswap[n].ksw_total), 980 CONVERT(kswap[n].ksw_used), 981 CONVERT(kswap[n].ksw_total - kswap[n].ksw_used), 982 (double)kswap[n].ksw_used * 100.0 / 983 (double)kswap[n].ksw_total 984 ); 985 } 986 } 987