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 const 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 #if 0 42 static char sccsid[] = "@(#)fstat.c 8.3 (Berkeley) 5/2/95"; 43 #endif 44 static const char rcsid[] = 45 "$FreeBSD$"; 46 #endif /* not lint */ 47 48 #include <sys/param.h> 49 #include <sys/time.h> 50 #include <sys/proc.h> 51 #include <sys/user.h> 52 #include <sys/stat.h> 53 #include <sys/vnode.h> 54 #include <sys/socket.h> 55 #include <sys/socketvar.h> 56 #include <sys/domain.h> 57 #include <sys/protosw.h> 58 #include <sys/un.h> 59 #include <sys/unpcb.h> 60 #include <sys/sysctl.h> 61 #include <sys/filedesc.h> 62 #include <sys/queue.h> 63 #include <sys/pipe.h> 64 #define _KERNEL 65 #include <sys/conf.h> 66 #include <sys/file.h> 67 #include <ufs/ufs/quota.h> 68 #include <ufs/ufs/inode.h> 69 #include <fs/devfs/devfs.h> 70 #include <sys/mount.h> 71 #undef _KERNEL 72 #include <nfs/nfsproto.h> 73 #include <nfs/rpcv2.h> 74 #include <nfsclient/nfs.h> 75 #include <nfsclient/nfsnode.h> 76 77 78 #include <vm/vm.h> 79 #include <vm/vm_map.h> 80 #include <vm/vm_object.h> 81 82 #include <net/route.h> 83 #include <netinet/in.h> 84 #include <netinet/in_systm.h> 85 #include <netinet/ip.h> 86 #include <netinet/in_pcb.h> 87 88 #include <ctype.h> 89 #include <err.h> 90 #include <fcntl.h> 91 #include <kvm.h> 92 #include <limits.h> 93 #include <nlist.h> 94 #include <paths.h> 95 #include <pwd.h> 96 #include <stdio.h> 97 #include <stdlib.h> 98 #include <string.h> 99 #include <unistd.h> 100 #include <netdb.h> 101 102 #include "fstat.h" 103 104 #define TEXT -1 105 #define CDIR -2 106 #define RDIR -3 107 #define TRACE -4 108 #define MMAP -5 109 110 DEVS *devs; 111 112 #ifdef notdef 113 struct nlist nl[] = { 114 { "" }, 115 }; 116 #endif 117 118 int fsflg, /* show files on same filesystem as file(s) argument */ 119 pflg, /* show files open by a particular pid */ 120 uflg; /* show files open by a particular (effective) user */ 121 int checkfile; /* true if restricting to particular files or filesystems */ 122 int nflg; /* (numerical) display f.s. and rdev as dev_t */ 123 int vflg; /* display errors in locating kernel data objects etc... */ 124 int mflg; /* include memory-mapped files */ 125 126 127 struct file **ofiles; /* buffer of pointers to file structures */ 128 int maxfiles; 129 #define ALLOC_OFILES(d) \ 130 if ((d) > maxfiles) { \ 131 free(ofiles); \ 132 ofiles = malloc((d) * sizeof(struct file *)); \ 133 if (ofiles == NULL) { \ 134 err(1, NULL); \ 135 } \ 136 maxfiles = (d); \ 137 } 138 139 char *memf, *nlistf; 140 kvm_t *kd; 141 142 static void fstat_kvm(int, int); 143 static void fstat_sysctl(int, int); 144 void dofiles(struct kinfo_proc *kp); 145 void dommap(struct kinfo_proc *kp); 146 void vtrans(struct vnode *vp, int i, int flag); 147 int ufs_filestat(struct vnode *vp, struct filestat *fsp); 148 int nfs_filestat(struct vnode *vp, struct filestat *fsp); 149 int devfs_filestat(struct vnode *vp, struct filestat *fsp); 150 char *getmnton(struct mount *m); 151 void pipetrans(struct pipe *pi, int i, int flag); 152 void socktrans(struct socket *sock, int i); 153 void getinetproto(int number); 154 int getfname(const char *filename); 155 void usage(void); 156 157 158 int 159 main(argc, argv) 160 int argc; 161 char **argv; 162 { 163 struct passwd *passwd; 164 int arg, ch, what; 165 166 arg = 0; 167 what = KERN_PROC_ALL; 168 nlistf = memf = NULL; 169 while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1) 170 switch((char)ch) { 171 case 'f': 172 fsflg = 1; 173 break; 174 case 'M': 175 memf = optarg; 176 break; 177 case 'N': 178 nlistf = optarg; 179 break; 180 case 'm': 181 mflg = 1; 182 break; 183 case 'n': 184 nflg = 1; 185 break; 186 case 'p': 187 if (pflg++) 188 usage(); 189 if (!isdigit(*optarg)) { 190 warnx("-p requires a process id"); 191 usage(); 192 } 193 what = KERN_PROC_PID; 194 arg = atoi(optarg); 195 break; 196 case 'u': 197 if (uflg++) 198 usage(); 199 if (!(passwd = getpwnam(optarg))) 200 errx(1, "%s: unknown uid", optarg); 201 what = KERN_PROC_UID; 202 arg = passwd->pw_uid; 203 break; 204 case 'v': 205 vflg = 1; 206 break; 207 case '?': 208 default: 209 usage(); 210 } 211 212 if (*(argv += optind)) { 213 for (; *argv; ++argv) { 214 if (getfname(*argv)) 215 checkfile = 1; 216 } 217 if (!checkfile) /* file(s) specified, but none accessable */ 218 exit(1); 219 } 220 221 if (fsflg && !checkfile) { 222 /* -f with no files means use wd */ 223 if (getfname(".") == 0) 224 exit(1); 225 checkfile = 1; 226 } 227 228 if (memf != NULL) 229 fstat_kvm(what, arg); 230 else 231 fstat_sysctl(what, arg); 232 exit(0); 233 } 234 235 static void 236 print_header(void) 237 { 238 239 if (nflg) 240 printf("%s", 241 "USER CMD PID FD DEV INUM MODE SZ|DV R/W"); 242 else 243 printf("%s", 244 "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W"); 245 if (checkfile && fsflg == 0) 246 printf(" NAME\n"); 247 else 248 putchar('\n'); 249 } 250 251 static void 252 fstat_kvm(int what, int arg) 253 { 254 struct kinfo_proc *p, *plast; 255 char buf[_POSIX2_LINE_MAX]; 256 int cnt; 257 258 ALLOC_OFILES(256); /* reserve space for file pointers */ 259 260 /* 261 * Discard setgid privileges if not the running kernel so that bad 262 * guys can't print interesting stuff from kernel memory. 263 */ 264 if (nlistf != NULL || memf != NULL) 265 setgid(getgid()); 266 267 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL) 268 errx(1, "%s", buf); 269 setgid(getgid()); 270 #ifdef notdef 271 if (kvm_nlist(kd, nl) != 0) 272 errx(1, "no namelist: %s", kvm_geterr(kd)); 273 #endif 274 if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) 275 errx(1, "%s", kvm_geterr(kd)); 276 for (plast = &p[cnt]; p < plast; ++p) { 277 if (p->ki_stat == SZOMB) 278 continue; 279 dofiles(p); 280 if (mflg) 281 dommap(p); 282 } 283 } 284 285 static void 286 fstat_sysctl(int what, int arg) 287 { 288 289 /* not yet implemented */ 290 fstat_kvm(what, arg); 291 } 292 293 const char *Uname, *Comm; 294 int Pid; 295 296 #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \ 297 switch(i) { \ 298 case TEXT: \ 299 printf(" text"); \ 300 break; \ 301 case CDIR: \ 302 printf(" wd"); \ 303 break; \ 304 case RDIR: \ 305 printf(" root"); \ 306 break; \ 307 case TRACE: \ 308 printf(" tr"); \ 309 break; \ 310 case MMAP: \ 311 printf(" mmap"); \ 312 break; \ 313 default: \ 314 printf(" %4d", i); \ 315 break; \ 316 } 317 318 /* 319 * print open files attributed to this process 320 */ 321 void 322 dofiles(kp) 323 struct kinfo_proc *kp; 324 { 325 int i; 326 struct file file; 327 struct filedesc0 filed0; 328 #define filed filed0.fd_fd 329 330 Uname = user_from_uid(kp->ki_uid, 0); 331 Pid = kp->ki_pid; 332 Comm = kp->ki_comm; 333 334 if (kp->ki_fd == NULL) 335 return; 336 if (!KVM_READ(kp->ki_fd, &filed0, sizeof (filed0))) { 337 dprintf(stderr, "can't read filedesc at %p for pid %d\n", 338 (void *)kp->ki_fd, Pid); 339 return; 340 } 341 /* 342 * root directory vnode, if one 343 */ 344 if (filed.fd_rdir) 345 vtrans(filed.fd_rdir, RDIR, FREAD); 346 /* 347 * current working directory vnode 348 */ 349 vtrans(filed.fd_cdir, CDIR, FREAD); 350 /* 351 * ktrace vnode, if one 352 */ 353 if (kp->ki_tracep) 354 vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE); 355 /* 356 * text vnode, if one 357 */ 358 if (kp->ki_textvp) 359 vtrans(kp->ki_textvp, TEXT, FREAD); 360 /* 361 * open files 362 */ 363 #define FPSIZE (sizeof (struct file *)) 364 ALLOC_OFILES(filed.fd_lastfile+1); 365 if (filed.fd_nfiles > NDFILE) { 366 if (!KVM_READ(filed.fd_ofiles, ofiles, 367 (filed.fd_lastfile+1) * FPSIZE)) { 368 dprintf(stderr, 369 "can't read file structures at %p for pid %d\n", 370 (void *)filed.fd_ofiles, Pid); 371 return; 372 } 373 } else 374 bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE); 375 for (i = 0; i <= filed.fd_lastfile; i++) { 376 if (ofiles[i] == NULL) 377 continue; 378 if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) { 379 dprintf(stderr, "can't read file %d at %p for pid %d\n", 380 i, (void *)ofiles[i], Pid); 381 continue; 382 } 383 if (file.f_type == DTYPE_VNODE) 384 vtrans((struct vnode *)file.f_data, i, file.f_flag); 385 else if (file.f_type == DTYPE_SOCKET) { 386 if (checkfile == 0) 387 socktrans((struct socket *)file.f_data, i); 388 } 389 #ifdef DTYPE_PIPE 390 else if (file.f_type == DTYPE_PIPE) { 391 if (checkfile == 0) 392 pipetrans((struct pipe *)file.f_data, i, 393 file.f_flag); 394 } 395 #endif 396 #ifdef DTYPE_FIFO 397 else if (file.f_type == DTYPE_FIFO) { 398 if (checkfile == 0) 399 vtrans((struct vnode *)file.f_data, i, 400 file.f_flag); 401 } 402 #endif 403 else { 404 dprintf(stderr, 405 "unknown file type %d for file %d of pid %d\n", 406 file.f_type, i, Pid); 407 } 408 } 409 } 410 411 void 412 dommap(kp) 413 struct kinfo_proc *kp; 414 { 415 vm_map_t map; 416 struct vmspace vmspace; 417 struct vm_map_entry entry; 418 vm_map_entry_t entryp; 419 struct vm_object object; 420 vm_object_t objp; 421 int prot, fflags; 422 423 if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) { 424 dprintf(stderr, 425 "can't read vmspace at %p for pid %d\n", 426 (void *)kp->ki_vmspace, Pid); 427 return; 428 } 429 map = &vmspace.vm_map; 430 431 for (entryp = map->header.next; 432 entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) { 433 if (!KVM_READ(entryp, &entry, sizeof(entry))) { 434 dprintf(stderr, 435 "can't read vm_map_entry at %p for pid %d\n", 436 (void *)entryp, Pid); 437 return; 438 } 439 440 if (entry.eflags & MAP_ENTRY_IS_SUB_MAP) 441 continue; 442 443 if ((objp = entry.object.vm_object) == NULL) 444 continue; 445 446 for (; objp; objp = object.backing_object) { 447 if (!KVM_READ(objp, &object, sizeof(object))) { 448 dprintf(stderr, 449 "can't read vm_object at %p for pid %d\n", 450 (void *)objp, Pid); 451 return; 452 } 453 } 454 455 prot = entry.protection; 456 fflags = (prot & VM_PROT_READ ? FREAD : 0) | 457 (prot & VM_PROT_WRITE ? FWRITE : 0); 458 459 switch (object.type) { 460 case OBJT_VNODE: 461 vtrans((struct vnode *)object.handle, MMAP, fflags); 462 break; 463 default: 464 break; 465 } 466 } 467 } 468 469 void 470 vtrans(vp, i, flag) 471 struct vnode *vp; 472 int i; 473 int flag; 474 { 475 struct vnode vn; 476 struct filestat fst; 477 char rw[3], mode[15]; 478 const char *badtype, *filename; 479 480 filename = badtype = NULL; 481 if (!KVM_READ(vp, &vn, sizeof (struct vnode))) { 482 dprintf(stderr, "can't read vnode at %p for pid %d\n", 483 (void *)vp, Pid); 484 return; 485 } 486 if (vn.v_type == VNON || vn.v_tag == VT_NON) 487 badtype = "none"; 488 else if (vn.v_type == VBAD) 489 badtype = "bad"; 490 else 491 switch (vn.v_tag) { 492 case VT_UFS: 493 if (!ufs_filestat(&vn, &fst)) 494 badtype = "error"; 495 break; 496 497 case VT_DEVFS: 498 if (!devfs_filestat(&vn, &fst)) 499 badtype = "error"; 500 break; 501 502 case VT_NFS: 503 if (!nfs_filestat(&vn, &fst)) 504 badtype = "error"; 505 break; 506 507 case VT_MSDOSFS: 508 if (!msdosfs_filestat(&vn, &fst)) 509 badtype = "error"; 510 break; 511 512 case VT_ISOFS: 513 if (!isofs_filestat(&vn, &fst)) 514 badtype = "error"; 515 break; 516 517 default: { 518 static char unknown[10]; 519 sprintf(unknown, "?(%x)", vn.v_tag); 520 badtype = unknown; 521 break;; 522 } 523 } 524 if (checkfile) { 525 int fsmatch = 0; 526 DEVS *d; 527 528 if (badtype) 529 return; 530 for (d = devs; d != NULL; d = d->next) 531 if (d->fsid == fst.fsid) { 532 fsmatch = 1; 533 if (d->ino == fst.fileid) { 534 filename = d->name; 535 break; 536 } 537 } 538 if (fsmatch == 0 || (filename == NULL && fsflg == 0)) 539 return; 540 } 541 PREFIX(i); 542 if (badtype) { 543 (void)printf(" - - %10s -\n", badtype); 544 return; 545 } 546 if (nflg) 547 (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid)); 548 else 549 (void)printf(" %-8s", getmnton(vn.v_mount)); 550 if (nflg) 551 (void)sprintf(mode, "%o", fst.mode); 552 else 553 strmode(fst.mode, mode); 554 (void)printf(" %6ld %10s", fst.fileid, mode); 555 switch (vn.v_type) { 556 case VBLK: 557 case VCHR: { 558 char *name; 559 560 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ? 561 S_IFCHR : S_IFBLK)) == NULL)) 562 printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev)); 563 else 564 printf(" %6s", name); 565 break; 566 } 567 default: 568 printf(" %6lu", fst.size); 569 } 570 rw[0] = '\0'; 571 if (flag & FREAD) 572 strcat(rw, "r"); 573 if (flag & FWRITE) 574 strcat(rw, "w"); 575 printf(" %2s", rw); 576 if (filename && !fsflg) 577 printf(" %s", filename); 578 putchar('\n'); 579 } 580 581 int 582 ufs_filestat(vp, fsp) 583 struct vnode *vp; 584 struct filestat *fsp; 585 { 586 struct inode inode; 587 588 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 589 dprintf(stderr, "can't read inode at %p for pid %d\n", 590 (void *)VTOI(vp), Pid); 591 return 0; 592 } 593 /* 594 * The st_dev from stat(2) is a udev_t. These kernel structures 595 * contain dev_t structures. We need to convert to udev to make 596 * comparisons 597 */ 598 fsp->fsid = dev2udev(inode.i_dev); 599 fsp->fileid = (long)inode.i_number; 600 fsp->mode = (mode_t)inode.i_mode; 601 fsp->size = (u_long)inode.i_size; 602 fsp->rdev = inode.i_rdev; 603 604 return 1; 605 } 606 607 int 608 devfs_filestat(vp, fsp) 609 struct vnode *vp; 610 struct filestat *fsp; 611 { 612 struct devfs_dirent devfs_dirent; 613 struct mount mount; 614 struct vnode vnode; 615 616 if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) { 617 dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n", 618 (void *)vp->v_data, Pid); 619 return 0; 620 } 621 if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) { 622 dprintf(stderr, "can't read mount at %p for pid %d\n", 623 (void *)vp->v_mount, Pid); 624 return 0; 625 } 626 if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) { 627 dprintf(stderr, "can't read vnode at %p for pid %d\n", 628 (void *)devfs_dirent.de_vnode, Pid); 629 return 0; 630 } 631 fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0]; 632 fsp->fileid = devfs_dirent.de_inode; 633 fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR; 634 fsp->size = 0; 635 fsp->rdev = dev2udev((dev_t)vnode.v_rdev); 636 637 return 1; 638 } 639 640 int 641 nfs_filestat(vp, fsp) 642 struct vnode *vp; 643 struct filestat *fsp; 644 { 645 struct nfsnode nfsnode; 646 mode_t mode; 647 648 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) { 649 dprintf(stderr, "can't read nfsnode at %p for pid %d\n", 650 (void *)VTONFS(vp), Pid); 651 return 0; 652 } 653 fsp->fsid = nfsnode.n_vattr.va_fsid; 654 fsp->fileid = nfsnode.n_vattr.va_fileid; 655 fsp->size = nfsnode.n_size; 656 fsp->rdev = nfsnode.n_vattr.va_rdev; 657 mode = (mode_t)nfsnode.n_vattr.va_mode; 658 switch (vp->v_type) { 659 case VREG: 660 mode |= S_IFREG; 661 break; 662 case VDIR: 663 mode |= S_IFDIR; 664 break; 665 case VBLK: 666 mode |= S_IFBLK; 667 break; 668 case VCHR: 669 mode |= S_IFCHR; 670 break; 671 case VLNK: 672 mode |= S_IFLNK; 673 break; 674 case VSOCK: 675 mode |= S_IFSOCK; 676 break; 677 case VFIFO: 678 mode |= S_IFIFO; 679 break; 680 case VNON: 681 case VBAD: 682 return 0; 683 }; 684 fsp->mode = mode; 685 686 return 1; 687 } 688 689 690 char * 691 getmnton(m) 692 struct mount *m; 693 { 694 static struct mount mount; 695 static struct mtab { 696 struct mtab *next; 697 struct mount *m; 698 char mntonname[MNAMELEN]; 699 } *mhead = NULL; 700 struct mtab *mt; 701 702 for (mt = mhead; mt != NULL; mt = mt->next) 703 if (m == mt->m) 704 return (mt->mntonname); 705 if (!KVM_READ(m, &mount, sizeof(struct mount))) { 706 warnx("can't read mount table at %p", (void *)m); 707 return (NULL); 708 } 709 if ((mt = malloc(sizeof (struct mtab))) == NULL) 710 err(1, NULL); 711 mt->m = m; 712 bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN); 713 mt->next = mhead; 714 mhead = mt; 715 return (mt->mntonname); 716 } 717 718 void 719 pipetrans(pi, i, flag) 720 struct pipe *pi; 721 int i; 722 int flag; 723 { 724 struct pipe pip; 725 char rw[3]; 726 727 PREFIX(i); 728 729 /* fill in socket */ 730 if (!KVM_READ(pi, &pip, sizeof(struct pipe))) { 731 dprintf(stderr, "can't read pipe at %p\n", (void *)pi); 732 goto bad; 733 } 734 735 printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer); 736 printf(" %6d", (int)pip.pipe_buffer.cnt); 737 rw[0] = '\0'; 738 if (flag & FREAD) 739 strcat(rw, "r"); 740 if (flag & FWRITE) 741 strcat(rw, "w"); 742 printf(" %2s", rw); 743 putchar('\n'); 744 return; 745 746 bad: 747 printf("* error\n"); 748 } 749 750 void 751 socktrans(sock, i) 752 struct socket *sock; 753 int i; 754 { 755 static const char *stypename[] = { 756 "unused", /* 0 */ 757 "stream", /* 1 */ 758 "dgram", /* 2 */ 759 "raw", /* 3 */ 760 "rdm", /* 4 */ 761 "seqpak" /* 5 */ 762 }; 763 #define STYPEMAX 5 764 struct socket so; 765 struct protosw proto; 766 struct domain dom; 767 struct inpcb inpcb; 768 struct unpcb unpcb; 769 int len; 770 char dname[32]; 771 772 PREFIX(i); 773 774 /* fill in socket */ 775 if (!KVM_READ(sock, &so, sizeof(struct socket))) { 776 dprintf(stderr, "can't read sock at %p\n", (void *)sock); 777 goto bad; 778 } 779 780 /* fill in protosw entry */ 781 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) { 782 dprintf(stderr, "can't read protosw at %p", 783 (void *)so.so_proto); 784 goto bad; 785 } 786 787 /* fill in domain */ 788 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) { 789 dprintf(stderr, "can't read domain at %p\n", 790 (void *)proto.pr_domain); 791 goto bad; 792 } 793 794 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname, 795 sizeof(dname) - 1)) < 0) { 796 dprintf(stderr, "can't read domain name at %p\n", 797 (void *)dom.dom_name); 798 dname[0] = '\0'; 799 } 800 else 801 dname[len] = '\0'; 802 803 if ((u_short)so.so_type > STYPEMAX) 804 printf("* %s ?%d", dname, so.so_type); 805 else 806 printf("* %s %s", dname, stypename[so.so_type]); 807 808 /* 809 * protocol specific formatting 810 * 811 * Try to find interesting things to print. For tcp, the interesting 812 * thing is the address of the tcpcb, for udp and others, just the 813 * inpcb (socket pcb). For unix domain, its the address of the socket 814 * pcb and the address of the connected pcb (if connected). Otherwise 815 * just print the protocol number and address of the socket itself. 816 * The idea is not to duplicate netstat, but to make available enough 817 * information for further analysis. 818 */ 819 switch(dom.dom_family) { 820 case AF_INET: 821 case AF_INET6: 822 getinetproto(proto.pr_protocol); 823 if (proto.pr_protocol == IPPROTO_TCP ) { 824 if (so.so_pcb) { 825 if (kvm_read(kd, (u_long)so.so_pcb, 826 (char *)&inpcb, sizeof(struct inpcb)) 827 != sizeof(struct inpcb)) { 828 dprintf(stderr, 829 "can't read inpcb at %p\n", 830 (void *)so.so_pcb); 831 goto bad; 832 } 833 printf(" %lx", (u_long)inpcb.inp_ppcb); 834 } 835 } 836 else if (so.so_pcb) 837 printf(" %lx", (u_long)so.so_pcb); 838 break; 839 case AF_UNIX: 840 /* print address of pcb and connected pcb */ 841 if (so.so_pcb) { 842 printf(" %lx", (u_long)so.so_pcb); 843 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb, 844 sizeof(struct unpcb)) != sizeof(struct unpcb)){ 845 dprintf(stderr, "can't read unpcb at %p\n", 846 (void *)so.so_pcb); 847 goto bad; 848 } 849 if (unpcb.unp_conn) { 850 char shoconn[4], *cp; 851 852 cp = shoconn; 853 if (!(so.so_state & SS_CANTRCVMORE)) 854 *cp++ = '<'; 855 *cp++ = '-'; 856 if (!(so.so_state & SS_CANTSENDMORE)) 857 *cp++ = '>'; 858 *cp = '\0'; 859 printf(" %s %lx", shoconn, 860 (u_long)unpcb.unp_conn); 861 } 862 } 863 break; 864 default: 865 /* print protocol number and socket address */ 866 printf(" %d %lx", proto.pr_protocol, (u_long)sock); 867 } 868 printf("\n"); 869 return; 870 bad: 871 printf("* error\n"); 872 } 873 874 875 /* 876 * Read the specinfo structure in the kernel (as pointed to by a dev_t) 877 * in order to work out the associated udev_t 878 */ 879 udev_t 880 dev2udev(dev) 881 dev_t dev; 882 { 883 struct specinfo si; 884 885 if (KVM_READ(dev, &si, sizeof si)) { 886 return si.si_udev; 887 } else { 888 dprintf(stderr, "can't convert dev_t %x to a udev_t\n", dev); 889 return -1; 890 } 891 } 892 893 /* 894 * getinetproto -- 895 * print name of protocol number 896 */ 897 void 898 getinetproto(number) 899 int number; 900 { 901 static int isopen; 902 struct protoent *pe; 903 904 if (!isopen) 905 setprotoent(++isopen); 906 if ((pe = getprotobynumber(number)) != NULL) 907 printf(" %s", pe->p_name); 908 else 909 printf(" %d", number); 910 } 911 912 int 913 getfname(filename) 914 const char *filename; 915 { 916 struct stat statbuf; 917 DEVS *cur; 918 919 if (stat(filename, &statbuf)) { 920 warn("%s", filename); 921 return(0); 922 } 923 if ((cur = malloc(sizeof(DEVS))) == NULL) 924 err(1, NULL); 925 cur->next = devs; 926 devs = cur; 927 928 cur->ino = statbuf.st_ino; 929 cur->fsid = statbuf.st_dev; 930 cur->name = filename; 931 return(1); 932 } 933 934 void 935 usage() 936 { 937 (void)fprintf(stderr, 938 "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n"); 939 exit(1); 940 } 941