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