xref: /openbsd-src/usr.bin/fstat/fstat.c (revision 850e275390052b330d93020bf619a739a3c277ac)
1 /*	$OpenBSD: fstat.c,v 1.61 2008/04/08 14:46:45 thib Exp $	*/
2 
3 /*-
4  * Copyright (c) 1988, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31 
32 #ifndef lint
33 static char copyright[] =
34 "@(#) Copyright (c) 1988, 1993\n\
35 	The Regents of the University of California.  All rights reserved.\n";
36 #endif /* not lint */
37 
38 #ifndef lint
39 /*static char sccsid[] = "from: @(#)fstat.c	8.1 (Berkeley) 6/6/93";*/
40 static char *rcsid = "$OpenBSD: fstat.c,v 1.61 2008/04/08 14:46:45 thib Exp $";
41 #endif /* not lint */
42 
43 #include <sys/param.h>
44 #include <sys/time.h>
45 #include <sys/proc.h>
46 #include <sys/user.h>
47 #include <sys/stat.h>
48 #include <sys/vnode.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/domain.h>
52 #include <sys/protosw.h>
53 #include <sys/event.h>
54 #include <sys/eventvar.h>
55 #include <sys/unpcb.h>
56 #include <sys/sysctl.h>
57 #include <sys/filedesc.h>
58 #define	_KERNEL
59 #include <sys/mount.h>
60 #include <crypto/cryptodev.h>
61 #include <dev/systrace.h>
62 #include <sys/file.h>
63 #include <ufs/ufs/quota.h>
64 #include <ufs/ufs/inode.h>
65 #undef _KERNEL
66 #define NFS
67 #include <nfs/nfsproto.h>
68 #include <nfs/rpcv2.h>
69 #include <nfs/nfs.h>
70 #include <nfs/nfsnode.h>
71 #undef NFS
72 
73 #include <xfs/xfs_config.h>
74 #include <xfs/xfs_node.h>
75 
76 #include <miscfs/specfs/specdev.h>
77 
78 #include <net/route.h>
79 #include <netinet/in.h>
80 #include <netinet/in_systm.h>
81 #include <netinet/ip.h>
82 #include <netinet/in_pcb.h>
83 
84 #ifdef INET6
85 #include <netinet/ip6.h>
86 #include <netinet6/ip6_var.h>
87 #endif
88 
89 #include <netdb.h>
90 #include <arpa/inet.h>
91 
92 #include <sys/pipe.h>
93 
94 #include <ctype.h>
95 #include <errno.h>
96 #include <kvm.h>
97 #include <limits.h>
98 #include <nlist.h>
99 #include <paths.h>
100 #include <pwd.h>
101 #include <stdio.h>
102 #include <stdlib.h>
103 #include <string.h>
104 #include <unistd.h>
105 #include <netdb.h>
106 #include <err.h>
107 #include "fstat.h"
108 
109 #define	TEXT	-1
110 #define	CDIR	-2
111 #define	RDIR	-3
112 #define	TRACE	-4
113 
114 typedef struct devs {
115 	struct	devs *next;
116 	long	fsid;
117 	ino_t	ino;
118 	char	*name;
119 } DEVS;
120 DEVS *devs;
121 
122 int	fsflg;	/* show files on same filesystem as file(s) argument */
123 int	pflg;	/* show files open by a particular pid */
124 int	uflg;	/* show files open by a particular (effective) user */
125 int	checkfile; /* true if restricting to particular files or filesystems */
126 int	nflg;	/* (numerical) display f.s. and rdev as dev_t */
127 int	oflg;	/* display file offset */
128 int	sflg;	/* display file xfer/bytes counters */
129 int	vflg;	/* display errors in locating kernel data objects etc... */
130 
131 struct file **ofiles;	/* buffer of pointers to file structures */
132 int maxfiles;
133 #define ALLOC_OFILES(d)	\
134 	if ((d) > maxfiles) { \
135 		free(ofiles); \
136 		ofiles = calloc((d), sizeof(struct file *)); \
137 		if (ofiles == NULL) \
138 			err(1, "malloc"); \
139 		maxfiles = (d); \
140 	}
141 
142 /*
143  * a kvm_read that returns true if everything is read
144  */
145 #define KVM_READ(kaddr, paddr, len) \
146 	(kvm_read(kd, (u_long)(kaddr), (void *)(paddr), (len)) == (len))
147 
148 kvm_t *kd;
149 
150 int ufs_filestat(struct vnode *, struct filestat *);
151 int ext2fs_filestat(struct vnode *, struct filestat *);
152 int isofs_filestat(struct vnode *, struct filestat *);
153 int msdos_filestat(struct vnode *, struct filestat *);
154 int nfs_filestat(struct vnode *, struct filestat *);
155 int xfs_filestat(struct vnode *, struct filestat *);
156 int spec_filestat(struct vnode *, struct filestat *);
157 void dofiles(struct kinfo_proc2 *);
158 void getinetproto(int);
159 void usage(void);
160 int getfname(char *);
161 void socktrans(struct socket *, int, struct file *);
162 void vtrans(struct vnode *, int, int, struct file *);
163 void pipetrans(struct pipe *, int, struct file *);
164 void kqueuetrans(struct kqueue *, int, struct file *);
165 void cryptotrans(void *, int, struct file *);
166 void systracetrans(struct fsystrace *, int, struct file *);
167 char *getmnton(struct mount *);
168 const char *inet6_addrstr(struct in6_addr *);
169 
170 int
171 main(int argc, char *argv[])
172 {
173 	extern char *optarg;
174 	extern int optind;
175 	struct passwd *passwd;
176 	struct kinfo_proc2 *p, *plast;
177 	int arg, ch, what;
178 	char *memf, *nlistf;
179 	char buf[_POSIX2_LINE_MAX];
180 	const char *errstr;
181 	int cnt;
182 	gid_t gid;
183 
184 	arg = 0;
185 	what = KERN_PROC_ALL;
186 	nlistf = memf = NULL;
187 	oflg = 0;
188 	while ((ch = getopt(argc, argv, "fnop:su:vN:M:")) != -1)
189 		switch ((char)ch) {
190 		case 'f':
191 			fsflg = 1;
192 			break;
193 		case 'M':
194 			memf = optarg;
195 			break;
196 		case 'N':
197 			nlistf = optarg;
198 			break;
199 		case 'n':
200 			nflg = 1;
201 			break;
202 		case 'o':
203 			oflg = 1;
204 			break;
205 		case 'p':
206 			if (pflg++)
207 				usage();
208 			arg = strtonum(optarg, 0, INT_MAX, &errstr);
209 			if (errstr != NULL) {
210 				warnx("-p requires a process id, %s: %s",
211 					errstr, optarg);
212 				usage();
213 			}
214 			what = KERN_PROC_PID;
215 			break;
216 		case 's':
217 			sflg = 1;
218 			break;
219 		case 'u':
220 			if (uflg++)
221 				usage();
222 			if (!(passwd = getpwnam(optarg)))
223 				errx(1, "%s: unknown uid", optarg);
224 			what = KERN_PROC_UID;
225 			arg = passwd->pw_uid;
226 			break;
227 		case 'v':
228 			vflg = 1;
229 			break;
230 		default:
231 			usage();
232 		}
233 
234 	/*
235 	 * Discard setgid privileges if not the running kernel so that bad
236 	 * guys can't print interesting stuff from kernel memory.
237 	 */
238 	gid = getgid();
239 	if (nlistf != NULL || memf != NULL)
240 		if (setresgid(gid, gid, gid) == -1)
241 			err(1, "setresgid");
242 
243 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
244 		errx(1, "%s", buf);
245 
246 	if (nlistf == NULL && memf == NULL)
247 		if (setresgid(gid, gid, gid) == -1)
248 			err(1, "setresgid");
249 
250 	if (*(argv += optind)) {
251 		for (; *argv; ++argv) {
252 			if (getfname(*argv))
253 				checkfile = 1;
254 		}
255 		if (!checkfile)	/* file(s) specified, but none accessible */
256 			exit(1);
257 	}
258 
259 	ALLOC_OFILES(256);	/* reserve space for file pointers */
260 
261 	if (fsflg && !checkfile) {
262 		/* -f with no files means use wd */
263 		if (getfname(".") == 0)
264 			exit(1);
265 		checkfile = 1;
266 	}
267 
268 	if ((p = kvm_getproc2(kd, what, arg, sizeof(*p), &cnt)) == NULL)
269 		errx(1, "%s", kvm_geterr(kd));
270 	if (nflg)
271 		printf("%s",
272 "USER     CMD          PID   FD  DEV      INUM       MODE R/W    SZ|DV");
273 	else
274 		printf("%s",
275 "USER     CMD          PID   FD MOUNT        INUM MODE       R/W    SZ|DV");
276 	if (oflg)
277 		printf("%s", ":OFFSET  ");
278 	if (checkfile && fsflg == 0)
279 		printf(" NAME");
280 	if (sflg)
281 		printf("    XFERS   KBYTES");
282 	putchar('\n');
283 
284 	for (plast = &p[cnt]; p < plast; ++p) {
285 		if (p->p_stat == SZOMB)
286 			continue;
287 		dofiles(p);
288 	}
289 	exit(0);
290 }
291 
292 char	*Uname, *Comm;
293 pid_t	Pid;
294 
295 #define PREFIX(i) do { \
296 	printf("%-8.8s %-10s %5ld", Uname, Comm, (long)Pid); \
297 	switch (i) { \
298 	case TEXT: \
299 		printf(" text"); \
300 		break; \
301 	case CDIR: \
302 		printf("   wd"); \
303 		break; \
304 	case RDIR: \
305 		printf(" root"); \
306 		break; \
307 	case TRACE: \
308 		printf("   tr"); \
309 		break; \
310 	default: \
311 		printf(" %4d", i); \
312 		break; \
313 	} \
314 } while (0)
315 
316 /*
317  * print open files attributed to this process
318  */
319 void
320 dofiles(struct kinfo_proc2 *kp)
321 {
322 	int i;
323 	struct file file;
324 	struct filedesc0 filed0;
325 #define	filed	filed0.fd_fd
326 
327 	Uname = user_from_uid(kp->p_uid, 0);
328 	Pid = kp->p_pid;
329 	Comm = kp->p_comm;
330 
331 	if (kp->p_fd == 0)
332 		return;
333 	if (!KVM_READ(kp->p_fd, &filed0, sizeof (filed0))) {
334 		dprintf("can't read filedesc at %p for pid %ld",
335 		    (void *)(u_long)kp->p_fd, (long)Pid);
336 		return;
337 	}
338 	if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
339 	    filed.fd_freefile > filed.fd_lastfile + 1) {
340 		dprintf("filedesc corrupted at %p for pid %ld",
341 		    (void *)(u_long)kp->p_fd, (long)Pid);
342 		return;
343 	}
344 	/*
345 	 * root directory vnode, if one
346 	 */
347 	if (filed.fd_rdir)
348 		vtrans(filed.fd_rdir, RDIR, FREAD, NULL);
349 	/*
350 	 * current working directory vnode
351 	 */
352 	vtrans(filed.fd_cdir, CDIR, FREAD, NULL);
353 
354 	/*
355 	 * ktrace vnode, if one
356 	 */
357 	if (kp->p_tracep)
358 		vtrans((struct vnode *)(u_long)kp->p_tracep, TRACE, FREAD|FWRITE, NULL);
359 	/*
360 	 * open files
361 	 */
362 #define FPSIZE	(sizeof (struct file *))
363 	ALLOC_OFILES(filed.fd_lastfile+1);
364 	if (filed.fd_nfiles > NDFILE) {
365 		if (!KVM_READ(filed.fd_ofiles, ofiles,
366 		    (filed.fd_lastfile+1) * FPSIZE)) {
367 			dprintf("can't read file structures at %p for pid %ld",
368 			    filed.fd_ofiles, (long)Pid);
369 			return;
370 		}
371 	} else
372 		bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
373 	for (i = 0; i <= filed.fd_lastfile; i++) {
374 		if (ofiles[i] == NULL)
375 			continue;
376 		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
377 			dprintf("can't read file %d at %p for pid %ld",
378 				i, ofiles[i], (long)Pid);
379 			continue;
380 		}
381 		if (file.f_type == DTYPE_VNODE)
382 			vtrans((struct vnode *)file.f_data, i, file.f_flag,
383 			    &file);
384 		else if (file.f_type == DTYPE_SOCKET) {
385 			if (checkfile == 0)
386 				socktrans((struct socket *)file.f_data, i,
387 				    &file);
388 		} else if (file.f_type == DTYPE_PIPE) {
389 			if (checkfile == 0)
390 				pipetrans((struct pipe *)file.f_data, i, &file);
391 		} else if (file.f_type == DTYPE_KQUEUE) {
392 			if (checkfile == 0)
393 				kqueuetrans((struct kqueue *)file.f_data, i,
394 				    &file);
395 		} else if (file.f_type == DTYPE_CRYPTO) {
396 			if (checkfile == 0)
397 				cryptotrans(file.f_data, i, &file);
398 		} else if (file.f_type == DTYPE_SYSTRACE) {
399 			if (checkfile == 0)
400 				systracetrans((struct fsystrace *)file.f_data,
401 				    i, &file);
402 		} else {
403 			dprintf("unknown file type %d for file %d of pid %ld",
404 				file.f_type, i, (long)Pid);
405 		}
406 	}
407 }
408 
409 void
410 vtrans(struct vnode *vp, int i, int flag, struct file *fp)
411 {
412 	struct vnode vn;
413 	struct filestat fst;
414 	char rw[3], mode[17];
415 	char *badtype = NULL, *filename;
416 
417 	filename = badtype = NULL;
418 	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
419 		dprintf("can't read vnode at %p for pid %ld", vp, (long)Pid);
420 		return;
421 	}
422 	if (vn.v_type == VNON)
423 		badtype = "none";
424 	else if (vn.v_type == VBAD)
425 		badtype = "bad";
426 	else
427 		switch (vn.v_tag) {
428 		case VT_UFS:
429 		case VT_MFS:
430 			if (!ufs_filestat(&vn, &fst))
431 				badtype = "error";
432 			break;
433 		case VT_NFS:
434 			if (!nfs_filestat(&vn, &fst))
435 				badtype = "error";
436 			break;
437 		case VT_EXT2FS:
438 			if (!ext2fs_filestat(&vn, &fst))
439 				badtype = "error";
440 			break;
441 		case VT_ISOFS:
442 			if (!isofs_filestat(&vn, &fst))
443 				badtype = "error";
444 			break;
445 		case VT_MSDOSFS:
446 			if (!msdos_filestat(&vn, &fst))
447 				badtype = "error";
448 			break;
449 		case VT_XFS:
450 			if (!xfs_filestat(&vn, &fst))
451 				badtype = "error";
452 			break;
453 		case VT_NON:
454 			if (vn.v_flag & VCLONE) {
455 				if (!spec_filestat(&vn, &fst))
456 					badtype = "error";
457 			} else {
458 				badtype = "none";	/* not a clone */
459 			}
460 			break;
461 		default: {
462 			static char unknown[30];
463 			snprintf(badtype = unknown, sizeof unknown,
464 			    "?(%x)", vn.v_tag);
465 			break;
466 		}
467 	}
468 	if (checkfile) {
469 		int fsmatch = 0;
470 		DEVS *d;
471 
472 		if (badtype)
473 			return;
474 		for (d = devs; d != NULL; d = d->next)
475 			if (d->fsid == fst.fsid) {
476 				fsmatch = 1;
477 				if (d->ino == fst.fileid) {
478 					filename = d->name;
479 					break;
480 				}
481 			}
482 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
483 			return;
484 	}
485 	PREFIX(i);
486 	if (badtype) {
487 		(void)printf(" -           -  %10s    -\n", badtype);
488 		return;
489 	}
490 
491 	if (nflg)
492 		(void)printf(" %2ld,%-2ld", (long)major(fst.fsid),
493 		    (long)minor(fst.fsid));
494 	else if (!(vn.v_flag & VCLONE))
495 		(void)printf(" %-8s", getmnton(vn.v_mount));
496 	else
497 		(void)printf(" clone");
498 	if (nflg)
499 		(void)snprintf(mode, sizeof mode, "%o", fst.mode);
500 	else
501 		strmode(fst.mode, mode);
502 	(void)printf(" %8ld %11s", fst.fileid, mode);
503 	rw[0] = '\0';
504 	if (flag & FREAD)
505 		strlcat(rw, "r", sizeof rw);
506 	if (flag & FWRITE)
507 		strlcat(rw, "w", sizeof rw);
508 	printf(" %2s", rw);
509 	switch (vn.v_type) {
510 	case VBLK:
511 	case VCHR: {
512 		char *name;
513 
514 		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
515 		    S_IFCHR : S_IFBLK)) == NULL))
516 			printf("   %2d,%-3d", major(fst.rdev), minor(fst.rdev));
517 		else
518 			printf("  %7s", name);
519 		if (oflg)
520 			printf("         ");
521 		break;
522 	}
523 	default:
524 		printf(" %8lld", (long long)fst.size);
525 		if (oflg)
526 			printf(":%-8lld", (long long)(fp? fp->f_offset : 0));
527 	}
528 	if (sflg)
529 		printf(" %8lld %8lld",
530 		    (long long)(fp? fp->f_rxfer + fp->f_wxfer : 0),
531 		    (long long)(fp? fp->f_rbytes + fp->f_wbytes : 0) / 1024);
532 	if (filename && !fsflg)
533 		printf(" %s", filename);
534 	putchar('\n');
535 }
536 
537 int
538 ufs_filestat(struct vnode *vp, struct filestat *fsp)
539 {
540 	struct inode inode;
541 	struct ufs1_dinode di1;
542 
543 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
544 		dprintf("can't read inode at %p for pid %ld",
545 		    VTOI(vp), (long)Pid);
546 		return 0;
547 	}
548 
549 	if (!KVM_READ(inode.i_din1, &di1, sizeof(struct ufs1_dinode))) {
550 		dprintf("can't read dinode at %p for pid %ld",
551 		    inode.i_din1, (long)Pid);
552 		return (0);
553 	}
554 
555 	inode.i_din1 = &di1;
556 
557 	fsp->fsid = inode.i_dev & 0xffff;
558 	fsp->fileid = (long)inode.i_number;
559 	fsp->mode = inode.i_ffs1_mode;
560 	fsp->size = inode.i_ffs1_size;
561 	fsp->rdev = inode.i_ffs1_rdev;
562 
563 	return 1;
564 }
565 
566 int
567 ext2fs_filestat(struct vnode *vp, struct filestat *fsp)
568 {
569 	struct inode inode;
570 	struct ext2fs_dinode e2di;
571 
572 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
573 		dprintf("can't read inode at %p for pid %ld",
574 		    VTOI(vp), (long)Pid);
575 		return 0;
576 	}
577 
578 	if (!KVM_READ(inode.i_e2din, &e2di, sizeof(struct ext2fs_dinode))) {
579 		dprintf("can't read dinode at %p for pid %ld",
580 		    inode.i_e2din, (long)Pid);
581 		return (0);
582 	}
583 
584 	inode.i_e2din = &e2di;
585 
586 	fsp->fsid = inode.i_dev & 0xffff;
587 	fsp->fileid = (long)inode.i_number;
588 	fsp->mode = inode.i_e2fs_mode;
589 	fsp->size = inode.i_e2fs_size;
590 	fsp->rdev = 0;	/* XXX */
591 
592 	return 1;
593 }
594 
595 int
596 msdos_filestat(struct vnode *vp, struct filestat *fsp)
597 {
598 #if 0
599 	struct inode inode;
600 
601 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
602 		dprintf("can't read inode at %p for pid %ld",
603 		    VTOI(vp), (long)Pid);
604 		return 0;
605 	}
606 	fsp->fsid = inode.i_dev & 0xffff;
607 	fsp->fileid = (long)inode.i_number;
608 	fsp->mode = inode.i_e2fs_mode;
609 	fsp->size = inode.i_e2fs_size;
610 	fsp->rdev = 0;	/* XXX */
611 #endif
612 
613 	return 1;
614 }
615 
616 int
617 nfs_filestat(struct vnode *vp, struct filestat *fsp)
618 {
619 	struct nfsnode nfsnode;
620 	mode_t mode;
621 
622 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
623 		dprintf("can't read nfsnode at %p for pid %ld",
624 		    VTONFS(vp), (long)Pid);
625 		return 0;
626 	}
627 	fsp->fsid = nfsnode.n_vattr.va_fsid;
628 	fsp->fileid = nfsnode.n_vattr.va_fileid;
629 	fsp->size = nfsnode.n_size;
630 	fsp->rdev = nfsnode.n_vattr.va_rdev;
631 	mode = (mode_t)nfsnode.n_vattr.va_mode;
632 	switch (vp->v_type) {
633 	case VREG:
634 		mode |= S_IFREG;
635 		break;
636 	case VDIR:
637 		mode |= S_IFDIR;
638 		break;
639 	case VBLK:
640 		mode |= S_IFBLK;
641 		break;
642 	case VCHR:
643 		mode |= S_IFCHR;
644 		break;
645 	case VLNK:
646 		mode |= S_IFLNK;
647 		break;
648 	case VSOCK:
649 		mode |= S_IFSOCK;
650 		break;
651 	case VFIFO:
652 		mode |= S_IFIFO;
653 		break;
654 	default:
655 		break;
656 	}
657 	fsp->mode = mode;
658 
659 	return 1;
660 }
661 
662 int
663 xfs_filestat(struct vnode *vp, struct filestat *fsp)
664 {
665 	struct xfs_node xfs_node;
666 
667 	if (!KVM_READ(VNODE_TO_XNODE(vp), &xfs_node, sizeof (xfs_node))) {
668 		dprintf("can't read xfs_node at %p for pid %ld",
669 		    VTOI(vp), (long)Pid);
670 		return 0;
671 	}
672 	fsp->fsid = xfs_node.attr.va_fsid;
673 	fsp->fileid = (long)xfs_node.attr.va_fileid;
674 	fsp->mode = xfs_node.attr.va_mode;
675 	fsp->size = xfs_node.attr.va_size;
676 	fsp->rdev = xfs_node.attr.va_rdev;
677 
678 	return 1;
679 }
680 
681 int
682 spec_filestat(struct vnode *vp, struct filestat *fsp)
683 {
684 	struct specinfo		specinfo;
685 	struct vnode		parent;
686 
687 	if (!KVM_READ(vp->v_specinfo, &specinfo, sizeof(struct specinfo))) {
688 		dprintf("can't read specinfo at %p for pid %ld",
689 		     vp->v_specinfo, (long) Pid);
690 		return (0);
691 	}
692 
693 	vp->v_specinfo = &specinfo;
694 
695 	if (!KVM_READ(vp->v_specparent, &parent, sizeof(struct vnode))) {
696 		dprintf("can't read parent vnode at %p for pid %ld",
697 		     vp->v_specparent, (long) Pid);
698 		return (0);
699 	}
700 
701 	if (!ufs_filestat(&parent, fsp))
702 		return (0);
703 
704 	if (nflg)
705 		fsp->rdev = vp->v_rdev;
706 
707 	return (1);
708 }
709 
710 char *
711 getmnton(struct mount *m)
712 {
713 	static struct mount mount;
714 	static struct mtab {
715 		struct mtab *next;
716 		struct mount *m;
717 		char mntonname[MNAMELEN];
718 	} *mhead = NULL;
719 	struct mtab *mt;
720 
721 	for (mt = mhead; mt != NULL; mt = mt->next)
722 		if (m == mt->m)
723 			return (mt->mntonname);
724 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
725 		warn("can't read mount table at %p", m);
726 		return (NULL);
727 	}
728 	if ((mt = malloc(sizeof (struct mtab))) == NULL)
729 		err(1, "malloc");
730 	mt->m = m;
731 	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
732 	mt->next = mhead;
733 	mhead = mt;
734 	return (mt->mntonname);
735 }
736 
737 void
738 pipetrans(struct pipe *pipe, int i, struct file *fp)
739 {
740 	struct pipe pi;
741 	void *maxaddr;
742 
743 	PREFIX(i);
744 
745 	printf(" ");
746 
747 	/* fill in socket */
748 	if (!KVM_READ(pipe, &pi, sizeof(struct pipe))) {
749 		dprintf("can't read pipe at %p", pipe);
750 		goto bad;
751 	}
752 
753 	/*
754 	 * We don't have enough space to fit both peer and own address, so
755 	 * we select the higher address so both ends of the pipe have the
756 	 * same visible addr. (it's the higher address because when the other
757 	 * end closes, it becomes 0)
758 	 */
759 	maxaddr = MAX(pipe, pi.pipe_peer);
760 
761 	printf("pipe %p state: %s%s%s", maxaddr,
762 	    (pi.pipe_state & PIPE_WANTR) ? "R" : "",
763 	    (pi.pipe_state & PIPE_WANTW) ? "W" : "",
764 	    (pi.pipe_state & PIPE_EOF) ? "E" : "");
765 	if (sflg)
766 		printf("\t%8lld %8lld",
767 		    (long long)(fp? fp->f_rxfer + fp->f_wxfer : 0),
768 		    (long long)(fp? fp->f_rbytes + fp->f_wbytes : 0) / 1024);
769 	printf("\n");
770 	return;
771 bad:
772 	printf("* error\n");
773 }
774 
775 void
776 kqueuetrans(struct kqueue *kq, int i, struct file *fp)
777 {
778 	struct kqueue kqi;
779 
780 	PREFIX(i);
781 
782 	printf(" ");
783 
784 	/* fill it in */
785 	if (!KVM_READ(kq, &kqi, sizeof(struct kqueue))) {
786 		dprintf("can't read kqueue at %p", kq);
787 		goto bad;
788 	}
789 
790 	printf("kqueue %p %d state: %s%s\n", kq, kqi.kq_count,
791 	    (kqi.kq_state & KQ_SEL) ? "S" : "",
792 	    (kqi.kq_state & KQ_SLEEP) ? "W" : "");
793 	return;
794 bad:
795 	printf("* error\n");
796 }
797 
798 void
799 cryptotrans(void *f, int i, struct file *fp)
800 {
801 	PREFIX(i);
802 
803 	printf(" ");
804 
805 	printf("crypto %p\n", f);
806 }
807 
808 void
809 systracetrans(struct fsystrace *f, int i, struct file *fp)
810 {
811 	struct fsystrace fi;
812 
813 	PREFIX(i);
814 
815 	printf(" ");
816 
817 	/* fill it in */
818 	if (!KVM_READ(f, &fi, sizeof(fi))) {
819 		dprintf("can't read fsystrace at %p", f);
820 		goto bad;
821 	}
822 
823 	printf("systrace %p npol %d\n", f, fi.npolicies);
824 	return;
825 bad:
826 	printf("* error\n");
827 }
828 
829 #ifdef INET6
830 const char *
831 inet6_addrstr(struct in6_addr *p)
832 {
833 	struct sockaddr_in6 sin6;
834 	static char hbuf[NI_MAXHOST];
835 	const int niflags = NI_NUMERICHOST;
836 
837 	memset(&sin6, 0, sizeof(sin6));
838 	sin6.sin6_family = AF_INET6;
839 	sin6.sin6_len = sizeof(struct sockaddr_in6);
840 	sin6.sin6_addr = *p;
841 	if (IN6_IS_ADDR_LINKLOCAL(p) &&
842 	    *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
843 		sin6.sin6_scope_id =
844 		    ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
845 		sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0;
846 	}
847 
848 	if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
849 	    hbuf, sizeof(hbuf), NULL, 0, niflags))
850 		return "invalid";
851 
852 	return hbuf;
853 }
854 #endif
855 
856 void
857 socktrans(struct socket *sock, int i, struct file *fp)
858 {
859 	static char *stypename[] = {
860 		"unused",	/* 0 */
861 		"stream",	/* 1 */
862 		"dgram",	/* 2 */
863 		"raw",		/* 3 */
864 		"rdm",		/* 4 */
865 		"seqpak"	/* 5 */
866 	};
867 #define	STYPEMAX 5
868 	struct socket	so;
869 	struct protosw	proto;
870 	struct domain	dom;
871 	struct inpcb	inpcb;
872 	struct unpcb	unpcb;
873 	int len;
874 	char dname[32];
875 #ifdef INET6
876 	char xaddrbuf[NI_MAXHOST + 2];
877 #endif
878 
879 	PREFIX(i);
880 
881 	/* fill in socket */
882 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
883 		dprintf("can't read sock at %p", sock);
884 		goto bad;
885 	}
886 
887 	/* fill in protosw entry */
888 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
889 		dprintf("can't read protosw at %p", so.so_proto);
890 		goto bad;
891 	}
892 
893 	/* fill in domain */
894 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
895 		dprintf("can't read domain at %p", proto.pr_domain);
896 		goto bad;
897 	}
898 
899 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
900 	    sizeof(dname) - 1)) != sizeof(dname) -1) {
901 		dprintf("can't read domain name at %p", dom.dom_name);
902 		dname[0] = '\0';
903 	} else
904 		dname[len] = '\0';
905 
906 	if ((u_short)so.so_type > STYPEMAX)
907 		printf("* %s ?%d", dname, so.so_type);
908 	else
909 		printf("* %s %s", dname, stypename[so.so_type]);
910 
911 	/*
912 	 * protocol specific formatting
913 	 *
914 	 * Try to find interesting things to print.  For tcp, the interesting
915 	 * thing is the address of the tcpcb, for udp and others, just the
916 	 * inpcb (socket pcb).  For unix domain, its the address of the socket
917 	 * pcb and the address of the connected pcb (if connected).  Otherwise
918 	 * just print the protocol number and address of the socket itself.
919 	 * The idea is not to duplicate netstat, but to make available enough
920 	 * information for further analysis.
921 	 */
922 	switch (dom.dom_family) {
923 	case AF_INET:
924 		getinetproto(proto.pr_protocol);
925 		if (proto.pr_protocol == IPPROTO_TCP) {
926 			if (so.so_pcb == NULL)
927 				break;
928 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
929 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
930 				dprintf("can't read inpcb at %p", so.so_pcb);
931 				goto bad;
932 			}
933 			printf(" %p", inpcb.inp_ppcb);
934 			printf(" %s:%d",
935 			    inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
936 			    inet_ntoa(inpcb.inp_laddr),
937 			    ntohs(inpcb.inp_lport));
938 			if (inpcb.inp_fport) {
939 				if (so.so_state & SS_CONNECTOUT)
940 					printf(" --> ");
941 				else
942 					printf(" <-- ");
943 				printf("%s:%d",
944 				    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
945 				    inet_ntoa(inpcb.inp_faddr),
946 				    ntohs(inpcb.inp_fport));
947 			}
948 		} else if (proto.pr_protocol == IPPROTO_UDP) {
949 			if (so.so_pcb == NULL)
950 				break;
951 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
952 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
953 				dprintf("can't read inpcb at %p", so.so_pcb);
954 				goto bad;
955 			}
956 			printf(" %s:%d",
957 			    inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
958 			    inet_ntoa(inpcb.inp_laddr),
959 			    ntohs(inpcb.inp_lport));
960 			if (inpcb.inp_fport)
961 				printf(" <-> %s:%d",
962 				    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
963 				    inet_ntoa(inpcb.inp_faddr),
964 				    ntohs(inpcb.inp_fport));
965 		} else if (so.so_pcb)
966 			printf(" %p", so.so_pcb);
967 		break;
968 #ifdef INET6
969 	case AF_INET6:
970 		getinetproto(proto.pr_protocol);
971 		if (proto.pr_protocol == IPPROTO_TCP) {
972 			if (so.so_pcb == NULL)
973 				break;
974 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
975 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
976 				dprintf("can't read inpcb at %p", so.so_pcb);
977 				goto bad;
978 			}
979 			printf(" %p", inpcb.inp_ppcb);
980 			snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
981 			    inet6_addrstr(&inpcb.inp_laddr6));
982 			printf(" %s:%d",
983 			    IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6) ? "*" :
984 			    xaddrbuf,
985 			    ntohs(inpcb.inp_lport));
986 			if (inpcb.inp_fport) {
987 				if (so.so_state & SS_CONNECTOUT)
988 					printf(" --> ");
989 				else
990 					printf(" <-- ");
991 				snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
992 				    inet6_addrstr(&inpcb.inp_faddr6));
993 				printf("%s:%d",
994 				    IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6) ? "*" :
995 				    xaddrbuf,
996 				    ntohs(inpcb.inp_fport));
997 			}
998 		} else if (proto.pr_protocol == IPPROTO_UDP) {
999 			if (so.so_pcb == NULL)
1000 				break;
1001 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
1002 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
1003 				dprintf("can't read inpcb at %p", so.so_pcb);
1004 				goto bad;
1005 			}
1006 			snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
1007 			    inet6_addrstr(&inpcb.inp_laddr6));
1008 			printf(" %s:%d",
1009 			    IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6) ? "*" :
1010 			    xaddrbuf,
1011 			    ntohs(inpcb.inp_lport));
1012 			if (inpcb.inp_fport) {
1013 				snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
1014 				    inet6_addrstr(&inpcb.inp_faddr6));
1015 				printf(" <-> %s:%d",
1016 				    IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6) ? "*" :
1017 				    xaddrbuf,
1018 				    ntohs(inpcb.inp_fport));
1019 			}
1020 		} else if (so.so_pcb)
1021 			printf(" %p", so.so_pcb);
1022 		break;
1023 #endif
1024 	case AF_UNIX:
1025 		/* print address of pcb and connected pcb */
1026 		if (so.so_pcb) {
1027 			printf(" %p", so.so_pcb);
1028 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
1029 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
1030 				dprintf("can't read unpcb at %p", so.so_pcb);
1031 				goto bad;
1032 			}
1033 			if (unpcb.unp_conn) {
1034 				char shoconn[4], *cp;
1035 
1036 				cp = shoconn;
1037 				if (!(so.so_state & SS_CANTRCVMORE))
1038 					*cp++ = '<';
1039 				*cp++ = '-';
1040 				if (!(so.so_state & SS_CANTSENDMORE))
1041 					*cp++ = '>';
1042 				*cp = '\0';
1043 				printf(" %s %p", shoconn,
1044 				    unpcb.unp_conn);
1045 			}
1046 		}
1047 		break;
1048 	default:
1049 		/* print protocol number and socket address */
1050 		printf(" %d %p", proto.pr_protocol, sock);
1051 	}
1052 	if (sflg)
1053 		printf("\t%8lld %8lld",
1054 		    (long long)(fp? fp->f_rxfer + fp->f_wxfer : 0),
1055 		    (long long)(fp? fp->f_rbytes + fp->f_wbytes : 0) / 1024);
1056 	printf("\n");
1057 	return;
1058 bad:
1059 	printf("* error\n");
1060 }
1061 
1062 /*
1063  * getinetproto --
1064  *	print name of protocol number
1065  */
1066 void
1067 getinetproto(number)
1068 	int number;
1069 {
1070 	static int isopen;
1071 	struct protoent *pe;
1072 
1073 	if (!isopen)
1074 		setprotoent(++isopen);
1075 	if ((pe = getprotobynumber(number)) != NULL)
1076 		printf(" %s", pe->p_name);
1077 	else
1078 		printf(" %d", number);
1079 }
1080 
1081 int
1082 getfname(char *filename)
1083 {
1084 	struct stat statbuf;
1085 	DEVS *cur;
1086 
1087 	if (stat(filename, &statbuf)) {
1088 		warn("%s", filename);
1089 		return(0);
1090 	}
1091 	if ((cur = malloc(sizeof(DEVS))) == NULL)
1092 		err(1, "malloc");
1093 	cur->next = devs;
1094 	devs = cur;
1095 
1096 	cur->ino = statbuf.st_ino;
1097 	cur->fsid = statbuf.st_dev & 0xffff;
1098 	cur->name = filename;
1099 	return(1);
1100 }
1101 
1102 void
1103 usage(void)
1104 {
1105 	fprintf(stderr, "usage: fstat [-fnosv] [-M core] [-N system] "
1106 	    "[-p pid] [-u user] [file ...]\n");
1107 	exit(1);
1108 }
1109