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