xref: /netbsd-src/usr.bin/fstat/fstat.c (revision f89f6560d453f5e37386cc7938c072d2f528b9fa)
1 /*	$NetBSD: fstat.c,v 1.109 2015/03/08 06:46:51 mlelstv 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.109 2015/03/08 06:46:51 mlelstv 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/mount.h>
62 #undef _KERNEL
63 #define	_KERNEL
64 #include <sys/file.h>
65 #include <ufs/ufs/inode.h>
66 #include <ufs/ufs/ufsmount.h>
67 #undef _KERNEL
68 #define NFS
69 #include <nfs/nfsproto.h>
70 #include <nfs/rpcv2.h>
71 #include <nfs/nfs.h>
72 #include <nfs/nfsnode.h>
73 #undef NFS
74 #include <msdosfs/denode.h>
75 #include <msdosfs/bpb.h>
76 #define	_KERNEL
77 #include <msdosfs/msdosfsmount.h>
78 #undef _KERNEL
79 #define	_KERNEL
80 #include <miscfs/genfs/layer.h>
81 #undef _KERNEL
82 
83 #include <net/route.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
87 #include <netinet/in_pcb.h>
88 
89 #ifdef INET6
90 #include <netinet/ip6.h>
91 #include <netinet6/in6.h>
92 #include <netinet6/ip6_var.h>
93 #include <netinet6/in6_pcb.h>
94 #endif
95 
96 #include <netatalk/at.h>
97 #include <netatalk/ddp_var.h>
98 
99 #include <netdb.h>
100 #include <arpa/inet.h>
101 
102 #include <ctype.h>
103 #include <errno.h>
104 #include <kvm.h>
105 #include <limits.h>
106 #include <nlist.h>
107 #include <paths.h>
108 #include <pwd.h>
109 #include <stdio.h>
110 #include <stdlib.h>
111 #include <string.h>
112 #include <unistd.h>
113 #include <err.h>
114 #include <util.h>
115 
116 #include "fstat.h"
117 
118 #define	TEXT	-1
119 #define	CDIR	-2
120 #define	RDIR	-3
121 #define	TRACE	-4
122 
123 typedef struct devs {
124 	struct	devs *next;
125 	long	fsid;
126 	ino_t	ino;
127 	const char *name;
128 } DEVS;
129 static DEVS *devs;
130 
131 static int 	fsflg,	/* show files on same filesystem as file(s) argument */
132 	pflg,	/* show files open by a particular pid */
133 	uflg;	/* show files open by a particular (effective) user */
134 static int 	checkfile; /* true if restricting to particular files or filesystems */
135 static int	nflg;	/* (numerical) display f.s. and rdev as dev_t */
136 static int	Aflg;	/* prefix with address of file structure */
137 int	vflg;	/* display errors in locating kernel data objects etc... */
138 
139 static fdfile_t **ofiles; /* buffer of pointers to file structures */
140 static int fstat_maxfiles;
141 #define ALLOC_OFILES(d)	\
142 	if ((d) > fstat_maxfiles) { \
143 		size_t len = (d) * sizeof(fdfile_t *); \
144 		free(ofiles); \
145 		ofiles = malloc(len); \
146 		if (ofiles == NULL) { \
147 			err(1, "malloc(%zu)", len);	\
148 		} \
149 		fstat_maxfiles = (d); \
150 	}
151 
152 kvm_t *kd;
153 
154 static const char *const dtypes[] = {
155 	DTYPE_NAMES
156 };
157 
158 static void	dofiles(struct kinfo_proc2 *);
159 static int	ext2fs_filestat(struct vnode *, struct filestat *);
160 static int	getfname(const char *);
161 static void	getinetproto(char *, size_t, int);
162 static void	getatproto(char *, size_t, int);
163 static char   *getmnton(struct mount *);
164 static const char   *layer_filestat(struct vnode *, struct filestat *);
165 static int	msdosfs_filestat(struct vnode *, struct filestat *);
166 static int	nfs_filestat(struct vnode *, struct filestat *);
167 static const char *inet_addrstr(char *, size_t, const struct in_addr *,
168     uint16_t, bool);
169 #ifdef INET6
170 static const char *inet6_addrstr(char *, size_t, const struct in6_addr *,
171     uint16_t, bool);
172 #endif
173 static const char *at_addrstr(char *, size_t, const struct sockaddr_at *);
174 static void	socktrans(struct socket *, int);
175 static void	misctrans(struct file *, int);
176 static int	ufs_filestat(struct vnode *, struct filestat *);
177 static void	usage(void) __dead;
178 static const char   *vfilestat(struct vnode *, struct filestat *);
179 static void	vtrans(struct vnode *, int, int, long);
180 static void	ftrans(fdfile_t *, int);
181 static void	ptrans(struct file *, struct pipe *, int);
182 static void	kdriver_init(void);
183 
184 int
185 main(int argc, char **argv)
186 {
187 	struct passwd *passwd;
188 	struct kinfo_proc2 *p, *plast;
189 	int arg, ch, what;
190 	char *memf, *nlistf;
191 	char buf[_POSIX2_LINE_MAX];
192 	int cnt;
193 	gid_t egid = getegid();
194 
195 	(void)setegid(getgid());
196 	arg = 0;
197 	what = KERN_PROC_ALL;
198 	nlistf = memf = NULL;
199 	while ((ch = getopt(argc, argv, "fnAp:u:vN:M:")) != -1)
200 		switch((char)ch) {
201 		case 'f':
202 			fsflg = 1;
203 			break;
204 		case 'M':
205 			memf = optarg;
206 			break;
207 		case 'N':
208 			nlistf = optarg;
209 			break;
210 		case 'n':
211 			nflg = 1;
212 			break;
213 		case 'A':
214 			Aflg = 1;
215 			break;
216 		case 'p':
217 			if (pflg++)
218 				usage();
219 			if (!isdigit((unsigned char)*optarg)) {
220 				warnx("-p requires a process id");
221 				usage();
222 			}
223 			what = KERN_PROC_PID;
224 			arg = atoi(optarg);
225 			break;
226 		case 'u':
227 			if (uflg++)
228 				usage();
229 			if (!(passwd = getpwnam(optarg))) {
230 				errx(1, "%s: unknown uid", optarg);
231 			}
232 			what = KERN_PROC_UID;
233 			arg = passwd->pw_uid;
234 			break;
235 		case 'v':
236 			vflg = 1;
237 			break;
238 		case '?':
239 		default:
240 			usage();
241 		}
242 
243 	kdriver_init();
244 
245 	if (*(argv += optind)) {
246 		for (; *argv; ++argv) {
247 			if (getfname(*argv))
248 				checkfile = 1;
249 		}
250 		if (!checkfile)	/* file(s) specified, but none accessible */
251 			exit(1);
252 	}
253 
254 	ALLOC_OFILES(256);	/* reserve space for file pointers */
255 
256 	if (fsflg && !checkfile) {
257 		/* -f with no files means use wd */
258 		if (getfname(".") == 0)
259 			exit(1);
260 		checkfile = 1;
261 	}
262 
263 	/*
264 	 * Discard setgid privileges.  If not the running kernel, we toss
265 	 * them away totally so that bad guys can't print interesting stuff
266 	 * from kernel memory, otherwise switch back to kmem for the
267 	 * duration of the kvm_openfiles() call.
268 	 */
269 	if (nlistf != NULL || memf != NULL)
270 		(void)setgid(getgid());
271 	else
272 		(void)setegid(egid);
273 
274 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
275 		errx(1, "%s", buf);
276 
277 	/* get rid of it now anyway */
278 	if (nlistf == NULL && memf == NULL)
279 		(void)setgid(getgid());
280 
281 	if ((p = kvm_getproc2(kd, what, arg, sizeof *p, &cnt)) == NULL) {
282 		errx(1, "%s", kvm_geterr(kd));
283 	}
284 	if (Aflg)
285 		(void)printf("%-*s ", 2*(int)(sizeof(void*)), "ADDR");
286 	if (nflg)
287 		(void)printf("%s",
288 "USER     CMD          PID   FD  DEV     INUM  MODE  SZ|DV R/W");
289 	else
290 		(void)printf("%s",
291 "USER     CMD          PID   FD MOUNT       INUM MODE         SZ|DV R/W");
292 	if (checkfile && fsflg == 0)
293 		(void)printf(" NAME\n");
294 	else
295 		(void)putchar('\n');
296 
297 	for (plast = &p[cnt]; p < plast; ++p) {
298 		if (p->p_stat == SZOMB)
299 			continue;
300 		dofiles(p);
301 	}
302 	return 0;
303 }
304 
305 static const	char *Uname, *Comm;
306 pid_t	Pid;
307 
308 #define PREFIX(i) (void)printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
309 	switch(i) { \
310 	case TEXT: \
311 		(void)printf(" text"); \
312 		break; \
313 	case CDIR: \
314 		(void)printf("   wd"); \
315 		break; \
316 	case RDIR: \
317 		(void)printf(" root"); \
318 		break; \
319 	case TRACE: \
320 		(void)printf("   tr"); \
321 		break; \
322 	default: \
323 		(void)printf(" %4d", i); \
324 		break; \
325 	}
326 
327 static struct kinfo_drivers *kdriver;
328 static size_t kdriverlen;
329 
330 static int
331 kdriver_comp(const void *a, const void *b)
332 {
333 	const struct kinfo_drivers *ka = a;
334 	const struct kinfo_drivers *kb = b;
335 	int kac = ka->d_cmajor == -1 ? 0 : ka->d_cmajor;
336 	int kbc = kb->d_cmajor == -1 ? 0 : kb->d_cmajor;
337 	int kab = ka->d_bmajor == -1 ? 0 : ka->d_bmajor;
338 	int kbb = kb->d_bmajor == -1 ? 0 : kb->d_bmajor;
339 	int c = kac - kbc;
340 	if (c == 0)
341 		return kab - kbb;
342 	else
343 		return c;
344 }
345 
346 static const char *
347 kdriver_search(int type, dev_t num)
348 {
349 	struct kinfo_drivers k, *kp;
350 	static char buf[64];
351 
352 	if (nflg)
353 		goto out;
354 
355 	if (type == VBLK) {
356 		k.d_bmajor = num;
357 		k.d_cmajor = -1;
358 	} else {
359 		k.d_bmajor = -1;
360 		k.d_cmajor = num;
361 	}
362 	kp = bsearch(&k, kdriver, kdriverlen, sizeof(*kdriver), kdriver_comp);
363 	if (kp)
364 		return kp->d_name;
365 out:
366 	snprintf(buf, sizeof(buf), "%llu", (unsigned long long)num);
367 	return buf;
368 }
369 
370 
371 static void
372 kdriver_init(void)
373 {
374 	size_t sz;
375 	int error;
376 	static const int name[2] = { CTL_KERN, KERN_DRIVERS };
377 
378 	error = sysctl(name, __arraycount(name), NULL, &sz, NULL, 0);
379 	if (error == -1) {
380 		warn("sysctl kern.drivers");
381 		return;
382 	}
383 
384 	if (sz % sizeof(*kdriver)) {
385 		warnx("bad size %zu for kern.drivers", sz);
386 		return;
387 	}
388 
389 	kdriver = malloc(sz);
390 	if (kdriver == NULL) {
391 		warn("malloc");
392 		return;
393 	}
394 
395 	error = sysctl(name, __arraycount(name), kdriver, &sz, NULL, 0);
396 	if (error == -1) {
397 		warn("sysctl kern.drivers");
398 		return;
399 	}
400 
401 	kdriverlen = sz / sizeof(*kdriver);
402 	qsort(kdriver, kdriverlen, sizeof(*kdriver), kdriver_comp);
403 #ifdef DEBUG
404 	for (size_t i = 0; i < kdriverlen; i++)
405 		printf("%d %d %s\n", kdriver[i].d_cmajor, kdriver[i].d_bmajor,
406 		    kdriver[i].d_name);
407 #endif
408 }
409 
410 /*
411  * print open files attributed to this process
412  */
413 static void
414 dofiles(struct kinfo_proc2 *p)
415 {
416 	int i;
417 	struct filedesc filed;
418 	struct cwdinfo cwdi;
419 	struct fdtab dt;
420 
421 	Uname = user_from_uid(p->p_uid, 0);
422 	Pid = p->p_pid;
423 	Comm = p->p_comm;
424 
425 	if (p->p_fd == 0 || p->p_cwdi == 0)
426 		return;
427 	if (!KVM_READ(p->p_fd, &filed, sizeof (filed))) {
428 		warnx("can't read filedesc at %p for pid %d",
429 		    (void *)(uintptr_t)p->p_fd, Pid);
430 		return;
431 	}
432 	if (filed.fd_lastfile == -1)
433 		return;
434 	if (!KVM_READ(p->p_cwdi, &cwdi, sizeof(cwdi))) {
435 		warnx("can't read cwdinfo at %p for pid %d",
436 		    (void *)(uintptr_t)p->p_cwdi, Pid);
437 		return;
438 	}
439 	if (!KVM_READ(filed.fd_dt, &dt, sizeof(dt))) {
440 		warnx("can't read dtab at %p for pid %d", filed.fd_dt, Pid);
441 		return;
442 	}
443 	if ((unsigned)filed.fd_lastfile >= dt.dt_nfiles ||
444 	    filed.fd_freefile > filed.fd_lastfile + 1) {
445 		dprintf("filedesc corrupted at %p for pid %d",
446 		    (void *)(uintptr_t)p->p_fd, Pid);
447 		return;
448 	}
449 	/*
450 	 * root directory vnode, if one
451 	 */
452 	if (cwdi.cwdi_rdir)
453 		vtrans(cwdi.cwdi_rdir, RDIR, FREAD, (long)cwdi.cwdi_rdir);
454 	/*
455 	 * current working directory vnode
456 	 */
457 	vtrans(cwdi.cwdi_cdir, CDIR, FREAD, (long)cwdi.cwdi_cdir);
458 #if 0
459 	/*
460 	 * Disable for now, since p->p_tracep appears to point to a ktr_desc *
461 	 * ktrace vnode, if one
462 	 */
463 	if (p->p_tracep)
464 		ftrans(p->p_tracep, TRACE);
465 #endif
466 	/*
467 	 * open files
468 	 */
469 #define FPSIZE	(sizeof (fdfile_t *))
470 	ALLOC_OFILES(filed.fd_lastfile+1);
471 	if (!KVM_READ(&filed.fd_dt->dt_ff, ofiles,
472 	    (filed.fd_lastfile+1) * FPSIZE)) {
473 		dprintf("can't read file structures at %p for pid %d",
474 		    &filed.fd_dt->dt_ff, Pid);
475 		return;
476 	}
477 	for (i = 0; i <= filed.fd_lastfile; i++) {
478 		if (ofiles[i] == NULL)
479 			continue;
480 		ftrans(ofiles[i], i);
481 	}
482 }
483 
484 static void
485 ftrans(fdfile_t *fp, int i)
486 {
487 	struct file file;
488 	fdfile_t fdfile;
489 
490 	if (!KVM_READ(fp, &fdfile, sizeof(fdfile))) {
491 		dprintf("can't read file %d at %p for pid %d",
492 		    i, fp, Pid);
493 		return;
494 	}
495 	if (fdfile.ff_file == NULL) {
496 		dprintf("null ff_file for %d at %p for pid %d",
497 		    i, fp, Pid);
498 		return;
499 	}
500 	if (!KVM_READ(fdfile.ff_file, &file, sizeof(file))) {
501 		dprintf("can't read file %d at %p for pid %d",
502 		    i, fdfile.ff_file, Pid);
503 		return;
504 	}
505 	if (Aflg && file.f_type != DTYPE_VNODE && checkfile == 0)
506 		(void)printf("%*lx ",
507 			2*(int)(sizeof(void*)), (long)fdfile.ff_file);
508 	switch (file.f_type) {
509 	case DTYPE_VNODE:
510 		vtrans(file.f_data, i, file.f_flag, (long)fdfile.ff_file);
511 		break;
512 	case DTYPE_SOCKET:
513 		socktrans(file.f_data, i);
514 		break;
515 	case DTYPE_PIPE:
516 		if (checkfile == 0)
517 			ptrans(&file, file.f_data, i);
518 		break;
519 	case DTYPE_MISC:
520 	case DTYPE_KQUEUE:
521 	case DTYPE_CRYPTO:
522 	case DTYPE_MQUEUE:
523 	case DTYPE_SEM:
524 		if (checkfile == 0)
525 			misctrans(&file, i);
526 		break;
527 	default:
528 		dprintf("unknown file type %d for file %d of pid %d",
529 		    file.f_type, i, Pid);
530 		break;
531 	}
532 }
533 
534 static const char dead[] = "dead";
535 
536 static const char *
537 vfilestat(struct vnode *vp, struct filestat *fsp)
538 {
539 	const char *badtype = NULL;
540 
541 	if (vp->v_type == VNON)
542 		badtype = "none";
543 	else if (vp->v_type == VBAD)
544 		badtype = "bad";
545 	else
546 		switch (vp->v_tag) {
547 		case VT_NON:
548 			badtype = dead;
549 			break;
550 		case VT_UFS:
551 		case VT_LFS:
552 		case VT_MFS:
553 			if (!ufs_filestat(vp, fsp))
554 				badtype = "error";
555 			break;
556 		case VT_MSDOSFS:
557 			if (!msdosfs_filestat(vp, fsp))
558 				badtype = "error";
559 			break;
560 		case VT_NFS:
561 			if (!nfs_filestat(vp, fsp))
562 				badtype = "error";
563 			break;
564 		case VT_EXT2FS:
565 			if (!ext2fs_filestat(vp, fsp))
566 				badtype = "error";
567 			break;
568 		case VT_ISOFS:
569 			if (!isofs_filestat(vp, fsp))
570 				badtype = "error";
571 			break;
572 		case VT_NTFS:
573 			if (!ntfs_filestat(vp, fsp))
574 				badtype = "error";
575 			break;
576 		case VT_PTYFS:
577 			if (!ptyfs_filestat(vp, fsp))
578 				badtype = "error";
579 			break;
580 		case VT_TMPFS:
581 			if (!tmpfs_filestat(vp, fsp))
582 				badtype = "error";
583 			break;
584 		case VT_NULL:
585 		case VT_OVERLAY:
586 		case VT_UMAP:
587 			badtype = layer_filestat(vp, fsp);
588 			break;
589 		default: {
590 			static char unknown[10];
591 			(void)snprintf(unknown, sizeof unknown,
592 			    "?(%x)", vp->v_tag);
593 			badtype = unknown;
594 			break;
595 		}
596 	}
597 	return badtype;
598 }
599 
600 static int
601 checkfs(struct vnode *vp, struct vnode *vn, struct filestat *fst,
602     const char **type, const char **fname)
603 {
604 	*fname = NULL;
605 	if (!KVM_READ(vp, vn, sizeof(*vn))) {
606 		dprintf("can't read vnode at %p for pid %d", vp, Pid);
607 		return 0;
608 	}
609 	*type = vfilestat(vn, fst);
610 	if (checkfile) {
611 		int fsmatch = 0;
612 		DEVS *d;
613 #if 0
614 		if (*type && *type != dead)
615 			return 0;
616 #endif
617 		for (d = devs; d != NULL; d = d->next) {
618 			if (d->fsid == fst->fsid) {
619 				fsmatch = 1;
620 				if (d->ino == fst->fileid) {
621 					*fname = d->name;
622 					break;
623 				}
624 			}
625 		}
626 		if (fsmatch == 0 || (*fname == NULL && fsflg == 0))
627 			return 0;
628 	}
629 	return 1;
630 }
631 
632 static void
633 vtrans(struct vnode *vp, int i, int flag, long addr)
634 {
635 	struct vnode vn;
636 	char mode[15], rw[3];
637 	const char *badtype, *filename;
638 	struct filestat fst;
639 
640 	if (!checkfs(vp, &vn, &fst, &badtype, &filename))
641 		return;
642 
643 	if (Aflg)
644 		(void)printf("%*lx ", 2*(int)(sizeof(void*)), addr);
645 	PREFIX(i);
646 	if (badtype == dead) {
647 		char buf[1024];
648 		(void)snprintb(buf, sizeof(buf), VNODE_FLAGBITS,
649 		    vn.v_iflag | vn.v_vflag | vn.v_uflag);
650 		(void)printf(" flags %s\n", buf);
651 		return;
652 	} else if (badtype) {
653 		(void)printf(" -         -  %10s    -\n", badtype);
654 		return;
655 	}
656 	if (nflg)
657 		(void)printf(" %2llu,%-2llu",
658 		    (unsigned long long)major(fst.fsid),
659 		    (unsigned long long)minor(fst.fsid));
660 	else
661 		(void)printf(" %-8s", getmnton(vn.v_mount));
662 	if (nflg)
663 		(void)snprintf(mode, sizeof mode, "%o", fst.mode);
664 	else
665 		strmode(fst.mode, mode);
666 	(void)printf(" %7"PRIu64" %*s", fst.fileid, nflg ? 5 : 10, mode);
667 	switch (vn.v_type) {
668 	case VBLK:
669 	case VCHR: {
670 		char *name;
671 
672 		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
673 		    S_IFCHR : S_IFBLK)) == NULL))
674 			(void)printf("  %s,%-2llu",
675 			    kdriver_search(vn.v_type, major(fst.rdev)),
676 			    (unsigned long long)minor(fst.rdev));
677 		else
678 			(void)printf(" %6s", name);
679 		break;
680 	}
681 	default:
682 		(void)printf(" %6lld", (long long)fst.size);
683 	}
684 	rw[0] = '\0';
685 	if (flag & FREAD)
686 		(void)strlcat(rw, "r", sizeof(rw));
687 	if (flag & FWRITE)
688 		(void)strlcat(rw, "w", sizeof(rw));
689 	(void)printf(" %-2s", rw);
690 	if (filename && !fsflg)
691 		(void)printf("  %s", filename);
692 	(void)putchar('\n');
693 }
694 
695 static int
696 ufs_filestat(struct vnode *vp, struct filestat *fsp)
697 {
698 	struct inode inode;
699 	struct ufsmount ufsmount;
700 	union dinode {
701 		struct ufs1_dinode dp1;
702 		struct ufs2_dinode dp2;
703 	} dip;
704 
705 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
706 		dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
707 		return 0;
708 	}
709 
710 	if (!KVM_READ(inode.i_ump, &ufsmount, sizeof (struct ufsmount))) {
711 		dprintf("can't read ufsmount at %p for pid %d", inode.i_ump, Pid);
712 		return 0;
713 	}
714 
715 	switch (ufsmount.um_fstype) {
716 	case UFS1:
717 		if (!KVM_READ(inode.i_din.ffs1_din, &dip,
718 		    sizeof(struct ufs1_dinode))) {
719 			dprintf("can't read dinode at %p for pid %d",
720 				inode.i_din.ffs1_din, Pid);
721 			return 0;
722 		}
723 		fsp->rdev = dip.dp1.di_rdev;
724 		break;
725 	case UFS2:
726 		if (!KVM_READ(inode.i_din.ffs2_din, &dip,
727 		    sizeof(struct ufs2_dinode))) {
728 			dprintf("can't read dinode at %p for pid %d",
729 			    inode.i_din.ffs2_din, Pid);
730 			return 0;
731 		}
732 		fsp->rdev = dip.dp2.di_rdev;
733 		break;
734 	default:
735 		dprintf("unknown ufs type %ld for pid %d",
736 			ufsmount.um_fstype, Pid);
737 		break;
738 	}
739 	fsp->fsid = inode.i_dev & 0xffff;
740 	fsp->fileid = inode.i_number;
741 	fsp->mode = (mode_t)inode.i_mode;
742 	fsp->size = inode.i_size;
743 
744 	return 1;
745 }
746 
747 static int
748 ext2fs_filestat(struct vnode *vp, struct filestat *fsp)
749 {
750 	struct inode inode;
751 	struct ext2fs_dinode dinode;
752 
753 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
754 		dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
755 		return 0;
756 	}
757 	fsp->fsid = inode.i_dev & 0xffff;
758 	fsp->fileid = inode.i_number;
759 
760 	if (!KVM_READ(inode.i_din.e2fs_din, &dinode, sizeof dinode)) {
761 		dprintf("can't read ext2fs_dinode at %p for pid %d",
762 			inode.i_din.e2fs_din, Pid);
763 		return 0;
764 	}
765 	fsp->mode = dinode.e2di_mode;
766 	fsp->size = dinode.e2di_size;
767 	fsp->rdev = dinode.e2di_rdev;
768 
769 	return 1;
770 }
771 
772 static int
773 nfs_filestat(struct vnode *vp, struct filestat *fsp)
774 {
775 	struct nfsnode nfsnode;
776 	struct vattr va;
777 
778 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
779 		dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp),
780 		    Pid);
781 		return 0;
782 	}
783 	if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
784 		dprintf("can't read vnode attributes at %p for pid %d",
785 		    nfsnode.n_vattr, Pid);
786 		return 0;
787 	}
788 	fsp->fsid = va.va_fsid;
789 	fsp->fileid = va.va_fileid;
790 	fsp->size = nfsnode.n_size;
791 	fsp->rdev = va.va_rdev;
792 	fsp->mode = (mode_t)va.va_mode | getftype(vp->v_type);
793 
794 	return 1;
795 }
796 
797 static int
798 msdosfs_filestat(struct vnode *vp, struct filestat *fsp)
799 {
800 	struct denode de;
801 	struct msdosfsmount mp;
802 
803 	if (!KVM_READ(VTONFS(vp), &de, sizeof(de))) {
804 		dprintf("can't read denode at %p for pid %d", VTONFS(vp),
805 		    Pid);
806 		return 0;
807 	}
808 	if (!KVM_READ(de.de_pmp, &mp, sizeof(mp))) {
809 		dprintf("can't read mount struct at %p for pid %d", de.de_pmp,
810 		    Pid);
811 		return 0;
812 	}
813 
814 	fsp->fsid = de.de_dev & 0xffff;
815 	fsp->fileid = 0; /* XXX see msdosfs_vptofh() for more info */
816 	fsp->size = de.de_FileSize;
817 	fsp->rdev = 0;	/* msdosfs doesn't support device files */
818 	fsp->mode = (0777 & mp.pm_mask) | getftype(vp->v_type);
819 	return 1;
820 }
821 
822 static const char *
823 layer_filestat(struct vnode *vp, struct filestat *fsp)
824 {
825 	struct layer_node layer_node;
826 	struct mount mount;
827 	struct vnode vn;
828 	const char *badtype;
829 
830 	if (!KVM_READ(VTOLAYER(vp), &layer_node, sizeof(layer_node))) {
831 		dprintf("can't read layer_node at %p for pid %d",
832 		    VTOLAYER(vp), Pid);
833 		return "error";
834 	}
835 	if (!KVM_READ(vp->v_mount, &mount, sizeof(struct mount))) {
836 		dprintf("can't read mount struct at %p for pid %d",
837 		    vp->v_mount, Pid);
838 		return "error";
839 	}
840 	vp = layer_node.layer_lowervp;
841 	if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
842 		dprintf("can't read vnode at %p for pid %d", vp, Pid);
843 		return "error";
844 	}
845 	if ((badtype = vfilestat(&vn, fsp)) == NULL)
846 		fsp->fsid = mount.mnt_stat.f_fsidx.__fsid_val[0];
847 	return badtype;
848 }
849 
850 static char *
851 getmnton(struct mount *m)
852 {
853 	static struct mount mount;
854 	static struct mtab {
855 		struct mtab *next;
856 		struct mount *m;
857 		char mntonname[MNAMELEN];
858 	} *mhead = NULL;
859 	struct mtab *mt;
860 
861 	for (mt = mhead; mt != NULL; mt = mt->next)
862 		if (m == mt->m)
863 			return mt->mntonname;
864 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
865 		warnx("can't read mount table at %p", m);
866 		return NULL;
867 	}
868 	if ((mt = malloc(sizeof (struct mtab))) == NULL) {
869 		err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab));
870 	}
871 	mt->m = m;
872 	(void)memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0],
873 	    MNAMELEN);
874 	mt->next = mhead;
875 	mhead = mt;
876 	return mt->mntonname;
877 }
878 
879 static const char *
880 inet_addrstr(char *buf, size_t len, const struct in_addr *a, uint16_t p, bool isdg)
881 {
882 	char addr[256], serv[256];
883 	struct sockaddr_in sin;
884 	const int niflags =
885 		(nflg ? (NI_NUMERICHOST|NI_NUMERICSERV) : 0) |
886 		(isdg ? NI_DGRAM : 0);
887 
888 
889 	(void)memset(&sin, 0, sizeof(sin));
890 	sin.sin_family = AF_INET;
891 	sin.sin_len = sizeof(sin);
892 	sin.sin_addr = *a;
893 	sin.sin_port = htons(p);
894 
895 	serv[0] = '\0';
896 
897 	if (getnameinfo((struct sockaddr *)&sin, sin.sin_len,
898 	    addr, sizeof(addr), serv, sizeof(serv), niflags)) {
899 		if (inet_ntop(AF_INET, a, addr, sizeof(addr)) == NULL)
900 			strlcpy(addr, "invalid", sizeof(addr));
901 	}
902 
903 	if (serv[0] == '\0')
904 		snprintf(serv, sizeof(serv), "%u", p);
905 
906 	if (a->s_addr == INADDR_ANY) {
907 		if (p == 0)
908 			buf[0] = '\0';
909 		else
910 			snprintf(buf, len, "*:%s", serv);
911 		return buf;
912 	}
913 
914 	snprintf(buf, len, "%s:%s", addr, serv);
915 	return buf;
916 }
917 
918 #ifdef INET6
919 static const char *
920 inet6_addrstr(char *buf, size_t len, const struct in6_addr *a, uint16_t p, bool isdg)
921 {
922 	char addr[256], serv[256];
923 	struct sockaddr_in6 sin6;
924 	const int niflags =
925 		(nflg ? (NI_NUMERICHOST|NI_NUMERICSERV) : 0) |
926 		(isdg ? NI_DGRAM : 0);
927 
928 	(void)memset(&sin6, 0, sizeof(sin6));
929 	sin6.sin6_family = AF_INET6;
930 	sin6.sin6_len = sizeof(sin6);
931 	sin6.sin6_addr = *a;
932 	sin6.sin6_port = htons(p);
933 
934 	inet6_getscopeid(&sin6, INET6_IS_ADDR_LINKLOCAL);
935 	serv[0] = '\0';
936 
937 	if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
938 	    addr, sizeof(addr), serv, sizeof(serv), niflags)) {
939 		if (inet_ntop(AF_INET6, a, addr, sizeof(addr)) == NULL)
940 			strlcpy(addr, "invalid", sizeof(addr));
941 	}
942 
943 	if (serv[0] == '\0')
944 		snprintf(serv, sizeof(serv), "%u", p);
945 
946 	if (IN6_IS_ADDR_UNSPECIFIED(a)) {
947 		if (p == 0)
948 			buf[0] = '\0';
949 		else
950 			snprintf(buf, len, "*:%s", serv);
951 		return buf;
952 	}
953 
954 	if (strchr(addr, ':') == NULL)
955 		snprintf(buf, len, "%s:%s", addr, serv);
956 	else
957 		snprintf(buf, len, "[%s]:%s", addr, serv);
958 
959 	return buf;
960 }
961 #endif
962 
963 static const char *
964 at_addrstr(char *buf, size_t len, const struct sockaddr_at *sat)
965 {
966 	const struct netrange *nr = &sat->sat_range.r_netrange;
967 	const struct at_addr *at = &sat->sat_addr;
968 	char addr[64], phase[64], range[64];
969 
970 	if (sat->sat_port || at->s_net || at->s_node) {
971 		if (at->s_net || at->s_node)
972 			snprintf(addr, sizeof(addr), "%u.%u:%u",
973 			    ntohs(at->s_net), at->s_node, sat->sat_port);
974 		else
975 			snprintf(addr, sizeof(addr), "*:%u", sat->sat_port);
976 	} else
977 		addr[0] = '\0';
978 
979 	if (nr->nr_phase)
980 		snprintf(phase, sizeof(phase), " phase %u", nr->nr_phase);
981 	else
982 		phase[0] = '\0';
983 
984 	if (nr->nr_firstnet || nr->nr_lastnet)
985 		snprintf(range, sizeof(range), " range [%u-%u]",
986 		    ntohs(nr->nr_firstnet), ntohs(nr->nr_lastnet));
987 	else
988 		range[0] = '\0';
989 
990 	snprintf(buf, len, "%s%s%s", addr, phase, range);
991 	return buf;
992 }
993 
994 static void
995 socktrans(struct socket *sock, int i)
996 {
997 	static const char *stypename[] = {
998 		"unused",	/* 0 */
999 		"stream", 	/* 1 */
1000 		"dgram",	/* 2 */
1001 		"raw",		/* 3 */
1002 		"rdm",		/* 4 */
1003 		"seqpak"	/* 5 */
1004 	};
1005 #define	STYPEMAX 5
1006 	struct socket	so;
1007 	struct protosw	proto;
1008 	struct domain	dom;
1009 	struct inpcb	inpcb;
1010 #ifdef INET6
1011 	struct in6pcb	in6pcb;
1012 #endif
1013 	struct unpcb	unpcb;
1014 	struct ddpcb	ddpcb;
1015 	int len;
1016 	char dname[32];
1017 	char lbuf[512], fbuf[512], pbuf[24];
1018 	bool isdgram;
1019 
1020 	pbuf[0] = '\0';
1021 	/* fill in socket */
1022 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
1023 		dprintf("can't read sock at %p", sock);
1024 		goto bad;
1025 	}
1026 
1027 	/* fill in protosw entry */
1028 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
1029 		dprintf("can't read protosw at %p", so.so_proto);
1030 		goto bad;
1031 	}
1032 
1033 	/* fill in domain */
1034 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
1035 		dprintf("can't read domain at %p", proto.pr_domain);
1036 		goto bad;
1037 	}
1038 
1039 	if (checkfile && dom.dom_family != AF_LOCAL)
1040 		return;
1041 
1042 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
1043 	    sizeof(dname) - 1)) != sizeof(dname) -1) {
1044 		dprintf("can't read domain name at %p", dom.dom_name);
1045 		dname[0] = '\0';
1046 	}
1047 	else
1048 		dname[len] = '\0';
1049 
1050 	/*
1051 	 * protocol specific formatting
1052 	 *
1053 	 * Try to find interesting things to print.  For TCP, the interesting
1054 	 * thing is the address of the tcpcb, for UDP and others, just the
1055 	 * inpcb (socket pcb).  For UNIX domain, its the address of the socket
1056 	 * pcb and the address of the connected pcb (if connected).  Otherwise
1057 	 * just print the protocol number and address of the socket itself.
1058 	 * The idea is not to duplicate netstat, but to make available enough
1059 	 * information for further analysis.
1060 	 */
1061 	fbuf[0] = '\0';
1062 	lbuf[0] = '\0';
1063 	isdgram = false;
1064 	switch(dom.dom_family) {
1065 	case AF_INET:
1066 		getinetproto(pbuf, sizeof(pbuf), proto.pr_protocol);
1067 		switch (proto.pr_protocol) {
1068 		case IPPROTO_UDP:
1069 			isdgram = true;
1070 		case IPPROTO_TCP:
1071 			if (so.so_pcb == NULL)
1072 				break;
1073 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
1074 			    sizeof(inpcb)) != sizeof(inpcb)) {
1075 				dprintf("can't read inpcb at %p", so.so_pcb);
1076 				goto bad;
1077 			}
1078 			inet_addrstr(lbuf, sizeof(lbuf), &inpcb.inp_laddr,
1079 			    ntohs(inpcb.inp_lport), isdgram);
1080 			inet_addrstr(fbuf, sizeof(fbuf), &inpcb.inp_faddr,
1081 			    ntohs(inpcb.inp_fport), isdgram);
1082 			break;
1083 		default:
1084 			break;
1085 		}
1086 		break;
1087 #ifdef INET6
1088 	case AF_INET6:
1089 		getinetproto(pbuf, sizeof(pbuf), proto.pr_protocol);
1090 		switch (proto.pr_protocol) {
1091 		case IPPROTO_UDP:
1092 			isdgram = true;
1093 		case IPPROTO_TCP:
1094 			if (so.so_pcb == NULL)
1095 				break;
1096 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb,
1097 			    sizeof(in6pcb)) != sizeof(in6pcb)) {
1098 				dprintf("can't read in6pcb at %p", so.so_pcb);
1099 				goto bad;
1100 			}
1101 			inet6_addrstr(lbuf, sizeof(lbuf), &in6pcb.in6p_laddr,
1102 			    ntohs(in6pcb.in6p_lport), isdgram);
1103 			inet6_addrstr(fbuf, sizeof(fbuf), &in6pcb.in6p_faddr,
1104 			    ntohs(in6pcb.in6p_fport), isdgram);
1105 			break;
1106 		default:
1107 			break;
1108 		}
1109 		break;
1110 #endif
1111 	case AF_LOCAL:
1112 		/* print address of pcb and connected pcb */
1113 		if (so.so_pcb) {
1114 			char shoconn[4], *cp;
1115 			void *pcb[2];
1116 			size_t p = 0;
1117 
1118 			pcb[0] = so.so_pcb;
1119 
1120 			cp = shoconn;
1121 			if (!(so.so_state & SS_CANTRCVMORE))
1122 				*cp++ = '<';
1123 			*cp++ = '-';
1124 			if (!(so.so_state & SS_CANTSENDMORE))
1125 				*cp++ = '>';
1126 			*cp = '\0';
1127 again:
1128 			if (kvm_read(kd, (u_long)pcb[p], (char *)&unpcb,
1129 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
1130 				dprintf("can't read unpcb at %p", so.so_pcb);
1131 				goto bad;
1132 			}
1133 			if (checkfile) {
1134 				struct vnode vn;
1135 				struct filestat fst;
1136 				const char *badtype, *filename;
1137 				if (unpcb.unp_vnode == NULL)
1138 					return;
1139 				if (!checkfs(unpcb.unp_vnode, &vn, &fst,
1140 				    &badtype, &filename))
1141 					return;
1142 			}
1143 
1144 			if (unpcb.unp_addr) {
1145 				struct sockaddr_un *sun =
1146 					malloc(unpcb.unp_addrlen);
1147 				if (sun == NULL)
1148 				    err(1, "malloc(%zu)",
1149 					unpcb.unp_addrlen);
1150 				if (kvm_read(kd, (u_long)unpcb.unp_addr,
1151 				    sun, unpcb.unp_addrlen) !=
1152 				    (ssize_t)unpcb.unp_addrlen) {
1153 					dprintf("can't read sun at %p",
1154 					    unpcb.unp_addr);
1155 					free(sun);
1156 				} else {
1157 					snprintf(fbuf, sizeof(fbuf), " %s %s %s",
1158 					    shoconn, sun->sun_path,
1159 					    p == 0 ? "[creat]" : "[using]");
1160 					free(sun);
1161 					break;
1162 				}
1163 			}
1164 			if (unpcb.unp_conn) {
1165 				if (p == 0) {
1166 					pcb[++p] = unpcb.unp_conn;
1167 					goto again;
1168 				} else
1169 					snprintf(fbuf, sizeof(fbuf),
1170 					    " %p %s %p", pcb[0], shoconn,
1171 					    pcb[1]);
1172 			}
1173 		}
1174 		break;
1175 	case AF_APPLETALK:
1176 		getatproto(pbuf, sizeof(pbuf), proto.pr_protocol);
1177 		if (so.so_pcb) {
1178 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&ddpcb,
1179 			    sizeof(ddpcb)) != sizeof(ddpcb)){
1180 				dprintf("can't read ddpcb at %p", so.so_pcb);
1181 				goto bad;
1182 			}
1183 			at_addrstr(fbuf, sizeof(fbuf), &ddpcb.ddp_fsat);
1184 			at_addrstr(lbuf, sizeof(lbuf), &ddpcb.ddp_lsat);
1185 		}
1186 		break;
1187 	default:
1188 		/* print protocol number and socket address */
1189 		snprintf(fbuf, sizeof(fbuf), " %d %jx", proto.pr_protocol,
1190 		    (uintmax_t)(uintptr_t)sock);
1191 		break;
1192 	}
1193 	PREFIX(i);
1194 	if ((u_short)so.so_type > STYPEMAX)
1195 		(void)printf("* %s ?%d", dname, so.so_type);
1196 	else
1197 		(void)printf("* %s %s", dname, stypename[so.so_type]);
1198 
1199 	if (pbuf[0])
1200 		printf("%s", pbuf);
1201 	if (fbuf[0] || lbuf[0])
1202 		printf(" %s%s%s", fbuf, (fbuf[0] && lbuf[0]) ? " <-> " : "",
1203 		    lbuf);
1204 	else if (so.so_pcb)
1205 		printf(" %jx", (uintmax_t)(uintptr_t)so.so_pcb);
1206 	(void)printf("\n");
1207 	return;
1208 bad:
1209 	(void)printf("* error\n");
1210 }
1211 
1212 static void
1213 ptrans(struct file *fp, struct pipe *cpipe, int i)
1214 {
1215 	struct pipe cp;
1216 
1217 	PREFIX(i);
1218 
1219 	/* fill in pipe */
1220 	if (!KVM_READ(cpipe, &cp, sizeof(struct pipe))) {
1221 		dprintf("can't read pipe at %p", cpipe);
1222 		goto bad;
1223 	}
1224 
1225 	/* pipe descriptor is either read or write, never both */
1226 	(void)printf("* pipe %p %s %p %s%s%s", cpipe,
1227 		(fp->f_flag & FWRITE) ? "->" : "<-",
1228 		cp.pipe_peer,
1229 		(fp->f_flag & FWRITE) ? "w" : "r",
1230 		(fp->f_flag & FNONBLOCK) ? "n" : "",
1231 		(cp.pipe_state & PIPE_ASYNC) ? "a" : "");
1232 	(void)printf("\n");
1233 	return;
1234 bad:
1235 	(void)printf("* error\n");
1236 }
1237 
1238 static void
1239 misctrans(struct file *file, int i)
1240 {
1241 
1242 	PREFIX(i);
1243 	pmisc(file, dtypes[file->f_type]);
1244 }
1245 
1246 /*
1247  * getinetproto --
1248  *	print name of protocol number
1249  */
1250 static void
1251 getinetproto(char *buf, size_t len, int number)
1252 {
1253 	const char *cp;
1254 
1255 	switch (number) {
1256 	case IPPROTO_IP:
1257 		cp = "ip"; break;
1258 	case IPPROTO_ICMP:
1259 		cp ="icmp"; break;
1260 	case IPPROTO_GGP:
1261 		cp ="ggp"; break;
1262 	case IPPROTO_TCP:
1263 		cp ="tcp"; break;
1264 	case IPPROTO_EGP:
1265 		cp ="egp"; break;
1266 	case IPPROTO_PUP:
1267 		cp ="pup"; break;
1268 	case IPPROTO_UDP:
1269 		cp ="udp"; break;
1270 	case IPPROTO_IDP:
1271 		cp ="idp"; break;
1272 	case IPPROTO_RAW:
1273 		cp ="raw"; break;
1274 	case IPPROTO_ICMPV6:
1275 		cp ="icmp6"; break;
1276 	default:
1277 		(void)snprintf(buf, len, " %d", number);
1278 		return;
1279 	}
1280 	(void)snprintf(buf, len, " %s", cp);
1281 }
1282 
1283 /*
1284  * getatproto --
1285  *	print name of protocol number
1286  */
1287 static void
1288 getatproto(char *buf, size_t len, int number)
1289 {
1290 	const char *cp;
1291 
1292 	switch (number) {
1293 	case ATPROTO_DDP:
1294 		cp = "ddp"; break;
1295 	case ATPROTO_AARP:
1296 		cp ="aarp"; break;
1297 	default:
1298 		(void)snprintf(buf, len, " %d", number);
1299 		return;
1300 	}
1301 	(void)snprintf(buf, len, " %s", cp);
1302 }
1303 
1304 static int
1305 getfname(const char *filename)
1306 {
1307 	struct stat statbuf;
1308 	DEVS *cur;
1309 
1310 	if (stat(filename, &statbuf)) {
1311 		warn("stat(%s)", filename);
1312 		return 0;
1313 	}
1314 	if ((cur = malloc(sizeof(*cur))) == NULL) {
1315 		err(1, "malloc(%zu)", sizeof(*cur));
1316 	}
1317 	cur->next = devs;
1318 	devs = cur;
1319 
1320 	cur->ino = statbuf.st_ino;
1321 	cur->fsid = statbuf.st_dev & 0xffff;
1322 	cur->name = filename;
1323 	return 1;
1324 }
1325 
1326 mode_t
1327 getftype(enum vtype v_type)
1328 {
1329 	mode_t ftype;
1330 
1331 	switch (v_type) {
1332 	case VREG:
1333 		ftype = S_IFREG;
1334 		break;
1335 	case VDIR:
1336 		ftype = S_IFDIR;
1337 		break;
1338 	case VBLK:
1339 		ftype = S_IFBLK;
1340 		break;
1341 	case VCHR:
1342 		ftype = S_IFCHR;
1343 		break;
1344 	case VLNK:
1345 		ftype = S_IFLNK;
1346 		break;
1347 	case VSOCK:
1348 		ftype = S_IFSOCK;
1349 		break;
1350 	case VFIFO:
1351 		ftype = S_IFIFO;
1352 		break;
1353 	default:
1354 		ftype = 0;
1355 		break;
1356 	};
1357 
1358 	return ftype;
1359 }
1360 
1361 static void
1362 usage(void)
1363 {
1364 	(void)fprintf(stderr, "Usage: %s [-Afnv] [-M core] [-N system] "
1365 	    "[-p pid] [-u user] [file ...]\n", getprogname());
1366 	exit(1);
1367 }
1368