pstat.c revision 1.109
1/* $OpenBSD: pstat.c,v 1.109 2016/09/19 20:08:12 tb Exp $ */ 2/* $NetBSD: pstat.c,v 1.27 1996/10/23 22:50:06 cgd Exp $ */ 3 4/*- 5 * Copyright (c) 1980, 1991, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33#include <sys/param.h> /* MAXCOMLEN DEV_BSIZE */ 34#include <sys/types.h> 35#include <sys/proc.h> 36#include <sys/time.h> 37#include <sys/vnode.h> 38#include <sys/ucred.h> 39#include <sys/stat.h> 40#define _KERNEL 41#include <sys/file.h> 42#include <ufs/ufs/quota.h> 43#include <ufs/ufs/inode.h> 44#include <sys/mount.h> 45#undef _KERNEL 46#include <nfs/nfsproto.h> 47#include <nfs/rpcv2.h> 48#include <nfs/nfsnode.h> 49#include <sys/ioctl.h> 50#include <sys/tty.h> 51#include <sys/conf.h> 52#include <sys/device.h> 53#include <sys/swap.h> 54 55#include <sys/sysctl.h> 56 57#include <stdint.h> 58#include <endian.h> 59#include <err.h> 60#include <kvm.h> 61#include <limits.h> 62#include <nlist.h> 63#include <paths.h> 64#include <stdio.h> 65#include <stdlib.h> 66#include <string.h> 67#include <unistd.h> 68 69struct nlist vnodenl[] = { 70#define FNL_NFILE 0 /* sysctl */ 71 {"_nfiles"}, 72#define FNL_MAXFILE 1 /* sysctl */ 73 {"_maxfiles"}, 74#define TTY_NTTY 2 /* sysctl */ 75 {"_tty_count"}, 76#define V_NUMV 3 /* sysctl */ 77 { "_numvnodes" }, 78#define TTY_TTYLIST 4 /* sysctl */ 79 {"_ttylist"}, 80#define V_MOUNTLIST 5 /* no sysctl */ 81 { "_mountlist" }, 82 { NULL } 83}; 84 85struct nlist *globalnl; 86 87struct e_vnode { 88 struct vnode *vptr; 89 struct vnode vnode; 90}; 91 92int need_nlist; 93int usenumflag; 94int totalflag; 95int kflag; 96char *nlistf = NULL; 97char *memf = NULL; 98kvm_t *kd = NULL; 99 100#define SVAR(var) __STRING(var) /* to force expansion */ 101#define KGET(idx, var) \ 102 KGET1(idx, &var, sizeof(var), SVAR(var)) 103#define KGET1(idx, p, s, msg) \ 104 KGET2(globalnl[idx].n_value, p, s, msg) 105#define KGET2(addr, p, s, msg) \ 106 if (kvm_read(kd, (u_long)(addr), p, s) != s) \ 107 warnx("cannot read %s: %s", msg, kvm_geterr(kd)) 108#define KGETRET(addr, p, s, msg) \ 109 if (kvm_read(kd, (u_long)(addr), p, s) != s) { \ 110 warnx("cannot read %s: %s", msg, kvm_geterr(kd)); \ 111 return (0); \ 112 } 113 114void filemode(void); 115struct mount * 116 getmnt(struct mount *); 117struct e_vnode * 118 kinfo_vnodes(int *); 119void mount_print(struct mount *); 120void nfs_header(void); 121int nfs_print(struct vnode *); 122void swapmode(void); 123void ttymode(void); 124void ttyprt(struct itty *); 125void tty2itty(struct tty *tp, struct itty *itp); 126void ufs_header(void); 127int ufs_print(struct vnode *); 128void ext2fs_header(void); 129int ext2fs_print(struct vnode *); 130void usage(void); 131void vnode_header(void); 132void vnode_print(struct vnode *, struct vnode *); 133void vnodemode(void); 134 135int hideroot; 136 137int 138main(int argc, char *argv[]) 139{ 140 int fileflag = 0, swapflag = 0, ttyflag = 0, vnodeflag = 0, ch; 141 char buf[_POSIX2_LINE_MAX]; 142 const char *dformat = NULL; 143 extern char *optarg; 144 extern int optind; 145 int i; 146 147 hideroot = getuid(); 148 149 while ((ch = getopt(argc, argv, "d:TM:N:fiknstv")) != -1) 150 switch (ch) { 151 case 'd': 152 dformat = optarg; 153 break; 154 case 'f': 155 fileflag = 1; 156 break; 157 case 'M': 158 memf = optarg; 159 break; 160 case 'N': 161 nlistf = optarg; 162 break; 163 case 'n': 164 usenumflag = 1; 165 break; 166 case 's': 167 swapflag = 1; 168 break; 169 case 'T': 170 totalflag = 1; 171 break; 172 case 't': 173 ttyflag = 1; 174 break; 175 case 'k': 176 kflag = 1; 177 break; 178 case 'v': 179 case 'i': /* Backward compatibility. */ 180 vnodeflag = 1; 181 break; 182 default: 183 usage(); 184 } 185 argc -= optind; 186 argv += optind; 187 188 if (dformat && getuid()) 189 errx(1, "Only root can use -d"); 190 191 if ((dformat == NULL && argc > 0) || (dformat && argc == 0)) 192 usage(); 193 194 need_nlist = vnodeflag || dformat; 195 196 if (nlistf != NULL || memf != NULL) { 197 if (fileflag || totalflag) 198 need_nlist = 1; 199 } 200 201 if (vnodeflag || fileflag || dformat || need_nlist) 202 if ((kd = kvm_openfiles(nlistf, memf, NULL, 203 O_RDONLY | (need_nlist ? 0 : KVM_NO_FILES), buf)) == 0) 204 errx(1, "kvm_openfiles: %s", buf); 205 206 if (dformat) { 207 struct nlist *nl; 208 int longformat = 0, stringformat = 0, error = 0, n; 209 uint32_t mask = ~0; 210 char format[10], buf[1024]; 211 212 n = strlen(dformat); 213 if (n == 0) 214 errx(1, "illegal format"); 215 216 /* 217 * Support p, c, s, and {l, ll, h, hh, j, t, z, }[diouxX] 218 */ 219 if (strcmp(dformat, "p") == 0) 220 longformat = sizeof(long) == 8; 221 else if (strcmp(dformat, "c") == 0) 222 mask = 0xff; 223 else if (strcmp(dformat, "s") == 0) 224 stringformat = 1; 225 else if (strchr("diouxX", dformat[n - 1])) { 226 char *ptbl[]= {"l", "ll", "h", "hh", "j", "t", "z", ""}; 227 int i; 228 229 char *mod; 230 for (i = 0; i < sizeof(ptbl)/sizeof(ptbl[0]); i++) { 231 mod = ptbl[i]; 232 if (strlen(mod) == n - 1 && 233 strncmp(mod, dformat, strlen(mod)) == 0) 234 break; 235 } 236 if (i == sizeof(ptbl)/sizeof(ptbl[0]) 237 && dformat[1] != '\0') 238 errx(1, "illegal format"); 239 if (strcmp(mod, "l") == 0) 240 longformat = sizeof(long) == sizeof(long long); 241 else if (strcmp(mod, "h") == 0) 242 mask = 0xffff; 243 else if (strcmp(mod, "hh") == 0) 244 mask = 0xff; 245 else 246 longformat = 1; 247 248 } else 249 errx(1, "illegal format"); 250 251 if (*dformat == 's') { 252 stringformat = 1; 253 snprintf(format, sizeof(format), "%%.%zus", 254 sizeof buf); 255 } else 256 snprintf(format, sizeof(format), "%%%s", dformat); 257 258 nl = calloc(argc + 1, sizeof *nl); 259 if (!nl) 260 err(1, "calloc nl: "); 261 for (i = 0; i < argc; i++) { 262 if (asprintf(&nl[i].n_name, "_%s", 263 argv[i]) == -1) 264 warn("asprintf"); 265 } 266 kvm_nlist(kd, nl); 267 globalnl = nl; 268 for (i = 0; i < argc; i++) { 269 uint64_t v; 270 271 printf("%s ", argv[i]); 272 if (!nl[i].n_value && argv[i][0] == '0') { 273 nl[i].n_value = strtoul(argv[i], NULL, 16); 274 nl[i].n_type = N_DATA; 275 } 276 if (!nl[i].n_value) { 277 printf("not found\n"); 278 error++; 279 continue; 280 } 281 282 printf("at %p: ", (void *)nl[i].n_value); 283 if (nl[i].n_type == N_DATA) { 284 if (stringformat) { 285 KGET1(i, &buf, sizeof(buf), argv[i]); 286 buf[sizeof(buf) - 1] = '\0'; 287 } else 288 KGET1(i, &v, sizeof(v), argv[i]); 289 if (stringformat) 290 printf(format, &buf); 291 else if (longformat) 292 printf(format, v); 293 else { 294#if BYTE_ORDER == BIG_ENDIAN 295 switch (mask) { 296 case 0xff: 297 v >>= 8; 298 /* FALLTHROUGH */ 299 case 0xffff: 300 v >>= 16; 301 /* FALLTHROUGH */ 302 case 0xffffffff: 303 v >>= 32; 304 break; 305 } 306#endif 307 printf(format, ((uint32_t)v) & mask); 308 } 309 } 310 printf("\n"); 311 } 312 for (i = 0; i < argc; i++) 313 free(nl[i].n_name); 314 free(nl); 315 exit(error); 316 } 317 318 if (need_nlist) 319 if (kvm_nlist(kd, vnodenl) == -1) 320 errx(1, "kvm_nlist: %s", kvm_geterr(kd)); 321 322 if (!(fileflag | vnodeflag | ttyflag | swapflag | totalflag || dformat)) 323 usage(); 324 if (fileflag || totalflag) 325 filemode(); 326 if (vnodeflag || totalflag) 327 vnodemode(); 328 if (ttyflag) 329 ttymode(); 330 if (swapflag || totalflag) 331 swapmode(); 332 exit(0); 333} 334 335void 336vnodemode(void) 337{ 338 struct e_vnode *e_vnodebase, *endvnode, *evp; 339 struct vnode *vp; 340 struct mount *maddr, *mp = NULL; 341 int numvnodes; 342 343 globalnl = vnodenl; 344 345 e_vnodebase = kinfo_vnodes(&numvnodes); 346 if (totalflag) { 347 (void)printf("%7d vnodes\n", numvnodes); 348 return; 349 } 350 if (!e_vnodebase) 351 return; 352 endvnode = e_vnodebase + numvnodes; 353 (void)printf("%d active vnodes\n", numvnodes); 354 355 maddr = NULL; 356 for (evp = e_vnodebase; evp < endvnode; evp++) { 357 vp = &evp->vnode; 358 if (vp->v_mount != maddr) { 359 /* 360 * New filesystem 361 */ 362 if ((mp = getmnt(vp->v_mount)) == NULL) 363 continue; 364 maddr = vp->v_mount; 365 mount_print(mp); 366 vnode_header(); 367 if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_FFS, MFSNAMELEN) || 368 !strncmp(mp->mnt_stat.f_fstypename, MOUNT_MFS, MFSNAMELEN)) { 369 ufs_header(); 370 } else if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_NFS, 371 MFSNAMELEN)) { 372 nfs_header(); 373 } else if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_EXT2FS, 374 MFSNAMELEN)) { 375 ext2fs_header(); 376 } 377 (void)printf("\n"); 378 } 379 vnode_print(evp->vptr, vp); 380 381 /* Syncer vnodes have no associated fs-specific data */ 382 if (vp->v_data == NULL) { 383 printf(" %6c %5c %7c\n", '-', '-', '-'); 384 continue; 385 } 386 387 if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_FFS, MFSNAMELEN) || 388 !strncmp(mp->mnt_stat.f_fstypename, MOUNT_MFS, MFSNAMELEN)) { 389 ufs_print(vp); 390 } else if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_NFS, MFSNAMELEN)) { 391 nfs_print(vp); 392 } else if (!strncmp(mp->mnt_stat.f_fstypename, MOUNT_EXT2FS, 393 MFSNAMELEN)) { 394 ext2fs_print(vp); 395 } 396 (void)printf("\n"); 397 } 398 free(e_vnodebase); 399} 400 401void 402vnode_header(void) 403{ 404 (void)printf("%*s TYP VFLAG USE HOLD", 2 * (int)sizeof(long), "ADDR"); 405} 406 407void 408vnode_print(struct vnode *avnode, struct vnode *vp) 409{ 410 char *type, flags[16]; 411 char *fp; 412 int flag; 413 414 /* 415 * set type 416 */ 417 switch (vp->v_type) { 418 case VNON: 419 type = "non"; break; 420 case VREG: 421 type = "reg"; break; 422 case VDIR: 423 type = "dir"; break; 424 case VBLK: 425 type = "blk"; break; 426 case VCHR: 427 type = "chr"; break; 428 case VLNK: 429 type = "lnk"; break; 430 case VSOCK: 431 type = "soc"; break; 432 case VFIFO: 433 type = "fif"; break; 434 case VBAD: 435 type = "bad"; break; 436 default: 437 type = "unk"; break; 438 } 439 /* 440 * gather flags 441 */ 442 fp = flags; 443 flag = vp->v_flag; 444 if (flag & VROOT) 445 *fp++ = 'R'; 446 if (flag & VTEXT) 447 *fp++ = 'T'; 448 if (flag & VSYSTEM) 449 *fp++ = 'S'; 450 if (flag & VISTTY) 451 *fp++ = 'I'; 452 if (flag & VXLOCK) 453 *fp++ = 'L'; 454 if (flag & VXWANT) 455 *fp++ = 'W'; 456 if (vp->v_bioflag & VBIOWAIT) 457 *fp++ = 'B'; 458 if (flag & VALIASED) 459 *fp++ = 'A'; 460 if (vp->v_bioflag & VBIOONFREELIST) 461 *fp++ = 'F'; 462 if (flag & VLOCKSWORK) 463 *fp++ = 'l'; 464 if (vp->v_bioflag & VBIOONSYNCLIST) 465 *fp++ = 's'; 466 if (fp == flags) 467 *fp++ = '-'; 468 *fp = '\0'; 469 (void)printf("%0*lx %s %5s %4d %4u", 470 2 * (int)sizeof(long), hideroot ? 0L : (long)avnode, 471 type, flags, vp->v_usecount, vp->v_holdcnt); 472} 473 474void 475ufs_header(void) 476{ 477 (void)printf(" FILEID IFLAG RDEV|SZ"); 478} 479 480int 481ufs_print(struct vnode *vp) 482{ 483 int flag; 484 struct inode inode, *ip = &inode; 485 struct ufs1_dinode di1; 486 char flagbuf[16], *flags = flagbuf; 487 char *name; 488 mode_t type; 489 490 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 491 KGETRET(inode.i_din1, &di1, sizeof(struct ufs1_dinode), 492 "vnode's dinode"); 493 494 inode.i_din1 = &di1; 495 flag = ip->i_flag; 496#if 0 497 if (flag & IN_LOCKED) 498 *flags++ = 'L'; 499 if (flag & IN_WANTED) 500 *flags++ = 'W'; 501 if (flag & IN_LWAIT) 502 *flags++ = 'Z'; 503#endif 504 if (flag & IN_ACCESS) 505 *flags++ = 'A'; 506 if (flag & IN_CHANGE) 507 *flags++ = 'C'; 508 if (flag & IN_UPDATE) 509 *flags++ = 'U'; 510 if (flag & IN_MODIFIED) 511 *flags++ = 'M'; 512 if (flag & IN_LAZYMOD) 513 *flags++ = 'm'; 514 if (flag & IN_RENAME) 515 *flags++ = 'R'; 516 if (flag & IN_SHLOCK) 517 *flags++ = 'S'; 518 if (flag & IN_EXLOCK) 519 *flags++ = 'E'; 520 if (flag == 0) 521 *flags++ = '-'; 522 *flags = '\0'; 523 524 (void)printf(" %6d %5s", ip->i_number, flagbuf); 525 type = ip->i_ffs1_mode & S_IFMT; 526 if (S_ISCHR(ip->i_ffs1_mode) || S_ISBLK(ip->i_ffs1_mode)) 527 if (usenumflag || 528 ((name = devname(ip->i_ffs1_rdev, type)) == NULL)) 529 (void)printf(" %2d,%-2d", 530 major(ip->i_ffs1_rdev), minor(ip->i_ffs1_rdev)); 531 else 532 (void)printf(" %7s", name); 533 else 534 (void)printf(" %7lld", (long long)ip->i_ffs1_size); 535 return (0); 536} 537 538void 539ext2fs_header(void) 540{ 541 (void)printf(" FILEID IFLAG SZ"); 542} 543 544int 545ext2fs_print(struct vnode *vp) 546{ 547 int flag; 548 struct inode inode, *ip = &inode; 549 struct ext2fs_dinode di; 550 char flagbuf[16], *flags = flagbuf; 551 552 KGETRET(VTOI(vp), &inode, sizeof(struct inode), "vnode's inode"); 553 KGETRET(inode.i_e2din, &di, sizeof(struct ext2fs_dinode), 554 "vnode's dinode"); 555 556 inode.i_e2din = &di; 557 flag = ip->i_flag; 558 559#if 0 560 if (flag & IN_LOCKED) 561 *flags++ = 'L'; 562 if (flag & IN_WANTED) 563 *flags++ = 'W'; 564 if (flag & IN_LWAIT) 565 *flags++ = 'Z'; 566#endif 567 if (flag & IN_ACCESS) 568 *flags++ = 'A'; 569 if (flag & IN_CHANGE) 570 *flags++ = 'C'; 571 if (flag & IN_UPDATE) 572 *flags++ = 'U'; 573 if (flag & IN_MODIFIED) 574 *flags++ = 'M'; 575 if (flag & IN_RENAME) 576 *flags++ = 'R'; 577 if (flag & IN_SHLOCK) 578 *flags++ = 'S'; 579 if (flag & IN_EXLOCK) 580 *flags++ = 'E'; 581 if (flag == 0) 582 *flags++ = '-'; 583 *flags = '\0'; 584 585 (void)printf(" %6d %5s %2d", ip->i_number, flagbuf, ip->i_e2fs_size); 586 return (0); 587} 588 589void 590nfs_header(void) 591{ 592 (void)printf(" FILEID NFLAG RDEV|SZ"); 593} 594 595int 596nfs_print(struct vnode *vp) 597{ 598 struct nfsnode nfsnode, *np = &nfsnode; 599 char flagbuf[16], *flags = flagbuf; 600 int flag; 601 char *name; 602 mode_t type; 603 604 KGETRET(VTONFS(vp), &nfsnode, sizeof(nfsnode), "vnode's nfsnode"); 605 flag = np->n_flag; 606 if (flag & NFLUSHWANT) 607 *flags++ = 'W'; 608 if (flag & NFLUSHINPROG) 609 *flags++ = 'P'; 610 if (flag & NMODIFIED) 611 *flags++ = 'M'; 612 if (flag & NWRITEERR) 613 *flags++ = 'E'; 614 if (flag & NACC) 615 *flags++ = 'A'; 616 if (flag & NUPD) 617 *flags++ = 'U'; 618 if (flag & NCHG) 619 *flags++ = 'C'; 620 if (flag == 0) 621 *flags++ = '-'; 622 *flags = '\0'; 623 624 (void)printf(" %6lld %5s", (long long)np->n_vattr.va_fileid, flagbuf); 625 type = np->n_vattr.va_mode & S_IFMT; 626 if (S_ISCHR(np->n_vattr.va_mode) || S_ISBLK(np->n_vattr.va_mode)) 627 if (usenumflag || 628 ((name = devname(np->n_vattr.va_rdev, type)) == NULL)) 629 (void)printf(" %2d,%-2d", major(np->n_vattr.va_rdev), 630 minor(np->n_vattr.va_rdev)); 631 else 632 (void)printf(" %7s", name); 633 else 634 (void)printf(" %7lld", (long long)np->n_size); 635 return (0); 636} 637 638/* 639 * Given a pointer to a mount structure in kernel space, 640 * read it in and return a usable pointer to it. 641 */ 642struct mount * 643getmnt(struct mount *maddr) 644{ 645 static struct mtab { 646 struct mtab *next; 647 struct mount *maddr; 648 struct mount mount; 649 } *mhead = NULL; 650 struct mtab *mt; 651 652 for (mt = mhead; mt != NULL; mt = mt->next) 653 if (maddr == mt->maddr) 654 return (&mt->mount); 655 if ((mt = malloc(sizeof(struct mtab))) == NULL) 656 err(1, "malloc: mount table"); 657 KGETRET(maddr, &mt->mount, sizeof(struct mount), "mount table"); 658 mt->maddr = maddr; 659 mt->next = mhead; 660 mhead = mt; 661 return (&mt->mount); 662} 663 664void 665mount_print(struct mount *mp) 666{ 667 int flags; 668 669 (void)printf("*** MOUNT "); 670 (void)printf("%.*s %s on %s", MFSNAMELEN, 671 mp->mnt_stat.f_fstypename, mp->mnt_stat.f_mntfromname, 672 mp->mnt_stat.f_mntonname); 673 if ((flags = mp->mnt_flag)) { 674 char *comma = "("; 675 676 putchar(' '); 677 /* user visible flags */ 678 if (flags & MNT_RDONLY) { 679 (void)printf("%srdonly", comma); 680 flags &= ~MNT_RDONLY; 681 comma = ","; 682 } 683 if (flags & MNT_SYNCHRONOUS) { 684 (void)printf("%ssynchronous", comma); 685 flags &= ~MNT_SYNCHRONOUS; 686 comma = ","; 687 } 688 if (flags & MNT_NOEXEC) { 689 (void)printf("%snoexec", comma); 690 flags &= ~MNT_NOEXEC; 691 comma = ","; 692 } 693 if (flags & MNT_NOSUID) { 694 (void)printf("%snosuid", comma); 695 flags &= ~MNT_NOSUID; 696 comma = ","; 697 } 698 if (flags & MNT_NODEV) { 699 (void)printf("%snodev", comma); 700 flags &= ~MNT_NODEV; 701 comma = ","; 702 } 703 if (flags & MNT_ASYNC) { 704 (void)printf("%sasync", comma); 705 flags &= ~MNT_ASYNC; 706 comma = ","; 707 } 708 if (flags & MNT_EXRDONLY) { 709 (void)printf("%sexrdonly", comma); 710 flags &= ~MNT_EXRDONLY; 711 comma = ","; 712 } 713 if (flags & MNT_EXPORTED) { 714 (void)printf("%sexport", comma); 715 flags &= ~MNT_EXPORTED; 716 comma = ","; 717 } 718 if (flags & MNT_DEFEXPORTED) { 719 (void)printf("%sdefdexported", comma); 720 flags &= ~MNT_DEFEXPORTED; 721 comma = ","; 722 } 723 if (flags & MNT_EXPORTANON) { 724 (void)printf("%sexportanon", comma); 725 flags &= ~MNT_EXPORTANON; 726 comma = ","; 727 } 728 if (flags & MNT_WXALLOWED) { 729 (void)printf("%swxallowed", comma); 730 flags &= ~MNT_WXALLOWED; 731 comma = ","; 732 } 733 if (flags & MNT_LOCAL) { 734 (void)printf("%slocal", comma); 735 flags &= ~MNT_LOCAL; 736 comma = ","; 737 } 738 if (flags & MNT_QUOTA) { 739 (void)printf("%squota", comma); 740 flags &= ~MNT_QUOTA; 741 comma = ","; 742 } 743 if (flags & MNT_ROOTFS) { 744 (void)printf("%srootfs", comma); 745 flags &= ~MNT_ROOTFS; 746 comma = ","; 747 } 748 if (flags & MNT_NOATIME) { 749 (void)printf("%snoatime", comma); 750 flags &= ~MNT_NOATIME; 751 comma = ","; 752 } 753 /* filesystem control flags */ 754 if (flags & MNT_UPDATE) { 755 (void)printf("%supdate", comma); 756 flags &= ~MNT_UPDATE; 757 comma = ","; 758 } 759 if (flags & MNT_DELEXPORT) { 760 (void)printf("%sdelexport", comma); 761 flags &= ~MNT_DELEXPORT; 762 comma = ","; 763 } 764 if (flags & MNT_RELOAD) { 765 (void)printf("%sreload", comma); 766 flags &= ~MNT_RELOAD; 767 comma = ","; 768 } 769 if (flags & MNT_FORCE) { 770 (void)printf("%sforce", comma); 771 flags &= ~MNT_FORCE; 772 comma = ","; 773 } 774 if (flags & MNT_WANTRDWR) { 775 (void)printf("%swantrdwr", comma); 776 flags &= ~MNT_WANTRDWR; 777 comma = ","; 778 } 779 if (flags & MNT_SOFTDEP) { 780 (void)printf("%ssoftdep", comma); 781 flags &= ~MNT_SOFTDEP; 782 comma = ","; 783 } 784 if (flags) 785 (void)printf("%sunknown_flags:%x", comma, flags); 786 (void)printf(")"); 787 } 788 (void)printf("\n"); 789} 790 791/* 792 * simulate what a running kernel does in kinfo_vnode 793 */ 794struct e_vnode * 795kinfo_vnodes(int *avnodes) 796{ 797 struct mntlist kvm_mountlist; 798 struct mount *mp, mount; 799 struct vnode *vp, vnode; 800 char *vbuf, *evbuf, *bp; 801 int mib[2], numvnodes; 802 size_t num; 803 804 if (kd == 0) { 805 mib[0] = CTL_KERN; 806 mib[1] = KERN_NUMVNODES; 807 num = sizeof(numvnodes); 808 if (sysctl(mib, 2, &numvnodes, &num, NULL, 0) < 0) 809 err(1, "sysctl(KERN_NUMVNODES) failed"); 810 } else 811 KGET(V_NUMV, numvnodes); 812 *avnodes = numvnodes; 813 if (totalflag) 814 return NULL; 815 if ((vbuf = calloc(numvnodes + 20, 816 sizeof(struct vnode *) + sizeof(struct vnode))) == NULL) 817 err(1, "malloc: vnode buffer"); 818 bp = vbuf; 819 evbuf = vbuf + (numvnodes + 20) * 820 (sizeof(struct vnode *) + sizeof(struct vnode)); 821 KGET(V_MOUNTLIST, kvm_mountlist); 822 num = 0; 823 for (mp = TAILQ_FIRST(&kvm_mountlist); mp != NULL; 824 mp = TAILQ_NEXT(&mount, mnt_list)) { 825 KGETRET(mp, &mount, sizeof(mount), "mount entry"); 826 for (vp = LIST_FIRST(&mount.mnt_vnodelist); 827 vp != NULL; vp = LIST_NEXT(&vnode, v_mntvnodes)) { 828 KGETRET(vp, &vnode, sizeof(vnode), "vnode"); 829 if ((bp + sizeof(struct vnode *) + 830 sizeof(struct vnode)) > evbuf) 831 /* XXX - should realloc */ 832 errx(1, "no more room for vnodes"); 833 memmove(bp, &vp, sizeof(struct vnode *)); 834 bp += sizeof(struct vnode *); 835 memmove(bp, &vnode, sizeof(struct vnode)); 836 bp += sizeof(struct vnode); 837 num++; 838 } 839 } 840 *avnodes = num; 841 return ((struct e_vnode *)vbuf); 842} 843 844const char hdr[] = 845" LINE RAW CAN OUT HWT LWT COL STATE SESS PGID DISC\n"; 846 847void 848tty2itty(struct tty *tp, struct itty *itp) 849{ 850 itp->t_dev = tp->t_dev; 851 itp->t_rawq_c_cc = tp->t_rawq.c_cc; 852 itp->t_canq_c_cc = tp->t_canq.c_cc; 853 itp->t_outq_c_cc = tp->t_outq.c_cc; 854 itp->t_hiwat = tp->t_hiwat; 855 itp->t_lowat = tp->t_lowat; 856 itp->t_column = tp->t_column; 857 itp->t_state = tp->t_state; 858 itp->t_session = tp->t_session; 859 if (tp->t_pgrp != NULL) 860 KGET2(&tp->t_pgrp->pg_id, &itp->t_pgrp_pg_id, sizeof(pid_t), "pgid"); 861 itp->t_line = tp->t_line; 862} 863 864void 865ttymode(void) 866{ 867 struct ttylist_head tty_head; 868 struct tty *tp, tty; 869 int mib[3], ntty, i; 870 struct itty itty, *itp; 871 size_t nlen; 872 873 if (!need_nlist) { 874 mib[0] = CTL_KERN; 875 mib[1] = KERN_TTYCOUNT; 876 nlen = sizeof(ntty); 877 if (sysctl(mib, 2, &ntty, &nlen, NULL, 0) < 0) 878 err(1, "sysctl(KERN_TTYCOUNT) failed"); 879 } else 880 KGET(TTY_NTTY, ntty); 881 (void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s"); 882 (void)printf("%s", hdr); 883 if (!need_nlist) { 884 mib[0] = CTL_KERN; 885 mib[1] = KERN_TTY; 886 mib[2] = KERN_TTY_INFO; 887 if ((itp = reallocarray(NULL, ntty, sizeof(struct itty))) == NULL) 888 err(1, "malloc"); 889 nlen = ntty * sizeof(struct itty); 890 if (sysctl(mib, 3, itp, &nlen, NULL, 0) < 0) 891 err(1, "sysctl(KERN_TTY_INFO) failed"); 892 for (i = 0; i < ntty; i++) 893 ttyprt(&itp[i]); 894 free(itp); 895 } else { 896 KGET(TTY_TTYLIST, tty_head); 897 for (tp = TAILQ_FIRST(&tty_head); tp; 898 tp = TAILQ_NEXT(&tty, tty_link)) { 899 KGET2(tp, &tty, sizeof tty, "tty struct"); 900 tty2itty(&tty, &itty); 901 ttyprt(&itty); 902 } 903 } 904} 905 906struct { 907 int flag; 908 char val; 909} ttystates[] = { 910 { TS_WOPEN, 'W'}, 911 { TS_ISOPEN, 'O'}, 912 { TS_CARR_ON, 'C'}, 913 { TS_TIMEOUT, 'T'}, 914 { TS_FLUSH, 'F'}, 915 { TS_BUSY, 'B'}, 916 { TS_ASLEEP, 'A'}, 917 { TS_XCLUDE, 'X'}, 918 { TS_TTSTOP, 'S'}, 919 { TS_TBLOCK, 'K'}, 920 { TS_ASYNC, 'Y'}, 921 { TS_BKSL, 'D'}, 922 { TS_ERASE, 'E'}, 923 { TS_LNCH, 'L'}, 924 { TS_TYPEN, 'P'}, 925 { TS_CNTTB, 'N'}, 926 { 0, '\0'}, 927}; 928 929void 930ttyprt(struct itty *tp) 931{ 932 char *name, state[20]; 933 int i, j; 934 935 if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL) 936 (void)printf("%2d,%-3d ", major(tp->t_dev), minor(tp->t_dev)); 937 else 938 (void)printf("%7s ", name); 939 (void)printf("%3d %4d ", tp->t_rawq_c_cc, tp->t_canq_c_cc); 940 (void)printf("%4d %4d %3d %6d ", tp->t_outq_c_cc, 941 tp->t_hiwat, tp->t_lowat, tp->t_column); 942 for (i = j = 0; ttystates[i].flag; i++) 943 if (tp->t_state&ttystates[i].flag) 944 state[j++] = ttystates[i].val; 945 if (j == 0) 946 state[j++] = '-'; 947 state[j] = '\0'; 948 (void)printf("%-6s %8lx", state, 949 hideroot ? 0 : (u_long)tp->t_session & 0xffffffff); 950 (void)printf("%6d ", tp->t_pgrp_pg_id); 951 switch (tp->t_line) { 952 case TTYDISC: 953 (void)printf("term\n"); 954 break; 955 case TABLDISC: 956 (void)printf("tab\n"); 957 break; 958 case SLIPDISC: 959 (void)printf("slip\n"); 960 break; 961 case PPPDISC: 962 (void)printf("ppp\n"); 963 break; 964 case STRIPDISC: 965 (void)printf("strip\n"); 966 break; 967 case NMEADISC: 968 (void)printf("nmea\n"); 969 break; 970 default: 971 (void)printf("%d\n", tp->t_line); 972 break; 973 } 974} 975 976void 977filemode(void) 978{ 979 struct kinfo_file *kf; 980 char flagbuf[16], *fbp; 981 static char *dtypes[] = { "???", "inode", "socket", "pipe", "kqueue", "???", "???" }; 982 int mib[2], maxfile, nfile; 983 size_t len; 984 985 globalnl = vnodenl; 986 987 if (nlistf == NULL && memf == NULL) { 988 mib[0] = CTL_KERN; 989 mib[1] = KERN_MAXFILES; 990 len = sizeof(maxfile); 991 if (sysctl(mib, 2, &maxfile, &len, NULL, 0) < 0) 992 err(1, "sysctl(KERN_MAXFILES) failed"); 993 if (totalflag) { 994 mib[0] = CTL_KERN; 995 mib[1] = KERN_NFILES; 996 len = sizeof(nfile); 997 if (sysctl(mib, 2, &nfile, &len, NULL, 0) < 0) 998 err(1, "sysctl(KERN_NFILES) failed"); 999 } 1000 } else { 1001 KGET(FNL_MAXFILE, maxfile); 1002 if (totalflag) { 1003 KGET(FNL_NFILE, nfile); 1004 (void)printf("%3d/%3d files\n", nfile, maxfile); 1005 return; 1006 } 1007 } 1008 1009 if (!totalflag) { 1010 kf = kvm_getfiles(kd, KERN_FILE_BYFILE, 0, sizeof *kf, &nfile); 1011 if (kf == NULL) { 1012 warnx("kvm_getfiles: %s", kvm_geterr(kd)); 1013 return; 1014 } 1015 } 1016 1017 (void)printf("%d/%d open files\n", nfile, maxfile); 1018 if (totalflag) 1019 return; 1020 1021 (void)printf("%*s TYPE FLG CNT MSG %*s OFFSET\n", 1022 2 * (int)sizeof(long), "LOC", 2 * (int)sizeof(long), "DATA"); 1023 for (; nfile-- > 0; kf++) { 1024 (void)printf("%0*llx ", 2 * (int)sizeof(long), 1025 hideroot ? 0LL : kf->f_fileaddr); 1026 (void)printf("%-8.8s", dtypes[ 1027 (kf->f_type >= (sizeof(dtypes)/sizeof(dtypes[0]))) 1028 ? 0 : kf->f_type]); 1029 fbp = flagbuf; 1030 if (kf->f_flag & FREAD) 1031 *fbp++ = 'R'; 1032 if (kf->f_flag & FWRITE) 1033 *fbp++ = 'W'; 1034 if (kf->f_flag & FAPPEND) 1035 *fbp++ = 'A'; 1036 if (kf->f_flag & FASYNC) 1037 *fbp++ = 'I'; 1038 1039 if (kf->f_iflags & FIF_HASLOCK) 1040 *fbp++ = 'L'; 1041 if (kf->f_iflags & FIF_LARVAL) 1042 *fbp++ = 'l'; 1043 1044 *fbp = '\0'; 1045 (void)printf("%6s %3ld", flagbuf, (long)kf->f_count); 1046 (void)printf(" %3ld", (long)kf->f_msgcount); 1047 (void)printf(" %0*lx", 2 * (int)sizeof(long), 1048 hideroot ? 0L : (long)kf->f_data); 1049 1050 if (kf->f_offset == (uint64_t)-1) 1051 (void)printf(" *\n"); 1052 else if (kf->f_offset > INT64_MAX) { 1053 /* would have been negative */ 1054 (void)printf(" %llx\n", 1055 hideroot ? 0LL : (long long)kf->f_offset); 1056 } else 1057 (void)printf(" %lld\n", 1058 hideroot ? 0LL : (long long)kf->f_offset); 1059 } 1060} 1061 1062/* 1063 * swapmode is based on a program called swapinfo written 1064 * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 1065 */ 1066void 1067swapmode(void) 1068{ 1069 char *header; 1070 int hlen = 10, nswap; 1071 int bdiv, i, avail, nfree, npfree, used; 1072 long blocksize; 1073 struct swapent *swdev; 1074 1075 if (kflag) { 1076 header = "1K-blocks"; 1077 blocksize = 1024; 1078 hlen = strlen(header); 1079 } else 1080 header = getbsize(&hlen, &blocksize); 1081 1082 nswap = swapctl(SWAP_NSWAP, 0, 0); 1083 if (nswap == 0) { 1084 if (!totalflag) 1085 (void)printf("%-11s %*s %8s %8s %8s %s\n", 1086 "Device", hlen, header, 1087 "Used", "Avail", "Capacity", "Priority"); 1088 (void)printf("%-11s %*d %8d %8d %5.0f%%\n", 1089 "Total", hlen, 0, 0, 0, 0.0); 1090 return; 1091 } 1092 if ((swdev = calloc(nswap, sizeof(*swdev))) == NULL) 1093 err(1, "malloc"); 1094 if (swapctl(SWAP_STATS, swdev, nswap) == -1) 1095 err(1, "swapctl"); 1096 1097 if (!totalflag) 1098 (void)printf("%-11s %*s %8s %8s %8s %s\n", 1099 "Device", hlen, header, 1100 "Used", "Avail", "Capacity", "Priority"); 1101 1102 /* Run through swap list, doing the funky monkey. */ 1103 bdiv = blocksize / DEV_BSIZE; 1104 avail = nfree = npfree = 0; 1105 for (i = 0; i < nswap; i++) { 1106 int xsize, xfree; 1107 1108 if (!(swdev[i].se_flags & SWF_ENABLE)) 1109 continue; 1110 1111 if (!totalflag) { 1112 if (usenumflag) 1113 (void)printf("%2d,%-2d %*d ", 1114 major(swdev[i].se_dev), 1115 minor(swdev[i].se_dev), 1116 hlen, swdev[i].se_nblks / bdiv); 1117 else 1118 (void)printf("%-11s %*d ", swdev[i].se_path, 1119 hlen, swdev[i].se_nblks / bdiv); 1120 } 1121 1122 xsize = swdev[i].se_nblks; 1123 used = swdev[i].se_inuse; 1124 xfree = xsize - used; 1125 nfree += (xsize - used); 1126 npfree++; 1127 avail += xsize; 1128 if (totalflag) 1129 continue; 1130 (void)printf("%8d %8d %5.0f%% %d\n", 1131 used / bdiv, xfree / bdiv, 1132 (double)used / (double)xsize * 100.0, 1133 swdev[i].se_priority); 1134 } 1135 free(swdev); 1136 1137 /* 1138 * If only one partition has been set up via swapon(8), we don't 1139 * need to bother with totals. 1140 */ 1141 used = avail - nfree; 1142 if (totalflag) { 1143 (void)printf("%dM/%dM swap space\n", 1144 used / (1048576 / DEV_BSIZE), 1145 avail / (1048576 / DEV_BSIZE)); 1146 return; 1147 } 1148 if (npfree > 1) { 1149 (void)printf("%-11s %*d %8d %8d %5.0f%%\n", 1150 "Total", hlen, avail / bdiv, used / bdiv, nfree / bdiv, 1151 (double)used / (double)avail * 100.0); 1152 } 1153} 1154 1155void 1156usage(void) 1157{ 1158 (void)fprintf(stderr, "usage: " 1159 "pstat [-fknsTtv] [-M core] [-N system] [-d format symbol ...]\n"); 1160 exit(1); 1161} 1162