1 /* $NetBSD: pstat.c,v 1.128 2017/05/04 16:26:09 sevan 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\ 35 The Regents of the University of California. All rights reserved."); 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.128 2017/05/04 16:26:09 sevan Exp $"); 43 #endif 44 #endif /* not lint */ 45 46 #define _KERNEL 47 #include <sys/types.h> 48 #undef _KERNEL 49 #include <sys/param.h> 50 #include <sys/time.h> 51 #include <sys/vnode.h> 52 #include <sys/ucred.h> 53 #include <stdbool.h> 54 #define _KERNEL 55 #define NFS 56 #include <sys/mount.h> 57 #undef NFS 58 #include <sys/file.h> 59 #include <ufs/ufs/inode.h> 60 #include <ufs/ufs/ufsmount.h> 61 #include <sys/uio.h> 62 #include <miscfs/genfs/layer.h> 63 #undef _KERNEL 64 #include <sys/stat.h> 65 #include <nfs/nfsproto.h> 66 #include <nfs/rpcv2.h> 67 #include <nfs/nfs.h> 68 #include <nfs/nfsnode.h> 69 #include <sys/ioctl.h> 70 #include <sys/tty.h> 71 #include <sys/conf.h> 72 73 #include <sys/sysctl.h> 74 75 #include <err.h> 76 #include <errno.h> 77 #include <kvm.h> 78 #include <limits.h> 79 #include <nlist.h> 80 #include <stdio.h> 81 #include <stdlib.h> 82 #include <string.h> 83 #include <unistd.h> 84 85 #include "swapctl.h" 86 87 struct nlist nl[] = { 88 #define V_LRU_FREE_LIST 0 89 { "_lru_free_list", 0, 0, 0, 0 }, /* address of lru free list. */ 90 #define V_LRU_HOLD_LIST 1 91 { "_lru_hold_list", 0, 0, 0, 0 }, /* address of lru hold list. */ 92 #define V_LRU_VRELE_LIST 2 93 { "_lru_vrele_list", 0, 0, 0, 0 }, /* address of lru vrele list. */ 94 #define V_NUMV 3 95 { "_numvnodes", 0, 0, 0, 0 }, 96 #define V_NEXT_OFFSET 4 97 { "_vnode_offset_next_by_lru", 0, 0, 0, 0 }, 98 #define FNL_NFILE 5 99 { "_nfiles", 0, 0, 0, 0 }, 100 #define FNL_MAXFILE 6 101 { "_maxfiles", 0, 0, 0, 0 }, 102 #define TTY_NTTY 7 103 { "_tty_count", 0, 0, 0, 0 }, 104 #define TTY_TTYLIST 8 105 { "_ttylist", 0, 0, 0, 0 }, 106 #define NLMANDATORY TTY_TTYLIST /* names up to here are mandatory */ 107 { "", 0, 0, 0, 0 } 108 }; 109 110 int usenumflag; 111 int totalflag; 112 int kflag; 113 int hflag; 114 char *nlistf = NULL; 115 char *memf = NULL; 116 kvm_t *kd; 117 118 static const char * const dtypes[] = { DTYPE_NAMES }; 119 120 121 static const struct { 122 u_int m_flag; 123 u_int m_visible; 124 const char *m_name; 125 } mnt_flags[] = { 126 __MNT_FLAGS 127 }; 128 129 struct flagbit_desc { 130 u_int fd_flags; 131 char fd_mark; 132 }; 133 134 #define SVAR(var) __STRING(var) /* to force expansion */ 135 #define KGET(idx, var) \ 136 KGET1(idx, &var, sizeof(var), SVAR(var)) 137 #define KGET1(idx, p, s, msg) \ 138 KGET2(nl[idx].n_value, p, s, msg) 139 #define KGET2(addr, p, s, msg) do { \ 140 if (kvm_read(kd, (u_long)(addr), p, s) != s) \ 141 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 142 } while (/* CONSTCOND */0) 143 #define KGETRET(addr, p, s, msg) do { \ 144 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \ 145 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 146 return (0); \ 147 } \ 148 } while (/* CONSTCOND */0) 149 150 #if 1 /* This is copied from vmstat/vmstat.c */ 151 /* 152 * Print single word. `ovflow' is number of characters didn't fit 153 * on the last word. `fmt' is a format string to print this word. 154 * It must contain asterisk for field width. `width' is a width 155 * occupied by this word. `fixed' is a number of constant chars in 156 * `fmt'. `val' is a value to be printed using format string `fmt'. 157 */ 158 #define PRWORD(ovflw, fmt, width, fixed, val) do { \ 159 (ovflw) += printf((fmt), \ 160 (width) - (fixed) - (ovflw) > 0 ? \ 161 (width) - (fixed) - (ovflw) : 0, \ 162 (val)) - (width); \ 163 if ((ovflw) < 0) \ 164 (ovflw) = 0; \ 165 } while (/* CONSTCOND */0) 166 #endif 167 168 void filemode(void); 169 int getfiles(char **, int *, char **); 170 int getflags(const struct flagbit_desc *, char *, u_int); 171 struct mount * 172 getmnt(struct mount *); 173 char * kinfo_vnodes(int *); 174 void layer_header(void); 175 int layer_print(struct vnode *, int); 176 char * loadvnodes(int *); 177 void mount_print(struct mount *); 178 void nfs_header(void); 179 int nfs_print(struct vnode *, int); 180 void ttymode(void); 181 void ttyprt(struct tty *); 182 void ufs_header(void); 183 int ufs_print(struct vnode *, int); 184 int ext2fs_print(struct vnode *, int); 185 __dead void usage(void); 186 void vnode_header(void); 187 int vnode_print(struct vnode *, struct vnode *); 188 void vnodemode(void); 189 190 int 191 main(int argc, char *argv[]) 192 { 193 int ch, i, quit, ret, use_sysctl; 194 int fileflag, swapflag, ttyflag, vnodeflag; 195 gid_t egid = getegid(); 196 char buf[_POSIX2_LINE_MAX]; 197 198 setegid(getgid()); 199 fileflag = swapflag = ttyflag = vnodeflag = 0; 200 while ((ch = getopt(argc, argv, "TM:N:fghikmnstv")) != -1) 201 switch (ch) { 202 case 'f': 203 fileflag = 1; 204 break; 205 case 'M': 206 memf = optarg; 207 break; 208 case 'N': 209 nlistf = optarg; 210 break; 211 case 'n': 212 usenumflag = 1; 213 break; 214 case 's': 215 swapflag = 1; 216 break; 217 case 'T': 218 totalflag = 1; 219 break; 220 case 't': 221 ttyflag = 1; 222 break; 223 case 'k': 224 kflag = 1; 225 break; 226 case 'g': 227 kflag = 3; /* 1k ^ 3 */ 228 break; 229 case 'h': 230 hflag = 1; 231 break; 232 case 'm': 233 kflag = 2; /* 1k ^ 2 */ 234 break; 235 case 'v': 236 case 'i': /* Backward compatibility. */ 237 vnodeflag = 1; 238 break; 239 default: 240 usage(); 241 } 242 argc -= optind; 243 argv += optind; 244 245 /* 246 * Discard setgid privileges. If not the running kernel, we toss 247 * them away totally so that bad guys can't print interesting stuff 248 * from kernel memory, otherwise switch back to kmem for the 249 * duration of the kvm_openfiles() call. 250 */ 251 if (nlistf != NULL || memf != NULL) 252 (void)setgid(getgid()); 253 else 254 (void)setegid(egid); 255 256 use_sysctl = (nlistf == NULL && memf == NULL); 257 258 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == 0) 259 errx(1, "kvm_openfiles: %s", buf); 260 261 /* get rid of it now anyway */ 262 if (nlistf == NULL && memf == NULL) 263 (void)setgid(getgid()); 264 if ((ret = kvm_nlist(kd, nl)) != 0) { 265 if (ret == -1) 266 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 267 for (i = quit = 0; i <= NLMANDATORY; i++) 268 if (!nl[i].n_value) { 269 quit = 1; 270 warnx("undefined symbol: %s", nl[i].n_name); 271 } 272 if (quit) 273 exit(1); 274 } 275 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag)) 276 usage(); 277 if (fileflag || totalflag) 278 filemode(); 279 if (vnodeflag || totalflag) 280 vnodemode(); 281 if (ttyflag) 282 ttymode(); 283 if (swapflag || totalflag) 284 if (use_sysctl) 285 list_swap(0, kflag, 0, totalflag, 1, hflag); 286 exit(0); 287 } 288 289 #define VPTRSZ sizeof(struct vnode *) 290 #define VNODESZ sizeof(struct vnode) 291 #define PTRSTRWIDTH ((int)sizeof(void *) * 2) /* Width of resulting string 292 when pointer is printed 293 in hexadecimal. */ 294 295 static void 296 devprintf(char *buf, size_t buflen, dev_t dev) 297 { 298 (void)snprintf(buf, buflen, "%llu,%llu", 299 (unsigned long long)major(dev), 300 (unsigned long long)minor(dev)); 301 } 302 303 void 304 vnodemode(void) 305 { 306 char *e_vnodebase, *endvnode, *evp; 307 struct vnode *vp; 308 struct mount *maddr, *mp; 309 int numvnodes, ovflw; 310 int (*vnode_fsprint) (struct vnode *, int); /* per-fs data printer */ 311 312 mp = NULL; 313 e_vnodebase = loadvnodes(&numvnodes); 314 if (totalflag) { 315 (void)printf("%7d vnodes\n", numvnodes); 316 goto out; 317 } 318 endvnode = e_vnodebase + numvnodes * (VPTRSZ + VNODESZ); 319 (void)printf("%d active vnodes\n", numvnodes); 320 321 #define ST mp->mnt_stat 322 #define FSTYPE_IS(mp, name) \ 323 (strncmp((mp)->mnt_stat.f_fstypename, (name), \ 324 sizeof((mp)->mnt_stat.f_fstypename)) == 0) 325 maddr = NULL; 326 vnode_fsprint = NULL; 327 for (evp = e_vnodebase; evp < endvnode; evp += VPTRSZ + VNODESZ) { 328 vp = (struct vnode *)(evp + VPTRSZ); 329 if (vp->v_mount != maddr) { 330 /* 331 * New filesystem 332 */ 333 if ((mp = getmnt(vp->v_mount)) == NULL) 334 continue; 335 maddr = vp->v_mount; 336 mount_print(mp); 337 vnode_header(); 338 /* 339 * XXX do this in a more fs-independent way 340 */ 341 if (FSTYPE_IS(mp, MOUNT_FFS) || 342 FSTYPE_IS(mp, MOUNT_MFS)) { 343 ufs_header(); 344 vnode_fsprint = ufs_print; 345 } else if (FSTYPE_IS(mp, MOUNT_NFS)) { 346 nfs_header(); 347 vnode_fsprint = nfs_print; 348 } else if (FSTYPE_IS(mp, MOUNT_EXT2FS)) { 349 ufs_header(); 350 vnode_fsprint = ext2fs_print; 351 } else if (FSTYPE_IS(mp, MOUNT_NULL) || 352 FSTYPE_IS(mp, MOUNT_OVERLAY) || 353 FSTYPE_IS(mp, MOUNT_UMAP)) { 354 layer_header(); 355 vnode_fsprint = layer_print; 356 } else 357 vnode_fsprint = NULL; 358 (void)printf("\n"); 359 } 360 ovflw = vnode_print(*(struct vnode **)evp, vp); 361 if (VTOI(vp) != NULL && vnode_fsprint != NULL) 362 (*vnode_fsprint)(vp, ovflw); 363 (void)printf("\n"); 364 } 365 366 out: 367 if (e_vnodebase) 368 free(e_vnodebase); 369 } 370 371 int 372 getflags(const struct flagbit_desc *fd, char *p, u_int flags) 373 { 374 char *q = p; 375 376 if (flags == 0) { 377 *p++ = '-'; 378 *p = '\0'; 379 return (0); 380 } 381 382 for (; fd->fd_flags != 0; fd++) 383 if ((flags & fd->fd_flags) != 0) 384 *p++ = fd->fd_mark; 385 *p = '\0'; 386 return (p - q); 387 } 388 389 const struct flagbit_desc vnode_flags[] = { 390 { VV_ROOT, 'R' }, 391 { VI_TEXT, 'T' }, 392 { VV_SYSTEM, 'S' }, 393 { VV_ISTTY, 'I' }, 394 { VI_EXECMAP, 'E' }, 395 { VU_DIROP, 'D' }, 396 { VI_ONWORKLST, 'O' }, 397 { 0, '\0' }, 398 }; 399 400 void 401 vnode_header(void) 402 { 403 404 (void)printf("%-*s TYP VFLAG USE HOLD TAG NPAGE", 405 PTRSTRWIDTH, "ADDR"); 406 } 407 408 int 409 vnode_print(struct vnode *avnode, struct vnode *vp) 410 { 411 const char *type; 412 char flags[sizeof(vnode_flags) / sizeof(vnode_flags[0])]; 413 int ovflw; 414 415 /* 416 * set type 417 */ 418 switch (vp->v_type) { 419 case VNON: 420 type = "non"; break; 421 case VREG: 422 type = "reg"; break; 423 case VDIR: 424 type = "dir"; break; 425 case VBLK: 426 type = "blk"; break; 427 case VCHR: 428 type = "chr"; break; 429 case VLNK: 430 type = "lnk"; break; 431 case VSOCK: 432 type = "soc"; break; 433 case VFIFO: 434 type = "fif"; break; 435 case VBAD: 436 type = "bad"; break; 437 default: 438 type = "unk"; break; 439 } 440 /* 441 * gather flags 442 */ 443 (void)getflags(vnode_flags, flags, 444 vp->v_uflag | vp->v_iflag | vp->v_vflag); 445 446 ovflw = 0; 447 PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)avnode); 448 PRWORD(ovflw, " %*s", 4, 1, type); 449 PRWORD(ovflw, " %*s", 6, 1, flags); 450 PRWORD(ovflw, " %*ld", 5, 1, (long)vp->v_usecount); 451 PRWORD(ovflw, " %*ld", 5, 1, (long)vp->v_holdcnt); 452 PRWORD(ovflw, " %*d", 4, 1, vp->v_tag); 453 PRWORD(ovflw, " %*d", 6, 1, vp->v_uobj.uo_npages); 454 return (ovflw); 455 } 456 457 const struct flagbit_desc ufs_flags[] = { 458 { IN_ACCESS, 'A' }, 459 { IN_CHANGE, 'C' }, 460 { IN_UPDATE, 'U' }, 461 { IN_MODIFIED, 'M' }, 462 { IN_ACCESSED, 'a' }, 463 { IN_SHLOCK, 'S' }, 464 { IN_EXLOCK, 'E' }, 465 { IN_SPACECOUNTED, 's' }, 466 { 0, '\0' }, 467 }; 468 469 void 470 ufs_header(void) 471 { 472 473 (void)printf(" FILEID IFLAG RDEV|SZ"); 474 } 475 476 int 477 ufs_print(struct vnode *vp, int ovflw) 478 { 479 struct inode inode, *ip = &inode; 480 union dinode { 481 struct ufs1_dinode dp1; 482 struct ufs2_dinode dp2; 483 } dip; 484 struct ufsmount ump; 485 char flags[sizeof(ufs_flags) / sizeof(ufs_flags[0])]; 486 char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */ 487 char *name; 488 mode_t type; 489 dev_t rdev; 490 491 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 492 KGETRET(ip->i_ump, &ump, sizeof(struct ufsmount), 493 "vnode's mount point"); 494 495 if (ump.um_fstype == UFS1) { 496 KGETRET(ip->i_din.ffs1_din, &dip, sizeof (struct ufs1_dinode), 497 "inode's dinode"); 498 rdev = (uint32_t)dip.dp1.di_rdev; 499 } else { 500 KGETRET(ip->i_din.ffs2_din, &dip, sizeof (struct ufs2_dinode), 501 "inode's UFS2 dinode"); 502 rdev = dip.dp2.di_rdev; 503 } 504 505 /* 506 * XXX need to to locking state. 507 */ 508 509 (void)getflags(ufs_flags, flags, ip->i_flag); 510 PRWORD(ovflw, " %*llu", 7, 1, (unsigned long long)ip->i_number); 511 PRWORD(ovflw, " %*s", 6, 1, flags); 512 type = ip->i_mode & S_IFMT; 513 if (S_ISCHR(ip->i_mode) || S_ISBLK(ip->i_mode)) { 514 if (usenumflag || 515 (name = devname(rdev, type)) == NULL) { 516 devprintf(dev, sizeof(dev), rdev); 517 name = dev; 518 } 519 PRWORD(ovflw, " %*s", 8, 1, name); 520 } else 521 PRWORD(ovflw, " %*lld", 8, 1, (long long)ip->i_size); 522 return 0; 523 } 524 525 int 526 ext2fs_print(struct vnode *vp, int ovflw) 527 { 528 struct inode inode, *ip = &inode; 529 struct ext2fs_dinode dip; 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 KGETRET(ip->i_din.e2fs_din, &dip, sizeof (struct ext2fs_dinode), 537 "inode's dinode"); 538 539 /* 540 * XXX need to to locking state. 541 */ 542 543 (void)getflags(ufs_flags, flags, ip->i_flag); 544 PRWORD(ovflw, " %*llu", 7, 1, (unsigned long long)ip->i_number); 545 PRWORD(ovflw, " %*s", 6, 1, flags); 546 type = dip.e2di_mode & S_IFMT; 547 if (S_ISCHR(dip.e2di_mode) || S_ISBLK(dip.e2di_mode)) { 548 if (usenumflag || 549 (name = devname(dip.e2di_rdev, type)) == NULL) { 550 devprintf(dev, sizeof(dev), dip.e2di_rdev); 551 name = dev; 552 } 553 PRWORD(ovflw, " %*s", 8, 1, name); 554 } else 555 PRWORD(ovflw, " %*u", 8, 1, (u_int)dip.e2di_size); 556 return (0); 557 } 558 559 const struct flagbit_desc nfs_flags[] = { 560 { NFLUSHWANT, 'W' }, 561 { NFLUSHINPROG, 'P' }, 562 { NMODIFIED, 'M' }, 563 { NWRITEERR, 'E' }, 564 { NACC, 'A' }, 565 { NUPD, 'U' }, 566 { NCHG, 'C' }, 567 { 0, '\0' }, 568 }; 569 570 void 571 nfs_header(void) 572 { 573 574 (void)printf(" FILEID NFLAG RDEV|SZ"); 575 } 576 577 int 578 nfs_print(struct vnode *vp, int ovflw) 579 { 580 struct nfsnode nfsnode, *np = &nfsnode; 581 char flags[sizeof(nfs_flags) / sizeof(nfs_flags[0])]; 582 char dev[4 + 1 + 7 + 1]; /* 12bit marjor + 20bit minor */ 583 struct vattr va; 584 char *name; 585 mode_t type; 586 587 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode"); 588 (void)getflags(nfs_flags, flags, np->n_flag); 589 590 KGETRET(np->n_vattr, &va, sizeof(va), "vnode attr"); 591 PRWORD(ovflw, " %*ld", 7, 1, (long)va.va_fileid); 592 PRWORD(ovflw, " %*s", 6, 1, flags); 593 switch (va.va_type) { 594 case VCHR: 595 type = S_IFCHR; 596 goto device; 597 598 case VBLK: 599 type = S_IFBLK; 600 device: 601 if (usenumflag || (name = devname(va.va_rdev, type)) == NULL) { 602 devprintf(dev, sizeof(dev), va.va_rdev); 603 name = dev; 604 } 605 PRWORD(ovflw, " %*s", 8, 1, name); 606 break; 607 default: 608 PRWORD(ovflw, " %*lld", 8, 1, (long long)np->n_size); 609 break; 610 } 611 return (0); 612 } 613 614 void 615 layer_header(void) 616 { 617 618 (void)printf(" %*s", PTRSTRWIDTH, "LOWER"); 619 } 620 621 int 622 layer_print(struct vnode *vp, int ovflw) 623 { 624 struct layer_node lnode, *lp = &lnode; 625 626 KGETRET(VTOLAYER(vp), &lnode, sizeof(lnode), "layer vnode"); 627 628 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 1, (long)lp->layer_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(struct mount *maddr) 638 { 639 static struct mtab { 640 struct mtab *next; 641 struct mount *maddr; 642 struct mount mount; 643 } *mhead = NULL; 644 struct mtab *mt; 645 struct mount mb; 646 647 for (mt = mhead; mt != NULL; mt = mt->next) 648 if (maddr == mt->maddr) 649 return (&mt->mount); 650 KGETRET(maddr, &mb, sizeof(struct mount), "mount table"); 651 if ((mt = malloc(sizeof(struct mtab))) == NULL) 652 err(1, "malloc"); 653 mt->mount = mb; 654 mt->maddr = maddr; 655 mt->next = mhead; 656 mhead = mt; 657 return (&mt->mount); 658 } 659 660 void 661 mount_print(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 size_t 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(int *avnodes) 687 { 688 int mib[2]; 689 int status; 690 size_t copysize; 691 #if 0 692 size_t oldsize; 693 #endif 694 char *vnodebase; 695 696 if (totalflag) { 697 KGET(V_NUMV, *avnodes); 698 return NULL; 699 } 700 if (memf != NULL) { 701 /* 702 * do it by hand 703 */ 704 return (kinfo_vnodes(avnodes)); 705 } 706 mib[0] = CTL_KERN; 707 mib[1] = KERN_VNODE; 708 /* 709 * First sysctl call gets the necessary buffer size; second 710 * sysctl call gets the data. We allow for some growth in the 711 * data size between the two sysctl calls (increases of a few 712 * thousand vnodes in between the two calls have been observed). 713 * We ignore ENOMEM from the second sysctl call, which can 714 * happen if the kernel's data grew by even more than we allowed 715 * for. 716 */ 717 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1) 718 err(1, "sysctl: KERN_VNODE"); 719 #if 0 720 oldsize = copysize; 721 #endif 722 copysize += 100 * sizeof(struct vnode) + copysize / 20; 723 if ((vnodebase = malloc(copysize)) == NULL) 724 err(1, "malloc"); 725 status = sysctl(mib, 2, vnodebase, ©size, NULL, 0); 726 if (status == -1 && errno != ENOMEM) 727 err(1, "sysctl: KERN_VNODE"); 728 #if 0 /* for debugging the amount of growth allowed for */ 729 if (copysize != oldsize) { 730 warnx("count changed from %ld to %ld (%+ld)%s", 731 (long)(oldsize / sizeof(struct vnode)), 732 (long)(copysize / sizeof(struct vnode)), 733 (long)(copysize / sizeof(struct vnode)) - 734 (long)(oldsize / sizeof(struct vnode)), 735 (status == 0 ? "" : ", and errno = ENOMEM")); 736 } 737 #endif 738 if (copysize % (VPTRSZ + VNODESZ)) 739 errx(1, "vnode size mismatch"); 740 *avnodes = copysize / (VPTRSZ + VNODESZ); 741 742 return (vnodebase); 743 } 744 745 /* 746 * simulate what a running kernel does in in kinfo_vnode 747 */ 748 static int 749 vnode_cmp(const void *p1, const void *p2) 750 { 751 const char *s1 = (const char *)p1; 752 const char *s2 = (const char *)p2; 753 const struct vnode *v1 = (const struct vnode *)(s1 + VPTRSZ); 754 const struct vnode *v2 = (const struct vnode *)(s2 + VPTRSZ); 755 756 return (v2->v_mount - v1->v_mount); 757 } 758 759 char * 760 kinfo_vnodes(int *avnodes) 761 { 762 int i; 763 char *beg, *bp, *ep; 764 int numvnodes, next_offset; 765 766 KGET(V_NUMV, numvnodes); 767 if ((bp = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL) 768 err(1, "malloc"); 769 beg = bp; 770 ep = bp + (numvnodes + 20) * (VPTRSZ + VNODESZ); 771 KGET(V_NEXT_OFFSET, next_offset); 772 773 for (i = V_LRU_FREE_LIST; i <= V_LRU_VRELE_LIST; i++) { 774 TAILQ_HEAD(vnodelst, vnode) lru_head; 775 struct vnode *vp, vnode; 776 777 KGET(i, lru_head); 778 vp = TAILQ_FIRST(&lru_head); 779 while (vp != NULL) { 780 KGET2(vp, &vnode, sizeof(vnode), "vnode"); 781 if (bp + VPTRSZ + VNODESZ > ep) 782 /* XXX - should realloc */ 783 errx(1, "no more room for vnodes"); 784 memmove(bp, &vp, VPTRSZ); 785 bp += VPTRSZ; 786 memmove(bp, &vnode, VNODESZ); 787 bp += VNODESZ; 788 KGET2((char *)vp + next_offset, &vp, sizeof(vp), "nvp"); 789 } 790 } 791 *avnodes = (bp - beg) / (VPTRSZ + VNODESZ); 792 /* Sort by mount like we get it from sysctl. */ 793 qsort(beg, *avnodes, VPTRSZ + VNODESZ, vnode_cmp); 794 return (beg); 795 } 796 797 void 798 ttymode(void) 799 { 800 int ntty; 801 struct ttylist_head tty_head; 802 struct tty *tp, tty; 803 804 KGET(TTY_NTTY, ntty); 805 (void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s"); 806 KGET(TTY_TTYLIST, tty_head); 807 (void)printf( 808 " LINE RAW CAN OUT HWT LWT COL STATE %-*s PGID DISC\n", 809 PTRSTRWIDTH, "SESS"); 810 for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) { 811 KGET2(tp, &tty, sizeof tty, "tty struct"); 812 ttyprt(&tty); 813 } 814 } 815 816 static const struct flagbit_desc ttystates[] = { 817 { TS_ISOPEN, 'O'}, 818 { TS_DIALOUT, '>'}, 819 { TS_CARR_ON, 'C'}, 820 { TS_TIMEOUT, 'T'}, 821 { TS_FLUSH, 'F'}, 822 { TS_BUSY, 'B'}, 823 { TS_XCLUDE, 'X'}, 824 { TS_TTSTOP, 'S'}, 825 { TS_TBLOCK, 'K'}, 826 { TS_ASYNC, 'Y'}, 827 { TS_BKSL, 'D'}, 828 { TS_ERASE, 'E'}, 829 { TS_LNCH, 'L'}, 830 { TS_TYPEN, 'P'}, 831 { TS_CNTTB, 'N'}, 832 { 0, '\0'}, 833 }; 834 835 void 836 ttyprt(struct tty *tp) 837 { 838 char state[sizeof(ttystates) / sizeof(ttystates[0]) + 1]; 839 char dev[2 + 3 + 1 + 5 + 1]; /* 12bit major + 20bit minor */ 840 struct linesw t_linesw; 841 const char *name; 842 char buffer; 843 pid_t pgid; 844 int n, ovflw; 845 846 if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL) { 847 devprintf(dev, sizeof(dev), tp->t_dev); 848 name = dev; 849 } 850 ovflw = 0; 851 PRWORD(ovflw, "%-*s", 7, 0, name); 852 PRWORD(ovflw, " %*d", 3, 1, tp->t_rawq.c_cc); 853 PRWORD(ovflw, " %*d", 4, 1, tp->t_canq.c_cc); 854 PRWORD(ovflw, " %*d", 4, 1, tp->t_outq.c_cc); 855 PRWORD(ovflw, " %*d", 5, 1, tp->t_hiwat); 856 PRWORD(ovflw, " %*d", 4, 1, tp->t_lowat); 857 PRWORD(ovflw, " %*d", 8, 1, tp->t_column); 858 n = getflags(ttystates, state, tp->t_state); 859 if (tp->t_wopen) { 860 state[n++] = 'W'; 861 state[n] = '\0'; 862 } 863 PRWORD(ovflw, " %-*s", 7, 1, state); 864 PRWORD(ovflw, " %*lX", PTRSTRWIDTH + 1, 1, (u_long)tp->t_session); 865 pgid = 0; 866 if (tp->t_pgrp != NULL) 867 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid"); 868 PRWORD(ovflw, " %*d", 6, 1, pgid); 869 KGET2(tp->t_linesw, &t_linesw, sizeof(t_linesw), 870 "line discipline switch table"); 871 name = t_linesw.l_name; 872 (void)putchar(' '); 873 for (;;) { 874 KGET2(name, &buffer, sizeof(buffer), "line discipline name"); 875 if (buffer == '\0') 876 break; 877 (void)putchar(buffer); 878 name++; 879 } 880 (void)putchar('\n'); 881 } 882 883 static const struct flagbit_desc filemode_flags[] = { 884 { FREAD, 'R' }, 885 { FWRITE, 'W' }, 886 { FAPPEND, 'A' }, 887 #ifdef FSHLOCK /* currently gone */ 888 { FSHLOCK, 'S' }, 889 { FEXLOCK, 'X' }, 890 #endif 891 { FASYNC, 'I' }, 892 { 0, '\0' }, 893 }; 894 895 void 896 filemode(void) 897 { 898 struct kinfo_file *ki; 899 char flags[sizeof(filemode_flags) / sizeof(filemode_flags[0])]; 900 char *buf, *offset; 901 int len, maxfile, nfile, ovflw; 902 903 KGET(FNL_MAXFILE, maxfile); 904 if (totalflag) { 905 KGET(FNL_NFILE, nfile); 906 (void)printf("%3d/%3d files\n", nfile, maxfile); 907 return; 908 } 909 if (getfiles(&buf, &len, &offset) == -1) 910 return; 911 /* 912 * Getfiles returns in malloc'd memory to an array of kinfo_file2 913 * structures. 914 */ 915 nfile = len / sizeof(struct kinfo_file); 916 917 (void)printf("%d/%d open files\n", nfile, maxfile); 918 (void)printf("%*s%s%*s TYPE FLG CNT MSG %*s%s%*s IFLG OFFSET\n", 919 (PTRSTRWIDTH - 4) / 2, "", " LOC", (PTRSTRWIDTH - 4) / 2, "", 920 (PTRSTRWIDTH - 4) / 2, "", "DATA", (PTRSTRWIDTH - 4) / 2, ""); 921 for (ki = (struct kinfo_file *)offset; nfile--; ki++) { 922 if ((unsigned)ki->ki_ftype >= sizeof(dtypes) / sizeof(dtypes[0])) 923 continue; 924 ovflw = 0; 925 (void)getflags(filemode_flags, flags, ki->ki_flag); 926 PRWORD(ovflw, "%*lx", PTRSTRWIDTH, 0, (long)ki->ki_fileaddr); 927 PRWORD(ovflw, " %-*s", 9, 1, dtypes[ki->ki_ftype]); 928 PRWORD(ovflw, " %*s", 6, 1, flags); 929 PRWORD(ovflw, " %*d", 5, 1, ki->ki_count); 930 PRWORD(ovflw, " %*d", 5, 1, ki->ki_msgcount); 931 PRWORD(ovflw, " %*lx", PTRSTRWIDTH + 1, 2, (long)ki->ki_fdata); 932 PRWORD(ovflw, " %*x", 5, 1, 0); 933 if ((off_t)ki->ki_foffset < 0) 934 PRWORD(ovflw, " %-*lld\n", PTRSTRWIDTH + 1, 2, 935 (long long)ki->ki_foffset); 936 else 937 PRWORD(ovflw, " %-*lld\n", PTRSTRWIDTH + 1, 2, 938 (long long)ki->ki_foffset); 939 } 940 free(buf); 941 } 942 943 int 944 getfiles(char **abuf, int *alen, char **aoffset) 945 { 946 size_t len; 947 int mib[6]; 948 char *buf; 949 size_t offset; 950 951 /* 952 * XXX 953 * Add emulation of KINFO_FILE here. 954 */ 955 if (memf != NULL) 956 errx(1, "files on dead kernel, not implemented"); 957 958 mib[0] = CTL_KERN; 959 mib[1] = KERN_FILE2; 960 mib[2] = KERN_FILE_BYFILE; 961 mib[3] = 0; 962 mib[4] = sizeof(struct kinfo_file); 963 mib[5] = 0; 964 if (sysctl(mib, 6, NULL, &len, NULL, 0) == -1) { 965 warn("sysctl: KERN_FILE2"); 966 return (-1); 967 } 968 /* We need to align (struct kinfo_file *) in the buffer. */ 969 offset = len % sizeof(off_t); 970 mib[5] = len / sizeof(struct kinfo_file); 971 if ((buf = malloc(len + offset)) == NULL) 972 err(1, "malloc"); 973 if (sysctl(mib, 6, buf + offset, &len, NULL, 0) == -1) { 974 warn("sysctl: 2nd KERN_FILE2"); 975 free(buf); 976 return (-1); 977 } 978 *abuf = buf; 979 *alen = len; 980 *aoffset = (buf + offset); 981 return (0); 982 } 983 984 void 985 usage(void) 986 { 987 988 (void)fprintf(stderr, 989 "usage: %s [-T|-f|-s|-t|-v] [-ghkmn] [-M core] [-N system]\n", 990 getprogname()); 991 exit(1); 992 } 993