1 /* $NetBSD: pstat.c,v 1.61 2001/07/09 17:30:37 ad Exp $ */ 2 3 /*- 4 * Copyright (c) 1980, 1991, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)pstat.c 8.16 (Berkeley) 5/9/95"; 45 #else 46 __RCSID("$NetBSD: pstat.c,v 1.61 2001/07/09 17:30:37 ad Exp $"); 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/time.h> 52 #include <sys/vnode.h> 53 #include <sys/map.h> 54 #include <sys/ucred.h> 55 #define _KERNEL 56 #include <sys/file.h> 57 #include <ufs/ufs/quota.h> 58 #include <ufs/ufs/inode.h> 59 #define NFS 60 #include <sys/mount.h> 61 #undef NFS 62 #include <sys/uio.h> 63 #include <sys/namei.h> 64 #include <miscfs/genfs/layer.h> 65 #include <miscfs/union/union.h> 66 #undef _KERNEL 67 #include <sys/stat.h> 68 #include <nfs/nfsproto.h> 69 #include <nfs/rpcv2.h> 70 #include <nfs/nfs.h> 71 #include <nfs/nfsnode.h> 72 #include <sys/ioctl.h> 73 #include <sys/tty.h> 74 #include <sys/conf.h> 75 #include <sys/device.h> 76 77 #include <sys/sysctl.h> 78 79 #include <err.h> 80 #include <kvm.h> 81 #include <limits.h> 82 #include <nlist.h> 83 #include <stdio.h> 84 #include <stdlib.h> 85 #include <string.h> 86 #include <unistd.h> 87 88 #include "swapctl.h" 89 90 struct nlist nl[] = { 91 #define V_MOUNTLIST 0 92 { "_mountlist" }, /* address of head of mount list. */ 93 #define V_NUMV 1 94 { "_numvnodes" }, 95 #define FNL_NFILE 2 96 { "_nfiles" }, 97 #define FNL_MAXFILE 3 98 { "_maxfiles" }, 99 #define TTY_NTTY 4 100 { "_tty_count" }, 101 #define TTY_TTYLIST 5 102 { "_ttylist" }, 103 #define NLMANDATORY TTY_TTYLIST /* names up to here are mandatory */ 104 { "" } 105 }; 106 107 int usenumflag; 108 int totalflag; 109 int kflag; 110 char *nlistf = NULL; 111 char *memf = NULL; 112 kvm_t *kd; 113 114 struct { 115 int m_flag; 116 const char *m_name; 117 } mnt_flags[] = { 118 { MNT_RDONLY, "rdonly" }, 119 { MNT_SYNCHRONOUS, "sync" }, 120 { MNT_NOEXEC, "noexec" }, 121 { MNT_NOSUID, "nosuid" }, 122 { MNT_NODEV, "nodev" }, 123 { MNT_UNION, "union" }, 124 { MNT_ASYNC, "async" }, 125 { MNT_NOCOREDUMP, "nocoredump" }, 126 { MNT_NOATIME, "noatime" }, 127 { MNT_SYMPERM, "symperm" }, 128 { MNT_NODEVMTIME, "nodevmtime" }, 129 { MNT_SOFTDEP, "softdep" }, 130 { MNT_EXRDONLY, "exrdonly" }, 131 { MNT_EXPORTED, "exported" }, 132 { MNT_DEFEXPORTED, "defexported" }, 133 { MNT_EXPORTANON, "exportanon" }, 134 { MNT_EXKERB, "exkerb" }, 135 { MNT_EXNORESPORT, "exnoresport" }, 136 { MNT_EXPUBLIC, "expublic" }, 137 { MNT_LOCAL, "local" }, 138 { MNT_QUOTA, "quota" }, 139 { MNT_ROOTFS, "rootfs" }, 140 { MNT_UPDATE, "update" }, 141 { MNT_DELEXPORT, "delexport" }, 142 { MNT_RELOAD, "reload" }, 143 { MNT_FORCE, "force" }, 144 { MNT_GONE, "gone" }, 145 { MNT_UNMOUNT, "unmount" }, 146 { MNT_WANTRDWR, "wantrdwr" }, 147 { 0 } 148 }; 149 150 #define SVAR(var) __STRING(var) /* to force expansion */ 151 #define KGET(idx, var) \ 152 KGET1(idx, &var, sizeof(var), SVAR(var)) 153 #define KGET1(idx, p, s, msg) \ 154 KGET2(nl[idx].n_value, p, s, msg) 155 #define KGET2(addr, p, s, msg) do { \ 156 if (kvm_read(kd, (u_long)(addr), p, s) != s) \ 157 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 158 } while (/* CONSTCOND */0) 159 #define KGETRET(addr, p, s, msg) do { \ 160 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \ 161 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 162 return (0); \ 163 } \ 164 } while (/* CONSTCOND */0) 165 166 void filemode __P((void)); 167 int getfiles __P((char **, int *)); 168 struct mount * 169 getmnt __P((struct mount *)); 170 char * kinfo_vnodes __P((int *)); 171 void layer_header __P((void)); 172 int layer_print __P((struct vnode *)); 173 char * loadvnodes __P((int *)); 174 int main __P((int, char **)); 175 void mount_print __P((struct mount *)); 176 void nfs_header __P((void)); 177 int nfs_print __P((struct vnode *)); 178 void ttymode __P((void)); 179 void ttyprt __P((struct tty *)); 180 void ufs_getflags __P((struct vnode *, struct inode *, char *)); 181 void ufs_header __P((void)); 182 int ufs_print __P((struct vnode *)); 183 int ext2fs_print __P((struct vnode *)); 184 void union_header __P((void)); 185 int union_print __P((struct vnode *)); 186 void usage __P((void)); 187 void vnode_header __P((void)); 188 void vnode_print __P((struct vnode *, struct vnode *)); 189 void vnodemode __P((void)); 190 191 int 192 main(argc, argv) 193 int argc; 194 char *argv[]; 195 { 196 int ch, i, quit, ret; 197 int fileflag, swapflag, ttyflag, vnodeflag; 198 gid_t egid = getegid(); 199 char buf[_POSIX2_LINE_MAX]; 200 201 setegid(getgid()); 202 fileflag = swapflag = ttyflag = vnodeflag = 0; 203 while ((ch = getopt(argc, argv, "TM:N:fiknstv")) != -1) 204 switch (ch) { 205 case 'f': 206 fileflag = 1; 207 break; 208 case 'M': 209 memf = optarg; 210 break; 211 case 'N': 212 nlistf = optarg; 213 break; 214 case 'n': 215 usenumflag = 1; 216 break; 217 case 's': 218 swapflag = 1; 219 break; 220 case 'T': 221 totalflag = 1; 222 break; 223 case 't': 224 ttyflag = 1; 225 break; 226 case 'k': 227 kflag = 1; 228 break; 229 case 'v': 230 case 'i': /* Backward compatibility. */ 231 vnodeflag = 1; 232 break; 233 default: 234 usage(); 235 } 236 argc -= optind; 237 argv += optind; 238 239 /* 240 * Discard setgid privileges. If not the running kernel, we toss 241 * them away totally so that bad guys can't print interesting stuff 242 * from kernel memory, otherwise switch back to kmem for the 243 * duration of the kvm_openfiles() call. 244 */ 245 if (nlistf != NULL || memf != NULL) 246 (void)setgid(getgid()); 247 else 248 (void)setegid(egid); 249 250 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0) 251 errx(1, "kvm_openfiles: %s", buf); 252 253 /* get rid of it now anyway */ 254 if (nlistf == NULL && memf == NULL) 255 (void)setgid(getgid()); 256 if ((ret = kvm_nlist(kd, nl)) != 0) { 257 if (ret == -1) 258 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 259 for (i = quit = 0; i <= NLMANDATORY; i++) 260 if (!nl[i].n_value) { 261 quit = 1; 262 warnx("undefined symbol: %s", nl[i].n_name); 263 } 264 if (quit) 265 exit(1); 266 } 267 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag)) 268 usage(); 269 if (fileflag || totalflag) 270 filemode(); 271 if (vnodeflag || totalflag) 272 vnodemode(); 273 if (ttyflag) 274 ttymode(); 275 if (swapflag || totalflag) 276 list_swap(0, kflag, 0, totalflag, 1); 277 exit (0); 278 } 279 280 #define VPTRSZ sizeof(struct vnode *) 281 #define VNODESZ sizeof(struct vnode) 282 283 void 284 vnodemode() 285 { 286 char *e_vnodebase, *endvnode, *evp; 287 struct vnode *vp; 288 struct mount *maddr, *mp; 289 int numvnodes; 290 int (*vnode_fsprint) __P((struct vnode *)); /* per-fs data printer */ 291 292 mp = NULL; 293 e_vnodebase = loadvnodes(&numvnodes); 294 if (totalflag) { 295 (void)printf("%7d vnodes\n", numvnodes); 296 return; 297 } 298 endvnode = e_vnodebase + numvnodes * (VPTRSZ + VNODESZ); 299 (void)printf("%d active vnodes\n", numvnodes); 300 301 #define ST mp->mnt_stat 302 #define FSTYPE_IS(mp, name) \ 303 (strncmp((mp)->mnt_stat.f_fstypename, (name), MFSNAMELEN) == 0) 304 maddr = NULL; 305 vnode_fsprint = NULL; 306 for (evp = e_vnodebase; evp < endvnode; evp += VPTRSZ + VNODESZ) { 307 vp = (struct vnode *)(evp + VPTRSZ); 308 if (vp->v_mount != maddr) { 309 /* 310 * New filesystem 311 */ 312 if ((mp = getmnt(vp->v_mount)) == NULL) 313 continue; 314 maddr = vp->v_mount; 315 mount_print(mp); 316 vnode_header(); 317 if (FSTYPE_IS(mp, MOUNT_FFS) || 318 FSTYPE_IS(mp, MOUNT_MFS)) { 319 ufs_header(); 320 vnode_fsprint = ufs_print; 321 } else if (FSTYPE_IS(mp, MOUNT_NFS)) { 322 nfs_header(); 323 vnode_fsprint = nfs_print; 324 } else if (FSTYPE_IS(mp, MOUNT_EXT2FS)) { 325 ufs_header(); 326 vnode_fsprint = ext2fs_print; 327 } else if (FSTYPE_IS(mp, MOUNT_NULL) || 328 FSTYPE_IS(mp, MOUNT_OVERLAY) || 329 FSTYPE_IS(mp, MOUNT_UMAP)) { 330 layer_header(); 331 vnode_fsprint = layer_print; 332 } else if (FSTYPE_IS(mp, MOUNT_UNION)) { 333 union_header(); 334 vnode_fsprint = union_print; 335 } else 336 vnode_fsprint = NULL; 337 (void)printf("\n"); 338 } 339 vnode_print(*(struct vnode **)evp, vp); 340 if (VTOI(vp) != NULL && vnode_fsprint != NULL) 341 (*vnode_fsprint)(vp); 342 (void)printf("\n"); 343 } 344 free(e_vnodebase); 345 } 346 347 void 348 vnode_header() 349 { 350 351 (void)printf("ADDR TYP VFLAG USE HOLD TAG"); 352 } 353 354 void 355 vnode_print(avnode, vp) 356 struct vnode *avnode; 357 struct vnode *vp; 358 { 359 char *type, flags[16]; 360 char *fp = flags; 361 int flag; 362 363 /* 364 * set type 365 */ 366 switch (vp->v_type) { 367 case VNON: 368 type = "non"; break; 369 case VREG: 370 type = "reg"; break; 371 case VDIR: 372 type = "dir"; break; 373 case VBLK: 374 type = "blk"; break; 375 case VCHR: 376 type = "chr"; break; 377 case VLNK: 378 type = "lnk"; break; 379 case VSOCK: 380 type = "soc"; break; 381 case VFIFO: 382 type = "fif"; break; 383 case VBAD: 384 type = "bad"; break; 385 default: 386 type = "unk"; break; 387 } 388 /* 389 * gather flags 390 */ 391 flag = vp->v_flag; 392 if (flag & VROOT) 393 *fp++ = 'R'; 394 if (flag & VTEXT) 395 *fp++ = 'T'; 396 if (flag & VSYSTEM) 397 *fp++ = 'S'; 398 if (flag & VISTTY) 399 *fp++ = 'I'; 400 if (flag & VXLOCK) 401 *fp++ = 'L'; 402 if (flag & VXWANT) 403 *fp++ = 'W'; 404 if (flag & VBWAIT) 405 *fp++ = 'B'; 406 if (flag & VALIASED) 407 *fp++ = 'A'; 408 if (flag & VDIROP) 409 *fp++ = 'D'; 410 if (flag & VLAYER) 411 *fp++ = 'Y'; 412 if (flag & VONWORKLST) 413 *fp++ = 'O'; 414 if (flag == 0) 415 *fp++ = '-'; 416 *fp = '\0'; 417 (void)printf("%8lx %s %5s %4ld %4ld %3d", 418 (long)avnode, type, flags, (long)vp->v_usecount, 419 (long)vp->v_holdcnt, vp->v_tag); 420 } 421 422 void 423 ufs_getflags(vp, ip, flags) 424 struct vnode *vp; 425 struct inode *ip; 426 char *flags; 427 { 428 int flag; 429 430 /* 431 * XXX need to to locking state. 432 */ 433 434 flag = ip->i_flag; 435 if (flag & IN_ACCESS) 436 *flags++ = 'A'; 437 if (flag & IN_CHANGE) 438 *flags++ = 'C'; 439 if (flag & IN_UPDATE) 440 *flags++ = 'U'; 441 if (flag & IN_MODIFIED) 442 *flags++ = 'M'; 443 if (flag & IN_ACCESSED) 444 *flags++ = 'a'; 445 if (flag & IN_RENAME) 446 *flags++ = 'R'; 447 if (flag & IN_SHLOCK) 448 *flags++ = 'S'; 449 if (flag & IN_EXLOCK) 450 *flags++ = 'E'; 451 if (flag & IN_CLEANING) 452 *flags++ = 'c'; 453 if (flag & IN_ADIROP) 454 *flags++ = 'a'; 455 if (flag == 0) 456 *flags++ = '-'; 457 *flags = '\0'; 458 459 } 460 461 void 462 ufs_header() 463 { 464 465 (void)printf(" FILEID IFLAG RDEV|SZ"); 466 } 467 468 int 469 ufs_print(vp) 470 struct vnode *vp; 471 { 472 struct inode inode, *ip = &inode; 473 char flagbuf[16]; 474 char *name; 475 mode_t type; 476 477 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 478 ufs_getflags(vp, ip, flagbuf); 479 (void)printf(" %6d %5s", ip->i_number, flagbuf); 480 type = ip->i_ffs_mode & S_IFMT; 481 if (S_ISCHR(ip->i_ffs_mode) || S_ISBLK(ip->i_ffs_mode)) 482 if (usenumflag || 483 ((name = devname(ip->i_ffs_rdev, type)) == NULL)) 484 (void)printf(" %2d,%-2d", 485 major(ip->i_ffs_rdev), minor(ip->i_ffs_rdev)); 486 else 487 (void)printf(" %7s", name); 488 else 489 (void)printf(" %7lld", (long long)ip->i_ffs_size); 490 return (0); 491 } 492 493 int 494 ext2fs_print(vp) 495 struct vnode *vp; 496 { 497 struct inode inode, *ip = &inode; 498 char flagbuf[16]; 499 char *name; 500 mode_t type; 501 502 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 503 ufs_getflags(vp, ip, flagbuf); 504 (void)printf(" %6d %5s", ip->i_number, flagbuf); 505 type = ip->i_e2fs_mode & S_IFMT; 506 if (S_ISCHR(ip->i_e2fs_mode) || S_ISBLK(ip->i_e2fs_mode)) 507 if (usenumflag || 508 ((name = devname(ip->i_e2fs_rdev, type)) == NULL)) 509 (void)printf(" %2d,%-2d", 510 major(ip->i_e2fs_rdev), minor(ip->i_e2fs_rdev)); 511 else 512 (void)printf(" %7s", name); 513 else 514 (void)printf(" %7u", (u_int)ip->i_e2fs_size); 515 return (0); 516 } 517 518 void 519 nfs_header() 520 { 521 522 (void)printf(" FILEID NFLAG RDEV|SZ"); 523 } 524 525 int 526 nfs_print(vp) 527 struct vnode *vp; 528 { 529 struct nfsnode nfsnode, *np = &nfsnode; 530 char flagbuf[16], *flags = flagbuf; 531 int flag; 532 struct vattr va; 533 char *name; 534 mode_t type; 535 536 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode"); 537 flag = np->n_flag; 538 if (flag & NFLUSHWANT) 539 *flags++ = 'W'; 540 if (flag & NFLUSHINPROG) 541 *flags++ = 'P'; 542 if (flag & NMODIFIED) 543 *flags++ = 'M'; 544 if (flag & NWRITEERR) 545 *flags++ = 'E'; 546 if (flag & NQNFSNONCACHE) 547 *flags++ = 'X'; 548 if (flag & NQNFSWRITE) 549 *flags++ = 'O'; 550 if (flag & NQNFSEVICTED) 551 *flags++ = 'G'; 552 if (flag & NACC) 553 *flags++ = 'A'; 554 if (flag & NUPD) 555 *flags++ = 'U'; 556 if (flag & NCHG) 557 *flags++ = 'C'; 558 if (flag == 0) 559 *flags++ = '-'; 560 *flags = '\0'; 561 562 KGETRET(np->n_vattr, &va, sizeof(va), "vnode attr"); 563 (void)printf(" %6ld %5s", (long)va.va_fileid, flagbuf); 564 switch (va.va_type) { 565 case VCHR: 566 type = S_IFCHR; 567 goto device; 568 569 case VBLK: 570 type = S_IFBLK; 571 device: 572 if (usenumflag || ((name = devname(va.va_rdev, type)) == NULL)) 573 (void)printf(" %2d,%-2d", 574 major(va.va_rdev), minor(va.va_rdev)); 575 else 576 (void)printf(" %7s", name); 577 break; 578 default: 579 (void)printf(" %7lld", (long long)np->n_size); 580 break; 581 } 582 return (0); 583 } 584 585 void 586 layer_header() 587 { 588 589 (void)printf(" LOWER"); 590 } 591 592 int 593 layer_print(vp) 594 struct vnode *vp; 595 { 596 struct layer_node lnode, *lp = &lnode; 597 598 KGETRET(VTOLAYER(vp), &lnode, sizeof(lnode), "layer vnode"); 599 600 (void)printf(" %8lx", (long)lp->layer_lowervp); 601 return (0); 602 } 603 604 void 605 union_header() 606 { 607 608 (void)printf(" UPPER LOWER"); 609 } 610 611 int 612 union_print(vp) 613 struct vnode *vp; 614 { 615 struct union_node unode, *up = &unode; 616 617 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode"); 618 619 (void)printf(" %8lx %8lx", (long)up->un_uppervp, (long)up->un_lowervp); 620 return (0); 621 } 622 623 /* 624 * Given a pointer to a mount structure in kernel space, 625 * read it in and return a usable pointer to it. 626 */ 627 struct mount * 628 getmnt(maddr) 629 struct mount *maddr; 630 { 631 static struct mtab { 632 struct mtab *next; 633 struct mount *maddr; 634 struct mount mount; 635 } *mhead = NULL; 636 struct mtab *mt; 637 638 for (mt = mhead; mt != NULL; mt = mt->next) 639 if (maddr == mt->maddr) 640 return (&mt->mount); 641 if ((mt = malloc(sizeof(struct mtab))) == NULL) 642 err(1, "malloc"); 643 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table"); 644 mt->maddr = maddr; 645 mt->next = mhead; 646 mhead = mt; 647 return (&mt->mount); 648 } 649 650 void 651 mount_print(mp) 652 struct mount *mp; 653 { 654 int flags; 655 656 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename, 657 ST.f_mntfromname, ST.f_mntonname); 658 if ((flags = mp->mnt_flag) != 0) { 659 int i; 660 const char *sep = " ("; 661 662 for (i = 0; mnt_flags[i].m_flag; i++) { 663 if (flags & mnt_flags[i].m_flag) { 664 (void)printf("%s%s", sep, mnt_flags[i].m_name); 665 flags &= ~mnt_flags[i].m_flag; 666 sep = ","; 667 } 668 } 669 if (flags) 670 (void)printf("%sunknown_flags:%x", sep, flags); 671 (void)printf(")"); 672 } 673 (void)printf("\n"); 674 } 675 676 char * 677 loadvnodes(avnodes) 678 int *avnodes; 679 { 680 int mib[2]; 681 size_t copysize; 682 char *vnodebase; 683 684 if (memf != NULL) { 685 /* 686 * do it by hand 687 */ 688 return (kinfo_vnodes(avnodes)); 689 } 690 mib[0] = CTL_KERN; 691 mib[1] = KERN_VNODE; 692 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1) 693 err(1, "sysctl: KERN_VNODE"); 694 if ((vnodebase = malloc(copysize)) == NULL) 695 err(1, "malloc"); 696 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1) 697 err(1, "sysctl: KERN_VNODE"); 698 if (copysize % (VPTRSZ + VNODESZ)) 699 errx(1, "vnode size mismatch"); 700 *avnodes = copysize / (VPTRSZ + VNODESZ); 701 702 return (vnodebase); 703 } 704 705 /* 706 * simulate what a running kernel does in in kinfo_vnode 707 */ 708 char * 709 kinfo_vnodes(avnodes) 710 int *avnodes; 711 { 712 struct mntlist mountlist; 713 struct mount *mp, mount; 714 struct vnode *vp, vnode; 715 char *beg, *bp, *ep; 716 int numvnodes; 717 718 KGET(V_NUMV, numvnodes); 719 if ((bp = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL) 720 err(1, "malloc"); 721 beg = bp; 722 ep = bp + (numvnodes + 20) * (VPTRSZ + VNODESZ); 723 KGET(V_MOUNTLIST, mountlist); 724 for (mp = mountlist.cqh_first;; 725 mp = mount.mnt_list.cqe_next) { 726 KGET2(mp, &mount, sizeof(mount), "mount entry"); 727 for (vp = mount.mnt_vnodelist.lh_first; 728 vp != NULL; vp = vnode.v_mntvnodes.le_next) { 729 KGET2(vp, &vnode, sizeof(vnode), "vnode"); 730 if (bp + VPTRSZ + VNODESZ > ep) 731 /* XXX - should realloc */ 732 errx(1, "no more room for vnodes"); 733 memmove(bp, &vp, VPTRSZ); 734 bp += VPTRSZ; 735 memmove(bp, &vnode, VNODESZ); 736 bp += VNODESZ; 737 } 738 if (mp == mountlist.cqh_last) 739 break; 740 } 741 *avnodes = (bp - beg) / (VPTRSZ + VNODESZ); 742 return (beg); 743 } 744 745 const char hdr[]=" LINE RAW CAN OUT HWT LWT COL STATE SESS PGID DISC\n"; 746 int ttyspace = 128; 747 748 void 749 ttymode() 750 { 751 int ntty; 752 struct ttylist_head tty_head; 753 struct tty *tp, tty; 754 755 KGET(TTY_NTTY, ntty); 756 (void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s"); 757 KGET(TTY_TTYLIST, tty_head); 758 (void)printf(hdr); 759 for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) { 760 KGET2(tp, &tty, sizeof tty, "tty struct"); 761 ttyprt(&tty); 762 } 763 } 764 765 struct { 766 int flag; 767 char val; 768 } ttystates[] = { 769 { TS_ISOPEN, 'O'}, 770 { TS_DIALOUT, '>'}, 771 { TS_CARR_ON, 'C'}, 772 { TS_TIMEOUT, 'T'}, 773 { TS_FLUSH, 'F'}, 774 { TS_BUSY, 'B'}, 775 { TS_ASLEEP, 'A'}, 776 { TS_XCLUDE, 'X'}, 777 { TS_TTSTOP, 'S'}, 778 { TS_TBLOCK, 'K'}, 779 { TS_ASYNC, 'Y'}, 780 { TS_BKSL, 'D'}, 781 { TS_ERASE, 'E'}, 782 { TS_LNCH, 'L'}, 783 { TS_TYPEN, 'P'}, 784 { TS_CNTTB, 'N'}, 785 { 0, '\0'}, 786 }; 787 788 void 789 ttyprt(tp) 790 struct tty *tp; 791 { 792 int i, j; 793 pid_t pgid; 794 char *name, state[20], buffer; 795 struct linesw t_linesw; 796 797 if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL) 798 (void)printf("0x%3x:%1x ", major(tp->t_dev), minor(tp->t_dev)); 799 else 800 (void)printf("%-7s ", name); 801 (void)printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc); 802 (void)printf("%3d %4d %3d %7d ", tp->t_outq.c_cc, 803 tp->t_hiwat, tp->t_lowat, tp->t_column); 804 for (i = j = 0; ttystates[i].flag; i++) 805 if (tp->t_state&ttystates[i].flag) 806 state[j++] = ttystates[i].val; 807 if (tp->t_wopen) 808 state[j++] = 'W'; 809 if (j == 0) 810 state[j++] = '-'; 811 state[j] = '\0'; 812 (void)printf("%-6s %8lX", state, (u_long)tp->t_session); 813 pgid = 0; 814 if (tp->t_pgrp != NULL) 815 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid"); 816 (void)printf("%6d ", pgid); 817 KGET2(tp->t_linesw, &t_linesw, sizeof(t_linesw), 818 "line discipline switch table"); 819 name = t_linesw.l_name; 820 for (;;) { 821 KGET2(name, &buffer, sizeof(buffer), "line discipline name"); 822 if (buffer == '\0') 823 break; 824 (void)putchar(buffer); 825 name++; 826 } 827 (void)putchar('\n'); 828 } 829 830 void 831 filemode() 832 { 833 struct file *fp; 834 struct file *addr; 835 char *buf, flagbuf[16], *fbp; 836 int len, maxfile, nfile; 837 static char *dtypes[] = { "???", "inode", "socket" }; 838 839 KGET(FNL_MAXFILE, maxfile); 840 if (totalflag) { 841 KGET(FNL_NFILE, nfile); 842 (void)printf("%3d/%3d files\n", nfile, maxfile); 843 return; 844 } 845 if (getfiles(&buf, &len) == -1) 846 return; 847 /* 848 * Getfiles returns in malloc'd memory a pointer to the first file 849 * structure, and then an array of file structs (whose addresses are 850 * derivable from the previous entry). 851 */ 852 addr = ((struct filelist *)buf)->lh_first; 853 fp = (struct file *)(buf + sizeof(struct filelist)); 854 nfile = (len - sizeof(struct filelist)) / sizeof(struct file); 855 856 (void)printf("%d/%d open files\n", nfile, maxfile); 857 (void)printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n"); 858 for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) { 859 if ((unsigned)fp->f_type > DTYPE_SOCKET) 860 continue; 861 (void)printf("%lx ", (long)addr); 862 (void)printf("%-8.8s", dtypes[fp->f_type]); 863 fbp = flagbuf; 864 if (fp->f_flag & FREAD) 865 *fbp++ = 'R'; 866 if (fp->f_flag & FWRITE) 867 *fbp++ = 'W'; 868 if (fp->f_flag & FAPPEND) 869 *fbp++ = 'A'; 870 #ifdef FSHLOCK /* currently gone */ 871 if (fp->f_flag & FSHLOCK) 872 *fbp++ = 'S'; 873 if (fp->f_flag & FEXLOCK) 874 *fbp++ = 'X'; 875 #endif 876 if (fp->f_flag & FASYNC) 877 *fbp++ = 'I'; 878 *fbp = '\0'; 879 (void)printf("%6s %3d", flagbuf, fp->f_count); 880 (void)printf(" %3d", fp->f_msgcount); 881 (void)printf(" %8.1lx", (long)fp->f_data); 882 if (fp->f_offset < 0) 883 (void)printf(" %llx\n", (long long)fp->f_offset); 884 else 885 (void)printf(" %lld\n", (long long)fp->f_offset); 886 } 887 free(buf); 888 } 889 890 int 891 getfiles(abuf, alen) 892 char **abuf; 893 int *alen; 894 { 895 size_t len; 896 int mib[2]; 897 char *buf; 898 899 /* 900 * XXX 901 * Add emulation of KINFO_FILE here. 902 */ 903 if (memf != NULL) 904 errx(1, "files on dead kernel, not implemented"); 905 906 mib[0] = CTL_KERN; 907 mib[1] = KERN_FILE; 908 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 909 warn("sysctl: KERN_FILE"); 910 return (-1); 911 } 912 if ((buf = malloc(len)) == NULL) 913 err(1, "malloc"); 914 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 915 warn("sysctl: KERN_FILE"); 916 return (-1); 917 } 918 *abuf = buf; 919 *alen = len; 920 return (0); 921 } 922 923 void 924 usage() 925 { 926 927 (void)fprintf(stderr, 928 "usage: pstat [-T|-f|-s|-t|-v] [-kn] [-M core] [-N system]\n"); 929 exit(1); 930 } 931