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