xref: /openbsd-src/usr.bin/fstat/fstat.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: fstat.c,v 1.62 2008/10/07 02:20:12 deraadt 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.62 2008/10/07 02:20:12 deraadt 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 uid_t uid;
150 
151 int ufs_filestat(struct vnode *, struct filestat *);
152 int ext2fs_filestat(struct vnode *, struct filestat *);
153 int isofs_filestat(struct vnode *, struct filestat *);
154 int msdos_filestat(struct vnode *, struct filestat *);
155 int nfs_filestat(struct vnode *, struct filestat *);
156 int xfs_filestat(struct vnode *, struct filestat *);
157 int spec_filestat(struct vnode *, struct filestat *);
158 void dofiles(struct kinfo_proc2 *);
159 void getinetproto(int);
160 void usage(void);
161 int getfname(char *);
162 void socktrans(struct socket *, int, struct file *);
163 void vtrans(struct vnode *, int, int, struct file *);
164 void pipetrans(struct pipe *, int, struct file *);
165 void kqueuetrans(struct kqueue *, int, struct file *);
166 void cryptotrans(void *, int, struct file *);
167 void systracetrans(struct fsystrace *, int, struct file *);
168 char *getmnton(struct mount *);
169 const char *inet6_addrstr(struct in6_addr *);
170 
171 int
172 main(int argc, char *argv[])
173 {
174 	extern char *optarg;
175 	extern int optind;
176 	struct passwd *passwd;
177 	struct kinfo_proc2 *p, *plast;
178 	int arg, ch, what;
179 	char *memf, *nlistf;
180 	char buf[_POSIX2_LINE_MAX];
181 	const char *errstr;
182 	int cnt;
183 	gid_t gid;
184 
185 	arg = 0;
186 	what = KERN_PROC_ALL;
187 	nlistf = memf = NULL;
188 	oflg = 0;
189 	while ((ch = getopt(argc, argv, "fnop:su:vN:M:")) != -1)
190 		switch ((char)ch) {
191 		case 'f':
192 			fsflg = 1;
193 			break;
194 		case 'M':
195 			memf = optarg;
196 			break;
197 		case 'N':
198 			nlistf = optarg;
199 			break;
200 		case 'n':
201 			nflg = 1;
202 			break;
203 		case 'o':
204 			oflg = 1;
205 			break;
206 		case 'p':
207 			if (pflg++)
208 				usage();
209 			arg = strtonum(optarg, 0, INT_MAX, &errstr);
210 			if (errstr != NULL) {
211 				warnx("-p requires a process id, %s: %s",
212 					errstr, optarg);
213 				usage();
214 			}
215 			what = KERN_PROC_PID;
216 			break;
217 		case 's':
218 			sflg = 1;
219 			break;
220 		case 'u':
221 			if (uflg++)
222 				usage();
223 			if (!(passwd = getpwnam(optarg)))
224 				errx(1, "%s: unknown uid", optarg);
225 			what = KERN_PROC_UID;
226 			arg = passwd->pw_uid;
227 			break;
228 		case 'v':
229 			vflg = 1;
230 			break;
231 		default:
232 			usage();
233 		}
234 
235 	/*
236 	 * get the uid, for oflg and sflg
237 	 */
238 
239 	uid = getuid();
240 
241 	/*
242 	 * Discard setgid privileges if not the running kernel so that bad
243 	 * guys can't print interesting stuff from kernel memory.
244 	 */
245 	gid = getgid();
246 	if (nlistf != NULL || memf != NULL)
247 		if (setresgid(gid, gid, gid) == -1)
248 			err(1, "setresgid");
249 
250 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
251 		errx(1, "%s", buf);
252 
253 	if (nlistf == NULL && memf == NULL)
254 		if (setresgid(gid, gid, gid) == -1)
255 			err(1, "setresgid");
256 
257 	if (*(argv += optind)) {
258 		for (; *argv; ++argv) {
259 			if (getfname(*argv))
260 				checkfile = 1;
261 		}
262 		if (!checkfile)	/* file(s) specified, but none accessible */
263 			exit(1);
264 	}
265 
266 	ALLOC_OFILES(256);	/* reserve space for file pointers */
267 
268 	if (fsflg && !checkfile) {
269 		/* -f with no files means use wd */
270 		if (getfname(".") == 0)
271 			exit(1);
272 		checkfile = 1;
273 	}
274 
275 	if ((p = kvm_getproc2(kd, what, arg, sizeof(*p), &cnt)) == NULL)
276 		errx(1, "%s", kvm_geterr(kd));
277 	if (nflg)
278 		printf("%s",
279 "USER     CMD          PID   FD  DEV      INUM       MODE R/W    SZ|DV");
280 	else
281 		printf("%s",
282 "USER     CMD          PID   FD MOUNT        INUM MODE       R/W    SZ|DV");
283 	if (oflg)
284 		printf("%s", ":OFFSET  ");
285 	if (checkfile && fsflg == 0)
286 		printf(" NAME");
287 	if (sflg)
288 		printf("    XFERS   KBYTES");
289 	putchar('\n');
290 
291 	for (plast = &p[cnt]; p < plast; ++p) {
292 		if (p->p_stat == SZOMB)
293 			continue;
294 		dofiles(p);
295 	}
296 	exit(0);
297 }
298 
299 char	*Uname, *Comm;
300 uid_t	*procuid;
301 pid_t	Pid;
302 
303 #define PREFIX(i) do { \
304 	printf("%-8.8s %-10s %5ld", Uname, Comm, (long)Pid); \
305 	switch (i) { \
306 	case TEXT: \
307 		printf(" text"); \
308 		break; \
309 	case CDIR: \
310 		printf("   wd"); \
311 		break; \
312 	case RDIR: \
313 		printf(" root"); \
314 		break; \
315 	case TRACE: \
316 		printf("   tr"); \
317 		break; \
318 	default: \
319 		printf(" %4d", i); \
320 		break; \
321 	} \
322 } while (0)
323 
324 /*
325  * print open files attributed to this process
326  */
327 void
328 dofiles(struct kinfo_proc2 *kp)
329 {
330 	int i;
331 	struct file file;
332 	struct filedesc0 filed0;
333 #define	filed	filed0.fd_fd
334 
335 	Uname = user_from_uid(kp->p_uid, 0);
336 	procuid = &kp->p_uid;
337 	Pid = kp->p_pid;
338 	Comm = kp->p_comm;
339 
340 	if (kp->p_fd == 0)
341 		return;
342 	if (!KVM_READ(kp->p_fd, &filed0, sizeof (filed0))) {
343 		dprintf("can't read filedesc at %p for pid %ld",
344 		    (void *)(u_long)kp->p_fd, (long)Pid);
345 		return;
346 	}
347 	if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
348 	    filed.fd_freefile > filed.fd_lastfile + 1) {
349 		dprintf("filedesc corrupted at %p for pid %ld",
350 		    (void *)(u_long)kp->p_fd, (long)Pid);
351 		return;
352 	}
353 	/*
354 	 * root directory vnode, if one
355 	 */
356 	if (filed.fd_rdir)
357 		vtrans(filed.fd_rdir, RDIR, FREAD, NULL);
358 	/*
359 	 * current working directory vnode
360 	 */
361 	vtrans(filed.fd_cdir, CDIR, FREAD, NULL);
362 
363 	/*
364 	 * ktrace vnode, if one
365 	 */
366 	if (kp->p_tracep)
367 		vtrans((struct vnode *)(u_long)kp->p_tracep, TRACE, FREAD|FWRITE, NULL);
368 	/*
369 	 * open files
370 	 */
371 #define FPSIZE	(sizeof (struct file *))
372 	ALLOC_OFILES(filed.fd_lastfile+1);
373 	if (filed.fd_nfiles > NDFILE) {
374 		if (!KVM_READ(filed.fd_ofiles, ofiles,
375 		    (filed.fd_lastfile+1) * FPSIZE)) {
376 			dprintf("can't read file structures at %p for pid %ld",
377 			    filed.fd_ofiles, (long)Pid);
378 			return;
379 		}
380 	} else
381 		bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
382 	for (i = 0; i <= filed.fd_lastfile; i++) {
383 		if (ofiles[i] == NULL)
384 			continue;
385 		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
386 			dprintf("can't read file %d at %p for pid %ld",
387 				i, ofiles[i], (long)Pid);
388 			continue;
389 		}
390 		if (file.f_type == DTYPE_VNODE)
391 			vtrans((struct vnode *)file.f_data, i, file.f_flag,
392 			    &file);
393 		else if (file.f_type == DTYPE_SOCKET) {
394 			if (checkfile == 0)
395 				socktrans((struct socket *)file.f_data, i,
396 				    &file);
397 		} else if (file.f_type == DTYPE_PIPE) {
398 			if (checkfile == 0)
399 				pipetrans((struct pipe *)file.f_data, i, &file);
400 		} else if (file.f_type == DTYPE_KQUEUE) {
401 			if (checkfile == 0)
402 				kqueuetrans((struct kqueue *)file.f_data, i,
403 				    &file);
404 		} else if (file.f_type == DTYPE_CRYPTO) {
405 			if (checkfile == 0)
406 				cryptotrans(file.f_data, i, &file);
407 		} else if (file.f_type == DTYPE_SYSTRACE) {
408 			if (checkfile == 0)
409 				systracetrans((struct fsystrace *)file.f_data,
410 				    i, &file);
411 		} else {
412 			dprintf("unknown file type %d for file %d of pid %ld",
413 				file.f_type, i, (long)Pid);
414 		}
415 	}
416 }
417 
418 void
419 vtrans(struct vnode *vp, int i, int flag, struct file *fp)
420 {
421 	struct vnode vn;
422 	struct filestat fst;
423 	char rw[3], mode[17];
424 	char *badtype = NULL, *filename;
425 
426 	filename = badtype = NULL;
427 	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
428 		dprintf("can't read vnode at %p for pid %ld", vp, (long)Pid);
429 		return;
430 	}
431 	if (vn.v_type == VNON)
432 		badtype = "none";
433 	else if (vn.v_type == VBAD)
434 		badtype = "bad";
435 	else
436 		switch (vn.v_tag) {
437 		case VT_UFS:
438 		case VT_MFS:
439 			if (!ufs_filestat(&vn, &fst))
440 				badtype = "error";
441 			break;
442 		case VT_NFS:
443 			if (!nfs_filestat(&vn, &fst))
444 				badtype = "error";
445 			break;
446 		case VT_EXT2FS:
447 			if (!ext2fs_filestat(&vn, &fst))
448 				badtype = "error";
449 			break;
450 		case VT_ISOFS:
451 			if (!isofs_filestat(&vn, &fst))
452 				badtype = "error";
453 			break;
454 		case VT_MSDOSFS:
455 			if (!msdos_filestat(&vn, &fst))
456 				badtype = "error";
457 			break;
458 		case VT_XFS:
459 			if (!xfs_filestat(&vn, &fst))
460 				badtype = "error";
461 			break;
462 		case VT_NON:
463 			if (vn.v_flag & VCLONE) {
464 				if (!spec_filestat(&vn, &fst))
465 					badtype = "error";
466 			} else {
467 				badtype = "none";	/* not a clone */
468 			}
469 			break;
470 		default: {
471 			static char unknown[30];
472 			snprintf(badtype = unknown, sizeof unknown,
473 			    "?(%x)", vn.v_tag);
474 			break;
475 		}
476 	}
477 	if (checkfile) {
478 		int fsmatch = 0;
479 		DEVS *d;
480 
481 		if (badtype)
482 			return;
483 		for (d = devs; d != NULL; d = d->next)
484 			if (d->fsid == fst.fsid) {
485 				fsmatch = 1;
486 				if (d->ino == fst.fileid) {
487 					filename = d->name;
488 					break;
489 				}
490 			}
491 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
492 			return;
493 	}
494 	PREFIX(i);
495 	if (badtype) {
496 		(void)printf(" -           -  %10s    -\n", badtype);
497 		return;
498 	}
499 
500 	if (nflg)
501 		(void)printf(" %2ld,%-2ld", (long)major(fst.fsid),
502 		    (long)minor(fst.fsid));
503 	else if (!(vn.v_flag & VCLONE))
504 		(void)printf(" %-8s", getmnton(vn.v_mount));
505 	else
506 		(void)printf(" clone");
507 	if (nflg)
508 		(void)snprintf(mode, sizeof mode, "%o", fst.mode);
509 	else
510 		strmode(fst.mode, mode);
511 	(void)printf(" %8ld %11s", fst.fileid, mode);
512 	rw[0] = '\0';
513 	if (flag & FREAD)
514 		strlcat(rw, "r", sizeof rw);
515 	if (flag & FWRITE)
516 		strlcat(rw, "w", sizeof rw);
517 	printf(" %2s", rw);
518 	switch (vn.v_type) {
519 	case VBLK:
520 	case VCHR: {
521 		char *name;
522 
523 		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
524 		    S_IFCHR : S_IFBLK)) == NULL))
525 			printf("   %2d,%-3d", major(fst.rdev), minor(fst.rdev));
526 		else
527 			printf("  %7s", name);
528 		if (oflg)
529 			printf("         ");
530 		break;
531 	}
532 	default:
533 		printf(" %8lld", (long long)fst.size);
534 		if (oflg) {
535 			if (uid == 0 || uid == *procuid)
536 				printf(":%-8lld", (long long)(fp? fp->f_offset : 0));
537 			else
538 				printf(":%-8s", "*");
539 		}
540 	}
541 	if (sflg) {
542 		if (uid == 0 || uid == *procuid) {
543 			printf(" %8lld %8lld",
544 		    	(long long)(fp? fp->f_rxfer + fp->f_wxfer : 0),
545 		    	(long long)(fp? fp->f_rbytes + fp->f_wbytes : 0) / 1024);
546 		} else {
547 			printf(" %8s %8s", "*", "*");
548 		}
549 	}
550 	if (filename && !fsflg)
551 		printf(" %s", filename);
552 	putchar('\n');
553 }
554 
555 int
556 ufs_filestat(struct vnode *vp, struct filestat *fsp)
557 {
558 	struct inode inode;
559 	struct ufs1_dinode di1;
560 
561 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
562 		dprintf("can't read inode at %p for pid %ld",
563 		    VTOI(vp), (long)Pid);
564 		return 0;
565 	}
566 
567 	if (!KVM_READ(inode.i_din1, &di1, sizeof(struct ufs1_dinode))) {
568 		dprintf("can't read dinode at %p for pid %ld",
569 		    inode.i_din1, (long)Pid);
570 		return (0);
571 	}
572 
573 	inode.i_din1 = &di1;
574 
575 	fsp->fsid = inode.i_dev & 0xffff;
576 	fsp->fileid = (long)inode.i_number;
577 	fsp->mode = inode.i_ffs1_mode;
578 	fsp->size = inode.i_ffs1_size;
579 	fsp->rdev = inode.i_ffs1_rdev;
580 
581 	return 1;
582 }
583 
584 int
585 ext2fs_filestat(struct vnode *vp, struct filestat *fsp)
586 {
587 	struct inode inode;
588 	struct ext2fs_dinode e2di;
589 
590 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
591 		dprintf("can't read inode at %p for pid %ld",
592 		    VTOI(vp), (long)Pid);
593 		return 0;
594 	}
595 
596 	if (!KVM_READ(inode.i_e2din, &e2di, sizeof(struct ext2fs_dinode))) {
597 		dprintf("can't read dinode at %p for pid %ld",
598 		    inode.i_e2din, (long)Pid);
599 		return (0);
600 	}
601 
602 	inode.i_e2din = &e2di;
603 
604 	fsp->fsid = inode.i_dev & 0xffff;
605 	fsp->fileid = (long)inode.i_number;
606 	fsp->mode = inode.i_e2fs_mode;
607 	fsp->size = inode.i_e2fs_size;
608 	fsp->rdev = 0;	/* XXX */
609 
610 	return 1;
611 }
612 
613 int
614 msdos_filestat(struct vnode *vp, struct filestat *fsp)
615 {
616 #if 0
617 	struct inode inode;
618 
619 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
620 		dprintf("can't read inode at %p for pid %ld",
621 		    VTOI(vp), (long)Pid);
622 		return 0;
623 	}
624 	fsp->fsid = inode.i_dev & 0xffff;
625 	fsp->fileid = (long)inode.i_number;
626 	fsp->mode = inode.i_e2fs_mode;
627 	fsp->size = inode.i_e2fs_size;
628 	fsp->rdev = 0;	/* XXX */
629 #endif
630 
631 	return 1;
632 }
633 
634 int
635 nfs_filestat(struct vnode *vp, struct filestat *fsp)
636 {
637 	struct nfsnode nfsnode;
638 	mode_t mode;
639 
640 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
641 		dprintf("can't read nfsnode at %p for pid %ld",
642 		    VTONFS(vp), (long)Pid);
643 		return 0;
644 	}
645 	fsp->fsid = nfsnode.n_vattr.va_fsid;
646 	fsp->fileid = nfsnode.n_vattr.va_fileid;
647 	fsp->size = nfsnode.n_size;
648 	fsp->rdev = nfsnode.n_vattr.va_rdev;
649 	mode = (mode_t)nfsnode.n_vattr.va_mode;
650 	switch (vp->v_type) {
651 	case VREG:
652 		mode |= S_IFREG;
653 		break;
654 	case VDIR:
655 		mode |= S_IFDIR;
656 		break;
657 	case VBLK:
658 		mode |= S_IFBLK;
659 		break;
660 	case VCHR:
661 		mode |= S_IFCHR;
662 		break;
663 	case VLNK:
664 		mode |= S_IFLNK;
665 		break;
666 	case VSOCK:
667 		mode |= S_IFSOCK;
668 		break;
669 	case VFIFO:
670 		mode |= S_IFIFO;
671 		break;
672 	default:
673 		break;
674 	}
675 	fsp->mode = mode;
676 
677 	return 1;
678 }
679 
680 int
681 xfs_filestat(struct vnode *vp, struct filestat *fsp)
682 {
683 	struct xfs_node xfs_node;
684 
685 	if (!KVM_READ(VNODE_TO_XNODE(vp), &xfs_node, sizeof (xfs_node))) {
686 		dprintf("can't read xfs_node at %p for pid %ld",
687 		    VTOI(vp), (long)Pid);
688 		return 0;
689 	}
690 	fsp->fsid = xfs_node.attr.va_fsid;
691 	fsp->fileid = (long)xfs_node.attr.va_fileid;
692 	fsp->mode = xfs_node.attr.va_mode;
693 	fsp->size = xfs_node.attr.va_size;
694 	fsp->rdev = xfs_node.attr.va_rdev;
695 
696 	return 1;
697 }
698 
699 int
700 spec_filestat(struct vnode *vp, struct filestat *fsp)
701 {
702 	struct specinfo		specinfo;
703 	struct vnode		parent;
704 
705 	if (!KVM_READ(vp->v_specinfo, &specinfo, sizeof(struct specinfo))) {
706 		dprintf("can't read specinfo at %p for pid %ld",
707 		     vp->v_specinfo, (long) Pid);
708 		return (0);
709 	}
710 
711 	vp->v_specinfo = &specinfo;
712 
713 	if (!KVM_READ(vp->v_specparent, &parent, sizeof(struct vnode))) {
714 		dprintf("can't read parent vnode at %p for pid %ld",
715 		     vp->v_specparent, (long) Pid);
716 		return (0);
717 	}
718 
719 	if (!ufs_filestat(&parent, fsp))
720 		return (0);
721 
722 	if (nflg)
723 		fsp->rdev = vp->v_rdev;
724 
725 	return (1);
726 }
727 
728 char *
729 getmnton(struct mount *m)
730 {
731 	static struct mount mount;
732 	static struct mtab {
733 		struct mtab *next;
734 		struct mount *m;
735 		char mntonname[MNAMELEN];
736 	} *mhead = NULL;
737 	struct mtab *mt;
738 
739 	for (mt = mhead; mt != NULL; mt = mt->next)
740 		if (m == mt->m)
741 			return (mt->mntonname);
742 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
743 		warn("can't read mount table at %p", m);
744 		return (NULL);
745 	}
746 	if ((mt = malloc(sizeof (struct mtab))) == NULL)
747 		err(1, "malloc");
748 	mt->m = m;
749 	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
750 	mt->next = mhead;
751 	mhead = mt;
752 	return (mt->mntonname);
753 }
754 
755 void
756 pipetrans(struct pipe *pipe, int i, struct file *fp)
757 {
758 	struct pipe pi;
759 	void *maxaddr;
760 
761 	PREFIX(i);
762 
763 	printf(" ");
764 
765 	/* fill in socket */
766 	if (!KVM_READ(pipe, &pi, sizeof(struct pipe))) {
767 		dprintf("can't read pipe at %p", pipe);
768 		goto bad;
769 	}
770 
771 	/*
772 	 * We don't have enough space to fit both peer and own address, so
773 	 * we select the higher address so both ends of the pipe have the
774 	 * same visible addr. (it's the higher address because when the other
775 	 * end closes, it becomes 0)
776 	 */
777 	maxaddr = MAX(pipe, pi.pipe_peer);
778 
779 	printf("pipe %p state: %s%s%s", maxaddr,
780 	    (pi.pipe_state & PIPE_WANTR) ? "R" : "",
781 	    (pi.pipe_state & PIPE_WANTW) ? "W" : "",
782 	    (pi.pipe_state & PIPE_EOF) ? "E" : "");
783 	if (sflg)
784 		printf("\t%8lld %8lld",
785 		    (long long)(fp? fp->f_rxfer + fp->f_wxfer : 0),
786 		    (long long)(fp? fp->f_rbytes + fp->f_wbytes : 0) / 1024);
787 	printf("\n");
788 	return;
789 bad:
790 	printf("* error\n");
791 }
792 
793 void
794 kqueuetrans(struct kqueue *kq, int i, struct file *fp)
795 {
796 	struct kqueue kqi;
797 
798 	PREFIX(i);
799 
800 	printf(" ");
801 
802 	/* fill it in */
803 	if (!KVM_READ(kq, &kqi, sizeof(struct kqueue))) {
804 		dprintf("can't read kqueue at %p", kq);
805 		goto bad;
806 	}
807 
808 	printf("kqueue %p %d state: %s%s\n", kq, kqi.kq_count,
809 	    (kqi.kq_state & KQ_SEL) ? "S" : "",
810 	    (kqi.kq_state & KQ_SLEEP) ? "W" : "");
811 	return;
812 bad:
813 	printf("* error\n");
814 }
815 
816 void
817 cryptotrans(void *f, int i, struct file *fp)
818 {
819 	PREFIX(i);
820 
821 	printf(" ");
822 
823 	printf("crypto %p\n", f);
824 }
825 
826 void
827 systracetrans(struct fsystrace *f, int i, struct file *fp)
828 {
829 	struct fsystrace fi;
830 
831 	PREFIX(i);
832 
833 	printf(" ");
834 
835 	/* fill it in */
836 	if (!KVM_READ(f, &fi, sizeof(fi))) {
837 		dprintf("can't read fsystrace at %p", f);
838 		goto bad;
839 	}
840 
841 	printf("systrace %p npol %d\n", f, fi.npolicies);
842 	return;
843 bad:
844 	printf("* error\n");
845 }
846 
847 #ifdef INET6
848 const char *
849 inet6_addrstr(struct in6_addr *p)
850 {
851 	struct sockaddr_in6 sin6;
852 	static char hbuf[NI_MAXHOST];
853 	const int niflags = NI_NUMERICHOST;
854 
855 	memset(&sin6, 0, sizeof(sin6));
856 	sin6.sin6_family = AF_INET6;
857 	sin6.sin6_len = sizeof(struct sockaddr_in6);
858 	sin6.sin6_addr = *p;
859 	if (IN6_IS_ADDR_LINKLOCAL(p) &&
860 	    *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
861 		sin6.sin6_scope_id =
862 		    ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
863 		sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0;
864 	}
865 
866 	if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
867 	    hbuf, sizeof(hbuf), NULL, 0, niflags))
868 		return "invalid";
869 
870 	return hbuf;
871 }
872 #endif
873 
874 void
875 socktrans(struct socket *sock, int i, struct file *fp)
876 {
877 	static char *stypename[] = {
878 		"unused",	/* 0 */
879 		"stream",	/* 1 */
880 		"dgram",	/* 2 */
881 		"raw",		/* 3 */
882 		"rdm",		/* 4 */
883 		"seqpak"	/* 5 */
884 	};
885 #define	STYPEMAX 5
886 	struct socket	so;
887 	struct protosw	proto;
888 	struct domain	dom;
889 	struct inpcb	inpcb;
890 	struct unpcb	unpcb;
891 	int len;
892 	char dname[32];
893 #ifdef INET6
894 	char xaddrbuf[NI_MAXHOST + 2];
895 #endif
896 
897 	PREFIX(i);
898 
899 	/* fill in socket */
900 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
901 		dprintf("can't read sock at %p", sock);
902 		goto bad;
903 	}
904 
905 	/* fill in protosw entry */
906 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
907 		dprintf("can't read protosw at %p", so.so_proto);
908 		goto bad;
909 	}
910 
911 	/* fill in domain */
912 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
913 		dprintf("can't read domain at %p", proto.pr_domain);
914 		goto bad;
915 	}
916 
917 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
918 	    sizeof(dname) - 1)) != sizeof(dname) -1) {
919 		dprintf("can't read domain name at %p", dom.dom_name);
920 		dname[0] = '\0';
921 	} else
922 		dname[len] = '\0';
923 
924 	if ((u_short)so.so_type > STYPEMAX)
925 		printf("* %s ?%d", dname, so.so_type);
926 	else
927 		printf("* %s %s", dname, stypename[so.so_type]);
928 
929 	/*
930 	 * protocol specific formatting
931 	 *
932 	 * Try to find interesting things to print.  For tcp, the interesting
933 	 * thing is the address of the tcpcb, for udp and others, just the
934 	 * inpcb (socket pcb).  For unix domain, its the address of the socket
935 	 * pcb and the address of the connected pcb (if connected).  Otherwise
936 	 * just print the protocol number and address of the socket itself.
937 	 * The idea is not to duplicate netstat, but to make available enough
938 	 * information for further analysis.
939 	 */
940 	switch (dom.dom_family) {
941 	case AF_INET:
942 		getinetproto(proto.pr_protocol);
943 		if (proto.pr_protocol == IPPROTO_TCP) {
944 			if (so.so_pcb == NULL)
945 				break;
946 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
947 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
948 				dprintf("can't read inpcb at %p", so.so_pcb);
949 				goto bad;
950 			}
951 			printf(" %p", inpcb.inp_ppcb);
952 			printf(" %s:%d",
953 			    inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
954 			    inet_ntoa(inpcb.inp_laddr),
955 			    ntohs(inpcb.inp_lport));
956 			if (inpcb.inp_fport) {
957 				if (so.so_state & SS_CONNECTOUT)
958 					printf(" --> ");
959 				else
960 					printf(" <-- ");
961 				printf("%s:%d",
962 				    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
963 				    inet_ntoa(inpcb.inp_faddr),
964 				    ntohs(inpcb.inp_fport));
965 			}
966 		} else if (proto.pr_protocol == IPPROTO_UDP) {
967 			if (so.so_pcb == NULL)
968 				break;
969 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
970 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
971 				dprintf("can't read inpcb at %p", so.so_pcb);
972 				goto bad;
973 			}
974 			printf(" %s:%d",
975 			    inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
976 			    inet_ntoa(inpcb.inp_laddr),
977 			    ntohs(inpcb.inp_lport));
978 			if (inpcb.inp_fport)
979 				printf(" <-> %s:%d",
980 				    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
981 				    inet_ntoa(inpcb.inp_faddr),
982 				    ntohs(inpcb.inp_fport));
983 		} else if (so.so_pcb)
984 			printf(" %p", so.so_pcb);
985 		break;
986 #ifdef INET6
987 	case AF_INET6:
988 		getinetproto(proto.pr_protocol);
989 		if (proto.pr_protocol == IPPROTO_TCP) {
990 			if (so.so_pcb == NULL)
991 				break;
992 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
993 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
994 				dprintf("can't read inpcb at %p", so.so_pcb);
995 				goto bad;
996 			}
997 			printf(" %p", inpcb.inp_ppcb);
998 			snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
999 			    inet6_addrstr(&inpcb.inp_laddr6));
1000 			printf(" %s:%d",
1001 			    IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6) ? "*" :
1002 			    xaddrbuf,
1003 			    ntohs(inpcb.inp_lport));
1004 			if (inpcb.inp_fport) {
1005 				if (so.so_state & SS_CONNECTOUT)
1006 					printf(" --> ");
1007 				else
1008 					printf(" <-- ");
1009 				snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
1010 				    inet6_addrstr(&inpcb.inp_faddr6));
1011 				printf("%s:%d",
1012 				    IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6) ? "*" :
1013 				    xaddrbuf,
1014 				    ntohs(inpcb.inp_fport));
1015 			}
1016 		} else if (proto.pr_protocol == IPPROTO_UDP) {
1017 			if (so.so_pcb == NULL)
1018 				break;
1019 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
1020 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
1021 				dprintf("can't read inpcb at %p", so.so_pcb);
1022 				goto bad;
1023 			}
1024 			snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
1025 			    inet6_addrstr(&inpcb.inp_laddr6));
1026 			printf(" %s:%d",
1027 			    IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_laddr6) ? "*" :
1028 			    xaddrbuf,
1029 			    ntohs(inpcb.inp_lport));
1030 			if (inpcb.inp_fport) {
1031 				snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]",
1032 				    inet6_addrstr(&inpcb.inp_faddr6));
1033 				printf(" <-> %s:%d",
1034 				    IN6_IS_ADDR_UNSPECIFIED(&inpcb.inp_faddr6) ? "*" :
1035 				    xaddrbuf,
1036 				    ntohs(inpcb.inp_fport));
1037 			}
1038 		} else if (so.so_pcb)
1039 			printf(" %p", so.so_pcb);
1040 		break;
1041 #endif
1042 	case AF_UNIX:
1043 		/* print address of pcb and connected pcb */
1044 		if (so.so_pcb) {
1045 			printf(" %p", so.so_pcb);
1046 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
1047 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
1048 				dprintf("can't read unpcb at %p", so.so_pcb);
1049 				goto bad;
1050 			}
1051 			if (unpcb.unp_conn) {
1052 				char shoconn[4], *cp;
1053 
1054 				cp = shoconn;
1055 				if (!(so.so_state & SS_CANTRCVMORE))
1056 					*cp++ = '<';
1057 				*cp++ = '-';
1058 				if (!(so.so_state & SS_CANTSENDMORE))
1059 					*cp++ = '>';
1060 				*cp = '\0';
1061 				printf(" %s %p", shoconn,
1062 				    unpcb.unp_conn);
1063 			}
1064 		}
1065 		break;
1066 	default:
1067 		/* print protocol number and socket address */
1068 		printf(" %d %p", proto.pr_protocol, sock);
1069 	}
1070 	if (sflg)
1071 		printf("\t%8lld %8lld",
1072 		    (long long)(fp? fp->f_rxfer + fp->f_wxfer : 0),
1073 		    (long long)(fp? fp->f_rbytes + fp->f_wbytes : 0) / 1024);
1074 	printf("\n");
1075 	return;
1076 bad:
1077 	printf("* error\n");
1078 }
1079 
1080 /*
1081  * getinetproto --
1082  *	print name of protocol number
1083  */
1084 void
1085 getinetproto(number)
1086 	int number;
1087 {
1088 	static int isopen;
1089 	struct protoent *pe;
1090 
1091 	if (!isopen)
1092 		setprotoent(++isopen);
1093 	if ((pe = getprotobynumber(number)) != NULL)
1094 		printf(" %s", pe->p_name);
1095 	else
1096 		printf(" %d", number);
1097 }
1098 
1099 int
1100 getfname(char *filename)
1101 {
1102 	struct stat statbuf;
1103 	DEVS *cur;
1104 
1105 	if (stat(filename, &statbuf)) {
1106 		warn("%s", filename);
1107 		return(0);
1108 	}
1109 	if ((cur = malloc(sizeof(DEVS))) == NULL)
1110 		err(1, "malloc");
1111 	cur->next = devs;
1112 	devs = cur;
1113 
1114 	cur->ino = statbuf.st_ino;
1115 	cur->fsid = statbuf.st_dev & 0xffff;
1116 	cur->name = filename;
1117 	return(1);
1118 }
1119 
1120 void
1121 usage(void)
1122 {
1123 	fprintf(stderr, "usage: fstat [-fnosv] [-M core] [-N system] "
1124 	    "[-p pid] [-u user] [file ...]\n");
1125 	exit(1);
1126 }
1127