Home | History | Annotate | Line # | Download | only in fstat
fstat.c revision 1.30
      1 /*	$NetBSD: fstat.c,v 1.30 1998/07/16 22:23:30 ross Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1988, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 __COPYRIGHT("@(#) Copyright (c) 1988, 1993\n\
     39 	The Regents of the University of California.  All rights reserved.\n");
     40 #endif /* not lint */
     41 
     42 #ifndef lint
     43 #if 0
     44 static char sccsid[] = "@(#)fstat.c	8.3 (Berkeley) 5/2/95";
     45 #else
     46 __RCSID("$NetBSD: fstat.c,v 1.30 1998/07/16 22:23:30 ross Exp $");
     47 #endif
     48 #endif /* not lint */
     49 
     50 #include <sys/param.h>
     51 #include <sys/time.h>
     52 #include <sys/proc.h>
     53 #include <sys/user.h>
     54 #include <sys/stat.h>
     55 #include <sys/vnode.h>
     56 #include <sys/socket.h>
     57 #include <sys/socketvar.h>
     58 #include <sys/domain.h>
     59 #include <sys/protosw.h>
     60 #include <sys/unpcb.h>
     61 #include <sys/sysctl.h>
     62 #include <sys/filedesc.h>
     63 #define	_KERNEL
     64 #include <sys/file.h>
     65 #include <ufs/ufs/quota.h>
     66 #include <ufs/ufs/inode.h>
     67 #undef _KERNEL
     68 #define NFS
     69 #include <sys/mount.h>
     70 #include <nfs/nfsproto.h>
     71 #include <nfs/rpcv2.h>
     72 #include <nfs/nfs.h>
     73 #include <nfs/nfsnode.h>
     74 #undef NFS
     75 
     76 #include <net/route.h>
     77 #include <netinet/in.h>
     78 #include <netinet/in_systm.h>
     79 #include <netinet/ip.h>
     80 #include <netinet/in_pcb.h>
     81 
     82 #include <ctype.h>
     83 #include <errno.h>
     84 #include <kvm.h>
     85 #include <limits.h>
     86 #include <nlist.h>
     87 #include <paths.h>
     88 #include <pwd.h>
     89 #include <stdio.h>
     90 #include <stdlib.h>
     91 #include <string.h>
     92 #include <unistd.h>
     93 #include <err.h>
     94 
     95 #define	TEXT	-1
     96 #define	CDIR	-2
     97 #define	RDIR	-3
     98 #define	TRACE	-4
     99 
    100 typedef struct devs {
    101 	struct	devs *next;
    102 	long	fsid;
    103 	ino_t	ino;
    104 	char	*name;
    105 } DEVS;
    106 DEVS *devs;
    107 
    108 struct  filestat {
    109 	long	fsid;
    110 	long	fileid;
    111 	mode_t	mode;
    112 	u_long	size;
    113 	dev_t	rdev;
    114 };
    115 
    116 #ifdef notdef
    117 struct nlist nl[] = {
    118 	{ "" },
    119 };
    120 #endif
    121 
    122 int 	fsflg,	/* show files on same filesystem as file(s) argument */
    123 	pflg,	/* show files open by a particular pid */
    124 	uflg;	/* show files open by a particular (effective) user */
    125 int 	checkfile; /* true if restricting to particular files or filesystems */
    126 int	nflg;	/* (numerical) display f.s. and rdev as dev_t */
    127 int	vflg;	/* display errors in locating kernel data objects etc... */
    128 
    129 #define dprintf	if (vflg) fprintf
    130 
    131 struct file **ofiles;	/* buffer of pointers to file structures */
    132 int maxfiles;
    133 #define ALLOC_OFILES(d)	\
    134 	if ((d) > maxfiles) { \
    135 		free(ofiles); \
    136 		ofiles = malloc((d) * sizeof(struct file *)); \
    137 		if (ofiles == NULL) { \
    138 			err(1, "malloc(%u)", (d) *	\
    139 					(unsigned int)sizeof(struct file *)); \
    140 		} \
    141 		maxfiles = (d); \
    142 	}
    143 
    144 /*
    145  * a kvm_read that returns true if everything is read
    146  */
    147 #define KVM_READ(kaddr, paddr, len) \
    148 	(kvm_read(kd, (u_long)(kaddr), (char *)(paddr), (len)) == (len))
    149 
    150 kvm_t *kd;
    151 
    152 void	dofiles __P((struct kinfo_proc *));
    153 int	ext2fs_filestat __P((struct vnode *, struct filestat *));
    154 int	getfname __P((char *));
    155 void	getinetproto __P((int));
    156 char   *getmnton __P((struct mount *));
    157 int	main __P((int, char **));
    158 int	nfs_filestat __P((struct vnode *, struct filestat *));
    159 void	socktrans __P((struct socket *, int));
    160 int	ufs_filestat __P((struct vnode *, struct filestat *));
    161 void	usage __P((void));
    162 void	vtrans __P((struct vnode *, int, int));
    163 
    164 int
    165 main(argc, argv)
    166 	int argc;
    167 	char **argv;
    168 {
    169 	struct passwd *passwd;
    170 	struct kinfo_proc *p, *plast;
    171 	int arg, ch, what;
    172 	char *memf, *nlistf;
    173 	char buf[_POSIX2_LINE_MAX];
    174 	int cnt;
    175 	gid_t egid = getegid();
    176 
    177 	(void)setegid(getgid());
    178 	arg = 0;
    179 	what = KERN_PROC_ALL;
    180 	nlistf = memf = NULL;
    181 	while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1)
    182 		switch((char)ch) {
    183 		case 'f':
    184 			fsflg = 1;
    185 			break;
    186 		case 'M':
    187 			memf = optarg;
    188 			break;
    189 		case 'N':
    190 			nlistf = optarg;
    191 			break;
    192 		case 'n':
    193 			nflg = 1;
    194 			break;
    195 		case 'p':
    196 			if (pflg++)
    197 				usage();
    198 			if (!isdigit(*optarg)) {
    199 				warnx("-p requires a process id\n");
    200 				usage();
    201 			}
    202 			what = KERN_PROC_PID;
    203 			arg = atoi(optarg);
    204 			break;
    205 		case 'u':
    206 			if (uflg++)
    207 				usage();
    208 			if (!(passwd = getpwnam(optarg))) {
    209 				errx(1, "%s: unknown uid\n", optarg);
    210 			}
    211 			what = KERN_PROC_UID;
    212 			arg = passwd->pw_uid;
    213 			break;
    214 		case 'v':
    215 			vflg = 1;
    216 			break;
    217 		case '?':
    218 		default:
    219 			usage();
    220 		}
    221 
    222 	if (*(argv += optind)) {
    223 		for (; *argv; ++argv) {
    224 			if (getfname(*argv))
    225 				checkfile = 1;
    226 		}
    227 		if (!checkfile)	/* file(s) specified, but none accessable */
    228 			exit(1);
    229 	}
    230 
    231 	ALLOC_OFILES(256);	/* reserve space for file pointers */
    232 
    233 	if (fsflg && !checkfile) {
    234 		/* -f with no files means use wd */
    235 		if (getfname(".") == 0)
    236 			exit(1);
    237 		checkfile = 1;
    238 	}
    239 
    240 	/*
    241 	 * Discard setgid privileges.  If not the running kernel, we toss
    242 	 * them away totally so that bad guys can't print interesting stuff
    243 	 * from kernel memory, otherwise switch back to kmem for the
    244 	 * duration of the kvm_openfiles() call.
    245 	 */
    246 	if (nlistf != NULL || memf != NULL)
    247 		(void)setgid(getgid());
    248 	else
    249 		(void)setegid(egid);
    250 
    251 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
    252 		errx(1, "%s", buf);
    253 
    254 	/* get rid of it now anyway */
    255 	if (nlistf == NULL && memf == NULL)
    256 		(void)setgid(getgid());
    257 
    258 #ifdef notdef
    259 	if (kvm_nlist(kd, nl) != 0) {
    260 		errx(1, "no namelist: %s\n", kvm_geterr(kd));
    261 	}
    262 #endif
    263 	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) {
    264 		errx(1, "%s\n", kvm_geterr(kd));
    265 	}
    266 	if (nflg)
    267 		printf("%s",
    268 "USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
    269 	else
    270 		printf("%s",
    271 "USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
    272 	if (checkfile && fsflg == 0)
    273 		printf(" NAME\n");
    274 	else
    275 		putchar('\n');
    276 
    277 	for (plast = &p[cnt]; p < plast; ++p) {
    278 		if (p->kp_proc.p_stat == SZOMB)
    279 			continue;
    280 		dofiles(p);
    281 	}
    282 	exit(0);
    283 }
    284 
    285 char	*Uname, *Comm;
    286 int	Pid;
    287 
    288 #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
    289 	switch(i) { \
    290 	case TEXT: \
    291 		printf(" text"); \
    292 		break; \
    293 	case CDIR: \
    294 		printf("   wd"); \
    295 		break; \
    296 	case RDIR: \
    297 		printf(" root"); \
    298 		break; \
    299 	case TRACE: \
    300 		printf("   tr"); \
    301 		break; \
    302 	default: \
    303 		printf(" %4d", i); \
    304 		break; \
    305 	}
    306 
    307 /*
    308  * print open files attributed to this process
    309  */
    310 void
    311 dofiles(kp)
    312 	struct kinfo_proc *kp;
    313 {
    314 	int i;
    315 	struct file file;
    316 	struct filedesc0 filed0;
    317 #define	filed	filed0.fd_fd
    318 	struct proc *p = &kp->kp_proc;
    319 	struct eproc *ep = &kp->kp_eproc;
    320 
    321 	Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
    322 	Pid = p->p_pid;
    323 	Comm = p->p_comm;
    324 
    325 	if (p->p_fd == NULL)
    326 		return;
    327 	if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
    328 		dprintf(stderr, "can't read filedesc at %lx for pid %d\n",
    329 			(long)p->p_fd, Pid);
    330 		return;
    331 	}
    332 	if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
    333 	    filed.fd_freefile > filed.fd_lastfile + 1) {
    334 		dprintf(stderr, "filedesc corrupted at %lx for pid %d\n",
    335 			(long)p->p_fd, Pid);
    336 		return;
    337 	}
    338 	/*
    339 	 * root directory vnode, if one
    340 	 */
    341 	if (filed.fd_rdir)
    342 		vtrans(filed.fd_rdir, RDIR, FREAD);
    343 	/*
    344 	 * current working directory vnode
    345 	 */
    346 	vtrans(filed.fd_cdir, CDIR, FREAD);
    347 	/*
    348 	 * ktrace vnode, if one
    349 	 */
    350 	if (p->p_tracep)
    351 		vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
    352 	/*
    353 	 * open files
    354 	 */
    355 #define FPSIZE	(sizeof (struct file *))
    356 	ALLOC_OFILES(filed.fd_lastfile+1);
    357 	if (filed.fd_nfiles > NDFILE) {
    358 		if (!KVM_READ(filed.fd_ofiles, ofiles,
    359 		    (filed.fd_lastfile+1) * FPSIZE)) {
    360 			dprintf(stderr,
    361 			    "can't read file structures at %lx for pid %d\n",
    362 			    (long)filed.fd_ofiles, Pid);
    363 			return;
    364 		}
    365 	} else
    366 		memmove(ofiles, filed0.fd_dfiles,
    367 		    (filed.fd_lastfile+1) * FPSIZE);
    368 	for (i = 0; i <= filed.fd_lastfile; i++) {
    369 		if (ofiles[i] == NULL)
    370 			continue;
    371 		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
    372 			dprintf(stderr,
    373 			    "can't read file %d at %lx for pid %d\n",
    374 			    i, (long)ofiles[i], Pid);
    375 			continue;
    376 		}
    377 		if (file.f_type == DTYPE_VNODE)
    378 			vtrans((struct vnode *)file.f_data, i, file.f_flag);
    379 		else if (file.f_type == DTYPE_SOCKET) {
    380 			if (checkfile == 0)
    381 				socktrans((struct socket *)file.f_data, i);
    382 		}
    383 		else {
    384 			dprintf(stderr,
    385 				"unknown file type %d for file %d of pid %d\n",
    386 				file.f_type, i, Pid);
    387 		}
    388 	}
    389 }
    390 
    391 void
    392 vtrans(vp, i, flag)
    393 	struct vnode *vp;
    394 	int i;
    395 	int flag;
    396 {
    397 	struct vnode vn;
    398 	struct filestat fst;
    399 	char mode[15];
    400 	char *badtype = NULL, *filename;
    401 
    402 	filename = badtype = NULL;
    403 	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
    404 		dprintf(stderr, "can't read vnode at %lx for pid %d\n",
    405 			(long)vp, Pid);
    406 		return;
    407 	}
    408 	if (vn.v_type == VNON || vn.v_tag == VT_NON)
    409 		badtype = "none";
    410 	else if (vn.v_type == VBAD)
    411 		badtype = "bad";
    412 	else
    413 		switch (vn.v_tag) {
    414 		case VT_UFS:
    415 			if (!ufs_filestat(&vn, &fst))
    416 				badtype = "error";
    417 			break;
    418 		case VT_MFS:
    419 			if (!ufs_filestat(&vn, &fst))
    420 				badtype = "error";
    421 			break;
    422 		case VT_NFS:
    423 			if (!nfs_filestat(&vn, &fst))
    424 				badtype = "error";
    425 			break;
    426 		case VT_EXT2FS:
    427 			if (!ext2fs_filestat(&vn, &fst))
    428 				badtype = "error";
    429 			break;
    430 		default: {
    431 			static char unknown[10];
    432 			(void)snprintf(badtype = unknown, sizeof unknown,
    433 			    "?(%x)", vn.v_tag);
    434 			break;;
    435 		}
    436 	}
    437 	if (checkfile) {
    438 		int fsmatch = 0;
    439 		DEVS *d;
    440 
    441 		if (badtype)
    442 			return;
    443 		for (d = devs; d != NULL; d = d->next)
    444 			if (d->fsid == fst.fsid) {
    445 				fsmatch = 1;
    446 				if (d->ino == fst.fileid) {
    447 					filename = d->name;
    448 					break;
    449 				}
    450 			}
    451 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
    452 			return;
    453 	}
    454 	PREFIX(i);
    455 	if (badtype) {
    456 		(void)printf(" -         -  %10s    -\n", badtype);
    457 		return;
    458 	}
    459 	if (nflg)
    460 		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
    461 	else
    462 		(void)printf(" %-8s", getmnton(vn.v_mount));
    463 	if (nflg)
    464 		(void)snprintf(mode, sizeof mode, "%o", fst.mode);
    465 	else
    466 		strmode(fst.mode, mode);
    467 	(void)printf(" %6ld %10s", (long)fst.fileid, mode);
    468 	switch (vn.v_type) {
    469 	case VBLK:
    470 	case VCHR: {
    471 		char *name;
    472 
    473 		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
    474 		    S_IFCHR : S_IFBLK)) == NULL))
    475 			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
    476 		else
    477 			printf(" %6s", name);
    478 		break;
    479 	}
    480 	default:
    481 		printf(" %6ld", (long)fst.size);
    482 	}
    483 	putchar(' ');
    484 	if (flag & FREAD)
    485 		putchar('r');
    486 	if (flag & FWRITE)
    487 		putchar('w');
    488 	if (filename && !fsflg)
    489 		printf("  %s", filename);
    490 	putchar('\n');
    491 }
    492 
    493 int
    494 ufs_filestat(vp, fsp)
    495 	struct vnode *vp;
    496 	struct filestat *fsp;
    497 {
    498 	struct inode inode;
    499 
    500 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
    501 		dprintf(stderr, "can't read inode at %lx for pid %d\n",
    502 			(long)VTOI(vp), Pid);
    503 		return 0;
    504 	}
    505 	fsp->fsid = inode.i_dev & 0xffff;
    506 	fsp->fileid = (long)inode.i_number;
    507 	fsp->mode = (mode_t)inode.i_ffs_mode;
    508 	fsp->size = (u_long)inode.i_ffs_size;
    509 	fsp->rdev = inode.i_ffs_rdev;
    510 
    511 	return 1;
    512 }
    513 
    514 int
    515 ext2fs_filestat(vp, fsp)
    516 	struct vnode *vp;
    517 	struct filestat *fsp;
    518 {
    519 	struct inode inode;
    520 
    521 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
    522 		dprintf(stderr, "can't read inode at %lx for pid %d\n",
    523 		    (long)VTOI(vp), Pid);
    524 		return 0;
    525 	}
    526 	fsp->fsid = inode.i_dev & 0xffff;
    527 	fsp->fileid = (long)inode.i_number;
    528 	fsp->mode = (mode_t)inode.i_e2fs_mode;
    529 	fsp->size = (u_long)inode.i_e2fs_size;
    530 	fsp->rdev = 0;  /* XXX */
    531 	return 1;
    532 }
    533 
    534 int
    535 nfs_filestat(vp, fsp)
    536 	struct vnode *vp;
    537 	struct filestat *fsp;
    538 {
    539 	struct nfsnode nfsnode;
    540 	struct vattr va;
    541 	mode_t mode;
    542 
    543 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
    544 		dprintf(stderr, "can't read nfsnode at 0x%lx for pid %d\n",
    545 			(u_long)VTONFS(vp), Pid);
    546 		return 0;
    547 	}
    548 	if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
    549 		dprintf(stderr,
    550 		    "can't read vnode attributes at 0x%lx for pid %d\n",
    551 		    (u_long)nfsnode.n_vattr, Pid);
    552 		return 0;
    553 	}
    554 	fsp->fsid = va.va_fsid;
    555 	fsp->fileid = va.va_fileid;
    556 	fsp->size = nfsnode.n_size;
    557 	fsp->rdev = va.va_rdev;
    558 	mode = (mode_t)va.va_mode;
    559 	switch (vp->v_type) {
    560 	case VREG:
    561 		mode |= S_IFREG;
    562 		break;
    563 	case VDIR:
    564 		mode |= S_IFDIR;
    565 		break;
    566 	case VBLK:
    567 		mode |= S_IFBLK;
    568 		break;
    569 	case VCHR:
    570 		mode |= S_IFCHR;
    571 		break;
    572 	case VLNK:
    573 		mode |= S_IFLNK;
    574 		break;
    575 	case VSOCK:
    576 		mode |= S_IFSOCK;
    577 		break;
    578 	case VFIFO:
    579 		mode |= S_IFIFO;
    580 		break;
    581 	default:
    582 		break;
    583 	};
    584 	fsp->mode = mode;
    585 
    586 	return 1;
    587 }
    588 
    589 
    590 char *
    591 getmnton(m)
    592 	struct mount *m;
    593 {
    594 	static struct mount mount;
    595 	static struct mtab {
    596 		struct mtab *next;
    597 		struct mount *m;
    598 		char mntonname[MNAMELEN];
    599 	} *mhead = NULL;
    600 	struct mtab *mt;
    601 
    602 	for (mt = mhead; mt != NULL; mt = mt->next)
    603 		if (m == mt->m)
    604 			return (mt->mntonname);
    605 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
    606 		warnx("can't read mount table at %lx\n", (long)m);
    607 		return (NULL);
    608 	}
    609 	if ((mt = malloc(sizeof (struct mtab))) == NULL) {
    610 		err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab));
    611 	}
    612 	mt->m = m;
    613 	memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0], MNAMELEN);
    614 	mt->next = mhead;
    615 	mhead = mt;
    616 	return (mt->mntonname);
    617 }
    618 
    619 void
    620 socktrans(sock, i)
    621 	struct socket *sock;
    622 	int i;
    623 {
    624 	static char *stypename[] = {
    625 		"unused",	/* 0 */
    626 		"stream", 	/* 1 */
    627 		"dgram",	/* 2 */
    628 		"raw",		/* 3 */
    629 		"rdm",		/* 4 */
    630 		"seqpak"	/* 5 */
    631 	};
    632 #define	STYPEMAX 5
    633 	struct socket	so;
    634 	struct protosw	proto;
    635 	struct domain	dom;
    636 	struct inpcb	inpcb;
    637 	struct unpcb	unpcb;
    638 	int len;
    639 	char dname[32];
    640 
    641 	PREFIX(i);
    642 
    643 	/* fill in socket */
    644 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
    645 		dprintf(stderr, "can't read sock at %lx\n", (long)sock);
    646 		goto bad;
    647 	}
    648 
    649 	/* fill in protosw entry */
    650 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
    651 		dprintf(stderr, "can't read protosw at %lx", (long)so.so_proto);
    652 		goto bad;
    653 	}
    654 
    655 	/* fill in domain */
    656 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
    657 		dprintf(stderr, "can't read domain at %lx\n",
    658 		    (long)proto.pr_domain);
    659 		goto bad;
    660 	}
    661 
    662 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
    663 	    sizeof(dname) - 1)) != sizeof(dname) -1) {
    664 		dprintf(stderr, "can't read domain name at %lx\n",
    665 			(long)dom.dom_name);
    666 		dname[0] = '\0';
    667 	}
    668 	else
    669 		dname[len] = '\0';
    670 
    671 	if ((u_short)so.so_type > STYPEMAX)
    672 		printf("* %s ?%d", dname, so.so_type);
    673 	else
    674 		printf("* %s %s", dname, stypename[so.so_type]);
    675 
    676 	/*
    677 	 * protocol specific formatting
    678 	 *
    679 	 * Try to find interesting things to print.  For tcp, the interesting
    680 	 * thing is the address of the tcpcb, for udp and others, just the
    681 	 * inpcb (socket pcb).  For unix domain, its the address of the socket
    682 	 * pcb and the address of the connected pcb (if connected).  Otherwise
    683 	 * just print the protocol number and address of the socket itself.
    684 	 * The idea is not to duplicate netstat, but to make available enough
    685 	 * information for further analysis.
    686 	 */
    687 	switch(dom.dom_family) {
    688 	case AF_INET:
    689 		getinetproto(proto.pr_protocol);
    690 		if (proto.pr_protocol == IPPROTO_TCP ) {
    691 			if (so.so_pcb) {
    692 				if (kvm_read(kd, (u_long)so.so_pcb,
    693 				    (char *)&inpcb, sizeof(struct inpcb))
    694 				    != sizeof(struct inpcb)) {
    695 					dprintf(stderr,
    696 					    "can't read inpcb at %lx\n",
    697 					    (long)so.so_pcb);
    698 					goto bad;
    699 				}
    700 				printf(" %lx", (long)inpcb.inp_ppcb);
    701 			}
    702 		}
    703 		else if (so.so_pcb)
    704 			printf(" %lx", (long)so.so_pcb);
    705 		break;
    706 	case AF_UNIX:
    707 		/* print address of pcb and connected pcb */
    708 		if (so.so_pcb) {
    709 			printf(" %lx", (long)so.so_pcb);
    710 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
    711 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
    712 				dprintf(stderr, "can't read unpcb at %lx\n",
    713 				    (long)so.so_pcb);
    714 				goto bad;
    715 			}
    716 			if (unpcb.unp_conn) {
    717 				char shoconn[4], *cp;
    718 
    719 				cp = shoconn;
    720 				if (!(so.so_state & SS_CANTRCVMORE))
    721 					*cp++ = '<';
    722 				*cp++ = '-';
    723 				if (!(so.so_state & SS_CANTSENDMORE))
    724 					*cp++ = '>';
    725 				*cp = '\0';
    726 				printf(" %s %lx", shoconn,
    727 				    (long)unpcb.unp_conn);
    728 			}
    729 		}
    730 		break;
    731 	default:
    732 		/* print protocol number and socket address */
    733 		printf(" %d %lx", proto.pr_protocol, (long)sock);
    734 	}
    735 	printf("\n");
    736 	return;
    737 bad:
    738 	printf("* error\n");
    739 }
    740 
    741 /*
    742  * getinetproto --
    743  *	print name of protocol number
    744  */
    745 void
    746 getinetproto(number)
    747 	int number;
    748 {
    749 	char *cp;
    750 
    751 	switch (number) {
    752 	case IPPROTO_IP:
    753 		cp = "ip"; break;
    754 	case IPPROTO_ICMP:
    755 		cp ="icmp"; break;
    756 	case IPPROTO_GGP:
    757 		cp ="ggp"; break;
    758 	case IPPROTO_TCP:
    759 		cp ="tcp"; break;
    760 	case IPPROTO_EGP:
    761 		cp ="egp"; break;
    762 	case IPPROTO_PUP:
    763 		cp ="pup"; break;
    764 	case IPPROTO_UDP:
    765 		cp ="udp"; break;
    766 	case IPPROTO_IDP:
    767 		cp ="idp"; break;
    768 	case IPPROTO_RAW:
    769 		cp ="raw"; break;
    770 	default:
    771 		printf(" %d", number);
    772 		return;
    773 	}
    774 	printf(" %s", cp);
    775 }
    776 
    777 int
    778 getfname(filename)
    779 	char *filename;
    780 {
    781 	struct stat statbuf;
    782 	DEVS *cur;
    783 
    784 	if (stat(filename, &statbuf)) {
    785 		warn("stat(%s)", filename);
    786 		return(0);
    787 	}
    788 	if ((cur = malloc(sizeof(DEVS))) == NULL) {
    789 		err(1, "malloc(%u)", (unsigned int)sizeof(DEVS));
    790 	}
    791 	cur->next = devs;
    792 	devs = cur;
    793 
    794 	cur->ino = statbuf.st_ino;
    795 	cur->fsid = statbuf.st_dev & 0xffff;
    796 	cur->name = filename;
    797 	return(1);
    798 }
    799 
    800 void
    801 usage()
    802 {
    803 	errx(1,
    804  "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
    805 }
    806