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 "$Id$"; 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 <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/rlist.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 struct nlist nl[] = { 86 #define VM_SWAPLIST 0 87 { "_swaplist" },/* list of free swap areas */ 88 #define VM_SWDEVT 1 89 { "_swdevt" }, /* list of swap devices and sizes */ 90 #define VM_NSWAP 2 91 { "_nswap" }, /* size of largest swap device */ 92 #define VM_NSWDEV 3 93 { "_nswdev" }, /* number of swap devices */ 94 #define VM_DMMAX 4 95 { "_dmmax" }, /* maximum size of a swap block */ 96 #define V_MOUNTLIST 5 97 { "_mountlist" }, /* address of head of mount list. */ 98 #define V_NUMV 6 99 { "_numvnodes" }, 100 #define FNL_NFILE 7 101 {"_nfiles"}, 102 #define FNL_MAXFILE 8 103 {"_maxfiles"}, 104 #define NLMANDATORY FNL_MAXFILE /* names up to here are mandatory */ 105 #define SCONS NLMANDATORY + 1 106 { "_cons" }, 107 #define SPTY NLMANDATORY + 2 108 { "_pt_tty" }, 109 #define SNPTY NLMANDATORY + 3 110 { "_npty" }, 111 112 #ifdef hp300 113 #define SDCA (SNPTY+1) 114 { "_dca_tty" }, 115 #define SNDCA (SNPTY+2) 116 { "_ndca" }, 117 #define SDCM (SNPTY+3) 118 { "_dcm_tty" }, 119 #define SNDCM (SNPTY+4) 120 { "_ndcm" }, 121 #define SDCL (SNPTY+5) 122 { "_dcl_tty" }, 123 #define SNDCL (SNPTY+6) 124 { "_ndcl" }, 125 #define SITE (SNPTY+7) 126 { "_ite_tty" }, 127 #define SNITE (SNPTY+8) 128 { "_nite" }, 129 #endif 130 131 #ifdef mips 132 #define SDC (SNPTY+1) 133 { "_dc_tty" }, 134 #define SNDC (SNPTY+2) 135 { "_dc_cnt" }, 136 #endif 137 138 #ifdef __FreeBSD__ 139 #define SCCONS (SNPTY+1) 140 { "_sccons" }, 141 #define NSCCONS (SNPTY+2) 142 { "_nsccons" }, 143 #define SIO (SNPTY+3) 144 { "_sio_tty" }, 145 #define NSIO (SNPTY+4) 146 { "_nsio_tty" }, 147 #define RC (SNPTY+5) 148 { "_rc_tty" }, 149 #define NRC (SNPTY+6) 150 { "_nrc_tty" }, 151 #define CY (SNPTY+7) 152 { "_cy_tty" }, 153 #define NCY (SNPTY+8) 154 { "_ncy_tty" }, 155 #define SI (SNPTY+9) 156 { "_si_tty" }, 157 #define NSI (SNPTY+10) 158 { "_si_Nports" }, 159 #endif 160 { "" } 161 }; 162 163 int usenumflag; 164 int totalflag; 165 char *nlistf = NULL; 166 char *memf = NULL; 167 kvm_t *kd; 168 169 char *usagestr; 170 171 struct { 172 int m_flag; 173 const char *m_name; 174 } mnt_flags[] = { 175 { MNT_RDONLY, "rdonly" }, 176 { MNT_SYNCHRONOUS, "sync" }, 177 { MNT_NOEXEC, "noexec" }, 178 { MNT_NOSUID, "nosuid" }, 179 { MNT_NODEV, "nodev" }, 180 { MNT_UNION, "union" }, 181 { MNT_ASYNC, "async" }, 182 { MNT_NOATIME, "noatime" }, 183 { MNT_EXRDONLY, "exrdonly" }, 184 { MNT_EXPORTED, "exported" }, 185 { MNT_DEFEXPORTED, "defexported" }, 186 { MNT_EXPORTANON, "exportanon" }, 187 { MNT_EXKERB, "exkerb" }, 188 { MNT_LOCAL, "local" }, 189 { MNT_QUOTA, "quota" }, 190 { MNT_ROOTFS, "rootfs" }, 191 { MNT_USER, "user" }, 192 { MNT_UPDATE, "update" }, 193 { MNT_DELEXPORT }, 194 { MNT_UPDATE, "update" }, 195 { MNT_DELEXPORT, "delexport" }, 196 { MNT_RELOAD, "reload" }, 197 { MNT_FORCE, "force" }, 198 { MNT_UNMOUNT, "unmount" }, 199 { MNT_MWAIT, "mwait" }, 200 { MNT_WANTRDWR, "wantrdwr" }, 201 { 0 } 202 }; 203 204 205 #define SVAR(var) __STRING(var) /* to force expansion */ 206 #define KGET(idx, var) \ 207 KGET1(idx, &var, sizeof(var), SVAR(var)) 208 #define KGET1(idx, p, s, msg) \ 209 KGET2(nl[idx].n_value, p, s, msg) 210 #define KGET2(addr, p, s, msg) \ 211 if (kvm_read(kd, (u_long)(addr), p, s) != s) \ 212 warnx("cannot read %s: %s", msg, kvm_geterr(kd)) 213 #define KGETRET(addr, p, s, msg) \ 214 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \ 215 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 216 return (0); \ 217 } 218 219 void filemode __P((void)); 220 int getfiles __P((char **, int *)); 221 struct mount * 222 getmnt __P((struct mount *)); 223 struct e_vnode * 224 kinfo_vnodes __P((int *)); 225 struct e_vnode * 226 loadvnodes __P((int *)); 227 void mount_print __P((struct mount *)); 228 void nfs_header __P((void)); 229 int nfs_print __P((struct vnode *)); 230 void swapmode __P((void)); 231 void ttymode __P((void)); 232 void ttyprt __P((struct tty *, int)); 233 void ttytype __P((struct tty *, char *, int, int, int)); 234 void ufs_header __P((void)); 235 int ufs_print __P((struct vnode *)); 236 void union_header __P((void)); 237 int union_print __P((struct vnode *)); 238 static void usage __P((void)); 239 void vnode_header __P((void)); 240 void vnode_print __P((struct vnode *, struct vnode *)); 241 void vnodemode __P((void)); 242 243 int 244 main(argc, argv) 245 int argc; 246 char *argv[]; 247 { 248 int ch, i, quit, ret; 249 int fileflag, swapflag, ttyflag, vnodeflag; 250 char buf[_POSIX2_LINE_MAX],*opts; 251 252 fileflag = swapflag = ttyflag = vnodeflag = 0; 253 254 /* We will behave like good old swapinfo if thus invoked */ 255 opts = strrchr(argv[0],'/'); 256 if (opts) 257 opts++; 258 else 259 opts = argv[0]; 260 if (!strcmp(opts,"swapinfo")) { 261 swapflag = 1; 262 opts = "kM:N:"; 263 usagestr = "swapinfo [-k] [-M core] [-N system]"; 264 } else { 265 opts = "TM:N:fiknstv"; 266 usagestr = "pstat [-Tfknstv] [-M core] [-N system]"; 267 } 268 269 while ((ch = getopt(argc, argv, opts)) != -1) 270 switch (ch) { 271 case 'f': 272 fileflag = 1; 273 break; 274 case 'k': 275 putenv("BLOCKSIZE=1K"); 276 break; 277 case 'M': 278 memf = optarg; 279 break; 280 case 'N': 281 nlistf = optarg; 282 break; 283 case 'n': 284 usenumflag = 1; 285 break; 286 case 's': 287 swapflag = 1; 288 break; 289 case 'T': 290 totalflag = 1; 291 break; 292 case 't': 293 ttyflag = 1; 294 break; 295 case 'v': 296 case 'i': /* Backward compatibility. */ 297 vnodeflag = 1; 298 break; 299 default: 300 usage(); 301 } 302 argc -= optind; 303 argv += optind; 304 305 /* 306 * Discard setgid privileges if not the running kernel so that bad 307 * guys can't print interesting stuff from kernel memory. 308 */ 309 if (nlistf != NULL || memf != NULL) 310 (void)setgid(getgid()); 311 312 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0) 313 errx(1, "kvm_openfiles: %s", buf); 314 if ((ret = kvm_nlist(kd, nl)) != 0) { 315 if (ret == -1) 316 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 317 for (i = quit = 0; i <= NLMANDATORY; i++) 318 if (!nl[i].n_value) { 319 quit = 1; 320 warnx("undefined symbol: %s", nl[i].n_name); 321 } 322 if (quit) 323 exit(1); 324 } 325 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag)) 326 usage(); 327 if (fileflag || totalflag) 328 filemode(); 329 if (vnodeflag || totalflag) 330 vnodemode(); 331 if (ttyflag) 332 ttymode(); 333 if (swapflag || totalflag) 334 swapmode(); 335 exit (0); 336 } 337 338 static void 339 usage() 340 { 341 fprintf(stderr, "usage: %s\n", usagestr); 342 exit (1); 343 } 344 345 struct e_vnode { 346 struct vnode *avnode; 347 struct vnode vnode; 348 }; 349 350 void 351 vnodemode() 352 { 353 struct e_vnode *e_vnodebase, *endvnode, *evp; 354 struct vnode *vp; 355 struct mount *maddr, *mp; 356 int numvnodes; 357 358 e_vnodebase = loadvnodes(&numvnodes); 359 if (totalflag) { 360 (void)printf("%7d vnodes\n", numvnodes); 361 return; 362 } 363 endvnode = e_vnodebase + numvnodes; 364 (void)printf("%d active vnodes\n", numvnodes); 365 366 367 #define ST mp->mnt_stat 368 maddr = NULL; 369 for (evp = e_vnodebase; evp < endvnode; evp++) { 370 vp = &evp->vnode; 371 if (vp->v_mount != maddr) { 372 /* 373 * New filesystem 374 */ 375 if ((mp = getmnt(vp->v_mount)) == NULL) 376 continue; 377 maddr = vp->v_mount; 378 mount_print(mp); 379 vnode_header(); 380 if (!strcmp(ST.f_fstypename, "ufs") || 381 !strcmp(ST.f_fstypename, "mfs")) 382 ufs_header(); 383 else if (!strcmp(ST.f_fstypename, "nfs")) 384 nfs_header(); 385 else if (!strcmp(ST.f_fstypename, "union")) 386 union_header(); 387 (void)printf("\n"); 388 } 389 vnode_print(evp->avnode, vp); 390 if (!strcmp(ST.f_fstypename, "ufs") || 391 !strcmp(ST.f_fstypename, "mfs")) 392 ufs_print(vp); 393 else if (!strcmp(ST.f_fstypename, "nfs")) 394 nfs_print(vp); 395 else if (!strcmp(ST.f_fstypename, "union")) 396 union_print(vp); 397 (void)printf("\n"); 398 } 399 free(e_vnodebase); 400 } 401 402 void 403 vnode_header() 404 { 405 (void)printf("ADDR TYP VFLAG USE HOLD"); 406 } 407 408 void 409 vnode_print(avnode, vp) 410 struct vnode *avnode; 411 struct vnode *vp; 412 { 413 char *type, flags[16]; 414 char *fp = flags; 415 int flag; 416 417 /* 418 * set type 419 */ 420 switch (vp->v_type) { 421 case VNON: 422 type = "non"; break; 423 case VREG: 424 type = "reg"; break; 425 case VDIR: 426 type = "dir"; break; 427 case VBLK: 428 type = "blk"; break; 429 case VCHR: 430 type = "chr"; break; 431 case VLNK: 432 type = "lnk"; break; 433 case VSOCK: 434 type = "soc"; break; 435 case VFIFO: 436 type = "fif"; break; 437 case VBAD: 438 type = "bad"; break; 439 default: 440 type = "unk"; break; 441 } 442 /* 443 * gather flags 444 */ 445 flag = vp->v_flag; 446 if (flag & VROOT) 447 *fp++ = 'R'; 448 if (flag & VTEXT) 449 *fp++ = 'T'; 450 if (flag & VSYSTEM) 451 *fp++ = 'S'; 452 if (flag & VISTTY) 453 *fp++ = 't'; 454 if (flag & VXLOCK) 455 *fp++ = 'L'; 456 if (flag & VXWANT) 457 *fp++ = 'W'; 458 if (flag & VBWAIT) 459 *fp++ = 'B'; 460 if (flag & VALIASED) 461 *fp++ = 'A'; 462 if (flag & VVMIO) 463 *fp++ = 'V'; 464 if (flag & VAGE) 465 *fp++ = 'a'; 466 if (flag & VOLOCK) 467 *fp++ = 'l'; 468 if (flag & VOWANT) 469 *fp++ = 'w'; 470 if (flag == 0) 471 *fp++ = '-'; 472 *fp = '\0'; 473 (void)printf("%8x %s %5s %4d %4d", 474 avnode, type, flags, vp->v_usecount, vp->v_holdcnt); 475 } 476 477 void 478 ufs_header() 479 { 480 (void)printf(" FILEID IFLAG RDEV|SZ"); 481 } 482 483 int 484 ufs_print(vp) 485 struct vnode *vp; 486 { 487 int flag; 488 struct inode inode, *ip = &inode; 489 char flagbuf[16], *flags = flagbuf; 490 char *name; 491 mode_t type; 492 493 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 494 flag = ip->i_flag; 495 if (flag & IN_RENAME) 496 *flags++ = 'R'; 497 if (flag & IN_UPDATE) 498 *flags++ = 'U'; 499 if (flag & IN_ACCESS) 500 *flags++ = 'A'; 501 if (flag & IN_CHANGE) 502 *flags++ = 'C'; 503 if (flag & IN_MODIFIED) 504 *flags++ = 'M'; 505 if (flag & IN_SHLOCK) 506 *flags++ = 'S'; 507 if (flag & IN_EXLOCK) 508 *flags++ = 'E'; 509 if (flag == 0) 510 *flags++ = '-'; 511 *flags = '\0'; 512 513 (void)printf(" %6d %5s", ip->i_number, flagbuf); 514 type = ip->i_mode & S_IFMT; 515 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) 516 if (usenumflag || ((name = devname(ip->i_rdev, type)) == NULL)) 517 (void)printf(" %2d,%-2d", 518 major(ip->i_rdev), minor(ip->i_rdev)); 519 else 520 (void)printf(" %7s", name); 521 else 522 (void)printf(" %7qd", ip->i_size); 523 return (0); 524 } 525 526 void 527 nfs_header() 528 { 529 (void)printf(" FILEID NFLAG RDEV|SZ"); 530 } 531 532 int 533 nfs_print(vp) 534 struct vnode *vp; 535 { 536 struct nfsnode nfsnode, *np = &nfsnode; 537 char flagbuf[16], *flags = flagbuf; 538 int flag; 539 char *name; 540 mode_t type; 541 542 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode"); 543 flag = np->n_flag; 544 if (flag & NFLUSHWANT) 545 *flags++ = 'W'; 546 if (flag & NFLUSHINPROG) 547 *flags++ = 'P'; 548 if (flag & NMODIFIED) 549 *flags++ = 'M'; 550 if (flag & NWRITEERR) 551 *flags++ = 'E'; 552 if (flag & NQNFSNONCACHE) 553 *flags++ = 'X'; 554 if (flag & NQNFSWRITE) 555 *flags++ = 'O'; 556 if (flag & NQNFSEVICTED) 557 *flags++ = 'G'; 558 if (flag == 0) 559 *flags++ = '-'; 560 *flags = '\0'; 561 562 #define VT np->n_vattr 563 (void)printf(" %6d %5s", VT.va_fileid, flagbuf); 564 type = VT.va_mode & S_IFMT; 565 if (S_ISCHR(VT.va_mode) || S_ISBLK(VT.va_mode)) 566 if (usenumflag || ((name = devname(VT.va_rdev, type)) == NULL)) 567 (void)printf(" %2d,%-2d", 568 major(VT.va_rdev), minor(VT.va_rdev)); 569 else 570 (void)printf(" %7s", name); 571 else 572 (void)printf(" %7qd", np->n_size); 573 return (0); 574 } 575 576 void 577 union_header() 578 { 579 (void)printf(" UPPER LOWER"); 580 } 581 582 int 583 union_print(vp) 584 struct vnode *vp; 585 { 586 struct union_node unode, *up = &unode; 587 588 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode"); 589 590 (void)printf(" %8x %8x", up->un_uppervp, up->un_lowervp); 591 return (0); 592 } 593 594 /* 595 * Given a pointer to a mount structure in kernel space, 596 * read it in and return a usable pointer to it. 597 */ 598 struct mount * 599 getmnt(maddr) 600 struct mount *maddr; 601 { 602 static struct mtab { 603 struct mtab *next; 604 struct mount *maddr; 605 struct mount mount; 606 } *mhead = NULL; 607 struct mtab *mt; 608 609 for (mt = mhead; mt != NULL; mt = mt->next) 610 if (maddr == mt->maddr) 611 return (&mt->mount); 612 if ((mt = malloc(sizeof(struct mtab))) == NULL) 613 errx(1, "malloc"); 614 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table"); 615 mt->maddr = maddr; 616 mt->next = mhead; 617 mhead = mt; 618 return (&mt->mount); 619 } 620 621 void 622 mount_print(mp) 623 struct mount *mp; 624 { 625 int flags; 626 const char *type; 627 628 #define ST mp->mnt_stat 629 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename, 630 ST.f_mntfromname, ST.f_mntonname); 631 if ((flags = mp->mnt_flag)) { 632 int i; 633 const char *sep = " ("; 634 635 for (i = 0; mnt_flags[i].m_flag; i++) { 636 if (flags & mnt_flags[i].m_flag) { 637 (void)printf("%s%s", sep, mnt_flags[i].m_name); 638 flags &= ~mnt_flags[i].m_flag; 639 sep = ","; 640 } 641 } 642 if (flags) 643 (void)printf("%sunknown_flags:%x", sep, flags); 644 (void)printf(")"); 645 } 646 (void)printf("\n"); 647 #undef ST 648 } 649 650 struct e_vnode * 651 loadvnodes(avnodes) 652 int *avnodes; 653 { 654 int mib[2]; 655 size_t copysize; 656 struct e_vnode *vnodebase; 657 658 if (memf != NULL) { 659 /* 660 * do it by hand 661 */ 662 return (kinfo_vnodes(avnodes)); 663 } 664 mib[0] = CTL_KERN; 665 mib[1] = KERN_VNODE; 666 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1) 667 err(1, "sysctl: KERN_VNODE"); 668 if ((vnodebase = malloc(copysize)) == NULL) 669 errx(1, "malloc"); 670 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1) 671 err(1, "sysctl: KERN_VNODE"); 672 if (copysize % sizeof(struct e_vnode)) 673 errx(1, "vnode size mismatch"); 674 *avnodes = copysize / sizeof(struct e_vnode); 675 676 return (vnodebase); 677 } 678 679 /* 680 * simulate what a running kernel does in in kinfo_vnode 681 */ 682 struct e_vnode * 683 kinfo_vnodes(avnodes) 684 int *avnodes; 685 { 686 struct mntlist mountlist; 687 struct mount *mp, mount, *mp_next; 688 struct vnode *vp, vnode, *vp_next; 689 char *vbuf, *evbuf, *bp; 690 int num, numvnodes; 691 692 #define VPTRSZ sizeof(struct vnode *) 693 #define VNODESZ sizeof(struct vnode) 694 695 KGET(V_NUMV, numvnodes); 696 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL) 697 errx(1, "malloc"); 698 bp = vbuf; 699 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ); 700 KGET(V_MOUNTLIST, mountlist); 701 for (num = 0, mp = mountlist.cqh_first; ; mp = mp_next) { 702 KGET2(mp, &mount, sizeof(mount), "mount entry"); 703 mp_next = mount.mnt_list.cqe_next; 704 for (vp = mount.mnt_vnodelist.lh_first; 705 vp != NULL; vp = vp_next) { 706 KGET2(vp, &vnode, sizeof(vnode), "vnode"); 707 vp_next = vnode.v_mntvnodes.le_next; 708 if ((bp + VPTRSZ + VNODESZ) > evbuf) 709 /* XXX - should realloc */ 710 errx(1, "no more room for vnodes"); 711 memmove(bp, &vp, VPTRSZ); 712 bp += VPTRSZ; 713 memmove(bp, &vnode, VNODESZ); 714 bp += VNODESZ; 715 num++; 716 } 717 if (mp == mountlist.cqh_last) 718 break; 719 } 720 *avnodes = num; 721 return ((struct e_vnode *)vbuf); 722 } 723 724 char hdr[]=" LINE RAW CAN OUT HWT LWT COL STATE SESS PGID DISC\n"; 725 int ttyspace = 128; 726 727 void 728 ttymode() 729 { 730 struct tty *tty; 731 732 if ((tty = malloc(ttyspace * sizeof(*tty))) == NULL) 733 errx(1, "malloc"); 734 #if !defined(hp300) && !defined(mips) 735 if (nl[SCONS].n_type != 0) { 736 (void)printf("1 console\n"); 737 KGET(SCONS, *tty); 738 (void)printf(hdr); 739 ttyprt(&tty[0], 0); 740 } 741 #endif 742 #ifdef vax 743 if (nl[SNQD].n_type != 0) 744 qdss(); 745 if (nl[SNDZ].n_type != 0) 746 ttytype(tty, "dz", SDZ, SNDZ, 0); 747 if (nl[SNDH].n_type != 0) 748 ttytype(tty, "dh", SDH, SNDH, 0); 749 if (nl[SNDMF].n_type != 0) 750 ttytype(tty, "dmf", SDMF, SNDMF, 0); 751 if (nl[SNDHU].n_type != 0) 752 ttytype(tty, "dhu", SDHU, SNDHU, 0); 753 if (nl[SNDMZ].n_type != 0) 754 ttytype(tty, "dmz", SDMZ, SNDMZ, 0); 755 #endif 756 #ifdef tahoe 757 if (nl[SNVX].n_type != 0) 758 ttytype(tty, "vx", SVX, SNVX, 0); 759 if (nl[SNMP].n_type != 0) 760 ttytype(tty, "mp", SMP, SNMP, 0); 761 #endif 762 #ifdef hp300 763 if (nl[SNITE].n_type != 0) 764 ttytype(tty, "ite", SITE, SNITE, 0); 765 if (nl[SNDCA].n_type != 0) 766 ttytype(tty, "dca", SDCA, SNDCA, 0); 767 if (nl[SNDCM].n_type != 0) 768 ttytype(tty, "dcm", SDCM, SNDCM, 0); 769 if (nl[SNDCL].n_type != 0) 770 ttytype(tty, "dcl", SDCL, SNDCL, 0); 771 #endif 772 #ifdef mips 773 if (nl[SNDC].n_type != 0) 774 ttytype(tty, "dc", SDC, SNDC, 0); 775 #endif 776 #ifdef __FreeBSD__ 777 if (nl[NSCCONS].n_type != 0) 778 ttytype(tty, "vty", SCCONS, NSCCONS, 0); 779 if (nl[NSIO].n_type != 0) 780 ttytype(tty, "sio", SIO, NSIO, 0); 781 if (nl[NRC].n_type != 0) 782 ttytype(tty, "rc", RC, NRC, 0); 783 if (nl[NCY].n_type != 0) 784 ttytype(tty, "cy", CY, NCY, 0); 785 if (nl[NSI].n_type != 0) 786 ttytype(tty, "si", SI, NSI, 1); 787 #endif 788 if (nl[SNPTY].n_type != 0) 789 ttytype(tty, "pty", SPTY, SNPTY, 0); 790 } 791 792 void 793 ttytype(tty, name, type, number, indir) 794 struct tty *tty; 795 char *name; 796 int type, number, indir; 797 { 798 struct tty *tp; 799 int ntty; 800 struct tty **ttyaddr; 801 802 if (tty == NULL) 803 return; 804 KGET(number, ntty); 805 (void)printf("%d %s %s\n", ntty, name, (ntty == 1) ? "line" : "lines"); 806 if (ntty > ttyspace) { 807 ttyspace = ntty; 808 if ((tty = realloc(tty, ttyspace * sizeof(*tty))) == 0) 809 errx(1, "realloc"); 810 } 811 if (indir) { 812 KGET(type, ttyaddr); 813 KGET2(ttyaddr, tty, ntty * sizeof(struct tty), "tty structs"); 814 } else { 815 KGET1(type, tty, ntty * sizeof(struct tty), "tty structs"); 816 } 817 (void)printf(hdr); 818 for (tp = tty; tp < &tty[ntty]; tp++) 819 ttyprt(tp, tp - tty); 820 } 821 822 struct { 823 int flag; 824 char val; 825 } ttystates[] = { 826 #ifdef TS_WOPEN 827 { TS_WOPEN, 'W'}, 828 #endif 829 { TS_ISOPEN, 'O'}, 830 { TS_CARR_ON, 'C'}, 831 #ifdef TS_CONNECTED 832 { TS_CONNECTED, 'c'}, 833 #endif 834 { TS_TIMEOUT, 'T'}, 835 { TS_FLUSH, 'F'}, 836 { TS_BUSY, 'B'}, 837 #ifdef TS_ASLEEP 838 { TS_ASLEEP, 'A'}, 839 #endif 840 #ifdef TS_SO_OLOWAT 841 { TS_SO_OLOWAT, 'A'}, 842 #endif 843 #ifdef TS_SO_OCOMPLETE 844 { TS_SO_OCOMPLETE, 'a'}, 845 #endif 846 { TS_XCLUDE, 'X'}, 847 { TS_TTSTOP, 'S'}, 848 #ifdef TS_CAR_OFLOW 849 { TS_CAR_OFLOW, 'm'}, 850 #endif 851 #ifdef TS_CTS_OFLOW 852 { TS_CTS_OFLOW, 'o'}, 853 #endif 854 #ifdef TS_DSR_OFLOW 855 { TS_DSR_OFLOW, 'd'}, 856 #endif 857 { TS_TBLOCK, 'K'}, 858 { TS_ASYNC, 'Y'}, 859 { TS_BKSL, 'D'}, 860 { TS_ERASE, 'E'}, 861 { TS_LNCH, 'L'}, 862 { TS_TYPEN, 'P'}, 863 { TS_CNTTB, 'N'}, 864 #ifdef TS_CAN_BYPASS_L_RINT 865 { TS_CAN_BYPASS_L_RINT, 'l'}, 866 #endif 867 #ifdef TS_SNOOP 868 { TS_SNOOP, 's'}, 869 #endif 870 #ifdef TS_ZOMBIE 871 { TS_ZOMBIE, 'Z'}, 872 #endif 873 { 0, '\0'}, 874 }; 875 876 void 877 ttyprt(tp, line) 878 struct tty *tp; 879 int line; 880 { 881 int i, j; 882 pid_t pgid; 883 char *name, state[20]; 884 885 if (usenumflag || tp->t_dev == 0 || 886 (name = devname(tp->t_dev, S_IFCHR)) == NULL) 887 (void)printf("%7d ", line); 888 else 889 (void)printf("%7s ", name); 890 (void)printf("%2d %3d ", tp->t_rawq.c_cc, tp->t_canq.c_cc); 891 (void)printf("%3d %4d %3d %7d ", tp->t_outq.c_cc, 892 tp->t_hiwat, tp->t_lowat, tp->t_column); 893 for (i = j = 0; ttystates[i].flag; i++) 894 if (tp->t_state&ttystates[i].flag) 895 state[j++] = ttystates[i].val; 896 if (j == 0) 897 state[j++] = '-'; 898 state[j] = '\0'; 899 (void)printf("%-6s %8x", state, (u_long)tp->t_session); 900 pgid = 0; 901 if (tp->t_pgrp != NULL) 902 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid"); 903 (void)printf("%6d ", pgid); 904 switch (tp->t_line) { 905 case TTYDISC: 906 (void)printf("term\n"); 907 break; 908 case NTTYDISC: 909 (void)printf("ntty\n"); 910 break; 911 case TABLDISC: 912 (void)printf("tab\n"); 913 break; 914 case SLIPDISC: 915 (void)printf("slip\n"); 916 break; 917 case PPPDISC: 918 (void)printf("ppp\n"); 919 break; 920 default: 921 (void)printf("%d\n", tp->t_line); 922 break; 923 } 924 } 925 926 void 927 filemode() 928 { 929 struct file *fp; 930 struct file *addr; 931 char *buf, flagbuf[16], *fbp; 932 int len, maxfile, nfile; 933 static char *dtypes[] = { "???", "inode", "socket" }; 934 935 KGET(FNL_MAXFILE, maxfile); 936 if (totalflag) { 937 KGET(FNL_NFILE, nfile); 938 (void)printf("%3d/%3d files\n", nfile, maxfile); 939 return; 940 } 941 if (getfiles(&buf, &len) == -1) 942 return; 943 /* 944 * Getfiles returns in malloc'd memory a pointer to the first file 945 * structure, and then an array of file structs (whose addresses are 946 * derivable from the previous entry). 947 */ 948 addr = ((struct filelist *)buf)->lh_first; 949 fp = (struct file *)(buf + sizeof(struct filelist)); 950 nfile = (len - sizeof(struct filelist)) / sizeof(struct file); 951 952 (void)printf("%d/%d open files\n", nfile, maxfile); 953 (void)printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n"); 954 for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) { 955 if ((unsigned)fp->f_type > DTYPE_SOCKET) 956 continue; 957 (void)printf("%x ", addr); 958 (void)printf("%-8.8s", dtypes[fp->f_type]); 959 fbp = flagbuf; 960 if (fp->f_flag & FREAD) 961 *fbp++ = 'R'; 962 if (fp->f_flag & FWRITE) 963 *fbp++ = 'W'; 964 if (fp->f_flag & FAPPEND) 965 *fbp++ = 'A'; 966 #ifdef FSHLOCK /* currently gone */ 967 if (fp->f_flag & FSHLOCK) 968 *fbp++ = 'S'; 969 if (fp->f_flag & FEXLOCK) 970 *fbp++ = 'X'; 971 #endif 972 if (fp->f_flag & FASYNC) 973 *fbp++ = 'I'; 974 *fbp = '\0'; 975 (void)printf("%6s %3d", flagbuf, fp->f_count); 976 (void)printf(" %3d", fp->f_msgcount); 977 (void)printf(" %8.1x", fp->f_data); 978 if (fp->f_offset < 0) 979 (void)printf(" %qx\n", fp->f_offset); 980 else 981 (void)printf(" %qd\n", fp->f_offset); 982 } 983 free(buf); 984 } 985 986 int 987 getfiles(abuf, alen) 988 char **abuf; 989 int *alen; 990 { 991 size_t len; 992 int mib[2]; 993 char *buf; 994 995 /* 996 * XXX 997 * Add emulation of KINFO_FILE here. 998 */ 999 if (memf != NULL) 1000 errx(1, "files on dead kernel, not implemented"); 1001 1002 mib[0] = CTL_KERN; 1003 mib[1] = KERN_FILE; 1004 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 1005 warn("sysctl: KERN_FILE"); 1006 return (-1); 1007 } 1008 if ((buf = malloc(len)) == NULL) 1009 errx(1, "malloc"); 1010 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 1011 warn("sysctl: KERN_FILE"); 1012 return (-1); 1013 } 1014 *abuf = buf; 1015 *alen = len; 1016 return (0); 1017 } 1018 1019 /* 1020 * swapmode is based on a program called swapinfo written 1021 * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 1022 */ 1023 void 1024 swapmode() 1025 { 1026 char *header, *p; 1027 int hlen, nswap, nswdev, dmmax; 1028 int i, div, avail, nfree, npfree, used; 1029 struct swdevt *sw; 1030 long blocksize, *perdev; 1031 struct rlist head; 1032 struct rlisthdr swaplist; 1033 struct rlist *swapptr; 1034 u_long ptr; 1035 1036 KGET(VM_NSWAP, nswap); 1037 KGET(VM_NSWDEV, nswdev); 1038 KGET(VM_DMMAX, dmmax); 1039 KGET1(VM_SWAPLIST, &swaplist, sizeof swaplist, "swaplist"); 1040 if ((sw = malloc(nswdev * sizeof(*sw))) == NULL || 1041 (perdev = malloc(nswdev * sizeof(*perdev))) == NULL) 1042 errx(1, "malloc"); 1043 KGET1(VM_SWDEVT, &ptr, sizeof ptr, "swdevt"); 1044 KGET2(ptr, sw, nswdev * sizeof(*sw), "*swdevt"); 1045 1046 /* Count up swap space. */ 1047 nfree = 0; 1048 memset(perdev, 0, nswdev * sizeof(*perdev)); 1049 swapptr = swaplist.rlh_list; 1050 while (swapptr) { 1051 int top, bottom, next_block; 1052 1053 KGET2(swapptr, &head, sizeof(struct rlist), "swapptr"); 1054 1055 top = head.rl_end; 1056 bottom = head.rl_start; 1057 1058 nfree += top - bottom + 1; 1059 1060 /* 1061 * Swap space is split up among the configured disks. 1062 * 1063 * For interleaved swap devices, the first dmmax blocks 1064 * of swap space some from the first disk, the next dmmax 1065 * blocks from the next, and so on up to nswap blocks. 1066 * 1067 * The list of free space joins adjacent free blocks, 1068 * ignoring device boundries. If we want to keep track 1069 * of this information per device, we'll just have to 1070 * extract it ourselves. 1071 */ 1072 while (top / dmmax != bottom / dmmax) { 1073 next_block = ((bottom + dmmax) / dmmax); 1074 perdev[(bottom / dmmax) % nswdev] += 1075 next_block * dmmax - bottom; 1076 bottom = next_block * dmmax; 1077 } 1078 perdev[(bottom / dmmax) % nswdev] += 1079 top - bottom + 1; 1080 1081 swapptr = head.rl_next; 1082 } 1083 1084 header = getbsize(&hlen, &blocksize); 1085 if (!totalflag) 1086 (void)printf("%-11s %*s %8s %8s %8s %s\n", 1087 "Device", hlen, header, 1088 "Used", "Avail", "Capacity", "Type"); 1089 div = blocksize / 512; 1090 avail = npfree = 0; 1091 for (i = 0; i < nswdev; i++) { 1092 int xsize, xfree; 1093 1094 /* 1095 * Don't report statistics for partitions which have not 1096 * yet been activated via swapon(8). 1097 */ 1098 if (!(sw[i].sw_flags & SW_FREED)) 1099 continue; 1100 1101 if (!totalflag) 1102 if (sw[i].sw_dev != NODEV) { 1103 p = devname(sw[i].sw_dev, S_IFBLK); 1104 (void)printf("/dev/%-6s %*d ", 1105 p == NULL ? "??" : p, 1106 hlen, sw[i].sw_nblks / div); 1107 } else 1108 (void)printf("[NFS swap] %*d ", 1109 hlen, sw[i].sw_nblks / div); 1110 1111 /* The first dmmax is never allocated to avoid trashing of 1112 * disklabels 1113 */ 1114 xsize = sw[i].sw_nblks - dmmax; 1115 xfree = perdev[i]; 1116 used = xsize - xfree; 1117 npfree++; 1118 avail += xsize; 1119 if (totalflag) 1120 continue; 1121 (void)printf("%8d %8d %5.0f%% %s\n", 1122 used / div, xfree / div, 1123 (double)used / (double)xsize * 100.0, 1124 (sw[i].sw_flags & SW_SEQUENTIAL) ? 1125 "Sequential" : "Interleaved"); 1126 } 1127 1128 /* 1129 * If only one partition has been set up via swapon(8), we don't 1130 * need to bother with totals. 1131 */ 1132 used = avail - nfree; 1133 free(sw); 1134 free(perdev); 1135 if (totalflag) { 1136 (void)printf("%dM/%dM swap space\n", used / 2048, avail / 2048); 1137 return; 1138 } 1139 if (npfree > 1) { 1140 (void)printf("%-11s %*d %8d %8d %5.0f%%\n", 1141 "Total", hlen, avail / div, used / div, nfree / div, 1142 (double)used / (double)avail * 100.0); 1143 } 1144 } 1145