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. 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 * @(#) Copyright (c) 1988, 1993 The Regents of the University of California. All rights reserved. 30 * @(#)fstat.c 8.3 (Berkeley) 5/2/95 31 * $FreeBSD: src/usr.bin/fstat/fstat.c,v 1.21.2.7 2001/11/21 10:49:37 dwmalone Exp $ 32 */ 33 34 #include <sys/user.h> 35 #include <sys/param.h> 36 #include <sys/time.h> 37 #include <sys/stat.h> 38 #include <sys/vnode.h> 39 #include <sys/socket.h> 40 #include <sys/socketvar.h> 41 #include <sys/domain.h> 42 #include <sys/protosw.h> 43 #include <sys/un.h> 44 #include <sys/unpcb.h> 45 #include <sys/sysctl.h> 46 #include <sys/filedesc.h> 47 #include <sys/queue.h> 48 #include <sys/pipe.h> 49 #include <sys/conf.h> 50 #include <sys/file.h> 51 #include <sys/ktrace.h> 52 #include <vfs/ufs/quota.h> 53 #include <vfs/ufs/inode.h> 54 #include <sys/mount.h> 55 #include <sys/namecache.h> 56 #include <vfs/nfs/nfsproto.h> 57 #include <vfs/nfs/rpcv2.h> 58 #include <vfs/nfs/nfs.h> 59 #include <vfs/nfs/nfsnode.h> 60 #include <sys/devfs.h> 61 62 #include <vm/vm.h> 63 #include <vm/vm_map.h> 64 #include <vm/vm_object.h> 65 66 #include <net/route.h> 67 #include <netinet/in.h> 68 #include <netinet/in_systm.h> 69 #include <netinet/ip.h> 70 #include <netinet/in_pcb.h> 71 72 #include <ctype.h> 73 #include <err.h> 74 #include <fcntl.h> 75 #include <kvm.h> 76 #include <limits.h> 77 #include <nlist.h> 78 #include <paths.h> 79 #include <pwd.h> 80 #include <stdio.h> 81 #include <stdlib.h> 82 #include <string.h> 83 #include <unistd.h> 84 #include <netdb.h> 85 86 #include "fstat.h" 87 88 #define TEXT -1 89 #define CDIR -2 90 #define RDIR -3 91 #define TRACE -4 92 #define MMAP -5 93 94 DEVS *devs; 95 96 static void make_printable(char *buf, int len); 97 98 #ifdef notdef 99 struct nlist nl[] = { 100 { "" }, 101 }; 102 #endif 103 104 int fsflg, /* show files on same filesystem as file(s) argument */ 105 pflg, /* show files open by a particular pid */ 106 uflg; /* show files open by a particular (effective) user */ 107 int checkfile; /* true if restricting to particular files or filesystems */ 108 int nflg; /* (numerical) display f.s. and rdev as dev_t */ 109 int vflg; /* display errors in locating kernel data objects etc... */ 110 int mflg; /* include memory-mapped files */ 111 int wflg_mnt = 16; 112 int wflg_cmd = 10; 113 int pid_width = 5; 114 int ino_width = 6; 115 116 const char *Uname; 117 char *Comm; 118 int Pid; 119 120 struct fdnode *ofiles; /* buffer of pointers to file structures */ 121 int maxfiles; 122 123 #define ALLOC_OFILES(d) \ 124 if ((d) > maxfiles) { \ 125 free(ofiles); \ 126 ofiles = malloc((d) * sizeof(struct fdnode)); \ 127 if (ofiles == NULL) { \ 128 err(1, NULL); \ 129 } \ 130 maxfiles = (d); \ 131 } 132 133 kvm_t *kd; 134 135 static void dofiles(struct kinfo_proc *, struct proc *); 136 static void dommap(struct proc *); 137 static void vtrans(struct vnode *, struct nchandle *, int, int, off_t); 138 static int ufs_filestat(struct vnode *, struct filestat *); 139 static int nfs_filestat(struct vnode *, struct filestat *); 140 static int devfs_filestat(struct vnode *, struct filestat *); 141 static char *getmnton(struct mount *, struct namecache_list *, struct nchandle *); 142 static void pipetrans(struct pipe *, int, int); 143 static void socktrans(struct socket *, int); 144 static void getinetproto(int); 145 static int getfname(const char *); 146 static void usage(void) __dead2; 147 148 149 int 150 main(int argc, char **argv) 151 { 152 struct passwd *passwd; 153 struct kinfo_proc *p, *plast; 154 struct proc proc; 155 int arg, ch, what; 156 char *memf, *nlistf; 157 char buf[_POSIX2_LINE_MAX]; 158 int cnt; 159 160 arg = 0; 161 what = KERN_PROC_ALL; 162 nlistf = memf = NULL; 163 while ((ch = getopt(argc, argv, "fmnp:u:vwN:M:")) != -1) 164 switch((char)ch) { 165 case 'f': 166 fsflg = 1; 167 break; 168 case 'M': 169 memf = optarg; 170 break; 171 case 'N': 172 nlistf = optarg; 173 break; 174 case 'm': 175 mflg = 1; 176 break; 177 case 'n': 178 nflg = 1; 179 break; 180 case 'p': 181 if (pflg++) 182 usage(); 183 if (!isdigit(*optarg)) { 184 warnx("-p requires a process id"); 185 usage(); 186 } 187 what = KERN_PROC_PID; 188 arg = atoi(optarg); 189 break; 190 case 'u': 191 if (uflg++) 192 usage(); 193 if (!(passwd = getpwnam(optarg))) 194 errx(1, "%s: unknown uid", optarg); 195 what = KERN_PROC_UID; 196 arg = passwd->pw_uid; 197 break; 198 case 'v': 199 vflg = 1; 200 break; 201 case 'w': 202 wflg_mnt = 40; 203 wflg_cmd = 16; 204 break; 205 case '?': 206 default: 207 usage(); 208 } 209 210 if (*(argv += optind)) { 211 for (; *argv; ++argv) { 212 if (getfname(*argv)) 213 checkfile = 1; 214 } 215 if (!checkfile) /* file(s) specified, but none accessable */ 216 exit(1); 217 } 218 219 ALLOC_OFILES(256); /* reserve space for file pointers */ 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 /* 229 * Discard setgid privileges if not the running kernel so that bad 230 * guys can't print interesting stuff from kernel memory. 231 */ 232 if (nlistf != NULL || memf != NULL) 233 setgid(getgid()); 234 235 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL) 236 errx(1, "%s", buf); 237 #ifdef notdef 238 if (kvm_nlist(kd, nl) != 0) 239 errx(1, "no namelist: %s", kvm_geterr(kd)); 240 #endif 241 if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) 242 errx(1, "%s", kvm_geterr(kd)); 243 if (nflg) 244 printf("USER %-*.*s %*.*s FD DEV %*.*s MODE SZ|DV R/W", 245 wflg_cmd, wflg_cmd, "CMD", 246 pid_width, pid_width, "PID", 247 ino_width, ino_width, "INUM"); 248 else 249 printf("USER %-*.*s %*.*s FD %-*.*s %*.*s MODE SZ|DV R/W", 250 wflg_cmd, wflg_cmd, "CMD", 251 pid_width, pid_width, "PID", 252 wflg_mnt, wflg_mnt, "PATH", 253 ino_width, ino_width, "INUM"); 254 if (checkfile && fsflg == 0) 255 printf(" NAME\n"); 256 else 257 putchar('\n'); 258 259 for (plast = &p[cnt]; p < plast; ++p) { 260 if (p->kp_stat == SZOMB) 261 continue; 262 if (!kread((void *)p->kp_paddr, &proc, sizeof(proc))) { 263 dprintf(stderr, "can't read proc at %p for pid %d\n", 264 (void *)p->kp_paddr, Pid); 265 continue; 266 } 267 dofiles(p, &proc); 268 if (mflg) 269 dommap(&proc); 270 } 271 exit(0); 272 } 273 274 #define PREFIX(i) \ 275 printf("%-8.8s %-*s %*d", Uname, wflg_cmd, Comm, pid_width, Pid); \ 276 switch(i) { \ 277 case TEXT: \ 278 printf(" text"); \ 279 break; \ 280 case CDIR: \ 281 printf(" wd"); \ 282 break; \ 283 case RDIR: \ 284 printf(" root"); \ 285 break; \ 286 case TRACE: \ 287 printf(" tr"); \ 288 break; \ 289 case MMAP: \ 290 printf(" mmap"); \ 291 break; \ 292 default: \ 293 printf(" %4d", i); \ 294 break; \ 295 } 296 297 /* 298 * print open files attributed to this process 299 */ 300 static void 301 dofiles(struct kinfo_proc *kp, struct proc *p) 302 { 303 int i; 304 struct file file; 305 struct filedesc filed; 306 struct ktrace_node ktrace_node; 307 308 Uname = user_from_uid(kp->kp_uid, 0); 309 Pid = kp->kp_pid; 310 Comm = kp->kp_comm; 311 make_printable(Comm, strlen(Comm)); 312 313 if (p->p_fd == NULL) 314 return; 315 if (!kread(p->p_fd, &filed, sizeof (filed))) { 316 dprintf(stderr, "can't read filedesc at %p for pid %d\n", 317 (void *)p->p_fd, Pid); 318 return; 319 } 320 /* 321 * root directory vnode, if one 322 */ 323 if (filed.fd_rdir) 324 vtrans(filed.fd_rdir, &filed.fd_nrdir, RDIR, FREAD, 0); 325 /* 326 * current working directory vnode 327 */ 328 vtrans(filed.fd_cdir, &filed.fd_ncdir, CDIR, FREAD, 0); 329 /* 330 * ktrace vnode, if one 331 */ 332 if (p->p_tracenode) { 333 if (kread(p->p_tracenode, &ktrace_node, sizeof (ktrace_node))) 334 vtrans(ktrace_node.kn_vp, NULL, TRACE, FREAD|FWRITE, 0); 335 } 336 /* 337 * text vnode, if one 338 */ 339 if (p->p_textvp) 340 vtrans(p->p_textvp, NULL, TEXT, FREAD, 0); 341 /* 342 * open files 343 */ 344 ALLOC_OFILES(filed.fd_lastfile+1); 345 if (!kread(filed.fd_files, ofiles, 346 (filed.fd_lastfile+1) * sizeof(struct fdnode))) { 347 dprintf(stderr, 348 "can't read file structures at %p for pid %d\n", 349 (void *)filed.fd_files, Pid); 350 return; 351 } 352 for (i = 0; i <= filed.fd_lastfile; i++) { 353 if (ofiles[i].fp == NULL) 354 continue; 355 if (!kread(ofiles[i].fp, &file, sizeof (struct file))) { 356 dprintf(stderr, "can't read file %d at %p for pid %d\n", 357 i, (void *)ofiles[i].fp, Pid); 358 continue; 359 } 360 if (file.f_type == DTYPE_VNODE) { 361 vtrans((struct vnode *)file.f_data, &file.f_nchandle, 362 i, file.f_flag, file.f_offset); 363 } else if (file.f_type == DTYPE_SOCKET) { 364 if (checkfile == 0) 365 socktrans((struct socket *)file.f_data, i); 366 } 367 #ifdef DTYPE_PIPE 368 else if (file.f_type == DTYPE_PIPE) { 369 if (checkfile == 0) 370 pipetrans((struct pipe *)file.f_data, i, 371 file.f_flag); 372 } 373 #endif 374 #ifdef DTYPE_FIFO 375 else if (file.f_type == DTYPE_FIFO) { 376 if (checkfile == 0) 377 vtrans((struct vnode *)file.f_data, 378 &file.f_nchandle, 379 i, file.f_flag, file.f_offset); 380 } 381 #endif 382 else { 383 dprintf(stderr, 384 "unknown file type %d for file %d of pid %d\n", 385 file.f_type, i, Pid); 386 } 387 } 388 } 389 390 static void 391 dommap(struct proc *p) 392 { 393 struct vmspace vmspace; 394 vm_map_t map; 395 struct vm_map_entry entry; 396 vm_map_entry_t ken; 397 struct vm_object object; 398 struct vm_map_backing ba; 399 vm_object_t objp; 400 int prot, fflags; 401 402 if (!kread(p->p_vmspace, &vmspace, sizeof(vmspace))) { 403 dprintf(stderr, "can't read vmspace at %p for pid %d\n", 404 (void *)p->p_vmspace, Pid); 405 return; 406 } 407 408 map = &vmspace.vm_map; 409 for (ken = kvm_vm_map_entry_first(kd, map, &entry); 410 ken; ken = kvm_vm_map_entry_next(kd, ken, &entry)) { 411 if (entry.maptype == VM_MAPTYPE_SUBMAP) 412 continue; 413 414 if (entry.ba.object == NULL) 415 continue; 416 ba = entry.ba; 417 for (;;) { 418 if ((objp = entry.ba.object) != NULL) { 419 if (!kread(objp, &object, sizeof(object))) { 420 dprintf(stderr, 421 "can't read vm_object at %p " 422 "for pid %d\n", 423 (void *)objp, Pid); 424 return; 425 } 426 } 427 if (ba.backing_ba == NULL) 428 break; 429 if (!kread(ba.backing_ba, &ba, sizeof(ba))) { 430 dprintf(stderr, 431 "can't read map_backing at %p " 432 "for pid %d\n", 433 (void *)ba.backing_ba, Pid); 434 return; 435 } 436 } 437 438 prot = entry.protection; 439 fflags = (prot & VM_PROT_READ ? FREAD : 0) | 440 (prot & VM_PROT_WRITE ? FWRITE : 0); 441 442 switch (object.type) { 443 case OBJT_VNODE: 444 vtrans((struct vnode *)object.handle, NULL, 445 MMAP, fflags, 0); 446 break; 447 default: 448 break; 449 } 450 } 451 } 452 453 static void 454 vtrans(struct vnode *vp, struct nchandle *ncr, int i, int flag, off_t off) 455 { 456 struct vnode vn; 457 struct filestat fst; 458 char rw[3], mode[15]; 459 const char *badtype = NULL, *filename; 460 char *name; 461 462 fst.offset = off; 463 filename = badtype = NULL; 464 if (!kread(vp, &vn, sizeof (struct vnode))) { 465 dprintf(stderr, "can't read vnode at %p for pid %d\n", 466 (void *)vp, Pid); 467 return; 468 } 469 if (vn.v_type == VNON || vn.v_tag == VT_NON) 470 badtype = "none"; 471 else if (vn.v_type == VBAD) 472 badtype = "bad"; 473 else 474 switch (vn.v_tag) { 475 case VT_HAMMER: 476 if (!hammer_filestat(&vn, &fst)) 477 badtype = "error"; 478 break; 479 case VT_HAMMER2: 480 if (!hammer2_filestat(&vn, &fst)) 481 badtype = "error"; 482 break; 483 case VT_TMPFS: 484 if (!tmpfs_filestat(&vn, &fst)) 485 badtype = "error"; 486 break; 487 case VT_UFS: 488 if (!ufs_filestat(&vn, &fst)) 489 badtype = "error"; 490 break; 491 case VT_MFS: 492 if (!ufs_filestat(&vn, &fst)) 493 badtype = "error"; 494 break; 495 case VT_NFS: 496 if (!nfs_filestat(&vn, &fst)) 497 badtype = "error"; 498 break; 499 case VT_NTFS: 500 if (!ntfs_filestat(&vn, &fst)) 501 badtype = "error"; 502 break; 503 case VT_EXT2FS: 504 if (!ext2fs_filestat(&vn, &fst)) 505 badtype = "error"; 506 break; 507 508 case VT_MSDOSFS: 509 if (!msdosfs_filestat(&vn, &fst)) 510 badtype = "error"; 511 break; 512 513 case VT_ISOFS: 514 if (!isofs_filestat(&vn, &fst)) 515 badtype = "error"; 516 break; 517 518 case VT_DEVFS: 519 if (!devfs_filestat(&vn, &fst)) 520 badtype = "error"; 521 break; 522 523 default: { 524 static char unknown[10]; 525 sprintf(unknown, "?(%x)", vn.v_tag); 526 badtype=unknown; 527 break; 528 } 529 } 530 if (checkfile) { 531 int fsmatch = 0; 532 DEVS *d; 533 534 if (badtype) 535 return; 536 for (d = devs; d != NULL; d = d->next) 537 if (d->fsid == fst.fsid) { 538 fsmatch = 1; 539 if (d->ino == (ino_t)fst.fileid) { 540 filename = d->name; 541 break; 542 } 543 } 544 if (fsmatch == 0 || (filename == NULL && fsflg == 0)) 545 return; 546 } 547 PREFIX(i); 548 if (badtype) { 549 (void)printf(" %-*s %10s %jd\n", 550 wflg_mnt, 551 getmnton(vn.v_mount, &vn.v_namecache, ncr), 552 badtype, 553 (intmax_t)off); 554 return; 555 } 556 if (nflg) 557 printf(" %3u,%-9u ", 558 major(fst.fsid), minor(fst.fsid)); 559 else 560 printf(" %-*s", 561 wflg_mnt, getmnton(vn.v_mount, &vn.v_namecache, ncr)); 562 if (nflg) 563 sprintf(mode, "%o", fst.mode); 564 else 565 strmode(fst.mode, mode); 566 567 printf(" %*ld %10s", ino_width, fst.fileid, mode); 568 569 switch (vn.v_type) { 570 case VBLK: 571 case VCHR: 572 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ? 573 S_IFCHR : S_IFBLK)) == NULL)) 574 printf(" %3u,%-4u", major(fst.rdev), minor(fst.rdev)); 575 else 576 printf(" %8s", name); 577 break; 578 case VREG: 579 printf(" %jd", (intmax_t)fst.offset); 580 break; 581 default: 582 printf(" %8ju", (uintmax_t)fst.size); 583 } 584 rw[0] = '\0'; 585 if (flag & FREAD) 586 strcat(rw, "r"); 587 if (flag & FWRITE) 588 strcat(rw, "w"); 589 printf(" %2s", rw); 590 if (filename && !fsflg) 591 printf(" %s", filename); 592 putchar('\n'); 593 } 594 595 static int 596 ufs_filestat(struct vnode *vp, struct filestat *fsp) 597 { 598 struct inode inode; 599 600 if (!kread(VTOI(vp), &inode, sizeof (inode))) { 601 dprintf(stderr, "can't read inode at %p for pid %d\n", 602 (void *)VTOI(vp), Pid); 603 return 0; 604 } 605 /* 606 * The st_dev from stat(2) is a udev_t. These kernel structures 607 * contain dev_t structures. We need to convert to udev to make 608 * comparisons 609 */ 610 fsp->fsid = fstat_dev2udev(inode.i_dev); 611 fsp->fileid = (long)inode.i_number; 612 fsp->mode = (mode_t)inode.i_mode; 613 fsp->size = inode.i_size; 614 fsp->rdev = inode.i_rdev; 615 616 return 1; 617 } 618 619 static int 620 nfs_filestat(struct vnode *vp, struct filestat *fsp) 621 { 622 struct nfsnode nfsnode; 623 624 if (!kread(VTONFS(vp), &nfsnode, sizeof (nfsnode))) { 625 dprintf(stderr, "can't read nfsnode at %p for pid %d\n", 626 (void *)VTONFS(vp), Pid); 627 return 0; 628 } 629 fsp->fsid = nfsnode.n_vattr.va_fsid; 630 fsp->fileid = nfsnode.n_vattr.va_fileid; 631 fsp->size = nfsnode.n_size; 632 fsp->rdev = makeudev(nfsnode.n_vattr.va_rmajor, 633 nfsnode.n_vattr.va_rminor); 634 fsp->mode = nfsnode.n_vattr.va_mode | mtrans(vp->v_type); 635 636 return 1; 637 } 638 639 static int 640 devfs_filestat(struct vnode *vp, struct filestat *fsp) 641 { 642 struct devfs_node devfs_node; 643 644 if (!kread(vp->v_data, &devfs_node, sizeof (devfs_node))) { 645 dprintf(stderr, "can't read devfs_node at %p for pid %d\n", 646 (void *)vp->v_data, Pid); 647 return 0; 648 } 649 fsp->fsid = fsp->rdev = fstat_dev2udev(vp->v_rdev); 650 fsp->fileid = devfs_node.d_dir.d_ino; 651 fsp->mode = (devfs_node.mode & ~S_IFMT) | S_IFCHR; 652 fsp->size = 0; 653 654 return 1; 655 } 656 657 static char * 658 getmnton(struct mount *m, struct namecache_list *ncplist, struct nchandle *ncr) 659 { 660 static struct mount mount_l; 661 static struct mtab { 662 struct mtab *next; 663 struct mount *m; 664 char mntonname[MNAMELEN]; 665 } *mhead = NULL; 666 struct mtab *mt; 667 struct namecache *ncp; 668 struct namecache ncp_copy; 669 static char path[1024]; 670 int i; 671 672 /* 673 * If no ncp is passed try to find one via ncplist. Make sure 674 * we are using the correct mount pointer or the matching code 675 * will not know how to transition mount points properly. 676 */ 677 if (ncr == NULL || ncr->ncp == NULL) { 678 ncp = ncplist->tqh_first; 679 } else { 680 ncp = ncr->ncp; 681 if (ncr->mount) 682 m = ncr->mount; 683 } 684 685 /* 686 * If we have an ncp, traceback the path. This is a kvm pointer. 687 */ 688 if (ncp) { 689 if (!kread(m, &mount_l, sizeof(struct mount))) { 690 warnx("can't read mount table at %p", (void *)m); 691 return (NULL); 692 } 693 i = sizeof(path) - 1; 694 path[i] = 0; 695 while (ncp) { 696 /* 697 * If this is the root of the mount then traverse 698 * to the parent mount. 699 */ 700 if (ncp == mount_l.mnt_ncmountpt.ncp) { 701 ncp = mount_l.mnt_ncmounton.ncp; 702 if (ncp == NULL) 703 break; 704 m = mount_l.mnt_ncmounton.mount; 705 if (!kread(m, &mount_l, sizeof(struct mount))) { 706 warnx("can't read mount table at %p", (void *)m); 707 return (NULL); 708 } 709 } 710 711 /* 712 * Ok, pull out the ncp and extract the name 713 */ 714 if (!kread(ncp, &ncp_copy, sizeof(ncp_copy))) { 715 warnx("can't read ncp at %p", ncp); 716 return (NULL); 717 } 718 if (i <= ncp_copy.nc_nlen) 719 break; 720 i -= ncp_copy.nc_nlen; 721 if (!kread(ncp_copy.nc_name, path + i, ncp_copy.nc_nlen)) { 722 warnx("can't read ncp %p path component at %p", ncp, ncp_copy.nc_name); 723 return (NULL); 724 } 725 make_printable(path + i, ncp_copy.nc_nlen); 726 path[--i] = '/'; 727 ncp = ncp_copy.nc_parent; 728 } 729 if (i == sizeof(path) - 1) 730 path[--i] = '/'; 731 return(path + i); 732 } 733 734 /* 735 * If all else fails print out the mount point path 736 */ 737 for (mt = mhead; mt != NULL; mt = mt->next) { 738 if (m == mt->m) 739 return (mt->mntonname); 740 } 741 if (!kread(m, &mount_l, sizeof(struct mount))) { 742 warnx("can't read mount table at %p", (void *)m); 743 return (NULL); 744 } 745 if ((mt = malloc(sizeof (struct mtab))) == NULL) 746 err(1, NULL); 747 mt->m = m; 748 bcopy(&mount_l.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN); 749 mt->next = mhead; 750 mhead = mt; 751 return (mt->mntonname); 752 } 753 754 static void 755 pipetrans(struct pipe *pi, int i, int flag) 756 { 757 struct pipe pip; 758 struct pipebuf *b1; 759 struct pipebuf *b2; 760 char rw[3]; 761 char side1; 762 char side2; 763 764 PREFIX(i); 765 if ((intptr_t)pi & 1) { 766 side1 = 'B'; 767 side2 = 'A'; 768 b1 = &pip.bufferB; 769 b2 = &pip.bufferA; 770 } else { 771 side1 = 'A'; 772 side2 = 'B'; 773 b1 = &pip.bufferA; 774 b2 = &pip.bufferB; 775 } 776 pi = (void *)((intptr_t)pi & ~(intptr_t)1); 777 778 /* fill in socket */ 779 if (!kread(pi, &pip, sizeof(struct pipe))) { 780 dprintf(stderr, "can't read pipe at %p\n", (void *)pi); 781 goto bad; 782 } 783 784 printf("* pipe %8lx (%c<->%c)", (u_long)pi, side1, side2); 785 printf(" ravail %-zd wavail %-zd", 786 b1->windex - b1->rindex, 787 b2->windex - b2->rindex); 788 rw[0] = '\0'; 789 if (flag & FREAD) 790 strcat(rw, "r"); 791 if (flag & FWRITE) 792 strcat(rw, "w"); 793 printf(" %2s", rw); 794 putchar('\n'); 795 return; 796 797 bad: 798 printf("* error\n"); 799 } 800 801 static void 802 socktrans(struct socket *sock, int i) 803 { 804 static const char *stypename[] = { 805 "unused", /* 0 */ 806 "stream", /* 1 */ 807 "dgram", /* 2 */ 808 "raw", /* 3 */ 809 "rdm", /* 4 */ 810 "seqpak" /* 5 */ 811 }; 812 #define STYPEMAX 5 813 struct socket so; 814 struct protosw proto; 815 struct domain dom; 816 struct inpcb inpcb; 817 struct unpcb unpcb; 818 int len; 819 char dname[32]; 820 821 PREFIX(i); 822 823 /* fill in socket */ 824 if (!kread(sock, &so, sizeof(struct socket))) { 825 dprintf(stderr, "can't read sock at %p\n", (void *)sock); 826 goto bad; 827 } 828 829 /* fill in protosw entry */ 830 if (!kread(so.so_proto, &proto, sizeof(struct protosw))) { 831 dprintf(stderr, "can't read protosw at %p", 832 (void *)so.so_proto); 833 goto bad; 834 } 835 836 /* fill in domain */ 837 if (!kread(proto.pr_domain, &dom, sizeof(struct domain))) { 838 dprintf(stderr, "can't read domain at %p\n", 839 (const void *)proto.pr_domain); 840 goto bad; 841 } 842 843 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname, 844 sizeof(dname) - 1)) < 0) { 845 dprintf(stderr, "can't read domain name at %p\n", 846 (void *)dom.dom_name); 847 dname[0] = '\0'; 848 } 849 else 850 dname[len] = '\0'; 851 852 if ((u_short)so.so_type > STYPEMAX) 853 printf("* %s ?%d", dname, so.so_type); 854 else 855 printf("* %s %s", dname, stypename[so.so_type]); 856 857 /* 858 * protocol specific formatting 859 * 860 * Try to find interesting things to print. For tcp, the interesting 861 * thing is the address of the tcpcb, for udp and others, just the 862 * inpcb (socket pcb). For unix domain, its the address of the socket 863 * pcb and the address of the connected pcb (if connected). Otherwise 864 * just print the protocol number and address of the socket itself. 865 * The idea is not to duplicate netstat, but to make available enough 866 * information for further analysis. 867 */ 868 switch(dom.dom_family) { 869 case AF_INET: 870 case AF_INET6: 871 getinetproto(proto.pr_protocol); 872 if (proto.pr_protocol == IPPROTO_TCP ) { 873 if (so.so_pcb) { 874 if (kvm_read(kd, (u_long)so.so_pcb, 875 (char *)&inpcb, sizeof(struct inpcb)) 876 != sizeof(struct inpcb)) { 877 dprintf(stderr, 878 "can't read inpcb at %p\n", 879 (void *)so.so_pcb); 880 goto bad; 881 } 882 printf(" %lx", (u_long)inpcb.inp_ppcb); 883 } 884 } 885 else if (so.so_pcb) 886 printf(" %lx", (u_long)so.so_pcb); 887 break; 888 case AF_UNIX: 889 /* print address of pcb and connected pcb */ 890 if (so.so_pcb) { 891 printf(" %lx", (u_long)so.so_pcb); 892 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb, 893 sizeof(struct unpcb)) != sizeof(struct unpcb)){ 894 dprintf(stderr, "can't read unpcb at %p\n", 895 (void *)so.so_pcb); 896 goto bad; 897 } 898 if (unpcb.unp_conn) { 899 char shoconn[4], *cp; 900 901 cp = shoconn; 902 if (!(so.so_state & SS_CANTRCVMORE)) 903 *cp++ = '<'; 904 *cp++ = '-'; 905 if (!(so.so_state & SS_CANTSENDMORE)) 906 *cp++ = '>'; 907 *cp = '\0'; 908 printf(" %s %lx", shoconn, 909 (u_long)unpcb.unp_conn); 910 } 911 } 912 break; 913 default: 914 /* print protocol number and socket address */ 915 printf(" %d %lx", proto.pr_protocol, (u_long)sock); 916 } 917 printf("\n"); 918 return; 919 bad: 920 printf("* error\n"); 921 } 922 923 924 /* 925 * Read the cdev structure in the kernel (as pointed to by a dev_t) 926 * in order to work out the associated udev_t 927 */ 928 udev_t 929 fstat_dev2udev(void *dev) 930 { 931 struct cdev si; 932 933 if (kread(dev, &si, sizeof si)) { 934 if ((si.si_umajor & 0xffffff00) || 935 (si.si_uminor & 0x0000ff00)) { 936 return NOUDEV; 937 } 938 return((si.si_umajor << 8) | si.si_uminor); 939 } else { 940 dprintf(stderr, "can't convert dev_t %p to a udev_t\n", dev); 941 return NOUDEV; 942 } 943 } 944 945 udev_t 946 makeudev(int x, int y) 947 { 948 if ((x & 0xffffff00) || (y & 0x0000ff00)) 949 return NOUDEV; 950 return ((x << 8) | y); 951 } 952 953 /* 954 * getinetproto -- 955 * print name of protocol number 956 */ 957 static void 958 getinetproto(int number) 959 { 960 static int isopen; 961 struct protoent *pe; 962 963 if (!isopen) 964 setprotoent(++isopen); 965 if ((pe = getprotobynumber(number)) != NULL) 966 printf(" %s", pe->p_name); 967 else 968 printf(" %d", number); 969 } 970 971 static int 972 getfname(const char *filename) 973 { 974 struct stat statbuf; 975 DEVS *cur; 976 977 if (stat(filename, &statbuf)) { 978 warn("%s", filename); 979 return(0); 980 } 981 if ((cur = malloc(sizeof(DEVS))) == NULL) 982 err(1, NULL); 983 cur->next = devs; 984 devs = cur; 985 986 cur->ino = statbuf.st_ino; 987 cur->fsid = statbuf.st_dev; 988 cur->name = filename; 989 return(1); 990 } 991 992 static void 993 usage(void) 994 { 995 (void)fprintf(stderr, 996 "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n"); 997 exit(1); 998 } 999 1000 static 1001 void 1002 make_printable(char *buf, int len) 1003 { 1004 while (len > 0) { 1005 if (!isprint(*buf)) 1006 *buf = '?'; 1007 ++buf; 1008 --len; 1009 } 1010 } 1011 1012 ssize_t 1013 kread(const void *kaddr, void *uaddr, size_t nbytes) 1014 { 1015 if (nbytes > 0x10000000) 1016 return(0); 1017 1018 if (kvm_read(kd, (u_long)kaddr, (char *)uaddr, nbytes) == (ssize_t)nbytes) 1019 return(1); 1020 else 1021 return(0); 1022 } 1023