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