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 35 static 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 42 static char sccsid[] = "@(#)fstat.c 8.3 (Berkeley) 5/2/95"; 43 #endif 44 #endif /* not lint */ 45 #include <sys/cdefs.h> 46 __FBSDID("$FreeBSD$"); 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 #undef _KERNEL 72 #include <nfs/nfsproto.h> 73 #include <nfs/rpcv2.h> 74 #include <nfsclient/nfs.h> 75 #include <nfsclient/nfsnode.h> 76 77 78 #include <vm/vm.h> 79 #include <vm/vm_map.h> 80 #include <vm/vm_object.h> 81 82 #include <net/route.h> 83 #include <netinet/in.h> 84 #include <netinet/in_systm.h> 85 #include <netinet/ip.h> 86 #include <netinet/in_pcb.h> 87 88 #include <ctype.h> 89 #include <err.h> 90 #include <fcntl.h> 91 #include <kvm.h> 92 #include <limits.h> 93 #include <nlist.h> 94 #include <paths.h> 95 #include <pwd.h> 96 #include <stdio.h> 97 #include <stdlib.h> 98 #include <string.h> 99 #include <unistd.h> 100 #include <netdb.h> 101 102 #include "fstat.h" 103 104 #define TEXT -1 105 #define CDIR -2 106 #define RDIR -3 107 #define TRACE -4 108 #define MMAP -5 109 #define JDIR -6 110 111 DEVS *devs; 112 113 #ifdef notdef 114 struct nlist nl[] = { 115 { "" }, 116 }; 117 #endif 118 119 int fsflg, /* show files on same filesystem as file(s) argument */ 120 pflg, /* show files open by a particular pid */ 121 uflg; /* show files open by a particular (effective) user */ 122 int checkfile; /* true if restricting to particular files or filesystems */ 123 int nflg; /* (numerical) display f.s. and rdev as dev_t */ 124 int vflg; /* display errors in locating kernel data objects etc... */ 125 int mflg; /* include memory-mapped files */ 126 127 128 struct file **ofiles; /* buffer of pointers to file structures */ 129 int maxfiles; 130 #define ALLOC_OFILES(d) \ 131 if ((d) > maxfiles) { \ 132 free(ofiles); \ 133 ofiles = malloc((d) * sizeof(struct file *)); \ 134 if (ofiles == NULL) { \ 135 err(1, NULL); \ 136 } \ 137 maxfiles = (d); \ 138 } 139 140 char *memf, *nlistf; 141 kvm_t *kd; 142 143 static void fstat_kvm(int, int); 144 static void fstat_sysctl(int, int); 145 void dofiles(struct kinfo_proc *kp); 146 void dommap(struct kinfo_proc *kp); 147 void vtrans(struct vnode *vp, int i, int flag); 148 int ufs_filestat(struct vnode *vp, struct filestat *fsp); 149 int nfs_filestat(struct vnode *vp, struct filestat *fsp); 150 int devfs_filestat(struct vnode *vp, struct filestat *fsp); 151 char *getmnton(struct mount *m); 152 void pipetrans(struct pipe *pi, int i, int flag); 153 void socktrans(struct socket *sock, int i); 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_ALL; 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 vtrans(filed.fd_cdir, CDIR, FREAD); 351 /* 352 * jail root, if any. 353 */ 354 if (filed.fd_jdir) 355 vtrans(filed.fd_jdir, JDIR, FREAD); 356 /* 357 * ktrace vnode, if one 358 */ 359 if (kp->ki_tracep) 360 vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE); 361 /* 362 * text vnode, if one 363 */ 364 if (kp->ki_textvp) 365 vtrans(kp->ki_textvp, TEXT, FREAD); 366 /* 367 * open files 368 */ 369 #define FPSIZE (sizeof (struct file *)) 370 #define MAX_LASTFILE (0x1000000) 371 372 /* Sanity check on filed.fd_lastfile */ 373 if (filed.fd_lastfile <= -1 || filed.fd_lastfile > MAX_LASTFILE) 374 return; 375 376 ALLOC_OFILES(filed.fd_lastfile+1); 377 if (!KVM_READ(filed.fd_ofiles, ofiles, 378 (filed.fd_lastfile+1) * FPSIZE)) { 379 dprintf(stderr, 380 "can't read file structures at %p for pid %d\n", 381 (void *)filed.fd_ofiles, Pid); 382 return; 383 } 384 for (i = 0; i <= filed.fd_lastfile; i++) { 385 if (ofiles[i] == NULL) 386 continue; 387 if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) { 388 dprintf(stderr, "can't read file %d at %p for pid %d\n", 389 i, (void *)ofiles[i], Pid); 390 continue; 391 } 392 if (file.f_type == DTYPE_VNODE) 393 vtrans(file.f_vnode, i, file.f_flag); 394 else if (file.f_type == DTYPE_SOCKET) { 395 if (checkfile == 0) 396 socktrans(file.f_data, i); 397 } 398 #ifdef DTYPE_PIPE 399 else if (file.f_type == DTYPE_PIPE) { 400 if (checkfile == 0) 401 pipetrans(file.f_data, i, file.f_flag); 402 } 403 #endif 404 #ifdef DTYPE_FIFO 405 else if (file.f_type == DTYPE_FIFO) { 406 if (checkfile == 0) 407 vtrans(file.f_vnode, i, file.f_flag); 408 } 409 #endif 410 else { 411 dprintf(stderr, 412 "unknown file type %d for file %d of pid %d\n", 413 file.f_type, i, Pid); 414 } 415 } 416 } 417 418 void 419 dommap(struct kinfo_proc *kp) 420 { 421 vm_map_t map; 422 struct vmspace vmspace; 423 struct vm_map_entry entry; 424 vm_map_entry_t entryp; 425 struct vm_object object; 426 vm_object_t objp; 427 int prot, fflags; 428 429 if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) { 430 dprintf(stderr, 431 "can't read vmspace at %p for pid %d\n", 432 (void *)kp->ki_vmspace, Pid); 433 return; 434 } 435 map = &vmspace.vm_map; 436 437 for (entryp = map->header.next; 438 entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) { 439 if (!KVM_READ(entryp, &entry, sizeof(entry))) { 440 dprintf(stderr, 441 "can't read vm_map_entry at %p for pid %d\n", 442 (void *)entryp, Pid); 443 return; 444 } 445 446 if (entry.eflags & MAP_ENTRY_IS_SUB_MAP) 447 continue; 448 449 if ((objp = entry.object.vm_object) == NULL) 450 continue; 451 452 for (; objp; objp = object.backing_object) { 453 if (!KVM_READ(objp, &object, sizeof(object))) { 454 dprintf(stderr, 455 "can't read vm_object at %p for pid %d\n", 456 (void *)objp, Pid); 457 return; 458 } 459 } 460 461 prot = entry.protection; 462 fflags = (prot & VM_PROT_READ ? FREAD : 0) | 463 (prot & VM_PROT_WRITE ? FWRITE : 0); 464 465 switch (object.type) { 466 case OBJT_VNODE: 467 vtrans((struct vnode *)object.handle, MMAP, fflags); 468 break; 469 default: 470 break; 471 } 472 } 473 } 474 475 char * 476 kdevtoname(struct cdev *dev) 477 { 478 struct cdev si; 479 480 if (!KVM_READ(dev, &si, sizeof si)) 481 return (NULL); 482 return (strdup(si.__si_namebuf)); 483 } 484 485 void 486 vtrans(struct vnode *vp, int i, int flag) 487 { 488 struct vnode vn; 489 struct filestat fst; 490 char rw[3], mode[15], tagstr[12], *tagptr; 491 const char *badtype, *filename; 492 493 filename = badtype = NULL; 494 if (!KVM_READ(vp, &vn, sizeof (struct vnode))) { 495 dprintf(stderr, "can't read vnode at %p for pid %d\n", 496 (void *)vp, Pid); 497 return; 498 } 499 if (!KVM_READ(&vp->v_tag, &tagptr, sizeof tagptr) || 500 !KVM_READ(tagptr, tagstr, sizeof tagstr)) { 501 dprintf(stderr, "can't read v_tag at %p for pid %d\n", 502 (void *)vp, Pid); 503 return; 504 } 505 tagstr[sizeof(tagstr) - 1] = '\0'; 506 if (vn.v_type == VNON) 507 badtype = "none"; 508 else if (vn.v_type == VBAD) 509 badtype = "bad"; 510 else { 511 if (!strcmp("ufs", tagstr)) { 512 if (!ufs_filestat(&vn, &fst)) 513 badtype = "error"; 514 } else if (!strcmp("devfs", tagstr)) { 515 if (!devfs_filestat(&vn, &fst)) 516 badtype = "error"; 517 } else if (!strcmp("nfs", tagstr)) { 518 if (!nfs_filestat(&vn, &fst)) 519 badtype = "error"; 520 } else if (!strcmp("msdosfs", tagstr)) { 521 if (!msdosfs_filestat(&vn, &fst)) 522 badtype = "error"; 523 } else if (!strcmp("isofs", tagstr)) { 524 if (!isofs_filestat(&vn, &fst)) 525 badtype = "error"; 526 } else { 527 static char unknown[32]; 528 snprintf(unknown, sizeof unknown, "?(%s)", tagstr); 529 badtype = unknown; 530 } 531 } 532 if (checkfile) { 533 int fsmatch = 0; 534 DEVS *d; 535 536 if (badtype) 537 return; 538 for (d = devs; d != NULL; d = d->next) 539 if (d->fsid == fst.fsid) { 540 fsmatch = 1; 541 if (d->ino == fst.fileid) { 542 filename = d->name; 543 break; 544 } 545 } 546 if (fsmatch == 0 || (filename == NULL && fsflg == 0)) 547 return; 548 } 549 PREFIX(i); 550 if (badtype) { 551 (void)printf(" - - %10s -\n", badtype); 552 return; 553 } 554 if (nflg) 555 (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid)); 556 else 557 (void)printf(" %-8s", getmnton(vn.v_mount)); 558 if (nflg) 559 (void)sprintf(mode, "%o", fst.mode); 560 else 561 strmode(fst.mode, mode); 562 (void)printf(" %6ld %10s", fst.fileid, mode); 563 switch (vn.v_type) { 564 case VBLK: 565 case VCHR: { 566 char *name; 567 568 name = kdevtoname(vn.v_rdev); 569 if (nflg || !name) 570 printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev)); 571 else { 572 printf(" %6s", name); 573 free(name); 574 } 575 break; 576 } 577 default: 578 printf(" %6lu", fst.size); 579 } 580 rw[0] = '\0'; 581 if (flag & FREAD) 582 strcat(rw, "r"); 583 if (flag & FWRITE) 584 strcat(rw, "w"); 585 printf(" %2s", rw); 586 if (filename && !fsflg) 587 printf(" %s", filename); 588 putchar('\n'); 589 } 590 591 int 592 ufs_filestat(struct vnode *vp, struct filestat *fsp) 593 { 594 struct inode inode; 595 596 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 597 dprintf(stderr, "can't read inode at %p for pid %d\n", 598 (void *)VTOI(vp), Pid); 599 return 0; 600 } 601 /* 602 * The st_dev from stat(2) is a dev_t. These kernel structures 603 * contain cdev pointers. We need to convert to dev_t to make 604 * comparisons 605 */ 606 fsp->fsid = dev2udev(inode.i_dev); 607 fsp->fileid = (long)inode.i_number; 608 fsp->mode = (mode_t)inode.i_mode; 609 fsp->size = (u_long)inode.i_size; 610 #if should_be_but_is_hard 611 /* XXX - need to load i_ump and i_din[12] from kernel memory */ 612 if (inode.i_ump->um_fstype == UFS1) 613 fsp->rdev = inode.i_din1->di_rdev; 614 else 615 fsp->rdev = inode.i_din2->di_rdev; 616 #else 617 fsp->rdev = 0; 618 #endif 619 620 return 1; 621 } 622 623 int 624 devfs_filestat(struct vnode *vp, struct filestat *fsp) 625 { 626 struct devfs_dirent devfs_dirent; 627 struct mount mount; 628 struct vnode vnode; 629 630 if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) { 631 dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n", 632 (void *)vp->v_data, Pid); 633 return 0; 634 } 635 if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) { 636 dprintf(stderr, "can't read mount at %p for pid %d\n", 637 (void *)vp->v_mount, Pid); 638 return 0; 639 } 640 if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) { 641 dprintf(stderr, "can't read vnode at %p for pid %d\n", 642 (void *)devfs_dirent.de_vnode, Pid); 643 return 0; 644 } 645 fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0]; 646 fsp->fileid = devfs_dirent.de_inode; 647 fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR; 648 fsp->size = 0; 649 fsp->rdev = dev2udev(vnode.v_rdev); 650 651 return 1; 652 } 653 654 int 655 nfs_filestat(struct vnode *vp, struct filestat *fsp) 656 { 657 struct nfsnode nfsnode; 658 mode_t mode; 659 660 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) { 661 dprintf(stderr, "can't read nfsnode at %p for pid %d\n", 662 (void *)VTONFS(vp), Pid); 663 return 0; 664 } 665 fsp->fsid = nfsnode.n_vattr.va_fsid; 666 fsp->fileid = nfsnode.n_vattr.va_fileid; 667 fsp->size = nfsnode.n_size; 668 fsp->rdev = nfsnode.n_vattr.va_rdev; 669 mode = (mode_t)nfsnode.n_vattr.va_mode; 670 switch (vp->v_type) { 671 case VREG: 672 mode |= S_IFREG; 673 break; 674 case VDIR: 675 mode |= S_IFDIR; 676 break; 677 case VBLK: 678 mode |= S_IFBLK; 679 break; 680 case VCHR: 681 mode |= S_IFCHR; 682 break; 683 case VLNK: 684 mode |= S_IFLNK; 685 break; 686 case VSOCK: 687 mode |= S_IFSOCK; 688 break; 689 case VFIFO: 690 mode |= S_IFIFO; 691 break; 692 case VNON: 693 case VBAD: 694 return 0; 695 }; 696 fsp->mode = mode; 697 698 return 1; 699 } 700 701 702 char * 703 getmnton(struct mount *m) 704 { 705 static struct mount mount; 706 static struct mtab { 707 struct mtab *next; 708 struct mount *m; 709 char mntonname[MNAMELEN]; 710 } *mhead = NULL; 711 struct mtab *mt; 712 713 for (mt = mhead; mt != NULL; mt = mt->next) 714 if (m == mt->m) 715 return (mt->mntonname); 716 if (!KVM_READ(m, &mount, sizeof(struct mount))) { 717 warnx("can't read mount table at %p", (void *)m); 718 return (NULL); 719 } 720 if ((mt = malloc(sizeof (struct mtab))) == NULL) 721 err(1, NULL); 722 mt->m = m; 723 bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN); 724 mt->next = mhead; 725 mhead = mt; 726 return (mt->mntonname); 727 } 728 729 void 730 pipetrans(struct pipe *pi, int i, int flag) 731 { 732 struct pipe pip; 733 char rw[3]; 734 735 PREFIX(i); 736 737 /* fill in socket */ 738 if (!KVM_READ(pi, &pip, sizeof(struct pipe))) { 739 dprintf(stderr, "can't read pipe at %p\n", (void *)pi); 740 goto bad; 741 } 742 743 printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer); 744 printf(" %6d", (int)pip.pipe_buffer.cnt); 745 rw[0] = '\0'; 746 if (flag & FREAD) 747 strcat(rw, "r"); 748 if (flag & FWRITE) 749 strcat(rw, "w"); 750 printf(" %2s", rw); 751 putchar('\n'); 752 return; 753 754 bad: 755 printf("* error\n"); 756 } 757 758 void 759 socktrans(struct socket *sock, int i) 760 { 761 static const char *stypename[] = { 762 "unused", /* 0 */ 763 "stream", /* 1 */ 764 "dgram", /* 2 */ 765 "raw", /* 3 */ 766 "rdm", /* 4 */ 767 "seqpak" /* 5 */ 768 }; 769 #define STYPEMAX 5 770 struct socket so; 771 struct protosw proto; 772 struct domain dom; 773 struct inpcb inpcb; 774 struct unpcb unpcb; 775 int len; 776 char dname[32]; 777 778 PREFIX(i); 779 780 /* fill in socket */ 781 if (!KVM_READ(sock, &so, sizeof(struct socket))) { 782 dprintf(stderr, "can't read sock at %p\n", (void *)sock); 783 goto bad; 784 } 785 786 /* fill in protosw entry */ 787 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) { 788 dprintf(stderr, "can't read protosw at %p", 789 (void *)so.so_proto); 790 goto bad; 791 } 792 793 /* fill in domain */ 794 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) { 795 dprintf(stderr, "can't read domain at %p\n", 796 (void *)proto.pr_domain); 797 goto bad; 798 } 799 800 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname, 801 sizeof(dname) - 1)) < 0) { 802 dprintf(stderr, "can't read domain name at %p\n", 803 (void *)dom.dom_name); 804 dname[0] = '\0'; 805 } 806 else 807 dname[len] = '\0'; 808 809 if ((u_short)so.so_type > STYPEMAX) 810 printf("* %s ?%d", dname, so.so_type); 811 else 812 printf("* %s %s", dname, stypename[so.so_type]); 813 814 /* 815 * protocol specific formatting 816 * 817 * Try to find interesting things to print. For tcp, the interesting 818 * thing is the address of the tcpcb, for udp and others, just the 819 * inpcb (socket pcb). For unix domain, its the address of the socket 820 * pcb and the address of the connected pcb (if connected). Otherwise 821 * just print the protocol number and address of the socket itself. 822 * The idea is not to duplicate netstat, but to make available enough 823 * information for further analysis. 824 */ 825 switch(dom.dom_family) { 826 case AF_INET: 827 case AF_INET6: 828 getinetproto(proto.pr_protocol); 829 if (proto.pr_protocol == IPPROTO_TCP ) { 830 if (so.so_pcb) { 831 if (kvm_read(kd, (u_long)so.so_pcb, 832 (char *)&inpcb, sizeof(struct inpcb)) 833 != sizeof(struct inpcb)) { 834 dprintf(stderr, 835 "can't read inpcb at %p\n", 836 (void *)so.so_pcb); 837 goto bad; 838 } 839 printf(" %lx", (u_long)inpcb.inp_ppcb); 840 } 841 } 842 else if (so.so_pcb) 843 printf(" %lx", (u_long)so.so_pcb); 844 break; 845 case AF_UNIX: 846 /* print address of pcb and connected pcb */ 847 if (so.so_pcb) { 848 printf(" %lx", (u_long)so.so_pcb); 849 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb, 850 sizeof(struct unpcb)) != sizeof(struct unpcb)){ 851 dprintf(stderr, "can't read unpcb at %p\n", 852 (void *)so.so_pcb); 853 goto bad; 854 } 855 if (unpcb.unp_conn) { 856 char shoconn[4], *cp; 857 858 cp = shoconn; 859 if (!(so.so_rcv.sb_state & SBS_CANTRCVMORE)) 860 *cp++ = '<'; 861 *cp++ = '-'; 862 if (!(so.so_snd.sb_state & SBS_CANTSENDMORE)) 863 *cp++ = '>'; 864 *cp = '\0'; 865 printf(" %s %lx", shoconn, 866 (u_long)unpcb.unp_conn); 867 } 868 } 869 break; 870 default: 871 /* print protocol number and socket address */ 872 printf(" %d %lx", proto.pr_protocol, (u_long)sock); 873 } 874 printf("\n"); 875 return; 876 bad: 877 printf("* error\n"); 878 } 879 880 881 /* 882 * Read the cdev structure in the kernel in order to work out the 883 * associated dev_t 884 */ 885 dev_t 886 dev2udev(struct cdev *dev) 887 { 888 struct cdev si; 889 890 if (KVM_READ(dev, &si, sizeof si)) { 891 /* XXX: FIXME! */ 892 return 0; 893 } else { 894 dprintf(stderr, "can't convert cdev *%p to a dev_t\n", dev); 895 return -1; 896 } 897 } 898 899 /* 900 * getinetproto -- 901 * print name of protocol number 902 */ 903 void 904 getinetproto(int number) 905 { 906 static int isopen; 907 struct protoent *pe; 908 909 if (!isopen) 910 setprotoent(++isopen); 911 if ((pe = getprotobynumber(number)) != NULL) 912 printf(" %s", pe->p_name); 913 else 914 printf(" %d", number); 915 } 916 917 int 918 getfname(const char *filename) 919 { 920 struct stat statbuf; 921 DEVS *cur; 922 923 if (stat(filename, &statbuf)) { 924 warn("%s", filename); 925 return(0); 926 } 927 if ((cur = malloc(sizeof(DEVS))) == NULL) 928 err(1, NULL); 929 cur->next = devs; 930 devs = cur; 931 932 cur->ino = statbuf.st_ino; 933 cur->fsid = statbuf.st_dev; 934 cur->name = filename; 935 return(1); 936 } 937 938 void 939 usage(void) 940 { 941 (void)fprintf(stderr, 942 "usage: fstat [-fmnv] [-M core] [-N system] [-p pid] [-u user] [file ...]\n"); 943 exit(1); 944 } 945