Home | History | Annotate | Line # | Download | only in fstat
fstat.c revision 1.92
      1  1.92  christos /*	$NetBSD: fstat.c,v 1.92 2011/09/10 18:35:28 christos 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.64       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.23     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34  1.85     lukem __COPYRIGHT("@(#) Copyright (c) 1988, 1993\
     35  1.85     lukem  The Regents of the University of California.  All rights reserved.");
     36   1.1       cgd #endif /* not lint */
     37   1.1       cgd 
     38   1.1       cgd #ifndef lint
     39  1.21       mrg #if 0
     40  1.21       mrg static char sccsid[] = "@(#)fstat.c	8.3 (Berkeley) 5/2/95";
     41  1.21       mrg #else
     42  1.92  christos __RCSID("$NetBSD: fstat.c,v 1.92 2011/09/10 18:35:28 christos Exp $");
     43  1.21       mrg #endif
     44   1.1       cgd #endif /* not lint */
     45   1.1       cgd 
     46  1.78        ad #include <sys/types.h>
     47   1.1       cgd #include <sys/param.h>
     48   1.1       cgd #include <sys/time.h>
     49   1.1       cgd #include <sys/proc.h>
     50   1.1       cgd #include <sys/stat.h>
     51   1.1       cgd #include <sys/vnode.h>
     52   1.1       cgd #include <sys/socket.h>
     53   1.1       cgd #include <sys/socketvar.h>
     54   1.1       cgd #include <sys/domain.h>
     55   1.1       cgd #include <sys/protosw.h>
     56   1.1       cgd #include <sys/unpcb.h>
     57   1.8       cgd #include <sys/sysctl.h>
     58   1.1       cgd #include <sys/filedesc.h>
     59  1.47  jdolecek #include <sys/pipe.h>
     60  1.14       jtc #define	_KERNEL
     61   1.1       cgd #include <sys/file.h>
     62  1.10       cgd #include <ufs/ufs/inode.h>
     63  1.14       jtc #undef _KERNEL
     64  1.41  jdolecek #define _KERNEL
     65  1.41  jdolecek #include <sys/mount.h>
     66  1.41  jdolecek #undef _KERNEL
     67   1.8       cgd #define NFS
     68  1.15      fvdl #include <nfs/nfsproto.h>
     69   1.8       cgd #include <nfs/rpcv2.h>
     70   1.1       cgd #include <nfs/nfs.h>
     71   1.1       cgd #include <nfs/nfsnode.h>
     72   1.8       cgd #undef NFS
     73  1.41  jdolecek #include <msdosfs/denode.h>
     74  1.41  jdolecek #include <msdosfs/bpb.h>
     75  1.41  jdolecek #define	_KERNEL
     76  1.41  jdolecek #include <msdosfs/msdosfsmount.h>
     77  1.41  jdolecek #undef _KERNEL
     78  1.43     enami #define	_KERNEL
     79  1.43     enami #include <miscfs/genfs/layer.h>
     80  1.43     enami #undef _KERNEL
     81   1.1       cgd 
     82   1.1       cgd #include <net/route.h>
     83   1.1       cgd #include <netinet/in.h>
     84   1.1       cgd #include <netinet/in_systm.h>
     85   1.1       cgd #include <netinet/ip.h>
     86   1.1       cgd #include <netinet/in_pcb.h>
     87   1.1       cgd 
     88  1.38    itojun #ifdef INET6
     89  1.38    itojun #include <netinet/ip6.h>
     90  1.38    itojun #include <netinet6/ip6_var.h>
     91  1.38    itojun #include <netinet6/in6_pcb.h>
     92  1.38    itojun #endif
     93  1.38    itojun 
     94  1.38    itojun #include <netdb.h>
     95  1.34     lukem #include <arpa/inet.h>
     96  1.34     lukem 
     97   1.8       cgd #include <ctype.h>
     98   1.1       cgd #include <errno.h>
     99   1.8       cgd #include <kvm.h>
    100  1.15      fvdl #include <limits.h>
    101   1.1       cgd #include <nlist.h>
    102   1.8       cgd #include <paths.h>
    103   1.1       cgd #include <pwd.h>
    104   1.1       cgd #include <stdio.h>
    105   1.1       cgd #include <stdlib.h>
    106   1.1       cgd #include <string.h>
    107  1.23     lukem #include <unistd.h>
    108  1.27      fair #include <err.h>
    109  1.92  christos #include <util.h>
    110   1.1       cgd 
    111  1.34     lukem #include "fstat.h"
    112  1.34     lukem 
    113   1.1       cgd #define	TEXT	-1
    114   1.1       cgd #define	CDIR	-2
    115   1.1       cgd #define	RDIR	-3
    116   1.1       cgd #define	TRACE	-4
    117   1.1       cgd 
    118   1.1       cgd typedef struct devs {
    119   1.1       cgd 	struct	devs *next;
    120   1.1       cgd 	long	fsid;
    121   1.1       cgd 	ino_t	ino;
    122  1.72  christos 	const char *name;
    123   1.1       cgd } DEVS;
    124  1.72  christos static DEVS *devs;
    125   1.1       cgd 
    126  1.72  christos static int 	fsflg,	/* show files on same filesystem as file(s) argument */
    127   1.1       cgd 	pflg,	/* show files open by a particular pid */
    128   1.1       cgd 	uflg;	/* show files open by a particular (effective) user */
    129  1.72  christos static int 	checkfile; /* true if restricting to particular files or filesystems */
    130  1.72  christos static int	nflg;	/* (numerical) display f.s. and rdev as dev_t */
    131   1.1       cgd int	vflg;	/* display errors in locating kernel data objects etc... */
    132   1.1       cgd 
    133  1.80  christos static fdfile_t **ofiles; /* buffer of pointers to file structures */
    134  1.74       mrg static int fstat_maxfiles;
    135   1.1       cgd #define ALLOC_OFILES(d)	\
    136  1.74       mrg 	if ((d) > fstat_maxfiles) { \
    137  1.80  christos 		size_t len = (d) * sizeof(fdfile_t *); \
    138   1.1       cgd 		free(ofiles); \
    139  1.80  christos 		ofiles = malloc(len); \
    140   1.1       cgd 		if (ofiles == NULL) { \
    141  1.80  christos 			err(1, "malloc(%zu)", len);	\
    142   1.1       cgd 		} \
    143  1.74       mrg 		fstat_maxfiles = (d); \
    144   1.1       cgd 	}
    145   1.1       cgd 
    146   1.8       cgd kvm_t *kd;
    147   1.1       cgd 
    148  1.81  christos static const char *const dtypes[] = {
    149  1.81  christos 	DTYPE_NAMES
    150  1.81  christos };
    151  1.81  christos 
    152  1.72  christos static void	dofiles(struct kinfo_proc2 *);
    153  1.72  christos static int	ext2fs_filestat(struct vnode *, struct filestat *);
    154  1.72  christos static int	getfname(const char *);
    155  1.72  christos static void	getinetproto(int);
    156  1.72  christos static char   *getmnton(struct mount *);
    157  1.72  christos static const char   *layer_filestat(struct vnode *, struct filestat *);
    158  1.72  christos static int	msdosfs_filestat(struct vnode *, struct filestat *);
    159  1.72  christos static int	nfs_filestat(struct vnode *, struct filestat *);
    160  1.38    itojun #ifdef INET6
    161  1.72  christos static const char *inet6_addrstr(struct in6_addr *);
    162  1.38    itojun #endif
    163  1.72  christos static void	socktrans(struct socket *, int);
    164  1.84  christos static void	misctrans(struct file *);
    165  1.72  christos static int	ufs_filestat(struct vnode *, struct filestat *);
    166  1.77     perry static void	usage(void) __dead;
    167  1.72  christos static const char   *vfilestat(struct vnode *, struct filestat *);
    168  1.72  christos static void	vtrans(struct vnode *, int, int);
    169  1.80  christos static void	ftrans(fdfile_t *, int);
    170  1.72  christos static void	ptrans(struct file *, struct pipe *, int);
    171   1.1       cgd 
    172  1.23     lukem int
    173  1.72  christos main(int argc, char **argv)
    174   1.1       cgd {
    175  1.23     lukem 	struct passwd *passwd;
    176  1.58       dsl 	struct kinfo_proc2 *p, *plast;
    177   1.1       cgd 	int arg, ch, what;
    178   1.8       cgd 	char *memf, *nlistf;
    179  1.15      fvdl 	char buf[_POSIX2_LINE_MAX];
    180   1.8       cgd 	int cnt;
    181  1.26       mrg 	gid_t egid = getegid();
    182   1.1       cgd 
    183  1.26       mrg 	(void)setegid(getgid());
    184   1.1       cgd 	arg = 0;
    185   1.8       cgd 	what = KERN_PROC_ALL;
    186   1.8       cgd 	nlistf = memf = NULL;
    187  1.22     lukem 	while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1)
    188   1.1       cgd 		switch((char)ch) {
    189   1.1       cgd 		case 'f':
    190   1.1       cgd 			fsflg = 1;
    191   1.1       cgd 			break;
    192   1.8       cgd 		case 'M':
    193   1.8       cgd 			memf = optarg;
    194   1.8       cgd 			break;
    195   1.8       cgd 		case 'N':
    196   1.8       cgd 			nlistf = optarg;
    197   1.8       cgd 			break;
    198   1.1       cgd 		case 'n':
    199   1.1       cgd 			nflg = 1;
    200   1.1       cgd 			break;
    201   1.1       cgd 		case 'p':
    202   1.1       cgd 			if (pflg++)
    203   1.1       cgd 				usage();
    204  1.68       dsl 			if (!isdigit((unsigned char)*optarg)) {
    205  1.34     lukem 				warnx("-p requires a process id");
    206   1.1       cgd 				usage();
    207   1.1       cgd 			}
    208   1.8       cgd 			what = KERN_PROC_PID;
    209   1.1       cgd 			arg = atoi(optarg);
    210   1.1       cgd 			break;
    211   1.1       cgd 		case 'u':
    212   1.1       cgd 			if (uflg++)
    213   1.1       cgd 				usage();
    214   1.1       cgd 			if (!(passwd = getpwnam(optarg))) {
    215  1.34     lukem 				errx(1, "%s: unknown uid", optarg);
    216   1.1       cgd 			}
    217   1.8       cgd 			what = KERN_PROC_UID;
    218   1.1       cgd 			arg = passwd->pw_uid;
    219   1.1       cgd 			break;
    220   1.1       cgd 		case 'v':
    221   1.1       cgd 			vflg = 1;
    222   1.1       cgd 			break;
    223   1.1       cgd 		case '?':
    224   1.1       cgd 		default:
    225   1.1       cgd 			usage();
    226   1.1       cgd 		}
    227   1.1       cgd 
    228   1.1       cgd 	if (*(argv += optind)) {
    229   1.1       cgd 		for (; *argv; ++argv) {
    230   1.1       cgd 			if (getfname(*argv))
    231   1.1       cgd 				checkfile = 1;
    232   1.1       cgd 		}
    233  1.49       wiz 		if (!checkfile)	/* file(s) specified, but none accessible */
    234   1.1       cgd 			exit(1);
    235   1.1       cgd 	}
    236   1.1       cgd 
    237   1.1       cgd 	ALLOC_OFILES(256);	/* reserve space for file pointers */
    238   1.1       cgd 
    239   1.1       cgd 	if (fsflg && !checkfile) {
    240   1.1       cgd 		/* -f with no files means use wd */
    241   1.1       cgd 		if (getfname(".") == 0)
    242   1.1       cgd 			exit(1);
    243   1.1       cgd 		checkfile = 1;
    244   1.1       cgd 	}
    245   1.1       cgd 
    246   1.8       cgd 	/*
    247  1.26       mrg 	 * Discard setgid privileges.  If not the running kernel, we toss
    248  1.26       mrg 	 * them away totally so that bad guys can't print interesting stuff
    249  1.26       mrg 	 * from kernel memory, otherwise switch back to kmem for the
    250  1.26       mrg 	 * duration of the kvm_openfiles() call.
    251   1.8       cgd 	 */
    252   1.8       cgd 	if (nlistf != NULL || memf != NULL)
    253  1.26       mrg 		(void)setgid(getgid());
    254  1.26       mrg 	else
    255  1.26       mrg 		(void)setegid(egid);
    256  1.26       mrg 
    257  1.26       mrg 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
    258  1.26       mrg 		errx(1, "%s", buf);
    259  1.26       mrg 
    260  1.26       mrg 	/* get rid of it now anyway */
    261  1.26       mrg 	if (nlistf == NULL && memf == NULL)
    262  1.26       mrg 		(void)setgid(getgid());
    263   1.8       cgd 
    264  1.58       dsl 	if ((p = kvm_getproc2(kd, what, arg, sizeof *p, &cnt)) == NULL) {
    265  1.34     lukem 		errx(1, "%s", kvm_geterr(kd));
    266   1.1       cgd 	}
    267   1.1       cgd 	if (nflg)
    268  1.72  christos 		(void)printf("%s",
    269  1.55    simonb "USER     CMD          PID   FD  DEV     INUM  MODE  SZ|DV R/W");
    270   1.1       cgd 	else
    271  1.72  christos 		(void)printf("%s",
    272  1.55    simonb "USER     CMD          PID   FD MOUNT       INUM MODE         SZ|DV R/W");
    273   1.1       cgd 	if (checkfile && fsflg == 0)
    274  1.72  christos 		(void)printf(" NAME\n");
    275   1.1       cgd 	else
    276  1.72  christos 		(void)putchar('\n');
    277   1.1       cgd 
    278   1.8       cgd 	for (plast = &p[cnt]; p < plast; ++p) {
    279  1.58       dsl 		if (p->p_stat == SZOMB)
    280   1.1       cgd 			continue;
    281   1.1       cgd 		dofiles(p);
    282   1.1       cgd 	}
    283  1.72  christos 	return 0;
    284   1.1       cgd }
    285   1.1       cgd 
    286  1.72  christos static const	char *Uname, *Comm;
    287  1.34     lukem pid_t	Pid;
    288   1.1       cgd 
    289  1.72  christos #define PREFIX(i) (void)printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
    290   1.1       cgd 	switch(i) { \
    291   1.1       cgd 	case TEXT: \
    292  1.72  christos 		(void)printf(" text"); \
    293   1.1       cgd 		break; \
    294   1.1       cgd 	case CDIR: \
    295  1.72  christos 		(void)printf("   wd"); \
    296   1.1       cgd 		break; \
    297   1.1       cgd 	case RDIR: \
    298  1.72  christos 		(void)printf(" root"); \
    299   1.1       cgd 		break; \
    300   1.1       cgd 	case TRACE: \
    301  1.72  christos 		(void)printf("   tr"); \
    302   1.1       cgd 		break; \
    303   1.1       cgd 	default: \
    304  1.72  christos 		(void)printf(" %4d", i); \
    305   1.1       cgd 		break; \
    306   1.1       cgd 	}
    307   1.1       cgd 
    308   1.1       cgd /*
    309   1.1       cgd  * print open files attributed to this process
    310   1.1       cgd  */
    311  1.72  christos static void
    312  1.72  christos dofiles(struct kinfo_proc2 *p)
    313   1.1       cgd {
    314  1.23     lukem 	int i;
    315  1.78        ad 	struct filedesc filed;
    316  1.35   thorpej 	struct cwdinfo cwdi;
    317  1.89        ad 	struct fdtab dt;
    318   1.1       cgd 
    319  1.58       dsl 	Uname = user_from_uid(p->p_uid, 0);
    320   1.1       cgd 	Pid = p->p_pid;
    321   1.1       cgd 	Comm = p->p_comm;
    322   1.1       cgd 
    323  1.65      fvdl 	if (p->p_fd == 0 || p->p_cwdi == 0)
    324   1.1       cgd 		return;
    325  1.78        ad 	if (!KVM_READ(p->p_fd, &filed, sizeof (filed))) {
    326  1.91  christos 		warnx("can't read filedesc at %p for pid %d",
    327  1.91  christos 		    (void *)(uintptr_t)p->p_fd, Pid);
    328  1.13   mycroft 		return;
    329  1.13   mycroft 	}
    330  1.91  christos 	if (filed.fd_lastfile == -1)
    331  1.91  christos 		return;
    332  1.35   thorpej 	if (!KVM_READ(p->p_cwdi, &cwdi, sizeof(cwdi))) {
    333  1.91  christos 		warnx("can't read cwdinfo at %p for pid %d",
    334  1.91  christos 		    (void *)(uintptr_t)p->p_cwdi, Pid);
    335  1.35   thorpej 		return;
    336  1.35   thorpej 	}
    337  1.89        ad 	if (!KVM_READ(filed.fd_dt, &dt, sizeof(dt))) {
    338  1.89        ad 		warnx("can't read dtab at %p for pid %d", filed.fd_dt, Pid);
    339  1.89        ad 		return;
    340  1.89        ad 	}
    341  1.89        ad 	if ((unsigned)filed.fd_lastfile >= dt.dt_nfiles ||
    342  1.13   mycroft 	    filed.fd_freefile > filed.fd_lastfile + 1) {
    343  1.91  christos 		dprintf("filedesc corrupted at %p for pid %d",
    344  1.91  christos 		    (void *)(uintptr_t)p->p_fd, Pid);
    345   1.1       cgd 		return;
    346   1.1       cgd 	}
    347   1.1       cgd 	/*
    348   1.1       cgd 	 * root directory vnode, if one
    349   1.1       cgd 	 */
    350  1.35   thorpej 	if (cwdi.cwdi_rdir)
    351  1.35   thorpej 		vtrans(cwdi.cwdi_rdir, RDIR, FREAD);
    352   1.1       cgd 	/*
    353   1.1       cgd 	 * current working directory vnode
    354   1.1       cgd 	 */
    355  1.35   thorpej 	vtrans(cwdi.cwdi_cdir, CDIR, FREAD);
    356  1.80  christos #if 0
    357   1.1       cgd 	/*
    358  1.80  christos 	 * Disable for now, since p->p_tracep appears to point to a ktr_desc *
    359   1.1       cgd 	 * ktrace vnode, if one
    360   1.1       cgd 	 */
    361   1.1       cgd 	if (p->p_tracep)
    362  1.92  christos 		ftrans(p->p_tracep, TRACE);
    363  1.80  christos #endif
    364   1.1       cgd 	/*
    365   1.1       cgd 	 * open files
    366   1.1       cgd 	 */
    367  1.80  christos #define FPSIZE	(sizeof (fdfile_t *))
    368   1.8       cgd 	ALLOC_OFILES(filed.fd_lastfile+1);
    369  1.89        ad 	if (!KVM_READ(&filed.fd_dt->dt_ff, ofiles,
    370  1.76    dyoung 	    (filed.fd_lastfile+1) * FPSIZE)) {
    371  1.76    dyoung 		dprintf("can't read file structures at %p for pid %d",
    372  1.89        ad 		    &filed.fd_dt->dt_ff, Pid);
    373  1.76    dyoung 		return;
    374  1.76    dyoung 	}
    375   1.1       cgd 	for (i = 0; i <= filed.fd_lastfile; i++) {
    376   1.1       cgd 		if (ofiles[i] == NULL)
    377   1.1       cgd 			continue;
    378  1.42  sommerfe 		ftrans(ofiles[i], i);
    379  1.42  sommerfe 	}
    380  1.42  sommerfe }
    381  1.42  sommerfe 
    382  1.72  christos static void
    383  1.80  christos ftrans(fdfile_t *fp, int i)
    384  1.42  sommerfe {
    385  1.42  sommerfe 	struct file file;
    386  1.80  christos 	fdfile_t fdfile;
    387  1.42  sommerfe 
    388  1.80  christos 	if (!KVM_READ(fp, &fdfile, sizeof(fdfile))) {
    389  1.42  sommerfe 		dprintf("can't read file %d at %p for pid %d",
    390  1.42  sommerfe 		    i, fp, Pid);
    391  1.42  sommerfe 		return;
    392  1.42  sommerfe 	}
    393  1.82  christos 	if (fdfile.ff_file == NULL)
    394  1.82  christos 		return;
    395  1.80  christos 	if (!KVM_READ(fdfile.ff_file, &file, sizeof(file))) {
    396  1.80  christos 		dprintf("can't read file %d at %p for pid %d",
    397  1.80  christos 		    i, fdfile.ff_file, Pid);
    398  1.80  christos 		return;
    399  1.80  christos 	}
    400  1.48     lukem 	switch (file.f_type) {
    401  1.48     lukem 	case DTYPE_VNODE:
    402  1.92  christos 		vtrans(file.f_data, i, file.f_flag);
    403  1.48     lukem 		break;
    404  1.48     lukem 	case DTYPE_SOCKET:
    405  1.42  sommerfe 		if (checkfile == 0)
    406  1.92  christos 			socktrans(file.f_data, i);
    407  1.48     lukem 		break;
    408  1.48     lukem 	case DTYPE_PIPE:
    409  1.47  jdolecek 		if (checkfile == 0)
    410  1.92  christos 			ptrans(&file, file.f_data, i);
    411  1.48     lukem 		break;
    412  1.84  christos 	case DTYPE_MISC:
    413  1.56  jdolecek 	case DTYPE_KQUEUE:
    414  1.81  christos 	case DTYPE_CRYPTO:
    415  1.81  christos 	case DTYPE_MQUEUE:
    416  1.90     rmind 	case DTYPE_SEM:
    417  1.56  jdolecek 		if (checkfile == 0)
    418  1.84  christos 			misctrans(&file);
    419  1.56  jdolecek 		break;
    420  1.48     lukem 	default:
    421  1.42  sommerfe 		dprintf("unknown file type %d for file %d of pid %d",
    422  1.42  sommerfe 		    file.f_type, i, Pid);
    423  1.48     lukem 		break;
    424   1.1       cgd 	}
    425   1.1       cgd }
    426   1.1       cgd 
    427  1.92  christos static const char dead[] = "dead";
    428  1.92  christos 
    429  1.72  christos static const char *
    430  1.72  christos vfilestat(struct vnode *vp, struct filestat *fsp)
    431   1.1       cgd {
    432  1.72  christos 	const char *badtype = NULL;
    433   1.1       cgd 
    434  1.92  christos 	if (vp->v_type == VNON)
    435   1.1       cgd 		badtype = "none";
    436  1.44     enami 	else if (vp->v_type == VBAD)
    437   1.1       cgd 		badtype = "bad";
    438   1.1       cgd 	else
    439  1.44     enami 		switch (vp->v_tag) {
    440  1.92  christos 		case VT_NON:
    441  1.92  christos 			badtype = dead;
    442  1.92  christos 			break;
    443   1.1       cgd 		case VT_UFS:
    444  1.57      yamt 		case VT_LFS:
    445   1.1       cgd 		case VT_MFS:
    446  1.44     enami 			if (!ufs_filestat(vp, fsp))
    447   1.8       cgd 				badtype = "error";
    448   1.1       cgd 			break;
    449  1.41  jdolecek 		case VT_MSDOSFS:
    450  1.44     enami 			if (!msdosfs_filestat(vp, fsp))
    451  1.41  jdolecek 				badtype = "error";
    452  1.41  jdolecek 			break;
    453   1.1       cgd 		case VT_NFS:
    454  1.44     enami 			if (!nfs_filestat(vp, fsp))
    455   1.8       cgd 				badtype = "error";
    456   1.1       cgd 			break;
    457  1.20    bouyer 		case VT_EXT2FS:
    458  1.44     enami 			if (!ext2fs_filestat(vp, fsp))
    459  1.20    bouyer 				badtype = "error";
    460  1.20    bouyer 			break;
    461  1.34     lukem 		case VT_ISOFS:
    462  1.44     enami 			if (!isofs_filestat(vp, fsp))
    463  1.37  jdolecek 				badtype = "error";
    464  1.37  jdolecek 			break;
    465  1.37  jdolecek 		case VT_NTFS:
    466  1.44     enami 			if (!ntfs_filestat(vp, fsp))
    467  1.34     lukem 				badtype = "error";
    468  1.34     lukem 			break;
    469  1.70  christos 		case VT_PTYFS:
    470  1.70  christos 			if (!ptyfs_filestat(vp, fsp))
    471  1.70  christos 				badtype = "error";
    472  1.70  christos 			break;
    473  1.73  christos 		case VT_TMPFS:
    474  1.73  christos 			if (!tmpfs_filestat(vp, fsp))
    475  1.73  christos 				badtype = "error";
    476  1.73  christos 			break;
    477  1.43     enami 		case VT_NULL:
    478  1.43     enami 		case VT_OVERLAY:
    479  1.43     enami 		case VT_UMAP:
    480  1.44     enami 			badtype = layer_filestat(vp, fsp);
    481  1.43     enami 			break;
    482   1.1       cgd 		default: {
    483   1.1       cgd 			static char unknown[10];
    484  1.72  christos 			(void)snprintf(unknown, sizeof unknown,
    485  1.44     enami 			    "?(%x)", vp->v_tag);
    486  1.72  christos 			badtype = unknown;
    487  1.44     enami 			break;
    488   1.1       cgd 		}
    489   1.1       cgd 	}
    490  1.91  christos 	return badtype;
    491  1.44     enami }
    492  1.44     enami 
    493  1.72  christos static void
    494  1.72  christos vtrans(struct vnode *vp, int i, int flag)
    495  1.44     enami {
    496  1.44     enami 	struct vnode vn;
    497  1.44     enami 	struct filestat fst;
    498  1.44     enami 	char mode[15], rw[3];
    499  1.72  christos 	const char *badtype, *filename;
    500  1.44     enami 
    501  1.44     enami 	filename = NULL;
    502  1.44     enami 	if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
    503  1.44     enami 		dprintf("can't read vnode at %p for pid %d", vp, Pid);
    504  1.44     enami 		return;
    505  1.44     enami 	}
    506  1.44     enami 	badtype = vfilestat(&vn, &fst);
    507   1.1       cgd 	if (checkfile) {
    508   1.1       cgd 		int fsmatch = 0;
    509  1.23     lukem 		DEVS *d;
    510   1.1       cgd 
    511  1.92  christos 		if (badtype && badtype != dead)
    512   1.1       cgd 			return;
    513   1.1       cgd 		for (d = devs; d != NULL; d = d->next)
    514   1.1       cgd 			if (d->fsid == fst.fsid) {
    515   1.1       cgd 				fsmatch = 1;
    516   1.1       cgd 				if (d->ino == fst.fileid) {
    517   1.1       cgd 					filename = d->name;
    518   1.1       cgd 					break;
    519   1.1       cgd 				}
    520   1.1       cgd 			}
    521   1.1       cgd 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
    522   1.1       cgd 			return;
    523   1.1       cgd 	}
    524   1.1       cgd 	PREFIX(i);
    525  1.92  christos 	if (badtype == dead) {
    526  1.92  christos 		char buf[1024];
    527  1.92  christos 		(void)snprintb(buf, sizeof(buf), VNODE_FLAGBITS,
    528  1.92  christos 		    vn.v_iflag | vn.v_vflag | vn.v_uflag);
    529  1.92  christos 		(void)printf(" flags %s\n", buf);
    530  1.92  christos 		return;
    531  1.92  christos 	} else if (badtype) {
    532   1.1       cgd 		(void)printf(" -         -  %10s    -\n", badtype);
    533   1.1       cgd 		return;
    534   1.1       cgd 	}
    535   1.1       cgd 	if (nflg)
    536  1.87  christos 		(void)printf(" %2llu,%-2llu",
    537  1.87  christos 		    (unsigned long long)major(fst.fsid),
    538  1.87  christos 		    (unsigned long long)minor(fst.fsid));
    539   1.1       cgd 	else
    540   1.1       cgd 		(void)printf(" %-8s", getmnton(vn.v_mount));
    541   1.1       cgd 	if (nflg)
    542  1.18       mrg 		(void)snprintf(mode, sizeof mode, "%o", fst.mode);
    543   1.1       cgd 	else
    544   1.1       cgd 		strmode(fst.mode, mode);
    545  1.88     lukem 	(void)printf(" %7"PRIu64" %*s", fst.fileid, nflg ? 5 : 10, mode);
    546   1.1       cgd 	switch (vn.v_type) {
    547   1.1       cgd 	case VBLK:
    548   1.1       cgd 	case VCHR: {
    549   1.1       cgd 		char *name;
    550   1.1       cgd 
    551   1.1       cgd 		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
    552   1.1       cgd 		    S_IFCHR : S_IFBLK)) == NULL))
    553  1.87  christos 			(void)printf("  %2llu,%-2llu",
    554  1.87  christos 			    (unsigned long long)major(fst.rdev),
    555  1.87  christos 			    (unsigned long long)minor(fst.rdev));
    556   1.1       cgd 		else
    557  1.72  christos 			(void)printf(" %6s", name);
    558   1.1       cgd 		break;
    559   1.1       cgd 	}
    560   1.1       cgd 	default:
    561  1.72  christos 		(void)printf(" %6lld", (long long)fst.size);
    562   1.1       cgd 	}
    563  1.34     lukem 	rw[0] = '\0';
    564   1.8       cgd 	if (flag & FREAD)
    565  1.72  christos 		(void)strlcat(rw, "r", sizeof(rw));
    566   1.8       cgd 	if (flag & FWRITE)
    567  1.72  christos 		(void)strlcat(rw, "w", sizeof(rw));
    568  1.72  christos 	(void)printf(" %-2s", rw);
    569   1.1       cgd 	if (filename && !fsflg)
    570  1.72  christos 		(void)printf("  %s", filename);
    571  1.72  christos 	(void)putchar('\n');
    572   1.1       cgd }
    573   1.1       cgd 
    574  1.72  christos static int
    575  1.72  christos ufs_filestat(struct vnode *vp, struct filestat *fsp)
    576   1.1       cgd {
    577   1.8       cgd 	struct inode inode;
    578  1.62      fvdl 	union dinode {
    579  1.62      fvdl 		struct ufs1_dinode dp1;
    580  1.62      fvdl 		struct ufs2_dinode dp2;
    581  1.62      fvdl 	} dip;
    582   1.8       cgd 
    583   1.8       cgd 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
    584  1.34     lukem 		dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
    585   1.8       cgd 		return 0;
    586   1.8       cgd 	}
    587  1.62      fvdl 
    588  1.62      fvdl 	if (!KVM_READ(inode.i_din.ffs1_din, &dip, sizeof(struct ufs1_dinode))) {
    589  1.62      fvdl 		dprintf("can't read dinode at %p for pid %d",
    590  1.62      fvdl 		    inode.i_din.ffs1_din, Pid);
    591  1.62      fvdl 		return 0;
    592  1.62      fvdl 	}
    593  1.62      fvdl 	if (inode.i_size == dip.dp1.di_size)
    594  1.62      fvdl 		fsp->rdev = dip.dp1.di_rdev;
    595  1.62      fvdl 	else {
    596  1.62      fvdl 		if (!KVM_READ(inode.i_din.ffs1_din, &dip,
    597  1.62      fvdl 		    sizeof(struct ufs2_dinode))) {
    598  1.62      fvdl 			dprintf("can't read dinode at %p for pid %d",
    599  1.62      fvdl 			    inode.i_din.ffs1_din, Pid);
    600  1.62      fvdl 			return 0;
    601  1.62      fvdl 		}
    602  1.62      fvdl 		fsp->rdev = dip.dp2.di_rdev;
    603  1.62      fvdl 	}
    604  1.62      fvdl 
    605   1.8       cgd 	fsp->fsid = inode.i_dev & 0xffff;
    606  1.88     lukem 	fsp->fileid = inode.i_number;
    607  1.62      fvdl 	fsp->mode = (mode_t)inode.i_mode;
    608  1.62      fvdl 	fsp->size = inode.i_size;
    609  1.20    bouyer 
    610  1.20    bouyer 	return 1;
    611  1.20    bouyer }
    612  1.20    bouyer 
    613  1.72  christos static int
    614  1.72  christos ext2fs_filestat(struct vnode *vp, struct filestat *fsp)
    615  1.20    bouyer {
    616  1.20    bouyer 	struct inode inode;
    617  1.66   aymeric 	u_int16_t mode;
    618  1.66   aymeric 	u_int32_t size;
    619   1.1       cgd 
    620  1.20    bouyer 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
    621  1.34     lukem 		dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
    622  1.20    bouyer 		return 0;
    623  1.20    bouyer 	}
    624  1.20    bouyer 	fsp->fsid = inode.i_dev & 0xffff;
    625  1.88     lukem 	fsp->fileid = inode.i_number;
    626  1.66   aymeric 
    627  1.66   aymeric 	if (!KVM_READ(&inode.i_e2fs_mode, &mode, sizeof mode)) {
    628  1.66   aymeric 		dprintf("can't read inode %p's mode at %p for pid %d", VTOI(vp),
    629  1.66   aymeric 			&inode.i_e2fs_mode, Pid);
    630  1.66   aymeric 		return 0;
    631  1.66   aymeric 	}
    632  1.66   aymeric 	fsp->mode = mode;
    633  1.66   aymeric 
    634  1.66   aymeric 	if (!KVM_READ(&inode.i_e2fs_size, &size, sizeof size)) {
    635  1.66   aymeric 		dprintf("can't read inode %p's size at %p for pid %d", VTOI(vp),
    636  1.66   aymeric 			&inode.i_e2fs_size, Pid);
    637  1.66   aymeric 		return 0;
    638  1.66   aymeric 	}
    639  1.66   aymeric 	fsp->size = size;
    640  1.20    bouyer 	fsp->rdev = 0;  /* XXX */
    641   1.8       cgd 	return 1;
    642   1.1       cgd }
    643   1.1       cgd 
    644  1.72  christos static int
    645  1.72  christos nfs_filestat(struct vnode *vp, struct filestat *fsp)
    646   1.1       cgd {
    647   1.8       cgd 	struct nfsnode nfsnode;
    648  1.24   thorpej 	struct vattr va;
    649   1.1       cgd 
    650   1.8       cgd 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
    651  1.34     lukem 		dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp),
    652  1.34     lukem 		    Pid);
    653   1.8       cgd 		return 0;
    654   1.8       cgd 	}
    655  1.24   thorpej 	if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
    656  1.34     lukem 		dprintf("can't read vnode attributes at %p for pid %d",
    657  1.34     lukem 		    nfsnode.n_vattr, Pid);
    658  1.24   thorpej 		return 0;
    659  1.24   thorpej 	}
    660  1.24   thorpej 	fsp->fsid = va.va_fsid;
    661  1.24   thorpej 	fsp->fileid = va.va_fileid;
    662   1.8       cgd 	fsp->size = nfsnode.n_size;
    663  1.24   thorpej 	fsp->rdev = va.va_rdev;
    664  1.40  jdolecek 	fsp->mode = (mode_t)va.va_mode | getftype(vp->v_type);
    665   1.8       cgd 
    666   1.8       cgd 	return 1;
    667   1.1       cgd }
    668   1.1       cgd 
    669  1.72  christos static int
    670  1.72  christos msdosfs_filestat(struct vnode *vp, struct filestat *fsp)
    671  1.41  jdolecek {
    672  1.41  jdolecek 	struct denode de;
    673  1.41  jdolecek 	struct msdosfsmount mp;
    674  1.41  jdolecek 
    675  1.41  jdolecek 	if (!KVM_READ(VTONFS(vp), &de, sizeof(de))) {
    676  1.41  jdolecek 		dprintf("can't read denode at %p for pid %d", VTONFS(vp),
    677  1.41  jdolecek 		    Pid);
    678  1.41  jdolecek 		return 0;
    679  1.41  jdolecek 	}
    680  1.41  jdolecek 	if (!KVM_READ(de.de_pmp, &mp, sizeof(mp))) {
    681  1.41  jdolecek 		dprintf("can't read mount struct at %p for pid %d", de.de_pmp,
    682  1.41  jdolecek 		    Pid);
    683  1.41  jdolecek 		return 0;
    684  1.41  jdolecek 	}
    685  1.41  jdolecek 
    686  1.41  jdolecek 	fsp->fsid = de.de_dev & 0xffff;
    687  1.41  jdolecek 	fsp->fileid = 0; /* XXX see msdosfs_vptofh() for more info */
    688  1.41  jdolecek 	fsp->size = de.de_FileSize;
    689  1.41  jdolecek 	fsp->rdev = 0;	/* msdosfs doesn't support device files */
    690  1.41  jdolecek 	fsp->mode = (0777 & mp.pm_mask) | getftype(vp->v_type);
    691  1.41  jdolecek 	return 1;
    692  1.43     enami }
    693  1.43     enami 
    694  1.72  christos static const char *
    695  1.72  christos layer_filestat(struct vnode *vp, struct filestat *fsp)
    696  1.43     enami {
    697  1.43     enami 	struct layer_node layer_node;
    698  1.44     enami 	struct mount mount;
    699  1.44     enami 	struct vnode vn;
    700  1.72  christos 	const char *badtype;
    701  1.43     enami 
    702  1.43     enami 	if (!KVM_READ(VTOLAYER(vp), &layer_node, sizeof(layer_node))) {
    703  1.43     enami 		dprintf("can't read layer_node at %p for pid %d",
    704  1.43     enami 		    VTOLAYER(vp), Pid);
    705  1.91  christos 		return "error";
    706  1.44     enami 	}
    707  1.44     enami 	if (!KVM_READ(vp->v_mount, &mount, sizeof(struct mount))) {
    708  1.44     enami 		dprintf("can't read mount struct at %p for pid %d",
    709  1.44     enami 		    vp->v_mount, Pid);
    710  1.91  christos 		return "error";
    711  1.44     enami 	}
    712  1.44     enami 	vp = layer_node.layer_lowervp;
    713  1.44     enami 	if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
    714  1.44     enami 		dprintf("can't read vnode at %p for pid %d", vp, Pid);
    715  1.91  christos 		return "error";
    716  1.43     enami 	}
    717  1.44     enami 	if ((badtype = vfilestat(&vn, fsp)) == NULL)
    718  1.67  christos 		fsp->fsid = mount.mnt_stat.f_fsidx.__fsid_val[0];
    719  1.91  christos 	return badtype;
    720  1.41  jdolecek }
    721   1.1       cgd 
    722  1.72  christos static char *
    723  1.72  christos getmnton(struct mount *m)
    724   1.1       cgd {
    725   1.1       cgd 	static struct mount mount;
    726   1.1       cgd 	static struct mtab {
    727   1.1       cgd 		struct mtab *next;
    728   1.1       cgd 		struct mount *m;
    729   1.1       cgd 		char mntonname[MNAMELEN];
    730   1.1       cgd 	} *mhead = NULL;
    731  1.23     lukem 	struct mtab *mt;
    732   1.1       cgd 
    733   1.1       cgd 	for (mt = mhead; mt != NULL; mt = mt->next)
    734   1.1       cgd 		if (m == mt->m)
    735  1.91  christos 			return mt->mntonname;
    736   1.1       cgd 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
    737  1.34     lukem 		warnx("can't read mount table at %p", m);
    738  1.91  christos 		return NULL;
    739   1.1       cgd 	}
    740   1.1       cgd 	if ((mt = malloc(sizeof (struct mtab))) == NULL) {
    741  1.30      ross 		err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab));
    742   1.1       cgd 	}
    743   1.1       cgd 	mt->m = m;
    744  1.72  christos 	(void)memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0],
    745  1.72  christos 	    MNAMELEN);
    746   1.1       cgd 	mt->next = mhead;
    747   1.1       cgd 	mhead = mt;
    748  1.91  christos 	return mt->mntonname;
    749   1.1       cgd }
    750   1.1       cgd 
    751  1.38    itojun #ifdef INET6
    752  1.38    itojun static const char *
    753  1.72  christos inet6_addrstr(struct in6_addr *p)
    754  1.38    itojun {
    755  1.38    itojun 	struct sockaddr_in6 sin6;
    756  1.38    itojun 	static char hbuf[NI_MAXHOST];
    757  1.39  jdolecek 	const int niflags = NI_NUMERICHOST;
    758  1.38    itojun 
    759  1.72  christos 	(void)memset(&sin6, 0, sizeof(sin6));
    760  1.38    itojun 	sin6.sin6_family = AF_INET6;
    761  1.38    itojun 	sin6.sin6_len = sizeof(struct sockaddr_in6);
    762  1.38    itojun 	sin6.sin6_addr = *p;
    763  1.38    itojun 	if (IN6_IS_ADDR_LINKLOCAL(p) &&
    764  1.38    itojun 	    *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
    765  1.38    itojun 		sin6.sin6_scope_id =
    766  1.38    itojun 			ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
    767  1.38    itojun 		sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0;
    768  1.38    itojun 	}
    769  1.38    itojun 
    770  1.38    itojun 	if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
    771  1.38    itojun 			hbuf, sizeof(hbuf), NULL, 0, niflags))
    772  1.38    itojun 		return "invalid";
    773  1.38    itojun 
    774  1.38    itojun 	return hbuf;
    775  1.38    itojun }
    776  1.38    itojun #endif
    777  1.38    itojun 
    778  1.72  christos static void
    779  1.72  christos socktrans(struct socket *sock, int i)
    780   1.1       cgd {
    781  1.72  christos 	static const char *stypename[] = {
    782   1.1       cgd 		"unused",	/* 0 */
    783   1.1       cgd 		"stream", 	/* 1 */
    784   1.1       cgd 		"dgram",	/* 2 */
    785   1.1       cgd 		"raw",		/* 3 */
    786   1.1       cgd 		"rdm",		/* 4 */
    787   1.1       cgd 		"seqpak"	/* 5 */
    788   1.1       cgd 	};
    789   1.1       cgd #define	STYPEMAX 5
    790   1.1       cgd 	struct socket	so;
    791   1.1       cgd 	struct protosw	proto;
    792   1.1       cgd 	struct domain	dom;
    793   1.1       cgd 	struct inpcb	inpcb;
    794  1.38    itojun #ifdef INET6
    795  1.38    itojun 	struct in6pcb	in6pcb;
    796  1.38    itojun #endif
    797   1.1       cgd 	struct unpcb	unpcb;
    798   1.1       cgd 	int len;
    799  1.18       mrg 	char dname[32];
    800  1.38    itojun #ifdef INET6
    801  1.38    itojun 	char xaddrbuf[NI_MAXHOST + 2];
    802  1.38    itojun #endif
    803   1.1       cgd 
    804   1.1       cgd 	PREFIX(i);
    805   1.1       cgd 
    806   1.1       cgd 	/* fill in socket */
    807   1.1       cgd 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
    808  1.34     lukem 		dprintf("can't read sock at %p", sock);
    809   1.1       cgd 		goto bad;
    810   1.1       cgd 	}
    811   1.1       cgd 
    812   1.1       cgd 	/* fill in protosw entry */
    813   1.1       cgd 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
    814  1.34     lukem 		dprintf("can't read protosw at %p", so.so_proto);
    815   1.1       cgd 		goto bad;
    816   1.1       cgd 	}
    817   1.1       cgd 
    818   1.1       cgd 	/* fill in domain */
    819   1.1       cgd 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
    820  1.34     lukem 		dprintf("can't read domain at %p", proto.pr_domain);
    821   1.1       cgd 		goto bad;
    822   1.1       cgd 	}
    823   1.1       cgd 
    824   1.8       cgd 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
    825  1.25   msaitoh 	    sizeof(dname) - 1)) != sizeof(dname) -1) {
    826  1.34     lukem 		dprintf("can't read domain name at %p", dom.dom_name);
    827   1.1       cgd 		dname[0] = '\0';
    828   1.1       cgd 	}
    829   1.1       cgd 	else
    830   1.1       cgd 		dname[len] = '\0';
    831   1.1       cgd 
    832   1.1       cgd 	if ((u_short)so.so_type > STYPEMAX)
    833  1.72  christos 		(void)printf("* %s ?%d", dname, so.so_type);
    834   1.1       cgd 	else
    835  1.72  christos 		(void)printf("* %s %s", dname, stypename[so.so_type]);
    836   1.1       cgd 
    837   1.1       cgd 	/*
    838   1.1       cgd 	 * protocol specific formatting
    839   1.1       cgd 	 *
    840  1.34     lukem 	 * Try to find interesting things to print.  For TCP, the interesting
    841  1.34     lukem 	 * thing is the address of the tcpcb, for UDP and others, just the
    842  1.34     lukem 	 * inpcb (socket pcb).  For UNIX domain, its the address of the socket
    843   1.1       cgd 	 * pcb and the address of the connected pcb (if connected).  Otherwise
    844   1.1       cgd 	 * just print the protocol number and address of the socket itself.
    845   1.1       cgd 	 * The idea is not to duplicate netstat, but to make available enough
    846   1.1       cgd 	 * information for further analysis.
    847   1.1       cgd 	 */
    848   1.1       cgd 	switch(dom.dom_family) {
    849   1.1       cgd 	case AF_INET:
    850   1.1       cgd 		getinetproto(proto.pr_protocol);
    851  1.34     lukem 		if (proto.pr_protocol == IPPROTO_TCP) {
    852  1.34     lukem 			if (so.so_pcb == NULL)
    853  1.34     lukem 				break;
    854  1.34     lukem 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
    855  1.34     lukem 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
    856  1.34     lukem 				dprintf("can't read inpcb at %p", so.so_pcb);
    857  1.34     lukem 				goto bad;
    858  1.34     lukem 			}
    859  1.72  christos 			(void)printf(" %lx", (long)inpcb.inp_ppcb);
    860  1.72  christos 			(void)printf(" %s:%d",
    861  1.34     lukem 			    inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
    862  1.34     lukem 			    inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
    863  1.34     lukem 			if (inpcb.inp_fport) {
    864  1.72  christos 				(void)printf(" <-> %s:%d",
    865  1.34     lukem 				    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
    866  1.34     lukem 				    inet_ntoa(inpcb.inp_faddr),
    867  1.34     lukem 				    ntohs(inpcb.inp_fport));
    868  1.34     lukem 			}
    869  1.34     lukem 		} else if (proto.pr_protocol == IPPROTO_UDP) {
    870  1.34     lukem 			if (so.so_pcb == NULL)
    871  1.34     lukem 				break;
    872  1.34     lukem 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
    873  1.34     lukem 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
    874  1.34     lukem 				dprintf("can't read inpcb at %p", so.so_pcb);
    875  1.34     lukem 				goto bad;
    876   1.1       cgd 			}
    877  1.72  christos 			(void)printf(" %lx", (long)so.so_pcb);
    878  1.72  christos 			(void)printf(" %s:%d",
    879  1.34     lukem 			    inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
    880  1.34     lukem 			    inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
    881  1.34     lukem 			if (inpcb.inp_fport)
    882  1.72  christos 				(void)printf(" <-> %s:%d",
    883  1.34     lukem 				    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
    884  1.34     lukem 				    inet_ntoa(inpcb.inp_faddr),
    885  1.34     lukem 				    ntohs(inpcb.inp_fport));
    886  1.34     lukem 		} else if (so.so_pcb)
    887  1.72  christos 			(void)printf(" %lx", (long)so.so_pcb);
    888   1.1       cgd 		break;
    889  1.38    itojun #ifdef INET6
    890  1.38    itojun 	case AF_INET6:
    891  1.38    itojun 		getinetproto(proto.pr_protocol);
    892  1.38    itojun 		if (proto.pr_protocol == IPPROTO_TCP) {
    893  1.38    itojun 			if (so.so_pcb == NULL)
    894  1.38    itojun 				break;
    895  1.38    itojun 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb,
    896  1.38    itojun 			    sizeof(struct in6pcb)) != sizeof(struct in6pcb)) {
    897  1.38    itojun 				dprintf("can't read in6pcb at %p", so.so_pcb);
    898  1.38    itojun 				goto bad;
    899  1.38    itojun 			}
    900  1.72  christos 			(void)printf(" %lx", (long)in6pcb.in6p_ppcb);
    901  1.72  christos 			(void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
    902  1.38    itojun 			    inet6_addrstr(&in6pcb.in6p_laddr));
    903  1.72  christos 			(void)printf(" %s:%d",
    904  1.38    itojun 			    IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" :
    905  1.38    itojun 			    xaddrbuf,
    906  1.38    itojun 			    ntohs(in6pcb.in6p_lport));
    907  1.38    itojun 			if (in6pcb.in6p_fport) {
    908  1.72  christos 				(void)snprintf(xaddrbuf, sizeof(xaddrbuf),
    909  1.72  christos 				    "[%s]", inet6_addrstr(&in6pcb.in6p_faddr));
    910  1.72  christos 				(void)printf(" <-> %s:%d",
    911  1.38    itojun 			            IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" :
    912  1.38    itojun 				    xaddrbuf,
    913  1.38    itojun 				    ntohs(in6pcb.in6p_fport));
    914  1.38    itojun 			}
    915  1.38    itojun 		} else if (proto.pr_protocol == IPPROTO_UDP) {
    916  1.38    itojun 			if (so.so_pcb == NULL)
    917  1.38    itojun 				break;
    918  1.38    itojun 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb,
    919  1.38    itojun 			    sizeof(struct in6pcb)) != sizeof(struct in6pcb)) {
    920  1.38    itojun 				dprintf("can't read inpcb at %p", so.so_pcb);
    921  1.38    itojun 				goto bad;
    922  1.38    itojun 			}
    923  1.72  christos 			(void)printf(" %lx", (long)so.so_pcb);
    924  1.72  christos 			(void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
    925  1.38    itojun 			    inet6_addrstr(&in6pcb.in6p_laddr));
    926  1.72  christos 			(void)printf(" %s:%d",
    927  1.38    itojun 		            IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" :
    928  1.38    itojun 			    xaddrbuf,
    929  1.38    itojun 			    ntohs(in6pcb.in6p_lport));
    930  1.38    itojun 			if (in6pcb.in6p_fport) {
    931  1.72  christos 				(void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
    932  1.38    itojun 				    inet6_addrstr(&in6pcb.in6p_faddr));
    933  1.72  christos 				(void)printf(" <-> %s:%d",
    934  1.38    itojun 			            IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" :
    935  1.38    itojun 				    xaddrbuf,
    936  1.38    itojun 				    ntohs(in6pcb.in6p_fport));
    937  1.38    itojun 			}
    938  1.38    itojun 		} else if (so.so_pcb)
    939  1.72  christos 			(void)printf(" %lx", (long)so.so_pcb);
    940  1.38    itojun 		break;
    941  1.38    itojun #endif
    942  1.31     lukem 	case AF_LOCAL:
    943   1.1       cgd 		/* print address of pcb and connected pcb */
    944   1.1       cgd 		if (so.so_pcb) {
    945  1.91  christos 			char shoconn[4], *cp;
    946  1.91  christos 
    947  1.72  christos 			(void)printf(" %lx", (long)so.so_pcb);
    948   1.8       cgd 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
    949   1.1       cgd 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
    950  1.34     lukem 				dprintf("can't read unpcb at %p", so.so_pcb);
    951   1.1       cgd 				goto bad;
    952   1.1       cgd 			}
    953  1.91  christos 
    954  1.91  christos 			cp = shoconn;
    955  1.91  christos 			if (!(so.so_state & SS_CANTRCVMORE))
    956  1.91  christos 				*cp++ = '<';
    957  1.91  christos 			*cp++ = '-';
    958  1.91  christos 			if (!(so.so_state & SS_CANTSENDMORE))
    959  1.91  christos 				*cp++ = '>';
    960  1.91  christos 			*cp = '\0';
    961  1.91  christos 			if (unpcb.unp_addr) {
    962  1.91  christos 				struct sockaddr_un *sun =
    963  1.91  christos 					malloc(unpcb.unp_addrlen);
    964  1.91  christos 				if (sun == NULL)
    965  1.91  christos 				    err(1, "malloc(%zu)",
    966  1.91  christos 					unpcb.unp_addrlen);
    967  1.91  christos 				if (kvm_read(kd, (u_long)unpcb.unp_addr,
    968  1.91  christos 				    sun, unpcb.unp_addrlen) !=
    969  1.91  christos 				    (ssize_t)unpcb.unp_addrlen) {
    970  1.91  christos 					dprintf("can't read sun at %p",
    971  1.91  christos 					    unpcb.unp_addr);
    972  1.91  christos 					free(sun);
    973  1.91  christos 				} else {
    974  1.91  christos 					(void)printf(" %s %s",
    975  1.91  christos 					    shoconn, sun->sun_path);
    976  1.91  christos 					free(sun);
    977  1.91  christos 					break;
    978  1.91  christos 				}
    979  1.91  christos 			}
    980   1.1       cgd 			if (unpcb.unp_conn) {
    981  1.72  christos 				(void)printf(" %s %lx", shoconn,
    982  1.12       cgd 				    (long)unpcb.unp_conn);
    983   1.1       cgd 			}
    984   1.1       cgd 		}
    985   1.1       cgd 		break;
    986   1.1       cgd 	default:
    987   1.1       cgd 		/* print protocol number and socket address */
    988  1.72  christos 		(void)printf(" %d %lx", proto.pr_protocol, (long)sock);
    989   1.1       cgd 	}
    990  1.72  christos 	(void)printf("\n");
    991  1.47  jdolecek 	return;
    992  1.47  jdolecek bad:
    993  1.72  christos 	(void)printf("* error\n");
    994  1.47  jdolecek }
    995  1.47  jdolecek 
    996  1.72  christos static void
    997  1.72  christos ptrans(struct file *fp, struct pipe *cpipe, int i)
    998  1.47  jdolecek {
    999  1.47  jdolecek 	struct pipe cp;
   1000  1.47  jdolecek 
   1001  1.47  jdolecek 	PREFIX(i);
   1002  1.47  jdolecek 
   1003  1.50  jdolecek 	/* fill in pipe */
   1004  1.47  jdolecek 	if (!KVM_READ(cpipe, &cp, sizeof(struct pipe))) {
   1005  1.47  jdolecek 		dprintf("can't read pipe at %p", cpipe);
   1006  1.47  jdolecek 		goto bad;
   1007  1.47  jdolecek 	}
   1008  1.47  jdolecek 
   1009  1.52  jdolecek 	/* pipe descriptor is either read or write, never both */
   1010  1.72  christos 	(void)printf("* pipe %p %s %p %s%s%s", cpipe,
   1011  1.51  jdolecek 		(fp->f_flag & FWRITE) ? "->" : "<-",
   1012  1.52  jdolecek 		cp.pipe_peer,
   1013  1.54  jdolecek 		(fp->f_flag & FWRITE) ? "w" : "r",
   1014  1.54  jdolecek 		(fp->f_flag & FNONBLOCK) ? "n" : "",
   1015  1.54  jdolecek 		(cp.pipe_state & PIPE_ASYNC) ? "a" : "");
   1016  1.72  christos 	(void)printf("\n");
   1017   1.1       cgd 	return;
   1018   1.1       cgd bad:
   1019  1.72  christos 	(void)printf("* error\n");
   1020  1.56  jdolecek }
   1021  1.56  jdolecek 
   1022  1.72  christos static void
   1023  1.84  christos misctrans(struct file *file)
   1024  1.56  jdolecek {
   1025  1.56  jdolecek 
   1026  1.86  gmcgarry 	PREFIX((int)file->f_type);
   1027  1.84  christos 	pmisc(file, dtypes[file->f_type]);
   1028   1.1       cgd }
   1029   1.1       cgd 
   1030   1.1       cgd /*
   1031   1.1       cgd  * getinetproto --
   1032   1.1       cgd  *	print name of protocol number
   1033   1.1       cgd  */
   1034  1.72  christos static void
   1035  1.72  christos getinetproto(int number)
   1036   1.1       cgd {
   1037  1.72  christos 	const char *cp;
   1038   1.1       cgd 
   1039  1.26       mrg 	switch (number) {
   1040   1.1       cgd 	case IPPROTO_IP:
   1041   1.1       cgd 		cp = "ip"; break;
   1042   1.1       cgd 	case IPPROTO_ICMP:
   1043   1.1       cgd 		cp ="icmp"; break;
   1044   1.1       cgd 	case IPPROTO_GGP:
   1045   1.1       cgd 		cp ="ggp"; break;
   1046   1.1       cgd 	case IPPROTO_TCP:
   1047   1.1       cgd 		cp ="tcp"; break;
   1048   1.1       cgd 	case IPPROTO_EGP:
   1049   1.1       cgd 		cp ="egp"; break;
   1050   1.1       cgd 	case IPPROTO_PUP:
   1051   1.1       cgd 		cp ="pup"; break;
   1052   1.1       cgd 	case IPPROTO_UDP:
   1053   1.1       cgd 		cp ="udp"; break;
   1054   1.1       cgd 	case IPPROTO_IDP:
   1055   1.1       cgd 		cp ="idp"; break;
   1056   1.1       cgd 	case IPPROTO_RAW:
   1057   1.1       cgd 		cp ="raw"; break;
   1058  1.38    itojun 	case IPPROTO_ICMPV6:
   1059  1.38    itojun 		cp ="icmp6"; break;
   1060   1.1       cgd 	default:
   1061  1.72  christos 		(void)printf(" %d", number);
   1062   1.1       cgd 		return;
   1063   1.1       cgd 	}
   1064  1.72  christos 	(void)printf(" %s", cp);
   1065   1.1       cgd }
   1066   1.1       cgd 
   1067  1.72  christos static int
   1068  1.72  christos getfname(const char *filename)
   1069   1.1       cgd {
   1070   1.1       cgd 	struct stat statbuf;
   1071   1.1       cgd 	DEVS *cur;
   1072   1.1       cgd 
   1073   1.1       cgd 	if (stat(filename, &statbuf)) {
   1074  1.28      fair 		warn("stat(%s)", filename);
   1075  1.72  christos 		return 0;
   1076   1.1       cgd 	}
   1077  1.91  christos 	if ((cur = malloc(sizeof(*cur))) == NULL) {
   1078  1.91  christos 		err(1, "malloc(%zu)", sizeof(*cur));
   1079   1.1       cgd 	}
   1080   1.1       cgd 	cur->next = devs;
   1081   1.1       cgd 	devs = cur;
   1082   1.1       cgd 
   1083   1.1       cgd 	cur->ino = statbuf.st_ino;
   1084   1.1       cgd 	cur->fsid = statbuf.st_dev & 0xffff;
   1085   1.1       cgd 	cur->name = filename;
   1086  1.72  christos 	return 1;
   1087  1.40  jdolecek }
   1088  1.40  jdolecek 
   1089  1.40  jdolecek mode_t
   1090  1.72  christos getftype(enum vtype v_type)
   1091  1.40  jdolecek {
   1092  1.40  jdolecek 	mode_t ftype;
   1093  1.40  jdolecek 
   1094  1.40  jdolecek 	switch (v_type) {
   1095  1.40  jdolecek 	case VREG:
   1096  1.40  jdolecek 		ftype = S_IFREG;
   1097  1.40  jdolecek 		break;
   1098  1.40  jdolecek 	case VDIR:
   1099  1.40  jdolecek 		ftype = S_IFDIR;
   1100  1.40  jdolecek 		break;
   1101  1.40  jdolecek 	case VBLK:
   1102  1.40  jdolecek 		ftype = S_IFBLK;
   1103  1.40  jdolecek 		break;
   1104  1.40  jdolecek 	case VCHR:
   1105  1.40  jdolecek 		ftype = S_IFCHR;
   1106  1.40  jdolecek 		break;
   1107  1.40  jdolecek 	case VLNK:
   1108  1.40  jdolecek 		ftype = S_IFLNK;
   1109  1.40  jdolecek 		break;
   1110  1.40  jdolecek 	case VSOCK:
   1111  1.40  jdolecek 		ftype = S_IFSOCK;
   1112  1.40  jdolecek 		break;
   1113  1.40  jdolecek 	case VFIFO:
   1114  1.40  jdolecek 		ftype = S_IFIFO;
   1115  1.40  jdolecek 		break;
   1116  1.40  jdolecek 	default:
   1117  1.40  jdolecek 		ftype = 0;
   1118  1.40  jdolecek 		break;
   1119  1.40  jdolecek 	};
   1120  1.40  jdolecek 
   1121  1.40  jdolecek 	return ftype;
   1122   1.1       cgd }
   1123   1.1       cgd 
   1124  1.72  christos static void
   1125  1.70  christos usage(void)
   1126   1.1       cgd {
   1127  1.70  christos 	(void)fprintf(stderr, "Usage: %s [-fnv] [-p pid] [-u user] "
   1128  1.70  christos 	    "[-N system] [-M core] [file ...]\n", getprogname());
   1129  1.70  christos 	exit(1);
   1130   1.1       cgd }
   1131