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