fstat.c revision 101872
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 101872 2002-08-14 16:34:13Z bmilekic $");
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	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	default: \
314		printf(" %4d", i); \
315		break; \
316	}
317
318/*
319 * print open files attributed to this process
320 */
321void
322dofiles(kp)
323	struct kinfo_proc *kp;
324{
325	int i;
326	struct file file;
327	struct filedesc0 filed0;
328#define	filed	filed0.fd_fd
329
330	Uname = user_from_uid(kp->ki_uid, 0);
331	Pid = kp->ki_pid;
332	Comm = kp->ki_comm;
333
334	if (kp->ki_fd == NULL)
335		return;
336	if (!KVM_READ(kp->ki_fd, &filed0, sizeof (filed0))) {
337		dprintf(stderr, "can't read filedesc at %p for pid %d\n",
338		    (void *)kp->ki_fd, Pid);
339		return;
340	}
341	/*
342	 * root directory vnode, if one
343	 */
344	if (filed.fd_rdir)
345		vtrans(filed.fd_rdir, RDIR, FREAD);
346	/*
347	 * current working directory vnode
348	 */
349	vtrans(filed.fd_cdir, CDIR, FREAD);
350	/*
351	 * ktrace vnode, if one
352	 */
353	if (kp->ki_tracep)
354		vtrans(kp->ki_tracep, TRACE, FREAD|FWRITE);
355	/*
356	 * text vnode, if one
357	 */
358	if (kp->ki_textvp)
359		vtrans(kp->ki_textvp, TEXT, FREAD);
360	/*
361	 * open files
362	 */
363#define FPSIZE	(sizeof (struct file *))
364	ALLOC_OFILES(filed.fd_lastfile+1);
365	if (filed.fd_nfiles > NDFILE) {
366		if (!KVM_READ(filed.fd_ofiles, ofiles,
367		    (filed.fd_lastfile+1) * FPSIZE)) {
368			dprintf(stderr,
369			    "can't read file structures at %p for pid %d\n",
370			    (void *)filed.fd_ofiles, Pid);
371			return;
372		}
373	} else
374		bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
375	for (i = 0; i <= filed.fd_lastfile; i++) {
376		if (ofiles[i] == NULL)
377			continue;
378		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
379			dprintf(stderr, "can't read file %d at %p for pid %d\n",
380			    i, (void *)ofiles[i], Pid);
381			continue;
382		}
383		if (file.f_type == DTYPE_VNODE)
384			vtrans((struct vnode *)file.f_data, i, file.f_flag);
385		else if (file.f_type == DTYPE_SOCKET) {
386			if (checkfile == 0)
387				socktrans((struct socket *)file.f_data, i);
388		}
389#ifdef DTYPE_PIPE
390		else if (file.f_type == DTYPE_PIPE) {
391			if (checkfile == 0)
392				pipetrans((struct pipe *)file.f_data, i,
393				    file.f_flag);
394		}
395#endif
396#ifdef DTYPE_FIFO
397		else if (file.f_type == DTYPE_FIFO) {
398			if (checkfile == 0)
399				vtrans((struct vnode *)file.f_data, i,
400				    file.f_flag);
401		}
402#endif
403		else {
404			dprintf(stderr,
405			    "unknown file type %d for file %d of pid %d\n",
406			    file.f_type, i, Pid);
407		}
408	}
409}
410
411void
412dommap(kp)
413	struct kinfo_proc *kp;
414{
415	vm_map_t map;
416	struct vmspace vmspace;
417	struct vm_map_entry entry;
418	vm_map_entry_t entryp;
419	struct vm_object object;
420	vm_object_t objp;
421	int prot, fflags;
422
423	if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) {
424		dprintf(stderr,
425		    "can't read vmspace at %p for pid %d\n",
426		    (void *)kp->ki_vmspace, Pid);
427		return;
428	}
429	map = &vmspace.vm_map;
430
431	for (entryp = map->header.next;
432	    entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) {
433		if (!KVM_READ(entryp, &entry, sizeof(entry))) {
434			dprintf(stderr,
435			    "can't read vm_map_entry at %p for pid %d\n",
436			    (void *)entryp, Pid);
437			return;
438		}
439
440		if (entry.eflags & MAP_ENTRY_IS_SUB_MAP)
441			continue;
442
443		if ((objp = entry.object.vm_object) == NULL)
444			continue;
445
446		for (; objp; objp = object.backing_object) {
447			if (!KVM_READ(objp, &object, sizeof(object))) {
448				dprintf(stderr,
449				    "can't read vm_object at %p for pid %d\n",
450				    (void *)objp, Pid);
451				return;
452			}
453		}
454
455		prot = entry.protection;
456		fflags = (prot & VM_PROT_READ ? FREAD : 0) |
457		    (prot & VM_PROT_WRITE ? FWRITE : 0);
458
459		switch (object.type) {
460		case OBJT_VNODE:
461			vtrans((struct vnode *)object.handle, MMAP, fflags);
462			break;
463		default:
464			break;
465		}
466	}
467}
468
469void
470vtrans(vp, i, flag)
471	struct vnode *vp;
472	int i;
473	int flag;
474{
475	struct vnode vn;
476	struct filestat fst;
477	char rw[3], mode[15];
478	const char *badtype, *filename;
479
480	filename = badtype = NULL;
481	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
482		dprintf(stderr, "can't read vnode at %p for pid %d\n",
483		    (void *)vp, Pid);
484		return;
485	}
486	if (vn.v_type == VNON || vn.v_tag == VT_NON)
487		badtype = "none";
488	else if (vn.v_type == VBAD)
489		badtype = "bad";
490	else
491		switch (vn.v_tag) {
492		case VT_UFS:
493			if (!ufs_filestat(&vn, &fst))
494				badtype = "error";
495			break;
496
497		case VT_DEVFS:
498			if (!devfs_filestat(&vn, &fst))
499				badtype = "error";
500			break;
501
502		case VT_NFS:
503			if (!nfs_filestat(&vn, &fst))
504				badtype = "error";
505			break;
506
507		case VT_MSDOSFS:
508			if (!msdosfs_filestat(&vn, &fst))
509				badtype = "error";
510			break;
511
512		case VT_ISOFS:
513			if (!isofs_filestat(&vn, &fst))
514				badtype = "error";
515			break;
516
517		default: {
518			static char unknown[10];
519			sprintf(unknown, "?(%x)", vn.v_tag);
520			badtype = unknown;
521			break;;
522		}
523	}
524	if (checkfile) {
525		int fsmatch = 0;
526		DEVS *d;
527
528		if (badtype)
529			return;
530		for (d = devs; d != NULL; d = d->next)
531			if (d->fsid == fst.fsid) {
532				fsmatch = 1;
533				if (d->ino == fst.fileid) {
534					filename = d->name;
535					break;
536				}
537			}
538		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
539			return;
540	}
541	PREFIX(i);
542	if (badtype) {
543		(void)printf(" -         -  %10s    -\n", badtype);
544		return;
545	}
546	if (nflg)
547		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
548	else
549		(void)printf(" %-8s", getmnton(vn.v_mount));
550	if (nflg)
551		(void)sprintf(mode, "%o", fst.mode);
552	else
553		strmode(fst.mode, mode);
554	(void)printf(" %6ld %10s", fst.fileid, mode);
555	switch (vn.v_type) {
556	case VBLK:
557	case VCHR: {
558		char *name;
559
560		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
561		    S_IFCHR : S_IFBLK)) == NULL))
562			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
563		else
564			printf(" %6s", name);
565		break;
566	}
567	default:
568		printf(" %6lu", fst.size);
569	}
570	rw[0] = '\0';
571	if (flag & FREAD)
572		strcat(rw, "r");
573	if (flag & FWRITE)
574		strcat(rw, "w");
575	printf(" %2s", rw);
576	if (filename && !fsflg)
577		printf("  %s", filename);
578	putchar('\n');
579}
580
581int
582ufs_filestat(vp, fsp)
583	struct vnode *vp;
584	struct filestat *fsp;
585{
586	struct inode inode;
587
588	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
589		dprintf(stderr, "can't read inode at %p for pid %d\n",
590		    (void *)VTOI(vp), Pid);
591		return 0;
592	}
593	/*
594	 * The st_dev from stat(2) is a udev_t. These kernel structures
595	 * contain dev_t structures. We need to convert to udev to make
596	 * comparisons
597	 */
598	fsp->fsid = dev2udev(inode.i_dev);
599	fsp->fileid = (long)inode.i_number;
600	fsp->mode = (mode_t)inode.i_mode;
601	fsp->size = (u_long)inode.i_size;
602#if should_be_but_is_hard
603	fsp->rdev = inode.i_rdev;
604#else
605	fsp->rdev = 0;
606#endif
607
608	return 1;
609}
610
611int
612devfs_filestat(vp, fsp)
613	struct vnode *vp;
614	struct filestat *fsp;
615{
616	struct devfs_dirent devfs_dirent;
617	struct mount mount;
618	struct vnode vnode;
619
620	if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) {
621		dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n",
622		    (void *)vp->v_data, Pid);
623		return 0;
624	}
625	if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) {
626		dprintf(stderr, "can't read mount at %p for pid %d\n",
627		    (void *)vp->v_mount, Pid);
628		return 0;
629	}
630	if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) {
631		dprintf(stderr, "can't read vnode at %p for pid %d\n",
632		    (void *)devfs_dirent.de_vnode, Pid);
633		return 0;
634	}
635	fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0];
636	fsp->fileid = devfs_dirent.de_inode;
637	fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR;
638	fsp->size = 0;
639	fsp->rdev = dev2udev((dev_t)vnode.v_rdev);
640
641	return 1;
642}
643
644int
645nfs_filestat(vp, fsp)
646	struct vnode *vp;
647	struct filestat *fsp;
648{
649	struct nfsnode nfsnode;
650	mode_t mode;
651
652	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
653		dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
654		    (void *)VTONFS(vp), Pid);
655		return 0;
656	}
657	fsp->fsid = nfsnode.n_vattr.va_fsid;
658	fsp->fileid = nfsnode.n_vattr.va_fileid;
659	fsp->size = nfsnode.n_size;
660	fsp->rdev = nfsnode.n_vattr.va_rdev;
661	mode = (mode_t)nfsnode.n_vattr.va_mode;
662	switch (vp->v_type) {
663	case VREG:
664		mode |= S_IFREG;
665		break;
666	case VDIR:
667		mode |= S_IFDIR;
668		break;
669	case VBLK:
670		mode |= S_IFBLK;
671		break;
672	case VCHR:
673		mode |= S_IFCHR;
674		break;
675	case VLNK:
676		mode |= S_IFLNK;
677		break;
678	case VSOCK:
679		mode |= S_IFSOCK;
680		break;
681	case VFIFO:
682		mode |= S_IFIFO;
683		break;
684	case VNON:
685	case VBAD:
686		return 0;
687	};
688	fsp->mode = mode;
689
690	return 1;
691}
692
693
694char *
695getmnton(m)
696	struct mount *m;
697{
698	static struct mount mount;
699	static struct mtab {
700		struct mtab *next;
701		struct mount *m;
702		char mntonname[MNAMELEN];
703	} *mhead = NULL;
704	struct mtab *mt;
705
706	for (mt = mhead; mt != NULL; mt = mt->next)
707		if (m == mt->m)
708			return (mt->mntonname);
709	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
710		warnx("can't read mount table at %p", (void *)m);
711		return (NULL);
712	}
713	if ((mt = malloc(sizeof (struct mtab))) == NULL)
714		err(1, NULL);
715	mt->m = m;
716	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
717	mt->next = mhead;
718	mhead = mt;
719	return (mt->mntonname);
720}
721
722void
723pipetrans(pi, i, flag)
724	struct pipe *pi;
725	int i;
726	int flag;
727{
728	struct pipe pip;
729	char rw[3];
730
731	PREFIX(i);
732
733	/* fill in socket */
734	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
735		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
736		goto bad;
737	}
738
739	printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
740	printf(" %6d", (int)pip.pipe_buffer.cnt);
741	rw[0] = '\0';
742	if (flag & FREAD)
743		strcat(rw, "r");
744	if (flag & FWRITE)
745		strcat(rw, "w");
746	printf(" %2s", rw);
747	putchar('\n');
748	return;
749
750bad:
751	printf("* error\n");
752}
753
754void
755socktrans(sock, i)
756	struct socket *sock;
757	int i;
758{
759	static const char *stypename[] = {
760		"unused",	/* 0 */
761		"stream", 	/* 1 */
762		"dgram",	/* 2 */
763		"raw",		/* 3 */
764		"rdm",		/* 4 */
765		"seqpak"	/* 5 */
766	};
767#define	STYPEMAX 5
768	struct socket	so;
769	struct protosw	proto;
770	struct domain	dom;
771	struct inpcb	inpcb;
772	struct unpcb	unpcb;
773	int len;
774	char dname[32];
775
776	PREFIX(i);
777
778	/* fill in socket */
779	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
780		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
781		goto bad;
782	}
783
784	/* fill in protosw entry */
785	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
786		dprintf(stderr, "can't read protosw at %p",
787		    (void *)so.so_proto);
788		goto bad;
789	}
790
791	/* fill in domain */
792	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
793		dprintf(stderr, "can't read domain at %p\n",
794		    (void *)proto.pr_domain);
795		goto bad;
796	}
797
798	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
799	    sizeof(dname) - 1)) < 0) {
800		dprintf(stderr, "can't read domain name at %p\n",
801		    (void *)dom.dom_name);
802		dname[0] = '\0';
803	}
804	else
805		dname[len] = '\0';
806
807	if ((u_short)so.so_type > STYPEMAX)
808		printf("* %s ?%d", dname, so.so_type);
809	else
810		printf("* %s %s", dname, stypename[so.so_type]);
811
812	/*
813	 * protocol specific formatting
814	 *
815	 * Try to find interesting things to print.  For tcp, the interesting
816	 * thing is the address of the tcpcb, for udp and others, just the
817	 * inpcb (socket pcb).  For unix domain, its the address of the socket
818	 * pcb and the address of the connected pcb (if connected).  Otherwise
819	 * just print the protocol number and address of the socket itself.
820	 * The idea is not to duplicate netstat, but to make available enough
821	 * information for further analysis.
822	 */
823	switch(dom.dom_family) {
824	case AF_INET:
825	case AF_INET6:
826		getinetproto(proto.pr_protocol);
827		if (proto.pr_protocol == IPPROTO_TCP ) {
828			if (so.so_pcb) {
829				if (kvm_read(kd, (u_long)so.so_pcb,
830				    (char *)&inpcb, sizeof(struct inpcb))
831				    != sizeof(struct inpcb)) {
832					dprintf(stderr,
833					    "can't read inpcb at %p\n",
834					    (void *)so.so_pcb);
835					goto bad;
836				}
837				printf(" %lx", (u_long)inpcb.inp_ppcb);
838			}
839		}
840		else if (so.so_pcb)
841			printf(" %lx", (u_long)so.so_pcb);
842		break;
843	case AF_UNIX:
844		/* print address of pcb and connected pcb */
845		if (so.so_pcb) {
846			printf(" %lx", (u_long)so.so_pcb);
847			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
848			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
849				dprintf(stderr, "can't read unpcb at %p\n",
850				    (void *)so.so_pcb);
851				goto bad;
852			}
853			if (unpcb.unp_conn) {
854				char shoconn[4], *cp;
855
856				cp = shoconn;
857				if (!(so.so_state & SS_CANTRCVMORE))
858					*cp++ = '<';
859				*cp++ = '-';
860				if (!(so.so_state & SS_CANTSENDMORE))
861					*cp++ = '>';
862				*cp = '\0';
863				printf(" %s %lx", shoconn,
864				    (u_long)unpcb.unp_conn);
865			}
866		}
867		break;
868	default:
869		/* print protocol number and socket address */
870		printf(" %d %lx", proto.pr_protocol, (u_long)sock);
871	}
872	printf("\n");
873	return;
874bad:
875	printf("* error\n");
876}
877
878
879/*
880 * Read the specinfo structure in the kernel (as pointed to by a dev_t)
881 * in order to work out the associated udev_t
882 */
883udev_t
884dev2udev(dev)
885	dev_t dev;
886{
887	struct specinfo si;
888
889	if (KVM_READ(dev, &si, sizeof si)) {
890		return si.si_udev;
891	} else {
892		dprintf(stderr, "can't convert dev_t %x to a udev_t\n", dev);
893		return -1;
894	}
895}
896
897/*
898 * getinetproto --
899 *	print name of protocol number
900 */
901void
902getinetproto(number)
903	int number;
904{
905	static int isopen;
906	struct protoent *pe;
907
908	if (!isopen)
909		setprotoent(++isopen);
910	if ((pe = getprotobynumber(number)) != NULL)
911		printf(" %s", pe->p_name);
912	else
913		printf(" %d", number);
914}
915
916int
917getfname(filename)
918	const char *filename;
919{
920	struct stat statbuf;
921	DEVS *cur;
922
923	if (stat(filename, &statbuf)) {
924		warn("%s", filename);
925		return(0);
926	}
927	if ((cur = malloc(sizeof(DEVS))) == NULL)
928		err(1, NULL);
929	cur->next = devs;
930	devs = cur;
931
932	cur->ino = statbuf.st_ino;
933	cur->fsid = statbuf.st_dev;
934	cur->name = filename;
935	return(1);
936}
937
938void
939usage()
940{
941	(void)fprintf(stderr,
942 "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
943	exit(1);
944}
945