1 /* $NetBSD: pstat.c,v 1.74 2002/11/06 09:32:26 jdolecek Exp $ */ 2 3 /*- 4 * Copyright (c) 1980, 1991, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993, 1994\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)pstat.c 8.16 (Berkeley) 5/9/95"; 45 #else 46 __RCSID("$NetBSD: pstat.c,v 1.74 2002/11/06 09:32:26 jdolecek Exp $"); 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/time.h> 52 #include <sys/vnode.h> 53 #include <sys/ucred.h> 54 #define _KERNEL 55 #include <sys/file.h> 56 #include <ufs/ufs/inode.h> 57 #define NFS 58 #include <sys/mount.h> 59 #undef NFS 60 #include <sys/uio.h> 61 #include <sys/namei.h> 62 #include <miscfs/genfs/layer.h> 63 #include <miscfs/union/union.h> 64 #undef _KERNEL 65 #include <sys/stat.h> 66 #include <nfs/nfsproto.h> 67 #include <nfs/rpcv2.h> 68 #include <nfs/nfs.h> 69 #include <nfs/nfsnode.h> 70 #include <sys/ioctl.h> 71 #include <sys/tty.h> 72 #include <sys/conf.h> 73 #include <sys/device.h> 74 75 #include <sys/sysctl.h> 76 77 #include <err.h> 78 #include <kvm.h> 79 #include <limits.h> 80 #include <nlist.h> 81 #include <stdio.h> 82 #include <stdlib.h> 83 #include <string.h> 84 #include <unistd.h> 85 86 #include "swapctl.h" 87 88 struct nlist nl[] = { 89 #define V_MOUNTLIST 0 90 { "_mountlist" }, /* address of head of mount list. */ 91 #define V_NUMV 1 92 { "_numvnodes" }, 93 #define FNL_NFILE 2 94 { "_nfiles" }, 95 #define FNL_MAXFILE 3 96 { "_maxfiles" }, 97 #define TTY_NTTY 4 98 { "_tty_count" }, 99 #define TTY_TTYLIST 5 100 { "_ttylist" }, 101 #define NLMANDATORY TTY_TTYLIST /* names up to here are mandatory */ 102 { "" } 103 }; 104 105 int usenumflag; 106 int totalflag; 107 int kflag; 108 char *nlistf = NULL; 109 char *memf = NULL; 110 kvm_t *kd; 111 112 static const struct { 113 u_int m_flag; 114 const char *m_name; 115 } mnt_flags[] = { 116 { MNT_RDONLY, "rdonly" }, 117 { MNT_SYNCHRONOUS, "sync" }, 118 { MNT_NOEXEC, "noexec" }, 119 { MNT_NOSUID, "nosuid" }, 120 { MNT_NODEV, "nodev" }, 121 { MNT_UNION, "union" }, 122 { MNT_ASYNC, "async" }, 123 { MNT_NOCOREDUMP, "nocoredump" }, 124 { MNT_NOATIME, "noatime" }, 125 { MNT_SYMPERM, "symperm" }, 126 { MNT_NODEVMTIME, "nodevmtime" }, 127 { MNT_SOFTDEP, "softdep" }, 128 { MNT_EXRDONLY, "exrdonly" }, 129 { MNT_EXPORTED, "exported" }, 130 { MNT_DEFEXPORTED, "defexported" }, 131 { MNT_EXPORTANON, "exportanon" }, 132 { MNT_EXKERB, "exkerb" }, 133 { MNT_EXNORESPORT, "exnoresport" }, 134 { MNT_EXPUBLIC, "expublic" }, 135 { MNT_LOCAL, "local" }, 136 { MNT_QUOTA, "quota" }, 137 { MNT_ROOTFS, "rootfs" }, 138 { MNT_UPDATE, "update" }, 139 { MNT_DELEXPORT, "delexport" }, 140 { MNT_RELOAD, "reload" }, 141 { MNT_FORCE, "force" }, 142 { MNT_GONE, "gone" }, 143 { MNT_UNMOUNT, "unmount" }, 144 { MNT_WANTRDWR, "wantrdwr" }, 145 { 0 } 146 }; 147 148 struct flagbit_desc { 149 u_int fd_flags; 150 char fd_mark; 151 }; 152 153 #define SVAR(var) __STRING(var) /* to force expansion */ 154 #define KGET(idx, var) \ 155 KGET1(idx, &var, sizeof(var), SVAR(var)) 156 #define KGET1(idx, p, s, msg) \ 157 KGET2(nl[idx].n_value, p, s, msg) 158 #define KGET2(addr, p, s, msg) do { \ 159 if (kvm_read(kd, (u_long)(addr), p, s) != s) \ 160 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 161 } while (/* CONSTCOND */0) 162 #define KGETRET(addr, p, s, msg) do { \ 163 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \ 164 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 165 return (0); \ 166 } \ 167 } while (/* CONSTCOND */0) 168 169 #if 1 /* This is copied from vmstat/vmstat.c */ 170 /* 171 * Print single word. `ovflow' is number of characters didn't fit 172 * on the last word. `fmt' is a format string to print this word. 173 * It must contain asterisk for field width. `width' is a width 174 * occupied by this word. `fixed' is a number of constant chars in 175 * `fmt'. `val' is a value to be printed using format string `fmt'. 176 */ 177 #define PRWORD(ovflw, fmt, width, fixed, val) do { \ 178 (ovflw) += printf((fmt), \ 179 (width) - (fixed) - (ovflw) > 0 ? \ 180 (width) - (fixed) - (ovflw) : 0, \ 181 (val)) - (width); \ 182 if ((ovflw) < 0) \ 183 (ovflw) = 0; \ 184 } while (/* CONSTCOND */0) 185 #endif 186 187 void filemode __P((void)); 188 int getfiles __P((char **, int *)); 189 int getflags __P((const struct flagbit_desc *, char *, u_int)); 190 struct mount * 191 getmnt __P((struct mount *)); 192 char * kinfo_vnodes __P((int *)); 193 void layer_header __P((void)); 194 int layer_print __P((struct vnode *, int)); 195 char * loadvnodes __P((int *)); 196 int main __P((int, char **)); 197 void mount_print __P((struct mount *)); 198 void nfs_header __P((void)); 199 int nfs_print __P((struct vnode *, int)); 200 void ttymode __P((void)); 201 void ttyprt __P((struct tty *)); 202 void ufs_header __P((void)); 203 int ufs_print __P((struct vnode *, int)); 204 int ext2fs_print __P((struct vnode *, int)); 205 void union_header __P((void)); 206 int union_print __P((struct vnode *, int)); 207 void usage __P((void)); 208 void vnode_header __P((void)); 209 int vnode_print __P((struct vnode *, struct vnode *)); 210 void vnodemode __P((void)); 211 212 int 213 main(argc, argv) 214 int argc; 215 char *argv[]; 216 { 217 int ch, i, quit, ret; 218 int fileflag, swapflag, ttyflag, vnodeflag; 219 gid_t egid = getegid(); 220 char buf[_POSIX2_LINE_MAX]; 221 222 setegid(getgid()); 223 fileflag = swapflag = ttyflag = vnodeflag = 0; 224 while ((ch = getopt(argc, argv, "TM:N:fiknstv")) != -1) 225 switch (ch) { 226 case 'f': 227 fileflag = 1; 228 break; 229 case 'M': 230 memf = optarg; 231 break; 232 case 'N': 233 nlistf = optarg; 234 break; 235 case 'n': 236 usenumflag = 1; 237 break; 238 case 's': 239 swapflag = 1; 240 break; 241 case 'T': 242 totalflag = 1; 243 break; 244 case 't': 245 ttyflag = 1; 246 break; 247 case 'k': 248 kflag = 1; 249 break; 250 case 'v': 251 case 'i': /* Backward compatibility. */ 252 vnodeflag = 1; 253 break; 254 default: 255 usage(); 256 } 257 argc -= optind; 258 argv += optind; 259 260 /* 261 * Discard setgid privileges. If not the running kernel, we toss 262 * them away totally so that bad guys can't print interesting stuff 263 * from kernel memory, otherwise switch back to kmem for the 264 * duration of the kvm_openfiles() call. 265 */ 266 if (nlistf != NULL || memf != NULL) 267 (void)setgid(getgid()); 268 else 269 (void)setegid(egid); 270 271 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0) 272 errx(1, "kvm_openfiles: %s", buf); 273 274 /* get rid of it now anyway */ 275 if (nlistf == NULL && memf == NULL) 276 (void)setgid(getgid()); 277 if ((ret = kvm_nlist(kd, nl)) != 0) { 278 if (ret == -1) 279 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 280 for (i = quit = 0; i <= NLMANDATORY; i++) 281 if (!nl[i].n_value) { 282 quit = 1; 283 warnx("undefined symbol: %s", nl[i].n_name); 284 } 285 if (quit) 286 exit(1); 287 } 288 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag)) 289 usage(); 290 if (fileflag || totalflag) 291 filemode(); 292 if (vnodeflag || totalflag) 293 vnodemode(); 294 if (ttyflag) 295 ttymode(); 296 if (swapflag || totalflag) 297 list_swap(0, kflag, 0, totalflag, 1); 298 exit(0); 299 } 300 301 #define VPTRSZ sizeof(struct vnode *) 302 #define VNODESZ sizeof(struct vnode) 303 #define PTRSTRWIDTH ((int)sizeof(void *) * 2) /* Width of resulting string 304 when pointer is printed 305 in hexadecimal. */ 306 307 void 308 vnodemode() 309 { 310 char *e_vnodebase, *endvnode, *evp; 311 struct vnode *vp; 312 struct mount *maddr, *mp; 313 int numvnodes, ovflw; 314 int (*vnode_fsprint) 315 __P((struct vnode *, int)); /* per-fs data printer */ 316 317 mp = NULL; 318 e_vnodebase = loadvnodes(&numvnodes); 319 if (totalflag) { 320 (void)printf("%7d vnodes\n", numvnodes); 321 goto out; 322 } 323 endvnode = e_vnodebase + numvnodes * (VPTRSZ + VNODESZ); 324 (void)printf("%d active vnodes\n", numvnodes); 325 326 #define ST mp->mnt_stat 327 #define FSTYPE_IS(mp, name) \ 328 (strncmp((mp)->mnt_stat.f_fstypename, (name), MFSNAMELEN) == 0) 329 maddr = NULL; 330 vnode_fsprint = NULL; 331 for (evp = e_vnodebase; evp < endvnode; evp += VPTRSZ + VNODESZ) { 332 vp = (struct vnode *)(evp + VPTRSZ); 333 if (vp->v_mount != maddr) { 334 /* 335 * New filesystem 336 */ 337 if ((mp = getmnt(vp->v_mount)) == NULL) 338 continue; 339 maddr = vp->v_mount; 340 mount_print(mp); 341 vnode_header(); 342 if (FSTYPE_IS(mp, MOUNT_FFS) || 343 FSTYPE_IS(mp, MOUNT_MFS)) { 344 ufs_header(); 345 vnode_fsprint = ufs_print; 346 } else if (FSTYPE_IS(mp, MOUNT_NFS)) { 347 nfs_header(); 348 vnode_fsprint = nfs_print; 349 } else if (FSTYPE_IS(mp, MOUNT_EXT2FS)) { 350 ufs_header(); 351 vnode_fsprint = ext2fs_print; 352 } else if (FSTYPE_IS(mp, MOUNT_NULL) || 353 FSTYPE_IS(mp, MOUNT_OVERLAY) || 354 FSTYPE_IS(mp, MOUNT_UMAP)) { 355 layer_header(); 356 vnode_fsprint = layer_print; 357 } else if (FSTYPE_IS(mp, MOUNT_UNION)) { 358 union_header(); 359 vnode_fsprint = union_print; 360 } else 361 vnode_fsprint = NULL; 362 (void)printf("\n"); 363 } 364 ovflw = vnode_print(*(struct vnode **)evp, vp); 365 if (VTOI(vp) != NULL && vnode_fsprint != NULL) 366 (*vnode_fsprint)(vp, ovflw); 367 (void)printf("\n"); 368 } 369 370 out: 371 free(e_vnodebase); 372 } 373 374 int 375 getflags(fd, p, flags) 376 const struct flagbit_desc *fd; 377 char *p; 378 u_int flags; 379 { 380 char *q = p; 381 382 if (flags == 0) { 383 *p++ = '-'; 384 *p = '\0'; 385 return (0); 386 } 387 388 for (; fd->fd_flags != 0; fd++) 389 if ((flags & fd->fd_flags) != 0) 390 *p++ = fd->fd_mark; 391 *p = '\0'; 392 return (p - q); 393 } 394 395 const struct flagbit_desc vnode_flags[] = { 396 { VROOT, 'R' }, 397 { VTEXT, 'T' }, 398 { VSYSTEM, 'S' }, 399 { VISTTY, 'I' }, 400 { VEXECMAP, 'E' }, 401 { VXLOCK, 'L' }, 402 { VXWANT, 'W' }, 403 { VBWAIT, 'B' }, 404 { VALIASED, 'A' }, 405 { VDIROP, 'D' }, 406 { VLAYER, 'Y' }, 407 { VONWORKLST, 'O' }, 408 { 0, '\0' }, 409 }; 410 411 void 412 vnode_header() 413 { 414 415 (void)printf("%-*s TYP VFLAG USE HOLD TAG NPAGE", 416 PTRSTRWIDTH, "ADDR"); 417 } 418 419 int 420 vnode_print(avnode, vp) 421 struct vnode *avnode; 422 struct vnode *vp; 423 { 424 char *type, flags[sizeof(vnode_flags) / sizeof(vnode_flags[0])]; 425 int ovflw; 426 427 /* 428 * set type 429 */ 430 switch (vp->v_type) { 431 case VNON: 432 type = "non"; break; 433 case VREG: 434 type = "reg"; break; 435 case VDIR: 436 type = "dir"; break; 437 case VBLK: 438 type = "blk"; break; 439 case VCHR: 440 type = "chr"; break; 441 case VLNK: 442 type = "lnk"; break; 443 case VSOCK: 444 type = "soc"; break; 445 case VFIFO: 446 type = "fif"; break; 447 case VBAD: 448 type = "bad"; break; 449 default: 450 type = "unk"; break; 451 } 452 /* 453 * gather flags 454 */ 455 (void)getflags(vnode_flags, flags, vp->v_flag); 456 457 ovflw = 0; 458 PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)avnode); 459 PRWORD(ovflw, " %*s", 4, 1, type); 460 PRWORD(ovflw, " %*s", 6, 1, flags); 461 PRWORD(ovflw, " %*ld", 5, 1, (long)vp->v_usecount); 462 PRWORD(ovflw, " %*ld", 5, 1, (long)vp->v_holdcnt); 463 PRWORD(ovflw, " %*d", 4, 1, vp->v_tag); 464 PRWORD(ovflw, " %*d", 6, 1, vp->v_uobj.uo_npages); 465 return (ovflw); 466 } 467 468 const struct flagbit_desc ufs_flags[] = { 469 { IN_ACCESS, 'A' }, 470 { IN_CHANGE, 'C' }, 471 { IN_UPDATE, 'U' }, 472 { IN_MODIFIED, 'M' }, 473 { IN_ACCESSED, 'a' }, 474 { IN_RENAME, 'R' }, 475 { IN_SHLOCK, 'S' }, 476 { IN_EXLOCK, 'E' }, 477 { IN_CLEANING, 'c' }, 478 { IN_ADIROP, 'D' }, 479 { IN_SPACECOUNTED, 's' }, 480 { 0, '\0' }, 481 }; 482 483 void 484 ufs_header() 485 { 486 487 (void)printf(" FILEID IFLAG RDEV|SZ"); 488 } 489 490 int 491 ufs_print(vp, ovflw) 492 struct vnode *vp; 493 int ovflw; 494 { 495 struct inode inode, *ip = &inode; 496 char flags[sizeof(ufs_flags) / sizeof(ufs_flags[0])]; 497 char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */ 498 char *name; 499 mode_t type; 500 501 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 502 503 /* 504 * XXX need to to locking state. 505 */ 506 507 (void)getflags(ufs_flags, flags, ip->i_flag); 508 PRWORD(ovflw, " %*d", 7, 1, ip->i_number); 509 PRWORD(ovflw, " %*s", 6, 1, flags); 510 type = ip->i_ffs_mode & S_IFMT; 511 if (S_ISCHR(ip->i_ffs_mode) || S_ISBLK(ip->i_ffs_mode)) { 512 if (usenumflag || 513 (name = devname(ip->i_ffs_rdev, type)) == NULL) { 514 snprintf(dev, sizeof(dev), "%d,%d", 515 major(ip->i_ffs_rdev), minor(ip->i_ffs_rdev)); 516 name = dev; 517 } 518 PRWORD(ovflw, " %*s", 8, 1, name); 519 } else 520 PRWORD(ovflw, " %*lld", 8, 1, (long long)ip->i_ffs_size); 521 return (0); 522 } 523 524 int 525 ext2fs_print(vp, ovflw) 526 struct vnode *vp; 527 int ovflw; 528 { 529 struct inode inode, *ip = &inode; 530 char flags[sizeof(ufs_flags) / sizeof(ufs_flags[0])]; 531 char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */ 532 char *name; 533 mode_t type; 534 535 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 536 537 /* 538 * XXX need to to locking state. 539 */ 540 541 (void)getflags(ufs_flags, flags, ip->i_flag); 542 PRWORD(ovflw, " %*d", 7, 1, ip->i_number); 543 PRWORD(ovflw, " %*s", 6, 1, flags); 544 type = ip->i_e2fs_mode & S_IFMT; 545 if (S_ISCHR(ip->i_e2fs_mode) || S_ISBLK(ip->i_e2fs_mode)) { 546 if (usenumflag || 547 (name = devname(ip->i_e2fs_rdev, type)) == NULL) { 548 snprintf(dev, sizeof(dev), "%d,%d", 549 major(ip->i_e2fs_rdev), minor(ip->i_e2fs_rdev)); 550 name = dev; 551 } 552 PRWORD(ovflw, " %*s", 8, 1, name); 553 } else 554 PRWORD(ovflw, " %*u", 8, 1, (u_int)ip->i_e2fs_size); 555 return (0); 556 } 557 558 const struct flagbit_desc nfs_flags[] = { 559 { NFLUSHWANT, 'W' }, 560 { NFLUSHINPROG, 'P' }, 561 { NMODIFIED, 'M' }, 562 { NWRITEERR, 'E' }, 563 { NQNFSNONCACHE, 'X' }, 564 { NQNFSWRITE, 'O' }, 565 { NQNFSEVICTED, 'G' }, 566 { NACC, 'A' }, 567 { NUPD, 'U' }, 568 { NCHG, 'C' }, 569 { 0, '\0' }, 570 }; 571 572 void 573 nfs_header() 574 { 575 576 (void)printf(" FILEID NFLAG RDEV|SZ"); 577 } 578 579 int 580 nfs_print(vp, ovflw) 581 struct vnode *vp; 582 int ovflw; 583 { 584 struct nfsnode nfsnode, *np = &nfsnode; 585 char flags[sizeof(nfs_flags) / sizeof(nfs_flags[0])]; 586 char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */ 587 struct vattr va; 588 char *name; 589 mode_t type; 590 591 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode"); 592 (void)getflags(nfs_flags, flags, np->n_flag); 593 594 KGETRET(np->n_vattr, &va, sizeof(va), "vnode attr"); 595 PRWORD(ovflw, " %*ld", 7, 1, (long)va.va_fileid); 596 PRWORD(ovflw, " %*s", 6, 1, flags); 597 switch (va.va_type) { 598 case VCHR: 599 type = S_IFCHR; 600 goto device; 601 602 case VBLK: 603 type = S_IFBLK; 604 device: 605 if (usenumflag || (name = devname(va.va_rdev, type)) == NULL) { 606 (void)snprintf(dev, sizeof(dev), "%d,%d", 607 major(va.va_rdev), minor(va.va_rdev)); 608 name = dev; 609 } 610 PRWORD(ovflw, " %*s", 8, 1, name); 611 break; 612 default: 613 PRWORD(ovflw, " %*lld", 8, 1, (long long)np->n_size); 614 break; 615 } 616 return (0); 617 } 618 619 void 620 layer_header() 621 { 622 623 (void)printf(" %*s", PTRSTRWIDTH, "LOWER"); 624 } 625 626 int 627 layer_print(vp, ovflw) 628 struct vnode *vp; 629 int ovflw; 630 { 631 struct layer_node lnode, *lp = &lnode; 632 633 KGETRET(VTOLAYER(vp), &lnode, sizeof(lnode), "layer vnode"); 634 635 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)lp->layer_lowervp); 636 return (0); 637 } 638 639 void 640 union_header() 641 { 642 643 (void)printf(" %*s %*s", PTRSTRWIDTH, "UPPER", PTRSTRWIDTH, "LOWER"); 644 } 645 646 int 647 union_print(vp, ovflw) 648 struct vnode *vp; 649 int ovflw; 650 { 651 struct union_node unode, *up = &unode; 652 653 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode"); 654 655 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)up->un_uppervp); 656 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)up->un_lowervp); 657 return (0); 658 } 659 660 /* 661 * Given a pointer to a mount structure in kernel space, 662 * read it in and return a usable pointer to it. 663 */ 664 struct mount * 665 getmnt(maddr) 666 struct mount *maddr; 667 { 668 static struct mtab { 669 struct mtab *next; 670 struct mount *maddr; 671 struct mount mount; 672 } *mhead = NULL; 673 struct mtab *mt; 674 675 for (mt = mhead; mt != NULL; mt = mt->next) 676 if (maddr == mt->maddr) 677 return (&mt->mount); 678 if ((mt = malloc(sizeof(struct mtab))) == NULL) 679 err(1, "malloc"); 680 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table"); 681 mt->maddr = maddr; 682 mt->next = mhead; 683 mhead = mt; 684 return (&mt->mount); 685 } 686 687 void 688 mount_print(mp) 689 struct mount *mp; 690 { 691 int flags; 692 693 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename, 694 ST.f_mntfromname, ST.f_mntonname); 695 if ((flags = mp->mnt_flag) != 0) { 696 int i; 697 const char *sep = " ("; 698 699 for (i = 0; mnt_flags[i].m_flag; i++) { 700 if (flags & mnt_flags[i].m_flag) { 701 (void)printf("%s%s", sep, mnt_flags[i].m_name); 702 flags &= ~mnt_flags[i].m_flag; 703 sep = ","; 704 } 705 } 706 if (flags) 707 (void)printf("%sunknown_flags:%x", sep, flags); 708 (void)printf(")"); 709 } 710 (void)printf("\n"); 711 } 712 713 char * 714 loadvnodes(avnodes) 715 int *avnodes; 716 { 717 int mib[2]; 718 size_t copysize; 719 char *vnodebase; 720 721 if (memf != NULL) { 722 /* 723 * do it by hand 724 */ 725 return (kinfo_vnodes(avnodes)); 726 } 727 mib[0] = CTL_KERN; 728 mib[1] = KERN_VNODE; 729 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1) 730 err(1, "sysctl: KERN_VNODE"); 731 if ((vnodebase = malloc(copysize)) == NULL) 732 err(1, "malloc"); 733 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1) 734 err(1, "sysctl: KERN_VNODE"); 735 if (copysize % (VPTRSZ + VNODESZ)) 736 errx(1, "vnode size mismatch"); 737 *avnodes = copysize / (VPTRSZ + VNODESZ); 738 739 return (vnodebase); 740 } 741 742 /* 743 * simulate what a running kernel does in in kinfo_vnode 744 */ 745 char * 746 kinfo_vnodes(avnodes) 747 int *avnodes; 748 { 749 struct mntlist mountlist; 750 struct mount *mp, mount; 751 struct vnode *vp, vnode; 752 char *beg, *bp, *ep; 753 int numvnodes; 754 755 KGET(V_NUMV, numvnodes); 756 if ((bp = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL) 757 err(1, "malloc"); 758 beg = bp; 759 ep = bp + (numvnodes + 20) * (VPTRSZ + VNODESZ); 760 KGET(V_MOUNTLIST, mountlist); 761 for (mp = mountlist.cqh_first;; 762 mp = mount.mnt_list.cqe_next) { 763 KGET2(mp, &mount, sizeof(mount), "mount entry"); 764 for (vp = mount.mnt_vnodelist.lh_first; 765 vp != NULL; vp = vnode.v_mntvnodes.le_next) { 766 KGET2(vp, &vnode, sizeof(vnode), "vnode"); 767 if (bp + VPTRSZ + VNODESZ > ep) 768 /* XXX - should realloc */ 769 errx(1, "no more room for vnodes"); 770 memmove(bp, &vp, VPTRSZ); 771 bp += VPTRSZ; 772 memmove(bp, &vnode, VNODESZ); 773 bp += VNODESZ; 774 } 775 if (mp == mountlist.cqh_last) 776 break; 777 } 778 *avnodes = (bp - beg) / (VPTRSZ + VNODESZ); 779 return (beg); 780 } 781 782 void 783 ttymode() 784 { 785 int ntty; 786 struct ttylist_head tty_head; 787 struct tty *tp, tty; 788 789 KGET(TTY_NTTY, ntty); 790 (void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s"); 791 KGET(TTY_TTYLIST, tty_head); 792 (void)printf( 793 " LINE RAW CAN OUT HWT LWT COL STATE %-*s PGID DISC\n", 794 PTRSTRWIDTH, "SESS"); 795 for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) { 796 KGET2(tp, &tty, sizeof tty, "tty struct"); 797 ttyprt(&tty); 798 } 799 } 800 801 static const struct flagbit_desc ttystates[] = { 802 { TS_ISOPEN, 'O'}, 803 { TS_DIALOUT, '>'}, 804 { TS_CARR_ON, 'C'}, 805 { TS_TIMEOUT, 'T'}, 806 { TS_FLUSH, 'F'}, 807 { TS_BUSY, 'B'}, 808 { TS_ASLEEP, 'A'}, 809 { TS_XCLUDE, 'X'}, 810 { TS_TTSTOP, 'S'}, 811 { TS_TBLOCK, 'K'}, 812 { TS_ASYNC, 'Y'}, 813 { TS_BKSL, 'D'}, 814 { TS_ERASE, 'E'}, 815 { TS_LNCH, 'L'}, 816 { TS_TYPEN, 'P'}, 817 { TS_CNTTB, 'N'}, 818 { 0, '\0'}, 819 }; 820 821 void 822 ttyprt(tp) 823 struct tty *tp; 824 { 825 char state[sizeof(ttystates) / sizeof(ttystates[0]) + 1]; 826 char dev[2 + 3 + 1 + 5 + 1]; /* 12bit major + 20bit minor */ 827 struct linesw t_linesw; 828 char *name, buffer; 829 pid_t pgid; 830 int n, ovflw; 831 832 if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL) { 833 (void)snprintf(dev, sizeof(dev), "0x%3x:%x", 834 major(tp->t_dev), minor(tp->t_dev)); 835 name = dev; 836 } 837 ovflw = 0; 838 PRWORD(ovflw, "%-*s", 7, 0, name); 839 PRWORD(ovflw, " %*d", 3, 1, tp->t_rawq.c_cc); 840 PRWORD(ovflw, " %*d", 4, 1, tp->t_canq.c_cc); 841 PRWORD(ovflw, " %*d", 4, 1, tp->t_outq.c_cc); 842 PRWORD(ovflw, " %*d", 5, 1, tp->t_hiwat); 843 PRWORD(ovflw, " %*d", 4, 1, tp->t_lowat); 844 PRWORD(ovflw, " %*d", 8, 1, tp->t_column); 845 n = getflags(ttystates, state, tp->t_state); 846 if (tp->t_wopen) { 847 state[n++] = 'W'; 848 state[n] = '\0'; 849 } 850 PRWORD(ovflw, " %-*s", 7, 1, state); 851 PRWORD(ovflw, " %*lX", PTRSTRWIDTH + 1, 1, (u_long)tp->t_session); 852 pgid = 0; 853 if (tp->t_pgrp != NULL) 854 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid"); 855 PRWORD(ovflw, " %*d", 6, 1, pgid); 856 KGET2(tp->t_linesw, &t_linesw, sizeof(t_linesw), 857 "line discipline switch table"); 858 name = t_linesw.l_name; 859 (void)putchar(' '); 860 for (;;) { 861 KGET2(name, &buffer, sizeof(buffer), "line discipline name"); 862 if (buffer == '\0') 863 break; 864 (void)putchar(buffer); 865 name++; 866 } 867 (void)putchar('\n'); 868 } 869 870 static const struct flagbit_desc filemode_flags[] = { 871 { FREAD, 'R' }, 872 { FWRITE, 'W' }, 873 { FAPPEND, 'A' }, 874 #ifdef FSHLOCK /* currently gone */ 875 { FSHLOCK, 'S' }, 876 { FEXLOCK, 'X' }, 877 #endif 878 { FASYNC, 'I' }, 879 { 0, '\0' }, 880 }; 881 882 void 883 filemode() 884 { 885 struct file *fp; 886 struct file *addr; 887 char flags[sizeof(filemode_flags) / sizeof(filemode_flags[0])]; 888 char *buf; 889 int len, maxfile, nfile, ovflw; 890 static const char * const dtypes[] = 891 { "???", "inode", "socket", "pipe" }; 892 893 KGET(FNL_MAXFILE, maxfile); 894 if (totalflag) { 895 KGET(FNL_NFILE, nfile); 896 (void)printf("%3d/%3d files\n", nfile, maxfile); 897 return; 898 } 899 if (getfiles(&buf, &len) == -1) 900 return; 901 /* 902 * Getfiles returns in malloc'd memory a pointer to the first file 903 * structure, and then an array of file structs (whose addresses are 904 * derivable from the previous entry). 905 */ 906 addr = ((struct filelist *)buf)->lh_first; 907 fp = (struct file *)(buf + sizeof(struct filelist)); 908 nfile = (len - sizeof(struct filelist)) / sizeof(struct file); 909 910 (void)printf("%d/%d open files\n", nfile, maxfile); 911 (void)printf("%*s%s%*s TYPE FLG CNT MSG %*s%s%*s OFFSET\n", 912 (PTRSTRWIDTH - 4) / 2, "", " LOC", (PTRSTRWIDTH - 4) / 2, "", 913 (PTRSTRWIDTH - 4) / 2, "", "DATA", (PTRSTRWIDTH - 4) / 2, ""); 914 for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) { 915 if ((unsigned)fp->f_type > DTYPE_PIPE) 916 continue; 917 ovflw = 0; 918 (void)getflags(filemode_flags, flags, fp->f_flag); 919 PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)addr); 920 PRWORD(ovflw, " %-*s", 9, 1, dtypes[fp->f_type]); 921 PRWORD(ovflw, " %*s", 6, 1, flags); 922 PRWORD(ovflw, " %*d", 5, 1, fp->f_count); 923 PRWORD(ovflw, " %*d", 5, 1, fp->f_msgcount); 924 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 2, (long)fp->f_data); 925 if (fp->f_offset < 0) 926 PRWORD(ovflw, " %-*llx\n", PTRSTRWIDTH + 1, 2, 927 (long long)fp->f_offset); 928 else 929 PRWORD(ovflw, " %-*lld\n", PTRSTRWIDTH + 1, 2, 930 (long long)fp->f_offset); 931 } 932 free(buf); 933 } 934 935 int 936 getfiles(abuf, alen) 937 char **abuf; 938 int *alen; 939 { 940 size_t len; 941 int mib[2]; 942 char *buf; 943 944 /* 945 * XXX 946 * Add emulation of KINFO_FILE here. 947 */ 948 if (memf != NULL) 949 errx(1, "files on dead kernel, not implemented"); 950 951 mib[0] = CTL_KERN; 952 mib[1] = KERN_FILE; 953 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 954 warn("sysctl: KERN_FILE"); 955 return (-1); 956 } 957 if ((buf = malloc(len)) == NULL) 958 err(1, "malloc"); 959 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 960 warn("sysctl: KERN_FILE"); 961 return (-1); 962 } 963 *abuf = buf; 964 *alen = len; 965 return (0); 966 } 967 968 void 969 usage() 970 { 971 972 (void)fprintf(stderr, 973 "usage: %s [-T|-f|-s|-t|-v] [-kn] [-M core] [-N system]\n", 974 getprogname()); 975 exit(1); 976 } 977