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