xref: /freebsd/usr.bin/fstat/fstat.c (revision c678bc4f13a340ad88debe321afd0097db2590cb)
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_NFS:
463 			if (!nfs_filestat(&vn, &fst))
464 				badtype = "error";
465 			break;
466 
467 		case VT_MSDOSFS:
468 			if (!msdosfs_filestat(&vn, &fst))
469 				badtype = "error";
470 			break;
471 
472 		case VT_ISOFS:
473 			if (!isofs_filestat(&vn, &fst))
474 				badtype = "error";
475 			break;
476 
477 		default: {
478 			static char unknown[10];
479 			sprintf(badtype = unknown, "?(%x)", vn.v_tag);
480 			break;;
481 		}
482 	}
483 	if (checkfile) {
484 		int fsmatch = 0;
485 		register DEVS *d;
486 
487 		if (badtype)
488 			return;
489 		for (d = devs; d != NULL; d = d->next)
490 			if (d->fsid == fst.fsid) {
491 				fsmatch = 1;
492 				if (d->ino == fst.fileid) {
493 					filename = d->name;
494 					break;
495 				}
496 			}
497 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
498 			return;
499 	}
500 	PREFIX(i);
501 	if (badtype) {
502 		(void)printf(" -         -  %10s    -\n", badtype);
503 		return;
504 	}
505 	if (nflg)
506 		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
507 	else
508 		(void)printf(" %-8s", getmnton(vn.v_mount));
509 	if (nflg)
510 		(void)sprintf(mode, "%o", fst.mode);
511 	else
512 		strmode(fst.mode, mode);
513 	(void)printf(" %6ld %10s", fst.fileid, mode);
514 	switch (vn.v_type) {
515 	case VBLK:
516 	case VCHR: {
517 		char *name;
518 
519 		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
520 		    S_IFCHR : S_IFBLK)) == NULL))
521 			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
522 		else
523 			printf(" %6s", name);
524 		break;
525 	}
526 	default:
527 		printf(" %6lu", fst.size);
528 	}
529 	rw[0] = '\0';
530 	if (flag & FREAD)
531 		strcat(rw, "r");
532 	if (flag & FWRITE)
533 		strcat(rw, "w");
534 	printf(" %2s", rw);
535 	if (filename && !fsflg)
536 		printf("  %s", filename);
537 	putchar('\n');
538 }
539 
540 int
541 ufs_filestat(vp, fsp)
542 	struct vnode *vp;
543 	struct filestat *fsp;
544 {
545 	struct inode inode;
546 
547 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
548 		dprintf(stderr, "can't read inode at %p for pid %d\n",
549 		    (void *)VTOI(vp), Pid);
550 		return 0;
551 	}
552 	/*
553 	 * The st_dev from stat(2) is a udev_t. These kernel structures
554 	 * contain dev_t structures. We need to convert to udev to make
555 	 * comparisons
556 	 */
557 	fsp->fsid = dev2udev(inode.i_dev) & 0xffff;
558 	fsp->fileid = (long)inode.i_number;
559 	fsp->mode = (mode_t)inode.i_mode;
560 	fsp->size = (u_long)inode.i_size;
561 	fsp->rdev = inode.i_rdev;
562 
563 	return 1;
564 }
565 
566 int
567 nfs_filestat(vp, fsp)
568 	struct vnode *vp;
569 	struct filestat *fsp;
570 {
571 	struct nfsnode nfsnode;
572 	register mode_t mode;
573 
574 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
575 		dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
576 		    (void *)VTONFS(vp), Pid);
577 		return 0;
578 	}
579 	fsp->fsid = nfsnode.n_vattr.va_fsid;
580 	fsp->fileid = nfsnode.n_vattr.va_fileid;
581 	fsp->size = nfsnode.n_size;
582 	fsp->rdev = nfsnode.n_vattr.va_rdev;
583 	mode = (mode_t)nfsnode.n_vattr.va_mode;
584 	switch (vp->v_type) {
585 	case VREG:
586 		mode |= S_IFREG;
587 		break;
588 	case VDIR:
589 		mode |= S_IFDIR;
590 		break;
591 	case VBLK:
592 		mode |= S_IFBLK;
593 		break;
594 	case VCHR:
595 		mode |= S_IFCHR;
596 		break;
597 	case VLNK:
598 		mode |= S_IFLNK;
599 		break;
600 	case VSOCK:
601 		mode |= S_IFSOCK;
602 		break;
603 	case VFIFO:
604 		mode |= S_IFIFO;
605 		break;
606 	case VNON:
607 	case VBAD:
608 		return 0;
609 	};
610 	fsp->mode = mode;
611 
612 	return 1;
613 }
614 
615 
616 char *
617 getmnton(m)
618 	struct mount *m;
619 {
620 	static struct mount mount;
621 	static struct mtab {
622 		struct mtab *next;
623 		struct mount *m;
624 		char mntonname[MNAMELEN];
625 	} *mhead = NULL;
626 	register struct mtab *mt;
627 
628 	for (mt = mhead; mt != NULL; mt = mt->next)
629 		if (m == mt->m)
630 			return (mt->mntonname);
631 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
632 		warnx("can't read mount table at %p", (void *)m);
633 		return (NULL);
634 	}
635 	if ((mt = malloc(sizeof (struct mtab))) == NULL)
636 		err(1, NULL);
637 	mt->m = m;
638 	bcopy(&mount.mnt_stat.f_mntonname[0], &mt->mntonname[0], MNAMELEN);
639 	mt->next = mhead;
640 	mhead = mt;
641 	return (mt->mntonname);
642 }
643 
644 void
645 pipetrans(pi, i, flag)
646 	struct pipe *pi;
647 	int i;
648 	int flag;
649 {
650 	struct pipe pip;
651 	char rw[3];
652 
653 	PREFIX(i);
654 
655 	/* fill in socket */
656 	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
657 		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
658 		goto bad;
659 	}
660 
661 	printf("* pipe %8x <-> %8x", (int)pi, (int)pip.pipe_peer);
662 	printf(" %6d", (int)pip.pipe_buffer.cnt);
663 	rw[0] = '\0';
664 	if (flag & FREAD)
665 		strcat(rw, "r");
666 	if (flag & FWRITE)
667 		strcat(rw, "w");
668 	printf(" %2s", rw);
669 	putchar('\n');
670 	return;
671 
672 bad:
673 	printf("* error\n");
674 }
675 
676 void
677 socktrans(sock, i)
678 	struct socket *sock;
679 	int i;
680 {
681 	static char *stypename[] = {
682 		"unused",	/* 0 */
683 		"stream", 	/* 1 */
684 		"dgram",	/* 2 */
685 		"raw",		/* 3 */
686 		"rdm",		/* 4 */
687 		"seqpak"	/* 5 */
688 	};
689 #define	STYPEMAX 5
690 	struct socket	so;
691 	struct protosw	proto;
692 	struct domain	dom;
693 	struct inpcb	inpcb;
694 	struct unpcb	unpcb;
695 	int len;
696 	char dname[32], *strcpy();
697 
698 	PREFIX(i);
699 
700 	/* fill in socket */
701 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
702 		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
703 		goto bad;
704 	}
705 
706 	/* fill in protosw entry */
707 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
708 		dprintf(stderr, "can't read protosw at %p",
709 		    (void *)so.so_proto);
710 		goto bad;
711 	}
712 
713 	/* fill in domain */
714 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
715 		dprintf(stderr, "can't read domain at %p\n",
716 		    (void *)proto.pr_domain);
717 		goto bad;
718 	}
719 
720 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
721 	    sizeof(dname) - 1)) < 0) {
722 		dprintf(stderr, "can't read domain name at %p\n",
723 		    (void *)dom.dom_name);
724 		dname[0] = '\0';
725 	}
726 	else
727 		dname[len] = '\0';
728 
729 	if ((u_short)so.so_type > STYPEMAX)
730 		printf("* %s ?%d", dname, so.so_type);
731 	else
732 		printf("* %s %s", dname, stypename[so.so_type]);
733 
734 	/*
735 	 * protocol specific formatting
736 	 *
737 	 * Try to find interesting things to print.  For tcp, the interesting
738 	 * thing is the address of the tcpcb, for udp and others, just the
739 	 * inpcb (socket pcb).  For unix domain, its the address of the socket
740 	 * pcb and the address of the connected pcb (if connected).  Otherwise
741 	 * just print the protocol number and address of the socket itself.
742 	 * The idea is not to duplicate netstat, but to make available enough
743 	 * information for further analysis.
744 	 */
745 	switch(dom.dom_family) {
746 	case AF_INET:
747 	case AF_INET6:
748 		getinetproto(proto.pr_protocol);
749 		if (proto.pr_protocol == IPPROTO_TCP ) {
750 			if (so.so_pcb) {
751 				if (kvm_read(kd, (u_long)so.so_pcb,
752 				    (char *)&inpcb, sizeof(struct inpcb))
753 				    != sizeof(struct inpcb)) {
754 					dprintf(stderr,
755 					    "can't read inpcb at %p\n",
756 					    (void *)so.so_pcb);
757 					goto bad;
758 				}
759 				printf(" %x", (int)inpcb.inp_ppcb);
760 			}
761 		}
762 		else if (so.so_pcb)
763 			printf(" %x", (int)so.so_pcb);
764 		break;
765 	case AF_UNIX:
766 		/* print address of pcb and connected pcb */
767 		if (so.so_pcb) {
768 			printf(" %x", (int)so.so_pcb);
769 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
770 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
771 				dprintf(stderr, "can't read unpcb at %p\n",
772 				    (void *)so.so_pcb);
773 				goto bad;
774 			}
775 			if (unpcb.unp_conn) {
776 				char shoconn[4], *cp;
777 
778 				cp = shoconn;
779 				if (!(so.so_state & SS_CANTRCVMORE))
780 					*cp++ = '<';
781 				*cp++ = '-';
782 				if (!(so.so_state & SS_CANTSENDMORE))
783 					*cp++ = '>';
784 				*cp = '\0';
785 				printf(" %s %x", shoconn,
786 				    (int)unpcb.unp_conn);
787 			}
788 		}
789 		break;
790 	default:
791 		/* print protocol number and socket address */
792 		printf(" %d %x", proto.pr_protocol, (int)sock);
793 	}
794 	printf("\n");
795 	return;
796 bad:
797 	printf("* error\n");
798 }
799 
800 
801 /*
802  * Read the specinfo structure in the kernel (as pointed to by a dev_t)
803  * in order to work out the associated udev_t
804  */
805 udev_t
806 dev2udev(dev)
807 	dev_t dev;
808 {
809 	struct specinfo si;
810 
811 	if (KVM_READ(dev, &si, sizeof si)) {
812 		return si.si_udev;
813 	} else {
814 		dprintf(stderr, "can't convert dev_t %p to a udev_t\n",
815 		    (void *)dev);
816 		return -1;
817 	}
818 }
819 
820 /*
821  * getinetproto --
822  *	print name of protocol number
823  */
824 void
825 getinetproto(number)
826 	int number;
827 {
828 	static int isopen;
829 	register struct protoent *pe;
830 
831 	if (!isopen)
832 		setprotoent(++isopen);
833 	if ((pe = getprotobynumber(number)) != NULL)
834 		printf(" %s", pe->p_name);
835 	else
836 		printf(" %d", number);
837 }
838 
839 int
840 getfname(filename)
841 	char *filename;
842 {
843 	struct stat statbuf;
844 	DEVS *cur;
845 
846 	if (stat(filename, &statbuf)) {
847 		warn("%s", filename);
848 		return(0);
849 	}
850 	if ((cur = malloc(sizeof(DEVS))) == NULL)
851 		err(1, NULL);
852 	cur->next = devs;
853 	devs = cur;
854 
855 	cur->ino = statbuf.st_ino;
856 	cur->fsid = statbuf.st_dev & 0xffff;
857 	cur->name = filename;
858 	return(1);
859 }
860 
861 void
862 usage()
863 {
864 	(void)fprintf(stderr,
865  "usage: fstat [-fmnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
866 	exit(1);
867 }
868