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