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