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