fstat.c revision 103325
1139749Simp/*-
2116258Sharti * Copyright (c) 1988, 1993
3116258Sharti *	The Regents of the University of California.  All rights reserved.
4116258Sharti *
5116258Sharti * Redistribution and use in source and binary forms, with or without
6116258Sharti * modification, are permitted provided that the following conditions
7116258Sharti * are met:
8116258Sharti * 1. Redistributions of source code must retain the above copyright
9116258Sharti *    notice, this list of conditions and the following disclaimer.
10116258Sharti * 2. Redistributions in binary form must reproduce the above copyright
11116258Sharti *    notice, this list of conditions and the following disclaimer in the
12116258Sharti *    documentation and/or other materials provided with the distribution.
13116258Sharti * 3. All advertising materials mentioning features or use of this software
14116258Sharti *    must display the following acknowledgement:
15116258Sharti *	This product includes software developed by the University of
16116258Sharti *	California, Berkeley and its contributors.
17116258Sharti * 4. Neither the name of the University nor the names of its contributors
18116258Sharti *    may be used to endorse or promote products derived from this software
19116258Sharti *    without specific prior written permission.
20116258Sharti *
21116258Sharti * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22116258Sharti * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23116258Sharti * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24116258Sharti * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25116258Sharti * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26116258Sharti * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27116258Sharti * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28116258Sharti * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29116258Sharti * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30116258Sharti * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31116258Sharti * SUCH DAMAGE.
32116258Sharti */
33116258Sharti
34116258Sharti#ifndef lint
35116258Shartistatic const char copyright[] =
36116258Sharti"@(#) Copyright (c) 1988, 1993\n\
37116258Sharti	The Regents of the University of California.  All rights reserved.\n";
38116258Sharti#endif /* not lint */
39116258Sharti
40116258Sharti#ifndef lint
41116258Sharti#if 0
42116258Shartistatic char sccsid[] = "@(#)fstat.c	8.3 (Berkeley) 5/2/95";
43116258Sharti#endif
44116258Sharti#endif /* not lint */
45116258Sharti#include <sys/cdefs.h>
46116258Sharti__FBSDID("$FreeBSD: head/usr.bin/fstat/fstat.c 103325 2002-09-14 18:51:10Z njl $");
47116258Sharti
48116258Sharti#include <sys/param.h>
49116258Sharti#include <sys/time.h>
50116258Sharti#include <sys/proc.h>
51116258Sharti#include <sys/user.h>
52116258Sharti#include <sys/stat.h>
53142384Sharti#include <sys/vnode.h>
54116258Sharti#include <sys/socket.h>
55116258Sharti#include <sys/socketvar.h>
56142384Sharti#include <sys/domain.h>
57116258Sharti#include <sys/protosw.h>
58116258Sharti#include <sys/un.h>
59116258Sharti#include <sys/unpcb.h>
60116258Sharti#include <sys/sysctl.h>
61116258Sharti#include <sys/filedesc.h>
62116258Sharti#include <sys/queue.h>
63116258Sharti#include <sys/pipe.h>
64116258Sharti#define	_KERNEL
65116258Sharti#include <sys/conf.h>
66116258Sharti#include <sys/file.h>
67116258Sharti#include <sys/mount.h>
68116258Sharti#include <ufs/ufs/quota.h>
69116258Sharti#include <ufs/ufs/inode.h>
70116258Sharti#include <fs/devfs/devfs.h>
71116258Sharti#undef _KERNEL
72116258Sharti#include <nfs/nfsproto.h>
73116258Sharti#include <nfs/rpcv2.h>
74116258Sharti#include <nfsclient/nfs.h>
75116258Sharti#include <nfsclient/nfsnode.h>
76116258Sharti
77116258Sharti
78116258Sharti#include <vm/vm.h>
79116258Sharti#include <vm/vm_map.h>
80116258Sharti#include <vm/vm_object.h>
81116258Sharti
82116258Sharti#include <net/route.h>
83116258Sharti#include <netinet/in.h>
84116258Sharti#include <netinet/in_systm.h>
85116258Sharti#include <netinet/ip.h>
86116258Sharti#include <netinet/in_pcb.h>
87116258Sharti
88116258Sharti#include <ctype.h>
89116258Sharti#include <err.h>
90117546Sharti#include <fcntl.h>
91142384Sharti#include <kvm.h>
92116258Sharti#include <limits.h>
93117552Sharti#include <nlist.h>
94117552Sharti#include <paths.h>
95117552Sharti#include <pwd.h>
96117552Sharti#include <stdio.h>
97117552Sharti#include <stdlib.h>
98117552Sharti#include <string.h>
99117552Sharti#include <unistd.h>
100117552Sharti#include <netdb.h>
101117552Sharti
102117552Sharti#include "fstat.h"
103117552Sharti
104117552Sharti#define	TEXT	-1
105117552Sharti#define	CDIR	-2
106117552Sharti#define	RDIR	-3
107117552Sharti#define	TRACE	-4
108117552Sharti#define	MMAP	-5
109117552Sharti
110117552ShartiDEVS *devs;
111117552Sharti
112117552Sharti#ifdef notdef
113116258Shartistruct nlist nl[] = {
114116258Sharti	{ "" },
115116258Sharti};
116116258Sharti#endif
117116258Sharti
118116258Shartiint 	fsflg,	/* show files on same filesystem as file(s) argument */
119116258Sharti	pflg,	/* show files open by a particular pid */
120116258Sharti	uflg;	/* show files open by a particular (effective) user */
121116258Shartiint 	checkfile; /* true if restricting to particular files or filesystems */
122116258Shartiint	nflg;	/* (numerical) display f.s. and rdev as dev_t */
123116258Shartiint	vflg;	/* display errors in locating kernel data objects etc... */
124116258Shartiint	mflg;	/* include memory-mapped files */
125116258Sharti
126116258Sharti
127116258Shartistruct file **ofiles;	/* buffer of pointers to file structures */
128116258Shartiint maxfiles;
129116258Sharti#define ALLOC_OFILES(d)	\
130116258Sharti	if ((d) > maxfiles) { \
131116258Sharti		free(ofiles); \
132116258Sharti		ofiles = malloc((d) * sizeof(struct file *)); \
133116258Sharti		if (ofiles == NULL) { \
134116258Sharti			err(1, NULL); \
135116258Sharti		} \
136116258Sharti		maxfiles = (d); \
137116258Sharti	}
138116258Sharti
139116258Shartichar *memf, *nlistf;
140116258Shartikvm_t *kd;
141116258Sharti
142117552Shartistatic void fstat_kvm(int, int);
143116258Shartistatic void fstat_sysctl(int, int);
144116258Shartivoid dofiles(struct kinfo_proc *kp);
145116258Shartivoid dommap(struct kinfo_proc *kp);
146116258Shartivoid vtrans(struct vnode *vp, int i, int flag);
147116258Shartiint  ufs_filestat(struct vnode *vp, struct filestat *fsp);
148116258Shartiint  nfs_filestat(struct vnode *vp, struct filestat *fsp);
149116258Shartiint  devfs_filestat(struct vnode *vp, struct filestat *fsp);
150116258Shartichar *getmnton(struct mount *m);
151116258Shartivoid pipetrans(struct pipe *pi, int i, int flag);
152116258Shartivoid socktrans(struct socket *sock, int i);
153116258Shartivoid getinetproto(int number);
154116258Shartiint  getfname(const char *filename);
155116258Shartivoid usage(void);
156116258Sharti
157116258Sharti
158116258Shartiint
159116258Shartimain(argc, argv)
160116258Sharti	int argc;
161116258Sharti	char **argv;
162116258Sharti{
163116258Sharti	struct passwd *passwd;
164116258Sharti	int arg, ch, what;
165116258Sharti
166116258Sharti	arg = 0;
167116258Sharti	what = KERN_PROC_ALL;
168116258Sharti	nlistf = memf = NULL;
169116258Sharti	while ((ch = getopt(argc, argv, "fmnp:u:vN:M:")) != -1)
170116258Sharti		switch((char)ch) {
171116258Sharti		case 'f':
172116258Sharti			fsflg = 1;
173117552Sharti			break;
174117552Sharti		case 'M':
175117552Sharti			memf = optarg;
176116258Sharti			break;
177116258Sharti		case 'N':
178116258Sharti			nlistf = optarg;
179116258Sharti			break;
180116258Sharti		case 'm':
181116258Sharti			mflg = 1;
182116258Sharti			break;
183116258Sharti		case 'n':
184116258Sharti			nflg = 1;
185116258Sharti			break;
186116258Sharti		case 'p':
187116258Sharti			if (pflg++)
188116258Sharti				usage();
189116258Sharti			if (!isdigit(*optarg)) {
190116258Sharti				warnx("-p requires a process id");
191116258Sharti				usage();
192116258Sharti			}
193116258Sharti			what = KERN_PROC_PID;
194116258Sharti			arg = atoi(optarg);
195116258Sharti			break;
196116258Sharti		case 'u':
197117552Sharti			if (uflg++)
198116258Sharti				usage();
199116258Sharti			if (!(passwd = getpwnam(optarg)))
200116258Sharti				errx(1, "%s: unknown uid", optarg);
201116258Sharti			what = KERN_PROC_UID;
202116258Sharti			arg = passwd->pw_uid;
203116258Sharti			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], tagstr[12], *tagptr;
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 (!KVM_READ(&vp->v_tag, &tagptr, sizeof tagptr) ||
487	    !KVM_READ(tagptr, tagstr, sizeof tagstr)) {
488		dprintf(stderr, "can't read v_tag at %p for pid %d\n",
489		    (void *)vp, Pid);
490		return;
491	}
492	tagstr[sizeof(tagstr) - 1] = '\0';
493	if (vn.v_type == VNON)
494		badtype = "none";
495	else if (vn.v_type == VBAD)
496		badtype = "bad";
497	else {
498		if (!strcmp("ufs", tagstr)) {
499			if (!ufs_filestat(&vn, &fst))
500				badtype = "error";
501		} else if (!strcmp("devfs", tagstr)) {
502			if (!devfs_filestat(&vn, &fst))
503				badtype = "error";
504		} else if (!strcmp("nfs", tagstr)) {
505			if (!nfs_filestat(&vn, &fst))
506				badtype = "error";
507		} else if (!strcmp("msdosfs", tagstr)) {
508			if (!msdosfs_filestat(&vn, &fst))
509				badtype = "error";
510		} else if (!strcmp("isofs", tagstr)) {
511			if (!isofs_filestat(&vn, &fst))
512				badtype = "error";
513		} else {
514			static char unknown[32];
515			snprintf(unknown, sizeof unknown, "?(%s)", tagstr);
516			badtype = unknown;
517		}
518	}
519	if (checkfile) {
520		int fsmatch = 0;
521		DEVS *d;
522
523		if (badtype)
524			return;
525		for (d = devs; d != NULL; d = d->next)
526			if (d->fsid == fst.fsid) {
527				fsmatch = 1;
528				if (d->ino == fst.fileid) {
529					filename = d->name;
530					break;
531				}
532			}
533		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
534			return;
535	}
536	PREFIX(i);
537	if (badtype) {
538		(void)printf(" -         -  %10s    -\n", badtype);
539		return;
540	}
541	if (nflg)
542		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
543	else
544		(void)printf(" %-8s", getmnton(vn.v_mount));
545	if (nflg)
546		(void)sprintf(mode, "%o", fst.mode);
547	else
548		strmode(fst.mode, mode);
549	(void)printf(" %6ld %10s", fst.fileid, mode);
550	switch (vn.v_type) {
551	case VBLK:
552	case VCHR: {
553		char *name;
554
555		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
556		    S_IFCHR : S_IFBLK)) == NULL))
557			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
558		else
559			printf(" %6s", name);
560		break;
561	}
562	default:
563		printf(" %6lu", fst.size);
564	}
565	rw[0] = '\0';
566	if (flag & FREAD)
567		strcat(rw, "r");
568	if (flag & FWRITE)
569		strcat(rw, "w");
570	printf(" %2s", rw);
571	if (filename && !fsflg)
572		printf("  %s", filename);
573	putchar('\n');
574}
575
576int
577ufs_filestat(vp, fsp)
578	struct vnode *vp;
579	struct filestat *fsp;
580{
581	struct inode inode;
582
583	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
584		dprintf(stderr, "can't read inode at %p for pid %d\n",
585		    (void *)VTOI(vp), Pid);
586		return 0;
587	}
588	/*
589	 * The st_dev from stat(2) is a udev_t. These kernel structures
590	 * contain dev_t structures. We need to convert to udev to make
591	 * comparisons
592	 */
593	fsp->fsid = dev2udev(inode.i_dev);
594	fsp->fileid = (long)inode.i_number;
595	fsp->mode = (mode_t)inode.i_mode;
596	fsp->size = (u_long)inode.i_size;
597#if should_be_but_is_hard
598	fsp->rdev = inode.i_rdev;
599#else
600	fsp->rdev = 0;
601#endif
602
603	return 1;
604}
605
606int
607devfs_filestat(vp, fsp)
608	struct vnode *vp;
609	struct filestat *fsp;
610{
611	struct devfs_dirent devfs_dirent;
612	struct mount mount;
613	struct vnode vnode;
614
615	if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) {
616		dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n",
617		    (void *)vp->v_data, Pid);
618		return 0;
619	}
620	if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) {
621		dprintf(stderr, "can't read mount at %p for pid %d\n",
622		    (void *)vp->v_mount, Pid);
623		return 0;
624	}
625	if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) {
626		dprintf(stderr, "can't read vnode at %p for pid %d\n",
627		    (void *)devfs_dirent.de_vnode, Pid);
628		return 0;
629	}
630	fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0];
631	fsp->fileid = devfs_dirent.de_inode;
632	fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR;
633	fsp->size = 0;
634	fsp->rdev = dev2udev((dev_t)vnode.v_rdev);
635
636	return 1;
637}
638
639int
640nfs_filestat(vp, fsp)
641	struct vnode *vp;
642	struct filestat *fsp;
643{
644	struct nfsnode nfsnode;
645	mode_t mode;
646
647	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
648		dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
649		    (void *)VTONFS(vp), Pid);
650		return 0;
651	}
652	fsp->fsid = nfsnode.n_vattr.va_fsid;
653	fsp->fileid = nfsnode.n_vattr.va_fileid;
654	fsp->size = nfsnode.n_size;
655	fsp->rdev = nfsnode.n_vattr.va_rdev;
656	mode = (mode_t)nfsnode.n_vattr.va_mode;
657	switch (vp->v_type) {
658	case VREG:
659		mode |= S_IFREG;
660		break;
661	case VDIR:
662		mode |= S_IFDIR;
663		break;
664	case VBLK:
665		mode |= S_IFBLK;
666		break;
667	case VCHR:
668		mode |= S_IFCHR;
669		break;
670	case VLNK:
671		mode |= S_IFLNK;
672		break;
673	case VSOCK:
674		mode |= S_IFSOCK;
675		break;
676	case VFIFO:
677		mode |= S_IFIFO;
678		break;
679	case VNON:
680	case VBAD:
681		return 0;
682	};
683	fsp->mode = mode;
684
685	return 1;
686}
687
688
689char *
690getmnton(m)
691	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(pi, i, flag)
719	struct pipe *pi;
720	int i;
721	int flag;
722{
723	struct pipe pip;
724	char rw[3];
725
726	PREFIX(i);
727
728	/* fill in socket */
729	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
730		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
731		goto bad;
732	}
733
734	printf("* pipe %8lx <-> %8lx", (u_long)pi, (u_long)pip.pipe_peer);
735	printf(" %6d", (int)pip.pipe_buffer.cnt);
736	rw[0] = '\0';
737	if (flag & FREAD)
738		strcat(rw, "r");
739	if (flag & FWRITE)
740		strcat(rw, "w");
741	printf(" %2s", rw);
742	putchar('\n');
743	return;
744
745bad:
746	printf("* error\n");
747}
748
749void
750socktrans(sock, i)
751	struct socket *sock;
752	int i;
753{
754	static const char *stypename[] = {
755		"unused",	/* 0 */
756		"stream", 	/* 1 */
757		"dgram",	/* 2 */
758		"raw",		/* 3 */
759		"rdm",		/* 4 */
760		"seqpak"	/* 5 */
761	};
762#define	STYPEMAX 5
763	struct socket	so;
764	struct protosw	proto;
765	struct domain	dom;
766	struct inpcb	inpcb;
767	struct unpcb	unpcb;
768	int len;
769	char dname[32];
770
771	PREFIX(i);
772
773	/* fill in socket */
774	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
775		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
776		goto bad;
777	}
778
779	/* fill in protosw entry */
780	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
781		dprintf(stderr, "can't read protosw at %p",
782		    (void *)so.so_proto);
783		goto bad;
784	}
785
786	/* fill in domain */
787	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
788		dprintf(stderr, "can't read domain at %p\n",
789		    (void *)proto.pr_domain);
790		goto bad;
791	}
792
793	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
794	    sizeof(dname) - 1)) < 0) {
795		dprintf(stderr, "can't read domain name at %p\n",
796		    (void *)dom.dom_name);
797		dname[0] = '\0';
798	}
799	else
800		dname[len] = '\0';
801
802	if ((u_short)so.so_type > STYPEMAX)
803		printf("* %s ?%d", dname, so.so_type);
804	else
805		printf("* %s %s", dname, stypename[so.so_type]);
806
807	/*
808	 * protocol specific formatting
809	 *
810	 * Try to find interesting things to print.  For tcp, the interesting
811	 * thing is the address of the tcpcb, for udp and others, just the
812	 * inpcb (socket pcb).  For unix domain, its the address of the socket
813	 * pcb and the address of the connected pcb (if connected).  Otherwise
814	 * just print the protocol number and address of the socket itself.
815	 * The idea is not to duplicate netstat, but to make available enough
816	 * information for further analysis.
817	 */
818	switch(dom.dom_family) {
819	case AF_INET:
820	case AF_INET6:
821		getinetproto(proto.pr_protocol);
822		if (proto.pr_protocol == IPPROTO_TCP ) {
823			if (so.so_pcb) {
824				if (kvm_read(kd, (u_long)so.so_pcb,
825				    (char *)&inpcb, sizeof(struct inpcb))
826				    != sizeof(struct inpcb)) {
827					dprintf(stderr,
828					    "can't read inpcb at %p\n",
829					    (void *)so.so_pcb);
830					goto bad;
831				}
832				printf(" %lx", (u_long)inpcb.inp_ppcb);
833			}
834		}
835		else if (so.so_pcb)
836			printf(" %lx", (u_long)so.so_pcb);
837		break;
838	case AF_UNIX:
839		/* print address of pcb and connected pcb */
840		if (so.so_pcb) {
841			printf(" %lx", (u_long)so.so_pcb);
842			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
843			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
844				dprintf(stderr, "can't read unpcb at %p\n",
845				    (void *)so.so_pcb);
846				goto bad;
847			}
848			if (unpcb.unp_conn) {
849				char shoconn[4], *cp;
850
851				cp = shoconn;
852				if (!(so.so_state & SS_CANTRCVMORE))
853					*cp++ = '<';
854				*cp++ = '-';
855				if (!(so.so_state & SS_CANTSENDMORE))
856					*cp++ = '>';
857				*cp = '\0';
858				printf(" %s %lx", shoconn,
859				    (u_long)unpcb.unp_conn);
860			}
861		}
862		break;
863	default:
864		/* print protocol number and socket address */
865		printf(" %d %lx", proto.pr_protocol, (u_long)sock);
866	}
867	printf("\n");
868	return;
869bad:
870	printf("* error\n");
871}
872
873
874/*
875 * Read the specinfo structure in the kernel (as pointed to by a dev_t)
876 * in order to work out the associated udev_t
877 */
878udev_t
879dev2udev(dev)
880	dev_t dev;
881{
882	struct specinfo si;
883
884	if (KVM_READ(dev, &si, sizeof si)) {
885		return si.si_udev;
886	} else {
887		dprintf(stderr, "can't convert dev_t %x to a udev_t\n", dev);
888		return -1;
889	}
890}
891
892/*
893 * getinetproto --
894 *	print name of protocol number
895 */
896void
897getinetproto(number)
898	int number;
899{
900	static int isopen;
901	struct protoent *pe;
902
903	if (!isopen)
904		setprotoent(++isopen);
905	if ((pe = getprotobynumber(number)) != NULL)
906		printf(" %s", pe->p_name);
907	else
908		printf(" %d", number);
909}
910
911int
912getfname(filename)
913	const char *filename;
914{
915	struct stat statbuf;
916	DEVS *cur;
917
918	if (stat(filename, &statbuf)) {
919		warn("%s", filename);
920		return(0);
921	}
922	if ((cur = malloc(sizeof(DEVS))) == NULL)
923		err(1, NULL);
924	cur->next = devs;
925	devs = cur;
926
927	cur->ino = statbuf.st_ino;
928	cur->fsid = statbuf.st_dev;
929	cur->name = filename;
930	return(1);
931}
932
933void
934usage()
935{
936	(void)fprintf(stderr,
937 "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
938	exit(1);
939}
940