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