fstat.c revision 92920
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
44static const char rcsid[] =
45  "$FreeBSD: head/usr.bin/fstat/fstat.c 92920 2002-03-22 01:22:50Z imp $";
46#endif /* not lint */
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/filedesc.h>
62#include <sys/queue.h>
63#include <sys/pipe.h>
64#define	_KERNEL
65#include <sys/conf.h>
66#include <sys/file.h>
67#include <ufs/ufs/quota.h>
68#include <ufs/ufs/inode.h>
69#include <fs/devfs/devfs.h>
70#include <sys/mount.h>
71#undef _KERNEL
72#include <nfs/nfsproto.h>
73#include <nfs/rpcv2.h>
74#include <nfsclient/nfs.h>
75#include <nfsclient/nfsnode.h>
76
77
78#include <vm/vm.h>
79#include <vm/vm_map.h>
80#include <vm/vm_object.h>
81
82#include <net/route.h>
83#include <netinet/in.h>
84#include <netinet/in_systm.h>
85#include <netinet/ip.h>
86#include <netinet/in_pcb.h>
87
88#include <ctype.h>
89#include <err.h>
90#include <fcntl.h>
91#include <kvm.h>
92#include <limits.h>
93#include <nlist.h>
94#include <paths.h>
95#include <pwd.h>
96#include <stdio.h>
97#include <stdlib.h>
98#include <string.h>
99#include <unistd.h>
100#include <netdb.h>
101
102#include "fstat.h"
103
104#define	TEXT	-1
105#define	CDIR	-2
106#define	RDIR	-3
107#define	TRACE	-4
108#define	MMAP	-5
109
110DEVS *devs;
111
112#ifdef notdef
113struct nlist nl[] = {
114	{ "" },
115};
116#endif
117
118int 	fsflg,	/* show files on same filesystem as file(s) argument */
119	pflg,	/* show files open by a particular pid */
120	uflg;	/* show files open by a particular (effective) user */
121int 	checkfile; /* true if restricting to particular files or filesystems */
122int	nflg;	/* (numerical) display f.s. and rdev as dev_t */
123int	vflg;	/* display errors in locating kernel data objects etc... */
124int	mflg;	/* include memory-mapped files */
125
126
127struct file **ofiles;	/* buffer of pointers to file structures */
128int maxfiles;
129#define ALLOC_OFILES(d)	\
130	if ((d) > maxfiles) { \
131		free(ofiles); \
132		ofiles = malloc((d) * sizeof(struct file *)); \
133		if (ofiles == NULL) { \
134			err(1, NULL); \
135		} \
136		maxfiles = (d); \
137	}
138
139kvm_t *kd;
140
141void dofiles(struct kinfo_proc *kp);
142void dommap(struct kinfo_proc *kp);
143void vtrans(struct vnode *vp, int i, int flag);
144int  ufs_filestat(struct vnode *vp, struct filestat *fsp);
145int  nfs_filestat(struct vnode *vp, struct filestat *fsp);
146int  devfs_filestat(struct vnode *vp, struct filestat *fsp);
147char *getmnton(struct mount *m);
148void pipetrans(struct pipe *pi, int i, int flag);
149void socktrans(struct socket *sock, int i);
150void getinetproto(int number);
151int  getfname(char *filename);
152void usage(void);
153
154
155int
156main(argc, argv)
157	int argc;
158	char **argv;
159{
160	register struct passwd *passwd;
161	struct kinfo_proc *p, *plast;
162	int arg, ch, what;
163	char *memf, *nlistf;
164	char buf[_POSIX2_LINE_MAX];
165	int cnt;
166
167	arg = 0;
168	what = KERN_PROC_ALL;
169	nlistf = memf = NULL;
170	while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1)
171		switch((char)ch) {
172		case 'f':
173			fsflg = 1;
174			break;
175		case 'M':
176			memf = optarg;
177			break;
178		case 'N':
179			nlistf = optarg;
180			break;
181		case 'm':
182			mflg = 1;
183			break;
184		case 'n':
185			nflg = 1;
186			break;
187		case 'p':
188			if (pflg++)
189				usage();
190			if (!isdigit(*optarg)) {
191				warnx("-p requires a process id");
192				usage();
193			}
194			what = KERN_PROC_PID;
195			arg = atoi(optarg);
196			break;
197		case 'u':
198			if (uflg++)
199				usage();
200			if (!(passwd = getpwnam(optarg)))
201				errx(1, "%s: unknown uid", optarg);
202			what = KERN_PROC_UID;
203			arg = passwd->pw_uid;
204			break;
205		case 'v':
206			vflg = 1;
207			break;
208		case '?':
209		default:
210			usage();
211		}
212
213	if (*(argv += optind)) {
214		for (; *argv; ++argv) {
215			if (getfname(*argv))
216				checkfile = 1;
217		}
218		if (!checkfile)	/* file(s) specified, but none accessable */
219			exit(1);
220	}
221
222	ALLOC_OFILES(256);	/* reserve space for file pointers */
223
224	if (fsflg && !checkfile) {
225		/* -f with no files means use wd */
226		if (getfname(".") == 0)
227			exit(1);
228		checkfile = 1;
229	}
230
231	/*
232	 * Discard setgid privileges if not the running kernel so that bad
233	 * guys can't print interesting stuff from kernel memory.
234	 */
235	if (nlistf != NULL || memf != NULL)
236		setgid(getgid());
237
238	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
239		errx(1, "%s", buf);
240	setgid(getgid());
241#ifdef notdef
242	if (kvm_nlist(kd, nl) != 0)
243		errx(1, "no namelist: %s", kvm_geterr(kd));
244#endif
245	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
246		errx(1, "%s", kvm_geterr(kd));
247	if (nflg)
248		printf("%s",
249"USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
250	else
251		printf("%s",
252"USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
253	if (checkfile && fsflg == 0)
254		printf(" NAME\n");
255	else
256		putchar('\n');
257
258	for (plast = &p[cnt]; p < plast; ++p) {
259		if (p->ki_stat == SZOMB)
260			continue;
261		dofiles(p);
262		if (mflg)
263			dommap(p);
264	}
265	exit(0);
266}
267
268char	*Uname, *Comm;
269int	Pid;
270
271#define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, 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 */
296void
297dofiles(kp)
298	struct kinfo_proc *kp;
299{
300	int i;
301	struct file file;
302	struct filedesc0 filed0;
303#define	filed	filed0.fd_fd
304
305	Uname = user_from_uid(kp->ki_uid, 0);
306	Pid = kp->ki_pid;
307	Comm = kp->ki_comm;
308
309	if (kp->ki_fd == NULL)
310		return;
311	if (!KVM_READ(kp->ki_fd, &filed0, sizeof (filed0))) {
312		dprintf(stderr, "can't read filedesc at %p for pid %d\n",
313		    (void *)kp->ki_fd, Pid);
314		return;
315	}
316	/*
317	 * root directory vnode, if one
318	 */
319	if (filed.fd_rdir)
320		vtrans(filed.fd_rdir, RDIR, FREAD);
321	/*
322	 * current working directory vnode
323	 */
324	vtrans(filed.fd_cdir, CDIR, FREAD);
325	/*
326	 * ktrace vnode, if one
327	 */
328	if (kp->ki_tracep)
329		vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE);
330	/*
331	 * text vnode, if one
332	 */
333	if (kp->ki_textvp)
334		vtrans(kp->ki_textvp, TEXT, FREAD);
335	/*
336	 * open files
337	 */
338#define FPSIZE	(sizeof (struct file *))
339	ALLOC_OFILES(filed.fd_lastfile+1);
340	if (filed.fd_nfiles > NDFILE) {
341		if (!KVM_READ(filed.fd_ofiles, ofiles,
342		    (filed.fd_lastfile+1) * FPSIZE)) {
343			dprintf(stderr,
344			    "can't read file structures at %p for pid %d\n",
345			    (void *)filed.fd_ofiles, Pid);
346			return;
347		}
348	} else
349		bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
350	for (i = 0; i <= filed.fd_lastfile; i++) {
351		if (ofiles[i] == NULL)
352			continue;
353		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
354			dprintf(stderr, "can't read file %d at %p for pid %d\n",
355			    i, (void *)ofiles[i], Pid);
356			continue;
357		}
358		if (file.f_type == DTYPE_VNODE)
359			vtrans((struct vnode *)file.f_data, i, file.f_flag);
360		else if (file.f_type == DTYPE_SOCKET) {
361			if (checkfile == 0)
362				socktrans((struct socket *)file.f_data, i);
363		}
364#ifdef DTYPE_PIPE
365		else if (file.f_type == DTYPE_PIPE) {
366			if (checkfile == 0)
367				pipetrans((struct pipe *)file.f_data, i,
368				    file.f_flag);
369		}
370#endif
371#ifdef DTYPE_FIFO
372		else if (file.f_type == DTYPE_FIFO) {
373			if (checkfile == 0)
374				vtrans((struct vnode *)file.f_data, i,
375				    file.f_flag);
376		}
377#endif
378		else {
379			dprintf(stderr,
380			    "unknown file type %d for file %d of pid %d\n",
381			    file.f_type, i, Pid);
382		}
383	}
384}
385
386void
387dommap(kp)
388	struct kinfo_proc *kp;
389{
390	vm_map_t map;
391	struct vmspace vmspace;
392	struct vm_map_entry entry;
393	vm_map_entry_t entryp;
394	struct vm_object object;
395	vm_object_t objp;
396	int prot, fflags;
397
398	if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) {
399		dprintf(stderr,
400		    "can't read vmspace at %p for pid %d\n",
401		    (void *)kp->ki_vmspace, Pid);
402		return;
403	}
404	map = &vmspace.vm_map;
405
406	for (entryp = map->header.next;
407	    entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) {
408		if (!KVM_READ(entryp, &entry, sizeof(entry))) {
409			dprintf(stderr,
410			    "can't read vm_map_entry at %p for pid %d\n",
411			    (void *)entryp, Pid);
412			return;
413		}
414
415		if (entry.eflags & MAP_ENTRY_IS_SUB_MAP)
416			continue;
417
418		if ((objp = entry.object.vm_object) == NULL)
419			continue;
420
421		for (; objp; objp = object.backing_object) {
422			if (!KVM_READ(objp, &object, sizeof(object))) {
423				dprintf(stderr,
424				    "can't read vm_object at %p for pid %d\n",
425				    (void *)objp, Pid);
426				return;
427			}
428		}
429
430		prot = entry.protection;
431		fflags = (prot & VM_PROT_READ ? FREAD : 0) |
432		    (prot & VM_PROT_WRITE ? FWRITE : 0);
433
434		switch (object.type) {
435		case OBJT_VNODE:
436			vtrans((struct vnode *)object.handle, MMAP, fflags);
437			break;
438		default:
439			break;
440		}
441	}
442}
443
444void
445vtrans(vp, i, flag)
446	struct vnode *vp;
447	int i;
448	int flag;
449{
450	struct vnode vn;
451	struct filestat fst;
452	char rw[3], mode[15];
453	char *badtype = NULL, *filename, *getmnton();
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
472		case VT_DEVFS:
473			if (!devfs_filestat(&vn, &fst))
474				badtype = "error";
475			break;
476
477		case VT_NFS:
478			if (!nfs_filestat(&vn, &fst))
479				badtype = "error";
480			break;
481
482		case VT_MSDOSFS:
483			if (!msdosfs_filestat(&vn, &fst))
484				badtype = "error";
485			break;
486
487		case VT_ISOFS:
488			if (!isofs_filestat(&vn, &fst))
489				badtype = "error";
490			break;
491
492		default: {
493			static char unknown[10];
494			sprintf(badtype = unknown, "?(%x)", vn.v_tag);
495			break;;
496		}
497	}
498	if (checkfile) {
499		int fsmatch = 0;
500		register DEVS *d;
501
502		if (badtype)
503			return;
504		for (d = devs; d != NULL; d = d->next)
505			if (d->fsid == fst.fsid) {
506				fsmatch = 1;
507				if (d->ino == fst.fileid) {
508					filename = d->name;
509					break;
510				}
511			}
512		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
513			return;
514	}
515	PREFIX(i);
516	if (badtype) {
517		(void)printf(" -         -  %10s    -\n", badtype);
518		return;
519	}
520	if (nflg)
521		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
522	else
523		(void)printf(" %-8s", getmnton(vn.v_mount));
524	if (nflg)
525		(void)sprintf(mode, "%o", fst.mode);
526	else
527		strmode(fst.mode, mode);
528	(void)printf(" %6ld %10s", fst.fileid, mode);
529	switch (vn.v_type) {
530	case VBLK:
531	case VCHR: {
532		char *name;
533
534		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
535		    S_IFCHR : S_IFBLK)) == NULL))
536			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
537		else
538			printf(" %6s", name);
539		break;
540	}
541	default:
542		printf(" %6lu", fst.size);
543	}
544	rw[0] = '\0';
545	if (flag & FREAD)
546		strcat(rw, "r");
547	if (flag & FWRITE)
548		strcat(rw, "w");
549	printf(" %2s", rw);
550	if (filename && !fsflg)
551		printf("  %s", filename);
552	putchar('\n');
553}
554
555int
556ufs_filestat(vp, fsp)
557	struct vnode *vp;
558	struct filestat *fsp;
559{
560	struct inode inode;
561
562	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
563		dprintf(stderr, "can't read inode at %p for pid %d\n",
564		    (void *)VTOI(vp), Pid);
565		return 0;
566	}
567	/*
568	 * The st_dev from stat(2) is a udev_t. These kernel structures
569	 * contain dev_t structures. We need to convert to udev to make
570	 * comparisons
571	 */
572	fsp->fsid = dev2udev(inode.i_dev);
573	fsp->fileid = (long)inode.i_number;
574	fsp->mode = (mode_t)inode.i_mode;
575	fsp->size = (u_long)inode.i_size;
576	fsp->rdev = inode.i_rdev;
577
578	return 1;
579}
580
581int
582devfs_filestat(vp, fsp)
583	struct vnode *vp;
584	struct filestat *fsp;
585{
586	struct devfs_dirent devfs_dirent;
587	struct mount mount;
588	struct vnode vnode;
589
590	if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) {
591		dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n",
592		    (void *)vp->v_data, Pid);
593		return 0;
594	}
595	if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) {
596		dprintf(stderr, "can't read mount at %p for pid %d\n",
597		    (void *)vp->v_mount, Pid);
598		return 0;
599	}
600	if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) {
601		dprintf(stderr, "can't read vnode at %p for pid %d\n",
602		    (void *)devfs_dirent.de_vnode, Pid);
603		return 0;
604	}
605	fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0];
606	fsp->fileid = devfs_dirent.de_inode;
607	fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR;
608	fsp->size = 0;
609	fsp->rdev = dev2udev((dev_t)vnode.v_rdev);
610
611	return 1;
612}
613
614int
615nfs_filestat(vp, fsp)
616	struct vnode *vp;
617	struct filestat *fsp;
618{
619	struct nfsnode nfsnode;
620	register mode_t mode;
621
622	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
623		dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
624		    (void *)VTONFS(vp), 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	case VNON:
655	case VBAD:
656		return 0;
657	};
658	fsp->mode = mode;
659
660	return 1;
661}
662
663
664char *
665getmnton(m)
666	struct mount *m;
667{
668	static struct mount mount;
669	static struct mtab {
670		struct mtab *next;
671		struct mount *m;
672		char mntonname[MNAMELEN];
673	} *mhead = NULL;
674	register struct mtab *mt;
675
676	for (mt = mhead; mt != NULL; mt = mt->next)
677		if (m == mt->m)
678			return (mt->mntonname);
679	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
680		warnx("can't read mount table at %p", (void *)m);
681		return (NULL);
682	}
683	if ((mt = malloc(sizeof (struct mtab))) == NULL)
684		err(1, NULL);
685	mt->m = m;
686	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
687	mt->next = mhead;
688	mhead = mt;
689	return (mt->mntonname);
690}
691
692void
693pipetrans(pi, i, flag)
694	struct pipe *pi;
695	int i;
696	int flag;
697{
698	struct pipe pip;
699	char rw[3];
700
701	PREFIX(i);
702
703	/* fill in socket */
704	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
705		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
706		goto bad;
707	}
708
709	printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
710	printf(" %6d", (int)pip.pipe_buffer.cnt);
711	rw[0] = '\0';
712	if (flag & FREAD)
713		strcat(rw, "r");
714	if (flag & FWRITE)
715		strcat(rw, "w");
716	printf(" %2s", rw);
717	putchar('\n');
718	return;
719
720bad:
721	printf("* error\n");
722}
723
724void
725socktrans(sock, i)
726	struct socket *sock;
727	int i;
728{
729	static char *stypename[] = {
730		"unused",	/* 0 */
731		"stream", 	/* 1 */
732		"dgram",	/* 2 */
733		"raw",		/* 3 */
734		"rdm",		/* 4 */
735		"seqpak"	/* 5 */
736	};
737#define	STYPEMAX 5
738	struct socket	so;
739	struct protosw	proto;
740	struct domain	dom;
741	struct inpcb	inpcb;
742	struct unpcb	unpcb;
743	int len;
744	char dname[32], *strcpy();
745
746	PREFIX(i);
747
748	/* fill in socket */
749	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
750		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
751		goto bad;
752	}
753
754	/* fill in protosw entry */
755	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
756		dprintf(stderr, "can't read protosw at %p",
757		    (void *)so.so_proto);
758		goto bad;
759	}
760
761	/* fill in domain */
762	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
763		dprintf(stderr, "can't read domain at %p\n",
764		    (void *)proto.pr_domain);
765		goto bad;
766	}
767
768	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
769	    sizeof(dname) - 1)) < 0) {
770		dprintf(stderr, "can't read domain name at %p\n",
771		    (void *)dom.dom_name);
772		dname[0] = '\0';
773	}
774	else
775		dname[len] = '\0';
776
777	if ((u_short)so.so_type > STYPEMAX)
778		printf("* %s ?%d", dname, so.so_type);
779	else
780		printf("* %s %s", dname, stypename[so.so_type]);
781
782	/*
783	 * protocol specific formatting
784	 *
785	 * Try to find interesting things to print.  For tcp, the interesting
786	 * thing is the address of the tcpcb, for udp and others, just the
787	 * inpcb (socket pcb).  For unix domain, its the address of the socket
788	 * pcb and the address of the connected pcb (if connected).  Otherwise
789	 * just print the protocol number and address of the socket itself.
790	 * The idea is not to duplicate netstat, but to make available enough
791	 * information for further analysis.
792	 */
793	switch(dom.dom_family) {
794	case AF_INET:
795	case AF_INET6:
796		getinetproto(proto.pr_protocol);
797		if (proto.pr_protocol == IPPROTO_TCP ) {
798			if (so.so_pcb) {
799				if (kvm_read(kd, (u_long)so.so_pcb,
800				    (char *)&inpcb, sizeof(struct inpcb))
801				    != sizeof(struct inpcb)) {
802					dprintf(stderr,
803					    "can't read inpcb at %p\n",
804					    (void *)so.so_pcb);
805					goto bad;
806				}
807				printf(" %lx", (u_long)inpcb.inp_ppcb);
808			}
809		}
810		else if (so.so_pcb)
811			printf(" %lx", (u_long)so.so_pcb);
812		break;
813	case AF_UNIX:
814		/* print address of pcb and connected pcb */
815		if (so.so_pcb) {
816			printf(" %lx", (u_long)so.so_pcb);
817			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
818			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
819				dprintf(stderr, "can't read unpcb at %p\n",
820				    (void *)so.so_pcb);
821				goto bad;
822			}
823			if (unpcb.unp_conn) {
824				char shoconn[4], *cp;
825
826				cp = shoconn;
827				if (!(so.so_state & SS_CANTRCVMORE))
828					*cp++ = '<';
829				*cp++ = '-';
830				if (!(so.so_state & SS_CANTSENDMORE))
831					*cp++ = '>';
832				*cp = '\0';
833				printf(" %s %lx", shoconn,
834				    (u_long)unpcb.unp_conn);
835			}
836		}
837		break;
838	default:
839		/* print protocol number and socket address */
840		printf(" %d %lx", proto.pr_protocol, (u_long)sock);
841	}
842	printf("\n");
843	return;
844bad:
845	printf("* error\n");
846}
847
848
849/*
850 * Read the specinfo structure in the kernel (as pointed to by a dev_t)
851 * in order to work out the associated udev_t
852 */
853udev_t
854dev2udev(dev)
855	dev_t dev;
856{
857	struct specinfo si;
858
859	if (KVM_READ(dev, &si, sizeof si)) {
860		return si.si_udev;
861	} else {
862		dprintf(stderr, "can't convert dev_t %x to a udev_t\n", dev);
863		return -1;
864	}
865}
866
867/*
868 * getinetproto --
869 *	print name of protocol number
870 */
871void
872getinetproto(number)
873	int number;
874{
875	static int isopen;
876	register struct protoent *pe;
877
878	if (!isopen)
879		setprotoent(++isopen);
880	if ((pe = getprotobynumber(number)) != NULL)
881		printf(" %s", pe->p_name);
882	else
883		printf(" %d", number);
884}
885
886int
887getfname(filename)
888	char *filename;
889{
890	struct stat statbuf;
891	DEVS *cur;
892
893	if (stat(filename, &statbuf)) {
894		warn("%s", filename);
895		return(0);
896	}
897	if ((cur = malloc(sizeof(DEVS))) == NULL)
898		err(1, NULL);
899	cur->next = devs;
900	devs = cur;
901
902	cur->ino = statbuf.st_ino;
903	cur->fsid = statbuf.st_dev;
904	cur->name = filename;
905	return(1);
906}
907
908void
909usage()
910{
911	(void)fprintf(stderr,
912 "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
913	exit(1);
914}
915