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