pstat.c revision 1.19
1/* $OpenBSD: pstat.c,v 1.19 1999/05/22 21:43:52 weingart 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.19 1999/05/22 21:43:52 weingart 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) 659 (void)printf("%sunknown_flags:%x", comma, flags); 660 (void)printf(")"); 661 } 662 (void)printf("\n"); 663#undef ST 664} 665 666struct e_vnode * 667loadvnodes(avnodes) 668 int *avnodes; 669{ 670 int mib[2]; 671 size_t copysize; 672 struct e_vnode *vnodebase; 673 674 if (memf != NULL) { 675 /* 676 * do it by hand 677 */ 678 return (kinfo_vnodes(avnodes)); 679 } 680 mib[0] = CTL_KERN; 681 mib[1] = KERN_VNODE; 682 if (sysctl(mib, 2, NULL, ©size, NULL, 0) == -1) 683 err(1, "sysctl: KERN_VNODE"); 684 if ((vnodebase = malloc(copysize)) == NULL) 685 err(1, "malloc: vnode table"); 686 if (sysctl(mib, 2, vnodebase, ©size, NULL, 0) == -1) 687 err(1, "sysctl: KERN_VNODE"); 688 if (copysize % sizeof(struct e_vnode)) 689 errx(1, "vnode size mismatch"); 690 *avnodes = copysize / sizeof(struct e_vnode); 691 692 return (vnodebase); 693} 694 695/* 696 * simulate what a running kernel does in in kinfo_vnode 697 */ 698struct e_vnode * 699kinfo_vnodes(avnodes) 700 int *avnodes; 701{ 702 struct mntlist mountlist; 703 struct mount *mp, mount; 704 struct vnode *vp, vnode; 705 char *vbuf, *evbuf, *bp; 706 int num, numvnodes; 707 708#define VPTRSZ sizeof(struct vnode *) 709#define VNODESZ sizeof(struct vnode) 710 711 KGET(V_NUMV, numvnodes); 712 if ((vbuf = malloc((numvnodes + 20) * (VPTRSZ + VNODESZ))) == NULL) 713 err(1, "malloc: vnode buffer"); 714 bp = vbuf; 715 evbuf = vbuf + (numvnodes + 20) * (VPTRSZ + VNODESZ); 716 KGET(V_MOUNTLIST, mountlist); 717 for (num = 0, mp = mountlist.cqh_first; ; mp = mount.mnt_list.cqe_next) { 718 KGET2(mp, &mount, sizeof(mount), "mount entry"); 719 for (vp = mount.mnt_vnodelist.lh_first; 720 vp != NULL; vp = vnode.v_mntvnodes.le_next) { 721 KGET2(vp, &vnode, sizeof(vnode), "vnode"); 722 if ((bp + VPTRSZ + VNODESZ) > evbuf) 723 /* XXX - should realloc */ 724 errx(1, "no more room for vnodes"); 725 memmove(bp, &vp, VPTRSZ); 726 bp += VPTRSZ; 727 memmove(bp, &vnode, VNODESZ); 728 bp += VNODESZ; 729 num++; 730 } 731 if (mp == mountlist.cqh_last) 732 break; 733 } 734 *avnodes = num; 735 return ((struct e_vnode *)vbuf); 736} 737 738char hdr[]=" LINE RAW CAN OUT HWT LWT COL STATE SESS PGID DISC\n"; 739 740void 741ttymode() 742{ 743 int ntty; 744 struct ttylist_head tty_head; 745 struct tty *tp, tty; 746 747 KGET(TTY_NTTY, ntty); 748 (void)printf("%d terminal device%s\n", ntty, ntty == 1 ? "" : "s"); 749 KGET(TTY_TTYLIST, tty_head); 750 (void)printf(hdr); 751 for (tp = tty_head.tqh_first; tp; tp = tty.tty_link.tqe_next) { 752 KGET2(tp, &tty, sizeof tty, "tty struct"); 753 ttyprt(&tty); 754 } 755} 756 757struct { 758 int flag; 759 char val; 760} ttystates[] = { 761 { TS_WOPEN, 'W'}, 762 { TS_ISOPEN, 'O'}, 763 { TS_CARR_ON, 'C'}, 764 { TS_TIMEOUT, 'T'}, 765 { TS_FLUSH, 'F'}, 766 { TS_BUSY, 'B'}, 767 { TS_ASLEEP, 'A'}, 768 { TS_XCLUDE, 'X'}, 769 { TS_TTSTOP, 'S'}, 770 { TS_TBLOCK, 'K'}, 771 { TS_ASYNC, 'Y'}, 772 { TS_BKSL, 'D'}, 773 { TS_ERASE, 'E'}, 774 { TS_LNCH, 'L'}, 775 { TS_TYPEN, 'P'}, 776 { TS_CNTTB, 'N'}, 777 { 0, '\0'}, 778}; 779 780void 781ttyprt(tp) 782 register struct tty *tp; 783{ 784 register int i, j; 785 pid_t pgid; 786 char *name, state[20]; 787 788 if (usenumflag || (name = devname(tp->t_dev, S_IFCHR)) == NULL) 789 (void)printf("%2d,%-3d ", major(tp->t_dev), minor(tp->t_dev)); 790 else 791 (void)printf("%7s ", name); 792 (void)printf("%3d %4d ", tp->t_rawq.c_cc, tp->t_canq.c_cc); 793 (void)printf("%4d %4d %3d %6d ", tp->t_outq.c_cc, 794 tp->t_hiwat, tp->t_lowat, tp->t_column); 795 for (i = j = 0; ttystates[i].flag; i++) 796 if (tp->t_state&ttystates[i].flag) 797 state[j++] = ttystates[i].val; 798 if (j == 0) 799 state[j++] = '-'; 800 state[j] = '\0'; 801 (void)printf("%-6s %8lx", state, (u_long)tp->t_session & ~KERNBASE); 802 pgid = 0; 803 if (tp->t_pgrp != NULL) 804 KGET2(&tp->t_pgrp->pg_id, &pgid, sizeof(pid_t), "pgid"); 805 (void)printf("%6d ", pgid); 806 switch (tp->t_line) { 807 case TTYDISC: 808 (void)printf("term\n"); 809 break; 810 case TABLDISC: 811 (void)printf("tab\n"); 812 break; 813 case SLIPDISC: 814 (void)printf("slip\n"); 815 break; 816 case PPPDISC: 817 (void)printf("ppp\n"); 818 break; 819 case STRIPDISC: 820 (void)printf("strip\n"); 821 break; 822 default: 823 (void)printf("%d\n", tp->t_line); 824 break; 825 } 826} 827 828void 829filemode() 830{ 831 register struct file *fp; 832 struct file *addr; 833 char *buf, flagbuf[16], *fbp; 834 int len, maxfile, nfile; 835 static char *dtypes[] = { "???", "inode", "socket" }; 836 837 KGET(FNL_MAXFILE, maxfile); 838 if (totalflag) { 839 KGET(FNL_NFILE, nfile); 840 (void)printf("%3d/%3d files\n", nfile, maxfile); 841 return; 842 } 843 if (getfiles(&buf, &len) == -1) 844 return; 845 /* 846 * Getfiles returns in malloc'd memory a pointer to the first file 847 * structure, and then an array of file structs (whose addresses are 848 * derivable from the previous entry). 849 */ 850 addr = ((struct filelist *)buf)->lh_first; 851 fp = (struct file *)(buf + sizeof(struct filelist)); 852 nfile = (len - sizeof(struct filelist)) / sizeof(struct file); 853 854 (void)printf("%d/%d open files\n", nfile, maxfile); 855 (void)printf(" LOC TYPE FLG CNT MSG DATA OFFSET\n"); 856 for (; (char *)fp < buf + len; addr = fp->f_list.le_next, fp++) { 857 if ((unsigned)fp->f_type > DTYPE_SOCKET) 858 continue; 859 (void)printf("%lx ", (long)addr); 860 (void)printf("%-8.8s", dtypes[fp->f_type]); 861 fbp = flagbuf; 862 if (fp->f_flag & FREAD) 863 *fbp++ = 'R'; 864 if (fp->f_flag & FWRITE) 865 *fbp++ = 'W'; 866 if (fp->f_flag & FAPPEND) 867 *fbp++ = 'A'; 868#ifdef FSHLOCK /* currently gone */ 869 if (fp->f_flag & FSHLOCK) 870 *fbp++ = 'S'; 871 if (fp->f_flag & FEXLOCK) 872 *fbp++ = 'X'; 873#endif 874 if (fp->f_flag & FASYNC) 875 *fbp++ = 'I'; 876 *fbp = '\0'; 877 (void)printf("%6s %3ld", flagbuf, fp->f_count); 878 (void)printf(" %3ld", fp->f_msgcount); 879 (void)printf(" %8.1lx", (long)fp->f_data); 880 if (fp->f_offset < 0) 881 (void)printf(" %qx\n", fp->f_offset); 882 else 883 (void)printf(" %qd\n", fp->f_offset); 884 } 885 free(buf); 886} 887 888int 889getfiles(abuf, alen) 890 char **abuf; 891 int *alen; 892{ 893 size_t len; 894 int mib[2]; 895 char *buf; 896 897 /* 898 * XXX 899 * Add emulation of KINFO_FILE here. 900 */ 901 if (memf != NULL) 902 errx(1, "files on dead kernel, not implemented"); 903 904 mib[0] = CTL_KERN; 905 mib[1] = KERN_FILE; 906 if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1) { 907 warn("sysctl: KERN_FILE"); 908 return (-1); 909 } 910 if ((buf = malloc(len)) == NULL) 911 err(1, "malloc: KERN_FILE"); 912 if (sysctl(mib, 2, buf, &len, NULL, 0) == -1) { 913 warn("sysctl: KERN_FILE"); 914 return (-1); 915 } 916 *abuf = buf; 917 *alen = len; 918 return (0); 919} 920 921/* 922 * swapmode is based on a program called swapinfo written 923 * by Kevin Lahey <kml@rokkaku.atl.ga.us>. 924 */ 925void 926swapmode() 927{ 928 char *header; 929 int hlen = 10, nswap, rnswap; 930 int div, i, avail, nfree, npfree, used; 931 long blocksize; 932 struct swapent *swdev; 933 934 if (kflag) { 935 header = "1K-blocks"; 936 blocksize = 1024; 937 hlen = strlen(header); 938 } else 939 header = getbsize(&hlen, &blocksize); 940 941 nswap = swapctl(SWAP_NSWAP, 0, 0); 942 if (nswap == 0) { 943 if (!totalflag) 944 (void)printf("%-11s %*s %8s %8s %8s %s\n", 945 "Device", hlen, header, 946 "Used", "Avail", "Capacity", "Priority"); 947 (void)printf("%-11s %*d %8d %8d %5.0f%%\n", 948 "Total", hlen, 0, 0, 0, 0.0); 949 return; 950 } 951 if ((swdev = malloc(nswap * sizeof(*swdev))) == NULL) 952 err(1, "malloc"); 953 rnswap = swapctl(SWAP_STATS, swdev, nswap); 954 955 if (!totalflag) 956 (void)printf("%-11s %*s %8s %8s %8s %s\n", 957 "Device", hlen, header, 958 "Used", "Avail", "Capacity", "Priority"); 959 960 /* Run through swap list, doing the funky monkey. */ 961 div = blocksize / 512; 962 avail = nfree = npfree = 0; 963 for (i = 0; i < nswap; i++) { 964 int xsize, xfree; 965 966 if (!(swdev[i].se_flags & SWF_ENABLE)) 967 continue; 968 969 if (!totalflag) { 970 if (usenumflag) 971 (void)printf("%2d,%-2d %*d ", 972 major(swdev[i].se_dev), 973 minor(swdev[i].se_dev), 974 hlen, swdev[i].se_nblks / div); 975 else 976 (void)printf("%-11s %*d ", swdev[i].se_path, 977 hlen, swdev[i].se_nblks / div); 978 } 979 980 xsize = swdev[i].se_nblks; 981 used = swdev[i].se_inuse; 982 xfree = xsize - used; 983 nfree += (xsize - used); 984 npfree++; 985 avail += xsize; 986 if (totalflag) 987 continue; 988 (void)printf("%8d %8d %5.0f%% %d\n", 989 used / div, xfree / div, 990 (double)used / (double)xsize * 100.0, 991 swdev[i].se_priority); 992 } 993 994 /* 995 * If only one partition has been set up via swapon(8), we don't 996 * need to bother with totals. 997 */ 998 used = avail - nfree; 999 if (totalflag) { 1000 (void)printf("%dM/%dM swap space\n", used / 2048, avail / 2048); 1001 return; 1002 } 1003 if (npfree > 1) { 1004 (void)printf("%-11s %*d %8d %8d %5.0f%%\n", 1005 "Total", hlen, avail / div, used / div, nfree / div, 1006 (double)used / (double)avail * 100.0); 1007 } 1008 1009 free(swdev); 1010} 1011 1012void 1013usage() 1014{ 1015 (void)fprintf(stderr, 1016 "usage: pstat [-Tfknstv] [-M core] [-N system]\n"); 1017 exit(1); 1018} 1019