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