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