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