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