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