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