fstat.c revision 82664
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 44static const char rcsid[] = 45 "$FreeBSD: head/usr.bin/fstat/fstat.c 82664 2001-08-31 16:26:37Z ru $"; 46#endif /* not lint */ 47 48#include <sys/param.h> 49#include <sys/lock.h> 50#include <sys/time.h> 51#include <sys/proc.h> 52#include <sys/user.h> 53#include <sys/stat.h> 54#include <sys/vnode.h> 55#include <sys/socket.h> 56#include <sys/socketvar.h> 57#include <sys/domain.h> 58#include <sys/protosw.h> 59#include <sys/un.h> 60#include <sys/unpcb.h> 61#include <sys/sysctl.h> 62#include <sys/filedesc.h> 63#include <sys/queue.h> 64#include <sys/pipe.h> 65#define _KERNEL 66#include <sys/conf.h> 67#include <sys/file.h> 68#include <ufs/ufs/quota.h> 69#include <ufs/ufs/inode.h> 70#include <sys/mount.h> 71#undef _KERNEL 72#include <nfs/nfsproto.h> 73#include <nfs/rpcv2.h> 74#include <nfs/nfs.h> 75#include <nfs/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 110DEVS *devs; 111 112#ifdef notdef 113struct nlist nl[] = { 114 { "" }, 115}; 116#endif 117 118int fsflg, /* show files on same filesystem as file(s) argument */ 119 pflg, /* show files open by a particular pid */ 120 uflg; /* show files open by a particular (effective) user */ 121int checkfile; /* true if restricting to particular files or filesystems */ 122int nflg; /* (numerical) display f.s. and rdev as dev_t */ 123int vflg; /* display errors in locating kernel data objects etc... */ 124int mflg; /* include memory-mapped files */ 125 126 127struct file **ofiles; /* buffer of pointers to file structures */ 128int maxfiles; 129#define ALLOC_OFILES(d) \ 130 if ((d) > maxfiles) { \ 131 free(ofiles); \ 132 ofiles = malloc((d) * sizeof(struct file *)); \ 133 if (ofiles == NULL) { \ 134 err(1, NULL); \ 135 } \ 136 maxfiles = (d); \ 137 } 138 139kvm_t *kd; 140 141void dofiles __P((struct kinfo_proc *kp)); 142void dommap __P((struct kinfo_proc *kp)); 143void vtrans __P((struct vnode *vp, int i, int flag)); 144int ufs_filestat __P((struct vnode *vp, struct filestat *fsp)); 145int nfs_filestat __P((struct vnode *vp, struct filestat *fsp)); 146char *getmnton __P((struct mount *m)); 147void pipetrans __P((struct pipe *pi, int i, int flag)); 148void socktrans __P((struct socket *sock, int i)); 149void getinetproto __P((int number)); 150int getfname __P((char *filename)); 151void usage __P((void)); 152 153 154int 155main(argc, argv) 156 int argc; 157 char **argv; 158{ 159 register struct passwd *passwd; 160 struct kinfo_proc *p, *plast; 161 int arg, ch, what; 162 char *memf, *nlistf; 163 char buf[_POSIX2_LINE_MAX]; 164 int cnt; 165 166 arg = 0; 167 what = KERN_PROC_ALL; 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 ALLOC_OFILES(256); /* reserve space for file pointers */ 222 223 if (fsflg && !checkfile) { 224 /* -f with no files means use wd */ 225 if (getfname(".") == 0) 226 exit(1); 227 checkfile = 1; 228 } 229 230 /* 231 * Discard setgid privileges if not the running kernel so that bad 232 * guys can't print interesting stuff from kernel memory. 233 */ 234 if (nlistf != NULL || memf != NULL) 235 setgid(getgid()); 236 237 if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL) 238 errx(1, "%s", buf); 239 setgid(getgid()); 240#ifdef notdef 241 if (kvm_nlist(kd, nl) != 0) 242 errx(1, "no namelist: %s", kvm_geterr(kd)); 243#endif 244 if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) 245 errx(1, "%s", kvm_geterr(kd)); 246 if (nflg) 247 printf("%s", 248"USER CMD PID FD DEV INUM MODE SZ|DV R/W"); 249 else 250 printf("%s", 251"USER CMD PID FD MOUNT INUM MODE SZ|DV R/W"); 252 if (checkfile && fsflg == 0) 253 printf(" NAME\n"); 254 else 255 putchar('\n'); 256 257 for (plast = &p[cnt]; p < plast; ++p) { 258 if (p->ki_stat == SZOMB) 259 continue; 260 dofiles(p); 261 if (mflg) 262 dommap(p); 263 } 264 exit(0); 265} 266 267char *Uname, *Comm; 268int Pid; 269 270#define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \ 271 switch(i) { \ 272 case TEXT: \ 273 printf(" text"); \ 274 break; \ 275 case CDIR: \ 276 printf(" wd"); \ 277 break; \ 278 case RDIR: \ 279 printf(" root"); \ 280 break; \ 281 case TRACE: \ 282 printf(" tr"); \ 283 break; \ 284 case MMAP: \ 285 printf(" mmap"); \ 286 break; \ 287 default: \ 288 printf(" %4d", i); \ 289 break; \ 290 } 291 292/* 293 * print open files attributed to this process 294 */ 295void 296dofiles(kp) 297 struct kinfo_proc *kp; 298{ 299 int i; 300 struct file file; 301 struct filedesc0 filed0; 302#define filed filed0.fd_fd 303 304 Uname = user_from_uid(kp->ki_uid, 0); 305 Pid = kp->ki_pid; 306 Comm = kp->ki_comm; 307 308 if (kp->ki_fd == NULL) 309 return; 310 if (!KVM_READ(kp->ki_fd, &filed0, sizeof (filed0))) { 311 dprintf(stderr, "can't read filedesc at %p for pid %d\n", 312 (void *)kp->ki_fd, Pid); 313 return; 314 } 315 /* 316 * root directory vnode, if one 317 */ 318 if (filed.fd_rdir) 319 vtrans(filed.fd_rdir, RDIR, FREAD); 320 /* 321 * current working directory vnode 322 */ 323 vtrans(filed.fd_cdir, CDIR, FREAD); 324 /* 325 * ktrace vnode, if one 326 */ 327 if (kp->ki_tracep) 328 vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE); 329 /* 330 * text vnode, if one 331 */ 332 if (kp->ki_textvp) 333 vtrans(kp->ki_textvp, TEXT, FREAD); 334 /* 335 * open files 336 */ 337#define FPSIZE (sizeof (struct file *)) 338 ALLOC_OFILES(filed.fd_lastfile+1); 339 if (filed.fd_nfiles > NDFILE) { 340 if (!KVM_READ(filed.fd_ofiles, ofiles, 341 (filed.fd_lastfile+1) * FPSIZE)) { 342 dprintf(stderr, 343 "can't read file structures at %p for pid %d\n", 344 (void *)filed.fd_ofiles, Pid); 345 return; 346 } 347 } else 348 bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE); 349 for (i = 0; i <= filed.fd_lastfile; i++) { 350 if (ofiles[i] == NULL) 351 continue; 352 if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) { 353 dprintf(stderr, "can't read file %d at %p for pid %d\n", 354 i, (void *)ofiles[i], Pid); 355 continue; 356 } 357 if (file.f_type == DTYPE_VNODE) 358 vtrans((struct vnode *)file.f_data, i, file.f_flag); 359 else if (file.f_type == DTYPE_SOCKET) { 360 if (checkfile == 0) 361 socktrans((struct socket *)file.f_data, i); 362 } 363#ifdef DTYPE_PIPE 364 else if (file.f_type == DTYPE_PIPE) { 365 if (checkfile == 0) 366 pipetrans((struct pipe *)file.f_data, i, 367 file.f_flag); 368 } 369#endif 370#ifdef DTYPE_FIFO 371 else if (file.f_type == DTYPE_FIFO) { 372 if (checkfile == 0) 373 vtrans((struct vnode *)file.f_data, i, 374 file.f_flag); 375 } 376#endif 377 else { 378 dprintf(stderr, 379 "unknown file type %d for file %d of pid %d\n", 380 file.f_type, i, Pid); 381 } 382 } 383} 384 385void 386dommap(kp) 387 struct kinfo_proc *kp; 388{ 389 vm_map_t map; 390 struct vmspace vmspace; 391 struct vm_map_entry entry; 392 vm_map_entry_t entryp; 393 struct vm_object object; 394 vm_object_t objp; 395 int prot, fflags; 396 397 if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) { 398 dprintf(stderr, 399 "can't read vmspace at %p for pid %d\n", 400 (void *)kp->ki_vmspace, Pid); 401 return; 402 } 403 map = &vmspace.vm_map; 404 405 for (entryp = map->header.next; 406 entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) { 407 if (!KVM_READ(entryp, &entry, sizeof(entry))) { 408 dprintf(stderr, 409 "can't read vm_map_entry at %p for pid %d\n", 410 (void *)entryp, Pid); 411 return; 412 } 413 414 if (entry.eflags & MAP_ENTRY_IS_SUB_MAP) 415 continue; 416 417 if ((objp = entry.object.vm_object) == NULL) 418 continue; 419 420 for (; objp; objp = object.backing_object) { 421 if (!KVM_READ(objp, &object, sizeof(object))) { 422 dprintf(stderr, 423 "can't read vm_object at %p for pid %d\n", 424 (void *)objp, Pid); 425 return; 426 } 427 } 428 429 prot = entry.protection; 430 fflags = (prot & VM_PROT_READ ? FREAD : 0) | 431 (prot & VM_PROT_WRITE ? FWRITE : 0); 432 433 switch (object.type) { 434 case OBJT_VNODE: 435 vtrans((struct vnode *)object.handle, MMAP, fflags); 436 break; 437 default: 438 break; 439 } 440 } 441} 442 443void 444vtrans(vp, i, flag) 445 struct vnode *vp; 446 int i; 447 int flag; 448{ 449 struct vnode vn; 450 struct filestat fst; 451 char rw[3], mode[15]; 452 char *badtype = NULL, *filename, *getmnton(); 453 454 filename = badtype = NULL; 455 if (!KVM_READ(vp, &vn, sizeof (struct vnode))) { 456 dprintf(stderr, "can't read vnode at %p for pid %d\n", 457 (void *)vp, Pid); 458 return; 459 } 460 if (vn.v_type == VNON || vn.v_tag == VT_NON) 461 badtype = "none"; 462 else if (vn.v_type == VBAD) 463 badtype = "bad"; 464 else 465 switch (vn.v_tag) { 466 case VT_UFS: 467 if (!ufs_filestat(&vn, &fst)) 468 badtype = "error"; 469 break; 470 case VT_NFS: 471 if (!nfs_filestat(&vn, &fst)) 472 badtype = "error"; 473 break; 474 475 case VT_MSDOSFS: 476 if (!msdosfs_filestat(&vn, &fst)) 477 badtype = "error"; 478 break; 479 480 case VT_ISOFS: 481 if (!isofs_filestat(&vn, &fst)) 482 badtype = "error"; 483 break; 484 485 default: { 486 static char unknown[10]; 487 sprintf(badtype = unknown, "?(%x)", vn.v_tag); 488 break;; 489 } 490 } 491 if (checkfile) { 492 int fsmatch = 0; 493 register DEVS *d; 494 495 if (badtype) 496 return; 497 for (d = devs; d != NULL; d = d->next) 498 if (d->fsid == fst.fsid) { 499 fsmatch = 1; 500 if (d->ino == fst.fileid) { 501 filename = d->name; 502 break; 503 } 504 } 505 if (fsmatch == 0 || (filename == NULL && fsflg == 0)) 506 return; 507 } 508 PREFIX(i); 509 if (badtype) { 510 (void)printf(" - - %10s -\n", badtype); 511 return; 512 } 513 if (nflg) 514 (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid)); 515 else 516 (void)printf(" %-8s", getmnton(vn.v_mount)); 517 if (nflg) 518 (void)sprintf(mode, "%o", fst.mode); 519 else 520 strmode(fst.mode, mode); 521 (void)printf(" %6ld %10s", fst.fileid, mode); 522 switch (vn.v_type) { 523 case VBLK: 524 case VCHR: { 525 char *name; 526 527 if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ? 528 S_IFCHR : S_IFBLK)) == NULL)) 529 printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev)); 530 else 531 printf(" %6s", name); 532 break; 533 } 534 default: 535 printf(" %6lu", fst.size); 536 } 537 rw[0] = '\0'; 538 if (flag & FREAD) 539 strcat(rw, "r"); 540 if (flag & FWRITE) 541 strcat(rw, "w"); 542 printf(" %2s", rw); 543 if (filename && !fsflg) 544 printf(" %s", filename); 545 putchar('\n'); 546} 547 548int 549ufs_filestat(vp, fsp) 550 struct vnode *vp; 551 struct filestat *fsp; 552{ 553 struct inode inode; 554 555 if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 556 dprintf(stderr, "can't read inode at %p for pid %d\n", 557 (void *)VTOI(vp), Pid); 558 return 0; 559 } 560 /* 561 * The st_dev from stat(2) is a udev_t. These kernel structures 562 * contain dev_t structures. We need to convert to udev to make 563 * comparisons 564 */ 565 fsp->fsid = dev2udev(inode.i_dev) & 0xffff; 566 fsp->fileid = (long)inode.i_number; 567 fsp->mode = (mode_t)inode.i_mode; 568 fsp->size = (u_long)inode.i_size; 569 fsp->rdev = inode.i_rdev; 570 571 return 1; 572} 573 574int 575nfs_filestat(vp, fsp) 576 struct vnode *vp; 577 struct filestat *fsp; 578{ 579 struct nfsnode nfsnode; 580 register mode_t mode; 581 582 if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) { 583 dprintf(stderr, "can't read nfsnode at %p for pid %d\n", 584 (void *)VTONFS(vp), Pid); 585 return 0; 586 } 587 fsp->fsid = nfsnode.n_vattr.va_fsid; 588 fsp->fileid = nfsnode.n_vattr.va_fileid; 589 fsp->size = nfsnode.n_size; 590 fsp->rdev = nfsnode.n_vattr.va_rdev; 591 mode = (mode_t)nfsnode.n_vattr.va_mode; 592 switch (vp->v_type) { 593 case VREG: 594 mode |= S_IFREG; 595 break; 596 case VDIR: 597 mode |= S_IFDIR; 598 break; 599 case VBLK: 600 mode |= S_IFBLK; 601 break; 602 case VCHR: 603 mode |= S_IFCHR; 604 break; 605 case VLNK: 606 mode |= S_IFLNK; 607 break; 608 case VSOCK: 609 mode |= S_IFSOCK; 610 break; 611 case VFIFO: 612 mode |= S_IFIFO; 613 break; 614 case VNON: 615 case VBAD: 616 return 0; 617 }; 618 fsp->mode = mode; 619 620 return 1; 621} 622 623 624char * 625getmnton(m) 626 struct mount *m; 627{ 628 static struct mount mount; 629 static struct mtab { 630 struct mtab *next; 631 struct mount *m; 632 char mntonname[MNAMELEN]; 633 } *mhead = NULL; 634 register struct mtab *mt; 635 636 for (mt = mhead; mt != NULL; mt = mt->next) 637 if (m == mt->m) 638 return (mt->mntonname); 639 if (!KVM_READ(m, &mount, sizeof(struct mount))) { 640 warnx("can't read mount table at %p", (void *)m); 641 return (NULL); 642 } 643 if ((mt = malloc(sizeof (struct mtab))) == NULL) 644 err(1, NULL); 645 mt->m = m; 646 bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN); 647 mt->next = mhead; 648 mhead = mt; 649 return (mt->mntonname); 650} 651 652void 653pipetrans(pi, i, flag) 654 struct pipe *pi; 655 int i; 656 int flag; 657{ 658 struct pipe pip; 659 char rw[3]; 660 661 PREFIX(i); 662 663 /* fill in socket */ 664 if (!KVM_READ(pi, &pip, sizeof(struct pipe))) { 665 dprintf(stderr, "can't read pipe at %p\n", (void *)pi); 666 goto bad; 667 } 668 669 printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer); 670 printf(" %6d", (int)pip.pipe_buffer.cnt); 671 rw[0] = '\0'; 672 if (flag & FREAD) 673 strcat(rw, "r"); 674 if (flag & FWRITE) 675 strcat(rw, "w"); 676 printf(" %2s", rw); 677 putchar('\n'); 678 return; 679 680bad: 681 printf("* error\n"); 682} 683 684void 685socktrans(sock, i) 686 struct socket *sock; 687 int i; 688{ 689 static char *stypename[] = { 690 "unused", /* 0 */ 691 "stream", /* 1 */ 692 "dgram", /* 2 */ 693 "raw", /* 3 */ 694 "rdm", /* 4 */ 695 "seqpak" /* 5 */ 696 }; 697#define STYPEMAX 5 698 struct socket so; 699 struct protosw proto; 700 struct domain dom; 701 struct inpcb inpcb; 702 struct unpcb unpcb; 703 int len; 704 char dname[32], *strcpy(); 705 706 PREFIX(i); 707 708 /* fill in socket */ 709 if (!KVM_READ(sock, &so, sizeof(struct socket))) { 710 dprintf(stderr, "can't read sock at %p\n", (void *)sock); 711 goto bad; 712 } 713 714 /* fill in protosw entry */ 715 if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) { 716 dprintf(stderr, "can't read protosw at %p", 717 (void *)so.so_proto); 718 goto bad; 719 } 720 721 /* fill in domain */ 722 if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) { 723 dprintf(stderr, "can't read domain at %p\n", 724 (void *)proto.pr_domain); 725 goto bad; 726 } 727 728 if ((len = kvm_read(kd, (u_long)dom.dom_name, dname, 729 sizeof(dname) - 1)) < 0) { 730 dprintf(stderr, "can't read domain name at %p\n", 731 (void *)dom.dom_name); 732 dname[0] = '\0'; 733 } 734 else 735 dname[len] = '\0'; 736 737 if ((u_short)so.so_type > STYPEMAX) 738 printf("* %s ?%d", dname, so.so_type); 739 else 740 printf("* %s %s", dname, stypename[so.so_type]); 741 742 /* 743 * protocol specific formatting 744 * 745 * Try to find interesting things to print. For tcp, the interesting 746 * thing is the address of the tcpcb, for udp and others, just the 747 * inpcb (socket pcb). For unix domain, its the address of the socket 748 * pcb and the address of the connected pcb (if connected). Otherwise 749 * just print the protocol number and address of the socket itself. 750 * The idea is not to duplicate netstat, but to make available enough 751 * information for further analysis. 752 */ 753 switch(dom.dom_family) { 754 case AF_INET: 755 case AF_INET6: 756 getinetproto(proto.pr_protocol); 757 if (proto.pr_protocol == IPPROTO_TCP ) { 758 if (so.so_pcb) { 759 if (kvm_read(kd, (u_long)so.so_pcb, 760 (char *)&inpcb, sizeof(struct inpcb)) 761 != sizeof(struct inpcb)) { 762 dprintf(stderr, 763 "can't read inpcb at %p\n", 764 (void *)so.so_pcb); 765 goto bad; 766 } 767 printf(" %lx", (u_long)inpcb.inp_ppcb); 768 } 769 } 770 else if (so.so_pcb) 771 printf(" %lx", (u_long)so.so_pcb); 772 break; 773 case AF_UNIX: 774 /* print address of pcb and connected pcb */ 775 if (so.so_pcb) { 776 printf(" %lx", (u_long)so.so_pcb); 777 if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb, 778 sizeof(struct unpcb)) != sizeof(struct unpcb)){ 779 dprintf(stderr, "can't read unpcb at %p\n", 780 (void *)so.so_pcb); 781 goto bad; 782 } 783 if (unpcb.unp_conn) { 784 char shoconn[4], *cp; 785 786 cp = shoconn; 787 if (!(so.so_state & SS_CANTRCVMORE)) 788 *cp++ = '<'; 789 *cp++ = '-'; 790 if (!(so.so_state & SS_CANTSENDMORE)) 791 *cp++ = '>'; 792 *cp = '\0'; 793 printf(" %s %lx", shoconn, 794 (u_long)unpcb.unp_conn); 795 } 796 } 797 break; 798 default: 799 /* print protocol number and socket address */ 800 printf(" %d %lx", proto.pr_protocol, (u_long)sock); 801 } 802 printf("\n"); 803 return; 804bad: 805 printf("* error\n"); 806} 807 808 809/* 810 * Read the specinfo structure in the kernel (as pointed to by a dev_t) 811 * in order to work out the associated udev_t 812 */ 813udev_t 814dev2udev(dev) 815 dev_t dev; 816{ 817 struct specinfo si; 818 819 if (KVM_READ(dev, &si, sizeof si)) { 820 return si.si_udev; 821 } else { 822 dprintf(stderr, "can't convert dev_t %x to a udev_t\n", dev); 823 return -1; 824 } 825} 826 827/* 828 * getinetproto -- 829 * print name of protocol number 830 */ 831void 832getinetproto(number) 833 int number; 834{ 835 static int isopen; 836 register struct protoent *pe; 837 838 if (!isopen) 839 setprotoent(++isopen); 840 if ((pe = getprotobynumber(number)) != NULL) 841 printf(" %s", pe->p_name); 842 else 843 printf(" %d", number); 844} 845 846int 847getfname(filename) 848 char *filename; 849{ 850 struct stat statbuf; 851 DEVS *cur; 852 853 if (stat(filename, &statbuf)) { 854 warn("%s", filename); 855 return(0); 856 } 857 if ((cur = malloc(sizeof(DEVS))) == NULL) 858 err(1, NULL); 859 cur->next = devs; 860 devs = cur; 861 862 cur->ino = statbuf.st_ino; 863 cur->fsid = statbuf.st_dev & 0xffff; 864 cur->name = filename; 865 return(1); 866} 867 868void 869usage() 870{ 871 (void)fprintf(stderr, 872 "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n"); 873 exit(1); 874} 875