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