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