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