xref: /freebsd/usr.bin/fstat/fstat.c (revision 1c17fc996d386433fe8843024b5c8dec4226ff43)
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 	 * open files
336 	 */
337 #define FPSIZE	(sizeof (struct file *))
338 	ALLOC_OFILES(filed.fd_lastfile+1);
339 	if (filed.fd_nfiles > NDFILE) {
340 		if (!KVM_READ(filed.fd_ofiles, ofiles,
341 		    (filed.fd_lastfile+1) * FPSIZE)) {
342 			dprintf(stderr,
343 			    "can't read file structures at %x for pid %d\n",
344 			    filed.fd_ofiles, Pid);
345 			return;
346 		}
347 	} else
348 		bcopy(filed0.fd_dfiles, ofiles, (filed.fd_lastfile+1) * FPSIZE);
349 	for (i = 0; i <= filed.fd_lastfile; i++) {
350 		if (ofiles[i] == NULL)
351 			continue;
352 		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
353 			dprintf(stderr, "can't read file %d at %x for pid %d\n",
354 				i, ofiles[i], Pid);
355 			continue;
356 		}
357 		if (file.f_type == DTYPE_VNODE)
358 			vtrans((struct vnode *)file.f_data, i, file.f_flag);
359 		else if (file.f_type == DTYPE_SOCKET) {
360 			if (checkfile == 0)
361 				socktrans((struct socket *)file.f_data, i);
362 		}
363 #ifdef DTYPE_PIPE
364 		else if (file.f_type == DTYPE_PIPE) {
365 			if (checkfile == 0)
366 				pipetrans((struct pipe *)file.f_data, i,
367 					file.f_flag);
368 		}
369 #endif
370 		else {
371 			dprintf(stderr,
372 				"unknown file type %d for file %d of pid %d\n",
373 				file.f_type, i, Pid);
374 		}
375 	}
376 }
377 
378 void
379 vtrans(vp, i, flag)
380 	struct vnode *vp;
381 	int i;
382 	int flag;
383 {
384 	struct vnode vn;
385 	struct filestat fst;
386 	char rw[3], mode[15];
387 	char *badtype = NULL, *filename, *getmnton();
388 
389 	filename = badtype = NULL;
390 	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
391 		dprintf(stderr, "can't read vnode at %x for pid %d\n",
392 			vp, Pid);
393 		return;
394 	}
395 	if (vn.v_type == VNON || vn.v_tag == VT_NON)
396 		badtype = "none";
397 	else if (vn.v_type == VBAD)
398 		badtype = "bad";
399 	else
400 		switch (vn.v_tag) {
401 		case VT_UFS:
402 			if (!ufs_filestat(&vn, &fst))
403 				badtype = "error";
404 			break;
405 		case VT_MFS:
406 			if (!ufs_filestat(&vn, &fst))
407 				badtype = "error";
408 			break;
409 		case VT_NFS:
410 			if (!nfs_filestat(&vn, &fst))
411 				badtype = "error";
412 			break;
413 		default: {
414 			static char unknown[10];
415 			sprintf(badtype = unknown, "?(%x)", vn.v_tag);
416 			break;;
417 		}
418 	}
419 	if (checkfile) {
420 		int fsmatch = 0;
421 		register DEVS *d;
422 
423 		if (badtype)
424 			return;
425 		for (d = devs; d != NULL; d = d->next)
426 			if (d->fsid == fst.fsid) {
427 				fsmatch = 1;
428 				if (d->ino == fst.fileid) {
429 					filename = d->name;
430 					break;
431 				}
432 			}
433 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
434 			return;
435 	}
436 	PREFIX(i);
437 	if (badtype) {
438 		(void)printf(" -         -  %10s    -\n", badtype);
439 		return;
440 	}
441 	if (nflg)
442 		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
443 	else
444 		(void)printf(" %-8s", getmnton(vn.v_mount));
445 	if (nflg)
446 		(void)sprintf(mode, "%o", fst.mode);
447 	else
448 		strmode(fst.mode, mode);
449 	(void)printf(" %6d %10s", fst.fileid, mode);
450 	switch (vn.v_type) {
451 	case VBLK:
452 	case VCHR: {
453 		char *name;
454 
455 		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
456 		    S_IFCHR : S_IFBLK)) == NULL))
457 			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
458 		else
459 			printf(" %6s", name);
460 		break;
461 	}
462 	default:
463 		printf(" %6d", fst.size);
464 	}
465 	rw[0] = '\0';
466 	if (flag & FREAD)
467 		strcat(rw, "r");
468 	if (flag & FWRITE)
469 		strcat(rw, "w");
470 	printf(" %2s", rw);
471 	if (filename && !fsflg)
472 		printf("  %s", filename);
473 	putchar('\n');
474 }
475 
476 int
477 ufs_filestat(vp, fsp)
478 	struct vnode *vp;
479 	struct filestat *fsp;
480 {
481 	struct inode inode;
482 
483 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
484 		dprintf(stderr, "can't read inode at %x for pid %d\n",
485 			VTOI(vp), Pid);
486 		return 0;
487 	}
488 	fsp->fsid = inode.i_dev & 0xffff;
489 	fsp->fileid = (long)inode.i_number;
490 	fsp->mode = (mode_t)inode.i_mode;
491 	fsp->size = (u_long)inode.i_size;
492 	fsp->rdev = inode.i_rdev;
493 
494 	return 1;
495 }
496 
497 int
498 nfs_filestat(vp, fsp)
499 	struct vnode *vp;
500 	struct filestat *fsp;
501 {
502 	struct nfsnode nfsnode;
503 	register mode_t mode;
504 
505 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
506 		dprintf(stderr, "can't read nfsnode at %x for pid %d\n",
507 			VTONFS(vp), Pid);
508 		return 0;
509 	}
510 	fsp->fsid = nfsnode.n_vattr.va_fsid;
511 	fsp->fileid = nfsnode.n_vattr.va_fileid;
512 	fsp->size = nfsnode.n_size;
513 	fsp->rdev = nfsnode.n_vattr.va_rdev;
514 	mode = (mode_t)nfsnode.n_vattr.va_mode;
515 	switch (vp->v_type) {
516 	case VREG:
517 		mode |= S_IFREG;
518 		break;
519 	case VDIR:
520 		mode |= S_IFDIR;
521 		break;
522 	case VBLK:
523 		mode |= S_IFBLK;
524 		break;
525 	case VCHR:
526 		mode |= S_IFCHR;
527 		break;
528 	case VLNK:
529 		mode |= S_IFLNK;
530 		break;
531 	case VSOCK:
532 		mode |= S_IFSOCK;
533 		break;
534 	case VFIFO:
535 		mode |= S_IFIFO;
536 		break;
537 	};
538 	fsp->mode = mode;
539 
540 	return 1;
541 }
542 
543 
544 char *
545 getmnton(m)
546 	struct mount *m;
547 {
548 	static struct mount mount;
549 	static struct mtab {
550 		struct mtab *next;
551 		struct mount *m;
552 		char mntonname[MNAMELEN];
553 	} *mhead = NULL;
554 	register struct mtab *mt;
555 
556 	for (mt = mhead; mt != NULL; mt = mt->next)
557 		if (m == mt->m)
558 			return (mt->mntonname);
559 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
560 		fprintf(stderr, "can't read mount table at %x\n", m);
561 		return (NULL);
562 	}
563 	if ((mt = malloc(sizeof (struct mtab))) == NULL) {
564 		fprintf(stderr, "fstat: %s\n", strerror(errno));
565 		exit(1);
566 	}
567 	mt->m = m;
568 	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
569 	mt->next = mhead;
570 	mhead = mt;
571 	return (mt->mntonname);
572 }
573 
574 void
575 pipetrans(pi, i, flag)
576 	struct pipe *pi;
577 	int i;
578 	int flag;
579 {
580 	struct pipe pip;
581 	char rw[3];
582 
583 	PREFIX(i);
584 
585 	/* fill in socket */
586 	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
587 		dprintf(stderr, "can't read pipe at %x\n", pi);
588 		goto bad;
589 	}
590 
591 	printf("* pipe %8x <-> %8x", (int)pi, (int)pip.pipe_peer);
592 	printf(" %6d", (int)pip.pipe_buffer.cnt);
593 	rw[0] = '\0';
594 	if (flag & FREAD)
595 		strcat(rw, "r");
596 	if (flag & FWRITE)
597 		strcat(rw, "w");
598 	printf(" %2s", rw);
599 	putchar('\n');
600 	return;
601 
602 bad:
603 	printf("* error\n");
604 }
605 
606 void
607 socktrans(sock, i)
608 	struct socket *sock;
609 	int i;
610 {
611 	static char *stypename[] = {
612 		"unused",	/* 0 */
613 		"stream", 	/* 1 */
614 		"dgram",	/* 2 */
615 		"raw",		/* 3 */
616 		"rdm",		/* 4 */
617 		"seqpak"	/* 5 */
618 	};
619 #define	STYPEMAX 5
620 	struct socket	so;
621 	struct protosw	proto;
622 	struct domain	dom;
623 	struct inpcb	inpcb;
624 	struct unpcb	unpcb;
625 	int len;
626 	char dname[32], *strcpy();
627 
628 	PREFIX(i);
629 
630 	/* fill in socket */
631 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
632 		dprintf(stderr, "can't read sock at %x\n", sock);
633 		goto bad;
634 	}
635 
636 	/* fill in protosw entry */
637 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
638 		dprintf(stderr, "can't read protosw at %x", so.so_proto);
639 		goto bad;
640 	}
641 
642 	/* fill in domain */
643 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
644 		dprintf(stderr, "can't read domain at %x\n", proto.pr_domain);
645 		goto bad;
646 	}
647 
648 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
649 	    sizeof(dname) - 1)) < 0) {
650 		dprintf(stderr, "can't read domain name at %x\n",
651 			dom.dom_name);
652 		dname[0] = '\0';
653 	}
654 	else
655 		dname[len] = '\0';
656 
657 	if ((u_short)so.so_type > STYPEMAX)
658 		printf("* %s ?%d", dname, so.so_type);
659 	else
660 		printf("* %s %s", dname, stypename[so.so_type]);
661 
662 	/*
663 	 * protocol specific formatting
664 	 *
665 	 * Try to find interesting things to print.  For tcp, the interesting
666 	 * thing is the address of the tcpcb, for udp and others, just the
667 	 * inpcb (socket pcb).  For unix domain, its the address of the socket
668 	 * pcb and the address of the connected pcb (if connected).  Otherwise
669 	 * just print the protocol number and address of the socket itself.
670 	 * The idea is not to duplicate netstat, but to make available enough
671 	 * information for further analysis.
672 	 */
673 	switch(dom.dom_family) {
674 	case AF_INET:
675 		getinetproto(proto.pr_protocol);
676 		if (proto.pr_protocol == IPPROTO_TCP ) {
677 			if (so.so_pcb) {
678 				if (kvm_read(kd, (u_long)so.so_pcb,
679 				    (char *)&inpcb, sizeof(struct inpcb))
680 				    != sizeof(struct inpcb)) {
681 					dprintf(stderr,
682 					    "can't read inpcb at %x\n",
683 					    so.so_pcb);
684 					goto bad;
685 				}
686 				printf(" %x", (int)inpcb.inp_ppcb);
687 			}
688 		}
689 		else if (so.so_pcb)
690 			printf(" %x", (int)so.so_pcb);
691 		break;
692 	case AF_UNIX:
693 		/* print address of pcb and connected pcb */
694 		if (so.so_pcb) {
695 			printf(" %x", (int)so.so_pcb);
696 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
697 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
698 				dprintf(stderr, "can't read unpcb at %x\n",
699 				    so.so_pcb);
700 				goto bad;
701 			}
702 			if (unpcb.unp_conn) {
703 				char shoconn[4], *cp;
704 
705 				cp = shoconn;
706 				if (!(so.so_state & SS_CANTRCVMORE))
707 					*cp++ = '<';
708 				*cp++ = '-';
709 				if (!(so.so_state & SS_CANTSENDMORE))
710 					*cp++ = '>';
711 				*cp = '\0';
712 				printf(" %s %x", shoconn,
713 				    (int)unpcb.unp_conn);
714 			}
715 		}
716 		break;
717 	default:
718 		/* print protocol number and socket address */
719 		printf(" %d %x", proto.pr_protocol, (int)sock);
720 	}
721 	printf("\n");
722 	return;
723 bad:
724 	printf("* error\n");
725 }
726 
727 /*
728  * getinetproto --
729  *	print name of protocol number
730  */
731 void
732 getinetproto(number)
733 	int number;
734 {
735 	char *cp;
736 
737 	switch(number) {
738 	case IPPROTO_IP:
739 		cp = "ip"; break;
740 	case IPPROTO_ICMP:
741 		cp ="icmp"; break;
742 	case IPPROTO_GGP:
743 		cp ="ggp"; break;
744 	case IPPROTO_TCP:
745 		cp ="tcp"; break;
746 	case IPPROTO_EGP:
747 		cp ="egp"; break;
748 	case IPPROTO_PUP:
749 		cp ="pup"; break;
750 	case IPPROTO_UDP:
751 		cp ="udp"; break;
752 	case IPPROTO_IDP:
753 		cp ="idp"; break;
754 	case IPPROTO_RAW:
755 		cp ="raw"; break;
756 	default:
757 		printf(" %d", number);
758 		return;
759 	}
760 	printf(" %s", cp);
761 }
762 
763 int
764 getfname(filename)
765 	char *filename;
766 {
767 	struct stat statbuf;
768 	DEVS *cur;
769 
770 	if (stat(filename, &statbuf)) {
771 		fprintf(stderr, "fstat: %s: %s\n", filename, strerror(errno));
772 		return(0);
773 	}
774 	if ((cur = malloc(sizeof(DEVS))) == NULL) {
775 		fprintf(stderr, "fstat: %s\n", strerror(errno));
776 		exit(1);
777 	}
778 	cur->next = devs;
779 	devs = cur;
780 
781 	cur->ino = statbuf.st_ino;
782 	cur->fsid = statbuf.st_dev & 0xffff;
783 	cur->name = filename;
784 	return(1);
785 }
786 
787 void
788 usage()
789 {
790 	(void)fprintf(stderr,
791  "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
792 	exit(1);
793 }
794