fstat.c revision 109153
1144094Sdas/*-
2144094Sdas * Copyright (c) 1988, 1993
3144094Sdas *	The Regents of the University of California.  All rights reserved.
4144094Sdas *
5144094Sdas * Redistribution and use in source and binary forms, with or without
6144094Sdas * modification, are permitted provided that the following conditions
7144094Sdas * are met:
8144094Sdas * 1. Redistributions of source code must retain the above copyright
9144094Sdas *    notice, this list of conditions and the following disclaimer.
10144094Sdas * 2. Redistributions in binary form must reproduce the above copyright
11144094Sdas *    notice, this list of conditions and the following disclaimer in the
12144094Sdas *    documentation and/or other materials provided with the distribution.
13144094Sdas * 3. All advertising materials mentioning features or use of this software
14144094Sdas *    must display the following acknowledgement:
15144094Sdas *	This product includes software developed by the University of
16144094Sdas *	California, Berkeley and its contributors.
17144094Sdas * 4. Neither the name of the University nor the names of its contributors
18144094Sdas *    may be used to endorse or promote products derived from this software
19144094Sdas *    without specific prior written permission.
20144094Sdas *
21144094Sdas * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22144094Sdas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23144094Sdas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24144094Sdas * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25144094Sdas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26144094Sdas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27144094Sdas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28144094Sdas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29144094Sdas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30144094Sdas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31144094Sdas * SUCH DAMAGE.
32144094Sdas */
33144094Sdas
34144094Sdas#ifndef lint
35144094Sdasstatic const char copyright[] =
36144094Sdas"@(#) Copyright (c) 1988, 1993\n\
37144094Sdas	The Regents of the University of California.  All rights reserved.\n";
38144094Sdas#endif /* not lint */
39144094Sdas
40144094Sdas#ifndef lint
41144094Sdas#if 0
42144094Sdasstatic char sccsid[] = "@(#)fstat.c	8.3 (Berkeley) 5/2/95";
43144094Sdas#endif
44144094Sdas#endif /* not lint */
45144094Sdas#include <sys/cdefs.h>
46144094Sdas__FBSDID("$FreeBSD: head/usr.bin/fstat/fstat.c 109153 2003-01-13 00:33:17Z dillon $");
47144094Sdas
48144094Sdas#include <sys/param.h>
49144094Sdas#include <sys/time.h>
50144094Sdas#include <sys/proc.h>
51144094Sdas#include <sys/user.h>
52144094Sdas#include <sys/stat.h>
53144094Sdas#include <sys/vnode.h>
54144094Sdas#include <sys/socket.h>
55144094Sdas#include <sys/socketvar.h>
56144094Sdas#include <sys/domain.h>
57144094Sdas#include <sys/protosw.h>
58144094Sdas#include <sys/un.h>
59144094Sdas#include <sys/unpcb.h>
60144094Sdas#include <sys/sysctl.h>
61144094Sdas#include <sys/filedesc.h>
62144094Sdas#include <sys/queue.h>
63144094Sdas#include <sys/pipe.h>
64144094Sdas#define	_KERNEL
65144094Sdas#include <sys/conf.h>
66144094Sdas#include <sys/file.h>
67144094Sdas#include <sys/mount.h>
68144094Sdas#include <ufs/ufs/quota.h>
69144094Sdas#include <ufs/ufs/inode.h>
70144094Sdas#include <fs/devfs/devfs.h>
71144094Sdas#undef _KERNEL
72144094Sdas#include <nfs/nfsproto.h>
73144094Sdas#include <nfs/rpcv2.h>
74144094Sdas#include <nfsclient/nfs.h>
75144094Sdas#include <nfsclient/nfsnode.h>
76144094Sdas
77144094Sdas
78144094Sdas#include <vm/vm.h>
79144094Sdas#include <vm/vm_map.h>
80144094Sdas#include <vm/vm_object.h>
81144094Sdas
82144094Sdas#include <net/route.h>
83144094Sdas#include <netinet/in.h>
84144094Sdas#include <netinet/in_systm.h>
85144094Sdas#include <netinet/ip.h>
86144094Sdas#include <netinet/in_pcb.h>
87144094Sdas
88144094Sdas#include <ctype.h>
89144094Sdas#include <err.h>
90144094Sdas#include <fcntl.h>
91144094Sdas#include <kvm.h>
92144094Sdas#include <limits.h>
93144094Sdas#include <nlist.h>
94144094Sdas#include <paths.h>
95144094Sdas#include <pwd.h>
96144094Sdas#include <stdio.h>
97144094Sdas#include <stdlib.h>
98144094Sdas#include <string.h>
99144094Sdas#include <unistd.h>
100144094Sdas#include <netdb.h>
101144094Sdas
102144094Sdas#include "fstat.h"
103165856Sdas
104144094Sdas#define	TEXT	-1
105165856Sdas#define	CDIR	-2
106144094Sdas#define	RDIR	-3
107144094Sdas#define	TRACE	-4
108144094Sdas#define	MMAP	-5
109165856Sdas
110144094SdasDEVS *devs;
111165856Sdas
112144094Sdas#ifdef notdef
113144094Sdasstruct nlist nl[] = {
114144094Sdas	{ "" },
115144094Sdas};
116144094Sdas#endif
117144094Sdas
118144094Sdasint 	fsflg,	/* show files on same filesystem as file(s) argument */
119144094Sdas	pflg,	/* show files open by a particular pid */
120144094Sdas	uflg;	/* show files open by a particular (effective) user */
121144094Sdasint 	checkfile; /* true if restricting to particular files or filesystems */
122144094Sdasint	nflg;	/* (numerical) display f.s. and rdev as dev_t */
123144094Sdasint	vflg;	/* display errors in locating kernel data objects etc... */
124144094Sdasint	mflg;	/* include memory-mapped files */
125144094Sdas
126144094Sdas
127144094Sdasstruct file **ofiles;	/* buffer of pointers to file structures */
128144094Sdasint maxfiles;
129144094Sdas#define ALLOC_OFILES(d)	\
130165856Sdas	if ((d) > maxfiles) { \
131165856Sdas		free(ofiles); \
132165856Sdas		ofiles = malloc((d) * sizeof(struct file *)); \
133165856Sdas		if (ofiles == NULL) { \
134165856Sdas			err(1, NULL); \
135144094Sdas		} \
136144094Sdas		maxfiles = (d); \
137144094Sdas	}
138144094Sdas
139144094Sdaschar *memf, *nlistf;
140144094Sdaskvm_t *kd;
141144094Sdas
142144094Sdasstatic void fstat_kvm(int, int);
143144094Sdasstatic void fstat_sysctl(int, int);
144144094Sdasvoid dofiles(struct kinfo_proc *kp);
145144094Sdasvoid dommap(struct kinfo_proc *kp);
146165856Sdasvoid vtrans(struct vnode *vp, int i, int flag);
147165856Sdasint  ufs_filestat(struct vnode *vp, struct filestat *fsp);
148165856Sdasint  nfs_filestat(struct vnode *vp, struct filestat *fsp);
149165856Sdasint  devfs_filestat(struct vnode *vp, struct filestat *fsp);
150165856Sdaschar *getmnton(struct mount *m);
151144094Sdasvoid 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(file.f_data, i, file.f_flag);
385		else if (file.f_type == DTYPE_SOCKET) {
386			if (checkfile == 0)
387				socktrans(file.f_data, i);
388		}
389#ifdef DTYPE_PIPE
390		else if (file.f_type == DTYPE_PIPE) {
391			if (checkfile == 0)
392				pipetrans(file.f_data, i, file.f_flag);
393		}
394#endif
395#ifdef DTYPE_FIFO
396		else if (file.f_type == DTYPE_FIFO) {
397			if (checkfile == 0)
398				vtrans(file.f_data, i, file.f_flag);
399		}
400#endif
401		else {
402			dprintf(stderr,
403			    "unknown file type %d for file %d of pid %d\n",
404			    file.f_type, i, Pid);
405		}
406	}
407}
408
409void
410dommap(kp)
411	struct kinfo_proc *kp;
412{
413	vm_map_t map;
414	struct vmspace vmspace;
415	struct vm_map_entry entry;
416	vm_map_entry_t entryp;
417	struct vm_object object;
418	vm_object_t objp;
419	int prot, fflags;
420
421	if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) {
422		dprintf(stderr,
423		    "can't read vmspace at %p for pid %d\n",
424		    (void *)kp->ki_vmspace, Pid);
425		return;
426	}
427	map = &vmspace.vm_map;
428
429	for (entryp = map->header.next;
430	    entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) {
431		if (!KVM_READ(entryp, &entry, sizeof(entry))) {
432			dprintf(stderr,
433			    "can't read vm_map_entry at %p for pid %d\n",
434			    (void *)entryp, Pid);
435			return;
436		}
437
438		if (entry.eflags & MAP_ENTRY_IS_SUB_MAP)
439			continue;
440
441		if ((objp = entry.object.vm_object) == NULL)
442			continue;
443
444		for (; objp; objp = object.backing_object) {
445			if (!KVM_READ(objp, &object, sizeof(object))) {
446				dprintf(stderr,
447				    "can't read vm_object at %p for pid %d\n",
448				    (void *)objp, Pid);
449				return;
450			}
451		}
452
453		prot = entry.protection;
454		fflags = (prot & VM_PROT_READ ? FREAD : 0) |
455		    (prot & VM_PROT_WRITE ? FWRITE : 0);
456
457		switch (object.type) {
458		case OBJT_VNODE:
459			vtrans((struct vnode *)object.handle, MMAP, fflags);
460			break;
461		default:
462			break;
463		}
464	}
465}
466
467void
468vtrans(vp, i, flag)
469	struct vnode *vp;
470	int i;
471	int flag;
472{
473	struct vnode vn;
474	struct filestat fst;
475	char rw[3], mode[15], tagstr[12], *tagptr;
476	const char *badtype, *filename;
477
478	filename = badtype = NULL;
479	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
480		dprintf(stderr, "can't read vnode at %p for pid %d\n",
481		    (void *)vp, Pid);
482		return;
483	}
484	if (!KVM_READ(&vp->v_tag, &tagptr, sizeof tagptr) ||
485	    !KVM_READ(tagptr, tagstr, sizeof tagstr)) {
486		dprintf(stderr, "can't read v_tag at %p for pid %d\n",
487		    (void *)vp, Pid);
488		return;
489	}
490	tagstr[sizeof(tagstr) - 1] = '\0';
491	if (vn.v_type == VNON)
492		badtype = "none";
493	else if (vn.v_type == VBAD)
494		badtype = "bad";
495	else {
496		if (!strcmp("ufs", tagstr)) {
497			if (!ufs_filestat(&vn, &fst))
498				badtype = "error";
499		} else if (!strcmp("devfs", tagstr)) {
500			if (!devfs_filestat(&vn, &fst))
501				badtype = "error";
502		} else if (!strcmp("nfs", tagstr)) {
503			if (!nfs_filestat(&vn, &fst))
504				badtype = "error";
505		} else if (!strcmp("msdosfs", tagstr)) {
506			if (!msdosfs_filestat(&vn, &fst))
507				badtype = "error";
508		} else if (!strcmp("isofs", tagstr)) {
509			if (!isofs_filestat(&vn, &fst))
510				badtype = "error";
511		} else {
512			static char unknown[32];
513			snprintf(unknown, sizeof unknown, "?(%s)", tagstr);
514			badtype = unknown;
515		}
516	}
517	if (checkfile) {
518		int fsmatch = 0;
519		DEVS *d;
520
521		if (badtype)
522			return;
523		for (d = devs; d != NULL; d = d->next)
524			if (d->fsid == fst.fsid) {
525				fsmatch = 1;
526				if (d->ino == fst.fileid) {
527					filename = d->name;
528					break;
529				}
530			}
531		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
532			return;
533	}
534	PREFIX(i);
535	if (badtype) {
536		(void)printf(" -         -  %10s    -\n", badtype);
537		return;
538	}
539	if (nflg)
540		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
541	else
542		(void)printf(" %-8s", getmnton(vn.v_mount));
543	if (nflg)
544		(void)sprintf(mode, "%o", fst.mode);
545	else
546		strmode(fst.mode, mode);
547	(void)printf(" %6ld %10s", fst.fileid, mode);
548	switch (vn.v_type) {
549	case VBLK:
550	case VCHR: {
551		char *name;
552
553		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
554		    S_IFCHR : S_IFBLK)) == NULL))
555			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
556		else
557			printf(" %6s", name);
558		break;
559	}
560	default:
561		printf(" %6lu", fst.size);
562	}
563	rw[0] = '\0';
564	if (flag & FREAD)
565		strcat(rw, "r");
566	if (flag & FWRITE)
567		strcat(rw, "w");
568	printf(" %2s", rw);
569	if (filename && !fsflg)
570		printf("  %s", filename);
571	putchar('\n');
572}
573
574int
575ufs_filestat(vp, fsp)
576	struct vnode *vp;
577	struct filestat *fsp;
578{
579	struct inode inode;
580
581	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
582		dprintf(stderr, "can't read inode at %p for pid %d\n",
583		    (void *)VTOI(vp), Pid);
584		return 0;
585	}
586	/*
587	 * The st_dev from stat(2) is a udev_t. These kernel structures
588	 * contain dev_t structures. We need to convert to udev to make
589	 * comparisons
590	 */
591	fsp->fsid = dev2udev(inode.i_dev);
592	fsp->fileid = (long)inode.i_number;
593	fsp->mode = (mode_t)inode.i_mode;
594	fsp->size = (u_long)inode.i_size;
595#if should_be_but_is_hard
596	fsp->rdev = inode.i_rdev;
597#else
598	fsp->rdev = 0;
599#endif
600
601	return 1;
602}
603
604int
605devfs_filestat(vp, fsp)
606	struct vnode *vp;
607	struct filestat *fsp;
608{
609	struct devfs_dirent devfs_dirent;
610	struct mount mount;
611	struct vnode vnode;
612
613	if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) {
614		dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n",
615		    (void *)vp->v_data, Pid);
616		return 0;
617	}
618	if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) {
619		dprintf(stderr, "can't read mount at %p for pid %d\n",
620		    (void *)vp->v_mount, Pid);
621		return 0;
622	}
623	if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) {
624		dprintf(stderr, "can't read vnode at %p for pid %d\n",
625		    (void *)devfs_dirent.de_vnode, Pid);
626		return 0;
627	}
628	fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0];
629	fsp->fileid = devfs_dirent.de_inode;
630	fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR;
631	fsp->size = 0;
632	fsp->rdev = dev2udev((dev_t)vnode.v_rdev);
633
634	return 1;
635}
636
637int
638nfs_filestat(vp, fsp)
639	struct vnode *vp;
640	struct filestat *fsp;
641{
642	struct nfsnode nfsnode;
643	mode_t mode;
644
645	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
646		dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
647		    (void *)VTONFS(vp), Pid);
648		return 0;
649	}
650	fsp->fsid = nfsnode.n_vattr.va_fsid;
651	fsp->fileid = nfsnode.n_vattr.va_fileid;
652	fsp->size = nfsnode.n_size;
653	fsp->rdev = nfsnode.n_vattr.va_rdev;
654	mode = (mode_t)nfsnode.n_vattr.va_mode;
655	switch (vp->v_type) {
656	case VREG:
657		mode |= S_IFREG;
658		break;
659	case VDIR:
660		mode |= S_IFDIR;
661		break;
662	case VBLK:
663		mode |= S_IFBLK;
664		break;
665	case VCHR:
666		mode |= S_IFCHR;
667		break;
668	case VLNK:
669		mode |= S_IFLNK;
670		break;
671	case VSOCK:
672		mode |= S_IFSOCK;
673		break;
674	case VFIFO:
675		mode |= S_IFIFO;
676		break;
677	case VNON:
678	case VBAD:
679		return 0;
680	};
681	fsp->mode = mode;
682
683	return 1;
684}
685
686
687char *
688getmnton(m)
689	struct mount *m;
690{
691	static struct mount mount;
692	static struct mtab {
693		struct mtab *next;
694		struct mount *m;
695		char mntonname[MNAMELEN];
696	} *mhead = NULL;
697	struct mtab *mt;
698
699	for (mt = mhead; mt != NULL; mt = mt->next)
700		if (m == mt->m)
701			return (mt->mntonname);
702	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
703		warnx("can't read mount table at %p", (void *)m);
704		return (NULL);
705	}
706	if ((mt = malloc(sizeof (struct mtab))) == NULL)
707		err(1, NULL);
708	mt->m = m;
709	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
710	mt->next = mhead;
711	mhead = mt;
712	return (mt->mntonname);
713}
714
715void
716pipetrans(pi, i, flag)
717	struct pipe *pi;
718	int i;
719	int flag;
720{
721	struct pipe pip;
722	char rw[3];
723
724	PREFIX(i);
725
726	/* fill in socket */
727	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
728		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
729		goto bad;
730	}
731
732	printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
733	printf(" %6d", (int)pip.pipe_buffer.cnt);
734	rw[0] = '\0';
735	if (flag & FREAD)
736		strcat(rw, "r");
737	if (flag & FWRITE)
738		strcat(rw, "w");
739	printf(" %2s", rw);
740	putchar('\n');
741	return;
742
743bad:
744	printf("* error\n");
745}
746
747void
748socktrans(sock, i)
749	struct socket *sock;
750	int i;
751{
752	static const char *stypename[] = {
753		"unused",	/* 0 */
754		"stream", 	/* 1 */
755		"dgram",	/* 2 */
756		"raw",		/* 3 */
757		"rdm",		/* 4 */
758		"seqpak"	/* 5 */
759	};
760#define	STYPEMAX 5
761	struct socket	so;
762	struct protosw	proto;
763	struct domain	dom;
764	struct inpcb	inpcb;
765	struct unpcb	unpcb;
766	int len;
767	char dname[32];
768
769	PREFIX(i);
770
771	/* fill in socket */
772	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
773		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
774		goto bad;
775	}
776
777	/* fill in protosw entry */
778	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
779		dprintf(stderr, "can't read protosw at %p",
780		    (void *)so.so_proto);
781		goto bad;
782	}
783
784	/* fill in domain */
785	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
786		dprintf(stderr, "can't read domain at %p\n",
787		    (void *)proto.pr_domain);
788		goto bad;
789	}
790
791	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
792	    sizeof(dname) - 1)) < 0) {
793		dprintf(stderr, "can't read domain name at %p\n",
794		    (void *)dom.dom_name);
795		dname[0] = '\0';
796	}
797	else
798		dname[len] = '\0';
799
800	if ((u_short)so.so_type > STYPEMAX)
801		printf("* %s ?%d", dname, so.so_type);
802	else
803		printf("* %s %s", dname, stypename[so.so_type]);
804
805	/*
806	 * protocol specific formatting
807	 *
808	 * Try to find interesting things to print.  For tcp, the interesting
809	 * thing is the address of the tcpcb, for udp and others, just the
810	 * inpcb (socket pcb).  For unix domain, its the address of the socket
811	 * pcb and the address of the connected pcb (if connected).  Otherwise
812	 * just print the protocol number and address of the socket itself.
813	 * The idea is not to duplicate netstat, but to make available enough
814	 * information for further analysis.
815	 */
816	switch(dom.dom_family) {
817	case AF_INET:
818	case AF_INET6:
819		getinetproto(proto.pr_protocol);
820		if (proto.pr_protocol == IPPROTO_TCP ) {
821			if (so.so_pcb) {
822				if (kvm_read(kd, (u_long)so.so_pcb,
823				    (char *)&inpcb, sizeof(struct inpcb))
824				    != sizeof(struct inpcb)) {
825					dprintf(stderr,
826					    "can't read inpcb at %p\n",
827					    (void *)so.so_pcb);
828					goto bad;
829				}
830				printf(" %lx", (u_long)inpcb.inp_ppcb);
831			}
832		}
833		else if (so.so_pcb)
834			printf(" %lx", (u_long)so.so_pcb);
835		break;
836	case AF_UNIX:
837		/* print address of pcb and connected pcb */
838		if (so.so_pcb) {
839			printf(" %lx", (u_long)so.so_pcb);
840			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
841			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
842				dprintf(stderr, "can't read unpcb at %p\n",
843				    (void *)so.so_pcb);
844				goto bad;
845			}
846			if (unpcb.unp_conn) {
847				char shoconn[4], *cp;
848
849				cp = shoconn;
850				if (!(so.so_state & SS_CANTRCVMORE))
851					*cp++ = '<';
852				*cp++ = '-';
853				if (!(so.so_state & SS_CANTSENDMORE))
854					*cp++ = '>';
855				*cp = '\0';
856				printf(" %s %lx", shoconn,
857				    (u_long)unpcb.unp_conn);
858			}
859		}
860		break;
861	default:
862		/* print protocol number and socket address */
863		printf(" %d %lx", proto.pr_protocol, (u_long)sock);
864	}
865	printf("\n");
866	return;
867bad:
868	printf("* error\n");
869}
870
871
872/*
873 * Read the cdev structure in the kernel (as pointed to by a dev_t)
874 * in order to work out the associated udev_t
875 */
876udev_t
877dev2udev(dev)
878	dev_t dev;
879{
880	struct cdev si;
881
882	if (KVM_READ(dev, &si, sizeof si)) {
883		return si.si_udev;
884	} else {
885		dprintf(stderr, "can't convert dev_t %x to a udev_t\n", dev);
886		return -1;
887	}
888}
889
890/*
891 * getinetproto --
892 *	print name of protocol number
893 */
894void
895getinetproto(number)
896	int number;
897{
898	static int isopen;
899	struct protoent *pe;
900
901	if (!isopen)
902		setprotoent(++isopen);
903	if ((pe = getprotobynumber(number)) != NULL)
904		printf(" %s", pe->p_name);
905	else
906		printf(" %d", number);
907}
908
909int
910getfname(filename)
911	const char *filename;
912{
913	struct stat statbuf;
914	DEVS *cur;
915
916	if (stat(filename, &statbuf)) {
917		warn("%s", filename);
918		return(0);
919	}
920	if ((cur = malloc(sizeof(DEVS))) == NULL)
921		err(1, NULL);
922	cur->next = devs;
923	devs = cur;
924
925	cur->ino = statbuf.st_ino;
926	cur->fsid = statbuf.st_dev;
927	cur->name = filename;
928	return(1);
929}
930
931void
932usage()
933{
934	(void)fprintf(stderr,
935 "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
936	exit(1);
937}
938