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