xref: /freebsd/usr.bin/fstat/fstat.c (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
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 #undef _KERNEL
70 #include <sys/mount.h>
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->kp_proc.p_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 	struct proc *p = &kp->kp_proc;
302 	struct eproc *ep = &kp->kp_eproc;
303 
304 	Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
305 	Pid = p->p_pid;
306 	Comm = p->p_comm;
307 
308 	if (p->p_fd == NULL)
309 		return;
310 	if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
311 		dprintf(stderr, "can't read filedesc at %p for pid %d\n",
312 		    (void *)p->p_fd, Pid);
313 		return;
314 	}
315 	/*
316 	 * root directory vnode, if one
317 	 */
318 	if (filed.fd_rdir)
319 		vtrans(filed.fd_rdir, RDIR, FREAD);
320 	/*
321 	 * current working directory vnode
322 	 */
323 	vtrans(filed.fd_cdir, CDIR, FREAD);
324 	/*
325 	 * ktrace vnode, if one
326 	 */
327 	if (p->p_tracep)
328 		vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
329 	/*
330 	 * text vnode, if one
331 	 */
332 	if (p->p_textvp)
333 		vtrans(p->p_textvp, TEXT, FREAD);
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 %p for pid %d\n",
344 			    (void *)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 %p for pid %d\n",
354 			    i, (void *)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 dommap(kp)
380 	struct kinfo_proc *kp;
381 {
382 	struct proc *p = &kp->kp_proc;
383 	struct vmspace vmspace;
384 	vm_map_t map;
385 	struct vm_map_entry entry;
386 	vm_map_entry_t entryp;
387 	struct vm_object object;
388 	vm_object_t objp;
389 	int prot, fflags;
390 
391 	if (!KVM_READ(p->p_vmspace, &vmspace, sizeof(vmspace))) {
392 		dprintf(stderr, "can't read vmspace at %p for pid %d\n",
393 		    (void *)p->p_vmspace, Pid);
394 		return;
395 	}
396 
397 	map = &vmspace.vm_map;
398 
399 	for (entryp = map->header.next; entryp != &p->p_vmspace->vm_map.header;
400 	    entryp = entry.next) {
401 		if (!KVM_READ(entryp, &entry, sizeof(entry))) {
402 			dprintf(stderr,
403 			    "can't read vm_map_entry at %p for pid %d\n",
404 			    (void *)entryp, Pid);
405 			return;
406 		}
407 
408 		if (entry.eflags & MAP_ENTRY_IS_SUB_MAP)
409 			continue;
410 
411 		if ((objp = entry.object.vm_object) == NULL)
412 			continue;
413 
414 		for (; objp; objp = object.backing_object) {
415 			if (!KVM_READ(objp, &object, sizeof(object))) {
416 				dprintf(stderr,
417 				    "can't read vm_object at %p for pid %d\n",
418 				    (void *)objp, Pid);
419 				return;
420 			}
421 		}
422 
423 		prot = entry.protection;
424 		fflags = (prot & VM_PROT_READ ? FREAD : 0) |
425 		    (prot & VM_PROT_WRITE ? FWRITE : 0);
426 
427 		switch (object.type) {
428 		case OBJT_VNODE:
429 			vtrans((struct vnode *)object.handle, MMAP, fflags);
430 			break;
431 		default:
432 			break;
433 		}
434 	}
435 }
436 
437 void
438 vtrans(vp, i, flag)
439 	struct vnode *vp;
440 	int i;
441 	int flag;
442 {
443 	struct vnode vn;
444 	struct filestat fst;
445 	char rw[3], mode[15];
446 	char *badtype = NULL, *filename, *getmnton();
447 
448 	filename = badtype = NULL;
449 	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
450 		dprintf(stderr, "can't read vnode at %p for pid %d\n",
451 		    (void *)vp, Pid);
452 		return;
453 	}
454 	if (vn.v_type == VNON || vn.v_tag == VT_NON)
455 		badtype = "none";
456 	else if (vn.v_type == VBAD)
457 		badtype = "bad";
458 	else
459 		switch (vn.v_tag) {
460 		case VT_UFS:
461 			if (!ufs_filestat(&vn, &fst))
462 				badtype = "error";
463 			break;
464 		case VT_MFS:
465 			if (!ufs_filestat(&vn, &fst))
466 				badtype = "error";
467 			break;
468 		case VT_NFS:
469 			if (!nfs_filestat(&vn, &fst))
470 				badtype = "error";
471 			break;
472 
473 		case VT_MSDOSFS:
474 			if (!msdosfs_filestat(&vn, &fst))
475 				badtype = "error";
476 			break;
477 
478 		case VT_ISOFS:
479 			if (!isofs_filestat(&vn, &fst))
480 				badtype = "error";
481 			break;
482 
483 		default: {
484 			static char unknown[10];
485 			sprintf(badtype = unknown, "?(%x)", vn.v_tag);
486 			break;;
487 		}
488 	}
489 	if (checkfile) {
490 		int fsmatch = 0;
491 		register DEVS *d;
492 
493 		if (badtype)
494 			return;
495 		for (d = devs; d != NULL; d = d->next)
496 			if (d->fsid == fst.fsid) {
497 				fsmatch = 1;
498 				if (d->ino == fst.fileid) {
499 					filename = d->name;
500 					break;
501 				}
502 			}
503 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
504 			return;
505 	}
506 	PREFIX(i);
507 	if (badtype) {
508 		(void)printf(" -         -  %10s    -\n", badtype);
509 		return;
510 	}
511 	if (nflg)
512 		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
513 	else
514 		(void)printf(" %-8s", getmnton(vn.v_mount));
515 	if (nflg)
516 		(void)sprintf(mode, "%o", fst.mode);
517 	else
518 		strmode(fst.mode, mode);
519 	(void)printf(" %6ld %10s", fst.fileid, mode);
520 	switch (vn.v_type) {
521 	case VBLK:
522 	case VCHR: {
523 		char *name;
524 
525 		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
526 		    S_IFCHR : S_IFBLK)) == NULL))
527 			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
528 		else
529 			printf(" %6s", name);
530 		break;
531 	}
532 	default:
533 		printf(" %6lu", fst.size);
534 	}
535 	rw[0] = '\0';
536 	if (flag & FREAD)
537 		strcat(rw, "r");
538 	if (flag & FWRITE)
539 		strcat(rw, "w");
540 	printf(" %2s", rw);
541 	if (filename && !fsflg)
542 		printf("  %s", filename);
543 	putchar('\n');
544 }
545 
546 int
547 ufs_filestat(vp, fsp)
548 	struct vnode *vp;
549 	struct filestat *fsp;
550 {
551 	struct inode inode;
552 
553 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
554 		dprintf(stderr, "can't read inode at %p for pid %d\n",
555 		    (void *)VTOI(vp), Pid);
556 		return 0;
557 	}
558 	/*
559 	 * The st_dev from stat(2) is a udev_t. These kernel structures
560 	 * contain dev_t structures. We need to convert to udev to make
561 	 * comparisons
562 	 */
563 	fsp->fsid = dev2udev(inode.i_dev) & 0xffff;
564 	fsp->fileid = (long)inode.i_number;
565 	fsp->mode = (mode_t)inode.i_mode;
566 	fsp->size = (u_long)inode.i_size;
567 	fsp->rdev = inode.i_rdev;
568 
569 	return 1;
570 }
571 
572 int
573 nfs_filestat(vp, fsp)
574 	struct vnode *vp;
575 	struct filestat *fsp;
576 {
577 	struct nfsnode nfsnode;
578 	register mode_t mode;
579 
580 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
581 		dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
582 		    (void *)VTONFS(vp), Pid);
583 		return 0;
584 	}
585 	fsp->fsid = nfsnode.n_vattr.va_fsid;
586 	fsp->fileid = nfsnode.n_vattr.va_fileid;
587 	fsp->size = nfsnode.n_size;
588 	fsp->rdev = nfsnode.n_vattr.va_rdev;
589 	mode = (mode_t)nfsnode.n_vattr.va_mode;
590 	switch (vp->v_type) {
591 	case VREG:
592 		mode |= S_IFREG;
593 		break;
594 	case VDIR:
595 		mode |= S_IFDIR;
596 		break;
597 	case VBLK:
598 		mode |= S_IFBLK;
599 		break;
600 	case VCHR:
601 		mode |= S_IFCHR;
602 		break;
603 	case VLNK:
604 		mode |= S_IFLNK;
605 		break;
606 	case VSOCK:
607 		mode |= S_IFSOCK;
608 		break;
609 	case VFIFO:
610 		mode |= S_IFIFO;
611 		break;
612 	case VNON:
613 	case VBAD:
614 		return 0;
615 	};
616 	fsp->mode = mode;
617 
618 	return 1;
619 }
620 
621 
622 char *
623 getmnton(m)
624 	struct mount *m;
625 {
626 	static struct mount mount;
627 	static struct mtab {
628 		struct mtab *next;
629 		struct mount *m;
630 		char mntonname[MNAMELEN];
631 	} *mhead = NULL;
632 	register struct mtab *mt;
633 
634 	for (mt = mhead; mt != NULL; mt = mt->next)
635 		if (m == mt->m)
636 			return (mt->mntonname);
637 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
638 		warnx("can't read mount table at %p", (void *)m);
639 		return (NULL);
640 	}
641 	if ((mt = malloc(sizeof (struct mtab))) == NULL)
642 		err(1, NULL);
643 	mt->m = m;
644 	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
645 	mt->next = mhead;
646 	mhead = mt;
647 	return (mt->mntonname);
648 }
649 
650 void
651 pipetrans(pi, i, flag)
652 	struct pipe *pi;
653 	int i;
654 	int flag;
655 {
656 	struct pipe pip;
657 	char rw[3];
658 
659 	PREFIX(i);
660 
661 	/* fill in socket */
662 	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
663 		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
664 		goto bad;
665 	}
666 
667 	printf("* pipe %8x <-> %8x", (int)pi, (int)pip.pipe_peer);
668 	printf(" %6d", (int)pip.pipe_buffer.cnt);
669 	rw[0] = '\0';
670 	if (flag & FREAD)
671 		strcat(rw, "r");
672 	if (flag & FWRITE)
673 		strcat(rw, "w");
674 	printf(" %2s", rw);
675 	putchar('\n');
676 	return;
677 
678 bad:
679 	printf("* error\n");
680 }
681 
682 void
683 socktrans(sock, i)
684 	struct socket *sock;
685 	int i;
686 {
687 	static char *stypename[] = {
688 		"unused",	/* 0 */
689 		"stream", 	/* 1 */
690 		"dgram",	/* 2 */
691 		"raw",		/* 3 */
692 		"rdm",		/* 4 */
693 		"seqpak"	/* 5 */
694 	};
695 #define	STYPEMAX 5
696 	struct socket	so;
697 	struct protosw	proto;
698 	struct domain	dom;
699 	struct inpcb	inpcb;
700 	struct unpcb	unpcb;
701 	int len;
702 	char dname[32], *strcpy();
703 
704 	PREFIX(i);
705 
706 	/* fill in socket */
707 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
708 		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
709 		goto bad;
710 	}
711 
712 	/* fill in protosw entry */
713 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
714 		dprintf(stderr, "can't read protosw at %p",
715 		    (void *)so.so_proto);
716 		goto bad;
717 	}
718 
719 	/* fill in domain */
720 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
721 		dprintf(stderr, "can't read domain at %p\n",
722 		    (void *)proto.pr_domain);
723 		goto bad;
724 	}
725 
726 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
727 	    sizeof(dname) - 1)) < 0) {
728 		dprintf(stderr, "can't read domain name at %p\n",
729 		    (void *)dom.dom_name);
730 		dname[0] = '\0';
731 	}
732 	else
733 		dname[len] = '\0';
734 
735 	if ((u_short)so.so_type > STYPEMAX)
736 		printf("* %s ?%d", dname, so.so_type);
737 	else
738 		printf("* %s %s", dname, stypename[so.so_type]);
739 
740 	/*
741 	 * protocol specific formatting
742 	 *
743 	 * Try to find interesting things to print.  For tcp, the interesting
744 	 * thing is the address of the tcpcb, for udp and others, just the
745 	 * inpcb (socket pcb).  For unix domain, its the address of the socket
746 	 * pcb and the address of the connected pcb (if connected).  Otherwise
747 	 * just print the protocol number and address of the socket itself.
748 	 * The idea is not to duplicate netstat, but to make available enough
749 	 * information for further analysis.
750 	 */
751 	switch(dom.dom_family) {
752 	case AF_INET:
753 	case AF_INET6:
754 		getinetproto(proto.pr_protocol);
755 		if (proto.pr_protocol == IPPROTO_TCP ) {
756 			if (so.so_pcb) {
757 				if (kvm_read(kd, (u_long)so.so_pcb,
758 				    (char *)&inpcb, sizeof(struct inpcb))
759 				    != sizeof(struct inpcb)) {
760 					dprintf(stderr,
761 					    "can't read inpcb at %p\n",
762 					    (void *)so.so_pcb);
763 					goto bad;
764 				}
765 				printf(" %x", (int)inpcb.inp_ppcb);
766 			}
767 		}
768 		else if (so.so_pcb)
769 			printf(" %x", (int)so.so_pcb);
770 		break;
771 	case AF_UNIX:
772 		/* print address of pcb and connected pcb */
773 		if (so.so_pcb) {
774 			printf(" %x", (int)so.so_pcb);
775 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
776 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
777 				dprintf(stderr, "can't read unpcb at %p\n",
778 				    (void *)so.so_pcb);
779 				goto bad;
780 			}
781 			if (unpcb.unp_conn) {
782 				char shoconn[4], *cp;
783 
784 				cp = shoconn;
785 				if (!(so.so_state & SS_CANTRCVMORE))
786 					*cp++ = '<';
787 				*cp++ = '-';
788 				if (!(so.so_state & SS_CANTSENDMORE))
789 					*cp++ = '>';
790 				*cp = '\0';
791 				printf(" %s %x", shoconn,
792 				    (int)unpcb.unp_conn);
793 			}
794 		}
795 		break;
796 	default:
797 		/* print protocol number and socket address */
798 		printf(" %d %x", proto.pr_protocol, (int)sock);
799 	}
800 	printf("\n");
801 	return;
802 bad:
803 	printf("* error\n");
804 }
805 
806 
807 /*
808  * Read the specinfo structure in the kernel (as pointed to by a dev_t)
809  * in order to work out the associated udev_t
810  */
811 udev_t
812 dev2udev(dev)
813 	dev_t dev;
814 {
815 	struct specinfo si;
816 
817 	if (KVM_READ(dev, &si, sizeof si)) {
818 		return si.si_udev;
819 	} else {
820 		dprintf(stderr, "can't convert dev_t %p to a udev_t\n",
821 		    (void *)dev);
822 		return -1;
823 	}
824 }
825 
826 /*
827  * getinetproto --
828  *	print name of protocol number
829  */
830 void
831 getinetproto(number)
832 	int number;
833 {
834 	static int isopen;
835 	register struct protoent *pe;
836 
837 	if (!isopen)
838 		setprotoent(++isopen);
839 	if ((pe = getprotobynumber(number)) != NULL)
840 		printf(" %s", pe->p_name);
841 	else
842 		printf(" %d", number);
843 }
844 
845 int
846 getfname(filename)
847 	char *filename;
848 {
849 	struct stat statbuf;
850 	DEVS *cur;
851 
852 	if (stat(filename, &statbuf)) {
853 		warn("%s", filename);
854 		return(0);
855 	}
856 	if ((cur = malloc(sizeof(DEVS))) == NULL)
857 		err(1, NULL);
858 	cur->next = devs;
859 	devs = cur;
860 
861 	cur->ino = statbuf.st_ino;
862 	cur->fsid = statbuf.st_dev & 0xffff;
863 	cur->name = filename;
864 	return(1);
865 }
866 
867 void
868 usage()
869 {
870 	(void)fprintf(stderr,
871  "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
872 	exit(1);
873 }
874