1 /* $NetBSD: fstat.c,v 1.57 2002/10/26 06:03:50 yamt 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 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) 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 #if 0 44 static char sccsid[] = "@(#)fstat.c 8.3 (Berkeley) 5/2/95"; 45 #else 46 __RCSID("$NetBSD: fstat.c,v 1.57 2002/10/26 06:03:50 yamt Exp $"); 47 #endif 48 #endif /* not lint */ 49 50 #include <sys/param.h> 51 #include <sys/time.h> 52 #include <sys/proc.h> 53 #include <sys/user.h> 54 #include <sys/stat.h> 55 #include <sys/vnode.h> 56 #include <sys/socket.h> 57 #include <sys/socketvar.h> 58 #include <sys/domain.h> 59 #include <sys/protosw.h> 60 #include <sys/unpcb.h> 61 #include <sys/sysctl.h> 62 #include <sys/filedesc.h> 63 #include <sys/pipe.h> 64 #define _KERNEL 65 #include <sys/file.h> 66 #include <ufs/ufs/inode.h> 67 #undef _KERNEL 68 #define _KERNEL 69 #include <sys/mount.h> 70 #undef _KERNEL 71 #define NFS 72 #include <nfs/nfsproto.h> 73 #include <nfs/rpcv2.h> 74 #include <nfs/nfs.h> 75 #include <nfs/nfsnode.h> 76 #undef NFS 77 #include <msdosfs/denode.h> 78 #include <msdosfs/bpb.h> 79 #define _KERNEL 80 #include <msdosfs/msdosfsmount.h> 81 #undef _KERNEL 82 #define _KERNEL 83 #include <miscfs/genfs/layer.h> 84 #undef _KERNEL 85 86 #include <net/route.h> 87 #include <netinet/in.h> 88 #include <netinet/in_systm.h> 89 #include <netinet/ip.h> 90 #include <netinet/in_pcb.h> 91 92 #ifdef INET6 93 #include <netinet/ip6.h> 94 #include <netinet6/ip6_var.h> 95 #include <netinet6/in6_pcb.h> 96 #endif 97 98 #include <netdb.h> 99 #include <arpa/inet.h> 100 101 #include <ctype.h> 102 #include <errno.h> 103 #include <kvm.h> 104 #include <limits.h> 105 #include <nlist.h> 106 #include <paths.h> 107 #include <pwd.h> 108 #include <stdio.h> 109 #include <stdlib.h> 110 #include <string.h> 111 #include <unistd.h> 112 #include <err.h> 113 114 #include "fstat.h" 115 116 #define TEXT -1 117 #define CDIR -2 118 #define RDIR -3 119 #define TRACE -4 120 121 typedef struct devs { 122 struct devs *next; 123 long fsid; 124 ino_t ino; 125 char *name; 126 } DEVS; 127 DEVS *devs; 128 129 int fsflg, /* show files on same filesystem as file(s) argument */ 130 pflg, /* show files open by a particular pid */ 131 uflg; /* show files open by a particular (effective) user */ 132 int checkfile; /* true if restricting to particular files or filesystems */ 133 int nflg; /* (numerical) display f.s. and rdev as dev_t */ 134 int vflg; /* display errors in locating kernel data objects etc... */ 135 136 struct file **ofiles; /* buffer of pointers to file structures */ 137 int maxfiles; 138 #define ALLOC_OFILES(d) \ 139 if ((d) > maxfiles) { \ 140 free(ofiles); \ 141 ofiles = malloc((d) * sizeof(struct file *)); \ 142 if (ofiles == NULL) { \ 143 err(1, "malloc(%u)", (d) * \ 144 (unsigned int)sizeof(struct file *)); \ 145 } \ 146 maxfiles = (d); \ 147 } 148 149 kvm_t *kd; 150 151 void dofiles __P((struct kinfo_proc *)); 152 int ext2fs_filestat __P((struct vnode *, struct filestat *)); 153 int getfname __P((char *)); 154 void getinetproto __P((int)); 155 char *getmnton __P((struct mount *)); 156 char *layer_filestat __P((struct vnode *, struct filestat *)); 157 int main __P((int, char **)); 158 int msdosfs_filestat __P((struct vnode *, struct filestat *)); 159 int nfs_filestat __P((struct vnode *, struct filestat *)); 160 #ifdef INET6 161 static const char *inet6_addrstr __P((struct in6_addr *)); 162 #endif 163 void socktrans __P((struct socket *, int)); 164 void kqueuetrans __P((void *, int)); 165 int ufs_filestat __P((struct vnode *, struct filestat *)); 166 void usage __P((void)); 167 char *vfilestat __P((struct vnode *, struct filestat *)); 168 void vtrans __P((struct vnode *, int, int)); 169 void ftrans __P((struct file *, int)); 170 void ptrans __P((struct file *, struct pipe *, int)); 171 172 int 173 main(argc, argv) 174 int argc; 175 char **argv; 176 { 177 struct passwd *passwd; 178 struct kinfo_proc *p, *plast; 179 int arg, ch, what; 180 char *memf, *nlistf; 181 char buf[_POSIX2_LINE_MAX]; 182 int cnt; 183 gid_t egid = getegid(); 184 185 (void)setegid(getgid()); 186 arg = 0; 187 what = KERN_PROC_ALL; 188 nlistf = memf = NULL; 189 while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1) 190 switch((char)ch) { 191 case 'f': 192 fsflg = 1; 193 break; 194 case 'M': 195 memf = optarg; 196 break; 197 case 'N': 198 nlistf = optarg; 199 break; 200 case 'n': 201 nflg = 1; 202 break; 203 case 'p': 204 if (pflg++) 205 usage(); 206 if (!isdigit(*optarg)) { 207 warnx("-p requires a process id"); 208 usage(); 209 } 210 what = KERN_PROC_PID; 211 arg = atoi(optarg); 212 break; 213 case 'u': 214 if (uflg++) 215 usage(); 216 if (!(passwd = getpwnam(optarg))) { 217 errx(1, "%s: unknown uid", optarg); 218 } 219 what = KERN_PROC_UID; 220 arg = passwd->pw_uid; 221 break; 222 case 'v': 223 vflg = 1; 224 break; 225 case '?': 226 default: 227 usage(); 228 } 229 230 if (*(argv += optind)) { 231 for (; *argv; ++argv) { 232 if (getfname(*argv)) 233 checkfile = 1; 234 } 235 if (!checkfile) /* file(s) specified, but none accessible */ 236 exit(1); 237 } 238 239 ALLOC_OFILES(256); /* reserve space for file pointers */ 240 241 if (fsflg && !checkfile) { 242 /* -f with no files means use wd */ 243 if (getfname(".") == 0) 244 exit(1); 245 checkfile = 1; 246 } 247 248 /* 249 * Discard setgid privileges. If not the running kernel, we toss 250 * them away totally so that bad guys can't print interesting stuff 251 * from kernel memory, otherwise switch back to kmem for the 252 * duration of the kvm_openfiles() call. 253 */ 254 if (nlistf != NULL || memf != NULL) 255 (void)setgid(getgid()); 256 else 257 (void)setegid(egid); 258 259 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL) 260 errx(1, "%s", buf); 261 262 /* get rid of it now anyway */ 263 if (nlistf == NULL && memf == NULL) 264 (void)setgid(getgid()); 265 266 if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) { 267 errx(1, "%s", kvm_geterr(kd)); 268 } 269 if (nflg) 270 printf("%s", 271 "USER CMD PID FD DEV INUM MODE SZ|DV R/W"); 272 else 273 printf("%s", 274 "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W"); 275 if (checkfile && fsflg == 0) 276 printf(" NAME\n"); 277 else 278 putchar('\n'); 279 280 for (plast = &p[cnt]; p < plast; ++p) { 281 if (p->kp_proc.p_stat == SZOMB) 282 continue; 283 dofiles(p); 284 } 285 exit(0); 286 } 287 288 const char *Uname, *Comm; 289 pid_t Pid; 290 291 #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \ 292 switch(i) { \ 293 case TEXT: \ 294 printf(" text"); \ 295 break; \ 296 case CDIR: \ 297 printf(" wd"); \ 298 break; \ 299 case RDIR: \ 300 printf(" root"); \ 301 break; \ 302 case TRACE: \ 303 printf(" tr"); \ 304 break; \ 305 default: \ 306 printf(" %4d", i); \ 307 break; \ 308 } 309 310 /* 311 * print open files attributed to this process 312 */ 313 void 314 dofiles(kp) 315 struct kinfo_proc *kp; 316 { 317 int i; 318 struct filedesc0 filed0; 319 #define filed filed0.fd_fd 320 struct cwdinfo cwdi; 321 struct proc *p = &kp->kp_proc; 322 struct eproc *ep = &kp->kp_eproc; 323 324 Uname = user_from_uid(ep->e_ucred.cr_uid, 0); 325 Pid = p->p_pid; 326 Comm = p->p_comm; 327 328 if (p->p_fd == NULL || p->p_cwdi == NULL) 329 return; 330 if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) { 331 warnx("can't read filedesc at %p for pid %d", p->p_fd, Pid); 332 return; 333 } 334 if (!KVM_READ(p->p_cwdi, &cwdi, sizeof(cwdi))) { 335 warnx("can't read cwdinfo at %p for pid %d", p->p_cwdi, Pid); 336 return; 337 } 338 if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles || 339 filed.fd_freefile > filed.fd_lastfile + 1) { 340 dprintf("filedesc corrupted at %p for pid %d", p->p_fd, Pid); 341 return; 342 } 343 /* 344 * root directory vnode, if one 345 */ 346 if (cwdi.cwdi_rdir) 347 vtrans(cwdi.cwdi_rdir, RDIR, FREAD); 348 /* 349 * current working directory vnode 350 */ 351 vtrans(cwdi.cwdi_cdir, CDIR, FREAD); 352 /* 353 * ktrace vnode, if one 354 */ 355 if (p->p_tracep) 356 ftrans(p->p_tracep, TRACE); 357 /* 358 * open files 359 */ 360 #define FPSIZE (sizeof (struct file *)) 361 ALLOC_OFILES(filed.fd_lastfile+1); 362 if (filed.fd_nfiles > NDFILE) { 363 if (!KVM_READ(filed.fd_ofiles, ofiles, 364 (filed.fd_lastfile+1) * FPSIZE)) { 365 dprintf("can't read file structures at %p for pid %d", 366 filed.fd_ofiles, Pid); 367 return; 368 } 369 } else 370 memmove(ofiles, filed0.fd_dfiles, 371 (filed.fd_lastfile+1) * FPSIZE); 372 for (i = 0; i <= filed.fd_lastfile; i++) { 373 if (ofiles[i] == NULL) 374 continue; 375 ftrans(ofiles[i], i); 376 } 377 } 378 379 void 380 ftrans (fp, i) 381 struct file *fp; 382 int i; 383 { 384 struct file file; 385 386 if (!KVM_READ(fp, &file, sizeof (struct file))) { 387 dprintf("can't read file %d at %p for pid %d", 388 i, fp, Pid); 389 return; 390 } 391 switch (file.f_type) { 392 case DTYPE_VNODE: 393 vtrans((struct vnode *)file.f_data, i, file.f_flag); 394 break; 395 case DTYPE_SOCKET: 396 if (checkfile == 0) 397 socktrans((struct socket *)file.f_data, i); 398 break; 399 case DTYPE_PIPE: 400 if (checkfile == 0) 401 ptrans(&file, (struct pipe *)file.f_data, i); 402 break; 403 case DTYPE_KQUEUE: 404 if (checkfile == 0) 405 kqueuetrans((void *)file.f_data, i); 406 break; 407 default: 408 dprintf("unknown file type %d for file %d of pid %d", 409 file.f_type, i, Pid); 410 break; 411 } 412 } 413 414 char * 415 vfilestat(vp, fsp) 416 struct vnode *vp; 417 struct filestat *fsp; 418 { 419 char *badtype = NULL; 420 421 if (vp->v_type == VNON || vp->v_tag == VT_NON) 422 badtype = "none"; 423 else if (vp->v_type == VBAD) 424 badtype = "bad"; 425 else 426 switch (vp->v_tag) { 427 case VT_UFS: 428 case VT_LFS: 429 case VT_MFS: 430 if (!ufs_filestat(vp, fsp)) 431 badtype = "error"; 432 break; 433 case VT_MSDOSFS: 434 if (!msdosfs_filestat(vp, fsp)) 435 badtype = "error"; 436 break; 437 case VT_NFS: 438 if (!nfs_filestat(vp, fsp)) 439 badtype = "error"; 440 break; 441 case VT_EXT2FS: 442 if (!ext2fs_filestat(vp, fsp)) 443 badtype = "error"; 444 break; 445 case VT_ISOFS: 446 if (!isofs_filestat(vp, fsp)) 447 badtype = "error"; 448 break; 449 case VT_NTFS: 450 if (!ntfs_filestat(vp, fsp)) 451 badtype = "error"; 452 break; 453 case VT_NULL: 454 case VT_OVERLAY: 455 case VT_UMAP: 456 badtype = layer_filestat(vp, fsp); 457 break; 458 default: { 459 static char unknown[10]; 460 (void)snprintf(badtype = unknown, sizeof unknown, 461 "?(%x)", vp->v_tag); 462 break; 463 } 464 } 465 return (badtype); 466 } 467 468 void 469 vtrans(vp, i, flag) 470 struct vnode *vp; 471 int i; 472 int flag; 473 { 474 struct vnode vn; 475 struct filestat fst; 476 char mode[15], rw[3]; 477 char *badtype, *filename; 478 479 filename = NULL; 480 if (!KVM_READ(vp, &vn, sizeof(struct vnode))) { 481 dprintf("can't read vnode at %p for pid %d", vp, Pid); 482 return; 483 } 484 badtype = vfilestat(&vn, &fst); 485 if (checkfile) { 486 int fsmatch = 0; 487 DEVS *d; 488 489 if (badtype) 490 return; 491 for (d = devs; d != NULL; d = d->next) 492 if (d->fsid == fst.fsid) { 493 fsmatch = 1; 494 if (d->ino == fst.fileid) { 495 filename = d->name; 496 break; 497 } 498 } 499 if (fsmatch == 0 || (filename == NULL && fsflg == 0)) 500 return; 501 } 502 PREFIX(i); 503 if (badtype) { 504 (void)printf(" - - %10s -\n", badtype); 505 return; 506 } 507 if (nflg) 508 (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid)); 509 else 510 (void)printf(" %-8s", getmnton(vn.v_mount)); 511 if (nflg) 512 (void)snprintf(mode, sizeof mode, "%o", fst.mode); 513 else 514 strmode(fst.mode, mode); 515 (void)printf(" %7ld %*s", (long)fst.fileid, nflg ? 5 : 10, mode); 516 switch (vn.v_type) { 517 case VBLK: 518 case VCHR: { 519 char *name; 520 521 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ? 522 S_IFCHR : S_IFBLK)) == NULL)) 523 printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev)); 524 else 525 printf(" %6s", name); 526 break; 527 } 528 default: 529 printf(" %6lld", (long long)fst.size); 530 } 531 rw[0] = '\0'; 532 if (flag & FREAD) 533 strcat(rw, "r"); 534 if (flag & FWRITE) 535 strcat(rw, "w"); 536 printf(" %-2s", rw); 537 if (filename && !fsflg) 538 printf(" %s", filename); 539 putchar('\n'); 540 } 541 542 int 543 ufs_filestat(vp, fsp) 544 struct vnode *vp; 545 struct filestat *fsp; 546 { 547 struct inode inode; 548 549 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 550 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid); 551 return 0; 552 } 553 fsp->fsid = inode.i_dev & 0xffff; 554 fsp->fileid = (long)inode.i_number; 555 fsp->mode = (mode_t)inode.i_ffs_mode; 556 fsp->size = inode.i_ffs_size; 557 fsp->rdev = inode.i_ffs_rdev; 558 559 return 1; 560 } 561 562 int 563 ext2fs_filestat(vp, fsp) 564 struct vnode *vp; 565 struct filestat *fsp; 566 { 567 struct inode inode; 568 569 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 570 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid); 571 return 0; 572 } 573 fsp->fsid = inode.i_dev & 0xffff; 574 fsp->fileid = (long)inode.i_number; 575 fsp->mode = (mode_t)inode.i_e2fs_mode; 576 fsp->size = inode.i_e2fs_size; 577 fsp->rdev = 0; /* XXX */ 578 return 1; 579 } 580 581 int 582 nfs_filestat(vp, fsp) 583 struct vnode *vp; 584 struct filestat *fsp; 585 { 586 struct nfsnode nfsnode; 587 struct vattr va; 588 589 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) { 590 dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp), 591 Pid); 592 return 0; 593 } 594 if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) { 595 dprintf("can't read vnode attributes at %p for pid %d", 596 nfsnode.n_vattr, Pid); 597 return 0; 598 } 599 fsp->fsid = va.va_fsid; 600 fsp->fileid = va.va_fileid; 601 fsp->size = nfsnode.n_size; 602 fsp->rdev = va.va_rdev; 603 fsp->mode = (mode_t)va.va_mode | getftype(vp->v_type); 604 605 return 1; 606 } 607 608 int 609 msdosfs_filestat(vp, fsp) 610 struct vnode *vp; 611 struct filestat *fsp; 612 { 613 struct denode de; 614 struct msdosfsmount mp; 615 616 if (!KVM_READ(VTONFS(vp), &de, sizeof(de))) { 617 dprintf("can't read denode at %p for pid %d", VTONFS(vp), 618 Pid); 619 return 0; 620 } 621 if (!KVM_READ(de.de_pmp, &mp, sizeof(mp))) { 622 dprintf("can't read mount struct at %p for pid %d", de.de_pmp, 623 Pid); 624 return 0; 625 } 626 627 fsp->fsid = de.de_dev & 0xffff; 628 fsp->fileid = 0; /* XXX see msdosfs_vptofh() for more info */ 629 fsp->size = de.de_FileSize; 630 fsp->rdev = 0; /* msdosfs doesn't support device files */ 631 fsp->mode = (0777 & mp.pm_mask) | getftype(vp->v_type); 632 return 1; 633 } 634 635 char * 636 layer_filestat(vp, fsp) 637 struct vnode *vp; 638 struct filestat *fsp; 639 { 640 struct layer_node layer_node; 641 struct mount mount; 642 struct vnode vn; 643 char *badtype; 644 645 if (!KVM_READ(VTOLAYER(vp), &layer_node, sizeof(layer_node))) { 646 dprintf("can't read layer_node at %p for pid %d", 647 VTOLAYER(vp), Pid); 648 return ("error"); 649 } 650 if (!KVM_READ(vp->v_mount, &mount, sizeof(struct mount))) { 651 dprintf("can't read mount struct at %p for pid %d", 652 vp->v_mount, Pid); 653 return ("error"); 654 } 655 vp = layer_node.layer_lowervp; 656 if (!KVM_READ(vp, &vn, sizeof(struct vnode))) { 657 dprintf("can't read vnode at %p for pid %d", vp, Pid); 658 return ("error"); 659 } 660 if ((badtype = vfilestat(&vn, fsp)) == NULL) 661 fsp->fsid = mount.mnt_stat.f_fsid.val[0]; 662 return (badtype); 663 } 664 665 char * 666 getmnton(m) 667 struct mount *m; 668 { 669 static struct mount mount; 670 static struct mtab { 671 struct mtab *next; 672 struct mount *m; 673 char mntonname[MNAMELEN]; 674 } *mhead = NULL; 675 struct mtab *mt; 676 677 for (mt = mhead; mt != NULL; mt = mt->next) 678 if (m == mt->m) 679 return (mt->mntonname); 680 if (!KVM_READ(m, &mount, sizeof(struct mount))) { 681 warnx("can't read mount table at %p", m); 682 return (NULL); 683 } 684 if ((mt = malloc(sizeof (struct mtab))) == NULL) { 685 err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab)); 686 } 687 mt->m = m; 688 memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0], MNAMELEN); 689 mt->next = mhead; 690 mhead = mt; 691 return (mt->mntonname); 692 } 693 694 #ifdef INET6 695 static const char * 696 inet6_addrstr(p) 697 struct in6_addr *p; 698 { 699 struct sockaddr_in6 sin6; 700 static char hbuf[NI_MAXHOST]; 701 #ifdef NI_WITHSCOPEID 702 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; 703 #else 704 const int niflags = NI_NUMERICHOST; 705 #endif 706 707 memset(&sin6, 0, sizeof(sin6)); 708 sin6.sin6_family = AF_INET6; 709 sin6.sin6_len = sizeof(struct sockaddr_in6); 710 sin6.sin6_addr = *p; 711 if (IN6_IS_ADDR_LINKLOCAL(p) && 712 *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) { 713 sin6.sin6_scope_id = 714 ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]); 715 sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0; 716 } 717 718 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, 719 hbuf, sizeof(hbuf), NULL, 0, niflags)) 720 return "invalid"; 721 722 return hbuf; 723 } 724 #endif 725 726 void 727 socktrans(sock, i) 728 struct socket *sock; 729 int i; 730 { 731 static char *stypename[] = { 732 "unused", /* 0 */ 733 "stream", /* 1 */ 734 "dgram", /* 2 */ 735 "raw", /* 3 */ 736 "rdm", /* 4 */ 737 "seqpak" /* 5 */ 738 }; 739 #define STYPEMAX 5 740 struct socket so; 741 struct protosw proto; 742 struct domain dom; 743 struct inpcb inpcb; 744 #ifdef INET6 745 struct in6pcb in6pcb; 746 #endif 747 struct unpcb unpcb; 748 int len; 749 char dname[32]; 750 #ifdef INET6 751 char xaddrbuf[NI_MAXHOST + 2]; 752 #endif 753 754 PREFIX(i); 755 756 /* fill in socket */ 757 if (!KVM_READ(sock, &so, sizeof(struct socket))) { 758 dprintf("can't read sock at %p", sock); 759 goto bad; 760 } 761 762 /* fill in protosw entry */ 763 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) { 764 dprintf("can't read protosw at %p", so.so_proto); 765 goto bad; 766 } 767 768 /* fill in domain */ 769 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) { 770 dprintf("can't read domain at %p", proto.pr_domain); 771 goto bad; 772 } 773 774 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname, 775 sizeof(dname) - 1)) != sizeof(dname) -1) { 776 dprintf("can't read domain name at %p", 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 getinetproto(proto.pr_protocol); 801 if (proto.pr_protocol == IPPROTO_TCP) { 802 if (so.so_pcb == NULL) 803 break; 804 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb, 805 sizeof(struct inpcb)) != sizeof(struct inpcb)) { 806 dprintf("can't read inpcb at %p", so.so_pcb); 807 goto bad; 808 } 809 printf(" %lx", (long)inpcb.inp_ppcb); 810 printf(" %s:%d", 811 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" : 812 inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport)); 813 if (inpcb.inp_fport) { 814 printf(" <-> %s:%d", 815 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" : 816 inet_ntoa(inpcb.inp_faddr), 817 ntohs(inpcb.inp_fport)); 818 } 819 } else if (proto.pr_protocol == IPPROTO_UDP) { 820 if (so.so_pcb == NULL) 821 break; 822 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb, 823 sizeof(struct inpcb)) != sizeof(struct inpcb)) { 824 dprintf("can't read inpcb at %p", so.so_pcb); 825 goto bad; 826 } 827 printf(" %lx", (long)so.so_pcb); 828 printf(" %s:%d", 829 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" : 830 inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport)); 831 if (inpcb.inp_fport) 832 printf(" <-> %s:%d", 833 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" : 834 inet_ntoa(inpcb.inp_faddr), 835 ntohs(inpcb.inp_fport)); 836 } else if (so.so_pcb) 837 printf(" %lx", (long)so.so_pcb); 838 break; 839 #ifdef INET6 840 case AF_INET6: 841 getinetproto(proto.pr_protocol); 842 if (proto.pr_protocol == IPPROTO_TCP) { 843 if (so.so_pcb == NULL) 844 break; 845 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb, 846 sizeof(struct in6pcb)) != sizeof(struct in6pcb)) { 847 dprintf("can't read in6pcb at %p", so.so_pcb); 848 goto bad; 849 } 850 printf(" %lx", (long)in6pcb.in6p_ppcb); 851 sprintf(xaddrbuf, "[%s]", 852 inet6_addrstr(&in6pcb.in6p_laddr)); 853 printf(" %s:%d", 854 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" : 855 xaddrbuf, 856 ntohs(in6pcb.in6p_lport)); 857 if (in6pcb.in6p_fport) { 858 sprintf(xaddrbuf, "[%s]", 859 inet6_addrstr(&in6pcb.in6p_faddr)); 860 printf(" <-> %s:%d", 861 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" : 862 xaddrbuf, 863 ntohs(in6pcb.in6p_fport)); 864 } 865 } else if (proto.pr_protocol == IPPROTO_UDP) { 866 if (so.so_pcb == NULL) 867 break; 868 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb, 869 sizeof(struct in6pcb)) != sizeof(struct in6pcb)) { 870 dprintf("can't read inpcb at %p", so.so_pcb); 871 goto bad; 872 } 873 printf(" %lx", (long)so.so_pcb); 874 sprintf(xaddrbuf, "[%s]", 875 inet6_addrstr(&in6pcb.in6p_laddr)); 876 printf(" %s:%d", 877 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" : 878 xaddrbuf, 879 ntohs(in6pcb.in6p_lport)); 880 if (in6pcb.in6p_fport) { 881 sprintf(xaddrbuf, "[%s]", 882 inet6_addrstr(&in6pcb.in6p_faddr)); 883 printf(" <-> %s:%d", 884 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" : 885 xaddrbuf, 886 ntohs(in6pcb.in6p_fport)); 887 } 888 } else if (so.so_pcb) 889 printf(" %lx", (long)so.so_pcb); 890 break; 891 #endif 892 case AF_LOCAL: 893 /* print address of pcb and connected pcb */ 894 if (so.so_pcb) { 895 printf(" %lx", (long)so.so_pcb); 896 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb, 897 sizeof(struct unpcb)) != sizeof(struct unpcb)){ 898 dprintf("can't read unpcb at %p", so.so_pcb); 899 goto bad; 900 } 901 if (unpcb.unp_conn) { 902 char shoconn[4], *cp; 903 904 cp = shoconn; 905 if (!(so.so_state & SS_CANTRCVMORE)) 906 *cp++ = '<'; 907 *cp++ = '-'; 908 if (!(so.so_state & SS_CANTSENDMORE)) 909 *cp++ = '>'; 910 *cp = '\0'; 911 printf(" %s %lx", shoconn, 912 (long)unpcb.unp_conn); 913 } 914 } 915 break; 916 default: 917 /* print protocol number and socket address */ 918 printf(" %d %lx", proto.pr_protocol, (long)sock); 919 } 920 printf("\n"); 921 return; 922 bad: 923 printf("* error\n"); 924 } 925 926 void 927 ptrans(fp, cpipe, i) 928 struct file *fp; 929 struct pipe *cpipe; 930 int i; 931 { 932 struct pipe cp; 933 934 PREFIX(i); 935 936 /* fill in pipe */ 937 if (!KVM_READ(cpipe, &cp, sizeof(struct pipe))) { 938 dprintf("can't read pipe at %p", cpipe); 939 goto bad; 940 } 941 942 /* pipe descriptor is either read or write, never both */ 943 printf("* pipe %p %s %p %s%s%s", cpipe, 944 (fp->f_flag & FWRITE) ? "->" : "<-", 945 cp.pipe_peer, 946 (fp->f_flag & FWRITE) ? "w" : "r", 947 (fp->f_flag & FNONBLOCK) ? "n" : "", 948 (cp.pipe_state & PIPE_ASYNC) ? "a" : ""); 949 printf("\n"); 950 return; 951 bad: 952 printf("* error\n"); 953 } 954 955 void 956 kqueuetrans(kq, i) 957 void *kq; 958 int i; 959 { 960 961 PREFIX(i); 962 printf("* kqueue %lx", (long)kq); 963 printf("\n"); 964 } 965 966 /* 967 * getinetproto -- 968 * print name of protocol number 969 */ 970 void 971 getinetproto(number) 972 int number; 973 { 974 char *cp; 975 976 switch (number) { 977 case IPPROTO_IP: 978 cp = "ip"; break; 979 case IPPROTO_ICMP: 980 cp ="icmp"; break; 981 case IPPROTO_GGP: 982 cp ="ggp"; break; 983 case IPPROTO_TCP: 984 cp ="tcp"; break; 985 case IPPROTO_EGP: 986 cp ="egp"; break; 987 case IPPROTO_PUP: 988 cp ="pup"; break; 989 case IPPROTO_UDP: 990 cp ="udp"; break; 991 case IPPROTO_IDP: 992 cp ="idp"; break; 993 case IPPROTO_RAW: 994 cp ="raw"; break; 995 case IPPROTO_ICMPV6: 996 cp ="icmp6"; break; 997 default: 998 printf(" %d", number); 999 return; 1000 } 1001 printf(" %s", cp); 1002 } 1003 1004 int 1005 getfname(filename) 1006 char *filename; 1007 { 1008 struct stat statbuf; 1009 DEVS *cur; 1010 1011 if (stat(filename, &statbuf)) { 1012 warn("stat(%s)", filename); 1013 return(0); 1014 } 1015 if ((cur = malloc(sizeof(DEVS))) == NULL) { 1016 err(1, "malloc(%u)", (unsigned int)sizeof(DEVS)); 1017 } 1018 cur->next = devs; 1019 devs = cur; 1020 1021 cur->ino = statbuf.st_ino; 1022 cur->fsid = statbuf.st_dev & 0xffff; 1023 cur->name = filename; 1024 return(1); 1025 } 1026 1027 mode_t 1028 getftype(v_type) 1029 enum vtype v_type; 1030 { 1031 mode_t ftype; 1032 1033 switch (v_type) { 1034 case VREG: 1035 ftype = S_IFREG; 1036 break; 1037 case VDIR: 1038 ftype = S_IFDIR; 1039 break; 1040 case VBLK: 1041 ftype = S_IFBLK; 1042 break; 1043 case VCHR: 1044 ftype = S_IFCHR; 1045 break; 1046 case VLNK: 1047 ftype = S_IFLNK; 1048 break; 1049 case VSOCK: 1050 ftype = S_IFSOCK; 1051 break; 1052 case VFIFO: 1053 ftype = S_IFIFO; 1054 break; 1055 default: 1056 ftype = 0; 1057 break; 1058 }; 1059 1060 return ftype; 1061 } 1062 1063 void 1064 usage() 1065 { 1066 errx(1, 1067 "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n"); 1068 } 1069