1 /*- 2 * Copyright (c) 1988 The Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 char copyright[] = 36 "@(#) Copyright (c) 1988 The Regents of the University of California.\n\ 37 All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 static char sccsid[] = "@(#)fstat.c 5.32 (Berkeley) 6/17/91"; 42 #endif /* not lint */ 43 44 /* 45 * fstat 46 */ 47 #include <sys/param.h> 48 #include <sys/time.h> 49 #include <sys/proc.h> 50 #include <sys/user.h> 51 #ifdef SPPWAIT 52 #define NEWVM 53 #endif 54 #ifndef NEWVM 55 #include <machine/pte.h> 56 #include <sys/vmmac.h> 57 #endif 58 #include <sys/stat.h> 59 #include <sys/vnode.h> 60 #include <sys/socket.h> 61 #include <sys/socketvar.h> 62 #include <sys/domain.h> 63 #include <sys/protosw.h> 64 #include <sys/unpcb.h> 65 #include <sys/kinfo.h> 66 #include <sys/filedesc.h> 67 #define KERNEL 68 #define NFSSERVER 69 #define NFS_CLIENT 70 #include <sys/file.h> 71 #include <sys/mount.h> 72 #include <ufs/quota.h> 73 #include <ufs/inode.h> 74 #include <nfs/nfsv2.h> 75 #include <nfs/nfs.h> 76 #include <nfs/nfsnode.h> 77 #undef KERNEL 78 79 #include <net/route.h> 80 #include <netinet/in.h> 81 #include <netinet/in_systm.h> 82 #include <netinet/ip.h> 83 #include <netinet/in_pcb.h> 84 85 #include <errno.h> 86 #include <nlist.h> 87 #include <kvm.h> 88 #include <pwd.h> 89 #include <stdio.h> 90 #include <paths.h> 91 #include <ctype.h> 92 #include <stdlib.h> 93 #include <string.h> 94 95 #define TEXT -1 96 #define CDIR -2 97 #define RDIR -3 98 #define TRACE -4 99 100 typedef struct devs { 101 struct devs *next; 102 long fsid; 103 ino_t ino; 104 char *name; 105 } DEVS; 106 DEVS *devs; 107 108 struct filestat { 109 long fsid; 110 long fileid; 111 mode_t mode; 112 u_long size; 113 dev_t rdev; 114 }; 115 116 #ifdef notdef 117 struct nlist nl[] = { 118 { "" }, 119 }; 120 #endif 121 122 int fsflg, /* show files on same filesystem as file(s) argument */ 123 pflg, /* show files open by a particular pid */ 124 uflg; /* show files open by a particular (effective) user */ 125 int checkfile; /* true if restricting to particular files or filesystems */ 126 int nflg; /* (numerical) display f.s. and rdev as dev_t */ 127 int vflg; /* display errors in locating kernel data objects etc... */ 128 129 #define dprintf if (vflg) fprintf 130 131 struct file **ofiles; /* buffer of pointers to file structures */ 132 int maxfiles; 133 #define ALLOC_OFILES(d) \ 134 if ((d) > maxfiles) { \ 135 free(ofiles); \ 136 ofiles = malloc((d) * sizeof(struct file *)); \ 137 if (ofiles == NULL) { \ 138 fprintf(stderr, "fstat: %s\n", strerror(errno)); \ 139 exit(1); \ 140 } \ 141 maxfiles = (d); \ 142 } 143 144 /* 145 * a kvm_read that returns true if everything is read 146 */ 147 #define KVM_READ(kaddr, paddr, len) (kvm_read((kaddr), (paddr), (len)) == (len)) 148 149 void dofiles(), getinetproto(), socktrans(), nfs_filestat(), ufs_filestat(); 150 void usage(), vtrans(); 151 152 main(argc, argv) 153 int argc; 154 char **argv; 155 { 156 extern char *optarg; 157 extern int optind; 158 register struct passwd *passwd; 159 struct proc *p; 160 int arg, ch, what; 161 char *namelist = NULL, *memfile = NULL; 162 163 arg = 0; 164 what = KINFO_PROC_ALL; 165 while ((ch = getopt(argc, argv, "fnp:u:vNM")) != EOF) 166 switch((char)ch) { 167 case 'f': 168 fsflg = 1; 169 break; 170 case 'n': 171 nflg = 1; 172 break; 173 case 'p': 174 if (pflg++) 175 usage(); 176 if (!isdigit(*optarg)) { 177 fprintf(stderr, 178 "fstat: -p requires a process id\n"); 179 usage(); 180 } 181 what = KINFO_PROC_PID; 182 arg = atoi(optarg); 183 break; 184 case 'u': 185 if (uflg++) 186 usage(); 187 if (!(passwd = getpwnam(optarg))) { 188 fprintf(stderr, "%s: unknown uid\n", 189 optarg); 190 exit(1); 191 } 192 what = KINFO_PROC_UID; 193 arg = passwd->pw_uid; 194 break; 195 case 'v': 196 vflg = 1; 197 break; 198 case 'N': 199 namelist = optarg; 200 break; 201 case 'M': 202 memfile = optarg; 203 break; 204 case '?': 205 default: 206 usage(); 207 } 208 209 if (*(argv += optind)) { 210 for (; *argv; ++argv) { 211 if (getfname(*argv)) 212 checkfile = 1; 213 } 214 if (!checkfile) /* file(s) specified, but none accessable */ 215 exit(1); 216 } 217 218 ALLOC_OFILES(256); /* reserve space for file pointers */ 219 220 if (fsflg && !checkfile) { 221 /* -f with no files means use wd */ 222 if (getfname(".") == 0) 223 exit(1); 224 checkfile = 1; 225 } 226 227 if (kvm_openfiles(namelist, memfile, NULL) == -1) { 228 fprintf(stderr, "fstat: %s\n", kvm_geterr()); 229 exit(1); 230 } 231 #ifdef notdef 232 if (kvm_nlist(nl) != 0) { 233 fprintf(stderr, "fstat: no namelist: %s\n", kvm_geterr()); 234 exit(1); 235 } 236 #endif 237 if (kvm_getprocs(what, arg) == -1) { 238 fprintf(stderr, "fstat: %s\n", kvm_geterr()); 239 exit(1); 240 } 241 if (nflg) 242 printf("%s", 243 "USER CMD PID FD DEV INUM MODE SZ|DV"); 244 else 245 printf("%s", 246 "USER CMD PID FD MOUNT INUM MODE SZ|DV"); 247 if (checkfile && fsflg == 0) 248 printf(" NAME\n"); 249 else 250 putchar('\n'); 251 252 while ((p = kvm_nextproc()) != NULL) { 253 if (p->p_stat == SZOMB) 254 continue; 255 dofiles(p); 256 } 257 exit(0); 258 } 259 260 char *Uname, *Comm; 261 int Pid; 262 263 #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \ 264 switch(i) { \ 265 case TEXT: \ 266 printf(" text"); \ 267 break; \ 268 case CDIR: \ 269 printf(" wd"); \ 270 break; \ 271 case RDIR: \ 272 printf(" root"); \ 273 break; \ 274 case TRACE: \ 275 printf(" tr"); \ 276 break; \ 277 default: \ 278 printf(" %4d", i); \ 279 break; \ 280 } 281 282 /* 283 * print open files attributed to this process 284 */ 285 void 286 dofiles(p) 287 struct proc *p; 288 { 289 int i, last; 290 struct file file; 291 #ifdef NEWVM 292 struct filedesc0 filed0; 293 #define filed filed0.fd_fd 294 struct eproc *ep; 295 #else 296 struct filedesc filed; 297 #endif 298 299 extern char *user_from_uid(); 300 #ifndef NEWVM 301 struct vnode *xvptr; 302 #endif 303 304 #ifdef NEWVM 305 ep = kvm_geteproc(p); 306 Uname = user_from_uid(ep->e_ucred.cr_uid, 0); 307 #else 308 Uname = user_from_uid(p->p_uid, 0); 309 #endif 310 Pid = p->p_pid; 311 Comm = p->p_comm; 312 313 if (p->p_fd == NULL) 314 return; 315 #ifdef NEWVM 316 if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) { 317 dprintf(stderr, "can't read filedesc at %x for pid %d\n", 318 p->p_fd, Pid); 319 return; 320 } 321 #else 322 if (!KVM_READ(p->p_fd, &filed, sizeof (filed))) { 323 dprintf(stderr, "can't read filedesc at %x for pid %d\n", 324 p->p_fd, Pid); 325 return; 326 } 327 #endif 328 /* 329 * root directory vnode, if one 330 */ 331 if (filed.fd_rdir) 332 vtrans(filed.fd_rdir, RDIR); 333 #ifndef NEWVM 334 /* 335 * text vnode 336 */ 337 if (p->p_textp && 338 KVM_READ(&(p->p_textp->x_vptr), &xvptr, sizeof (struct vnode *)) && 339 xvptr != NULL) 340 vtrans(xvptr, TEXT); 341 #endif 342 /* 343 * current working directory vnode 344 */ 345 vtrans(filed.fd_cdir, CDIR); 346 /* 347 * ktrace vnode, if one 348 */ 349 if (p->p_tracep) 350 vtrans(p->p_tracep, TRACE); 351 /* 352 * open files 353 */ 354 #define FPSIZE (sizeof (struct file *)) 355 ALLOC_OFILES(filed.fd_lastfile); 356 #ifdef NEWVM 357 if (filed.fd_nfiles > NDFILE) { 358 if (!KVM_READ(filed.fd_ofiles, ofiles, 359 filed.fd_lastfile * FPSIZE)) { 360 dprintf(stderr, 361 "can't read file structures at %x for pid %d\n", 362 filed.fd_ofiles, Pid); 363 return; 364 } 365 } else 366 bcopy(filed0.fd_dfiles, ofiles, filed.fd_lastfile * FPSIZE); 367 #else 368 bcopy(filed.fd_ofile, ofiles, MIN(filed.fd_lastfile, NDFILE) * FPSIZE); 369 last = filed.fd_lastfile; 370 if ((last > NDFILE) && !KVM_READ(filed.fd_moreofiles, &ofiles[NDFILE], 371 (last - NDFILE) * FPSIZE)) { 372 dprintf(stderr, "can't read rest of files at %x for pid %d\n", 373 filed.fd_moreofiles, Pid); 374 return; 375 } 376 #endif 377 for (i = 0; i <= filed.fd_lastfile; i++) { 378 if (ofiles[i] == NULL) 379 continue; 380 if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) { 381 dprintf(stderr, "can't read file %d at %x for pid %d\n", 382 i, ofiles[i], Pid); 383 continue; 384 } 385 if (file.f_type == DTYPE_VNODE) 386 vtrans((struct vnode *)file.f_data, i); 387 else if (file.f_type == DTYPE_SOCKET && checkfile == 0) 388 socktrans((struct socket *)file.f_data, i); 389 else { 390 dprintf(stderr, 391 "unknown file type %d for file %d of pid %d\n", 392 file.f_type, i, Pid); 393 } 394 } 395 } 396 397 void 398 vtrans(vp, i) 399 struct vnode *vp; 400 int i; 401 { 402 extern char *devname(); 403 struct vnode vn; 404 struct filestat fst; 405 char mode[15]; 406 char *badtype, *filename, *getmnton(); 407 408 filename = badtype = NULL; 409 if (!KVM_READ(vp, &vn, sizeof (struct vnode))) { 410 dprintf(stderr, "can't read vnode at %x for pid %d\n", 411 vp, Pid); 412 return; 413 } 414 if (vn.v_type == VNON || vn.v_tag == VT_NON) 415 badtype = "none"; 416 else if (vn.v_type == VBAD) 417 badtype = "bad"; 418 else 419 switch (vn.v_tag) { 420 case VT_UFS: 421 ufs_filestat(&vn, &fst); 422 break; 423 case VT_MFS: 424 ufs_filestat(&vn, &fst); 425 break; 426 case VT_NFS: 427 nfs_filestat(&vn, &fst); 428 break; 429 default: { 430 static char unknown[10]; 431 sprintf(badtype = unknown, "?(%x)", vn.v_tag); 432 break;; 433 } 434 } 435 if (checkfile) { 436 int fsmatch = 0; 437 register DEVS *d; 438 439 if (badtype) 440 return; 441 for (d = devs; d != NULL; d = d->next) 442 if (d->fsid == fst.fsid) { 443 fsmatch = 1; 444 if (d->ino == fst.fileid) { 445 filename = d->name; 446 break; 447 } 448 } 449 if (fsmatch == 0 || (filename == NULL && fsflg == 0)) 450 return; 451 } 452 PREFIX(i); 453 if (badtype) { 454 (void)printf(" - - %10s -\n", badtype); 455 return; 456 } 457 if (nflg) 458 (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid)); 459 else 460 (void)printf(" %-8s", getmnton(vn.v_mount)); 461 if (nflg) 462 (void)sprintf(mode, "%o", fst.mode); 463 else 464 strmode(fst.mode, mode); 465 (void)printf(" %6d %10s", fst.fileid, mode); 466 switch (vn.v_type) { 467 case VBLK: 468 case VCHR: { 469 char *name; 470 471 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ? 472 S_IFCHR : S_IFBLK)) == NULL)) 473 printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev)); 474 else 475 printf(" %6s", name); 476 break; 477 } 478 default: 479 printf(" %6d", fst.size); 480 } 481 if (filename && !fsflg) 482 printf(" %s", filename); 483 484 putchar('\n'); 485 } 486 487 void 488 ufs_filestat(vp, fsp) 489 struct vnode *vp; 490 struct filestat *fsp; 491 { 492 struct inode *ip = VTOI(vp); 493 494 fsp->fsid = ip->i_dev & 0xffff; 495 fsp->fileid = (long)ip->i_number; 496 fsp->mode = (mode_t)ip->i_mode; 497 fsp->size = (u_long)ip->i_size; 498 fsp->rdev = ip->i_rdev; 499 } 500 501 void 502 nfs_filestat(vp, fsp) 503 struct vnode *vp; 504 struct filestat *fsp; 505 { 506 register struct nfsnode *np = VTONFS(vp); 507 register mode_t mode; 508 509 fsp->fsid = np->n_vattr.va_fsid; 510 fsp->fileid = np->n_vattr.va_fileid; 511 fsp->size = np->n_size; 512 fsp->rdev = np->n_vattr.va_rdev; 513 mode = (mode_t)np->n_vattr.va_mode; 514 switch (vp->v_type) { 515 case VREG: 516 mode |= S_IFREG; 517 break; 518 case VDIR: 519 mode |= S_IFDIR; 520 break; 521 case VBLK: 522 mode |= S_IFBLK; 523 break; 524 case VCHR: 525 mode |= S_IFCHR; 526 break; 527 case VLNK: 528 mode |= S_IFLNK; 529 break; 530 case VSOCK: 531 mode |= S_IFSOCK; 532 break; 533 case VFIFO: 534 mode |= S_IFIFO; 535 break; 536 }; 537 fsp->mode = mode; 538 } 539 540 541 char * 542 getmnton(m) 543 struct mount *m; 544 { 545 static struct mount mount; 546 static struct mtab { 547 struct mtab *next; 548 struct mount *m; 549 char mntonname[MNAMELEN]; 550 } *mhead = NULL; 551 register struct mtab *mt; 552 553 for (mt = mhead; mt != NULL; mt = mt->next) 554 if (m == mt->m) 555 return (mt->mntonname); 556 if (!KVM_READ(m, &mount, sizeof(struct mount))) { 557 fprintf(stderr, "can't read mount table at %x\n", m); 558 return (NULL); 559 } 560 if ((mt = malloc(sizeof (struct mtab))) == NULL) { 561 fprintf(stderr, "fstat: %s\n", strerror(errno)); 562 exit(1); 563 } 564 mt->m = m; 565 bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN); 566 mt->next = mhead; 567 mhead = mt; 568 return (mt->mntonname); 569 } 570 571 void 572 socktrans(sock, i) 573 struct socket *sock; 574 int i; 575 { 576 static char *stypename[] = { 577 "unused", /* 0 */ 578 "stream", /* 1 */ 579 "dgram", /* 2 */ 580 "raw", /* 3 */ 581 "rdm", /* 4 */ 582 "seqpak" /* 5 */ 583 }; 584 #define STYPEMAX 5 585 struct socket so; 586 struct protosw proto; 587 struct domain dom; 588 struct inpcb inpcb; 589 struct unpcb unpcb; 590 int len; 591 char dname[32], *strcpy(); 592 593 PREFIX(i); 594 595 /* fill in socket */ 596 if (!KVM_READ(sock, &so, sizeof(struct socket))) { 597 dprintf(stderr, "can't read sock at %x\n", sock); 598 goto bad; 599 } 600 601 /* fill in protosw entry */ 602 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) { 603 dprintf(stderr, "can't read protosw at %x", so.so_proto); 604 goto bad; 605 } 606 607 /* fill in domain */ 608 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) { 609 dprintf(stderr, "can't read domain at %x\n", proto.pr_domain); 610 goto bad; 611 } 612 613 if ((len = 614 kvm_read(dom.dom_name, dname, sizeof(dname) - 1)) < 0) { 615 dprintf(stderr, "can't read domain name at %x\n", 616 dom.dom_name); 617 dname[0] = '\0'; 618 } 619 else 620 dname[len] = '\0'; 621 622 if ((u_short)so.so_type > STYPEMAX) 623 printf("* %s ?%d", dname, so.so_type); 624 else 625 printf("* %s %s", dname, stypename[so.so_type]); 626 627 /* 628 * protocol specific formatting 629 * 630 * Try to find interesting things to print. For tcp, the interesting 631 * thing is the address of the tcpcb, for udp and others, just the 632 * inpcb (socket pcb). For unix domain, its the address of the socket 633 * pcb and the address of the connected pcb (if connected). Otherwise 634 * just print the protocol number and address of the socket itself. 635 * The idea is not to duplicate netstat, but to make available enough 636 * information for further analysis. 637 */ 638 switch(dom.dom_family) { 639 case AF_INET: 640 getinetproto(proto.pr_protocol); 641 if (proto.pr_protocol == IPPROTO_TCP ) { 642 if (so.so_pcb) { 643 if (kvm_read(so.so_pcb, &inpcb, 644 sizeof(struct inpcb)) 645 != sizeof(struct inpcb)) { 646 dprintf(stderr, 647 "can't read inpcb at %x\n", 648 so.so_pcb); 649 goto bad; 650 } 651 printf(" %x", (int)inpcb.inp_ppcb); 652 } 653 } 654 else if (so.so_pcb) 655 printf(" %x", (int)so.so_pcb); 656 break; 657 case AF_UNIX: 658 /* print address of pcb and connected pcb */ 659 if (so.so_pcb) { 660 printf(" %x", (int)so.so_pcb); 661 if (kvm_read(so.so_pcb, &unpcb, 662 sizeof(struct unpcb)) != sizeof(struct unpcb)){ 663 dprintf(stderr, "can't read unpcb at %x\n", 664 so.so_pcb); 665 goto bad; 666 } 667 if (unpcb.unp_conn) { 668 char shoconn[4], *cp; 669 670 cp = shoconn; 671 if (!(so.so_state & SS_CANTRCVMORE)) 672 *cp++ = '<'; 673 *cp++ = '-'; 674 if (!(so.so_state & SS_CANTSENDMORE)) 675 *cp++ = '>'; 676 *cp = '\0'; 677 printf(" %s %x", shoconn, 678 (int)unpcb.unp_conn); 679 } 680 } 681 break; 682 default: 683 /* print protocol number and socket address */ 684 printf(" %d %x", proto.pr_protocol, (int)sock); 685 } 686 printf("\n"); 687 return; 688 bad: 689 printf("* error\n"); 690 } 691 692 /* 693 * getinetproto -- 694 * print name of protocol number 695 */ 696 void 697 getinetproto(number) 698 int number; 699 { 700 char *cp; 701 702 switch(number) { 703 case IPPROTO_IP: 704 cp = "ip"; break; 705 case IPPROTO_ICMP: 706 cp ="icmp"; break; 707 case IPPROTO_GGP: 708 cp ="ggp"; break; 709 case IPPROTO_TCP: 710 cp ="tcp"; break; 711 case IPPROTO_EGP: 712 cp ="egp"; break; 713 case IPPROTO_PUP: 714 cp ="pup"; break; 715 case IPPROTO_UDP: 716 cp ="udp"; break; 717 case IPPROTO_IDP: 718 cp ="idp"; break; 719 case IPPROTO_RAW: 720 cp ="raw"; break; 721 default: 722 printf(" %d", number); 723 return; 724 } 725 printf(" %s", cp); 726 } 727 728 getfname(filename) 729 char *filename; 730 { 731 struct stat statbuf; 732 DEVS *cur; 733 734 if (stat(filename, &statbuf)) { 735 fprintf(stderr, "fstat: %s: %s\n", strerror(errno), 736 filename); 737 return(0); 738 } 739 if ((cur = malloc(sizeof(DEVS))) == NULL) { 740 fprintf(stderr, "fstat: %s\n", strerror(errno)); 741 exit(1); 742 } 743 cur->next = devs; 744 devs = cur; 745 746 cur->ino = statbuf.st_ino; 747 cur->fsid = statbuf.st_dev & 0xffff; 748 cur->name = filename; 749 return(1); 750 } 751 752 void 753 usage() 754 { 755 (void)fprintf(stderr, 756 "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n"); 757 exit(1); 758 } 759