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