1 /* $NetBSD: fstat.c,v 1.62 2003/04/02 10:39:46 fvdl 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.62 2003/04/02 10:39:46 fvdl 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_proc2 *)); 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_proc2 *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_getproc2(kd, what, arg, sizeof *p, &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->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(p) 315 struct kinfo_proc2 *p; 316 { 317 int i; 318 struct filedesc0 filed0; 319 #define filed filed0.fd_fd 320 struct cwdinfo cwdi; 321 322 Uname = user_from_uid(p->p_uid, 0); 323 Pid = p->p_pid; 324 Comm = p->p_comm; 325 326 if (p->p_fd == NULL || p->p_cwdi == NULL) 327 return; 328 if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) { 329 warnx("can't read filedesc at %#llx for pid %d", (unsigned long long)p->p_fd, Pid); 330 return; 331 } 332 if (!KVM_READ(p->p_cwdi, &cwdi, sizeof(cwdi))) { 333 warnx("can't read cwdinfo at %#llx for pid %d", (unsigned long long)p->p_cwdi, Pid); 334 return; 335 } 336 if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles || 337 filed.fd_freefile > filed.fd_lastfile + 1) { 338 dprintf("filedesc corrupted at %#llx for pid %d", (unsigned long long)p->p_fd, Pid); 339 return; 340 } 341 /* 342 * root directory vnode, if one 343 */ 344 if (cwdi.cwdi_rdir) 345 vtrans(cwdi.cwdi_rdir, RDIR, FREAD); 346 /* 347 * current working directory vnode 348 */ 349 vtrans(cwdi.cwdi_cdir, CDIR, FREAD); 350 /* 351 * ktrace vnode, if one 352 */ 353 if (p->p_tracep) 354 ftrans((struct file *)(intptr_t)p->p_tracep, TRACE); 355 /* 356 * open files 357 */ 358 #define FPSIZE (sizeof (struct file *)) 359 ALLOC_OFILES(filed.fd_lastfile+1); 360 if (filed.fd_nfiles > NDFILE) { 361 if (!KVM_READ(filed.fd_ofiles, ofiles, 362 (filed.fd_lastfile+1) * FPSIZE)) { 363 dprintf("can't read file structures at %p for pid %d", 364 filed.fd_ofiles, Pid); 365 return; 366 } 367 } else 368 memmove(ofiles, filed0.fd_dfiles, 369 (filed.fd_lastfile+1) * FPSIZE); 370 for (i = 0; i <= filed.fd_lastfile; i++) { 371 if (ofiles[i] == NULL) 372 continue; 373 ftrans(ofiles[i], i); 374 } 375 } 376 377 void 378 ftrans (fp, i) 379 struct file *fp; 380 int i; 381 { 382 struct file file; 383 384 if (!KVM_READ(fp, &file, sizeof (struct file))) { 385 dprintf("can't read file %d at %p for pid %d", 386 i, fp, Pid); 387 return; 388 } 389 switch (file.f_type) { 390 case DTYPE_VNODE: 391 vtrans((struct vnode *)file.f_data, i, file.f_flag); 392 break; 393 case DTYPE_SOCKET: 394 if (checkfile == 0) 395 socktrans((struct socket *)file.f_data, i); 396 break; 397 case DTYPE_PIPE: 398 if (checkfile == 0) 399 ptrans(&file, (struct pipe *)file.f_data, i); 400 break; 401 case DTYPE_KQUEUE: 402 if (checkfile == 0) 403 kqueuetrans((void *)file.f_data, i); 404 break; 405 default: 406 dprintf("unknown file type %d for file %d of pid %d", 407 file.f_type, i, Pid); 408 break; 409 } 410 } 411 412 char * 413 vfilestat(vp, fsp) 414 struct vnode *vp; 415 struct filestat *fsp; 416 { 417 char *badtype = NULL; 418 419 if (vp->v_type == VNON || vp->v_tag == VT_NON) 420 badtype = "none"; 421 else if (vp->v_type == VBAD) 422 badtype = "bad"; 423 else 424 switch (vp->v_tag) { 425 case VT_UFS: 426 case VT_LFS: 427 case VT_MFS: 428 if (!ufs_filestat(vp, fsp)) 429 badtype = "error"; 430 break; 431 case VT_MSDOSFS: 432 if (!msdosfs_filestat(vp, fsp)) 433 badtype = "error"; 434 break; 435 case VT_NFS: 436 if (!nfs_filestat(vp, fsp)) 437 badtype = "error"; 438 break; 439 case VT_EXT2FS: 440 if (!ext2fs_filestat(vp, fsp)) 441 badtype = "error"; 442 break; 443 case VT_ISOFS: 444 if (!isofs_filestat(vp, fsp)) 445 badtype = "error"; 446 break; 447 case VT_NTFS: 448 if (!ntfs_filestat(vp, fsp)) 449 badtype = "error"; 450 break; 451 case VT_NULL: 452 case VT_OVERLAY: 453 case VT_UMAP: 454 badtype = layer_filestat(vp, fsp); 455 break; 456 default: { 457 static char unknown[10]; 458 (void)snprintf(badtype = unknown, sizeof unknown, 459 "?(%x)", vp->v_tag); 460 break; 461 } 462 } 463 return (badtype); 464 } 465 466 void 467 vtrans(vp, i, flag) 468 struct vnode *vp; 469 int i; 470 int flag; 471 { 472 struct vnode vn; 473 struct filestat fst; 474 char mode[15], rw[3]; 475 char *badtype, *filename; 476 477 filename = NULL; 478 if (!KVM_READ(vp, &vn, sizeof(struct vnode))) { 479 dprintf("can't read vnode at %p for pid %d", vp, Pid); 480 return; 481 } 482 badtype = vfilestat(&vn, &fst); 483 if (checkfile) { 484 int fsmatch = 0; 485 DEVS *d; 486 487 if (badtype) 488 return; 489 for (d = devs; d != NULL; d = d->next) 490 if (d->fsid == fst.fsid) { 491 fsmatch = 1; 492 if (d->ino == fst.fileid) { 493 filename = d->name; 494 break; 495 } 496 } 497 if (fsmatch == 0 || (filename == NULL && fsflg == 0)) 498 return; 499 } 500 PREFIX(i); 501 if (badtype) { 502 (void)printf(" - - %10s -\n", badtype); 503 return; 504 } 505 if (nflg) 506 (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid)); 507 else 508 (void)printf(" %-8s", getmnton(vn.v_mount)); 509 if (nflg) 510 (void)snprintf(mode, sizeof mode, "%o", fst.mode); 511 else 512 strmode(fst.mode, mode); 513 (void)printf(" %7ld %*s", (long)fst.fileid, nflg ? 5 : 10, mode); 514 switch (vn.v_type) { 515 case VBLK: 516 case VCHR: { 517 char *name; 518 519 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ? 520 S_IFCHR : S_IFBLK)) == NULL)) 521 printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev)); 522 else 523 printf(" %6s", name); 524 break; 525 } 526 default: 527 printf(" %6lld", (long long)fst.size); 528 } 529 rw[0] = '\0'; 530 if (flag & FREAD) 531 strcat(rw, "r"); 532 if (flag & FWRITE) 533 strcat(rw, "w"); 534 printf(" %-2s", rw); 535 if (filename && !fsflg) 536 printf(" %s", filename); 537 putchar('\n'); 538 } 539 540 int 541 ufs_filestat(vp, fsp) 542 struct vnode *vp; 543 struct filestat *fsp; 544 { 545 struct inode inode; 546 union dinode { 547 struct ufs1_dinode dp1; 548 struct ufs2_dinode dp2; 549 } dip; 550 551 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 552 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid); 553 return 0; 554 } 555 556 if (!KVM_READ(inode.i_din.ffs1_din, &dip, sizeof(struct ufs1_dinode))) { 557 dprintf("can't read dinode at %p for pid %d", 558 inode.i_din.ffs1_din, Pid); 559 return 0; 560 } 561 if (inode.i_size == dip.dp1.di_size) 562 fsp->rdev = dip.dp1.di_rdev; 563 else { 564 if (!KVM_READ(inode.i_din.ffs1_din, &dip, 565 sizeof(struct ufs2_dinode))) { 566 dprintf("can't read dinode at %p for pid %d", 567 inode.i_din.ffs1_din, Pid); 568 return 0; 569 } 570 fsp->rdev = dip.dp2.di_rdev; 571 } 572 573 fsp->fsid = inode.i_dev & 0xffff; 574 fsp->fileid = (long)inode.i_number; 575 fsp->mode = (mode_t)inode.i_mode; 576 fsp->size = inode.i_size; 577 578 return 1; 579 } 580 581 int 582 ext2fs_filestat(vp, fsp) 583 struct vnode *vp; 584 struct filestat *fsp; 585 { 586 struct inode inode; 587 588 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 589 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid); 590 return 0; 591 } 592 fsp->fsid = inode.i_dev & 0xffff; 593 fsp->fileid = (long)inode.i_number; 594 fsp->mode = (mode_t)inode.i_e2fs_mode; 595 fsp->size = inode.i_e2fs_size; 596 fsp->rdev = 0; /* XXX */ 597 return 1; 598 } 599 600 int 601 nfs_filestat(vp, fsp) 602 struct vnode *vp; 603 struct filestat *fsp; 604 { 605 struct nfsnode nfsnode; 606 struct vattr va; 607 608 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) { 609 dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp), 610 Pid); 611 return 0; 612 } 613 if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) { 614 dprintf("can't read vnode attributes at %p for pid %d", 615 nfsnode.n_vattr, Pid); 616 return 0; 617 } 618 fsp->fsid = va.va_fsid; 619 fsp->fileid = va.va_fileid; 620 fsp->size = nfsnode.n_size; 621 fsp->rdev = va.va_rdev; 622 fsp->mode = (mode_t)va.va_mode | getftype(vp->v_type); 623 624 return 1; 625 } 626 627 int 628 msdosfs_filestat(vp, fsp) 629 struct vnode *vp; 630 struct filestat *fsp; 631 { 632 struct denode de; 633 struct msdosfsmount mp; 634 635 if (!KVM_READ(VTONFS(vp), &de, sizeof(de))) { 636 dprintf("can't read denode at %p for pid %d", VTONFS(vp), 637 Pid); 638 return 0; 639 } 640 if (!KVM_READ(de.de_pmp, &mp, sizeof(mp))) { 641 dprintf("can't read mount struct at %p for pid %d", de.de_pmp, 642 Pid); 643 return 0; 644 } 645 646 fsp->fsid = de.de_dev & 0xffff; 647 fsp->fileid = 0; /* XXX see msdosfs_vptofh() for more info */ 648 fsp->size = de.de_FileSize; 649 fsp->rdev = 0; /* msdosfs doesn't support device files */ 650 fsp->mode = (0777 & mp.pm_mask) | getftype(vp->v_type); 651 return 1; 652 } 653 654 char * 655 layer_filestat(vp, fsp) 656 struct vnode *vp; 657 struct filestat *fsp; 658 { 659 struct layer_node layer_node; 660 struct mount mount; 661 struct vnode vn; 662 char *badtype; 663 664 if (!KVM_READ(VTOLAYER(vp), &layer_node, sizeof(layer_node))) { 665 dprintf("can't read layer_node at %p for pid %d", 666 VTOLAYER(vp), Pid); 667 return ("error"); 668 } 669 if (!KVM_READ(vp->v_mount, &mount, sizeof(struct mount))) { 670 dprintf("can't read mount struct at %p for pid %d", 671 vp->v_mount, Pid); 672 return ("error"); 673 } 674 vp = layer_node.layer_lowervp; 675 if (!KVM_READ(vp, &vn, sizeof(struct vnode))) { 676 dprintf("can't read vnode at %p for pid %d", vp, Pid); 677 return ("error"); 678 } 679 if ((badtype = vfilestat(&vn, fsp)) == NULL) 680 fsp->fsid = mount.mnt_stat.f_fsid.val[0]; 681 return (badtype); 682 } 683 684 char * 685 getmnton(m) 686 struct mount *m; 687 { 688 static struct mount mount; 689 static struct mtab { 690 struct mtab *next; 691 struct mount *m; 692 char mntonname[MNAMELEN]; 693 } *mhead = NULL; 694 struct mtab *mt; 695 696 for (mt = mhead; mt != NULL; mt = mt->next) 697 if (m == mt->m) 698 return (mt->mntonname); 699 if (!KVM_READ(m, &mount, sizeof(struct mount))) { 700 warnx("can't read mount table at %p", m); 701 return (NULL); 702 } 703 if ((mt = malloc(sizeof (struct mtab))) == NULL) { 704 err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab)); 705 } 706 mt->m = m; 707 memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0], MNAMELEN); 708 mt->next = mhead; 709 mhead = mt; 710 return (mt->mntonname); 711 } 712 713 #ifdef INET6 714 static const char * 715 inet6_addrstr(p) 716 struct in6_addr *p; 717 { 718 struct sockaddr_in6 sin6; 719 static char hbuf[NI_MAXHOST]; 720 #ifdef NI_WITHSCOPEID 721 const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID; 722 #else 723 const int niflags = NI_NUMERICHOST; 724 #endif 725 726 memset(&sin6, 0, sizeof(sin6)); 727 sin6.sin6_family = AF_INET6; 728 sin6.sin6_len = sizeof(struct sockaddr_in6); 729 sin6.sin6_addr = *p; 730 if (IN6_IS_ADDR_LINKLOCAL(p) && 731 *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) { 732 sin6.sin6_scope_id = 733 ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]); 734 sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0; 735 } 736 737 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, 738 hbuf, sizeof(hbuf), NULL, 0, niflags)) 739 return "invalid"; 740 741 return hbuf; 742 } 743 #endif 744 745 void 746 socktrans(sock, i) 747 struct socket *sock; 748 int i; 749 { 750 static char *stypename[] = { 751 "unused", /* 0 */ 752 "stream", /* 1 */ 753 "dgram", /* 2 */ 754 "raw", /* 3 */ 755 "rdm", /* 4 */ 756 "seqpak" /* 5 */ 757 }; 758 #define STYPEMAX 5 759 struct socket so; 760 struct protosw proto; 761 struct domain dom; 762 struct inpcb inpcb; 763 #ifdef INET6 764 struct in6pcb in6pcb; 765 #endif 766 struct unpcb unpcb; 767 int len; 768 char dname[32]; 769 #ifdef INET6 770 char xaddrbuf[NI_MAXHOST + 2]; 771 #endif 772 773 PREFIX(i); 774 775 /* fill in socket */ 776 if (!KVM_READ(sock, &so, sizeof(struct socket))) { 777 dprintf("can't read sock at %p", sock); 778 goto bad; 779 } 780 781 /* fill in protosw entry */ 782 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) { 783 dprintf("can't read protosw at %p", so.so_proto); 784 goto bad; 785 } 786 787 /* fill in domain */ 788 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) { 789 dprintf("can't read domain at %p", proto.pr_domain); 790 goto bad; 791 } 792 793 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname, 794 sizeof(dname) - 1)) != sizeof(dname) -1) { 795 dprintf("can't read domain name at %p", dom.dom_name); 796 dname[0] = '\0'; 797 } 798 else 799 dname[len] = '\0'; 800 801 if ((u_short)so.so_type > STYPEMAX) 802 printf("* %s ?%d", dname, so.so_type); 803 else 804 printf("* %s %s", dname, stypename[so.so_type]); 805 806 /* 807 * protocol specific formatting 808 * 809 * Try to find interesting things to print. For TCP, the interesting 810 * thing is the address of the tcpcb, for UDP and others, just the 811 * inpcb (socket pcb). For UNIX domain, its the address of the socket 812 * pcb and the address of the connected pcb (if connected). Otherwise 813 * just print the protocol number and address of the socket itself. 814 * The idea is not to duplicate netstat, but to make available enough 815 * information for further analysis. 816 */ 817 switch(dom.dom_family) { 818 case AF_INET: 819 getinetproto(proto.pr_protocol); 820 if (proto.pr_protocol == IPPROTO_TCP) { 821 if (so.so_pcb == NULL) 822 break; 823 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb, 824 sizeof(struct inpcb)) != sizeof(struct inpcb)) { 825 dprintf("can't read inpcb at %p", so.so_pcb); 826 goto bad; 827 } 828 printf(" %lx", (long)inpcb.inp_ppcb); 829 printf(" %s:%d", 830 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" : 831 inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport)); 832 if (inpcb.inp_fport) { 833 printf(" <-> %s:%d", 834 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" : 835 inet_ntoa(inpcb.inp_faddr), 836 ntohs(inpcb.inp_fport)); 837 } 838 } else if (proto.pr_protocol == IPPROTO_UDP) { 839 if (so.so_pcb == NULL) 840 break; 841 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb, 842 sizeof(struct inpcb)) != sizeof(struct inpcb)) { 843 dprintf("can't read inpcb at %p", so.so_pcb); 844 goto bad; 845 } 846 printf(" %lx", (long)so.so_pcb); 847 printf(" %s:%d", 848 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" : 849 inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport)); 850 if (inpcb.inp_fport) 851 printf(" <-> %s:%d", 852 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" : 853 inet_ntoa(inpcb.inp_faddr), 854 ntohs(inpcb.inp_fport)); 855 } else if (so.so_pcb) 856 printf(" %lx", (long)so.so_pcb); 857 break; 858 #ifdef INET6 859 case AF_INET6: 860 getinetproto(proto.pr_protocol); 861 if (proto.pr_protocol == IPPROTO_TCP) { 862 if (so.so_pcb == NULL) 863 break; 864 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb, 865 sizeof(struct in6pcb)) != sizeof(struct in6pcb)) { 866 dprintf("can't read in6pcb at %p", so.so_pcb); 867 goto bad; 868 } 869 printf(" %lx", (long)in6pcb.in6p_ppcb); 870 sprintf(xaddrbuf, "[%s]", 871 inet6_addrstr(&in6pcb.in6p_laddr)); 872 printf(" %s:%d", 873 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" : 874 xaddrbuf, 875 ntohs(in6pcb.in6p_lport)); 876 if (in6pcb.in6p_fport) { 877 sprintf(xaddrbuf, "[%s]", 878 inet6_addrstr(&in6pcb.in6p_faddr)); 879 printf(" <-> %s:%d", 880 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" : 881 xaddrbuf, 882 ntohs(in6pcb.in6p_fport)); 883 } 884 } else if (proto.pr_protocol == IPPROTO_UDP) { 885 if (so.so_pcb == NULL) 886 break; 887 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb, 888 sizeof(struct in6pcb)) != sizeof(struct in6pcb)) { 889 dprintf("can't read inpcb at %p", so.so_pcb); 890 goto bad; 891 } 892 printf(" %lx", (long)so.so_pcb); 893 sprintf(xaddrbuf, "[%s]", 894 inet6_addrstr(&in6pcb.in6p_laddr)); 895 printf(" %s:%d", 896 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" : 897 xaddrbuf, 898 ntohs(in6pcb.in6p_lport)); 899 if (in6pcb.in6p_fport) { 900 sprintf(xaddrbuf, "[%s]", 901 inet6_addrstr(&in6pcb.in6p_faddr)); 902 printf(" <-> %s:%d", 903 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" : 904 xaddrbuf, 905 ntohs(in6pcb.in6p_fport)); 906 } 907 } else if (so.so_pcb) 908 printf(" %lx", (long)so.so_pcb); 909 break; 910 #endif 911 case AF_LOCAL: 912 /* print address of pcb and connected pcb */ 913 if (so.so_pcb) { 914 printf(" %lx", (long)so.so_pcb); 915 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb, 916 sizeof(struct unpcb)) != sizeof(struct unpcb)){ 917 dprintf("can't read unpcb at %p", so.so_pcb); 918 goto bad; 919 } 920 if (unpcb.unp_conn) { 921 char shoconn[4], *cp; 922 923 cp = shoconn; 924 if (!(so.so_state & SS_CANTRCVMORE)) 925 *cp++ = '<'; 926 *cp++ = '-'; 927 if (!(so.so_state & SS_CANTSENDMORE)) 928 *cp++ = '>'; 929 *cp = '\0'; 930 printf(" %s %lx", shoconn, 931 (long)unpcb.unp_conn); 932 } 933 } 934 break; 935 default: 936 /* print protocol number and socket address */ 937 printf(" %d %lx", proto.pr_protocol, (long)sock); 938 } 939 printf("\n"); 940 return; 941 bad: 942 printf("* error\n"); 943 } 944 945 void 946 ptrans(fp, cpipe, i) 947 struct file *fp; 948 struct pipe *cpipe; 949 int i; 950 { 951 struct pipe cp; 952 953 PREFIX(i); 954 955 /* fill in pipe */ 956 if (!KVM_READ(cpipe, &cp, sizeof(struct pipe))) { 957 dprintf("can't read pipe at %p", cpipe); 958 goto bad; 959 } 960 961 /* pipe descriptor is either read or write, never both */ 962 printf("* pipe %p %s %p %s%s%s", cpipe, 963 (fp->f_flag & FWRITE) ? "->" : "<-", 964 cp.pipe_peer, 965 (fp->f_flag & FWRITE) ? "w" : "r", 966 (fp->f_flag & FNONBLOCK) ? "n" : "", 967 (cp.pipe_state & PIPE_ASYNC) ? "a" : ""); 968 printf("\n"); 969 return; 970 bad: 971 printf("* error\n"); 972 } 973 974 void 975 kqueuetrans(kq, i) 976 void *kq; 977 int i; 978 { 979 980 PREFIX(i); 981 printf("* kqueue %lx", (long)kq); 982 printf("\n"); 983 } 984 985 /* 986 * getinetproto -- 987 * print name of protocol number 988 */ 989 void 990 getinetproto(number) 991 int number; 992 { 993 char *cp; 994 995 switch (number) { 996 case IPPROTO_IP: 997 cp = "ip"; break; 998 case IPPROTO_ICMP: 999 cp ="icmp"; break; 1000 case IPPROTO_GGP: 1001 cp ="ggp"; break; 1002 case IPPROTO_TCP: 1003 cp ="tcp"; break; 1004 case IPPROTO_EGP: 1005 cp ="egp"; break; 1006 case IPPROTO_PUP: 1007 cp ="pup"; break; 1008 case IPPROTO_UDP: 1009 cp ="udp"; break; 1010 case IPPROTO_IDP: 1011 cp ="idp"; break; 1012 case IPPROTO_RAW: 1013 cp ="raw"; break; 1014 case IPPROTO_ICMPV6: 1015 cp ="icmp6"; break; 1016 default: 1017 printf(" %d", number); 1018 return; 1019 } 1020 printf(" %s", cp); 1021 } 1022 1023 int 1024 getfname(filename) 1025 char *filename; 1026 { 1027 struct stat statbuf; 1028 DEVS *cur; 1029 1030 if (stat(filename, &statbuf)) { 1031 warn("stat(%s)", filename); 1032 return(0); 1033 } 1034 if ((cur = malloc(sizeof(DEVS))) == NULL) { 1035 err(1, "malloc(%u)", (unsigned int)sizeof(DEVS)); 1036 } 1037 cur->next = devs; 1038 devs = cur; 1039 1040 cur->ino = statbuf.st_ino; 1041 cur->fsid = statbuf.st_dev & 0xffff; 1042 cur->name = filename; 1043 return(1); 1044 } 1045 1046 mode_t 1047 getftype(v_type) 1048 enum vtype v_type; 1049 { 1050 mode_t ftype; 1051 1052 switch (v_type) { 1053 case VREG: 1054 ftype = S_IFREG; 1055 break; 1056 case VDIR: 1057 ftype = S_IFDIR; 1058 break; 1059 case VBLK: 1060 ftype = S_IFBLK; 1061 break; 1062 case VCHR: 1063 ftype = S_IFCHR; 1064 break; 1065 case VLNK: 1066 ftype = S_IFLNK; 1067 break; 1068 case VSOCK: 1069 ftype = S_IFSOCK; 1070 break; 1071 case VFIFO: 1072 ftype = S_IFIFO; 1073 break; 1074 default: 1075 ftype = 0; 1076 break; 1077 }; 1078 1079 return ftype; 1080 } 1081 1082 void 1083 usage() 1084 { 1085 errx(1, 1086 "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n"); 1087 } 1088