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