fstat.c revision 53133
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[] =
4550477Speter  "$FreeBSD: head/usr.bin/fstat/fstat.c 53133 1999-11-13 21:13:25Z green $";
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>
6453133Sgreen#include <sys/conf.h>
651590Srgrimes#define	KERNEL
661590Srgrimes#include <sys/file.h>
671590Srgrimes#include <ufs/ufs/quota.h>
681590Srgrimes#include <ufs/ufs/inode.h>
691590Srgrimes#undef KERNEL
701590Srgrimes#include <sys/mount.h>
719336Sdfr#include <nfs/nfsproto.h>
721590Srgrimes#include <nfs/rpcv2.h>
731590Srgrimes#include <nfs/nfs.h>
741590Srgrimes#include <nfs/nfsnode.h>
751590Srgrimes
761590Srgrimes#include <net/route.h>
771590Srgrimes#include <netinet/in.h>
781590Srgrimes#include <netinet/in_systm.h>
791590Srgrimes#include <netinet/ip.h>
801590Srgrimes#include <netinet/in_pcb.h>
811590Srgrimes
821590Srgrimes#include <ctype.h>
8327272Scharnier#include <err.h>
8418570Sbde#include <fcntl.h>
851590Srgrimes#include <kvm.h>
8623693Speter#include <limits.h>
871590Srgrimes#include <nlist.h>
881590Srgrimes#include <paths.h>
891590Srgrimes#include <pwd.h>
901590Srgrimes#include <stdio.h>
911590Srgrimes#include <stdlib.h>
921590Srgrimes#include <string.h>
9323693Speter#include <unistd.h>
9448463Sru#include <netdb.h>
951590Srgrimes
961590Srgrimes#define	TEXT	-1
971590Srgrimes#define	CDIR	-2
981590Srgrimes#define	RDIR	-3
991590Srgrimes#define	TRACE	-4
1001590Srgrimes
1011590Srgrimestypedef struct devs {
1021590Srgrimes	struct	devs *next;
1031590Srgrimes	long	fsid;
1041590Srgrimes	ino_t	ino;
1051590Srgrimes	char	*name;
1061590Srgrimes} DEVS;
1071590SrgrimesDEVS *devs;
1081590Srgrimes
1091590Srgrimesstruct  filestat {
1101590Srgrimes	long	fsid;
1111590Srgrimes	long	fileid;
1121590Srgrimes	mode_t	mode;
1131590Srgrimes	u_long	size;
1141590Srgrimes	dev_t	rdev;
1151590Srgrimes};
1161590Srgrimes
1171590Srgrimes#ifdef notdef
1181590Srgrimesstruct nlist nl[] = {
1191590Srgrimes	{ "" },
1201590Srgrimes};
1211590Srgrimes#endif
1221590Srgrimes
1231590Srgrimesint 	fsflg,	/* show files on same filesystem as file(s) argument */
1241590Srgrimes	pflg,	/* show files open by a particular pid */
1251590Srgrimes	uflg;	/* show files open by a particular (effective) user */
1261590Srgrimesint 	checkfile; /* true if restricting to particular files or filesystems */
1271590Srgrimesint	nflg;	/* (numerical) display f.s. and rdev as dev_t */
1281590Srgrimesint	vflg;	/* display errors in locating kernel data objects etc... */
1291590Srgrimes
1301590Srgrimes#define dprintf	if (vflg) fprintf
1311590Srgrimes
1321590Srgrimesstruct file **ofiles;	/* buffer of pointers to file structures */
1331590Srgrimesint maxfiles;
1341590Srgrimes#define ALLOC_OFILES(d)	\
1351590Srgrimes	if ((d) > maxfiles) { \
1361590Srgrimes		free(ofiles); \
1371590Srgrimes		ofiles = malloc((d) * sizeof(struct file *)); \
1381590Srgrimes		if (ofiles == NULL) { \
13927272Scharnier			err(1, NULL); \
1401590Srgrimes		} \
1411590Srgrimes		maxfiles = (d); \
1421590Srgrimes	}
1431590Srgrimes
1441590Srgrimes/*
1458874Srgrimes * a kvm_read that returns true if everything is read
1461590Srgrimes */
1471590Srgrimes#define KVM_READ(kaddr, paddr, len) \
1481590Srgrimes	(kvm_read(kd, (u_long)(kaddr), (char *)(paddr), (len)) == (len))
1491590Srgrimes
1501590Srgrimeskvm_t *kd;
1511590Srgrimes
15217808Spetervoid dofiles __P((struct kinfo_proc *kp));
15317808Spetervoid vtrans __P((struct vnode *vp, int i, int flag));
15417808Speterint  ufs_filestat __P((struct vnode *vp, struct filestat *fsp));
15517808Speterint  nfs_filestat __P((struct vnode *vp, struct filestat *fsp));
15617808Speterchar *getmnton __P((struct mount *m));
15717808Spetervoid pipetrans __P((struct pipe *pi, int i, int flag));
15817808Spetervoid socktrans __P((struct socket *sock, int i));
15917808Spetervoid getinetproto __P((int number));
16017808Speterint  getfname __P((char *filename));
16153133Sgreenudev_t dev2udev __P((dev_t dev));
16217808Spetervoid usage __P((void));
1631590Srgrimes
16417808Speter
16517808Speterint
1661590Srgrimesmain(argc, argv)
1671590Srgrimes	int argc;
1681590Srgrimes	char **argv;
1691590Srgrimes{
1701590Srgrimes	register struct passwd *passwd;
1711590Srgrimes	struct kinfo_proc *p, *plast;
1721590Srgrimes	int arg, ch, what;
1731590Srgrimes	char *memf, *nlistf;
17423693Speter	char buf[_POSIX2_LINE_MAX];
1751590Srgrimes	int cnt;
1761590Srgrimes
1771590Srgrimes	arg = 0;
1781590Srgrimes	what = KERN_PROC_ALL;
1791590Srgrimes	nlistf = memf = NULL;
18029592Sdima	while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1)
1811590Srgrimes		switch((char)ch) {
1821590Srgrimes		case 'f':
1831590Srgrimes			fsflg = 1;
1841590Srgrimes			break;
1851590Srgrimes		case 'M':
1861590Srgrimes			memf = optarg;
1871590Srgrimes			break;
1881590Srgrimes		case 'N':
1891590Srgrimes			nlistf = optarg;
1901590Srgrimes			break;
1911590Srgrimes		case 'n':
1921590Srgrimes			nflg = 1;
1931590Srgrimes			break;
1941590Srgrimes		case 'p':
1951590Srgrimes			if (pflg++)
1961590Srgrimes				usage();
1971590Srgrimes			if (!isdigit(*optarg)) {
19827311Scharnier				warnx("-p requires a process id");
1991590Srgrimes				usage();
2001590Srgrimes			}
2011590Srgrimes			what = KERN_PROC_PID;
2021590Srgrimes			arg = atoi(optarg);
2031590Srgrimes			break;
2041590Srgrimes		case 'u':
2051590Srgrimes			if (uflg++)
2061590Srgrimes				usage();
20727272Scharnier			if (!(passwd = getpwnam(optarg)))
20827272Scharnier				errx(1, "%s: unknown uid", optarg);
2091590Srgrimes			what = KERN_PROC_UID;
2101590Srgrimes			arg = passwd->pw_uid;
2111590Srgrimes			break;
2121590Srgrimes		case 'v':
2131590Srgrimes			vflg = 1;
2141590Srgrimes			break;
2151590Srgrimes		case '?':
2161590Srgrimes		default:
2171590Srgrimes			usage();
2181590Srgrimes		}
2191590Srgrimes
2201590Srgrimes	if (*(argv += optind)) {
2211590Srgrimes		for (; *argv; ++argv) {
2221590Srgrimes			if (getfname(*argv))
2231590Srgrimes				checkfile = 1;
2241590Srgrimes		}
2251590Srgrimes		if (!checkfile)	/* file(s) specified, but none accessable */
2261590Srgrimes			exit(1);
2271590Srgrimes	}
2281590Srgrimes
2291590Srgrimes	ALLOC_OFILES(256);	/* reserve space for file pointers */
2301590Srgrimes
2318874Srgrimes	if (fsflg && !checkfile) {
2321590Srgrimes		/* -f with no files means use wd */
2331590Srgrimes		if (getfname(".") == 0)
2341590Srgrimes			exit(1);
2351590Srgrimes		checkfile = 1;
2361590Srgrimes	}
2371590Srgrimes
2381590Srgrimes	/*
2391590Srgrimes	 * Discard setgid privileges if not the running kernel so that bad
2401590Srgrimes	 * guys can't print interesting stuff from kernel memory.
2411590Srgrimes	 */
2421590Srgrimes	if (nlistf != NULL || memf != NULL)
2431590Srgrimes		setgid(getgid());
2441590Srgrimes
24527272Scharnier	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
24627272Scharnier		errx(1, "%s", buf);
2471590Srgrimes#ifdef notdef
24827272Scharnier	if (kvm_nlist(kd, nl) != 0)
24927272Scharnier		errx(1, "no namelist: %s", kvm_geterr(kd));
2501590Srgrimes#endif
25127272Scharnier	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
25227272Scharnier		errx(1, "%s", kvm_geterr(kd));
2531590Srgrimes	if (nflg)
2541590Srgrimes		printf("%s",
2551590Srgrimes"USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
2561590Srgrimes	else
2571590Srgrimes		printf("%s",
2581590Srgrimes"USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
2591590Srgrimes	if (checkfile && fsflg == 0)
2601590Srgrimes		printf(" NAME\n");
2611590Srgrimes	else
2621590Srgrimes		putchar('\n');
2631590Srgrimes
2641590Srgrimes	for (plast = &p[cnt]; p < plast; ++p) {
2651590Srgrimes		if (p->kp_proc.p_stat == SZOMB)
2661590Srgrimes			continue;
2671590Srgrimes		dofiles(p);
2681590Srgrimes	}
2691590Srgrimes	exit(0);
2701590Srgrimes}
2711590Srgrimes
2721590Srgrimeschar	*Uname, *Comm;
2731590Srgrimesint	Pid;
2741590Srgrimes
2751590Srgrimes#define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
2761590Srgrimes	switch(i) { \
2771590Srgrimes	case TEXT: \
2781590Srgrimes		printf(" text"); \
2791590Srgrimes		break; \
2801590Srgrimes	case CDIR: \
2811590Srgrimes		printf("   wd"); \
2821590Srgrimes		break; \
2831590Srgrimes	case RDIR: \
2841590Srgrimes		printf(" root"); \
2851590Srgrimes		break; \
2861590Srgrimes	case TRACE: \
2871590Srgrimes		printf("   tr"); \
2881590Srgrimes		break; \
2891590Srgrimes	default: \
2901590Srgrimes		printf(" %4d", i); \
2911590Srgrimes		break; \
2921590Srgrimes	}
2931590Srgrimes
2941590Srgrimes/*
2951590Srgrimes * print open files attributed to this process
2961590Srgrimes */
2971590Srgrimesvoid
2981590Srgrimesdofiles(kp)
2991590Srgrimes	struct kinfo_proc *kp;
3001590Srgrimes{
30127272Scharnier	int i;
3021590Srgrimes	struct file file;
3031590Srgrimes	struct filedesc0 filed0;
3041590Srgrimes#define	filed	filed0.fd_fd
3051590Srgrimes	struct proc *p = &kp->kp_proc;
3061590Srgrimes	struct eproc *ep = &kp->kp_eproc;
3071590Srgrimes
3081590Srgrimes	Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
3091590Srgrimes	Pid = p->p_pid;
3101590Srgrimes	Comm = p->p_comm;
3111590Srgrimes
3121590Srgrimes	if (p->p_fd == NULL)
3131590Srgrimes		return;
3141590Srgrimes	if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
31537453Sbde		dprintf(stderr, "can't read filedesc at %p for pid %d\n",
31637453Sbde		    (void *)p->p_fd, Pid);
3171590Srgrimes		return;
3181590Srgrimes	}
3191590Srgrimes	/*
3201590Srgrimes	 * root directory vnode, if one
3211590Srgrimes	 */
3221590Srgrimes	if (filed.fd_rdir)
3231590Srgrimes		vtrans(filed.fd_rdir, RDIR, FREAD);
3241590Srgrimes	/*
3251590Srgrimes	 * current working directory vnode
3261590Srgrimes	 */
3271590Srgrimes	vtrans(filed.fd_cdir, CDIR, FREAD);
3281590Srgrimes	/*
3291590Srgrimes	 * ktrace vnode, if one
3301590Srgrimes	 */
3311590Srgrimes	if (p->p_tracep)
3321590Srgrimes		vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
3331590Srgrimes	/*
33417813Speter	 * text vnode, if one
33517813Speter	 */
33617813Speter	if (p->p_textvp)
33717813Speter		vtrans(p->p_textvp, TEXT, FREAD);
33817813Speter	/*
3391590Srgrimes	 * open files
3401590Srgrimes	 */
3411590Srgrimes#define FPSIZE	(sizeof (struct file *))
3421590Srgrimes	ALLOC_OFILES(filed.fd_lastfile+1);
3431590Srgrimes	if (filed.fd_nfiles > NDFILE) {
3441590Srgrimes		if (!KVM_READ(filed.fd_ofiles, ofiles,
3451590Srgrimes		    (filed.fd_lastfile+1) * FPSIZE)) {
3461590Srgrimes			dprintf(stderr,
34737453Sbde			    "can't read file structures at %p for pid %d\n",
34837453Sbde			    (void *)filed.fd_ofiles, Pid);
3491590Srgrimes			return;
3501590Srgrimes		}
3511590Srgrimes	} else
3521590Srgrimes		bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
3531590Srgrimes	for (i = 0; i <= filed.fd_lastfile; i++) {
3541590Srgrimes		if (ofiles[i] == NULL)
3551590Srgrimes			continue;
3561590Srgrimes		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
35737453Sbde			dprintf(stderr, "can't read file %d at %p for pid %d\n",
35837453Sbde			    i, (void *)ofiles[i], Pid);
3591590Srgrimes			continue;
3601590Srgrimes		}
3611590Srgrimes		if (file.f_type == DTYPE_VNODE)
3621590Srgrimes			vtrans((struct vnode *)file.f_data, i, file.f_flag);
3631590Srgrimes		else if (file.f_type == DTYPE_SOCKET) {
3641590Srgrimes			if (checkfile == 0)
3651590Srgrimes				socktrans((struct socket *)file.f_data, i);
3661590Srgrimes		}
36717808Speter#ifdef DTYPE_PIPE
36817808Speter		else if (file.f_type == DTYPE_PIPE) {
36917808Speter			if (checkfile == 0)
37017808Speter				pipetrans((struct pipe *)file.f_data, i,
37117808Speter					file.f_flag);
37217808Speter		}
37317808Speter#endif
3741590Srgrimes		else {
3758874Srgrimes			dprintf(stderr,
3761590Srgrimes				"unknown file type %d for file %d of pid %d\n",
3771590Srgrimes				file.f_type, i, Pid);
3781590Srgrimes		}
3791590Srgrimes	}
3801590Srgrimes}
3811590Srgrimes
3821590Srgrimesvoid
3831590Srgrimesvtrans(vp, i, flag)
3841590Srgrimes	struct vnode *vp;
3851590Srgrimes	int i;
3861590Srgrimes	int flag;
3871590Srgrimes{
3881590Srgrimes	struct vnode vn;
3891590Srgrimes	struct filestat fst;
3901590Srgrimes	char rw[3], mode[15];
3911590Srgrimes	char *badtype = NULL, *filename, *getmnton();
3921590Srgrimes
3931590Srgrimes	filename = badtype = NULL;
3941590Srgrimes	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
39537453Sbde		dprintf(stderr, "can't read vnode at %p for pid %d\n",
39637453Sbde		    (void *)vp, Pid);
3971590Srgrimes		return;
3981590Srgrimes	}
3991590Srgrimes	if (vn.v_type == VNON || vn.v_tag == VT_NON)
4001590Srgrimes		badtype = "none";
4011590Srgrimes	else if (vn.v_type == VBAD)
4021590Srgrimes		badtype = "bad";
4031590Srgrimes	else
4041590Srgrimes		switch (vn.v_tag) {
4051590Srgrimes		case VT_UFS:
4061590Srgrimes			if (!ufs_filestat(&vn, &fst))
4071590Srgrimes				badtype = "error";
4081590Srgrimes			break;
4091590Srgrimes		case VT_MFS:
4101590Srgrimes			if (!ufs_filestat(&vn, &fst))
4111590Srgrimes				badtype = "error";
4121590Srgrimes			break;
4131590Srgrimes		case VT_NFS:
4141590Srgrimes			if (!nfs_filestat(&vn, &fst))
4151590Srgrimes				badtype = "error";
4161590Srgrimes			break;
4171590Srgrimes		default: {
4181590Srgrimes			static char unknown[10];
4191590Srgrimes			sprintf(badtype = unknown, "?(%x)", vn.v_tag);
4201590Srgrimes			break;;
4211590Srgrimes		}
4221590Srgrimes	}
4231590Srgrimes	if (checkfile) {
4241590Srgrimes		int fsmatch = 0;
4251590Srgrimes		register DEVS *d;
4261590Srgrimes
4271590Srgrimes		if (badtype)
4281590Srgrimes			return;
4291590Srgrimes		for (d = devs; d != NULL; d = d->next)
4301590Srgrimes			if (d->fsid == fst.fsid) {
4311590Srgrimes				fsmatch = 1;
4321590Srgrimes				if (d->ino == fst.fileid) {
4331590Srgrimes					filename = d->name;
4341590Srgrimes					break;
4351590Srgrimes				}
4361590Srgrimes			}
4371590Srgrimes		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
4381590Srgrimes			return;
4391590Srgrimes	}
4401590Srgrimes	PREFIX(i);
4411590Srgrimes	if (badtype) {
4421590Srgrimes		(void)printf(" -         -  %10s    -\n", badtype);
4431590Srgrimes		return;
4441590Srgrimes	}
4451590Srgrimes	if (nflg)
4461590Srgrimes		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
4471590Srgrimes	else
4481590Srgrimes		(void)printf(" %-8s", getmnton(vn.v_mount));
4491590Srgrimes	if (nflg)
4501590Srgrimes		(void)sprintf(mode, "%o", fst.mode);
4511590Srgrimes	else
4521590Srgrimes		strmode(fst.mode, mode);
45337453Sbde	(void)printf(" %6ld %10s", fst.fileid, mode);
4541590Srgrimes	switch (vn.v_type) {
4551590Srgrimes	case VBLK:
4561590Srgrimes	case VCHR: {
4571590Srgrimes		char *name;
4581590Srgrimes
4598874Srgrimes		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
4601590Srgrimes		    S_IFCHR : S_IFBLK)) == NULL))
4611590Srgrimes			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
4621590Srgrimes		else
4631590Srgrimes			printf(" %6s", name);
4641590Srgrimes		break;
4651590Srgrimes	}
4661590Srgrimes	default:
46728948Salex		printf(" %6lu", fst.size);
4681590Srgrimes	}
4691590Srgrimes	rw[0] = '\0';
4701590Srgrimes	if (flag & FREAD)
4711590Srgrimes		strcat(rw, "r");
4721590Srgrimes	if (flag & FWRITE)
4731590Srgrimes		strcat(rw, "w");
4741590Srgrimes	printf(" %2s", rw);
4751590Srgrimes	if (filename && !fsflg)
4761590Srgrimes		printf("  %s", filename);
4771590Srgrimes	putchar('\n');
4781590Srgrimes}
4791590Srgrimes
4801590Srgrimesint
4811590Srgrimesufs_filestat(vp, fsp)
4821590Srgrimes	struct vnode *vp;
4831590Srgrimes	struct filestat *fsp;
4841590Srgrimes{
4851590Srgrimes	struct inode inode;
4861590Srgrimes
4871590Srgrimes	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
48837453Sbde		dprintf(stderr, "can't read inode at %p for pid %d\n",
48937453Sbde		    (void *)VTOI(vp), Pid);
4901590Srgrimes		return 0;
4911590Srgrimes	}
49253133Sgreen	/*
49353133Sgreen	 * The st_dev from stat(2) is a udev_t. These kernel structures
49453133Sgreen	 * contain dev_t structures. We need to convert to udev to make
49553133Sgreen	 * comparisons
49653133Sgreen	 */
49753133Sgreen	fsp->fsid = dev2udev(inode.i_dev) & 0xffff;
4981590Srgrimes	fsp->fileid = (long)inode.i_number;
4991590Srgrimes	fsp->mode = (mode_t)inode.i_mode;
5001590Srgrimes	fsp->size = (u_long)inode.i_size;
5011590Srgrimes	fsp->rdev = inode.i_rdev;
5021590Srgrimes
5031590Srgrimes	return 1;
5041590Srgrimes}
5051590Srgrimes
5061590Srgrimesint
5071590Srgrimesnfs_filestat(vp, fsp)
5081590Srgrimes	struct vnode *vp;
5091590Srgrimes	struct filestat *fsp;
5101590Srgrimes{
5111590Srgrimes	struct nfsnode nfsnode;
5121590Srgrimes	register mode_t mode;
5131590Srgrimes
5141590Srgrimes	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
51537453Sbde		dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
51637453Sbde		    (void *)VTONFS(vp), Pid);
5171590Srgrimes		return 0;
5181590Srgrimes	}
5191590Srgrimes	fsp->fsid = nfsnode.n_vattr.va_fsid;
5201590Srgrimes	fsp->fileid = nfsnode.n_vattr.va_fileid;
5211590Srgrimes	fsp->size = nfsnode.n_size;
5221590Srgrimes	fsp->rdev = nfsnode.n_vattr.va_rdev;
5231590Srgrimes	mode = (mode_t)nfsnode.n_vattr.va_mode;
5241590Srgrimes	switch (vp->v_type) {
5251590Srgrimes	case VREG:
5261590Srgrimes		mode |= S_IFREG;
5271590Srgrimes		break;
5281590Srgrimes	case VDIR:
5291590Srgrimes		mode |= S_IFDIR;
5301590Srgrimes		break;
5311590Srgrimes	case VBLK:
5321590Srgrimes		mode |= S_IFBLK;
5331590Srgrimes		break;
5341590Srgrimes	case VCHR:
5351590Srgrimes		mode |= S_IFCHR;
5361590Srgrimes		break;
5371590Srgrimes	case VLNK:
5381590Srgrimes		mode |= S_IFLNK;
5391590Srgrimes		break;
5401590Srgrimes	case VSOCK:
5411590Srgrimes		mode |= S_IFSOCK;
5421590Srgrimes		break;
5431590Srgrimes	case VFIFO:
5441590Srgrimes		mode |= S_IFIFO;
5451590Srgrimes		break;
54648463Sru	case VNON:
54748463Sru	case VBAD:
54848463Sru		return 0;
5491590Srgrimes	};
5501590Srgrimes	fsp->mode = mode;
5511590Srgrimes
5521590Srgrimes	return 1;
5531590Srgrimes}
5541590Srgrimes
5551590Srgrimes
5561590Srgrimeschar *
5571590Srgrimesgetmnton(m)
5581590Srgrimes	struct mount *m;
5591590Srgrimes{
5601590Srgrimes	static struct mount mount;
5611590Srgrimes	static struct mtab {
5621590Srgrimes		struct mtab *next;
5631590Srgrimes		struct mount *m;
5641590Srgrimes		char mntonname[MNAMELEN];
5651590Srgrimes	} *mhead = NULL;
5661590Srgrimes	register struct mtab *mt;
5671590Srgrimes
5681590Srgrimes	for (mt = mhead; mt != NULL; mt = mt->next)
5691590Srgrimes		if (m == mt->m)
5701590Srgrimes			return (mt->mntonname);
5711590Srgrimes	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
57237453Sbde		warnx("can't read mount table at %p", (void *)m);
5731590Srgrimes		return (NULL);
5741590Srgrimes	}
57527272Scharnier	if ((mt = malloc(sizeof (struct mtab))) == NULL)
57627272Scharnier		err(1, NULL);
5771590Srgrimes	mt->m = m;
5781590Srgrimes	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
5791590Srgrimes	mt->next = mhead;
5801590Srgrimes	mhead = mt;
5811590Srgrimes	return (mt->mntonname);
5821590Srgrimes}
5831590Srgrimes
5841590Srgrimesvoid
58517808Speterpipetrans(pi, i, flag)
58617808Speter	struct pipe *pi;
58717808Speter	int i;
58817808Speter	int flag;
58917808Speter{
59017808Speter	struct pipe pip;
59117808Speter	char rw[3];
59217808Speter
59317808Speter	PREFIX(i);
59417808Speter
59517808Speter	/* fill in socket */
59617808Speter	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
59737453Sbde		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
59817808Speter		goto bad;
59917808Speter	}
60017808Speter
60117808Speter	printf("* pipe %8x <-> %8x", (int)pi, (int)pip.pipe_peer);
60217808Speter	printf(" %6d", (int)pip.pipe_buffer.cnt);
60317808Speter	rw[0] = '\0';
60417808Speter	if (flag & FREAD)
60517808Speter		strcat(rw, "r");
60617808Speter	if (flag & FWRITE)
60717808Speter		strcat(rw, "w");
60817808Speter	printf(" %2s", rw);
60917808Speter	putchar('\n');
61017808Speter	return;
61117808Speter
61217808Speterbad:
61317808Speter	printf("* error\n");
61417808Speter}
61517808Speter
61617808Spetervoid
6171590Srgrimessocktrans(sock, i)
6181590Srgrimes	struct socket *sock;
6191590Srgrimes	int i;
6201590Srgrimes{
6211590Srgrimes	static char *stypename[] = {
6221590Srgrimes		"unused",	/* 0 */
6231590Srgrimes		"stream", 	/* 1 */
6241590Srgrimes		"dgram",	/* 2 */
6251590Srgrimes		"raw",		/* 3 */
6261590Srgrimes		"rdm",		/* 4 */
6271590Srgrimes		"seqpak"	/* 5 */
6281590Srgrimes	};
6291590Srgrimes#define	STYPEMAX 5
6301590Srgrimes	struct socket	so;
6311590Srgrimes	struct protosw	proto;
6321590Srgrimes	struct domain	dom;
6331590Srgrimes	struct inpcb	inpcb;
6341590Srgrimes	struct unpcb	unpcb;
6351590Srgrimes	int len;
6361590Srgrimes	char dname[32], *strcpy();
6371590Srgrimes
6381590Srgrimes	PREFIX(i);
6391590Srgrimes
6401590Srgrimes	/* fill in socket */
6411590Srgrimes	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
64237453Sbde		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
6431590Srgrimes		goto bad;
6441590Srgrimes	}
6451590Srgrimes
6461590Srgrimes	/* fill in protosw entry */
6471590Srgrimes	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
64837453Sbde		dprintf(stderr, "can't read protosw at %p",
64937453Sbde		    (void *)so.so_proto);
6501590Srgrimes		goto bad;
6511590Srgrimes	}
6521590Srgrimes
6531590Srgrimes	/* fill in domain */
6541590Srgrimes	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
65537453Sbde		dprintf(stderr, "can't read domain at %p\n",
65637453Sbde		    (void *)proto.pr_domain);
6571590Srgrimes		goto bad;
6581590Srgrimes	}
6591590Srgrimes
6601590Srgrimes	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
6611590Srgrimes	    sizeof(dname) - 1)) < 0) {
66237453Sbde		dprintf(stderr, "can't read domain name at %p\n",
66337453Sbde		    (void *)dom.dom_name);
6641590Srgrimes		dname[0] = '\0';
6651590Srgrimes	}
6661590Srgrimes	else
6671590Srgrimes		dname[len] = '\0';
6681590Srgrimes
6691590Srgrimes	if ((u_short)so.so_type > STYPEMAX)
6701590Srgrimes		printf("* %s ?%d", dname, so.so_type);
6711590Srgrimes	else
6721590Srgrimes		printf("* %s %s", dname, stypename[so.so_type]);
6731590Srgrimes
6748874Srgrimes	/*
6751590Srgrimes	 * protocol specific formatting
6761590Srgrimes	 *
6771590Srgrimes	 * Try to find interesting things to print.  For tcp, the interesting
6781590Srgrimes	 * thing is the address of the tcpcb, for udp and others, just the
6791590Srgrimes	 * inpcb (socket pcb).  For unix domain, its the address of the socket
6801590Srgrimes	 * pcb and the address of the connected pcb (if connected).  Otherwise
6811590Srgrimes	 * just print the protocol number and address of the socket itself.
6821590Srgrimes	 * The idea is not to duplicate netstat, but to make available enough
6831590Srgrimes	 * information for further analysis.
6841590Srgrimes	 */
6851590Srgrimes	switch(dom.dom_family) {
6861590Srgrimes	case AF_INET:
6871590Srgrimes		getinetproto(proto.pr_protocol);
6881590Srgrimes		if (proto.pr_protocol == IPPROTO_TCP ) {
6891590Srgrimes			if (so.so_pcb) {
6901590Srgrimes				if (kvm_read(kd, (u_long)so.so_pcb,
6911590Srgrimes				    (char *)&inpcb, sizeof(struct inpcb))
6921590Srgrimes				    != sizeof(struct inpcb)) {
6938874Srgrimes					dprintf(stderr,
69437453Sbde					    "can't read inpcb at %p\n",
69537453Sbde					    (void *)so.so_pcb);
6961590Srgrimes					goto bad;
6971590Srgrimes				}
6981590Srgrimes				printf(" %x", (int)inpcb.inp_ppcb);
6991590Srgrimes			}
7001590Srgrimes		}
7011590Srgrimes		else if (so.so_pcb)
7021590Srgrimes			printf(" %x", (int)so.so_pcb);
7031590Srgrimes		break;
7041590Srgrimes	case AF_UNIX:
7051590Srgrimes		/* print address of pcb and connected pcb */
7061590Srgrimes		if (so.so_pcb) {
7071590Srgrimes			printf(" %x", (int)so.so_pcb);
7081590Srgrimes			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
7091590Srgrimes			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
71037453Sbde				dprintf(stderr, "can't read unpcb at %p\n",
71137453Sbde				    (void *)so.so_pcb);
7121590Srgrimes				goto bad;
7131590Srgrimes			}
7141590Srgrimes			if (unpcb.unp_conn) {
7151590Srgrimes				char shoconn[4], *cp;
7161590Srgrimes
7171590Srgrimes				cp = shoconn;
7181590Srgrimes				if (!(so.so_state & SS_CANTRCVMORE))
7191590Srgrimes					*cp++ = '<';
7201590Srgrimes				*cp++ = '-';
7211590Srgrimes				if (!(so.so_state & SS_CANTSENDMORE))
7221590Srgrimes					*cp++ = '>';
7231590Srgrimes				*cp = '\0';
7241590Srgrimes				printf(" %s %x", shoconn,
7251590Srgrimes				    (int)unpcb.unp_conn);
7261590Srgrimes			}
7271590Srgrimes		}
7281590Srgrimes		break;
7291590Srgrimes	default:
7301590Srgrimes		/* print protocol number and socket address */
7311590Srgrimes		printf(" %d %x", proto.pr_protocol, (int)sock);
7321590Srgrimes	}
7331590Srgrimes	printf("\n");
7341590Srgrimes	return;
7351590Srgrimesbad:
7361590Srgrimes	printf("* error\n");
7371590Srgrimes}
7381590Srgrimes
73953133Sgreen
7401590Srgrimes/*
74153133Sgreen * Read the specinfo structure in the kernel (as pointed to by a dev_t)
74253133Sgreen * in order to work out the associated udev_t
74353133Sgreen */
74453133Sgreenudev_t
74553133Sgreendev2udev(dev)
74653133Sgreen	dev_t dev;
74753133Sgreen{
74853133Sgreen	struct specinfo si;
74953133Sgreen
75053133Sgreen	if (KVM_READ(dev, &si, sizeof si)) {
75153133Sgreen		return si.si_udev;
75253133Sgreen	} else {
75353133Sgreen		dprintf(stderr, "can't convert dev_t %p to a udev_t\n",
75453133Sgreen		    (void *)dev);
75553133Sgreen		return -1;
75653133Sgreen	}
75753133Sgreen}
75853133Sgreen
75953133Sgreen/*
7601590Srgrimes * getinetproto --
7611590Srgrimes *	print name of protocol number
7621590Srgrimes */
7631590Srgrimesvoid
7641590Srgrimesgetinetproto(number)
7651590Srgrimes	int number;
7661590Srgrimes{
76748463Sru	static int isopen;
76848463Sru	register struct protoent *pe;
7691590Srgrimes
77048463Sru	if (!isopen)
77148463Sru		setprotoent(++isopen);
77248463Sru	if ((pe = getprotobynumber(number)) != NULL)
77348463Sru		printf(" %s", pe->p_name);
77448463Sru	else
7751590Srgrimes		printf(" %d", number);
7761590Srgrimes}
7771590Srgrimes
77817808Speterint
7791590Srgrimesgetfname(filename)
7801590Srgrimes	char *filename;
7811590Srgrimes{
7821590Srgrimes	struct stat statbuf;
7831590Srgrimes	DEVS *cur;
7841590Srgrimes
7851590Srgrimes	if (stat(filename, &statbuf)) {
78627272Scharnier		warn("%s", filename);
7871590Srgrimes		return(0);
7881590Srgrimes	}
78927272Scharnier	if ((cur = malloc(sizeof(DEVS))) == NULL)
79027272Scharnier		err(1, NULL);
7911590Srgrimes	cur->next = devs;
7921590Srgrimes	devs = cur;
7931590Srgrimes
7941590Srgrimes	cur->ino = statbuf.st_ino;
7951590Srgrimes	cur->fsid = statbuf.st_dev & 0xffff;
7961590Srgrimes	cur->name = filename;
7971590Srgrimes	return(1);
7981590Srgrimes}
7991590Srgrimes
8001590Srgrimesvoid
8011590Srgrimesusage()
8021590Srgrimes{
8031590Srgrimes	(void)fprintf(stderr,
8041590Srgrimes "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
8051590Srgrimes	exit(1);
8061590Srgrimes}
807