xref: /freebsd/usr.bin/fstat/fstat.c (revision 677e00c04ea6530f8c36fbefcf38c0bfd639f7f3)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1988, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
69b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
79b50d902SRodney W. Grimes  * are met:
89b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
99b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
109b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
119b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
129b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
139b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
149b50d902SRodney W. Grimes  *    must display the following acknowledgement:
159b50d902SRodney W. Grimes  *	This product includes software developed by the University of
169b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
179b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
189b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
199b50d902SRodney W. Grimes  *    without specific prior written permission.
209b50d902SRodney W. Grimes  *
219b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
229b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
239b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
249b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
259b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
269b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
279b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
289b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
299b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
309b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
319b50d902SRodney W. Grimes  * SUCH DAMAGE.
329b50d902SRodney W. Grimes  */
339b50d902SRodney W. Grimes 
349b50d902SRodney W. Grimes #ifndef lint
35c1e65942SPhilippe Charnier static const char copyright[] =
369b50d902SRodney W. Grimes "@(#) Copyright (c) 1988, 1993\n\
379b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
389b50d902SRodney W. Grimes #endif /* not lint */
399b50d902SRodney W. Grimes 
409b50d902SRodney W. Grimes #ifndef lint
41c1e65942SPhilippe Charnier #if 0
42df3f5d9dSPeter Wemm static char sccsid[] = "@(#)fstat.c	8.3 (Berkeley) 5/2/95";
43c1e65942SPhilippe Charnier #endif
44c1e65942SPhilippe Charnier static const char rcsid[] =
45c3aac50fSPeter Wemm   "$FreeBSD$";
469b50d902SRodney W. Grimes #endif /* not lint */
479b50d902SRodney W. Grimes 
489b50d902SRodney W. Grimes #include <sys/param.h>
499b50d902SRodney W. Grimes #include <sys/time.h>
509b50d902SRodney W. Grimes #include <sys/proc.h>
519b50d902SRodney W. Grimes #include <sys/user.h>
529b50d902SRodney W. Grimes #include <sys/stat.h>
539b50d902SRodney W. Grimes #include <sys/vnode.h>
549b50d902SRodney W. Grimes #include <sys/socket.h>
559b50d902SRodney W. Grimes #include <sys/socketvar.h>
569b50d902SRodney W. Grimes #include <sys/domain.h>
579b50d902SRodney W. Grimes #include <sys/protosw.h>
583492514dSMark Murray #include <sys/un.h>
599b50d902SRodney W. Grimes #include <sys/unpcb.h>
609b50d902SRodney W. Grimes #include <sys/sysctl.h>
619b50d902SRodney W. Grimes #include <sys/filedesc.h>
62d2bccb9fSDavid Greenman #include <sys/queue.h>
631c17fc99SPeter Wemm #include <sys/pipe.h>
6438cd1ee7SBrian Feldman #include <sys/conf.h>
65c4473420SPeter Wemm #define	_KERNEL
669b50d902SRodney W. Grimes #include <sys/file.h>
679b50d902SRodney W. Grimes #include <ufs/ufs/quota.h>
689b50d902SRodney W. Grimes #include <ufs/ufs/inode.h>
69c4473420SPeter Wemm #undef _KERNEL
709b50d902SRodney W. Grimes #include <sys/mount.h>
71a62dc406SDoug Rabson #include <nfs/nfsproto.h>
729b50d902SRodney W. Grimes #include <nfs/rpcv2.h>
739b50d902SRodney W. Grimes #include <nfs/nfs.h>
749b50d902SRodney W. Grimes #include <nfs/nfsnode.h>
759b50d902SRodney W. Grimes 
769b50d902SRodney W. Grimes #include <net/route.h>
779b50d902SRodney W. Grimes #include <netinet/in.h>
789b50d902SRodney W. Grimes #include <netinet/in_systm.h>
799b50d902SRodney W. Grimes #include <netinet/ip.h>
809b50d902SRodney W. Grimes #include <netinet/in_pcb.h>
819b50d902SRodney W. Grimes 
829b50d902SRodney W. Grimes #include <ctype.h>
83c1e65942SPhilippe Charnier #include <err.h>
845d98ce75SBruce Evans #include <fcntl.h>
859b50d902SRodney W. Grimes #include <kvm.h>
86df3f5d9dSPeter Wemm #include <limits.h>
879b50d902SRodney W. Grimes #include <nlist.h>
889b50d902SRodney W. Grimes #include <paths.h>
899b50d902SRodney W. Grimes #include <pwd.h>
909b50d902SRodney W. Grimes #include <stdio.h>
919b50d902SRodney W. Grimes #include <stdlib.h>
929b50d902SRodney W. Grimes #include <string.h>
93df3f5d9dSPeter Wemm #include <unistd.h>
940b7a57ddSRuslan Ermilov #include <netdb.h>
959b50d902SRodney W. Grimes 
969b50d902SRodney W. Grimes #define	TEXT	-1
979b50d902SRodney W. Grimes #define	CDIR	-2
989b50d902SRodney W. Grimes #define	RDIR	-3
999b50d902SRodney W. Grimes #define	TRACE	-4
1009b50d902SRodney W. Grimes 
1019b50d902SRodney W. Grimes typedef struct devs {
1029b50d902SRodney W. Grimes 	struct	devs *next;
1039b50d902SRodney W. Grimes 	long	fsid;
1049b50d902SRodney W. Grimes 	ino_t	ino;
1059b50d902SRodney W. Grimes 	char	*name;
1069b50d902SRodney W. Grimes } DEVS;
1079b50d902SRodney W. Grimes DEVS *devs;
1089b50d902SRodney W. Grimes 
1099b50d902SRodney W. Grimes struct  filestat {
1109b50d902SRodney W. Grimes 	long	fsid;
1119b50d902SRodney W. Grimes 	long	fileid;
1129b50d902SRodney W. Grimes 	mode_t	mode;
1139b50d902SRodney W. Grimes 	u_long	size;
1149b50d902SRodney W. Grimes 	dev_t	rdev;
1159b50d902SRodney W. Grimes };
1169b50d902SRodney W. Grimes 
1179b50d902SRodney W. Grimes #ifdef notdef
1189b50d902SRodney W. Grimes struct nlist nl[] = {
1199b50d902SRodney W. Grimes 	{ "" },
1209b50d902SRodney W. Grimes };
1219b50d902SRodney W. Grimes #endif
1229b50d902SRodney W. Grimes 
1239b50d902SRodney W. Grimes int 	fsflg,	/* show files on same filesystem as file(s) argument */
1249b50d902SRodney W. Grimes 	pflg,	/* show files open by a particular pid */
1259b50d902SRodney W. Grimes 	uflg;	/* show files open by a particular (effective) user */
1269b50d902SRodney W. Grimes int 	checkfile; /* true if restricting to particular files or filesystems */
1279b50d902SRodney W. Grimes int	nflg;	/* (numerical) display f.s. and rdev as dev_t */
1289b50d902SRodney W. Grimes int	vflg;	/* display errors in locating kernel data objects etc... */
1299b50d902SRodney W. Grimes 
1309b50d902SRodney W. Grimes #define dprintf	if (vflg) fprintf
1319b50d902SRodney W. Grimes 
1329b50d902SRodney W. Grimes struct file **ofiles;	/* buffer of pointers to file structures */
1339b50d902SRodney W. Grimes int maxfiles;
1349b50d902SRodney W. Grimes #define ALLOC_OFILES(d)	\
1359b50d902SRodney W. Grimes 	if ((d) > maxfiles) { \
1369b50d902SRodney W. Grimes 		free(ofiles); \
1379b50d902SRodney W. Grimes 		ofiles = malloc((d) * sizeof(struct file *)); \
1389b50d902SRodney W. Grimes 		if (ofiles == NULL) { \
139c1e65942SPhilippe Charnier 			err(1, NULL); \
1409b50d902SRodney W. Grimes 		} \
1419b50d902SRodney W. Grimes 		maxfiles = (d); \
1429b50d902SRodney W. Grimes 	}
1439b50d902SRodney W. Grimes 
1449b50d902SRodney W. Grimes /*
1459b50d902SRodney W. Grimes  * a kvm_read that returns true if everything is read
1469b50d902SRodney W. Grimes  */
1479b50d902SRodney W. Grimes #define KVM_READ(kaddr, paddr, len) \
1489b50d902SRodney W. Grimes 	(kvm_read(kd, (u_long)(kaddr), (char *)(paddr), (len)) == (len))
1499b50d902SRodney W. Grimes 
1509b50d902SRodney W. Grimes kvm_t *kd;
1519b50d902SRodney W. Grimes 
1521c17fc99SPeter Wemm void dofiles __P((struct kinfo_proc *kp));
1531c17fc99SPeter Wemm void vtrans __P((struct vnode *vp, int i, int flag));
1541c17fc99SPeter Wemm int  ufs_filestat __P((struct vnode *vp, struct filestat *fsp));
1551c17fc99SPeter Wemm int  nfs_filestat __P((struct vnode *vp, struct filestat *fsp));
1561c17fc99SPeter Wemm char *getmnton __P((struct mount *m));
1571c17fc99SPeter Wemm void pipetrans __P((struct pipe *pi, int i, int flag));
1581c17fc99SPeter Wemm void socktrans __P((struct socket *sock, int i));
1591c17fc99SPeter Wemm void getinetproto __P((int number));
1601c17fc99SPeter Wemm int  getfname __P((char *filename));
16138cd1ee7SBrian Feldman udev_t dev2udev __P((dev_t dev));
1621c17fc99SPeter Wemm void usage __P((void));
1639b50d902SRodney W. Grimes 
1641c17fc99SPeter Wemm 
1651c17fc99SPeter Wemm int
1669b50d902SRodney W. Grimes main(argc, argv)
1679b50d902SRodney W. Grimes 	int argc;
1689b50d902SRodney W. Grimes 	char **argv;
1699b50d902SRodney W. Grimes {
1709b50d902SRodney W. Grimes 	register struct passwd *passwd;
1719b50d902SRodney W. Grimes 	struct kinfo_proc *p, *plast;
1729b50d902SRodney W. Grimes 	int arg, ch, what;
1739b50d902SRodney W. Grimes 	char *memf, *nlistf;
174df3f5d9dSPeter Wemm 	char buf[_POSIX2_LINE_MAX];
1759b50d902SRodney W. Grimes 	int cnt;
1769b50d902SRodney W. Grimes 
1779b50d902SRodney W. Grimes 	arg = 0;
1789b50d902SRodney W. Grimes 	what = KERN_PROC_ALL;
1799b50d902SRodney W. Grimes 	nlistf = memf = NULL;
180d8ccfba4SDima Ruban 	while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1)
1819b50d902SRodney W. Grimes 		switch((char)ch) {
1829b50d902SRodney W. Grimes 		case 'f':
1839b50d902SRodney W. Grimes 			fsflg = 1;
1849b50d902SRodney W. Grimes 			break;
1859b50d902SRodney W. Grimes 		case 'M':
1869b50d902SRodney W. Grimes 			memf = optarg;
1879b50d902SRodney W. Grimes 			break;
1889b50d902SRodney W. Grimes 		case 'N':
1899b50d902SRodney W. Grimes 			nlistf = optarg;
1909b50d902SRodney W. Grimes 			break;
1919b50d902SRodney W. Grimes 		case 'n':
1929b50d902SRodney W. Grimes 			nflg = 1;
1939b50d902SRodney W. Grimes 			break;
1949b50d902SRodney W. Grimes 		case 'p':
1959b50d902SRodney W. Grimes 			if (pflg++)
1969b50d902SRodney W. Grimes 				usage();
1979b50d902SRodney W. Grimes 			if (!isdigit(*optarg)) {
198c1e65942SPhilippe Charnier 				warnx("-p requires a process id");
1999b50d902SRodney W. Grimes 				usage();
2009b50d902SRodney W. Grimes 			}
2019b50d902SRodney W. Grimes 			what = KERN_PROC_PID;
2029b50d902SRodney W. Grimes 			arg = atoi(optarg);
2039b50d902SRodney W. Grimes 			break;
2049b50d902SRodney W. Grimes 		case 'u':
2059b50d902SRodney W. Grimes 			if (uflg++)
2069b50d902SRodney W. Grimes 				usage();
207c1e65942SPhilippe Charnier 			if (!(passwd = getpwnam(optarg)))
208c1e65942SPhilippe Charnier 				errx(1, "%s: unknown uid", optarg);
2099b50d902SRodney W. Grimes 			what = KERN_PROC_UID;
2109b50d902SRodney W. Grimes 			arg = passwd->pw_uid;
2119b50d902SRodney W. Grimes 			break;
2129b50d902SRodney W. Grimes 		case 'v':
2139b50d902SRodney W. Grimes 			vflg = 1;
2149b50d902SRodney W. Grimes 			break;
2159b50d902SRodney W. Grimes 		case '?':
2169b50d902SRodney W. Grimes 		default:
2179b50d902SRodney W. Grimes 			usage();
2189b50d902SRodney W. Grimes 		}
2199b50d902SRodney W. Grimes 
2209b50d902SRodney W. Grimes 	if (*(argv += optind)) {
2219b50d902SRodney W. Grimes 		for (; *argv; ++argv) {
2229b50d902SRodney W. Grimes 			if (getfname(*argv))
2239b50d902SRodney W. Grimes 				checkfile = 1;
2249b50d902SRodney W. Grimes 		}
2259b50d902SRodney W. Grimes 		if (!checkfile)	/* file(s) specified, but none accessable */
2269b50d902SRodney W. Grimes 			exit(1);
2279b50d902SRodney W. Grimes 	}
2289b50d902SRodney W. Grimes 
2299b50d902SRodney W. Grimes 	ALLOC_OFILES(256);	/* reserve space for file pointers */
2309b50d902SRodney W. Grimes 
2319b50d902SRodney W. Grimes 	if (fsflg && !checkfile) {
2329b50d902SRodney W. Grimes 		/* -f with no files means use wd */
2339b50d902SRodney W. Grimes 		if (getfname(".") == 0)
2349b50d902SRodney W. Grimes 			exit(1);
2359b50d902SRodney W. Grimes 		checkfile = 1;
2369b50d902SRodney W. Grimes 	}
2379b50d902SRodney W. Grimes 
2389b50d902SRodney W. Grimes 	/*
2399b50d902SRodney W. Grimes 	 * Discard setgid privileges if not the running kernel so that bad
2409b50d902SRodney W. Grimes 	 * guys can't print interesting stuff from kernel memory.
2419b50d902SRodney W. Grimes 	 */
2429b50d902SRodney W. Grimes 	if (nlistf != NULL || memf != NULL)
2439b50d902SRodney W. Grimes 		setgid(getgid());
2449b50d902SRodney W. Grimes 
245c1e65942SPhilippe Charnier 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
246c1e65942SPhilippe Charnier 		errx(1, "%s", buf);
2479b50d902SRodney W. Grimes #ifdef notdef
248c1e65942SPhilippe Charnier 	if (kvm_nlist(kd, nl) != 0)
249c1e65942SPhilippe Charnier 		errx(1, "no namelist: %s", kvm_geterr(kd));
2509b50d902SRodney W. Grimes #endif
251c1e65942SPhilippe Charnier 	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
252c1e65942SPhilippe Charnier 		errx(1, "%s", kvm_geterr(kd));
2539b50d902SRodney W. Grimes 	if (nflg)
2549b50d902SRodney W. Grimes 		printf("%s",
2559b50d902SRodney W. Grimes "USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
2569b50d902SRodney W. Grimes 	else
2579b50d902SRodney W. Grimes 		printf("%s",
2589b50d902SRodney W. Grimes "USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
2599b50d902SRodney W. Grimes 	if (checkfile && fsflg == 0)
2609b50d902SRodney W. Grimes 		printf(" NAME\n");
2619b50d902SRodney W. Grimes 	else
2629b50d902SRodney W. Grimes 		putchar('\n');
2639b50d902SRodney W. Grimes 
2649b50d902SRodney W. Grimes 	for (plast = &p[cnt]; p < plast; ++p) {
2659b50d902SRodney W. Grimes 		if (p->kp_proc.p_stat == SZOMB)
2669b50d902SRodney W. Grimes 			continue;
2679b50d902SRodney W. Grimes 		dofiles(p);
2689b50d902SRodney W. Grimes 	}
2699b50d902SRodney W. Grimes 	exit(0);
2709b50d902SRodney W. Grimes }
2719b50d902SRodney W. Grimes 
2729b50d902SRodney W. Grimes char	*Uname, *Comm;
2739b50d902SRodney W. Grimes int	Pid;
2749b50d902SRodney W. Grimes 
2759b50d902SRodney W. Grimes #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
2769b50d902SRodney W. Grimes 	switch(i) { \
2779b50d902SRodney W. Grimes 	case TEXT: \
2789b50d902SRodney W. Grimes 		printf(" text"); \
2799b50d902SRodney W. Grimes 		break; \
2809b50d902SRodney W. Grimes 	case CDIR: \
2819b50d902SRodney W. Grimes 		printf("   wd"); \
2829b50d902SRodney W. Grimes 		break; \
2839b50d902SRodney W. Grimes 	case RDIR: \
2849b50d902SRodney W. Grimes 		printf(" root"); \
2859b50d902SRodney W. Grimes 		break; \
2869b50d902SRodney W. Grimes 	case TRACE: \
2879b50d902SRodney W. Grimes 		printf("   tr"); \
2889b50d902SRodney W. Grimes 		break; \
2899b50d902SRodney W. Grimes 	default: \
2909b50d902SRodney W. Grimes 		printf(" %4d", i); \
2919b50d902SRodney W. Grimes 		break; \
2929b50d902SRodney W. Grimes 	}
2939b50d902SRodney W. Grimes 
2949b50d902SRodney W. Grimes /*
2959b50d902SRodney W. Grimes  * print open files attributed to this process
2969b50d902SRodney W. Grimes  */
2979b50d902SRodney W. Grimes void
2989b50d902SRodney W. Grimes dofiles(kp)
2999b50d902SRodney W. Grimes 	struct kinfo_proc *kp;
3009b50d902SRodney W. Grimes {
301c1e65942SPhilippe Charnier 	int i;
3029b50d902SRodney W. Grimes 	struct file file;
3039b50d902SRodney W. Grimes 	struct filedesc0 filed0;
3049b50d902SRodney W. Grimes #define	filed	filed0.fd_fd
3059b50d902SRodney W. Grimes 	struct proc *p = &kp->kp_proc;
3069b50d902SRodney W. Grimes 	struct eproc *ep = &kp->kp_eproc;
3079b50d902SRodney W. Grimes 
3089b50d902SRodney W. Grimes 	Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
3099b50d902SRodney W. Grimes 	Pid = p->p_pid;
3109b50d902SRodney W. Grimes 	Comm = p->p_comm;
3119b50d902SRodney W. Grimes 
3129b50d902SRodney W. Grimes 	if (p->p_fd == NULL)
3139b50d902SRodney W. Grimes 		return;
3149b50d902SRodney W. Grimes 	if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
31522694ebaSBruce Evans 		dprintf(stderr, "can't read filedesc at %p for pid %d\n",
31622694ebaSBruce Evans 		    (void *)p->p_fd, Pid);
3179b50d902SRodney W. Grimes 		return;
3189b50d902SRodney W. Grimes 	}
3199b50d902SRodney W. Grimes 	/*
3209b50d902SRodney W. Grimes 	 * root directory vnode, if one
3219b50d902SRodney W. Grimes 	 */
3229b50d902SRodney W. Grimes 	if (filed.fd_rdir)
3239b50d902SRodney W. Grimes 		vtrans(filed.fd_rdir, RDIR, FREAD);
3249b50d902SRodney W. Grimes 	/*
3259b50d902SRodney W. Grimes 	 * current working directory vnode
3269b50d902SRodney W. Grimes 	 */
3279b50d902SRodney W. Grimes 	vtrans(filed.fd_cdir, CDIR, FREAD);
3289b50d902SRodney W. Grimes 	/*
3299b50d902SRodney W. Grimes 	 * ktrace vnode, if one
3309b50d902SRodney W. Grimes 	 */
3319b50d902SRodney W. Grimes 	if (p->p_tracep)
3329b50d902SRodney W. Grimes 		vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
3339b50d902SRodney W. Grimes 	/*
334ca7e3117SPeter Wemm 	 * text vnode, if one
335ca7e3117SPeter Wemm 	 */
336ca7e3117SPeter Wemm 	if (p->p_textvp)
337ca7e3117SPeter Wemm 		vtrans(p->p_textvp, TEXT, FREAD);
338ca7e3117SPeter Wemm 	/*
3399b50d902SRodney W. Grimes 	 * open files
3409b50d902SRodney W. Grimes 	 */
3419b50d902SRodney W. Grimes #define FPSIZE	(sizeof (struct file *))
3429b50d902SRodney W. Grimes 	ALLOC_OFILES(filed.fd_lastfile+1);
3439b50d902SRodney W. Grimes 	if (filed.fd_nfiles > NDFILE) {
3449b50d902SRodney W. Grimes 		if (!KVM_READ(filed.fd_ofiles, ofiles,
3459b50d902SRodney W. Grimes 		    (filed.fd_lastfile+1) * FPSIZE)) {
3469b50d902SRodney W. Grimes 			dprintf(stderr,
34722694ebaSBruce Evans 			    "can't read file structures at %p for pid %d\n",
34822694ebaSBruce Evans 			    (void *)filed.fd_ofiles, Pid);
3499b50d902SRodney W. Grimes 			return;
3509b50d902SRodney W. Grimes 		}
3519b50d902SRodney W. Grimes 	} else
3529b50d902SRodney W. Grimes 		bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
3539b50d902SRodney W. Grimes 	for (i = 0; i <= filed.fd_lastfile; i++) {
3549b50d902SRodney W. Grimes 		if (ofiles[i] == NULL)
3559b50d902SRodney W. Grimes 			continue;
3569b50d902SRodney W. Grimes 		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
35722694ebaSBruce Evans 			dprintf(stderr, "can't read file %d at %p for pid %d\n",
35822694ebaSBruce Evans 			    i, (void *)ofiles[i], Pid);
3599b50d902SRodney W. Grimes 			continue;
3609b50d902SRodney W. Grimes 		}
3619b50d902SRodney W. Grimes 		if (file.f_type == DTYPE_VNODE)
3629b50d902SRodney W. Grimes 			vtrans((struct vnode *)file.f_data, i, file.f_flag);
3639b50d902SRodney W. Grimes 		else if (file.f_type == DTYPE_SOCKET) {
3649b50d902SRodney W. Grimes 			if (checkfile == 0)
3659b50d902SRodney W. Grimes 				socktrans((struct socket *)file.f_data, i);
3669b50d902SRodney W. Grimes 		}
3671c17fc99SPeter Wemm #ifdef DTYPE_PIPE
3681c17fc99SPeter Wemm 		else if (file.f_type == DTYPE_PIPE) {
3691c17fc99SPeter Wemm 			if (checkfile == 0)
3701c17fc99SPeter Wemm 				pipetrans((struct pipe *)file.f_data, i,
3711c17fc99SPeter Wemm 					file.f_flag);
3721c17fc99SPeter Wemm 		}
3731c17fc99SPeter Wemm #endif
3749b50d902SRodney W. Grimes 		else {
3759b50d902SRodney W. Grimes 			dprintf(stderr,
3769b50d902SRodney W. Grimes 				"unknown file type %d for file %d of pid %d\n",
3779b50d902SRodney W. Grimes 				file.f_type, i, Pid);
3789b50d902SRodney W. Grimes 		}
3799b50d902SRodney W. Grimes 	}
3809b50d902SRodney W. Grimes }
3819b50d902SRodney W. Grimes 
3829b50d902SRodney W. Grimes void
3839b50d902SRodney W. Grimes vtrans(vp, i, flag)
3849b50d902SRodney W. Grimes 	struct vnode *vp;
3859b50d902SRodney W. Grimes 	int i;
3869b50d902SRodney W. Grimes 	int flag;
3879b50d902SRodney W. Grimes {
3889b50d902SRodney W. Grimes 	struct vnode vn;
3899b50d902SRodney W. Grimes 	struct filestat fst;
3909b50d902SRodney W. Grimes 	char rw[3], mode[15];
3919b50d902SRodney W. Grimes 	char *badtype = NULL, *filename, *getmnton();
3929b50d902SRodney W. Grimes 
3939b50d902SRodney W. Grimes 	filename = badtype = NULL;
3949b50d902SRodney W. Grimes 	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
39522694ebaSBruce Evans 		dprintf(stderr, "can't read vnode at %p for pid %d\n",
39622694ebaSBruce Evans 		    (void *)vp, Pid);
3979b50d902SRodney W. Grimes 		return;
3989b50d902SRodney W. Grimes 	}
3999b50d902SRodney W. Grimes 	if (vn.v_type == VNON || vn.v_tag == VT_NON)
4009b50d902SRodney W. Grimes 		badtype = "none";
4019b50d902SRodney W. Grimes 	else if (vn.v_type == VBAD)
4029b50d902SRodney W. Grimes 		badtype = "bad";
4039b50d902SRodney W. Grimes 	else
4049b50d902SRodney W. Grimes 		switch (vn.v_tag) {
4059b50d902SRodney W. Grimes 		case VT_UFS:
4069b50d902SRodney W. Grimes 			if (!ufs_filestat(&vn, &fst))
4079b50d902SRodney W. Grimes 				badtype = "error";
4089b50d902SRodney W. Grimes 			break;
4099b50d902SRodney W. Grimes 		case VT_MFS:
4109b50d902SRodney W. Grimes 			if (!ufs_filestat(&vn, &fst))
4119b50d902SRodney W. Grimes 				badtype = "error";
4129b50d902SRodney W. Grimes 			break;
4139b50d902SRodney W. Grimes 		case VT_NFS:
4149b50d902SRodney W. Grimes 			if (!nfs_filestat(&vn, &fst))
4159b50d902SRodney W. Grimes 				badtype = "error";
4169b50d902SRodney W. Grimes 			break;
4179b50d902SRodney W. Grimes 		default: {
4189b50d902SRodney W. Grimes 			static char unknown[10];
4199b50d902SRodney W. Grimes 			sprintf(badtype = unknown, "?(%x)", vn.v_tag);
4209b50d902SRodney W. Grimes 			break;;
4219b50d902SRodney W. Grimes 		}
4229b50d902SRodney W. Grimes 	}
4239b50d902SRodney W. Grimes 	if (checkfile) {
4249b50d902SRodney W. Grimes 		int fsmatch = 0;
4259b50d902SRodney W. Grimes 		register DEVS *d;
4269b50d902SRodney W. Grimes 
4279b50d902SRodney W. Grimes 		if (badtype)
4289b50d902SRodney W. Grimes 			return;
4299b50d902SRodney W. Grimes 		for (d = devs; d != NULL; d = d->next)
4309b50d902SRodney W. Grimes 			if (d->fsid == fst.fsid) {
4319b50d902SRodney W. Grimes 				fsmatch = 1;
4329b50d902SRodney W. Grimes 				if (d->ino == fst.fileid) {
4339b50d902SRodney W. Grimes 					filename = d->name;
4349b50d902SRodney W. Grimes 					break;
4359b50d902SRodney W. Grimes 				}
4369b50d902SRodney W. Grimes 			}
4379b50d902SRodney W. Grimes 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
4389b50d902SRodney W. Grimes 			return;
4399b50d902SRodney W. Grimes 	}
4409b50d902SRodney W. Grimes 	PREFIX(i);
4419b50d902SRodney W. Grimes 	if (badtype) {
4429b50d902SRodney W. Grimes 		(void)printf(" -         -  %10s    -\n", badtype);
4439b50d902SRodney W. Grimes 		return;
4449b50d902SRodney W. Grimes 	}
4459b50d902SRodney W. Grimes 	if (nflg)
4469b50d902SRodney W. Grimes 		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
4479b50d902SRodney W. Grimes 	else
4489b50d902SRodney W. Grimes 		(void)printf(" %-8s", getmnton(vn.v_mount));
4499b50d902SRodney W. Grimes 	if (nflg)
4509b50d902SRodney W. Grimes 		(void)sprintf(mode, "%o", fst.mode);
4519b50d902SRodney W. Grimes 	else
4529b50d902SRodney W. Grimes 		strmode(fst.mode, mode);
45322694ebaSBruce Evans 	(void)printf(" %6ld %10s", fst.fileid, mode);
4549b50d902SRodney W. Grimes 	switch (vn.v_type) {
4559b50d902SRodney W. Grimes 	case VBLK:
4569b50d902SRodney W. Grimes 	case VCHR: {
4579b50d902SRodney W. Grimes 		char *name;
4589b50d902SRodney W. Grimes 
4599b50d902SRodney W. Grimes 		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
4609b50d902SRodney W. Grimes 		    S_IFCHR : S_IFBLK)) == NULL))
4619b50d902SRodney W. Grimes 			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
4629b50d902SRodney W. Grimes 		else
4639b50d902SRodney W. Grimes 			printf(" %6s", name);
4649b50d902SRodney W. Grimes 		break;
4659b50d902SRodney W. Grimes 	}
4669b50d902SRodney W. Grimes 	default:
467df8327e7SAlexander Langer 		printf(" %6lu", fst.size);
4689b50d902SRodney W. Grimes 	}
4699b50d902SRodney W. Grimes 	rw[0] = '\0';
4709b50d902SRodney W. Grimes 	if (flag & FREAD)
4719b50d902SRodney W. Grimes 		strcat(rw, "r");
4729b50d902SRodney W. Grimes 	if (flag & FWRITE)
4739b50d902SRodney W. Grimes 		strcat(rw, "w");
4749b50d902SRodney W. Grimes 	printf(" %2s", rw);
4759b50d902SRodney W. Grimes 	if (filename && !fsflg)
4769b50d902SRodney W. Grimes 		printf("  %s", filename);
4779b50d902SRodney W. Grimes 	putchar('\n');
4789b50d902SRodney W. Grimes }
4799b50d902SRodney W. Grimes 
4809b50d902SRodney W. Grimes int
4819b50d902SRodney W. Grimes ufs_filestat(vp, fsp)
4829b50d902SRodney W. Grimes 	struct vnode *vp;
4839b50d902SRodney W. Grimes 	struct filestat *fsp;
4849b50d902SRodney W. Grimes {
4859b50d902SRodney W. Grimes 	struct inode inode;
4869b50d902SRodney W. Grimes 
4879b50d902SRodney W. Grimes 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
48822694ebaSBruce Evans 		dprintf(stderr, "can't read inode at %p for pid %d\n",
48922694ebaSBruce Evans 		    (void *)VTOI(vp), Pid);
4909b50d902SRodney W. Grimes 		return 0;
4919b50d902SRodney W. Grimes 	}
49238cd1ee7SBrian Feldman 	/*
49338cd1ee7SBrian Feldman 	 * The st_dev from stat(2) is a udev_t. These kernel structures
49438cd1ee7SBrian Feldman 	 * contain dev_t structures. We need to convert to udev to make
49538cd1ee7SBrian Feldman 	 * comparisons
49638cd1ee7SBrian Feldman 	 */
49738cd1ee7SBrian Feldman 	fsp->fsid = dev2udev(inode.i_dev) & 0xffff;
4989b50d902SRodney W. Grimes 	fsp->fileid = (long)inode.i_number;
4999b50d902SRodney W. Grimes 	fsp->mode = (mode_t)inode.i_mode;
5009b50d902SRodney W. Grimes 	fsp->size = (u_long)inode.i_size;
5019b50d902SRodney W. Grimes 	fsp->rdev = inode.i_rdev;
5029b50d902SRodney W. Grimes 
5039b50d902SRodney W. Grimes 	return 1;
5049b50d902SRodney W. Grimes }
5059b50d902SRodney W. Grimes 
5069b50d902SRodney W. Grimes int
5079b50d902SRodney W. Grimes nfs_filestat(vp, fsp)
5089b50d902SRodney W. Grimes 	struct vnode *vp;
5099b50d902SRodney W. Grimes 	struct filestat *fsp;
5109b50d902SRodney W. Grimes {
5119b50d902SRodney W. Grimes 	struct nfsnode nfsnode;
5129b50d902SRodney W. Grimes 	register mode_t mode;
5139b50d902SRodney W. Grimes 
5149b50d902SRodney W. Grimes 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
51522694ebaSBruce Evans 		dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
51622694ebaSBruce Evans 		    (void *)VTONFS(vp), Pid);
5179b50d902SRodney W. Grimes 		return 0;
5189b50d902SRodney W. Grimes 	}
5199b50d902SRodney W. Grimes 	fsp->fsid = nfsnode.n_vattr.va_fsid;
5209b50d902SRodney W. Grimes 	fsp->fileid = nfsnode.n_vattr.va_fileid;
5219b50d902SRodney W. Grimes 	fsp->size = nfsnode.n_size;
5229b50d902SRodney W. Grimes 	fsp->rdev = nfsnode.n_vattr.va_rdev;
5239b50d902SRodney W. Grimes 	mode = (mode_t)nfsnode.n_vattr.va_mode;
5249b50d902SRodney W. Grimes 	switch (vp->v_type) {
5259b50d902SRodney W. Grimes 	case VREG:
5269b50d902SRodney W. Grimes 		mode |= S_IFREG;
5279b50d902SRodney W. Grimes 		break;
5289b50d902SRodney W. Grimes 	case VDIR:
5299b50d902SRodney W. Grimes 		mode |= S_IFDIR;
5309b50d902SRodney W. Grimes 		break;
5319b50d902SRodney W. Grimes 	case VBLK:
5329b50d902SRodney W. Grimes 		mode |= S_IFBLK;
5339b50d902SRodney W. Grimes 		break;
5349b50d902SRodney W. Grimes 	case VCHR:
5359b50d902SRodney W. Grimes 		mode |= S_IFCHR;
5369b50d902SRodney W. Grimes 		break;
5379b50d902SRodney W. Grimes 	case VLNK:
5389b50d902SRodney W. Grimes 		mode |= S_IFLNK;
5399b50d902SRodney W. Grimes 		break;
5409b50d902SRodney W. Grimes 	case VSOCK:
5419b50d902SRodney W. Grimes 		mode |= S_IFSOCK;
5429b50d902SRodney W. Grimes 		break;
5439b50d902SRodney W. Grimes 	case VFIFO:
5449b50d902SRodney W. Grimes 		mode |= S_IFIFO;
5459b50d902SRodney W. Grimes 		break;
5460b7a57ddSRuslan Ermilov 	case VNON:
5470b7a57ddSRuslan Ermilov 	case VBAD:
5480b7a57ddSRuslan Ermilov 		return 0;
5499b50d902SRodney W. Grimes 	};
5509b50d902SRodney W. Grimes 	fsp->mode = mode;
5519b50d902SRodney W. Grimes 
5529b50d902SRodney W. Grimes 	return 1;
5539b50d902SRodney W. Grimes }
5549b50d902SRodney W. Grimes 
5559b50d902SRodney W. Grimes 
5569b50d902SRodney W. Grimes char *
5579b50d902SRodney W. Grimes getmnton(m)
5589b50d902SRodney W. Grimes 	struct mount *m;
5599b50d902SRodney W. Grimes {
5609b50d902SRodney W. Grimes 	static struct mount mount;
5619b50d902SRodney W. Grimes 	static struct mtab {
5629b50d902SRodney W. Grimes 		struct mtab *next;
5639b50d902SRodney W. Grimes 		struct mount *m;
5649b50d902SRodney W. Grimes 		char mntonname[MNAMELEN];
5659b50d902SRodney W. Grimes 	} *mhead = NULL;
5669b50d902SRodney W. Grimes 	register struct mtab *mt;
5679b50d902SRodney W. Grimes 
5689b50d902SRodney W. Grimes 	for (mt = mhead; mt != NULL; mt = mt->next)
5699b50d902SRodney W. Grimes 		if (m == mt->m)
5709b50d902SRodney W. Grimes 			return (mt->mntonname);
5719b50d902SRodney W. Grimes 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
57222694ebaSBruce Evans 		warnx("can't read mount table at %p", (void *)m);
5739b50d902SRodney W. Grimes 		return (NULL);
5749b50d902SRodney W. Grimes 	}
575c1e65942SPhilippe Charnier 	if ((mt = malloc(sizeof (struct mtab))) == NULL)
576c1e65942SPhilippe Charnier 		err(1, NULL);
5779b50d902SRodney W. Grimes 	mt->m = m;
5789b50d902SRodney W. Grimes 	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
5799b50d902SRodney W. Grimes 	mt->next = mhead;
5809b50d902SRodney W. Grimes 	mhead = mt;
5819b50d902SRodney W. Grimes 	return (mt->mntonname);
5829b50d902SRodney W. Grimes }
5839b50d902SRodney W. Grimes 
5849b50d902SRodney W. Grimes void
5851c17fc99SPeter Wemm pipetrans(pi, i, flag)
5861c17fc99SPeter Wemm 	struct pipe *pi;
5871c17fc99SPeter Wemm 	int i;
5881c17fc99SPeter Wemm 	int flag;
5891c17fc99SPeter Wemm {
5901c17fc99SPeter Wemm 	struct pipe pip;
5911c17fc99SPeter Wemm 	char rw[3];
5921c17fc99SPeter Wemm 
5931c17fc99SPeter Wemm 	PREFIX(i);
5941c17fc99SPeter Wemm 
5951c17fc99SPeter Wemm 	/* fill in socket */
5961c17fc99SPeter Wemm 	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
59722694ebaSBruce Evans 		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
5981c17fc99SPeter Wemm 		goto bad;
5991c17fc99SPeter Wemm 	}
6001c17fc99SPeter Wemm 
6011c17fc99SPeter Wemm 	printf("* pipe %8x <-> %8x", (int)pi, (int)pip.pipe_peer);
6021c17fc99SPeter Wemm 	printf(" %6d", (int)pip.pipe_buffer.cnt);
6031c17fc99SPeter Wemm 	rw[0] = '\0';
6041c17fc99SPeter Wemm 	if (flag & FREAD)
6051c17fc99SPeter Wemm 		strcat(rw, "r");
6061c17fc99SPeter Wemm 	if (flag & FWRITE)
6071c17fc99SPeter Wemm 		strcat(rw, "w");
6081c17fc99SPeter Wemm 	printf(" %2s", rw);
6091c17fc99SPeter Wemm 	putchar('\n');
6101c17fc99SPeter Wemm 	return;
6111c17fc99SPeter Wemm 
6121c17fc99SPeter Wemm bad:
6131c17fc99SPeter Wemm 	printf("* error\n");
6141c17fc99SPeter Wemm }
6151c17fc99SPeter Wemm 
6161c17fc99SPeter Wemm void
6179b50d902SRodney W. Grimes socktrans(sock, i)
6189b50d902SRodney W. Grimes 	struct socket *sock;
6199b50d902SRodney W. Grimes 	int i;
6209b50d902SRodney W. Grimes {
6219b50d902SRodney W. Grimes 	static char *stypename[] = {
6229b50d902SRodney W. Grimes 		"unused",	/* 0 */
6239b50d902SRodney W. Grimes 		"stream", 	/* 1 */
6249b50d902SRodney W. Grimes 		"dgram",	/* 2 */
6259b50d902SRodney W. Grimes 		"raw",		/* 3 */
6269b50d902SRodney W. Grimes 		"rdm",		/* 4 */
6279b50d902SRodney W. Grimes 		"seqpak"	/* 5 */
6289b50d902SRodney W. Grimes 	};
6299b50d902SRodney W. Grimes #define	STYPEMAX 5
6309b50d902SRodney W. Grimes 	struct socket	so;
6319b50d902SRodney W. Grimes 	struct protosw	proto;
6329b50d902SRodney W. Grimes 	struct domain	dom;
6339b50d902SRodney W. Grimes 	struct inpcb	inpcb;
6349b50d902SRodney W. Grimes 	struct unpcb	unpcb;
6359b50d902SRodney W. Grimes 	int len;
6369b50d902SRodney W. Grimes 	char dname[32], *strcpy();
6379b50d902SRodney W. Grimes 
6389b50d902SRodney W. Grimes 	PREFIX(i);
6399b50d902SRodney W. Grimes 
6409b50d902SRodney W. Grimes 	/* fill in socket */
6419b50d902SRodney W. Grimes 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
64222694ebaSBruce Evans 		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
6439b50d902SRodney W. Grimes 		goto bad;
6449b50d902SRodney W. Grimes 	}
6459b50d902SRodney W. Grimes 
6469b50d902SRodney W. Grimes 	/* fill in protosw entry */
6479b50d902SRodney W. Grimes 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
64822694ebaSBruce Evans 		dprintf(stderr, "can't read protosw at %p",
64922694ebaSBruce Evans 		    (void *)so.so_proto);
6509b50d902SRodney W. Grimes 		goto bad;
6519b50d902SRodney W. Grimes 	}
6529b50d902SRodney W. Grimes 
6539b50d902SRodney W. Grimes 	/* fill in domain */
6549b50d902SRodney W. Grimes 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
65522694ebaSBruce Evans 		dprintf(stderr, "can't read domain at %p\n",
65622694ebaSBruce Evans 		    (void *)proto.pr_domain);
6579b50d902SRodney W. Grimes 		goto bad;
6589b50d902SRodney W. Grimes 	}
6599b50d902SRodney W. Grimes 
6609b50d902SRodney W. Grimes 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
6619b50d902SRodney W. Grimes 	    sizeof(dname) - 1)) < 0) {
66222694ebaSBruce Evans 		dprintf(stderr, "can't read domain name at %p\n",
66322694ebaSBruce Evans 		    (void *)dom.dom_name);
6649b50d902SRodney W. Grimes 		dname[0] = '\0';
6659b50d902SRodney W. Grimes 	}
6669b50d902SRodney W. Grimes 	else
6679b50d902SRodney W. Grimes 		dname[len] = '\0';
6689b50d902SRodney W. Grimes 
6699b50d902SRodney W. Grimes 	if ((u_short)so.so_type > STYPEMAX)
6709b50d902SRodney W. Grimes 		printf("* %s ?%d", dname, so.so_type);
6719b50d902SRodney W. Grimes 	else
6729b50d902SRodney W. Grimes 		printf("* %s %s", dname, stypename[so.so_type]);
6739b50d902SRodney W. Grimes 
6749b50d902SRodney W. Grimes 	/*
6759b50d902SRodney W. Grimes 	 * protocol specific formatting
6769b50d902SRodney W. Grimes 	 *
6779b50d902SRodney W. Grimes 	 * Try to find interesting things to print.  For tcp, the interesting
6789b50d902SRodney W. Grimes 	 * thing is the address of the tcpcb, for udp and others, just the
6799b50d902SRodney W. Grimes 	 * inpcb (socket pcb).  For unix domain, its the address of the socket
6809b50d902SRodney W. Grimes 	 * pcb and the address of the connected pcb (if connected).  Otherwise
6819b50d902SRodney W. Grimes 	 * just print the protocol number and address of the socket itself.
6829b50d902SRodney W. Grimes 	 * The idea is not to duplicate netstat, but to make available enough
6839b50d902SRodney W. Grimes 	 * information for further analysis.
6849b50d902SRodney W. Grimes 	 */
6859b50d902SRodney W. Grimes 	switch(dom.dom_family) {
6869b50d902SRodney W. Grimes 	case AF_INET:
687677e00c0SYoshinobu Inoue 	case AF_INET6:
6889b50d902SRodney W. Grimes 		getinetproto(proto.pr_protocol);
6899b50d902SRodney W. Grimes 		if (proto.pr_protocol == IPPROTO_TCP ) {
6909b50d902SRodney W. Grimes 			if (so.so_pcb) {
6919b50d902SRodney W. Grimes 				if (kvm_read(kd, (u_long)so.so_pcb,
6929b50d902SRodney W. Grimes 				    (char *)&inpcb, sizeof(struct inpcb))
6939b50d902SRodney W. Grimes 				    != sizeof(struct inpcb)) {
6949b50d902SRodney W. Grimes 					dprintf(stderr,
69522694ebaSBruce Evans 					    "can't read inpcb at %p\n",
69622694ebaSBruce Evans 					    (void *)so.so_pcb);
6979b50d902SRodney W. Grimes 					goto bad;
6989b50d902SRodney W. Grimes 				}
6999b50d902SRodney W. Grimes 				printf(" %x", (int)inpcb.inp_ppcb);
7009b50d902SRodney W. Grimes 			}
7019b50d902SRodney W. Grimes 		}
7029b50d902SRodney W. Grimes 		else if (so.so_pcb)
7039b50d902SRodney W. Grimes 			printf(" %x", (int)so.so_pcb);
7049b50d902SRodney W. Grimes 		break;
7059b50d902SRodney W. Grimes 	case AF_UNIX:
7069b50d902SRodney W. Grimes 		/* print address of pcb and connected pcb */
7079b50d902SRodney W. Grimes 		if (so.so_pcb) {
7089b50d902SRodney W. Grimes 			printf(" %x", (int)so.so_pcb);
7099b50d902SRodney W. Grimes 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
7109b50d902SRodney W. Grimes 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
71122694ebaSBruce Evans 				dprintf(stderr, "can't read unpcb at %p\n",
71222694ebaSBruce Evans 				    (void *)so.so_pcb);
7139b50d902SRodney W. Grimes 				goto bad;
7149b50d902SRodney W. Grimes 			}
7159b50d902SRodney W. Grimes 			if (unpcb.unp_conn) {
7169b50d902SRodney W. Grimes 				char shoconn[4], *cp;
7179b50d902SRodney W. Grimes 
7189b50d902SRodney W. Grimes 				cp = shoconn;
7199b50d902SRodney W. Grimes 				if (!(so.so_state & SS_CANTRCVMORE))
7209b50d902SRodney W. Grimes 					*cp++ = '<';
7219b50d902SRodney W. Grimes 				*cp++ = '-';
7229b50d902SRodney W. Grimes 				if (!(so.so_state & SS_CANTSENDMORE))
7239b50d902SRodney W. Grimes 					*cp++ = '>';
7249b50d902SRodney W. Grimes 				*cp = '\0';
7259b50d902SRodney W. Grimes 				printf(" %s %x", shoconn,
7269b50d902SRodney W. Grimes 				    (int)unpcb.unp_conn);
7279b50d902SRodney W. Grimes 			}
7289b50d902SRodney W. Grimes 		}
7299b50d902SRodney W. Grimes 		break;
7309b50d902SRodney W. Grimes 	default:
7319b50d902SRodney W. Grimes 		/* print protocol number and socket address */
7329b50d902SRodney W. Grimes 		printf(" %d %x", proto.pr_protocol, (int)sock);
7339b50d902SRodney W. Grimes 	}
7349b50d902SRodney W. Grimes 	printf("\n");
7359b50d902SRodney W. Grimes 	return;
7369b50d902SRodney W. Grimes bad:
7379b50d902SRodney W. Grimes 	printf("* error\n");
7389b50d902SRodney W. Grimes }
7399b50d902SRodney W. Grimes 
74038cd1ee7SBrian Feldman 
74138cd1ee7SBrian Feldman /*
74238cd1ee7SBrian Feldman  * Read the specinfo structure in the kernel (as pointed to by a dev_t)
74338cd1ee7SBrian Feldman  * in order to work out the associated udev_t
74438cd1ee7SBrian Feldman  */
74538cd1ee7SBrian Feldman udev_t
74638cd1ee7SBrian Feldman dev2udev(dev)
74738cd1ee7SBrian Feldman 	dev_t dev;
74838cd1ee7SBrian Feldman {
74938cd1ee7SBrian Feldman 	struct specinfo si;
75038cd1ee7SBrian Feldman 
75138cd1ee7SBrian Feldman 	if (KVM_READ(dev, &si, sizeof si)) {
75238cd1ee7SBrian Feldman 		return si.si_udev;
75338cd1ee7SBrian Feldman 	} else {
75438cd1ee7SBrian Feldman 		dprintf(stderr, "can't convert dev_t %p to a udev_t\n",
75538cd1ee7SBrian Feldman 		    (void *)dev);
75638cd1ee7SBrian Feldman 		return -1;
75738cd1ee7SBrian Feldman 	}
75838cd1ee7SBrian Feldman }
75938cd1ee7SBrian Feldman 
7609b50d902SRodney W. Grimes /*
7619b50d902SRodney W. Grimes  * getinetproto --
7629b50d902SRodney W. Grimes  *	print name of protocol number
7639b50d902SRodney W. Grimes  */
7649b50d902SRodney W. Grimes void
7659b50d902SRodney W. Grimes getinetproto(number)
7669b50d902SRodney W. Grimes 	int number;
7679b50d902SRodney W. Grimes {
7680b7a57ddSRuslan Ermilov 	static int isopen;
7690b7a57ddSRuslan Ermilov 	register struct protoent *pe;
7709b50d902SRodney W. Grimes 
7710b7a57ddSRuslan Ermilov 	if (!isopen)
7720b7a57ddSRuslan Ermilov 		setprotoent(++isopen);
7730b7a57ddSRuslan Ermilov 	if ((pe = getprotobynumber(number)) != NULL)
7740b7a57ddSRuslan Ermilov 		printf(" %s", pe->p_name);
7750b7a57ddSRuslan Ermilov 	else
7769b50d902SRodney W. Grimes 		printf(" %d", number);
7779b50d902SRodney W. Grimes }
7789b50d902SRodney W. Grimes 
7791c17fc99SPeter Wemm int
7809b50d902SRodney W. Grimes getfname(filename)
7819b50d902SRodney W. Grimes 	char *filename;
7829b50d902SRodney W. Grimes {
7839b50d902SRodney W. Grimes 	struct stat statbuf;
7849b50d902SRodney W. Grimes 	DEVS *cur;
7859b50d902SRodney W. Grimes 
7869b50d902SRodney W. Grimes 	if (stat(filename, &statbuf)) {
787c1e65942SPhilippe Charnier 		warn("%s", filename);
7889b50d902SRodney W. Grimes 		return(0);
7899b50d902SRodney W. Grimes 	}
790c1e65942SPhilippe Charnier 	if ((cur = malloc(sizeof(DEVS))) == NULL)
791c1e65942SPhilippe Charnier 		err(1, NULL);
7929b50d902SRodney W. Grimes 	cur->next = devs;
7939b50d902SRodney W. Grimes 	devs = cur;
7949b50d902SRodney W. Grimes 
7959b50d902SRodney W. Grimes 	cur->ino = statbuf.st_ino;
7969b50d902SRodney W. Grimes 	cur->fsid = statbuf.st_dev & 0xffff;
7979b50d902SRodney W. Grimes 	cur->name = filename;
7989b50d902SRodney W. Grimes 	return(1);
7999b50d902SRodney W. Grimes }
8009b50d902SRodney W. Grimes 
8019b50d902SRodney W. Grimes void
8029b50d902SRodney W. Grimes usage()
8039b50d902SRodney W. Grimes {
8049b50d902SRodney W. Grimes 	(void)fprintf(stderr,
8059b50d902SRodney W. Grimes  "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
8069b50d902SRodney W. Grimes 	exit(1);
8079b50d902SRodney W. Grimes }
808