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