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