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