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