fstat.c revision 99112
11590Srgrimes/*-
21590Srgrimes * Copyright (c) 1988, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 3. All advertising materials mentioning features or use of this software
141590Srgrimes *    must display the following acknowledgement:
151590Srgrimes *	This product includes software developed by the University of
161590Srgrimes *	California, Berkeley and its contributors.
171590Srgrimes * 4. Neither the name of the University nor the names of its contributors
181590Srgrimes *    may be used to endorse or promote products derived from this software
191590Srgrimes *    without specific prior written permission.
201590Srgrimes *
211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311590Srgrimes * SUCH DAMAGE.
321590Srgrimes */
331590Srgrimes
341590Srgrimes#ifndef lint
3527272Scharnierstatic const char copyright[] =
361590Srgrimes"@(#) Copyright (c) 1988, 1993\n\
371590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381590Srgrimes#endif /* not lint */
391590Srgrimes
401590Srgrimes#ifndef lint
4127272Scharnier#if 0
4223693Speterstatic char sccsid[] = "@(#)fstat.c	8.3 (Berkeley) 5/2/95";
4327272Scharnier#endif
441590Srgrimes#endif /* not lint */
4599112Sobrien#include <sys/cdefs.h>
4699112Sobrien__FBSDID("$FreeBSD: head/usr.bin/fstat/fstat.c 99112 2002-06-30 05:25:07Z obrien $");
471590Srgrimes
481590Srgrimes#include <sys/param.h>
491590Srgrimes#include <sys/time.h>
501590Srgrimes#include <sys/proc.h>
511590Srgrimes#include <sys/user.h>
521590Srgrimes#include <sys/stat.h>
531590Srgrimes#include <sys/vnode.h>
541590Srgrimes#include <sys/socket.h>
551590Srgrimes#include <sys/socketvar.h>
561590Srgrimes#include <sys/domain.h>
571590Srgrimes#include <sys/protosw.h>
5836110Smarkm#include <sys/un.h>
591590Srgrimes#include <sys/unpcb.h>
601590Srgrimes#include <sys/sysctl.h>
611590Srgrimes#include <sys/filedesc.h>
627726Sdg#include <sys/queue.h>
6317808Speter#include <sys/pipe.h>
6477099Sphk#define	_KERNEL
6553133Sgreen#include <sys/conf.h>
661590Srgrimes#include <sys/file.h>
671590Srgrimes#include <ufs/ufs/quota.h>
681590Srgrimes#include <ufs/ufs/inode.h>
6988051Sgreen#include <fs/devfs/devfs.h>
7069564Salfred#include <sys/mount.h>
7155206Speter#undef _KERNEL
729336Sdfr#include <nfs/nfsproto.h>
731590Srgrimes#include <nfs/rpcv2.h>
7483653Speter#include <nfsclient/nfs.h>
7583653Speter#include <nfsclient/nfsnode.h>
761590Srgrimes
7758125Sgreen
7859029Sgreen#include <vm/vm.h>
7959029Sgreen#include <vm/vm_map.h>
8059029Sgreen#include <vm/vm_object.h>
8159029Sgreen
821590Srgrimes#include <net/route.h>
831590Srgrimes#include <netinet/in.h>
841590Srgrimes#include <netinet/in_systm.h>
851590Srgrimes#include <netinet/ip.h>
861590Srgrimes#include <netinet/in_pcb.h>
871590Srgrimes
881590Srgrimes#include <ctype.h>
8927272Scharnier#include <err.h>
9018570Sbde#include <fcntl.h>
911590Srgrimes#include <kvm.h>
9223693Speter#include <limits.h>
931590Srgrimes#include <nlist.h>
941590Srgrimes#include <paths.h>
951590Srgrimes#include <pwd.h>
961590Srgrimes#include <stdio.h>
971590Srgrimes#include <stdlib.h>
981590Srgrimes#include <string.h>
9923693Speter#include <unistd.h>
10048463Sru#include <netdb.h>
1011590Srgrimes
10258125Sgreen#include "fstat.h"
10358125Sgreen
1041590Srgrimes#define	TEXT	-1
1051590Srgrimes#define	CDIR	-2
1061590Srgrimes#define	RDIR	-3
1071590Srgrimes#define	TRACE	-4
10859029Sgreen#define	MMAP	-5
1091590Srgrimes
1101590SrgrimesDEVS *devs;
1111590Srgrimes
1121590Srgrimes#ifdef notdef
1131590Srgrimesstruct nlist nl[] = {
1141590Srgrimes	{ "" },
1151590Srgrimes};
1161590Srgrimes#endif
1171590Srgrimes
1181590Srgrimesint 	fsflg,	/* show files on same filesystem as file(s) argument */
1191590Srgrimes	pflg,	/* show files open by a particular pid */
1201590Srgrimes	uflg;	/* show files open by a particular (effective) user */
1211590Srgrimesint 	checkfile; /* true if restricting to particular files or filesystems */
1221590Srgrimesint	nflg;	/* (numerical) display f.s. and rdev as dev_t */
1231590Srgrimesint	vflg;	/* display errors in locating kernel data objects etc... */
12459029Sgreenint	mflg;	/* include memory-mapped files */
1251590Srgrimes
1261590Srgrimes
1271590Srgrimesstruct file **ofiles;	/* buffer of pointers to file structures */
1281590Srgrimesint maxfiles;
1291590Srgrimes#define ALLOC_OFILES(d)	\
1301590Srgrimes	if ((d) > maxfiles) { \
1311590Srgrimes		free(ofiles); \
1321590Srgrimes		ofiles = malloc((d) * sizeof(struct file *)); \
1331590Srgrimes		if (ofiles == NULL) { \
13427272Scharnier			err(1, NULL); \
1351590Srgrimes		} \
1361590Srgrimes		maxfiles = (d); \
1371590Srgrimes	}
1381590Srgrimes
13997946Sdeschar *memf, *nlistf;
1401590Srgrimeskvm_t *kd;
1411590Srgrimes
14297946Sdesstatic void fstat_kvm(int, int);
14397946Sdesstatic void fstat_sysctl(int, int);
14492920Simpvoid dofiles(struct kinfo_proc *kp);
14592920Simpvoid dommap(struct kinfo_proc *kp);
14692920Simpvoid vtrans(struct vnode *vp, int i, int flag);
14792920Simpint  ufs_filestat(struct vnode *vp, struct filestat *fsp);
14892920Simpint  nfs_filestat(struct vnode *vp, struct filestat *fsp);
14992920Simpint  devfs_filestat(struct vnode *vp, struct filestat *fsp);
15092920Simpchar *getmnton(struct mount *m);
15192920Simpvoid pipetrans(struct pipe *pi, int i, int flag);
15292920Simpvoid socktrans(struct socket *sock, int i);
15392920Simpvoid getinetproto(int number);
15493427Sdwmaloneint  getfname(const char *filename);
15592920Simpvoid usage(void);
1561590Srgrimes
15717808Speter
15817808Speterint
1591590Srgrimesmain(argc, argv)
1601590Srgrimes	int argc;
1611590Srgrimes	char **argv;
1621590Srgrimes{
16393427Sdwmalone	struct passwd *passwd;
1641590Srgrimes	int arg, ch, what;
1651590Srgrimes
1661590Srgrimes	arg = 0;
1671590Srgrimes	what = KERN_PROC_ALL;
1681590Srgrimes	nlistf = memf = NULL;
16959029Sgreen	while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1)
1701590Srgrimes		switch((char)ch) {
1711590Srgrimes		case 'f':
1721590Srgrimes			fsflg = 1;
1731590Srgrimes			break;
1741590Srgrimes		case 'M':
1751590Srgrimes			memf = optarg;
1761590Srgrimes			break;
1771590Srgrimes		case 'N':
1781590Srgrimes			nlistf = optarg;
1791590Srgrimes			break;
18059029Sgreen		case 'm':
18159029Sgreen			mflg = 1;
18259029Sgreen			break;
1831590Srgrimes		case 'n':
1841590Srgrimes			nflg = 1;
1851590Srgrimes			break;
1861590Srgrimes		case 'p':
1871590Srgrimes			if (pflg++)
1881590Srgrimes				usage();
1891590Srgrimes			if (!isdigit(*optarg)) {
19027311Scharnier				warnx("-p requires a process id");
1911590Srgrimes				usage();
1921590Srgrimes			}
1931590Srgrimes			what = KERN_PROC_PID;
1941590Srgrimes			arg = atoi(optarg);
1951590Srgrimes			break;
1961590Srgrimes		case 'u':
1971590Srgrimes			if (uflg++)
1981590Srgrimes				usage();
19927272Scharnier			if (!(passwd = getpwnam(optarg)))
20027272Scharnier				errx(1, "%s: unknown uid", optarg);
2011590Srgrimes			what = KERN_PROC_UID;
2021590Srgrimes			arg = passwd->pw_uid;
2031590Srgrimes			break;
2041590Srgrimes		case 'v':
2051590Srgrimes			vflg = 1;
2061590Srgrimes			break;
2071590Srgrimes		case '?':
2081590Srgrimes		default:
2091590Srgrimes			usage();
2101590Srgrimes		}
2111590Srgrimes
2121590Srgrimes	if (*(argv += optind)) {
2131590Srgrimes		for (; *argv; ++argv) {
2141590Srgrimes			if (getfname(*argv))
2151590Srgrimes				checkfile = 1;
2161590Srgrimes		}
2171590Srgrimes		if (!checkfile)	/* file(s) specified, but none accessable */
2181590Srgrimes			exit(1);
2191590Srgrimes	}
2201590Srgrimes
2218874Srgrimes	if (fsflg && !checkfile) {
2221590Srgrimes		/* -f with no files means use wd */
2231590Srgrimes		if (getfname(".") == 0)
2241590Srgrimes			exit(1);
2251590Srgrimes		checkfile = 1;
2261590Srgrimes	}
2271590Srgrimes
22897946Sdes	if (memf != NULL)
22997946Sdes		fstat_kvm(what, arg);
23097946Sdes	else
23197946Sdes		fstat_sysctl(what, arg);
23297946Sdes	exit(0);
23397946Sdes}
23497946Sdes
23597946Sdesstatic void
23697946Sdesprint_header(void)
23797946Sdes{
23897946Sdes
23997946Sdes	if (nflg)
24097946Sdes		printf("%s",
24197946Sdes"USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
24297946Sdes	else
24397946Sdes		printf("%s",
24497946Sdes"USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
24597946Sdes	if (checkfile && fsflg == 0)
24697946Sdes		printf(" NAME\n");
24797946Sdes	else
24897946Sdes		putchar('\n');
24997946Sdes}
25097946Sdes
25197946Sdesstatic void
25297946Sdesfstat_kvm(int what, int arg)
25397946Sdes{
25497946Sdes	struct kinfo_proc *p, *plast;
25597946Sdes	char buf[_POSIX2_LINE_MAX];
25697946Sdes	int cnt;
25797946Sdes
25897946Sdes	ALLOC_OFILES(256);	/* reserve space for file pointers */
25997946Sdes
2601590Srgrimes	/*
2611590Srgrimes	 * Discard setgid privileges if not the running kernel so that bad
2621590Srgrimes	 * guys can't print interesting stuff from kernel memory.
2631590Srgrimes	 */
2641590Srgrimes	if (nlistf != NULL || memf != NULL)
2651590Srgrimes		setgid(getgid());
2661590Srgrimes
26727272Scharnier	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
26827272Scharnier		errx(1, "%s", buf);
26982664Sru	setgid(getgid());
2701590Srgrimes#ifdef notdef
27127272Scharnier	if (kvm_nlist(kd, nl) != 0)
27227272Scharnier		errx(1, "no namelist: %s", kvm_geterr(kd));
2731590Srgrimes#endif
27427272Scharnier	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
27527272Scharnier		errx(1, "%s", kvm_geterr(kd));
2761590Srgrimes	for (plast = &p[cnt]; p < plast; ++p) {
27769896Smckusick		if (p->ki_stat == SZOMB)
2781590Srgrimes			continue;
2791590Srgrimes		dofiles(p);
28059029Sgreen		if (mflg)
28159029Sgreen			dommap(p);
2821590Srgrimes	}
2831590Srgrimes}
2841590Srgrimes
28597946Sdesstatic void
28697946Sdesfstat_sysctl(int what, int arg)
28797946Sdes{
28897946Sdes
28997946Sdes	/* not yet implemented */
29097946Sdes	fstat_kvm(what, arg);
29197946Sdes}
29297946Sdes
29393427Sdwmaloneconst char	*Uname, *Comm;
2941590Srgrimesint	Pid;
2951590Srgrimes
2961590Srgrimes#define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
2971590Srgrimes	switch(i) { \
2981590Srgrimes	case TEXT: \
2991590Srgrimes		printf(" text"); \
3001590Srgrimes		break; \
3011590Srgrimes	case CDIR: \
3021590Srgrimes		printf("   wd"); \
3031590Srgrimes		break; \
3041590Srgrimes	case RDIR: \
3051590Srgrimes		printf(" root"); \
3061590Srgrimes		break; \
3071590Srgrimes	case TRACE: \
3081590Srgrimes		printf("   tr"); \
3091590Srgrimes		break; \
31059029Sgreen	case MMAP: \
31159029Sgreen		printf(" mmap"); \
31259029Sgreen		break; \
3131590Srgrimes	default: \
3141590Srgrimes		printf(" %4d", i); \
3151590Srgrimes		break; \
3161590Srgrimes	}
3171590Srgrimes
3181590Srgrimes/*
3191590Srgrimes * print open files attributed to this process
3201590Srgrimes */
3211590Srgrimesvoid
3221590Srgrimesdofiles(kp)
3231590Srgrimes	struct kinfo_proc *kp;
3241590Srgrimes{
32527272Scharnier	int i;
3261590Srgrimes	struct file file;
3271590Srgrimes	struct filedesc0 filed0;
3281590Srgrimes#define	filed	filed0.fd_fd
3291590Srgrimes
33069896Smckusick	Uname = user_from_uid(kp->ki_uid, 0);
33169896Smckusick	Pid = kp->ki_pid;
33269896Smckusick	Comm = kp->ki_comm;
3331590Srgrimes
33469896Smckusick	if (kp->ki_fd == NULL)
3351590Srgrimes		return;
33669896Smckusick	if (!KVM_READ(kp->ki_fd, &filed0, sizeof (filed0))) {
33737453Sbde		dprintf(stderr, "can't read filedesc at %p for pid %d\n",
33869896Smckusick		    (void *)kp->ki_fd, Pid);
3391590Srgrimes		return;
3401590Srgrimes	}
3411590Srgrimes	/*
3421590Srgrimes	 * root directory vnode, if one
3431590Srgrimes	 */
3441590Srgrimes	if (filed.fd_rdir)
3451590Srgrimes		vtrans(filed.fd_rdir, RDIR, FREAD);
3461590Srgrimes	/*
3471590Srgrimes	 * current working directory vnode
3481590Srgrimes	 */
3491590Srgrimes	vtrans(filed.fd_cdir, CDIR, FREAD);
3501590Srgrimes	/*
3511590Srgrimes	 * ktrace vnode, if one
3521590Srgrimes	 */
35369896Smckusick	if (kp->ki_tracep)
35469896Smckusick		vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE);
3551590Srgrimes	/*
35617813Speter	 * text vnode, if one
35717813Speter	 */
35869896Smckusick	if (kp->ki_textvp)
35969896Smckusick		vtrans(kp->ki_textvp, TEXT, FREAD);
36017813Speter	/*
3611590Srgrimes	 * open files
3621590Srgrimes	 */
3631590Srgrimes#define FPSIZE	(sizeof (struct file *))
3641590Srgrimes	ALLOC_OFILES(filed.fd_lastfile+1);
3651590Srgrimes	if (filed.fd_nfiles > NDFILE) {
3661590Srgrimes		if (!KVM_READ(filed.fd_ofiles, ofiles,
3671590Srgrimes		    (filed.fd_lastfile+1) * FPSIZE)) {
3681590Srgrimes			dprintf(stderr,
36937453Sbde			    "can't read file structures at %p for pid %d\n",
37037453Sbde			    (void *)filed.fd_ofiles, Pid);
3711590Srgrimes			return;
3721590Srgrimes		}
3731590Srgrimes	} else
3741590Srgrimes		bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
3751590Srgrimes	for (i = 0; i <= filed.fd_lastfile; i++) {
3761590Srgrimes		if (ofiles[i] == NULL)
3771590Srgrimes			continue;
3781590Srgrimes		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
37937453Sbde			dprintf(stderr, "can't read file %d at %p for pid %d\n",
38037453Sbde			    i, (void *)ofiles[i], Pid);
3811590Srgrimes			continue;
3821590Srgrimes		}
3831590Srgrimes		if (file.f_type == DTYPE_VNODE)
3841590Srgrimes			vtrans((struct vnode *)file.f_data, i, file.f_flag);
3851590Srgrimes		else if (file.f_type == DTYPE_SOCKET) {
3861590Srgrimes			if (checkfile == 0)
3871590Srgrimes				socktrans((struct socket *)file.f_data, i);
3881590Srgrimes		}
38917808Speter#ifdef DTYPE_PIPE
39017808Speter		else if (file.f_type == DTYPE_PIPE) {
39117808Speter			if (checkfile == 0)
39217808Speter				pipetrans((struct pipe *)file.f_data, i,
39378401Sroam				    file.f_flag);
39417808Speter		}
39517808Speter#endif
39678401Sroam#ifdef DTYPE_FIFO
39778401Sroam		else if (file.f_type == DTYPE_FIFO) {
39878401Sroam			if (checkfile == 0)
39978401Sroam				vtrans((struct vnode *)file.f_data, i,
40078401Sroam				    file.f_flag);
40178401Sroam		}
40278401Sroam#endif
4031590Srgrimes		else {
4048874Srgrimes			dprintf(stderr,
40578401Sroam			    "unknown file type %d for file %d of pid %d\n",
40678401Sroam			    file.f_type, i, Pid);
4071590Srgrimes		}
4081590Srgrimes	}
4091590Srgrimes}
4101590Srgrimes
4111590Srgrimesvoid
41259029Sgreendommap(kp)
41359029Sgreen	struct kinfo_proc *kp;
41459029Sgreen{
41569896Smckusick	vm_map_t map;
41659029Sgreen	struct vmspace vmspace;
41759029Sgreen	struct vm_map_entry entry;
41859029Sgreen	vm_map_entry_t entryp;
41959029Sgreen	struct vm_object object;
42059029Sgreen	vm_object_t objp;
42159029Sgreen	int prot, fflags;
42259029Sgreen
42369896Smckusick	if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) {
42469896Smckusick		dprintf(stderr,
42569896Smckusick		    "can't read vmspace at %p for pid %d\n",
42669896Smckusick		    (void *)kp->ki_vmspace, Pid);
42759029Sgreen		return;
42859029Sgreen	}
42959029Sgreen	map = &vmspace.vm_map;
43059029Sgreen
43172527Siedowse	for (entryp = map->header.next;
43272527Siedowse	    entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) {
43359029Sgreen		if (!KVM_READ(entryp, &entry, sizeof(entry))) {
43459029Sgreen			dprintf(stderr,
43559029Sgreen			    "can't read vm_map_entry at %p for pid %d\n",
43659029Sgreen			    (void *)entryp, Pid);
43759029Sgreen			return;
43859029Sgreen		}
43959029Sgreen
44059029Sgreen		if (entry.eflags & MAP_ENTRY_IS_SUB_MAP)
44159029Sgreen			continue;
44259029Sgreen
44359029Sgreen		if ((objp = entry.object.vm_object) == NULL)
44459029Sgreen			continue;
44559029Sgreen
44659029Sgreen		for (; objp; objp = object.backing_object) {
44759029Sgreen			if (!KVM_READ(objp, &object, sizeof(object))) {
44859029Sgreen				dprintf(stderr,
44959029Sgreen				    "can't read vm_object at %p for pid %d\n",
45059029Sgreen				    (void *)objp, Pid);
45159029Sgreen				return;
45259029Sgreen			}
45359029Sgreen		}
45459029Sgreen
45559029Sgreen		prot = entry.protection;
45659029Sgreen		fflags = (prot & VM_PROT_READ ? FREAD : 0) |
45759029Sgreen		    (prot & VM_PROT_WRITE ? FWRITE : 0);
45859029Sgreen
45959029Sgreen		switch (object.type) {
46059029Sgreen		case OBJT_VNODE:
46159029Sgreen			vtrans((struct vnode *)object.handle, MMAP, fflags);
46259029Sgreen			break;
46359029Sgreen		default:
46459029Sgreen			break;
46559029Sgreen		}
46659029Sgreen	}
46759029Sgreen}
46859029Sgreen
46959029Sgreenvoid
4701590Srgrimesvtrans(vp, i, flag)
4711590Srgrimes	struct vnode *vp;
4721590Srgrimes	int i;
4731590Srgrimes	int flag;
4741590Srgrimes{
4751590Srgrimes	struct vnode vn;
4761590Srgrimes	struct filestat fst;
4771590Srgrimes	char rw[3], mode[15];
47893427Sdwmalone	const char *badtype, *filename;
4791590Srgrimes
4801590Srgrimes	filename = badtype = NULL;
4811590Srgrimes	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
48237453Sbde		dprintf(stderr, "can't read vnode at %p for pid %d\n",
48337453Sbde		    (void *)vp, Pid);
4841590Srgrimes		return;
4851590Srgrimes	}
4861590Srgrimes	if (vn.v_type == VNON || vn.v_tag == VT_NON)
4871590Srgrimes		badtype = "none";
4881590Srgrimes	else if (vn.v_type == VBAD)
4891590Srgrimes		badtype = "bad";
4901590Srgrimes	else
4911590Srgrimes		switch (vn.v_tag) {
4921590Srgrimes		case VT_UFS:
4931590Srgrimes			if (!ufs_filestat(&vn, &fst))
4941590Srgrimes				badtype = "error";
4951590Srgrimes			break;
49688051Sgreen
49788051Sgreen		case VT_DEVFS:
49888051Sgreen			if (!devfs_filestat(&vn, &fst))
49988051Sgreen				badtype = "error";
50088051Sgreen			break;
50188051Sgreen
5021590Srgrimes		case VT_NFS:
5031590Srgrimes			if (!nfs_filestat(&vn, &fst))
5041590Srgrimes				badtype = "error";
5051590Srgrimes			break;
50658125Sgreen
50758125Sgreen		case VT_MSDOSFS:
50858125Sgreen			if (!msdosfs_filestat(&vn, &fst))
50958125Sgreen				badtype = "error";
51058125Sgreen			break;
51158125Sgreen
51258125Sgreen		case VT_ISOFS:
51358125Sgreen			if (!isofs_filestat(&vn, &fst))
51458125Sgreen				badtype = "error";
51558125Sgreen			break;
51658125Sgreen
5171590Srgrimes		default: {
5181590Srgrimes			static char unknown[10];
51993427Sdwmalone			sprintf(unknown, "?(%x)", vn.v_tag);
52093427Sdwmalone			badtype = unknown;
5211590Srgrimes			break;;
5221590Srgrimes		}
5231590Srgrimes	}
5241590Srgrimes	if (checkfile) {
5251590Srgrimes		int fsmatch = 0;
52693427Sdwmalone		DEVS *d;
5271590Srgrimes
5281590Srgrimes		if (badtype)
5291590Srgrimes			return;
5301590Srgrimes		for (d = devs; d != NULL; d = d->next)
5311590Srgrimes			if (d->fsid == fst.fsid) {
5321590Srgrimes				fsmatch = 1;
5331590Srgrimes				if (d->ino == fst.fileid) {
5341590Srgrimes					filename = d->name;
5351590Srgrimes					break;
5361590Srgrimes				}
5371590Srgrimes			}
5381590Srgrimes		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
5391590Srgrimes			return;
5401590Srgrimes	}
5411590Srgrimes	PREFIX(i);
5421590Srgrimes	if (badtype) {
5431590Srgrimes		(void)printf(" -         -  %10s    -\n", badtype);
5441590Srgrimes		return;
5451590Srgrimes	}
5461590Srgrimes	if (nflg)
5471590Srgrimes		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
5481590Srgrimes	else
5491590Srgrimes		(void)printf(" %-8s", getmnton(vn.v_mount));
5501590Srgrimes	if (nflg)
5511590Srgrimes		(void)sprintf(mode, "%o", fst.mode);
5521590Srgrimes	else
5531590Srgrimes		strmode(fst.mode, mode);
55437453Sbde	(void)printf(" %6ld %10s", fst.fileid, mode);
5551590Srgrimes	switch (vn.v_type) {
5561590Srgrimes	case VBLK:
5571590Srgrimes	case VCHR: {
5581590Srgrimes		char *name;
5591590Srgrimes
5608874Srgrimes		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
5611590Srgrimes		    S_IFCHR : S_IFBLK)) == NULL))
5621590Srgrimes			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
5631590Srgrimes		else
5641590Srgrimes			printf(" %6s", name);
5651590Srgrimes		break;
5661590Srgrimes	}
5671590Srgrimes	default:
56828948Salex		printf(" %6lu", fst.size);
5691590Srgrimes	}
5701590Srgrimes	rw[0] = '\0';
5711590Srgrimes	if (flag & FREAD)
5721590Srgrimes		strcat(rw, "r");
5731590Srgrimes	if (flag & FWRITE)
5741590Srgrimes		strcat(rw, "w");
5751590Srgrimes	printf(" %2s", rw);
5761590Srgrimes	if (filename && !fsflg)
5771590Srgrimes		printf("  %s", filename);
5781590Srgrimes	putchar('\n');
5791590Srgrimes}
5801590Srgrimes
5811590Srgrimesint
5821590Srgrimesufs_filestat(vp, fsp)
5831590Srgrimes	struct vnode *vp;
5841590Srgrimes	struct filestat *fsp;
5851590Srgrimes{
5861590Srgrimes	struct inode inode;
5871590Srgrimes
5881590Srgrimes	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
58937453Sbde		dprintf(stderr, "can't read inode at %p for pid %d\n",
59037453Sbde		    (void *)VTOI(vp), Pid);
5911590Srgrimes		return 0;
5921590Srgrimes	}
59353133Sgreen	/*
59453133Sgreen	 * The st_dev from stat(2) is a udev_t. These kernel structures
59553133Sgreen	 * contain dev_t structures. We need to convert to udev to make
59653133Sgreen	 * comparisons
59753133Sgreen	 */
59886100Sdwmalone	fsp->fsid = dev2udev(inode.i_dev);
5991590Srgrimes	fsp->fileid = (long)inode.i_number;
6001590Srgrimes	fsp->mode = (mode_t)inode.i_mode;
6011590Srgrimes	fsp->size = (u_long)inode.i_size;
60298542Smckusick#if should_be_but_is_hard
6031590Srgrimes	fsp->rdev = inode.i_rdev;
60498542Smckusick#else
60598542Smckusick	fsp->rdev = 0;
60698542Smckusick#endif
6071590Srgrimes
6081590Srgrimes	return 1;
6091590Srgrimes}
6101590Srgrimes
6111590Srgrimesint
61288051Sgreendevfs_filestat(vp, fsp)
61388051Sgreen	struct vnode *vp;
61488051Sgreen	struct filestat *fsp;
61588051Sgreen{
61688051Sgreen	struct devfs_dirent devfs_dirent;
61788051Sgreen	struct mount mount;
61888051Sgreen	struct vnode vnode;
61988051Sgreen
62088051Sgreen	if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) {
62188051Sgreen		dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n",
62288051Sgreen		    (void *)vp->v_data, Pid);
62388051Sgreen		return 0;
62488051Sgreen	}
62588051Sgreen	if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) {
62688051Sgreen		dprintf(stderr, "can't read mount at %p for pid %d\n",
62788051Sgreen		    (void *)vp->v_mount, Pid);
62888051Sgreen		return 0;
62988051Sgreen	}
63088051Sgreen	if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) {
63188051Sgreen		dprintf(stderr, "can't read vnode at %p for pid %d\n",
63288051Sgreen		    (void *)devfs_dirent.de_vnode, Pid);
63388051Sgreen		return 0;
63488051Sgreen	}
63588051Sgreen	fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0];
63688051Sgreen	fsp->fileid = devfs_dirent.de_inode;
63788051Sgreen	fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR;
63888051Sgreen	fsp->size = 0;
63988051Sgreen	fsp->rdev = dev2udev((dev_t)vnode.v_rdev);
64088051Sgreen
64188051Sgreen	return 1;
64288051Sgreen}
64388051Sgreen
64488051Sgreenint
6451590Srgrimesnfs_filestat(vp, fsp)
6461590Srgrimes	struct vnode *vp;
6471590Srgrimes	struct filestat *fsp;
6481590Srgrimes{
6491590Srgrimes	struct nfsnode nfsnode;
65093427Sdwmalone	mode_t mode;
6511590Srgrimes
6521590Srgrimes	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
65337453Sbde		dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
65437453Sbde		    (void *)VTONFS(vp), Pid);
6551590Srgrimes		return 0;
6561590Srgrimes	}
6571590Srgrimes	fsp->fsid = nfsnode.n_vattr.va_fsid;
6581590Srgrimes	fsp->fileid = nfsnode.n_vattr.va_fileid;
6591590Srgrimes	fsp->size = nfsnode.n_size;
6601590Srgrimes	fsp->rdev = nfsnode.n_vattr.va_rdev;
6611590Srgrimes	mode = (mode_t)nfsnode.n_vattr.va_mode;
6621590Srgrimes	switch (vp->v_type) {
6631590Srgrimes	case VREG:
6641590Srgrimes		mode |= S_IFREG;
6651590Srgrimes		break;
6661590Srgrimes	case VDIR:
6671590Srgrimes		mode |= S_IFDIR;
6681590Srgrimes		break;
6691590Srgrimes	case VBLK:
6701590Srgrimes		mode |= S_IFBLK;
6711590Srgrimes		break;
6721590Srgrimes	case VCHR:
6731590Srgrimes		mode |= S_IFCHR;
6741590Srgrimes		break;
6751590Srgrimes	case VLNK:
6761590Srgrimes		mode |= S_IFLNK;
6771590Srgrimes		break;
6781590Srgrimes	case VSOCK:
6791590Srgrimes		mode |= S_IFSOCK;
6801590Srgrimes		break;
6811590Srgrimes	case VFIFO:
6821590Srgrimes		mode |= S_IFIFO;
6831590Srgrimes		break;
68448463Sru	case VNON:
68548463Sru	case VBAD:
68648463Sru		return 0;
6871590Srgrimes	};
6881590Srgrimes	fsp->mode = mode;
6891590Srgrimes
6901590Srgrimes	return 1;
6911590Srgrimes}
6921590Srgrimes
6931590Srgrimes
6941590Srgrimeschar *
6951590Srgrimesgetmnton(m)
6961590Srgrimes	struct mount *m;
6971590Srgrimes{
6981590Srgrimes	static struct mount mount;
6991590Srgrimes	static struct mtab {
7001590Srgrimes		struct mtab *next;
7011590Srgrimes		struct mount *m;
7021590Srgrimes		char mntonname[MNAMELEN];
7031590Srgrimes	} *mhead = NULL;
70493427Sdwmalone	struct mtab *mt;
7051590Srgrimes
7061590Srgrimes	for (mt = mhead; mt != NULL; mt = mt->next)
7071590Srgrimes		if (m == mt->m)
7081590Srgrimes			return (mt->mntonname);
7091590Srgrimes	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
71037453Sbde		warnx("can't read mount table at %p", (void *)m);
7111590Srgrimes		return (NULL);
7121590Srgrimes	}
71327272Scharnier	if ((mt = malloc(sizeof (struct mtab))) == NULL)
71427272Scharnier		err(1, NULL);
7151590Srgrimes	mt->m = m;
7161590Srgrimes	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
7171590Srgrimes	mt->next = mhead;
7181590Srgrimes	mhead = mt;
7191590Srgrimes	return (mt->mntonname);
7201590Srgrimes}
7211590Srgrimes
7221590Srgrimesvoid
72317808Speterpipetrans(pi, i, flag)
72417808Speter	struct pipe *pi;
72517808Speter	int i;
72617808Speter	int flag;
72717808Speter{
72817808Speter	struct pipe pip;
72917808Speter	char rw[3];
73017808Speter
73117808Speter	PREFIX(i);
73217808Speter
73317808Speter	/* fill in socket */
73417808Speter	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
73537453Sbde		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
73617808Speter		goto bad;
73717808Speter	}
73817808Speter
73980355Smjacob	printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
74017808Speter	printf(" %6d", (int)pip.pipe_buffer.cnt);
74117808Speter	rw[0] = '\0';
74217808Speter	if (flag & FREAD)
74317808Speter		strcat(rw, "r");
74417808Speter	if (flag & FWRITE)
74517808Speter		strcat(rw, "w");
74617808Speter	printf(" %2s", rw);
74717808Speter	putchar('\n');
74817808Speter	return;
74917808Speter
75017808Speterbad:
75117808Speter	printf("* error\n");
75217808Speter}
75317808Speter
75417808Spetervoid
7551590Srgrimessocktrans(sock, i)
7561590Srgrimes	struct socket *sock;
7571590Srgrimes	int i;
7581590Srgrimes{
75993427Sdwmalone	static const char *stypename[] = {
7601590Srgrimes		"unused",	/* 0 */
7611590Srgrimes		"stream", 	/* 1 */
7621590Srgrimes		"dgram",	/* 2 */
7631590Srgrimes		"raw",		/* 3 */
7641590Srgrimes		"rdm",		/* 4 */
7651590Srgrimes		"seqpak"	/* 5 */
7661590Srgrimes	};
7671590Srgrimes#define	STYPEMAX 5
7681590Srgrimes	struct socket	so;
7691590Srgrimes	struct protosw	proto;
7701590Srgrimes	struct domain	dom;
7711590Srgrimes	struct inpcb	inpcb;
7721590Srgrimes	struct unpcb	unpcb;
7731590Srgrimes	int len;
77493427Sdwmalone	char dname[32];
7751590Srgrimes
7761590Srgrimes	PREFIX(i);
7771590Srgrimes
7781590Srgrimes	/* fill in socket */
7791590Srgrimes	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
78037453Sbde		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
7811590Srgrimes		goto bad;
7821590Srgrimes	}
7831590Srgrimes
7841590Srgrimes	/* fill in protosw entry */
7851590Srgrimes	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
78637453Sbde		dprintf(stderr, "can't read protosw at %p",
78737453Sbde		    (void *)so.so_proto);
7881590Srgrimes		goto bad;
7891590Srgrimes	}
7901590Srgrimes
7911590Srgrimes	/* fill in domain */
7921590Srgrimes	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
79337453Sbde		dprintf(stderr, "can't read domain at %p\n",
79437453Sbde		    (void *)proto.pr_domain);
7951590Srgrimes		goto bad;
7961590Srgrimes	}
7971590Srgrimes
7981590Srgrimes	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
7991590Srgrimes	    sizeof(dname) - 1)) < 0) {
80037453Sbde		dprintf(stderr, "can't read domain name at %p\n",
80137453Sbde		    (void *)dom.dom_name);
8021590Srgrimes		dname[0] = '\0';
8031590Srgrimes	}
8041590Srgrimes	else
8051590Srgrimes		dname[len] = '\0';
8061590Srgrimes
8071590Srgrimes	if ((u_short)so.so_type > STYPEMAX)
8081590Srgrimes		printf("* %s ?%d", dname, so.so_type);
8091590Srgrimes	else
8101590Srgrimes		printf("* %s %s", dname, stypename[so.so_type]);
8111590Srgrimes
8128874Srgrimes	/*
8131590Srgrimes	 * protocol specific formatting
8141590Srgrimes	 *
8151590Srgrimes	 * Try to find interesting things to print.  For tcp, the interesting
8161590Srgrimes	 * thing is the address of the tcpcb, for udp and others, just the
8171590Srgrimes	 * inpcb (socket pcb).  For unix domain, its the address of the socket
8181590Srgrimes	 * pcb and the address of the connected pcb (if connected).  Otherwise
8191590Srgrimes	 * just print the protocol number and address of the socket itself.
8201590Srgrimes	 * The idea is not to duplicate netstat, but to make available enough
8211590Srgrimes	 * information for further analysis.
8221590Srgrimes	 */
8231590Srgrimes	switch(dom.dom_family) {
8241590Srgrimes	case AF_INET:
82557345Sshin	case AF_INET6:
8261590Srgrimes		getinetproto(proto.pr_protocol);
8271590Srgrimes		if (proto.pr_protocol == IPPROTO_TCP ) {
8281590Srgrimes			if (so.so_pcb) {
8291590Srgrimes				if (kvm_read(kd, (u_long)so.so_pcb,
8301590Srgrimes				    (char *)&inpcb, sizeof(struct inpcb))
8311590Srgrimes				    != sizeof(struct inpcb)) {
8328874Srgrimes					dprintf(stderr,
83337453Sbde					    "can't read inpcb at %p\n",
83437453Sbde					    (void *)so.so_pcb);
8351590Srgrimes					goto bad;
8361590Srgrimes				}
83780355Smjacob				printf(" %lx", (u_long)inpcb.inp_ppcb);
8381590Srgrimes			}
8391590Srgrimes		}
8401590Srgrimes		else if (so.so_pcb)
84180355Smjacob			printf(" %lx", (u_long)so.so_pcb);
8421590Srgrimes		break;
8431590Srgrimes	case AF_UNIX:
8441590Srgrimes		/* print address of pcb and connected pcb */
8451590Srgrimes		if (so.so_pcb) {
84680355Smjacob			printf(" %lx", (u_long)so.so_pcb);
8471590Srgrimes			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
8481590Srgrimes			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
84937453Sbde				dprintf(stderr, "can't read unpcb at %p\n",
85037453Sbde				    (void *)so.so_pcb);
8511590Srgrimes				goto bad;
8521590Srgrimes			}
8531590Srgrimes			if (unpcb.unp_conn) {
8541590Srgrimes				char shoconn[4], *cp;
8551590Srgrimes
8561590Srgrimes				cp = shoconn;
8571590Srgrimes				if (!(so.so_state & SS_CANTRCVMORE))
8581590Srgrimes					*cp++ = '<';
8591590Srgrimes				*cp++ = '-';
8601590Srgrimes				if (!(so.so_state & SS_CANTSENDMORE))
8611590Srgrimes					*cp++ = '>';
8621590Srgrimes				*cp = '\0';
86380355Smjacob				printf(" %s %lx", shoconn,
86480355Smjacob				    (u_long)unpcb.unp_conn);
8651590Srgrimes			}
8661590Srgrimes		}
8671590Srgrimes		break;
8681590Srgrimes	default:
8691590Srgrimes		/* print protocol number and socket address */
87080355Smjacob		printf(" %d %lx", proto.pr_protocol, (u_long)sock);
8711590Srgrimes	}
8721590Srgrimes	printf("\n");
8731590Srgrimes	return;
8741590Srgrimesbad:
8751590Srgrimes	printf("* error\n");
8761590Srgrimes}
8771590Srgrimes
87853133Sgreen
8791590Srgrimes/*
88053133Sgreen * Read the specinfo structure in the kernel (as pointed to by a dev_t)
88153133Sgreen * in order to work out the associated udev_t
88253133Sgreen */
88353133Sgreenudev_t
88453133Sgreendev2udev(dev)
88553133Sgreen	dev_t dev;
88653133Sgreen{
88753133Sgreen	struct specinfo si;
88853133Sgreen
88953133Sgreen	if (KVM_READ(dev, &si, sizeof si)) {
89053133Sgreen		return si.si_udev;
89153133Sgreen	} else {
89280355Smjacob		dprintf(stderr, "can't convert dev_t %x to a udev_t\n", dev);
89353133Sgreen		return -1;
89453133Sgreen	}
89553133Sgreen}
89653133Sgreen
89753133Sgreen/*
8981590Srgrimes * getinetproto --
8991590Srgrimes *	print name of protocol number
9001590Srgrimes */
9011590Srgrimesvoid
9021590Srgrimesgetinetproto(number)
9031590Srgrimes	int number;
9041590Srgrimes{
90548463Sru	static int isopen;
90693427Sdwmalone	struct protoent *pe;
9071590Srgrimes
90848463Sru	if (!isopen)
90948463Sru		setprotoent(++isopen);
91048463Sru	if ((pe = getprotobynumber(number)) != NULL)
91148463Sru		printf(" %s", pe->p_name);
91248463Sru	else
9131590Srgrimes		printf(" %d", number);
9141590Srgrimes}
9151590Srgrimes
91617808Speterint
9171590Srgrimesgetfname(filename)
91893427Sdwmalone	const char *filename;
9191590Srgrimes{
9201590Srgrimes	struct stat statbuf;
9211590Srgrimes	DEVS *cur;
9221590Srgrimes
9231590Srgrimes	if (stat(filename, &statbuf)) {
92427272Scharnier		warn("%s", filename);
9251590Srgrimes		return(0);
9261590Srgrimes	}
92727272Scharnier	if ((cur = malloc(sizeof(DEVS))) == NULL)
92827272Scharnier		err(1, NULL);
9291590Srgrimes	cur->next = devs;
9301590Srgrimes	devs = cur;
9311590Srgrimes
9321590Srgrimes	cur->ino = statbuf.st_ino;
93386100Sdwmalone	cur->fsid = statbuf.st_dev;
9341590Srgrimes	cur->name = filename;
9351590Srgrimes	return(1);
9361590Srgrimes}
9371590Srgrimes
9381590Srgrimesvoid
9391590Srgrimesusage()
9401590Srgrimes{
9411590Srgrimes	(void)fprintf(stderr,
94259029Sgreen "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
9431590Srgrimes	exit(1);
9441590Srgrimes}
945