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