xref: /freebsd/usr.bin/fstat/fstat.c (revision df94d4d2802fad3473dc91636c437af570460c49)
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>
496e547eabSMark Murray #include <sys/lock.h>
509b50d902SRodney W. Grimes #include <sys/time.h>
519b50d902SRodney W. Grimes #include <sys/proc.h>
529b50d902SRodney W. Grimes #include <sys/user.h>
539b50d902SRodney W. Grimes #include <sys/stat.h>
549b50d902SRodney W. Grimes #include <sys/vnode.h>
559b50d902SRodney W. Grimes #include <sys/socket.h>
569b50d902SRodney W. Grimes #include <sys/socketvar.h>
579b50d902SRodney W. Grimes #include <sys/domain.h>
589b50d902SRodney W. Grimes #include <sys/protosw.h>
593492514dSMark Murray #include <sys/un.h>
609b50d902SRodney W. Grimes #include <sys/unpcb.h>
619b50d902SRodney W. Grimes #include <sys/sysctl.h>
629b50d902SRodney W. Grimes #include <sys/filedesc.h>
63d2bccb9fSDavid Greenman #include <sys/queue.h>
641c17fc99SPeter Wemm #include <sys/pipe.h>
65c4473420SPeter Wemm #define	_KERNEL
6611529d1aSPoul-Henning Kamp #include <sys/conf.h>
679b50d902SRodney W. Grimes #include <sys/file.h>
689b50d902SRodney W. Grimes #include <ufs/ufs/quota.h>
699b50d902SRodney W. Grimes #include <ufs/ufs/inode.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>
749b50d902SRodney W. Grimes #include <nfs/nfs.h>
759b50d902SRodney W. Grimes #include <nfs/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 
1399b50d902SRodney W. Grimes kvm_t *kd;
1409b50d902SRodney W. Grimes 
1411c17fc99SPeter Wemm void dofiles __P((struct kinfo_proc *kp));
142d0482be8SBrian Feldman void dommap __P((struct kinfo_proc *kp));
1431c17fc99SPeter Wemm void vtrans __P((struct vnode *vp, int i, int flag));
1441c17fc99SPeter Wemm int  ufs_filestat __P((struct vnode *vp, struct filestat *fsp));
1451c17fc99SPeter Wemm int  nfs_filestat __P((struct vnode *vp, struct filestat *fsp));
1461c17fc99SPeter Wemm char *getmnton __P((struct mount *m));
1471c17fc99SPeter Wemm void pipetrans __P((struct pipe *pi, int i, int flag));
1481c17fc99SPeter Wemm void socktrans __P((struct socket *sock, int i));
1491c17fc99SPeter Wemm void getinetproto __P((int number));
1501c17fc99SPeter Wemm int  getfname __P((char *filename));
1511c17fc99SPeter Wemm void usage __P((void));
1529b50d902SRodney W. Grimes 
1531c17fc99SPeter Wemm 
1541c17fc99SPeter Wemm int
1559b50d902SRodney W. Grimes main(argc, argv)
1569b50d902SRodney W. Grimes 	int argc;
1579b50d902SRodney W. Grimes 	char **argv;
1589b50d902SRodney W. Grimes {
1599b50d902SRodney W. Grimes 	register struct passwd *passwd;
1609b50d902SRodney W. Grimes 	struct kinfo_proc *p, *plast;
1619b50d902SRodney W. Grimes 	int arg, ch, what;
1629b50d902SRodney W. Grimes 	char *memf, *nlistf;
163df3f5d9dSPeter Wemm 	char buf[_POSIX2_LINE_MAX];
1649b50d902SRodney W. Grimes 	int cnt;
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 	ALLOC_OFILES(256);	/* reserve space for file pointers */
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 
2309b50d902SRodney W. Grimes 	/*
2319b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
2329b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
2339b50d902SRodney W. Grimes 	 */
2349b50d902SRodney W. Grimes 	if (nlistf != NULL || memf != NULL)
2359b50d902SRodney W. Grimes 		setgid(getgid());
2369b50d902SRodney W. Grimes 
237c1e65942SPhilippe Charnier 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
238c1e65942SPhilippe Charnier 		errx(1, "%s", buf);
2399b50d902SRodney W. Grimes #ifdef notdef
240c1e65942SPhilippe Charnier 	if (kvm_nlist(kd, nl) != 0)
241c1e65942SPhilippe Charnier 		errx(1, "no namelist: %s", kvm_geterr(kd));
2429b50d902SRodney W. Grimes #endif
243c1e65942SPhilippe Charnier 	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
244c1e65942SPhilippe Charnier 		errx(1, "%s", kvm_geterr(kd));
2459b50d902SRodney W. Grimes 	if (nflg)
2469b50d902SRodney W. Grimes 		printf("%s",
2479b50d902SRodney W. Grimes "USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
2489b50d902SRodney W. Grimes 	else
2499b50d902SRodney W. Grimes 		printf("%s",
2509b50d902SRodney W. Grimes "USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
2519b50d902SRodney W. Grimes 	if (checkfile && fsflg == 0)
2529b50d902SRodney W. Grimes 		printf(" NAME\n");
2539b50d902SRodney W. Grimes 	else
2549b50d902SRodney W. Grimes 		putchar('\n');
2559b50d902SRodney W. Grimes 
2569b50d902SRodney W. Grimes 	for (plast = &p[cnt]; p < plast; ++p) {
2571f7d2501SKirk McKusick 		if (p->ki_stat == SZOMB)
2589b50d902SRodney W. Grimes 			continue;
2599b50d902SRodney W. Grimes 		dofiles(p);
260d0482be8SBrian Feldman 		if (mflg)
261d0482be8SBrian Feldman 			dommap(p);
2629b50d902SRodney W. Grimes 	}
2639b50d902SRodney W. Grimes 	exit(0);
2649b50d902SRodney W. Grimes }
2659b50d902SRodney W. Grimes 
2669b50d902SRodney W. Grimes char	*Uname, *Comm;
2679b50d902SRodney W. Grimes int	Pid;
2689b50d902SRodney W. Grimes 
2699b50d902SRodney W. Grimes #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
2709b50d902SRodney W. Grimes 	switch(i) { \
2719b50d902SRodney W. Grimes 	case TEXT: \
2729b50d902SRodney W. Grimes 		printf(" text"); \
2739b50d902SRodney W. Grimes 		break; \
2749b50d902SRodney W. Grimes 	case CDIR: \
2759b50d902SRodney W. Grimes 		printf("   wd"); \
2769b50d902SRodney W. Grimes 		break; \
2779b50d902SRodney W. Grimes 	case RDIR: \
2789b50d902SRodney W. Grimes 		printf(" root"); \
2799b50d902SRodney W. Grimes 		break; \
2809b50d902SRodney W. Grimes 	case TRACE: \
2819b50d902SRodney W. Grimes 		printf("   tr"); \
2829b50d902SRodney W. Grimes 		break; \
283d0482be8SBrian Feldman 	case MMAP: \
284d0482be8SBrian Feldman 		printf(" mmap"); \
285d0482be8SBrian Feldman 		break; \
2869b50d902SRodney W. Grimes 	default: \
2879b50d902SRodney W. Grimes 		printf(" %4d", i); \
2889b50d902SRodney W. Grimes 		break; \
2899b50d902SRodney W. Grimes 	}
2909b50d902SRodney W. Grimes 
2919b50d902SRodney W. Grimes /*
2929b50d902SRodney W. Grimes  * print open files attributed to this process
2939b50d902SRodney W. Grimes  */
2949b50d902SRodney W. Grimes void
2959b50d902SRodney W. Grimes dofiles(kp)
2969b50d902SRodney W. Grimes 	struct kinfo_proc *kp;
2979b50d902SRodney W. Grimes {
298c1e65942SPhilippe Charnier 	int i;
2999b50d902SRodney W. Grimes 	struct file file;
3009b50d902SRodney W. Grimes 	struct filedesc0 filed0;
3019b50d902SRodney W. Grimes #define	filed	filed0.fd_fd
3029b50d902SRodney W. Grimes 
3031f7d2501SKirk McKusick 	Uname = user_from_uid(kp->ki_uid, 0);
3041f7d2501SKirk McKusick 	Pid = kp->ki_pid;
3051f7d2501SKirk McKusick 	Comm = kp->ki_comm;
3069b50d902SRodney W. Grimes 
3071f7d2501SKirk McKusick 	if (kp->ki_fd == NULL)
3089b50d902SRodney W. Grimes 		return;
3091f7d2501SKirk McKusick 	if (!KVM_READ(kp->ki_fd, &filed0, sizeof (filed0))) {
31022694ebaSBruce Evans 		dprintf(stderr, "can't read filedesc at %p for pid %d\n",
3111f7d2501SKirk McKusick 		    (void *)kp->ki_fd, Pid);
3129b50d902SRodney W. Grimes 		return;
3139b50d902SRodney W. Grimes 	}
3149b50d902SRodney W. Grimes 	/*
3159b50d902SRodney W. Grimes 	 * root directory vnode, if one
3169b50d902SRodney W. Grimes 	 */
3179b50d902SRodney W. Grimes 	if (filed.fd_rdir)
3189b50d902SRodney W. Grimes 		vtrans(filed.fd_rdir, RDIR, FREAD);
3199b50d902SRodney W. Grimes 	/*
3209b50d902SRodney W. Grimes 	 * current working directory vnode
3219b50d902SRodney W. Grimes 	 */
3229b50d902SRodney W. Grimes 	vtrans(filed.fd_cdir, CDIR, FREAD);
3239b50d902SRodney W. Grimes 	/*
3249b50d902SRodney W. Grimes 	 * ktrace vnode, if one
3259b50d902SRodney W. Grimes 	 */
3261f7d2501SKirk McKusick 	if (kp->ki_tracep)
3271f7d2501SKirk McKusick 		vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE);
3289b50d902SRodney W. Grimes 	/*
329ca7e3117SPeter Wemm 	 * text vnode, if one
330ca7e3117SPeter Wemm 	 */
3311f7d2501SKirk McKusick 	if (kp->ki_textvp)
3321f7d2501SKirk McKusick 		vtrans(kp->ki_textvp, TEXT, FREAD);
333ca7e3117SPeter Wemm 	/*
3349b50d902SRodney W. Grimes 	 * open files
3359b50d902SRodney W. Grimes 	 */
3369b50d902SRodney W. Grimes #define FPSIZE	(sizeof (struct file *))
3379b50d902SRodney W. Grimes 	ALLOC_OFILES(filed.fd_lastfile+1);
3389b50d902SRodney W. Grimes 	if (filed.fd_nfiles > NDFILE) {
3399b50d902SRodney W. Grimes 		if (!KVM_READ(filed.fd_ofiles, ofiles,
3409b50d902SRodney W. Grimes 		    (filed.fd_lastfile+1) * FPSIZE)) {
3419b50d902SRodney W. Grimes 			dprintf(stderr,
34222694ebaSBruce Evans 			    "can't read file structures at %p for pid %d\n",
34322694ebaSBruce Evans 			    (void *)filed.fd_ofiles, Pid);
3449b50d902SRodney W. Grimes 			return;
3459b50d902SRodney W. Grimes 		}
3469b50d902SRodney W. Grimes 	} else
3479b50d902SRodney W. Grimes 		bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
3489b50d902SRodney W. Grimes 	for (i = 0; i <= filed.fd_lastfile; i++) {
3499b50d902SRodney W. Grimes 		if (ofiles[i] == NULL)
3509b50d902SRodney W. Grimes 			continue;
3519b50d902SRodney W. Grimes 		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
35222694ebaSBruce Evans 			dprintf(stderr, "can't read file %d at %p for pid %d\n",
35322694ebaSBruce Evans 			    i, (void *)ofiles[i], Pid);
3549b50d902SRodney W. Grimes 			continue;
3559b50d902SRodney W. Grimes 		}
3569b50d902SRodney W. Grimes 		if (file.f_type == DTYPE_VNODE)
3579b50d902SRodney W. Grimes 			vtrans((struct vnode *)file.f_data, i, file.f_flag);
3589b50d902SRodney W. Grimes 		else if (file.f_type == DTYPE_SOCKET) {
3599b50d902SRodney W. Grimes 			if (checkfile == 0)
3609b50d902SRodney W. Grimes 				socktrans((struct socket *)file.f_data, i);
3619b50d902SRodney W. Grimes 		}
3621c17fc99SPeter Wemm #ifdef DTYPE_PIPE
3631c17fc99SPeter Wemm 		else if (file.f_type == DTYPE_PIPE) {
3641c17fc99SPeter Wemm 			if (checkfile == 0)
3651c17fc99SPeter Wemm 				pipetrans((struct pipe *)file.f_data, i,
3661c17fc99SPeter Wemm 				    file.f_flag);
3671c17fc99SPeter Wemm 		}
3681c17fc99SPeter Wemm #endif
369e6ebeb5fSPeter Pentchev #ifdef DTYPE_FIFO
370e6ebeb5fSPeter Pentchev 		else if (file.f_type == DTYPE_FIFO) {
371e6ebeb5fSPeter Pentchev 			if (checkfile == 0)
372e6ebeb5fSPeter Pentchev 				vtrans((struct vnode *)file.f_data, i,
373e6ebeb5fSPeter Pentchev 				    file.f_flag);
374e6ebeb5fSPeter Pentchev 		}
375e6ebeb5fSPeter Pentchev #endif
3769b50d902SRodney W. Grimes 		else {
3779b50d902SRodney W. Grimes 			dprintf(stderr,
3789b50d902SRodney W. Grimes 			    "unknown file type %d for file %d of pid %d\n",
3799b50d902SRodney W. Grimes 			    file.f_type, i, Pid);
3809b50d902SRodney W. Grimes 		}
3819b50d902SRodney W. Grimes 	}
3829b50d902SRodney W. Grimes }
3839b50d902SRodney W. Grimes 
3849b50d902SRodney W. Grimes void
385d0482be8SBrian Feldman dommap(kp)
386d0482be8SBrian Feldman 	struct kinfo_proc *kp;
387d0482be8SBrian Feldman {
388d0482be8SBrian Feldman 	vm_map_t map;
3891f7d2501SKirk McKusick 	struct vmspace vmspace;
390d0482be8SBrian Feldman 	struct vm_map_entry entry;
391d0482be8SBrian Feldman 	vm_map_entry_t entryp;
392d0482be8SBrian Feldman 	struct vm_object object;
393d0482be8SBrian Feldman 	vm_object_t objp;
394d0482be8SBrian Feldman 	int prot, fflags;
395d0482be8SBrian Feldman 
3961f7d2501SKirk McKusick 	if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) {
3971f7d2501SKirk McKusick 		dprintf(stderr,
3981f7d2501SKirk McKusick 		    "can't read vmspace at %p for pid %d\n",
3991f7d2501SKirk McKusick 		    (void *)kp->ki_vmspace, Pid);
400d0482be8SBrian Feldman 		return;
401d0482be8SBrian Feldman 	}
402d0482be8SBrian Feldman 	map = &vmspace.vm_map;
403d0482be8SBrian Feldman 
404b654e106SIan Dowse 	for (entryp = map->header.next;
405b654e106SIan Dowse 	    entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) {
406d0482be8SBrian Feldman 		if (!KVM_READ(entryp, &entry, sizeof(entry))) {
407d0482be8SBrian Feldman 			dprintf(stderr,
408d0482be8SBrian Feldman 			    "can't read vm_map_entry at %p for pid %d\n",
409d0482be8SBrian Feldman 			    (void *)entryp, Pid);
410d0482be8SBrian Feldman 			return;
411d0482be8SBrian Feldman 		}
412d0482be8SBrian Feldman 
413d0482be8SBrian Feldman 		if (entry.eflags & MAP_ENTRY_IS_SUB_MAP)
414d0482be8SBrian Feldman 			continue;
415d0482be8SBrian Feldman 
416d0482be8SBrian Feldman 		if ((objp = entry.object.vm_object) == NULL)
417d0482be8SBrian Feldman 			continue;
418d0482be8SBrian Feldman 
419d0482be8SBrian Feldman 		for (; objp; objp = object.backing_object) {
420d0482be8SBrian Feldman 			if (!KVM_READ(objp, &object, sizeof(object))) {
421d0482be8SBrian Feldman 				dprintf(stderr,
422d0482be8SBrian Feldman 				    "can't read vm_object at %p for pid %d\n",
423d0482be8SBrian Feldman 				    (void *)objp, Pid);
424d0482be8SBrian Feldman 				return;
425d0482be8SBrian Feldman 			}
426d0482be8SBrian Feldman 		}
427d0482be8SBrian Feldman 
428d0482be8SBrian Feldman 		prot = entry.protection;
429d0482be8SBrian Feldman 		fflags = (prot & VM_PROT_READ ? FREAD : 0) |
430d0482be8SBrian Feldman 		    (prot & VM_PROT_WRITE ? FWRITE : 0);
431d0482be8SBrian Feldman 
432d0482be8SBrian Feldman 		switch (object.type) {
433d0482be8SBrian Feldman 		case OBJT_VNODE:
434d0482be8SBrian Feldman 			vtrans((struct vnode *)object.handle, MMAP, fflags);
435d0482be8SBrian Feldman 			break;
436d0482be8SBrian Feldman 		default:
437d0482be8SBrian Feldman 			break;
438d0482be8SBrian Feldman 		}
439d0482be8SBrian Feldman 	}
440d0482be8SBrian Feldman }
441d0482be8SBrian Feldman 
442d0482be8SBrian Feldman void
4439b50d902SRodney W. Grimes vtrans(vp, i, flag)
4449b50d902SRodney W. Grimes 	struct vnode *vp;
4459b50d902SRodney W. Grimes 	int i;
4469b50d902SRodney W. Grimes 	int flag;
4479b50d902SRodney W. Grimes {
4489b50d902SRodney W. Grimes 	struct vnode vn;
4499b50d902SRodney W. Grimes 	struct filestat fst;
4509b50d902SRodney W. Grimes 	char rw[3], mode[15];
4519b50d902SRodney W. Grimes 	char *badtype = NULL, *filename, *getmnton();
4529b50d902SRodney W. Grimes 
4539b50d902SRodney W. Grimes 	filename = badtype = NULL;
4549b50d902SRodney W. Grimes 	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
45522694ebaSBruce Evans 		dprintf(stderr, "can't read vnode at %p for pid %d\n",
45622694ebaSBruce Evans 		    (void *)vp, Pid);
4579b50d902SRodney W. Grimes 		return;
4589b50d902SRodney W. Grimes 	}
4599b50d902SRodney W. Grimes 	if (vn.v_type == VNON || vn.v_tag == VT_NON)
4609b50d902SRodney W. Grimes 		badtype = "none";
4619b50d902SRodney W. Grimes 	else if (vn.v_type == VBAD)
4629b50d902SRodney W. Grimes 		badtype = "bad";
4639b50d902SRodney W. Grimes 	else
4649b50d902SRodney W. Grimes 		switch (vn.v_tag) {
4659b50d902SRodney W. Grimes 		case VT_UFS:
4669b50d902SRodney W. Grimes 			if (!ufs_filestat(&vn, &fst))
4679b50d902SRodney W. Grimes 				badtype = "error";
4689b50d902SRodney W. Grimes 			break;
4699b50d902SRodney W. Grimes 		case VT_NFS:
4709b50d902SRodney W. Grimes 			if (!nfs_filestat(&vn, &fst))
4719b50d902SRodney W. Grimes 				badtype = "error";
4729b50d902SRodney W. Grimes 			break;
47306e88916SBrian Feldman 
47406e88916SBrian Feldman 		case VT_MSDOSFS:
47506e88916SBrian Feldman 			if (!msdosfs_filestat(&vn, &fst))
47606e88916SBrian Feldman 				badtype = "error";
47706e88916SBrian Feldman 			break;
47806e88916SBrian Feldman 
47906e88916SBrian Feldman 		case VT_ISOFS:
48006e88916SBrian Feldman 			if (!isofs_filestat(&vn, &fst))
48106e88916SBrian Feldman 				badtype = "error";
48206e88916SBrian Feldman 			break;
48306e88916SBrian Feldman 
4849b50d902SRodney W. Grimes 		default: {
4859b50d902SRodney W. Grimes 			static char unknown[10];
4869b50d902SRodney W. Grimes 			sprintf(badtype = unknown, "?(%x)", vn.v_tag);
4879b50d902SRodney W. Grimes 			break;;
4889b50d902SRodney W. Grimes 		}
4899b50d902SRodney W. Grimes 	}
4909b50d902SRodney W. Grimes 	if (checkfile) {
4919b50d902SRodney W. Grimes 		int fsmatch = 0;
4929b50d902SRodney W. Grimes 		register DEVS *d;
4939b50d902SRodney W. Grimes 
4949b50d902SRodney W. Grimes 		if (badtype)
4959b50d902SRodney W. Grimes 			return;
4969b50d902SRodney W. Grimes 		for (d = devs; d != NULL; d = d->next)
4979b50d902SRodney W. Grimes 			if (d->fsid == fst.fsid) {
4989b50d902SRodney W. Grimes 				fsmatch = 1;
4999b50d902SRodney W. Grimes 				if (d->ino == fst.fileid) {
5009b50d902SRodney W. Grimes 					filename = d->name;
5019b50d902SRodney W. Grimes 					break;
5029b50d902SRodney W. Grimes 				}
5039b50d902SRodney W. Grimes 			}
5049b50d902SRodney W. Grimes 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
5059b50d902SRodney W. Grimes 			return;
5069b50d902SRodney W. Grimes 	}
5079b50d902SRodney W. Grimes 	PREFIX(i);
5089b50d902SRodney W. Grimes 	if (badtype) {
5099b50d902SRodney W. Grimes 		(void)printf(" -         -  %10s    -\n", badtype);
5109b50d902SRodney W. Grimes 		return;
5119b50d902SRodney W. Grimes 	}
5129b50d902SRodney W. Grimes 	if (nflg)
5139b50d902SRodney W. Grimes 		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
5149b50d902SRodney W. Grimes 	else
5159b50d902SRodney W. Grimes 		(void)printf(" %-8s", getmnton(vn.v_mount));
5169b50d902SRodney W. Grimes 	if (nflg)
5179b50d902SRodney W. Grimes 		(void)sprintf(mode, "%o", fst.mode);
5189b50d902SRodney W. Grimes 	else
5199b50d902SRodney W. Grimes 		strmode(fst.mode, mode);
52022694ebaSBruce Evans 	(void)printf(" %6ld %10s", fst.fileid, mode);
5219b50d902SRodney W. Grimes 	switch (vn.v_type) {
5229b50d902SRodney W. Grimes 	case VBLK:
5239b50d902SRodney W. Grimes 	case VCHR: {
5249b50d902SRodney W. Grimes 		char *name;
5259b50d902SRodney W. Grimes 
5269b50d902SRodney W. Grimes 		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
5279b50d902SRodney W. Grimes 		    S_IFCHR : S_IFBLK)) == NULL))
5289b50d902SRodney W. Grimes 			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
5299b50d902SRodney W. Grimes 		else
5309b50d902SRodney W. Grimes 			printf(" %6s", name);
5319b50d902SRodney W. Grimes 		break;
5329b50d902SRodney W. Grimes 	}
5339b50d902SRodney W. Grimes 	default:
534df8327e7SAlexander Langer 		printf(" %6lu", fst.size);
5359b50d902SRodney W. Grimes 	}
5369b50d902SRodney W. Grimes 	rw[0] = '\0';
5379b50d902SRodney W. Grimes 	if (flag & FREAD)
5389b50d902SRodney W. Grimes 		strcat(rw, "r");
5399b50d902SRodney W. Grimes 	if (flag & FWRITE)
5409b50d902SRodney W. Grimes 		strcat(rw, "w");
5419b50d902SRodney W. Grimes 	printf(" %2s", rw);
5429b50d902SRodney W. Grimes 	if (filename && !fsflg)
5439b50d902SRodney W. Grimes 		printf("  %s", filename);
5449b50d902SRodney W. Grimes 	putchar('\n');
5459b50d902SRodney W. Grimes }
5469b50d902SRodney W. Grimes 
5479b50d902SRodney W. Grimes int
5489b50d902SRodney W. Grimes ufs_filestat(vp, fsp)
5499b50d902SRodney W. Grimes 	struct vnode *vp;
5509b50d902SRodney W. Grimes 	struct filestat *fsp;
5519b50d902SRodney W. Grimes {
5529b50d902SRodney W. Grimes 	struct inode inode;
5539b50d902SRodney W. Grimes 
5549b50d902SRodney W. Grimes 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
55522694ebaSBruce Evans 		dprintf(stderr, "can't read inode at %p for pid %d\n",
55622694ebaSBruce Evans 		    (void *)VTOI(vp), Pid);
5579b50d902SRodney W. Grimes 		return 0;
5589b50d902SRodney W. Grimes 	}
55938cd1ee7SBrian Feldman 	/*
56038cd1ee7SBrian Feldman 	 * The st_dev from stat(2) is a udev_t. These kernel structures
56138cd1ee7SBrian Feldman 	 * contain dev_t structures. We need to convert to udev to make
56238cd1ee7SBrian Feldman 	 * comparisons
56338cd1ee7SBrian Feldman 	 */
56438cd1ee7SBrian Feldman 	fsp->fsid = dev2udev(inode.i_dev) & 0xffff;
5659b50d902SRodney W. Grimes 	fsp->fileid = (long)inode.i_number;
5669b50d902SRodney W. Grimes 	fsp->mode = (mode_t)inode.i_mode;
5679b50d902SRodney W. Grimes 	fsp->size = (u_long)inode.i_size;
5689b50d902SRodney W. Grimes 	fsp->rdev = inode.i_rdev;
5699b50d902SRodney W. Grimes 
5709b50d902SRodney W. Grimes 	return 1;
5719b50d902SRodney W. Grimes }
5729b50d902SRodney W. Grimes 
5739b50d902SRodney W. Grimes int
5749b50d902SRodney W. Grimes nfs_filestat(vp, fsp)
5759b50d902SRodney W. Grimes 	struct vnode *vp;
5769b50d902SRodney W. Grimes 	struct filestat *fsp;
5779b50d902SRodney W. Grimes {
5789b50d902SRodney W. Grimes 	struct nfsnode nfsnode;
5799b50d902SRodney W. Grimes 	register mode_t mode;
5809b50d902SRodney W. Grimes 
5819b50d902SRodney W. Grimes 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
58222694ebaSBruce Evans 		dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
58322694ebaSBruce Evans 		    (void *)VTONFS(vp), Pid);
5849b50d902SRodney W. Grimes 		return 0;
5859b50d902SRodney W. Grimes 	}
5869b50d902SRodney W. Grimes 	fsp->fsid = nfsnode.n_vattr.va_fsid;
5879b50d902SRodney W. Grimes 	fsp->fileid = nfsnode.n_vattr.va_fileid;
5889b50d902SRodney W. Grimes 	fsp->size = nfsnode.n_size;
5899b50d902SRodney W. Grimes 	fsp->rdev = nfsnode.n_vattr.va_rdev;
5909b50d902SRodney W. Grimes 	mode = (mode_t)nfsnode.n_vattr.va_mode;
5919b50d902SRodney W. Grimes 	switch (vp->v_type) {
5929b50d902SRodney W. Grimes 	case VREG:
5939b50d902SRodney W. Grimes 		mode |= S_IFREG;
5949b50d902SRodney W. Grimes 		break;
5959b50d902SRodney W. Grimes 	case VDIR:
5969b50d902SRodney W. Grimes 		mode |= S_IFDIR;
5979b50d902SRodney W. Grimes 		break;
5989b50d902SRodney W. Grimes 	case VBLK:
5999b50d902SRodney W. Grimes 		mode |= S_IFBLK;
6009b50d902SRodney W. Grimes 		break;
6019b50d902SRodney W. Grimes 	case VCHR:
6029b50d902SRodney W. Grimes 		mode |= S_IFCHR;
6039b50d902SRodney W. Grimes 		break;
6049b50d902SRodney W. Grimes 	case VLNK:
6059b50d902SRodney W. Grimes 		mode |= S_IFLNK;
6069b50d902SRodney W. Grimes 		break;
6079b50d902SRodney W. Grimes 	case VSOCK:
6089b50d902SRodney W. Grimes 		mode |= S_IFSOCK;
6099b50d902SRodney W. Grimes 		break;
6109b50d902SRodney W. Grimes 	case VFIFO:
6119b50d902SRodney W. Grimes 		mode |= S_IFIFO;
6129b50d902SRodney W. Grimes 		break;
6130b7a57ddSRuslan Ermilov 	case VNON:
6140b7a57ddSRuslan Ermilov 	case VBAD:
6150b7a57ddSRuslan Ermilov 		return 0;
6169b50d902SRodney W. Grimes 	};
6179b50d902SRodney W. Grimes 	fsp->mode = mode;
6189b50d902SRodney W. Grimes 
6199b50d902SRodney W. Grimes 	return 1;
6209b50d902SRodney W. Grimes }
6219b50d902SRodney W. Grimes 
6229b50d902SRodney W. Grimes 
6239b50d902SRodney W. Grimes char *
6249b50d902SRodney W. Grimes getmnton(m)
6259b50d902SRodney W. Grimes 	struct mount *m;
6269b50d902SRodney W. Grimes {
6279b50d902SRodney W. Grimes 	static struct mount mount;
6289b50d902SRodney W. Grimes 	static struct mtab {
6299b50d902SRodney W. Grimes 		struct mtab *next;
6309b50d902SRodney W. Grimes 		struct mount *m;
6319b50d902SRodney W. Grimes 		char mntonname[MNAMELEN];
6329b50d902SRodney W. Grimes 	} *mhead = NULL;
6339b50d902SRodney W. Grimes 	register struct mtab *mt;
6349b50d902SRodney W. Grimes 
6359b50d902SRodney W. Grimes 	for (mt = mhead; mt != NULL; mt = mt->next)
6369b50d902SRodney W. Grimes 		if (m == mt->m)
6379b50d902SRodney W. Grimes 			return (mt->mntonname);
6389b50d902SRodney W. Grimes 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
63922694ebaSBruce Evans 		warnx("can't read mount table at %p", (void *)m);
6409b50d902SRodney W. Grimes 		return (NULL);
6419b50d902SRodney W. Grimes 	}
642c1e65942SPhilippe Charnier 	if ((mt = malloc(sizeof (struct mtab))) == NULL)
643c1e65942SPhilippe Charnier 		err(1, NULL);
6449b50d902SRodney W. Grimes 	mt->m = m;
6459b50d902SRodney W. Grimes 	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
6469b50d902SRodney W. Grimes 	mt->next = mhead;
6479b50d902SRodney W. Grimes 	mhead = mt;
6489b50d902SRodney W. Grimes 	return (mt->mntonname);
6499b50d902SRodney W. Grimes }
6509b50d902SRodney W. Grimes 
6519b50d902SRodney W. Grimes void
6521c17fc99SPeter Wemm pipetrans(pi, i, flag)
6531c17fc99SPeter Wemm 	struct pipe *pi;
6541c17fc99SPeter Wemm 	int i;
6551c17fc99SPeter Wemm 	int flag;
6561c17fc99SPeter Wemm {
6571c17fc99SPeter Wemm 	struct pipe pip;
6581c17fc99SPeter Wemm 	char rw[3];
6591c17fc99SPeter Wemm 
6601c17fc99SPeter Wemm 	PREFIX(i);
6611c17fc99SPeter Wemm 
6621c17fc99SPeter Wemm 	/* fill in socket */
6631c17fc99SPeter Wemm 	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
66422694ebaSBruce Evans 		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
6651c17fc99SPeter Wemm 		goto bad;
6661c17fc99SPeter Wemm 	}
6671c17fc99SPeter Wemm 
668df94d4d2SMatt Jacob 	printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
6691c17fc99SPeter Wemm 	printf(" %6d", (int)pip.pipe_buffer.cnt);
6701c17fc99SPeter Wemm 	rw[0] = '\0';
6711c17fc99SPeter Wemm 	if (flag & FREAD)
6721c17fc99SPeter Wemm 		strcat(rw, "r");
6731c17fc99SPeter Wemm 	if (flag & FWRITE)
6741c17fc99SPeter Wemm 		strcat(rw, "w");
6751c17fc99SPeter Wemm 	printf(" %2s", rw);
6761c17fc99SPeter Wemm 	putchar('\n');
6771c17fc99SPeter Wemm 	return;
6781c17fc99SPeter Wemm 
6791c17fc99SPeter Wemm bad:
6801c17fc99SPeter Wemm 	printf("* error\n");
6811c17fc99SPeter Wemm }
6821c17fc99SPeter Wemm 
6831c17fc99SPeter Wemm void
6849b50d902SRodney W. Grimes socktrans(sock, i)
6859b50d902SRodney W. Grimes 	struct socket *sock;
6869b50d902SRodney W. Grimes 	int i;
6879b50d902SRodney W. Grimes {
6889b50d902SRodney W. Grimes 	static char *stypename[] = {
6899b50d902SRodney W. Grimes 		"unused",	/* 0 */
6909b50d902SRodney W. Grimes 		"stream", 	/* 1 */
6919b50d902SRodney W. Grimes 		"dgram",	/* 2 */
6929b50d902SRodney W. Grimes 		"raw",		/* 3 */
6939b50d902SRodney W. Grimes 		"rdm",		/* 4 */
6949b50d902SRodney W. Grimes 		"seqpak"	/* 5 */
6959b50d902SRodney W. Grimes 	};
6969b50d902SRodney W. Grimes #define	STYPEMAX 5
6979b50d902SRodney W. Grimes 	struct socket	so;
6989b50d902SRodney W. Grimes 	struct protosw	proto;
6999b50d902SRodney W. Grimes 	struct domain	dom;
7009b50d902SRodney W. Grimes 	struct inpcb	inpcb;
7019b50d902SRodney W. Grimes 	struct unpcb	unpcb;
7029b50d902SRodney W. Grimes 	int len;
7039b50d902SRodney W. Grimes 	char dname[32], *strcpy();
7049b50d902SRodney W. Grimes 
7059b50d902SRodney W. Grimes 	PREFIX(i);
7069b50d902SRodney W. Grimes 
7079b50d902SRodney W. Grimes 	/* fill in socket */
7089b50d902SRodney W. Grimes 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
70922694ebaSBruce Evans 		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
7109b50d902SRodney W. Grimes 		goto bad;
7119b50d902SRodney W. Grimes 	}
7129b50d902SRodney W. Grimes 
7139b50d902SRodney W. Grimes 	/* fill in protosw entry */
7149b50d902SRodney W. Grimes 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
71522694ebaSBruce Evans 		dprintf(stderr, "can't read protosw at %p",
71622694ebaSBruce Evans 		    (void *)so.so_proto);
7179b50d902SRodney W. Grimes 		goto bad;
7189b50d902SRodney W. Grimes 	}
7199b50d902SRodney W. Grimes 
7209b50d902SRodney W. Grimes 	/* fill in domain */
7219b50d902SRodney W. Grimes 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
72222694ebaSBruce Evans 		dprintf(stderr, "can't read domain at %p\n",
72322694ebaSBruce Evans 		    (void *)proto.pr_domain);
7249b50d902SRodney W. Grimes 		goto bad;
7259b50d902SRodney W. Grimes 	}
7269b50d902SRodney W. Grimes 
7279b50d902SRodney W. Grimes 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
7289b50d902SRodney W. Grimes 	    sizeof(dname) - 1)) < 0) {
72922694ebaSBruce Evans 		dprintf(stderr, "can't read domain name at %p\n",
73022694ebaSBruce Evans 		    (void *)dom.dom_name);
7319b50d902SRodney W. Grimes 		dname[0] = '\0';
7329b50d902SRodney W. Grimes 	}
7339b50d902SRodney W. Grimes 	else
7349b50d902SRodney W. Grimes 		dname[len] = '\0';
7359b50d902SRodney W. Grimes 
7369b50d902SRodney W. Grimes 	if ((u_short)so.so_type > STYPEMAX)
7379b50d902SRodney W. Grimes 		printf("* %s ?%d", dname, so.so_type);
7389b50d902SRodney W. Grimes 	else
7399b50d902SRodney W. Grimes 		printf("* %s %s", dname, stypename[so.so_type]);
7409b50d902SRodney W. Grimes 
7419b50d902SRodney W. Grimes 	/*
7429b50d902SRodney W. Grimes 	 * protocol specific formatting
7439b50d902SRodney W. Grimes 	 *
7449b50d902SRodney W. Grimes 	 * Try to find interesting things to print.  For tcp, the interesting
7459b50d902SRodney W. Grimes 	 * thing is the address of the tcpcb, for udp and others, just the
7469b50d902SRodney W. Grimes 	 * inpcb (socket pcb).  For unix domain, its the address of the socket
7479b50d902SRodney W. Grimes 	 * pcb and the address of the connected pcb (if connected).  Otherwise
7489b50d902SRodney W. Grimes 	 * just print the protocol number and address of the socket itself.
7499b50d902SRodney W. Grimes 	 * The idea is not to duplicate netstat, but to make available enough
7509b50d902SRodney W. Grimes 	 * information for further analysis.
7519b50d902SRodney W. Grimes 	 */
7529b50d902SRodney W. Grimes 	switch(dom.dom_family) {
7539b50d902SRodney W. Grimes 	case AF_INET:
754677e00c0SYoshinobu Inoue 	case AF_INET6:
7559b50d902SRodney W. Grimes 		getinetproto(proto.pr_protocol);
7569b50d902SRodney W. Grimes 		if (proto.pr_protocol == IPPROTO_TCP ) {
7579b50d902SRodney W. Grimes 			if (so.so_pcb) {
7589b50d902SRodney W. Grimes 				if (kvm_read(kd, (u_long)so.so_pcb,
7599b50d902SRodney W. Grimes 				    (char *)&inpcb, sizeof(struct inpcb))
7609b50d902SRodney W. Grimes 				    != sizeof(struct inpcb)) {
7619b50d902SRodney W. Grimes 					dprintf(stderr,
76222694ebaSBruce Evans 					    "can't read inpcb at %p\n",
76322694ebaSBruce Evans 					    (void *)so.so_pcb);
7649b50d902SRodney W. Grimes 					goto bad;
7659b50d902SRodney W. Grimes 				}
766df94d4d2SMatt Jacob 				printf(" %lx", (u_long)inpcb.inp_ppcb);
7679b50d902SRodney W. Grimes 			}
7689b50d902SRodney W. Grimes 		}
7699b50d902SRodney W. Grimes 		else if (so.so_pcb)
770df94d4d2SMatt Jacob 			printf(" %lx", (u_long)so.so_pcb);
7719b50d902SRodney W. Grimes 		break;
7729b50d902SRodney W. Grimes 	case AF_UNIX:
7739b50d902SRodney W. Grimes 		/* print address of pcb and connected pcb */
7749b50d902SRodney W. Grimes 		if (so.so_pcb) {
775df94d4d2SMatt Jacob 			printf(" %lx", (u_long)so.so_pcb);
7769b50d902SRodney W. Grimes 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
7779b50d902SRodney W. Grimes 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
77822694ebaSBruce Evans 				dprintf(stderr, "can't read unpcb at %p\n",
77922694ebaSBruce Evans 				    (void *)so.so_pcb);
7809b50d902SRodney W. Grimes 				goto bad;
7819b50d902SRodney W. Grimes 			}
7829b50d902SRodney W. Grimes 			if (unpcb.unp_conn) {
7839b50d902SRodney W. Grimes 				char shoconn[4], *cp;
7849b50d902SRodney W. Grimes 
7859b50d902SRodney W. Grimes 				cp = shoconn;
7869b50d902SRodney W. Grimes 				if (!(so.so_state & SS_CANTRCVMORE))
7879b50d902SRodney W. Grimes 					*cp++ = '<';
7889b50d902SRodney W. Grimes 				*cp++ = '-';
7899b50d902SRodney W. Grimes 				if (!(so.so_state & SS_CANTSENDMORE))
7909b50d902SRodney W. Grimes 					*cp++ = '>';
7919b50d902SRodney W. Grimes 				*cp = '\0';
792df94d4d2SMatt Jacob 				printf(" %s %lx", shoconn,
793df94d4d2SMatt Jacob 				    (u_long)unpcb.unp_conn);
7949b50d902SRodney W. Grimes 			}
7959b50d902SRodney W. Grimes 		}
7969b50d902SRodney W. Grimes 		break;
7979b50d902SRodney W. Grimes 	default:
7989b50d902SRodney W. Grimes 		/* print protocol number and socket address */
799df94d4d2SMatt Jacob 		printf(" %d %lx", proto.pr_protocol, (u_long)sock);
8009b50d902SRodney W. Grimes 	}
8019b50d902SRodney W. Grimes 	printf("\n");
8029b50d902SRodney W. Grimes 	return;
8039b50d902SRodney W. Grimes bad:
8049b50d902SRodney W. Grimes 	printf("* error\n");
8059b50d902SRodney W. Grimes }
8069b50d902SRodney W. Grimes 
80738cd1ee7SBrian Feldman 
80838cd1ee7SBrian Feldman /*
80938cd1ee7SBrian Feldman  * Read the specinfo structure in the kernel (as pointed to by a dev_t)
81038cd1ee7SBrian Feldman  * in order to work out the associated udev_t
81138cd1ee7SBrian Feldman  */
81238cd1ee7SBrian Feldman udev_t
81338cd1ee7SBrian Feldman dev2udev(dev)
81438cd1ee7SBrian Feldman 	dev_t dev;
81538cd1ee7SBrian Feldman {
81638cd1ee7SBrian Feldman 	struct specinfo si;
81738cd1ee7SBrian Feldman 
81838cd1ee7SBrian Feldman 	if (KVM_READ(dev, &si, sizeof si)) {
81938cd1ee7SBrian Feldman 		return si.si_udev;
82038cd1ee7SBrian Feldman 	} else {
821df94d4d2SMatt Jacob 		dprintf(stderr, "can't convert dev_t %x to a udev_t\n", dev);
82238cd1ee7SBrian Feldman 		return -1;
82338cd1ee7SBrian Feldman 	}
82438cd1ee7SBrian Feldman }
82538cd1ee7SBrian Feldman 
8269b50d902SRodney W. Grimes /*
8279b50d902SRodney W. Grimes  * getinetproto --
8289b50d902SRodney W. Grimes  *	print name of protocol number
8299b50d902SRodney W. Grimes  */
8309b50d902SRodney W. Grimes void
8319b50d902SRodney W. Grimes getinetproto(number)
8329b50d902SRodney W. Grimes 	int number;
8339b50d902SRodney W. Grimes {
8340b7a57ddSRuslan Ermilov 	static int isopen;
8350b7a57ddSRuslan Ermilov 	register struct protoent *pe;
8369b50d902SRodney W. Grimes 
8370b7a57ddSRuslan Ermilov 	if (!isopen)
8380b7a57ddSRuslan Ermilov 		setprotoent(++isopen);
8390b7a57ddSRuslan Ermilov 	if ((pe = getprotobynumber(number)) != NULL)
8400b7a57ddSRuslan Ermilov 		printf(" %s", pe->p_name);
8410b7a57ddSRuslan Ermilov 	else
8429b50d902SRodney W. Grimes 		printf(" %d", number);
8439b50d902SRodney W. Grimes }
8449b50d902SRodney W. Grimes 
8451c17fc99SPeter Wemm int
8469b50d902SRodney W. Grimes getfname(filename)
8479b50d902SRodney W. Grimes 	char *filename;
8489b50d902SRodney W. Grimes {
8499b50d902SRodney W. Grimes 	struct stat statbuf;
8509b50d902SRodney W. Grimes 	DEVS *cur;
8519b50d902SRodney W. Grimes 
8529b50d902SRodney W. Grimes 	if (stat(filename, &statbuf)) {
853c1e65942SPhilippe Charnier 		warn("%s", filename);
8549b50d902SRodney W. Grimes 		return(0);
8559b50d902SRodney W. Grimes 	}
856c1e65942SPhilippe Charnier 	if ((cur = malloc(sizeof(DEVS))) == NULL)
857c1e65942SPhilippe Charnier 		err(1, NULL);
8589b50d902SRodney W. Grimes 	cur->next = devs;
8599b50d902SRodney W. Grimes 	devs = cur;
8609b50d902SRodney W. Grimes 
8619b50d902SRodney W. Grimes 	cur->ino = statbuf.st_ino;
8629b50d902SRodney W. Grimes 	cur->fsid = statbuf.st_dev & 0xffff;
8639b50d902SRodney W. Grimes 	cur->name = filename;
8649b50d902SRodney W. Grimes 	return(1);
8659b50d902SRodney W. Grimes }
8669b50d902SRodney W. Grimes 
8679b50d902SRodney W. Grimes void
8689b50d902SRodney W. Grimes usage()
8699b50d902SRodney W. Grimes {
8709b50d902SRodney W. Grimes 	(void)fprintf(stderr,
871d0482be8SBrian Feldman  "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
8729b50d902SRodney W. Grimes 	exit(1);
8739b50d902SRodney W. Grimes }
874