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