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