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