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