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