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