1 /* $NetBSD: fstat.c,v 1.90 2011/04/14 00:35:35 rmind Exp $ */ 2 3 /*- 4 * Copyright (c) 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\ 35 The Regents of the University of California. All rights reserved."); 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)fstat.c 8.3 (Berkeley) 5/2/95"; 41 #else 42 __RCSID("$NetBSD: fstat.c,v 1.90 2011/04/14 00:35:35 rmind Exp $"); 43 #endif 44 #endif /* not lint */ 45 46 #include <sys/types.h> 47 #include <sys/param.h> 48 #include <sys/time.h> 49 #include <sys/proc.h> 50 #include <sys/stat.h> 51 #include <sys/vnode.h> 52 #include <sys/socket.h> 53 #include <sys/socketvar.h> 54 #include <sys/domain.h> 55 #include <sys/protosw.h> 56 #include <sys/unpcb.h> 57 #include <sys/sysctl.h> 58 #include <sys/filedesc.h> 59 #include <sys/pipe.h> 60 #define _KERNEL 61 #include <sys/file.h> 62 #include <ufs/ufs/inode.h> 63 #undef _KERNEL 64 #define _KERNEL 65 #include <sys/mount.h> 66 #undef _KERNEL 67 #define NFS 68 #include <nfs/nfsproto.h> 69 #include <nfs/rpcv2.h> 70 #include <nfs/nfs.h> 71 #include <nfs/nfsnode.h> 72 #undef NFS 73 #include <msdosfs/denode.h> 74 #include <msdosfs/bpb.h> 75 #define _KERNEL 76 #include <msdosfs/msdosfsmount.h> 77 #undef _KERNEL 78 #define _KERNEL 79 #include <miscfs/genfs/layer.h> 80 #undef _KERNEL 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 #ifdef INET6 89 #include <netinet/ip6.h> 90 #include <netinet6/ip6_var.h> 91 #include <netinet6/in6_pcb.h> 92 #endif 93 94 #include <netdb.h> 95 #include <arpa/inet.h> 96 97 #include <ctype.h> 98 #include <errno.h> 99 #include <kvm.h> 100 #include <limits.h> 101 #include <nlist.h> 102 #include <paths.h> 103 #include <pwd.h> 104 #include <stdio.h> 105 #include <stdlib.h> 106 #include <string.h> 107 #include <unistd.h> 108 #include <err.h> 109 110 #include "fstat.h" 111 112 #define TEXT -1 113 #define CDIR -2 114 #define RDIR -3 115 #define TRACE -4 116 117 typedef struct devs { 118 struct devs *next; 119 long fsid; 120 ino_t ino; 121 const char *name; 122 } DEVS; 123 static DEVS *devs; 124 125 static int fsflg, /* show files on same filesystem as file(s) argument */ 126 pflg, /* show files open by a particular pid */ 127 uflg; /* show files open by a particular (effective) user */ 128 static int checkfile; /* true if restricting to particular files or filesystems */ 129 static int nflg; /* (numerical) display f.s. and rdev as dev_t */ 130 int vflg; /* display errors in locating kernel data objects etc... */ 131 132 static fdfile_t **ofiles; /* buffer of pointers to file structures */ 133 static int fstat_maxfiles; 134 #define ALLOC_OFILES(d) \ 135 if ((d) > fstat_maxfiles) { \ 136 size_t len = (d) * sizeof(fdfile_t *); \ 137 free(ofiles); \ 138 ofiles = malloc(len); \ 139 if (ofiles == NULL) { \ 140 err(1, "malloc(%zu)", len); \ 141 } \ 142 fstat_maxfiles = (d); \ 143 } 144 145 kvm_t *kd; 146 147 static const char *const dtypes[] = { 148 DTYPE_NAMES 149 }; 150 151 static void dofiles(struct kinfo_proc2 *); 152 static int ext2fs_filestat(struct vnode *, struct filestat *); 153 static int getfname(const char *); 154 static void getinetproto(int); 155 static char *getmnton(struct mount *); 156 static const char *layer_filestat(struct vnode *, struct filestat *); 157 static int msdosfs_filestat(struct vnode *, struct filestat *); 158 static int nfs_filestat(struct vnode *, struct filestat *); 159 #ifdef INET6 160 static const char *inet6_addrstr(struct in6_addr *); 161 #endif 162 static void socktrans(struct socket *, int); 163 static void misctrans(struct file *); 164 static int ufs_filestat(struct vnode *, struct filestat *); 165 static void usage(void) __dead; 166 static const char *vfilestat(struct vnode *, struct filestat *); 167 static void vtrans(struct vnode *, int, int); 168 static void ftrans(fdfile_t *, int); 169 static void ptrans(struct file *, struct pipe *, int); 170 171 int 172 main(int argc, char **argv) 173 { 174 struct passwd *passwd; 175 struct kinfo_proc2 *p, *plast; 176 int arg, ch, what; 177 char *memf, *nlistf; 178 char buf[_POSIX2_LINE_MAX]; 179 int cnt; 180 gid_t egid = getegid(); 181 182 (void)setegid(getgid()); 183 arg = 0; 184 what = KERN_PROC_ALL; 185 nlistf = memf = NULL; 186 while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1) 187 switch((char)ch) { 188 case 'f': 189 fsflg = 1; 190 break; 191 case 'M': 192 memf = optarg; 193 break; 194 case 'N': 195 nlistf = optarg; 196 break; 197 case 'n': 198 nflg = 1; 199 break; 200 case 'p': 201 if (pflg++) 202 usage(); 203 if (!isdigit((unsigned char)*optarg)) { 204 warnx("-p requires a process id"); 205 usage(); 206 } 207 what = KERN_PROC_PID; 208 arg = atoi(optarg); 209 break; 210 case 'u': 211 if (uflg++) 212 usage(); 213 if (!(passwd = getpwnam(optarg))) { 214 errx(1, "%s: unknown uid", optarg); 215 } 216 what = KERN_PROC_UID; 217 arg = passwd->pw_uid; 218 break; 219 case 'v': 220 vflg = 1; 221 break; 222 case '?': 223 default: 224 usage(); 225 } 226 227 if (*(argv += optind)) { 228 for (; *argv; ++argv) { 229 if (getfname(*argv)) 230 checkfile = 1; 231 } 232 if (!checkfile) /* file(s) specified, but none accessible */ 233 exit(1); 234 } 235 236 ALLOC_OFILES(256); /* reserve space for file pointers */ 237 238 if (fsflg && !checkfile) { 239 /* -f with no files means use wd */ 240 if (getfname(".") == 0) 241 exit(1); 242 checkfile = 1; 243 } 244 245 /* 246 * Discard setgid privileges. If not the running kernel, we toss 247 * them away totally so that bad guys can't print interesting stuff 248 * from kernel memory, otherwise switch back to kmem for the 249 * duration of the kvm_openfiles() call. 250 */ 251 if (nlistf != NULL || memf != NULL) 252 (void)setgid(getgid()); 253 else 254 (void)setegid(egid); 255 256 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL) 257 errx(1, "%s", buf); 258 259 /* get rid of it now anyway */ 260 if (nlistf == NULL && memf == NULL) 261 (void)setgid(getgid()); 262 263 if ((p = kvm_getproc2(kd, what, arg, sizeof *p, &cnt)) == NULL) { 264 errx(1, "%s", kvm_geterr(kd)); 265 } 266 if (nflg) 267 (void)printf("%s", 268 "USER CMD PID FD DEV INUM MODE SZ|DV R/W"); 269 else 270 (void)printf("%s", 271 "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W"); 272 if (checkfile && fsflg == 0) 273 (void)printf(" NAME\n"); 274 else 275 (void)putchar('\n'); 276 277 for (plast = &p[cnt]; p < plast; ++p) { 278 if (p->p_stat == SZOMB) 279 continue; 280 dofiles(p); 281 } 282 return 0; 283 } 284 285 static const char *Uname, *Comm; 286 pid_t Pid; 287 288 #define PREFIX(i) (void)printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \ 289 switch(i) { \ 290 case TEXT: \ 291 (void)printf(" text"); \ 292 break; \ 293 case CDIR: \ 294 (void)printf(" wd"); \ 295 break; \ 296 case RDIR: \ 297 (void)printf(" root"); \ 298 break; \ 299 case TRACE: \ 300 (void)printf(" tr"); \ 301 break; \ 302 default: \ 303 (void)printf(" %4d", i); \ 304 break; \ 305 } 306 307 /* 308 * print open files attributed to this process 309 */ 310 static void 311 dofiles(struct kinfo_proc2 *p) 312 { 313 int i; 314 struct filedesc filed; 315 struct cwdinfo cwdi; 316 struct fdtab dt; 317 318 Uname = user_from_uid(p->p_uid, 0); 319 Pid = p->p_pid; 320 Comm = p->p_comm; 321 322 if (p->p_fd == 0 || p->p_cwdi == 0) 323 return; 324 if (!KVM_READ(p->p_fd, &filed, sizeof (filed))) { 325 warnx("can't read filedesc at %p for pid %d", (void *)(uintptr_t)p->p_fd, Pid); 326 return; 327 } 328 if (!KVM_READ(p->p_cwdi, &cwdi, sizeof(cwdi))) { 329 warnx("can't read cwdinfo at %p for pid %d", (void *)(uintptr_t)p->p_cwdi, Pid); 330 return; 331 } 332 if (!KVM_READ(filed.fd_dt, &dt, sizeof(dt))) { 333 warnx("can't read dtab at %p for pid %d", filed.fd_dt, Pid); 334 return; 335 } 336 if ((unsigned)filed.fd_lastfile >= dt.dt_nfiles || 337 filed.fd_freefile > filed.fd_lastfile + 1) { 338 dprintf("filedesc corrupted at %p for pid %d", (void *)(uintptr_t)p->p_fd, Pid); 339 return; 340 } 341 /* 342 * root directory vnode, if one 343 */ 344 if (cwdi.cwdi_rdir) 345 vtrans(cwdi.cwdi_rdir, RDIR, FREAD); 346 /* 347 * current working directory vnode 348 */ 349 vtrans(cwdi.cwdi_cdir, CDIR, FREAD); 350 #if 0 351 /* 352 * Disable for now, since p->p_tracep appears to point to a ktr_desc * 353 * ktrace vnode, if one 354 */ 355 if (p->p_tracep) 356 ftrans((struct file *)(intptr_t)p->p_tracep, TRACE); 357 #endif 358 /* 359 * open files 360 */ 361 #define FPSIZE (sizeof (fdfile_t *)) 362 ALLOC_OFILES(filed.fd_lastfile+1); 363 if (!KVM_READ(&filed.fd_dt->dt_ff, ofiles, 364 (filed.fd_lastfile+1) * FPSIZE)) { 365 dprintf("can't read file structures at %p for pid %d", 366 &filed.fd_dt->dt_ff, Pid); 367 return; 368 } 369 for (i = 0; i <= filed.fd_lastfile; i++) { 370 if (ofiles[i] == NULL) 371 continue; 372 ftrans(ofiles[i], i); 373 } 374 } 375 376 static void 377 ftrans(fdfile_t *fp, int i) 378 { 379 struct file file; 380 fdfile_t fdfile; 381 382 if (!KVM_READ(fp, &fdfile, sizeof(fdfile))) { 383 dprintf("can't read file %d at %p for pid %d", 384 i, fp, Pid); 385 return; 386 } 387 if (fdfile.ff_file == NULL) 388 return; 389 if (!KVM_READ(fdfile.ff_file, &file, sizeof(file))) { 390 dprintf("can't read file %d at %p for pid %d", 391 i, fdfile.ff_file, Pid); 392 return; 393 } 394 switch (file.f_type) { 395 case DTYPE_VNODE: 396 vtrans((struct vnode *)file.f_data, i, file.f_flag); 397 break; 398 case DTYPE_SOCKET: 399 if (checkfile == 0) 400 socktrans((struct socket *)file.f_data, i); 401 break; 402 case DTYPE_PIPE: 403 if (checkfile == 0) 404 ptrans(&file, (struct pipe *)file.f_data, i); 405 break; 406 case DTYPE_MISC: 407 case DTYPE_KQUEUE: 408 case DTYPE_CRYPTO: 409 case DTYPE_MQUEUE: 410 case DTYPE_SEM: 411 if (checkfile == 0) 412 misctrans(&file); 413 break; 414 default: 415 dprintf("unknown file type %d for file %d of pid %d", 416 file.f_type, i, Pid); 417 break; 418 } 419 } 420 421 static const char * 422 vfilestat(struct vnode *vp, struct filestat *fsp) 423 { 424 const char *badtype = NULL; 425 426 if (vp->v_type == VNON || vp->v_tag == VT_NON) 427 badtype = "none"; 428 else if (vp->v_type == VBAD) 429 badtype = "bad"; 430 else 431 switch (vp->v_tag) { 432 case VT_UFS: 433 case VT_LFS: 434 case VT_MFS: 435 if (!ufs_filestat(vp, fsp)) 436 badtype = "error"; 437 break; 438 case VT_MSDOSFS: 439 if (!msdosfs_filestat(vp, fsp)) 440 badtype = "error"; 441 break; 442 case VT_NFS: 443 if (!nfs_filestat(vp, fsp)) 444 badtype = "error"; 445 break; 446 case VT_EXT2FS: 447 if (!ext2fs_filestat(vp, fsp)) 448 badtype = "error"; 449 break; 450 case VT_ISOFS: 451 if (!isofs_filestat(vp, fsp)) 452 badtype = "error"; 453 break; 454 case VT_NTFS: 455 if (!ntfs_filestat(vp, fsp)) 456 badtype = "error"; 457 break; 458 case VT_PTYFS: 459 if (!ptyfs_filestat(vp, fsp)) 460 badtype = "error"; 461 break; 462 case VT_TMPFS: 463 if (!tmpfs_filestat(vp, fsp)) 464 badtype = "error"; 465 break; 466 case VT_NULL: 467 case VT_OVERLAY: 468 case VT_UMAP: 469 badtype = layer_filestat(vp, fsp); 470 break; 471 default: { 472 static char unknown[10]; 473 (void)snprintf(unknown, sizeof unknown, 474 "?(%x)", vp->v_tag); 475 badtype = unknown; 476 break; 477 } 478 } 479 return (badtype); 480 } 481 482 static void 483 vtrans(struct vnode *vp, int i, int flag) 484 { 485 struct vnode vn; 486 struct filestat fst; 487 char mode[15], rw[3]; 488 const char *badtype, *filename; 489 490 filename = NULL; 491 if (!KVM_READ(vp, &vn, sizeof(struct vnode))) { 492 dprintf("can't read vnode at %p for pid %d", vp, Pid); 493 return; 494 } 495 badtype = vfilestat(&vn, &fst); 496 if (checkfile) { 497 int fsmatch = 0; 498 DEVS *d; 499 500 if (badtype) 501 return; 502 for (d = devs; d != NULL; d = d->next) 503 if (d->fsid == fst.fsid) { 504 fsmatch = 1; 505 if (d->ino == fst.fileid) { 506 filename = d->name; 507 break; 508 } 509 } 510 if (fsmatch == 0 || (filename == NULL && fsflg == 0)) 511 return; 512 } 513 PREFIX(i); 514 if (badtype) { 515 (void)printf(" - - %10s -\n", badtype); 516 return; 517 } 518 if (nflg) 519 (void)printf(" %2llu,%-2llu", 520 (unsigned long long)major(fst.fsid), 521 (unsigned long long)minor(fst.fsid)); 522 else 523 (void)printf(" %-8s", getmnton(vn.v_mount)); 524 if (nflg) 525 (void)snprintf(mode, sizeof mode, "%o", fst.mode); 526 else 527 strmode(fst.mode, mode); 528 (void)printf(" %7"PRIu64" %*s", fst.fileid, nflg ? 5 : 10, mode); 529 switch (vn.v_type) { 530 case VBLK: 531 case VCHR: { 532 char *name; 533 534 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ? 535 S_IFCHR : S_IFBLK)) == NULL)) 536 (void)printf(" %2llu,%-2llu", 537 (unsigned long long)major(fst.rdev), 538 (unsigned long long)minor(fst.rdev)); 539 else 540 (void)printf(" %6s", name); 541 break; 542 } 543 default: 544 (void)printf(" %6lld", (long long)fst.size); 545 } 546 rw[0] = '\0'; 547 if (flag & FREAD) 548 (void)strlcat(rw, "r", sizeof(rw)); 549 if (flag & FWRITE) 550 (void)strlcat(rw, "w", sizeof(rw)); 551 (void)printf(" %-2s", rw); 552 if (filename && !fsflg) 553 (void)printf(" %s", filename); 554 (void)putchar('\n'); 555 } 556 557 static int 558 ufs_filestat(struct vnode *vp, struct filestat *fsp) 559 { 560 struct inode inode; 561 union dinode { 562 struct ufs1_dinode dp1; 563 struct ufs2_dinode dp2; 564 } dip; 565 566 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 567 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid); 568 return 0; 569 } 570 571 if (!KVM_READ(inode.i_din.ffs1_din, &dip, sizeof(struct ufs1_dinode))) { 572 dprintf("can't read dinode at %p for pid %d", 573 inode.i_din.ffs1_din, Pid); 574 return 0; 575 } 576 if (inode.i_size == dip.dp1.di_size) 577 fsp->rdev = dip.dp1.di_rdev; 578 else { 579 if (!KVM_READ(inode.i_din.ffs1_din, &dip, 580 sizeof(struct ufs2_dinode))) { 581 dprintf("can't read dinode at %p for pid %d", 582 inode.i_din.ffs1_din, Pid); 583 return 0; 584 } 585 fsp->rdev = dip.dp2.di_rdev; 586 } 587 588 fsp->fsid = inode.i_dev & 0xffff; 589 fsp->fileid = inode.i_number; 590 fsp->mode = (mode_t)inode.i_mode; 591 fsp->size = inode.i_size; 592 593 return 1; 594 } 595 596 static int 597 ext2fs_filestat(struct vnode *vp, struct filestat *fsp) 598 { 599 struct inode inode; 600 u_int16_t mode; 601 u_int32_t size; 602 603 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 604 dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid); 605 return 0; 606 } 607 fsp->fsid = inode.i_dev & 0xffff; 608 fsp->fileid = inode.i_number; 609 610 if (!KVM_READ(&inode.i_e2fs_mode, &mode, sizeof mode)) { 611 dprintf("can't read inode %p's mode at %p for pid %d", VTOI(vp), 612 &inode.i_e2fs_mode, Pid); 613 return 0; 614 } 615 fsp->mode = mode; 616 617 if (!KVM_READ(&inode.i_e2fs_size, &size, sizeof size)) { 618 dprintf("can't read inode %p's size at %p for pid %d", VTOI(vp), 619 &inode.i_e2fs_size, Pid); 620 return 0; 621 } 622 fsp->size = size; 623 fsp->rdev = 0; /* XXX */ 624 return 1; 625 } 626 627 static int 628 nfs_filestat(struct vnode *vp, struct filestat *fsp) 629 { 630 struct nfsnode nfsnode; 631 struct vattr va; 632 633 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) { 634 dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp), 635 Pid); 636 return 0; 637 } 638 if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) { 639 dprintf("can't read vnode attributes at %p for pid %d", 640 nfsnode.n_vattr, Pid); 641 return 0; 642 } 643 fsp->fsid = va.va_fsid; 644 fsp->fileid = va.va_fileid; 645 fsp->size = nfsnode.n_size; 646 fsp->rdev = va.va_rdev; 647 fsp->mode = (mode_t)va.va_mode | getftype(vp->v_type); 648 649 return 1; 650 } 651 652 static int 653 msdosfs_filestat(struct vnode *vp, struct filestat *fsp) 654 { 655 struct denode de; 656 struct msdosfsmount mp; 657 658 if (!KVM_READ(VTONFS(vp), &de, sizeof(de))) { 659 dprintf("can't read denode at %p for pid %d", VTONFS(vp), 660 Pid); 661 return 0; 662 } 663 if (!KVM_READ(de.de_pmp, &mp, sizeof(mp))) { 664 dprintf("can't read mount struct at %p for pid %d", de.de_pmp, 665 Pid); 666 return 0; 667 } 668 669 fsp->fsid = de.de_dev & 0xffff; 670 fsp->fileid = 0; /* XXX see msdosfs_vptofh() for more info */ 671 fsp->size = de.de_FileSize; 672 fsp->rdev = 0; /* msdosfs doesn't support device files */ 673 fsp->mode = (0777 & mp.pm_mask) | getftype(vp->v_type); 674 return 1; 675 } 676 677 static const char * 678 layer_filestat(struct vnode *vp, struct filestat *fsp) 679 { 680 struct layer_node layer_node; 681 struct mount mount; 682 struct vnode vn; 683 const char *badtype; 684 685 if (!KVM_READ(VTOLAYER(vp), &layer_node, sizeof(layer_node))) { 686 dprintf("can't read layer_node at %p for pid %d", 687 VTOLAYER(vp), Pid); 688 return ("error"); 689 } 690 if (!KVM_READ(vp->v_mount, &mount, sizeof(struct mount))) { 691 dprintf("can't read mount struct at %p for pid %d", 692 vp->v_mount, Pid); 693 return ("error"); 694 } 695 vp = layer_node.layer_lowervp; 696 if (!KVM_READ(vp, &vn, sizeof(struct vnode))) { 697 dprintf("can't read vnode at %p for pid %d", vp, Pid); 698 return ("error"); 699 } 700 if ((badtype = vfilestat(&vn, fsp)) == NULL) 701 fsp->fsid = mount.mnt_stat.f_fsidx.__fsid_val[0]; 702 return (badtype); 703 } 704 705 static char * 706 getmnton(struct mount *m) 707 { 708 static struct mount mount; 709 static struct mtab { 710 struct mtab *next; 711 struct mount *m; 712 char mntonname[MNAMELEN]; 713 } *mhead = NULL; 714 struct mtab *mt; 715 716 for (mt = mhead; mt != NULL; mt = mt->next) 717 if (m == mt->m) 718 return (mt->mntonname); 719 if (!KVM_READ(m, &mount, sizeof(struct mount))) { 720 warnx("can't read mount table at %p", m); 721 return (NULL); 722 } 723 if ((mt = malloc(sizeof (struct mtab))) == NULL) { 724 err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab)); 725 } 726 mt->m = m; 727 (void)memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0], 728 MNAMELEN); 729 mt->next = mhead; 730 mhead = mt; 731 return (mt->mntonname); 732 } 733 734 #ifdef INET6 735 static const char * 736 inet6_addrstr(struct in6_addr *p) 737 { 738 struct sockaddr_in6 sin6; 739 static char hbuf[NI_MAXHOST]; 740 const int niflags = NI_NUMERICHOST; 741 742 (void)memset(&sin6, 0, sizeof(sin6)); 743 sin6.sin6_family = AF_INET6; 744 sin6.sin6_len = sizeof(struct sockaddr_in6); 745 sin6.sin6_addr = *p; 746 if (IN6_IS_ADDR_LINKLOCAL(p) && 747 *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) { 748 sin6.sin6_scope_id = 749 ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]); 750 sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0; 751 } 752 753 if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, 754 hbuf, sizeof(hbuf), NULL, 0, niflags)) 755 return "invalid"; 756 757 return hbuf; 758 } 759 #endif 760 761 static void 762 socktrans(struct socket *sock, int i) 763 { 764 static const char *stypename[] = { 765 "unused", /* 0 */ 766 "stream", /* 1 */ 767 "dgram", /* 2 */ 768 "raw", /* 3 */ 769 "rdm", /* 4 */ 770 "seqpak" /* 5 */ 771 }; 772 #define STYPEMAX 5 773 struct socket so; 774 struct protosw proto; 775 struct domain dom; 776 struct inpcb inpcb; 777 #ifdef INET6 778 struct in6pcb in6pcb; 779 #endif 780 struct unpcb unpcb; 781 int len; 782 char dname[32]; 783 #ifdef INET6 784 char xaddrbuf[NI_MAXHOST + 2]; 785 #endif 786 787 PREFIX(i); 788 789 /* fill in socket */ 790 if (!KVM_READ(sock, &so, sizeof(struct socket))) { 791 dprintf("can't read sock at %p", sock); 792 goto bad; 793 } 794 795 /* fill in protosw entry */ 796 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) { 797 dprintf("can't read protosw at %p", 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("can't read domain at %p", proto.pr_domain); 804 goto bad; 805 } 806 807 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname, 808 sizeof(dname) - 1)) != sizeof(dname) -1) { 809 dprintf("can't read domain name at %p", dom.dom_name); 810 dname[0] = '\0'; 811 } 812 else 813 dname[len] = '\0'; 814 815 if ((u_short)so.so_type > STYPEMAX) 816 (void)printf("* %s ?%d", dname, so.so_type); 817 else 818 (void)printf("* %s %s", dname, stypename[so.so_type]); 819 820 /* 821 * protocol specific formatting 822 * 823 * Try to find interesting things to print. For TCP, the interesting 824 * thing is the address of the tcpcb, for UDP and others, just the 825 * inpcb (socket pcb). For UNIX domain, its the address of the socket 826 * pcb and the address of the connected pcb (if connected). Otherwise 827 * just print the protocol number and address of the socket itself. 828 * The idea is not to duplicate netstat, but to make available enough 829 * information for further analysis. 830 */ 831 switch(dom.dom_family) { 832 case AF_INET: 833 getinetproto(proto.pr_protocol); 834 if (proto.pr_protocol == IPPROTO_TCP) { 835 if (so.so_pcb == NULL) 836 break; 837 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb, 838 sizeof(struct inpcb)) != sizeof(struct inpcb)) { 839 dprintf("can't read inpcb at %p", so.so_pcb); 840 goto bad; 841 } 842 (void)printf(" %lx", (long)inpcb.inp_ppcb); 843 (void)printf(" %s:%d", 844 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" : 845 inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport)); 846 if (inpcb.inp_fport) { 847 (void)printf(" <-> %s:%d", 848 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" : 849 inet_ntoa(inpcb.inp_faddr), 850 ntohs(inpcb.inp_fport)); 851 } 852 } else if (proto.pr_protocol == IPPROTO_UDP) { 853 if (so.so_pcb == NULL) 854 break; 855 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb, 856 sizeof(struct inpcb)) != sizeof(struct inpcb)) { 857 dprintf("can't read inpcb at %p", so.so_pcb); 858 goto bad; 859 } 860 (void)printf(" %lx", (long)so.so_pcb); 861 (void)printf(" %s:%d", 862 inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" : 863 inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport)); 864 if (inpcb.inp_fport) 865 (void)printf(" <-> %s:%d", 866 inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" : 867 inet_ntoa(inpcb.inp_faddr), 868 ntohs(inpcb.inp_fport)); 869 } else if (so.so_pcb) 870 (void)printf(" %lx", (long)so.so_pcb); 871 break; 872 #ifdef INET6 873 case AF_INET6: 874 getinetproto(proto.pr_protocol); 875 if (proto.pr_protocol == IPPROTO_TCP) { 876 if (so.so_pcb == NULL) 877 break; 878 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb, 879 sizeof(struct in6pcb)) != sizeof(struct in6pcb)) { 880 dprintf("can't read in6pcb at %p", so.so_pcb); 881 goto bad; 882 } 883 (void)printf(" %lx", (long)in6pcb.in6p_ppcb); 884 (void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]", 885 inet6_addrstr(&in6pcb.in6p_laddr)); 886 (void)printf(" %s:%d", 887 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" : 888 xaddrbuf, 889 ntohs(in6pcb.in6p_lport)); 890 if (in6pcb.in6p_fport) { 891 (void)snprintf(xaddrbuf, sizeof(xaddrbuf), 892 "[%s]", inet6_addrstr(&in6pcb.in6p_faddr)); 893 (void)printf(" <-> %s:%d", 894 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" : 895 xaddrbuf, 896 ntohs(in6pcb.in6p_fport)); 897 } 898 } else if (proto.pr_protocol == IPPROTO_UDP) { 899 if (so.so_pcb == NULL) 900 break; 901 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb, 902 sizeof(struct in6pcb)) != sizeof(struct in6pcb)) { 903 dprintf("can't read inpcb at %p", so.so_pcb); 904 goto bad; 905 } 906 (void)printf(" %lx", (long)so.so_pcb); 907 (void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]", 908 inet6_addrstr(&in6pcb.in6p_laddr)); 909 (void)printf(" %s:%d", 910 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" : 911 xaddrbuf, 912 ntohs(in6pcb.in6p_lport)); 913 if (in6pcb.in6p_fport) { 914 (void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]", 915 inet6_addrstr(&in6pcb.in6p_faddr)); 916 (void)printf(" <-> %s:%d", 917 IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" : 918 xaddrbuf, 919 ntohs(in6pcb.in6p_fport)); 920 } 921 } else if (so.so_pcb) 922 (void)printf(" %lx", (long)so.so_pcb); 923 break; 924 #endif 925 case AF_LOCAL: 926 /* print address of pcb and connected pcb */ 927 if (so.so_pcb) { 928 (void)printf(" %lx", (long)so.so_pcb); 929 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb, 930 sizeof(struct unpcb)) != sizeof(struct unpcb)){ 931 dprintf("can't read unpcb at %p", so.so_pcb); 932 goto bad; 933 } 934 if (unpcb.unp_conn) { 935 char shoconn[4], *cp; 936 937 cp = shoconn; 938 if (!(so.so_state & SS_CANTRCVMORE)) 939 *cp++ = '<'; 940 *cp++ = '-'; 941 if (!(so.so_state & SS_CANTSENDMORE)) 942 *cp++ = '>'; 943 *cp = '\0'; 944 (void)printf(" %s %lx", shoconn, 945 (long)unpcb.unp_conn); 946 } 947 } 948 break; 949 default: 950 /* print protocol number and socket address */ 951 (void)printf(" %d %lx", proto.pr_protocol, (long)sock); 952 } 953 (void)printf("\n"); 954 return; 955 bad: 956 (void)printf("* error\n"); 957 } 958 959 static void 960 ptrans(struct file *fp, struct pipe *cpipe, int i) 961 { 962 struct pipe cp; 963 964 PREFIX(i); 965 966 /* fill in pipe */ 967 if (!KVM_READ(cpipe, &cp, sizeof(struct pipe))) { 968 dprintf("can't read pipe at %p", cpipe); 969 goto bad; 970 } 971 972 /* pipe descriptor is either read or write, never both */ 973 (void)printf("* pipe %p %s %p %s%s%s", cpipe, 974 (fp->f_flag & FWRITE) ? "->" : "<-", 975 cp.pipe_peer, 976 (fp->f_flag & FWRITE) ? "w" : "r", 977 (fp->f_flag & FNONBLOCK) ? "n" : "", 978 (cp.pipe_state & PIPE_ASYNC) ? "a" : ""); 979 (void)printf("\n"); 980 return; 981 bad: 982 (void)printf("* error\n"); 983 } 984 985 static void 986 misctrans(struct file *file) 987 { 988 989 PREFIX((int)file->f_type); 990 pmisc(file, dtypes[file->f_type]); 991 } 992 993 /* 994 * getinetproto -- 995 * print name of protocol number 996 */ 997 static void 998 getinetproto(int number) 999 { 1000 const char *cp; 1001 1002 switch (number) { 1003 case IPPROTO_IP: 1004 cp = "ip"; break; 1005 case IPPROTO_ICMP: 1006 cp ="icmp"; break; 1007 case IPPROTO_GGP: 1008 cp ="ggp"; break; 1009 case IPPROTO_TCP: 1010 cp ="tcp"; break; 1011 case IPPROTO_EGP: 1012 cp ="egp"; break; 1013 case IPPROTO_PUP: 1014 cp ="pup"; break; 1015 case IPPROTO_UDP: 1016 cp ="udp"; break; 1017 case IPPROTO_IDP: 1018 cp ="idp"; break; 1019 case IPPROTO_RAW: 1020 cp ="raw"; break; 1021 case IPPROTO_ICMPV6: 1022 cp ="icmp6"; break; 1023 default: 1024 (void)printf(" %d", number); 1025 return; 1026 } 1027 (void)printf(" %s", cp); 1028 } 1029 1030 static int 1031 getfname(const char *filename) 1032 { 1033 struct stat statbuf; 1034 DEVS *cur; 1035 1036 if (stat(filename, &statbuf)) { 1037 warn("stat(%s)", filename); 1038 return 0; 1039 } 1040 if ((cur = malloc(sizeof(DEVS))) == NULL) { 1041 err(1, "malloc(%u)", (unsigned int)sizeof(DEVS)); 1042 } 1043 cur->next = devs; 1044 devs = cur; 1045 1046 cur->ino = statbuf.st_ino; 1047 cur->fsid = statbuf.st_dev & 0xffff; 1048 cur->name = filename; 1049 return 1; 1050 } 1051 1052 mode_t 1053 getftype(enum vtype v_type) 1054 { 1055 mode_t ftype; 1056 1057 switch (v_type) { 1058 case VREG: 1059 ftype = S_IFREG; 1060 break; 1061 case VDIR: 1062 ftype = S_IFDIR; 1063 break; 1064 case VBLK: 1065 ftype = S_IFBLK; 1066 break; 1067 case VCHR: 1068 ftype = S_IFCHR; 1069 break; 1070 case VLNK: 1071 ftype = S_IFLNK; 1072 break; 1073 case VSOCK: 1074 ftype = S_IFSOCK; 1075 break; 1076 case VFIFO: 1077 ftype = S_IFIFO; 1078 break; 1079 default: 1080 ftype = 0; 1081 break; 1082 }; 1083 1084 return ftype; 1085 } 1086 1087 static void 1088 usage(void) 1089 { 1090 (void)fprintf(stderr, "Usage: %s [-fnv] [-p pid] [-u user] " 1091 "[-N system] [-M core] [file ...]\n", getprogname()); 1092 exit(1); 1093 } 1094