1 /* $OpenBSD: fstat.c,v 1.34 2001/12/01 18:59:59 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.34 2001/12/01 18:59:59 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 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) do { \ 284 printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \ 285 switch(i) { \ 286 case TEXT: \ 287 printf(" text"); \ 288 break; \ 289 case CDIR: \ 290 printf(" wd"); \ 291 break; \ 292 case RDIR: \ 293 printf(" root"); \ 294 break; \ 295 case TRACE: \ 296 printf(" tr"); \ 297 break; \ 298 default: \ 299 printf(" %4d", i); \ 300 break; \ 301 } \ 302 } while (0) 303 304 /* 305 * print open files attributed to this process 306 */ 307 void 308 dofiles(kp) 309 struct kinfo_proc *kp; 310 { 311 int i; 312 struct file file; 313 struct filedesc0 filed0; 314 #define filed filed0.fd_fd 315 struct proc *p = &kp->kp_proc; 316 struct eproc *ep = &kp->kp_eproc; 317 318 extern char *user_from_uid(); 319 320 Uname = user_from_uid(ep->e_ucred.cr_uid, 0); 321 Pid = p->p_pid; 322 Comm = p->p_comm; 323 324 if (p->p_fd == NULL) 325 return; 326 if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) { 327 dprintf("can't read filedesc at %p for pid %d", p->p_fd, Pid); 328 return; 329 } 330 if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles || 331 filed.fd_freefile > filed.fd_lastfile + 1) { 332 dprintf("filedesc corrupted at %p for pid %d", p->p_fd, Pid); 333 return; 334 } 335 /* 336 * root directory vnode, if one 337 */ 338 if (filed.fd_rdir) 339 vtrans(filed.fd_rdir, RDIR, FREAD, 0); 340 /* 341 * current working directory vnode 342 */ 343 vtrans(filed.fd_cdir, CDIR, FREAD, 0); 344 /* 345 * ktrace vnode, if one 346 */ 347 if (p->p_tracep) 348 vtrans(p->p_tracep, TRACE, FREAD|FWRITE, 0); 349 /* 350 * open files 351 */ 352 #define FPSIZE (sizeof (struct file *)) 353 ALLOC_OFILES(filed.fd_lastfile+1); 354 if (filed.fd_nfiles > NDFILE) { 355 if (!KVM_READ(filed.fd_ofiles, ofiles, 356 (filed.fd_lastfile+1) * FPSIZE)) { 357 dprintf("can't read file structures at %p for pid %d", 358 filed.fd_ofiles, Pid); 359 return; 360 } 361 } else 362 bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE); 363 for (i = 0; i <= filed.fd_lastfile; i++) { 364 if (ofiles[i] == NULL) 365 continue; 366 if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) { 367 dprintf("can't read file %d at %p for pid %d", 368 i, ofiles[i], Pid); 369 continue; 370 } 371 if (file.f_type == DTYPE_VNODE) 372 vtrans((struct vnode *)file.f_data, i, file.f_flag, file.f_offset); 373 else if (file.f_type == DTYPE_SOCKET) { 374 if (checkfile == 0) 375 socktrans((struct socket *)file.f_data, i); 376 } else if (file.f_type == DTYPE_PIPE) { 377 if (checkfile == 0) 378 pipetrans((struct pipe *)file.f_data, i); 379 } else { 380 dprintf("unknown file type %d for file %d of pid %d", 381 file.f_type, i, Pid); 382 } 383 } 384 } 385 386 void 387 vtrans(vp, i, flag, offset) 388 struct vnode *vp; 389 int i; 390 int flag; 391 off_t offset; 392 { 393 struct vnode vn; 394 struct filestat fst; 395 char rw[3], mode[17]; 396 char *badtype = NULL, *filename, *getmnton(); 397 398 filename = badtype = NULL; 399 if (!KVM_READ(vp, &vn, sizeof (struct vnode))) { 400 dprintf("can't read vnode at %p for pid %d", vp, Pid); 401 return; 402 } 403 if (vn.v_type == VNON || vn.v_tag == VT_NON) 404 badtype = "none"; 405 else if (vn.v_type == VBAD) 406 badtype = "bad"; 407 else 408 switch (vn.v_tag) { 409 case VT_UFS: 410 case VT_MFS: 411 if (!ufs_filestat(&vn, &fst)) 412 badtype = "error"; 413 break; 414 case VT_NFS: 415 if (!nfs_filestat(&vn, &fst)) 416 badtype = "error"; 417 break; 418 case VT_EXT2FS: 419 if (!ext2fs_filestat(&vn, &fst)) 420 badtype = "error"; 421 break; 422 case VT_ISOFS: 423 if (!isofs_filestat(&vn, &fst)) 424 badtype = "error"; 425 break; 426 case VT_MSDOSFS: 427 if (!msdos_filestat(&vn, &fst)) 428 badtype = "error"; 429 break; 430 case VT_XFS: 431 if (!xfs_filestat(&vn, &fst)) 432 badtype = "error"; 433 break; 434 case VT_NULL: 435 if (!null_filestat(&vn, &fst)) 436 badtype = "error"; 437 break; 438 default: { 439 static char unknown[30]; 440 snprintf(badtype = unknown, sizeof unknown, 441 "?(%x)", vn.v_tag); 442 break; 443 } 444 } 445 if (checkfile) { 446 int fsmatch = 0; 447 DEVS *d; 448 449 if (badtype) 450 return; 451 for (d = devs; d != NULL; d = d->next) 452 if (d->fsid == fst.fsid) { 453 fsmatch = 1; 454 if (d->ino == fst.fileid) { 455 filename = d->name; 456 break; 457 } 458 } 459 if (fsmatch == 0 || (filename == NULL && fsflg == 0)) 460 return; 461 } 462 PREFIX(i); 463 if (badtype) { 464 (void)printf(" - - %10s -\n", badtype); 465 return; 466 } 467 if (nflg) 468 (void)printf(" %2ld,%-2ld", (long)major(fst.fsid), 469 (long)minor(fst.fsid)); 470 else 471 (void)printf(" %-8s", getmnton(vn.v_mount)); 472 if (nflg) 473 (void)snprintf(mode, sizeof mode, "%o", fst.mode); 474 else 475 strmode(fst.mode, mode); 476 (void)printf(" %6ld %11s", fst.fileid, mode); 477 rw[0] = '\0'; 478 if (flag & FREAD) 479 strlcat(rw, "r", sizeof rw); 480 if (flag & FWRITE) 481 strlcat(rw, "w", sizeof rw); 482 printf(" %2s", rw); 483 switch (vn.v_type) { 484 case VBLK: 485 case VCHR: { 486 char *name; 487 488 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ? 489 S_IFCHR : S_IFBLK)) == NULL)) 490 printf(" %2d,%-3d", major(fst.rdev), minor(fst.rdev)); 491 else 492 printf(" %7s", name); 493 if (oflg) 494 printf(" "); 495 break; 496 } 497 default: 498 printf(" %8lld", (long long)fst.size); 499 if (oflg) 500 printf(":%-8lld", (long long)offset); 501 } 502 if (filename && !fsflg) 503 printf(" %s", filename); 504 putchar('\n'); 505 } 506 507 int 508 ufs_filestat(vp, fsp) 509 struct vnode *vp; 510 struct filestat *fsp; 511 { 512 struct inode inode; 513 514 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 515 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid); 516 return 0; 517 } 518 fsp->fsid = inode.i_dev & 0xffff; 519 fsp->fileid = (long)inode.i_number; 520 fsp->mode = inode.i_ffs_mode; 521 fsp->size = inode.i_ffs_size; 522 fsp->rdev = inode.i_ffs_rdev; 523 524 return 1; 525 } 526 527 int 528 ext2fs_filestat(vp, fsp) 529 struct vnode *vp; 530 struct filestat *fsp; 531 { 532 struct inode inode; 533 534 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 535 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid); 536 return 0; 537 } 538 fsp->fsid = inode.i_dev & 0xffff; 539 fsp->fileid = (long)inode.i_number; 540 fsp->mode = inode.i_e2fs_mode; 541 fsp->size = inode.i_e2fs_size; 542 fsp->rdev = 0; /* XXX */ 543 544 return 1; 545 } 546 547 int 548 msdos_filestat(vp, fsp) 549 struct vnode *vp; 550 struct filestat *fsp; 551 { 552 #if 0 553 struct inode inode; 554 555 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 556 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid); 557 return 0; 558 } 559 fsp->fsid = inode.i_dev & 0xffff; 560 fsp->fileid = (long)inode.i_number; 561 fsp->mode = inode.i_e2fs_mode; 562 fsp->size = inode.i_e2fs_size; 563 fsp->rdev = 0; /* XXX */ 564 #endif 565 566 return 1; 567 } 568 569 int 570 nfs_filestat(vp, fsp) 571 struct vnode *vp; 572 struct filestat *fsp; 573 { 574 struct nfsnode nfsnode; 575 mode_t mode; 576 577 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) { 578 dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp), Pid); 579 return 0; 580 } 581 fsp->fsid = nfsnode.n_vattr.va_fsid; 582 fsp->fileid = nfsnode.n_vattr.va_fileid; 583 fsp->size = nfsnode.n_size; 584 fsp->rdev = nfsnode.n_vattr.va_rdev; 585 mode = (mode_t)nfsnode.n_vattr.va_mode; 586 switch (vp->v_type) { 587 case VREG: 588 mode |= S_IFREG; 589 break; 590 case VDIR: 591 mode |= S_IFDIR; 592 break; 593 case VBLK: 594 mode |= S_IFBLK; 595 break; 596 case VCHR: 597 mode |= S_IFCHR; 598 break; 599 case VLNK: 600 mode |= S_IFLNK; 601 break; 602 case VSOCK: 603 mode |= S_IFSOCK; 604 break; 605 case VFIFO: 606 mode |= S_IFIFO; 607 break; 608 default: 609 break; 610 } 611 fsp->mode = mode; 612 613 return 1; 614 } 615 616 int 617 xfs_filestat(vp, fsp) 618 struct vnode *vp; 619 struct filestat *fsp; 620 { 621 struct xfs_node xfs_node; 622 623 if (!KVM_READ(VNODE_TO_XNODE(vp), &xfs_node, sizeof (xfs_node))) { 624 dprintf("can't read xfs_node at %p for pid %d", VTOI(vp), Pid); 625 return 0; 626 } 627 fsp->fsid = xfs_node.attr.va_fsid; 628 fsp->fileid = (long)xfs_node.attr.va_fileid; 629 fsp->mode = xfs_node.attr.va_mode; 630 fsp->size = xfs_node.attr.va_size; 631 fsp->rdev = xfs_node.attr.va_rdev; 632 633 return 1; 634 } 635 636 int 637 null_filestat(vp, fsp) 638 struct vnode *vp; 639 struct filestat *fsp; 640 { 641 struct null_node node; 642 struct filestat fst; 643 struct vnode vn; 644 int fail = 1; 645 646 memset(&fst, 0, sizeof fst); 647 648 if (!KVM_READ(VTONULL(vp), &node, sizeof (node))) { 649 dprintf("can't read node at %p for pid %d", VTONULL(vp), Pid); 650 return 0; 651 } 652 653 /* 654 * Attempt to find information that might be useful. 655 */ 656 if (node.null_lowervp) { 657 if (!KVM_READ(node.null_lowervp, &vn, sizeof (vn))) { 658 dprintf("can't read vnode at %p for pid %d", 659 node.null_lowervp, Pid); 660 return 0; 661 } 662 663 fail = 0; 664 if (vn.v_type == VNON || vn.v_tag == VT_NON) 665 fail = 1; 666 else if (vn.v_type == VBAD) 667 fail = 1; 668 else 669 switch (vn.v_tag) { 670 case VT_UFS: 671 case VT_MFS: 672 if (!ufs_filestat(&vn, &fst)) 673 fail = 1; 674 break; 675 case VT_NFS: 676 if (!nfs_filestat(&vn, &fst)) 677 fail = 1; 678 break; 679 case VT_EXT2FS: 680 if (!ext2fs_filestat(&vn, &fst)) 681 fail = 1; 682 break; 683 case VT_ISOFS: 684 if (!isofs_filestat(&vn, &fst)) 685 fail = 1; 686 break; 687 case VT_MSDOSFS: 688 if (!msdos_filestat(&vn, &fst)) 689 fail = 1; 690 break; 691 case VT_XFS: 692 if (!xfs_filestat(&vn, &fst)) 693 fail = 1; 694 break; 695 default: 696 break; 697 } 698 } 699 700 fsp->fsid = (long)node.null_vnode; 701 if (fail) 702 fsp->fileid = (long)node.null_lowervp; 703 else 704 fsp->fileid = fst.fileid; 705 fsp->mode = fst.mode; 706 fsp->size = fst.mode; 707 fsp->rdev = fst.mode; 708 709 return 1; 710 } 711 712 char * 713 getmnton(m) 714 struct mount *m; 715 { 716 static struct mount mount; 717 static struct mtab { 718 struct mtab *next; 719 struct mount *m; 720 char mntonname[MNAMELEN]; 721 } *mhead = NULL; 722 struct mtab *mt; 723 724 for (mt = mhead; mt != NULL; mt = mt->next) 725 if (m == mt->m) 726 return (mt->mntonname); 727 if (!KVM_READ(m, &mount, sizeof(struct mount))) { 728 warn("can't read mount table at %p", m); 729 return (NULL); 730 } 731 if ((mt = malloc(sizeof (struct mtab))) == NULL) 732 err(1, "malloc"); 733 mt->m = m; 734 bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN); 735 mt->next = mhead; 736 mhead = mt; 737 return (mt->mntonname); 738 } 739 740 void 741 pipetrans(pipe, i) 742 struct pipe *pipe; 743 int i; 744 { 745 struct pipe pi; 746 void *maxaddr; 747 748 PREFIX(i); 749 750 printf(" "); 751 752 /* fill in socket */ 753 if (!KVM_READ(pipe, &pi, sizeof(struct pipe))) { 754 dprintf("can't read pipe at %p", pipe); 755 goto bad; 756 } 757 758 /* 759 * We don't have enough space to fit both peer and own address, so 760 * we select the higher address so both ends of the pipe have the 761 * same visible addr. (it's the higher address because when the other 762 * end closes, it becomes 0) 763 */ 764 maxaddr = MAX(pipe, pi.pipe_peer); 765 766 printf("pipe %p state: %s%s%s", maxaddr, 767 (pi.pipe_state & PIPE_WANTR) ? "R" : "", 768 (pi.pipe_state & PIPE_WANTW) ? "W" : "", 769 (pi.pipe_state & PIPE_EOF) ? "E" : ""); 770 771 printf("\n"); 772 return; 773 bad: 774 printf("* error\n"); 775 } 776 777 #ifdef INET6 778 const char * 779 inet6_addrstr(p) 780 struct in6_addr *p; 781 { 782 struct sockaddr_in6 sin6; 783 static char hbuf[NI_MAXHOST]; 784 #ifdef NI_WITHSCOPEID 785 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; 786 #else 787 const int niflags = NI_NUMERICHOST 788 #endif 789 790 memset(&sin6, 0, sizeof(sin6)); 791 sin6.sin6_family = AF_INET6; 792 sin6.sin6_len = sizeof(struct sockaddr_in6); 793 sin6.sin6_addr = *p; 794 if (IN6_IS_ADDR_LINKLOCAL(p) && 795 *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) { 796 sin6.sin6_scope_id = 797 ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]); 798 sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0; 799 } 800 801 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, 802 hbuf, sizeof(hbuf), NULL, 0, niflags)) 803 return "invalid"; 804 805 return hbuf; 806 } 807 #endif 808 809 void 810 socktrans(sock, i) 811 struct socket *sock; 812 int i; 813 { 814 static char *stypename[] = { 815 "unused", /* 0 */ 816 "stream", /* 1 */ 817 "dgram", /* 2 */ 818 "raw", /* 3 */ 819 "rdm", /* 4 */ 820 "seqpak" /* 5 */ 821 }; 822 #define STYPEMAX 5 823 struct socket so; 824 struct protosw proto; 825 struct domain dom; 826 struct inpcb inpcb; 827 struct unpcb unpcb; 828 int len; 829 char dname[32]; 830 #ifdef INET6 831 char xaddrbuf[NI_MAXHOST + 2]; 832 #endif 833 834 PREFIX(i); 835 836 /* fill in socket */ 837 if (!KVM_READ(sock, &so, sizeof(struct socket))) { 838 dprintf("can't read sock at %p", sock); 839 goto bad; 840 } 841 842 /* fill in protosw entry */ 843 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) { 844 dprintf("can't read protosw at %p", so.so_proto); 845 goto bad; 846 } 847 848 /* fill in domain */ 849 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) { 850 dprintf("can't read domain at %p", proto.pr_domain); 851 goto bad; 852 } 853 854 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname, 855 sizeof(dname) - 1)) != sizeof(dname) -1) { 856 dprintf("can't read domain name at %p", dom.dom_name); 857 dname[0] = '\0'; 858 } 859 else 860 dname[len] = '\0'; 861 862 if ((u_short)so.so_type > STYPEMAX) 863 printf("* %s ?%d", dname, so.so_type); 864 else 865 printf("* %s %s", dname, stypename[so.so_type]); 866 867 /* 868 * protocol specific formatting 869 * 870 * Try to find interesting things to print. For tcp, the interesting 871 * thing is the address of the tcpcb, for udp and others, just the 872 * inpcb (socket pcb). For unix domain, its the address of the socket 873 * pcb and the address of the connected pcb (if connected). Otherwise 874 * just print the protocol number and address of the socket itself. 875 * The idea is not to duplicate netstat, but to make available enough 876 * information for further analysis. 877 */ 878 switch(dom.dom_family) { 879 case AF_INET: 880 getinetproto(proto.pr_protocol); 881 if (proto.pr_protocol == IPPROTO_TCP) { 882 if (so.so_pcb == NULL) 883 break; 884 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb, 885 sizeof(struct inpcb)) != sizeof(struct inpcb)) { 886 dprintf("can't read inpcb at %p", so.so_pcb); 887 goto bad; 888 } 889 printf(" %p", inpcb.inp_ppcb); 890 printf(" %s:%d", 891 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" : 892 inet_ntoa(inpcb.inp_laddr), 893 ntohs(inpcb.inp_lport)); 894 if (inpcb.inp_fport) { 895 if (so.so_state & SS_CONNECTOUT) 896 printf(" --> "); 897 else 898 printf(" <-- "); 899 printf("%s:%d", 900 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" : 901 inet_ntoa(inpcb.inp_faddr), 902 ntohs(inpcb.inp_fport)); 903 } 904 } else if (proto.pr_protocol == IPPROTO_UDP) { 905 if (so.so_pcb == NULL) 906 break; 907 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb, 908 sizeof(struct inpcb)) != sizeof(struct inpcb)) { 909 dprintf("can't read inpcb at %p", so.so_pcb); 910 goto bad; 911 } 912 printf(" %s:%d", 913 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" : 914 inet_ntoa(inpcb.inp_laddr), 915 ntohs(inpcb.inp_lport)); 916 if (inpcb.inp_fport) 917 printf(" <-> %s:%d", 918 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" : 919 inet_ntoa(inpcb.inp_faddr), 920 ntohs(inpcb.inp_fport)); 921 } else if (so.so_pcb) 922 printf(" %p", so.so_pcb); 923 break; 924 #ifdef INET6 925 case AF_INET6: 926 getinetproto(proto.pr_protocol); 927 if (proto.pr_protocol == IPPROTO_TCP) { 928 if (so.so_pcb == NULL) 929 break; 930 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb, 931 sizeof(struct inpcb)) != sizeof(struct inpcb)) { 932 dprintf("can't read inpcb at %p", so.so_pcb); 933 goto bad; 934 } 935 printf(" %p", inpcb.inp_ppcb); 936 snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]", 937 inet6_addrstr(&inpcb.inp_laddr6)); 938 printf(" %s:%d", 939 IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6) ? "*" : 940 xaddrbuf, 941 ntohs(inpcb.inp_lport)); 942 if (inpcb.inp_fport) { 943 if (so.so_state & SS_CONNECTOUT) 944 printf(" --> "); 945 else 946 printf(" <-- "); 947 snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]", 948 inet6_addrstr(&inpcb.inp_faddr6)); 949 printf("%s:%d", 950 IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6) ? "*" : 951 xaddrbuf, 952 ntohs(inpcb.inp_fport)); 953 } 954 } else if (proto.pr_protocol == IPPROTO_UDP) { 955 if (so.so_pcb == NULL) 956 break; 957 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb, 958 sizeof(struct inpcb)) != sizeof(struct inpcb)) { 959 dprintf("can't read inpcb at %p", so.so_pcb); 960 goto bad; 961 } 962 snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]", 963 inet6_addrstr(&inpcb.inp_laddr6)); 964 printf(" %s:%d", 965 IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6) ? "*" : 966 xaddrbuf, 967 ntohs(inpcb.inp_lport)); 968 if (inpcb.inp_fport) { 969 snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]", 970 inet6_addrstr(&inpcb.inp_faddr6)); 971 printf(" <-> %s:%d", 972 IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6) ? "*" : 973 xaddrbuf, 974 ntohs(inpcb.inp_fport)); 975 } 976 } else if (so.so_pcb) 977 printf(" %p", so.so_pcb); 978 break; 979 #endif 980 case AF_UNIX: 981 /* print address of pcb and connected pcb */ 982 if (so.so_pcb) { 983 printf(" %p", so.so_pcb); 984 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb, 985 sizeof(struct unpcb)) != sizeof(struct unpcb)){ 986 dprintf("can't read unpcb at %p", so.so_pcb); 987 goto bad; 988 } 989 if (unpcb.unp_conn) { 990 char shoconn[4], *cp; 991 992 cp = shoconn; 993 if (!(so.so_state & SS_CANTRCVMORE)) 994 *cp++ = '<'; 995 *cp++ = '-'; 996 if (!(so.so_state & SS_CANTSENDMORE)) 997 *cp++ = '>'; 998 *cp = '\0'; 999 printf(" %s %p", shoconn, 1000 unpcb.unp_conn); 1001 } 1002 } 1003 break; 1004 default: 1005 /* print protocol number and socket address */ 1006 printf(" %d %p", proto.pr_protocol, sock); 1007 } 1008 printf("\n"); 1009 return; 1010 bad: 1011 printf("* error\n"); 1012 } 1013 1014 /* 1015 * getinetproto -- 1016 * print name of protocol number 1017 */ 1018 void 1019 getinetproto(number) 1020 int number; 1021 { 1022 static int isopen; 1023 struct protoent *pe; 1024 1025 if (!isopen) 1026 setprotoent(++isopen); 1027 if ((pe = getprotobynumber(number)) != NULL) 1028 printf(" %s", pe->p_name); 1029 else 1030 printf(" %d", number); 1031 } 1032 1033 int 1034 getfname(filename) 1035 char *filename; 1036 { 1037 struct stat statbuf; 1038 DEVS *cur; 1039 1040 if (stat(filename, &statbuf)) { 1041 warn("%s", filename); 1042 return(0); 1043 } 1044 if ((cur = malloc(sizeof(DEVS))) == NULL) 1045 err(1, "malloc"); 1046 cur->next = devs; 1047 devs = cur; 1048 1049 cur->ino = statbuf.st_ino; 1050 cur->fsid = statbuf.st_dev & 0xffff; 1051 cur->name = filename; 1052 return(1); 1053 } 1054 1055 void 1056 usage() 1057 { 1058 (void)fprintf(stderr, 1059 "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n"); 1060 exit(1); 1061 } 1062