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