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