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