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