1 /* $OpenBSD: fstat.c,v 1.31 2001/07/12 05:17:06 deraadt Exp $ */ 2 3 /*- 4 * Copyright (c) 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #ifndef lint 37 static char copyright[] = 38 "@(#) Copyright (c) 1988, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"; 40 #endif /* not lint */ 41 42 #ifndef lint 43 /*static char sccsid[] = "from: @(#)fstat.c 8.1 (Berkeley) 6/6/93";*/ 44 static char *rcsid = "$OpenBSD: fstat.c,v 1.31 2001/07/12 05:17:06 deraadt Exp $"; 45 #endif /* not lint */ 46 47 #include <sys/param.h> 48 #include <sys/time.h> 49 #include <sys/proc.h> 50 #include <sys/user.h> 51 #include <sys/stat.h> 52 #include <sys/vnode.h> 53 #include <sys/socket.h> 54 #include <sys/socketvar.h> 55 #include <sys/domain.h> 56 #include <sys/protosw.h> 57 #include <sys/unpcb.h> 58 #include <sys/sysctl.h> 59 #include <sys/filedesc.h> 60 #include <sys/mount.h> 61 #define _KERNEL 62 #include <sys/file.h> 63 #include <ufs/ufs/quota.h> 64 #include <ufs/ufs/inode.h> 65 #include <miscfs/nullfs/null.h> 66 #undef _KERNEL 67 #define NFS 68 #include <nfs/nfsproto.h> 69 #include <nfs/rpcv2.h> 70 #include <nfs/nfs.h> 71 #include <nfs/nfsnode.h> 72 #undef NFS 73 74 #include <xfs/xfs_config.h> 75 #include <xfs/xfs_node.h> 76 77 #include <net/route.h> 78 #include <netinet/in.h> 79 #include <netinet/in_systm.h> 80 #include <netinet/ip.h> 81 #include <netinet/in_pcb.h> 82 83 #ifdef INET6 84 #include <netinet/ip6.h> 85 #include <netinet6/ip6_var.h> 86 #endif 87 88 #include <netdb.h> 89 #include <arpa/inet.h> 90 91 #include <sys/pipe.h> 92 93 #include <ctype.h> 94 #include <errno.h> 95 #include <kvm.h> 96 #include <limits.h> 97 #include <nlist.h> 98 #include <paths.h> 99 #include <pwd.h> 100 #include <stdio.h> 101 #include <stdlib.h> 102 #include <string.h> 103 #include <unistd.h> 104 #include <netdb.h> 105 #include <err.h> 106 #include "fstat.h" 107 108 #define TEXT -1 109 #define CDIR -2 110 #define RDIR -3 111 #define TRACE -4 112 113 typedef struct devs { 114 struct devs *next; 115 long fsid; 116 ino_t ino; 117 char *name; 118 } DEVS; 119 DEVS *devs; 120 121 int fsflg, /* show files on same filesystem as file(s) argument */ 122 pflg, /* show files open by a particular pid */ 123 uflg; /* show files open by a particular (effective) user */ 124 int checkfile; /* true if restricting to particular files or filesystems */ 125 int nflg; /* (numerical) display f.s. and rdev as dev_t */ 126 int oflg; /* display file offset */ 127 int vflg; /* display errors in locating kernel data objects etc... */ 128 129 struct file **ofiles; /* buffer of pointers to file structures */ 130 int maxfiles; 131 #define ALLOC_OFILES(d) \ 132 if ((d) > maxfiles) { \ 133 free(ofiles); \ 134 ofiles = malloc((d) * sizeof(struct file *)); \ 135 if (ofiles == NULL) \ 136 err(1, "malloc"); \ 137 maxfiles = (d); \ 138 } 139 140 /* 141 * a kvm_read that returns true if everything is read 142 */ 143 #define KVM_READ(kaddr, paddr, len) \ 144 (kvm_read(kd, (u_long)(kaddr), (void *)(paddr), (len)) == (len)) 145 146 kvm_t *kd; 147 148 int ufs_filestat __P((struct vnode *, struct filestat *)); 149 int ext2fs_filestat __P((struct vnode *, struct filestat *)); 150 int isofs_filestat __P((struct vnode *, struct filestat *)); 151 int msdos_filestat __P((struct vnode *, struct filestat *)); 152 int nfs_filestat __P((struct vnode *, struct filestat *)); 153 int xfs_filestat __P((struct vnode *, struct filestat *)); 154 int null_filestat __P((struct vnode *, struct filestat *)); 155 void dofiles __P((struct kinfo_proc *)); 156 void getinetproto __P((int)); 157 void socktrans __P((struct socket *, int)); 158 void usage __P((void)); 159 void vtrans __P((struct vnode *, int, int, off_t)); 160 int getfname __P((char *)); 161 void pipetrans __P((struct pipe *, int)); 162 163 int 164 main(argc, argv) 165 int argc; 166 char **argv; 167 { 168 extern char *optarg; 169 extern int optind; 170 register struct passwd *passwd; 171 struct kinfo_proc *p, *plast; 172 int arg, ch, what; 173 char *memf, *nlistf; 174 char buf[_POSIX2_LINE_MAX]; 175 int cnt; 176 177 arg = 0; 178 what = KERN_PROC_ALL; 179 nlistf = memf = NULL; 180 oflg = 0; 181 while ((ch = getopt(argc, argv, "fnop:u:vN:M:")) != -1) 182 switch((char)ch) { 183 case 'f': 184 fsflg = 1; 185 break; 186 case 'M': 187 memf = optarg; 188 break; 189 case 'N': 190 nlistf = optarg; 191 break; 192 case 'n': 193 nflg = 1; 194 break; 195 case 'o': 196 oflg = 1; 197 break; 198 case 'p': 199 if (pflg++) 200 usage(); 201 if (!isdigit(*optarg)) { 202 warnx( "-p requires a process id\n"); 203 usage(); 204 } 205 what = KERN_PROC_PID; 206 arg = atoi(optarg); 207 break; 208 case 'u': 209 if (uflg++) 210 usage(); 211 if (!(passwd = getpwnam(optarg))) 212 errx(1, "%s: unknown uid", optarg); 213 what = KERN_PROC_UID; 214 arg = passwd->pw_uid; 215 break; 216 case 'v': 217 vflg = 1; 218 break; 219 case '?': 220 default: 221 usage(); 222 } 223 224 if (*(argv += optind)) { 225 for (; *argv; ++argv) { 226 if (getfname(*argv)) 227 checkfile = 1; 228 } 229 if (!checkfile) /* file(s) specified, but none accessible */ 230 exit(1); 231 } 232 233 ALLOC_OFILES(256); /* reserve space for file pointers */ 234 235 if (fsflg && !checkfile) { 236 /* -f with no files means use wd */ 237 if (getfname(".") == 0) 238 exit(1); 239 checkfile = 1; 240 } 241 242 /* 243 * Discard setgid privileges if not the running kernel so that bad 244 * guys can't print interesting stuff from kernel memory. 245 */ 246 if (nlistf != NULL || memf != NULL) { 247 setegid(getgid()); 248 setgid(getgid()); 249 } 250 251 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL) 252 errx(1, "%s", buf); 253 254 setegid(getgid()); 255 setgid(getgid()); 256 257 if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) 258 errx(1, "%s", kvm_geterr(kd)); 259 if (nflg) 260 printf("%s", 261 "USER CMD PID FD DEV INUM MODE R/W DV|SZ"); 262 else 263 printf("%s", 264 "USER CMD PID FD MOUNT INUM MODE R/W DV|SZ"); 265 if (oflg) 266 printf("%s", ":OFFSET "); 267 if (checkfile && fsflg == 0) 268 printf(" NAME\n"); 269 else 270 putchar('\n'); 271 272 for (plast = &p[cnt]; p < plast; ++p) { 273 if (p->kp_proc.p_stat == SZOMB) 274 continue; 275 dofiles(p); 276 } 277 exit(0); 278 } 279 280 char *Uname, *Comm; 281 pid_t Pid; 282 283 #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \ 284 switch(i) { \ 285 case TEXT: \ 286 printf(" text"); \ 287 break; \ 288 case CDIR: \ 289 printf(" wd"); \ 290 break; \ 291 case RDIR: \ 292 printf(" root"); \ 293 break; \ 294 case TRACE: \ 295 printf(" tr"); \ 296 break; \ 297 default: \ 298 printf(" %4d", i); \ 299 break; \ 300 } 301 302 /* 303 * print open files attributed to this process 304 */ 305 void 306 dofiles(kp) 307 struct kinfo_proc *kp; 308 { 309 int i; 310 struct file file; 311 struct filedesc0 filed0; 312 #define filed filed0.fd_fd 313 struct proc *p = &kp->kp_proc; 314 struct eproc *ep = &kp->kp_eproc; 315 316 extern char *user_from_uid(); 317 318 Uname = user_from_uid(ep->e_ucred.cr_uid, 0); 319 Pid = p->p_pid; 320 Comm = p->p_comm; 321 322 if (p->p_fd == NULL) 323 return; 324 if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) { 325 dprintf("can't read filedesc at %p for pid %d", p->p_fd, Pid); 326 return; 327 } 328 if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles || 329 filed.fd_freefile > filed.fd_lastfile + 1) { 330 dprintf("filedesc corrupted at %p for pid %d", p->p_fd, Pid); 331 return; 332 } 333 /* 334 * root directory vnode, if one 335 */ 336 if (filed.fd_rdir) 337 vtrans(filed.fd_rdir, RDIR, FREAD, 0); 338 /* 339 * current working directory vnode 340 */ 341 vtrans(filed.fd_cdir, CDIR, FREAD, 0); 342 /* 343 * ktrace vnode, if one 344 */ 345 if (p->p_tracep) 346 vtrans(p->p_tracep, TRACE, FREAD|FWRITE, 0); 347 /* 348 * open files 349 */ 350 #define FPSIZE (sizeof (struct file *)) 351 ALLOC_OFILES(filed.fd_lastfile+1); 352 if (filed.fd_nfiles > NDFILE) { 353 if (!KVM_READ(filed.fd_ofiles, ofiles, 354 (filed.fd_lastfile+1) * FPSIZE)) { 355 dprintf("can't read file structures at %p for pid %d", 356 filed.fd_ofiles, Pid); 357 return; 358 } 359 } else 360 bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE); 361 for (i = 0; i <= filed.fd_lastfile; i++) { 362 if (ofiles[i] == NULL) 363 continue; 364 if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) { 365 dprintf("can't read file %d at %p for pid %d", 366 i, ofiles[i], Pid); 367 continue; 368 } 369 if (file.f_type == DTYPE_VNODE) 370 vtrans((struct vnode *)file.f_data, i, file.f_flag, file.f_offset); 371 else if (file.f_type == DTYPE_SOCKET) { 372 if (checkfile == 0) 373 socktrans((struct socket *)file.f_data, i); 374 } else if (file.f_type == DTYPE_PIPE) { 375 if (checkfile == 0) 376 pipetrans((struct pipe *)file.f_data, i); 377 } else { 378 dprintf("unknown file type %d for file %d of pid %d", 379 file.f_type, i, Pid); 380 } 381 } 382 } 383 384 void 385 vtrans(vp, i, flag, offset) 386 struct vnode *vp; 387 int i; 388 int flag; 389 off_t offset; 390 { 391 struct vnode vn; 392 struct filestat fst; 393 char rw[3], mode[17]; 394 char *badtype = NULL, *filename, *getmnton(); 395 396 filename = badtype = NULL; 397 if (!KVM_READ(vp, &vn, sizeof (struct vnode))) { 398 dprintf("can't read vnode at %p for pid %d", vp, Pid); 399 return; 400 } 401 if (vn.v_type == VNON || vn.v_tag == VT_NON) 402 badtype = "none"; 403 else if (vn.v_type == VBAD) 404 badtype = "bad"; 405 else 406 switch (vn.v_tag) { 407 case VT_UFS: 408 case VT_MFS: 409 if (!ufs_filestat(&vn, &fst)) 410 badtype = "error"; 411 break; 412 case VT_NFS: 413 if (!nfs_filestat(&vn, &fst)) 414 badtype = "error"; 415 break; 416 case VT_EXT2FS: 417 if (!ext2fs_filestat(&vn, &fst)) 418 badtype = "error"; 419 break; 420 case VT_ISOFS: 421 if (!isofs_filestat(&vn, &fst)) 422 badtype = "error"; 423 break; 424 case VT_MSDOSFS: 425 if (!msdos_filestat(&vn, &fst)) 426 badtype = "error"; 427 break; 428 case VT_XFS: 429 if (!xfs_filestat(&vn, &fst)) 430 badtype = "error"; 431 break; 432 case VT_NULL: 433 if (!null_filestat(&vn, &fst)) 434 badtype = "error"; 435 break; 436 default: { 437 static char unknown[30]; 438 sprintf(badtype = unknown, "?(%x)", vn.v_tag); 439 break; 440 } 441 } 442 if (checkfile) { 443 int fsmatch = 0; 444 register DEVS *d; 445 446 if (badtype) 447 return; 448 for (d = devs; d != NULL; d = d->next) 449 if (d->fsid == fst.fsid) { 450 fsmatch = 1; 451 if (d->ino == fst.fileid) { 452 filename = d->name; 453 break; 454 } 455 } 456 if (fsmatch == 0 || (filename == NULL && fsflg == 0)) 457 return; 458 } 459 PREFIX(i); 460 if (badtype) { 461 (void)printf(" - - %10s -\n", badtype); 462 return; 463 } 464 if (nflg) 465 (void)printf(" %2ld,%-2ld", (long)major(fst.fsid), 466 (long)minor(fst.fsid)); 467 else 468 (void)printf(" %-8s", getmnton(vn.v_mount)); 469 if (nflg) 470 (void)sprintf(mode, "%o", fst.mode); 471 else 472 strmode(fst.mode, mode); 473 (void)printf(" %6ld %11s", fst.fileid, mode); 474 rw[0] = '\0'; 475 if (flag & FREAD) 476 strcat(rw, "r"); 477 if (flag & FWRITE) 478 strcat(rw, "w"); 479 printf(" %2s", rw); 480 switch (vn.v_type) { 481 case VBLK: 482 case VCHR: { 483 char *name; 484 485 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ? 486 S_IFCHR : S_IFBLK)) == NULL)) 487 printf(" %2d,%-3d", major(fst.rdev), minor(fst.rdev)); 488 else 489 printf(" %7s", name); 490 if (oflg) 491 printf(" "); 492 break; 493 } 494 default: 495 printf(" %8lld", (long long)fst.size); 496 if (oflg) 497 printf(":%-8lld", (long long)offset); 498 } 499 if (filename && !fsflg) 500 printf(" %s", filename); 501 putchar('\n'); 502 } 503 504 int 505 ufs_filestat(vp, fsp) 506 struct vnode *vp; 507 struct filestat *fsp; 508 { 509 struct inode inode; 510 511 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 512 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid); 513 return 0; 514 } 515 fsp->fsid = inode.i_dev & 0xffff; 516 fsp->fileid = (long)inode.i_number; 517 fsp->mode = inode.i_ffs_mode; 518 fsp->size = inode.i_ffs_size; 519 fsp->rdev = inode.i_ffs_rdev; 520 521 return 1; 522 } 523 524 int 525 ext2fs_filestat(vp, fsp) 526 struct vnode *vp; 527 struct filestat *fsp; 528 { 529 struct inode inode; 530 531 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 532 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid); 533 return 0; 534 } 535 fsp->fsid = inode.i_dev & 0xffff; 536 fsp->fileid = (long)inode.i_number; 537 fsp->mode = inode.i_e2fs_mode; 538 fsp->size = inode.i_e2fs_size; 539 fsp->rdev = 0; /* XXX */ 540 541 return 1; 542 } 543 544 int 545 msdos_filestat(vp, fsp) 546 struct vnode *vp; 547 struct filestat *fsp; 548 { 549 #if 0 550 struct inode inode; 551 552 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 553 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid); 554 return 0; 555 } 556 fsp->fsid = inode.i_dev & 0xffff; 557 fsp->fileid = (long)inode.i_number; 558 fsp->mode = inode.i_e2fs_mode; 559 fsp->size = inode.i_e2fs_size; 560 fsp->rdev = 0; /* XXX */ 561 #endif 562 563 return 1; 564 } 565 566 int 567 nfs_filestat(vp, fsp) 568 struct vnode *vp; 569 struct filestat *fsp; 570 { 571 struct nfsnode nfsnode; 572 register mode_t mode; 573 574 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) { 575 dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp), Pid); 576 return 0; 577 } 578 fsp->fsid = nfsnode.n_vattr.va_fsid; 579 fsp->fileid = nfsnode.n_vattr.va_fileid; 580 fsp->size = nfsnode.n_size; 581 fsp->rdev = nfsnode.n_vattr.va_rdev; 582 mode = (mode_t)nfsnode.n_vattr.va_mode; 583 switch (vp->v_type) { 584 case VREG: 585 mode |= S_IFREG; 586 break; 587 case VDIR: 588 mode |= S_IFDIR; 589 break; 590 case VBLK: 591 mode |= S_IFBLK; 592 break; 593 case VCHR: 594 mode |= S_IFCHR; 595 break; 596 case VLNK: 597 mode |= S_IFLNK; 598 break; 599 case VSOCK: 600 mode |= S_IFSOCK; 601 break; 602 case VFIFO: 603 mode |= S_IFIFO; 604 break; 605 default: 606 break; 607 }; 608 fsp->mode = mode; 609 610 return 1; 611 } 612 613 int 614 xfs_filestat(vp, fsp) 615 struct vnode *vp; 616 struct filestat *fsp; 617 { 618 struct xfs_node xfs_node; 619 620 if (!KVM_READ(VNODE_TO_XNODE(vp), &xfs_node, sizeof (xfs_node))) { 621 dprintf("can't read xfs_node at %p for pid %d", VTOI(vp), Pid); 622 return 0; 623 } 624 fsp->fsid = xfs_node.attr.va_fsid; 625 fsp->fileid = (long)xfs_node.attr.va_fileid; 626 fsp->mode = xfs_node.attr.va_mode; 627 fsp->size = xfs_node.attr.va_size; 628 fsp->rdev = xfs_node.attr.va_rdev; 629 630 return 1; 631 } 632 633 int 634 null_filestat(vp, fsp) 635 struct vnode *vp; 636 struct filestat *fsp; 637 { 638 struct null_node node; 639 struct filestat fst; 640 struct vnode vn; 641 int fail = 1; 642 643 memset(&fst, 0, sizeof fst); 644 645 if (!KVM_READ(VTONULL(vp), &node, sizeof (node))) { 646 dprintf("can't read node at %p for pid %d", VTONULL(vp), Pid); 647 return 0; 648 } 649 650 /* 651 * Attempt to find information that might be useful. 652 */ 653 if (node.null_lowervp) { 654 if (!KVM_READ(node.null_lowervp, &vn, sizeof (vn))) { 655 dprintf("can't read vnode at %p for pid %d", 656 node.null_lowervp, Pid); 657 return 0; 658 } 659 660 fail = 0; 661 if (vn.v_type == VNON || vn.v_tag == VT_NON) 662 fail = 1; 663 else if (vn.v_type == VBAD) 664 fail = 1; 665 else 666 switch (vn.v_tag) { 667 case VT_UFS: 668 case VT_MFS: 669 if (!ufs_filestat(&vn, &fst)) 670 fail = 1; 671 break; 672 case VT_NFS: 673 if (!nfs_filestat(&vn, &fst)) 674 fail = 1; 675 break; 676 case VT_EXT2FS: 677 if (!ext2fs_filestat(&vn, &fst)) 678 fail = 1; 679 break; 680 case VT_ISOFS: 681 if (!isofs_filestat(&vn, &fst)) 682 fail = 1; 683 break; 684 case VT_MSDOSFS: 685 if (!msdos_filestat(&vn, &fst)) 686 fail = 1; 687 break; 688 case VT_XFS: 689 if (!xfs_filestat(&vn, &fst)) 690 fail = 1; 691 break; 692 default: 693 break; 694 } 695 } 696 697 fsp->fsid = (long)node.null_vnode; 698 if (fail) 699 fsp->fileid = (long)node.null_lowervp; 700 else 701 fsp->fileid = fst.fileid; 702 fsp->mode = fst.mode; 703 fsp->size = fst.mode; 704 fsp->rdev = fst.mode; 705 706 return 1; 707 } 708 709 char * 710 getmnton(m) 711 struct mount *m; 712 { 713 static struct mount mount; 714 static struct mtab { 715 struct mtab *next; 716 struct mount *m; 717 char mntonname[MNAMELEN]; 718 } *mhead = NULL; 719 register struct mtab *mt; 720 721 for (mt = mhead; mt != NULL; mt = mt->next) 722 if (m == mt->m) 723 return (mt->mntonname); 724 if (!KVM_READ(m, &mount, sizeof(struct mount))) { 725 warn("can't read mount table at %p", m); 726 return (NULL); 727 } 728 if ((mt = malloc(sizeof (struct mtab))) == NULL) 729 err(1, "malloc"); 730 mt->m = m; 731 bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN); 732 mt->next = mhead; 733 mhead = mt; 734 return (mt->mntonname); 735 } 736 737 void 738 pipetrans(pipe, i) 739 struct pipe *pipe; 740 int i; 741 { 742 struct pipe pi; 743 void *maxaddr; 744 745 PREFIX(i); 746 747 printf(" "); 748 749 /* fill in socket */ 750 if (!KVM_READ(pipe, &pi, sizeof(struct pipe))) { 751 dprintf("can't read pipe at %p", pipe); 752 goto bad; 753 } 754 755 /* 756 * We don't have enough space to fit both peer and own address, so 757 * we select the higher address so both ends of the pipe have the 758 * same visible addr. (it's the higher address because when the other 759 * end closes, it becomes 0) 760 */ 761 maxaddr = MAX(pipe, pi.pipe_peer); 762 763 printf("pipe %p state: %s%s%s", maxaddr, 764 (pi.pipe_state & PIPE_WANTR) ? "R" : "", 765 (pi.pipe_state & PIPE_WANTW) ? "W" : "", 766 (pi.pipe_state & PIPE_EOF) ? "E" : ""); 767 768 printf("\n"); 769 return; 770 bad: 771 printf("* error\n"); 772 } 773 774 #ifdef INET6 775 const char * 776 inet6_addrstr(p) 777 struct in6_addr *p; 778 { 779 struct sockaddr_in6 sin6; 780 static char hbuf[NI_MAXHOST]; 781 #ifdef NI_WITHSCOPEID 782 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; 783 #else 784 const int niflags = NI_NUMERICHOST 785 #endif 786 787 memset(&sin6, 0, sizeof(sin6)); 788 sin6.sin6_family = AF_INET6; 789 sin6.sin6_len = sizeof(struct sockaddr_in6); 790 sin6.sin6_addr = *p; 791 if (IN6_IS_ADDR_LINKLOCAL(p) && 792 *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) { 793 sin6.sin6_scope_id = 794 ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]); 795 sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0; 796 } 797 798 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, 799 hbuf, sizeof(hbuf), NULL, 0, niflags)) 800 return "invalid"; 801 802 return hbuf; 803 } 804 #endif 805 806 void 807 socktrans(sock, i) 808 struct socket *sock; 809 int i; 810 { 811 static char *stypename[] = { 812 "unused", /* 0 */ 813 "stream", /* 1 */ 814 "dgram", /* 2 */ 815 "raw", /* 3 */ 816 "rdm", /* 4 */ 817 "seqpak" /* 5 */ 818 }; 819 #define STYPEMAX 5 820 struct socket so; 821 struct protosw proto; 822 struct domain dom; 823 struct inpcb inpcb; 824 struct unpcb unpcb; 825 int len; 826 char dname[32]; 827 #ifdef INET6 828 char xaddrbuf[NI_MAXHOST + 2]; 829 #endif 830 831 PREFIX(i); 832 833 /* fill in socket */ 834 if (!KVM_READ(sock, &so, sizeof(struct socket))) { 835 dprintf("can't read sock at %p", sock); 836 goto bad; 837 } 838 839 /* fill in protosw entry */ 840 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) { 841 dprintf("can't read protosw at %p", so.so_proto); 842 goto bad; 843 } 844 845 /* fill in domain */ 846 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) { 847 dprintf("can't read domain at %p", proto.pr_domain); 848 goto bad; 849 } 850 851 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname, 852 sizeof(dname) - 1)) != sizeof(dname) -1) { 853 dprintf("can't read domain name at %p", dom.dom_name); 854 dname[0] = '\0'; 855 } 856 else 857 dname[len] = '\0'; 858 859 if ((u_short)so.so_type > STYPEMAX) 860 printf("* %s ?%d", dname, so.so_type); 861 else 862 printf("* %s %s", dname, stypename[so.so_type]); 863 864 /* 865 * protocol specific formatting 866 * 867 * Try to find interesting things to print. For tcp, the interesting 868 * thing is the address of the tcpcb, for udp and others, just the 869 * inpcb (socket pcb). For unix domain, its the address of the socket 870 * pcb and the address of the connected pcb (if connected). Otherwise 871 * just print the protocol number and address of the socket itself. 872 * The idea is not to duplicate netstat, but to make available enough 873 * information for further analysis. 874 */ 875 switch(dom.dom_family) { 876 case AF_INET: 877 getinetproto(proto.pr_protocol); 878 if (proto.pr_protocol == IPPROTO_TCP) { 879 if (so.so_pcb == NULL) 880 break; 881 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb, 882 sizeof(struct inpcb)) != sizeof(struct inpcb)) { 883 dprintf("can't read inpcb at %p", so.so_pcb); 884 goto bad; 885 } 886 printf(" %p", inpcb.inp_ppcb); 887 printf(" %s:%d", 888 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" : 889 inet_ntoa(inpcb.inp_laddr), 890 ntohs(inpcb.inp_lport)); 891 if (inpcb.inp_fport) { 892 if (so.so_state & SS_CONNECTOUT) 893 printf(" --> "); 894 else 895 printf(" <-- "); 896 printf("%s:%d", 897 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" : 898 inet_ntoa(inpcb.inp_faddr), 899 ntohs(inpcb.inp_fport)); 900 } 901 } else if (proto.pr_protocol == IPPROTO_UDP) { 902 if (so.so_pcb == NULL) 903 break; 904 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb, 905 sizeof(struct inpcb)) != sizeof(struct inpcb)) { 906 dprintf("can't read inpcb at %p", so.so_pcb); 907 goto bad; 908 } 909 printf(" %s:%d", 910 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" : 911 inet_ntoa(inpcb.inp_laddr), 912 ntohs(inpcb.inp_lport)); 913 if (inpcb.inp_fport) 914 printf(" <-> %s:%d", 915 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" : 916 inet_ntoa(inpcb.inp_faddr), 917 ntohs(inpcb.inp_fport)); 918 } else if (so.so_pcb) 919 printf(" %p", so.so_pcb); 920 break; 921 #ifdef INET6 922 case AF_INET6: 923 getinetproto(proto.pr_protocol); 924 if (proto.pr_protocol == IPPROTO_TCP) { 925 if (so.so_pcb == NULL) 926 break; 927 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb, 928 sizeof(struct inpcb)) != sizeof(struct inpcb)) { 929 dprintf("can't read inpcb at %p", so.so_pcb); 930 goto bad; 931 } 932 printf(" %p", inpcb.inp_ppcb); 933 snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]", 934 inet6_addrstr(&inpcb.inp_laddr6)); 935 printf(" %s:%d", 936 IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6) ? "*" : 937 xaddrbuf, 938 ntohs(inpcb.inp_lport)); 939 if (inpcb.inp_fport) { 940 if (so.so_state & SS_CONNECTOUT) 941 printf(" --> "); 942 else 943 printf(" <-- "); 944 snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]", 945 inet6_addrstr(&inpcb.inp_faddr6)); 946 printf("%s:%d", 947 IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6) ? "*" : 948 xaddrbuf, 949 ntohs(inpcb.inp_fport)); 950 } 951 } else if (proto.pr_protocol == IPPROTO_UDP) { 952 if (so.so_pcb == NULL) 953 break; 954 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb, 955 sizeof(struct inpcb)) != sizeof(struct inpcb)) { 956 dprintf("can't read inpcb at %p", so.so_pcb); 957 goto bad; 958 } 959 snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]", 960 inet6_addrstr(&inpcb.inp_laddr6)); 961 printf(" %s:%d", 962 IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6) ? "*" : 963 xaddrbuf, 964 ntohs(inpcb.inp_lport)); 965 if (inpcb.inp_fport) 966 snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]", 967 inet6_addrstr(&inpcb.inp_faddr6)); 968 printf(" <-> %s:%d", 969 IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6) ? "*" : 970 xaddrbuf, 971 ntohs(inpcb.inp_fport)); 972 } else if (so.so_pcb) 973 printf(" %p", so.so_pcb); 974 break; 975 #endif 976 case AF_UNIX: 977 /* print address of pcb and connected pcb */ 978 if (so.so_pcb) { 979 printf(" %p", so.so_pcb); 980 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb, 981 sizeof(struct unpcb)) != sizeof(struct unpcb)){ 982 dprintf("can't read unpcb at %p", so.so_pcb); 983 goto bad; 984 } 985 if (unpcb.unp_conn) { 986 char shoconn[4], *cp; 987 988 cp = shoconn; 989 if (!(so.so_state & SS_CANTRCVMORE)) 990 *cp++ = '<'; 991 *cp++ = '-'; 992 if (!(so.so_state & SS_CANTSENDMORE)) 993 *cp++ = '>'; 994 *cp = '\0'; 995 printf(" %s %p", shoconn, 996 unpcb.unp_conn); 997 } 998 } 999 break; 1000 default: 1001 /* print protocol number and socket address */ 1002 printf(" %d %p", proto.pr_protocol, sock); 1003 } 1004 printf("\n"); 1005 return; 1006 bad: 1007 printf("* error\n"); 1008 } 1009 1010 /* 1011 * getinetproto -- 1012 * print name of protocol number 1013 */ 1014 void 1015 getinetproto(number) 1016 int number; 1017 { 1018 static int isopen; 1019 register struct protoent *pe; 1020 1021 if (!isopen) 1022 setprotoent(++isopen); 1023 if ((pe = getprotobynumber(number)) != NULL) 1024 printf(" %s", pe->p_name); 1025 else 1026 printf(" %d", number); 1027 } 1028 1029 int 1030 getfname(filename) 1031 char *filename; 1032 { 1033 struct stat statbuf; 1034 DEVS *cur; 1035 1036 if (stat(filename, &statbuf)) { 1037 warn("%s", filename); 1038 return(0); 1039 } 1040 if ((cur = malloc(sizeof(DEVS))) == NULL) 1041 err(1, "malloc"); 1042 cur->next = devs; 1043 devs = cur; 1044 1045 cur->ino = statbuf.st_ino; 1046 cur->fsid = statbuf.st_dev & 0xffff; 1047 cur->name = filename; 1048 return(1); 1049 } 1050 1051 void 1052 usage() 1053 { 1054 (void)fprintf(stderr, 1055 "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n"); 1056 exit(1); 1057 } 1058