fstat.c revision 146466
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 146466 2005-05-21 09:55:10Z ru $");
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>
63132477Ssilby#define	_KERNEL
6417808Speter#include <sys/pipe.h>
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
109140958Sphk#define	JDIR	-6
1101590Srgrimes
1111590SrgrimesDEVS *devs;
1121590Srgrimes
1131590Srgrimes#ifdef notdef
1141590Srgrimesstruct nlist nl[] = {
1151590Srgrimes	{ "" },
1161590Srgrimes};
1171590Srgrimes#endif
1181590Srgrimes
1191590Srgrimesint 	fsflg,	/* show files on same filesystem as file(s) argument */
1201590Srgrimes	pflg,	/* show files open by a particular pid */
1211590Srgrimes	uflg;	/* show files open by a particular (effective) user */
1221590Srgrimesint 	checkfile; /* true if restricting to particular files or filesystems */
1231590Srgrimesint	nflg;	/* (numerical) display f.s. and rdev as dev_t */
1241590Srgrimesint	vflg;	/* display errors in locating kernel data objects etc... */
12559029Sgreenint	mflg;	/* include memory-mapped files */
1261590Srgrimes
1271590Srgrimes
1281590Srgrimesstruct file **ofiles;	/* buffer of pointers to file structures */
1291590Srgrimesint maxfiles;
1301590Srgrimes#define ALLOC_OFILES(d)	\
1311590Srgrimes	if ((d) > maxfiles) { \
1321590Srgrimes		free(ofiles); \
1331590Srgrimes		ofiles = malloc((d) * sizeof(struct file *)); \
1341590Srgrimes		if (ofiles == NULL) { \
13527272Scharnier			err(1, NULL); \
1361590Srgrimes		} \
1371590Srgrimes		maxfiles = (d); \
1381590Srgrimes	}
1391590Srgrimes
14097946Sdeschar *memf, *nlistf;
1411590Srgrimeskvm_t *kd;
1421590Srgrimes
14397946Sdesstatic void fstat_kvm(int, int);
14497946Sdesstatic void fstat_sysctl(int, int);
14592920Simpvoid dofiles(struct kinfo_proc *kp);
14692920Simpvoid dommap(struct kinfo_proc *kp);
14792920Simpvoid vtrans(struct vnode *vp, int i, int flag);
14892920Simpint  ufs_filestat(struct vnode *vp, struct filestat *fsp);
14992920Simpint  nfs_filestat(struct vnode *vp, struct filestat *fsp);
15092920Simpint  devfs_filestat(struct vnode *vp, struct filestat *fsp);
15192920Simpchar *getmnton(struct mount *m);
15292920Simpvoid pipetrans(struct pipe *pi, int i, int flag);
15392920Simpvoid socktrans(struct socket *sock, int i);
15492920Simpvoid getinetproto(int number);
15593427Sdwmaloneint  getfname(const char *filename);
15692920Simpvoid usage(void);
1571590Srgrimes
15817808Speter
15917808Speterint
160131293Sdwmalonemain(int argc, char **argv)
1611590Srgrimes{
16293427Sdwmalone	struct passwd *passwd;
1631590Srgrimes	int arg, ch, what;
1641590Srgrimes
1651590Srgrimes	arg = 0;
1661590Srgrimes	what = KERN_PROC_ALL;
1671590Srgrimes	nlistf = memf = NULL;
16859029Sgreen	while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1)
1691590Srgrimes		switch((char)ch) {
1701590Srgrimes		case 'f':
1711590Srgrimes			fsflg = 1;
1721590Srgrimes			break;
1731590Srgrimes		case 'M':
1741590Srgrimes			memf = optarg;
1751590Srgrimes			break;
1761590Srgrimes		case 'N':
1771590Srgrimes			nlistf = optarg;
1781590Srgrimes			break;
17959029Sgreen		case 'm':
18059029Sgreen			mflg = 1;
18159029Sgreen			break;
1821590Srgrimes		case 'n':
1831590Srgrimes			nflg = 1;
1841590Srgrimes			break;
1851590Srgrimes		case 'p':
1861590Srgrimes			if (pflg++)
1871590Srgrimes				usage();
1881590Srgrimes			if (!isdigit(*optarg)) {
18927311Scharnier				warnx("-p requires a process id");
1901590Srgrimes				usage();
1911590Srgrimes			}
1921590Srgrimes			what = KERN_PROC_PID;
1931590Srgrimes			arg = atoi(optarg);
1941590Srgrimes			break;
1951590Srgrimes		case 'u':
1961590Srgrimes			if (uflg++)
1971590Srgrimes				usage();
19827272Scharnier			if (!(passwd = getpwnam(optarg)))
19927272Scharnier				errx(1, "%s: unknown uid", optarg);
2001590Srgrimes			what = KERN_PROC_UID;
2011590Srgrimes			arg = passwd->pw_uid;
2021590Srgrimes			break;
2031590Srgrimes		case 'v':
2041590Srgrimes			vflg = 1;
2051590Srgrimes			break;
2061590Srgrimes		case '?':
2071590Srgrimes		default:
2081590Srgrimes			usage();
2091590Srgrimes		}
2101590Srgrimes
2111590Srgrimes	if (*(argv += optind)) {
2121590Srgrimes		for (; *argv; ++argv) {
2131590Srgrimes			if (getfname(*argv))
2141590Srgrimes				checkfile = 1;
2151590Srgrimes		}
2161590Srgrimes		if (!checkfile)	/* file(s) specified, but none accessable */
2171590Srgrimes			exit(1);
2181590Srgrimes	}
2191590Srgrimes
2208874Srgrimes	if (fsflg && !checkfile) {
2211590Srgrimes		/* -f with no files means use wd */
2221590Srgrimes		if (getfname(".") == 0)
2231590Srgrimes			exit(1);
2241590Srgrimes		checkfile = 1;
2251590Srgrimes	}
2261590Srgrimes
22797946Sdes	if (memf != NULL)
22897946Sdes		fstat_kvm(what, arg);
22997946Sdes	else
23097946Sdes		fstat_sysctl(what, arg);
23197946Sdes	exit(0);
23297946Sdes}
23397946Sdes
23497946Sdesstatic void
23597946Sdesprint_header(void)
23697946Sdes{
23797946Sdes
23897946Sdes	if (nflg)
23997946Sdes		printf("%s",
24097946Sdes"USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
24197946Sdes	else
24297946Sdes		printf("%s",
24397946Sdes"USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
24497946Sdes	if (checkfile && fsflg == 0)
24597946Sdes		printf(" NAME\n");
24697946Sdes	else
24797946Sdes		putchar('\n');
24897946Sdes}
24997946Sdes
25097946Sdesstatic void
25197946Sdesfstat_kvm(int what, int arg)
25297946Sdes{
25397946Sdes	struct kinfo_proc *p, *plast;
25497946Sdes	char buf[_POSIX2_LINE_MAX];
25597946Sdes	int cnt;
25697946Sdes
25797946Sdes	ALLOC_OFILES(256);	/* reserve space for file pointers */
25897946Sdes
2591590Srgrimes	/*
2601590Srgrimes	 * Discard setgid privileges if not the running kernel so that bad
2611590Srgrimes	 * guys can't print interesting stuff from kernel memory.
2621590Srgrimes	 */
2631590Srgrimes	if (nlistf != NULL || memf != NULL)
2641590Srgrimes		setgid(getgid());
2651590Srgrimes
26627272Scharnier	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
26727272Scharnier		errx(1, "%s", buf);
26882664Sru	setgid(getgid());
2691590Srgrimes#ifdef notdef
27027272Scharnier	if (kvm_nlist(kd, nl) != 0)
27127272Scharnier		errx(1, "no namelist: %s", kvm_geterr(kd));
2721590Srgrimes#endif
27327272Scharnier	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
27427272Scharnier		errx(1, "%s", kvm_geterr(kd));
275116717Smaxim	print_header();
2761590Srgrimes	for (plast = &p[cnt]; p < plast; ++p) {
27769896Smckusick		if (p->ki_stat == SZOMB)
2781590Srgrimes			continue;
2791590Srgrimes		dofiles(p);
28059029Sgreen		if (mflg)
28159029Sgreen			dommap(p);
2821590Srgrimes	}
2831590Srgrimes}
2841590Srgrimes
28597946Sdesstatic void
28697946Sdesfstat_sysctl(int what, int arg)
28797946Sdes{
28897946Sdes
28997946Sdes	/* not yet implemented */
29097946Sdes	fstat_kvm(what, arg);
29197946Sdes}
29297946Sdes
29393427Sdwmaloneconst char	*Uname, *Comm;
2941590Srgrimesint	Pid;
2951590Srgrimes
2961590Srgrimes#define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
2971590Srgrimes	switch(i) { \
2981590Srgrimes	case TEXT: \
2991590Srgrimes		printf(" text"); \
3001590Srgrimes		break; \
3011590Srgrimes	case CDIR: \
3021590Srgrimes		printf("   wd"); \
3031590Srgrimes		break; \
3041590Srgrimes	case RDIR: \
3051590Srgrimes		printf(" root"); \
3061590Srgrimes		break; \
3071590Srgrimes	case TRACE: \
3081590Srgrimes		printf("   tr"); \
3091590Srgrimes		break; \
31059029Sgreen	case MMAP: \
31159029Sgreen		printf(" mmap"); \
31259029Sgreen		break; \
313140958Sphk	case JDIR: \
314140958Sphk		printf(" jail"); \
315140958Sphk		break; \
3161590Srgrimes	default: \
3171590Srgrimes		printf(" %4d", i); \
3181590Srgrimes		break; \
3191590Srgrimes	}
3201590Srgrimes
3211590Srgrimes/*
3221590Srgrimes * print open files attributed to this process
3231590Srgrimes */
3241590Srgrimesvoid
325131293Sdwmalonedofiles(struct kinfo_proc *kp)
3261590Srgrimes{
32727272Scharnier	int i;
3281590Srgrimes	struct file file;
329137352Sphk	struct filedesc filed;
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;
337137352Sphk	if (!KVM_READ(kp->ki_fd, &filed, sizeof (filed))) {
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	/*
352140958Sphk	 * jail root, if any.
353140958Sphk	 */
354140958Sphk	if (filed.fd_jdir)
355140958Sphk		vtrans(filed.fd_jdir, JDIR, FREAD);
356140958Sphk	/*
3571590Srgrimes	 * ktrace vnode, if one
3581590Srgrimes	 */
35969896Smckusick	if (kp->ki_tracep)
36069896Smckusick		vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE);
3611590Srgrimes	/*
36217813Speter	 * text vnode, if one
36317813Speter	 */
36469896Smckusick	if (kp->ki_textvp)
36569896Smckusick		vtrans(kp->ki_textvp, TEXT, FREAD);
36617813Speter	/*
3671590Srgrimes	 * open files
3681590Srgrimes	 */
3691590Srgrimes#define FPSIZE	(sizeof (struct file *))
370140958Sphk#define MAX_LASTFILE	(0x1000000)
371140958Sphk
372140958Sphk	/* Sanity check on filed.fd_lastfile */
373140958Sphk	if (filed.fd_lastfile <= -1 || filed.fd_lastfile > MAX_LASTFILE)
374140958Sphk		return;
375140958Sphk
3761590Srgrimes	ALLOC_OFILES(filed.fd_lastfile+1);
377137352Sphk	if (!KVM_READ(filed.fd_ofiles, ofiles,
378137352Sphk	    (filed.fd_lastfile+1) * FPSIZE)) {
379137352Sphk		dprintf(stderr,
380137352Sphk		    "can't read file structures at %p for pid %d\n",
381137352Sphk		    (void *)filed.fd_ofiles, Pid);
382137352Sphk		return;
383137352Sphk	}
3841590Srgrimes	for (i = 0; i <= filed.fd_lastfile; i++) {
3851590Srgrimes		if (ofiles[i] == NULL)
3861590Srgrimes			continue;
3871590Srgrimes		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
38837453Sbde			dprintf(stderr, "can't read file %d at %p for pid %d\n",
38937453Sbde			    i, (void *)ofiles[i], Pid);
3901590Srgrimes			continue;
3911590Srgrimes		}
3921590Srgrimes		if (file.f_type == DTYPE_VNODE)
393140078Sssouhlal			vtrans(file.f_vnode, i, file.f_flag);
3941590Srgrimes		else if (file.f_type == DTYPE_SOCKET) {
3951590Srgrimes			if (checkfile == 0)
396109153Sdillon				socktrans(file.f_data, i);
3971590Srgrimes		}
39817808Speter#ifdef DTYPE_PIPE
39917808Speter		else if (file.f_type == DTYPE_PIPE) {
40017808Speter			if (checkfile == 0)
401109153Sdillon				pipetrans(file.f_data, i, file.f_flag);
40217808Speter		}
40317808Speter#endif
40478401Sroam#ifdef DTYPE_FIFO
40578401Sroam		else if (file.f_type == DTYPE_FIFO) {
40678401Sroam			if (checkfile == 0)
407140078Sssouhlal				vtrans(file.f_vnode, i, file.f_flag);
40878401Sroam		}
40978401Sroam#endif
4101590Srgrimes		else {
4118874Srgrimes			dprintf(stderr,
41278401Sroam			    "unknown file type %d for file %d of pid %d\n",
41378401Sroam			    file.f_type, i, Pid);
4141590Srgrimes		}
4151590Srgrimes	}
4161590Srgrimes}
4171590Srgrimes
4181590Srgrimesvoid
419131293Sdwmalonedommap(struct kinfo_proc *kp)
42059029Sgreen{
42169896Smckusick	vm_map_t map;
42259029Sgreen	struct vmspace vmspace;
42359029Sgreen	struct vm_map_entry entry;
42459029Sgreen	vm_map_entry_t entryp;
42559029Sgreen	struct vm_object object;
42659029Sgreen	vm_object_t objp;
42759029Sgreen	int prot, fflags;
42859029Sgreen
42969896Smckusick	if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) {
43069896Smckusick		dprintf(stderr,
43169896Smckusick		    "can't read vmspace at %p for pid %d\n",
43269896Smckusick		    (void *)kp->ki_vmspace, Pid);
43359029Sgreen		return;
43459029Sgreen	}
43559029Sgreen	map = &vmspace.vm_map;
43659029Sgreen
43772527Siedowse	for (entryp = map->header.next;
43872527Siedowse	    entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) {
43959029Sgreen		if (!KVM_READ(entryp, &entry, sizeof(entry))) {
44059029Sgreen			dprintf(stderr,
44159029Sgreen			    "can't read vm_map_entry at %p for pid %d\n",
44259029Sgreen			    (void *)entryp, Pid);
44359029Sgreen			return;
44459029Sgreen		}
44559029Sgreen
44659029Sgreen		if (entry.eflags & MAP_ENTRY_IS_SUB_MAP)
44759029Sgreen			continue;
44859029Sgreen
44959029Sgreen		if ((objp = entry.object.vm_object) == NULL)
45059029Sgreen			continue;
45159029Sgreen
45259029Sgreen		for (; objp; objp = object.backing_object) {
45359029Sgreen			if (!KVM_READ(objp, &object, sizeof(object))) {
45459029Sgreen				dprintf(stderr,
45559029Sgreen				    "can't read vm_object at %p for pid %d\n",
45659029Sgreen				    (void *)objp, Pid);
45759029Sgreen				return;
45859029Sgreen			}
45959029Sgreen		}
46059029Sgreen
46159029Sgreen		prot = entry.protection;
46259029Sgreen		fflags = (prot & VM_PROT_READ ? FREAD : 0) |
46359029Sgreen		    (prot & VM_PROT_WRITE ? FWRITE : 0);
46459029Sgreen
46559029Sgreen		switch (object.type) {
46659029Sgreen		case OBJT_VNODE:
46759029Sgreen			vtrans((struct vnode *)object.handle, MMAP, fflags);
46859029Sgreen			break;
46959029Sgreen		default:
47059029Sgreen			break;
47159029Sgreen		}
47259029Sgreen	}
47359029Sgreen}
47459029Sgreen
47559029Sgreenvoid
476131293Sdwmalonevtrans(struct vnode *vp, int i, int flag)
4771590Srgrimes{
4781590Srgrimes	struct vnode vn;
4791590Srgrimes	struct filestat fst;
480103325Snjl	char rw[3], mode[15], tagstr[12], *tagptr;
48193427Sdwmalone	const char *badtype, *filename;
4821590Srgrimes
4831590Srgrimes	filename = badtype = NULL;
4841590Srgrimes	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
48537453Sbde		dprintf(stderr, "can't read vnode at %p for pid %d\n",
48637453Sbde		    (void *)vp, Pid);
4871590Srgrimes		return;
4881590Srgrimes	}
489103325Snjl	if (!KVM_READ(&vp->v_tag, &tagptr, sizeof tagptr) ||
490103325Snjl	    !KVM_READ(tagptr, tagstr, sizeof tagstr)) {
491103325Snjl		dprintf(stderr, "can't read v_tag at %p for pid %d\n",
492103325Snjl		    (void *)vp, Pid);
493103325Snjl		return;
494103325Snjl	}
495103325Snjl	tagstr[sizeof(tagstr) - 1] = '\0';
496103325Snjl	if (vn.v_type == VNON)
4971590Srgrimes		badtype = "none";
4981590Srgrimes	else if (vn.v_type == VBAD)
4991590Srgrimes		badtype = "bad";
500103325Snjl	else {
501103325Snjl		if (!strcmp("ufs", tagstr)) {
5021590Srgrimes			if (!ufs_filestat(&vn, &fst))
5031590Srgrimes				badtype = "error";
504103325Snjl		} else if (!strcmp("devfs", tagstr)) {
50588051Sgreen			if (!devfs_filestat(&vn, &fst))
50688051Sgreen				badtype = "error";
507103325Snjl		} else if (!strcmp("nfs", tagstr)) {
5081590Srgrimes			if (!nfs_filestat(&vn, &fst))
5091590Srgrimes				badtype = "error";
510103325Snjl		} else if (!strcmp("msdosfs", tagstr)) {
51158125Sgreen			if (!msdosfs_filestat(&vn, &fst))
51258125Sgreen				badtype = "error";
513103325Snjl		} else if (!strcmp("isofs", tagstr)) {
51458125Sgreen			if (!isofs_filestat(&vn, &fst))
51558125Sgreen				badtype = "error";
516103325Snjl		} else {
517103325Snjl			static char unknown[32];
518103325Snjl			snprintf(unknown, sizeof unknown, "?(%s)", tagstr);
51993427Sdwmalone			badtype = unknown;
5201590Srgrimes		}
5211590Srgrimes	}
5221590Srgrimes	if (checkfile) {
5231590Srgrimes		int fsmatch = 0;
52493427Sdwmalone		DEVS *d;
5251590Srgrimes
5261590Srgrimes		if (badtype)
5271590Srgrimes			return;
5281590Srgrimes		for (d = devs; d != NULL; d = d->next)
5291590Srgrimes			if (d->fsid == fst.fsid) {
5301590Srgrimes				fsmatch = 1;
5311590Srgrimes				if (d->ino == fst.fileid) {
5321590Srgrimes					filename = d->name;
5331590Srgrimes					break;
5341590Srgrimes				}
5351590Srgrimes			}
5361590Srgrimes		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
5371590Srgrimes			return;
5381590Srgrimes	}
5391590Srgrimes	PREFIX(i);
5401590Srgrimes	if (badtype) {
5411590Srgrimes		(void)printf(" -         -  %10s    -\n", badtype);
5421590Srgrimes		return;
5431590Srgrimes	}
5441590Srgrimes	if (nflg)
5451590Srgrimes		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
5461590Srgrimes	else
5471590Srgrimes		(void)printf(" %-8s", getmnton(vn.v_mount));
5481590Srgrimes	if (nflg)
5491590Srgrimes		(void)sprintf(mode, "%o", fst.mode);
5501590Srgrimes	else
5511590Srgrimes		strmode(fst.mode, mode);
55237453Sbde	(void)printf(" %6ld %10s", fst.fileid, mode);
5531590Srgrimes	switch (vn.v_type) {
5541590Srgrimes	case VBLK:
5551590Srgrimes	case VCHR: {
5561590Srgrimes		char *name;
5571590Srgrimes
5588874Srgrimes		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
5591590Srgrimes		    S_IFCHR : S_IFBLK)) == NULL))
5601590Srgrimes			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
5611590Srgrimes		else
5621590Srgrimes			printf(" %6s", name);
5631590Srgrimes		break;
5641590Srgrimes	}
5651590Srgrimes	default:
56628948Salex		printf(" %6lu", fst.size);
5671590Srgrimes	}
5681590Srgrimes	rw[0] = '\0';
5691590Srgrimes	if (flag & FREAD)
5701590Srgrimes		strcat(rw, "r");
5711590Srgrimes	if (flag & FWRITE)
5721590Srgrimes		strcat(rw, "w");
5731590Srgrimes	printf(" %2s", rw);
5741590Srgrimes	if (filename && !fsflg)
5751590Srgrimes		printf("  %s", filename);
5761590Srgrimes	putchar('\n');
5771590Srgrimes}
5781590Srgrimes
5791590Srgrimesint
580131293Sdwmaloneufs_filestat(struct vnode *vp, struct filestat *fsp)
5811590Srgrimes{
5821590Srgrimes	struct inode inode;
5831590Srgrimes
5841590Srgrimes	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
58537453Sbde		dprintf(stderr, "can't read inode at %p for pid %d\n",
58637453Sbde		    (void *)VTOI(vp), Pid);
5871590Srgrimes		return 0;
5881590Srgrimes	}
58953133Sgreen	/*
590130640Sphk	 * The st_dev from stat(2) is a dev_t. These kernel structures
591130640Sphk	 * contain cdev pointers. We need to convert to dev_t to make
59253133Sgreen	 * comparisons
59353133Sgreen	 */
59486100Sdwmalone	fsp->fsid = dev2udev(inode.i_dev);
5951590Srgrimes	fsp->fileid = (long)inode.i_number;
5961590Srgrimes	fsp->mode = (mode_t)inode.i_mode;
5971590Srgrimes	fsp->size = (u_long)inode.i_size;
59898542Smckusick#if should_be_but_is_hard
599116780Sjmg	/* XXX - need to load i_ump and i_din[12] from kernel memory */
600116780Sjmg	if (inode.i_ump->um_fstype == UFS1)
601116780Sjmg		fsp->rdev = inode.i_din1->di_rdev;
602116780Sjmg	else
603116780Sjmg		fsp->rdev = inode.i_din2->di_rdev;
60498542Smckusick#else
60598542Smckusick	fsp->rdev = 0;
60698542Smckusick#endif
6071590Srgrimes
6081590Srgrimes	return 1;
6091590Srgrimes}
6101590Srgrimes
6111590Srgrimesint
612131293Sdwmalonedevfs_filestat(struct vnode *vp, 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
643131293Sdwmalonenfs_filestat(struct vnode *vp, struct filestat *fsp)
6441590Srgrimes{
6451590Srgrimes	struct nfsnode nfsnode;
64693427Sdwmalone	mode_t mode;
6471590Srgrimes
6481590Srgrimes	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
64937453Sbde		dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
65037453Sbde		    (void *)VTONFS(vp), Pid);
6511590Srgrimes		return 0;
6521590Srgrimes	}
6531590Srgrimes	fsp->fsid = nfsnode.n_vattr.va_fsid;
6541590Srgrimes	fsp->fileid = nfsnode.n_vattr.va_fileid;
6551590Srgrimes	fsp->size = nfsnode.n_size;
6561590Srgrimes	fsp->rdev = nfsnode.n_vattr.va_rdev;
6571590Srgrimes	mode = (mode_t)nfsnode.n_vattr.va_mode;
6581590Srgrimes	switch (vp->v_type) {
6591590Srgrimes	case VREG:
6601590Srgrimes		mode |= S_IFREG;
6611590Srgrimes		break;
6621590Srgrimes	case VDIR:
6631590Srgrimes		mode |= S_IFDIR;
6641590Srgrimes		break;
6651590Srgrimes	case VBLK:
6661590Srgrimes		mode |= S_IFBLK;
6671590Srgrimes		break;
6681590Srgrimes	case VCHR:
6691590Srgrimes		mode |= S_IFCHR;
6701590Srgrimes		break;
6711590Srgrimes	case VLNK:
6721590Srgrimes		mode |= S_IFLNK;
6731590Srgrimes		break;
6741590Srgrimes	case VSOCK:
6751590Srgrimes		mode |= S_IFSOCK;
6761590Srgrimes		break;
6771590Srgrimes	case VFIFO:
6781590Srgrimes		mode |= S_IFIFO;
6791590Srgrimes		break;
68048463Sru	case VNON:
68148463Sru	case VBAD:
68248463Sru		return 0;
6831590Srgrimes	};
6841590Srgrimes	fsp->mode = mode;
6851590Srgrimes
6861590Srgrimes	return 1;
6871590Srgrimes}
6881590Srgrimes
6891590Srgrimes
6901590Srgrimeschar *
691131293Sdwmalonegetmnton(struct mount *m)
6921590Srgrimes{
6931590Srgrimes	static struct mount mount;
6941590Srgrimes	static struct mtab {
6951590Srgrimes		struct mtab *next;
6961590Srgrimes		struct mount *m;
6971590Srgrimes		char mntonname[MNAMELEN];
6981590Srgrimes	} *mhead = NULL;
69993427Sdwmalone	struct mtab *mt;
7001590Srgrimes
7011590Srgrimes	for (mt = mhead; mt != NULL; mt = mt->next)
7021590Srgrimes		if (m == mt->m)
7031590Srgrimes			return (mt->mntonname);
7041590Srgrimes	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
70537453Sbde		warnx("can't read mount table at %p", (void *)m);
7061590Srgrimes		return (NULL);
7071590Srgrimes	}
70827272Scharnier	if ((mt = malloc(sizeof (struct mtab))) == NULL)
70927272Scharnier		err(1, NULL);
7101590Srgrimes	mt->m = m;
7111590Srgrimes	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
7121590Srgrimes	mt->next = mhead;
7131590Srgrimes	mhead = mt;
7141590Srgrimes	return (mt->mntonname);
7151590Srgrimes}
7161590Srgrimes
7171590Srgrimesvoid
718131293Sdwmalonepipetrans(struct pipe *pi, int i, int flag)
71917808Speter{
72017808Speter	struct pipe pip;
72117808Speter	char rw[3];
72217808Speter
72317808Speter	PREFIX(i);
72417808Speter
72517808Speter	/* fill in socket */
72617808Speter	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
72737453Sbde		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
72817808Speter		goto bad;
72917808Speter	}
73017808Speter
73180355Smjacob	printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
73217808Speter	printf(" %6d", (int)pip.pipe_buffer.cnt);
73317808Speter	rw[0] = '\0';
73417808Speter	if (flag & FREAD)
73517808Speter		strcat(rw, "r");
73617808Speter	if (flag & FWRITE)
73717808Speter		strcat(rw, "w");
73817808Speter	printf(" %2s", rw);
73917808Speter	putchar('\n');
74017808Speter	return;
74117808Speter
74217808Speterbad:
74317808Speter	printf("* error\n");
74417808Speter}
74517808Speter
74617808Spetervoid
747131293Sdwmalonesocktrans(struct socket *sock, int i)
7481590Srgrimes{
74993427Sdwmalone	static const char *stypename[] = {
7501590Srgrimes		"unused",	/* 0 */
7511590Srgrimes		"stream", 	/* 1 */
7521590Srgrimes		"dgram",	/* 2 */
7531590Srgrimes		"raw",		/* 3 */
7541590Srgrimes		"rdm",		/* 4 */
7551590Srgrimes		"seqpak"	/* 5 */
7561590Srgrimes	};
7571590Srgrimes#define	STYPEMAX 5
7581590Srgrimes	struct socket	so;
7591590Srgrimes	struct protosw	proto;
7601590Srgrimes	struct domain	dom;
7611590Srgrimes	struct inpcb	inpcb;
7621590Srgrimes	struct unpcb	unpcb;
7631590Srgrimes	int len;
76493427Sdwmalone	char dname[32];
7651590Srgrimes
7661590Srgrimes	PREFIX(i);
7671590Srgrimes
7681590Srgrimes	/* fill in socket */
7691590Srgrimes	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
77037453Sbde		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
7711590Srgrimes		goto bad;
7721590Srgrimes	}
7731590Srgrimes
7741590Srgrimes	/* fill in protosw entry */
7751590Srgrimes	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
77637453Sbde		dprintf(stderr, "can't read protosw at %p",
77737453Sbde		    (void *)so.so_proto);
7781590Srgrimes		goto bad;
7791590Srgrimes	}
7801590Srgrimes
7811590Srgrimes	/* fill in domain */
7821590Srgrimes	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
78337453Sbde		dprintf(stderr, "can't read domain at %p\n",
78437453Sbde		    (void *)proto.pr_domain);
7851590Srgrimes		goto bad;
7861590Srgrimes	}
7871590Srgrimes
7881590Srgrimes	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
7891590Srgrimes	    sizeof(dname) - 1)) < 0) {
79037453Sbde		dprintf(stderr, "can't read domain name at %p\n",
79137453Sbde		    (void *)dom.dom_name);
7921590Srgrimes		dname[0] = '\0';
7931590Srgrimes	}
7941590Srgrimes	else
7951590Srgrimes		dname[len] = '\0';
7961590Srgrimes
7971590Srgrimes	if ((u_short)so.so_type > STYPEMAX)
7981590Srgrimes		printf("* %s ?%d", dname, so.so_type);
7991590Srgrimes	else
8001590Srgrimes		printf("* %s %s", dname, stypename[so.so_type]);
8011590Srgrimes
8028874Srgrimes	/*
8031590Srgrimes	 * protocol specific formatting
8041590Srgrimes	 *
8051590Srgrimes	 * Try to find interesting things to print.  For tcp, the interesting
8061590Srgrimes	 * thing is the address of the tcpcb, for udp and others, just the
8071590Srgrimes	 * inpcb (socket pcb).  For unix domain, its the address of the socket
8081590Srgrimes	 * pcb and the address of the connected pcb (if connected).  Otherwise
8091590Srgrimes	 * just print the protocol number and address of the socket itself.
8101590Srgrimes	 * The idea is not to duplicate netstat, but to make available enough
8111590Srgrimes	 * information for further analysis.
8121590Srgrimes	 */
8131590Srgrimes	switch(dom.dom_family) {
8141590Srgrimes	case AF_INET:
81557345Sshin	case AF_INET6:
8161590Srgrimes		getinetproto(proto.pr_protocol);
8171590Srgrimes		if (proto.pr_protocol == IPPROTO_TCP ) {
8181590Srgrimes			if (so.so_pcb) {
8191590Srgrimes				if (kvm_read(kd, (u_long)so.so_pcb,
8201590Srgrimes				    (char *)&inpcb, sizeof(struct inpcb))
8211590Srgrimes				    != sizeof(struct inpcb)) {
8228874Srgrimes					dprintf(stderr,
82337453Sbde					    "can't read inpcb at %p\n",
82437453Sbde					    (void *)so.so_pcb);
8251590Srgrimes					goto bad;
8261590Srgrimes				}
82780355Smjacob				printf(" %lx", (u_long)inpcb.inp_ppcb);
8281590Srgrimes			}
8291590Srgrimes		}
8301590Srgrimes		else if (so.so_pcb)
83180355Smjacob			printf(" %lx", (u_long)so.so_pcb);
8321590Srgrimes		break;
8331590Srgrimes	case AF_UNIX:
8341590Srgrimes		/* print address of pcb and connected pcb */
8351590Srgrimes		if (so.so_pcb) {
83680355Smjacob			printf(" %lx", (u_long)so.so_pcb);
8371590Srgrimes			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
8381590Srgrimes			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
83937453Sbde				dprintf(stderr, "can't read unpcb at %p\n",
84037453Sbde				    (void *)so.so_pcb);
8411590Srgrimes				goto bad;
8421590Srgrimes			}
8431590Srgrimes			if (unpcb.unp_conn) {
8441590Srgrimes				char shoconn[4], *cp;
8451590Srgrimes
8461590Srgrimes				cp = shoconn;
847130489Srwatson				if (!(so.so_rcv.sb_state & SBS_CANTRCVMORE))
8481590Srgrimes					*cp++ = '<';
8491590Srgrimes				*cp++ = '-';
850130489Srwatson				if (!(so.so_snd.sb_state & SBS_CANTSENDMORE))
8511590Srgrimes					*cp++ = '>';
8521590Srgrimes				*cp = '\0';
85380355Smjacob				printf(" %s %lx", shoconn,
85480355Smjacob				    (u_long)unpcb.unp_conn);
8551590Srgrimes			}
8561590Srgrimes		}
8571590Srgrimes		break;
8581590Srgrimes	default:
8591590Srgrimes		/* print protocol number and socket address */
86080355Smjacob		printf(" %d %lx", proto.pr_protocol, (u_long)sock);
8611590Srgrimes	}
8621590Srgrimes	printf("\n");
8631590Srgrimes	return;
8641590Srgrimesbad:
8651590Srgrimes	printf("* error\n");
8661590Srgrimes}
8671590Srgrimes
86853133Sgreen
8691590Srgrimes/*
870130640Sphk * Read the cdev structure in the kernel in order to work out the
871130640Sphk * associated dev_t
87253133Sgreen */
873130640Sphkdev_t
874131293Sdwmalonedev2udev(struct cdev *dev)
87553133Sgreen{
876104043Sphk	struct cdev si;
87753133Sgreen
87853133Sgreen	if (KVM_READ(dev, &si, sizeof si)) {
879143651Sphk		/* XXX: FIXME! */
880143651Sphk		return 0;
88153133Sgreen	} else {
882131293Sdwmalone		dprintf(stderr, "can't convert cdev *%p to a dev_t\n", dev);
88353133Sgreen		return -1;
88453133Sgreen	}
88553133Sgreen}
88653133Sgreen
88753133Sgreen/*
8881590Srgrimes * getinetproto --
8891590Srgrimes *	print name of protocol number
8901590Srgrimes */
8911590Srgrimesvoid
892131293Sdwmalonegetinetproto(int number)
8931590Srgrimes{
89448463Sru	static int isopen;
89593427Sdwmalone	struct protoent *pe;
8961590Srgrimes
89748463Sru	if (!isopen)
89848463Sru		setprotoent(++isopen);
89948463Sru	if ((pe = getprotobynumber(number)) != NULL)
90048463Sru		printf(" %s", pe->p_name);
90148463Sru	else
9021590Srgrimes		printf(" %d", number);
9031590Srgrimes}
9041590Srgrimes
90517808Speterint
906131293Sdwmalonegetfname(const char *filename)
9071590Srgrimes{
9081590Srgrimes	struct stat statbuf;
9091590Srgrimes	DEVS *cur;
9101590Srgrimes
9111590Srgrimes	if (stat(filename, &statbuf)) {
91227272Scharnier		warn("%s", filename);
9131590Srgrimes		return(0);
9141590Srgrimes	}
91527272Scharnier	if ((cur = malloc(sizeof(DEVS))) == NULL)
91627272Scharnier		err(1, NULL);
9171590Srgrimes	cur->next = devs;
9181590Srgrimes	devs = cur;
9191590Srgrimes
9201590Srgrimes	cur->ino = statbuf.st_ino;
92186100Sdwmalone	cur->fsid = statbuf.st_dev;
9221590Srgrimes	cur->name = filename;
9231590Srgrimes	return(1);
9241590Srgrimes}
9251590Srgrimes
9261590Srgrimesvoid
927131293Sdwmaloneusage(void)
9281590Srgrimes{
9291590Srgrimes	(void)fprintf(stderr,
930146466Sru "usage: fstat [-fmnv] [-M core] [-N system] [-p pid] [-u user] [file ...]\n");
9311590Srgrimes	exit(1);
9321590Srgrimes}
933