Home | History | Annotate | Line # | Download | only in fstat
fstat.c revision 1.91
      1 /*	$NetBSD: fstat.c,v 1.91 2011/09/03 13:24:19 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.91 2011/09/03 13:24:19 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 	struct fdtab dt;
    317 
    318 	Uname = user_from_uid(p->p_uid, 0);
    319 	Pid = p->p_pid;
    320 	Comm = p->p_comm;
    321 
    322 	if (p->p_fd == 0 || p->p_cwdi == 0)
    323 		return;
    324 	if (!KVM_READ(p->p_fd, &filed, sizeof (filed))) {
    325 		warnx("can't read filedesc at %p for pid %d",
    326 		    (void *)(uintptr_t)p->p_fd, Pid);
    327 		return;
    328 	}
    329 	if (filed.fd_lastfile == -1)
    330 		return;
    331 	if (!KVM_READ(p->p_cwdi, &cwdi, sizeof(cwdi))) {
    332 		warnx("can't read cwdinfo at %p for pid %d",
    333 		    (void *)(uintptr_t)p->p_cwdi, Pid);
    334 		return;
    335 	}
    336 	if (!KVM_READ(filed.fd_dt, &dt, sizeof(dt))) {
    337 		warnx("can't read dtab at %p for pid %d", filed.fd_dt, Pid);
    338 		return;
    339 	}
    340 	if ((unsigned)filed.fd_lastfile >= dt.dt_nfiles ||
    341 	    filed.fd_freefile > filed.fd_lastfile + 1) {
    342 		dprintf("filedesc corrupted at %p for pid %d",
    343 		    (void *)(uintptr_t)p->p_fd, Pid);
    344 		return;
    345 	}
    346 	/*
    347 	 * root directory vnode, if one
    348 	 */
    349 	if (cwdi.cwdi_rdir)
    350 		vtrans(cwdi.cwdi_rdir, RDIR, FREAD);
    351 	/*
    352 	 * current working directory vnode
    353 	 */
    354 	vtrans(cwdi.cwdi_cdir, CDIR, FREAD);
    355 #if 0
    356 	/*
    357 	 * Disable for now, since p->p_tracep appears to point to a ktr_desc *
    358 	 * ktrace vnode, if one
    359 	 */
    360 	if (p->p_tracep)
    361 		ftrans((struct file *)(intptr_t)p->p_tracep, TRACE);
    362 #endif
    363 	/*
    364 	 * open files
    365 	 */
    366 #define FPSIZE	(sizeof (fdfile_t *))
    367 	ALLOC_OFILES(filed.fd_lastfile+1);
    368 	if (!KVM_READ(&filed.fd_dt->dt_ff, ofiles,
    369 	    (filed.fd_lastfile+1) * FPSIZE)) {
    370 		dprintf("can't read file structures at %p for pid %d",
    371 		    &filed.fd_dt->dt_ff, Pid);
    372 		return;
    373 	}
    374 	for (i = 0; i <= filed.fd_lastfile; i++) {
    375 		if (ofiles[i] == NULL)
    376 			continue;
    377 		ftrans(ofiles[i], i);
    378 	}
    379 }
    380 
    381 static void
    382 ftrans(fdfile_t *fp, int i)
    383 {
    384 	struct file file;
    385 	fdfile_t fdfile;
    386 
    387 	if (!KVM_READ(fp, &fdfile, sizeof(fdfile))) {
    388 		dprintf("can't read file %d at %p for pid %d",
    389 		    i, fp, Pid);
    390 		return;
    391 	}
    392 	if (fdfile.ff_file == NULL)
    393 		return;
    394 	if (!KVM_READ(fdfile.ff_file, &file, sizeof(file))) {
    395 		dprintf("can't read file %d at %p for pid %d",
    396 		    i, fdfile.ff_file, Pid);
    397 		return;
    398 	}
    399 	switch (file.f_type) {
    400 	case DTYPE_VNODE:
    401 		vtrans((struct vnode *)file.f_data, i, file.f_flag);
    402 		break;
    403 	case DTYPE_SOCKET:
    404 		if (checkfile == 0)
    405 			socktrans((struct socket *)file.f_data, i);
    406 		break;
    407 	case DTYPE_PIPE:
    408 		if (checkfile == 0)
    409 			ptrans(&file, (struct pipe *)file.f_data, i);
    410 		break;
    411 	case DTYPE_MISC:
    412 	case DTYPE_KQUEUE:
    413 	case DTYPE_CRYPTO:
    414 	case DTYPE_MQUEUE:
    415 	case DTYPE_SEM:
    416 		if (checkfile == 0)
    417 			misctrans(&file);
    418 		break;
    419 	default:
    420 		dprintf("unknown file type %d for file %d of pid %d",
    421 		    file.f_type, i, Pid);
    422 		break;
    423 	}
    424 }
    425 
    426 static const char *
    427 vfilestat(struct vnode *vp, struct filestat *fsp)
    428 {
    429 	const char *badtype = NULL;
    430 
    431 	if (vp->v_type == VNON || vp->v_tag == VT_NON)
    432 		badtype = "none";
    433 	else if (vp->v_type == VBAD)
    434 		badtype = "bad";
    435 	else
    436 		switch (vp->v_tag) {
    437 		case VT_UFS:
    438 		case VT_LFS:
    439 		case VT_MFS:
    440 			if (!ufs_filestat(vp, fsp))
    441 				badtype = "error";
    442 			break;
    443 		case VT_MSDOSFS:
    444 			if (!msdosfs_filestat(vp, fsp))
    445 				badtype = "error";
    446 			break;
    447 		case VT_NFS:
    448 			if (!nfs_filestat(vp, fsp))
    449 				badtype = "error";
    450 			break;
    451 		case VT_EXT2FS:
    452 			if (!ext2fs_filestat(vp, fsp))
    453 				badtype = "error";
    454 			break;
    455 		case VT_ISOFS:
    456 			if (!isofs_filestat(vp, fsp))
    457 				badtype = "error";
    458 			break;
    459 		case VT_NTFS:
    460 			if (!ntfs_filestat(vp, fsp))
    461 				badtype = "error";
    462 			break;
    463 		case VT_PTYFS:
    464 			if (!ptyfs_filestat(vp, fsp))
    465 				badtype = "error";
    466 			break;
    467 		case VT_TMPFS:
    468 			if (!tmpfs_filestat(vp, fsp))
    469 				badtype = "error";
    470 			break;
    471 		case VT_NULL:
    472 		case VT_OVERLAY:
    473 		case VT_UMAP:
    474 			badtype = layer_filestat(vp, fsp);
    475 			break;
    476 		default: {
    477 			static char unknown[10];
    478 			(void)snprintf(unknown, sizeof unknown,
    479 			    "?(%x)", vp->v_tag);
    480 			badtype = unknown;
    481 			break;
    482 		}
    483 	}
    484 	return badtype;
    485 }
    486 
    487 static void
    488 vtrans(struct vnode *vp, int i, int flag)
    489 {
    490 	struct vnode vn;
    491 	struct filestat fst;
    492 	char mode[15], rw[3];
    493 	const char *badtype, *filename;
    494 
    495 	filename = NULL;
    496 	if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
    497 		dprintf("can't read vnode at %p for pid %d", vp, Pid);
    498 		return;
    499 	}
    500 	badtype = vfilestat(&vn, &fst);
    501 	if (checkfile) {
    502 		int fsmatch = 0;
    503 		DEVS *d;
    504 
    505 		if (badtype)
    506 			return;
    507 		for (d = devs; d != NULL; d = d->next)
    508 			if (d->fsid == fst.fsid) {
    509 				fsmatch = 1;
    510 				if (d->ino == fst.fileid) {
    511 					filename = d->name;
    512 					break;
    513 				}
    514 			}
    515 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
    516 			return;
    517 	}
    518 	PREFIX(i);
    519 	if (badtype) {
    520 		(void)printf(" -         -  %10s    -\n", badtype);
    521 		return;
    522 	}
    523 	if (nflg)
    524 		(void)printf(" %2llu,%-2llu",
    525 		    (unsigned long long)major(fst.fsid),
    526 		    (unsigned long long)minor(fst.fsid));
    527 	else
    528 		(void)printf(" %-8s", getmnton(vn.v_mount));
    529 	if (nflg)
    530 		(void)snprintf(mode, sizeof mode, "%o", fst.mode);
    531 	else
    532 		strmode(fst.mode, mode);
    533 	(void)printf(" %7"PRIu64" %*s", fst.fileid, nflg ? 5 : 10, mode);
    534 	switch (vn.v_type) {
    535 	case VBLK:
    536 	case VCHR: {
    537 		char *name;
    538 
    539 		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
    540 		    S_IFCHR : S_IFBLK)) == NULL))
    541 			(void)printf("  %2llu,%-2llu",
    542 			    (unsigned long long)major(fst.rdev),
    543 			    (unsigned long long)minor(fst.rdev));
    544 		else
    545 			(void)printf(" %6s", name);
    546 		break;
    547 	}
    548 	default:
    549 		(void)printf(" %6lld", (long long)fst.size);
    550 	}
    551 	rw[0] = '\0';
    552 	if (flag & FREAD)
    553 		(void)strlcat(rw, "r", sizeof(rw));
    554 	if (flag & FWRITE)
    555 		(void)strlcat(rw, "w", sizeof(rw));
    556 	(void)printf(" %-2s", rw);
    557 	if (filename && !fsflg)
    558 		(void)printf("  %s", filename);
    559 	(void)putchar('\n');
    560 }
    561 
    562 static int
    563 ufs_filestat(struct vnode *vp, struct filestat *fsp)
    564 {
    565 	struct inode inode;
    566 	union dinode {
    567 		struct ufs1_dinode dp1;
    568 		struct ufs2_dinode dp2;
    569 	} dip;
    570 
    571 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
    572 		dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
    573 		return 0;
    574 	}
    575 
    576 	if (!KVM_READ(inode.i_din.ffs1_din, &dip, sizeof(struct ufs1_dinode))) {
    577 		dprintf("can't read dinode at %p for pid %d",
    578 		    inode.i_din.ffs1_din, Pid);
    579 		return 0;
    580 	}
    581 	if (inode.i_size == dip.dp1.di_size)
    582 		fsp->rdev = dip.dp1.di_rdev;
    583 	else {
    584 		if (!KVM_READ(inode.i_din.ffs1_din, &dip,
    585 		    sizeof(struct ufs2_dinode))) {
    586 			dprintf("can't read dinode at %p for pid %d",
    587 			    inode.i_din.ffs1_din, Pid);
    588 			return 0;
    589 		}
    590 		fsp->rdev = dip.dp2.di_rdev;
    591 	}
    592 
    593 	fsp->fsid = inode.i_dev & 0xffff;
    594 	fsp->fileid = inode.i_number;
    595 	fsp->mode = (mode_t)inode.i_mode;
    596 	fsp->size = inode.i_size;
    597 
    598 	return 1;
    599 }
    600 
    601 static int
    602 ext2fs_filestat(struct vnode *vp, struct filestat *fsp)
    603 {
    604 	struct inode inode;
    605 	u_int16_t mode;
    606 	u_int32_t size;
    607 
    608 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
    609 		dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
    610 		return 0;
    611 	}
    612 	fsp->fsid = inode.i_dev & 0xffff;
    613 	fsp->fileid = inode.i_number;
    614 
    615 	if (!KVM_READ(&inode.i_e2fs_mode, &mode, sizeof mode)) {
    616 		dprintf("can't read inode %p's mode at %p for pid %d", VTOI(vp),
    617 			&inode.i_e2fs_mode, Pid);
    618 		return 0;
    619 	}
    620 	fsp->mode = mode;
    621 
    622 	if (!KVM_READ(&inode.i_e2fs_size, &size, sizeof size)) {
    623 		dprintf("can't read inode %p's size at %p for pid %d", VTOI(vp),
    624 			&inode.i_e2fs_size, Pid);
    625 		return 0;
    626 	}
    627 	fsp->size = size;
    628 	fsp->rdev = 0;  /* XXX */
    629 	return 1;
    630 }
    631 
    632 static int
    633 nfs_filestat(struct vnode *vp, struct filestat *fsp)
    634 {
    635 	struct nfsnode nfsnode;
    636 	struct vattr va;
    637 
    638 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
    639 		dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp),
    640 		    Pid);
    641 		return 0;
    642 	}
    643 	if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
    644 		dprintf("can't read vnode attributes at %p for pid %d",
    645 		    nfsnode.n_vattr, Pid);
    646 		return 0;
    647 	}
    648 	fsp->fsid = va.va_fsid;
    649 	fsp->fileid = va.va_fileid;
    650 	fsp->size = nfsnode.n_size;
    651 	fsp->rdev = va.va_rdev;
    652 	fsp->mode = (mode_t)va.va_mode | getftype(vp->v_type);
    653 
    654 	return 1;
    655 }
    656 
    657 static int
    658 msdosfs_filestat(struct vnode *vp, struct filestat *fsp)
    659 {
    660 	struct denode de;
    661 	struct msdosfsmount mp;
    662 
    663 	if (!KVM_READ(VTONFS(vp), &de, sizeof(de))) {
    664 		dprintf("can't read denode at %p for pid %d", VTONFS(vp),
    665 		    Pid);
    666 		return 0;
    667 	}
    668 	if (!KVM_READ(de.de_pmp, &mp, sizeof(mp))) {
    669 		dprintf("can't read mount struct at %p for pid %d", de.de_pmp,
    670 		    Pid);
    671 		return 0;
    672 	}
    673 
    674 	fsp->fsid = de.de_dev & 0xffff;
    675 	fsp->fileid = 0; /* XXX see msdosfs_vptofh() for more info */
    676 	fsp->size = de.de_FileSize;
    677 	fsp->rdev = 0;	/* msdosfs doesn't support device files */
    678 	fsp->mode = (0777 & mp.pm_mask) | getftype(vp->v_type);
    679 	return 1;
    680 }
    681 
    682 static const char *
    683 layer_filestat(struct vnode *vp, struct filestat *fsp)
    684 {
    685 	struct layer_node layer_node;
    686 	struct mount mount;
    687 	struct vnode vn;
    688 	const char *badtype;
    689 
    690 	if (!KVM_READ(VTOLAYER(vp), &layer_node, sizeof(layer_node))) {
    691 		dprintf("can't read layer_node at %p for pid %d",
    692 		    VTOLAYER(vp), Pid);
    693 		return "error";
    694 	}
    695 	if (!KVM_READ(vp->v_mount, &mount, sizeof(struct mount))) {
    696 		dprintf("can't read mount struct at %p for pid %d",
    697 		    vp->v_mount, Pid);
    698 		return "error";
    699 	}
    700 	vp = layer_node.layer_lowervp;
    701 	if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
    702 		dprintf("can't read vnode at %p for pid %d", vp, Pid);
    703 		return "error";
    704 	}
    705 	if ((badtype = vfilestat(&vn, fsp)) == NULL)
    706 		fsp->fsid = mount.mnt_stat.f_fsidx.__fsid_val[0];
    707 	return badtype;
    708 }
    709 
    710 static char *
    711 getmnton(struct mount *m)
    712 {
    713 	static struct mount mount;
    714 	static struct mtab {
    715 		struct mtab *next;
    716 		struct mount *m;
    717 		char mntonname[MNAMELEN];
    718 	} *mhead = NULL;
    719 	struct mtab *mt;
    720 
    721 	for (mt = mhead; mt != NULL; mt = mt->next)
    722 		if (m == mt->m)
    723 			return mt->mntonname;
    724 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
    725 		warnx("can't read mount table at %p", m);
    726 		return NULL;
    727 	}
    728 	if ((mt = malloc(sizeof (struct mtab))) == NULL) {
    729 		err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab));
    730 	}
    731 	mt->m = m;
    732 	(void)memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0],
    733 	    MNAMELEN);
    734 	mt->next = mhead;
    735 	mhead = mt;
    736 	return mt->mntonname;
    737 }
    738 
    739 #ifdef INET6
    740 static const char *
    741 inet6_addrstr(struct in6_addr *p)
    742 {
    743 	struct sockaddr_in6 sin6;
    744 	static char hbuf[NI_MAXHOST];
    745 	const int niflags = NI_NUMERICHOST;
    746 
    747 	(void)memset(&sin6, 0, sizeof(sin6));
    748 	sin6.sin6_family = AF_INET6;
    749 	sin6.sin6_len = sizeof(struct sockaddr_in6);
    750 	sin6.sin6_addr = *p;
    751 	if (IN6_IS_ADDR_LINKLOCAL(p) &&
    752 	    *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
    753 		sin6.sin6_scope_id =
    754 			ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
    755 		sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0;
    756 	}
    757 
    758 	if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
    759 			hbuf, sizeof(hbuf), NULL, 0, niflags))
    760 		return "invalid";
    761 
    762 	return hbuf;
    763 }
    764 #endif
    765 
    766 static void
    767 socktrans(struct socket *sock, int i)
    768 {
    769 	static const char *stypename[] = {
    770 		"unused",	/* 0 */
    771 		"stream", 	/* 1 */
    772 		"dgram",	/* 2 */
    773 		"raw",		/* 3 */
    774 		"rdm",		/* 4 */
    775 		"seqpak"	/* 5 */
    776 	};
    777 #define	STYPEMAX 5
    778 	struct socket	so;
    779 	struct protosw	proto;
    780 	struct domain	dom;
    781 	struct inpcb	inpcb;
    782 #ifdef INET6
    783 	struct in6pcb	in6pcb;
    784 #endif
    785 	struct unpcb	unpcb;
    786 	int len;
    787 	char dname[32];
    788 #ifdef INET6
    789 	char xaddrbuf[NI_MAXHOST + 2];
    790 #endif
    791 
    792 	PREFIX(i);
    793 
    794 	/* fill in socket */
    795 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
    796 		dprintf("can't read sock at %p", sock);
    797 		goto bad;
    798 	}
    799 
    800 	/* fill in protosw entry */
    801 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
    802 		dprintf("can't read protosw at %p", so.so_proto);
    803 		goto bad;
    804 	}
    805 
    806 	/* fill in domain */
    807 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
    808 		dprintf("can't read domain at %p", proto.pr_domain);
    809 		goto bad;
    810 	}
    811 
    812 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
    813 	    sizeof(dname) - 1)) != sizeof(dname) -1) {
    814 		dprintf("can't read domain name at %p", dom.dom_name);
    815 		dname[0] = '\0';
    816 	}
    817 	else
    818 		dname[len] = '\0';
    819 
    820 	if ((u_short)so.so_type > STYPEMAX)
    821 		(void)printf("* %s ?%d", dname, so.so_type);
    822 	else
    823 		(void)printf("* %s %s", dname, stypename[so.so_type]);
    824 
    825 	/*
    826 	 * protocol specific formatting
    827 	 *
    828 	 * Try to find interesting things to print.  For TCP, the interesting
    829 	 * thing is the address of the tcpcb, for UDP and others, just the
    830 	 * inpcb (socket pcb).  For UNIX domain, its the address of the socket
    831 	 * pcb and the address of the connected pcb (if connected).  Otherwise
    832 	 * just print the protocol number and address of the socket itself.
    833 	 * The idea is not to duplicate netstat, but to make available enough
    834 	 * information for further analysis.
    835 	 */
    836 	switch(dom.dom_family) {
    837 	case AF_INET:
    838 		getinetproto(proto.pr_protocol);
    839 		if (proto.pr_protocol == IPPROTO_TCP) {
    840 			if (so.so_pcb == NULL)
    841 				break;
    842 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
    843 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
    844 				dprintf("can't read inpcb at %p", so.so_pcb);
    845 				goto bad;
    846 			}
    847 			(void)printf(" %lx", (long)inpcb.inp_ppcb);
    848 			(void)printf(" %s:%d",
    849 			    inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
    850 			    inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
    851 			if (inpcb.inp_fport) {
    852 				(void)printf(" <-> %s:%d",
    853 				    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
    854 				    inet_ntoa(inpcb.inp_faddr),
    855 				    ntohs(inpcb.inp_fport));
    856 			}
    857 		} else if (proto.pr_protocol == IPPROTO_UDP) {
    858 			if (so.so_pcb == NULL)
    859 				break;
    860 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
    861 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
    862 				dprintf("can't read inpcb at %p", so.so_pcb);
    863 				goto bad;
    864 			}
    865 			(void)printf(" %lx", (long)so.so_pcb);
    866 			(void)printf(" %s:%d",
    867 			    inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
    868 			    inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
    869 			if (inpcb.inp_fport)
    870 				(void)printf(" <-> %s:%d",
    871 				    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
    872 				    inet_ntoa(inpcb.inp_faddr),
    873 				    ntohs(inpcb.inp_fport));
    874 		} else if (so.so_pcb)
    875 			(void)printf(" %lx", (long)so.so_pcb);
    876 		break;
    877 #ifdef INET6
    878 	case AF_INET6:
    879 		getinetproto(proto.pr_protocol);
    880 		if (proto.pr_protocol == IPPROTO_TCP) {
    881 			if (so.so_pcb == NULL)
    882 				break;
    883 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb,
    884 			    sizeof(struct in6pcb)) != sizeof(struct in6pcb)) {
    885 				dprintf("can't read in6pcb at %p", so.so_pcb);
    886 				goto bad;
    887 			}
    888 			(void)printf(" %lx", (long)in6pcb.in6p_ppcb);
    889 			(void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
    890 			    inet6_addrstr(&in6pcb.in6p_laddr));
    891 			(void)printf(" %s:%d",
    892 			    IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" :
    893 			    xaddrbuf,
    894 			    ntohs(in6pcb.in6p_lport));
    895 			if (in6pcb.in6p_fport) {
    896 				(void)snprintf(xaddrbuf, sizeof(xaddrbuf),
    897 				    "[%s]", inet6_addrstr(&in6pcb.in6p_faddr));
    898 				(void)printf(" <-> %s:%d",
    899 			            IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" :
    900 				    xaddrbuf,
    901 				    ntohs(in6pcb.in6p_fport));
    902 			}
    903 		} else if (proto.pr_protocol == IPPROTO_UDP) {
    904 			if (so.so_pcb == NULL)
    905 				break;
    906 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb,
    907 			    sizeof(struct in6pcb)) != sizeof(struct in6pcb)) {
    908 				dprintf("can't read inpcb at %p", so.so_pcb);
    909 				goto bad;
    910 			}
    911 			(void)printf(" %lx", (long)so.so_pcb);
    912 			(void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
    913 			    inet6_addrstr(&in6pcb.in6p_laddr));
    914 			(void)printf(" %s:%d",
    915 		            IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" :
    916 			    xaddrbuf,
    917 			    ntohs(in6pcb.in6p_lport));
    918 			if (in6pcb.in6p_fport) {
    919 				(void)snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
    920 				    inet6_addrstr(&in6pcb.in6p_faddr));
    921 				(void)printf(" <-> %s:%d",
    922 			            IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" :
    923 				    xaddrbuf,
    924 				    ntohs(in6pcb.in6p_fport));
    925 			}
    926 		} else if (so.so_pcb)
    927 			(void)printf(" %lx", (long)so.so_pcb);
    928 		break;
    929 #endif
    930 	case AF_LOCAL:
    931 		/* print address of pcb and connected pcb */
    932 		if (so.so_pcb) {
    933 			char shoconn[4], *cp;
    934 
    935 			(void)printf(" %lx", (long)so.so_pcb);
    936 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
    937 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
    938 				dprintf("can't read unpcb at %p", so.so_pcb);
    939 				goto bad;
    940 			}
    941 
    942 			cp = shoconn;
    943 			if (!(so.so_state & SS_CANTRCVMORE))
    944 				*cp++ = '<';
    945 			*cp++ = '-';
    946 			if (!(so.so_state & SS_CANTSENDMORE))
    947 				*cp++ = '>';
    948 			*cp = '\0';
    949 			if (unpcb.unp_addr) {
    950 				struct sockaddr_un *sun =
    951 					malloc(unpcb.unp_addrlen);
    952 				if (sun == NULL)
    953 				    err(1, "malloc(%zu)",
    954 					unpcb.unp_addrlen);
    955 				if (kvm_read(kd, (u_long)unpcb.unp_addr,
    956 				    sun, unpcb.unp_addrlen) !=
    957 				    (ssize_t)unpcb.unp_addrlen) {
    958 					dprintf("can't read sun at %p",
    959 					    unpcb.unp_addr);
    960 					free(sun);
    961 				} else {
    962 					(void)printf(" %s %s",
    963 					    shoconn, sun->sun_path);
    964 					free(sun);
    965 					break;
    966 				}
    967 			}
    968 			if (unpcb.unp_conn) {
    969 				(void)printf(" %s %lx", shoconn,
    970 				    (long)unpcb.unp_conn);
    971 			}
    972 		}
    973 		break;
    974 	default:
    975 		/* print protocol number and socket address */
    976 		(void)printf(" %d %lx", proto.pr_protocol, (long)sock);
    977 	}
    978 	(void)printf("\n");
    979 	return;
    980 bad:
    981 	(void)printf("* error\n");
    982 }
    983 
    984 static void
    985 ptrans(struct file *fp, struct pipe *cpipe, int i)
    986 {
    987 	struct pipe cp;
    988 
    989 	PREFIX(i);
    990 
    991 	/* fill in pipe */
    992 	if (!KVM_READ(cpipe, &cp, sizeof(struct pipe))) {
    993 		dprintf("can't read pipe at %p", cpipe);
    994 		goto bad;
    995 	}
    996 
    997 	/* pipe descriptor is either read or write, never both */
    998 	(void)printf("* pipe %p %s %p %s%s%s", cpipe,
    999 		(fp->f_flag & FWRITE) ? "->" : "<-",
   1000 		cp.pipe_peer,
   1001 		(fp->f_flag & FWRITE) ? "w" : "r",
   1002 		(fp->f_flag & FNONBLOCK) ? "n" : "",
   1003 		(cp.pipe_state & PIPE_ASYNC) ? "a" : "");
   1004 	(void)printf("\n");
   1005 	return;
   1006 bad:
   1007 	(void)printf("* error\n");
   1008 }
   1009 
   1010 static void
   1011 misctrans(struct file *file)
   1012 {
   1013 
   1014 	PREFIX((int)file->f_type);
   1015 	pmisc(file, dtypes[file->f_type]);
   1016 }
   1017 
   1018 /*
   1019  * getinetproto --
   1020  *	print name of protocol number
   1021  */
   1022 static void
   1023 getinetproto(int number)
   1024 {
   1025 	const char *cp;
   1026 
   1027 	switch (number) {
   1028 	case IPPROTO_IP:
   1029 		cp = "ip"; break;
   1030 	case IPPROTO_ICMP:
   1031 		cp ="icmp"; break;
   1032 	case IPPROTO_GGP:
   1033 		cp ="ggp"; break;
   1034 	case IPPROTO_TCP:
   1035 		cp ="tcp"; break;
   1036 	case IPPROTO_EGP:
   1037 		cp ="egp"; break;
   1038 	case IPPROTO_PUP:
   1039 		cp ="pup"; break;
   1040 	case IPPROTO_UDP:
   1041 		cp ="udp"; break;
   1042 	case IPPROTO_IDP:
   1043 		cp ="idp"; break;
   1044 	case IPPROTO_RAW:
   1045 		cp ="raw"; break;
   1046 	case IPPROTO_ICMPV6:
   1047 		cp ="icmp6"; break;
   1048 	default:
   1049 		(void)printf(" %d", number);
   1050 		return;
   1051 	}
   1052 	(void)printf(" %s", cp);
   1053 }
   1054 
   1055 static int
   1056 getfname(const char *filename)
   1057 {
   1058 	struct stat statbuf;
   1059 	DEVS *cur;
   1060 
   1061 	if (stat(filename, &statbuf)) {
   1062 		warn("stat(%s)", filename);
   1063 		return 0;
   1064 	}
   1065 	if ((cur = malloc(sizeof(*cur))) == NULL) {
   1066 		err(1, "malloc(%zu)", sizeof(*cur));
   1067 	}
   1068 	cur->next = devs;
   1069 	devs = cur;
   1070 
   1071 	cur->ino = statbuf.st_ino;
   1072 	cur->fsid = statbuf.st_dev & 0xffff;
   1073 	cur->name = filename;
   1074 	return 1;
   1075 }
   1076 
   1077 mode_t
   1078 getftype(enum vtype v_type)
   1079 {
   1080 	mode_t ftype;
   1081 
   1082 	switch (v_type) {
   1083 	case VREG:
   1084 		ftype = S_IFREG;
   1085 		break;
   1086 	case VDIR:
   1087 		ftype = S_IFDIR;
   1088 		break;
   1089 	case VBLK:
   1090 		ftype = S_IFBLK;
   1091 		break;
   1092 	case VCHR:
   1093 		ftype = S_IFCHR;
   1094 		break;
   1095 	case VLNK:
   1096 		ftype = S_IFLNK;
   1097 		break;
   1098 	case VSOCK:
   1099 		ftype = S_IFSOCK;
   1100 		break;
   1101 	case VFIFO:
   1102 		ftype = S_IFIFO;
   1103 		break;
   1104 	default:
   1105 		ftype = 0;
   1106 		break;
   1107 	};
   1108 
   1109 	return ftype;
   1110 }
   1111 
   1112 static void
   1113 usage(void)
   1114 {
   1115 	(void)fprintf(stderr, "Usage: %s [-fnv] [-p pid] [-u user] "
   1116 	    "[-N system] [-M core] [file ...]\n", getprogname());
   1117 	exit(1);
   1118 }
   1119