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