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