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