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