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