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 <fs/unionfs/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_HASHED) 495 *flags++ = 'H'; 496 if (flag & IN_LAZYMOD) 497 *flags++ = 'L'; 498 if (flag == 0) 499 *flags++ = '-'; 500 *flags = '\0'; 501 502 (void)printf(" %6d %5s", ip->i_number, flagbuf); 503 type = ip->i_mode & S_IFMT; 504 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) 505 if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL)) 506 (void)printf(" %2d,%-2d", 507 major(ip->i_rdev), minor(ip->i_rdev)); 508 else 509 (void)printf(" %7s", name); 510 else 511 (void)printf(" %7qd", ip->i_size); 512 return (0); 513 } 514 515 void 516 nfs_header() 517 { 518 (void)printf(" FILEID NFLAG RDEV|SZ"); 519 } 520 521 int 522 nfs_print(vp) 523 struct vnode *vp; 524 { 525 struct nfsnode nfsnode, *np = &nfsnode; 526 char flagbuf[16], *flags = flagbuf; 527 int flag; 528 char *name; 529 mode_t type; 530 531 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode"); 532 flag = np->n_flag; 533 if (flag & NFLUSHWANT) 534 *flags++ = 'W'; 535 if (flag & NFLUSHINPROG) 536 *flags++ = 'P'; 537 if (flag & NMODIFIED) 538 *flags++ = 'M'; 539 if (flag & NWRITEERR) 540 *flags++ = 'E'; 541 if (flag & NQNFSNONCACHE) 542 *flags++ = 'X'; 543 if (flag & NQNFSWRITE) 544 *flags++ = 'O'; 545 if (flag & NQNFSEVICTED) 546 *flags++ = 'G'; 547 if (flag & NACC) 548 *flags++ = 'A'; 549 if (flag & NUPD) 550 *flags++ = 'U'; 551 if (flag & NCHG) 552 *flags++ = 'C'; 553 if (flag & NLOCKED) 554 *flags++ = 'L'; 555 if (flag & NWANTED) 556 *flags++ = 'w'; 557 if (flag == 0) 558 *flags++ = '-'; 559 *flags = '\0'; 560 561 #define VT np->n_vattr 562 (void)printf(" %6ld %5s", VT.va_fileid, flagbuf); 563 type = VT.va_mode & S_IFMT; 564 if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode)) 565 if (usenumflag || ((name = devname(VT.va_rdev, type)) == NULL)) 566 (void)printf(" %2d,%-2d", 567 major(VT.va_rdev), minor(VT.va_rdev)); 568 else 569 (void)printf(" %7s", name); 570 else 571 (void)printf(" %7qd", np->n_size); 572 return (0); 573 } 574 575 void 576 union_header() 577 { 578 (void)printf(" UPPER LOWER"); 579 } 580 581 int 582 union_print(vp) 583 struct vnode *vp; 584 { 585 struct union_node unode, *up = &unode; 586 587 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode"); 588 589 (void)printf(" %8lx %8lx", (u_long)(void *)up->un_uppervp, 590 (u_long)(void *)up->un_lowervp); 591 return (0); 592 } 593 594 /* 595 * Given a pointer to a mount structure in kernel space, 596 * read it in and return a usable pointer to it. 597 */ 598 struct mount * 599 getmnt(maddr) 600 struct mount *maddr; 601 { 602 static struct mtab { 603 struct mtab *next; 604 struct mount *maddr; 605 struct mount mount; 606 } *mhead = NULL; 607 struct mtab *mt; 608 609 for (mt = mhead; mt != NULL; mt = mt->next) 610 if (maddr == mt->maddr) 611 return (&mt->mount); 612 if ((mt = malloc(sizeof(struct mtab))) == NULL) 613 errx(1, "malloc"); 614 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table"); 615 mt->maddr = maddr; 616 mt->next = mhead; 617 mhead = mt; 618 return (&mt->mount); 619 } 620 621 void 622 mount_print(mp) 623 struct mount *mp; 624 { 625 int flags; 626 const char *type; 627 628 #define ST mp->mnt_stat 629 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename, 630 ST.f_mntfromname, ST.f_mntonname); 631 if ((flags = mp->mnt_flag)) { 632 int i; 633 const char *sep = " ("; 634 635 for (i = 0; mnt_flags[i].m_flag; i++) { 636 if (flags & mnt_flags[i].m_flag) { 637 (void)printf("%s%s", sep, mnt_flags[i].m_name); 638 flags &= ~mnt_flags[i].m_flag; 639 sep = ","; 640 } 641 } 642 if (flags) 643 (void)printf("%sunknown_flags:%x", sep, flags); 644 (void)printf(")"); 645 } 646 (void)printf("\n"); 647 #undef ST 648 } 649 650 struct e_vnode * 651 loadvnodes(avnodes) 652 int *avnodes; 653 { 654 int mib[2]; 655 size_t copysize; 656 struct e_vnode *vnodebase; 657 658 if (memf != NULL) { 659 /* 660 * do it by hand 661 */ 662 return (kinfo_vnodes(avnodes)); 663 } 664 mib[0] = CTL_KERN; 665 mib[1] = KERN_VNODE; 666 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1) 667 err(1, "sysctl: KERN_VNODE"); 668 if ((vnodebase = malloc(copysize)) == NULL) 669 errx(1, "malloc"); 670 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1) 671 err(1, "sysctl: KERN_VNODE"); 672 if (copysize % sizeof(struct e_vnode)) 673 errx(1, "vnode size mismatch"); 674 *avnodes = copysize / sizeof(struct e_vnode); 675 676 return (vnodebase); 677 } 678 679 /* 680 * simulate what a running kernel does in in kinfo_vnode 681 */ 682 struct e_vnode * 683 kinfo_vnodes(avnodes) 684 int *avnodes; 685 { 686 struct mntlist mountlist; 687 struct mount *mp, mount, *mp_next; 688 struct vnode *vp, vnode, *vp_next; 689 char *vbuf, *evbuf, *bp; 690 int num, numvnodes; 691 692 #define VPTRSZ sizeof(struct vnode *) 693 #define VNODESZ sizeof(struct vnode) 694 695 KGET(V_NUMV, numvnodes); 696 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL) 697 errx(1, "malloc"); 698 bp = vbuf; 699 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ); 700 KGET(V_MOUNTLIST, mountlist); 701 for (num = 0, mp = TAILQ_FIRST(&mountlist); ; mp = mp_next) { 702 KGET2(mp, &mount, sizeof(mount), "mount entry"); 703 mp_next = TAILQ_NEXT(&mount, mnt_list); 704 for (vp = LIST_FIRST(&mount.mnt_vnodelist); 705 vp != NULL; vp = vp_next) { 706 KGET2(vp, &vnode, sizeof(vnode), "vnode"); 707 vp_next = LIST_NEXT(&vnode, v_mntvnodes); 708 if ((bp + VPTRSZ + VNODESZ) > evbuf) 709 /* XXX - should realloc */ 710 errx(1, "no more room for vnodes"); 711 memmove(bp, &vp, VPTRSZ); 712 bp += VPTRSZ; 713 memmove(bp, &vnode, VNODESZ); 714 bp += VNODESZ; 715 num++; 716 } 717 if (mp == TAILQ_LAST(&mountlist, mntlist)) 718 break; 719 } 720 *avnodes = num; 721 return ((struct e_vnode *)vbuf); 722 } 723 724 const char hdr[] = 725 " LINE RAW CAN OUT IHIWT ILOWT OHWT LWT COL STATE SESS PGID DISC\n"; 726 int ttyspace = 128; 727 728 void 729 ttymode() 730 { 731 struct tty *tty; 732 struct tty ttyb[1000]; 733 int error, len, i; 734 735 (void)printf(hdr); 736 len = sizeof(ttyb); 737 error = sysctlbyname("kern.ttys", &ttyb, &len, 0, 0); 738 if (!error) { 739 len /= sizeof(ttyb[0]); 740 for (i = 0; i < len; i++) { 741 ttyprt(&ttyb[i], 0); 742 } 743 } 744 if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL) 745 errx(1, "malloc"); 746 if (nl[SCONS].n_type != 0) { 747 (void)printf("1 console\n"); 748 KGET(SCONS, *tty); 749 ttyprt(&tty[0], 0); 750 } 751 #ifdef __FreeBSD__ 752 if (nl[NSCCONS].n_type != 0) 753 ttytype(tty, "vty", SCCONS, NSCCONS, 0); 754 if (nl[NSIO].n_type != 0) 755 ttytype(tty, "sio", SIO, NSIO, 0); 756 if (nl[NRC].n_type != 0) 757 ttytype(tty, "rc", RC, NRC, 0); 758 if (nl[NCY].n_type != 0) 759 ttytype(tty, "cy", CY, NCY, 0); 760 if (nl[NSI].n_type != 0) 761 ttytype(tty, "si", SI, NSI, 1); 762 #endif 763 if (nl[SNPTY].n_type != 0) 764 ttytype(tty, "pty", SPTY, SNPTY, 0); 765 } 766 767 void 768 ttytype(tty, name, type, number, indir) 769 struct tty *tty; 770 char *name; 771 int type, number, indir; 772 { 773 struct tty *tp; 774 int ntty; 775 struct tty **ttyaddr; 776 777 if (tty == NULL) 778 return; 779 KGET(number, ntty); 780 (void)printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines"); 781 if (ntty > ttyspace) { 782 ttyspace = ntty; 783 if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == 0) 784 errx(1, "realloc"); 785 } 786 if (indir) { 787 KGET(type, ttyaddr); 788 KGET2(ttyaddr, tty, ntty * sizeof(struct tty), "tty structs"); 789 } else { 790 KGET1(type, tty, ntty * sizeof(struct tty), "tty structs"); 791 } 792 (void)printf(hdr); 793 for (tp = tty; tp < &tty[ntty]; tp++) 794 ttyprt(tp, tp - tty); 795 } 796 797 struct { 798 int flag; 799 char val; 800 } ttystates[] = { 801 #ifdef TS_WOPEN 802 { TS_WOPEN, 'W'}, 803 #endif 804 { TS_ISOPEN, 'O'}, 805 { TS_CARR_ON, 'C'}, 806 #ifdef TS_CONNECTED 807 { TS_CONNECTED, 'c'}, 808 #endif 809 { TS_TIMEOUT, 'T'}, 810 { TS_FLUSH, 'F'}, 811 { TS_BUSY, 'B'}, 812 #ifdef TS_ASLEEP 813 { TS_ASLEEP, 'A'}, 814 #endif 815 #ifdef TS_SO_OLOWAT 816 { TS_SO_OLOWAT, 'A'}, 817 #endif 818 #ifdef TS_SO_OCOMPLETE 819 { TS_SO_OCOMPLETE, 'a'}, 820 #endif 821 { TS_XCLUDE, 'X'}, 822 { TS_TTSTOP, 'S'}, 823 #ifdef TS_CAR_OFLOW 824 { TS_CAR_OFLOW, 'm'}, 825 #endif 826 #ifdef TS_CTS_OFLOW 827 { TS_CTS_OFLOW, 'o'}, 828 #endif 829 #ifdef TS_DSR_OFLOW 830 { TS_DSR_OFLOW, 'd'}, 831 #endif 832 { TS_TBLOCK, 'K'}, 833 { TS_ASYNC, 'Y'}, 834 { TS_BKSL, 'D'}, 835 { TS_ERASE, 'E'}, 836 { TS_LNCH, 'L'}, 837 { TS_TYPEN, 'P'}, 838 { TS_CNTTB, 'N'}, 839 #ifdef TS_CAN_BYPASS_L_RINT 840 { TS_CAN_BYPASS_L_RINT, 'l'}, 841 #endif 842 #ifdef TS_SNOOP 843 { TS_SNOOP, 's'}, 844 #endif 845 #ifdef TS_ZOMBIE 846 { TS_ZOMBIE, 'Z'}, 847 #endif 848 { 0, '\0'}, 849 }; 850 851 void 852 ttyprt(tp, line) 853 struct tty *tp; 854 int line; 855 { 856 int i, j; 857 pid_t pgid; 858 char *name, state[20]; 859 860 if (usenumflag || tp->t_dev == 0 || 861 (name = devname(tp->t_dev, S_IFCHR)) == NULL) 862 (void)printf("%7d ", line); 863 else 864 (void)printf("%7s ", name); 865 (void)printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc); 866 (void)printf("%3d %5d %5d %4d %3d %7d ", tp->t_outq.c_cc, 867 tp->t_ihiwat, tp->t_ilowat, tp->t_ohiwat, tp->t_olowat, 868 tp->t_column); 869 for (i = j = 0; ttystates[i].flag; i++) 870 if (tp->t_state&ttystates[i].flag) 871 state[j++] = ttystates[i].val; 872 if (j == 0) 873 state[j++] = '-'; 874 state[j] = '\0'; 875 (void)printf("%-6s %8lx", state, (u_long)(void *)tp->t_session); 876 pgid = 0; 877 if (tp->t_pgrp != NULL) 878 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid"); 879 (void)printf("%6d ", pgid); 880 switch (tp->t_line) { 881 case TTYDISC: 882 (void)printf("term\n"); 883 break; 884 case NTTYDISC: 885 (void)printf("ntty\n"); 886 break; 887 case SLIPDISC: 888 (void)printf("slip\n"); 889 break; 890 case PPPDISC: 891 (void)printf("ppp\n"); 892 break; 893 default: 894 (void)printf("%d\n", tp->t_line); 895 break; 896 } 897 } 898 899 void 900 filemode() 901 { 902 struct file *fp; 903 struct file *addr; 904 char *buf, flagbuf[16], *fbp; 905 int len, maxfile, nfile; 906 static char *dtypes[] = { "???", "inode", "socket" }; 907 908 KGET(FNL_MAXFILE, maxfile); 909 if (totalflag) { 910 KGET(FNL_NFILE, nfile); 911 (void)printf("%3d/%3d files\n", nfile, maxfile); 912 return; 913 } 914 if (getfiles(&buf, &len) == -1) 915 return; 916 /* 917 * Getfiles returns in malloc'd memory a pointer to the first file 918 * structure, and then an array of file structs (whose addresses are 919 * derivable from the previous entry). 920 */ 921 addr = LIST_FIRST((struct filelist *)buf); 922 fp = (struct file *)(buf + sizeof(struct filelist)); 923 nfile = (len - sizeof(struct filelist)) / sizeof(struct file); 924 925 (void)printf("%d/%d open files\n", nfile, maxfile); 926 (void)printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n"); 927 for (; (char *)fp < buf + len; addr = LIST_NEXT(fp, f_list), fp++) { 928 if ((unsigned)fp->f_type > DTYPE_SOCKET) 929 continue; 930 (void)printf("%8lx ", (u_long)(void *)addr); 931 (void)printf("%-8.8s", dtypes[fp->f_type]); 932 fbp = flagbuf; 933 if (fp->f_flag & FREAD) 934 *fbp++ = 'R'; 935 if (fp->f_flag & FWRITE) 936 *fbp++ = 'W'; 937 if (fp->f_flag & FAPPEND) 938 *fbp++ = 'A'; 939 if (fp->f_flag & FASYNC) 940 *fbp++ = 'I'; 941 *fbp = '\0'; 942 (void)printf("%6s %3d", flagbuf, fp->f_count); 943 (void)printf(" %3d", fp->f_msgcount); 944 (void)printf(" %8lx", (u_long)(void *)fp->f_data); 945 if (fp->f_offset < 0) 946 (void)printf(" %qx\n", fp->f_offset); 947 else 948 (void)printf(" %qd\n", fp->f_offset); 949 } 950 free(buf); 951 } 952 953 int 954 getfiles(abuf, alen) 955 char **abuf; 956 int *alen; 957 { 958 size_t len; 959 int mib[2]; 960 char *buf; 961 962 /* 963 * XXX 964 * Add emulation of KINFO_FILE here. 965 */ 966 if (memf != NULL) 967 errx(1, "files on dead kernel, not implemented"); 968 969 mib[0] = CTL_KERN; 970 mib[1] = KERN_FILE; 971 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 972 warn("sysctl: KERN_FILE"); 973 return (-1); 974 } 975 if ((buf = malloc(len)) == NULL) 976 errx(1, "malloc"); 977 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 978 warn("sysctl: KERN_FILE"); 979 return (-1); 980 } 981 *abuf = buf; 982 *alen = len; 983 return (0); 984 } 985 986 /* 987 * swapmode is based on a program called swapinfo written 988 * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 989 */ 990 void 991 swapmode(void) 992 { 993 struct kvm_swap kswap[16]; 994 int i; 995 int n; 996 int pagesize = getpagesize(); 997 const char *header; 998 int hlen; 999 long blocksize; 1000 1001 n = kvm_getswapinfo( 1002 kd, 1003 kswap, 1004 sizeof(kswap)/sizeof(kswap[0]), 1005 ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX 1006 ); 1007 1008 #define CONVERT(v) ((int)((quad_t)(v) * pagesize / blocksize)) 1009 1010 header = getbsize(&hlen, &blocksize); 1011 if (totalflag == 0) { 1012 (void)printf("%-15s %*s %8s %8s %8s %s\n", 1013 "Device", hlen, header, 1014 "Used", "Avail", "Capacity", "Type"); 1015 1016 for (i = 0; i < n; ++i) { 1017 (void)printf( 1018 "%-15s %*d ", 1019 kswap[i].ksw_devname, 1020 hlen, 1021 CONVERT(kswap[i].ksw_total) 1022 ); 1023 (void)printf( 1024 "%8d %8d %5.0f%% %s\n", 1025 CONVERT(kswap[i].ksw_used), 1026 CONVERT(kswap[i].ksw_total - kswap[i].ksw_used), 1027 (double)kswap[i].ksw_used * 100.0 / 1028 (double)kswap[i].ksw_total, 1029 (kswap[i].ksw_flags & SW_SEQUENTIAL) ? 1030 "Sequential" : "Interleaved" 1031 ); 1032 } 1033 } 1034 1035 if (totalflag) { 1036 blocksize = 1024 * 1024; 1037 1038 (void)printf( 1039 "%dM/%dM swap space\n", 1040 CONVERT(kswap[n].ksw_used), 1041 CONVERT(kswap[n].ksw_total) 1042 ); 1043 } else if (n > 1) { 1044 (void)printf( 1045 "%-15s %*d %8d %8d %5.0f%%\n", 1046 "Total", 1047 hlen, 1048 CONVERT(kswap[n].ksw_total), 1049 CONVERT(kswap[n].ksw_used), 1050 CONVERT(kswap[n].ksw_total - kswap[n].ksw_used), 1051 (double)kswap[n].ksw_used * 100.0 / 1052 (double)kswap[n].ksw_total 1053 ); 1054 } 1055 } 1056