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