1 /* $NetBSD: pstat.c,v 1.38 1997/10/20 18:12:56 drochner 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.38 1997/10/20 18:12:56 drochner 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/union/union.h> 65 #undef _KERNEL 66 #include <sys/stat.h> 67 #include <nfs/nfsproto.h> 68 #include <nfs/rpcv2.h> 69 #include <nfs/nfs.h> 70 #include <nfs/nfsnode.h> 71 #include <sys/ioctl.h> 72 #include <sys/tty.h> 73 #include <sys/conf.h> 74 #include <sys/device.h> 75 76 #include <sys/sysctl.h> 77 78 #include <err.h> 79 #include <kvm.h> 80 #include <limits.h> 81 #include <nlist.h> 82 #include <stdio.h> 83 #include <stdlib.h> 84 #include <string.h> 85 #include <unistd.h> 86 87 #include "swapctl.h" 88 89 struct nlist nl[] = { 90 #define V_MOUNTLIST 0 91 { "_mountlist" }, /* address of head of mount list. */ 92 #define V_NUMV 1 93 { "_numvnodes" }, 94 #define FNL_NFILE 2 95 {"_nfiles"}, 96 #define FNL_MAXFILE 3 97 {"_maxfiles"}, 98 #define TTY_NTTY 4 99 {"_tty_count"}, 100 #define TTY_TTYLIST 5 101 {"_ttylist"}, 102 #define NLMANDATORY TTY_TTYLIST /* names up to here are mandatory */ 103 { "" } 104 }; 105 106 int usenumflag; 107 int totalflag; 108 int kflag; 109 char *nlistf = NULL; 110 char *memf = NULL; 111 kvm_t *kd; 112 113 struct { 114 int m_flag; 115 const char *m_name; 116 } mnt_flags[] = { 117 { MNT_RDONLY, "rdonly" }, 118 { MNT_SYNCHRONOUS, "sync" }, 119 { MNT_NOEXEC, "noexec" }, 120 { MNT_NOSUID, "nosuid" }, 121 { MNT_NODEV, "nodev" }, 122 { MNT_UNION, "union" }, 123 { MNT_ASYNC, "async" }, 124 { MNT_NOCOREDUMP, "nocoredump" }, 125 { MNT_EXRDONLY, "exrdonly" }, 126 { MNT_EXPORTED, "exported" }, 127 { MNT_DEFEXPORTED, "defexported" }, 128 { MNT_EXPORTANON, "exportanon" }, 129 { MNT_EXKERB, "exkerb" }, 130 { MNT_LOCAL, "local" }, 131 { MNT_QUOTA, "quota" }, 132 { MNT_ROOTFS, "rootfs" }, 133 { MNT_UPDATE, "update" }, 134 { MNT_DELEXPORT, "delexport" }, 135 { MNT_RELOAD, "reload" }, 136 { MNT_FORCE, "force" }, 137 { MNT_MLOCK, "mlock" }, 138 { MNT_WAIT, "wait" }, 139 { MNT_MPBUSY, "mpbusy" }, 140 { MNT_MPWANT, "mpwant" }, 141 { MNT_UNMOUNT, "unmount" }, 142 { MNT_WANTRDWR, "wantrdwr" }, 143 { 0 } 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 KGETRET(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 return (0); \ 159 } 160 161 void filemode __P((void)); 162 int getfiles __P((char **, int *)); 163 struct mount * 164 getmnt __P((struct mount *)); 165 struct e_vnode * 166 kinfo_vnodes __P((int *)); 167 struct e_vnode * 168 loadvnodes __P((int *)); 169 int main __P((int, char **)); 170 void mount_print __P((struct mount *)); 171 void nfs_header __P((void)); 172 int nfs_print __P((struct vnode *)); 173 void ttymode __P((void)); 174 void ttyprt __P((struct tty *)); 175 void ufs_getflags __P((struct vnode *, struct inode *, char *)); 176 void ufs_header __P((void)); 177 int ufs_print __P((struct vnode *)); 178 int ext2fs_print __P((struct vnode *)); 179 void union_header __P((void)); 180 int union_print __P((struct vnode *)); 181 void usage __P((void)); 182 void vnode_header __P((void)); 183 void vnode_print __P((struct vnode *, struct vnode *)); 184 void vnodemode __P((void)); 185 186 int 187 main(argc, argv) 188 int argc; 189 char *argv[]; 190 { 191 extern char *optarg; 192 extern int optind; 193 int ch, i, quit, ret; 194 int fileflag, swapflag, ttyflag, vnodeflag; 195 char buf[_POSIX2_LINE_MAX]; 196 197 fileflag = swapflag = ttyflag = vnodeflag = 0; 198 while ((ch = getopt(argc, argv, "TM:N:fiknstv")) != -1) 199 switch (ch) { 200 case 'f': 201 fileflag = 1; 202 break; 203 case 'M': 204 memf = optarg; 205 break; 206 case 'N': 207 nlistf = optarg; 208 break; 209 case 'n': 210 usenumflag = 1; 211 break; 212 case 's': 213 swapflag = 1; 214 break; 215 case 'T': 216 totalflag = 1; 217 break; 218 case 't': 219 ttyflag = 1; 220 break; 221 case 'k': 222 kflag = 1; 223 break; 224 case 'v': 225 case 'i': /* Backward compatibility. */ 226 vnodeflag = 1; 227 break; 228 default: 229 usage(); 230 } 231 argc -= optind; 232 argv += optind; 233 234 /* 235 * Discard setgid privileges if not the running kernel so that bad 236 * guys can't print interesting stuff from kernel memory. 237 */ 238 if (nlistf != NULL || memf != NULL) 239 (void)setgid(getgid()); 240 241 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0) 242 errx(1, "kvm_openfiles: %s", buf); 243 if ((ret = kvm_nlist(kd, nl)) != 0) { 244 if (ret == -1) 245 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 246 for (i = quit = 0; i <= NLMANDATORY; i++) 247 if (!nl[i].n_value) { 248 quit = 1; 249 warnx("undefined symbol: %s", nl[i].n_name); 250 } 251 if (quit) 252 exit(1); 253 } 254 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag)) 255 usage(); 256 if (fileflag || totalflag) 257 filemode(); 258 if (vnodeflag || totalflag) 259 vnodemode(); 260 if (ttyflag) 261 ttymode(); 262 if (swapflag || totalflag) 263 list_swap(0, kflag, 0, totalflag, 1); 264 exit (0); 265 } 266 267 struct e_vnode { 268 struct vnode *avnode; 269 struct vnode vnode; 270 }; 271 272 void 273 vnodemode() 274 { 275 struct e_vnode *e_vnodebase, *endvnode, *evp; 276 struct vnode *vp; 277 struct mount *maddr, *mp; 278 int numvnodes; 279 280 mp = NULL; 281 e_vnodebase = loadvnodes(&numvnodes); 282 if (totalflag) { 283 (void)printf("%7d vnodes\n", numvnodes); 284 return; 285 } 286 endvnode = e_vnodebase + numvnodes; 287 (void)printf("%d active vnodes\n", numvnodes); 288 289 290 #define ST mp->mnt_stat 291 maddr = NULL; 292 for (evp = e_vnodebase; evp < endvnode; evp++) { 293 vp = &evp->vnode; 294 if (vp->v_mount != maddr) { 295 /* 296 * New filesystem 297 */ 298 if ((mp = getmnt(vp->v_mount)) == NULL) 299 continue; 300 maddr = vp->v_mount; 301 mount_print(mp); 302 vnode_header(); 303 if (!strncmp(ST.f_fstypename, MOUNT_FFS, MFSNAMELEN) || 304 !strncmp(ST.f_fstypename, MOUNT_MFS, MFSNAMELEN)) 305 ufs_header(); 306 else if (!strncmp(ST.f_fstypename, MOUNT_NFS, 307 MFSNAMELEN)) 308 nfs_header(); 309 else if (!strncmp(ST.f_fstypename, MOUNT_EXT2FS, 310 MFSNAMELEN)) 311 ufs_header(); 312 else if (!strcmp(ST.f_fstypename, "union")) 313 union_header(); 314 (void)printf("\n"); 315 } 316 vnode_print(evp->avnode, vp); 317 if (!strncmp(ST.f_fstypename, MOUNT_FFS, MFSNAMELEN) || 318 !strncmp(ST.f_fstypename, MOUNT_MFS, MFSNAMELEN)) { 319 ufs_print(vp); 320 } else if (!strncmp(ST.f_fstypename, MOUNT_NFS, MFSNAMELEN)) { 321 nfs_print(vp); 322 } else if (!strncmp(ST.f_fstypename, MOUNT_EXT2FS, MFSNAMELEN)) { 323 ext2fs_print(vp); 324 } 325 (void)printf("\n"); 326 } 327 free(e_vnodebase); 328 } 329 330 void 331 vnode_header() 332 { 333 (void)printf("ADDR TYP VFLAG USE HOLD"); 334 } 335 336 void 337 vnode_print(avnode, vp) 338 struct vnode *avnode; 339 struct vnode *vp; 340 { 341 char *type, flags[16]; 342 char *fp = flags; 343 int flag; 344 345 /* 346 * set type 347 */ 348 switch (vp->v_type) { 349 case VNON: 350 type = "non"; break; 351 case VREG: 352 type = "reg"; break; 353 case VDIR: 354 type = "dir"; break; 355 case VBLK: 356 type = "blk"; break; 357 case VCHR: 358 type = "chr"; break; 359 case VLNK: 360 type = "lnk"; break; 361 case VSOCK: 362 type = "soc"; break; 363 case VFIFO: 364 type = "fif"; break; 365 case VBAD: 366 type = "bad"; break; 367 default: 368 type = "unk"; break; 369 } 370 /* 371 * gather flags 372 */ 373 flag = vp->v_flag; 374 if (flag & VROOT) 375 *fp++ = 'R'; 376 if (flag & VTEXT) 377 *fp++ = 'T'; 378 if (flag & VSYSTEM) 379 *fp++ = 'S'; 380 if (flag & VISTTY) 381 *fp++ = 'I'; 382 if (flag & VXLOCK) 383 *fp++ = 'L'; 384 if (flag & VXWANT) 385 *fp++ = 'W'; 386 if (flag & VBWAIT) 387 *fp++ = 'B'; 388 if (flag & VALIASED) 389 *fp++ = 'A'; 390 if (flag & VDIROP) 391 *fp++ = 'D'; 392 if (flag == 0) 393 *fp++ = '-'; 394 *fp = '\0'; 395 (void)printf("%8lx %s %5s %4d %4ld", 396 (long)avnode, type, flags, vp->v_usecount, (long)vp->v_holdcnt); 397 } 398 399 void 400 ufs_getflags(vp, ip, flags) 401 struct vnode *vp; 402 struct inode *ip; 403 char *flags; 404 { 405 int flag; 406 407 flag = ip->i_flag; 408 if (flag & IN_LOCKED) 409 *flags++ = 'L'; 410 if (flag & IN_WANTED) 411 *flags++ = 'W'; 412 if (flag & IN_RENAME) 413 *flags++ = 'R'; 414 if (flag & IN_UPDATE) 415 *flags++ = 'U'; 416 if (flag & IN_ACCESS) 417 *flags++ = 'A'; 418 if (flag & IN_CHANGE) 419 *flags++ = 'C'; 420 if (flag & IN_MODIFIED) 421 *flags++ = 'M'; 422 if (flag & IN_SHLOCK) 423 *flags++ = 'S'; 424 if (flag & IN_EXLOCK) 425 *flags++ = 'E'; 426 if (flag & IN_LWAIT) 427 *flags++ = 'Z'; 428 if (flag == 0) 429 *flags++ = '-'; 430 *flags = '\0'; 431 432 } 433 434 void 435 ufs_header() 436 { 437 (void)printf(" FILEID IFLAG RDEV|SZ"); 438 } 439 440 int 441 ufs_print(vp) 442 struct vnode *vp; 443 { 444 struct inode inode, *ip = &inode; 445 char flagbuf[16]; 446 char *name; 447 mode_t type; 448 449 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 450 ufs_getflags(vp, ip, flagbuf); 451 (void)printf(" %6d %5s", ip->i_number, flagbuf); 452 type = ip->i_ffs_mode & S_IFMT; 453 if (S_ISCHR(ip->i_ffs_mode) || S_ISBLK(ip->i_ffs_mode)) 454 if (usenumflag || ((name = devname(ip->i_ffs_rdev, type)) == NULL)) 455 (void)printf(" %2d,%-2d", 456 major(ip->i_ffs_rdev), minor(ip->i_ffs_rdev)); 457 else 458 (void)printf(" %7s", name); 459 else 460 (void)printf(" %7qd", (long long)ip->i_ffs_size); 461 return (0); 462 } 463 464 int 465 ext2fs_print(vp) 466 struct vnode *vp; 467 { 468 struct inode inode, *ip = &inode; 469 char flagbuf[16]; 470 char *name; 471 mode_t type; 472 473 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 474 ufs_getflags(vp, ip, flagbuf); 475 (void)printf(" %6d %5s", ip->i_number, flagbuf); 476 type = ip->i_e2fs_mode & S_IFMT; 477 if (S_ISCHR(ip->i_e2fs_mode) || S_ISBLK(ip->i_e2fs_mode)) 478 if (usenumflag || ((name = devname(ip->i_din.e2fs_din.e2di_rdev, 479 type)) == NULL)) 480 (void)printf(" %2d,%-2d", 481 major(ip->i_din.e2fs_din.e2di_rdev), 482 minor(ip->i_din.e2fs_din.e2di_rdev)); 483 else 484 (void)printf(" %7s", name); 485 else 486 (void)printf(" %7u", (u_int)ip->i_e2fs_size); 487 return (0); 488 } 489 490 void 491 nfs_header() 492 { 493 (void)printf(" FILEID NFLAG RDEV|SZ"); 494 } 495 496 int 497 nfs_print(vp) 498 struct vnode *vp; 499 { 500 struct nfsnode nfsnode, *np = &nfsnode; 501 char flagbuf[16], *flags = flagbuf; 502 int flag; 503 struct vattr va; 504 char *name; 505 mode_t type; 506 507 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode"); 508 flag = np->n_flag; 509 if (flag & NFLUSHWANT) 510 *flags++ = 'W'; 511 if (flag & NFLUSHINPROG) 512 *flags++ = 'P'; 513 if (flag & NMODIFIED) 514 *flags++ = 'M'; 515 if (flag & NWRITEERR) 516 *flags++ = 'E'; 517 if (flag & NQNFSNONCACHE) 518 *flags++ = 'X'; 519 if (flag & NQNFSWRITE) 520 *flags++ = 'O'; 521 if (flag & NQNFSEVICTED) 522 *flags++ = 'G'; 523 if (flag == 0) 524 *flags++ = '-'; 525 *flags = '\0'; 526 527 KGETRET(np->n_vattr, &va, sizeof(va), "vnode attr"); 528 (void)printf(" %6ld %5s", (long)va.va_fileid, flagbuf); 529 type = va.va_mode & S_IFMT; 530 if (S_ISCHR(va.va_mode) || S_ISBLK(va.va_mode)) 531 if (usenumflag || ((name = devname(va.va_rdev, type)) == NULL)) 532 (void)printf(" %2d,%-2d", 533 major(va.va_rdev), minor(va.va_rdev)); 534 else 535 (void)printf(" %7s", name); 536 else 537 (void)printf(" %7qd", (long long)np->n_size); 538 return (0); 539 } 540 541 void 542 union_header() 543 { 544 (void)printf(" UPPER LOWER"); 545 } 546 547 int 548 union_print(vp) 549 struct vnode *vp; 550 { 551 struct union_node unode, *up = &unode; 552 553 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode"); 554 555 (void)printf(" %8lx %8lx", (long)up->un_uppervp, (long)up->un_lowervp); 556 return (0); 557 } 558 559 /* 560 * Given a pointer to a mount structure in kernel space, 561 * read it in and return a usable pointer to it. 562 */ 563 struct mount * 564 getmnt(maddr) 565 struct mount *maddr; 566 { 567 static struct mtab { 568 struct mtab *next; 569 struct mount *maddr; 570 struct mount mount; 571 } *mhead = NULL; 572 struct mtab *mt; 573 574 for (mt = mhead; mt != NULL; mt = mt->next) 575 if (maddr == mt->maddr) 576 return (&mt->mount); 577 if ((mt = malloc(sizeof(struct mtab))) == NULL) 578 err(1, "malloc"); 579 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table"); 580 mt->maddr = maddr; 581 mt->next = mhead; 582 mhead = mt; 583 return (&mt->mount); 584 } 585 586 void 587 mount_print(mp) 588 struct mount *mp; 589 { 590 int flags; 591 592 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename, 593 ST.f_mntfromname, ST.f_mntonname); 594 if ((flags = mp->mnt_flag) != 0) { 595 int i; 596 const char *sep = " ("; 597 598 for (i = 0; mnt_flags[i].m_flag; i++) { 599 if (flags & mnt_flags[i].m_flag) { 600 (void)printf("%s%s", sep, mnt_flags[i].m_name); 601 flags &= ~mnt_flags[i].m_flag; 602 sep = ","; 603 } 604 } 605 if (flags) 606 (void)printf("%sunknown_flags:%x", sep, flags); 607 (void)printf(")"); 608 } 609 (void)printf("\n"); 610 } 611 612 struct e_vnode * 613 loadvnodes(avnodes) 614 int *avnodes; 615 { 616 int mib[2]; 617 size_t copysize; 618 struct e_vnode *vnodebase; 619 620 if (memf != NULL) { 621 /* 622 * do it by hand 623 */ 624 return (kinfo_vnodes(avnodes)); 625 } 626 mib[0] = CTL_KERN; 627 mib[1] = KERN_VNODE; 628 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1) 629 err(1, "sysctl: KERN_VNODE"); 630 if ((vnodebase = malloc(copysize)) == NULL) 631 err(1, "malloc"); 632 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1) 633 err(1, "sysctl: KERN_VNODE"); 634 if (copysize % sizeof(struct e_vnode)) 635 errx(1, "vnode size mismatch"); 636 *avnodes = copysize / sizeof(struct e_vnode); 637 638 return (vnodebase); 639 } 640 641 /* 642 * simulate what a running kernel does in in kinfo_vnode 643 */ 644 struct e_vnode * 645 kinfo_vnodes(avnodes) 646 int *avnodes; 647 { 648 struct mntlist mountlist; 649 struct mount *mp, mount; 650 struct vnode *vp, vnode; 651 char *vbuf, *evbuf, *bp; 652 int num, numvnodes; 653 654 #define VPTRSZ sizeof(struct vnode *) 655 #define VNODESZ sizeof(struct vnode) 656 657 KGET(V_NUMV, numvnodes); 658 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL) 659 err(1, "malloc"); 660 bp = vbuf; 661 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ); 662 KGET(V_MOUNTLIST, mountlist); 663 for (num = 0, mp = mountlist.cqh_first; ; mp = mount.mnt_list.cqe_next) { 664 KGET2(mp, &mount, sizeof(mount), "mount entry"); 665 for (vp = mount.mnt_vnodelist.lh_first; 666 vp != NULL; vp = vnode.v_mntvnodes.le_next) { 667 KGET2(vp, &vnode, sizeof(vnode), "vnode"); 668 if ((bp + VPTRSZ + VNODESZ) > evbuf) 669 /* XXX - should realloc */ 670 errx(1, "no more room for vnodes"); 671 memmove(bp, &vp, VPTRSZ); 672 bp += VPTRSZ; 673 memmove(bp, &vnode, VNODESZ); 674 bp += VNODESZ; 675 num++; 676 } 677 if (mp == mountlist.cqh_last) 678 break; 679 } 680 *avnodes = num; 681 return ((struct e_vnode *)vbuf); 682 } 683 684 char hdr[]=" LINE RAW CAN OUT HWT LWT COL STATE SESS PGID DISC\n"; 685 int ttyspace = 128; 686 687 void 688 ttymode() 689 { 690 int ntty; 691 struct ttylist_head tty_head; 692 struct tty *tp, tty; 693 694 KGET(TTY_NTTY, ntty); 695 (void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s"); 696 KGET(TTY_TTYLIST, tty_head); 697 (void)printf(hdr); 698 for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) { 699 KGET2(tp, &tty, sizeof tty, "tty struct"); 700 ttyprt(&tty); 701 } 702 } 703 704 struct { 705 int flag; 706 char val; 707 } ttystates[] = { 708 { TS_WOPEN, 'W'}, 709 { TS_ISOPEN, 'O'}, 710 { TS_CARR_ON, 'C'}, 711 { TS_TIMEOUT, 'T'}, 712 { TS_FLUSH, 'F'}, 713 { TS_BUSY, 'B'}, 714 { TS_ASLEEP, 'A'}, 715 { TS_XCLUDE, 'X'}, 716 { TS_TTSTOP, 'S'}, 717 { TS_TBLOCK, 'K'}, 718 { TS_ASYNC, 'Y'}, 719 { TS_BKSL, 'D'}, 720 { TS_ERASE, 'E'}, 721 { TS_LNCH, 'L'}, 722 { TS_TYPEN, 'P'}, 723 { TS_CNTTB, 'N'}, 724 { 0, '\0'}, 725 }; 726 727 void 728 ttyprt(tp) 729 struct tty *tp; 730 { 731 int i, j; 732 pid_t pgid; 733 char *name, state[20]; 734 735 if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL) 736 (void)printf("0x%3x:%1x ", major(tp->t_dev), minor(tp->t_dev)); 737 else 738 (void)printf("%-7s ", name); 739 (void)printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc); 740 (void)printf("%3d %4d %3d %7d ", tp->t_outq.c_cc, 741 tp->t_hiwat, tp->t_lowat, tp->t_column); 742 for (i = j = 0; ttystates[i].flag; i++) 743 if (tp->t_state&ttystates[i].flag) 744 state[j++] = ttystates[i].val; 745 if (j == 0) 746 state[j++] = '-'; 747 state[j] = '\0'; 748 (void)printf("%-6s %8lX", state, (u_long)tp->t_session); 749 pgid = 0; 750 if (tp->t_pgrp != NULL) 751 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid"); 752 (void)printf("%6d ", pgid); 753 switch (tp->t_line) { 754 case TTYDISC: 755 (void)printf("term\n"); 756 break; 757 case TABLDISC: 758 (void)printf("tab\n"); 759 break; 760 case SLIPDISC: 761 (void)printf("slip\n"); 762 break; 763 case PPPDISC: 764 (void)printf("ppp\n"); 765 break; 766 case STRIPDISC: 767 (void)printf("strip\n"); 768 break; 769 default: 770 (void)printf("%d\n", tp->t_line); 771 break; 772 } 773 } 774 775 void 776 filemode() 777 { 778 struct file *fp; 779 struct file *addr; 780 char *buf, flagbuf[16], *fbp; 781 int len, maxfile, nfile; 782 static char *dtypes[] = { "???", "inode", "socket" }; 783 784 KGET(FNL_MAXFILE, maxfile); 785 if (totalflag) { 786 KGET(FNL_NFILE, nfile); 787 (void)printf("%3d/%3d files\n", nfile, maxfile); 788 return; 789 } 790 if (getfiles(&buf, &len) == -1) 791 return; 792 /* 793 * Getfiles returns in malloc'd memory a pointer to the first file 794 * structure, and then an array of file structs (whose addresses are 795 * derivable from the previous entry). 796 */ 797 addr = ((struct filelist *)buf)->lh_first; 798 fp = (struct file *)(buf + sizeof(struct filelist)); 799 nfile = (len - sizeof(struct filelist)) / sizeof(struct file); 800 801 (void)printf("%d/%d open files\n", nfile, maxfile); 802 (void)printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n"); 803 for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) { 804 if ((unsigned)fp->f_type > DTYPE_SOCKET) 805 continue; 806 (void)printf("%lx ", (long)addr); 807 (void)printf("%-8.8s", dtypes[fp->f_type]); 808 fbp = flagbuf; 809 if (fp->f_flag & FREAD) 810 *fbp++ = 'R'; 811 if (fp->f_flag & FWRITE) 812 *fbp++ = 'W'; 813 if (fp->f_flag & FAPPEND) 814 *fbp++ = 'A'; 815 #ifdef FSHLOCK /* currently gone */ 816 if (fp->f_flag & FSHLOCK) 817 *fbp++ = 'S'; 818 if (fp->f_flag & FEXLOCK) 819 *fbp++ = 'X'; 820 #endif 821 if (fp->f_flag & FASYNC) 822 *fbp++ = 'I'; 823 *fbp = '\0'; 824 (void)printf("%6s %3d", flagbuf, fp->f_count); 825 (void)printf(" %3d", fp->f_msgcount); 826 (void)printf(" %8.1lx", (long)fp->f_data); 827 if (fp->f_offset < 0) 828 (void)printf(" %qx\n", (long long)fp->f_offset); 829 else 830 (void)printf(" %qd\n", (long long)fp->f_offset); 831 } 832 free(buf); 833 } 834 835 int 836 getfiles(abuf, alen) 837 char **abuf; 838 int *alen; 839 { 840 size_t len; 841 int mib[2]; 842 char *buf; 843 844 /* 845 * XXX 846 * Add emulation of KINFO_FILE here. 847 */ 848 if (memf != NULL) 849 errx(1, "files on dead kernel, not implemented\n"); 850 851 mib[0] = CTL_KERN; 852 mib[1] = KERN_FILE; 853 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 854 warn("sysctl: KERN_FILE"); 855 return (-1); 856 } 857 if ((buf = malloc(len)) == NULL) 858 err(1, "malloc"); 859 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 860 warn("sysctl: KERN_FILE"); 861 return (-1); 862 } 863 *abuf = buf; 864 *alen = len; 865 return (0); 866 } 867 868 void 869 usage() 870 { 871 (void)fprintf(stderr, 872 "usage: pstat [-T|-f|-s|-t|-v] [-kn] [-M core] [-N system]\n"); 873 exit(1); 874 } 875