xref: /freebsd/usr.bin/fstat/fstat.c (revision bc093719ca478fe10b938cef32c30b528042cbcd)
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>
61bc093719SEd Schouten #include <sys/tty.h>
629b50d902SRodney W. Grimes #include <sys/filedesc.h>
63d2bccb9fSDavid Greenman #include <sys/queue.h>
64037dab57SPawel Jakub Dawidek #define	_WANT_FILE
65037dab57SPawel Jakub Dawidek #include <sys/file.h>
66436f2e52SEd Schouten #include <sys/conf.h>
67c4473420SPeter Wemm #define	_KERNEL
6827eabcedSMike Silbersack #include <sys/pipe.h>
69d8aadd88SBosko Milekic #include <sys/mount.h>
709b50d902SRodney W. Grimes #include <ufs/ufs/quota.h>
719b50d902SRodney W. Grimes #include <ufs/ufs/inode.h>
72fca066feSBrian Feldman #include <fs/devfs/devfs.h>
73997591caSChristian S.J. Peron #include <fs/devfs/devfs_int.h>
747a8671e9SAlfred Perlstein #undef _KERNEL
75a62dc406SDoug Rabson #include <nfs/nfsproto.h>
769b50d902SRodney W. Grimes #include <nfs/rpcv2.h>
7791196234SPeter Wemm #include <nfsclient/nfs.h>
7891196234SPeter Wemm #include <nfsclient/nfsnode.h>
799b50d902SRodney W. Grimes 
8006e88916SBrian Feldman 
81d0482be8SBrian Feldman #include <vm/vm.h>
82d0482be8SBrian Feldman #include <vm/vm_map.h>
83d0482be8SBrian Feldman #include <vm/vm_object.h>
84d0482be8SBrian Feldman 
859b50d902SRodney W. Grimes #include <net/route.h>
869b50d902SRodney W. Grimes #include <netinet/in.h>
879b50d902SRodney W. Grimes #include <netinet/in_systm.h>
889b50d902SRodney W. Grimes #include <netinet/ip.h>
899b50d902SRodney W. Grimes #include <netinet/in_pcb.h>
909b50d902SRodney W. Grimes 
919b50d902SRodney W. Grimes #include <ctype.h>
92c1e65942SPhilippe Charnier #include <err.h>
935d98ce75SBruce Evans #include <fcntl.h>
949b50d902SRodney W. Grimes #include <kvm.h>
95df3f5d9dSPeter Wemm #include <limits.h>
969b50d902SRodney W. Grimes #include <nlist.h>
979b50d902SRodney W. Grimes #include <paths.h>
989b50d902SRodney W. Grimes #include <pwd.h>
999b50d902SRodney W. Grimes #include <stdio.h>
1009b50d902SRodney W. Grimes #include <stdlib.h>
10105427aafSKonstantin Belousov #include <stddef.h>
1029b50d902SRodney W. Grimes #include <string.h>
103df3f5d9dSPeter Wemm #include <unistd.h>
1040b7a57ddSRuslan Ermilov #include <netdb.h>
1059b50d902SRodney W. Grimes 
10606e88916SBrian Feldman #include "fstat.h"
10706e88916SBrian Feldman 
1089b50d902SRodney W. Grimes #define	TEXT	-1
1099b50d902SRodney W. Grimes #define	CDIR	-2
1109b50d902SRodney W. Grimes #define	RDIR	-3
1119b50d902SRodney W. Grimes #define	TRACE	-4
112d0482be8SBrian Feldman #define	MMAP	-5
1135dc1f79eSPoul-Henning Kamp #define	JDIR	-6
1149b50d902SRodney W. Grimes 
1159b50d902SRodney W. Grimes DEVS *devs;
1169b50d902SRodney W. Grimes 
1179b50d902SRodney W. Grimes #ifdef notdef
1189b50d902SRodney W. Grimes struct nlist nl[] = {
1199b50d902SRodney W. Grimes 	{ "" },
1209b50d902SRodney W. Grimes };
1219b50d902SRodney W. Grimes #endif
1229b50d902SRodney W. Grimes 
1239b50d902SRodney W. Grimes int 	fsflg,	/* show files on same filesystem as file(s) argument */
1249b50d902SRodney W. Grimes 	pflg,	/* show files open by a particular pid */
1259b50d902SRodney W. Grimes 	uflg;	/* show files open by a particular (effective) user */
1269b50d902SRodney W. Grimes int 	checkfile; /* true if restricting to particular files or filesystems */
1279b50d902SRodney W. Grimes int	nflg;	/* (numerical) display f.s. and rdev as dev_t */
1289b50d902SRodney W. Grimes int	vflg;	/* display errors in locating kernel data objects etc... */
129d0482be8SBrian Feldman int	mflg;	/* include memory-mapped files */
1309b50d902SRodney W. Grimes 
1319b50d902SRodney W. Grimes 
1329b50d902SRodney W. Grimes struct file **ofiles;	/* buffer of pointers to file structures */
1339b50d902SRodney W. Grimes int maxfiles;
1349b50d902SRodney W. Grimes #define ALLOC_OFILES(d)	\
1359b50d902SRodney W. Grimes 	if ((d) > maxfiles) { \
1369b50d902SRodney W. Grimes 		free(ofiles); \
1379b50d902SRodney W. Grimes 		ofiles = malloc((d) * sizeof(struct file *)); \
1389b50d902SRodney W. Grimes 		if (ofiles == NULL) { \
139c1e65942SPhilippe Charnier 			err(1, NULL); \
1409b50d902SRodney W. Grimes 		} \
1419b50d902SRodney W. Grimes 		maxfiles = (d); \
1429b50d902SRodney W. Grimes 	}
1439b50d902SRodney W. Grimes 
1441f910d6cSDag-Erling Smørgrav char *memf, *nlistf;
1459b50d902SRodney W. Grimes kvm_t *kd;
1469b50d902SRodney W. Grimes 
1471f910d6cSDag-Erling Smørgrav static void fstat_kvm(int, int);
1481f910d6cSDag-Erling Smørgrav static void fstat_sysctl(int, int);
149f1bb2cd2SWarner Losh void dofiles(struct kinfo_proc *kp);
150f1bb2cd2SWarner Losh void dommap(struct kinfo_proc *kp);
151f1bb2cd2SWarner Losh void vtrans(struct vnode *vp, int i, int flag);
152f1bb2cd2SWarner Losh int  ufs_filestat(struct vnode *vp, struct filestat *fsp);
153f1bb2cd2SWarner Losh int  nfs_filestat(struct vnode *vp, struct filestat *fsp);
154f1bb2cd2SWarner Losh int  devfs_filestat(struct vnode *vp, struct filestat *fsp);
155f1bb2cd2SWarner Losh char *getmnton(struct mount *m);
156f1bb2cd2SWarner Losh void pipetrans(struct pipe *pi, int i, int flag);
157f1bb2cd2SWarner Losh void socktrans(struct socket *sock, int i);
158bc093719SEd Schouten void ptstrans(struct tty *tp, int i, int flag);
159f1bb2cd2SWarner Losh void getinetproto(int number);
16027d57ea9SDavid Malone int  getfname(const char *filename);
161f1bb2cd2SWarner Losh void usage(void);
162a4cdc045SChristian S.J. Peron char *kdevtoname(struct cdev *dev);
1631c17fc99SPeter Wemm 
1641c17fc99SPeter Wemm int
1651e925017SDavid Malone main(int argc, char **argv)
1669b50d902SRodney W. Grimes {
16727d57ea9SDavid Malone 	struct passwd *passwd;
1689b50d902SRodney W. Grimes 	int arg, ch, what;
1699b50d902SRodney W. Grimes 
1709b50d902SRodney W. Grimes 	arg = 0;
171f9feee17SEd Maste 	what = KERN_PROC_PROC;
1729b50d902SRodney W. Grimes 	nlistf = memf = NULL;
173d0482be8SBrian Feldman 	while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1)
1749b50d902SRodney W. Grimes 		switch((char)ch) {
1759b50d902SRodney W. Grimes 		case 'f':
1769b50d902SRodney W. Grimes 			fsflg = 1;
1779b50d902SRodney W. Grimes 			break;
1789b50d902SRodney W. Grimes 		case 'M':
1799b50d902SRodney W. Grimes 			memf = optarg;
1809b50d902SRodney W. Grimes 			break;
1819b50d902SRodney W. Grimes 		case 'N':
1829b50d902SRodney W. Grimes 			nlistf = optarg;
1839b50d902SRodney W. Grimes 			break;
184d0482be8SBrian Feldman 		case 'm':
185d0482be8SBrian Feldman 			mflg = 1;
186d0482be8SBrian Feldman 			break;
1879b50d902SRodney W. Grimes 		case 'n':
1889b50d902SRodney W. Grimes 			nflg = 1;
1899b50d902SRodney W. Grimes 			break;
1909b50d902SRodney W. Grimes 		case 'p':
1919b50d902SRodney W. Grimes 			if (pflg++)
1929b50d902SRodney W. Grimes 				usage();
1939b50d902SRodney W. Grimes 			if (!isdigit(*optarg)) {
194c1e65942SPhilippe Charnier 				warnx("-p requires a process id");
1959b50d902SRodney W. Grimes 				usage();
1969b50d902SRodney W. Grimes 			}
1979b50d902SRodney W. Grimes 			what = KERN_PROC_PID;
1989b50d902SRodney W. Grimes 			arg = atoi(optarg);
1999b50d902SRodney W. Grimes 			break;
2009b50d902SRodney W. Grimes 		case 'u':
2019b50d902SRodney W. Grimes 			if (uflg++)
2029b50d902SRodney W. Grimes 				usage();
203c1e65942SPhilippe Charnier 			if (!(passwd = getpwnam(optarg)))
204c1e65942SPhilippe Charnier 				errx(1, "%s: unknown uid", optarg);
2059b50d902SRodney W. Grimes 			what = KERN_PROC_UID;
2069b50d902SRodney W. Grimes 			arg = passwd->pw_uid;
2079b50d902SRodney W. Grimes 			break;
2089b50d902SRodney W. Grimes 		case 'v':
2099b50d902SRodney W. Grimes 			vflg = 1;
2109b50d902SRodney W. Grimes 			break;
2119b50d902SRodney W. Grimes 		case '?':
2129b50d902SRodney W. Grimes 		default:
2139b50d902SRodney W. Grimes 			usage();
2149b50d902SRodney W. Grimes 		}
2159b50d902SRodney W. Grimes 
2169b50d902SRodney W. Grimes 	if (*(argv += optind)) {
2179b50d902SRodney W. Grimes 		for (; *argv; ++argv) {
2189b50d902SRodney W. Grimes 			if (getfname(*argv))
2199b50d902SRodney W. Grimes 				checkfile = 1;
2209b50d902SRodney W. Grimes 		}
2219b50d902SRodney W. Grimes 		if (!checkfile)	/* file(s) specified, but none accessable */
2229b50d902SRodney W. Grimes 			exit(1);
2239b50d902SRodney W. Grimes 	}
2249b50d902SRodney W. Grimes 
2259b50d902SRodney W. Grimes 	if (fsflg && !checkfile) {
2269b50d902SRodney W. Grimes 		/* -f with no files means use wd */
2279b50d902SRodney W. Grimes 		if (getfname(".") == 0)
2289b50d902SRodney W. Grimes 			exit(1);
2299b50d902SRodney W. Grimes 		checkfile = 1;
2309b50d902SRodney W. Grimes 	}
2319b50d902SRodney W. Grimes 
2321f910d6cSDag-Erling Smørgrav 	if (memf != NULL)
2331f910d6cSDag-Erling Smørgrav 		fstat_kvm(what, arg);
2341f910d6cSDag-Erling Smørgrav 	else
2351f910d6cSDag-Erling Smørgrav 		fstat_sysctl(what, arg);
2361f910d6cSDag-Erling Smørgrav 	exit(0);
2371f910d6cSDag-Erling Smørgrav }
2381f910d6cSDag-Erling Smørgrav 
2391f910d6cSDag-Erling Smørgrav static void
2401f910d6cSDag-Erling Smørgrav print_header(void)
2411f910d6cSDag-Erling Smørgrav {
2421f910d6cSDag-Erling Smørgrav 
2431f910d6cSDag-Erling Smørgrav 	if (nflg)
2441f910d6cSDag-Erling Smørgrav 		printf("%s",
2451f910d6cSDag-Erling Smørgrav "USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
2461f910d6cSDag-Erling Smørgrav 	else
2471f910d6cSDag-Erling Smørgrav 		printf("%s",
2481f910d6cSDag-Erling Smørgrav "USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
2491f910d6cSDag-Erling Smørgrav 	if (checkfile && fsflg == 0)
2501f910d6cSDag-Erling Smørgrav 		printf(" NAME\n");
2511f910d6cSDag-Erling Smørgrav 	else
2521f910d6cSDag-Erling Smørgrav 		putchar('\n');
2531f910d6cSDag-Erling Smørgrav }
2541f910d6cSDag-Erling Smørgrav 
2551f910d6cSDag-Erling Smørgrav static void
2561f910d6cSDag-Erling Smørgrav fstat_kvm(int what, int arg)
2571f910d6cSDag-Erling Smørgrav {
2581f910d6cSDag-Erling Smørgrav 	struct kinfo_proc *p, *plast;
2591f910d6cSDag-Erling Smørgrav 	char buf[_POSIX2_LINE_MAX];
2601f910d6cSDag-Erling Smørgrav 	int cnt;
2611f910d6cSDag-Erling Smørgrav 
2621f910d6cSDag-Erling Smørgrav 	ALLOC_OFILES(256);	/* reserve space for file pointers */
2631f910d6cSDag-Erling Smørgrav 
2649b50d902SRodney W. Grimes 	/*
2659b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
2669b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
2679b50d902SRodney W. Grimes 	 */
2689b50d902SRodney W. Grimes 	if (nlistf != NULL || memf != NULL)
2699b50d902SRodney W. Grimes 		setgid(getgid());
2709b50d902SRodney W. Grimes 
271c1e65942SPhilippe Charnier 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
272c1e65942SPhilippe Charnier 		errx(1, "%s", buf);
2733b7e5cccSRuslan Ermilov 	setgid(getgid());
2749b50d902SRodney W. Grimes #ifdef notdef
275c1e65942SPhilippe Charnier 	if (kvm_nlist(kd, nl) != 0)
276c1e65942SPhilippe Charnier 		errx(1, "no namelist: %s", kvm_geterr(kd));
2779b50d902SRodney W. Grimes #endif
278c1e65942SPhilippe Charnier 	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
279c1e65942SPhilippe Charnier 		errx(1, "%s", kvm_geterr(kd));
280c30afbc5SMaxim Konovalov 	print_header();
2819b50d902SRodney W. Grimes 	for (plast = &p[cnt]; p < plast; ++p) {
2821f7d2501SKirk McKusick 		if (p->ki_stat == SZOMB)
2839b50d902SRodney W. Grimes 			continue;
2849b50d902SRodney W. Grimes 		dofiles(p);
285d0482be8SBrian Feldman 		if (mflg)
286d0482be8SBrian Feldman 			dommap(p);
2879b50d902SRodney W. Grimes 	}
2881f910d6cSDag-Erling Smørgrav }
2891f910d6cSDag-Erling Smørgrav 
2901f910d6cSDag-Erling Smørgrav static void
2911f910d6cSDag-Erling Smørgrav fstat_sysctl(int what, int arg)
2921f910d6cSDag-Erling Smørgrav {
2931f910d6cSDag-Erling Smørgrav 
2941f910d6cSDag-Erling Smørgrav 	/* not yet implemented */
2951f910d6cSDag-Erling Smørgrav 	fstat_kvm(what, arg);
2969b50d902SRodney W. Grimes }
2979b50d902SRodney W. Grimes 
29827d57ea9SDavid Malone const char	*Uname, *Comm;
2999b50d902SRodney W. Grimes int	Pid;
3009b50d902SRodney W. Grimes 
3019b50d902SRodney W. Grimes #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
3029b50d902SRodney W. Grimes 	switch(i) { \
3039b50d902SRodney W. Grimes 	case TEXT: \
3049b50d902SRodney W. Grimes 		printf(" text"); \
3059b50d902SRodney W. Grimes 		break; \
3069b50d902SRodney W. Grimes 	case CDIR: \
3079b50d902SRodney W. Grimes 		printf("   wd"); \
3089b50d902SRodney W. Grimes 		break; \
3099b50d902SRodney W. Grimes 	case RDIR: \
3109b50d902SRodney W. Grimes 		printf(" root"); \
3119b50d902SRodney W. Grimes 		break; \
3129b50d902SRodney W. Grimes 	case TRACE: \
3139b50d902SRodney W. Grimes 		printf("   tr"); \
3149b50d902SRodney W. Grimes 		break; \
315d0482be8SBrian Feldman 	case MMAP: \
316d0482be8SBrian Feldman 		printf(" mmap"); \
317d0482be8SBrian Feldman 		break; \
3185dc1f79eSPoul-Henning Kamp 	case JDIR: \
3195dc1f79eSPoul-Henning Kamp 		printf(" jail"); \
3205dc1f79eSPoul-Henning Kamp 		break; \
3219b50d902SRodney W. Grimes 	default: \
3229b50d902SRodney W. Grimes 		printf(" %4d", i); \
3239b50d902SRodney W. Grimes 		break; \
3249b50d902SRodney W. Grimes 	}
3259b50d902SRodney W. Grimes 
3269b50d902SRodney W. Grimes /*
3279b50d902SRodney W. Grimes  * print open files attributed to this process
3289b50d902SRodney W. Grimes  */
3299b50d902SRodney W. Grimes void
3301e925017SDavid Malone dofiles(struct kinfo_proc *kp)
3319b50d902SRodney W. Grimes {
332c1e65942SPhilippe Charnier 	int i;
3339b50d902SRodney W. Grimes 	struct file file;
334e93bd4efSPoul-Henning Kamp 	struct filedesc filed;
3359b50d902SRodney W. Grimes 
3361f7d2501SKirk McKusick 	Uname = user_from_uid(kp->ki_uid, 0);
3371f7d2501SKirk McKusick 	Pid = kp->ki_pid;
3381f7d2501SKirk McKusick 	Comm = kp->ki_comm;
3399b50d902SRodney W. Grimes 
3401f7d2501SKirk McKusick 	if (kp->ki_fd == NULL)
3419b50d902SRodney W. Grimes 		return;
342e93bd4efSPoul-Henning Kamp 	if (!KVM_READ(kp->ki_fd, &filed, sizeof (filed))) {
34322694ebaSBruce Evans 		dprintf(stderr, "can't read filedesc at %p for pid %d\n",
3441f7d2501SKirk McKusick 		    (void *)kp->ki_fd, Pid);
3459b50d902SRodney W. Grimes 		return;
3469b50d902SRodney W. Grimes 	}
3479b50d902SRodney W. Grimes 	/*
3489b50d902SRodney W. Grimes 	 * root directory vnode, if one
3499b50d902SRodney W. Grimes 	 */
3509b50d902SRodney W. Grimes 	if (filed.fd_rdir)
3519b50d902SRodney W. Grimes 		vtrans(filed.fd_rdir, RDIR, FREAD);
3529b50d902SRodney W. Grimes 	/*
3539b50d902SRodney W. Grimes 	 * current working directory vnode
3549b50d902SRodney W. Grimes 	 */
35502e50214SJohn Baldwin 	if (filed.fd_cdir)
3569b50d902SRodney W. Grimes 		vtrans(filed.fd_cdir, CDIR, FREAD);
3579b50d902SRodney W. Grimes 	/*
3585dc1f79eSPoul-Henning Kamp 	 * jail root, if any.
3595dc1f79eSPoul-Henning Kamp 	 */
3605dc1f79eSPoul-Henning Kamp 	if (filed.fd_jdir)
3615dc1f79eSPoul-Henning Kamp 		vtrans(filed.fd_jdir, JDIR, FREAD);
3625dc1f79eSPoul-Henning Kamp 	/*
3639b50d902SRodney W. Grimes 	 * ktrace vnode, if one
3649b50d902SRodney W. Grimes 	 */
3651f7d2501SKirk McKusick 	if (kp->ki_tracep)
3661f7d2501SKirk McKusick 		vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE);
3679b50d902SRodney W. Grimes 	/*
368ca7e3117SPeter Wemm 	 * text vnode, if one
369ca7e3117SPeter Wemm 	 */
3701f7d2501SKirk McKusick 	if (kp->ki_textvp)
3711f7d2501SKirk McKusick 		vtrans(kp->ki_textvp, TEXT, FREAD);
372ca7e3117SPeter Wemm 	/*
3739b50d902SRodney W. Grimes 	 * open files
3749b50d902SRodney W. Grimes 	 */
3759b50d902SRodney W. Grimes #define FPSIZE	(sizeof (struct file *))
3765dc1f79eSPoul-Henning Kamp #define MAX_LASTFILE	(0x1000000)
3775dc1f79eSPoul-Henning Kamp 
3785dc1f79eSPoul-Henning Kamp 	/* Sanity check on filed.fd_lastfile */
3795dc1f79eSPoul-Henning Kamp 	if (filed.fd_lastfile <= -1 || filed.fd_lastfile > MAX_LASTFILE)
3805dc1f79eSPoul-Henning Kamp 		return;
3815dc1f79eSPoul-Henning Kamp 
3829b50d902SRodney W. Grimes 	ALLOC_OFILES(filed.fd_lastfile+1);
3839b50d902SRodney W. Grimes 	if (!KVM_READ(filed.fd_ofiles, ofiles,
3849b50d902SRodney W. Grimes 	    (filed.fd_lastfile+1) * FPSIZE)) {
3859b50d902SRodney W. Grimes 		dprintf(stderr,
38622694ebaSBruce Evans 		    "can't read file structures at %p for pid %d\n",
38722694ebaSBruce Evans 		    (void *)filed.fd_ofiles, Pid);
3889b50d902SRodney W. Grimes 		return;
3899b50d902SRodney W. Grimes 	}
3909b50d902SRodney W. Grimes 	for (i = 0; i <= filed.fd_lastfile; i++) {
3919b50d902SRodney W. Grimes 		if (ofiles[i] == NULL)
3929b50d902SRodney W. Grimes 			continue;
3939b50d902SRodney W. Grimes 		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
39422694ebaSBruce Evans 			dprintf(stderr, "can't read file %d at %p for pid %d\n",
39522694ebaSBruce Evans 			    i, (void *)ofiles[i], Pid);
3969b50d902SRodney W. Grimes 			continue;
3979b50d902SRodney W. Grimes 		}
3989b50d902SRodney W. Grimes 		if (file.f_type == DTYPE_VNODE)
3997a13f01fSSuleiman Souhlal 			vtrans(file.f_vnode, i, file.f_flag);
4009b50d902SRodney W. Grimes 		else if (file.f_type == DTYPE_SOCKET) {
4019b50d902SRodney W. Grimes 			if (checkfile == 0)
40248e3128bSMatthew Dillon 				socktrans(file.f_data, i);
4039b50d902SRodney W. Grimes 		}
4041c17fc99SPeter Wemm #ifdef DTYPE_PIPE
4051c17fc99SPeter Wemm 		else if (file.f_type == DTYPE_PIPE) {
4061c17fc99SPeter Wemm 			if (checkfile == 0)
40748e3128bSMatthew Dillon 				pipetrans(file.f_data, i, file.f_flag);
4081c17fc99SPeter Wemm 		}
4091c17fc99SPeter Wemm #endif
410e6ebeb5fSPeter Pentchev #ifdef DTYPE_FIFO
411e6ebeb5fSPeter Pentchev 		else if (file.f_type == DTYPE_FIFO) {
412e6ebeb5fSPeter Pentchev 			if (checkfile == 0)
4137a13f01fSSuleiman Souhlal 				vtrans(file.f_vnode, i, file.f_flag);
414e6ebeb5fSPeter Pentchev 		}
415e6ebeb5fSPeter Pentchev #endif
416bc093719SEd Schouten #ifdef DTYPE_PTS
417bc093719SEd Schouten 		else if (file.f_type == DTYPE_PTS) {
418bc093719SEd Schouten 			if (checkfile == 0)
419bc093719SEd Schouten 				ptstrans(file.f_data, i, file.f_flag);
420bc093719SEd Schouten 		}
421bc093719SEd Schouten #endif
4229b50d902SRodney W. Grimes 		else {
4239b50d902SRodney W. Grimes 			dprintf(stderr,
4249b50d902SRodney W. Grimes 			    "unknown file type %d for file %d of pid %d\n",
4259b50d902SRodney W. Grimes 			    file.f_type, i, Pid);
4269b50d902SRodney W. Grimes 		}
4279b50d902SRodney W. Grimes 	}
4289b50d902SRodney W. Grimes }
4299b50d902SRodney W. Grimes 
4309b50d902SRodney W. Grimes void
4311e925017SDavid Malone dommap(struct kinfo_proc *kp)
432d0482be8SBrian Feldman {
433d0482be8SBrian Feldman 	vm_map_t map;
4341f7d2501SKirk McKusick 	struct vmspace vmspace;
435d0482be8SBrian Feldman 	struct vm_map_entry entry;
436d0482be8SBrian Feldman 	vm_map_entry_t entryp;
437d0482be8SBrian Feldman 	struct vm_object object;
438d0482be8SBrian Feldman 	vm_object_t objp;
439d0482be8SBrian Feldman 	int prot, fflags;
440d0482be8SBrian Feldman 
4411f7d2501SKirk McKusick 	if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) {
4421f7d2501SKirk McKusick 		dprintf(stderr,
4431f7d2501SKirk McKusick 		    "can't read vmspace at %p for pid %d\n",
4441f7d2501SKirk McKusick 		    (void *)kp->ki_vmspace, Pid);
445d0482be8SBrian Feldman 		return;
446d0482be8SBrian Feldman 	}
447d0482be8SBrian Feldman 	map = &vmspace.vm_map;
448d0482be8SBrian Feldman 
449b654e106SIan Dowse 	for (entryp = map->header.next;
450b654e106SIan Dowse 	    entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) {
451d0482be8SBrian Feldman 		if (!KVM_READ(entryp, &entry, sizeof(entry))) {
452d0482be8SBrian Feldman 			dprintf(stderr,
453d0482be8SBrian Feldman 			    "can't read vm_map_entry at %p for pid %d\n",
454d0482be8SBrian Feldman 			    (void *)entryp, Pid);
455d0482be8SBrian Feldman 			return;
456d0482be8SBrian Feldman 		}
457d0482be8SBrian Feldman 
458d0482be8SBrian Feldman 		if (entry.eflags & MAP_ENTRY_IS_SUB_MAP)
459d0482be8SBrian Feldman 			continue;
460d0482be8SBrian Feldman 
461d0482be8SBrian Feldman 		if ((objp = entry.object.vm_object) == NULL)
462d0482be8SBrian Feldman 			continue;
463d0482be8SBrian Feldman 
464d0482be8SBrian Feldman 		for (; objp; objp = object.backing_object) {
465d0482be8SBrian Feldman 			if (!KVM_READ(objp, &object, sizeof(object))) {
466d0482be8SBrian Feldman 				dprintf(stderr,
467d0482be8SBrian Feldman 				    "can't read vm_object at %p for pid %d\n",
468d0482be8SBrian Feldman 				    (void *)objp, Pid);
469d0482be8SBrian Feldman 				return;
470d0482be8SBrian Feldman 			}
471d0482be8SBrian Feldman 		}
472d0482be8SBrian Feldman 
473d0482be8SBrian Feldman 		prot = entry.protection;
474d0482be8SBrian Feldman 		fflags = (prot & VM_PROT_READ ? FREAD : 0) |
475d0482be8SBrian Feldman 		    (prot & VM_PROT_WRITE ? FWRITE : 0);
476d0482be8SBrian Feldman 
477d0482be8SBrian Feldman 		switch (object.type) {
478d0482be8SBrian Feldman 		case OBJT_VNODE:
479d0482be8SBrian Feldman 			vtrans((struct vnode *)object.handle, MMAP, fflags);
480d0482be8SBrian Feldman 			break;
481d0482be8SBrian Feldman 		default:
482d0482be8SBrian Feldman 			break;
483d0482be8SBrian Feldman 		}
484d0482be8SBrian Feldman 	}
485d0482be8SBrian Feldman }
486d0482be8SBrian Feldman 
487a4cdc045SChristian S.J. Peron char *
488a4cdc045SChristian S.J. Peron kdevtoname(struct cdev *dev)
489a4cdc045SChristian S.J. Peron {
490a4cdc045SChristian S.J. Peron 	struct cdev si;
491a4cdc045SChristian S.J. Peron 
492a4cdc045SChristian S.J. Peron 	if (!KVM_READ(dev, &si, sizeof si))
493a4cdc045SChristian S.J. Peron 		return (NULL);
494a4cdc045SChristian S.J. Peron 	return (strdup(si.__si_namebuf));
495a4cdc045SChristian S.J. Peron }
496a4cdc045SChristian S.J. Peron 
497d0482be8SBrian Feldman void
4981e925017SDavid Malone vtrans(struct vnode *vp, int i, int flag)
4999b50d902SRodney W. Grimes {
5009b50d902SRodney W. Grimes 	struct vnode vn;
5019b50d902SRodney W. Grimes 	struct filestat fst;
502e47c2178SNate Lawson 	char rw[3], mode[15], tagstr[12], *tagptr;
50327d57ea9SDavid Malone 	const char *badtype, *filename;
5049b50d902SRodney W. Grimes 
5059b50d902SRodney W. Grimes 	filename = badtype = NULL;
5069b50d902SRodney W. Grimes 	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
50722694ebaSBruce Evans 		dprintf(stderr, "can't read vnode at %p for pid %d\n",
50822694ebaSBruce Evans 		    (void *)vp, Pid);
5099b50d902SRodney W. Grimes 		return;
5109b50d902SRodney W. Grimes 	}
511e47c2178SNate Lawson 	if (!KVM_READ(&vp->v_tag, &tagptr, sizeof tagptr) ||
512e47c2178SNate Lawson 	    !KVM_READ(tagptr, tagstr, sizeof tagstr)) {
513e47c2178SNate Lawson 		dprintf(stderr, "can't read v_tag at %p for pid %d\n",
514e47c2178SNate Lawson 		    (void *)vp, Pid);
515e47c2178SNate Lawson 		return;
516e47c2178SNate Lawson 	}
517e47c2178SNate Lawson 	tagstr[sizeof(tagstr) - 1] = '\0';
518e47c2178SNate Lawson 	if (vn.v_type == VNON)
5199b50d902SRodney W. Grimes 		badtype = "none";
5209b50d902SRodney W. Grimes 	else if (vn.v_type == VBAD)
5219b50d902SRodney W. Grimes 		badtype = "bad";
522e47c2178SNate Lawson 	else {
523e47c2178SNate Lawson 		if (!strcmp("ufs", tagstr)) {
5249b50d902SRodney W. Grimes 			if (!ufs_filestat(&vn, &fst))
5259b50d902SRodney W. Grimes 				badtype = "error";
526e47c2178SNate Lawson 		} else if (!strcmp("devfs", tagstr)) {
527fca066feSBrian Feldman 			if (!devfs_filestat(&vn, &fst))
528fca066feSBrian Feldman 				badtype = "error";
529e47c2178SNate Lawson 		} else if (!strcmp("nfs", tagstr)) {
5309b50d902SRodney W. Grimes 			if (!nfs_filestat(&vn, &fst))
5319b50d902SRodney W. Grimes 				badtype = "error";
532e47c2178SNate Lawson 		} else if (!strcmp("msdosfs", tagstr)) {
53306e88916SBrian Feldman 			if (!msdosfs_filestat(&vn, &fst))
53406e88916SBrian Feldman 				badtype = "error";
535e47c2178SNate Lawson 		} else if (!strcmp("isofs", tagstr)) {
53606e88916SBrian Feldman 			if (!isofs_filestat(&vn, &fst))
53706e88916SBrian Feldman 				badtype = "error";
538eec1c21fSUlf Lilleengen #ifdef ZFS
539eec1c21fSUlf Lilleengen 		} else if (!strcmp("zfs", tagstr)) {
540eec1c21fSUlf Lilleengen 			if (!zfs_filestat(&vn, &fst))
541eec1c21fSUlf Lilleengen 				badtype = "error";
542eec1c21fSUlf Lilleengen #endif
543e47c2178SNate Lawson 		} else {
544e47c2178SNate Lawson 			static char unknown[32];
545e47c2178SNate Lawson 			snprintf(unknown, sizeof unknown, "?(%s)", tagstr);
54627d57ea9SDavid Malone 			badtype = unknown;
5479b50d902SRodney W. Grimes 		}
5489b50d902SRodney W. Grimes 	}
5499b50d902SRodney W. Grimes 	if (checkfile) {
5509b50d902SRodney W. Grimes 		int fsmatch = 0;
55127d57ea9SDavid Malone 		DEVS *d;
5529b50d902SRodney W. Grimes 
5539b50d902SRodney W. Grimes 		if (badtype)
5549b50d902SRodney W. Grimes 			return;
5559b50d902SRodney W. Grimes 		for (d = devs; d != NULL; d = d->next)
5569b50d902SRodney W. Grimes 			if (d->fsid == fst.fsid) {
5579b50d902SRodney W. Grimes 				fsmatch = 1;
5589b50d902SRodney W. Grimes 				if (d->ino == fst.fileid) {
5599b50d902SRodney W. Grimes 					filename = d->name;
5609b50d902SRodney W. Grimes 					break;
5619b50d902SRodney W. Grimes 				}
5629b50d902SRodney W. Grimes 			}
5639b50d902SRodney W. Grimes 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
5649b50d902SRodney W. Grimes 			return;
5659b50d902SRodney W. Grimes 	}
5669b50d902SRodney W. Grimes 	PREFIX(i);
5679b50d902SRodney W. Grimes 	if (badtype) {
5689b50d902SRodney W. Grimes 		(void)printf(" -         -  %10s    -\n", badtype);
5699b50d902SRodney W. Grimes 		return;
5709b50d902SRodney W. Grimes 	}
5719b50d902SRodney W. Grimes 	if (nflg)
5729b50d902SRodney W. Grimes 		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
5739b50d902SRodney W. Grimes 	else
5749b50d902SRodney W. Grimes 		(void)printf(" %-8s", getmnton(vn.v_mount));
5759b50d902SRodney W. Grimes 	if (nflg)
5769b50d902SRodney W. Grimes 		(void)sprintf(mode, "%o", fst.mode);
5779b50d902SRodney W. Grimes 	else
5789b50d902SRodney W. Grimes 		strmode(fst.mode, mode);
57922694ebaSBruce Evans 	(void)printf(" %6ld %10s", fst.fileid, mode);
5809b50d902SRodney W. Grimes 	switch (vn.v_type) {
5819b50d902SRodney W. Grimes 	case VBLK:
5829b50d902SRodney W. Grimes 	case VCHR: {
5839b50d902SRodney W. Grimes 		char *name;
5849b50d902SRodney W. Grimes 
585a4cdc045SChristian S.J. Peron 		name = kdevtoname(vn.v_rdev);
586a4cdc045SChristian S.J. Peron 		if (nflg || !name)
5879b50d902SRodney W. Grimes 			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
588a4cdc045SChristian S.J. Peron 		else {
5899b50d902SRodney W. Grimes 			printf(" %6s", name);
590a4cdc045SChristian S.J. Peron 			free(name);
591a4cdc045SChristian S.J. Peron 		}
5929b50d902SRodney W. Grimes 		break;
5939b50d902SRodney W. Grimes 	}
5949b50d902SRodney W. Grimes 	default:
595df8327e7SAlexander Langer 		printf(" %6lu", fst.size);
5969b50d902SRodney W. Grimes 	}
5979b50d902SRodney W. Grimes 	rw[0] = '\0';
5989b50d902SRodney W. Grimes 	if (flag & FREAD)
5999b50d902SRodney W. Grimes 		strcat(rw, "r");
6009b50d902SRodney W. Grimes 	if (flag & FWRITE)
6019b50d902SRodney W. Grimes 		strcat(rw, "w");
6029b50d902SRodney W. Grimes 	printf(" %2s", rw);
6039b50d902SRodney W. Grimes 	if (filename && !fsflg)
6049b50d902SRodney W. Grimes 		printf("  %s", filename);
6059b50d902SRodney W. Grimes 	putchar('\n');
6069b50d902SRodney W. Grimes }
6079b50d902SRodney W. Grimes 
6089b50d902SRodney W. Grimes int
6091e925017SDavid Malone ufs_filestat(struct vnode *vp, struct filestat *fsp)
6109b50d902SRodney W. Grimes {
6119b50d902SRodney W. Grimes 	struct inode inode;
6129b50d902SRodney W. Grimes 
6139b50d902SRodney W. Grimes 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
61422694ebaSBruce Evans 		dprintf(stderr, "can't read inode at %p for pid %d\n",
61522694ebaSBruce Evans 		    (void *)VTOI(vp), Pid);
6169b50d902SRodney W. Grimes 		return 0;
6179b50d902SRodney W. Grimes 	}
61838cd1ee7SBrian Feldman 	/*
619f3732fd1SPoul-Henning Kamp 	 * The st_dev from stat(2) is a dev_t. These kernel structures
620f3732fd1SPoul-Henning Kamp 	 * contain cdev pointers. We need to convert to dev_t to make
62138cd1ee7SBrian Feldman 	 * comparisons
62238cd1ee7SBrian Feldman 	 */
623b628b0dcSDavid Malone 	fsp->fsid = dev2udev(inode.i_dev);
6249b50d902SRodney W. Grimes 	fsp->fileid = (long)inode.i_number;
6259b50d902SRodney W. Grimes 	fsp->mode = (mode_t)inode.i_mode;
6269b50d902SRodney W. Grimes 	fsp->size = (u_long)inode.i_size;
6271c85e6a3SKirk McKusick #if should_be_but_is_hard
628ab1897e9SJohn-Mark Gurney 	/* XXX - need to load i_ump and i_din[12] from kernel memory */
629ab1897e9SJohn-Mark Gurney 	if (inode.i_ump->um_fstype == UFS1)
630ab1897e9SJohn-Mark Gurney 		fsp->rdev = inode.i_din1->di_rdev;
631ab1897e9SJohn-Mark Gurney 	else
632ab1897e9SJohn-Mark Gurney 		fsp->rdev = inode.i_din2->di_rdev;
6331c85e6a3SKirk McKusick #else
6341c85e6a3SKirk McKusick 	fsp->rdev = 0;
6351c85e6a3SKirk McKusick #endif
6369b50d902SRodney W. Grimes 
6379b50d902SRodney W. Grimes 	return 1;
6389b50d902SRodney W. Grimes }
6399b50d902SRodney W. Grimes 
6409b50d902SRodney W. Grimes int
6411e925017SDavid Malone devfs_filestat(struct vnode *vp, struct filestat *fsp)
642fca066feSBrian Feldman {
643fca066feSBrian Feldman 	struct devfs_dirent devfs_dirent;
644fca066feSBrian Feldman 	struct mount mount;
645fca066feSBrian Feldman 	struct vnode vnode;
646fca066feSBrian Feldman 
647fca066feSBrian Feldman 	if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) {
648fca066feSBrian Feldman 		dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n",
649fca066feSBrian Feldman 		    (void *)vp->v_data, Pid);
650fca066feSBrian Feldman 		return 0;
651fca066feSBrian Feldman 	}
652fca066feSBrian Feldman 	if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) {
653fca066feSBrian Feldman 		dprintf(stderr, "can't read mount at %p for pid %d\n",
654fca066feSBrian Feldman 		    (void *)vp->v_mount, Pid);
655fca066feSBrian Feldman 		return 0;
656fca066feSBrian Feldman 	}
657fca066feSBrian Feldman 	if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) {
658fca066feSBrian Feldman 		dprintf(stderr, "can't read vnode at %p for pid %d\n",
659fca066feSBrian Feldman 		    (void *)devfs_dirent.de_vnode, Pid);
660fca066feSBrian Feldman 		return 0;
661fca066feSBrian Feldman 	}
662fca066feSBrian Feldman 	fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0];
663fca066feSBrian Feldman 	fsp->fileid = devfs_dirent.de_inode;
664fca066feSBrian Feldman 	fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR;
665fca066feSBrian Feldman 	fsp->size = 0;
666cf56713aSJohn-Mark Gurney 	fsp->rdev = dev2udev(vnode.v_rdev);
667fca066feSBrian Feldman 
668fca066feSBrian Feldman 	return 1;
669fca066feSBrian Feldman }
670fca066feSBrian Feldman 
671fca066feSBrian Feldman int
6721e925017SDavid Malone nfs_filestat(struct vnode *vp, struct filestat *fsp)
6739b50d902SRodney W. Grimes {
6749b50d902SRodney W. Grimes 	struct nfsnode nfsnode;
67527d57ea9SDavid Malone 	mode_t mode;
6769b50d902SRodney W. Grimes 
6779b50d902SRodney W. Grimes 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
67822694ebaSBruce Evans 		dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
67922694ebaSBruce Evans 		    (void *)VTONFS(vp), Pid);
6809b50d902SRodney W. Grimes 		return 0;
6819b50d902SRodney W. Grimes 	}
6829b50d902SRodney W. Grimes 	fsp->fsid = nfsnode.n_vattr.va_fsid;
6839b50d902SRodney W. Grimes 	fsp->fileid = nfsnode.n_vattr.va_fileid;
6849b50d902SRodney W. Grimes 	fsp->size = nfsnode.n_size;
6859b50d902SRodney W. Grimes 	fsp->rdev = nfsnode.n_vattr.va_rdev;
6869b50d902SRodney W. Grimes 	mode = (mode_t)nfsnode.n_vattr.va_mode;
6879b50d902SRodney W. Grimes 	switch (vp->v_type) {
6889b50d902SRodney W. Grimes 	case VREG:
6899b50d902SRodney W. Grimes 		mode |= S_IFREG;
6909b50d902SRodney W. Grimes 		break;
6919b50d902SRodney W. Grimes 	case VDIR:
6929b50d902SRodney W. Grimes 		mode |= S_IFDIR;
6939b50d902SRodney W. Grimes 		break;
6949b50d902SRodney W. Grimes 	case VBLK:
6959b50d902SRodney W. Grimes 		mode |= S_IFBLK;
6969b50d902SRodney W. Grimes 		break;
6979b50d902SRodney W. Grimes 	case VCHR:
6989b50d902SRodney W. Grimes 		mode |= S_IFCHR;
6999b50d902SRodney W. Grimes 		break;
7009b50d902SRodney W. Grimes 	case VLNK:
7019b50d902SRodney W. Grimes 		mode |= S_IFLNK;
7029b50d902SRodney W. Grimes 		break;
7039b50d902SRodney W. Grimes 	case VSOCK:
7049b50d902SRodney W. Grimes 		mode |= S_IFSOCK;
7059b50d902SRodney W. Grimes 		break;
7069b50d902SRodney W. Grimes 	case VFIFO:
7079b50d902SRodney W. Grimes 		mode |= S_IFIFO;
7089b50d902SRodney W. Grimes 		break;
7090b7a57ddSRuslan Ermilov 	case VNON:
7100b7a57ddSRuslan Ermilov 	case VBAD:
71182be0a5aSTor Egge 	case VMARKER:
7120b7a57ddSRuslan Ermilov 		return 0;
7139b50d902SRodney W. Grimes 	};
7149b50d902SRodney W. Grimes 	fsp->mode = mode;
7159b50d902SRodney W. Grimes 
7169b50d902SRodney W. Grimes 	return 1;
7179b50d902SRodney W. Grimes }
7189b50d902SRodney W. Grimes 
7199b50d902SRodney W. Grimes 
7209b50d902SRodney W. Grimes char *
7211e925017SDavid Malone getmnton(struct mount *m)
7229b50d902SRodney W. Grimes {
7239b50d902SRodney W. Grimes 	static struct mount mount;
7249b50d902SRodney W. Grimes 	static struct mtab {
7259b50d902SRodney W. Grimes 		struct mtab *next;
7269b50d902SRodney W. Grimes 		struct mount *m;
7279b50d902SRodney W. Grimes 		char mntonname[MNAMELEN];
7289b50d902SRodney W. Grimes 	} *mhead = NULL;
72927d57ea9SDavid Malone 	struct mtab *mt;
7309b50d902SRodney W. Grimes 
7319b50d902SRodney W. Grimes 	for (mt = mhead; mt != NULL; mt = mt->next)
7329b50d902SRodney W. Grimes 		if (m == mt->m)
7339b50d902SRodney W. Grimes 			return (mt->mntonname);
7349b50d902SRodney W. Grimes 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
73522694ebaSBruce Evans 		warnx("can't read mount table at %p", (void *)m);
7369b50d902SRodney W. Grimes 		return (NULL);
7379b50d902SRodney W. Grimes 	}
738c1e65942SPhilippe Charnier 	if ((mt = malloc(sizeof (struct mtab))) == NULL)
739c1e65942SPhilippe Charnier 		err(1, NULL);
7409b50d902SRodney W. Grimes 	mt->m = m;
7419b50d902SRodney W. Grimes 	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
7429b50d902SRodney W. Grimes 	mt->next = mhead;
7439b50d902SRodney W. Grimes 	mhead = mt;
7449b50d902SRodney W. Grimes 	return (mt->mntonname);
7459b50d902SRodney W. Grimes }
7469b50d902SRodney W. Grimes 
7479b50d902SRodney W. Grimes void
7481e925017SDavid Malone pipetrans(struct pipe *pi, int i, int flag)
7491c17fc99SPeter Wemm {
7501c17fc99SPeter Wemm 	struct pipe pip;
7511c17fc99SPeter Wemm 	char rw[3];
7521c17fc99SPeter Wemm 
7531c17fc99SPeter Wemm 	PREFIX(i);
7541c17fc99SPeter Wemm 
7551c17fc99SPeter Wemm 	/* fill in socket */
7561c17fc99SPeter Wemm 	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
75722694ebaSBruce Evans 		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
7581c17fc99SPeter Wemm 		goto bad;
7591c17fc99SPeter Wemm 	}
7601c17fc99SPeter Wemm 
761df94d4d2SMatt Jacob 	printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
7621c17fc99SPeter Wemm 	printf(" %6d", (int)pip.pipe_buffer.cnt);
7631c17fc99SPeter Wemm 	rw[0] = '\0';
7641c17fc99SPeter Wemm 	if (flag & FREAD)
7651c17fc99SPeter Wemm 		strcat(rw, "r");
7661c17fc99SPeter Wemm 	if (flag & FWRITE)
7671c17fc99SPeter Wemm 		strcat(rw, "w");
7681c17fc99SPeter Wemm 	printf(" %2s", rw);
7691c17fc99SPeter Wemm 	putchar('\n');
7701c17fc99SPeter Wemm 	return;
7711c17fc99SPeter Wemm 
7721c17fc99SPeter Wemm bad:
7731c17fc99SPeter Wemm 	printf("* error\n");
7741c17fc99SPeter Wemm }
7751c17fc99SPeter Wemm 
7761c17fc99SPeter Wemm void
7771e925017SDavid Malone socktrans(struct socket *sock, int i)
7789b50d902SRodney W. Grimes {
77927d57ea9SDavid Malone 	static const char *stypename[] = {
7809b50d902SRodney W. Grimes 		"unused",	/* 0 */
7819b50d902SRodney W. Grimes 		"stream", 	/* 1 */
7829b50d902SRodney W. Grimes 		"dgram",	/* 2 */
7839b50d902SRodney W. Grimes 		"raw",		/* 3 */
7849b50d902SRodney W. Grimes 		"rdm",		/* 4 */
7859b50d902SRodney W. Grimes 		"seqpak"	/* 5 */
7869b50d902SRodney W. Grimes 	};
7879b50d902SRodney W. Grimes #define	STYPEMAX 5
7889b50d902SRodney W. Grimes 	struct socket	so;
7899b50d902SRodney W. Grimes 	struct protosw	proto;
7909b50d902SRodney W. Grimes 	struct domain	dom;
7919b50d902SRodney W. Grimes 	struct inpcb	inpcb;
7929b50d902SRodney W. Grimes 	struct unpcb	unpcb;
7939b50d902SRodney W. Grimes 	int len;
79427d57ea9SDavid Malone 	char dname[32];
7959b50d902SRodney W. Grimes 
7969b50d902SRodney W. Grimes 	PREFIX(i);
7979b50d902SRodney W. Grimes 
7989b50d902SRodney W. Grimes 	/* fill in socket */
7999b50d902SRodney W. Grimes 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
80022694ebaSBruce Evans 		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
8019b50d902SRodney W. Grimes 		goto bad;
8029b50d902SRodney W. Grimes 	}
8039b50d902SRodney W. Grimes 
8049b50d902SRodney W. Grimes 	/* fill in protosw entry */
8059b50d902SRodney W. Grimes 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
80622694ebaSBruce Evans 		dprintf(stderr, "can't read protosw at %p",
80722694ebaSBruce Evans 		    (void *)so.so_proto);
8089b50d902SRodney W. Grimes 		goto bad;
8099b50d902SRodney W. Grimes 	}
8109b50d902SRodney W. Grimes 
8119b50d902SRodney W. Grimes 	/* fill in domain */
8129b50d902SRodney W. Grimes 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
81322694ebaSBruce Evans 		dprintf(stderr, "can't read domain at %p\n",
81422694ebaSBruce Evans 		    (void *)proto.pr_domain);
8159b50d902SRodney W. Grimes 		goto bad;
8169b50d902SRodney W. Grimes 	}
8179b50d902SRodney W. Grimes 
8189b50d902SRodney W. Grimes 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
8199b50d902SRodney W. Grimes 	    sizeof(dname) - 1)) < 0) {
82022694ebaSBruce Evans 		dprintf(stderr, "can't read domain name at %p\n",
82122694ebaSBruce Evans 		    (void *)dom.dom_name);
8229b50d902SRodney W. Grimes 		dname[0] = '\0';
8239b50d902SRodney W. Grimes 	}
8249b50d902SRodney W. Grimes 	else
8259b50d902SRodney W. Grimes 		dname[len] = '\0';
8269b50d902SRodney W. Grimes 
8279b50d902SRodney W. Grimes 	if ((u_short)so.so_type > STYPEMAX)
8289b50d902SRodney W. Grimes 		printf("* %s ?%d", dname, so.so_type);
8299b50d902SRodney W. Grimes 	else
8309b50d902SRodney W. Grimes 		printf("* %s %s", dname, stypename[so.so_type]);
8319b50d902SRodney W. Grimes 
8329b50d902SRodney W. Grimes 	/*
8339b50d902SRodney W. Grimes 	 * protocol specific formatting
8349b50d902SRodney W. Grimes 	 *
8359b50d902SRodney W. Grimes 	 * Try to find interesting things to print.  For tcp, the interesting
8369b50d902SRodney W. Grimes 	 * thing is the address of the tcpcb, for udp and others, just the
8379b50d902SRodney W. Grimes 	 * inpcb (socket pcb).  For unix domain, its the address of the socket
8389b50d902SRodney W. Grimes 	 * pcb and the address of the connected pcb (if connected).  Otherwise
8399b50d902SRodney W. Grimes 	 * just print the protocol number and address of the socket itself.
8409b50d902SRodney W. Grimes 	 * The idea is not to duplicate netstat, but to make available enough
8419b50d902SRodney W. Grimes 	 * information for further analysis.
8429b50d902SRodney W. Grimes 	 */
8439b50d902SRodney W. Grimes 	switch(dom.dom_family) {
8449b50d902SRodney W. Grimes 	case AF_INET:
845677e00c0SYoshinobu Inoue 	case AF_INET6:
8469b50d902SRodney W. Grimes 		getinetproto(proto.pr_protocol);
8479b50d902SRodney W. Grimes 		if (proto.pr_protocol == IPPROTO_TCP ) {
8489b50d902SRodney W. Grimes 			if (so.so_pcb) {
8499b50d902SRodney W. Grimes 				if (kvm_read(kd, (u_long)so.so_pcb,
8509b50d902SRodney W. Grimes 				    (char *)&inpcb, sizeof(struct inpcb))
8519b50d902SRodney W. Grimes 				    != sizeof(struct inpcb)) {
8529b50d902SRodney W. Grimes 					dprintf(stderr,
85322694ebaSBruce Evans 					    "can't read inpcb at %p\n",
85422694ebaSBruce Evans 					    (void *)so.so_pcb);
8559b50d902SRodney W. Grimes 					goto bad;
8569b50d902SRodney W. Grimes 				}
857df94d4d2SMatt Jacob 				printf(" %lx", (u_long)inpcb.inp_ppcb);
8589b50d902SRodney W. Grimes 			}
8599b50d902SRodney W. Grimes 		}
8609b50d902SRodney W. Grimes 		else if (so.so_pcb)
861df94d4d2SMatt Jacob 			printf(" %lx", (u_long)so.so_pcb);
8629b50d902SRodney W. Grimes 		break;
8639b50d902SRodney W. Grimes 	case AF_UNIX:
8649b50d902SRodney W. Grimes 		/* print address of pcb and connected pcb */
8659b50d902SRodney W. Grimes 		if (so.so_pcb) {
866df94d4d2SMatt Jacob 			printf(" %lx", (u_long)so.so_pcb);
8679b50d902SRodney W. Grimes 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
8689b50d902SRodney W. Grimes 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
86922694ebaSBruce Evans 				dprintf(stderr, "can't read unpcb at %p\n",
87022694ebaSBruce Evans 				    (void *)so.so_pcb);
8719b50d902SRodney W. Grimes 				goto bad;
8729b50d902SRodney W. Grimes 			}
8739b50d902SRodney W. Grimes 			if (unpcb.unp_conn) {
8749b50d902SRodney W. Grimes 				char shoconn[4], *cp;
8759b50d902SRodney W. Grimes 
8769b50d902SRodney W. Grimes 				cp = shoconn;
87797b88fbfSRobert Watson 				if (!(so.so_rcv.sb_state & SBS_CANTRCVMORE))
8789b50d902SRodney W. Grimes 					*cp++ = '<';
8799b50d902SRodney W. Grimes 				*cp++ = '-';
88097b88fbfSRobert Watson 				if (!(so.so_snd.sb_state & SBS_CANTSENDMORE))
8819b50d902SRodney W. Grimes 					*cp++ = '>';
8829b50d902SRodney W. Grimes 				*cp = '\0';
883df94d4d2SMatt Jacob 				printf(" %s %lx", shoconn,
884df94d4d2SMatt Jacob 				    (u_long)unpcb.unp_conn);
8859b50d902SRodney W. Grimes 			}
8869b50d902SRodney W. Grimes 		}
8879b50d902SRodney W. Grimes 		break;
8889b50d902SRodney W. Grimes 	default:
8899b50d902SRodney W. Grimes 		/* print protocol number and socket address */
890df94d4d2SMatt Jacob 		printf(" %d %lx", proto.pr_protocol, (u_long)sock);
8919b50d902SRodney W. Grimes 	}
8929b50d902SRodney W. Grimes 	printf("\n");
8939b50d902SRodney W. Grimes 	return;
8949b50d902SRodney W. Grimes bad:
8959b50d902SRodney W. Grimes 	printf("* error\n");
8969b50d902SRodney W. Grimes }
8979b50d902SRodney W. Grimes 
898bc093719SEd Schouten void
899bc093719SEd Schouten ptstrans(struct tty *tp, int i, int flag)
900bc093719SEd Schouten {
901bc093719SEd Schouten 	struct tty tty;
902bc093719SEd Schouten 	char *name;
903bc093719SEd Schouten 	char rw[3];
904bc093719SEd Schouten 	dev_t rdev;
905bc093719SEd Schouten 
906bc093719SEd Schouten 	PREFIX(i);
907bc093719SEd Schouten 
908bc093719SEd Schouten 	/* Obtain struct tty. */
909bc093719SEd Schouten 	if (!KVM_READ(tp, &tty, sizeof(struct tty))) {
910bc093719SEd Schouten 		dprintf(stderr, "can't read tty at %p\n", (void *)tp);
911bc093719SEd Schouten 		goto bad;
912bc093719SEd Schouten 	}
913bc093719SEd Schouten 
914bc093719SEd Schouten 	/* Figure out the device name. */
915bc093719SEd Schouten 	name = kdevtoname(tty.t_dev);
916bc093719SEd Schouten 	if (name == NULL) {
917bc093719SEd Schouten 		dprintf(stderr, "can't determine tty name at %p\n", (void *)tp);
918bc093719SEd Schouten 		goto bad;
919bc093719SEd Schouten 	}
920bc093719SEd Schouten 
921bc093719SEd Schouten 	rw[0] = '\0';
922bc093719SEd Schouten 	if (flag & FREAD)
923bc093719SEd Schouten 		strcat(rw, "r");
924bc093719SEd Schouten 	if (flag & FWRITE)
925bc093719SEd Schouten 		strcat(rw, "w");
926bc093719SEd Schouten 
927bc093719SEd Schouten 	printf("* pseudo-terminal master ");
928bc093719SEd Schouten 	if (nflg || !name) {
929bc093719SEd Schouten 		rdev = dev2udev(tty.t_dev);
930bc093719SEd Schouten 		printf("%10d,%-2d", major(rdev), minor(rdev));
931bc093719SEd Schouten 	} else {
932bc093719SEd Schouten 		printf("%10s", name);
933bc093719SEd Schouten 	}
934bc093719SEd Schouten 	printf(" %2s\n", rw);
935bc093719SEd Schouten 
936bc093719SEd Schouten 	free(name);
937bc093719SEd Schouten 
938bc093719SEd Schouten 	return;
939bc093719SEd Schouten bad:
940bc093719SEd Schouten 	printf("* error\n");
941bc093719SEd Schouten }
94238cd1ee7SBrian Feldman 
94338cd1ee7SBrian Feldman /*
944f3732fd1SPoul-Henning Kamp  * Read the cdev structure in the kernel in order to work out the
945f3732fd1SPoul-Henning Kamp  * associated dev_t
94638cd1ee7SBrian Feldman  */
947f3732fd1SPoul-Henning Kamp dev_t
9481e925017SDavid Malone dev2udev(struct cdev *dev)
94938cd1ee7SBrian Feldman {
950997591caSChristian S.J. Peron 	struct cdev_priv priv;
95138cd1ee7SBrian Feldman 
95205427aafSKonstantin Belousov 	if (KVM_READ(cdev2priv(dev), &priv, sizeof priv)) {
953997591caSChristian S.J. Peron 		return ((dev_t)priv.cdp_inode);
95438cd1ee7SBrian Feldman 	} else {
9551e925017SDavid Malone 		dprintf(stderr, "can't convert cdev *%p to a dev_t\n", dev);
95638cd1ee7SBrian Feldman 		return -1;
95738cd1ee7SBrian Feldman 	}
95838cd1ee7SBrian Feldman }
95938cd1ee7SBrian Feldman 
9609b50d902SRodney W. Grimes /*
9619b50d902SRodney W. Grimes  * getinetproto --
9629b50d902SRodney W. Grimes  *	print name of protocol number
9639b50d902SRodney W. Grimes  */
9649b50d902SRodney W. Grimes void
9651e925017SDavid Malone getinetproto(int number)
9669b50d902SRodney W. Grimes {
9670b7a57ddSRuslan Ermilov 	static int isopen;
96827d57ea9SDavid Malone 	struct protoent *pe;
9699b50d902SRodney W. Grimes 
9700b7a57ddSRuslan Ermilov 	if (!isopen)
9710b7a57ddSRuslan Ermilov 		setprotoent(++isopen);
9720b7a57ddSRuslan Ermilov 	if ((pe = getprotobynumber(number)) != NULL)
9730b7a57ddSRuslan Ermilov 		printf(" %s", pe->p_name);
9740b7a57ddSRuslan Ermilov 	else
9759b50d902SRodney W. Grimes 		printf(" %d", number);
9769b50d902SRodney W. Grimes }
9779b50d902SRodney W. Grimes 
9781c17fc99SPeter Wemm int
9791e925017SDavid Malone getfname(const char *filename)
9809b50d902SRodney W. Grimes {
9819b50d902SRodney W. Grimes 	struct stat statbuf;
9829b50d902SRodney W. Grimes 	DEVS *cur;
9839b50d902SRodney W. Grimes 
9849b50d902SRodney W. Grimes 	if (stat(filename, &statbuf)) {
985c1e65942SPhilippe Charnier 		warn("%s", filename);
9869b50d902SRodney W. Grimes 		return(0);
9879b50d902SRodney W. Grimes 	}
988c1e65942SPhilippe Charnier 	if ((cur = malloc(sizeof(DEVS))) == NULL)
989c1e65942SPhilippe Charnier 		err(1, NULL);
9909b50d902SRodney W. Grimes 	cur->next = devs;
9919b50d902SRodney W. Grimes 	devs = cur;
9929b50d902SRodney W. Grimes 
9939b50d902SRodney W. Grimes 	cur->ino = statbuf.st_ino;
994b628b0dcSDavid Malone 	cur->fsid = statbuf.st_dev;
9959b50d902SRodney W. Grimes 	cur->name = filename;
9969b50d902SRodney W. Grimes 	return(1);
9979b50d902SRodney W. Grimes }
9989b50d902SRodney W. Grimes 
999eec1c21fSUlf Lilleengen #ifdef ZFS
1000eec1c21fSUlf Lilleengen void *
1001eec1c21fSUlf Lilleengen getvnodedata(struct vnode *vp)
1002eec1c21fSUlf Lilleengen {
1003eec1c21fSUlf Lilleengen 	return (vp->v_data);
1004eec1c21fSUlf Lilleengen }
1005eec1c21fSUlf Lilleengen 
1006eec1c21fSUlf Lilleengen struct mount *
1007eec1c21fSUlf Lilleengen getvnodemount(struct vnode *vp)
1008eec1c21fSUlf Lilleengen {
1009eec1c21fSUlf Lilleengen 	return (vp->v_mount);
1010eec1c21fSUlf Lilleengen }
1011eec1c21fSUlf Lilleengen #endif
1012eec1c21fSUlf Lilleengen 
10139b50d902SRodney W. Grimes void
10141e925017SDavid Malone usage(void)
10159b50d902SRodney W. Grimes {
10169b50d902SRodney W. Grimes 	(void)fprintf(stderr,
1017f682f10cSRuslan Ermilov  "usage: fstat [-fmnv] [-M core] [-N system] [-p pid] [-u user] [file ...]\n");
10189b50d902SRodney W. Grimes 	exit(1);
10199b50d902SRodney W. Grimes }
1020