xref: /dflybsd-src/usr.bin/fstat/fstat.c (revision 17ea22213f86a5c5966c1e6bf8e95f022ebb92b9)
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.10 2005/01/31 18:05:09 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 #ifdef notdef
103 struct nlist nl[] = {
104 	{ "" },
105 };
106 #endif
107 
108 int 	fsflg,	/* show files on same filesystem as file(s) argument */
109 	pflg,	/* show files open by a particular pid */
110 	uflg;	/* show files open by a particular (effective) user */
111 int 	checkfile; /* true if restricting to particular files or filesystems */
112 int	nflg;	/* (numerical) display f.s. and rdev as dev_t */
113 int	vflg;	/* display errors in locating kernel data objects etc... */
114 int	mflg;	/* include memory-mapped files */
115 int	wflg_mnt = 16;
116 int	wflg_cmd = 10;
117 int	pid_width = 5;
118 int	ino_width = 6;
119 
120 
121 struct file **ofiles;	/* buffer of pointers to file structures */
122 int maxfiles;
123 #define ALLOC_OFILES(d)	\
124 	if ((d) > maxfiles) { \
125 		free(ofiles); \
126 		ofiles = malloc((d) * sizeof(struct file *)); \
127 		if (ofiles == NULL) { \
128 			err(1, NULL); \
129 		} \
130 		maxfiles = (d); \
131 	}
132 
133 kvm_t *kd;
134 
135 void dofiles(struct kinfo_proc *kp);
136 void dommap(struct kinfo_proc *kp);
137 void vtrans(struct vnode *vp, struct namecache *ncp, int i, int flag);
138 int  ufs_filestat(struct vnode *vp, struct filestat *fsp);
139 int  nfs_filestat(struct vnode *vp, struct filestat *fsp);
140 char *getmnton(struct mount *m, struct namecache_list *ncplist, struct namecache *ncp);
141 void pipetrans(struct pipe *pi, int i, int flag);
142 void socktrans(struct socket *sock, int i);
143 void getinetproto(int number);
144 int  getfname(char *filename);
145 void usage(void);
146 
147 
148 int
149 main(int argc, char **argv)
150 {
151 	register struct passwd *passwd;
152 	struct kinfo_proc *p, *plast;
153 	int arg, ch, what;
154 	char *memf, *nlistf;
155 	char buf[_POSIX2_LINE_MAX];
156 	int cnt;
157 
158 	arg = 0;
159 	what = KERN_PROC_ALL;
160 	nlistf = memf = NULL;
161 	while ((ch = getopt(argc, argv, "fmnp:u:vwN:M:")) != -1)
162 		switch((char)ch) {
163 		case 'f':
164 			fsflg = 1;
165 			break;
166 		case 'M':
167 			memf = optarg;
168 			break;
169 		case 'N':
170 			nlistf = optarg;
171 			break;
172 		case 'm':
173 			mflg = 1;
174 			break;
175 		case 'n':
176 			nflg = 1;
177 			break;
178 		case 'p':
179 			if (pflg++)
180 				usage();
181 			if (!isdigit(*optarg)) {
182 				warnx("-p requires a process id");
183 				usage();
184 			}
185 			what = KERN_PROC_PID;
186 			arg = atoi(optarg);
187 			break;
188 		case 'u':
189 			if (uflg++)
190 				usage();
191 			if (!(passwd = getpwnam(optarg)))
192 				errx(1, "%s: unknown uid", optarg);
193 			what = KERN_PROC_UID;
194 			arg = passwd->pw_uid;
195 			break;
196 		case 'v':
197 			vflg = 1;
198 			break;
199 		case 'w':
200 			wflg_mnt = 40;
201 			wflg_cmd = 16;
202 			break;
203 		case '?':
204 		default:
205 			usage();
206 		}
207 
208 	if (*(argv += optind)) {
209 		for (; *argv; ++argv) {
210 			if (getfname(*argv))
211 				checkfile = 1;
212 		}
213 		if (!checkfile)	/* file(s) specified, but none accessable */
214 			exit(1);
215 	}
216 
217 	ALLOC_OFILES(256);	/* reserve space for file pointers */
218 
219 	if (fsflg && !checkfile) {
220 		/* -f with no files means use wd */
221 		if (getfname(".") == 0)
222 			exit(1);
223 		checkfile = 1;
224 	}
225 
226 	/*
227 	 * Discard setgid privileges if not the running kernel so that bad
228 	 * guys can't print interesting stuff from kernel memory.
229 	 */
230 	if (nlistf != NULL || memf != NULL)
231 		setgid(getgid());
232 
233 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
234 		errx(1, "%s", buf);
235 #ifdef notdef
236 	if (kvm_nlist(kd, nl) != 0)
237 		errx(1, "no namelist: %s", kvm_geterr(kd));
238 #endif
239 	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
240 		errx(1, "%s", kvm_geterr(kd));
241 	if (nflg)
242 		printf("USER     %-*.*s %*.*s   FD DEV              %*.*s MODE   SZ|DV R/W",
243 			wflg_cmd, wflg_cmd, "CMD",
244 			pid_width, pid_width, "PID",
245 			ino_width, ino_width, "INUM");
246 	else
247 		printf("USER     %-*.*s %*.*s   FD %-*.*s %*.*s MODE           SZ|DV R/W",
248 			wflg_cmd, wflg_cmd, "CMD",
249 			pid_width, pid_width, "PID",
250 			wflg_mnt, wflg_mnt, "PATH",
251 			ino_width, ino_width, "INUM");
252 	if (checkfile && fsflg == 0)
253 		printf(" NAME\n");
254 	else
255 		putchar('\n');
256 
257 	for (plast = &p[cnt]; p < plast; ++p) {
258 		if (p->kp_proc.p_stat == SZOMB)
259 			continue;
260 		dofiles(p);
261 		if (mflg)
262 			dommap(p);
263 	}
264 	exit(0);
265 }
266 
267 char	*Uname, *Comm;
268 int	Pid;
269 
270 #define PREFIX(i) \
271 	printf("%-8.8s %-*s %*d", Uname, wflg_cmd, Comm, pid_width, Pid); \
272 	switch(i) { \
273 	case TEXT: \
274 		printf(" text"); \
275 		break; \
276 	case CDIR: \
277 		printf("   wd"); \
278 		break; \
279 	case RDIR: \
280 		printf(" root"); \
281 		break; \
282 	case TRACE: \
283 		printf("   tr"); \
284 		break; \
285 	case MMAP: \
286 		printf(" mmap"); \
287 		break; \
288 	default: \
289 		printf(" %4d", i); \
290 		break; \
291 	}
292 
293 /*
294  * print open files attributed to this process
295  */
296 void
297 dofiles(struct kinfo_proc *kp)
298 {
299 	int i;
300 	struct file file;
301 	struct filedesc0 filed0;
302 #define	filed	filed0.fd_fd
303 	struct proc *p = &kp->kp_proc;
304 	struct eproc *ep = &kp->kp_eproc;
305 
306 	Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
307 	Pid = p->p_pid;
308 	Comm = kp->kp_thread.td_comm;
309 
310 	if (p->p_fd == NULL)
311 		return;
312 	if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
313 		dprintf(stderr, "can't read filedesc at %p for pid %d\n",
314 		    (void *)p->p_fd, Pid);
315 		return;
316 	}
317 	/*
318 	 * root directory vnode, if one
319 	 */
320 	if (filed.fd_rdir)
321 		vtrans(filed.fd_rdir, filed.fd_nrdir, RDIR, FREAD);
322 	/*
323 	 * current working directory vnode
324 	 */
325 	vtrans(filed.fd_cdir, filed.fd_ncdir, CDIR, FREAD);
326 	/*
327 	 * ktrace vnode, if one
328 	 */
329 	if (p->p_tracep)
330 		vtrans(p->p_tracep, NULL, TRACE, FREAD|FWRITE);
331 	/*
332 	 * text vnode, if one
333 	 */
334 	if (p->p_textvp)
335 		vtrans(p->p_textvp, NULL, TEXT, FREAD);
336 	/*
337 	 * open files
338 	 */
339 #define FPSIZE	(sizeof (struct file *))
340 	ALLOC_OFILES(filed.fd_lastfile+1);
341 	if (filed.fd_nfiles > NDFILE) {
342 		if (!KVM_READ(filed.fd_ofiles, ofiles,
343 		    (filed.fd_lastfile+1) * FPSIZE)) {
344 			dprintf(stderr,
345 			    "can't read file structures at %p for pid %d\n",
346 			    (void *)filed.fd_ofiles, Pid);
347 			return;
348 		}
349 	} else
350 		bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
351 	for (i = 0; i <= filed.fd_lastfile; i++) {
352 		if (ofiles[i] == NULL)
353 			continue;
354 		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
355 			dprintf(stderr, "can't read file %d at %p for pid %d\n",
356 			    i, (void *)ofiles[i], Pid);
357 			continue;
358 		}
359 		if (file.f_type == DTYPE_VNODE) {
360 			vtrans((struct vnode *)file.f_data, file.f_ncp, i,
361 				file.f_flag);
362 		} else if (file.f_type == DTYPE_SOCKET) {
363 			if (checkfile == 0)
364 				socktrans((struct socket *)file.f_data, i);
365 		}
366 #ifdef DTYPE_PIPE
367 		else if (file.f_type == DTYPE_PIPE) {
368 			if (checkfile == 0)
369 				pipetrans((struct pipe *)file.f_data, i,
370 				    file.f_flag);
371 		}
372 #endif
373 #ifdef DTYPE_FIFO
374 		else if (file.f_type == DTYPE_FIFO) {
375 			if (checkfile == 0)
376 				vtrans((struct vnode *)file.f_data, file.f_ncp,
377 					i, file.f_flag);
378 		}
379 #endif
380 		else {
381 			dprintf(stderr,
382 			    "unknown file type %d for file %d of pid %d\n",
383 			    file.f_type, i, Pid);
384 		}
385 	}
386 }
387 
388 void
389 dommap(struct kinfo_proc *kp)
390 {
391 	struct proc *p = &kp->kp_proc;
392 	struct vmspace vmspace;
393 	vm_map_t map;
394 	struct vm_map_entry entry;
395 	vm_map_entry_t entryp;
396 	struct vm_object object;
397 	vm_object_t objp;
398 	int prot, fflags;
399 
400 	if (!KVM_READ(p->p_vmspace, &vmspace, sizeof(vmspace))) {
401 		dprintf(stderr, "can't read vmspace at %p for pid %d\n",
402 		    (void *)p->p_vmspace, Pid);
403 		return;
404 	}
405 
406 	map = &vmspace.vm_map;
407 
408 	for (entryp = map->header.next; entryp != &p->p_vmspace->vm_map.header;
409 	    entryp = entry.next) {
410 		if (!KVM_READ(entryp, &entry, sizeof(entry))) {
411 			dprintf(stderr,
412 			    "can't read vm_map_entry at %p for pid %d\n",
413 			    (void *)entryp, Pid);
414 			return;
415 		}
416 
417 		if (entry.eflags & MAP_ENTRY_IS_SUB_MAP)
418 			continue;
419 
420 		if ((objp = entry.object.vm_object) == NULL)
421 			continue;
422 
423 		for (; objp; objp = object.backing_object) {
424 			if (!KVM_READ(objp, &object, sizeof(object))) {
425 				dprintf(stderr,
426 				    "can't read vm_object at %p for pid %d\n",
427 				    (void *)objp, Pid);
428 				return;
429 			}
430 		}
431 
432 		prot = entry.protection;
433 		fflags = (prot & VM_PROT_READ ? FREAD : 0) |
434 		    (prot & VM_PROT_WRITE ? FWRITE : 0);
435 
436 		switch (object.type) {
437 		case OBJT_VNODE:
438 			vtrans((struct vnode *)object.handle, NULL,
439 				MMAP, fflags);
440 			break;
441 		default:
442 			break;
443 		}
444 	}
445 }
446 
447 void
448 vtrans(struct vnode *vp, struct namecache *ncp, int i, int flag)
449 {
450 	struct vnode vn;
451 	struct filestat fst;
452 	char rw[3], mode[15];
453 	char *badtype = NULL, *filename;
454 
455 	filename = badtype = NULL;
456 	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
457 		dprintf(stderr, "can't read vnode at %p for pid %d\n",
458 		    (void *)vp, Pid);
459 		return;
460 	}
461 	if (vn.v_type == VNON || vn.v_tag == VT_NON)
462 		badtype = "none";
463 	else if (vn.v_type == VBAD)
464 		badtype = "bad";
465 	else
466 		switch (vn.v_tag) {
467 		case VT_UFS:
468 			if (!ufs_filestat(&vn, &fst))
469 				badtype = "error";
470 			break;
471 		case VT_MFS:
472 			if (!ufs_filestat(&vn, &fst))
473 				badtype = "error";
474 			break;
475 		case VT_NFS:
476 			if (!nfs_filestat(&vn, &fst))
477 				badtype = "error";
478 			break;
479 
480 		case VT_MSDOSFS:
481 			if (!msdosfs_filestat(&vn, &fst))
482 				badtype = "error";
483 			break;
484 
485 		case VT_ISOFS:
486 			if (!isofs_filestat(&vn, &fst))
487 				badtype = "error";
488 			break;
489 
490 		default: {
491 			static char unknown[10];
492 			sprintf(badtype = unknown, "?(%x)", vn.v_tag);
493 			break;;
494 		}
495 	}
496 	if (checkfile) {
497 		int fsmatch = 0;
498 		register 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 == 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 	register 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;
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 			path[--i] = '/';
669 			ncp = ncp_copy.nc_parent;
670 		}
671 		if (i == sizeof(path) - 1)
672 			path[--i] = '/';
673 		return(path + i);
674 	}
675 
676 	/*
677 	 * If all else fails print out the mount point path
678 	 */
679 	for (mt = mhead; mt != NULL; mt = mt->next) {
680 		if (m == mt->m)
681 			return (mt->mntonname);
682 	}
683 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
684 		warnx("can't read mount table at %p", (void *)m);
685 		return (NULL);
686 	}
687 	if ((mt = malloc(sizeof (struct mtab))) == NULL)
688 		err(1, NULL);
689 	mt->m = m;
690 	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
691 	mt->next = mhead;
692 	mhead = mt;
693 	return (mt->mntonname);
694 }
695 
696 void
697 pipetrans(struct pipe *pi, int i, int flag)
698 {
699 	struct pipe pip;
700 	char rw[3];
701 
702 	PREFIX(i);
703 
704 	/* fill in socket */
705 	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
706 		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
707 		goto bad;
708 	}
709 
710 	printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
711 	printf(" %6d", (int)pip.pipe_buffer.cnt);
712 	rw[0] = '\0';
713 	if (flag & FREAD)
714 		strcat(rw, "r");
715 	if (flag & FWRITE)
716 		strcat(rw, "w");
717 	printf(" %2s", rw);
718 	putchar('\n');
719 	return;
720 
721 bad:
722 	printf("* error\n");
723 }
724 
725 void
726 socktrans(struct socket *sock, int i)
727 {
728 	static char *stypename[] = {
729 		"unused",	/* 0 */
730 		"stream", 	/* 1 */
731 		"dgram",	/* 2 */
732 		"raw",		/* 3 */
733 		"rdm",		/* 4 */
734 		"seqpak"	/* 5 */
735 	};
736 #define	STYPEMAX 5
737 	struct socket	so;
738 	struct protosw	proto;
739 	struct domain	dom;
740 	struct inpcb	inpcb;
741 	struct unpcb	unpcb;
742 	int len;
743 	char dname[32];
744 
745 	PREFIX(i);
746 
747 	/* fill in socket */
748 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
749 		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
750 		goto bad;
751 	}
752 
753 	/* fill in protosw entry */
754 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
755 		dprintf(stderr, "can't read protosw at %p",
756 		    (void *)so.so_proto);
757 		goto bad;
758 	}
759 
760 	/* fill in domain */
761 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
762 		dprintf(stderr, "can't read domain at %p\n",
763 		    (void *)proto.pr_domain);
764 		goto bad;
765 	}
766 
767 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
768 	    sizeof(dname) - 1)) < 0) {
769 		dprintf(stderr, "can't read domain name at %p\n",
770 		    (void *)dom.dom_name);
771 		dname[0] = '\0';
772 	}
773 	else
774 		dname[len] = '\0';
775 
776 	if ((u_short)so.so_type > STYPEMAX)
777 		printf("* %s ?%d", dname, so.so_type);
778 	else
779 		printf("* %s %s", dname, stypename[so.so_type]);
780 
781 	/*
782 	 * protocol specific formatting
783 	 *
784 	 * Try to find interesting things to print.  For tcp, the interesting
785 	 * thing is the address of the tcpcb, for udp and others, just the
786 	 * inpcb (socket pcb).  For unix domain, its the address of the socket
787 	 * pcb and the address of the connected pcb (if connected).  Otherwise
788 	 * just print the protocol number and address of the socket itself.
789 	 * The idea is not to duplicate netstat, but to make available enough
790 	 * information for further analysis.
791 	 */
792 	switch(dom.dom_family) {
793 	case AF_INET:
794 	case AF_INET6:
795 		getinetproto(proto.pr_protocol);
796 		if (proto.pr_protocol == IPPROTO_TCP ) {
797 			if (so.so_pcb) {
798 				if (kvm_read(kd, (u_long)so.so_pcb,
799 				    (char *)&inpcb, sizeof(struct inpcb))
800 				    != sizeof(struct inpcb)) {
801 					dprintf(stderr,
802 					    "can't read inpcb at %p\n",
803 					    (void *)so.so_pcb);
804 					goto bad;
805 				}
806 				printf(" %lx", (u_long)inpcb.inp_ppcb);
807 			}
808 		}
809 		else if (so.so_pcb)
810 			printf(" %lx", (u_long)so.so_pcb);
811 		break;
812 	case AF_UNIX:
813 		/* print address of pcb and connected pcb */
814 		if (so.so_pcb) {
815 			printf(" %lx", (u_long)so.so_pcb);
816 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
817 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
818 				dprintf(stderr, "can't read unpcb at %p\n",
819 				    (void *)so.so_pcb);
820 				goto bad;
821 			}
822 			if (unpcb.unp_conn) {
823 				char shoconn[4], *cp;
824 
825 				cp = shoconn;
826 				if (!(so.so_state & SS_CANTRCVMORE))
827 					*cp++ = '<';
828 				*cp++ = '-';
829 				if (!(so.so_state & SS_CANTSENDMORE))
830 					*cp++ = '>';
831 				*cp = '\0';
832 				printf(" %s %lx", shoconn,
833 				    (u_long)unpcb.unp_conn);
834 			}
835 		}
836 		break;
837 	default:
838 		/* print protocol number and socket address */
839 		printf(" %d %lx", proto.pr_protocol, (u_long)sock);
840 	}
841 	printf("\n");
842 	return;
843 bad:
844 	printf("* error\n");
845 }
846 
847 
848 /*
849  * Read the specinfo structure in the kernel (as pointed to by a dev_t)
850  * in order to work out the associated udev_t
851  */
852 udev_t
853 dev2udev(dev_t dev)
854 {
855 	struct specinfo si;
856 
857 	if (KVM_READ(dev, &si, sizeof si)) {
858 		return si.si_udev;
859 	} else {
860 		dprintf(stderr, "can't convert dev_t %x to a udev_t\n", dev);
861 		return -1;
862 	}
863 }
864 
865 /*
866  * getinetproto --
867  *	print name of protocol number
868  */
869 void
870 getinetproto(int number)
871 {
872 	static int isopen;
873 	register struct protoent *pe;
874 
875 	if (!isopen)
876 		setprotoent(++isopen);
877 	if ((pe = getprotobynumber(number)) != NULL)
878 		printf(" %s", pe->p_name);
879 	else
880 		printf(" %d", number);
881 }
882 
883 int
884 getfname(char *filename)
885 {
886 	struct stat statbuf;
887 	DEVS *cur;
888 
889 	if (stat(filename, &statbuf)) {
890 		warn("%s", filename);
891 		return(0);
892 	}
893 	if ((cur = malloc(sizeof(DEVS))) == NULL)
894 		err(1, NULL);
895 	cur->next = devs;
896 	devs = cur;
897 
898 	cur->ino = statbuf.st_ino;
899 	cur->fsid = statbuf.st_dev;
900 	cur->name = filename;
901 	return(1);
902 }
903 
904 void
905 usage(void)
906 {
907 	(void)fprintf(stderr,
908  "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
909 	exit(1);
910 }
911