fstat.c revision 116780
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 116780 2003-06-24 08:18:18Z jmg $");
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>
67101872Sbmilekic#include <sys/mount.h>
681590Srgrimes#include <ufs/ufs/quota.h>
691590Srgrimes#include <ufs/ufs/inode.h>
7088051Sgreen#include <fs/devfs/devfs.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));
276116717Smaxim	print_header();
2771590Srgrimes	for (plast = &p[cnt]; p < plast; ++p) {
27869896Smckusick		if (p->ki_stat == SZOMB)
2791590Srgrimes			continue;
2801590Srgrimes		dofiles(p);
28159029Sgreen		if (mflg)
28259029Sgreen			dommap(p);
2831590Srgrimes	}
2841590Srgrimes}
2851590Srgrimes
28697946Sdesstatic void
28797946Sdesfstat_sysctl(int what, int arg)
28897946Sdes{
28997946Sdes
29097946Sdes	/* not yet implemented */
29197946Sdes	fstat_kvm(what, arg);
29297946Sdes}
29397946Sdes
29493427Sdwmaloneconst char	*Uname, *Comm;
2951590Srgrimesint	Pid;
2961590Srgrimes
2971590Srgrimes#define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
2981590Srgrimes	switch(i) { \
2991590Srgrimes	case TEXT: \
3001590Srgrimes		printf(" text"); \
3011590Srgrimes		break; \
3021590Srgrimes	case CDIR: \
3031590Srgrimes		printf("   wd"); \
3041590Srgrimes		break; \
3051590Srgrimes	case RDIR: \
3061590Srgrimes		printf(" root"); \
3071590Srgrimes		break; \
3081590Srgrimes	case TRACE: \
3091590Srgrimes		printf("   tr"); \
3101590Srgrimes		break; \
31159029Sgreen	case MMAP: \
31259029Sgreen		printf(" mmap"); \
31359029Sgreen		break; \
3141590Srgrimes	default: \
3151590Srgrimes		printf(" %4d", i); \
3161590Srgrimes		break; \
3171590Srgrimes	}
3181590Srgrimes
3191590Srgrimes/*
3201590Srgrimes * print open files attributed to this process
3211590Srgrimes */
3221590Srgrimesvoid
3231590Srgrimesdofiles(kp)
3241590Srgrimes	struct kinfo_proc *kp;
3251590Srgrimes{
32627272Scharnier	int i;
3271590Srgrimes	struct file file;
3281590Srgrimes	struct filedesc0 filed0;
3291590Srgrimes#define	filed	filed0.fd_fd
3301590Srgrimes
33169896Smckusick	Uname = user_from_uid(kp->ki_uid, 0);
33269896Smckusick	Pid = kp->ki_pid;
33369896Smckusick	Comm = kp->ki_comm;
3341590Srgrimes
33569896Smckusick	if (kp->ki_fd == NULL)
3361590Srgrimes		return;
33769896Smckusick	if (!KVM_READ(kp->ki_fd, &filed0, sizeof (filed0))) {
33837453Sbde		dprintf(stderr, "can't read filedesc at %p for pid %d\n",
33969896Smckusick		    (void *)kp->ki_fd, Pid);
3401590Srgrimes		return;
3411590Srgrimes	}
3421590Srgrimes	/*
3431590Srgrimes	 * root directory vnode, if one
3441590Srgrimes	 */
3451590Srgrimes	if (filed.fd_rdir)
3461590Srgrimes		vtrans(filed.fd_rdir, RDIR, FREAD);
3471590Srgrimes	/*
3481590Srgrimes	 * current working directory vnode
3491590Srgrimes	 */
3501590Srgrimes	vtrans(filed.fd_cdir, CDIR, FREAD);
3511590Srgrimes	/*
3521590Srgrimes	 * ktrace vnode, if one
3531590Srgrimes	 */
35469896Smckusick	if (kp->ki_tracep)
35569896Smckusick		vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE);
3561590Srgrimes	/*
35717813Speter	 * text vnode, if one
35817813Speter	 */
35969896Smckusick	if (kp->ki_textvp)
36069896Smckusick		vtrans(kp->ki_textvp, TEXT, FREAD);
36117813Speter	/*
3621590Srgrimes	 * open files
3631590Srgrimes	 */
3641590Srgrimes#define FPSIZE	(sizeof (struct file *))
3651590Srgrimes	ALLOC_OFILES(filed.fd_lastfile+1);
3661590Srgrimes	if (filed.fd_nfiles > NDFILE) {
3671590Srgrimes		if (!KVM_READ(filed.fd_ofiles, ofiles,
3681590Srgrimes		    (filed.fd_lastfile+1) * FPSIZE)) {
3691590Srgrimes			dprintf(stderr,
37037453Sbde			    "can't read file structures at %p for pid %d\n",
37137453Sbde			    (void *)filed.fd_ofiles, Pid);
3721590Srgrimes			return;
3731590Srgrimes		}
3741590Srgrimes	} else
3751590Srgrimes		bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
3761590Srgrimes	for (i = 0; i <= filed.fd_lastfile; i++) {
3771590Srgrimes		if (ofiles[i] == NULL)
3781590Srgrimes			continue;
3791590Srgrimes		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
38037453Sbde			dprintf(stderr, "can't read file %d at %p for pid %d\n",
38137453Sbde			    i, (void *)ofiles[i], Pid);
3821590Srgrimes			continue;
3831590Srgrimes		}
3841590Srgrimes		if (file.f_type == DTYPE_VNODE)
385109153Sdillon			vtrans(file.f_data, i, file.f_flag);
3861590Srgrimes		else if (file.f_type == DTYPE_SOCKET) {
3871590Srgrimes			if (checkfile == 0)
388109153Sdillon				socktrans(file.f_data, i);
3891590Srgrimes		}
39017808Speter#ifdef DTYPE_PIPE
39117808Speter		else if (file.f_type == DTYPE_PIPE) {
39217808Speter			if (checkfile == 0)
393109153Sdillon				pipetrans(file.f_data, i, file.f_flag);
39417808Speter		}
39517808Speter#endif
39678401Sroam#ifdef DTYPE_FIFO
39778401Sroam		else if (file.f_type == DTYPE_FIFO) {
39878401Sroam			if (checkfile == 0)
399109153Sdillon				vtrans(file.f_data, i, file.f_flag);
40078401Sroam		}
40178401Sroam#endif
4021590Srgrimes		else {
4038874Srgrimes			dprintf(stderr,
40478401Sroam			    "unknown file type %d for file %d of pid %d\n",
40578401Sroam			    file.f_type, i, Pid);
4061590Srgrimes		}
4071590Srgrimes	}
4081590Srgrimes}
4091590Srgrimes
4101590Srgrimesvoid
41159029Sgreendommap(kp)
41259029Sgreen	struct kinfo_proc *kp;
41359029Sgreen{
41469896Smckusick	vm_map_t map;
41559029Sgreen	struct vmspace vmspace;
41659029Sgreen	struct vm_map_entry entry;
41759029Sgreen	vm_map_entry_t entryp;
41859029Sgreen	struct vm_object object;
41959029Sgreen	vm_object_t objp;
42059029Sgreen	int prot, fflags;
42159029Sgreen
42269896Smckusick	if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) {
42369896Smckusick		dprintf(stderr,
42469896Smckusick		    "can't read vmspace at %p for pid %d\n",
42569896Smckusick		    (void *)kp->ki_vmspace, Pid);
42659029Sgreen		return;
42759029Sgreen	}
42859029Sgreen	map = &vmspace.vm_map;
42959029Sgreen
43072527Siedowse	for (entryp = map->header.next;
43172527Siedowse	    entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) {
43259029Sgreen		if (!KVM_READ(entryp, &entry, sizeof(entry))) {
43359029Sgreen			dprintf(stderr,
43459029Sgreen			    "can't read vm_map_entry at %p for pid %d\n",
43559029Sgreen			    (void *)entryp, Pid);
43659029Sgreen			return;
43759029Sgreen		}
43859029Sgreen
43959029Sgreen		if (entry.eflags & MAP_ENTRY_IS_SUB_MAP)
44059029Sgreen			continue;
44159029Sgreen
44259029Sgreen		if ((objp = entry.object.vm_object) == NULL)
44359029Sgreen			continue;
44459029Sgreen
44559029Sgreen		for (; objp; objp = object.backing_object) {
44659029Sgreen			if (!KVM_READ(objp, &object, sizeof(object))) {
44759029Sgreen				dprintf(stderr,
44859029Sgreen				    "can't read vm_object at %p for pid %d\n",
44959029Sgreen				    (void *)objp, Pid);
45059029Sgreen				return;
45159029Sgreen			}
45259029Sgreen		}
45359029Sgreen
45459029Sgreen		prot = entry.protection;
45559029Sgreen		fflags = (prot & VM_PROT_READ ? FREAD : 0) |
45659029Sgreen		    (prot & VM_PROT_WRITE ? FWRITE : 0);
45759029Sgreen
45859029Sgreen		switch (object.type) {
45959029Sgreen		case OBJT_VNODE:
46059029Sgreen			vtrans((struct vnode *)object.handle, MMAP, fflags);
46159029Sgreen			break;
46259029Sgreen		default:
46359029Sgreen			break;
46459029Sgreen		}
46559029Sgreen	}
46659029Sgreen}
46759029Sgreen
46859029Sgreenvoid
4691590Srgrimesvtrans(vp, i, flag)
4701590Srgrimes	struct vnode *vp;
4711590Srgrimes	int i;
4721590Srgrimes	int flag;
4731590Srgrimes{
4741590Srgrimes	struct vnode vn;
4751590Srgrimes	struct filestat fst;
476103325Snjl	char rw[3], mode[15], tagstr[12], *tagptr;
47793427Sdwmalone	const char *badtype, *filename;
4781590Srgrimes
4791590Srgrimes	filename = badtype = NULL;
4801590Srgrimes	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
48137453Sbde		dprintf(stderr, "can't read vnode at %p for pid %d\n",
48237453Sbde		    (void *)vp, Pid);
4831590Srgrimes		return;
4841590Srgrimes	}
485103325Snjl	if (!KVM_READ(&vp->v_tag, &tagptr, sizeof tagptr) ||
486103325Snjl	    !KVM_READ(tagptr, tagstr, sizeof tagstr)) {
487103325Snjl		dprintf(stderr, "can't read v_tag at %p for pid %d\n",
488103325Snjl		    (void *)vp, Pid);
489103325Snjl		return;
490103325Snjl	}
491103325Snjl	tagstr[sizeof(tagstr) - 1] = '\0';
492103325Snjl	if (vn.v_type == VNON)
4931590Srgrimes		badtype = "none";
4941590Srgrimes	else if (vn.v_type == VBAD)
4951590Srgrimes		badtype = "bad";
496103325Snjl	else {
497103325Snjl		if (!strcmp("ufs", tagstr)) {
4981590Srgrimes			if (!ufs_filestat(&vn, &fst))
4991590Srgrimes				badtype = "error";
500103325Snjl		} else if (!strcmp("devfs", tagstr)) {
50188051Sgreen			if (!devfs_filestat(&vn, &fst))
50288051Sgreen				badtype = "error";
503103325Snjl		} else if (!strcmp("nfs", tagstr)) {
5041590Srgrimes			if (!nfs_filestat(&vn, &fst))
5051590Srgrimes				badtype = "error";
506103325Snjl		} else if (!strcmp("msdosfs", tagstr)) {
50758125Sgreen			if (!msdosfs_filestat(&vn, &fst))
50858125Sgreen				badtype = "error";
509103325Snjl		} else if (!strcmp("isofs", tagstr)) {
51058125Sgreen			if (!isofs_filestat(&vn, &fst))
51158125Sgreen				badtype = "error";
512103325Snjl		} else {
513103325Snjl			static char unknown[32];
514103325Snjl			snprintf(unknown, sizeof unknown, "?(%s)", tagstr);
51593427Sdwmalone			badtype = unknown;
5161590Srgrimes		}
5171590Srgrimes	}
5181590Srgrimes	if (checkfile) {
5191590Srgrimes		int fsmatch = 0;
52093427Sdwmalone		DEVS *d;
5211590Srgrimes
5221590Srgrimes		if (badtype)
5231590Srgrimes			return;
5241590Srgrimes		for (d = devs; d != NULL; d = d->next)
5251590Srgrimes			if (d->fsid == fst.fsid) {
5261590Srgrimes				fsmatch = 1;
5271590Srgrimes				if (d->ino == fst.fileid) {
5281590Srgrimes					filename = d->name;
5291590Srgrimes					break;
5301590Srgrimes				}
5311590Srgrimes			}
5321590Srgrimes		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
5331590Srgrimes			return;
5341590Srgrimes	}
5351590Srgrimes	PREFIX(i);
5361590Srgrimes	if (badtype) {
5371590Srgrimes		(void)printf(" -         -  %10s    -\n", badtype);
5381590Srgrimes		return;
5391590Srgrimes	}
5401590Srgrimes	if (nflg)
5411590Srgrimes		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
5421590Srgrimes	else
5431590Srgrimes		(void)printf(" %-8s", getmnton(vn.v_mount));
5441590Srgrimes	if (nflg)
5451590Srgrimes		(void)sprintf(mode, "%o", fst.mode);
5461590Srgrimes	else
5471590Srgrimes		strmode(fst.mode, mode);
54837453Sbde	(void)printf(" %6ld %10s", fst.fileid, mode);
5491590Srgrimes	switch (vn.v_type) {
5501590Srgrimes	case VBLK:
5511590Srgrimes	case VCHR: {
5521590Srgrimes		char *name;
5531590Srgrimes
5548874Srgrimes		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
5551590Srgrimes		    S_IFCHR : S_IFBLK)) == NULL))
5561590Srgrimes			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
5571590Srgrimes		else
5581590Srgrimes			printf(" %6s", name);
5591590Srgrimes		break;
5601590Srgrimes	}
5611590Srgrimes	default:
56228948Salex		printf(" %6lu", fst.size);
5631590Srgrimes	}
5641590Srgrimes	rw[0] = '\0';
5651590Srgrimes	if (flag & FREAD)
5661590Srgrimes		strcat(rw, "r");
5671590Srgrimes	if (flag & FWRITE)
5681590Srgrimes		strcat(rw, "w");
5691590Srgrimes	printf(" %2s", rw);
5701590Srgrimes	if (filename && !fsflg)
5711590Srgrimes		printf("  %s", filename);
5721590Srgrimes	putchar('\n');
5731590Srgrimes}
5741590Srgrimes
5751590Srgrimesint
5761590Srgrimesufs_filestat(vp, fsp)
5771590Srgrimes	struct vnode *vp;
5781590Srgrimes	struct filestat *fsp;
5791590Srgrimes{
5801590Srgrimes	struct inode inode;
5811590Srgrimes
5821590Srgrimes	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
58337453Sbde		dprintf(stderr, "can't read inode at %p for pid %d\n",
58437453Sbde		    (void *)VTOI(vp), Pid);
5851590Srgrimes		return 0;
5861590Srgrimes	}
58753133Sgreen	/*
58853133Sgreen	 * The st_dev from stat(2) is a udev_t. These kernel structures
58953133Sgreen	 * contain dev_t structures. We need to convert to udev to make
59053133Sgreen	 * comparisons
59153133Sgreen	 */
59286100Sdwmalone	fsp->fsid = dev2udev(inode.i_dev);
5931590Srgrimes	fsp->fileid = (long)inode.i_number;
5941590Srgrimes	fsp->mode = (mode_t)inode.i_mode;
5951590Srgrimes	fsp->size = (u_long)inode.i_size;
59698542Smckusick#if should_be_but_is_hard
597116780Sjmg	/* XXX - need to load i_ump and i_din[12] from kernel memory */
598116780Sjmg	if (inode.i_ump->um_fstype == UFS1)
599116780Sjmg		fsp->rdev = inode.i_din1->di_rdev;
600116780Sjmg	else
601116780Sjmg		fsp->rdev = inode.i_din2->di_rdev;
60298542Smckusick#else
60398542Smckusick	fsp->rdev = 0;
60498542Smckusick#endif
6051590Srgrimes
6061590Srgrimes	return 1;
6071590Srgrimes}
6081590Srgrimes
6091590Srgrimesint
61088051Sgreendevfs_filestat(vp, fsp)
61188051Sgreen	struct vnode *vp;
61288051Sgreen	struct filestat *fsp;
61388051Sgreen{
61488051Sgreen	struct devfs_dirent devfs_dirent;
61588051Sgreen	struct mount mount;
61688051Sgreen	struct vnode vnode;
61788051Sgreen
61888051Sgreen	if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) {
61988051Sgreen		dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n",
62088051Sgreen		    (void *)vp->v_data, Pid);
62188051Sgreen		return 0;
62288051Sgreen	}
62388051Sgreen	if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) {
62488051Sgreen		dprintf(stderr, "can't read mount at %p for pid %d\n",
62588051Sgreen		    (void *)vp->v_mount, Pid);
62688051Sgreen		return 0;
62788051Sgreen	}
62888051Sgreen	if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) {
62988051Sgreen		dprintf(stderr, "can't read vnode at %p for pid %d\n",
63088051Sgreen		    (void *)devfs_dirent.de_vnode, Pid);
63188051Sgreen		return 0;
63288051Sgreen	}
63388051Sgreen	fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0];
63488051Sgreen	fsp->fileid = devfs_dirent.de_inode;
63588051Sgreen	fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR;
63688051Sgreen	fsp->size = 0;
637116556Sjmg	fsp->rdev = dev2udev(vnode.v_rdev);
63888051Sgreen
63988051Sgreen	return 1;
64088051Sgreen}
64188051Sgreen
64288051Sgreenint
6431590Srgrimesnfs_filestat(vp, fsp)
6441590Srgrimes	struct vnode *vp;
6451590Srgrimes	struct filestat *fsp;
6461590Srgrimes{
6471590Srgrimes	struct nfsnode nfsnode;
64893427Sdwmalone	mode_t mode;
6491590Srgrimes
6501590Srgrimes	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
65137453Sbde		dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
65237453Sbde		    (void *)VTONFS(vp), Pid);
6531590Srgrimes		return 0;
6541590Srgrimes	}
6551590Srgrimes	fsp->fsid = nfsnode.n_vattr.va_fsid;
6561590Srgrimes	fsp->fileid = nfsnode.n_vattr.va_fileid;
6571590Srgrimes	fsp->size = nfsnode.n_size;
6581590Srgrimes	fsp->rdev = nfsnode.n_vattr.va_rdev;
6591590Srgrimes	mode = (mode_t)nfsnode.n_vattr.va_mode;
6601590Srgrimes	switch (vp->v_type) {
6611590Srgrimes	case VREG:
6621590Srgrimes		mode |= S_IFREG;
6631590Srgrimes		break;
6641590Srgrimes	case VDIR:
6651590Srgrimes		mode |= S_IFDIR;
6661590Srgrimes		break;
6671590Srgrimes	case VBLK:
6681590Srgrimes		mode |= S_IFBLK;
6691590Srgrimes		break;
6701590Srgrimes	case VCHR:
6711590Srgrimes		mode |= S_IFCHR;
6721590Srgrimes		break;
6731590Srgrimes	case VLNK:
6741590Srgrimes		mode |= S_IFLNK;
6751590Srgrimes		break;
6761590Srgrimes	case VSOCK:
6771590Srgrimes		mode |= S_IFSOCK;
6781590Srgrimes		break;
6791590Srgrimes	case VFIFO:
6801590Srgrimes		mode |= S_IFIFO;
6811590Srgrimes		break;
68248463Sru	case VNON:
68348463Sru	case VBAD:
68448463Sru		return 0;
6851590Srgrimes	};
6861590Srgrimes	fsp->mode = mode;
6871590Srgrimes
6881590Srgrimes	return 1;
6891590Srgrimes}
6901590Srgrimes
6911590Srgrimes
6921590Srgrimeschar *
6931590Srgrimesgetmnton(m)
6941590Srgrimes	struct mount *m;
6951590Srgrimes{
6961590Srgrimes	static struct mount mount;
6971590Srgrimes	static struct mtab {
6981590Srgrimes		struct mtab *next;
6991590Srgrimes		struct mount *m;
7001590Srgrimes		char mntonname[MNAMELEN];
7011590Srgrimes	} *mhead = NULL;
70293427Sdwmalone	struct mtab *mt;
7031590Srgrimes
7041590Srgrimes	for (mt = mhead; mt != NULL; mt = mt->next)
7051590Srgrimes		if (m == mt->m)
7061590Srgrimes			return (mt->mntonname);
7071590Srgrimes	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
70837453Sbde		warnx("can't read mount table at %p", (void *)m);
7091590Srgrimes		return (NULL);
7101590Srgrimes	}
71127272Scharnier	if ((mt = malloc(sizeof (struct mtab))) == NULL)
71227272Scharnier		err(1, NULL);
7131590Srgrimes	mt->m = m;
7141590Srgrimes	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
7151590Srgrimes	mt->next = mhead;
7161590Srgrimes	mhead = mt;
7171590Srgrimes	return (mt->mntonname);
7181590Srgrimes}
7191590Srgrimes
7201590Srgrimesvoid
72117808Speterpipetrans(pi, i, flag)
72217808Speter	struct pipe *pi;
72317808Speter	int i;
72417808Speter	int flag;
72517808Speter{
72617808Speter	struct pipe pip;
72717808Speter	char rw[3];
72817808Speter
72917808Speter	PREFIX(i);
73017808Speter
73117808Speter	/* fill in socket */
73217808Speter	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
73337453Sbde		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
73417808Speter		goto bad;
73517808Speter	}
73617808Speter
73780355Smjacob	printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
73817808Speter	printf(" %6d", (int)pip.pipe_buffer.cnt);
73917808Speter	rw[0] = '\0';
74017808Speter	if (flag & FREAD)
74117808Speter		strcat(rw, "r");
74217808Speter	if (flag & FWRITE)
74317808Speter		strcat(rw, "w");
74417808Speter	printf(" %2s", rw);
74517808Speter	putchar('\n');
74617808Speter	return;
74717808Speter
74817808Speterbad:
74917808Speter	printf("* error\n");
75017808Speter}
75117808Speter
75217808Spetervoid
7531590Srgrimessocktrans(sock, i)
7541590Srgrimes	struct socket *sock;
7551590Srgrimes	int i;
7561590Srgrimes{
75793427Sdwmalone	static const char *stypename[] = {
7581590Srgrimes		"unused",	/* 0 */
7591590Srgrimes		"stream", 	/* 1 */
7601590Srgrimes		"dgram",	/* 2 */
7611590Srgrimes		"raw",		/* 3 */
7621590Srgrimes		"rdm",		/* 4 */
7631590Srgrimes		"seqpak"	/* 5 */
7641590Srgrimes	};
7651590Srgrimes#define	STYPEMAX 5
7661590Srgrimes	struct socket	so;
7671590Srgrimes	struct protosw	proto;
7681590Srgrimes	struct domain	dom;
7691590Srgrimes	struct inpcb	inpcb;
7701590Srgrimes	struct unpcb	unpcb;
7711590Srgrimes	int len;
77293427Sdwmalone	char dname[32];
7731590Srgrimes
7741590Srgrimes	PREFIX(i);
7751590Srgrimes
7761590Srgrimes	/* fill in socket */
7771590Srgrimes	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
77837453Sbde		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
7791590Srgrimes		goto bad;
7801590Srgrimes	}
7811590Srgrimes
7821590Srgrimes	/* fill in protosw entry */
7831590Srgrimes	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
78437453Sbde		dprintf(stderr, "can't read protosw at %p",
78537453Sbde		    (void *)so.so_proto);
7861590Srgrimes		goto bad;
7871590Srgrimes	}
7881590Srgrimes
7891590Srgrimes	/* fill in domain */
7901590Srgrimes	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
79137453Sbde		dprintf(stderr, "can't read domain at %p\n",
79237453Sbde		    (void *)proto.pr_domain);
7931590Srgrimes		goto bad;
7941590Srgrimes	}
7951590Srgrimes
7961590Srgrimes	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
7971590Srgrimes	    sizeof(dname) - 1)) < 0) {
79837453Sbde		dprintf(stderr, "can't read domain name at %p\n",
79937453Sbde		    (void *)dom.dom_name);
8001590Srgrimes		dname[0] = '\0';
8011590Srgrimes	}
8021590Srgrimes	else
8031590Srgrimes		dname[len] = '\0';
8041590Srgrimes
8051590Srgrimes	if ((u_short)so.so_type > STYPEMAX)
8061590Srgrimes		printf("* %s ?%d", dname, so.so_type);
8071590Srgrimes	else
8081590Srgrimes		printf("* %s %s", dname, stypename[so.so_type]);
8091590Srgrimes
8108874Srgrimes	/*
8111590Srgrimes	 * protocol specific formatting
8121590Srgrimes	 *
8131590Srgrimes	 * Try to find interesting things to print.  For tcp, the interesting
8141590Srgrimes	 * thing is the address of the tcpcb, for udp and others, just the
8151590Srgrimes	 * inpcb (socket pcb).  For unix domain, its the address of the socket
8161590Srgrimes	 * pcb and the address of the connected pcb (if connected).  Otherwise
8171590Srgrimes	 * just print the protocol number and address of the socket itself.
8181590Srgrimes	 * The idea is not to duplicate netstat, but to make available enough
8191590Srgrimes	 * information for further analysis.
8201590Srgrimes	 */
8211590Srgrimes	switch(dom.dom_family) {
8221590Srgrimes	case AF_INET:
82357345Sshin	case AF_INET6:
8241590Srgrimes		getinetproto(proto.pr_protocol);
8251590Srgrimes		if (proto.pr_protocol == IPPROTO_TCP ) {
8261590Srgrimes			if (so.so_pcb) {
8271590Srgrimes				if (kvm_read(kd, (u_long)so.so_pcb,
8281590Srgrimes				    (char *)&inpcb, sizeof(struct inpcb))
8291590Srgrimes				    != sizeof(struct inpcb)) {
8308874Srgrimes					dprintf(stderr,
83137453Sbde					    "can't read inpcb at %p\n",
83237453Sbde					    (void *)so.so_pcb);
8331590Srgrimes					goto bad;
8341590Srgrimes				}
83580355Smjacob				printf(" %lx", (u_long)inpcb.inp_ppcb);
8361590Srgrimes			}
8371590Srgrimes		}
8381590Srgrimes		else if (so.so_pcb)
83980355Smjacob			printf(" %lx", (u_long)so.so_pcb);
8401590Srgrimes		break;
8411590Srgrimes	case AF_UNIX:
8421590Srgrimes		/* print address of pcb and connected pcb */
8431590Srgrimes		if (so.so_pcb) {
84480355Smjacob			printf(" %lx", (u_long)so.so_pcb);
8451590Srgrimes			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
8461590Srgrimes			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
84737453Sbde				dprintf(stderr, "can't read unpcb at %p\n",
84837453Sbde				    (void *)so.so_pcb);
8491590Srgrimes				goto bad;
8501590Srgrimes			}
8511590Srgrimes			if (unpcb.unp_conn) {
8521590Srgrimes				char shoconn[4], *cp;
8531590Srgrimes
8541590Srgrimes				cp = shoconn;
8551590Srgrimes				if (!(so.so_state & SS_CANTRCVMORE))
8561590Srgrimes					*cp++ = '<';
8571590Srgrimes				*cp++ = '-';
8581590Srgrimes				if (!(so.so_state & SS_CANTSENDMORE))
8591590Srgrimes					*cp++ = '>';
8601590Srgrimes				*cp = '\0';
86180355Smjacob				printf(" %s %lx", shoconn,
86280355Smjacob				    (u_long)unpcb.unp_conn);
8631590Srgrimes			}
8641590Srgrimes		}
8651590Srgrimes		break;
8661590Srgrimes	default:
8671590Srgrimes		/* print protocol number and socket address */
86880355Smjacob		printf(" %d %lx", proto.pr_protocol, (u_long)sock);
8691590Srgrimes	}
8701590Srgrimes	printf("\n");
8711590Srgrimes	return;
8721590Srgrimesbad:
8731590Srgrimes	printf("* error\n");
8741590Srgrimes}
8751590Srgrimes
87653133Sgreen
8771590Srgrimes/*
878104043Sphk * Read the cdev structure in the kernel (as pointed to by a dev_t)
87953133Sgreen * in order to work out the associated udev_t
88053133Sgreen */
88153133Sgreenudev_t
88253133Sgreendev2udev(dev)
883116556Sjmg	struct cdev *dev;
88453133Sgreen{
885104043Sphk	struct cdev si;
88653133Sgreen
88753133Sgreen	if (KVM_READ(dev, &si, sizeof si)) {
88853133Sgreen		return si.si_udev;
88953133Sgreen	} else {
89080355Smjacob		dprintf(stderr, "can't convert dev_t %x to a udev_t\n", dev);
89153133Sgreen		return -1;
89253133Sgreen	}
89353133Sgreen}
89453133Sgreen
89553133Sgreen/*
8961590Srgrimes * getinetproto --
8971590Srgrimes *	print name of protocol number
8981590Srgrimes */
8991590Srgrimesvoid
9001590Srgrimesgetinetproto(number)
9011590Srgrimes	int number;
9021590Srgrimes{
90348463Sru	static int isopen;
90493427Sdwmalone	struct protoent *pe;
9051590Srgrimes
90648463Sru	if (!isopen)
90748463Sru		setprotoent(++isopen);
90848463Sru	if ((pe = getprotobynumber(number)) != NULL)
90948463Sru		printf(" %s", pe->p_name);
91048463Sru	else
9111590Srgrimes		printf(" %d", number);
9121590Srgrimes}
9131590Srgrimes
91417808Speterint
9151590Srgrimesgetfname(filename)
91693427Sdwmalone	const char *filename;
9171590Srgrimes{
9181590Srgrimes	struct stat statbuf;
9191590Srgrimes	DEVS *cur;
9201590Srgrimes
9211590Srgrimes	if (stat(filename, &statbuf)) {
92227272Scharnier		warn("%s", filename);
9231590Srgrimes		return(0);
9241590Srgrimes	}
92527272Scharnier	if ((cur = malloc(sizeof(DEVS))) == NULL)
92627272Scharnier		err(1, NULL);
9271590Srgrimes	cur->next = devs;
9281590Srgrimes	devs = cur;
9291590Srgrimes
9301590Srgrimes	cur->ino = statbuf.st_ino;
93186100Sdwmalone	cur->fsid = statbuf.st_dev;
9321590Srgrimes	cur->name = filename;
9331590Srgrimes	return(1);
9341590Srgrimes}
9351590Srgrimes
9361590Srgrimesvoid
9371590Srgrimesusage()
9381590Srgrimes{
9391590Srgrimes	(void)fprintf(stderr,
94059029Sgreen "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
9411590Srgrimes	exit(1);
9421590Srgrimes}
943