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