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