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