1 /*- 2 * Copyright (c) 1980, 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 */ 7 8 #ifndef lint 9 static char copyright[] = 10 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 11 The Regents of the University of California. All rights reserved.\n"; 12 #endif /* not lint */ 13 14 #ifndef lint 15 static char sccsid[] = "@(#)pstat.c 8.11 (Berkeley) 08/14/94"; 16 #endif /* not lint */ 17 18 #include <sys/param.h> 19 #include <sys/time.h> 20 #include <sys/vnode.h> 21 #include <sys/map.h> 22 #include <sys/ucred.h> 23 #define KERNEL 24 #include <sys/file.h> 25 #include <ufs/ufs/quota.h> 26 #include <ufs/ufs/inode.h> 27 #define NFS 28 #include <sys/mount.h> 29 #undef NFS 30 #include <sys/uio.h> 31 #include <sys/namei.h> 32 #include <miscfs/union/union.h> 33 #undef KERNEL 34 #include <sys/stat.h> 35 #include <nfs/nfsnode.h> 36 #include <sys/ioctl.h> 37 #include <sys/tty.h> 38 #include <sys/conf.h> 39 40 #include <sys/sysctl.h> 41 42 #include <err.h> 43 #include <kvm.h> 44 #include <limits.h> 45 #include <nlist.h> 46 #include <stdio.h> 47 #include <stdlib.h> 48 #include <string.h> 49 #include <unistd.h> 50 51 struct nlist nl[] = { 52 #define VM_SWAPMAP 0 53 { "_swapmap" }, /* list of free swap areas */ 54 #define VM_NSWAPMAP 1 55 { "_nswapmap" },/* size of the swap map */ 56 #define VM_SWDEVT 2 57 { "_swdevt" }, /* list of swap devices and sizes */ 58 #define VM_NSWAP 3 59 { "_nswap" }, /* size of largest swap device */ 60 #define VM_NSWDEV 4 61 { "_nswdev" }, /* number of swap devices */ 62 #define VM_DMMAX 5 63 { "_dmmax" }, /* maximum size of a swap block */ 64 #define V_MOUNTLIST 6 65 { "_mountlist" }, /* address of head of mount list. */ 66 #define V_NUMV 7 67 { "_numvnodes" }, 68 #define FNL_NFILE 8 69 {"_nfiles"}, 70 #define FNL_MAXFILE 9 71 {"_maxfiles"}, 72 #define NLMANDATORY FNL_MAXFILE /* names up to here are mandatory */ 73 #define VM_NISWAP NLMANDATORY + 1 74 { "_niswap" }, 75 #define VM_NISWDEV NLMANDATORY + 2 76 { "_niswdev" }, 77 #define SCONS NLMANDATORY + 3 78 { "_cons" }, 79 #define SPTY NLMANDATORY + 4 80 { "_pt_tty" }, 81 #define SNPTY NLMANDATORY + 5 82 { "_npty" }, 83 84 #ifdef hp300 85 #define SDCA (SNPTY+1) 86 { "_dca_tty" }, 87 #define SNDCA (SNPTY+2) 88 { "_ndca" }, 89 #define SDCM (SNPTY+3) 90 { "_dcm_tty" }, 91 #define SNDCM (SNPTY+4) 92 { "_ndcm" }, 93 #define SDCL (SNPTY+5) 94 { "_dcl_tty" }, 95 #define SNDCL (SNPTY+6) 96 { "_ndcl" }, 97 #define SITE (SNPTY+7) 98 { "_ite_tty" }, 99 #define SNITE (SNPTY+8) 100 { "_nite" }, 101 #endif 102 103 #ifdef mips 104 #define SDC (SNPTY+1) 105 { "_dc_tty" }, 106 #define SNDC (SNPTY+2) 107 { "_dc_cnt" }, 108 #endif 109 110 { "" } 111 }; 112 113 int usenumflag; 114 int totalflag; 115 char *nlistf = NULL; 116 char *memf = NULL; 117 kvm_t *kd; 118 119 const char *mountname[] = INITMOUNTNAMES; 120 struct { 121 int m_flag; 122 const char *m_name; 123 } mnt_flags[] = { 124 { MNT_RDONLY, "rdonly" }, 125 { MNT_SYNCHRONOUS, "sync" }, 126 { MNT_NOEXEC, "noexec" }, 127 { MNT_NOSUID, "nosuid" }, 128 { MNT_NODEV, "nodev" }, 129 { MNT_UNION, "union" }, 130 { MNT_ASYNC, "async" }, 131 { MNT_EXRDONLY, "exrdonly" }, 132 { MNT_EXPORTED, "exported" }, 133 { MNT_DEFEXPORTED, "defexported" }, 134 { MNT_EXPORTANON, "exportanon" }, 135 { MNT_EXKERB, "exkerb" }, 136 { MNT_LOCAL, "local" }, 137 { MNT_QUOTA, "quota" }, 138 { MNT_ROOTFS, "rootfs" }, 139 { MNT_UPDATE, "update" }, 140 { MNT_DELEXPORT }, 141 { MNT_UPDATE, "update" }, 142 { MNT_DELEXPORT, "delexport" }, 143 { MNT_RELOAD, "reload" }, 144 { MNT_FORCE, "force" }, 145 { MNT_MLOCK, "mlock" }, 146 { MNT_WAIT, "wait" }, 147 { MNT_MPBUSY, "mpbusy" }, 148 { MNT_MPWANT, "mpwant" }, 149 { MNT_UNMOUNT, "unmount" }, 150 { MNT_WANTRDWR, "wantrdwr" }, 151 { 0 } 152 }; 153 154 155 #define SVAR(var) __STRING(var) /* to force expansion */ 156 #define KGET(idx, var) \ 157 KGET1(idx, &var, sizeof(var), SVAR(var)) 158 #define KGET1(idx, p, s, msg) \ 159 KGET2(nl[idx].n_value, p, s, msg) 160 #define KGET2(addr, p, s, msg) \ 161 if (kvm_read(kd, (u_long)(addr), p, s) != s) \ 162 warnx("cannot read %s: %s", msg, kvm_geterr(kd)) 163 #define KGETRET(addr, p, s, msg) \ 164 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \ 165 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 166 return (0); \ 167 } 168 169 void filemode __P((void)); 170 int getfiles __P((char **, int *)); 171 struct mount * 172 getmnt __P((struct mount *)); 173 struct e_vnode * 174 kinfo_vnodes __P((int *)); 175 struct e_vnode * 176 loadvnodes __P((int *)); 177 void mount_print __P((struct mount *)); 178 void nfs_header __P((void)); 179 int nfs_print __P((struct vnode *)); 180 void swapmode __P((void)); 181 void ttymode __P((void)); 182 void ttyprt __P((struct tty *, int)); 183 void ttytype __P((struct tty *, char *, int, int)); 184 void ufs_header __P((void)); 185 int ufs_print __P((struct vnode *)); 186 void union_header __P((void)); 187 int union_print __P((struct vnode *)); 188 void usage __P((void)); 189 void vnode_header __P((void)); 190 void vnode_print __P((struct vnode *, struct vnode *)); 191 void vnodemode __P((void)); 192 193 int 194 main(argc, argv) 195 int argc; 196 char *argv[]; 197 { 198 extern char *optarg; 199 extern int optind; 200 int ch, i, quit, ret; 201 int fileflag, swapflag, ttyflag, vnodeflag; 202 char buf[_POSIX2_LINE_MAX]; 203 204 fileflag = swapflag = ttyflag = vnodeflag = 0; 205 while ((ch = getopt(argc, argv, "TM:N:finstv")) != EOF) 206 switch (ch) { 207 case 'f': 208 fileflag = 1; 209 break; 210 case 'M': 211 memf = optarg; 212 break; 213 case 'N': 214 nlistf = optarg; 215 break; 216 case 'n': 217 usenumflag = 1; 218 break; 219 case 's': 220 swapflag = 1; 221 break; 222 case 'T': 223 totalflag = 1; 224 break; 225 case 't': 226 ttyflag = 1; 227 break; 228 case 'v': 229 case 'i': /* Backward compatibility. */ 230 vnodeflag = 1; 231 break; 232 default: 233 usage(); 234 } 235 argc -= optind; 236 argv += optind; 237 238 /* 239 * Discard setgid privileges if not the running kernel so that bad 240 * guys can't print interesting stuff from kernel memory. 241 */ 242 if (nlistf != NULL || memf != NULL) 243 (void)setgid(getgid()); 244 245 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0) 246 errx(1, "kvm_openfiles: %s", buf); 247 if ((ret = kvm_nlist(kd, nl)) != 0) { 248 if (ret == -1) 249 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 250 for (i = quit = 0; i <= NLMANDATORY; i++) 251 if (!nl[i].n_value) { 252 quit = 1; 253 warnx("undefined symbol: %s\n", nl[i].n_name); 254 } 255 if (quit) 256 exit(1); 257 } 258 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag)) 259 usage(); 260 if (fileflag || totalflag) 261 filemode(); 262 if (vnodeflag || totalflag) 263 vnodemode(); 264 if (ttyflag) 265 ttymode(); 266 if (swapflag || totalflag) 267 swapmode(); 268 exit (0); 269 } 270 271 struct e_vnode { 272 struct vnode *avnode; 273 struct vnode vnode; 274 }; 275 276 void 277 vnodemode() 278 { 279 struct e_vnode *e_vnodebase, *endvnode, *evp; 280 struct vnode *vp; 281 struct mount *maddr, *mp; 282 int numvnodes; 283 284 e_vnodebase = loadvnodes(&numvnodes); 285 if (totalflag) { 286 (void)printf("%7d vnodes\n", numvnodes); 287 return; 288 } 289 endvnode = e_vnodebase + numvnodes; 290 (void)printf("%d active vnodes\n", numvnodes); 291 292 293 #define ST mp->mnt_stat 294 maddr = NULL; 295 for (evp = e_vnodebase; evp < endvnode; evp++) { 296 vp = &evp->vnode; 297 if (vp->v_mount != maddr) { 298 /* 299 * New filesystem 300 */ 301 if ((mp = getmnt(vp->v_mount)) == NULL) 302 continue; 303 maddr = vp->v_mount; 304 mount_print(mp); 305 vnode_header(); 306 switch (ST.f_type) { 307 case MOUNT_UFS: 308 case MOUNT_MFS: 309 ufs_header(); 310 break; 311 case MOUNT_NFS: 312 nfs_header(); 313 break; 314 case MOUNT_UNION: 315 union_header(); 316 break; 317 case MOUNT_NONE: 318 case MOUNT_MSDOS: 319 default: 320 break; 321 } 322 (void)printf("\n"); 323 } 324 vnode_print(evp->avnode, vp); 325 switch (ST.f_type) { 326 case MOUNT_UFS: 327 case MOUNT_MFS: 328 ufs_print(vp); 329 break; 330 case MOUNT_NFS: 331 nfs_print(vp); 332 break; 333 case MOUNT_UNION: 334 union_print(vp); 335 break; 336 case MOUNT_NONE: 337 case MOUNT_MSDOS: 338 default: 339 break; 340 } 341 (void)printf("\n"); 342 } 343 free(e_vnodebase); 344 } 345 346 void 347 vnode_header() 348 { 349 (void)printf("ADDR TYP VFLAG USE HOLD"); 350 } 351 352 void 353 vnode_print(avnode, vp) 354 struct vnode *avnode; 355 struct vnode *vp; 356 { 357 char *type, flags[16]; 358 char *fp = flags; 359 int flag; 360 361 /* 362 * set type 363 */ 364 switch (vp->v_type) { 365 case VNON: 366 type = "non"; break; 367 case VREG: 368 type = "reg"; break; 369 case VDIR: 370 type = "dir"; break; 371 case VBLK: 372 type = "blk"; break; 373 case VCHR: 374 type = "chr"; break; 375 case VLNK: 376 type = "lnk"; break; 377 case VSOCK: 378 type = "soc"; break; 379 case VFIFO: 380 type = "fif"; break; 381 case VBAD: 382 type = "bad"; break; 383 default: 384 type = "unk"; break; 385 } 386 /* 387 * gather flags 388 */ 389 flag = vp->v_flag; 390 if (flag & VROOT) 391 *fp++ = 'R'; 392 if (flag & VTEXT) 393 *fp++ = 'T'; 394 if (flag & VSYSTEM) 395 *fp++ = 'S'; 396 if (flag & VXLOCK) 397 *fp++ = 'L'; 398 if (flag & VXWANT) 399 *fp++ = 'W'; 400 if (flag & VBWAIT) 401 *fp++ = 'B'; 402 if (flag & VALIASED) 403 *fp++ = 'A'; 404 if (flag == 0) 405 *fp++ = '-'; 406 *fp = '\0'; 407 (void)printf("%8x %s %5s %4d %4d", 408 avnode, type, flags, vp->v_usecount, vp->v_holdcnt); 409 } 410 411 void 412 ufs_header() 413 { 414 (void)printf(" FILEID IFLAG RDEV|SZ"); 415 } 416 417 int 418 ufs_print(vp) 419 struct vnode *vp; 420 { 421 int flag; 422 struct inode inode, *ip = &inode; 423 char flagbuf[16], *flags = flagbuf; 424 char *name; 425 mode_t type; 426 427 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 428 flag = ip->i_flag; 429 if (flag & IN_LOCKED) 430 *flags++ = 'L'; 431 if (flag & IN_WANTED) 432 *flags++ = 'W'; 433 if (flag & IN_RENAME) 434 *flags++ = 'R'; 435 if (flag & IN_UPDATE) 436 *flags++ = 'U'; 437 if (flag & IN_ACCESS) 438 *flags++ = 'A'; 439 if (flag & IN_CHANGE) 440 *flags++ = 'C'; 441 if (flag & IN_MODIFIED) 442 *flags++ = 'M'; 443 if (flag & IN_SHLOCK) 444 *flags++ = 'S'; 445 if (flag & IN_EXLOCK) 446 *flags++ = 'E'; 447 if (flag & IN_LWAIT) 448 *flags++ = 'Z'; 449 if (flag == 0) 450 *flags++ = '-'; 451 *flags = '\0'; 452 453 (void)printf(" %6d %5s", ip->i_number, flagbuf); 454 type = ip->i_mode & S_IFMT; 455 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) 456 if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL)) 457 (void)printf(" %2d,%-2d", 458 major(ip->i_rdev), minor(ip->i_rdev)); 459 else 460 (void)printf(" %7s", name); 461 else 462 (void)printf(" %7qd", ip->i_size); 463 return (0); 464 } 465 466 void 467 nfs_header() 468 { 469 (void)printf(" FILEID NFLAG RDEV|SZ"); 470 } 471 472 int 473 nfs_print(vp) 474 struct vnode *vp; 475 { 476 struct nfsnode nfsnode, *np = &nfsnode; 477 char flagbuf[16], *flags = flagbuf; 478 int flag; 479 char *name; 480 mode_t type; 481 482 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode"); 483 flag = np->n_flag; 484 if (flag & NFLUSHWANT) 485 *flags++ = 'W'; 486 if (flag & NFLUSHINPROG) 487 *flags++ = 'P'; 488 if (flag & NMODIFIED) 489 *flags++ = 'M'; 490 if (flag & NWRITEERR) 491 *flags++ = 'E'; 492 if (flag & NQNFSNONCACHE) 493 *flags++ = 'X'; 494 if (flag & NQNFSWRITE) 495 *flags++ = 'O'; 496 if (flag & NQNFSEVICTED) 497 *flags++ = 'G'; 498 if (flag == 0) 499 *flags++ = '-'; 500 *flags = '\0'; 501 502 #define VT np->n_vattr 503 (void)printf(" %6d %5s", VT.va_fileid, flagbuf); 504 type = VT.va_mode & S_IFMT; 505 if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode)) 506 if (usenumflag || ((name = devname(VT.va_rdev, type)) == NULL)) 507 (void)printf(" %2d,%-2d", 508 major(VT.va_rdev), minor(VT.va_rdev)); 509 else 510 (void)printf(" %7s", name); 511 else 512 (void)printf(" %7qd", np->n_size); 513 return (0); 514 } 515 516 void 517 union_header() 518 { 519 (void)printf(" UPPER LOWER"); 520 } 521 522 int 523 union_print(vp) 524 struct vnode *vp; 525 { 526 struct union_node unode, *up = &unode; 527 528 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode"); 529 530 (void)printf(" %8x %8x", up->un_uppervp, up->un_lowervp); 531 return (0); 532 } 533 534 /* 535 * Given a pointer to a mount structure in kernel space, 536 * read it in and return a usable pointer to it. 537 */ 538 struct mount * 539 getmnt(maddr) 540 struct mount *maddr; 541 { 542 static struct mtab { 543 struct mtab *next; 544 struct mount *maddr; 545 struct mount mount; 546 } *mhead = NULL; 547 struct mtab *mt; 548 549 for (mt = mhead; mt != NULL; mt = mt->next) 550 if (maddr == mt->maddr) 551 return (&mt->mount); 552 if ((mt = malloc(sizeof(struct mtab))) == NULL) 553 err(1, NULL); 554 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table"); 555 mt->maddr = maddr; 556 mt->next = mhead; 557 mhead = mt; 558 return (&mt->mount); 559 } 560 561 void 562 mount_print(mp) 563 struct mount *mp; 564 { 565 int flags; 566 const char *type; 567 568 #define ST mp->mnt_stat 569 (void)printf("*** MOUNT "); 570 if (ST.f_type >= sizeof(mountname)/sizeof(mountname[0])) 571 type = "unknown"; 572 else 573 type = mountname[ST.f_type]; 574 (void)printf("%s %s on %s", type, ST.f_mntfromname, ST.f_mntonname); 575 if (flags = mp->mnt_flag) { 576 int i; 577 const char *sep = " ("; 578 579 for (i = 0; mnt_flags[i].m_flag; i++) { 580 if (flags & mnt_flags[i].m_flag) { 581 (void)printf("%s%s", sep, mnt_flags[i].m_name); 582 flags &= ~mnt_flags[i].m_flag; 583 sep = ","; 584 } 585 } 586 if (flags) 587 (void)printf("%sunknown_flags:%x", sep, flags); 588 (void)printf(")"); 589 } 590 (void)printf("\n"); 591 #undef ST 592 } 593 594 struct e_vnode * 595 loadvnodes(avnodes) 596 int *avnodes; 597 { 598 int mib[2]; 599 size_t copysize; 600 struct e_vnode *vnodebase; 601 602 if (memf != NULL) { 603 /* 604 * do it by hand 605 */ 606 return (kinfo_vnodes(avnodes)); 607 } 608 mib[0] = CTL_KERN; 609 mib[1] = KERN_VNODE; 610 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1) 611 err(1, "sysctl: KERN_VNODE"); 612 if ((vnodebase = malloc(copysize)) == NULL) 613 err(1, NULL); 614 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1) 615 err(1, "sysctl: KERN_VNODE"); 616 if (copysize % sizeof(struct e_vnode)) 617 errx(1, "vnode size mismatch"); 618 *avnodes = copysize / sizeof(struct e_vnode); 619 620 return (vnodebase); 621 } 622 623 /* 624 * simulate what a running kernel does in in kinfo_vnode 625 */ 626 struct e_vnode * 627 kinfo_vnodes(avnodes) 628 int *avnodes; 629 { 630 struct mntlist mountlist; 631 struct mount *mp, mount; 632 struct vnode *vp, vnode; 633 char *vbuf, *evbuf, *bp; 634 int num, numvnodes; 635 636 #define VPTRSZ sizeof(struct vnode *) 637 #define VNODESZ sizeof(struct vnode) 638 639 KGET(V_NUMV, numvnodes); 640 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL) 641 err(1, NULL); 642 bp = vbuf; 643 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ); 644 KGET(V_MOUNTLIST, mountlist); 645 for (num = 0, mp = mountlist.tqh_first; 646 mp != NULL; mp = mp->mnt_list.tqe_next) { 647 KGET2(mp, &mount, sizeof(mount), "mount entry"); 648 for (vp = mount.mnt_vnodelist.lh_first; 649 vp != NULL; vp = vp->v_mntvnodes.le_next) { 650 KGET2(vp, &vnode, sizeof(vnode), "vnode"); 651 if ((bp + VPTRSZ + VNODESZ) > evbuf) 652 /* XXX - should realloc */ 653 errx(1, "no more room for vnodes"); 654 memmove(bp, &vp, VPTRSZ); 655 bp += VPTRSZ; 656 memmove(bp, &vnode, VNODESZ); 657 bp += VNODESZ; 658 num++; 659 } 660 } 661 *avnodes = num; 662 return ((struct e_vnode *)vbuf); 663 } 664 665 char hdr[]=" LINE RAW CAN OUT HWT LWT COL STATE SESS PGID DISC\n"; 666 int ttyspace = 128; 667 668 void 669 ttymode() 670 { 671 struct tty *tty; 672 673 if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL) 674 err(1, NULL); 675 #ifndef hp300 676 (void)printf("1 console\n"); 677 KGET(SCONS, *tty); 678 (void)printf(hdr); 679 ttyprt(&tty[0], 0); 680 #endif 681 #ifdef vax 682 if (nl[SNQD].n_type != 0) 683 qdss(); 684 if (nl[SNDZ].n_type != 0) 685 ttytype(tty, "dz", SDZ, SNDZ); 686 if (nl[SNDH].n_type != 0) 687 ttytype(tty, "dh", SDH, SNDH); 688 if (nl[SNDMF].n_type != 0) 689 ttytype(tty, "dmf", SDMF, SNDMF); 690 if (nl[SNDHU].n_type != 0) 691 ttytype(tty, "dhu", SDHU, SNDHU); 692 if (nl[SNDMZ].n_type != 0) 693 ttytype(tty, "dmz", SDMZ, SNDMZ); 694 #endif 695 #ifdef tahoe 696 if (nl[SNVX].n_type != 0) 697 ttytype(tty, "vx", SVX, SNVX); 698 if (nl[SNMP].n_type != 0) 699 ttytype(tty, "mp", SMP, SNMP); 700 #endif 701 #ifdef hp300 702 if (nl[SNITE].n_type != 0) 703 ttytype(tty, "ite", SITE, SNITE); 704 if (nl[SNDCA].n_type != 0) 705 ttytype(tty, "dca", SDCA, SNDCA); 706 if (nl[SNDCM].n_type != 0) 707 ttytype(tty, "dcm", SDCM, SNDCM); 708 if (nl[SNDCL].n_type != 0) 709 ttytype(tty, "dcl", SDCL, SNDCL); 710 #endif 711 #ifdef mips 712 if (nl[SNDC].n_type != 0) 713 ttytype(tty, "dc", SDC, SNDC); 714 #endif 715 if (nl[SNPTY].n_type != 0) 716 ttytype(tty, "pty", SPTY, SNPTY); 717 } 718 719 void 720 ttytype(tty, name, type, number) 721 struct tty *tty; 722 char *name; 723 int type, number; 724 { 725 struct tty *tp; 726 int ntty; 727 728 if (tty == NULL) 729 return; 730 KGET(number, ntty); 731 (void)printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines"); 732 if (ntty > ttyspace) { 733 ttyspace = ntty; 734 if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == 0) 735 err(1, NULL); 736 } 737 KGET1(type, tty, ntty * sizeof(struct tty), "tty structs"); 738 (void)printf(hdr); 739 for (tp = tty; tp < &tty[ntty]; tp++) 740 ttyprt(tp, tp - tty); 741 } 742 743 struct { 744 int flag; 745 char val; 746 } ttystates[] = { 747 { TS_WOPEN, 'W'}, 748 { TS_ISOPEN, 'O'}, 749 { TS_CARR_ON, 'C'}, 750 { TS_TIMEOUT, 'T'}, 751 { TS_FLUSH, 'F'}, 752 { TS_BUSY, 'B'}, 753 { TS_ASLEEP, 'A'}, 754 { TS_XCLUDE, 'X'}, 755 { TS_TTSTOP, 'S'}, 756 { TS_TBLOCK, 'K'}, 757 { TS_ASYNC, 'Y'}, 758 { TS_BKSL, 'D'}, 759 { TS_ERASE, 'E'}, 760 { TS_LNCH, 'L'}, 761 { TS_TYPEN, 'P'}, 762 { TS_CNTTB, 'N'}, 763 { 0, '\0'}, 764 }; 765 766 void 767 ttyprt(tp, line) 768 struct tty *tp; 769 int line; 770 { 771 int i, j; 772 pid_t pgid; 773 char *name, state[20]; 774 775 if (usenumflag || tp->t_dev == 0 || 776 (name = devname(tp->t_dev, S_IFCHR)) == NULL) 777 (void)printf("%7d ", line); 778 else 779 (void)printf("%7s ", name); 780 (void)printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc); 781 (void)printf("%3d %4d %3d %3d ", tp->t_outq.c_cc, 782 tp->t_hiwat, tp->t_lowat, tp->t_column); 783 for (i = j = 0; ttystates[i].flag; i++) 784 if (tp->t_state&ttystates[i].flag) 785 state[j++] = ttystates[i].val; 786 if (j == 0) 787 state[j++] = '-'; 788 state[j] = '\0'; 789 (void)printf("%-4s %6x", state, (u_long)tp->t_session & ~KERNBASE); 790 pgid = 0; 791 if (tp->t_pgrp != NULL) 792 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid"); 793 (void)printf("%6d ", pgid); 794 switch (tp->t_line) { 795 case TTYDISC: 796 (void)printf("term\n"); 797 break; 798 case TABLDISC: 799 (void)printf("tab\n"); 800 break; 801 case SLIPDISC: 802 (void)printf("slip\n"); 803 break; 804 default: 805 (void)printf("%d\n", tp->t_line); 806 break; 807 } 808 } 809 810 void 811 filemode() 812 { 813 struct file *fp; 814 struct file *addr; 815 char *buf, flagbuf[16], *fbp; 816 int len, maxfile, nfile; 817 static char *dtypes[] = { "???", "inode", "socket" }; 818 819 KGET(FNL_MAXFILE, maxfile); 820 if (totalflag) { 821 KGET(FNL_NFILE, nfile); 822 (void)printf("%3d/%3d files\n", nfile, maxfile); 823 return; 824 } 825 if (getfiles(&buf, &len) == -1) 826 return; 827 /* 828 * Getfiles returns in malloc'd memory a pointer to the first file 829 * structure, and then an array of file structs (whose addresses are 830 * derivable from the previous entry). 831 */ 832 addr = *((struct file **)buf); 833 fp = (struct file *)(buf + sizeof(struct file *)); 834 nfile = (len - sizeof(struct file *)) / sizeof(struct file); 835 836 (void)printf("%d/%d open files\n", nfile, maxfile); 837 (void)printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n"); 838 for (; (char *)fp < buf + len; addr = fp->f_filef, fp++) { 839 if ((unsigned)fp->f_type > DTYPE_SOCKET) 840 continue; 841 (void)printf("%x ", addr); 842 (void)printf("%-8.8s", dtypes[fp->f_type]); 843 fbp = flagbuf; 844 if (fp->f_flag & FREAD) 845 *fbp++ = 'R'; 846 if (fp->f_flag & FWRITE) 847 *fbp++ = 'W'; 848 if (fp->f_flag & FAPPEND) 849 *fbp++ = 'A'; 850 #ifdef FSHLOCK /* currently gone */ 851 if (fp->f_flag & FSHLOCK) 852 *fbp++ = 'S'; 853 if (fp->f_flag & FEXLOCK) 854 *fbp++ = 'X'; 855 #endif 856 if (fp->f_flag & FASYNC) 857 *fbp++ = 'I'; 858 *fbp = '\0'; 859 (void)printf("%6s %3d", flagbuf, fp->f_count); 860 (void)printf(" %3d", fp->f_msgcount); 861 (void)printf(" %8.1x", fp->f_data); 862 if (fp->f_offset < 0) 863 (void)printf(" %qx\n", fp->f_offset); 864 else 865 (void)printf(" %qd\n", fp->f_offset); 866 } 867 free(buf); 868 } 869 870 int 871 getfiles(abuf, alen) 872 char **abuf; 873 int *alen; 874 { 875 size_t len; 876 int mib[2]; 877 char *buf; 878 879 /* 880 * XXX 881 * Add emulation of KINFO_FILE here. 882 */ 883 if (memf != NULL) 884 errx(1, "files on dead kernel, not implemented\n"); 885 886 mib[0] = CTL_KERN; 887 mib[1] = KERN_FILE; 888 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 889 warn("sysctl: KERN_FILE"); 890 return (-1); 891 } 892 if ((buf = malloc(len)) == NULL) 893 err(1, NULL); 894 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 895 warn("sysctl: KERN_FILE"); 896 return (-1); 897 } 898 *abuf = buf; 899 *alen = len; 900 return (0); 901 } 902 903 /* 904 * swapmode is based on a program called swapinfo written 905 * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 906 */ 907 void 908 swapmode() 909 { 910 char *header; 911 int hlen, nswap, nswdev, dmmax, nswapmap, niswap, niswdev; 912 int s, e, div, i, l, avail, nfree, npfree, used; 913 struct swdevt *sw; 914 long blocksize, *perdev; 915 struct map *swapmap, *kswapmap; 916 struct mapent *mp; 917 918 KGET(VM_NSWAP, nswap); 919 KGET(VM_NSWDEV, nswdev); 920 KGET(VM_DMMAX, dmmax); 921 KGET(VM_NSWAPMAP, nswapmap); 922 KGET(VM_SWAPMAP, kswapmap); /* kernel `swapmap' is a pointer */ 923 if ((sw = malloc(nswdev * sizeof(*sw))) == NULL || 924 (perdev = malloc(nswdev * sizeof(*perdev))) == NULL || 925 (mp = malloc(nswapmap * sizeof(*mp))) == NULL) 926 err(1, "malloc"); 927 KGET1(VM_SWDEVT, sw, nswdev * sizeof(*sw), "swdevt"); 928 KGET2((long)kswapmap, mp, nswapmap * sizeof(*mp), "swapmap"); 929 930 /* Supports sequential swap */ 931 if (nl[VM_NISWAP].n_value != 0) { 932 KGET(VM_NISWAP, niswap); 933 KGET(VM_NISWDEV, niswdev); 934 } else { 935 niswap = nswap; 936 niswdev = nswdev; 937 } 938 939 /* First entry in map is `struct map'; rest are mapent's. */ 940 swapmap = (struct map *)mp; 941 if (nswapmap != swapmap->m_limit - (struct mapent *)kswapmap) 942 errx(1, "panic: nswapmap goof"); 943 944 /* Count up swap space. */ 945 nfree = 0; 946 memset(perdev, 0, nswdev * sizeof(*perdev)); 947 for (mp++; mp->m_addr != 0; mp++) { 948 s = mp->m_addr; /* start of swap region */ 949 e = mp->m_addr + mp->m_size; /* end of region */ 950 nfree += mp->m_size; 951 952 /* 953 * Swap space is split up among the configured disks. 954 * 955 * For interleaved swap devices, the first dmmax blocks 956 * of swap space some from the first disk, the next dmmax 957 * blocks from the next, and so on up to niswap blocks. 958 * 959 * Sequential swap devices follow the interleaved devices 960 * (i.e. blocks starting at niswap) in the order in which 961 * they appear in the swdev table. The size of each device 962 * will be a multiple of dmmax. 963 * 964 * The list of free space joins adjacent free blocks, 965 * ignoring device boundries. If we want to keep track 966 * of this information per device, we'll just have to 967 * extract it ourselves. We know that dmmax-sized chunks 968 * cannot span device boundaries (interleaved or sequential) 969 * so we loop over such chunks assigning them to devices. 970 */ 971 i = -1; 972 while (s < e) { /* XXX this is inefficient */ 973 int bound = roundup(s+1, dmmax); 974 975 if (bound > e) 976 bound = e; 977 if (bound <= niswap) { 978 /* Interleaved swap chunk. */ 979 if (i == -1) 980 i = (s / dmmax) % niswdev; 981 perdev[i] += bound - s; 982 if (++i >= niswdev) 983 i = 0; 984 } else { 985 /* Sequential swap chunk. */ 986 if (i < niswdev) { 987 i = niswdev; 988 l = niswap + sw[i].sw_nblks; 989 } 990 while (s >= l) { 991 /* XXX don't die on bogus blocks */ 992 if (i == nswdev-1) 993 break; 994 l += sw[++i].sw_nblks; 995 } 996 perdev[i] += bound - s; 997 } 998 s = bound; 999 } 1000 } 1001 1002 header = getbsize(&hlen, &blocksize); 1003 if (!totalflag) 1004 (void)printf("%-11s %*s %8s %8s %8s %s\n", 1005 "Device", hlen, header, 1006 "Used", "Avail", "Capacity", "Type"); 1007 div = blocksize / 512; 1008 avail = npfree = 0; 1009 for (i = 0; i < nswdev; i++) { 1010 int xsize, xfree; 1011 1012 if (!totalflag) 1013 (void)printf("/dev/%-6s %*d ", 1014 devname(sw[i].sw_dev, S_IFBLK), 1015 hlen, sw[i].sw_nblks / div); 1016 1017 /* 1018 * Don't report statistics for partitions which have not 1019 * yet been activated via swapon(8). 1020 */ 1021 if (!(sw[i].sw_flags & SW_FREED)) { 1022 if (totalflag) 1023 continue; 1024 (void)printf(" *** not available for swapping ***\n"); 1025 continue; 1026 } 1027 xsize = sw[i].sw_nblks; 1028 xfree = perdev[i]; 1029 used = xsize - xfree; 1030 npfree++; 1031 avail += xsize; 1032 if (totalflag) 1033 continue; 1034 (void)printf("%8d %8d %5.0f%% %s\n", 1035 used / div, xfree / div, 1036 (double)used / (double)xsize * 100.0, 1037 (sw[i].sw_flags & SW_SEQUENTIAL) ? 1038 "Sequential" : "Interleaved"); 1039 } 1040 1041 /* 1042 * If only one partition has been set up via swapon(8), we don't 1043 * need to bother with totals. 1044 */ 1045 used = avail - nfree; 1046 if (totalflag) { 1047 (void)printf("%dM/%dM swap space\n", used / 2048, avail / 2048); 1048 return; 1049 } 1050 if (npfree > 1) { 1051 (void)printf("%-11s %*d %8d %8d %5.0f%%\n", 1052 "Total", hlen, avail / div, used / div, nfree / div, 1053 (double)used / (double)avail * 100.0); 1054 } 1055 } 1056 1057 void 1058 usage() 1059 { 1060 (void)fprintf(stderr, 1061 "usage: pstat -Tfnstv [system] [-M core] [-N system]\n"); 1062 exit(1); 1063 } 1064