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