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