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