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