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