Home | History | Annotate | Line # | Download | only in fstat
fstat.c revision 1.32
      1 /*	$NetBSD: fstat.c,v 1.32 1998/07/28 21:16:42 mycroft 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.32 1998/07/28 21:16:42 mycroft 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 const	char *Uname;
    286 char	*Comm;
    287 int	Pid;
    288 
    289 #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
    290 	switch(i) { \
    291 	case TEXT: \
    292 		printf(" text"); \
    293 		break; \
    294 	case CDIR: \
    295 		printf("   wd"); \
    296 		break; \
    297 	case RDIR: \
    298 		printf(" root"); \
    299 		break; \
    300 	case TRACE: \
    301 		printf("   tr"); \
    302 		break; \
    303 	default: \
    304 		printf(" %4d", i); \
    305 		break; \
    306 	}
    307 
    308 /*
    309  * print open files attributed to this process
    310  */
    311 void
    312 dofiles(kp)
    313 	struct kinfo_proc *kp;
    314 {
    315 	int i;
    316 	struct file file;
    317 	struct filedesc0 filed0;
    318 #define	filed	filed0.fd_fd
    319 	struct proc *p = &kp->kp_proc;
    320 	struct eproc *ep = &kp->kp_eproc;
    321 
    322 	Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
    323 	Pid = p->p_pid;
    324 	Comm = p->p_comm;
    325 
    326 	if (p->p_fd == NULL)
    327 		return;
    328 	if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
    329 		dprintf(stderr, "can't read filedesc at %lx for pid %d\n",
    330 			(long)p->p_fd, Pid);
    331 		return;
    332 	}
    333 	if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
    334 	    filed.fd_freefile > filed.fd_lastfile + 1) {
    335 		dprintf(stderr, "filedesc corrupted at %lx for pid %d\n",
    336 			(long)p->p_fd, Pid);
    337 		return;
    338 	}
    339 	/*
    340 	 * root directory vnode, if one
    341 	 */
    342 	if (filed.fd_rdir)
    343 		vtrans(filed.fd_rdir, RDIR, FREAD);
    344 	/*
    345 	 * current working directory vnode
    346 	 */
    347 	vtrans(filed.fd_cdir, CDIR, FREAD);
    348 	/*
    349 	 * ktrace vnode, if one
    350 	 */
    351 	if (p->p_tracep)
    352 		vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
    353 	/*
    354 	 * open files
    355 	 */
    356 #define FPSIZE	(sizeof (struct file *))
    357 	ALLOC_OFILES(filed.fd_lastfile+1);
    358 	if (filed.fd_nfiles > NDFILE) {
    359 		if (!KVM_READ(filed.fd_ofiles, ofiles,
    360 		    (filed.fd_lastfile+1) * FPSIZE)) {
    361 			dprintf(stderr,
    362 			    "can't read file structures at %lx for pid %d\n",
    363 			    (long)filed.fd_ofiles, Pid);
    364 			return;
    365 		}
    366 	} else
    367 		memmove(ofiles, filed0.fd_dfiles,
    368 		    (filed.fd_lastfile+1) * FPSIZE);
    369 	for (i = 0; i <= filed.fd_lastfile; i++) {
    370 		if (ofiles[i] == NULL)
    371 			continue;
    372 		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
    373 			dprintf(stderr,
    374 			    "can't read file %d at %lx for pid %d\n",
    375 			    i, (long)ofiles[i], Pid);
    376 			continue;
    377 		}
    378 		if (file.f_type == DTYPE_VNODE)
    379 			vtrans((struct vnode *)file.f_data, i, file.f_flag);
    380 		else if (file.f_type == DTYPE_SOCKET) {
    381 			if (checkfile == 0)
    382 				socktrans((struct socket *)file.f_data, i);
    383 		}
    384 		else {
    385 			dprintf(stderr,
    386 				"unknown file type %d for file %d of pid %d\n",
    387 				file.f_type, i, Pid);
    388 		}
    389 	}
    390 }
    391 
    392 void
    393 vtrans(vp, i, flag)
    394 	struct vnode *vp;
    395 	int i;
    396 	int flag;
    397 {
    398 	struct vnode vn;
    399 	struct filestat fst;
    400 	char mode[15];
    401 	char *badtype = NULL, *filename;
    402 
    403 	filename = badtype = NULL;
    404 	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
    405 		dprintf(stderr, "can't read vnode at %lx for pid %d\n",
    406 			(long)vp, Pid);
    407 		return;
    408 	}
    409 	if (vn.v_type == VNON || vn.v_tag == VT_NON)
    410 		badtype = "none";
    411 	else if (vn.v_type == VBAD)
    412 		badtype = "bad";
    413 	else
    414 		switch (vn.v_tag) {
    415 		case VT_UFS:
    416 			if (!ufs_filestat(&vn, &fst))
    417 				badtype = "error";
    418 			break;
    419 		case VT_MFS:
    420 			if (!ufs_filestat(&vn, &fst))
    421 				badtype = "error";
    422 			break;
    423 		case VT_NFS:
    424 			if (!nfs_filestat(&vn, &fst))
    425 				badtype = "error";
    426 			break;
    427 		case VT_EXT2FS:
    428 			if (!ext2fs_filestat(&vn, &fst))
    429 				badtype = "error";
    430 			break;
    431 		default: {
    432 			static char unknown[10];
    433 			(void)snprintf(badtype = unknown, sizeof unknown,
    434 			    "?(%x)", vn.v_tag);
    435 			break;;
    436 		}
    437 	}
    438 	if (checkfile) {
    439 		int fsmatch = 0;
    440 		DEVS *d;
    441 
    442 		if (badtype)
    443 			return;
    444 		for (d = devs; d != NULL; d = d->next)
    445 			if (d->fsid == fst.fsid) {
    446 				fsmatch = 1;
    447 				if (d->ino == fst.fileid) {
    448 					filename = d->name;
    449 					break;
    450 				}
    451 			}
    452 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
    453 			return;
    454 	}
    455 	PREFIX(i);
    456 	if (badtype) {
    457 		(void)printf(" -         -  %10s    -\n", badtype);
    458 		return;
    459 	}
    460 	if (nflg)
    461 		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
    462 	else
    463 		(void)printf(" %-8s", getmnton(vn.v_mount));
    464 	if (nflg)
    465 		(void)snprintf(mode, sizeof mode, "%o", fst.mode);
    466 	else
    467 		strmode(fst.mode, mode);
    468 	(void)printf(" %6ld %10s", (long)fst.fileid, mode);
    469 	switch (vn.v_type) {
    470 	case VBLK:
    471 	case VCHR: {
    472 		char *name;
    473 
    474 		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
    475 		    S_IFCHR : S_IFBLK)) == NULL))
    476 			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
    477 		else
    478 			printf(" %6s", name);
    479 		break;
    480 	}
    481 	default:
    482 		printf(" %6ld", (long)fst.size);
    483 	}
    484 	putchar(' ');
    485 	if (flag & FREAD)
    486 		putchar('r');
    487 	if (flag & FWRITE)
    488 		putchar('w');
    489 	if (filename && !fsflg)
    490 		printf("  %s", filename);
    491 	putchar('\n');
    492 }
    493 
    494 int
    495 ufs_filestat(vp, fsp)
    496 	struct vnode *vp;
    497 	struct filestat *fsp;
    498 {
    499 	struct inode inode;
    500 
    501 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
    502 		dprintf(stderr, "can't read inode at %lx for pid %d\n",
    503 			(long)VTOI(vp), Pid);
    504 		return 0;
    505 	}
    506 	fsp->fsid = inode.i_dev & 0xffff;
    507 	fsp->fileid = (long)inode.i_number;
    508 	fsp->mode = (mode_t)inode.i_ffs_mode;
    509 	fsp->size = (u_long)inode.i_ffs_size;
    510 	fsp->rdev = inode.i_ffs_rdev;
    511 
    512 	return 1;
    513 }
    514 
    515 int
    516 ext2fs_filestat(vp, fsp)
    517 	struct vnode *vp;
    518 	struct filestat *fsp;
    519 {
    520 	struct inode inode;
    521 
    522 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
    523 		dprintf(stderr, "can't read inode at %lx for pid %d\n",
    524 		    (long)VTOI(vp), Pid);
    525 		return 0;
    526 	}
    527 	fsp->fsid = inode.i_dev & 0xffff;
    528 	fsp->fileid = (long)inode.i_number;
    529 	fsp->mode = (mode_t)inode.i_e2fs_mode;
    530 	fsp->size = (u_long)inode.i_e2fs_size;
    531 	fsp->rdev = 0;  /* XXX */
    532 	return 1;
    533 }
    534 
    535 int
    536 nfs_filestat(vp, fsp)
    537 	struct vnode *vp;
    538 	struct filestat *fsp;
    539 {
    540 	struct nfsnode nfsnode;
    541 	struct vattr va;
    542 	mode_t mode;
    543 
    544 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
    545 		dprintf(stderr, "can't read nfsnode at 0x%lx for pid %d\n",
    546 			(u_long)VTONFS(vp), Pid);
    547 		return 0;
    548 	}
    549 	if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
    550 		dprintf(stderr,
    551 		    "can't read vnode attributes at 0x%lx for pid %d\n",
    552 		    (u_long)nfsnode.n_vattr, Pid);
    553 		return 0;
    554 	}
    555 	fsp->fsid = va.va_fsid;
    556 	fsp->fileid = va.va_fileid;
    557 	fsp->size = nfsnode.n_size;
    558 	fsp->rdev = va.va_rdev;
    559 	mode = (mode_t)va.va_mode;
    560 	switch (vp->v_type) {
    561 	case VREG:
    562 		mode |= S_IFREG;
    563 		break;
    564 	case VDIR:
    565 		mode |= S_IFDIR;
    566 		break;
    567 	case VBLK:
    568 		mode |= S_IFBLK;
    569 		break;
    570 	case VCHR:
    571 		mode |= S_IFCHR;
    572 		break;
    573 	case VLNK:
    574 		mode |= S_IFLNK;
    575 		break;
    576 	case VSOCK:
    577 		mode |= S_IFSOCK;
    578 		break;
    579 	case VFIFO:
    580 		mode |= S_IFIFO;
    581 		break;
    582 	default:
    583 		break;
    584 	};
    585 	fsp->mode = mode;
    586 
    587 	return 1;
    588 }
    589 
    590 
    591 char *
    592 getmnton(m)
    593 	struct mount *m;
    594 {
    595 	static struct mount mount;
    596 	static struct mtab {
    597 		struct mtab *next;
    598 		struct mount *m;
    599 		char mntonname[MNAMELEN];
    600 	} *mhead = NULL;
    601 	struct mtab *mt;
    602 
    603 	for (mt = mhead; mt != NULL; mt = mt->next)
    604 		if (m == mt->m)
    605 			return (mt->mntonname);
    606 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
    607 		warnx("can't read mount table at %lx\n", (long)m);
    608 		return (NULL);
    609 	}
    610 	if ((mt = malloc(sizeof (struct mtab))) == NULL) {
    611 		err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab));
    612 	}
    613 	mt->m = m;
    614 	memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0], MNAMELEN);
    615 	mt->next = mhead;
    616 	mhead = mt;
    617 	return (mt->mntonname);
    618 }
    619 
    620 void
    621 socktrans(sock, i)
    622 	struct socket *sock;
    623 	int i;
    624 {
    625 	static char *stypename[] = {
    626 		"unused",	/* 0 */
    627 		"stream", 	/* 1 */
    628 		"dgram",	/* 2 */
    629 		"raw",		/* 3 */
    630 		"rdm",		/* 4 */
    631 		"seqpak"	/* 5 */
    632 	};
    633 #define	STYPEMAX 5
    634 	struct socket	so;
    635 	struct protosw	proto;
    636 	struct domain	dom;
    637 	struct inpcb	inpcb;
    638 	struct unpcb	unpcb;
    639 	int len;
    640 	char dname[32];
    641 
    642 	PREFIX(i);
    643 
    644 	/* fill in socket */
    645 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
    646 		dprintf(stderr, "can't read sock at %lx\n", (long)sock);
    647 		goto bad;
    648 	}
    649 
    650 	/* fill in protosw entry */
    651 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
    652 		dprintf(stderr, "can't read protosw at %lx", (long)so.so_proto);
    653 		goto bad;
    654 	}
    655 
    656 	/* fill in domain */
    657 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
    658 		dprintf(stderr, "can't read domain at %lx\n",
    659 		    (long)proto.pr_domain);
    660 		goto bad;
    661 	}
    662 
    663 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
    664 	    sizeof(dname) - 1)) != sizeof(dname) -1) {
    665 		dprintf(stderr, "can't read domain name at %lx\n",
    666 			(long)dom.dom_name);
    667 		dname[0] = '\0';
    668 	}
    669 	else
    670 		dname[len] = '\0';
    671 
    672 	if ((u_short)so.so_type > STYPEMAX)
    673 		printf("* %s ?%d", dname, so.so_type);
    674 	else
    675 		printf("* %s %s", dname, stypename[so.so_type]);
    676 
    677 	/*
    678 	 * protocol specific formatting
    679 	 *
    680 	 * Try to find interesting things to print.  For tcp, the interesting
    681 	 * thing is the address of the tcpcb, for udp and others, just the
    682 	 * inpcb (socket pcb).  For unix domain, its the address of the socket
    683 	 * pcb and the address of the connected pcb (if connected).  Otherwise
    684 	 * just print the protocol number and address of the socket itself.
    685 	 * The idea is not to duplicate netstat, but to make available enough
    686 	 * information for further analysis.
    687 	 */
    688 	switch(dom.dom_family) {
    689 	case AF_INET:
    690 		getinetproto(proto.pr_protocol);
    691 		if (proto.pr_protocol == IPPROTO_TCP ) {
    692 			if (so.so_pcb) {
    693 				if (kvm_read(kd, (u_long)so.so_pcb,
    694 				    (char *)&inpcb, sizeof(struct inpcb))
    695 				    != sizeof(struct inpcb)) {
    696 					dprintf(stderr,
    697 					    "can't read inpcb at %lx\n",
    698 					    (long)so.so_pcb);
    699 					goto bad;
    700 				}
    701 				printf(" %lx", (long)inpcb.inp_ppcb);
    702 			}
    703 		}
    704 		else if (so.so_pcb)
    705 			printf(" %lx", (long)so.so_pcb);
    706 		break;
    707 	case AF_LOCAL:
    708 		/* print address of pcb and connected pcb */
    709 		if (so.so_pcb) {
    710 			printf(" %lx", (long)so.so_pcb);
    711 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
    712 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
    713 				dprintf(stderr, "can't read unpcb at %lx\n",
    714 				    (long)so.so_pcb);
    715 				goto bad;
    716 			}
    717 			if (unpcb.unp_conn) {
    718 				char shoconn[4], *cp;
    719 
    720 				cp = shoconn;
    721 				if (!(so.so_state & SS_CANTRCVMORE))
    722 					*cp++ = '<';
    723 				*cp++ = '-';
    724 				if (!(so.so_state & SS_CANTSENDMORE))
    725 					*cp++ = '>';
    726 				*cp = '\0';
    727 				printf(" %s %lx", shoconn,
    728 				    (long)unpcb.unp_conn);
    729 			}
    730 		}
    731 		break;
    732 	default:
    733 		/* print protocol number and socket address */
    734 		printf(" %d %lx", proto.pr_protocol, (long)sock);
    735 	}
    736 	printf("\n");
    737 	return;
    738 bad:
    739 	printf("* error\n");
    740 }
    741 
    742 /*
    743  * getinetproto --
    744  *	print name of protocol number
    745  */
    746 void
    747 getinetproto(number)
    748 	int number;
    749 {
    750 	char *cp;
    751 
    752 	switch (number) {
    753 	case IPPROTO_IP:
    754 		cp = "ip"; break;
    755 	case IPPROTO_ICMP:
    756 		cp ="icmp"; break;
    757 	case IPPROTO_GGP:
    758 		cp ="ggp"; break;
    759 	case IPPROTO_TCP:
    760 		cp ="tcp"; break;
    761 	case IPPROTO_EGP:
    762 		cp ="egp"; break;
    763 	case IPPROTO_PUP:
    764 		cp ="pup"; break;
    765 	case IPPROTO_UDP:
    766 		cp ="udp"; break;
    767 	case IPPROTO_IDP:
    768 		cp ="idp"; break;
    769 	case IPPROTO_RAW:
    770 		cp ="raw"; break;
    771 	default:
    772 		printf(" %d", number);
    773 		return;
    774 	}
    775 	printf(" %s", cp);
    776 }
    777 
    778 int
    779 getfname(filename)
    780 	char *filename;
    781 {
    782 	struct stat statbuf;
    783 	DEVS *cur;
    784 
    785 	if (stat(filename, &statbuf)) {
    786 		warn("stat(%s)", filename);
    787 		return(0);
    788 	}
    789 	if ((cur = malloc(sizeof(DEVS))) == NULL) {
    790 		err(1, "malloc(%u)", (unsigned int)sizeof(DEVS));
    791 	}
    792 	cur->next = devs;
    793 	devs = cur;
    794 
    795 	cur->ino = statbuf.st_ino;
    796 	cur->fsid = statbuf.st_dev & 0xffff;
    797 	cur->name = filename;
    798 	return(1);
    799 }
    800 
    801 void
    802 usage()
    803 {
    804 	errx(1,
    805  "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
    806 }
    807