Home | History | Annotate | Line # | Download | only in fstat
fstat.c revision 1.28
      1 /*	$NetBSD: fstat.c,v 1.28 1998/07/06 21:47:07 fair 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.28 1998/07/06 21:47:07 fair 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, NULL); \
    139 		} \
    140 		maxfiles = (d); \
    141 	}
    142 
    143 /*
    144  * a kvm_read that returns true if everything is read
    145  */
    146 #define KVM_READ(kaddr, paddr, len) \
    147 	(kvm_read(kd, (u_long)(kaddr), (char *)(paddr), (len)) == (len))
    148 
    149 kvm_t *kd;
    150 
    151 void	dofiles __P((struct kinfo_proc *));
    152 int	ext2fs_filestat __P((struct vnode *, struct filestat *));
    153 int	getfname __P((char *));
    154 void	getinetproto __P((int));
    155 char   *getmnton __P((struct mount *));
    156 int	main __P((int, char **));
    157 int	nfs_filestat __P((struct vnode *, struct filestat *));
    158 void	socktrans __P((struct socket *, int));
    159 int	ufs_filestat __P((struct vnode *, struct filestat *));
    160 void	usage __P((void));
    161 void	vtrans __P((struct vnode *, int, int));
    162 
    163 int
    164 main(argc, argv)
    165 	int argc;
    166 	char **argv;
    167 {
    168 	struct passwd *passwd;
    169 	struct kinfo_proc *p, *plast;
    170 	int arg, ch, what;
    171 	char *memf, *nlistf;
    172 	char buf[_POSIX2_LINE_MAX];
    173 	int cnt;
    174 	gid_t egid = getegid();
    175 
    176 	(void)setegid(getgid());
    177 	arg = 0;
    178 	what = KERN_PROC_ALL;
    179 	nlistf = memf = NULL;
    180 	while ((ch = getopt(argc, argv, "fnp:u:vN:M:")) != -1)
    181 		switch((char)ch) {
    182 		case 'f':
    183 			fsflg = 1;
    184 			break;
    185 		case 'M':
    186 			memf = optarg;
    187 			break;
    188 		case 'N':
    189 			nlistf = optarg;
    190 			break;
    191 		case 'n':
    192 			nflg = 1;
    193 			break;
    194 		case 'p':
    195 			if (pflg++)
    196 				usage();
    197 			if (!isdigit(*optarg)) {
    198 				warnx("-p requires a process id\n");
    199 				usage();
    200 			}
    201 			what = KERN_PROC_PID;
    202 			arg = atoi(optarg);
    203 			break;
    204 		case 'u':
    205 			if (uflg++)
    206 				usage();
    207 			if (!(passwd = getpwnam(optarg))) {
    208 				errx(1, "%s: unknown uid\n", optarg);
    209 			}
    210 			what = KERN_PROC_UID;
    211 			arg = passwd->pw_uid;
    212 			break;
    213 		case 'v':
    214 			vflg = 1;
    215 			break;
    216 		case '?':
    217 		default:
    218 			usage();
    219 		}
    220 
    221 	if (*(argv += optind)) {
    222 		for (; *argv; ++argv) {
    223 			if (getfname(*argv))
    224 				checkfile = 1;
    225 		}
    226 		if (!checkfile)	/* file(s) specified, but none accessable */
    227 			exit(1);
    228 	}
    229 
    230 	ALLOC_OFILES(256);	/* reserve space for file pointers */
    231 
    232 	if (fsflg && !checkfile) {
    233 		/* -f with no files means use wd */
    234 		if (getfname(".") == 0)
    235 			exit(1);
    236 		checkfile = 1;
    237 	}
    238 
    239 	/*
    240 	 * Discard setgid privileges.  If not the running kernel, we toss
    241 	 * them away totally so that bad guys can't print interesting stuff
    242 	 * from kernel memory, otherwise switch back to kmem for the
    243 	 * duration of the kvm_openfiles() call.
    244 	 */
    245 	if (nlistf != NULL || memf != NULL)
    246 		(void)setgid(getgid());
    247 	else
    248 		(void)setegid(egid);
    249 
    250 	if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, buf)) == NULL)
    251 		errx(1, "%s", buf);
    252 
    253 	/* get rid of it now anyway */
    254 	if (nlistf == NULL && memf == NULL)
    255 		(void)setgid(getgid());
    256 
    257 #ifdef notdef
    258 	if (kvm_nlist(kd, nl) != 0) {
    259 		errx(1, "no namelist: %s\n", kvm_geterr(kd));
    260 	}
    261 #endif
    262 	if ((p = kvm_getprocs(kd, what, arg, &cnt)) == NULL) {
    263 		errx(1, "%s\n", kvm_geterr(kd));
    264 	}
    265 	if (nflg)
    266 		printf("%s",
    267 "USER     CMD          PID   FD  DEV    INUM       MODE SZ|DV R/W");
    268 	else
    269 		printf("%s",
    270 "USER     CMD          PID   FD MOUNT      INUM MODE         SZ|DV R/W");
    271 	if (checkfile && fsflg == 0)
    272 		printf(" NAME\n");
    273 	else
    274 		putchar('\n');
    275 
    276 	for (plast = &p[cnt]; p < plast; ++p) {
    277 		if (p->kp_proc.p_stat == SZOMB)
    278 			continue;
    279 		dofiles(p);
    280 	}
    281 	exit(0);
    282 }
    283 
    284 char	*Uname, *Comm;
    285 int	Pid;
    286 
    287 #define PREFIX(i) printf("%-8.8s %-10s %5d", Uname, Comm, Pid); \
    288 	switch(i) { \
    289 	case TEXT: \
    290 		printf(" text"); \
    291 		break; \
    292 	case CDIR: \
    293 		printf("   wd"); \
    294 		break; \
    295 	case RDIR: \
    296 		printf(" root"); \
    297 		break; \
    298 	case TRACE: \
    299 		printf("   tr"); \
    300 		break; \
    301 	default: \
    302 		printf(" %4d", i); \
    303 		break; \
    304 	}
    305 
    306 /*
    307  * print open files attributed to this process
    308  */
    309 void
    310 dofiles(kp)
    311 	struct kinfo_proc *kp;
    312 {
    313 	int i;
    314 	struct file file;
    315 	struct filedesc0 filed0;
    316 #define	filed	filed0.fd_fd
    317 	struct proc *p = &kp->kp_proc;
    318 	struct eproc *ep = &kp->kp_eproc;
    319 
    320 	Uname = user_from_uid(ep->e_ucred.cr_uid, 0);
    321 	Pid = p->p_pid;
    322 	Comm = p->p_comm;
    323 
    324 	if (p->p_fd == NULL)
    325 		return;
    326 	if (!KVM_READ(p->p_fd, &filed0, sizeof (filed0))) {
    327 		dprintf(stderr, "can't read filedesc at %lx for pid %d\n",
    328 			(long)p->p_fd, Pid);
    329 		return;
    330 	}
    331 	if (filed.fd_nfiles < 0 || filed.fd_lastfile >= filed.fd_nfiles ||
    332 	    filed.fd_freefile > filed.fd_lastfile + 1) {
    333 		dprintf(stderr, "filedesc corrupted at %lx for pid %d\n",
    334 			(long)p->p_fd, Pid);
    335 		return;
    336 	}
    337 	/*
    338 	 * root directory vnode, if one
    339 	 */
    340 	if (filed.fd_rdir)
    341 		vtrans(filed.fd_rdir, RDIR, FREAD);
    342 	/*
    343 	 * current working directory vnode
    344 	 */
    345 	vtrans(filed.fd_cdir, CDIR, FREAD);
    346 	/*
    347 	 * ktrace vnode, if one
    348 	 */
    349 	if (p->p_tracep)
    350 		vtrans(p->p_tracep, TRACE, FREAD|FWRITE);
    351 	/*
    352 	 * open files
    353 	 */
    354 #define FPSIZE	(sizeof (struct file *))
    355 	ALLOC_OFILES(filed.fd_lastfile+1);
    356 	if (filed.fd_nfiles > NDFILE) {
    357 		if (!KVM_READ(filed.fd_ofiles, ofiles,
    358 		    (filed.fd_lastfile+1) * FPSIZE)) {
    359 			dprintf(stderr,
    360 			    "can't read file structures at %lx for pid %d\n",
    361 			    (long)filed.fd_ofiles, Pid);
    362 			return;
    363 		}
    364 	} else
    365 		memmove(ofiles, filed0.fd_dfiles,
    366 		    (filed.fd_lastfile+1) * FPSIZE);
    367 	for (i = 0; i <= filed.fd_lastfile; i++) {
    368 		if (ofiles[i] == NULL)
    369 			continue;
    370 		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
    371 			dprintf(stderr,
    372 			    "can't read file %d at %lx for pid %d\n",
    373 			    i, (long)ofiles[i], Pid);
    374 			continue;
    375 		}
    376 		if (file.f_type == DTYPE_VNODE)
    377 			vtrans((struct vnode *)file.f_data, i, file.f_flag);
    378 		else if (file.f_type == DTYPE_SOCKET) {
    379 			if (checkfile == 0)
    380 				socktrans((struct socket *)file.f_data, i);
    381 		}
    382 		else {
    383 			dprintf(stderr,
    384 				"unknown file type %d for file %d of pid %d\n",
    385 				file.f_type, i, Pid);
    386 		}
    387 	}
    388 }
    389 
    390 void
    391 vtrans(vp, i, flag)
    392 	struct vnode *vp;
    393 	int i;
    394 	int flag;
    395 {
    396 	struct vnode vn;
    397 	struct filestat fst;
    398 	char mode[15];
    399 	char *badtype = NULL, *filename;
    400 
    401 	filename = badtype = NULL;
    402 	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
    403 		dprintf(stderr, "can't read vnode at %lx for pid %d\n",
    404 			(long)vp, Pid);
    405 		return;
    406 	}
    407 	if (vn.v_type == VNON || vn.v_tag == VT_NON)
    408 		badtype = "none";
    409 	else if (vn.v_type == VBAD)
    410 		badtype = "bad";
    411 	else
    412 		switch (vn.v_tag) {
    413 		case VT_UFS:
    414 			if (!ufs_filestat(&vn, &fst))
    415 				badtype = "error";
    416 			break;
    417 		case VT_MFS:
    418 			if (!ufs_filestat(&vn, &fst))
    419 				badtype = "error";
    420 			break;
    421 		case VT_NFS:
    422 			if (!nfs_filestat(&vn, &fst))
    423 				badtype = "error";
    424 			break;
    425 		case VT_EXT2FS:
    426 			if (!ext2fs_filestat(&vn, &fst))
    427 				badtype = "error";
    428 			break;
    429 		default: {
    430 			static char unknown[10];
    431 			(void)snprintf(badtype = unknown, sizeof unknown,
    432 			    "?(%x)", vn.v_tag);
    433 			break;;
    434 		}
    435 	}
    436 	if (checkfile) {
    437 		int fsmatch = 0;
    438 		DEVS *d;
    439 
    440 		if (badtype)
    441 			return;
    442 		for (d = devs; d != NULL; d = d->next)
    443 			if (d->fsid == fst.fsid) {
    444 				fsmatch = 1;
    445 				if (d->ino == fst.fileid) {
    446 					filename = d->name;
    447 					break;
    448 				}
    449 			}
    450 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
    451 			return;
    452 	}
    453 	PREFIX(i);
    454 	if (badtype) {
    455 		(void)printf(" -         -  %10s    -\n", badtype);
    456 		return;
    457 	}
    458 	if (nflg)
    459 		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
    460 	else
    461 		(void)printf(" %-8s", getmnton(vn.v_mount));
    462 	if (nflg)
    463 		(void)snprintf(mode, sizeof mode, "%o", fst.mode);
    464 	else
    465 		strmode(fst.mode, mode);
    466 	(void)printf(" %6ld %10s", (long)fst.fileid, mode);
    467 	switch (vn.v_type) {
    468 	case VBLK:
    469 	case VCHR: {
    470 		char *name;
    471 
    472 		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
    473 		    S_IFCHR : S_IFBLK)) == NULL))
    474 			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
    475 		else
    476 			printf(" %6s", name);
    477 		break;
    478 	}
    479 	default:
    480 		printf(" %6ld", (long)fst.size);
    481 	}
    482 	putchar(' ');
    483 	if (flag & FREAD)
    484 		putchar('r');
    485 	if (flag & FWRITE)
    486 		putchar('w');
    487 	if (filename && !fsflg)
    488 		printf("  %s", filename);
    489 	putchar('\n');
    490 }
    491 
    492 int
    493 ufs_filestat(vp, fsp)
    494 	struct vnode *vp;
    495 	struct filestat *fsp;
    496 {
    497 	struct inode inode;
    498 
    499 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
    500 		dprintf(stderr, "can't read inode at %lx for pid %d\n",
    501 			(long)VTOI(vp), Pid);
    502 		return 0;
    503 	}
    504 	fsp->fsid = inode.i_dev & 0xffff;
    505 	fsp->fileid = (long)inode.i_number;
    506 	fsp->mode = (mode_t)inode.i_ffs_mode;
    507 	fsp->size = (u_long)inode.i_ffs_size;
    508 	fsp->rdev = inode.i_ffs_rdev;
    509 
    510 	return 1;
    511 }
    512 
    513 int
    514 ext2fs_filestat(vp, fsp)
    515 	struct vnode *vp;
    516 	struct filestat *fsp;
    517 {
    518 	struct inode inode;
    519 
    520 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
    521 		dprintf(stderr, "can't read inode at %lx for pid %d\n",
    522 		    (long)VTOI(vp), Pid);
    523 		return 0;
    524 	}
    525 	fsp->fsid = inode.i_dev & 0xffff;
    526 	fsp->fileid = (long)inode.i_number;
    527 	fsp->mode = (mode_t)inode.i_e2fs_mode;
    528 	fsp->size = (u_long)inode.i_e2fs_size;
    529 	fsp->rdev = 0;  /* XXX */
    530 	return 1;
    531 }
    532 
    533 int
    534 nfs_filestat(vp, fsp)
    535 	struct vnode *vp;
    536 	struct filestat *fsp;
    537 {
    538 	struct nfsnode nfsnode;
    539 	struct vattr va;
    540 	mode_t mode;
    541 
    542 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
    543 		dprintf(stderr, "can't read nfsnode at 0x%lx for pid %d\n",
    544 			(u_long)VTONFS(vp), Pid);
    545 		return 0;
    546 	}
    547 	if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
    548 		dprintf(stderr,
    549 		    "can't read vnode attributes at 0x%lx for pid %d\n",
    550 		    (u_long)nfsnode.n_vattr, Pid);
    551 		return 0;
    552 	}
    553 	fsp->fsid = va.va_fsid;
    554 	fsp->fileid = va.va_fileid;
    555 	fsp->size = nfsnode.n_size;
    556 	fsp->rdev = va.va_rdev;
    557 	mode = (mode_t)va.va_mode;
    558 	switch (vp->v_type) {
    559 	case VREG:
    560 		mode |= S_IFREG;
    561 		break;
    562 	case VDIR:
    563 		mode |= S_IFDIR;
    564 		break;
    565 	case VBLK:
    566 		mode |= S_IFBLK;
    567 		break;
    568 	case VCHR:
    569 		mode |= S_IFCHR;
    570 		break;
    571 	case VLNK:
    572 		mode |= S_IFLNK;
    573 		break;
    574 	case VSOCK:
    575 		mode |= S_IFSOCK;
    576 		break;
    577 	case VFIFO:
    578 		mode |= S_IFIFO;
    579 		break;
    580 	default:
    581 		break;
    582 	};
    583 	fsp->mode = mode;
    584 
    585 	return 1;
    586 }
    587 
    588 
    589 char *
    590 getmnton(m)
    591 	struct mount *m;
    592 {
    593 	static struct mount mount;
    594 	static struct mtab {
    595 		struct mtab *next;
    596 		struct mount *m;
    597 		char mntonname[MNAMELEN];
    598 	} *mhead = NULL;
    599 	struct mtab *mt;
    600 
    601 	for (mt = mhead; mt != NULL; mt = mt->next)
    602 		if (m == mt->m)
    603 			return (mt->mntonname);
    604 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
    605 		warnx("can't read mount table at %lx\n", (long)m);
    606 		return (NULL);
    607 	}
    608 	if ((mt = malloc(sizeof (struct mtab))) == NULL) {
    609 		err(1, "malloc(%u)", sizeof (struct mtab));
    610 	}
    611 	mt->m = m;
    612 	memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0], MNAMELEN);
    613 	mt->next = mhead;
    614 	mhead = mt;
    615 	return (mt->mntonname);
    616 }
    617 
    618 void
    619 socktrans(sock, i)
    620 	struct socket *sock;
    621 	int i;
    622 {
    623 	static char *stypename[] = {
    624 		"unused",	/* 0 */
    625 		"stream", 	/* 1 */
    626 		"dgram",	/* 2 */
    627 		"raw",		/* 3 */
    628 		"rdm",		/* 4 */
    629 		"seqpak"	/* 5 */
    630 	};
    631 #define	STYPEMAX 5
    632 	struct socket	so;
    633 	struct protosw	proto;
    634 	struct domain	dom;
    635 	struct inpcb	inpcb;
    636 	struct unpcb	unpcb;
    637 	int len;
    638 	char dname[32];
    639 
    640 	PREFIX(i);
    641 
    642 	/* fill in socket */
    643 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
    644 		dprintf(stderr, "can't read sock at %lx\n", (long)sock);
    645 		goto bad;
    646 	}
    647 
    648 	/* fill in protosw entry */
    649 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
    650 		dprintf(stderr, "can't read protosw at %lx", (long)so.so_proto);
    651 		goto bad;
    652 	}
    653 
    654 	/* fill in domain */
    655 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
    656 		dprintf(stderr, "can't read domain at %lx\n",
    657 		    (long)proto.pr_domain);
    658 		goto bad;
    659 	}
    660 
    661 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
    662 	    sizeof(dname) - 1)) != sizeof(dname) -1) {
    663 		dprintf(stderr, "can't read domain name at %lx\n",
    664 			(long)dom.dom_name);
    665 		dname[0] = '\0';
    666 	}
    667 	else
    668 		dname[len] = '\0';
    669 
    670 	if ((u_short)so.so_type > STYPEMAX)
    671 		printf("* %s ?%d", dname, so.so_type);
    672 	else
    673 		printf("* %s %s", dname, stypename[so.so_type]);
    674 
    675 	/*
    676 	 * protocol specific formatting
    677 	 *
    678 	 * Try to find interesting things to print.  For tcp, the interesting
    679 	 * thing is the address of the tcpcb, for udp and others, just the
    680 	 * inpcb (socket pcb).  For unix domain, its the address of the socket
    681 	 * pcb and the address of the connected pcb (if connected).  Otherwise
    682 	 * just print the protocol number and address of the socket itself.
    683 	 * The idea is not to duplicate netstat, but to make available enough
    684 	 * information for further analysis.
    685 	 */
    686 	switch(dom.dom_family) {
    687 	case AF_INET:
    688 		getinetproto(proto.pr_protocol);
    689 		if (proto.pr_protocol == IPPROTO_TCP ) {
    690 			if (so.so_pcb) {
    691 				if (kvm_read(kd, (u_long)so.so_pcb,
    692 				    (char *)&inpcb, sizeof(struct inpcb))
    693 				    != sizeof(struct inpcb)) {
    694 					dprintf(stderr,
    695 					    "can't read inpcb at %lx\n",
    696 					    (long)so.so_pcb);
    697 					goto bad;
    698 				}
    699 				printf(" %lx", (long)inpcb.inp_ppcb);
    700 			}
    701 		}
    702 		else if (so.so_pcb)
    703 			printf(" %lx", (long)so.so_pcb);
    704 		break;
    705 	case AF_UNIX:
    706 		/* print address of pcb and connected pcb */
    707 		if (so.so_pcb) {
    708 			printf(" %lx", (long)so.so_pcb);
    709 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
    710 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
    711 				dprintf(stderr, "can't read unpcb at %lx\n",
    712 				    (long)so.so_pcb);
    713 				goto bad;
    714 			}
    715 			if (unpcb.unp_conn) {
    716 				char shoconn[4], *cp;
    717 
    718 				cp = shoconn;
    719 				if (!(so.so_state & SS_CANTRCVMORE))
    720 					*cp++ = '<';
    721 				*cp++ = '-';
    722 				if (!(so.so_state & SS_CANTSENDMORE))
    723 					*cp++ = '>';
    724 				*cp = '\0';
    725 				printf(" %s %lx", shoconn,
    726 				    (long)unpcb.unp_conn);
    727 			}
    728 		}
    729 		break;
    730 	default:
    731 		/* print protocol number and socket address */
    732 		printf(" %d %lx", proto.pr_protocol, (long)sock);
    733 	}
    734 	printf("\n");
    735 	return;
    736 bad:
    737 	printf("* error\n");
    738 }
    739 
    740 /*
    741  * getinetproto --
    742  *	print name of protocol number
    743  */
    744 void
    745 getinetproto(number)
    746 	int number;
    747 {
    748 	char *cp;
    749 
    750 	switch (number) {
    751 	case IPPROTO_IP:
    752 		cp = "ip"; break;
    753 	case IPPROTO_ICMP:
    754 		cp ="icmp"; break;
    755 	case IPPROTO_GGP:
    756 		cp ="ggp"; break;
    757 	case IPPROTO_TCP:
    758 		cp ="tcp"; break;
    759 	case IPPROTO_EGP:
    760 		cp ="egp"; break;
    761 	case IPPROTO_PUP:
    762 		cp ="pup"; break;
    763 	case IPPROTO_UDP:
    764 		cp ="udp"; break;
    765 	case IPPROTO_IDP:
    766 		cp ="idp"; break;
    767 	case IPPROTO_RAW:
    768 		cp ="raw"; break;
    769 	default:
    770 		printf(" %d", number);
    771 		return;
    772 	}
    773 	printf(" %s", cp);
    774 }
    775 
    776 int
    777 getfname(filename)
    778 	char *filename;
    779 {
    780 	struct stat statbuf;
    781 	DEVS *cur;
    782 
    783 	if (stat(filename, &statbuf)) {
    784 		warn("stat(%s)", filename);
    785 		return(0);
    786 	}
    787 	if ((cur = malloc(sizeof(DEVS))) == NULL) {
    788 		err(1, "malloc(%u)", sizeof(DEVS));
    789 	}
    790 	cur->next = devs;
    791 	devs = cur;
    792 
    793 	cur->ino = statbuf.st_ino;
    794 	cur->fsid = statbuf.st_dev & 0xffff;
    795 	cur->name = filename;
    796 	return(1);
    797 }
    798 
    799 void
    800 usage()
    801 {
    802 	errx(1,
    803  "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
    804 }
    805