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 (void)setgid(getgid()); 295 if ((ret = kvm_nlist(kd, nl)) != 0) { 296 if (ret == -1) 297 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 298 for (i = NLMANDATORYBEG, quit = 0; i <= NLMANDATORYEND; i++) 299 if (!nl[i].n_value) { 300 quit = 1; 301 warnx("undefined symbol: %s", nl[i].n_name); 302 } 303 if (quit) 304 exit(1); 305 } 306 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag)) 307 usage(); 308 if (fileflag || totalflag) 309 filemode(); 310 if (vnodeflag) 311 vnodemode(); 312 if (ttyflag) 313 ttymode(); 314 if (swapflag || totalflag) 315 swapmode(); 316 exit (0); 317 } 318 319 static void 320 usage() 321 { 322 fprintf(stderr, "usage: %s\n", usagestr); 323 exit (1); 324 } 325 326 struct e_vnode { 327 struct vnode *avnode; 328 struct vnode vnode; 329 }; 330 331 void 332 vnodemode() 333 { 334 struct e_vnode *e_vnodebase, *endvnode, *evp; 335 struct vnode *vp; 336 struct mount *maddr, *mp; 337 int numvnodes; 338 339 e_vnodebase = loadvnodes(&numvnodes); 340 if (totalflag) { 341 (void)printf("%7d vnodes\n", numvnodes); 342 return; 343 } 344 endvnode = e_vnodebase + numvnodes; 345 (void)printf("%d active vnodes\n", numvnodes); 346 347 348 #define ST mp->mnt_stat 349 maddr = NULL; 350 for (evp = e_vnodebase; evp < endvnode; evp++) { 351 vp = &evp->vnode; 352 if (vp->v_mount != maddr) { 353 /* 354 * New filesystem 355 */ 356 if ((mp = getmnt(vp->v_mount)) == NULL) 357 continue; 358 maddr = vp->v_mount; 359 mount_print(mp); 360 vnode_header(); 361 if (!strcmp(ST.f_fstypename, "ufs")) 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 ufs_print(vp); 372 else if (!strcmp(ST.f_fstypename, "nfs")) 373 nfs_print(vp); 374 else if (!strcmp(ST.f_fstypename, "union")) 375 union_print(vp); 376 (void)printf("\n"); 377 } 378 free(e_vnodebase); 379 } 380 381 void 382 vnode_header() 383 { 384 (void)printf("ADDR TYP VFLAG USE HOLD"); 385 } 386 387 void 388 vnode_print(avnode, vp) 389 struct vnode *avnode; 390 struct vnode *vp; 391 { 392 char *type, flags[16]; 393 char *fp = flags; 394 int flag; 395 396 /* 397 * set type 398 */ 399 switch (vp->v_type) { 400 case VNON: 401 type = "non"; break; 402 case VREG: 403 type = "reg"; break; 404 case VDIR: 405 type = "dir"; break; 406 case VBLK: 407 type = "blk"; break; 408 case VCHR: 409 type = "chr"; break; 410 case VLNK: 411 type = "lnk"; break; 412 case VSOCK: 413 type = "soc"; break; 414 case VFIFO: 415 type = "fif"; break; 416 case VBAD: 417 type = "bad"; break; 418 default: 419 type = "unk"; break; 420 } 421 /* 422 * gather flags 423 */ 424 flag = vp->v_flag; 425 if (flag & VROOT) 426 *fp++ = 'R'; 427 if (flag & VTEXT) 428 *fp++ = 'T'; 429 if (flag & VSYSTEM) 430 *fp++ = 'S'; 431 if (flag & VISTTY) 432 *fp++ = 't'; 433 if (flag & VXLOCK) 434 *fp++ = 'L'; 435 if (flag & VXWANT) 436 *fp++ = 'W'; 437 if (flag & VBWAIT) 438 *fp++ = 'B'; 439 if (flag & VOBJBUF) 440 *fp++ = 'V'; 441 if (flag & VCOPYONWRITE) 442 *fp++ = 'C'; 443 if (flag & VAGE) 444 *fp++ = 'a'; 445 if (flag & VOLOCK) 446 *fp++ = 'l'; 447 if (flag & VOWANT) 448 *fp++ = 'w'; 449 if (flag & VDOOMED) 450 *fp++ = 'D'; 451 if (flag & VFREE) 452 *fp++ = 'F'; 453 if (flag & VONWORKLST) 454 *fp++ = 'O'; 455 if (flag & VMOUNT) 456 *fp++ = 'M'; 457 458 if (flag == 0) 459 *fp++ = '-'; 460 *fp = '\0'; 461 (void)printf("%8lx %s %5s %4d %4d", 462 (u_long)(void *)avnode, type, flags, vp->v_usecount, vp->v_holdcnt); 463 } 464 465 void 466 ufs_header() 467 { 468 (void)printf(" FILEID IFLAG RDEV|SZ"); 469 } 470 471 int 472 ufs_print(vp) 473 struct vnode *vp; 474 { 475 int flag; 476 struct inode inode, *ip = &inode; 477 char flagbuf[16], *flags = flagbuf; 478 char *name; 479 mode_t type; 480 481 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 482 flag = ip->i_flag; 483 if (flag & IN_ACCESS) 484 *flags++ = 'A'; 485 if (flag & IN_CHANGE) 486 *flags++ = 'C'; 487 if (flag & IN_UPDATE) 488 *flags++ = 'U'; 489 if (flag & IN_MODIFIED) 490 *flags++ = 'M'; 491 if (flag & IN_RENAME) 492 *flags++ = 'R'; 493 if (flag & IN_HASHED) 494 *flags++ = 'H'; 495 if (flag & IN_LAZYMOD) 496 *flags++ = 'L'; 497 if (flag == 0) 498 *flags++ = '-'; 499 *flags = '\0'; 500 501 (void)printf(" %6d %5s", ip->i_number, flagbuf); 502 type = ip->i_mode & S_IFMT; 503 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) 504 if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL)) 505 (void)printf(" %2d,%-2d", 506 major(ip->i_rdev), minor(ip->i_rdev)); 507 else 508 (void)printf(" %7s", name); 509 else 510 (void)printf(" %7qd", ip->i_size); 511 return (0); 512 } 513 514 void 515 nfs_header() 516 { 517 (void)printf(" FILEID NFLAG RDEV|SZ"); 518 } 519 520 int 521 nfs_print(vp) 522 struct vnode *vp; 523 { 524 struct nfsnode nfsnode, *np = &nfsnode; 525 char flagbuf[16], *flags = flagbuf; 526 int flag; 527 char *name; 528 mode_t type; 529 530 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode"); 531 flag = np->n_flag; 532 if (flag & NFLUSHWANT) 533 *flags++ = 'W'; 534 if (flag & NFLUSHINPROG) 535 *flags++ = 'P'; 536 if (flag & NMODIFIED) 537 *flags++ = 'M'; 538 if (flag & NWRITEERR) 539 *flags++ = 'E'; 540 if (flag & NQNFSNONCACHE) 541 *flags++ = 'X'; 542 if (flag & NQNFSWRITE) 543 *flags++ = 'O'; 544 if (flag & NQNFSEVICTED) 545 *flags++ = 'G'; 546 if (flag & NACC) 547 *flags++ = 'A'; 548 if (flag & NUPD) 549 *flags++ = 'U'; 550 if (flag & NCHG) 551 *flags++ = 'C'; 552 if (flag & NLOCKED) 553 *flags++ = 'L'; 554 if (flag & NWANTED) 555 *flags++ = 'w'; 556 if (flag == 0) 557 *flags++ = '-'; 558 *flags = '\0'; 559 560 #define VT np->n_vattr 561 (void)printf(" %6ld %5s", VT.va_fileid, flagbuf); 562 type = VT.va_mode & S_IFMT; 563 if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode)) 564 if (usenumflag || ((name = devname(VT.va_rdev, type)) == NULL)) 565 (void)printf(" %2d,%-2d", 566 major(VT.va_rdev), minor(VT.va_rdev)); 567 else 568 (void)printf(" %7s", name); 569 else 570 (void)printf(" %7qd", np->n_size); 571 return (0); 572 } 573 574 void 575 union_header() 576 { 577 (void)printf(" UPPER LOWER"); 578 } 579 580 int 581 union_print(vp) 582 struct vnode *vp; 583 { 584 struct union_node unode, *up = &unode; 585 586 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode"); 587 588 (void)printf(" %8lx %8lx", (u_long)(void *)up->un_uppervp, 589 (u_long)(void *)up->un_lowervp); 590 return (0); 591 } 592 593 /* 594 * Given a pointer to a mount structure in kernel space, 595 * read it in and return a usable pointer to it. 596 */ 597 struct mount * 598 getmnt(maddr) 599 struct mount *maddr; 600 { 601 static struct mtab { 602 struct mtab *next; 603 struct mount *maddr; 604 struct mount mount; 605 } *mhead = NULL; 606 struct mtab *mt; 607 608 for (mt = mhead; mt != NULL; mt = mt->next) 609 if (maddr == mt->maddr) 610 return (&mt->mount); 611 if ((mt = malloc(sizeof(struct mtab))) == NULL) 612 errx(1, "malloc"); 613 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table"); 614 mt->maddr = maddr; 615 mt->next = mhead; 616 mhead = mt; 617 return (&mt->mount); 618 } 619 620 void 621 mount_print(mp) 622 struct mount *mp; 623 { 624 int flags; 625 626 #define ST mp->mnt_stat 627 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename, 628 ST.f_mntfromname, ST.f_mntonname); 629 if ((flags = mp->mnt_flag)) { 630 int i; 631 const char *sep = " ("; 632 633 for (i = 0; mnt_flags[i].m_flag; i++) { 634 if (flags & mnt_flags[i].m_flag) { 635 (void)printf("%s%s", sep, mnt_flags[i].m_name); 636 flags &= ~mnt_flags[i].m_flag; 637 sep = ","; 638 } 639 } 640 if (flags) 641 (void)printf("%sunknown_flags:%x", sep, flags); 642 (void)printf(")"); 643 } 644 (void)printf("\n"); 645 #undef ST 646 } 647 648 struct e_vnode * 649 loadvnodes(avnodes) 650 int *avnodes; 651 { 652 int mib[2]; 653 size_t copysize; 654 struct e_vnode *vnodebase; 655 656 if (memf != NULL) { 657 /* 658 * do it by hand 659 */ 660 return (kinfo_vnodes(avnodes)); 661 } 662 mib[0] = CTL_KERN; 663 mib[1] = KERN_VNODE; 664 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1) 665 err(1, "sysctl: KERN_VNODE"); 666 if ((vnodebase = malloc(copysize)) == NULL) 667 errx(1, "malloc"); 668 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1) 669 err(1, "sysctl: KERN_VNODE"); 670 if (copysize % sizeof(struct e_vnode)) 671 errx(1, "vnode size mismatch"); 672 *avnodes = copysize / sizeof(struct e_vnode); 673 674 return (vnodebase); 675 } 676 677 /* 678 * simulate what a running kernel does in in kinfo_vnode 679 */ 680 struct e_vnode * 681 kinfo_vnodes(avnodes) 682 int *avnodes; 683 { 684 struct mntlist mountlist; 685 struct mount *mp, mount, *mp_next; 686 struct vnode *vp, vnode, *vp_next; 687 char *vbuf, *evbuf, *bp; 688 int num, numvnodes; 689 690 #define VPTRSZ sizeof(struct vnode *) 691 #define VNODESZ sizeof(struct vnode) 692 693 KGET(V_NUMV, numvnodes); 694 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL) 695 errx(1, "malloc"); 696 bp = vbuf; 697 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ); 698 KGET(V_MOUNTLIST, mountlist); 699 for (num = 0, mp = TAILQ_FIRST(&mountlist); ; mp = mp_next) { 700 KGET2(mp, &mount, sizeof(mount), "mount entry"); 701 mp_next = TAILQ_NEXT(&mount, mnt_list); 702 for (vp = LIST_FIRST(&mount.mnt_vnodelist); 703 vp != NULL; vp = vp_next) { 704 KGET2(vp, &vnode, sizeof(vnode), "vnode"); 705 vp_next = LIST_NEXT(&vnode, v_mntvnodes); 706 if ((bp + VPTRSZ + VNODESZ) > evbuf) 707 /* XXX - should realloc */ 708 errx(1, "no more room for vnodes"); 709 memmove(bp, &vp, VPTRSZ); 710 bp += VPTRSZ; 711 memmove(bp, &vnode, VNODESZ); 712 bp += VNODESZ; 713 num++; 714 } 715 if (mp == TAILQ_LAST(&mountlist, mntlist)) 716 break; 717 } 718 *avnodes = num; 719 return ((struct e_vnode *)vbuf); 720 } 721 722 const char hdr[] = 723 " LINE RAW CAN OUT IHIWT ILOWT OHWT LWT COL STATE SESS PGID DISC\n"; 724 int ttyspace = 128; 725 726 void 727 ttymode() 728 { 729 struct tty *tty; 730 struct tty ttyb[1000]; 731 int error; 732 size_t len, i; 733 734 (void)printf("%s", hdr); 735 len = sizeof(ttyb); 736 error = sysctlbyname("kern.ttys", &ttyb, &len, 0, 0); 737 if (!error) { 738 len /= sizeof(ttyb[0]); 739 for (i = 0; i < len; i++) { 740 ttyprt(&ttyb[i], 0); 741 } 742 } 743 if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL) 744 errx(1, "malloc"); 745 if (nl[SCONS].n_type != 0) { 746 (void)printf("1 console\n"); 747 KGET(SCONS, *tty); 748 ttyprt(&tty[0], 0); 749 } 750 #ifdef __FreeBSD__ 751 if (nl[NSCCONS].n_type != 0) 752 ttytype(tty, "vty", SCCONS, NSCCONS, 0); 753 if (nl[NSIO].n_type != 0) 754 ttytype(tty, "sio", SIO, NSIO, 0); 755 if (nl[NRC].n_type != 0) 756 ttytype(tty, "rc", RC, NRC, 0); 757 if (nl[NCY].n_type != 0) 758 ttytype(tty, "cy", CY, NCY, 0); 759 if (nl[NSI].n_type != 0) 760 ttytype(tty, "si", SI, NSI, 1); 761 #endif 762 if (nl[SNPTY].n_type != 0) 763 ttytype(tty, "pty", SPTY, SNPTY, 0); 764 } 765 766 void 767 ttytype(tty, name, type, number, indir) 768 struct tty *tty; 769 char *name; 770 int type, number, indir; 771 { 772 struct tty *tp; 773 int ntty; 774 struct tty **ttyaddr; 775 776 if (tty == NULL) 777 return; 778 KGET(number, ntty); 779 (void)printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines"); 780 if (ntty > ttyspace) { 781 ttyspace = ntty; 782 if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == 0) 783 errx(1, "realloc"); 784 } 785 if (indir) { 786 KGET(type, ttyaddr); 787 KGET2(ttyaddr, tty, ntty * sizeof(struct tty), "tty structs"); 788 } else { 789 KGET1(type, tty, ntty * sizeof(struct tty), "tty structs"); 790 } 791 (void)printf("%s", hdr); 792 for (tp = tty; tp < &tty[ntty]; tp++) 793 ttyprt(tp, tp - tty); 794 } 795 796 struct { 797 int flag; 798 char val; 799 } ttystates[] = { 800 #ifdef TS_WOPEN 801 { TS_WOPEN, 'W'}, 802 #endif 803 { TS_ISOPEN, 'O'}, 804 { TS_CARR_ON, 'C'}, 805 #ifdef TS_CONNECTED 806 { TS_CONNECTED, 'c'}, 807 #endif 808 { TS_TIMEOUT, 'T'}, 809 { TS_FLUSH, 'F'}, 810 { TS_BUSY, 'B'}, 811 #ifdef TS_ASLEEP 812 { TS_ASLEEP, 'A'}, 813 #endif 814 #ifdef TS_SO_OLOWAT 815 { TS_SO_OLOWAT, 'A'}, 816 #endif 817 #ifdef TS_SO_OCOMPLETE 818 { TS_SO_OCOMPLETE, 'a'}, 819 #endif 820 { TS_XCLUDE, 'X'}, 821 { TS_TTSTOP, 'S'}, 822 #ifdef TS_CAR_OFLOW 823 { TS_CAR_OFLOW, 'm'}, 824 #endif 825 #ifdef TS_CTS_OFLOW 826 { TS_CTS_OFLOW, 'o'}, 827 #endif 828 #ifdef TS_DSR_OFLOW 829 { TS_DSR_OFLOW, 'd'}, 830 #endif 831 { TS_TBLOCK, 'K'}, 832 { TS_ASYNC, 'Y'}, 833 { TS_BKSL, 'D'}, 834 { TS_ERASE, 'E'}, 835 { TS_LNCH, 'L'}, 836 { TS_TYPEN, 'P'}, 837 { TS_CNTTB, 'N'}, 838 #ifdef TS_CAN_BYPASS_L_RINT 839 { TS_CAN_BYPASS_L_RINT, 'l'}, 840 #endif 841 #ifdef TS_SNOOP 842 { TS_SNOOP, 's'}, 843 #endif 844 #ifdef TS_ZOMBIE 845 { TS_ZOMBIE, 'Z'}, 846 #endif 847 { 0, '\0'}, 848 }; 849 850 void 851 ttyprt(tp, line) 852 struct tty *tp; 853 int line; 854 { 855 int i, j; 856 pid_t pgid; 857 char *name, state[20]; 858 859 if (usenumflag || tp->t_dev == 0 || 860 (name = devname(tp->t_dev, S_IFCHR)) == NULL) 861 (void)printf(" %2d,%-2d", major(tp->t_dev), minor(tp->t_dev)); 862 else 863 (void)printf("%7s ", name); 864 (void)printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc); 865 (void)printf("%3d %5d %5d %4d %3d %7d ", tp->t_outq.c_cc, 866 tp->t_ihiwat, tp->t_ilowat, tp->t_ohiwat, tp->t_olowat, 867 tp->t_column); 868 for (i = j = 0; ttystates[i].flag; i++) 869 if (tp->t_state&ttystates[i].flag) 870 state[j++] = ttystates[i].val; 871 if (j == 0) 872 state[j++] = '-'; 873 state[j] = '\0'; 874 (void)printf("%-6s %8lx", state, (u_long)(void *)tp->t_session); 875 pgid = 0; 876 if (tp->t_pgrp != NULL) 877 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid"); 878 (void)printf("%6d ", pgid); 879 switch (tp->t_line) { 880 case TTYDISC: 881 (void)printf("term\n"); 882 break; 883 case NTTYDISC: 884 (void)printf("ntty\n"); 885 break; 886 case SLIPDISC: 887 (void)printf("slip\n"); 888 break; 889 case PPPDISC: 890 (void)printf("ppp\n"); 891 break; 892 default: 893 (void)printf("%d\n", tp->t_line); 894 break; 895 } 896 } 897 898 void 899 filemode() 900 { 901 struct file *fp; 902 struct file *addr; 903 char *buf, flagbuf[16], *fbp; 904 int len, maxfile, nfile; 905 static char *dtypes[] = { "???", "inode", "socket" }; 906 907 KGET(FNL_MAXFILE, maxfile); 908 if (totalflag) { 909 KGET(FNL_NFILE, nfile); 910 (void)printf("%3d/%3d files\n", nfile, maxfile); 911 return; 912 } 913 if (getfiles(&buf, &len) == -1) 914 return; 915 /* 916 * Getfiles returns in malloc'd memory a pointer to the first file 917 * structure, and then an array of file structs (whose addresses are 918 * derivable from the previous entry). 919 */ 920 addr = LIST_FIRST((struct filelist *)buf); 921 fp = (struct file *)(buf + sizeof(struct filelist)); 922 nfile = (len - sizeof(struct filelist)) / sizeof(struct file); 923 924 (void)printf("%d/%d open files\n", nfile, maxfile); 925 (void)printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n"); 926 for (; (char *)fp < buf + len; addr = LIST_NEXT(fp, f_list), fp++) { 927 if ((unsigned)fp->f_type > DTYPE_SOCKET) 928 continue; 929 (void)printf("%8lx ", (u_long)(void *)addr); 930 (void)printf("%-8.8s", dtypes[fp->f_type]); 931 fbp = flagbuf; 932 if (fp->f_flag & FREAD) 933 *fbp++ = 'R'; 934 if (fp->f_flag & FWRITE) 935 *fbp++ = 'W'; 936 if (fp->f_flag & FAPPEND) 937 *fbp++ = 'A'; 938 if (fp->f_flag & FASYNC) 939 *fbp++ = 'I'; 940 *fbp = '\0'; 941 (void)printf("%6s %3d", flagbuf, fp->f_count); 942 (void)printf(" %3d", fp->f_msgcount); 943 (void)printf(" %8lx", (u_long)(void *)fp->f_data); 944 if (fp->f_offset < 0) 945 (void)printf(" %qx\n", fp->f_offset); 946 else 947 (void)printf(" %qd\n", fp->f_offset); 948 } 949 free(buf); 950 } 951 952 int 953 getfiles(abuf, alen) 954 char **abuf; 955 int *alen; 956 { 957 size_t len; 958 int mib[2]; 959 char *buf; 960 961 /* 962 * XXX 963 * Add emulation of KINFO_FILE here. 964 */ 965 if (memf != NULL) 966 errx(1, "files on dead kernel, not implemented"); 967 968 mib[0] = CTL_KERN; 969 mib[1] = KERN_FILE; 970 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 971 warn("sysctl: KERN_FILE"); 972 return (-1); 973 } 974 if ((buf = malloc(len)) == NULL) 975 errx(1, "malloc"); 976 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 977 warn("sysctl: KERN_FILE"); 978 return (-1); 979 } 980 *abuf = buf; 981 *alen = len; 982 return (0); 983 } 984 985 /* 986 * swapmode is based on a program called swapinfo written 987 * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 988 */ 989 void 990 swapmode(void) 991 { 992 struct kvm_swap kswap[16]; 993 int i; 994 int n; 995 int pagesize = getpagesize(); 996 const char *header; 997 int hlen; 998 long blocksize; 999 1000 n = kvm_getswapinfo( 1001 kd, 1002 kswap, 1003 sizeof(kswap)/sizeof(kswap[0]), 1004 ((swapflag > 1) ? SWIF_DUMP_TREE : 0) | SWIF_DEV_PREFIX 1005 ); 1006 1007 #define CONVERT(v) ((int)((quad_t)(v) * pagesize / blocksize)) 1008 1009 header = getbsize(&hlen, &blocksize); 1010 if (totalflag == 0) { 1011 (void)printf("%-15s %*s %8s %8s %8s %s\n", 1012 "Device", hlen, header, 1013 "Used", "Avail", "Capacity", "Type"); 1014 1015 for (i = 0; i < n; ++i) { 1016 (void)printf( 1017 "%-15s %*d ", 1018 kswap[i].ksw_devname, 1019 hlen, 1020 CONVERT(kswap[i].ksw_total) 1021 ); 1022 (void)printf( 1023 "%8d %8d %5.0f%% %s\n", 1024 CONVERT(kswap[i].ksw_used), 1025 CONVERT(kswap[i].ksw_total - kswap[i].ksw_used), 1026 (double)kswap[i].ksw_used * 100.0 / 1027 (double)kswap[i].ksw_total, 1028 (kswap[i].ksw_flags & SW_SEQUENTIAL) ? 1029 "Sequential" : "Interleaved" 1030 ); 1031 } 1032 } 1033 1034 if (totalflag) { 1035 blocksize = 1024 * 1024; 1036 1037 (void)printf( 1038 "%dM/%dM swap space\n", 1039 CONVERT(kswap[n].ksw_used), 1040 CONVERT(kswap[n].ksw_total) 1041 ); 1042 } else if (n > 1) { 1043 (void)printf( 1044 "%-15s %*d %8d %8d %5.0f%%\n", 1045 "Total", 1046 hlen, 1047 CONVERT(kswap[n].ksw_total), 1048 CONVERT(kswap[n].ksw_used), 1049 CONVERT(kswap[n].ksw_total - kswap[n].ksw_used), 1050 (double)kswap[n].ksw_used * 100.0 / 1051 (double)kswap[n].ksw_total 1052 ); 1053 } 1054 } 1055