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