Home | History | Annotate | Line # | Download | only in fstat
fstat.c revision 1.36
      1  1.36     tron /*	$NetBSD: fstat.c,v 1.36 1999/07/08 12:07:56 tron Exp $	*/
      2  1.17      tls 
      3   1.1      cgd /*-
      4   1.8      cgd  * Copyright (c) 1988, 1993
      5   1.8      cgd  *	The Regents of the University of California.  All rights reserved.
      6   1.1      cgd  *
      7   1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1      cgd  * modification, are permitted provided that the following conditions
      9   1.1      cgd  * are met:
     10   1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15   1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16   1.1      cgd  *    must display the following acknowledgement:
     17   1.1      cgd  *	This product includes software developed by the University of
     18   1.1      cgd  *	California, Berkeley and its contributors.
     19   1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20   1.1      cgd  *    may be used to endorse or promote products derived from this software
     21   1.1      cgd  *    without specific prior written permission.
     22   1.1      cgd  *
     23   1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24   1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25   1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26   1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27   1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28   1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29   1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30   1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31   1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32   1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33   1.1      cgd  * SUCH DAMAGE.
     34   1.1      cgd  */
     35   1.1      cgd 
     36  1.23    lukem #include <sys/cdefs.h>
     37   1.1      cgd #ifndef lint
     38  1.23    lukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
     39  1.23    lukem 	The Regents of the University of California.  All rights reserved.\n");
     40   1.1      cgd #endif /* not lint */
     41   1.1      cgd 
     42   1.1      cgd #ifndef lint
     43  1.21      mrg #if 0
     44  1.21      mrg static char sccsid[] = "@(#)fstat.c	8.3 (Berkeley) 5/2/95";
     45  1.21      mrg #else
     46  1.36     tron __RCSID("$NetBSD: fstat.c,v 1.36 1999/07/08 12:07:56 tron Exp $");
     47  1.21      mrg #endif
     48   1.1      cgd #endif /* not lint */
     49   1.1      cgd 
     50   1.1      cgd #include <sys/param.h>
     51   1.1      cgd #include <sys/time.h>
     52   1.1      cgd #include <sys/proc.h>
     53   1.1      cgd #include <sys/user.h>
     54   1.1      cgd #include <sys/stat.h>
     55   1.1      cgd #include <sys/vnode.h>
     56   1.1      cgd #include <sys/socket.h>
     57   1.1      cgd #include <sys/socketvar.h>
     58   1.1      cgd #include <sys/domain.h>
     59   1.1      cgd #include <sys/protosw.h>
     60   1.1      cgd #include <sys/unpcb.h>
     61   1.8      cgd #include <sys/sysctl.h>
     62   1.1      cgd #include <sys/filedesc.h>
     63  1.14      jtc #define	_KERNEL
     64  1.36     tron #define _LKM
     65   1.1      cgd #include <sys/file.h>
     66  1.10      cgd #include <ufs/ufs/quota.h>
     67  1.10      cgd #include <ufs/ufs/inode.h>
     68  1.36     tron #undef _LKM
     69  1.14      jtc #undef _KERNEL
     70   1.8      cgd #define NFS
     71   1.1      cgd #include <sys/mount.h>
     72  1.15     fvdl #include <nfs/nfsproto.h>
     73   1.8      cgd #include <nfs/rpcv2.h>
     74   1.1      cgd #include <nfs/nfs.h>
     75   1.1      cgd #include <nfs/nfsnode.h>
     76   1.8      cgd #undef NFS
     77   1.1      cgd 
     78   1.1      cgd #include <net/route.h>
     79   1.1      cgd #include <netinet/in.h>
     80   1.1      cgd #include <netinet/in_systm.h>
     81   1.1      cgd #include <netinet/ip.h>
     82   1.1      cgd #include <netinet/in_pcb.h>
     83   1.1      cgd 
     84  1.34    lukem #include <arpa/inet.h>
     85  1.34    lukem 
     86   1.8      cgd #include <ctype.h>
     87   1.1      cgd #include <errno.h>
     88   1.8      cgd #include <kvm.h>
     89  1.15     fvdl #include <limits.h>
     90   1.1      cgd #include <nlist.h>
     91   1.8      cgd #include <paths.h>
     92   1.1      cgd #include <pwd.h>
     93   1.1      cgd #include <stdio.h>
     94   1.1      cgd #include <stdlib.h>
     95   1.1      cgd #include <string.h>
     96  1.23    lukem #include <unistd.h>
     97  1.27     fair #include <err.h>
     98   1.1      cgd 
     99  1.34    lukem #include "fstat.h"
    100  1.34    lukem 
    101   1.1      cgd #define	TEXT	-1
    102   1.1      cgd #define	CDIR	-2
    103   1.1      cgd #define	RDIR	-3
    104   1.1      cgd #define	TRACE	-4
    105   1.1      cgd 
    106   1.1      cgd typedef struct devs {
    107   1.1      cgd 	struct	devs *next;
    108   1.1      cgd 	long	fsid;
    109   1.1      cgd 	ino_t	ino;
    110   1.1      cgd 	char	*name;
    111   1.1      cgd } DEVS;
    112   1.1      cgd DEVS *devs;
    113   1.1      cgd 
    114   1.1      cgd int 	fsflg,	/* show files on same filesystem as file(s) argument */
    115   1.1      cgd 	pflg,	/* show files open by a particular pid */
    116   1.1      cgd 	uflg;	/* show files open by a particular (effective) user */
    117   1.1      cgd int 	checkfile; /* true if restricting to particular files or filesystems */
    118   1.1      cgd int	nflg;	/* (numerical) display f.s. and rdev as dev_t */
    119   1.1      cgd int	vflg;	/* display errors in locating kernel data objects etc... */
    120   1.1      cgd 
    121   1.1      cgd struct file **ofiles;	/* buffer of pointers to file structures */
    122   1.1      cgd int maxfiles;
    123   1.1      cgd #define ALLOC_OFILES(d)	\
    124   1.1      cgd 	if ((d) > maxfiles) { \
    125   1.1      cgd 		free(ofiles); \
    126   1.1      cgd 		ofiles = malloc((d) * sizeof(struct file *)); \
    127   1.1      cgd 		if (ofiles == NULL) { \
    128  1.30     ross 			err(1, "malloc(%u)", (d) *	\
    129  1.30     ross 					(unsigned int)sizeof(struct file *)); \
    130   1.1      cgd 		} \
    131   1.1      cgd 		maxfiles = (d); \
    132   1.1      cgd 	}
    133   1.1      cgd 
    134   1.8      cgd kvm_t *kd;
    135   1.1      cgd 
    136  1.23    lukem void	dofiles __P((struct kinfo_proc *));
    137  1.23    lukem int	ext2fs_filestat __P((struct vnode *, struct filestat *));
    138  1.23    lukem int	getfname __P((char *));
    139  1.23    lukem void	getinetproto __P((int));
    140  1.23    lukem char   *getmnton __P((struct mount *));
    141  1.23    lukem int	main __P((int, char **));
    142  1.23    lukem int	nfs_filestat __P((struct vnode *, struct filestat *));
    143  1.23    lukem void	socktrans __P((struct socket *, int));
    144  1.23    lukem int	ufs_filestat __P((struct vnode *, struct filestat *));
    145  1.23    lukem void	usage __P((void));
    146  1.23    lukem void	vtrans __P((struct vnode *, int, int));
    147   1.1      cgd 
    148  1.23    lukem int
    149   1.1      cgd main(argc, argv)
    150   1.1      cgd 	int argc;
    151   1.1      cgd 	char **argv;
    152   1.1      cgd {
    153  1.23    lukem 	struct passwd *passwd;
    154   1.8      cgd 	struct kinfo_proc *p, *plast;
    155   1.1      cgd 	int arg, ch, what;
    156   1.8      cgd 	char *memf, *nlistf;
    157  1.15     fvdl 	char buf[_POSIX2_LINE_MAX];
    158   1.8      cgd 	int cnt;
    159  1.26      mrg 	gid_t egid = getegid();
    160   1.1      cgd 
    161  1.26      mrg 	(void)setegid(getgid());
    162   1.1      cgd 	arg = 0;
    163   1.8      cgd 	what = KERN_PROC_ALL;
    164   1.8      cgd 	nlistf = memf = NULL;
    165  1.22    lukem 	while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1)
    166   1.1      cgd 		switch((char)ch) {
    167   1.1      cgd 		case 'f':
    168   1.1      cgd 			fsflg = 1;
    169   1.1      cgd 			break;
    170   1.8      cgd 		case 'M':
    171   1.8      cgd 			memf = optarg;
    172   1.8      cgd 			break;
    173   1.8      cgd 		case 'N':
    174   1.8      cgd 			nlistf = optarg;
    175   1.8      cgd 			break;
    176   1.1      cgd 		case 'n':
    177   1.1      cgd 			nflg = 1;
    178   1.1      cgd 			break;
    179   1.1      cgd 		case 'p':
    180   1.1      cgd 			if (pflg++)
    181   1.1      cgd 				usage();
    182   1.1      cgd 			if (!isdigit(*optarg)) {
    183  1.34    lukem 				warnx("-p requires a process id");
    184   1.1      cgd 				usage();
    185   1.1      cgd 			}
    186   1.8      cgd 			what = KERN_PROC_PID;
    187   1.1      cgd 			arg = atoi(optarg);
    188   1.1      cgd 			break;
    189   1.1      cgd 		case 'u':
    190   1.1      cgd 			if (uflg++)
    191   1.1      cgd 				usage();
    192   1.1      cgd 			if (!(passwd = getpwnam(optarg))) {
    193  1.34    lukem 				errx(1, "%s: unknown uid", optarg);
    194   1.1      cgd 			}
    195   1.8      cgd 			what = KERN_PROC_UID;
    196   1.1      cgd 			arg = passwd->pw_uid;
    197   1.1      cgd 			break;
    198   1.1      cgd 		case 'v':
    199   1.1      cgd 			vflg = 1;
    200   1.1      cgd 			break;
    201   1.1      cgd 		case '?':
    202   1.1      cgd 		default:
    203   1.1      cgd 			usage();
    204   1.1      cgd 		}
    205   1.1      cgd 
    206   1.1      cgd 	if (*(argv += optind)) {
    207   1.1      cgd 		for (; *argv; ++argv) {
    208   1.1      cgd 			if (getfname(*argv))
    209   1.1      cgd 				checkfile = 1;
    210   1.1      cgd 		}
    211   1.1      cgd 		if (!checkfile)	/* file(s) specified, but none accessable */
    212   1.1      cgd 			exit(1);
    213   1.1      cgd 	}
    214   1.1      cgd 
    215   1.1      cgd 	ALLOC_OFILES(256);	/* reserve space for file pointers */
    216   1.1      cgd 
    217   1.1      cgd 	if (fsflg && !checkfile) {
    218   1.1      cgd 		/* -f with no files means use wd */
    219   1.1      cgd 		if (getfname(".") == 0)
    220   1.1      cgd 			exit(1);
    221   1.1      cgd 		checkfile = 1;
    222   1.1      cgd 	}
    223   1.1      cgd 
    224   1.8      cgd 	/*
    225  1.26      mrg 	 * Discard setgid privileges.  If not the running kernel, we toss
    226  1.26      mrg 	 * them away totally so that bad guys can't print interesting stuff
    227  1.26      mrg 	 * from kernel memory, otherwise switch back to kmem for the
    228  1.26      mrg 	 * duration of the kvm_openfiles() call.
    229   1.8      cgd 	 */
    230   1.8      cgd 	if (nlistf != NULL || memf != NULL)
    231  1.26      mrg 		(void)setgid(getgid());
    232  1.26      mrg 	else
    233  1.26      mrg 		(void)setegid(egid);
    234  1.26      mrg 
    235  1.26      mrg 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
    236  1.26      mrg 		errx(1, "%s", buf);
    237  1.26      mrg 
    238  1.26      mrg 	/* get rid of it now anyway */
    239  1.26      mrg 	if (nlistf == NULL && memf == NULL)
    240  1.26      mrg 		(void)setgid(getgid());
    241   1.8      cgd 
    242   1.8      cgd 	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) {
    243  1.34    lukem 		errx(1, "%s", kvm_geterr(kd));
    244   1.1      cgd 	}
    245   1.1      cgd 	if (nflg)
    246   1.1      cgd 		printf("%s",
    247   1.8      cgd "USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
    248   1.1      cgd 	else
    249   1.1      cgd 		printf("%s",
    250   1.8      cgd "USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
    251   1.1      cgd 	if (checkfile && fsflg == 0)
    252   1.1      cgd 		printf(" NAME\n");
    253   1.1      cgd 	else
    254   1.1      cgd 		putchar('\n');
    255   1.1      cgd 
    256   1.8      cgd 	for (plast = &p[cnt]; p < plast; ++p) {
    257   1.8      cgd 		if (p->kp_proc.p_stat == SZOMB)
    258   1.1      cgd 			continue;
    259   1.1      cgd 		dofiles(p);
    260   1.1      cgd 	}
    261   1.1      cgd 	exit(0);
    262   1.1      cgd }
    263   1.1      cgd 
    264  1.33  mycroft const	char *Uname, *Comm;
    265  1.34    lukem pid_t	Pid;
    266   1.1      cgd 
    267   1.1      cgd #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
    268   1.1      cgd 	switch(i) { \
    269   1.1      cgd 	case TEXT: \
    270   1.1      cgd 		printf(" text"); \
    271   1.1      cgd 		break; \
    272   1.1      cgd 	case CDIR: \
    273   1.1      cgd 		printf("   wd"); \
    274   1.1      cgd 		break; \
    275   1.1      cgd 	case RDIR: \
    276   1.1      cgd 		printf(" root"); \
    277   1.1      cgd 		break; \
    278   1.1      cgd 	case TRACE: \
    279   1.1      cgd 		printf("   tr"); \
    280   1.1      cgd 		break; \
    281   1.1      cgd 	default: \
    282   1.1      cgd 		printf(" %4d", i); \
    283   1.1      cgd 		break; \
    284   1.1      cgd 	}
    285   1.1      cgd 
    286   1.1      cgd /*
    287   1.1      cgd  * print open files attributed to this process
    288   1.1      cgd  */
    289   1.1      cgd void
    290   1.8      cgd dofiles(kp)
    291   1.8      cgd 	struct kinfo_proc *kp;
    292   1.1      cgd {
    293  1.23    lukem 	int i;
    294   1.1      cgd 	struct file file;
    295   1.1      cgd 	struct filedesc0 filed0;
    296   1.1      cgd #define	filed	filed0.fd_fd
    297  1.35  thorpej 	struct cwdinfo cwdi;
    298   1.8      cgd 	struct proc *p = &kp->kp_proc;
    299   1.8      cgd 	struct eproc *ep = &kp->kp_eproc;
    300   1.1      cgd 
    301   1.1      cgd 	Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
    302   1.1      cgd 	Pid = p->p_pid;
    303   1.1      cgd 	Comm = p->p_comm;
    304   1.1      cgd 
    305  1.35  thorpej 	if (p->p_fd == NULL || p->p_cwdi == NULL)
    306   1.1      cgd 		return;
    307   1.1      cgd 	if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
    308  1.34    lukem 		warnx("can't read filedesc at %p for pid %d", p->p_fd, Pid);
    309  1.13  mycroft 		return;
    310  1.13  mycroft 	}
    311  1.35  thorpej 	if (!KVM_READ(p->p_cwdi, &cwdi, sizeof(cwdi))) {
    312  1.35  thorpej 		warnx("can't read cwdinfo at %p for pid %d", p->p_cwdi, Pid);
    313  1.35  thorpej 		return;
    314  1.35  thorpej 	}
    315  1.13  mycroft 	if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
    316  1.13  mycroft 	    filed.fd_freefile > filed.fd_lastfile + 1) {
    317  1.34    lukem 		dprintf("filedesc corrupted at %p for pid %d", p->p_fd, Pid);
    318   1.1      cgd 		return;
    319   1.1      cgd 	}
    320   1.1      cgd 	/*
    321   1.1      cgd 	 * root directory vnode, if one
    322   1.1      cgd 	 */
    323  1.35  thorpej 	if (cwdi.cwdi_rdir)
    324  1.35  thorpej 		vtrans(cwdi.cwdi_rdir, RDIR, FREAD);
    325   1.1      cgd 	/*
    326   1.1      cgd 	 * current working directory vnode
    327   1.1      cgd 	 */
    328  1.35  thorpej 	vtrans(cwdi.cwdi_cdir, CDIR, FREAD);
    329   1.1      cgd 	/*
    330   1.1      cgd 	 * ktrace vnode, if one
    331   1.1      cgd 	 */
    332   1.1      cgd 	if (p->p_tracep)
    333   1.8      cgd 		vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
    334   1.1      cgd 	/*
    335   1.1      cgd 	 * open files
    336   1.1      cgd 	 */
    337   1.1      cgd #define FPSIZE	(sizeof (struct file *))
    338   1.8      cgd 	ALLOC_OFILES(filed.fd_lastfile+1);
    339   1.1      cgd 	if (filed.fd_nfiles > NDFILE) {
    340   1.1      cgd 		if (!KVM_READ(filed.fd_ofiles, ofiles,
    341   1.6      cgd 		    (filed.fd_lastfile+1) * FPSIZE)) {
    342  1.34    lukem 			dprintf("can't read file structures at %p for pid %d",
    343  1.34    lukem 			    filed.fd_ofiles, Pid);
    344   1.1      cgd 			return;
    345   1.1      cgd 		}
    346   1.1      cgd 	} else
    347  1.23    lukem 		memmove(ofiles, filed0.fd_dfiles,
    348  1.23    lukem 		    (filed.fd_lastfile+1) * FPSIZE);
    349   1.1      cgd 	for (i = 0; i <= filed.fd_lastfile; i++) {
    350   1.1      cgd 		if (ofiles[i] == NULL)
    351   1.1      cgd 			continue;
    352   1.1      cgd 		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
    353  1.34    lukem 			dprintf("can't read file %d at %p for pid %d",
    354  1.34    lukem 			    i, ofiles[i], Pid);
    355   1.1      cgd 			continue;
    356   1.1      cgd 		}
    357   1.1      cgd 		if (file.f_type == DTYPE_VNODE)
    358   1.8      cgd 			vtrans((struct vnode *)file.f_data, i, file.f_flag);
    359   1.8      cgd 		else if (file.f_type == DTYPE_SOCKET) {
    360   1.8      cgd 			if (checkfile == 0)
    361   1.8      cgd 				socktrans((struct socket *)file.f_data, i);
    362   1.8      cgd 		}
    363   1.1      cgd 		else {
    364  1.34    lukem 			dprintf("unknown file type %d for file %d of pid %d",
    365   1.1      cgd 				file.f_type, i, Pid);
    366   1.1      cgd 		}
    367   1.1      cgd 	}
    368   1.1      cgd }
    369   1.1      cgd 
    370   1.1      cgd void
    371   1.8      cgd vtrans(vp, i, flag)
    372   1.1      cgd 	struct vnode *vp;
    373   1.1      cgd 	int i;
    374   1.8      cgd 	int flag;
    375   1.1      cgd {
    376   1.1      cgd 	struct vnode vn;
    377   1.1      cgd 	struct filestat fst;
    378  1.34    lukem 	char mode[15], rw[3];
    379  1.23    lukem 	char *badtype = NULL, *filename;
    380   1.1      cgd 
    381   1.1      cgd 	filename = badtype = NULL;
    382   1.1      cgd 	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
    383  1.34    lukem 		dprintf("can't read vnode at %p for pid %d", vp, Pid);
    384   1.1      cgd 		return;
    385   1.1      cgd 	}
    386   1.1      cgd 	if (vn.v_type == VNON || vn.v_tag == VT_NON)
    387   1.1      cgd 		badtype = "none";
    388   1.1      cgd 	else if (vn.v_type == VBAD)
    389   1.1      cgd 		badtype = "bad";
    390   1.1      cgd 	else
    391   1.1      cgd 		switch (vn.v_tag) {
    392   1.1      cgd 		case VT_UFS:
    393   1.8      cgd 			if (!ufs_filestat(&vn, &fst))
    394   1.8      cgd 				badtype = "error";
    395   1.1      cgd 			break;
    396   1.1      cgd 		case VT_MFS:
    397   1.8      cgd 			if (!ufs_filestat(&vn, &fst))
    398   1.8      cgd 				badtype = "error";
    399   1.1      cgd 			break;
    400   1.1      cgd 		case VT_NFS:
    401   1.8      cgd 			if (!nfs_filestat(&vn, &fst))
    402   1.8      cgd 				badtype = "error";
    403   1.1      cgd 			break;
    404  1.20   bouyer 		case VT_EXT2FS:
    405  1.20   bouyer 			if (!ext2fs_filestat(&vn, &fst))
    406  1.20   bouyer 				badtype = "error";
    407  1.20   bouyer 			break;
    408  1.34    lukem 		case VT_ISOFS:
    409  1.34    lukem 			if (!isofs_filestat(&vn, &fst))
    410  1.34    lukem 				badtype = "error";
    411  1.34    lukem 			break;
    412   1.1      cgd 		default: {
    413   1.1      cgd 			static char unknown[10];
    414  1.18      mrg 			(void)snprintf(badtype = unknown, sizeof unknown,
    415  1.18      mrg 			    "?(%x)", vn.v_tag);
    416   1.1      cgd 			break;;
    417   1.1      cgd 		}
    418   1.1      cgd 	}
    419   1.1      cgd 	if (checkfile) {
    420   1.1      cgd 		int fsmatch = 0;
    421  1.23    lukem 		DEVS *d;
    422   1.1      cgd 
    423   1.1      cgd 		if (badtype)
    424   1.1      cgd 			return;
    425   1.1      cgd 		for (d = devs; d != NULL; d = d->next)
    426   1.1      cgd 			if (d->fsid == fst.fsid) {
    427   1.1      cgd 				fsmatch = 1;
    428   1.1      cgd 				if (d->ino == fst.fileid) {
    429   1.1      cgd 					filename = d->name;
    430   1.1      cgd 					break;
    431   1.1      cgd 				}
    432   1.1      cgd 			}
    433   1.1      cgd 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
    434   1.1      cgd 			return;
    435   1.1      cgd 	}
    436   1.1      cgd 	PREFIX(i);
    437   1.1      cgd 	if (badtype) {
    438   1.1      cgd 		(void)printf(" -         -  %10s    -\n", badtype);
    439   1.1      cgd 		return;
    440   1.1      cgd 	}
    441   1.1      cgd 	if (nflg)
    442   1.1      cgd 		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
    443   1.1      cgd 	else
    444   1.1      cgd 		(void)printf(" %-8s", getmnton(vn.v_mount));
    445   1.1      cgd 	if (nflg)
    446  1.18      mrg 		(void)snprintf(mode, sizeof mode, "%o", fst.mode);
    447   1.1      cgd 	else
    448   1.1      cgd 		strmode(fst.mode, mode);
    449  1.23    lukem 	(void)printf(" %6ld %10s", (long)fst.fileid, mode);
    450   1.1      cgd 	switch (vn.v_type) {
    451   1.1      cgd 	case VBLK:
    452   1.1      cgd 	case VCHR: {
    453   1.1      cgd 		char *name;
    454   1.1      cgd 
    455   1.1      cgd 		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
    456   1.1      cgd 		    S_IFCHR : S_IFBLK)) == NULL))
    457   1.1      cgd 			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
    458   1.1      cgd 		else
    459   1.1      cgd 			printf(" %6s", name);
    460   1.1      cgd 		break;
    461   1.1      cgd 	}
    462   1.1      cgd 	default:
    463  1.34    lukem 		printf(" %6qd", (long long)fst.size);
    464   1.1      cgd 	}
    465  1.34    lukem 	rw[0] = '\0';
    466   1.8      cgd 	if (flag & FREAD)
    467  1.34    lukem 		strcat(rw, "r");
    468   1.8      cgd 	if (flag & FWRITE)
    469  1.34    lukem 		strcat(rw, "w");
    470  1.34    lukem 	printf(" %-2s", rw);
    471   1.1      cgd 	if (filename && !fsflg)
    472   1.8      cgd 		printf("  %s", filename);
    473   1.1      cgd 	putchar('\n');
    474   1.1      cgd }
    475   1.1      cgd 
    476   1.8      cgd int
    477   1.1      cgd ufs_filestat(vp, fsp)
    478   1.1      cgd 	struct vnode *vp;
    479   1.1      cgd 	struct filestat *fsp;
    480   1.1      cgd {
    481   1.8      cgd 	struct inode inode;
    482   1.8      cgd 
    483   1.8      cgd 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
    484  1.34    lukem 		dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
    485   1.8      cgd 		return 0;
    486   1.8      cgd 	}
    487   1.8      cgd 	fsp->fsid = inode.i_dev & 0xffff;
    488   1.8      cgd 	fsp->fileid = (long)inode.i_number;
    489  1.20   bouyer 	fsp->mode = (mode_t)inode.i_ffs_mode;
    490  1.34    lukem 	fsp->size = inode.i_ffs_size;
    491  1.20   bouyer 	fsp->rdev = inode.i_ffs_rdev;
    492  1.20   bouyer 
    493  1.20   bouyer 	return 1;
    494  1.20   bouyer }
    495  1.20   bouyer 
    496  1.20   bouyer int
    497  1.20   bouyer ext2fs_filestat(vp, fsp)
    498  1.20   bouyer 	struct vnode *vp;
    499  1.20   bouyer 	struct filestat *fsp;
    500  1.20   bouyer {
    501  1.20   bouyer 	struct inode inode;
    502   1.1      cgd 
    503  1.20   bouyer 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
    504  1.34    lukem 		dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
    505  1.20   bouyer 		return 0;
    506  1.20   bouyer 	}
    507  1.20   bouyer 	fsp->fsid = inode.i_dev & 0xffff;
    508  1.20   bouyer 	fsp->fileid = (long)inode.i_number;
    509  1.20   bouyer 	fsp->mode = (mode_t)inode.i_e2fs_mode;
    510  1.34    lukem 	fsp->size = inode.i_e2fs_size;
    511  1.20   bouyer 	fsp->rdev = 0;  /* XXX */
    512   1.8      cgd 	return 1;
    513   1.1      cgd }
    514   1.1      cgd 
    515   1.8      cgd int
    516   1.1      cgd nfs_filestat(vp, fsp)
    517   1.1      cgd 	struct vnode *vp;
    518   1.1      cgd 	struct filestat *fsp;
    519   1.1      cgd {
    520   1.8      cgd 	struct nfsnode nfsnode;
    521  1.24  thorpej 	struct vattr va;
    522  1.23    lukem 	mode_t mode;
    523   1.1      cgd 
    524   1.8      cgd 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
    525  1.34    lukem 		dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp),
    526  1.34    lukem 		    Pid);
    527   1.8      cgd 		return 0;
    528   1.8      cgd 	}
    529  1.24  thorpej 	if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
    530  1.34    lukem 		dprintf("can't read vnode attributes at %p for pid %d",
    531  1.34    lukem 		    nfsnode.n_vattr, Pid);
    532  1.24  thorpej 		return 0;
    533  1.24  thorpej 	}
    534  1.24  thorpej 	fsp->fsid = va.va_fsid;
    535  1.24  thorpej 	fsp->fileid = va.va_fileid;
    536   1.8      cgd 	fsp->size = nfsnode.n_size;
    537  1.24  thorpej 	fsp->rdev = va.va_rdev;
    538  1.24  thorpej 	mode = (mode_t)va.va_mode;
    539   1.1      cgd 	switch (vp->v_type) {
    540   1.1      cgd 	case VREG:
    541   1.1      cgd 		mode |= S_IFREG;
    542   1.1      cgd 		break;
    543   1.1      cgd 	case VDIR:
    544   1.1      cgd 		mode |= S_IFDIR;
    545   1.1      cgd 		break;
    546   1.1      cgd 	case VBLK:
    547   1.1      cgd 		mode |= S_IFBLK;
    548   1.1      cgd 		break;
    549   1.1      cgd 	case VCHR:
    550   1.1      cgd 		mode |= S_IFCHR;
    551   1.1      cgd 		break;
    552   1.1      cgd 	case VLNK:
    553   1.1      cgd 		mode |= S_IFLNK;
    554   1.1      cgd 		break;
    555   1.1      cgd 	case VSOCK:
    556   1.1      cgd 		mode |= S_IFSOCK;
    557   1.1      cgd 		break;
    558   1.1      cgd 	case VFIFO:
    559   1.1      cgd 		mode |= S_IFIFO;
    560   1.1      cgd 		break;
    561  1.23    lukem 	default:
    562  1.23    lukem 		break;
    563   1.1      cgd 	};
    564   1.1      cgd 	fsp->mode = mode;
    565   1.8      cgd 
    566   1.8      cgd 	return 1;
    567   1.1      cgd }
    568   1.1      cgd 
    569   1.1      cgd 
    570   1.1      cgd char *
    571   1.1      cgd getmnton(m)
    572   1.1      cgd 	struct mount *m;
    573   1.1      cgd {
    574   1.1      cgd 	static struct mount mount;
    575   1.1      cgd 	static struct mtab {
    576   1.1      cgd 		struct mtab *next;
    577   1.1      cgd 		struct mount *m;
    578   1.1      cgd 		char mntonname[MNAMELEN];
    579   1.1      cgd 	} *mhead = NULL;
    580  1.23    lukem 	struct mtab *mt;
    581   1.1      cgd 
    582   1.1      cgd 	for (mt = mhead; mt != NULL; mt = mt->next)
    583   1.1      cgd 		if (m == mt->m)
    584   1.1      cgd 			return (mt->mntonname);
    585   1.1      cgd 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
    586  1.34    lukem 		warnx("can't read mount table at %p", m);
    587   1.1      cgd 		return (NULL);
    588   1.1      cgd 	}
    589   1.1      cgd 	if ((mt = malloc(sizeof (struct mtab))) == NULL) {
    590  1.30     ross 		err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab));
    591   1.1      cgd 	}
    592   1.1      cgd 	mt->m = m;
    593  1.23    lukem 	memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0], MNAMELEN);
    594   1.1      cgd 	mt->next = mhead;
    595   1.1      cgd 	mhead = mt;
    596   1.1      cgd 	return (mt->mntonname);
    597   1.1      cgd }
    598   1.1      cgd 
    599   1.1      cgd void
    600   1.1      cgd socktrans(sock, i)
    601   1.1      cgd 	struct socket *sock;
    602   1.1      cgd 	int i;
    603   1.1      cgd {
    604   1.1      cgd 	static char *stypename[] = {
    605   1.1      cgd 		"unused",	/* 0 */
    606   1.1      cgd 		"stream", 	/* 1 */
    607   1.1      cgd 		"dgram",	/* 2 */
    608   1.1      cgd 		"raw",		/* 3 */
    609   1.1      cgd 		"rdm",		/* 4 */
    610   1.1      cgd 		"seqpak"	/* 5 */
    611   1.1      cgd 	};
    612   1.1      cgd #define	STYPEMAX 5
    613   1.1      cgd 	struct socket	so;
    614   1.1      cgd 	struct protosw	proto;
    615   1.1      cgd 	struct domain	dom;
    616   1.1      cgd 	struct inpcb	inpcb;
    617   1.1      cgd 	struct unpcb	unpcb;
    618   1.1      cgd 	int len;
    619  1.18      mrg 	char dname[32];
    620   1.1      cgd 
    621   1.1      cgd 	PREFIX(i);
    622   1.1      cgd 
    623   1.1      cgd 	/* fill in socket */
    624   1.1      cgd 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
    625  1.34    lukem 		dprintf("can't read sock at %p", sock);
    626   1.1      cgd 		goto bad;
    627   1.1      cgd 	}
    628   1.1      cgd 
    629   1.1      cgd 	/* fill in protosw entry */
    630   1.1      cgd 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
    631  1.34    lukem 		dprintf("can't read protosw at %p", so.so_proto);
    632   1.1      cgd 		goto bad;
    633   1.1      cgd 	}
    634   1.1      cgd 
    635   1.1      cgd 	/* fill in domain */
    636   1.1      cgd 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
    637  1.34    lukem 		dprintf("can't read domain at %p", proto.pr_domain);
    638   1.1      cgd 		goto bad;
    639   1.1      cgd 	}
    640   1.1      cgd 
    641   1.8      cgd 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
    642  1.25  msaitoh 	    sizeof(dname) - 1)) != sizeof(dname) -1) {
    643  1.34    lukem 		dprintf("can't read domain name at %p", dom.dom_name);
    644   1.1      cgd 		dname[0] = '\0';
    645   1.1      cgd 	}
    646   1.1      cgd 	else
    647   1.1      cgd 		dname[len] = '\0';
    648   1.1      cgd 
    649   1.1      cgd 	if ((u_short)so.so_type > STYPEMAX)
    650   1.1      cgd 		printf("* %s ?%d", dname, so.so_type);
    651   1.1      cgd 	else
    652   1.1      cgd 		printf("* %s %s", dname, stypename[so.so_type]);
    653   1.1      cgd 
    654   1.1      cgd 	/*
    655   1.1      cgd 	 * protocol specific formatting
    656   1.1      cgd 	 *
    657  1.34    lukem 	 * Try to find interesting things to print.  For TCP, the interesting
    658  1.34    lukem 	 * thing is the address of the tcpcb, for UDP and others, just the
    659  1.34    lukem 	 * inpcb (socket pcb).  For UNIX domain, its the address of the socket
    660   1.1      cgd 	 * pcb and the address of the connected pcb (if connected).  Otherwise
    661   1.1      cgd 	 * just print the protocol number and address of the socket itself.
    662   1.1      cgd 	 * The idea is not to duplicate netstat, but to make available enough
    663   1.1      cgd 	 * information for further analysis.
    664   1.1      cgd 	 */
    665   1.1      cgd 	switch(dom.dom_family) {
    666   1.1      cgd 	case AF_INET:
    667   1.1      cgd 		getinetproto(proto.pr_protocol);
    668  1.34    lukem 		if (proto.pr_protocol == IPPROTO_TCP) {
    669  1.34    lukem 			if (so.so_pcb == NULL)
    670  1.34    lukem 				break;
    671  1.34    lukem 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
    672  1.34    lukem 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
    673  1.34    lukem 				dprintf("can't read inpcb at %p", so.so_pcb);
    674  1.34    lukem 				goto bad;
    675  1.34    lukem 			}
    676  1.34    lukem 			printf(" %lx", (long)inpcb.inp_ppcb);
    677  1.34    lukem 			printf(" %s:%d",
    678  1.34    lukem 			    inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
    679  1.34    lukem 			    inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
    680  1.34    lukem 			if (inpcb.inp_fport) {
    681  1.34    lukem 				printf(" <-> ");
    682  1.34    lukem 				printf("%s:%d",
    683  1.34    lukem 				    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
    684  1.34    lukem 				    inet_ntoa(inpcb.inp_faddr),
    685  1.34    lukem 				    ntohs(inpcb.inp_fport));
    686  1.34    lukem 			}
    687  1.34    lukem 		} else if (proto.pr_protocol == IPPROTO_UDP) {
    688  1.34    lukem 			if (so.so_pcb == NULL)
    689  1.34    lukem 				break;
    690  1.34    lukem 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
    691  1.34    lukem 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
    692  1.34    lukem 				dprintf("can't read inpcb at %p", so.so_pcb);
    693  1.34    lukem 				goto bad;
    694   1.1      cgd 			}
    695  1.34    lukem 			printf(" %lx", (long)so.so_pcb);
    696  1.34    lukem 			printf(" %s:%d",
    697  1.34    lukem 			    inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
    698  1.34    lukem 			    inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
    699  1.34    lukem 			if (inpcb.inp_fport)
    700  1.34    lukem 				printf(" <-> %s:%d",
    701  1.34    lukem 				    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
    702  1.34    lukem 				    inet_ntoa(inpcb.inp_faddr),
    703  1.34    lukem 				    ntohs(inpcb.inp_fport));
    704  1.34    lukem 		} else if (so.so_pcb)
    705  1.12      cgd 			printf(" %lx", (long)so.so_pcb);
    706   1.1      cgd 		break;
    707  1.31    lukem 	case AF_LOCAL:
    708   1.1      cgd 		/* print address of pcb and connected pcb */
    709   1.1      cgd 		if (so.so_pcb) {
    710  1.12      cgd 			printf(" %lx", (long)so.so_pcb);
    711   1.8      cgd 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
    712   1.1      cgd 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
    713  1.34    lukem 				dprintf("can't read unpcb at %p", so.so_pcb);
    714   1.1      cgd 				goto bad;
    715   1.1      cgd 			}
    716   1.1      cgd 			if (unpcb.unp_conn) {
    717   1.1      cgd 				char shoconn[4], *cp;
    718   1.1      cgd 
    719   1.1      cgd 				cp = shoconn;
    720   1.1      cgd 				if (!(so.so_state & SS_CANTRCVMORE))
    721   1.1      cgd 					*cp++ = '<';
    722   1.1      cgd 				*cp++ = '-';
    723   1.1      cgd 				if (!(so.so_state & SS_CANTSENDMORE))
    724   1.1      cgd 					*cp++ = '>';
    725   1.1      cgd 				*cp = '\0';
    726  1.12      cgd 				printf(" %s %lx", shoconn,
    727  1.12      cgd 				    (long)unpcb.unp_conn);
    728   1.1      cgd 			}
    729   1.1      cgd 		}
    730   1.1      cgd 		break;
    731   1.1      cgd 	default:
    732   1.1      cgd 		/* print protocol number and socket address */
    733  1.12      cgd 		printf(" %d %lx", proto.pr_protocol, (long)sock);
    734   1.1      cgd 	}
    735   1.1      cgd 	printf("\n");
    736   1.1      cgd 	return;
    737   1.1      cgd bad:
    738   1.1      cgd 	printf("* error\n");
    739   1.1      cgd }
    740   1.1      cgd 
    741   1.1      cgd /*
    742   1.1      cgd  * getinetproto --
    743   1.1      cgd  *	print name of protocol number
    744   1.1      cgd  */
    745   1.1      cgd void
    746   1.1      cgd getinetproto(number)
    747   1.1      cgd 	int number;
    748   1.1      cgd {
    749   1.1      cgd 	char *cp;
    750   1.1      cgd 
    751  1.26      mrg 	switch (number) {
    752   1.1      cgd 	case IPPROTO_IP:
    753   1.1      cgd 		cp = "ip"; break;
    754   1.1      cgd 	case IPPROTO_ICMP:
    755   1.1      cgd 		cp ="icmp"; break;
    756   1.1      cgd 	case IPPROTO_GGP:
    757   1.1      cgd 		cp ="ggp"; break;
    758   1.1      cgd 	case IPPROTO_TCP:
    759   1.1      cgd 		cp ="tcp"; break;
    760   1.1      cgd 	case IPPROTO_EGP:
    761   1.1      cgd 		cp ="egp"; break;
    762   1.1      cgd 	case IPPROTO_PUP:
    763   1.1      cgd 		cp ="pup"; break;
    764   1.1      cgd 	case IPPROTO_UDP:
    765   1.1      cgd 		cp ="udp"; break;
    766   1.1      cgd 	case IPPROTO_IDP:
    767   1.1      cgd 		cp ="idp"; break;
    768   1.1      cgd 	case IPPROTO_RAW:
    769   1.1      cgd 		cp ="raw"; break;
    770   1.1      cgd 	default:
    771   1.1      cgd 		printf(" %d", number);
    772   1.1      cgd 		return;
    773   1.1      cgd 	}
    774   1.1      cgd 	printf(" %s", cp);
    775   1.1      cgd }
    776   1.1      cgd 
    777  1.23    lukem int
    778   1.1      cgd getfname(filename)
    779   1.1      cgd 	char *filename;
    780   1.1      cgd {
    781   1.1      cgd 	struct stat statbuf;
    782   1.1      cgd 	DEVS *cur;
    783   1.1      cgd 
    784   1.1      cgd 	if (stat(filename, &statbuf)) {
    785  1.28     fair 		warn("stat(%s)", filename);
    786   1.1      cgd 		return(0);
    787   1.1      cgd 	}
    788   1.1      cgd 	if ((cur = malloc(sizeof(DEVS))) == NULL) {
    789  1.30     ross 		err(1, "malloc(%u)", (unsigned int)sizeof(DEVS));
    790   1.1      cgd 	}
    791   1.1      cgd 	cur->next = devs;
    792   1.1      cgd 	devs = cur;
    793   1.1      cgd 
    794   1.1      cgd 	cur->ino = statbuf.st_ino;
    795   1.1      cgd 	cur->fsid = statbuf.st_dev & 0xffff;
    796   1.1      cgd 	cur->name = filename;
    797   1.1      cgd 	return(1);
    798   1.1      cgd }
    799   1.1      cgd 
    800   1.1      cgd void
    801   1.1      cgd usage()
    802   1.1      cgd {
    803  1.28     fair 	errx(1,
    804   1.1      cgd  "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
    805   1.1      cgd }
    806