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