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