fstat.c revision 57345
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
44static const char rcsid[] =
45  "$FreeBSD: head/usr.bin/fstat/fstat.c 57345 2000-02-19 21:46:45Z shin $";
46#endif /* not lint */
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#include <sys/conf.h>
65#define	_KERNEL
66#include <sys/file.h>
67#include <ufs/ufs/quota.h>
68#include <ufs/ufs/inode.h>
69#undef _KERNEL
70#include <sys/mount.h>
71#include <nfs/nfsproto.h>
72#include <nfs/rpcv2.h>
73#include <nfs/nfs.h>
74#include <nfs/nfsnode.h>
75
76#include <net/route.h>
77#include <netinet/in.h>
78#include <netinet/in_systm.h>
79#include <netinet/ip.h>
80#include <netinet/in_pcb.h>
81
82#include <ctype.h>
83#include <err.h>
84#include <fcntl.h>
85#include <kvm.h>
86#include <limits.h>
87#include <nlist.h>
88#include <paths.h>
89#include <pwd.h>
90#include <stdio.h>
91#include <stdlib.h>
92#include <string.h>
93#include <unistd.h>
94#include <netdb.h>
95
96#define	TEXT	-1
97#define	CDIR	-2
98#define	RDIR	-3
99#define	TRACE	-4
100
101typedef struct devs {
102	struct	devs *next;
103	long	fsid;
104	ino_t	ino;
105	char	*name;
106} DEVS;
107DEVS *devs;
108
109struct  filestat {
110	long	fsid;
111	long	fileid;
112	mode_t	mode;
113	u_long	size;
114	dev_t	rdev;
115};
116
117#ifdef notdef
118struct nlist nl[] = {
119	{ "" },
120};
121#endif
122
123int 	fsflg,	/* show files on same filesystem as file(s) argument */
124	pflg,	/* show files open by a particular pid */
125	uflg;	/* show files open by a particular (effective) user */
126int 	checkfile; /* true if restricting to particular files or filesystems */
127int	nflg;	/* (numerical) display f.s. and rdev as dev_t */
128int	vflg;	/* display errors in locating kernel data objects etc... */
129
130#define dprintf	if (vflg) fprintf
131
132struct file **ofiles;	/* buffer of pointers to file structures */
133int maxfiles;
134#define ALLOC_OFILES(d)	\
135	if ((d) > maxfiles) { \
136		free(ofiles); \
137		ofiles = malloc((d) * sizeof(struct file *)); \
138		if (ofiles == NULL) { \
139			err(1, NULL); \
140		} \
141		maxfiles = (d); \
142	}
143
144/*
145 * a kvm_read that returns true if everything is read
146 */
147#define KVM_READ(kaddr, paddr, len) \
148	(kvm_read(kd, (u_long)(kaddr), (char *)(paddr), (len)) == (len))
149
150kvm_t *kd;
151
152void dofiles __P((struct kinfo_proc *kp));
153void vtrans __P((struct vnode *vp, int i, int flag));
154int  ufs_filestat __P((struct vnode *vp, struct filestat *fsp));
155int  nfs_filestat __P((struct vnode *vp, struct filestat *fsp));
156char *getmnton __P((struct mount *m));
157void pipetrans __P((struct pipe *pi, int i, int flag));
158void socktrans __P((struct socket *sock, int i));
159void getinetproto __P((int number));
160int  getfname __P((char *filename));
161udev_t dev2udev __P((dev_t dev));
162void usage __P((void));
163
164
165int
166main(argc, argv)
167	int argc;
168	char **argv;
169{
170	register struct passwd *passwd;
171	struct kinfo_proc *p, *plast;
172	int arg, ch, what;
173	char *memf, *nlistf;
174	char buf[_POSIX2_LINE_MAX];
175	int cnt;
176
177	arg = 0;
178	what = KERN_PROC_ALL;
179	nlistf = memf = NULL;
180	while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1)
181		switch((char)ch) {
182		case 'f':
183			fsflg = 1;
184			break;
185		case 'M':
186			memf = optarg;
187			break;
188		case 'N':
189			nlistf = optarg;
190			break;
191		case 'n':
192			nflg = 1;
193			break;
194		case 'p':
195			if (pflg++)
196				usage();
197			if (!isdigit(*optarg)) {
198				warnx("-p requires a process id");
199				usage();
200			}
201			what = KERN_PROC_PID;
202			arg = atoi(optarg);
203			break;
204		case 'u':
205			if (uflg++)
206				usage();
207			if (!(passwd = getpwnam(optarg)))
208				errx(1, "%s: unknown uid", optarg);
209			what = KERN_PROC_UID;
210			arg = passwd->pw_uid;
211			break;
212		case 'v':
213			vflg = 1;
214			break;
215		case '?':
216		default:
217			usage();
218		}
219
220	if (*(argv += optind)) {
221		for (; *argv; ++argv) {
222			if (getfname(*argv))
223				checkfile = 1;
224		}
225		if (!checkfile)	/* file(s) specified, but none accessable */
226			exit(1);
227	}
228
229	ALLOC_OFILES(256);	/* reserve space for file pointers */
230
231	if (fsflg && !checkfile) {
232		/* -f with no files means use wd */
233		if (getfname(".") == 0)
234			exit(1);
235		checkfile = 1;
236	}
237
238	/*
239	 * Discard setgid privileges if not the running kernel so that bad
240	 * guys can't print interesting stuff from kernel memory.
241	 */
242	if (nlistf != NULL || memf != NULL)
243		setgid(getgid());
244
245	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
246		errx(1, "%s", buf);
247#ifdef notdef
248	if (kvm_nlist(kd, nl) != 0)
249		errx(1, "no namelist: %s", kvm_geterr(kd));
250#endif
251	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL)
252		errx(1, "%s", kvm_geterr(kd));
253	if (nflg)
254		printf("%s",
255"USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
256	else
257		printf("%s",
258"USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
259	if (checkfile && fsflg == 0)
260		printf(" NAME\n");
261	else
262		putchar('\n');
263
264	for (plast = &p[cnt]; p < plast; ++p) {
265		if (p->kp_proc.p_stat == SZOMB)
266			continue;
267		dofiles(p);
268	}
269	exit(0);
270}
271
272char	*Uname, *Comm;
273int	Pid;
274
275#define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
276	switch(i) { \
277	case TEXT: \
278		printf(" text"); \
279		break; \
280	case CDIR: \
281		printf("   wd"); \
282		break; \
283	case RDIR: \
284		printf(" root"); \
285		break; \
286	case TRACE: \
287		printf("   tr"); \
288		break; \
289	default: \
290		printf(" %4d", i); \
291		break; \
292	}
293
294/*
295 * print open files attributed to this process
296 */
297void
298dofiles(kp)
299	struct kinfo_proc *kp;
300{
301	int i;
302	struct file file;
303	struct filedesc0 filed0;
304#define	filed	filed0.fd_fd
305	struct proc *p = &kp->kp_proc;
306	struct eproc *ep = &kp->kp_eproc;
307
308	Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
309	Pid = p->p_pid;
310	Comm = p->p_comm;
311
312	if (p->p_fd == NULL)
313		return;
314	if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
315		dprintf(stderr, "can't read filedesc at %p for pid %d\n",
316		    (void *)p->p_fd, Pid);
317		return;
318	}
319	/*
320	 * root directory vnode, if one
321	 */
322	if (filed.fd_rdir)
323		vtrans(filed.fd_rdir, RDIR, FREAD);
324	/*
325	 * current working directory vnode
326	 */
327	vtrans(filed.fd_cdir, CDIR, FREAD);
328	/*
329	 * ktrace vnode, if one
330	 */
331	if (p->p_tracep)
332		vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
333	/*
334	 * text vnode, if one
335	 */
336	if (p->p_textvp)
337		vtrans(p->p_textvp, TEXT, FREAD);
338	/*
339	 * open files
340	 */
341#define FPSIZE	(sizeof (struct file *))
342	ALLOC_OFILES(filed.fd_lastfile+1);
343	if (filed.fd_nfiles > NDFILE) {
344		if (!KVM_READ(filed.fd_ofiles, ofiles,
345		    (filed.fd_lastfile+1) * FPSIZE)) {
346			dprintf(stderr,
347			    "can't read file structures at %p for pid %d\n",
348			    (void *)filed.fd_ofiles, Pid);
349			return;
350		}
351	} else
352		bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
353	for (i = 0; i <= filed.fd_lastfile; i++) {
354		if (ofiles[i] == NULL)
355			continue;
356		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
357			dprintf(stderr, "can't read file %d at %p for pid %d\n",
358			    i, (void *)ofiles[i], Pid);
359			continue;
360		}
361		if (file.f_type == DTYPE_VNODE)
362			vtrans((struct vnode *)file.f_data, i, file.f_flag);
363		else if (file.f_type == DTYPE_SOCKET) {
364			if (checkfile == 0)
365				socktrans((struct socket *)file.f_data, i);
366		}
367#ifdef DTYPE_PIPE
368		else if (file.f_type == DTYPE_PIPE) {
369			if (checkfile == 0)
370				pipetrans((struct pipe *)file.f_data, i,
371					file.f_flag);
372		}
373#endif
374		else {
375			dprintf(stderr,
376				"unknown file type %d for file %d of pid %d\n",
377				file.f_type, i, Pid);
378		}
379	}
380}
381
382void
383vtrans(vp, i, flag)
384	struct vnode *vp;
385	int i;
386	int flag;
387{
388	struct vnode vn;
389	struct filestat fst;
390	char rw[3], mode[15];
391	char *badtype = NULL, *filename, *getmnton();
392
393	filename = badtype = NULL;
394	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
395		dprintf(stderr, "can't read vnode at %p for pid %d\n",
396		    (void *)vp, Pid);
397		return;
398	}
399	if (vn.v_type == VNON || vn.v_tag == VT_NON)
400		badtype = "none";
401	else if (vn.v_type == VBAD)
402		badtype = "bad";
403	else
404		switch (vn.v_tag) {
405		case VT_UFS:
406			if (!ufs_filestat(&vn, &fst))
407				badtype = "error";
408			break;
409		case VT_MFS:
410			if (!ufs_filestat(&vn, &fst))
411				badtype = "error";
412			break;
413		case VT_NFS:
414			if (!nfs_filestat(&vn, &fst))
415				badtype = "error";
416			break;
417		default: {
418			static char unknown[10];
419			sprintf(badtype = unknown, "?(%x)", vn.v_tag);
420			break;;
421		}
422	}
423	if (checkfile) {
424		int fsmatch = 0;
425		register DEVS *d;
426
427		if (badtype)
428			return;
429		for (d = devs; d != NULL; d = d->next)
430			if (d->fsid == fst.fsid) {
431				fsmatch = 1;
432				if (d->ino == fst.fileid) {
433					filename = d->name;
434					break;
435				}
436			}
437		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
438			return;
439	}
440	PREFIX(i);
441	if (badtype) {
442		(void)printf(" -         -  %10s    -\n", badtype);
443		return;
444	}
445	if (nflg)
446		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
447	else
448		(void)printf(" %-8s", getmnton(vn.v_mount));
449	if (nflg)
450		(void)sprintf(mode, "%o", fst.mode);
451	else
452		strmode(fst.mode, mode);
453	(void)printf(" %6ld %10s", fst.fileid, mode);
454	switch (vn.v_type) {
455	case VBLK:
456	case VCHR: {
457		char *name;
458
459		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
460		    S_IFCHR : S_IFBLK)) == NULL))
461			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
462		else
463			printf(" %6s", name);
464		break;
465	}
466	default:
467		printf(" %6lu", fst.size);
468	}
469	rw[0] = '\0';
470	if (flag & FREAD)
471		strcat(rw, "r");
472	if (flag & FWRITE)
473		strcat(rw, "w");
474	printf(" %2s", rw);
475	if (filename && !fsflg)
476		printf("  %s", filename);
477	putchar('\n');
478}
479
480int
481ufs_filestat(vp, fsp)
482	struct vnode *vp;
483	struct filestat *fsp;
484{
485	struct inode inode;
486
487	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
488		dprintf(stderr, "can't read inode at %p for pid %d\n",
489		    (void *)VTOI(vp), Pid);
490		return 0;
491	}
492	/*
493	 * The st_dev from stat(2) is a udev_t. These kernel structures
494	 * contain dev_t structures. We need to convert to udev to make
495	 * comparisons
496	 */
497	fsp->fsid = dev2udev(inode.i_dev) & 0xffff;
498	fsp->fileid = (long)inode.i_number;
499	fsp->mode = (mode_t)inode.i_mode;
500	fsp->size = (u_long)inode.i_size;
501	fsp->rdev = inode.i_rdev;
502
503	return 1;
504}
505
506int
507nfs_filestat(vp, fsp)
508	struct vnode *vp;
509	struct filestat *fsp;
510{
511	struct nfsnode nfsnode;
512	register mode_t mode;
513
514	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
515		dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
516		    (void *)VTONFS(vp), Pid);
517		return 0;
518	}
519	fsp->fsid = nfsnode.n_vattr.va_fsid;
520	fsp->fileid = nfsnode.n_vattr.va_fileid;
521	fsp->size = nfsnode.n_size;
522	fsp->rdev = nfsnode.n_vattr.va_rdev;
523	mode = (mode_t)nfsnode.n_vattr.va_mode;
524	switch (vp->v_type) {
525	case VREG:
526		mode |= S_IFREG;
527		break;
528	case VDIR:
529		mode |= S_IFDIR;
530		break;
531	case VBLK:
532		mode |= S_IFBLK;
533		break;
534	case VCHR:
535		mode |= S_IFCHR;
536		break;
537	case VLNK:
538		mode |= S_IFLNK;
539		break;
540	case VSOCK:
541		mode |= S_IFSOCK;
542		break;
543	case VFIFO:
544		mode |= S_IFIFO;
545		break;
546	case VNON:
547	case VBAD:
548		return 0;
549	};
550	fsp->mode = mode;
551
552	return 1;
553}
554
555
556char *
557getmnton(m)
558	struct mount *m;
559{
560	static struct mount mount;
561	static struct mtab {
562		struct mtab *next;
563		struct mount *m;
564		char mntonname[MNAMELEN];
565	} *mhead = NULL;
566	register struct mtab *mt;
567
568	for (mt = mhead; mt != NULL; mt = mt->next)
569		if (m == mt->m)
570			return (mt->mntonname);
571	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
572		warnx("can't read mount table at %p", (void *)m);
573		return (NULL);
574	}
575	if ((mt = malloc(sizeof (struct mtab))) == NULL)
576		err(1, NULL);
577	mt->m = m;
578	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
579	mt->next = mhead;
580	mhead = mt;
581	return (mt->mntonname);
582}
583
584void
585pipetrans(pi, i, flag)
586	struct pipe *pi;
587	int i;
588	int flag;
589{
590	struct pipe pip;
591	char rw[3];
592
593	PREFIX(i);
594
595	/* fill in socket */
596	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
597		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
598		goto bad;
599	}
600
601	printf("* pipe %8x <-> %8x", (int)pi, (int)pip.pipe_peer);
602	printf(" %6d", (int)pip.pipe_buffer.cnt);
603	rw[0] = '\0';
604	if (flag & FREAD)
605		strcat(rw, "r");
606	if (flag & FWRITE)
607		strcat(rw, "w");
608	printf(" %2s", rw);
609	putchar('\n');
610	return;
611
612bad:
613	printf("* error\n");
614}
615
616void
617socktrans(sock, i)
618	struct socket *sock;
619	int i;
620{
621	static char *stypename[] = {
622		"unused",	/* 0 */
623		"stream", 	/* 1 */
624		"dgram",	/* 2 */
625		"raw",		/* 3 */
626		"rdm",		/* 4 */
627		"seqpak"	/* 5 */
628	};
629#define	STYPEMAX 5
630	struct socket	so;
631	struct protosw	proto;
632	struct domain	dom;
633	struct inpcb	inpcb;
634	struct unpcb	unpcb;
635	int len;
636	char dname[32], *strcpy();
637
638	PREFIX(i);
639
640	/* fill in socket */
641	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
642		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
643		goto bad;
644	}
645
646	/* fill in protosw entry */
647	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
648		dprintf(stderr, "can't read protosw at %p",
649		    (void *)so.so_proto);
650		goto bad;
651	}
652
653	/* fill in domain */
654	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
655		dprintf(stderr, "can't read domain at %p\n",
656		    (void *)proto.pr_domain);
657		goto bad;
658	}
659
660	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
661	    sizeof(dname) - 1)) < 0) {
662		dprintf(stderr, "can't read domain name at %p\n",
663		    (void *)dom.dom_name);
664		dname[0] = '\0';
665	}
666	else
667		dname[len] = '\0';
668
669	if ((u_short)so.so_type > STYPEMAX)
670		printf("* %s ?%d", dname, so.so_type);
671	else
672		printf("* %s %s", dname, stypename[so.so_type]);
673
674	/*
675	 * protocol specific formatting
676	 *
677	 * Try to find interesting things to print.  For tcp, the interesting
678	 * thing is the address of the tcpcb, for udp and others, just the
679	 * inpcb (socket pcb).  For unix domain, its the address of the socket
680	 * pcb and the address of the connected pcb (if connected).  Otherwise
681	 * just print the protocol number and address of the socket itself.
682	 * The idea is not to duplicate netstat, but to make available enough
683	 * information for further analysis.
684	 */
685	switch(dom.dom_family) {
686	case AF_INET:
687	case AF_INET6:
688		getinetproto(proto.pr_protocol);
689		if (proto.pr_protocol == IPPROTO_TCP ) {
690			if (so.so_pcb) {
691				if (kvm_read(kd, (u_long)so.so_pcb,
692				    (char *)&inpcb, sizeof(struct inpcb))
693				    != sizeof(struct inpcb)) {
694					dprintf(stderr,
695					    "can't read inpcb at %p\n",
696					    (void *)so.so_pcb);
697					goto bad;
698				}
699				printf(" %x", (int)inpcb.inp_ppcb);
700			}
701		}
702		else if (so.so_pcb)
703			printf(" %x", (int)so.so_pcb);
704		break;
705	case AF_UNIX:
706		/* print address of pcb and connected pcb */
707		if (so.so_pcb) {
708			printf(" %x", (int)so.so_pcb);
709			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
710			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
711				dprintf(stderr, "can't read unpcb at %p\n",
712				    (void *)so.so_pcb);
713				goto bad;
714			}
715			if (unpcb.unp_conn) {
716				char shoconn[4], *cp;
717
718				cp = shoconn;
719				if (!(so.so_state & SS_CANTRCVMORE))
720					*cp++ = '<';
721				*cp++ = '-';
722				if (!(so.so_state & SS_CANTSENDMORE))
723					*cp++ = '>';
724				*cp = '\0';
725				printf(" %s %x", shoconn,
726				    (int)unpcb.unp_conn);
727			}
728		}
729		break;
730	default:
731		/* print protocol number and socket address */
732		printf(" %d %x", proto.pr_protocol, (int)sock);
733	}
734	printf("\n");
735	return;
736bad:
737	printf("* error\n");
738}
739
740
741/*
742 * Read the specinfo structure in the kernel (as pointed to by a dev_t)
743 * in order to work out the associated udev_t
744 */
745udev_t
746dev2udev(dev)
747	dev_t dev;
748{
749	struct specinfo si;
750
751	if (KVM_READ(dev, &si, sizeof si)) {
752		return si.si_udev;
753	} else {
754		dprintf(stderr, "can't convert dev_t %p to a udev_t\n",
755		    (void *)dev);
756		return -1;
757	}
758}
759
760/*
761 * getinetproto --
762 *	print name of protocol number
763 */
764void
765getinetproto(number)
766	int number;
767{
768	static int isopen;
769	register struct protoent *pe;
770
771	if (!isopen)
772		setprotoent(++isopen);
773	if ((pe = getprotobynumber(number)) != NULL)
774		printf(" %s", pe->p_name);
775	else
776		printf(" %d", number);
777}
778
779int
780getfname(filename)
781	char *filename;
782{
783	struct stat statbuf;
784	DEVS *cur;
785
786	if (stat(filename, &statbuf)) {
787		warn("%s", filename);
788		return(0);
789	}
790	if ((cur = malloc(sizeof(DEVS))) == NULL)
791		err(1, NULL);
792	cur->next = devs;
793	devs = cur;
794
795	cur->ino = statbuf.st_ino;
796	cur->fsid = statbuf.st_dev & 0xffff;
797	cur->name = filename;
798	return(1);
799}
800
801void
802usage()
803{
804	(void)fprintf(stderr,
805 "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
806	exit(1);
807}
808