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