1 /* $NetBSD: pstat.c,v 1.79 2003/02/02 02:25:50 christos 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.79 2003/02/02 02:25:50 christos 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 u_int m_visible; 115 const char *m_name; 116 } mnt_flags[] = { 117 __MNT_FLAGS 118 }; 119 120 struct flagbit_desc { 121 u_int fd_flags; 122 char fd_mark; 123 }; 124 125 #define SVAR(var) __STRING(var) /* to force expansion */ 126 #define KGET(idx, var) \ 127 KGET1(idx, &var, sizeof(var), SVAR(var)) 128 #define KGET1(idx, p, s, msg) \ 129 KGET2(nl[idx].n_value, p, s, msg) 130 #define KGET2(addr, p, s, msg) do { \ 131 if (kvm_read(kd, (u_long)(addr), p, s) != s) \ 132 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 133 } while (/* CONSTCOND */0) 134 #define KGETRET(addr, p, s, msg) do { \ 135 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \ 136 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 137 return (0); \ 138 } \ 139 } while (/* CONSTCOND */0) 140 141 #if 1 /* This is copied from vmstat/vmstat.c */ 142 /* 143 * Print single word. `ovflow' is number of characters didn't fit 144 * on the last word. `fmt' is a format string to print this word. 145 * It must contain asterisk for field width. `width' is a width 146 * occupied by this word. `fixed' is a number of constant chars in 147 * `fmt'. `val' is a value to be printed using format string `fmt'. 148 */ 149 #define PRWORD(ovflw, fmt, width, fixed, val) do { \ 150 (ovflw) += printf((fmt), \ 151 (width) - (fixed) - (ovflw) > 0 ? \ 152 (width) - (fixed) - (ovflw) : 0, \ 153 (val)) - (width); \ 154 if ((ovflw) < 0) \ 155 (ovflw) = 0; \ 156 } while (/* CONSTCOND */0) 157 #endif 158 159 void filemode __P((void)); 160 int getfiles __P((char **, int *)); 161 int getflags __P((const struct flagbit_desc *, char *, u_int)); 162 struct mount * 163 getmnt __P((struct mount *)); 164 char * kinfo_vnodes __P((int *)); 165 void layer_header __P((void)); 166 int layer_print __P((struct vnode *, int)); 167 char * loadvnodes __P((int *)); 168 int main __P((int, char **)); 169 void mount_print __P((struct mount *)); 170 void nfs_header __P((void)); 171 int nfs_print __P((struct vnode *, int)); 172 void ttymode __P((void)); 173 void ttyprt __P((struct tty *)); 174 void ufs_header __P((void)); 175 int ufs_print __P((struct vnode *, int)); 176 int ext2fs_print __P((struct vnode *, int)); 177 void union_header __P((void)); 178 int union_print __P((struct vnode *, int)); 179 void usage __P((void)); 180 void vnode_header __P((void)); 181 int vnode_print __P((struct vnode *, struct vnode *)); 182 void vnodemode __P((void)); 183 184 int 185 main(argc, argv) 186 int argc; 187 char *argv[]; 188 { 189 int ch, i, quit, ret; 190 int fileflag, swapflag, ttyflag, vnodeflag; 191 gid_t egid = getegid(); 192 char buf[_POSIX2_LINE_MAX]; 193 194 setegid(getgid()); 195 fileflag = swapflag = ttyflag = vnodeflag = 0; 196 while ((ch = getopt(argc, argv, "TM:N:fiknstv")) != -1) 197 switch (ch) { 198 case 'f': 199 fileflag = 1; 200 break; 201 case 'M': 202 memf = optarg; 203 break; 204 case 'N': 205 nlistf = optarg; 206 break; 207 case 'n': 208 usenumflag = 1; 209 break; 210 case 's': 211 swapflag = 1; 212 break; 213 case 'T': 214 totalflag = 1; 215 break; 216 case 't': 217 ttyflag = 1; 218 break; 219 case 'k': 220 kflag = 1; 221 break; 222 case 'v': 223 case 'i': /* Backward compatibility. */ 224 vnodeflag = 1; 225 break; 226 default: 227 usage(); 228 } 229 argc -= optind; 230 argv += optind; 231 232 /* 233 * Discard setgid privileges. If not the running kernel, we toss 234 * them away totally so that bad guys can't print interesting stuff 235 * from kernel memory, otherwise switch back to kmem for the 236 * duration of the kvm_openfiles() call. 237 */ 238 if (nlistf != NULL || memf != NULL) 239 (void)setgid(getgid()); 240 else 241 (void)setegid(egid); 242 243 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0) 244 errx(1, "kvm_openfiles: %s", buf); 245 246 /* get rid of it now anyway */ 247 if (nlistf == NULL && memf == NULL) 248 (void)setgid(getgid()); 249 if ((ret = kvm_nlist(kd, nl)) != 0) { 250 if (ret == -1) 251 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 252 for (i = quit = 0; i <= NLMANDATORY; i++) 253 if (!nl[i].n_value) { 254 quit = 1; 255 warnx("undefined symbol: %s", nl[i].n_name); 256 } 257 if (quit) 258 exit(1); 259 } 260 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag)) 261 usage(); 262 if (fileflag || totalflag) 263 filemode(); 264 if (vnodeflag || totalflag) 265 vnodemode(); 266 if (ttyflag) 267 ttymode(); 268 if (swapflag || totalflag) 269 list_swap(0, kflag, 0, totalflag, 1); 270 exit(0); 271 } 272 273 #define VPTRSZ sizeof(struct vnode *) 274 #define VNODESZ sizeof(struct vnode) 275 #define PTRSTRWIDTH ((int)sizeof(void *) * 2) /* Width of resulting string 276 when pointer is printed 277 in hexadecimal. */ 278 279 void 280 vnodemode() 281 { 282 char *e_vnodebase, *endvnode, *evp; 283 struct vnode *vp; 284 struct mount *maddr, *mp; 285 int numvnodes, ovflw; 286 int (*vnode_fsprint) 287 __P((struct vnode *, int)); /* per-fs data printer */ 288 289 mp = NULL; 290 e_vnodebase = loadvnodes(&numvnodes); 291 if (totalflag) { 292 (void)printf("%7d vnodes\n", numvnodes); 293 goto out; 294 } 295 endvnode = e_vnodebase + numvnodes * (VPTRSZ + VNODESZ); 296 (void)printf("%d active vnodes\n", numvnodes); 297 298 #define ST mp->mnt_stat 299 #define FSTYPE_IS(mp, name) \ 300 (strncmp((mp)->mnt_stat.f_fstypename, (name), MFSNAMELEN) == 0) 301 maddr = NULL; 302 vnode_fsprint = NULL; 303 for (evp = e_vnodebase; evp < endvnode; evp += VPTRSZ + VNODESZ) { 304 vp = (struct vnode *)(evp + VPTRSZ); 305 if (vp->v_mount != maddr) { 306 /* 307 * New filesystem 308 */ 309 if ((mp = getmnt(vp->v_mount)) == NULL) 310 continue; 311 maddr = vp->v_mount; 312 mount_print(mp); 313 vnode_header(); 314 if (FSTYPE_IS(mp, MOUNT_FFS) || 315 FSTYPE_IS(mp, MOUNT_MFS)) { 316 ufs_header(); 317 vnode_fsprint = ufs_print; 318 } else if (FSTYPE_IS(mp, MOUNT_NFS)) { 319 nfs_header(); 320 vnode_fsprint = nfs_print; 321 } else if (FSTYPE_IS(mp, MOUNT_EXT2FS)) { 322 ufs_header(); 323 vnode_fsprint = ext2fs_print; 324 } else if (FSTYPE_IS(mp, MOUNT_NULL) || 325 FSTYPE_IS(mp, MOUNT_OVERLAY) || 326 FSTYPE_IS(mp, MOUNT_UMAP)) { 327 layer_header(); 328 vnode_fsprint = layer_print; 329 } else if (FSTYPE_IS(mp, MOUNT_UNION)) { 330 union_header(); 331 vnode_fsprint = union_print; 332 } else 333 vnode_fsprint = NULL; 334 (void)printf("\n"); 335 } 336 ovflw = vnode_print(*(struct vnode **)evp, vp); 337 if (VTOI(vp) != NULL && vnode_fsprint != NULL) 338 (*vnode_fsprint)(vp, ovflw); 339 (void)printf("\n"); 340 } 341 342 out: 343 free(e_vnodebase); 344 } 345 346 int 347 getflags(fd, p, flags) 348 const struct flagbit_desc *fd; 349 char *p; 350 u_int flags; 351 { 352 char *q = p; 353 354 if (flags == 0) { 355 *p++ = '-'; 356 *p = '\0'; 357 return (0); 358 } 359 360 for (; fd->fd_flags != 0; fd++) 361 if ((flags & fd->fd_flags) != 0) 362 *p++ = fd->fd_mark; 363 *p = '\0'; 364 return (p - q); 365 } 366 367 const struct flagbit_desc vnode_flags[] = { 368 { VROOT, 'R' }, 369 { VTEXT, 'T' }, 370 { VSYSTEM, 'S' }, 371 { VISTTY, 'I' }, 372 { VEXECMAP, 'E' }, 373 { VXLOCK, 'L' }, 374 { VXWANT, 'W' }, 375 { VBWAIT, 'B' }, 376 { VALIASED, 'A' }, 377 { VDIROP, 'D' }, 378 { VLAYER, 'Y' }, 379 { VONWORKLST, 'O' }, 380 { 0, '\0' }, 381 }; 382 383 void 384 vnode_header() 385 { 386 387 (void)printf("%-*s TYP VFLAG USE HOLD TAG NPAGE", 388 PTRSTRWIDTH, "ADDR"); 389 } 390 391 int 392 vnode_print(avnode, vp) 393 struct vnode *avnode; 394 struct vnode *vp; 395 { 396 char *type, flags[sizeof(vnode_flags) / sizeof(vnode_flags[0])]; 397 int ovflw; 398 399 /* 400 * set type 401 */ 402 switch (vp->v_type) { 403 case VNON: 404 type = "non"; break; 405 case VREG: 406 type = "reg"; break; 407 case VDIR: 408 type = "dir"; break; 409 case VBLK: 410 type = "blk"; break; 411 case VCHR: 412 type = "chr"; break; 413 case VLNK: 414 type = "lnk"; break; 415 case VSOCK: 416 type = "soc"; break; 417 case VFIFO: 418 type = "fif"; break; 419 case VBAD: 420 type = "bad"; break; 421 default: 422 type = "unk"; break; 423 } 424 /* 425 * gather flags 426 */ 427 (void)getflags(vnode_flags, flags, vp->v_flag); 428 429 ovflw = 0; 430 PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)avnode); 431 PRWORD(ovflw, " %*s", 4, 1, type); 432 PRWORD(ovflw, " %*s", 6, 1, flags); 433 PRWORD(ovflw, " %*ld", 5, 1, (long)vp->v_usecount); 434 PRWORD(ovflw, " %*ld", 5, 1, (long)vp->v_holdcnt); 435 PRWORD(ovflw, " %*d", 4, 1, vp->v_tag); 436 PRWORD(ovflw, " %*d", 6, 1, vp->v_uobj.uo_npages); 437 return (ovflw); 438 } 439 440 const struct flagbit_desc ufs_flags[] = { 441 { IN_ACCESS, 'A' }, 442 { IN_CHANGE, 'C' }, 443 { IN_UPDATE, 'U' }, 444 { IN_MODIFIED, 'M' }, 445 { IN_ACCESSED, 'a' }, 446 { IN_RENAME, 'R' }, 447 { IN_SHLOCK, 'S' }, 448 { IN_EXLOCK, 'E' }, 449 { IN_CLEANING, 'c' }, 450 { IN_ADIROP, 'D' }, 451 { IN_SPACECOUNTED, 's' }, 452 { 0, '\0' }, 453 }; 454 455 void 456 ufs_header() 457 { 458 459 (void)printf(" FILEID IFLAG RDEV|SZ"); 460 } 461 462 int 463 ufs_print(vp, ovflw) 464 struct vnode *vp; 465 int ovflw; 466 { 467 struct inode inode, *ip = &inode; 468 char flags[sizeof(ufs_flags) / sizeof(ufs_flags[0])]; 469 char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */ 470 char *name; 471 mode_t type; 472 473 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 474 475 /* 476 * XXX need to to locking state. 477 */ 478 479 (void)getflags(ufs_flags, flags, ip->i_flag); 480 PRWORD(ovflw, " %*d", 7, 1, ip->i_number); 481 PRWORD(ovflw, " %*s", 6, 1, flags); 482 type = ip->i_ffs_mode & S_IFMT; 483 if (S_ISCHR(ip->i_ffs_mode) || S_ISBLK(ip->i_ffs_mode)) { 484 if (usenumflag || 485 (name = devname(ip->i_ffs_rdev, type)) == NULL) { 486 snprintf(dev, sizeof(dev), "%d,%d", 487 major(ip->i_ffs_rdev), minor(ip->i_ffs_rdev)); 488 name = dev; 489 } 490 PRWORD(ovflw, " %*s", 8, 1, name); 491 } else 492 PRWORD(ovflw, " %*lld", 8, 1, (long long)ip->i_ffs_size); 493 return (0); 494 } 495 496 int 497 ext2fs_print(vp, ovflw) 498 struct vnode *vp; 499 int ovflw; 500 { 501 struct inode inode, *ip = &inode; 502 char flags[sizeof(ufs_flags) / sizeof(ufs_flags[0])]; 503 char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */ 504 char *name; 505 mode_t type; 506 507 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 508 509 /* 510 * XXX need to to locking state. 511 */ 512 513 (void)getflags(ufs_flags, flags, ip->i_flag); 514 PRWORD(ovflw, " %*d", 7, 1, ip->i_number); 515 PRWORD(ovflw, " %*s", 6, 1, flags); 516 type = ip->i_e2fs_mode & S_IFMT; 517 if (S_ISCHR(ip->i_e2fs_mode) || S_ISBLK(ip->i_e2fs_mode)) { 518 if (usenumflag || 519 (name = devname(ip->i_e2fs_rdev, type)) == NULL) { 520 snprintf(dev, sizeof(dev), "%d,%d", 521 major(ip->i_e2fs_rdev), minor(ip->i_e2fs_rdev)); 522 name = dev; 523 } 524 PRWORD(ovflw, " %*s", 8, 1, name); 525 } else 526 PRWORD(ovflw, " %*u", 8, 1, (u_int)ip->i_e2fs_size); 527 return (0); 528 } 529 530 const struct flagbit_desc nfs_flags[] = { 531 { NFLUSHWANT, 'W' }, 532 { NFLUSHINPROG, 'P' }, 533 { NMODIFIED, 'M' }, 534 { NWRITEERR, 'E' }, 535 { NQNFSNONCACHE, 'X' }, 536 { NQNFSWRITE, 'O' }, 537 { NQNFSEVICTED, 'G' }, 538 { NACC, 'A' }, 539 { NUPD, 'U' }, 540 { NCHG, 'C' }, 541 { 0, '\0' }, 542 }; 543 544 void 545 nfs_header() 546 { 547 548 (void)printf(" FILEID NFLAG RDEV|SZ"); 549 } 550 551 int 552 nfs_print(vp, ovflw) 553 struct vnode *vp; 554 int ovflw; 555 { 556 struct nfsnode nfsnode, *np = &nfsnode; 557 char flags[sizeof(nfs_flags) / sizeof(nfs_flags[0])]; 558 char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */ 559 struct vattr va; 560 char *name; 561 mode_t type; 562 563 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode"); 564 (void)getflags(nfs_flags, flags, np->n_flag); 565 566 KGETRET(np->n_vattr, &va, sizeof(va), "vnode attr"); 567 PRWORD(ovflw, " %*ld", 7, 1, (long)va.va_fileid); 568 PRWORD(ovflw, " %*s", 6, 1, flags); 569 switch (va.va_type) { 570 case VCHR: 571 type = S_IFCHR; 572 goto device; 573 574 case VBLK: 575 type = S_IFBLK; 576 device: 577 if (usenumflag || (name = devname(va.va_rdev, type)) == NULL) { 578 (void)snprintf(dev, sizeof(dev), "%d,%d", 579 major(va.va_rdev), minor(va.va_rdev)); 580 name = dev; 581 } 582 PRWORD(ovflw, " %*s", 8, 1, name); 583 break; 584 default: 585 PRWORD(ovflw, " %*lld", 8, 1, (long long)np->n_size); 586 break; 587 } 588 return (0); 589 } 590 591 void 592 layer_header() 593 { 594 595 (void)printf(" %*s", PTRSTRWIDTH, "LOWER"); 596 } 597 598 int 599 layer_print(vp, ovflw) 600 struct vnode *vp; 601 int ovflw; 602 { 603 struct layer_node lnode, *lp = &lnode; 604 605 KGETRET(VTOLAYER(vp), &lnode, sizeof(lnode), "layer vnode"); 606 607 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)lp->layer_lowervp); 608 return (0); 609 } 610 611 void 612 union_header() 613 { 614 615 (void)printf(" %*s %*s", PTRSTRWIDTH, "UPPER", PTRSTRWIDTH, "LOWER"); 616 } 617 618 int 619 union_print(vp, ovflw) 620 struct vnode *vp; 621 int ovflw; 622 { 623 struct union_node unode, *up = &unode; 624 625 KGETRET(VTOUNION(vp), &unode, sizeof(unode), "vnode's unode"); 626 627 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)up->un_uppervp); 628 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)up->un_lowervp); 629 return (0); 630 } 631 632 /* 633 * Given a pointer to a mount structure in kernel space, 634 * read it in and return a usable pointer to it. 635 */ 636 struct mount * 637 getmnt(maddr) 638 struct mount *maddr; 639 { 640 static struct mtab { 641 struct mtab *next; 642 struct mount *maddr; 643 struct mount mount; 644 } *mhead = NULL; 645 struct mtab *mt; 646 647 for (mt = mhead; mt != NULL; mt = mt->next) 648 if (maddr == mt->maddr) 649 return (&mt->mount); 650 if ((mt = malloc(sizeof(struct mtab))) == NULL) 651 err(1, "malloc"); 652 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table"); 653 mt->maddr = maddr; 654 mt->next = mhead; 655 mhead = mt; 656 return (&mt->mount); 657 } 658 659 void 660 mount_print(mp) 661 struct mount *mp; 662 { 663 int flags; 664 665 (void)printf("*** MOUNT %s %s on %s", ST.f_fstypename, 666 ST.f_mntfromname, ST.f_mntonname); 667 if ((flags = mp->mnt_flag) != 0) { 668 int i; 669 const char *sep = " ("; 670 671 for (i = 0; i <= sizeof mnt_flags / sizeof mnt_flags[0]; i++) { 672 if (flags & mnt_flags[i].m_flag) { 673 (void)printf("%s%s", sep, mnt_flags[i].m_name); 674 flags &= ~mnt_flags[i].m_flag; 675 sep = ","; 676 } 677 } 678 if (flags) 679 (void)printf("%sunknown_flags:%x", sep, flags); 680 (void)printf(")"); 681 } 682 (void)printf("\n"); 683 } 684 685 char * 686 loadvnodes(avnodes) 687 int *avnodes; 688 { 689 int mib[2]; 690 size_t copysize; 691 char *vnodebase; 692 693 if (memf != NULL) { 694 /* 695 * do it by hand 696 */ 697 return (kinfo_vnodes(avnodes)); 698 } 699 mib[0] = CTL_KERN; 700 mib[1] = KERN_VNODE; 701 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1) 702 err(1, "sysctl: KERN_VNODE"); 703 if ((vnodebase = malloc(copysize)) == NULL) 704 err(1, "malloc"); 705 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1) 706 err(1, "sysctl: KERN_VNODE"); 707 if (copysize % (VPTRSZ + VNODESZ)) 708 errx(1, "vnode size mismatch"); 709 *avnodes = copysize / (VPTRSZ + VNODESZ); 710 711 return (vnodebase); 712 } 713 714 /* 715 * simulate what a running kernel does in in kinfo_vnode 716 */ 717 char * 718 kinfo_vnodes(avnodes) 719 int *avnodes; 720 { 721 struct mntlist mountlist; 722 struct mount *mp, mount; 723 struct vnode *vp, vnode; 724 char *beg, *bp, *ep; 725 int numvnodes; 726 727 KGET(V_NUMV, numvnodes); 728 if ((bp = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL) 729 err(1, "malloc"); 730 beg = bp; 731 ep = bp + (numvnodes + 20) * (VPTRSZ + VNODESZ); 732 KGET(V_MOUNTLIST, mountlist); 733 for (mp = mountlist.cqh_first;; 734 mp = mount.mnt_list.cqe_next) { 735 KGET2(mp, &mount, sizeof(mount), "mount entry"); 736 for (vp = mount.mnt_vnodelist.lh_first; 737 vp != NULL; vp = vnode.v_mntvnodes.le_next) { 738 KGET2(vp, &vnode, sizeof(vnode), "vnode"); 739 if (bp + VPTRSZ + VNODESZ > ep) 740 /* XXX - should realloc */ 741 errx(1, "no more room for vnodes"); 742 memmove(bp, &vp, VPTRSZ); 743 bp += VPTRSZ; 744 memmove(bp, &vnode, VNODESZ); 745 bp += VNODESZ; 746 } 747 if (mp == mountlist.cqh_last) 748 break; 749 } 750 *avnodes = (bp - beg) / (VPTRSZ + VNODESZ); 751 return (beg); 752 } 753 754 void 755 ttymode() 756 { 757 int ntty; 758 struct ttylist_head tty_head; 759 struct tty *tp, tty; 760 761 KGET(TTY_NTTY, ntty); 762 (void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s"); 763 KGET(TTY_TTYLIST, tty_head); 764 (void)printf( 765 " LINE RAW CAN OUT HWT LWT COL STATE %-*s PGID DISC\n", 766 PTRSTRWIDTH, "SESS"); 767 for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) { 768 KGET2(tp, &tty, sizeof tty, "tty struct"); 769 ttyprt(&tty); 770 } 771 } 772 773 static const struct flagbit_desc ttystates[] = { 774 { TS_ISOPEN, 'O'}, 775 { TS_DIALOUT, '>'}, 776 { TS_CARR_ON, 'C'}, 777 { TS_TIMEOUT, 'T'}, 778 { TS_FLUSH, 'F'}, 779 { TS_BUSY, 'B'}, 780 { TS_ASLEEP, 'A'}, 781 { TS_XCLUDE, 'X'}, 782 { TS_TTSTOP, 'S'}, 783 { TS_TBLOCK, 'K'}, 784 { TS_ASYNC, 'Y'}, 785 { TS_BKSL, 'D'}, 786 { TS_ERASE, 'E'}, 787 { TS_LNCH, 'L'}, 788 { TS_TYPEN, 'P'}, 789 { TS_CNTTB, 'N'}, 790 { 0, '\0'}, 791 }; 792 793 void 794 ttyprt(tp) 795 struct tty *tp; 796 { 797 char state[sizeof(ttystates) / sizeof(ttystates[0]) + 1]; 798 char dev[2 + 3 + 1 + 5 + 1]; /* 12bit major + 20bit minor */ 799 struct linesw t_linesw; 800 char *name, buffer; 801 pid_t pgid; 802 int n, ovflw; 803 804 if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL) { 805 (void)snprintf(dev, sizeof(dev), "0x%3x:%x", 806 major(tp->t_dev), minor(tp->t_dev)); 807 name = dev; 808 } 809 ovflw = 0; 810 PRWORD(ovflw, "%-*s", 7, 0, name); 811 PRWORD(ovflw, " %*d", 3, 1, tp->t_rawq.c_cc); 812 PRWORD(ovflw, " %*d", 4, 1, tp->t_canq.c_cc); 813 PRWORD(ovflw, " %*d", 4, 1, tp->t_outq.c_cc); 814 PRWORD(ovflw, " %*d", 5, 1, tp->t_hiwat); 815 PRWORD(ovflw, " %*d", 4, 1, tp->t_lowat); 816 PRWORD(ovflw, " %*d", 8, 1, tp->t_column); 817 n = getflags(ttystates, state, tp->t_state); 818 if (tp->t_wopen) { 819 state[n++] = 'W'; 820 state[n] = '\0'; 821 } 822 PRWORD(ovflw, " %-*s", 7, 1, state); 823 PRWORD(ovflw, " %*lX", PTRSTRWIDTH + 1, 1, (u_long)tp->t_session); 824 pgid = 0; 825 if (tp->t_pgrp != NULL) 826 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid"); 827 PRWORD(ovflw, " %*d", 6, 1, pgid); 828 KGET2(tp->t_linesw, &t_linesw, sizeof(t_linesw), 829 "line discipline switch table"); 830 name = t_linesw.l_name; 831 (void)putchar(' '); 832 for (;;) { 833 KGET2(name, &buffer, sizeof(buffer), "line discipline name"); 834 if (buffer == '\0') 835 break; 836 (void)putchar(buffer); 837 name++; 838 } 839 (void)putchar('\n'); 840 } 841 842 static const struct flagbit_desc filemode_flags[] = { 843 { FREAD, 'R' }, 844 { FWRITE, 'W' }, 845 { FAPPEND, 'A' }, 846 #ifdef FSHLOCK /* currently gone */ 847 { FSHLOCK, 'S' }, 848 { FEXLOCK, 'X' }, 849 #endif 850 { FASYNC, 'I' }, 851 { 0, '\0' }, 852 }; 853 854 void 855 filemode() 856 { 857 struct file *fp; 858 struct file *addr; 859 char flags[sizeof(filemode_flags) / sizeof(filemode_flags[0])]; 860 char *buf; 861 int len, maxfile, nfile, ovflw; 862 static const char * const dtypes[] = 863 { "???", "inode", "socket", "pipe" }; 864 865 KGET(FNL_MAXFILE, maxfile); 866 if (totalflag) { 867 KGET(FNL_NFILE, nfile); 868 (void)printf("%3d/%3d files\n", nfile, maxfile); 869 return; 870 } 871 if (getfiles(&buf, &len) == -1) 872 return; 873 /* 874 * Getfiles returns in malloc'd memory a pointer to the first file 875 * structure, and then an array of file structs (whose addresses are 876 * derivable from the previous entry). 877 */ 878 addr = ((struct filelist *)buf)->lh_first; 879 fp = (struct file *)(buf + sizeof(struct filelist)); 880 nfile = (len - sizeof(struct filelist)) / sizeof(struct file); 881 882 (void)printf("%d/%d open files\n", nfile, maxfile); 883 (void)printf("%*s%s%*s TYPE FLG CNT MSG %*s%s%*s OFFSET\n", 884 (PTRSTRWIDTH - 4) / 2, "", " LOC", (PTRSTRWIDTH - 4) / 2, "", 885 (PTRSTRWIDTH - 4) / 2, "", "DATA", (PTRSTRWIDTH - 4) / 2, ""); 886 for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) { 887 if ((unsigned)fp->f_type > DTYPE_PIPE) 888 continue; 889 ovflw = 0; 890 (void)getflags(filemode_flags, flags, fp->f_flag); 891 PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)addr); 892 PRWORD(ovflw, " %-*s", 9, 1, dtypes[fp->f_type]); 893 PRWORD(ovflw, " %*s", 6, 1, flags); 894 PRWORD(ovflw, " %*d", 5, 1, fp->f_count); 895 PRWORD(ovflw, " %*d", 5, 1, fp->f_msgcount); 896 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 2, (long)fp->f_data); 897 if (fp->f_offset < 0) 898 PRWORD(ovflw, " %-*llx\n", PTRSTRWIDTH + 1, 2, 899 (long long)fp->f_offset); 900 else 901 PRWORD(ovflw, " %-*lld\n", PTRSTRWIDTH + 1, 2, 902 (long long)fp->f_offset); 903 } 904 free(buf); 905 } 906 907 int 908 getfiles(abuf, alen) 909 char **abuf; 910 int *alen; 911 { 912 size_t len; 913 int mib[2]; 914 char *buf; 915 916 /* 917 * XXX 918 * Add emulation of KINFO_FILE here. 919 */ 920 if (memf != NULL) 921 errx(1, "files on dead kernel, not implemented"); 922 923 mib[0] = CTL_KERN; 924 mib[1] = KERN_FILE; 925 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 926 warn("sysctl: KERN_FILE"); 927 return (-1); 928 } 929 if ((buf = malloc(len)) == NULL) 930 err(1, "malloc"); 931 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 932 warn("sysctl: KERN_FILE"); 933 return (-1); 934 } 935 *abuf = buf; 936 *alen = len; 937 return (0); 938 } 939 940 void 941 usage() 942 { 943 944 (void)fprintf(stderr, 945 "usage: %s [-T|-f|-s|-t|-v] [-kn] [-M core] [-N system]\n", 946 getprogname()); 947 exit(1); 948 } 949