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 449b50d902SRodney W. Grimes #endif /* not lint */ 45e026a48cSDavid E. O'Brien #include <sys/cdefs.h> 46e026a48cSDavid E. O'Brien __FBSDID("$FreeBSD$"); 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> 63037dab57SPawel Jakub Dawidek #define _WANT_FILE 64037dab57SPawel Jakub Dawidek #include <sys/file.h> 65436f2e52SEd Schouten #include <sys/conf.h> 66c4473420SPeter Wemm #define _KERNEL 6727eabcedSMike Silbersack #include <sys/pipe.h> 68d8aadd88SBosko Milekic #include <sys/mount.h> 699b50d902SRodney W. Grimes #include <ufs/ufs/quota.h> 709b50d902SRodney W. Grimes #include <ufs/ufs/inode.h> 71fca066feSBrian Feldman #include <fs/devfs/devfs.h> 72997591caSChristian S.J. Peron #include <fs/devfs/devfs_int.h> 737a8671e9SAlfred Perlstein #undef _KERNEL 74a62dc406SDoug Rabson #include <nfs/nfsproto.h> 759b50d902SRodney W. Grimes #include <nfs/rpcv2.h> 7691196234SPeter Wemm #include <nfsclient/nfs.h> 7791196234SPeter Wemm #include <nfsclient/nfsnode.h> 789b50d902SRodney W. Grimes 7906e88916SBrian Feldman 80d0482be8SBrian Feldman #include <vm/vm.h> 81d0482be8SBrian Feldman #include <vm/vm_map.h> 82d0482be8SBrian Feldman #include <vm/vm_object.h> 83d0482be8SBrian Feldman 849b50d902SRodney W. Grimes #include <net/route.h> 859b50d902SRodney W. Grimes #include <netinet/in.h> 869b50d902SRodney W. Grimes #include <netinet/in_systm.h> 879b50d902SRodney W. Grimes #include <netinet/ip.h> 889b50d902SRodney W. Grimes #include <netinet/in_pcb.h> 899b50d902SRodney W. Grimes 909b50d902SRodney W. Grimes #include <ctype.h> 91c1e65942SPhilippe Charnier #include <err.h> 925d98ce75SBruce Evans #include <fcntl.h> 939b50d902SRodney W. Grimes #include <kvm.h> 94df3f5d9dSPeter Wemm #include <limits.h> 959b50d902SRodney W. Grimes #include <nlist.h> 969b50d902SRodney W. Grimes #include <paths.h> 979b50d902SRodney W. Grimes #include <pwd.h> 989b50d902SRodney W. Grimes #include <stdio.h> 999b50d902SRodney W. Grimes #include <stdlib.h> 10005427aafSKonstantin Belousov #include <stddef.h> 1019b50d902SRodney W. Grimes #include <string.h> 102df3f5d9dSPeter Wemm #include <unistd.h> 1030b7a57ddSRuslan Ermilov #include <netdb.h> 1049b50d902SRodney W. Grimes 10506e88916SBrian Feldman #include "fstat.h" 10606e88916SBrian Feldman 1079b50d902SRodney W. Grimes #define TEXT -1 1089b50d902SRodney W. Grimes #define CDIR -2 1099b50d902SRodney W. Grimes #define RDIR -3 1109b50d902SRodney W. Grimes #define TRACE -4 111d0482be8SBrian Feldman #define MMAP -5 1125dc1f79eSPoul-Henning Kamp #define JDIR -6 1139b50d902SRodney W. Grimes 1149b50d902SRodney W. Grimes DEVS *devs; 1159b50d902SRodney W. Grimes 1169b50d902SRodney W. Grimes #ifdef notdef 1179b50d902SRodney W. Grimes struct nlist nl[] = { 1189b50d902SRodney W. Grimes { "" }, 1199b50d902SRodney W. Grimes }; 1209b50d902SRodney W. Grimes #endif 1219b50d902SRodney W. Grimes 1229b50d902SRodney W. Grimes int fsflg, /* show files on same filesystem as file(s) argument */ 1239b50d902SRodney W. Grimes pflg, /* show files open by a particular pid */ 1249b50d902SRodney W. Grimes uflg; /* show files open by a particular (effective) user */ 1259b50d902SRodney W. Grimes int checkfile; /* true if restricting to particular files or filesystems */ 1269b50d902SRodney W. Grimes int nflg; /* (numerical) display f.s. and rdev as dev_t */ 1279b50d902SRodney W. Grimes int vflg; /* display errors in locating kernel data objects etc... */ 128d0482be8SBrian Feldman int mflg; /* include memory-mapped files */ 1299b50d902SRodney W. Grimes 1309b50d902SRodney W. Grimes 1319b50d902SRodney W. Grimes struct file **ofiles; /* buffer of pointers to file structures */ 1329b50d902SRodney W. Grimes int maxfiles; 1339b50d902SRodney W. Grimes #define ALLOC_OFILES(d) \ 1349b50d902SRodney W. Grimes if ((d) > maxfiles) { \ 1359b50d902SRodney W. Grimes free(ofiles); \ 1369b50d902SRodney W. Grimes ofiles = malloc((d) * sizeof(struct file *)); \ 1379b50d902SRodney W. Grimes if (ofiles == NULL) { \ 138c1e65942SPhilippe Charnier err(1, NULL); \ 1399b50d902SRodney W. Grimes } \ 1409b50d902SRodney W. Grimes maxfiles = (d); \ 1419b50d902SRodney W. Grimes } 1429b50d902SRodney W. Grimes 1431f910d6cSDag-Erling Smørgrav char *memf, *nlistf; 1449b50d902SRodney W. Grimes kvm_t *kd; 1459b50d902SRodney W. Grimes 1461f910d6cSDag-Erling Smørgrav static void fstat_kvm(int, int); 1471f910d6cSDag-Erling Smørgrav static void fstat_sysctl(int, int); 148f1bb2cd2SWarner Losh void dofiles(struct kinfo_proc *kp); 149f1bb2cd2SWarner Losh void dommap(struct kinfo_proc *kp); 150f1bb2cd2SWarner Losh void vtrans(struct vnode *vp, int i, int flag); 151f1bb2cd2SWarner Losh int ufs_filestat(struct vnode *vp, struct filestat *fsp); 152f1bb2cd2SWarner Losh int nfs_filestat(struct vnode *vp, struct filestat *fsp); 153f1bb2cd2SWarner Losh int devfs_filestat(struct vnode *vp, struct filestat *fsp); 154f1bb2cd2SWarner Losh char *getmnton(struct mount *m); 155f1bb2cd2SWarner Losh void pipetrans(struct pipe *pi, int i, int flag); 156f1bb2cd2SWarner Losh void socktrans(struct socket *sock, int i); 157f1bb2cd2SWarner Losh void getinetproto(int number); 15827d57ea9SDavid Malone int getfname(const char *filename); 159f1bb2cd2SWarner Losh void usage(void); 160a4cdc045SChristian S.J. Peron char *kdevtoname(struct cdev *dev); 1611c17fc99SPeter Wemm 1621c17fc99SPeter Wemm int 1631e925017SDavid Malone main(int argc, char **argv) 1649b50d902SRodney W. Grimes { 16527d57ea9SDavid Malone struct passwd *passwd; 1669b50d902SRodney W. Grimes int arg, ch, what; 1679b50d902SRodney W. Grimes 1689b50d902SRodney W. Grimes arg = 0; 169f9feee17SEd Maste what = KERN_PROC_PROC; 1709b50d902SRodney W. Grimes nlistf = memf = NULL; 171d0482be8SBrian Feldman while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1) 1729b50d902SRodney W. Grimes switch((char)ch) { 1739b50d902SRodney W. Grimes case 'f': 1749b50d902SRodney W. Grimes fsflg = 1; 1759b50d902SRodney W. Grimes break; 1769b50d902SRodney W. Grimes case 'M': 1779b50d902SRodney W. Grimes memf = optarg; 1789b50d902SRodney W. Grimes break; 1799b50d902SRodney W. Grimes case 'N': 1809b50d902SRodney W. Grimes nlistf = optarg; 1819b50d902SRodney W. Grimes break; 182d0482be8SBrian Feldman case 'm': 183d0482be8SBrian Feldman mflg = 1; 184d0482be8SBrian Feldman break; 1859b50d902SRodney W. Grimes case 'n': 1869b50d902SRodney W. Grimes nflg = 1; 1879b50d902SRodney W. Grimes break; 1889b50d902SRodney W. Grimes case 'p': 1899b50d902SRodney W. Grimes if (pflg++) 1909b50d902SRodney W. Grimes usage(); 1919b50d902SRodney W. Grimes if (!isdigit(*optarg)) { 192c1e65942SPhilippe Charnier warnx("-p requires a process id"); 1939b50d902SRodney W. Grimes usage(); 1949b50d902SRodney W. Grimes } 1959b50d902SRodney W. Grimes what = KERN_PROC_PID; 1969b50d902SRodney W. Grimes arg = atoi(optarg); 1979b50d902SRodney W. Grimes break; 1989b50d902SRodney W. Grimes case 'u': 1999b50d902SRodney W. Grimes if (uflg++) 2009b50d902SRodney W. Grimes usage(); 201c1e65942SPhilippe Charnier if (!(passwd = getpwnam(optarg))) 202c1e65942SPhilippe Charnier errx(1, "%s: unknown uid", optarg); 2039b50d902SRodney W. Grimes what = KERN_PROC_UID; 2049b50d902SRodney W. Grimes arg = passwd->pw_uid; 2059b50d902SRodney W. Grimes break; 2069b50d902SRodney W. Grimes case 'v': 2079b50d902SRodney W. Grimes vflg = 1; 2089b50d902SRodney W. Grimes break; 2099b50d902SRodney W. Grimes case '?': 2109b50d902SRodney W. Grimes default: 2119b50d902SRodney W. Grimes usage(); 2129b50d902SRodney W. Grimes } 2139b50d902SRodney W. Grimes 2149b50d902SRodney W. Grimes if (*(argv += optind)) { 2159b50d902SRodney W. Grimes for (; *argv; ++argv) { 2169b50d902SRodney W. Grimes if (getfname(*argv)) 2179b50d902SRodney W. Grimes checkfile = 1; 2189b50d902SRodney W. Grimes } 2199b50d902SRodney W. Grimes if (!checkfile) /* file(s) specified, but none accessable */ 2209b50d902SRodney W. Grimes exit(1); 2219b50d902SRodney W. Grimes } 2229b50d902SRodney W. Grimes 2239b50d902SRodney W. Grimes if (fsflg && !checkfile) { 2249b50d902SRodney W. Grimes /* -f with no files means use wd */ 2259b50d902SRodney W. Grimes if (getfname(".") == 0) 2269b50d902SRodney W. Grimes exit(1); 2279b50d902SRodney W. Grimes checkfile = 1; 2289b50d902SRodney W. Grimes } 2299b50d902SRodney W. Grimes 2301f910d6cSDag-Erling Smørgrav if (memf != NULL) 2311f910d6cSDag-Erling Smørgrav fstat_kvm(what, arg); 2321f910d6cSDag-Erling Smørgrav else 2331f910d6cSDag-Erling Smørgrav fstat_sysctl(what, arg); 2341f910d6cSDag-Erling Smørgrav exit(0); 2351f910d6cSDag-Erling Smørgrav } 2361f910d6cSDag-Erling Smørgrav 2371f910d6cSDag-Erling Smørgrav static void 2381f910d6cSDag-Erling Smørgrav print_header(void) 2391f910d6cSDag-Erling Smørgrav { 2401f910d6cSDag-Erling Smørgrav 2411f910d6cSDag-Erling Smørgrav if (nflg) 2421f910d6cSDag-Erling Smørgrav printf("%s", 2431f910d6cSDag-Erling Smørgrav "USER CMD PID FD DEV INUM MODE SZ|DV R/W"); 2441f910d6cSDag-Erling Smørgrav else 2451f910d6cSDag-Erling Smørgrav printf("%s", 2461f910d6cSDag-Erling Smørgrav "USER CMD PID FD MOUNT INUM MODE SZ|DV R/W"); 2471f910d6cSDag-Erling Smørgrav if (checkfile && fsflg == 0) 2481f910d6cSDag-Erling Smørgrav printf(" NAME\n"); 2491f910d6cSDag-Erling Smørgrav else 2501f910d6cSDag-Erling Smørgrav putchar('\n'); 2511f910d6cSDag-Erling Smørgrav } 2521f910d6cSDag-Erling Smørgrav 2531f910d6cSDag-Erling Smørgrav static void 2541f910d6cSDag-Erling Smørgrav fstat_kvm(int what, int arg) 2551f910d6cSDag-Erling Smørgrav { 2561f910d6cSDag-Erling Smørgrav struct kinfo_proc *p, *plast; 2571f910d6cSDag-Erling Smørgrav char buf[_POSIX2_LINE_MAX]; 2581f910d6cSDag-Erling Smørgrav int cnt; 2591f910d6cSDag-Erling Smørgrav 2601f910d6cSDag-Erling Smørgrav ALLOC_OFILES(256); /* reserve space for file pointers */ 2611f910d6cSDag-Erling Smørgrav 2629b50d902SRodney W. Grimes /* 2639b50d902SRodney W. Grimes * Discard setgid privileges if not the running kernel so that bad 2649b50d902SRodney W. Grimes * guys can't print interesting stuff from kernel memory. 2659b50d902SRodney W. Grimes */ 2669b50d902SRodney W. Grimes if (nlistf != NULL || memf != NULL) 2679b50d902SRodney W. Grimes setgid(getgid()); 2689b50d902SRodney W. Grimes 269c1e65942SPhilippe Charnier if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL) 270c1e65942SPhilippe Charnier errx(1, "%s", buf); 2713b7e5cccSRuslan Ermilov setgid(getgid()); 2729b50d902SRodney W. Grimes #ifdef notdef 273c1e65942SPhilippe Charnier if (kvm_nlist(kd, nl) != 0) 274c1e65942SPhilippe Charnier errx(1, "no namelist: %s", kvm_geterr(kd)); 2759b50d902SRodney W. Grimes #endif 276c1e65942SPhilippe Charnier if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) 277c1e65942SPhilippe Charnier errx(1, "%s", kvm_geterr(kd)); 278c30afbc5SMaxim Konovalov print_header(); 2799b50d902SRodney W. Grimes for (plast = &p[cnt]; p < plast; ++p) { 2801f7d2501SKirk McKusick if (p->ki_stat == SZOMB) 2819b50d902SRodney W. Grimes continue; 2829b50d902SRodney W. Grimes dofiles(p); 283d0482be8SBrian Feldman if (mflg) 284d0482be8SBrian Feldman dommap(p); 2859b50d902SRodney W. Grimes } 2861f910d6cSDag-Erling Smørgrav } 2871f910d6cSDag-Erling Smørgrav 2881f910d6cSDag-Erling Smørgrav static void 2891f910d6cSDag-Erling Smørgrav fstat_sysctl(int what, int arg) 2901f910d6cSDag-Erling Smørgrav { 2911f910d6cSDag-Erling Smørgrav 2921f910d6cSDag-Erling Smørgrav /* not yet implemented */ 2931f910d6cSDag-Erling Smørgrav fstat_kvm(what, arg); 2949b50d902SRodney W. Grimes } 2959b50d902SRodney W. Grimes 29627d57ea9SDavid Malone const char *Uname, *Comm; 2979b50d902SRodney W. Grimes int Pid; 2989b50d902SRodney W. Grimes 2999b50d902SRodney W. Grimes #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \ 3009b50d902SRodney W. Grimes switch(i) { \ 3019b50d902SRodney W. Grimes case TEXT: \ 3029b50d902SRodney W. Grimes printf(" text"); \ 3039b50d902SRodney W. Grimes break; \ 3049b50d902SRodney W. Grimes case CDIR: \ 3059b50d902SRodney W. Grimes printf(" wd"); \ 3069b50d902SRodney W. Grimes break; \ 3079b50d902SRodney W. Grimes case RDIR: \ 3089b50d902SRodney W. Grimes printf(" root"); \ 3099b50d902SRodney W. Grimes break; \ 3109b50d902SRodney W. Grimes case TRACE: \ 3119b50d902SRodney W. Grimes printf(" tr"); \ 3129b50d902SRodney W. Grimes break; \ 313d0482be8SBrian Feldman case MMAP: \ 314d0482be8SBrian Feldman printf(" mmap"); \ 315d0482be8SBrian Feldman break; \ 3165dc1f79eSPoul-Henning Kamp case JDIR: \ 3175dc1f79eSPoul-Henning Kamp printf(" jail"); \ 3185dc1f79eSPoul-Henning Kamp break; \ 3199b50d902SRodney W. Grimes default: \ 3209b50d902SRodney W. Grimes printf(" %4d", i); \ 3219b50d902SRodney W. Grimes break; \ 3229b50d902SRodney W. Grimes } 3239b50d902SRodney W. Grimes 3249b50d902SRodney W. Grimes /* 3259b50d902SRodney W. Grimes * print open files attributed to this process 3269b50d902SRodney W. Grimes */ 3279b50d902SRodney W. Grimes void 3281e925017SDavid Malone dofiles(struct kinfo_proc *kp) 3299b50d902SRodney W. Grimes { 330c1e65942SPhilippe Charnier int i; 3319b50d902SRodney W. Grimes struct file file; 332e93bd4efSPoul-Henning Kamp struct filedesc filed; 3339b50d902SRodney W. Grimes 3341f7d2501SKirk McKusick Uname = user_from_uid(kp->ki_uid, 0); 3351f7d2501SKirk McKusick Pid = kp->ki_pid; 3361f7d2501SKirk McKusick Comm = kp->ki_comm; 3379b50d902SRodney W. Grimes 3381f7d2501SKirk McKusick if (kp->ki_fd == NULL) 3399b50d902SRodney W. Grimes return; 340e93bd4efSPoul-Henning Kamp if (!KVM_READ(kp->ki_fd, &filed, sizeof (filed))) { 34122694ebaSBruce Evans dprintf(stderr, "can't read filedesc at %p for pid %d\n", 3421f7d2501SKirk McKusick (void *)kp->ki_fd, Pid); 3439b50d902SRodney W. Grimes return; 3449b50d902SRodney W. Grimes } 3459b50d902SRodney W. Grimes /* 3469b50d902SRodney W. Grimes * root directory vnode, if one 3479b50d902SRodney W. Grimes */ 3489b50d902SRodney W. Grimes if (filed.fd_rdir) 3499b50d902SRodney W. Grimes vtrans(filed.fd_rdir, RDIR, FREAD); 3509b50d902SRodney W. Grimes /* 3519b50d902SRodney W. Grimes * current working directory vnode 3529b50d902SRodney W. Grimes */ 35302e50214SJohn Baldwin if (filed.fd_cdir) 3549b50d902SRodney W. Grimes vtrans(filed.fd_cdir, CDIR, FREAD); 3559b50d902SRodney W. Grimes /* 3565dc1f79eSPoul-Henning Kamp * jail root, if any. 3575dc1f79eSPoul-Henning Kamp */ 3585dc1f79eSPoul-Henning Kamp if (filed.fd_jdir) 3595dc1f79eSPoul-Henning Kamp vtrans(filed.fd_jdir, JDIR, FREAD); 3605dc1f79eSPoul-Henning Kamp /* 3619b50d902SRodney W. Grimes * ktrace vnode, if one 3629b50d902SRodney W. Grimes */ 3631f7d2501SKirk McKusick if (kp->ki_tracep) 3641f7d2501SKirk McKusick vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE); 3659b50d902SRodney W. Grimes /* 366ca7e3117SPeter Wemm * text vnode, if one 367ca7e3117SPeter Wemm */ 3681f7d2501SKirk McKusick if (kp->ki_textvp) 3691f7d2501SKirk McKusick vtrans(kp->ki_textvp, TEXT, FREAD); 370ca7e3117SPeter Wemm /* 3719b50d902SRodney W. Grimes * open files 3729b50d902SRodney W. Grimes */ 3739b50d902SRodney W. Grimes #define FPSIZE (sizeof (struct file *)) 3745dc1f79eSPoul-Henning Kamp #define MAX_LASTFILE (0x1000000) 3755dc1f79eSPoul-Henning Kamp 3765dc1f79eSPoul-Henning Kamp /* Sanity check on filed.fd_lastfile */ 3775dc1f79eSPoul-Henning Kamp if (filed.fd_lastfile <= -1 || filed.fd_lastfile > MAX_LASTFILE) 3785dc1f79eSPoul-Henning Kamp return; 3795dc1f79eSPoul-Henning Kamp 3809b50d902SRodney W. Grimes ALLOC_OFILES(filed.fd_lastfile+1); 3819b50d902SRodney W. Grimes if (!KVM_READ(filed.fd_ofiles, ofiles, 3829b50d902SRodney W. Grimes (filed.fd_lastfile+1) * FPSIZE)) { 3839b50d902SRodney W. Grimes dprintf(stderr, 38422694ebaSBruce Evans "can't read file structures at %p for pid %d\n", 38522694ebaSBruce Evans (void *)filed.fd_ofiles, Pid); 3869b50d902SRodney W. Grimes return; 3879b50d902SRodney W. Grimes } 3889b50d902SRodney W. Grimes for (i = 0; i <= filed.fd_lastfile; i++) { 3899b50d902SRodney W. Grimes if (ofiles[i] == NULL) 3909b50d902SRodney W. Grimes continue; 3919b50d902SRodney W. Grimes if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) { 39222694ebaSBruce Evans dprintf(stderr, "can't read file %d at %p for pid %d\n", 39322694ebaSBruce Evans i, (void *)ofiles[i], Pid); 3949b50d902SRodney W. Grimes continue; 3959b50d902SRodney W. Grimes } 3969b50d902SRodney W. Grimes if (file.f_type == DTYPE_VNODE) 3977a13f01fSSuleiman Souhlal vtrans(file.f_vnode, i, file.f_flag); 3989b50d902SRodney W. Grimes else if (file.f_type == DTYPE_SOCKET) { 3999b50d902SRodney W. Grimes if (checkfile == 0) 40048e3128bSMatthew Dillon socktrans(file.f_data, i); 4019b50d902SRodney W. Grimes } 4021c17fc99SPeter Wemm #ifdef DTYPE_PIPE 4031c17fc99SPeter Wemm else if (file.f_type == DTYPE_PIPE) { 4041c17fc99SPeter Wemm if (checkfile == 0) 40548e3128bSMatthew Dillon pipetrans(file.f_data, i, file.f_flag); 4061c17fc99SPeter Wemm } 4071c17fc99SPeter Wemm #endif 408e6ebeb5fSPeter Pentchev #ifdef DTYPE_FIFO 409e6ebeb5fSPeter Pentchev else if (file.f_type == DTYPE_FIFO) { 410e6ebeb5fSPeter Pentchev if (checkfile == 0) 4117a13f01fSSuleiman Souhlal vtrans(file.f_vnode, i, file.f_flag); 412e6ebeb5fSPeter Pentchev } 413e6ebeb5fSPeter Pentchev #endif 4149b50d902SRodney W. Grimes else { 4159b50d902SRodney W. Grimes dprintf(stderr, 4169b50d902SRodney W. Grimes "unknown file type %d for file %d of pid %d\n", 4179b50d902SRodney W. Grimes file.f_type, i, Pid); 4189b50d902SRodney W. Grimes } 4199b50d902SRodney W. Grimes } 4209b50d902SRodney W. Grimes } 4219b50d902SRodney W. Grimes 4229b50d902SRodney W. Grimes void 4231e925017SDavid Malone dommap(struct kinfo_proc *kp) 424d0482be8SBrian Feldman { 425d0482be8SBrian Feldman vm_map_t map; 4261f7d2501SKirk McKusick struct vmspace vmspace; 427d0482be8SBrian Feldman struct vm_map_entry entry; 428d0482be8SBrian Feldman vm_map_entry_t entryp; 429d0482be8SBrian Feldman struct vm_object object; 430d0482be8SBrian Feldman vm_object_t objp; 431d0482be8SBrian Feldman int prot, fflags; 432d0482be8SBrian Feldman 4331f7d2501SKirk McKusick if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) { 4341f7d2501SKirk McKusick dprintf(stderr, 4351f7d2501SKirk McKusick "can't read vmspace at %p for pid %d\n", 4361f7d2501SKirk McKusick (void *)kp->ki_vmspace, Pid); 437d0482be8SBrian Feldman return; 438d0482be8SBrian Feldman } 439d0482be8SBrian Feldman map = &vmspace.vm_map; 440d0482be8SBrian Feldman 441b654e106SIan Dowse for (entryp = map->header.next; 442b654e106SIan Dowse entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) { 443d0482be8SBrian Feldman if (!KVM_READ(entryp, &entry, sizeof(entry))) { 444d0482be8SBrian Feldman dprintf(stderr, 445d0482be8SBrian Feldman "can't read vm_map_entry at %p for pid %d\n", 446d0482be8SBrian Feldman (void *)entryp, Pid); 447d0482be8SBrian Feldman return; 448d0482be8SBrian Feldman } 449d0482be8SBrian Feldman 450d0482be8SBrian Feldman if (entry.eflags & MAP_ENTRY_IS_SUB_MAP) 451d0482be8SBrian Feldman continue; 452d0482be8SBrian Feldman 453d0482be8SBrian Feldman if ((objp = entry.object.vm_object) == NULL) 454d0482be8SBrian Feldman continue; 455d0482be8SBrian Feldman 456d0482be8SBrian Feldman for (; objp; objp = object.backing_object) { 457d0482be8SBrian Feldman if (!KVM_READ(objp, &object, sizeof(object))) { 458d0482be8SBrian Feldman dprintf(stderr, 459d0482be8SBrian Feldman "can't read vm_object at %p for pid %d\n", 460d0482be8SBrian Feldman (void *)objp, Pid); 461d0482be8SBrian Feldman return; 462d0482be8SBrian Feldman } 463d0482be8SBrian Feldman } 464d0482be8SBrian Feldman 465d0482be8SBrian Feldman prot = entry.protection; 466d0482be8SBrian Feldman fflags = (prot & VM_PROT_READ ? FREAD : 0) | 467d0482be8SBrian Feldman (prot & VM_PROT_WRITE ? FWRITE : 0); 468d0482be8SBrian Feldman 469d0482be8SBrian Feldman switch (object.type) { 470d0482be8SBrian Feldman case OBJT_VNODE: 471d0482be8SBrian Feldman vtrans((struct vnode *)object.handle, MMAP, fflags); 472d0482be8SBrian Feldman break; 473d0482be8SBrian Feldman default: 474d0482be8SBrian Feldman break; 475d0482be8SBrian Feldman } 476d0482be8SBrian Feldman } 477d0482be8SBrian Feldman } 478d0482be8SBrian Feldman 479a4cdc045SChristian S.J. Peron char * 480a4cdc045SChristian S.J. Peron kdevtoname(struct cdev *dev) 481a4cdc045SChristian S.J. Peron { 482a4cdc045SChristian S.J. Peron struct cdev si; 483a4cdc045SChristian S.J. Peron 484a4cdc045SChristian S.J. Peron if (!KVM_READ(dev, &si, sizeof si)) 485a4cdc045SChristian S.J. Peron return (NULL); 486a4cdc045SChristian S.J. Peron return (strdup(si.__si_namebuf)); 487a4cdc045SChristian S.J. Peron } 488a4cdc045SChristian S.J. Peron 489d0482be8SBrian Feldman void 4901e925017SDavid Malone vtrans(struct vnode *vp, int i, int flag) 4919b50d902SRodney W. Grimes { 4929b50d902SRodney W. Grimes struct vnode vn; 4939b50d902SRodney W. Grimes struct filestat fst; 494e47c2178SNate Lawson char rw[3], mode[15], tagstr[12], *tagptr; 49527d57ea9SDavid Malone const char *badtype, *filename; 4969b50d902SRodney W. Grimes 4979b50d902SRodney W. Grimes filename = badtype = NULL; 4989b50d902SRodney W. Grimes if (!KVM_READ(vp, &vn, sizeof (struct vnode))) { 49922694ebaSBruce Evans dprintf(stderr, "can't read vnode at %p for pid %d\n", 50022694ebaSBruce Evans (void *)vp, Pid); 5019b50d902SRodney W. Grimes return; 5029b50d902SRodney W. Grimes } 503e47c2178SNate Lawson if (!KVM_READ(&vp->v_tag, &tagptr, sizeof tagptr) || 504e47c2178SNate Lawson !KVM_READ(tagptr, tagstr, sizeof tagstr)) { 505e47c2178SNate Lawson dprintf(stderr, "can't read v_tag at %p for pid %d\n", 506e47c2178SNate Lawson (void *)vp, Pid); 507e47c2178SNate Lawson return; 508e47c2178SNate Lawson } 509e47c2178SNate Lawson tagstr[sizeof(tagstr) - 1] = '\0'; 510e47c2178SNate Lawson if (vn.v_type == VNON) 5119b50d902SRodney W. Grimes badtype = "none"; 5129b50d902SRodney W. Grimes else if (vn.v_type == VBAD) 5139b50d902SRodney W. Grimes badtype = "bad"; 514e47c2178SNate Lawson else { 515e47c2178SNate Lawson if (!strcmp("ufs", tagstr)) { 5169b50d902SRodney W. Grimes if (!ufs_filestat(&vn, &fst)) 5179b50d902SRodney W. Grimes badtype = "error"; 518e47c2178SNate Lawson } else if (!strcmp("devfs", tagstr)) { 519fca066feSBrian Feldman if (!devfs_filestat(&vn, &fst)) 520fca066feSBrian Feldman badtype = "error"; 521e47c2178SNate Lawson } else if (!strcmp("nfs", tagstr)) { 5229b50d902SRodney W. Grimes if (!nfs_filestat(&vn, &fst)) 5239b50d902SRodney W. Grimes badtype = "error"; 524e47c2178SNate Lawson } else if (!strcmp("msdosfs", tagstr)) { 52506e88916SBrian Feldman if (!msdosfs_filestat(&vn, &fst)) 52606e88916SBrian Feldman badtype = "error"; 527e47c2178SNate Lawson } else if (!strcmp("isofs", tagstr)) { 52806e88916SBrian Feldman if (!isofs_filestat(&vn, &fst)) 52906e88916SBrian Feldman badtype = "error"; 530eec1c21fSUlf Lilleengen #ifdef ZFS 531eec1c21fSUlf Lilleengen } else if (!strcmp("zfs", tagstr)) { 532eec1c21fSUlf Lilleengen if (!zfs_filestat(&vn, &fst)) 533eec1c21fSUlf Lilleengen badtype = "error"; 534eec1c21fSUlf Lilleengen #endif 535e47c2178SNate Lawson } else { 536e47c2178SNate Lawson static char unknown[32]; 537e47c2178SNate Lawson snprintf(unknown, sizeof unknown, "?(%s)", tagstr); 53827d57ea9SDavid Malone badtype = unknown; 5399b50d902SRodney W. Grimes } 5409b50d902SRodney W. Grimes } 5419b50d902SRodney W. Grimes if (checkfile) { 5429b50d902SRodney W. Grimes int fsmatch = 0; 54327d57ea9SDavid Malone DEVS *d; 5449b50d902SRodney W. Grimes 5459b50d902SRodney W. Grimes if (badtype) 5469b50d902SRodney W. Grimes return; 5479b50d902SRodney W. Grimes for (d = devs; d != NULL; d = d->next) 5489b50d902SRodney W. Grimes if (d->fsid == fst.fsid) { 5499b50d902SRodney W. Grimes fsmatch = 1; 5509b50d902SRodney W. Grimes if (d->ino == fst.fileid) { 5519b50d902SRodney W. Grimes filename = d->name; 5529b50d902SRodney W. Grimes break; 5539b50d902SRodney W. Grimes } 5549b50d902SRodney W. Grimes } 5559b50d902SRodney W. Grimes if (fsmatch == 0 || (filename == NULL && fsflg == 0)) 5569b50d902SRodney W. Grimes return; 5579b50d902SRodney W. Grimes } 5589b50d902SRodney W. Grimes PREFIX(i); 5599b50d902SRodney W. Grimes if (badtype) { 5609b50d902SRodney W. Grimes (void)printf(" - - %10s -\n", badtype); 5619b50d902SRodney W. Grimes return; 5629b50d902SRodney W. Grimes } 5639b50d902SRodney W. Grimes if (nflg) 5649b50d902SRodney W. Grimes (void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid)); 5659b50d902SRodney W. Grimes else 5669b50d902SRodney W. Grimes (void)printf(" %-8s", getmnton(vn.v_mount)); 5679b50d902SRodney W. Grimes if (nflg) 5689b50d902SRodney W. Grimes (void)sprintf(mode, "%o", fst.mode); 5699b50d902SRodney W. Grimes else 5709b50d902SRodney W. Grimes strmode(fst.mode, mode); 57122694ebaSBruce Evans (void)printf(" %6ld %10s", fst.fileid, mode); 5729b50d902SRodney W. Grimes switch (vn.v_type) { 5739b50d902SRodney W. Grimes case VBLK: 5749b50d902SRodney W. Grimes case VCHR: { 5759b50d902SRodney W. Grimes char *name; 5769b50d902SRodney W. Grimes 577a4cdc045SChristian S.J. Peron name = kdevtoname(vn.v_rdev); 578a4cdc045SChristian S.J. Peron if (nflg || !name) 5799b50d902SRodney W. Grimes printf(" %2d,%-2d", major(fst.rdev), minor(fst.rdev)); 580a4cdc045SChristian S.J. Peron else { 5819b50d902SRodney W. Grimes printf(" %6s", name); 582a4cdc045SChristian S.J. Peron free(name); 583a4cdc045SChristian S.J. Peron } 5849b50d902SRodney W. Grimes break; 5859b50d902SRodney W. Grimes } 5869b50d902SRodney W. Grimes default: 587df8327e7SAlexander Langer printf(" %6lu", fst.size); 5889b50d902SRodney W. Grimes } 5899b50d902SRodney W. Grimes rw[0] = '\0'; 5909b50d902SRodney W. Grimes if (flag & FREAD) 5919b50d902SRodney W. Grimes strcat(rw, "r"); 5929b50d902SRodney W. Grimes if (flag & FWRITE) 5939b50d902SRodney W. Grimes strcat(rw, "w"); 5949b50d902SRodney W. Grimes printf(" %2s", rw); 5959b50d902SRodney W. Grimes if (filename && !fsflg) 5969b50d902SRodney W. Grimes printf(" %s", filename); 5979b50d902SRodney W. Grimes putchar('\n'); 5989b50d902SRodney W. Grimes } 5999b50d902SRodney W. Grimes 6009b50d902SRodney W. Grimes int 6011e925017SDavid Malone ufs_filestat(struct vnode *vp, struct filestat *fsp) 6029b50d902SRodney W. Grimes { 6039b50d902SRodney W. Grimes struct inode inode; 6049b50d902SRodney W. Grimes 6059b50d902SRodney W. Grimes if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) { 60622694ebaSBruce Evans dprintf(stderr, "can't read inode at %p for pid %d\n", 60722694ebaSBruce Evans (void *)VTOI(vp), Pid); 6089b50d902SRodney W. Grimes return 0; 6099b50d902SRodney W. Grimes } 61038cd1ee7SBrian Feldman /* 611f3732fd1SPoul-Henning Kamp * The st_dev from stat(2) is a dev_t. These kernel structures 612f3732fd1SPoul-Henning Kamp * contain cdev pointers. We need to convert to dev_t to make 61338cd1ee7SBrian Feldman * comparisons 61438cd1ee7SBrian Feldman */ 615b628b0dcSDavid Malone fsp->fsid = dev2udev(inode.i_dev); 6169b50d902SRodney W. Grimes fsp->fileid = (long)inode.i_number; 6179b50d902SRodney W. Grimes fsp->mode = (mode_t)inode.i_mode; 6189b50d902SRodney W. Grimes fsp->size = (u_long)inode.i_size; 6191c85e6a3SKirk McKusick #if should_be_but_is_hard 620ab1897e9SJohn-Mark Gurney /* XXX - need to load i_ump and i_din[12] from kernel memory */ 621ab1897e9SJohn-Mark Gurney if (inode.i_ump->um_fstype == UFS1) 622ab1897e9SJohn-Mark Gurney fsp->rdev = inode.i_din1->di_rdev; 623ab1897e9SJohn-Mark Gurney else 624ab1897e9SJohn-Mark Gurney fsp->rdev = inode.i_din2->di_rdev; 6251c85e6a3SKirk McKusick #else 6261c85e6a3SKirk McKusick fsp->rdev = 0; 6271c85e6a3SKirk McKusick #endif 6289b50d902SRodney W. Grimes 6299b50d902SRodney W. Grimes return 1; 6309b50d902SRodney W. Grimes } 6319b50d902SRodney W. Grimes 6329b50d902SRodney W. Grimes int 6331e925017SDavid Malone devfs_filestat(struct vnode *vp, struct filestat *fsp) 634fca066feSBrian Feldman { 635fca066feSBrian Feldman struct devfs_dirent devfs_dirent; 636fca066feSBrian Feldman struct mount mount; 637fca066feSBrian Feldman struct vnode vnode; 638fca066feSBrian Feldman 639fca066feSBrian Feldman if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) { 640fca066feSBrian Feldman dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n", 641fca066feSBrian Feldman (void *)vp->v_data, Pid); 642fca066feSBrian Feldman return 0; 643fca066feSBrian Feldman } 644fca066feSBrian Feldman if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) { 645fca066feSBrian Feldman dprintf(stderr, "can't read mount at %p for pid %d\n", 646fca066feSBrian Feldman (void *)vp->v_mount, Pid); 647fca066feSBrian Feldman return 0; 648fca066feSBrian Feldman } 649fca066feSBrian Feldman if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) { 650fca066feSBrian Feldman dprintf(stderr, "can't read vnode at %p for pid %d\n", 651fca066feSBrian Feldman (void *)devfs_dirent.de_vnode, Pid); 652fca066feSBrian Feldman return 0; 653fca066feSBrian Feldman } 654fca066feSBrian Feldman fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0]; 655fca066feSBrian Feldman fsp->fileid = devfs_dirent.de_inode; 656fca066feSBrian Feldman fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR; 657fca066feSBrian Feldman fsp->size = 0; 658cf56713aSJohn-Mark Gurney fsp->rdev = dev2udev(vnode.v_rdev); 659fca066feSBrian Feldman 660fca066feSBrian Feldman return 1; 661fca066feSBrian Feldman } 662fca066feSBrian Feldman 663fca066feSBrian Feldman int 6641e925017SDavid Malone nfs_filestat(struct vnode *vp, struct filestat *fsp) 6659b50d902SRodney W. Grimes { 6669b50d902SRodney W. Grimes struct nfsnode nfsnode; 66727d57ea9SDavid Malone mode_t mode; 6689b50d902SRodney W. Grimes 6699b50d902SRodney W. Grimes if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) { 67022694ebaSBruce Evans dprintf(stderr, "can't read nfsnode at %p for pid %d\n", 67122694ebaSBruce Evans (void *)VTONFS(vp), Pid); 6729b50d902SRodney W. Grimes return 0; 6739b50d902SRodney W. Grimes } 6749b50d902SRodney W. Grimes fsp->fsid = nfsnode.n_vattr.va_fsid; 6759b50d902SRodney W. Grimes fsp->fileid = nfsnode.n_vattr.va_fileid; 6769b50d902SRodney W. Grimes fsp->size = nfsnode.n_size; 6779b50d902SRodney W. Grimes fsp->rdev = nfsnode.n_vattr.va_rdev; 6789b50d902SRodney W. Grimes mode = (mode_t)nfsnode.n_vattr.va_mode; 6799b50d902SRodney W. Grimes switch (vp->v_type) { 6809b50d902SRodney W. Grimes case VREG: 6819b50d902SRodney W. Grimes mode |= S_IFREG; 6829b50d902SRodney W. Grimes break; 6839b50d902SRodney W. Grimes case VDIR: 6849b50d902SRodney W. Grimes mode |= S_IFDIR; 6859b50d902SRodney W. Grimes break; 6869b50d902SRodney W. Grimes case VBLK: 6879b50d902SRodney W. Grimes mode |= S_IFBLK; 6889b50d902SRodney W. Grimes break; 6899b50d902SRodney W. Grimes case VCHR: 6909b50d902SRodney W. Grimes mode |= S_IFCHR; 6919b50d902SRodney W. Grimes break; 6929b50d902SRodney W. Grimes case VLNK: 6939b50d902SRodney W. Grimes mode |= S_IFLNK; 6949b50d902SRodney W. Grimes break; 6959b50d902SRodney W. Grimes case VSOCK: 6969b50d902SRodney W. Grimes mode |= S_IFSOCK; 6979b50d902SRodney W. Grimes break; 6989b50d902SRodney W. Grimes case VFIFO: 6999b50d902SRodney W. Grimes mode |= S_IFIFO; 7009b50d902SRodney W. Grimes break; 7010b7a57ddSRuslan Ermilov case VNON: 7020b7a57ddSRuslan Ermilov case VBAD: 70382be0a5aSTor Egge case VMARKER: 7040b7a57ddSRuslan Ermilov return 0; 7059b50d902SRodney W. Grimes }; 7069b50d902SRodney W. Grimes fsp->mode = mode; 7079b50d902SRodney W. Grimes 7089b50d902SRodney W. Grimes return 1; 7099b50d902SRodney W. Grimes } 7109b50d902SRodney W. Grimes 7119b50d902SRodney W. Grimes 7129b50d902SRodney W. Grimes char * 7131e925017SDavid Malone getmnton(struct mount *m) 7149b50d902SRodney W. Grimes { 7159b50d902SRodney W. Grimes static struct mount mount; 7169b50d902SRodney W. Grimes static struct mtab { 7179b50d902SRodney W. Grimes struct mtab *next; 7189b50d902SRodney W. Grimes struct mount *m; 7199b50d902SRodney W. Grimes char mntonname[MNAMELEN]; 7209b50d902SRodney W. Grimes } *mhead = NULL; 72127d57ea9SDavid Malone struct mtab *mt; 7229b50d902SRodney W. Grimes 7239b50d902SRodney W. Grimes for (mt = mhead; mt != NULL; mt = mt->next) 7249b50d902SRodney W. Grimes if (m == mt->m) 7259b50d902SRodney W. Grimes return (mt->mntonname); 7269b50d902SRodney W. Grimes if (!KVM_READ(m, &mount, sizeof(struct mount))) { 72722694ebaSBruce Evans warnx("can't read mount table at %p", (void *)m); 7289b50d902SRodney W. Grimes return (NULL); 7299b50d902SRodney W. Grimes } 730c1e65942SPhilippe Charnier if ((mt = malloc(sizeof (struct mtab))) == NULL) 731c1e65942SPhilippe Charnier err(1, NULL); 7329b50d902SRodney W. Grimes mt->m = m; 7339b50d902SRodney W. Grimes bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN); 7349b50d902SRodney W. Grimes mt->next = mhead; 7359b50d902SRodney W. Grimes mhead = mt; 7369b50d902SRodney W. Grimes return (mt->mntonname); 7379b50d902SRodney W. Grimes } 7389b50d902SRodney W. Grimes 7399b50d902SRodney W. Grimes void 7401e925017SDavid Malone pipetrans(struct pipe *pi, int i, int flag) 7411c17fc99SPeter Wemm { 7421c17fc99SPeter Wemm struct pipe pip; 7431c17fc99SPeter Wemm char rw[3]; 7441c17fc99SPeter Wemm 7451c17fc99SPeter Wemm PREFIX(i); 7461c17fc99SPeter Wemm 7471c17fc99SPeter Wemm /* fill in socket */ 7481c17fc99SPeter Wemm if (!KVM_READ(pi, &pip, sizeof(struct pipe))) { 74922694ebaSBruce Evans dprintf(stderr, "can't read pipe at %p\n", (void *)pi); 7501c17fc99SPeter Wemm goto bad; 7511c17fc99SPeter Wemm } 7521c17fc99SPeter Wemm 753df94d4d2SMatt Jacob printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer); 7541c17fc99SPeter Wemm printf(" %6d", (int)pip.pipe_buffer.cnt); 7551c17fc99SPeter Wemm rw[0] = '\0'; 7561c17fc99SPeter Wemm if (flag & FREAD) 7571c17fc99SPeter Wemm strcat(rw, "r"); 7581c17fc99SPeter Wemm if (flag & FWRITE) 7591c17fc99SPeter Wemm strcat(rw, "w"); 7601c17fc99SPeter Wemm printf(" %2s", rw); 7611c17fc99SPeter Wemm putchar('\n'); 7621c17fc99SPeter Wemm return; 7631c17fc99SPeter Wemm 7641c17fc99SPeter Wemm bad: 7651c17fc99SPeter Wemm printf("* error\n"); 7661c17fc99SPeter Wemm } 7671c17fc99SPeter Wemm 7681c17fc99SPeter Wemm void 7691e925017SDavid Malone socktrans(struct socket *sock, int i) 7709b50d902SRodney W. Grimes { 77127d57ea9SDavid Malone static const char *stypename[] = { 7729b50d902SRodney W. Grimes "unused", /* 0 */ 7739b50d902SRodney W. Grimes "stream", /* 1 */ 7749b50d902SRodney W. Grimes "dgram", /* 2 */ 7759b50d902SRodney W. Grimes "raw", /* 3 */ 7769b50d902SRodney W. Grimes "rdm", /* 4 */ 7779b50d902SRodney W. Grimes "seqpak" /* 5 */ 7789b50d902SRodney W. Grimes }; 7799b50d902SRodney W. Grimes #define STYPEMAX 5 7809b50d902SRodney W. Grimes struct socket so; 7819b50d902SRodney W. Grimes struct protosw proto; 7829b50d902SRodney W. Grimes struct domain dom; 7839b50d902SRodney W. Grimes struct inpcb inpcb; 7849b50d902SRodney W. Grimes struct unpcb unpcb; 7859b50d902SRodney W. Grimes int len; 78627d57ea9SDavid Malone char dname[32]; 7879b50d902SRodney W. Grimes 7889b50d902SRodney W. Grimes PREFIX(i); 7899b50d902SRodney W. Grimes 7909b50d902SRodney W. Grimes /* fill in socket */ 7919b50d902SRodney W. Grimes if (!KVM_READ(sock, &so, sizeof(struct socket))) { 79222694ebaSBruce Evans dprintf(stderr, "can't read sock at %p\n", (void *)sock); 7939b50d902SRodney W. Grimes goto bad; 7949b50d902SRodney W. Grimes } 7959b50d902SRodney W. Grimes 7969b50d902SRodney W. Grimes /* fill in protosw entry */ 7979b50d902SRodney W. Grimes if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) { 79822694ebaSBruce Evans dprintf(stderr, "can't read protosw at %p", 79922694ebaSBruce Evans (void *)so.so_proto); 8009b50d902SRodney W. Grimes goto bad; 8019b50d902SRodney W. Grimes } 8029b50d902SRodney W. Grimes 8039b50d902SRodney W. Grimes /* fill in domain */ 8049b50d902SRodney W. Grimes if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) { 80522694ebaSBruce Evans dprintf(stderr, "can't read domain at %p\n", 80622694ebaSBruce Evans (void *)proto.pr_domain); 8079b50d902SRodney W. Grimes goto bad; 8089b50d902SRodney W. Grimes } 8099b50d902SRodney W. Grimes 8109b50d902SRodney W. Grimes if ((len = kvm_read(kd, (u_long)dom.dom_name, dname, 8119b50d902SRodney W. Grimes sizeof(dname) - 1)) < 0) { 81222694ebaSBruce Evans dprintf(stderr, "can't read domain name at %p\n", 81322694ebaSBruce Evans (void *)dom.dom_name); 8149b50d902SRodney W. Grimes dname[0] = '\0'; 8159b50d902SRodney W. Grimes } 8169b50d902SRodney W. Grimes else 8179b50d902SRodney W. Grimes dname[len] = '\0'; 8189b50d902SRodney W. Grimes 8199b50d902SRodney W. Grimes if ((u_short)so.so_type > STYPEMAX) 8209b50d902SRodney W. Grimes printf("* %s ?%d", dname, so.so_type); 8219b50d902SRodney W. Grimes else 8229b50d902SRodney W. Grimes printf("* %s %s", dname, stypename[so.so_type]); 8239b50d902SRodney W. Grimes 8249b50d902SRodney W. Grimes /* 8259b50d902SRodney W. Grimes * protocol specific formatting 8269b50d902SRodney W. Grimes * 8279b50d902SRodney W. Grimes * Try to find interesting things to print. For tcp, the interesting 8289b50d902SRodney W. Grimes * thing is the address of the tcpcb, for udp and others, just the 8299b50d902SRodney W. Grimes * inpcb (socket pcb). For unix domain, its the address of the socket 8309b50d902SRodney W. Grimes * pcb and the address of the connected pcb (if connected). Otherwise 8319b50d902SRodney W. Grimes * just print the protocol number and address of the socket itself. 8329b50d902SRodney W. Grimes * The idea is not to duplicate netstat, but to make available enough 8339b50d902SRodney W. Grimes * information for further analysis. 8349b50d902SRodney W. Grimes */ 8359b50d902SRodney W. Grimes switch(dom.dom_family) { 8369b50d902SRodney W. Grimes case AF_INET: 837677e00c0SYoshinobu Inoue case AF_INET6: 8389b50d902SRodney W. Grimes getinetproto(proto.pr_protocol); 8399b50d902SRodney W. Grimes if (proto.pr_protocol == IPPROTO_TCP ) { 8409b50d902SRodney W. Grimes if (so.so_pcb) { 8419b50d902SRodney W. Grimes if (kvm_read(kd, (u_long)so.so_pcb, 8429b50d902SRodney W. Grimes (char *)&inpcb, sizeof(struct inpcb)) 8439b50d902SRodney W. Grimes != sizeof(struct inpcb)) { 8449b50d902SRodney W. Grimes dprintf(stderr, 84522694ebaSBruce Evans "can't read inpcb at %p\n", 84622694ebaSBruce Evans (void *)so.so_pcb); 8479b50d902SRodney W. Grimes goto bad; 8489b50d902SRodney W. Grimes } 849df94d4d2SMatt Jacob printf(" %lx", (u_long)inpcb.inp_ppcb); 8509b50d902SRodney W. Grimes } 8519b50d902SRodney W. Grimes } 8529b50d902SRodney W. Grimes else if (so.so_pcb) 853df94d4d2SMatt Jacob printf(" %lx", (u_long)so.so_pcb); 8549b50d902SRodney W. Grimes break; 8559b50d902SRodney W. Grimes case AF_UNIX: 8569b50d902SRodney W. Grimes /* print address of pcb and connected pcb */ 8579b50d902SRodney W. Grimes if (so.so_pcb) { 858df94d4d2SMatt Jacob printf(" %lx", (u_long)so.so_pcb); 8599b50d902SRodney W. Grimes if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb, 8609b50d902SRodney W. Grimes sizeof(struct unpcb)) != sizeof(struct unpcb)){ 86122694ebaSBruce Evans dprintf(stderr, "can't read unpcb at %p\n", 86222694ebaSBruce Evans (void *)so.so_pcb); 8639b50d902SRodney W. Grimes goto bad; 8649b50d902SRodney W. Grimes } 8659b50d902SRodney W. Grimes if (unpcb.unp_conn) { 8669b50d902SRodney W. Grimes char shoconn[4], *cp; 8679b50d902SRodney W. Grimes 8689b50d902SRodney W. Grimes cp = shoconn; 86997b88fbfSRobert Watson if (!(so.so_rcv.sb_state & SBS_CANTRCVMORE)) 8709b50d902SRodney W. Grimes *cp++ = '<'; 8719b50d902SRodney W. Grimes *cp++ = '-'; 87297b88fbfSRobert Watson if (!(so.so_snd.sb_state & SBS_CANTSENDMORE)) 8739b50d902SRodney W. Grimes *cp++ = '>'; 8749b50d902SRodney W. Grimes *cp = '\0'; 875df94d4d2SMatt Jacob printf(" %s %lx", shoconn, 876df94d4d2SMatt Jacob (u_long)unpcb.unp_conn); 8779b50d902SRodney W. Grimes } 8789b50d902SRodney W. Grimes } 8799b50d902SRodney W. Grimes break; 8809b50d902SRodney W. Grimes default: 8819b50d902SRodney W. Grimes /* print protocol number and socket address */ 882df94d4d2SMatt Jacob printf(" %d %lx", proto.pr_protocol, (u_long)sock); 8839b50d902SRodney W. Grimes } 8849b50d902SRodney W. Grimes printf("\n"); 8859b50d902SRodney W. Grimes return; 8869b50d902SRodney W. Grimes bad: 8879b50d902SRodney W. Grimes printf("* error\n"); 8889b50d902SRodney W. Grimes } 8899b50d902SRodney W. Grimes 89038cd1ee7SBrian Feldman 89138cd1ee7SBrian Feldman /* 892f3732fd1SPoul-Henning Kamp * Read the cdev structure in the kernel in order to work out the 893f3732fd1SPoul-Henning Kamp * associated dev_t 89438cd1ee7SBrian Feldman */ 895f3732fd1SPoul-Henning Kamp dev_t 8961e925017SDavid Malone dev2udev(struct cdev *dev) 89738cd1ee7SBrian Feldman { 898997591caSChristian S.J. Peron struct cdev_priv priv; 89938cd1ee7SBrian Feldman 90005427aafSKonstantin Belousov if (KVM_READ(cdev2priv(dev), &priv, sizeof priv)) { 901997591caSChristian S.J. Peron return ((dev_t)priv.cdp_inode); 90238cd1ee7SBrian Feldman } else { 9031e925017SDavid Malone dprintf(stderr, "can't convert cdev *%p to a dev_t\n", dev); 90438cd1ee7SBrian Feldman return -1; 90538cd1ee7SBrian Feldman } 90638cd1ee7SBrian Feldman } 90738cd1ee7SBrian Feldman 9089b50d902SRodney W. Grimes /* 9099b50d902SRodney W. Grimes * getinetproto -- 9109b50d902SRodney W. Grimes * print name of protocol number 9119b50d902SRodney W. Grimes */ 9129b50d902SRodney W. Grimes void 9131e925017SDavid Malone getinetproto(int number) 9149b50d902SRodney W. Grimes { 9150b7a57ddSRuslan Ermilov static int isopen; 91627d57ea9SDavid Malone struct protoent *pe; 9179b50d902SRodney W. Grimes 9180b7a57ddSRuslan Ermilov if (!isopen) 9190b7a57ddSRuslan Ermilov setprotoent(++isopen); 9200b7a57ddSRuslan Ermilov if ((pe = getprotobynumber(number)) != NULL) 9210b7a57ddSRuslan Ermilov printf(" %s", pe->p_name); 9220b7a57ddSRuslan Ermilov else 9239b50d902SRodney W. Grimes printf(" %d", number); 9249b50d902SRodney W. Grimes } 9259b50d902SRodney W. Grimes 9261c17fc99SPeter Wemm int 9271e925017SDavid Malone getfname(const char *filename) 9289b50d902SRodney W. Grimes { 9299b50d902SRodney W. Grimes struct stat statbuf; 9309b50d902SRodney W. Grimes DEVS *cur; 9319b50d902SRodney W. Grimes 9329b50d902SRodney W. Grimes if (stat(filename, &statbuf)) { 933c1e65942SPhilippe Charnier warn("%s", filename); 9349b50d902SRodney W. Grimes return(0); 9359b50d902SRodney W. Grimes } 936c1e65942SPhilippe Charnier if ((cur = malloc(sizeof(DEVS))) == NULL) 937c1e65942SPhilippe Charnier err(1, NULL); 9389b50d902SRodney W. Grimes cur->next = devs; 9399b50d902SRodney W. Grimes devs = cur; 9409b50d902SRodney W. Grimes 9419b50d902SRodney W. Grimes cur->ino = statbuf.st_ino; 942b628b0dcSDavid Malone cur->fsid = statbuf.st_dev; 9439b50d902SRodney W. Grimes cur->name = filename; 9449b50d902SRodney W. Grimes return(1); 9459b50d902SRodney W. Grimes } 9469b50d902SRodney W. Grimes 947eec1c21fSUlf Lilleengen #ifdef ZFS 948eec1c21fSUlf Lilleengen void * 949eec1c21fSUlf Lilleengen getvnodedata(struct vnode *vp) 950eec1c21fSUlf Lilleengen { 951eec1c21fSUlf Lilleengen return (vp->v_data); 952eec1c21fSUlf Lilleengen } 953eec1c21fSUlf Lilleengen 954eec1c21fSUlf Lilleengen struct mount * 955eec1c21fSUlf Lilleengen getvnodemount(struct vnode *vp) 956eec1c21fSUlf Lilleengen { 957eec1c21fSUlf Lilleengen return (vp->v_mount); 958eec1c21fSUlf Lilleengen } 959eec1c21fSUlf Lilleengen #endif 960eec1c21fSUlf Lilleengen 9619b50d902SRodney W. Grimes void 9621e925017SDavid Malone usage(void) 9639b50d902SRodney W. Grimes { 9649b50d902SRodney W. Grimes (void)fprintf(stderr, 965f682f10cSRuslan Ermilov "usage: fstat [-fmnv] [-M core] [-N system] [-p pid] [-u user] [file ...]\n"); 9669b50d902SRodney W. Grimes exit(1); 9679b50d902SRodney W. Grimes } 968