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