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