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