fstat.c revision 48463
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
4427272Scharnierstatic const char rcsid[] =
4548463Sru	"$Id: fstat.c,v 1.16 1998/07/06 21:01:16 bde Exp $";
461590Srgrimes#endif /* not lint */
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>
641590Srgrimes#define	KERNEL
651590Srgrimes#include <sys/file.h>
661590Srgrimes#include <ufs/ufs/quota.h>
671590Srgrimes#include <ufs/ufs/inode.h>
681590Srgrimes#undef KERNEL
691590Srgrimes#include <sys/mount.h>
709336Sdfr#include <nfs/nfsproto.h>
711590Srgrimes#include <nfs/rpcv2.h>
721590Srgrimes#include <nfs/nfs.h>
731590Srgrimes#include <nfs/nfsnode.h>
741590Srgrimes
751590Srgrimes#include <net/route.h>
761590Srgrimes#include <netinet/in.h>
771590Srgrimes#include <netinet/in_systm.h>
781590Srgrimes#include <netinet/ip.h>
791590Srgrimes#include <netinet/in_pcb.h>
801590Srgrimes
811590Srgrimes#include <ctype.h>
8227272Scharnier#include <err.h>
8318570Sbde#include <fcntl.h>
841590Srgrimes#include <kvm.h>
8523693Speter#include <limits.h>
861590Srgrimes#include <nlist.h>
871590Srgrimes#include <paths.h>
881590Srgrimes#include <pwd.h>
891590Srgrimes#include <stdio.h>
901590Srgrimes#include <stdlib.h>
911590Srgrimes#include <string.h>
9223693Speter#include <unistd.h>
9348463Sru#include <netdb.h>
941590Srgrimes
951590Srgrimes#define	TEXT	-1
961590Srgrimes#define	CDIR	-2
971590Srgrimes#define	RDIR	-3
981590Srgrimes#define	TRACE	-4
991590Srgrimes
1001590Srgrimestypedef struct devs {
1011590Srgrimes	struct	devs *next;
1021590Srgrimes	long	fsid;
1031590Srgrimes	ino_t	ino;
1041590Srgrimes	char	*name;
1051590Srgrimes} DEVS;
1061590SrgrimesDEVS *devs;
1071590Srgrimes
1081590Srgrimesstruct  filestat {
1091590Srgrimes	long	fsid;
1101590Srgrimes	long	fileid;
1111590Srgrimes	mode_t	mode;
1121590Srgrimes	u_long	size;
1131590Srgrimes	dev_t	rdev;
1141590Srgrimes};
1151590Srgrimes
1161590Srgrimes#ifdef notdef
1171590Srgrimesstruct nlist nl[] = {
1181590Srgrimes	{ "" },
1191590Srgrimes};
1201590Srgrimes#endif
1211590Srgrimes
1221590Srgrimesint 	fsflg,	/* show files on same filesystem as file(s) argument */
1231590Srgrimes	pflg,	/* show files open by a particular pid */
1241590Srgrimes	uflg;	/* show files open by a particular (effective) user */
1251590Srgrimesint 	checkfile; /* true if restricting to particular files or filesystems */
1261590Srgrimesint	nflg;	/* (numerical) display f.s. and rdev as dev_t */
1271590Srgrimesint	vflg;	/* display errors in locating kernel data objects etc... */
1281590Srgrimes
1291590Srgrimes#define dprintf	if (vflg) fprintf
1301590Srgrimes
1311590Srgrimesstruct file **ofiles;	/* buffer of pointers to file structures */
1321590Srgrimesint maxfiles;
1331590Srgrimes#define ALLOC_OFILES(d)	\
1341590Srgrimes	if ((d) > maxfiles) { \
1351590Srgrimes		free(ofiles); \
1361590Srgrimes		ofiles = malloc((d) * sizeof(struct file *)); \
1371590Srgrimes		if (ofiles == NULL) { \
13827272Scharnier			err(1, NULL); \
1391590Srgrimes		} \
1401590Srgrimes		maxfiles = (d); \
1411590Srgrimes	}
1421590Srgrimes
1431590Srgrimes/*
1448874Srgrimes * a kvm_read that returns true if everything is read
1451590Srgrimes */
1461590Srgrimes#define KVM_READ(kaddr, paddr, len) \
1471590Srgrimes	(kvm_read(kd, (u_long)(kaddr), (char *)(paddr), (len)) == (len))
1481590Srgrimes
1491590Srgrimeskvm_t *kd;
1501590Srgrimes
15117808Spetervoid dofiles __P((struct kinfo_proc *kp));
15217808Spetervoid vtrans __P((struct vnode *vp, int i, int flag));
15317808Speterint  ufs_filestat __P((struct vnode *vp, struct filestat *fsp));
15417808Speterint  nfs_filestat __P((struct vnode *vp, struct filestat *fsp));
15517808Speterchar *getmnton __P((struct mount *m));
15617808Spetervoid pipetrans __P((struct pipe *pi, int i, int flag));
15717808Spetervoid socktrans __P((struct socket *sock, int i));
15817808Spetervoid getinetproto __P((int number));
15917808Speterint  getfname __P((char *filename));
16017808Spetervoid usage __P((void));
1611590Srgrimes
16217808Speter
16317808Speterint
1641590Srgrimesmain(argc, argv)
1651590Srgrimes	int argc;
1661590Srgrimes	char **argv;
1671590Srgrimes{
1681590Srgrimes	register struct passwd *passwd;
1691590Srgrimes	struct kinfo_proc *p, *plast;
1701590Srgrimes	int arg, ch, what;
1711590Srgrimes	char *memf, *nlistf;
17223693Speter	char buf[_POSIX2_LINE_MAX];
1731590Srgrimes	int cnt;
1741590Srgrimes
1751590Srgrimes	arg = 0;
1761590Srgrimes	what = KERN_PROC_ALL;
1771590Srgrimes	nlistf = memf = NULL;
17829592Sdima	while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1)
1791590Srgrimes		switch((char)ch) {
1801590Srgrimes		case 'f':
1811590Srgrimes			fsflg = 1;
1821590Srgrimes			break;
1831590Srgrimes		case 'M':
1841590Srgrimes			memf = optarg;
1851590Srgrimes			break;
1861590Srgrimes		case 'N':
1871590Srgrimes			nlistf = optarg;
1881590Srgrimes			break;
1891590Srgrimes		case 'n':
1901590Srgrimes			nflg = 1;
1911590Srgrimes			break;
1921590Srgrimes		case 'p':
1931590Srgrimes			if (pflg++)
1941590Srgrimes				usage();
1951590Srgrimes			if (!isdigit(*optarg)) {
19627311Scharnier				warnx("-p requires a process id");
1971590Srgrimes				usage();
1981590Srgrimes			}
1991590Srgrimes			what = KERN_PROC_PID;
2001590Srgrimes			arg = atoi(optarg);
2011590Srgrimes			break;
2021590Srgrimes		case 'u':
2031590Srgrimes			if (uflg++)
2041590Srgrimes				usage();
20527272Scharnier			if (!(passwd = getpwnam(optarg)))
20627272Scharnier				errx(1, "%s: unknown uid", optarg);
2071590Srgrimes			what = KERN_PROC_UID;
2081590Srgrimes			arg = passwd->pw_uid;
2091590Srgrimes			break;
2101590Srgrimes		case 'v':
2111590Srgrimes			vflg = 1;
2121590Srgrimes			break;
2131590Srgrimes		case '?':
2141590Srgrimes		default:
2151590Srgrimes			usage();
2161590Srgrimes		}
2171590Srgrimes
2181590Srgrimes	if (*(argv += optind)) {
2191590Srgrimes		for (; *argv; ++argv) {
2201590Srgrimes			if (getfname(*argv))
2211590Srgrimes				checkfile = 1;
2221590Srgrimes		}
2231590Srgrimes		if (!checkfile)	/* file(s) specified, but none accessable */
2241590Srgrimes			exit(1);
2251590Srgrimes	}
2261590Srgrimes
2271590Srgrimes	ALLOC_OFILES(256);	/* reserve space for file pointers */
2281590Srgrimes
2298874Srgrimes	if (fsflg && !checkfile) {
2301590Srgrimes		/* -f with no files means use wd */
2311590Srgrimes		if (getfname(".") == 0)
2321590Srgrimes			exit(1);
2331590Srgrimes		checkfile = 1;
2341590Srgrimes	}
2351590Srgrimes
2361590Srgrimes	/*
2371590Srgrimes	 * Discard setgid privileges if not the running kernel so that bad
2381590Srgrimes	 * guys can't print interesting stuff from kernel memory.
2391590Srgrimes	 */
2401590Srgrimes	if (nlistf != NULL || memf != NULL)
2411590Srgrimes		setgid(getgid());
2421590Srgrimes
24327272Scharnier	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
24427272Scharnier		errx(1, "%s", buf);
2451590Srgrimes#ifdef notdef
24627272Scharnier	if (kvm_nlist(kd, nl) != 0)
24727272Scharnier		errx(1, "no namelist: %s", kvm_geterr(kd));
2481590Srgrimes#endif
24927272Scharnier	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
25027272Scharnier		errx(1, "%s", kvm_geterr(kd));
2511590Srgrimes	if (nflg)
2521590Srgrimes		printf("%s",
2531590Srgrimes"USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
2541590Srgrimes	else
2551590Srgrimes		printf("%s",
2561590Srgrimes"USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
2571590Srgrimes	if (checkfile && fsflg == 0)
2581590Srgrimes		printf(" NAME\n");
2591590Srgrimes	else
2601590Srgrimes		putchar('\n');
2611590Srgrimes
2621590Srgrimes	for (plast = &p[cnt]; p < plast; ++p) {
2631590Srgrimes		if (p->kp_proc.p_stat == SZOMB)
2641590Srgrimes			continue;
2651590Srgrimes		dofiles(p);
2661590Srgrimes	}
2671590Srgrimes	exit(0);
2681590Srgrimes}
2691590Srgrimes
2701590Srgrimeschar	*Uname, *Comm;
2711590Srgrimesint	Pid;
2721590Srgrimes
2731590Srgrimes#define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
2741590Srgrimes	switch(i) { \
2751590Srgrimes	case TEXT: \
2761590Srgrimes		printf(" text"); \
2771590Srgrimes		break; \
2781590Srgrimes	case CDIR: \
2791590Srgrimes		printf("   wd"); \
2801590Srgrimes		break; \
2811590Srgrimes	case RDIR: \
2821590Srgrimes		printf(" root"); \
2831590Srgrimes		break; \
2841590Srgrimes	case TRACE: \
2851590Srgrimes		printf("   tr"); \
2861590Srgrimes		break; \
2871590Srgrimes	default: \
2881590Srgrimes		printf(" %4d", i); \
2891590Srgrimes		break; \
2901590Srgrimes	}
2911590Srgrimes
2921590Srgrimes/*
2931590Srgrimes * print open files attributed to this process
2941590Srgrimes */
2951590Srgrimesvoid
2961590Srgrimesdofiles(kp)
2971590Srgrimes	struct kinfo_proc *kp;
2981590Srgrimes{
29927272Scharnier	int i;
3001590Srgrimes	struct file file;
3011590Srgrimes	struct filedesc0 filed0;
3021590Srgrimes#define	filed	filed0.fd_fd
3031590Srgrimes	struct proc *p = &kp->kp_proc;
3041590Srgrimes	struct eproc *ep = &kp->kp_eproc;
3051590Srgrimes
3061590Srgrimes	Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
3071590Srgrimes	Pid = p->p_pid;
3081590Srgrimes	Comm = p->p_comm;
3091590Srgrimes
3101590Srgrimes	if (p->p_fd == NULL)
3111590Srgrimes		return;
3121590Srgrimes	if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
31337453Sbde		dprintf(stderr, "can't read filedesc at %p for pid %d\n",
31437453Sbde		    (void *)p->p_fd, Pid);
3151590Srgrimes		return;
3161590Srgrimes	}
3171590Srgrimes	/*
3181590Srgrimes	 * root directory vnode, if one
3191590Srgrimes	 */
3201590Srgrimes	if (filed.fd_rdir)
3211590Srgrimes		vtrans(filed.fd_rdir, RDIR, FREAD);
3221590Srgrimes	/*
3231590Srgrimes	 * current working directory vnode
3241590Srgrimes	 */
3251590Srgrimes	vtrans(filed.fd_cdir, CDIR, FREAD);
3261590Srgrimes	/*
3271590Srgrimes	 * ktrace vnode, if one
3281590Srgrimes	 */
3291590Srgrimes	if (p->p_tracep)
3301590Srgrimes		vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
3311590Srgrimes	/*
33217813Speter	 * text vnode, if one
33317813Speter	 */
33417813Speter	if (p->p_textvp)
33517813Speter		vtrans(p->p_textvp, TEXT, FREAD);
33617813Speter	/*
3371590Srgrimes	 * open files
3381590Srgrimes	 */
3391590Srgrimes#define FPSIZE	(sizeof (struct file *))
3401590Srgrimes	ALLOC_OFILES(filed.fd_lastfile+1);
3411590Srgrimes	if (filed.fd_nfiles > NDFILE) {
3421590Srgrimes		if (!KVM_READ(filed.fd_ofiles, ofiles,
3431590Srgrimes		    (filed.fd_lastfile+1) * FPSIZE)) {
3441590Srgrimes			dprintf(stderr,
34537453Sbde			    "can't read file structures at %p for pid %d\n",
34637453Sbde			    (void *)filed.fd_ofiles, Pid);
3471590Srgrimes			return;
3481590Srgrimes		}
3491590Srgrimes	} else
3501590Srgrimes		bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
3511590Srgrimes	for (i = 0; i <= filed.fd_lastfile; i++) {
3521590Srgrimes		if (ofiles[i] == NULL)
3531590Srgrimes			continue;
3541590Srgrimes		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
35537453Sbde			dprintf(stderr, "can't read file %d at %p for pid %d\n",
35637453Sbde			    i, (void *)ofiles[i], Pid);
3571590Srgrimes			continue;
3581590Srgrimes		}
3591590Srgrimes		if (file.f_type == DTYPE_VNODE)
3601590Srgrimes			vtrans((struct vnode *)file.f_data, i, file.f_flag);
3611590Srgrimes		else if (file.f_type == DTYPE_SOCKET) {
3621590Srgrimes			if (checkfile == 0)
3631590Srgrimes				socktrans((struct socket *)file.f_data, i);
3641590Srgrimes		}
36517808Speter#ifdef DTYPE_PIPE
36617808Speter		else if (file.f_type == DTYPE_PIPE) {
36717808Speter			if (checkfile == 0)
36817808Speter				pipetrans((struct pipe *)file.f_data, i,
36917808Speter					file.f_flag);
37017808Speter		}
37117808Speter#endif
3721590Srgrimes		else {
3738874Srgrimes			dprintf(stderr,
3741590Srgrimes				"unknown file type %d for file %d of pid %d\n",
3751590Srgrimes				file.f_type, i, Pid);
3761590Srgrimes		}
3771590Srgrimes	}
3781590Srgrimes}
3791590Srgrimes
3801590Srgrimesvoid
3811590Srgrimesvtrans(vp, i, flag)
3821590Srgrimes	struct vnode *vp;
3831590Srgrimes	int i;
3841590Srgrimes	int flag;
3851590Srgrimes{
3861590Srgrimes	struct vnode vn;
3871590Srgrimes	struct filestat fst;
3881590Srgrimes	char rw[3], mode[15];
3891590Srgrimes	char *badtype = NULL, *filename, *getmnton();
3901590Srgrimes
3911590Srgrimes	filename = badtype = NULL;
3921590Srgrimes	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
39337453Sbde		dprintf(stderr, "can't read vnode at %p for pid %d\n",
39437453Sbde		    (void *)vp, Pid);
3951590Srgrimes		return;
3961590Srgrimes	}
3971590Srgrimes	if (vn.v_type == VNON || vn.v_tag == VT_NON)
3981590Srgrimes		badtype = "none";
3991590Srgrimes	else if (vn.v_type == VBAD)
4001590Srgrimes		badtype = "bad";
4011590Srgrimes	else
4021590Srgrimes		switch (vn.v_tag) {
4031590Srgrimes		case VT_UFS:
4041590Srgrimes			if (!ufs_filestat(&vn, &fst))
4051590Srgrimes				badtype = "error";
4061590Srgrimes			break;
4071590Srgrimes		case VT_MFS:
4081590Srgrimes			if (!ufs_filestat(&vn, &fst))
4091590Srgrimes				badtype = "error";
4101590Srgrimes			break;
4111590Srgrimes		case VT_NFS:
4121590Srgrimes			if (!nfs_filestat(&vn, &fst))
4131590Srgrimes				badtype = "error";
4141590Srgrimes			break;
4151590Srgrimes		default: {
4161590Srgrimes			static char unknown[10];
4171590Srgrimes			sprintf(badtype = unknown, "?(%x)", vn.v_tag);
4181590Srgrimes			break;;
4191590Srgrimes		}
4201590Srgrimes	}
4211590Srgrimes	if (checkfile) {
4221590Srgrimes		int fsmatch = 0;
4231590Srgrimes		register DEVS *d;
4241590Srgrimes
4251590Srgrimes		if (badtype)
4261590Srgrimes			return;
4271590Srgrimes		for (d = devs; d != NULL; d = d->next)
4281590Srgrimes			if (d->fsid == fst.fsid) {
4291590Srgrimes				fsmatch = 1;
4301590Srgrimes				if (d->ino == fst.fileid) {
4311590Srgrimes					filename = d->name;
4321590Srgrimes					break;
4331590Srgrimes				}
4341590Srgrimes			}
4351590Srgrimes		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
4361590Srgrimes			return;
4371590Srgrimes	}
4381590Srgrimes	PREFIX(i);
4391590Srgrimes	if (badtype) {
4401590Srgrimes		(void)printf(" -         -  %10s    -\n", badtype);
4411590Srgrimes		return;
4421590Srgrimes	}
4431590Srgrimes	if (nflg)
4441590Srgrimes		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
4451590Srgrimes	else
4461590Srgrimes		(void)printf(" %-8s", getmnton(vn.v_mount));
4471590Srgrimes	if (nflg)
4481590Srgrimes		(void)sprintf(mode, "%o", fst.mode);
4491590Srgrimes	else
4501590Srgrimes		strmode(fst.mode, mode);
45137453Sbde	(void)printf(" %6ld %10s", fst.fileid, mode);
4521590Srgrimes	switch (vn.v_type) {
4531590Srgrimes	case VBLK:
4541590Srgrimes	case VCHR: {
4551590Srgrimes		char *name;
4561590Srgrimes
4578874Srgrimes		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
4581590Srgrimes		    S_IFCHR : S_IFBLK)) == NULL))
4591590Srgrimes			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
4601590Srgrimes		else
4611590Srgrimes			printf(" %6s", name);
4621590Srgrimes		break;
4631590Srgrimes	}
4641590Srgrimes	default:
46528948Salex		printf(" %6lu", fst.size);
4661590Srgrimes	}
4671590Srgrimes	rw[0] = '\0';
4681590Srgrimes	if (flag & FREAD)
4691590Srgrimes		strcat(rw, "r");
4701590Srgrimes	if (flag & FWRITE)
4711590Srgrimes		strcat(rw, "w");
4721590Srgrimes	printf(" %2s", rw);
4731590Srgrimes	if (filename && !fsflg)
4741590Srgrimes		printf("  %s", filename);
4751590Srgrimes	putchar('\n');
4761590Srgrimes}
4771590Srgrimes
4781590Srgrimesint
4791590Srgrimesufs_filestat(vp, fsp)
4801590Srgrimes	struct vnode *vp;
4811590Srgrimes	struct filestat *fsp;
4821590Srgrimes{
4831590Srgrimes	struct inode inode;
4841590Srgrimes
4851590Srgrimes	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
48637453Sbde		dprintf(stderr, "can't read inode at %p for pid %d\n",
48737453Sbde		    (void *)VTOI(vp), Pid);
4881590Srgrimes		return 0;
4891590Srgrimes	}
4901590Srgrimes	fsp->fsid = inode.i_dev & 0xffff;
4911590Srgrimes	fsp->fileid = (long)inode.i_number;
4921590Srgrimes	fsp->mode = (mode_t)inode.i_mode;
4931590Srgrimes	fsp->size = (u_long)inode.i_size;
4941590Srgrimes	fsp->rdev = inode.i_rdev;
4951590Srgrimes
4961590Srgrimes	return 1;
4971590Srgrimes}
4981590Srgrimes
4991590Srgrimesint
5001590Srgrimesnfs_filestat(vp, fsp)
5011590Srgrimes	struct vnode *vp;
5021590Srgrimes	struct filestat *fsp;
5031590Srgrimes{
5041590Srgrimes	struct nfsnode nfsnode;
5051590Srgrimes	register mode_t mode;
5061590Srgrimes
5071590Srgrimes	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
50837453Sbde		dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
50937453Sbde		    (void *)VTONFS(vp), Pid);
5101590Srgrimes		return 0;
5111590Srgrimes	}
5121590Srgrimes	fsp->fsid = nfsnode.n_vattr.va_fsid;
5131590Srgrimes	fsp->fileid = nfsnode.n_vattr.va_fileid;
5141590Srgrimes	fsp->size = nfsnode.n_size;
5151590Srgrimes	fsp->rdev = nfsnode.n_vattr.va_rdev;
5161590Srgrimes	mode = (mode_t)nfsnode.n_vattr.va_mode;
5171590Srgrimes	switch (vp->v_type) {
5181590Srgrimes	case VREG:
5191590Srgrimes		mode |= S_IFREG;
5201590Srgrimes		break;
5211590Srgrimes	case VDIR:
5221590Srgrimes		mode |= S_IFDIR;
5231590Srgrimes		break;
5241590Srgrimes	case VBLK:
5251590Srgrimes		mode |= S_IFBLK;
5261590Srgrimes		break;
5271590Srgrimes	case VCHR:
5281590Srgrimes		mode |= S_IFCHR;
5291590Srgrimes		break;
5301590Srgrimes	case VLNK:
5311590Srgrimes		mode |= S_IFLNK;
5321590Srgrimes		break;
5331590Srgrimes	case VSOCK:
5341590Srgrimes		mode |= S_IFSOCK;
5351590Srgrimes		break;
5361590Srgrimes	case VFIFO:
5371590Srgrimes		mode |= S_IFIFO;
5381590Srgrimes		break;
53948463Sru	case VNON:
54048463Sru	case VBAD:
54148463Sru		return 0;
5421590Srgrimes	};
5431590Srgrimes	fsp->mode = mode;
5441590Srgrimes
5451590Srgrimes	return 1;
5461590Srgrimes}
5471590Srgrimes
5481590Srgrimes
5491590Srgrimeschar *
5501590Srgrimesgetmnton(m)
5511590Srgrimes	struct mount *m;
5521590Srgrimes{
5531590Srgrimes	static struct mount mount;
5541590Srgrimes	static struct mtab {
5551590Srgrimes		struct mtab *next;
5561590Srgrimes		struct mount *m;
5571590Srgrimes		char mntonname[MNAMELEN];
5581590Srgrimes	} *mhead = NULL;
5591590Srgrimes	register struct mtab *mt;
5601590Srgrimes
5611590Srgrimes	for (mt = mhead; mt != NULL; mt = mt->next)
5621590Srgrimes		if (m == mt->m)
5631590Srgrimes			return (mt->mntonname);
5641590Srgrimes	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
56537453Sbde		warnx("can't read mount table at %p", (void *)m);
5661590Srgrimes		return (NULL);
5671590Srgrimes	}
56827272Scharnier	if ((mt = malloc(sizeof (struct mtab))) == NULL)
56927272Scharnier		err(1, NULL);
5701590Srgrimes	mt->m = m;
5711590Srgrimes	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
5721590Srgrimes	mt->next = mhead;
5731590Srgrimes	mhead = mt;
5741590Srgrimes	return (mt->mntonname);
5751590Srgrimes}
5761590Srgrimes
5771590Srgrimesvoid
57817808Speterpipetrans(pi, i, flag)
57917808Speter	struct pipe *pi;
58017808Speter	int i;
58117808Speter	int flag;
58217808Speter{
58317808Speter	struct pipe pip;
58417808Speter	char rw[3];
58517808Speter
58617808Speter	PREFIX(i);
58717808Speter
58817808Speter	/* fill in socket */
58917808Speter	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
59037453Sbde		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
59117808Speter		goto bad;
59217808Speter	}
59317808Speter
59417808Speter	printf("* pipe %8x <-> %8x", (int)pi, (int)pip.pipe_peer);
59517808Speter	printf(" %6d", (int)pip.pipe_buffer.cnt);
59617808Speter	rw[0] = '\0';
59717808Speter	if (flag & FREAD)
59817808Speter		strcat(rw, "r");
59917808Speter	if (flag & FWRITE)
60017808Speter		strcat(rw, "w");
60117808Speter	printf(" %2s", rw);
60217808Speter	putchar('\n');
60317808Speter	return;
60417808Speter
60517808Speterbad:
60617808Speter	printf("* error\n");
60717808Speter}
60817808Speter
60917808Spetervoid
6101590Srgrimessocktrans(sock, i)
6111590Srgrimes	struct socket *sock;
6121590Srgrimes	int i;
6131590Srgrimes{
6141590Srgrimes	static char *stypename[] = {
6151590Srgrimes		"unused",	/* 0 */
6161590Srgrimes		"stream", 	/* 1 */
6171590Srgrimes		"dgram",	/* 2 */
6181590Srgrimes		"raw",		/* 3 */
6191590Srgrimes		"rdm",		/* 4 */
6201590Srgrimes		"seqpak"	/* 5 */
6211590Srgrimes	};
6221590Srgrimes#define	STYPEMAX 5
6231590Srgrimes	struct socket	so;
6241590Srgrimes	struct protosw	proto;
6251590Srgrimes	struct domain	dom;
6261590Srgrimes	struct inpcb	inpcb;
6271590Srgrimes	struct unpcb	unpcb;
6281590Srgrimes	int len;
6291590Srgrimes	char dname[32], *strcpy();
6301590Srgrimes
6311590Srgrimes	PREFIX(i);
6321590Srgrimes
6331590Srgrimes	/* fill in socket */
6341590Srgrimes	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
63537453Sbde		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
6361590Srgrimes		goto bad;
6371590Srgrimes	}
6381590Srgrimes
6391590Srgrimes	/* fill in protosw entry */
6401590Srgrimes	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
64137453Sbde		dprintf(stderr, "can't read protosw at %p",
64237453Sbde		    (void *)so.so_proto);
6431590Srgrimes		goto bad;
6441590Srgrimes	}
6451590Srgrimes
6461590Srgrimes	/* fill in domain */
6471590Srgrimes	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
64837453Sbde		dprintf(stderr, "can't read domain at %p\n",
64937453Sbde		    (void *)proto.pr_domain);
6501590Srgrimes		goto bad;
6511590Srgrimes	}
6521590Srgrimes
6531590Srgrimes	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
6541590Srgrimes	    sizeof(dname) - 1)) < 0) {
65537453Sbde		dprintf(stderr, "can't read domain name at %p\n",
65637453Sbde		    (void *)dom.dom_name);
6571590Srgrimes		dname[0] = '\0';
6581590Srgrimes	}
6591590Srgrimes	else
6601590Srgrimes		dname[len] = '\0';
6611590Srgrimes
6621590Srgrimes	if ((u_short)so.so_type > STYPEMAX)
6631590Srgrimes		printf("* %s ?%d", dname, so.so_type);
6641590Srgrimes	else
6651590Srgrimes		printf("* %s %s", dname, stypename[so.so_type]);
6661590Srgrimes
6678874Srgrimes	/*
6681590Srgrimes	 * protocol specific formatting
6691590Srgrimes	 *
6701590Srgrimes	 * Try to find interesting things to print.  For tcp, the interesting
6711590Srgrimes	 * thing is the address of the tcpcb, for udp and others, just the
6721590Srgrimes	 * inpcb (socket pcb).  For unix domain, its the address of the socket
6731590Srgrimes	 * pcb and the address of the connected pcb (if connected).  Otherwise
6741590Srgrimes	 * just print the protocol number and address of the socket itself.
6751590Srgrimes	 * The idea is not to duplicate netstat, but to make available enough
6761590Srgrimes	 * information for further analysis.
6771590Srgrimes	 */
6781590Srgrimes	switch(dom.dom_family) {
6791590Srgrimes	case AF_INET:
6801590Srgrimes		getinetproto(proto.pr_protocol);
6811590Srgrimes		if (proto.pr_protocol == IPPROTO_TCP ) {
6821590Srgrimes			if (so.so_pcb) {
6831590Srgrimes				if (kvm_read(kd, (u_long)so.so_pcb,
6841590Srgrimes				    (char *)&inpcb, sizeof(struct inpcb))
6851590Srgrimes				    != sizeof(struct inpcb)) {
6868874Srgrimes					dprintf(stderr,
68737453Sbde					    "can't read inpcb at %p\n",
68837453Sbde					    (void *)so.so_pcb);
6891590Srgrimes					goto bad;
6901590Srgrimes				}
6911590Srgrimes				printf(" %x", (int)inpcb.inp_ppcb);
6921590Srgrimes			}
6931590Srgrimes		}
6941590Srgrimes		else if (so.so_pcb)
6951590Srgrimes			printf(" %x", (int)so.so_pcb);
6961590Srgrimes		break;
6971590Srgrimes	case AF_UNIX:
6981590Srgrimes		/* print address of pcb and connected pcb */
6991590Srgrimes		if (so.so_pcb) {
7001590Srgrimes			printf(" %x", (int)so.so_pcb);
7011590Srgrimes			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
7021590Srgrimes			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
70337453Sbde				dprintf(stderr, "can't read unpcb at %p\n",
70437453Sbde				    (void *)so.so_pcb);
7051590Srgrimes				goto bad;
7061590Srgrimes			}
7071590Srgrimes			if (unpcb.unp_conn) {
7081590Srgrimes				char shoconn[4], *cp;
7091590Srgrimes
7101590Srgrimes				cp = shoconn;
7111590Srgrimes				if (!(so.so_state & SS_CANTRCVMORE))
7121590Srgrimes					*cp++ = '<';
7131590Srgrimes				*cp++ = '-';
7141590Srgrimes				if (!(so.so_state & SS_CANTSENDMORE))
7151590Srgrimes					*cp++ = '>';
7161590Srgrimes				*cp = '\0';
7171590Srgrimes				printf(" %s %x", shoconn,
7181590Srgrimes				    (int)unpcb.unp_conn);
7191590Srgrimes			}
7201590Srgrimes		}
7211590Srgrimes		break;
7221590Srgrimes	default:
7231590Srgrimes		/* print protocol number and socket address */
7241590Srgrimes		printf(" %d %x", proto.pr_protocol, (int)sock);
7251590Srgrimes	}
7261590Srgrimes	printf("\n");
7271590Srgrimes	return;
7281590Srgrimesbad:
7291590Srgrimes	printf("* error\n");
7301590Srgrimes}
7311590Srgrimes
7321590Srgrimes/*
7331590Srgrimes * getinetproto --
7341590Srgrimes *	print name of protocol number
7351590Srgrimes */
7361590Srgrimesvoid
7371590Srgrimesgetinetproto(number)
7381590Srgrimes	int number;
7391590Srgrimes{
74048463Sru	static int isopen;
74148463Sru	register struct protoent *pe;
7421590Srgrimes
74348463Sru	if (!isopen)
74448463Sru		setprotoent(++isopen);
74548463Sru	if ((pe = getprotobynumber(number)) != NULL)
74648463Sru		printf(" %s", pe->p_name);
74748463Sru	else
7481590Srgrimes		printf(" %d", number);
7491590Srgrimes}
7501590Srgrimes
75117808Speterint
7521590Srgrimesgetfname(filename)
7531590Srgrimes	char *filename;
7541590Srgrimes{
7551590Srgrimes	struct stat statbuf;
7561590Srgrimes	DEVS *cur;
7571590Srgrimes
7581590Srgrimes	if (stat(filename, &statbuf)) {
75927272Scharnier		warn("%s", filename);
7601590Srgrimes		return(0);
7611590Srgrimes	}
76227272Scharnier	if ((cur = malloc(sizeof(DEVS))) == NULL)
76327272Scharnier		err(1, NULL);
7641590Srgrimes	cur->next = devs;
7651590Srgrimes	devs = cur;
7661590Srgrimes
7671590Srgrimes	cur->ino = statbuf.st_ino;
7681590Srgrimes	cur->fsid = statbuf.st_dev & 0xffff;
7691590Srgrimes	cur->name = filename;
7701590Srgrimes	return(1);
7711590Srgrimes}
7721590Srgrimes
7731590Srgrimesvoid
7741590Srgrimesusage()
7751590Srgrimes{
7761590Srgrimes	(void)fprintf(stderr,
7771590Srgrimes "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
7781590Srgrimes	exit(1);
7791590Srgrimes}
780