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