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