1 /* $NetBSD: pstat.c,v 1.50 1999/11/18 08:34:38 enami 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.50 1999/11/18 08:34:38 enami 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 #define _LKM 57 #include <sys/file.h> 58 #include <ufs/ufs/quota.h> 59 #include <ufs/ufs/inode.h> 60 #define NFS 61 #include <sys/mount.h> 62 #undef NFS 63 #include <sys/uio.h> 64 #include <sys/namei.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) \ 156 if (kvm_read(kd, (u_long)(addr), p, s) != s) \ 157 warnx("cannot read %s: %s", msg, kvm_geterr(kd)) 158 #define KGETRET(addr, p, s, msg) \ 159 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \ 160 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 161 return (0); \ 162 } 163 164 void filemode __P((void)); 165 int getfiles __P((char **, int *)); 166 struct mount * 167 getmnt __P((struct mount *)); 168 struct e_vnode * 169 kinfo_vnodes __P((int *)); 170 struct e_vnode * 171 loadvnodes __P((int *)); 172 int main __P((int, char **)); 173 void mount_print __P((struct mount *)); 174 void nfs_header __P((void)); 175 int nfs_print __P((struct vnode *)); 176 void ttymode __P((void)); 177 void ttyprt __P((struct tty *)); 178 void ufs_getflags __P((struct vnode *, struct inode *, char *)); 179 void ufs_header __P((void)); 180 int ufs_print __P((struct vnode *)); 181 int ext2fs_print __P((struct vnode *)); 182 void union_header __P((void)); 183 int union_print __P((struct vnode *)); 184 void usage __P((void)); 185 void vnode_header __P((void)); 186 void vnode_print __P((struct vnode *, struct vnode *)); 187 void vnodemode __P((void)); 188 189 int 190 main(argc, argv) 191 int argc; 192 char *argv[]; 193 { 194 extern char *optarg; 195 extern int optind; 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 struct e_vnode { 281 struct vnode *avnode; 282 struct vnode vnode; 283 }; 284 285 void 286 vnodemode() 287 { 288 struct e_vnode *e_vnodebase, *endvnode, *evp; 289 struct vnode *vp; 290 struct mount *maddr, *mp; 291 int numvnodes; 292 int (*vnode_fsprint) __P((struct vnode *)); /* per-fs data printer */ 293 294 mp = NULL; 295 e_vnodebase = loadvnodes(&numvnodes); 296 if (totalflag) { 297 (void)printf("%7d vnodes\n", numvnodes); 298 return; 299 } 300 endvnode = e_vnodebase + numvnodes; 301 (void)printf("%d active vnodes\n", numvnodes); 302 303 #define ST mp->mnt_stat 304 #define FSTYPE_IS(mp, name) \ 305 (strncmp((mp)->mnt_stat.f_fstypename, (name), MFSNAMELEN) == 0) 306 maddr = NULL; 307 vnode_fsprint = NULL; 308 for (evp = e_vnodebase; evp < endvnode; evp++) { 309 vp = &evp->vnode; 310 if (vp->v_mount != maddr) { 311 /* 312 * New filesystem 313 */ 314 if ((mp = getmnt(vp->v_mount)) == NULL) 315 continue; 316 maddr = vp->v_mount; 317 mount_print(mp); 318 vnode_header(); 319 if (FSTYPE_IS(mp, MOUNT_FFS) || 320 FSTYPE_IS(mp, MOUNT_MFS)) { 321 ufs_header(); 322 vnode_fsprint = ufs_print; 323 } else if (FSTYPE_IS(mp, MOUNT_NFS)) { 324 nfs_header(); 325 vnode_fsprint = nfs_print; 326 } else if (FSTYPE_IS(mp, MOUNT_EXT2FS)) { 327 ufs_header(); 328 vnode_fsprint = ext2fs_print; 329 } else if (FSTYPE_IS(mp, MOUNT_UNION)) { 330 union_header(); 331 vnode_fsprint = union_print; 332 } else 333 vnode_fsprint = NULL; 334 (void)printf("\n"); 335 } 336 vnode_print(evp->avnode, vp); 337 if (VTOI(vp) != NULL && vnode_fsprint != NULL) 338 (*vnode_fsprint)(vp); 339 (void)printf("\n"); 340 } 341 free(e_vnodebase); 342 } 343 344 void 345 vnode_header() 346 { 347 348 (void)printf("ADDR TYP VFLAG USE HOLD TAG"); 349 } 350 351 void 352 vnode_print(avnode, vp) 353 struct vnode *avnode; 354 struct vnode *vp; 355 { 356 char *type, flags[16]; 357 char *fp = flags; 358 int flag; 359 360 /* 361 * set type 362 */ 363 switch (vp->v_type) { 364 case VNON: 365 type = "non"; break; 366 case VREG: 367 type = "reg"; break; 368 case VDIR: 369 type = "dir"; break; 370 case VBLK: 371 type = "blk"; break; 372 case VCHR: 373 type = "chr"; break; 374 case VLNK: 375 type = "lnk"; break; 376 case VSOCK: 377 type = "soc"; break; 378 case VFIFO: 379 type = "fif"; break; 380 case VBAD: 381 type = "bad"; break; 382 default: 383 type = "unk"; break; 384 } 385 /* 386 * gather flags 387 */ 388 flag = vp->v_flag; 389 if (flag & VROOT) 390 *fp++ = 'R'; 391 if (flag & VTEXT) 392 *fp++ = 'T'; 393 if (flag & VSYSTEM) 394 *fp++ = 'S'; 395 if (flag & VISTTY) 396 *fp++ = 'I'; 397 if (flag & VXLOCK) 398 *fp++ = 'L'; 399 if (flag & VXWANT) 400 *fp++ = 'W'; 401 if (flag & VBWAIT) 402 *fp++ = 'B'; 403 if (flag & VALIASED) 404 *fp++ = 'A'; 405 if (flag & VDIROP) 406 *fp++ = 'D'; 407 if (flag & VLAYER) 408 *fp++ = 'Y'; 409 if (flag & VONWORKLST) 410 *fp++ = 'O'; 411 if (flag == 0) 412 *fp++ = '-'; 413 *fp = '\0'; 414 (void)printf("%8lx %s %5s %4ld %4ld %3d", 415 (long)avnode, type, flags, (long)vp->v_usecount, 416 (long)vp->v_holdcnt, vp->v_tag); 417 } 418 419 void 420 ufs_getflags(vp, ip, flags) 421 struct vnode *vp; 422 struct inode *ip; 423 char *flags; 424 { 425 int flag; 426 427 /* 428 * XXX need to to locking state. 429 */ 430 431 flag = ip->i_flag; 432 if (flag & IN_RENAME) 433 *flags++ = 'R'; 434 if (flag & IN_UPDATE) 435 *flags++ = 'U'; 436 if (flag & IN_ACCESS) 437 *flags++ = 'A'; 438 if (flag & IN_CHANGE) 439 *flags++ = 'C'; 440 if (flag & IN_MODIFIED) 441 *flags++ = 'M'; 442 if (flag & IN_SHLOCK) 443 *flags++ = 'S'; 444 if (flag & IN_EXLOCK) 445 *flags++ = 'E'; 446 if (flag == 0) 447 *flags++ = '-'; 448 *flags = '\0'; 449 450 } 451 452 void 453 ufs_header() 454 { 455 456 (void)printf(" FILEID IFLAG RDEV|SZ"); 457 } 458 459 int 460 ufs_print(vp) 461 struct vnode *vp; 462 { 463 struct inode inode, *ip = &inode; 464 char flagbuf[16]; 465 char *name; 466 mode_t type; 467 468 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 469 ufs_getflags(vp, ip, flagbuf); 470 (void)printf(" %6d %5s", ip->i_number, flagbuf); 471 type = ip->i_ffs_mode & S_IFMT; 472 if (S_ISCHR(ip->i_ffs_mode) || S_ISBLK(ip->i_ffs_mode)) 473 if (usenumflag || 474 ((name = devname(ip->i_ffs_rdev, type)) == NULL)) 475 (void)printf(" %2d,%-2d", 476 major(ip->i_ffs_rdev), minor(ip->i_ffs_rdev)); 477 else 478 (void)printf(" %7s", name); 479 else 480 (void)printf(" %7qd", (long long)ip->i_ffs_size); 481 return (0); 482 } 483 484 int 485 ext2fs_print(vp) 486 struct vnode *vp; 487 { 488 struct inode inode, *ip = &inode; 489 char flagbuf[16]; 490 char *name; 491 mode_t type; 492 493 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 494 ufs_getflags(vp, ip, flagbuf); 495 (void)printf(" %6d %5s", ip->i_number, flagbuf); 496 type = ip->i_e2fs_mode & S_IFMT; 497 if (S_ISCHR(ip->i_e2fs_mode) || S_ISBLK(ip->i_e2fs_mode)) 498 if (usenumflag || 499 ((name = devname(ip->i_e2fs_rdev, type)) == NULL)) 500 (void)printf(" %2d,%-2d", 501 major(ip->i_e2fs_rdev), minor(ip->i_e2fs_rdev)); 502 else 503 (void)printf(" %7s", name); 504 else 505 (void)printf(" %7u", (u_int)ip->i_e2fs_size); 506 return (0); 507 } 508 509 void 510 nfs_header() 511 { 512 513 (void)printf(" FILEID NFLAG RDEV|SZ"); 514 } 515 516 int 517 nfs_print(vp) 518 struct vnode *vp; 519 { 520 struct nfsnode nfsnode, *np = &nfsnode; 521 char flagbuf[16], *flags = flagbuf; 522 int flag; 523 struct vattr va; 524 char *name; 525 mode_t type; 526 527 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode"); 528 flag = np->n_flag; 529 if (flag & NFLUSHWANT) 530 *flags++ = 'W'; 531 if (flag & NFLUSHINPROG) 532 *flags++ = 'P'; 533 if (flag & NMODIFIED) 534 *flags++ = 'M'; 535 if (flag & NWRITEERR) 536 *flags++ = 'E'; 537 if (flag & NQNFSNONCACHE) 538 *flags++ = 'X'; 539 if (flag & NQNFSWRITE) 540 *flags++ = 'O'; 541 if (flag & NQNFSEVICTED) 542 *flags++ = 'G'; 543 if (flag == 0) 544 *flags++ = '-'; 545 *flags = '\0'; 546 547 KGETRET(np->n_vattr, &va, sizeof(va), "vnode attr"); 548 (void)printf(" %6ld %5s", (long)va.va_fileid, flagbuf); 549 type = va.va_mode & S_IFMT; 550 if (S_ISCHR(va.va_mode) || S_ISBLK(va.va_mode)) 551 if (usenumflag || ((name = devname(va.va_rdev, type)) == NULL)) 552 (void)printf(" %2d,%-2d", 553 major(va.va_rdev), minor(va.va_rdev)); 554 else 555 (void)printf(" %7s", name); 556 else 557 (void)printf(" %7qd", (long long)np->n_size); 558 return (0); 559 } 560 561 void 562 union_header() 563 { 564 565 (void)printf(" UPPER LOWER"); 566 } 567 568 int 569 union_print(vp) 570 struct vnode *vp; 571 { 572 struct union_node unode, *up = &unode; 573 574 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode"); 575 576 (void)printf(" %8lx %8lx", (long)up->un_uppervp, (long)up->un_lowervp); 577 return (0); 578 } 579 580 /* 581 * Given a pointer to a mount structure in kernel space, 582 * read it in and return a usable pointer to it. 583 */ 584 struct mount * 585 getmnt(maddr) 586 struct mount *maddr; 587 { 588 static struct mtab { 589 struct mtab *next; 590 struct mount *maddr; 591 struct mount mount; 592 } *mhead = NULL; 593 struct mtab *mt; 594 595 for (mt = mhead; mt != NULL; mt = mt->next) 596 if (maddr == mt->maddr) 597 return (&mt->mount); 598 if ((mt = malloc(sizeof(struct mtab))) == NULL) 599 err(1, "malloc"); 600 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table"); 601 mt->maddr = maddr; 602 mt->next = mhead; 603 mhead = mt; 604 return (&mt->mount); 605 } 606 607 void 608 mount_print(mp) 609 struct mount *mp; 610 { 611 int flags; 612 613 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename, 614 ST.f_mntfromname, ST.f_mntonname); 615 if ((flags = mp->mnt_flag) != 0) { 616 int i; 617 const char *sep = " ("; 618 619 for (i = 0; mnt_flags[i].m_flag; i++) { 620 if (flags & mnt_flags[i].m_flag) { 621 (void)printf("%s%s", sep, mnt_flags[i].m_name); 622 flags &= ~mnt_flags[i].m_flag; 623 sep = ","; 624 } 625 } 626 if (flags) 627 (void)printf("%sunknown_flags:%x", sep, flags); 628 (void)printf(")"); 629 } 630 (void)printf("\n"); 631 } 632 633 struct e_vnode * 634 loadvnodes(avnodes) 635 int *avnodes; 636 { 637 int mib[2]; 638 size_t copysize; 639 struct e_vnode *vnodebase; 640 641 if (memf != NULL) { 642 /* 643 * do it by hand 644 */ 645 return (kinfo_vnodes(avnodes)); 646 } 647 mib[0] = CTL_KERN; 648 mib[1] = KERN_VNODE; 649 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1) 650 err(1, "sysctl: KERN_VNODE"); 651 if ((vnodebase = malloc(copysize)) == NULL) 652 err(1, "malloc"); 653 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1) 654 err(1, "sysctl: KERN_VNODE"); 655 if (copysize % sizeof(struct e_vnode)) 656 errx(1, "vnode size mismatch"); 657 *avnodes = copysize / sizeof(struct e_vnode); 658 659 return (vnodebase); 660 } 661 662 /* 663 * simulate what a running kernel does in in kinfo_vnode 664 */ 665 struct e_vnode * 666 kinfo_vnodes(avnodes) 667 int *avnodes; 668 { 669 struct mntlist mountlist; 670 struct mount *mp, mount; 671 struct vnode *vp, vnode; 672 char *vbuf, *evbuf, *bp; 673 int num, numvnodes; 674 675 #define VPTRSZ sizeof(struct vnode *) 676 #define VNODESZ sizeof(struct vnode) 677 678 KGET(V_NUMV, numvnodes); 679 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL) 680 err(1, "malloc"); 681 bp = vbuf; 682 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ); 683 KGET(V_MOUNTLIST, mountlist); 684 for (num = 0, mp = mountlist.cqh_first;; 685 mp = mount.mnt_list.cqe_next) { 686 KGET2(mp, &mount, sizeof(mount), "mount entry"); 687 for (vp = mount.mnt_vnodelist.lh_first; 688 vp != NULL; vp = vnode.v_mntvnodes.le_next) { 689 KGET2(vp, &vnode, sizeof(vnode), "vnode"); 690 if ((bp + VPTRSZ + VNODESZ) > evbuf) 691 /* XXX - should realloc */ 692 errx(1, "no more room for vnodes"); 693 memmove(bp, &vp, VPTRSZ); 694 bp += VPTRSZ; 695 memmove(bp, &vnode, VNODESZ); 696 bp += VNODESZ; 697 num++; 698 } 699 if (mp == mountlist.cqh_last) 700 break; 701 } 702 *avnodes = num; 703 return ((struct e_vnode *)vbuf); 704 } 705 706 char hdr[]=" LINE RAW CAN OUT HWT LWT COL STATE SESS PGID DISC\n"; 707 int ttyspace = 128; 708 709 void 710 ttymode() 711 { 712 int ntty; 713 struct ttylist_head tty_head; 714 struct tty *tp, tty; 715 716 KGET(TTY_NTTY, ntty); 717 (void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s"); 718 KGET(TTY_TTYLIST, tty_head); 719 (void)printf(hdr); 720 for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) { 721 KGET2(tp, &tty, sizeof tty, "tty struct"); 722 ttyprt(&tty); 723 } 724 } 725 726 struct { 727 int flag; 728 char val; 729 } ttystates[] = { 730 { TS_ISOPEN, 'O'}, 731 { TS_DIALOUT, '>'}, 732 { TS_CARR_ON, 'C'}, 733 { TS_TIMEOUT, 'T'}, 734 { TS_FLUSH, 'F'}, 735 { TS_BUSY, 'B'}, 736 { TS_ASLEEP, 'A'}, 737 { TS_XCLUDE, 'X'}, 738 { TS_TTSTOP, 'S'}, 739 { TS_TBLOCK, 'K'}, 740 { TS_ASYNC, 'Y'}, 741 { TS_BKSL, 'D'}, 742 { TS_ERASE, 'E'}, 743 { TS_LNCH, 'L'}, 744 { TS_TYPEN, 'P'}, 745 { TS_CNTTB, 'N'}, 746 { 0, '\0'}, 747 }; 748 749 void 750 ttyprt(tp) 751 struct tty *tp; 752 { 753 int i, j; 754 pid_t pgid; 755 char *name, state[20]; 756 757 if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL) 758 (void)printf("0x%3x:%1x ", major(tp->t_dev), minor(tp->t_dev)); 759 else 760 (void)printf("%-7s ", name); 761 (void)printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc); 762 (void)printf("%3d %4d %3d %7d ", tp->t_outq.c_cc, 763 tp->t_hiwat, tp->t_lowat, tp->t_column); 764 for (i = j = 0; ttystates[i].flag; i++) 765 if (tp->t_state&ttystates[i].flag) 766 state[j++] = ttystates[i].val; 767 if (tp->t_wopen) 768 state[j++] = 'W'; 769 if (j == 0) 770 state[j++] = '-'; 771 state[j] = '\0'; 772 (void)printf("%-6s %8lX", state, (u_long)tp->t_session); 773 pgid = 0; 774 if (tp->t_pgrp != NULL) 775 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid"); 776 (void)printf("%6d ", pgid); 777 switch (tp->t_line) { 778 case TTYDISC: 779 (void)printf("term\n"); 780 break; 781 case TABLDISC: 782 (void)printf("tab\n"); 783 break; 784 case SLIPDISC: 785 (void)printf("slip\n"); 786 break; 787 case PPPDISC: 788 (void)printf("ppp\n"); 789 break; 790 case STRIPDISC: 791 (void)printf("strip\n"); 792 break; 793 default: 794 (void)printf("%d\n", tp->t_line); 795 break; 796 } 797 } 798 799 void 800 filemode() 801 { 802 struct file *fp; 803 struct file *addr; 804 char *buf, flagbuf[16], *fbp; 805 int len, maxfile, nfile; 806 static char *dtypes[] = { "???", "inode", "socket" }; 807 808 KGET(FNL_MAXFILE, maxfile); 809 if (totalflag) { 810 KGET(FNL_NFILE, nfile); 811 (void)printf("%3d/%3d files\n", nfile, maxfile); 812 return; 813 } 814 if (getfiles(&buf, &len) == -1) 815 return; 816 /* 817 * Getfiles returns in malloc'd memory a pointer to the first file 818 * structure, and then an array of file structs (whose addresses are 819 * derivable from the previous entry). 820 */ 821 addr = ((struct filelist *)buf)->lh_first; 822 fp = (struct file *)(buf + sizeof(struct filelist)); 823 nfile = (len - sizeof(struct filelist)) / sizeof(struct file); 824 825 (void)printf("%d/%d open files\n", nfile, maxfile); 826 (void)printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n"); 827 for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) { 828 if ((unsigned)fp->f_type > DTYPE_SOCKET) 829 continue; 830 (void)printf("%lx ", (long)addr); 831 (void)printf("%-8.8s", dtypes[fp->f_type]); 832 fbp = flagbuf; 833 if (fp->f_flag & FREAD) 834 *fbp++ = 'R'; 835 if (fp->f_flag & FWRITE) 836 *fbp++ = 'W'; 837 if (fp->f_flag & FAPPEND) 838 *fbp++ = 'A'; 839 #ifdef FSHLOCK /* currently gone */ 840 if (fp->f_flag & FSHLOCK) 841 *fbp++ = 'S'; 842 if (fp->f_flag & FEXLOCK) 843 *fbp++ = 'X'; 844 #endif 845 if (fp->f_flag & FASYNC) 846 *fbp++ = 'I'; 847 *fbp = '\0'; 848 (void)printf("%6s %3d", flagbuf, fp->f_count); 849 (void)printf(" %3d", fp->f_msgcount); 850 (void)printf(" %8.1lx", (long)fp->f_data); 851 if (fp->f_offset < 0) 852 (void)printf(" %qx\n", (long long)fp->f_offset); 853 else 854 (void)printf(" %qd\n", (long long)fp->f_offset); 855 } 856 free(buf); 857 } 858 859 int 860 getfiles(abuf, alen) 861 char **abuf; 862 int *alen; 863 { 864 size_t len; 865 int mib[2]; 866 char *buf; 867 868 /* 869 * XXX 870 * Add emulation of KINFO_FILE here. 871 */ 872 if (memf != NULL) 873 errx(1, "files on dead kernel, not implemented\n"); 874 875 mib[0] = CTL_KERN; 876 mib[1] = KERN_FILE; 877 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 878 warn("sysctl: KERN_FILE"); 879 return (-1); 880 } 881 if ((buf = malloc(len)) == NULL) 882 err(1, "malloc"); 883 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 884 warn("sysctl: KERN_FILE"); 885 return (-1); 886 } 887 *abuf = buf; 888 *alen = len; 889 return (0); 890 } 891 892 void 893 usage() 894 { 895 896 (void)fprintf(stderr, 897 "usage: pstat [-T|-f|-s|-t|-v] [-kn] [-M core] [-N system]\n"); 898 exit(1); 899 } 900