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