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