1 /*- 2 * Copyright (c) 1980, 1991, 1993, 1994 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 const char copyright[] = 36 "@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)pstat.c 8.16 (Berkeley) 5/9/95"; 43 #endif 44 static const char rcsid[] = 45 "$FreeBSD$"; 46 #endif /* not lint */ 47 48 #include <sys/param.h> 49 #include <sys/time.h> 50 #include <sys/vnode.h> 51 #include <sys/ucred.h> 52 #define _KERNEL 53 #include <sys/file.h> 54 #include <ufs/ufs/quota.h> 55 #include <ufs/ufs/inode.h> 56 #include <sys/mount.h> 57 #include <sys/uio.h> 58 #include <sys/namei.h> 59 #include <miscfs/union/union.h> 60 #undef _KERNEL 61 #include <sys/stat.h> 62 #include <nfs/rpcv2.h> 63 #include <nfs/nfsproto.h> 64 #include <nfs/nfs.h> 65 #include <nfs/nfsnode.h> 66 #include <sys/ioctl.h> 67 #include <sys/ioctl_compat.h> /* XXX NTTYDISC is too well hidden */ 68 #include <sys/tty.h> 69 #include <sys/conf.h> 70 #include <sys/blist.h> 71 72 #include <sys/user.h> 73 #include <sys/sysctl.h> 74 75 #include <err.h> 76 #include <fcntl.h> 77 #include <kvm.h> 78 #include <limits.h> 79 #include <nlist.h> 80 #include <stdio.h> 81 #include <stdlib.h> 82 #include <string.h> 83 #include <unistd.h> 84 85 struct nlist nl[] = { 86 #define NLMANDATORYBEG 0 87 #define V_MOUNTLIST 0 88 { "_mountlist" }, /* address of head of mount list. */ 89 #define V_NUMV 1 90 { "_numvnodes" }, 91 #define FNL_NFILE 2 92 {"_nfiles"}, 93 #define FNL_MAXFILE 3 94 {"_maxfiles"}, 95 #define NLMANDATORYEND FNL_MAXFILE /* names up to here are mandatory */ 96 #define SCONS NLMANDATORYEND + 1 97 { "_cons" }, 98 #define SPTY NLMANDATORYEND + 2 99 { "_pt_tty" }, 100 #define SNPTY NLMANDATORYEND + 3 101 { "_npty" }, 102 103 104 105 #ifdef __FreeBSD__ 106 #define SCCONS (SNPTY+1) 107 { "_sccons" }, 108 #define NSCCONS (SNPTY+2) 109 { "_nsccons" }, 110 #define SIO (SNPTY+3) 111 { "_sio_tty" }, 112 #define NSIO (SNPTY+4) 113 { "_nsio_tty" }, 114 #define RC (SNPTY+5) 115 { "_rc_tty" }, 116 #define NRC (SNPTY+6) 117 { "_nrc_tty" }, 118 #define CY (SNPTY+7) 119 { "_cy_tty" }, 120 #define NCY (SNPTY+8) 121 { "_ncy_tty" }, 122 #define SI (SNPTY+9) 123 { "_si_tty" }, 124 #define NSI (SNPTY+10) 125 { "_si_Nports" }, 126 #endif 127 { "" } 128 }; 129 130 int usenumflag; 131 int totalflag; 132 int swapflag; 133 char *nlistf = NULL; 134 char *memf = NULL; 135 kvm_t *kd; 136 137 char *usagestr; 138 139 struct { 140 int m_flag; 141 const char *m_name; 142 } mnt_flags[] = { 143 { MNT_RDONLY, "rdonly" }, 144 { MNT_SYNCHRONOUS, "sync" }, 145 { MNT_NOEXEC, "noexec" }, 146 { MNT_NOSUID, "nosuid" }, 147 { MNT_NODEV, "nodev" }, 148 { MNT_UNION, "union" }, 149 { MNT_ASYNC, "async" }, 150 { MNT_SUIDDIR, "suiddir" }, 151 { MNT_SOFTDEP, "softdep" }, 152 { MNT_NOSYMFOLLOW, "nosymfollow" }, 153 { MNT_NOATIME, "noatime" }, 154 { MNT_NOCLUSTERR, "noclusterread" }, 155 { MNT_NOCLUSTERW, "noclusterwrite" }, 156 { MNT_EXRDONLY, "exrdonly" }, 157 { MNT_EXPORTED, "exported" }, 158 { MNT_DEFEXPORTED, "defexported" }, 159 { MNT_EXPORTANON, "exportanon" }, 160 { MNT_EXKERB, "exkerb" }, 161 { MNT_EXPUBLIC, "public" }, 162 { MNT_LOCAL, "local" }, 163 { MNT_QUOTA, "quota" }, 164 { MNT_ROOTFS, "rootfs" }, 165 { MNT_USER, "user" }, 166 { MNT_IGNORE, "ignore" }, 167 { MNT_UPDATE, "update" }, 168 { MNT_DELEXPORT, "delexport" }, 169 { MNT_RELOAD, "reload" }, 170 { MNT_FORCE, "force" }, 171 { MNT_SNAPSHOT, "snapshot" }, 172 { 0 } 173 }; 174 175 176 #define SVAR(var) __STRING(var) /* to force expansion */ 177 #define KGET(idx, var) \ 178 KGET1(idx, &var, sizeof(var), SVAR(var)) 179 #define KGET1(idx, p, s, msg) \ 180 KGET2(nl[idx].n_value, p, s, msg) 181 #define KGET2(addr, p, s, msg) \ 182 if (kvm_read(kd, (u_long)(addr), p, s) != s) \ 183 warnx("cannot read %s: %s", msg, kvm_geterr(kd)) 184 #define KGETN(idx, var) \ 185 KGET1N(idx, &var, sizeof(var), SVAR(var)) 186 #define KGET1N(idx, p, s, msg) \ 187 KGET2N(nl[idx].n_value, p, s, msg) 188 #define KGET2N(addr, p, s, msg) \ 189 ((kvm_read(kd, (u_long)(addr), p, s) == s) ? 1 : 0) 190 #define KGETRET(addr, p, s, msg) \ 191 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \ 192 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 193 return (0); \ 194 } 195 196 void filemode __P((void)); 197 int getfiles __P((char **, int *)); 198 struct mount * 199 getmnt __P((struct mount *)); 200 struct e_vnode * 201 kinfo_vnodes __P((int *)); 202 struct e_vnode * 203 loadvnodes __P((int *)); 204 void mount_print __P((struct mount *)); 205 void nfs_header __P((void)); 206 int nfs_print __P((struct vnode *)); 207 void swapmode __P((void)); 208 void ttymode __P((void)); 209 void ttyprt __P((struct tty *, int)); 210 void ttytype __P((struct tty *, char *, int, int, int)); 211 void ufs_header __P((void)); 212 int ufs_print __P((struct vnode *)); 213 void union_header __P((void)); 214 int union_print __P((struct vnode *)); 215 static void usage __P((void)); 216 void vnode_header __P((void)); 217 void vnode_print __P((struct vnode *, struct vnode *)); 218 void vnodemode __P((void)); 219 220 int 221 main(argc, argv) 222 int argc; 223 char *argv[]; 224 { 225 int ch, i, quit, ret; 226 int fileflag, ttyflag, vnodeflag; 227 char buf[_POSIX2_LINE_MAX],*opts; 228 229 fileflag = swapflag = ttyflag = vnodeflag = 0; 230 231 /* We will behave like good old swapinfo if thus invoked */ 232 opts = strrchr(argv[0],'/'); 233 if (opts) 234 opts++; 235 else 236 opts = argv[0]; 237 if (!strcmp(opts,"swapinfo")) { 238 swapflag = 1; 239 opts = "kM:N:"; 240 usagestr = "swapinfo [-k] [-M core] [-N system]"; 241 } else { 242 opts = "TM:N:fiknstv"; 243 usagestr = "pstat [-Tfknstv] [-M core] [-N system]"; 244 } 245 246 while ((ch = getopt(argc, argv, opts)) != -1) 247 switch (ch) { 248 case 'f': 249 fileflag = 1; 250 break; 251 case 'k': 252 putenv("BLOCKSIZE=1K"); 253 break; 254 case 'M': 255 memf = optarg; 256 break; 257 case 'N': 258 nlistf = optarg; 259 break; 260 case 'n': 261 usenumflag = 1; 262 break; 263 case 's': 264 ++swapflag; 265 break; 266 case 'T': 267 totalflag = 1; 268 break; 269 case 't': 270 ttyflag = 1; 271 break; 272 case 'v': 273 case 'i': /* Backward compatibility. */ 274 errx(1, "vnode mode not supported"); 275 #if 0 276 vnodeflag = 1; 277 break; 278 #endif 279 default: 280 usage(); 281 } 282 argc -= optind; 283 argv += optind; 284 285 /* 286 * Discard setgid privileges if not the running kernel so that bad 287 * guys can't print interesting stuff from kernel memory. 288 */ 289 if (nlistf != NULL || memf != NULL) 290 (void)setgid(getgid()); 291 292 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0) 293 errx(1, "kvm_openfiles: %s", buf); 294 if ((ret = kvm_nlist(kd, nl)) != 0) { 295 if (ret == -1) 296 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 297 for (i = NLMANDATORYBEG, quit = 0; i <= NLMANDATORYEND; i++) 298 if (!nl[i].n_value) { 299 quit = 1; 300 warnx("undefined symbol: %s", nl[i].n_name); 301 } 302 if (quit) 303 exit(1); 304 } 305 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag)) 306 usage(); 307 if (fileflag || totalflag) 308 filemode(); 309 if (vnodeflag) 310 vnodemode(); 311 if (ttyflag) 312 ttymode(); 313 if (swapflag || totalflag) 314 swapmode(); 315 exit (0); 316 } 317 318 static void 319 usage() 320 { 321 fprintf(stderr, "usage: %s\n", usagestr); 322 exit (1); 323 } 324 325 struct e_vnode { 326 struct vnode *avnode; 327 struct vnode vnode; 328 }; 329 330 void 331 vnodemode() 332 { 333 struct e_vnode *e_vnodebase, *endvnode, *evp; 334 struct vnode *vp; 335 struct mount *maddr, *mp; 336 int numvnodes; 337 338 e_vnodebase = loadvnodes(&numvnodes); 339 if (totalflag) { 340 (void)printf("%7d vnodes\n", numvnodes); 341 return; 342 } 343 endvnode = e_vnodebase + numvnodes; 344 (void)printf("%d active vnodes\n", numvnodes); 345 346 347 #define ST mp->mnt_stat 348 maddr = NULL; 349 for (evp = e_vnodebase; evp < endvnode; evp++) { 350 vp = &evp->vnode; 351 if (vp->v_mount != maddr) { 352 /* 353 * New filesystem 354 */ 355 if ((mp = getmnt(vp->v_mount)) == NULL) 356 continue; 357 maddr = vp->v_mount; 358 mount_print(mp); 359 vnode_header(); 360 if (!strcmp(ST.f_fstypename, "ufs") || 361 !strcmp(ST.f_fstypename, "mfs")) 362 ufs_header(); 363 else if (!strcmp(ST.f_fstypename, "nfs")) 364 nfs_header(); 365 else if (!strcmp(ST.f_fstypename, "union")) 366 union_header(); 367 (void)printf("\n"); 368 } 369 vnode_print(evp->avnode, vp); 370 if (!strcmp(ST.f_fstypename, "ufs") || 371 !strcmp(ST.f_fstypename, "mfs")) 372 ufs_print(vp); 373 else if (!strcmp(ST.f_fstypename, "nfs")) 374 nfs_print(vp); 375 else if (!strcmp(ST.f_fstypename, "union")) 376 union_print(vp); 377 (void)printf("\n"); 378 } 379 free(e_vnodebase); 380 } 381 382 void 383 vnode_header() 384 { 385 (void)printf("ADDR TYP VFLAG USE HOLD"); 386 } 387 388 void 389 vnode_print(avnode, vp) 390 struct vnode *avnode; 391 struct vnode *vp; 392 { 393 char *type, flags[16]; 394 char *fp = flags; 395 int flag; 396 397 /* 398 * set type 399 */ 400 switch (vp->v_type) { 401 case VNON: 402 type = "non"; break; 403 case VREG: 404 type = "reg"; break; 405 case VDIR: 406 type = "dir"; break; 407 case VBLK: 408 type = "blk"; break; 409 case VCHR: 410 type = "chr"; break; 411 case VLNK: 412 type = "lnk"; break; 413 case VSOCK: 414 type = "soc"; break; 415 case VFIFO: 416 type = "fif"; break; 417 case VBAD: 418 type = "bad"; break; 419 default: 420 type = "unk"; break; 421 } 422 /* 423 * gather flags 424 */ 425 flag = vp->v_flag; 426 if (flag & VROOT) 427 *fp++ = 'R'; 428 if (flag & VTEXT) 429 *fp++ = 'T'; 430 if (flag & VSYSTEM) 431 *fp++ = 'S'; 432 if (flag & VISTTY) 433 *fp++ = 't'; 434 if (flag & VXLOCK) 435 *fp++ = 'L'; 436 if (flag & VXWANT) 437 *fp++ = 'W'; 438 if (flag & VBWAIT) 439 *fp++ = 'B'; 440 if (flag & VOBJBUF) 441 *fp++ = 'V'; 442 if (flag & VCOPYONWRITE) 443 *fp++ = 'C'; 444 if (flag & VAGE) 445 *fp++ = 'a'; 446 if (flag & VOLOCK) 447 *fp++ = 'l'; 448 if (flag & VOWANT) 449 *fp++ = 'w'; 450 if (flag & VDOOMED) 451 *fp++ = 'D'; 452 if (flag & VFREE) 453 *fp++ = 'F'; 454 if (flag & VONWORKLST) 455 *fp++ = 'O'; 456 if (flag & VMOUNT) 457 *fp++ = 'M'; 458 459 if (flag == 0) 460 *fp++ = '-'; 461 *fp = '\0'; 462 (void)printf("%8lx %s %5s %4d %4d", 463 (u_long)(void *)avnode, type, flags, vp->v_usecount, vp->v_holdcnt); 464 } 465 466 void 467 ufs_header() 468 { 469 (void)printf(" FILEID IFLAG RDEV|SZ"); 470 } 471 472 int 473 ufs_print(vp) 474 struct vnode *vp; 475 { 476 int flag; 477 struct inode inode, *ip = &inode; 478 char flagbuf[16], *flags = flagbuf; 479 char *name; 480 mode_t type; 481 482 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 483 flag = ip->i_flag; 484 if (flag & IN_ACCESS) 485 *flags++ = 'A'; 486 if (flag & IN_CHANGE) 487 *flags++ = 'C'; 488 if (flag & IN_UPDATE) 489 *flags++ = 'U'; 490 if (flag & IN_MODIFIED) 491 *flags++ = 'M'; 492 if (flag & IN_RENAME) 493 *flags++ = 'R'; 494 if (flag & IN_SHLOCK) 495 *flags++ = 'S'; 496 if (flag & IN_EXLOCK) 497 *flags++ = 'E'; 498 if (flag & IN_HASHED) 499 *flags++ = 'H'; 500 if (flag & IN_LAZYMOD) 501 *flags++ = 'L'; 502 if (flag == 0) 503 *flags++ = '-'; 504 *flags = '\0'; 505 506 (void)printf(" %6d %5s", ip->i_number, flagbuf); 507 type = ip->i_mode & S_IFMT; 508 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) 509 if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL)) 510 (void)printf(" %2d,%-2d", 511 major(ip->i_rdev), minor(ip->i_rdev)); 512 else 513 (void)printf(" %7s", name); 514 else 515 (void)printf(" %7qd", ip->i_size); 516 return (0); 517 } 518 519 void 520 nfs_header() 521 { 522 (void)printf(" FILEID NFLAG RDEV|SZ"); 523 } 524 525 int 526 nfs_print(vp) 527 struct vnode *vp; 528 { 529 struct nfsnode nfsnode, *np = &nfsnode; 530 char flagbuf[16], *flags = flagbuf; 531 int flag; 532 char *name; 533 mode_t type; 534 535 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode"); 536 flag = np->n_flag; 537 if (flag & NFLUSHWANT) 538 *flags++ = 'W'; 539 if (flag & NFLUSHINPROG) 540 *flags++ = 'P'; 541 if (flag & NMODIFIED) 542 *flags++ = 'M'; 543 if (flag & NWRITEERR) 544 *flags++ = 'E'; 545 if (flag & NQNFSNONCACHE) 546 *flags++ = 'X'; 547 if (flag & NQNFSWRITE) 548 *flags++ = 'O'; 549 if (flag & NQNFSEVICTED) 550 *flags++ = 'G'; 551 if (flag & NACC) 552 *flags++ = 'A'; 553 if (flag & NUPD) 554 *flags++ = 'U'; 555 if (flag & NCHG) 556 *flags++ = 'C'; 557 if (flag & NLOCKED) 558 *flags++ = 'L'; 559 if (flag & NWANTED) 560 *flags++ = 'w'; 561 if (flag == 0) 562 *flags++ = '-'; 563 *flags = '\0'; 564 565 #define VT np->n_vattr 566 (void)printf(" %6ld %5s", VT.va_fileid, flagbuf); 567 type = VT.va_mode & S_IFMT; 568 if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode)) 569 if (usenumflag || ((name = devname(VT.va_rdev, type)) == NULL)) 570 (void)printf(" %2d,%-2d", 571 major(VT.va_rdev), minor(VT.va_rdev)); 572 else 573 (void)printf(" %7s", name); 574 else 575 (void)printf(" %7qd", np->n_size); 576 return (0); 577 } 578 579 void 580 union_header() 581 { 582 (void)printf(" UPPER LOWER"); 583 } 584 585 int 586 union_print(vp) 587 struct vnode *vp; 588 { 589 struct union_node unode, *up = &unode; 590 591 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode"); 592 593 (void)printf(" %8lx %8lx", (u_long)(void *)up->un_uppervp, 594 (u_long)(void *)up->un_lowervp); 595 return (0); 596 } 597 598 /* 599 * Given a pointer to a mount structure in kernel space, 600 * read it in and return a usable pointer to it. 601 */ 602 struct mount * 603 getmnt(maddr) 604 struct mount *maddr; 605 { 606 static struct mtab { 607 struct mtab *next; 608 struct mount *maddr; 609 struct mount mount; 610 } *mhead = NULL; 611 struct mtab *mt; 612 613 for (mt = mhead; mt != NULL; mt = mt->next) 614 if (maddr == mt->maddr) 615 return (&mt->mount); 616 if ((mt = malloc(sizeof(struct mtab))) == NULL) 617 errx(1, "malloc"); 618 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table"); 619 mt->maddr = maddr; 620 mt->next = mhead; 621 mhead = mt; 622 return (&mt->mount); 623 } 624 625 void 626 mount_print(mp) 627 struct mount *mp; 628 { 629 int flags; 630 const char *type; 631 632 #define ST mp->mnt_stat 633 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename, 634 ST.f_mntfromname, ST.f_mntonname); 635 if ((flags = mp->mnt_flag)) { 636 int i; 637 const char *sep = " ("; 638 639 for (i = 0; mnt_flags[i].m_flag; i++) { 640 if (flags & mnt_flags[i].m_flag) { 641 (void)printf("%s%s", sep, mnt_flags[i].m_name); 642 flags &= ~mnt_flags[i].m_flag; 643 sep = ","; 644 } 645 } 646 if (flags) 647 (void)printf("%sunknown_flags:%x", sep, flags); 648 (void)printf(")"); 649 } 650 (void)printf("\n"); 651 #undef ST 652 } 653 654 struct e_vnode * 655 loadvnodes(avnodes) 656 int *avnodes; 657 { 658 int mib[2]; 659 size_t copysize; 660 struct e_vnode *vnodebase; 661 662 if (memf != NULL) { 663 /* 664 * do it by hand 665 */ 666 return (kinfo_vnodes(avnodes)); 667 } 668 mib[0] = CTL_KERN; 669 mib[1] = KERN_VNODE; 670 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1) 671 err(1, "sysctl: KERN_VNODE"); 672 if ((vnodebase = malloc(copysize)) == NULL) 673 errx(1, "malloc"); 674 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1) 675 err(1, "sysctl: KERN_VNODE"); 676 if (copysize % sizeof(struct e_vnode)) 677 errx(1, "vnode size mismatch"); 678 *avnodes = copysize / sizeof(struct e_vnode); 679 680 return (vnodebase); 681 } 682 683 /* 684 * simulate what a running kernel does in in kinfo_vnode 685 */ 686 struct e_vnode * 687 kinfo_vnodes(avnodes) 688 int *avnodes; 689 { 690 struct mntlist mountlist; 691 struct mount *mp, mount, *mp_next; 692 struct vnode *vp, vnode, *vp_next; 693 char *vbuf, *evbuf, *bp; 694 int num, numvnodes; 695 696 #define VPTRSZ sizeof(struct vnode *) 697 #define VNODESZ sizeof(struct vnode) 698 699 KGET(V_NUMV, numvnodes); 700 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL) 701 errx(1, "malloc"); 702 bp = vbuf; 703 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ); 704 KGET(V_MOUNTLIST, mountlist); 705 for (num = 0, mp = TAILQ_FIRST(&mountlist); ; mp = mp_next) { 706 KGET2(mp, &mount, sizeof(mount), "mount entry"); 707 mp_next = TAILQ_NEXT(&mount, mnt_list); 708 for (vp = LIST_FIRST(&mount.mnt_vnodelist); 709 vp != NULL; vp = vp_next) { 710 KGET2(vp, &vnode, sizeof(vnode), "vnode"); 711 vp_next = LIST_NEXT(&vnode, v_mntvnodes); 712 if ((bp + VPTRSZ + VNODESZ) > evbuf) 713 /* XXX - should realloc */ 714 errx(1, "no more room for vnodes"); 715 memmove(bp, &vp, VPTRSZ); 716 bp += VPTRSZ; 717 memmove(bp, &vnode, VNODESZ); 718 bp += VNODESZ; 719 num++; 720 } 721 if (mp == TAILQ_LAST(&mountlist, mntlist)) 722 break; 723 } 724 *avnodes = num; 725 return ((struct e_vnode *)vbuf); 726 } 727 728 const char hdr[] = 729 " LINE RAW CAN OUT IHIWT ILOWT OHWT LWT COL STATE SESS PGID DISC\n"; 730 int ttyspace = 128; 731 732 void 733 ttymode() 734 { 735 struct tty *tty; 736 struct tty ttyb[1000]; 737 int error, len, i; 738 739 (void)printf(hdr); 740 len = sizeof(ttyb); 741 error = sysctlbyname("kern.ttys", &ttyb, &len, 0, 0); 742 if (!error) { 743 len /= sizeof(ttyb[0]); 744 for (i = 0; i < len; i++) { 745 ttyprt(&ttyb[i], 0); 746 } 747 } 748 if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL) 749 errx(1, "malloc"); 750 if (nl[SCONS].n_type != 0) { 751 (void)printf("1 console\n"); 752 KGET(SCONS, *tty); 753 ttyprt(&tty[0], 0); 754 } 755 #ifdef __FreeBSD__ 756 if (nl[NSCCONS].n_type != 0) 757 ttytype(tty, "vty", SCCONS, NSCCONS, 0); 758 if (nl[NSIO].n_type != 0) 759 ttytype(tty, "sio", SIO, NSIO, 0); 760 if (nl[NRC].n_type != 0) 761 ttytype(tty, "rc", RC, NRC, 0); 762 if (nl[NCY].n_type != 0) 763 ttytype(tty, "cy", CY, NCY, 0); 764 if (nl[NSI].n_type != 0) 765 ttytype(tty, "si", SI, NSI, 1); 766 #endif 767 if (nl[SNPTY].n_type != 0) 768 ttytype(tty, "pty", SPTY, SNPTY, 0); 769 } 770 771 void 772 ttytype(tty, name, type, number, indir) 773 struct tty *tty; 774 char *name; 775 int type, number, indir; 776 { 777 struct tty *tp; 778 int ntty; 779 struct tty **ttyaddr; 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 errx(1, "realloc"); 789 } 790 if (indir) { 791 KGET(type, ttyaddr); 792 KGET2(ttyaddr, tty, ntty * sizeof(struct tty), "tty structs"); 793 } else { 794 KGET1(type, tty, ntty * sizeof(struct tty), "tty structs"); 795 } 796 (void)printf(hdr); 797 for (tp = tty; tp < &tty[ntty]; tp++) 798 ttyprt(tp, tp - tty); 799 } 800 801 struct { 802 int flag; 803 char val; 804 } ttystates[] = { 805 #ifdef TS_WOPEN 806 { TS_WOPEN, 'W'}, 807 #endif 808 { TS_ISOPEN, 'O'}, 809 { TS_CARR_ON, 'C'}, 810 #ifdef TS_CONNECTED 811 { TS_CONNECTED, 'c'}, 812 #endif 813 { TS_TIMEOUT, 'T'}, 814 { TS_FLUSH, 'F'}, 815 { TS_BUSY, 'B'}, 816 #ifdef TS_ASLEEP 817 { TS_ASLEEP, 'A'}, 818 #endif 819 #ifdef TS_SO_OLOWAT 820 { TS_SO_OLOWAT, 'A'}, 821 #endif 822 #ifdef TS_SO_OCOMPLETE 823 { TS_SO_OCOMPLETE, 'a'}, 824 #endif 825 { TS_XCLUDE, 'X'}, 826 { TS_TTSTOP, 'S'}, 827 #ifdef TS_CAR_OFLOW 828 { TS_CAR_OFLOW, 'm'}, 829 #endif 830 #ifdef TS_CTS_OFLOW 831 { TS_CTS_OFLOW, 'o'}, 832 #endif 833 #ifdef TS_DSR_OFLOW 834 { TS_DSR_OFLOW, 'd'}, 835 #endif 836 { TS_TBLOCK, 'K'}, 837 { TS_ASYNC, 'Y'}, 838 { TS_BKSL, 'D'}, 839 { TS_ERASE, 'E'}, 840 { TS_LNCH, 'L'}, 841 { TS_TYPEN, 'P'}, 842 { TS_CNTTB, 'N'}, 843 #ifdef TS_CAN_BYPASS_L_RINT 844 { TS_CAN_BYPASS_L_RINT, 'l'}, 845 #endif 846 #ifdef TS_SNOOP 847 { TS_SNOOP, 's'}, 848 #endif 849 #ifdef TS_ZOMBIE 850 { TS_ZOMBIE, 'Z'}, 851 #endif 852 { 0, '\0'}, 853 }; 854 855 void 856 ttyprt(tp, line) 857 struct tty *tp; 858 int line; 859 { 860 int i, j; 861 pid_t pgid; 862 char *name, state[20]; 863 864 if (usenumflag || tp->t_dev == 0 || 865 (name = devname(tp->t_dev, S_IFCHR)) == NULL) 866 (void)printf("%7d ", line); 867 else 868 (void)printf("%7s ", name); 869 (void)printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc); 870 (void)printf("%3d %5d %5d %4d %3d %7d ", tp->t_outq.c_cc, 871 tp->t_ihiwat, tp->t_ilowat, tp->t_ohiwat, tp->t_olowat, 872 tp->t_column); 873 for (i = j = 0; ttystates[i].flag; i++) 874 if (tp->t_state&ttystates[i].flag) 875 state[j++] = ttystates[i].val; 876 if (j == 0) 877 state[j++] = '-'; 878 state[j] = '\0'; 879 (void)printf("%-6s %8lx", state, (u_long)(void *)tp->t_session); 880 pgid = 0; 881 if (tp->t_pgrp != NULL) 882 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid"); 883 (void)printf("%6d ", pgid); 884 switch (tp->t_line) { 885 case TTYDISC: 886 (void)printf("term\n"); 887 break; 888 case NTTYDISC: 889 (void)printf("ntty\n"); 890 break; 891 case SLIPDISC: 892 (void)printf("slip\n"); 893 break; 894 case PPPDISC: 895 (void)printf("ppp\n"); 896 break; 897 default: 898 (void)printf("%d\n", tp->t_line); 899 break; 900 } 901 } 902 903 void 904 filemode() 905 { 906 struct file *fp; 907 struct file *addr; 908 char *buf, flagbuf[16], *fbp; 909 int len, maxfile, nfile; 910 static char *dtypes[] = { "???", "inode", "socket" }; 911 912 KGET(FNL_MAXFILE, maxfile); 913 if (totalflag) { 914 KGET(FNL_NFILE, nfile); 915 (void)printf("%3d/%3d files\n", nfile, maxfile); 916 return; 917 } 918 if (getfiles(&buf, &len) == -1) 919 return; 920 /* 921 * Getfiles returns in malloc'd memory a pointer to the first file 922 * structure, and then an array of file structs (whose addresses are 923 * derivable from the previous entry). 924 */ 925 addr = LIST_FIRST((struct filelist *)buf); 926 fp = (struct file *)(buf + sizeof(struct filelist)); 927 nfile = (len - sizeof(struct filelist)) / sizeof(struct file); 928 929 (void)printf("%d/%d open files\n", nfile, maxfile); 930 (void)printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n"); 931 for (; (char *)fp < buf + len; addr = LIST_NEXT(fp, f_list), fp++) { 932 if ((unsigned)fp->f_type > DTYPE_SOCKET) 933 continue; 934 (void)printf("%8lx ", (u_long)(void *)addr); 935 (void)printf("%-8.8s", dtypes[fp->f_type]); 936 fbp = flagbuf; 937 if (fp->f_flag & FREAD) 938 *fbp++ = 'R'; 939 if (fp->f_flag & FWRITE) 940 *fbp++ = 'W'; 941 if (fp->f_flag & FAPPEND) 942 *fbp++ = 'A'; 943 #ifdef FSHLOCK /* currently gone */ 944 if (fp->f_flag & FSHLOCK) 945 *fbp++ = 'S'; 946 if (fp->f_flag & FEXLOCK) 947 *fbp++ = 'X'; 948 #endif 949 if (fp->f_flag & FASYNC) 950 *fbp++ = 'I'; 951 *fbp = '\0'; 952 (void)printf("%6s %3d", flagbuf, fp->f_count); 953 (void)printf(" %3d", fp->f_msgcount); 954 (void)printf(" %8lx", (u_long)(void *)fp->f_data); 955 if (fp->f_offset < 0) 956 (void)printf(" %qx\n", fp->f_offset); 957 else 958 (void)printf(" %qd\n", fp->f_offset); 959 } 960 free(buf); 961 } 962 963 int 964 getfiles(abuf, alen) 965 char **abuf; 966 int *alen; 967 { 968 size_t len; 969 int mib[2]; 970 char *buf; 971 972 /* 973 * XXX 974 * Add emulation of KINFO_FILE here. 975 */ 976 if (memf != NULL) 977 errx(1, "files on dead kernel, not implemented"); 978 979 mib[0] = CTL_KERN; 980 mib[1] = KERN_FILE; 981 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 982 warn("sysctl: KERN_FILE"); 983 return (-1); 984 } 985 if ((buf = malloc(len)) == NULL) 986 errx(1, "malloc"); 987 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 988 warn("sysctl: KERN_FILE"); 989 return (-1); 990 } 991 *abuf = buf; 992 *alen = len; 993 return (0); 994 } 995 996 /* 997 * swapmode is based on a program called swapinfo written 998 * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 999 */ 1000 void 1001 swapmode(void) 1002 { 1003 struct kvm_swap kswap[16]; 1004 int i; 1005 int n; 1006 int pagesize = getpagesize(); 1007 const char *header; 1008 int hlen; 1009 long blocksize; 1010 1011 n = kvm_getswapinfo( 1012 kd, 1013 kswap, 1014 sizeof(kswap)/sizeof(kswap[0]), 1015 ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX 1016 ); 1017 1018 #define CONVERT(v) ((int)((quad_t)(v) * pagesize / blocksize)) 1019 1020 header = getbsize(&hlen, &blocksize); 1021 if (totalflag == 0) { 1022 (void)printf("%-15s %*s %8s %8s %8s %s\n", 1023 "Device", hlen, header, 1024 "Used", "Avail", "Capacity", "Type"); 1025 1026 for (i = 0; i < n; ++i) { 1027 (void)printf( 1028 "%-15s %*d ", 1029 kswap[i].ksw_devname, 1030 hlen, 1031 CONVERT(kswap[i].ksw_total) 1032 ); 1033 (void)printf( 1034 "%8d %8d %5.0f%% %s\n", 1035 CONVERT(kswap[i].ksw_used), 1036 CONVERT(kswap[i].ksw_total - kswap[i].ksw_used), 1037 (double)kswap[i].ksw_used * 100.0 / 1038 (double)kswap[i].ksw_total, 1039 (kswap[i].ksw_flags & SW_SEQUENTIAL) ? 1040 "Sequential" : "Interleaved" 1041 ); 1042 } 1043 } 1044 1045 if (totalflag) { 1046 blocksize = 1024 * 1024; 1047 1048 (void)printf( 1049 "%dM/%dM swap space\n", 1050 CONVERT(kswap[n].ksw_used), 1051 CONVERT(kswap[n].ksw_total) 1052 ); 1053 } else if (n > 1) { 1054 (void)printf( 1055 "%-15s %*d %8d %8d %5.0f%%\n", 1056 "Total", 1057 hlen, 1058 CONVERT(kswap[n].ksw_total), 1059 CONVERT(kswap[n].ksw_used), 1060 CONVERT(kswap[n].ksw_total - kswap[n].ksw_used), 1061 (double)kswap[n].ksw_used * 100.0 / 1062 (double)kswap[n].ksw_total 1063 ); 1064 } 1065 } 1066