19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1988, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 69b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 79b50d902SRodney W. Grimes * are met: 89b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 99b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 109b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 119b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 129b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 139b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 149b50d902SRodney W. Grimes * must display the following acknowledgement: 159b50d902SRodney W. Grimes * This product includes software developed by the University of 169b50d902SRodney W. Grimes * California, Berkeley and its contributors. 179b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 189b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 199b50d902SRodney W. Grimes * without specific prior written permission. 209b50d902SRodney W. Grimes * 219b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 229b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 239b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 249b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 259b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 269b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 279b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 289b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 299b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 309b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 319b50d902SRodney W. Grimes * SUCH DAMAGE. 329b50d902SRodney W. Grimes */ 339b50d902SRodney W. Grimes 349b50d902SRodney W. Grimes #ifndef lint 35c1e65942SPhilippe Charnier static const char copyright[] = 369b50d902SRodney W. Grimes "@(#) Copyright (c) 1988, 1993\n\ 379b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 389b50d902SRodney W. Grimes #endif /* not lint */ 399b50d902SRodney W. Grimes 409b50d902SRodney W. Grimes #ifndef lint 41c1e65942SPhilippe Charnier #if 0 42df3f5d9dSPeter Wemm static char sccsid[] = "@(#)fstat.c 8.3 (Berkeley) 5/2/95"; 43c1e65942SPhilippe Charnier #endif 44c1e65942SPhilippe Charnier static const char rcsid[] = 45c3aac50fSPeter Wemm "$FreeBSD$"; 469b50d902SRodney W. Grimes #endif /* not lint */ 479b50d902SRodney W. Grimes 489b50d902SRodney W. Grimes #include <sys/param.h> 499b50d902SRodney W. Grimes #include <sys/time.h> 509b50d902SRodney W. Grimes #include <sys/proc.h> 519b50d902SRodney W. Grimes #include <sys/user.h> 529b50d902SRodney W. Grimes #include <sys/stat.h> 539b50d902SRodney W. Grimes #include <sys/vnode.h> 549b50d902SRodney W. Grimes #include <sys/socket.h> 559b50d902SRodney W. Grimes #include <sys/socketvar.h> 569b50d902SRodney W. Grimes #include <sys/domain.h> 579b50d902SRodney W. Grimes #include <sys/protosw.h> 583492514dSMark Murray #include <sys/un.h> 599b50d902SRodney W. Grimes #include <sys/unpcb.h> 609b50d902SRodney W. Grimes #include <sys/sysctl.h> 619b50d902SRodney W. Grimes #include <sys/filedesc.h> 62d2bccb9fSDavid Greenman #include <sys/queue.h> 631c17fc99SPeter Wemm #include <sys/pipe.h> 64c4473420SPeter Wemm #define _KERNEL 6511529d1aSPoul-Henning Kamp #include <sys/conf.h> 669b50d902SRodney W. Grimes #include <sys/file.h> 679b50d902SRodney W. Grimes #include <ufs/ufs/quota.h> 689b50d902SRodney W. Grimes #include <ufs/ufs/inode.h> 69fca066feSBrian Feldman #include <fs/devfs/devfs.h> 709b50d902SRodney W. Grimes #include <sys/mount.h> 717a8671e9SAlfred Perlstein #undef _KERNEL 72a62dc406SDoug Rabson #include <nfs/nfsproto.h> 739b50d902SRodney W. Grimes #include <nfs/rpcv2.h> 7491196234SPeter Wemm #include <nfsclient/nfs.h> 7591196234SPeter Wemm #include <nfsclient/nfsnode.h> 769b50d902SRodney W. Grimes 7706e88916SBrian Feldman 78d0482be8SBrian Feldman #include <vm/vm.h> 79d0482be8SBrian Feldman #include <vm/vm_map.h> 80d0482be8SBrian Feldman #include <vm/vm_object.h> 81d0482be8SBrian Feldman 829b50d902SRodney W. Grimes #include <net/route.h> 839b50d902SRodney W. Grimes #include <netinet/in.h> 849b50d902SRodney W. Grimes #include <netinet/in_systm.h> 859b50d902SRodney W. Grimes #include <netinet/ip.h> 869b50d902SRodney W. Grimes #include <netinet/in_pcb.h> 879b50d902SRodney W. Grimes 889b50d902SRodney W. Grimes #include <ctype.h> 89c1e65942SPhilippe Charnier #include <err.h> 905d98ce75SBruce Evans #include <fcntl.h> 919b50d902SRodney W. Grimes #include <kvm.h> 92df3f5d9dSPeter Wemm #include <limits.h> 939b50d902SRodney W. Grimes #include <nlist.h> 949b50d902SRodney W. Grimes #include <paths.h> 959b50d902SRodney W. Grimes #include <pwd.h> 969b50d902SRodney W. Grimes #include <stdio.h> 979b50d902SRodney W. Grimes #include <stdlib.h> 989b50d902SRodney W. Grimes #include <string.h> 99df3f5d9dSPeter Wemm #include <unistd.h> 1000b7a57ddSRuslan Ermilov #include <netdb.h> 1019b50d902SRodney W. Grimes 10206e88916SBrian Feldman #include "fstat.h" 10306e88916SBrian Feldman 1049b50d902SRodney W. Grimes #define TEXT -1 1059b50d902SRodney W. Grimes #define CDIR -2 1069b50d902SRodney W. Grimes #define RDIR -3 1079b50d902SRodney W. Grimes #define TRACE -4 108d0482be8SBrian Feldman #define MMAP -5 1099b50d902SRodney W. Grimes 1109b50d902SRodney W. Grimes DEVS *devs; 1119b50d902SRodney W. Grimes 1129b50d902SRodney W. Grimes #ifdef notdef 1139b50d902SRodney W. Grimes struct nlist nl[] = { 1149b50d902SRodney W. Grimes { "" }, 1159b50d902SRodney W. Grimes }; 1169b50d902SRodney W. Grimes #endif 1179b50d902SRodney W. Grimes 1189b50d902SRodney W. Grimes int fsflg, /* show files on same filesystem as file(s) argument */ 1199b50d902SRodney W. Grimes pflg, /* show files open by a particular pid */ 1209b50d902SRodney W. Grimes uflg; /* show files open by a particular (effective) user */ 1219b50d902SRodney W. Grimes int checkfile; /* true if restricting to particular files or filesystems */ 1229b50d902SRodney W. Grimes int nflg; /* (numerical) display f.s. and rdev as dev_t */ 1239b50d902SRodney W. Grimes int vflg; /* display errors in locating kernel data objects etc... */ 124d0482be8SBrian Feldman int mflg; /* include memory-mapped files */ 1259b50d902SRodney W. Grimes 1269b50d902SRodney W. Grimes 1279b50d902SRodney W. Grimes struct file **ofiles; /* buffer of pointers to file structures */ 1289b50d902SRodney W. Grimes int maxfiles; 1299b50d902SRodney W. Grimes #define ALLOC_OFILES(d) \ 1309b50d902SRodney W. Grimes if ((d) > maxfiles) { \ 1319b50d902SRodney W. Grimes free(ofiles); \ 1329b50d902SRodney W. Grimes ofiles = malloc((d) * sizeof(struct file *)); \ 1339b50d902SRodney W. Grimes if (ofiles == NULL) { \ 134c1e65942SPhilippe Charnier err(1, NULL); \ 1359b50d902SRodney W. Grimes } \ 1369b50d902SRodney W. Grimes maxfiles = (d); \ 1379b50d902SRodney W. Grimes } 1389b50d902SRodney W. Grimes 1391f910d6cSDag-Erling Smørgrav char *memf, *nlistf; 1409b50d902SRodney W. Grimes kvm_t *kd; 1419b50d902SRodney W. Grimes 1421f910d6cSDag-Erling Smørgrav static void fstat_kvm(int, int); 1431f910d6cSDag-Erling Smørgrav static void fstat_sysctl(int, int); 144f1bb2cd2SWarner Losh void dofiles(struct kinfo_proc *kp); 145f1bb2cd2SWarner Losh void dommap(struct kinfo_proc *kp); 146f1bb2cd2SWarner Losh void vtrans(struct vnode *vp, int i, int flag); 147f1bb2cd2SWarner Losh int ufs_filestat(struct vnode *vp, struct filestat *fsp); 148f1bb2cd2SWarner Losh int nfs_filestat(struct vnode *vp, struct filestat *fsp); 149f1bb2cd2SWarner Losh int devfs_filestat(struct vnode *vp, struct filestat *fsp); 150f1bb2cd2SWarner Losh char *getmnton(struct mount *m); 151f1bb2cd2SWarner Losh void pipetrans(struct pipe *pi, int i, int flag); 152f1bb2cd2SWarner Losh void socktrans(struct socket *sock, int i); 153f1bb2cd2SWarner Losh void getinetproto(int number); 15427d57ea9SDavid Malone int getfname(const char *filename); 155f1bb2cd2SWarner Losh void usage(void); 1569b50d902SRodney W. Grimes 1571c17fc99SPeter Wemm 1581c17fc99SPeter Wemm int 1599b50d902SRodney W. Grimes main(argc, argv) 1609b50d902SRodney W. Grimes int argc; 1619b50d902SRodney W. Grimes char **argv; 1629b50d902SRodney W. Grimes { 16327d57ea9SDavid Malone struct passwd *passwd; 1649b50d902SRodney W. Grimes int arg, ch, what; 1659b50d902SRodney W. Grimes 1669b50d902SRodney W. Grimes arg = 0; 1679b50d902SRodney W. Grimes what = KERN_PROC_ALL; 1689b50d902SRodney W. Grimes nlistf = memf = NULL; 169d0482be8SBrian Feldman while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1) 1709b50d902SRodney W. Grimes switch((char)ch) { 1719b50d902SRodney W. Grimes case 'f': 1729b50d902SRodney W. Grimes fsflg = 1; 1739b50d902SRodney W. Grimes break; 1749b50d902SRodney W. Grimes case 'M': 1759b50d902SRodney W. Grimes memf = optarg; 1769b50d902SRodney W. Grimes break; 1779b50d902SRodney W. Grimes case 'N': 1789b50d902SRodney W. Grimes nlistf = optarg; 1799b50d902SRodney W. Grimes break; 180d0482be8SBrian Feldman case 'm': 181d0482be8SBrian Feldman mflg = 1; 182d0482be8SBrian Feldman break; 1839b50d902SRodney W. Grimes case 'n': 1849b50d902SRodney W. Grimes nflg = 1; 1859b50d902SRodney W. Grimes break; 1869b50d902SRodney W. Grimes case 'p': 1879b50d902SRodney W. Grimes if (pflg++) 1889b50d902SRodney W. Grimes usage(); 1899b50d902SRodney W. Grimes if (!isdigit(*optarg)) { 190c1e65942SPhilippe Charnier warnx("-p requires a process id"); 1919b50d902SRodney W. Grimes usage(); 1929b50d902SRodney W. Grimes } 1939b50d902SRodney W. Grimes what = KERN_PROC_PID; 1949b50d902SRodney W. Grimes arg = atoi(optarg); 1959b50d902SRodney W. Grimes break; 1969b50d902SRodney W. Grimes case 'u': 1979b50d902SRodney W. Grimes if (uflg++) 1989b50d902SRodney W. Grimes usage(); 199c1e65942SPhilippe Charnier if (!(passwd = getpwnam(optarg))) 200c1e65942SPhilippe Charnier errx(1, "%s: unknown uid", optarg); 2019b50d902SRodney W. Grimes what = KERN_PROC_UID; 2029b50d902SRodney W. Grimes arg = passwd->pw_uid; 2039b50d902SRodney W. Grimes break; 2049b50d902SRodney W. Grimes case 'v': 2059b50d902SRodney W. Grimes vflg = 1; 2069b50d902SRodney W. Grimes break; 2079b50d902SRodney W. Grimes case '?': 2089b50d902SRodney W. Grimes default: 2099b50d902SRodney W. Grimes usage(); 2109b50d902SRodney W. Grimes } 2119b50d902SRodney W. Grimes 2129b50d902SRodney W. Grimes if (*(argv += optind)) { 2139b50d902SRodney W. Grimes for (; *argv; ++argv) { 2149b50d902SRodney W. Grimes if (getfname(*argv)) 2159b50d902SRodney W. Grimes checkfile = 1; 2169b50d902SRodney W. Grimes } 2179b50d902SRodney W. Grimes if (!checkfile) /* file(s) specified, but none accessable */ 2189b50d902SRodney W. Grimes exit(1); 2199b50d902SRodney W. Grimes } 2209b50d902SRodney W. Grimes 2219b50d902SRodney W. Grimes if (fsflg && !checkfile) { 2229b50d902SRodney W. Grimes /* -f with no files means use wd */ 2239b50d902SRodney W. Grimes if (getfname(".") == 0) 2249b50d902SRodney W. Grimes exit(1); 2259b50d902SRodney W. Grimes checkfile = 1; 2269b50d902SRodney W. Grimes } 2279b50d902SRodney W. Grimes 2281f910d6cSDag-Erling Smørgrav if (memf != NULL) 2291f910d6cSDag-Erling Smørgrav fstat_kvm(what, arg); 2301f910d6cSDag-Erling Smørgrav else 2311f910d6cSDag-Erling Smørgrav fstat_sysctl(what, arg); 2321f910d6cSDag-Erling Smørgrav exit(0); 2331f910d6cSDag-Erling Smørgrav } 2341f910d6cSDag-Erling Smørgrav 2351f910d6cSDag-Erling Smørgrav static void 2361f910d6cSDag-Erling Smørgrav print_header(void) 2371f910d6cSDag-Erling Smørgrav { 2381f910d6cSDag-Erling Smørgrav 2391f910d6cSDag-Erling Smørgrav if (nflg) 2401f910d6cSDag-Erling Smørgrav printf("%s", 2411f910d6cSDag-Erling Smørgrav "USER CMD PID FD DEV INUM MODE SZ|DV R/W"); 2421f910d6cSDag-Erling Smørgrav else 2431f910d6cSDag-Erling Smørgrav printf("%s", 2441f910d6cSDag-Erling Smørgrav "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W"); 2451f910d6cSDag-Erling Smørgrav if (checkfile && fsflg == 0) 2461f910d6cSDag-Erling Smørgrav printf(" NAME\n"); 2471f910d6cSDag-Erling Smørgrav else 2481f910d6cSDag-Erling Smørgrav putchar('\n'); 2491f910d6cSDag-Erling Smørgrav } 2501f910d6cSDag-Erling Smørgrav 2511f910d6cSDag-Erling Smørgrav static void 2521f910d6cSDag-Erling Smørgrav fstat_kvm(int what, int arg) 2531f910d6cSDag-Erling Smørgrav { 2541f910d6cSDag-Erling Smørgrav struct kinfo_proc *p, *plast; 2551f910d6cSDag-Erling Smørgrav char buf[_POSIX2_LINE_MAX]; 2561f910d6cSDag-Erling Smørgrav int cnt; 2571f910d6cSDag-Erling Smørgrav 2581f910d6cSDag-Erling Smørgrav ALLOC_OFILES(256); /* reserve space for file pointers */ 2591f910d6cSDag-Erling Smørgrav 2609b50d902SRodney W. Grimes /* 2619b50d902SRodney W. Grimes * Discard setgid privileges if not the running kernel so that bad 2629b50d902SRodney W. Grimes * guys can't print interesting stuff from kernel memory. 2639b50d902SRodney W. Grimes */ 2649b50d902SRodney W. Grimes if (nlistf != NULL || memf != NULL) 2659b50d902SRodney W. Grimes setgid(getgid()); 2669b50d902SRodney W. Grimes 267c1e65942SPhilippe Charnier if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL) 268c1e65942SPhilippe Charnier errx(1, "%s", buf); 2693b7e5cccSRuslan Ermilov setgid(getgid()); 2709b50d902SRodney W. Grimes #ifdef notdef 271c1e65942SPhilippe Charnier if (kvm_nlist(kd, nl) != 0) 272c1e65942SPhilippe Charnier errx(1, "no namelist: %s", kvm_geterr(kd)); 2739b50d902SRodney W. Grimes #endif 274c1e65942SPhilippe Charnier if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) 275c1e65942SPhilippe Charnier errx(1, "%s", kvm_geterr(kd)); 2769b50d902SRodney W. Grimes for (plast = &p[cnt]; p < plast; ++p) { 2771f7d2501SKirk McKusick if (p->ki_stat == SZOMB) 2789b50d902SRodney W. Grimes continue; 2799b50d902SRodney W. Grimes dofiles(p); 280d0482be8SBrian Feldman if (mflg) 281d0482be8SBrian Feldman dommap(p); 2829b50d902SRodney W. Grimes } 2831f910d6cSDag-Erling Smørgrav } 2841f910d6cSDag-Erling Smørgrav 2851f910d6cSDag-Erling Smørgrav static void 2861f910d6cSDag-Erling Smørgrav fstat_sysctl(int what, int arg) 2871f910d6cSDag-Erling Smørgrav { 2881f910d6cSDag-Erling Smørgrav 2891f910d6cSDag-Erling Smørgrav /* not yet implemented */ 2901f910d6cSDag-Erling Smørgrav fstat_kvm(what, arg); 2919b50d902SRodney W. Grimes } 2929b50d902SRodney W. Grimes 29327d57ea9SDavid Malone const char *Uname, *Comm; 2949b50d902SRodney W. Grimes int Pid; 2959b50d902SRodney W. Grimes 2969b50d902SRodney W. Grimes #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \ 2979b50d902SRodney W. Grimes switch(i) { \ 2989b50d902SRodney W. Grimes case TEXT: \ 2999b50d902SRodney W. Grimes printf(" text"); \ 3009b50d902SRodney W. Grimes break; \ 3019b50d902SRodney W. Grimes case CDIR: \ 3029b50d902SRodney W. Grimes printf(" wd"); \ 3039b50d902SRodney W. Grimes break; \ 3049b50d902SRodney W. Grimes case RDIR: \ 3059b50d902SRodney W. Grimes printf(" root"); \ 3069b50d902SRodney W. Grimes break; \ 3079b50d902SRodney W. Grimes case TRACE: \ 3089b50d902SRodney W. Grimes printf(" tr"); \ 3099b50d902SRodney W. Grimes break; \ 310d0482be8SBrian Feldman case MMAP: \ 311d0482be8SBrian Feldman printf(" mmap"); \ 312d0482be8SBrian Feldman break; \ 3139b50d902SRodney W. Grimes default: \ 3149b50d902SRodney W. Grimes printf(" %4d", i); \ 3159b50d902SRodney W. Grimes break; \ 3169b50d902SRodney W. Grimes } 3179b50d902SRodney W. Grimes 3189b50d902SRodney W. Grimes /* 3199b50d902SRodney W. Grimes * print open files attributed to this process 3209b50d902SRodney W. Grimes */ 3219b50d902SRodney W. Grimes void 3229b50d902SRodney W. Grimes dofiles(kp) 3239b50d902SRodney W. Grimes struct kinfo_proc *kp; 3249b50d902SRodney W. Grimes { 325c1e65942SPhilippe Charnier int i; 3269b50d902SRodney W. Grimes struct file file; 3279b50d902SRodney W. Grimes struct filedesc0 filed0; 3289b50d902SRodney W. Grimes #define filed filed0.fd_fd 3299b50d902SRodney W. Grimes 3301f7d2501SKirk McKusick Uname = user_from_uid(kp->ki_uid, 0); 3311f7d2501SKirk McKusick Pid = kp->ki_pid; 3321f7d2501SKirk McKusick Comm = kp->ki_comm; 3339b50d902SRodney W. Grimes 3341f7d2501SKirk McKusick if (kp->ki_fd == NULL) 3359b50d902SRodney W. Grimes return; 3361f7d2501SKirk McKusick if (!KVM_READ(kp->ki_fd, &filed0, sizeof (filed0))) { 33722694ebaSBruce Evans dprintf(stderr, "can't read filedesc at %p for pid %d\n", 3381f7d2501SKirk McKusick (void *)kp->ki_fd, Pid); 3399b50d902SRodney W. Grimes return; 3409b50d902SRodney W. Grimes } 3419b50d902SRodney W. Grimes /* 3429b50d902SRodney W. Grimes * root directory vnode, if one 3439b50d902SRodney W. Grimes */ 3449b50d902SRodney W. Grimes if (filed.fd_rdir) 3459b50d902SRodney W. Grimes vtrans(filed.fd_rdir, RDIR, FREAD); 3469b50d902SRodney W. Grimes /* 3479b50d902SRodney W. Grimes * current working directory vnode 3489b50d902SRodney W. Grimes */ 3499b50d902SRodney W. Grimes vtrans(filed.fd_cdir, CDIR, FREAD); 3509b50d902SRodney W. Grimes /* 3519b50d902SRodney W. Grimes * ktrace vnode, if one 3529b50d902SRodney W. Grimes */ 3531f7d2501SKirk McKusick if (kp->ki_tracep) 3541f7d2501SKirk McKusick vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE); 3559b50d902SRodney W. Grimes /* 356ca7e3117SPeter Wemm * text vnode, if one 357ca7e3117SPeter Wemm */ 3581f7d2501SKirk McKusick if (kp->ki_textvp) 3591f7d2501SKirk McKusick vtrans(kp->ki_textvp, TEXT, FREAD); 360ca7e3117SPeter Wemm /* 3619b50d902SRodney W. Grimes * open files 3629b50d902SRodney W. Grimes */ 3639b50d902SRodney W. Grimes #define FPSIZE (sizeof (struct file *)) 3649b50d902SRodney W. Grimes ALLOC_OFILES(filed.fd_lastfile+1); 3659b50d902SRodney W. Grimes if (filed.fd_nfiles > NDFILE) { 3669b50d902SRodney W. Grimes if (!KVM_READ(filed.fd_ofiles, ofiles, 3679b50d902SRodney W. Grimes (filed.fd_lastfile+1) * FPSIZE)) { 3689b50d902SRodney W. Grimes dprintf(stderr, 36922694ebaSBruce Evans "can't read file structures at %p for pid %d\n", 37022694ebaSBruce Evans (void *)filed.fd_ofiles, Pid); 3719b50d902SRodney W. Grimes return; 3729b50d902SRodney W. Grimes } 3739b50d902SRodney W. Grimes } else 3749b50d902SRodney W. Grimes bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE); 3759b50d902SRodney W. Grimes for (i = 0; i <= filed.fd_lastfile; i++) { 3769b50d902SRodney W. Grimes if (ofiles[i] == NULL) 3779b50d902SRodney W. Grimes continue; 3789b50d902SRodney W. Grimes if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) { 37922694ebaSBruce Evans dprintf(stderr, "can't read file %d at %p for pid %d\n", 38022694ebaSBruce Evans i, (void *)ofiles[i], Pid); 3819b50d902SRodney W. Grimes continue; 3829b50d902SRodney W. Grimes } 3839b50d902SRodney W. Grimes if (file.f_type == DTYPE_VNODE) 3849b50d902SRodney W. Grimes vtrans((struct vnode *)file.f_data, i, file.f_flag); 3859b50d902SRodney W. Grimes else if (file.f_type == DTYPE_SOCKET) { 3869b50d902SRodney W. Grimes if (checkfile == 0) 3879b50d902SRodney W. Grimes socktrans((struct socket *)file.f_data, i); 3889b50d902SRodney W. Grimes } 3891c17fc99SPeter Wemm #ifdef DTYPE_PIPE 3901c17fc99SPeter Wemm else if (file.f_type == DTYPE_PIPE) { 3911c17fc99SPeter Wemm if (checkfile == 0) 3921c17fc99SPeter Wemm pipetrans((struct pipe *)file.f_data, i, 3931c17fc99SPeter Wemm file.f_flag); 3941c17fc99SPeter Wemm } 3951c17fc99SPeter Wemm #endif 396e6ebeb5fSPeter Pentchev #ifdef DTYPE_FIFO 397e6ebeb5fSPeter Pentchev else if (file.f_type == DTYPE_FIFO) { 398e6ebeb5fSPeter Pentchev if (checkfile == 0) 399e6ebeb5fSPeter Pentchev vtrans((struct vnode *)file.f_data, i, 400e6ebeb5fSPeter Pentchev file.f_flag); 401e6ebeb5fSPeter Pentchev } 402e6ebeb5fSPeter Pentchev #endif 4039b50d902SRodney W. Grimes else { 4049b50d902SRodney W. Grimes dprintf(stderr, 4059b50d902SRodney W. Grimes "unknown file type %d for file %d of pid %d\n", 4069b50d902SRodney W. Grimes file.f_type, i, Pid); 4079b50d902SRodney W. Grimes } 4089b50d902SRodney W. Grimes } 4099b50d902SRodney W. Grimes } 4109b50d902SRodney W. Grimes 4119b50d902SRodney W. Grimes void 412d0482be8SBrian Feldman dommap(kp) 413d0482be8SBrian Feldman struct kinfo_proc *kp; 414d0482be8SBrian Feldman { 415d0482be8SBrian Feldman vm_map_t map; 4161f7d2501SKirk McKusick struct vmspace vmspace; 417d0482be8SBrian Feldman struct vm_map_entry entry; 418d0482be8SBrian Feldman vm_map_entry_t entryp; 419d0482be8SBrian Feldman struct vm_object object; 420d0482be8SBrian Feldman vm_object_t objp; 421d0482be8SBrian Feldman int prot, fflags; 422d0482be8SBrian Feldman 4231f7d2501SKirk McKusick if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) { 4241f7d2501SKirk McKusick dprintf(stderr, 4251f7d2501SKirk McKusick "can't read vmspace at %p for pid %d\n", 4261f7d2501SKirk McKusick (void *)kp->ki_vmspace, Pid); 427d0482be8SBrian Feldman return; 428d0482be8SBrian Feldman } 429d0482be8SBrian Feldman map = &vmspace.vm_map; 430d0482be8SBrian Feldman 431b654e106SIan Dowse for (entryp = map->header.next; 432b654e106SIan Dowse entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) { 433d0482be8SBrian Feldman if (!KVM_READ(entryp, &entry, sizeof(entry))) { 434d0482be8SBrian Feldman dprintf(stderr, 435d0482be8SBrian Feldman "can't read vm_map_entry at %p for pid %d\n", 436d0482be8SBrian Feldman (void *)entryp, Pid); 437d0482be8SBrian Feldman return; 438d0482be8SBrian Feldman } 439d0482be8SBrian Feldman 440d0482be8SBrian Feldman if (entry.eflags & MAP_ENTRY_IS_SUB_MAP) 441d0482be8SBrian Feldman continue; 442d0482be8SBrian Feldman 443d0482be8SBrian Feldman if ((objp = entry.object.vm_object) == NULL) 444d0482be8SBrian Feldman continue; 445d0482be8SBrian Feldman 446d0482be8SBrian Feldman for (; objp; objp = object.backing_object) { 447d0482be8SBrian Feldman if (!KVM_READ(objp, &object, sizeof(object))) { 448d0482be8SBrian Feldman dprintf(stderr, 449d0482be8SBrian Feldman "can't read vm_object at %p for pid %d\n", 450d0482be8SBrian Feldman (void *)objp, Pid); 451d0482be8SBrian Feldman return; 452d0482be8SBrian Feldman } 453d0482be8SBrian Feldman } 454d0482be8SBrian Feldman 455d0482be8SBrian Feldman prot = entry.protection; 456d0482be8SBrian Feldman fflags = (prot & VM_PROT_READ ? FREAD : 0) | 457d0482be8SBrian Feldman (prot & VM_PROT_WRITE ? FWRITE : 0); 458d0482be8SBrian Feldman 459d0482be8SBrian Feldman switch (object.type) { 460d0482be8SBrian Feldman case OBJT_VNODE: 461d0482be8SBrian Feldman vtrans((struct vnode *)object.handle, MMAP, fflags); 462d0482be8SBrian Feldman break; 463d0482be8SBrian Feldman default: 464d0482be8SBrian Feldman break; 465d0482be8SBrian Feldman } 466d0482be8SBrian Feldman } 467d0482be8SBrian Feldman } 468d0482be8SBrian Feldman 469d0482be8SBrian Feldman void 4709b50d902SRodney W. Grimes vtrans(vp, i, flag) 4719b50d902SRodney W. Grimes struct vnode *vp; 4729b50d902SRodney W. Grimes int i; 4739b50d902SRodney W. Grimes int flag; 4749b50d902SRodney W. Grimes { 4759b50d902SRodney W. Grimes struct vnode vn; 4769b50d902SRodney W. Grimes struct filestat fst; 4779b50d902SRodney W. Grimes char rw[3], mode[15]; 47827d57ea9SDavid Malone const char *badtype, *filename; 4799b50d902SRodney W. Grimes 4809b50d902SRodney W. Grimes filename = badtype = NULL; 4819b50d902SRodney W. Grimes if (!KVM_READ(vp, &vn, sizeof (struct vnode))) { 48222694ebaSBruce Evans dprintf(stderr, "can't read vnode at %p for pid %d\n", 48322694ebaSBruce Evans (void *)vp, Pid); 4849b50d902SRodney W. Grimes return; 4859b50d902SRodney W. Grimes } 4869b50d902SRodney W. Grimes if (vn.v_type == VNON || vn.v_tag == VT_NON) 4879b50d902SRodney W. Grimes badtype = "none"; 4889b50d902SRodney W. Grimes else if (vn.v_type == VBAD) 4899b50d902SRodney W. Grimes badtype = "bad"; 4909b50d902SRodney W. Grimes else 4919b50d902SRodney W. Grimes switch (vn.v_tag) { 4929b50d902SRodney W. Grimes case VT_UFS: 4939b50d902SRodney W. Grimes if (!ufs_filestat(&vn, &fst)) 4949b50d902SRodney W. Grimes badtype = "error"; 4959b50d902SRodney W. Grimes break; 496fca066feSBrian Feldman 497fca066feSBrian Feldman case VT_DEVFS: 498fca066feSBrian Feldman if (!devfs_filestat(&vn, &fst)) 499fca066feSBrian Feldman badtype = "error"; 500fca066feSBrian Feldman break; 501fca066feSBrian Feldman 5029b50d902SRodney W. Grimes case VT_NFS: 5039b50d902SRodney W. Grimes if (!nfs_filestat(&vn, &fst)) 5049b50d902SRodney W. Grimes badtype = "error"; 5059b50d902SRodney W. Grimes break; 50606e88916SBrian Feldman 50706e88916SBrian Feldman case VT_MSDOSFS: 50806e88916SBrian Feldman if (!msdosfs_filestat(&vn, &fst)) 50906e88916SBrian Feldman badtype = "error"; 51006e88916SBrian Feldman break; 51106e88916SBrian Feldman 51206e88916SBrian Feldman case VT_ISOFS: 51306e88916SBrian Feldman if (!isofs_filestat(&vn, &fst)) 51406e88916SBrian Feldman badtype = "error"; 51506e88916SBrian Feldman break; 51606e88916SBrian Feldman 5179b50d902SRodney W. Grimes default: { 5189b50d902SRodney W. Grimes static char unknown[10]; 51927d57ea9SDavid Malone sprintf(unknown, "?(%x)", vn.v_tag); 52027d57ea9SDavid Malone badtype = unknown; 5219b50d902SRodney W. Grimes break;; 5229b50d902SRodney W. Grimes } 5239b50d902SRodney W. Grimes } 5249b50d902SRodney W. Grimes if (checkfile) { 5259b50d902SRodney W. Grimes int fsmatch = 0; 52627d57ea9SDavid Malone DEVS *d; 5279b50d902SRodney W. Grimes 5289b50d902SRodney W. Grimes if (badtype) 5299b50d902SRodney W. Grimes return; 5309b50d902SRodney W. Grimes for (d = devs; d != NULL; d = d->next) 5319b50d902SRodney W. Grimes if (d->fsid == fst.fsid) { 5329b50d902SRodney W. Grimes fsmatch = 1; 5339b50d902SRodney W. Grimes if (d->ino == fst.fileid) { 5349b50d902SRodney W. Grimes filename = d->name; 5359b50d902SRodney W. Grimes break; 5369b50d902SRodney W. Grimes } 5379b50d902SRodney W. Grimes } 5389b50d902SRodney W. Grimes if (fsmatch == 0 || (filename == NULL && fsflg == 0)) 5399b50d902SRodney W. Grimes return; 5409b50d902SRodney W. Grimes } 5419b50d902SRodney W. Grimes PREFIX(i); 5429b50d902SRodney W. Grimes if (badtype) { 5439b50d902SRodney W. Grimes (void)printf(" - - %10s -\n", badtype); 5449b50d902SRodney W. Grimes return; 5459b50d902SRodney W. Grimes } 5469b50d902SRodney W. Grimes if (nflg) 5479b50d902SRodney W. Grimes (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid)); 5489b50d902SRodney W. Grimes else 5499b50d902SRodney W. Grimes (void)printf(" %-8s", getmnton(vn.v_mount)); 5509b50d902SRodney W. Grimes if (nflg) 5519b50d902SRodney W. Grimes (void)sprintf(mode, "%o", fst.mode); 5529b50d902SRodney W. Grimes else 5539b50d902SRodney W. Grimes strmode(fst.mode, mode); 55422694ebaSBruce Evans (void)printf(" %6ld %10s", fst.fileid, mode); 5559b50d902SRodney W. Grimes switch (vn.v_type) { 5569b50d902SRodney W. Grimes case VBLK: 5579b50d902SRodney W. Grimes case VCHR: { 5589b50d902SRodney W. Grimes char *name; 5599b50d902SRodney W. Grimes 5609b50d902SRodney W. Grimes if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ? 5619b50d902SRodney W. Grimes S_IFCHR : S_IFBLK)) == NULL)) 5629b50d902SRodney W. Grimes printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev)); 5639b50d902SRodney W. Grimes else 5649b50d902SRodney W. Grimes printf(" %6s", name); 5659b50d902SRodney W. Grimes break; 5669b50d902SRodney W. Grimes } 5679b50d902SRodney W. Grimes default: 568df8327e7SAlexander Langer printf(" %6lu", fst.size); 5699b50d902SRodney W. Grimes } 5709b50d902SRodney W. Grimes rw[0] = '\0'; 5719b50d902SRodney W. Grimes if (flag & FREAD) 5729b50d902SRodney W. Grimes strcat(rw, "r"); 5739b50d902SRodney W. Grimes if (flag & FWRITE) 5749b50d902SRodney W. Grimes strcat(rw, "w"); 5759b50d902SRodney W. Grimes printf(" %2s", rw); 5769b50d902SRodney W. Grimes if (filename && !fsflg) 5779b50d902SRodney W. Grimes printf(" %s", filename); 5789b50d902SRodney W. Grimes putchar('\n'); 5799b50d902SRodney W. Grimes } 5809b50d902SRodney W. Grimes 5819b50d902SRodney W. Grimes int 5829b50d902SRodney W. Grimes ufs_filestat(vp, fsp) 5839b50d902SRodney W. Grimes struct vnode *vp; 5849b50d902SRodney W. Grimes struct filestat *fsp; 5859b50d902SRodney W. Grimes { 5869b50d902SRodney W. Grimes struct inode inode; 5879b50d902SRodney W. Grimes 5889b50d902SRodney W. Grimes if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 58922694ebaSBruce Evans dprintf(stderr, "can't read inode at %p for pid %d\n", 59022694ebaSBruce Evans (void *)VTOI(vp), Pid); 5919b50d902SRodney W. Grimes return 0; 5929b50d902SRodney W. Grimes } 59338cd1ee7SBrian Feldman /* 59438cd1ee7SBrian Feldman * The st_dev from stat(2) is a udev_t. These kernel structures 59538cd1ee7SBrian Feldman * contain dev_t structures. We need to convert to udev to make 59638cd1ee7SBrian Feldman * comparisons 59738cd1ee7SBrian Feldman */ 598b628b0dcSDavid Malone fsp->fsid = dev2udev(inode.i_dev); 5999b50d902SRodney W. Grimes fsp->fileid = (long)inode.i_number; 6009b50d902SRodney W. Grimes fsp->mode = (mode_t)inode.i_mode; 6019b50d902SRodney W. Grimes fsp->size = (u_long)inode.i_size; 6029b50d902SRodney W. Grimes fsp->rdev = inode.i_rdev; 6039b50d902SRodney W. Grimes 6049b50d902SRodney W. Grimes return 1; 6059b50d902SRodney W. Grimes } 6069b50d902SRodney W. Grimes 6079b50d902SRodney W. Grimes int 608fca066feSBrian Feldman devfs_filestat(vp, fsp) 609fca066feSBrian Feldman struct vnode *vp; 610fca066feSBrian Feldman struct filestat *fsp; 611fca066feSBrian Feldman { 612fca066feSBrian Feldman struct devfs_dirent devfs_dirent; 613fca066feSBrian Feldman struct mount mount; 614fca066feSBrian Feldman struct vnode vnode; 615fca066feSBrian Feldman 616fca066feSBrian Feldman if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) { 617fca066feSBrian Feldman dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n", 618fca066feSBrian Feldman (void *)vp->v_data, Pid); 619fca066feSBrian Feldman return 0; 620fca066feSBrian Feldman } 621fca066feSBrian Feldman if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) { 622fca066feSBrian Feldman dprintf(stderr, "can't read mount at %p for pid %d\n", 623fca066feSBrian Feldman (void *)vp->v_mount, Pid); 624fca066feSBrian Feldman return 0; 625fca066feSBrian Feldman } 626fca066feSBrian Feldman if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) { 627fca066feSBrian Feldman dprintf(stderr, "can't read vnode at %p for pid %d\n", 628fca066feSBrian Feldman (void *)devfs_dirent.de_vnode, Pid); 629fca066feSBrian Feldman return 0; 630fca066feSBrian Feldman } 631fca066feSBrian Feldman fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0]; 632fca066feSBrian Feldman fsp->fileid = devfs_dirent.de_inode; 633fca066feSBrian Feldman fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR; 634fca066feSBrian Feldman fsp->size = 0; 635fca066feSBrian Feldman fsp->rdev = dev2udev((dev_t)vnode.v_rdev); 636fca066feSBrian Feldman 637fca066feSBrian Feldman return 1; 638fca066feSBrian Feldman } 639fca066feSBrian Feldman 640fca066feSBrian Feldman int 6419b50d902SRodney W. Grimes nfs_filestat(vp, fsp) 6429b50d902SRodney W. Grimes struct vnode *vp; 6439b50d902SRodney W. Grimes struct filestat *fsp; 6449b50d902SRodney W. Grimes { 6459b50d902SRodney W. Grimes struct nfsnode nfsnode; 64627d57ea9SDavid Malone mode_t mode; 6479b50d902SRodney W. Grimes 6489b50d902SRodney W. Grimes if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) { 64922694ebaSBruce Evans dprintf(stderr, "can't read nfsnode at %p for pid %d\n", 65022694ebaSBruce Evans (void *)VTONFS(vp), Pid); 6519b50d902SRodney W. Grimes return 0; 6529b50d902SRodney W. Grimes } 6539b50d902SRodney W. Grimes fsp->fsid = nfsnode.n_vattr.va_fsid; 6549b50d902SRodney W. Grimes fsp->fileid = nfsnode.n_vattr.va_fileid; 6559b50d902SRodney W. Grimes fsp->size = nfsnode.n_size; 6569b50d902SRodney W. Grimes fsp->rdev = nfsnode.n_vattr.va_rdev; 6579b50d902SRodney W. Grimes mode = (mode_t)nfsnode.n_vattr.va_mode; 6589b50d902SRodney W. Grimes switch (vp->v_type) { 6599b50d902SRodney W. Grimes case VREG: 6609b50d902SRodney W. Grimes mode |= S_IFREG; 6619b50d902SRodney W. Grimes break; 6629b50d902SRodney W. Grimes case VDIR: 6639b50d902SRodney W. Grimes mode |= S_IFDIR; 6649b50d902SRodney W. Grimes break; 6659b50d902SRodney W. Grimes case VBLK: 6669b50d902SRodney W. Grimes mode |= S_IFBLK; 6679b50d902SRodney W. Grimes break; 6689b50d902SRodney W. Grimes case VCHR: 6699b50d902SRodney W. Grimes mode |= S_IFCHR; 6709b50d902SRodney W. Grimes break; 6719b50d902SRodney W. Grimes case VLNK: 6729b50d902SRodney W. Grimes mode |= S_IFLNK; 6739b50d902SRodney W. Grimes break; 6749b50d902SRodney W. Grimes case VSOCK: 6759b50d902SRodney W. Grimes mode |= S_IFSOCK; 6769b50d902SRodney W. Grimes break; 6779b50d902SRodney W. Grimes case VFIFO: 6789b50d902SRodney W. Grimes mode |= S_IFIFO; 6799b50d902SRodney W. Grimes break; 6800b7a57ddSRuslan Ermilov case VNON: 6810b7a57ddSRuslan Ermilov case VBAD: 6820b7a57ddSRuslan Ermilov return 0; 6839b50d902SRodney W. Grimes }; 6849b50d902SRodney W. Grimes fsp->mode = mode; 6859b50d902SRodney W. Grimes 6869b50d902SRodney W. Grimes return 1; 6879b50d902SRodney W. Grimes } 6889b50d902SRodney W. Grimes 6899b50d902SRodney W. Grimes 6909b50d902SRodney W. Grimes char * 6919b50d902SRodney W. Grimes getmnton(m) 6929b50d902SRodney W. Grimes struct mount *m; 6939b50d902SRodney W. Grimes { 6949b50d902SRodney W. Grimes static struct mount mount; 6959b50d902SRodney W. Grimes static struct mtab { 6969b50d902SRodney W. Grimes struct mtab *next; 6979b50d902SRodney W. Grimes struct mount *m; 6989b50d902SRodney W. Grimes char mntonname[MNAMELEN]; 6999b50d902SRodney W. Grimes } *mhead = NULL; 70027d57ea9SDavid Malone struct mtab *mt; 7019b50d902SRodney W. Grimes 7029b50d902SRodney W. Grimes for (mt = mhead; mt != NULL; mt = mt->next) 7039b50d902SRodney W. Grimes if (m == mt->m) 7049b50d902SRodney W. Grimes return (mt->mntonname); 7059b50d902SRodney W. Grimes if (!KVM_READ(m, &mount, sizeof(struct mount))) { 70622694ebaSBruce Evans warnx("can't read mount table at %p", (void *)m); 7079b50d902SRodney W. Grimes return (NULL); 7089b50d902SRodney W. Grimes } 709c1e65942SPhilippe Charnier if ((mt = malloc(sizeof (struct mtab))) == NULL) 710c1e65942SPhilippe Charnier err(1, NULL); 7119b50d902SRodney W. Grimes mt->m = m; 7129b50d902SRodney W. Grimes bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN); 7139b50d902SRodney W. Grimes mt->next = mhead; 7149b50d902SRodney W. Grimes mhead = mt; 7159b50d902SRodney W. Grimes return (mt->mntonname); 7169b50d902SRodney W. Grimes } 7179b50d902SRodney W. Grimes 7189b50d902SRodney W. Grimes void 7191c17fc99SPeter Wemm pipetrans(pi, i, flag) 7201c17fc99SPeter Wemm struct pipe *pi; 7211c17fc99SPeter Wemm int i; 7221c17fc99SPeter Wemm int flag; 7231c17fc99SPeter Wemm { 7241c17fc99SPeter Wemm struct pipe pip; 7251c17fc99SPeter Wemm char rw[3]; 7261c17fc99SPeter Wemm 7271c17fc99SPeter Wemm PREFIX(i); 7281c17fc99SPeter Wemm 7291c17fc99SPeter Wemm /* fill in socket */ 7301c17fc99SPeter Wemm if (!KVM_READ(pi, &pip, sizeof(struct pipe))) { 73122694ebaSBruce Evans dprintf(stderr, "can't read pipe at %p\n", (void *)pi); 7321c17fc99SPeter Wemm goto bad; 7331c17fc99SPeter Wemm } 7341c17fc99SPeter Wemm 735df94d4d2SMatt Jacob printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer); 7361c17fc99SPeter Wemm printf(" %6d", (int)pip.pipe_buffer.cnt); 7371c17fc99SPeter Wemm rw[0] = '\0'; 7381c17fc99SPeter Wemm if (flag & FREAD) 7391c17fc99SPeter Wemm strcat(rw, "r"); 7401c17fc99SPeter Wemm if (flag & FWRITE) 7411c17fc99SPeter Wemm strcat(rw, "w"); 7421c17fc99SPeter Wemm printf(" %2s", rw); 7431c17fc99SPeter Wemm putchar('\n'); 7441c17fc99SPeter Wemm return; 7451c17fc99SPeter Wemm 7461c17fc99SPeter Wemm bad: 7471c17fc99SPeter Wemm printf("* error\n"); 7481c17fc99SPeter Wemm } 7491c17fc99SPeter Wemm 7501c17fc99SPeter Wemm void 7519b50d902SRodney W. Grimes socktrans(sock, i) 7529b50d902SRodney W. Grimes struct socket *sock; 7539b50d902SRodney W. Grimes int i; 7549b50d902SRodney W. Grimes { 75527d57ea9SDavid Malone static const char *stypename[] = { 7569b50d902SRodney W. Grimes "unused", /* 0 */ 7579b50d902SRodney W. Grimes "stream", /* 1 */ 7589b50d902SRodney W. Grimes "dgram", /* 2 */ 7599b50d902SRodney W. Grimes "raw", /* 3 */ 7609b50d902SRodney W. Grimes "rdm", /* 4 */ 7619b50d902SRodney W. Grimes "seqpak" /* 5 */ 7629b50d902SRodney W. Grimes }; 7639b50d902SRodney W. Grimes #define STYPEMAX 5 7649b50d902SRodney W. Grimes struct socket so; 7659b50d902SRodney W. Grimes struct protosw proto; 7669b50d902SRodney W. Grimes struct domain dom; 7679b50d902SRodney W. Grimes struct inpcb inpcb; 7689b50d902SRodney W. Grimes struct unpcb unpcb; 7699b50d902SRodney W. Grimes int len; 77027d57ea9SDavid Malone char dname[32]; 7719b50d902SRodney W. Grimes 7729b50d902SRodney W. Grimes PREFIX(i); 7739b50d902SRodney W. Grimes 7749b50d902SRodney W. Grimes /* fill in socket */ 7759b50d902SRodney W. Grimes if (!KVM_READ(sock, &so, sizeof(struct socket))) { 77622694ebaSBruce Evans dprintf(stderr, "can't read sock at %p\n", (void *)sock); 7779b50d902SRodney W. Grimes goto bad; 7789b50d902SRodney W. Grimes } 7799b50d902SRodney W. Grimes 7809b50d902SRodney W. Grimes /* fill in protosw entry */ 7819b50d902SRodney W. Grimes if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) { 78222694ebaSBruce Evans dprintf(stderr, "can't read protosw at %p", 78322694ebaSBruce Evans (void *)so.so_proto); 7849b50d902SRodney W. Grimes goto bad; 7859b50d902SRodney W. Grimes } 7869b50d902SRodney W. Grimes 7879b50d902SRodney W. Grimes /* fill in domain */ 7889b50d902SRodney W. Grimes if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) { 78922694ebaSBruce Evans dprintf(stderr, "can't read domain at %p\n", 79022694ebaSBruce Evans (void *)proto.pr_domain); 7919b50d902SRodney W. Grimes goto bad; 7929b50d902SRodney W. Grimes } 7939b50d902SRodney W. Grimes 7949b50d902SRodney W. Grimes if ((len = kvm_read(kd, (u_long)dom.dom_name, dname, 7959b50d902SRodney W. Grimes sizeof(dname) - 1)) < 0) { 79622694ebaSBruce Evans dprintf(stderr, "can't read domain name at %p\n", 79722694ebaSBruce Evans (void *)dom.dom_name); 7989b50d902SRodney W. Grimes dname[0] = '\0'; 7999b50d902SRodney W. Grimes } 8009b50d902SRodney W. Grimes else 8019b50d902SRodney W. Grimes dname[len] = '\0'; 8029b50d902SRodney W. Grimes 8039b50d902SRodney W. Grimes if ((u_short)so.so_type > STYPEMAX) 8049b50d902SRodney W. Grimes printf("* %s ?%d", dname, so.so_type); 8059b50d902SRodney W. Grimes else 8069b50d902SRodney W. Grimes printf("* %s %s", dname, stypename[so.so_type]); 8079b50d902SRodney W. Grimes 8089b50d902SRodney W. Grimes /* 8099b50d902SRodney W. Grimes * protocol specific formatting 8109b50d902SRodney W. Grimes * 8119b50d902SRodney W. Grimes * Try to find interesting things to print. For tcp, the interesting 8129b50d902SRodney W. Grimes * thing is the address of the tcpcb, for udp and others, just the 8139b50d902SRodney W. Grimes * inpcb (socket pcb). For unix domain, its the address of the socket 8149b50d902SRodney W. Grimes * pcb and the address of the connected pcb (if connected). Otherwise 8159b50d902SRodney W. Grimes * just print the protocol number and address of the socket itself. 8169b50d902SRodney W. Grimes * The idea is not to duplicate netstat, but to make available enough 8179b50d902SRodney W. Grimes * information for further analysis. 8189b50d902SRodney W. Grimes */ 8199b50d902SRodney W. Grimes switch(dom.dom_family) { 8209b50d902SRodney W. Grimes case AF_INET: 821677e00c0SYoshinobu Inoue case AF_INET6: 8229b50d902SRodney W. Grimes getinetproto(proto.pr_protocol); 8239b50d902SRodney W. Grimes if (proto.pr_protocol == IPPROTO_TCP ) { 8249b50d902SRodney W. Grimes if (so.so_pcb) { 8259b50d902SRodney W. Grimes if (kvm_read(kd, (u_long)so.so_pcb, 8269b50d902SRodney W. Grimes (char *)&inpcb, sizeof(struct inpcb)) 8279b50d902SRodney W. Grimes != sizeof(struct inpcb)) { 8289b50d902SRodney W. Grimes dprintf(stderr, 82922694ebaSBruce Evans "can't read inpcb at %p\n", 83022694ebaSBruce Evans (void *)so.so_pcb); 8319b50d902SRodney W. Grimes goto bad; 8329b50d902SRodney W. Grimes } 833df94d4d2SMatt Jacob printf(" %lx", (u_long)inpcb.inp_ppcb); 8349b50d902SRodney W. Grimes } 8359b50d902SRodney W. Grimes } 8369b50d902SRodney W. Grimes else if (so.so_pcb) 837df94d4d2SMatt Jacob printf(" %lx", (u_long)so.so_pcb); 8389b50d902SRodney W. Grimes break; 8399b50d902SRodney W. Grimes case AF_UNIX: 8409b50d902SRodney W. Grimes /* print address of pcb and connected pcb */ 8419b50d902SRodney W. Grimes if (so.so_pcb) { 842df94d4d2SMatt Jacob printf(" %lx", (u_long)so.so_pcb); 8439b50d902SRodney W. Grimes if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb, 8449b50d902SRodney W. Grimes sizeof(struct unpcb)) != sizeof(struct unpcb)){ 84522694ebaSBruce Evans dprintf(stderr, "can't read unpcb at %p\n", 84622694ebaSBruce Evans (void *)so.so_pcb); 8479b50d902SRodney W. Grimes goto bad; 8489b50d902SRodney W. Grimes } 8499b50d902SRodney W. Grimes if (unpcb.unp_conn) { 8509b50d902SRodney W. Grimes char shoconn[4], *cp; 8519b50d902SRodney W. Grimes 8529b50d902SRodney W. Grimes cp = shoconn; 8539b50d902SRodney W. Grimes if (!(so.so_state & SS_CANTRCVMORE)) 8549b50d902SRodney W. Grimes *cp++ = '<'; 8559b50d902SRodney W. Grimes *cp++ = '-'; 8569b50d902SRodney W. Grimes if (!(so.so_state & SS_CANTSENDMORE)) 8579b50d902SRodney W. Grimes *cp++ = '>'; 8589b50d902SRodney W. Grimes *cp = '\0'; 859df94d4d2SMatt Jacob printf(" %s %lx", shoconn, 860df94d4d2SMatt Jacob (u_long)unpcb.unp_conn); 8619b50d902SRodney W. Grimes } 8629b50d902SRodney W. Grimes } 8639b50d902SRodney W. Grimes break; 8649b50d902SRodney W. Grimes default: 8659b50d902SRodney W. Grimes /* print protocol number and socket address */ 866df94d4d2SMatt Jacob printf(" %d %lx", proto.pr_protocol, (u_long)sock); 8679b50d902SRodney W. Grimes } 8689b50d902SRodney W. Grimes printf("\n"); 8699b50d902SRodney W. Grimes return; 8709b50d902SRodney W. Grimes bad: 8719b50d902SRodney W. Grimes printf("* error\n"); 8729b50d902SRodney W. Grimes } 8739b50d902SRodney W. Grimes 87438cd1ee7SBrian Feldman 87538cd1ee7SBrian Feldman /* 87638cd1ee7SBrian Feldman * Read the specinfo structure in the kernel (as pointed to by a dev_t) 87738cd1ee7SBrian Feldman * in order to work out the associated udev_t 87838cd1ee7SBrian Feldman */ 87938cd1ee7SBrian Feldman udev_t 88038cd1ee7SBrian Feldman dev2udev(dev) 88138cd1ee7SBrian Feldman dev_t dev; 88238cd1ee7SBrian Feldman { 88338cd1ee7SBrian Feldman struct specinfo si; 88438cd1ee7SBrian Feldman 88538cd1ee7SBrian Feldman if (KVM_READ(dev, &si, sizeof si)) { 88638cd1ee7SBrian Feldman return si.si_udev; 88738cd1ee7SBrian Feldman } else { 888df94d4d2SMatt Jacob dprintf(stderr, "can't convert dev_t %x to a udev_t\n", dev); 88938cd1ee7SBrian Feldman return -1; 89038cd1ee7SBrian Feldman } 89138cd1ee7SBrian Feldman } 89238cd1ee7SBrian Feldman 8939b50d902SRodney W. Grimes /* 8949b50d902SRodney W. Grimes * getinetproto -- 8959b50d902SRodney W. Grimes * print name of protocol number 8969b50d902SRodney W. Grimes */ 8979b50d902SRodney W. Grimes void 8989b50d902SRodney W. Grimes getinetproto(number) 8999b50d902SRodney W. Grimes int number; 9009b50d902SRodney W. Grimes { 9010b7a57ddSRuslan Ermilov static int isopen; 90227d57ea9SDavid Malone struct protoent *pe; 9039b50d902SRodney W. Grimes 9040b7a57ddSRuslan Ermilov if (!isopen) 9050b7a57ddSRuslan Ermilov setprotoent(++isopen); 9060b7a57ddSRuslan Ermilov if ((pe = getprotobynumber(number)) != NULL) 9070b7a57ddSRuslan Ermilov printf(" %s", pe->p_name); 9080b7a57ddSRuslan Ermilov else 9099b50d902SRodney W. Grimes printf(" %d", number); 9109b50d902SRodney W. Grimes } 9119b50d902SRodney W. Grimes 9121c17fc99SPeter Wemm int 9139b50d902SRodney W. Grimes getfname(filename) 91427d57ea9SDavid Malone const char *filename; 9159b50d902SRodney W. Grimes { 9169b50d902SRodney W. Grimes struct stat statbuf; 9179b50d902SRodney W. Grimes DEVS *cur; 9189b50d902SRodney W. Grimes 9199b50d902SRodney W. Grimes if (stat(filename, &statbuf)) { 920c1e65942SPhilippe Charnier warn("%s", filename); 9219b50d902SRodney W. Grimes return(0); 9229b50d902SRodney W. Grimes } 923c1e65942SPhilippe Charnier if ((cur = malloc(sizeof(DEVS))) == NULL) 924c1e65942SPhilippe Charnier err(1, NULL); 9259b50d902SRodney W. Grimes cur->next = devs; 9269b50d902SRodney W. Grimes devs = cur; 9279b50d902SRodney W. Grimes 9289b50d902SRodney W. Grimes cur->ino = statbuf.st_ino; 929b628b0dcSDavid Malone cur->fsid = statbuf.st_dev; 9309b50d902SRodney W. Grimes cur->name = filename; 9319b50d902SRodney W. Grimes return(1); 9329b50d902SRodney W. Grimes } 9339b50d902SRodney W. Grimes 9349b50d902SRodney W. Grimes void 9359b50d902SRodney W. Grimes usage() 9369b50d902SRodney W. Grimes { 9379b50d902SRodney W. Grimes (void)fprintf(stderr, 938d0482be8SBrian Feldman "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n"); 9399b50d902SRodney W. Grimes exit(1); 9409b50d902SRodney W. Grimes } 941