xref: /freebsd/usr.bin/fstat/fstat.c (revision c713eaa60353b6f1bc562075efcbe0720233249e)
1 /*-
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33 
34 #ifndef lint
35 static const char copyright[] =
36 "@(#) Copyright (c) 1988, 1993\n\
37 	The Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 #if 0
42 static char sccsid[] = "@(#)fstat.c	8.3 (Berkeley) 5/2/95";
43 #endif
44 #endif /* not lint */
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
47 
48 #include <sys/param.h>
49 #include <sys/time.h>
50 #include <sys/proc.h>
51 #include <sys/user.h>
52 #include <sys/stat.h>
53 #include <sys/vnode.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/domain.h>
57 #include <sys/protosw.h>
58 #include <sys/un.h>
59 #include <sys/unpcb.h>
60 #include <sys/sysctl.h>
61 #include <sys/filedesc.h>
62 #include <sys/queue.h>
63 #define	_WANT_FILE
64 #include <sys/file.h>
65 #include <sys/conf.h>
66 #define	_KERNEL
67 #include <sys/pipe.h>
68 #include <sys/mount.h>
69 #include <ufs/ufs/quota.h>
70 #include <ufs/ufs/inode.h>
71 #include <fs/devfs/devfs.h>
72 #include <fs/devfs/devfs_int.h>
73 #undef _KERNEL
74 #include <nfs/nfsproto.h>
75 #include <nfs/rpcv2.h>
76 #include <nfsclient/nfs.h>
77 #include <nfsclient/nfsnode.h>
78 
79 
80 #include <vm/vm.h>
81 #include <vm/vm_map.h>
82 #include <vm/vm_object.h>
83 
84 #include <net/route.h>
85 #include <netinet/in.h>
86 #include <netinet/in_systm.h>
87 #include <netinet/ip.h>
88 #include <netinet/in_pcb.h>
89 
90 #include <ctype.h>
91 #include <err.h>
92 #include <fcntl.h>
93 #include <kvm.h>
94 #include <limits.h>
95 #include <nlist.h>
96 #include <paths.h>
97 #include <pwd.h>
98 #include <stdio.h>
99 #include <stdlib.h>
100 #include <stddef.h>
101 #include <string.h>
102 #include <unistd.h>
103 #include <netdb.h>
104 
105 #include "fstat.h"
106 
107 #define	TEXT	-1
108 #define	CDIR	-2
109 #define	RDIR	-3
110 #define	TRACE	-4
111 #define	MMAP	-5
112 #define	JDIR	-6
113 
114 DEVS *devs;
115 
116 #ifdef notdef
117 struct nlist nl[] = {
118 	{ "" },
119 };
120 #endif
121 
122 int 	fsflg,	/* show files on same filesystem as file(s) argument */
123 	pflg,	/* show files open by a particular pid */
124 	uflg;	/* show files open by a particular (effective) user */
125 int 	checkfile; /* true if restricting to particular files or filesystems */
126 int	nflg;	/* (numerical) display f.s. and rdev as dev_t */
127 int	vflg;	/* display errors in locating kernel data objects etc... */
128 int	mflg;	/* include memory-mapped files */
129 
130 
131 struct file **ofiles;	/* buffer of pointers to file structures */
132 int maxfiles;
133 #define ALLOC_OFILES(d)	\
134 	if ((d) > maxfiles) { \
135 		free(ofiles); \
136 		ofiles = malloc((d) * sizeof(struct file *)); \
137 		if (ofiles == NULL) { \
138 			err(1, NULL); \
139 		} \
140 		maxfiles = (d); \
141 	}
142 
143 char *memf, *nlistf;
144 kvm_t *kd;
145 
146 static void fstat_kvm(int, int);
147 static void fstat_sysctl(int, int);
148 void dofiles(struct kinfo_proc *kp);
149 void dommap(struct kinfo_proc *kp);
150 void vtrans(struct vnode *vp, int i, int flag);
151 int  ufs_filestat(struct vnode *vp, struct filestat *fsp);
152 int  nfs_filestat(struct vnode *vp, struct filestat *fsp);
153 int  devfs_filestat(struct vnode *vp, struct filestat *fsp);
154 char *getmnton(struct mount *m);
155 void pipetrans(struct pipe *pi, int i, int flag);
156 void socktrans(struct socket *sock, int i);
157 void getinetproto(int number);
158 int  getfname(const char *filename);
159 void usage(void);
160 char *kdevtoname(struct cdev *dev);
161 
162 int
163 main(int argc, char **argv)
164 {
165 	struct passwd *passwd;
166 	int arg, ch, what;
167 
168 	arg = 0;
169 	what = KERN_PROC_PROC;
170 	nlistf = memf = NULL;
171 	while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1)
172 		switch((char)ch) {
173 		case 'f':
174 			fsflg = 1;
175 			break;
176 		case 'M':
177 			memf = optarg;
178 			break;
179 		case 'N':
180 			nlistf = optarg;
181 			break;
182 		case 'm':
183 			mflg = 1;
184 			break;
185 		case 'n':
186 			nflg = 1;
187 			break;
188 		case 'p':
189 			if (pflg++)
190 				usage();
191 			if (!isdigit(*optarg)) {
192 				warnx("-p requires a process id");
193 				usage();
194 			}
195 			what = KERN_PROC_PID;
196 			arg = atoi(optarg);
197 			break;
198 		case 'u':
199 			if (uflg++)
200 				usage();
201 			if (!(passwd = getpwnam(optarg)))
202 				errx(1, "%s: unknown uid", optarg);
203 			what = KERN_PROC_UID;
204 			arg = passwd->pw_uid;
205 			break;
206 		case 'v':
207 			vflg = 1;
208 			break;
209 		case '?':
210 		default:
211 			usage();
212 		}
213 
214 	if (*(argv += optind)) {
215 		for (; *argv; ++argv) {
216 			if (getfname(*argv))
217 				checkfile = 1;
218 		}
219 		if (!checkfile)	/* file(s) specified, but none accessable */
220 			exit(1);
221 	}
222 
223 	if (fsflg && !checkfile) {
224 		/* -f with no files means use wd */
225 		if (getfname(".") == 0)
226 			exit(1);
227 		checkfile = 1;
228 	}
229 
230 	if (memf != NULL)
231 		fstat_kvm(what, arg);
232 	else
233 		fstat_sysctl(what, arg);
234 	exit(0);
235 }
236 
237 static void
238 print_header(void)
239 {
240 
241 	if (nflg)
242 		printf("%s",
243 "USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
244 	else
245 		printf("%s",
246 "USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
247 	if (checkfile && fsflg == 0)
248 		printf(" NAME\n");
249 	else
250 		putchar('\n');
251 }
252 
253 static void
254 fstat_kvm(int what, int arg)
255 {
256 	struct kinfo_proc *p, *plast;
257 	char buf[_POSIX2_LINE_MAX];
258 	int cnt;
259 
260 	ALLOC_OFILES(256);	/* reserve space for file pointers */
261 
262 	/*
263 	 * Discard setgid privileges if not the running kernel so that bad
264 	 * guys can't print interesting stuff from kernel memory.
265 	 */
266 	if (nlistf != NULL || memf != NULL)
267 		setgid(getgid());
268 
269 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
270 		errx(1, "%s", buf);
271 	setgid(getgid());
272 #ifdef notdef
273 	if (kvm_nlist(kd, nl) != 0)
274 		errx(1, "no namelist: %s", kvm_geterr(kd));
275 #endif
276 	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
277 		errx(1, "%s", kvm_geterr(kd));
278 	print_header();
279 	for (plast = &p[cnt]; p < plast; ++p) {
280 		if (p->ki_stat == SZOMB)
281 			continue;
282 		dofiles(p);
283 		if (mflg)
284 			dommap(p);
285 	}
286 }
287 
288 static void
289 fstat_sysctl(int what, int arg)
290 {
291 
292 	/* not yet implemented */
293 	fstat_kvm(what, arg);
294 }
295 
296 const char	*Uname, *Comm;
297 int	Pid;
298 
299 #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
300 	switch(i) { \
301 	case TEXT: \
302 		printf(" text"); \
303 		break; \
304 	case CDIR: \
305 		printf("   wd"); \
306 		break; \
307 	case RDIR: \
308 		printf(" root"); \
309 		break; \
310 	case TRACE: \
311 		printf("   tr"); \
312 		break; \
313 	case MMAP: \
314 		printf(" mmap"); \
315 		break; \
316 	case JDIR: \
317 		printf(" jail"); \
318 		break; \
319 	default: \
320 		printf(" %4d", i); \
321 		break; \
322 	}
323 
324 /*
325  * print open files attributed to this process
326  */
327 void
328 dofiles(struct kinfo_proc *kp)
329 {
330 	int i;
331 	struct file file;
332 	struct filedesc filed;
333 
334 	Uname = user_from_uid(kp->ki_uid, 0);
335 	Pid = kp->ki_pid;
336 	Comm = kp->ki_comm;
337 
338 	if (kp->ki_fd == NULL)
339 		return;
340 	if (!KVM_READ(kp->ki_fd, &filed, sizeof (filed))) {
341 		dprintf(stderr, "can't read filedesc at %p for pid %d\n",
342 		    (void *)kp->ki_fd, Pid);
343 		return;
344 	}
345 	/*
346 	 * root directory vnode, if one
347 	 */
348 	if (filed.fd_rdir)
349 		vtrans(filed.fd_rdir, RDIR, FREAD);
350 	/*
351 	 * current working directory vnode
352 	 */
353 	if (filed.fd_cdir)
354 		vtrans(filed.fd_cdir, CDIR, FREAD);
355 	/*
356 	 * jail root, if any.
357 	 */
358 	if (filed.fd_jdir)
359 		vtrans(filed.fd_jdir, JDIR, FREAD);
360 	/*
361 	 * ktrace vnode, if one
362 	 */
363 	if (kp->ki_tracep)
364 		vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE);
365 	/*
366 	 * text vnode, if one
367 	 */
368 	if (kp->ki_textvp)
369 		vtrans(kp->ki_textvp, TEXT, FREAD);
370 	/*
371 	 * open files
372 	 */
373 #define FPSIZE	(sizeof (struct file *))
374 #define MAX_LASTFILE	(0x1000000)
375 
376 	/* Sanity check on filed.fd_lastfile */
377 	if (filed.fd_lastfile <= -1 || filed.fd_lastfile > MAX_LASTFILE)
378 		return;
379 
380 	ALLOC_OFILES(filed.fd_lastfile+1);
381 	if (!KVM_READ(filed.fd_ofiles, ofiles,
382 	    (filed.fd_lastfile+1) * FPSIZE)) {
383 		dprintf(stderr,
384 		    "can't read file structures at %p for pid %d\n",
385 		    (void *)filed.fd_ofiles, Pid);
386 		return;
387 	}
388 	for (i = 0; i <= filed.fd_lastfile; i++) {
389 		if (ofiles[i] == NULL)
390 			continue;
391 		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
392 			dprintf(stderr, "can't read file %d at %p for pid %d\n",
393 			    i, (void *)ofiles[i], Pid);
394 			continue;
395 		}
396 		if (file.f_type == DTYPE_VNODE)
397 			vtrans(file.f_vnode, i, file.f_flag);
398 		else if (file.f_type == DTYPE_SOCKET) {
399 			if (checkfile == 0)
400 				socktrans(file.f_data, i);
401 		}
402 #ifdef DTYPE_PIPE
403 		else if (file.f_type == DTYPE_PIPE) {
404 			if (checkfile == 0)
405 				pipetrans(file.f_data, i, file.f_flag);
406 		}
407 #endif
408 #ifdef DTYPE_FIFO
409 		else if (file.f_type == DTYPE_FIFO) {
410 			if (checkfile == 0)
411 				vtrans(file.f_vnode, i, file.f_flag);
412 		}
413 #endif
414 		else {
415 			dprintf(stderr,
416 			    "unknown file type %d for file %d of pid %d\n",
417 			    file.f_type, i, Pid);
418 		}
419 	}
420 }
421 
422 void
423 dommap(struct kinfo_proc *kp)
424 {
425 	vm_map_t map;
426 	struct vmspace vmspace;
427 	struct vm_map_entry entry;
428 	vm_map_entry_t entryp;
429 	struct vm_object object;
430 	vm_object_t objp;
431 	int prot, fflags;
432 
433 	if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) {
434 		dprintf(stderr,
435 		    "can't read vmspace at %p for pid %d\n",
436 		    (void *)kp->ki_vmspace, Pid);
437 		return;
438 	}
439 	map = &vmspace.vm_map;
440 
441 	for (entryp = map->header.next;
442 	    entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) {
443 		if (!KVM_READ(entryp, &entry, sizeof(entry))) {
444 			dprintf(stderr,
445 			    "can't read vm_map_entry at %p for pid %d\n",
446 			    (void *)entryp, Pid);
447 			return;
448 		}
449 
450 		if (entry.eflags & MAP_ENTRY_IS_SUB_MAP)
451 			continue;
452 
453 		if ((objp = entry.object.vm_object) == NULL)
454 			continue;
455 
456 		for (; objp; objp = object.backing_object) {
457 			if (!KVM_READ(objp, &object, sizeof(object))) {
458 				dprintf(stderr,
459 				    "can't read vm_object at %p for pid %d\n",
460 				    (void *)objp, Pid);
461 				return;
462 			}
463 		}
464 
465 		prot = entry.protection;
466 		fflags = (prot & VM_PROT_READ ? FREAD : 0) |
467 		    (prot & VM_PROT_WRITE ? FWRITE : 0);
468 
469 		switch (object.type) {
470 		case OBJT_VNODE:
471 			vtrans((struct vnode *)object.handle, MMAP, fflags);
472 			break;
473 		default:
474 			break;
475 		}
476 	}
477 }
478 
479 char *
480 kdevtoname(struct cdev *dev)
481 {
482 	struct cdev si;
483 
484 	if (!KVM_READ(dev, &si, sizeof si))
485 		return (NULL);
486 	return (strdup(si.__si_namebuf));
487 }
488 
489 void
490 vtrans(struct vnode *vp, int i, int flag)
491 {
492 	struct vnode vn;
493 	struct filestat fst;
494 	char rw[3], mode[15], tagstr[12], *tagptr;
495 	const char *badtype, *filename;
496 
497 	filename = badtype = NULL;
498 	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
499 		dprintf(stderr, "can't read vnode at %p for pid %d\n",
500 		    (void *)vp, Pid);
501 		return;
502 	}
503 	if (!KVM_READ(&vp->v_tag, &tagptr, sizeof tagptr) ||
504 	    !KVM_READ(tagptr, tagstr, sizeof tagstr)) {
505 		dprintf(stderr, "can't read v_tag at %p for pid %d\n",
506 		    (void *)vp, Pid);
507 		return;
508 	}
509 	tagstr[sizeof(tagstr) - 1] = '\0';
510 	if (vn.v_type == VNON)
511 		badtype = "none";
512 	else if (vn.v_type == VBAD)
513 		badtype = "bad";
514 	else {
515 		if (!strcmp("ufs", tagstr)) {
516 			if (!ufs_filestat(&vn, &fst))
517 				badtype = "error";
518 		} else if (!strcmp("devfs", tagstr)) {
519 			if (!devfs_filestat(&vn, &fst))
520 				badtype = "error";
521 		} else if (!strcmp("nfs", tagstr)) {
522 			if (!nfs_filestat(&vn, &fst))
523 				badtype = "error";
524 		} else if (!strcmp("msdosfs", tagstr)) {
525 			if (!msdosfs_filestat(&vn, &fst))
526 				badtype = "error";
527 		} else if (!strcmp("isofs", tagstr)) {
528 			if (!isofs_filestat(&vn, &fst))
529 				badtype = "error";
530 #ifdef ZFS
531 		} else if (!strcmp("zfs", tagstr)) {
532 			if (!zfs_filestat(&vn, &fst))
533 				badtype = "error";
534 #endif
535 		} else {
536 			static char unknown[32];
537 			snprintf(unknown, sizeof unknown, "?(%s)", tagstr);
538 			badtype = unknown;
539 		}
540 	}
541 	if (checkfile) {
542 		int fsmatch = 0;
543 		DEVS *d;
544 
545 		if (badtype)
546 			return;
547 		for (d = devs; d != NULL; d = d->next)
548 			if (d->fsid == fst.fsid) {
549 				fsmatch = 1;
550 				if (d->ino == fst.fileid) {
551 					filename = d->name;
552 					break;
553 				}
554 			}
555 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
556 			return;
557 	}
558 	PREFIX(i);
559 	if (badtype) {
560 		(void)printf(" -         -  %10s    -\n", badtype);
561 		return;
562 	}
563 	if (nflg)
564 		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
565 	else
566 		(void)printf(" %-8s", getmnton(vn.v_mount));
567 	if (nflg)
568 		(void)sprintf(mode, "%o", fst.mode);
569 	else
570 		strmode(fst.mode, mode);
571 	(void)printf(" %6ld %10s", fst.fileid, mode);
572 	switch (vn.v_type) {
573 	case VBLK:
574 	case VCHR: {
575 		char *name;
576 
577 		name = kdevtoname(vn.v_rdev);
578 		if (nflg || !name)
579 			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
580 		else {
581 			printf(" %6s", name);
582 			free(name);
583 		}
584 		break;
585 	}
586 	default:
587 		printf(" %6lu", fst.size);
588 	}
589 	rw[0] = '\0';
590 	if (flag & FREAD)
591 		strcat(rw, "r");
592 	if (flag & FWRITE)
593 		strcat(rw, "w");
594 	printf(" %2s", rw);
595 	if (filename && !fsflg)
596 		printf("  %s", filename);
597 	putchar('\n');
598 }
599 
600 int
601 ufs_filestat(struct vnode *vp, struct filestat *fsp)
602 {
603 	struct inode inode;
604 
605 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
606 		dprintf(stderr, "can't read inode at %p for pid %d\n",
607 		    (void *)VTOI(vp), Pid);
608 		return 0;
609 	}
610 	/*
611 	 * The st_dev from stat(2) is a dev_t. These kernel structures
612 	 * contain cdev pointers. We need to convert to dev_t to make
613 	 * comparisons
614 	 */
615 	fsp->fsid = dev2udev(inode.i_dev);
616 	fsp->fileid = (long)inode.i_number;
617 	fsp->mode = (mode_t)inode.i_mode;
618 	fsp->size = (u_long)inode.i_size;
619 #if should_be_but_is_hard
620 	/* XXX - need to load i_ump and i_din[12] from kernel memory */
621 	if (inode.i_ump->um_fstype == UFS1)
622 		fsp->rdev = inode.i_din1->di_rdev;
623 	else
624 		fsp->rdev = inode.i_din2->di_rdev;
625 #else
626 	fsp->rdev = 0;
627 #endif
628 
629 	return 1;
630 }
631 
632 int
633 devfs_filestat(struct vnode *vp, struct filestat *fsp)
634 {
635 	struct devfs_dirent devfs_dirent;
636 	struct mount mount;
637 	struct vnode vnode;
638 
639 	if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) {
640 		dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n",
641 		    (void *)vp->v_data, Pid);
642 		return 0;
643 	}
644 	if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) {
645 		dprintf(stderr, "can't read mount at %p for pid %d\n",
646 		    (void *)vp->v_mount, Pid);
647 		return 0;
648 	}
649 	if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) {
650 		dprintf(stderr, "can't read vnode at %p for pid %d\n",
651 		    (void *)devfs_dirent.de_vnode, Pid);
652 		return 0;
653 	}
654 	fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0];
655 	fsp->fileid = devfs_dirent.de_inode;
656 	fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR;
657 	fsp->size = 0;
658 	fsp->rdev = dev2udev(vnode.v_rdev);
659 
660 	return 1;
661 }
662 
663 int
664 nfs_filestat(struct vnode *vp, struct filestat *fsp)
665 {
666 	struct nfsnode nfsnode;
667 	mode_t mode;
668 
669 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
670 		dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
671 		    (void *)VTONFS(vp), Pid);
672 		return 0;
673 	}
674 	fsp->fsid = nfsnode.n_vattr.va_fsid;
675 	fsp->fileid = nfsnode.n_vattr.va_fileid;
676 	fsp->size = nfsnode.n_size;
677 	fsp->rdev = nfsnode.n_vattr.va_rdev;
678 	mode = (mode_t)nfsnode.n_vattr.va_mode;
679 	switch (vp->v_type) {
680 	case VREG:
681 		mode |= S_IFREG;
682 		break;
683 	case VDIR:
684 		mode |= S_IFDIR;
685 		break;
686 	case VBLK:
687 		mode |= S_IFBLK;
688 		break;
689 	case VCHR:
690 		mode |= S_IFCHR;
691 		break;
692 	case VLNK:
693 		mode |= S_IFLNK;
694 		break;
695 	case VSOCK:
696 		mode |= S_IFSOCK;
697 		break;
698 	case VFIFO:
699 		mode |= S_IFIFO;
700 		break;
701 	case VNON:
702 	case VBAD:
703 	case VMARKER:
704 		return 0;
705 	};
706 	fsp->mode = mode;
707 
708 	return 1;
709 }
710 
711 
712 char *
713 getmnton(struct mount *m)
714 {
715 	static struct mount mount;
716 	static struct mtab {
717 		struct mtab *next;
718 		struct mount *m;
719 		char mntonname[MNAMELEN];
720 	} *mhead = NULL;
721 	struct mtab *mt;
722 
723 	for (mt = mhead; mt != NULL; mt = mt->next)
724 		if (m == mt->m)
725 			return (mt->mntonname);
726 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
727 		warnx("can't read mount table at %p", (void *)m);
728 		return (NULL);
729 	}
730 	if ((mt = malloc(sizeof (struct mtab))) == NULL)
731 		err(1, NULL);
732 	mt->m = m;
733 	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
734 	mt->next = mhead;
735 	mhead = mt;
736 	return (mt->mntonname);
737 }
738 
739 void
740 pipetrans(struct pipe *pi, int i, int flag)
741 {
742 	struct pipe pip;
743 	char rw[3];
744 
745 	PREFIX(i);
746 
747 	/* fill in socket */
748 	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
749 		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
750 		goto bad;
751 	}
752 
753 	printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
754 	printf(" %6d", (int)pip.pipe_buffer.cnt);
755 	rw[0] = '\0';
756 	if (flag & FREAD)
757 		strcat(rw, "r");
758 	if (flag & FWRITE)
759 		strcat(rw, "w");
760 	printf(" %2s", rw);
761 	putchar('\n');
762 	return;
763 
764 bad:
765 	printf("* error\n");
766 }
767 
768 void
769 socktrans(struct socket *sock, int i)
770 {
771 	static const char *stypename[] = {
772 		"unused",	/* 0 */
773 		"stream", 	/* 1 */
774 		"dgram",	/* 2 */
775 		"raw",		/* 3 */
776 		"rdm",		/* 4 */
777 		"seqpak"	/* 5 */
778 	};
779 #define	STYPEMAX 5
780 	struct socket	so;
781 	struct protosw	proto;
782 	struct domain	dom;
783 	struct inpcb	inpcb;
784 	struct unpcb	unpcb;
785 	int len;
786 	char dname[32];
787 
788 	PREFIX(i);
789 
790 	/* fill in socket */
791 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
792 		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
793 		goto bad;
794 	}
795 
796 	/* fill in protosw entry */
797 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
798 		dprintf(stderr, "can't read protosw at %p",
799 		    (void *)so.so_proto);
800 		goto bad;
801 	}
802 
803 	/* fill in domain */
804 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
805 		dprintf(stderr, "can't read domain at %p\n",
806 		    (void *)proto.pr_domain);
807 		goto bad;
808 	}
809 
810 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
811 	    sizeof(dname) - 1)) < 0) {
812 		dprintf(stderr, "can't read domain name at %p\n",
813 		    (void *)dom.dom_name);
814 		dname[0] = '\0';
815 	}
816 	else
817 		dname[len] = '\0';
818 
819 	if ((u_short)so.so_type > STYPEMAX)
820 		printf("* %s ?%d", dname, so.so_type);
821 	else
822 		printf("* %s %s", dname, stypename[so.so_type]);
823 
824 	/*
825 	 * protocol specific formatting
826 	 *
827 	 * Try to find interesting things to print.  For tcp, the interesting
828 	 * thing is the address of the tcpcb, for udp and others, just the
829 	 * inpcb (socket pcb).  For unix domain, its the address of the socket
830 	 * pcb and the address of the connected pcb (if connected).  Otherwise
831 	 * just print the protocol number and address of the socket itself.
832 	 * The idea is not to duplicate netstat, but to make available enough
833 	 * information for further analysis.
834 	 */
835 	switch(dom.dom_family) {
836 	case AF_INET:
837 	case AF_INET6:
838 		getinetproto(proto.pr_protocol);
839 		if (proto.pr_protocol == IPPROTO_TCP ) {
840 			if (so.so_pcb) {
841 				if (kvm_read(kd, (u_long)so.so_pcb,
842 				    (char *)&inpcb, sizeof(struct inpcb))
843 				    != sizeof(struct inpcb)) {
844 					dprintf(stderr,
845 					    "can't read inpcb at %p\n",
846 					    (void *)so.so_pcb);
847 					goto bad;
848 				}
849 				printf(" %lx", (u_long)inpcb.inp_ppcb);
850 			}
851 		}
852 		else if (so.so_pcb)
853 			printf(" %lx", (u_long)so.so_pcb);
854 		break;
855 	case AF_UNIX:
856 		/* print address of pcb and connected pcb */
857 		if (so.so_pcb) {
858 			printf(" %lx", (u_long)so.so_pcb);
859 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
860 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
861 				dprintf(stderr, "can't read unpcb at %p\n",
862 				    (void *)so.so_pcb);
863 				goto bad;
864 			}
865 			if (unpcb.unp_conn) {
866 				char shoconn[4], *cp;
867 
868 				cp = shoconn;
869 				if (!(so.so_rcv.sb_state & SBS_CANTRCVMORE))
870 					*cp++ = '<';
871 				*cp++ = '-';
872 				if (!(so.so_snd.sb_state & SBS_CANTSENDMORE))
873 					*cp++ = '>';
874 				*cp = '\0';
875 				printf(" %s %lx", shoconn,
876 				    (u_long)unpcb.unp_conn);
877 			}
878 		}
879 		break;
880 	default:
881 		/* print protocol number and socket address */
882 		printf(" %d %lx", proto.pr_protocol, (u_long)sock);
883 	}
884 	printf("\n");
885 	return;
886 bad:
887 	printf("* error\n");
888 }
889 
890 
891 /*
892  * Read the cdev structure in the kernel in order to work out the
893  * associated dev_t
894  */
895 dev_t
896 dev2udev(struct cdev *dev)
897 {
898 	struct cdev_priv priv;
899 
900 	if (KVM_READ(cdev2priv(dev), &priv, sizeof priv)) {
901 		return ((dev_t)priv.cdp_inode);
902 	} else {
903 		dprintf(stderr, "can't convert cdev *%p to a dev_t\n", dev);
904 		return -1;
905 	}
906 }
907 
908 /*
909  * getinetproto --
910  *	print name of protocol number
911  */
912 void
913 getinetproto(int number)
914 {
915 	static int isopen;
916 	struct protoent *pe;
917 
918 	if (!isopen)
919 		setprotoent(++isopen);
920 	if ((pe = getprotobynumber(number)) != NULL)
921 		printf(" %s", pe->p_name);
922 	else
923 		printf(" %d", number);
924 }
925 
926 int
927 getfname(const char *filename)
928 {
929 	struct stat statbuf;
930 	DEVS *cur;
931 
932 	if (stat(filename, &statbuf)) {
933 		warn("%s", filename);
934 		return(0);
935 	}
936 	if ((cur = malloc(sizeof(DEVS))) == NULL)
937 		err(1, NULL);
938 	cur->next = devs;
939 	devs = cur;
940 
941 	cur->ino = statbuf.st_ino;
942 	cur->fsid = statbuf.st_dev;
943 	cur->name = filename;
944 	return(1);
945 }
946 
947 #ifdef ZFS
948 void *
949 getvnodedata(struct vnode *vp)
950 {
951 	return (vp->v_data);
952 }
953 
954 struct mount *
955 getvnodemount(struct vnode *vp)
956 {
957 	return (vp->v_mount);
958 }
959 #endif
960 
961 void
962 usage(void)
963 {
964 	(void)fprintf(stderr,
965  "usage: fstat [-fmnv] [-M core] [-N system] [-p pid] [-u user] [file ...]\n");
966 	exit(1);
967 }
968