Home | History | Annotate | Line # | Download | only in fstat
fstat.c revision 1.23
      1 /*	$NetBSD: fstat.c,v 1.23 1997/10/18 15:28:08 lukem 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.23 1997/10/18 15:28:08 lukem 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 	mode_t mode;
    536 
    537 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
    538 		dprintf(stderr, "can't read nfsnode at %lx for pid %d\n",
    539 			(long)VTONFS(vp), Pid);
    540 		return 0;
    541 	}
    542 	fsp->fsid = nfsnode.n_vattr.va_fsid;
    543 	fsp->fileid = nfsnode.n_vattr.va_fileid;
    544 	fsp->size = nfsnode.n_size;
    545 	fsp->rdev = nfsnode.n_vattr.va_rdev;
    546 	mode = (mode_t)nfsnode.n_vattr.va_mode;
    547 	switch (vp->v_type) {
    548 	case VREG:
    549 		mode |= S_IFREG;
    550 		break;
    551 	case VDIR:
    552 		mode |= S_IFDIR;
    553 		break;
    554 	case VBLK:
    555 		mode |= S_IFBLK;
    556 		break;
    557 	case VCHR:
    558 		mode |= S_IFCHR;
    559 		break;
    560 	case VLNK:
    561 		mode |= S_IFLNK;
    562 		break;
    563 	case VSOCK:
    564 		mode |= S_IFSOCK;
    565 		break;
    566 	case VFIFO:
    567 		mode |= S_IFIFO;
    568 		break;
    569 	default:
    570 		break;
    571 	};
    572 	fsp->mode = mode;
    573 
    574 	return 1;
    575 }
    576 
    577 
    578 char *
    579 getmnton(m)
    580 	struct mount *m;
    581 {
    582 	static struct mount mount;
    583 	static struct mtab {
    584 		struct mtab *next;
    585 		struct mount *m;
    586 		char mntonname[MNAMELEN];
    587 	} *mhead = NULL;
    588 	struct mtab *mt;
    589 
    590 	for (mt = mhead; mt != NULL; mt = mt->next)
    591 		if (m == mt->m)
    592 			return (mt->mntonname);
    593 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
    594 		fprintf(stderr, "can't read mount table at %lx\n", (long)m);
    595 		return (NULL);
    596 	}
    597 	if ((mt = malloc(sizeof (struct mtab))) == NULL) {
    598 		fprintf(stderr, "fstat: %s\n", strerror(errno));
    599 		exit(1);
    600 	}
    601 	mt->m = m;
    602 	memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0], MNAMELEN);
    603 	mt->next = mhead;
    604 	mhead = mt;
    605 	return (mt->mntonname);
    606 }
    607 
    608 void
    609 socktrans(sock, i)
    610 	struct socket *sock;
    611 	int i;
    612 {
    613 	static char *stypename[] = {
    614 		"unused",	/* 0 */
    615 		"stream", 	/* 1 */
    616 		"dgram",	/* 2 */
    617 		"raw",		/* 3 */
    618 		"rdm",		/* 4 */
    619 		"seqpak"	/* 5 */
    620 	};
    621 #define	STYPEMAX 5
    622 	struct socket	so;
    623 	struct protosw	proto;
    624 	struct domain	dom;
    625 	struct inpcb	inpcb;
    626 	struct unpcb	unpcb;
    627 	int len;
    628 	char dname[32];
    629 
    630 	PREFIX(i);
    631 
    632 	/* fill in socket */
    633 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
    634 		dprintf(stderr, "can't read sock at %lx\n", (long)sock);
    635 		goto bad;
    636 	}
    637 
    638 	/* fill in protosw entry */
    639 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
    640 		dprintf(stderr, "can't read protosw at %lx", (long)so.so_proto);
    641 		goto bad;
    642 	}
    643 
    644 	/* fill in domain */
    645 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
    646 		dprintf(stderr, "can't read domain at %lx\n",
    647 		    (long)proto.pr_domain);
    648 		goto bad;
    649 	}
    650 
    651 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
    652 	    sizeof(dname) - 1)) < 0) {
    653 		dprintf(stderr, "can't read domain name at %lx\n",
    654 			(long)dom.dom_name);
    655 		dname[0] = '\0';
    656 	}
    657 	else
    658 		dname[len] = '\0';
    659 
    660 	if ((u_short)so.so_type > STYPEMAX)
    661 		printf("* %s ?%d", dname, so.so_type);
    662 	else
    663 		printf("* %s %s", dname, stypename[so.so_type]);
    664 
    665 	/*
    666 	 * protocol specific formatting
    667 	 *
    668 	 * Try to find interesting things to print.  For tcp, the interesting
    669 	 * thing is the address of the tcpcb, for udp and others, just the
    670 	 * inpcb (socket pcb).  For unix domain, its the address of the socket
    671 	 * pcb and the address of the connected pcb (if connected).  Otherwise
    672 	 * just print the protocol number and address of the socket itself.
    673 	 * The idea is not to duplicate netstat, but to make available enough
    674 	 * information for further analysis.
    675 	 */
    676 	switch(dom.dom_family) {
    677 	case AF_INET:
    678 		getinetproto(proto.pr_protocol);
    679 		if (proto.pr_protocol == IPPROTO_TCP ) {
    680 			if (so.so_pcb) {
    681 				if (kvm_read(kd, (u_long)so.so_pcb,
    682 				    (char *)&inpcb, sizeof(struct inpcb))
    683 				    != sizeof(struct inpcb)) {
    684 					dprintf(stderr,
    685 					    "can't read inpcb at %lx\n",
    686 					    (long)so.so_pcb);
    687 					goto bad;
    688 				}
    689 				printf(" %lx", (long)inpcb.inp_ppcb);
    690 			}
    691 		}
    692 		else if (so.so_pcb)
    693 			printf(" %lx", (long)so.so_pcb);
    694 		break;
    695 	case AF_UNIX:
    696 		/* print address of pcb and connected pcb */
    697 		if (so.so_pcb) {
    698 			printf(" %lx", (long)so.so_pcb);
    699 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
    700 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
    701 				dprintf(stderr, "can't read unpcb at %lx\n",
    702 				    (long)so.so_pcb);
    703 				goto bad;
    704 			}
    705 			if (unpcb.unp_conn) {
    706 				char shoconn[4], *cp;
    707 
    708 				cp = shoconn;
    709 				if (!(so.so_state & SS_CANTRCVMORE))
    710 					*cp++ = '<';
    711 				*cp++ = '-';
    712 				if (!(so.so_state & SS_CANTSENDMORE))
    713 					*cp++ = '>';
    714 				*cp = '\0';
    715 				printf(" %s %lx", shoconn,
    716 				    (long)unpcb.unp_conn);
    717 			}
    718 		}
    719 		break;
    720 	default:
    721 		/* print protocol number and socket address */
    722 		printf(" %d %lx", proto.pr_protocol, (long)sock);
    723 	}
    724 	printf("\n");
    725 	return;
    726 bad:
    727 	printf("* error\n");
    728 }
    729 
    730 /*
    731  * getinetproto --
    732  *	print name of protocol number
    733  */
    734 void
    735 getinetproto(number)
    736 	int number;
    737 {
    738 	char *cp;
    739 
    740 	switch(number) {
    741 	case IPPROTO_IP:
    742 		cp = "ip"; break;
    743 	case IPPROTO_ICMP:
    744 		cp ="icmp"; break;
    745 	case IPPROTO_GGP:
    746 		cp ="ggp"; break;
    747 	case IPPROTO_TCP:
    748 		cp ="tcp"; break;
    749 	case IPPROTO_EGP:
    750 		cp ="egp"; break;
    751 	case IPPROTO_PUP:
    752 		cp ="pup"; break;
    753 	case IPPROTO_UDP:
    754 		cp ="udp"; break;
    755 	case IPPROTO_IDP:
    756 		cp ="idp"; break;
    757 	case IPPROTO_RAW:
    758 		cp ="raw"; break;
    759 	default:
    760 		printf(" %d", number);
    761 		return;
    762 	}
    763 	printf(" %s", cp);
    764 }
    765 
    766 int
    767 getfname(filename)
    768 	char *filename;
    769 {
    770 	struct stat statbuf;
    771 	DEVS *cur;
    772 
    773 	if (stat(filename, &statbuf)) {
    774 		fprintf(stderr, "fstat: %s: %s\n", filename, strerror(errno));
    775 		return(0);
    776 	}
    777 	if ((cur = malloc(sizeof(DEVS))) == NULL) {
    778 		fprintf(stderr, "fstat: %s\n", strerror(errno));
    779 		exit(1);
    780 	}
    781 	cur->next = devs;
    782 	devs = cur;
    783 
    784 	cur->ino = statbuf.st_ino;
    785 	cur->fsid = statbuf.st_dev & 0xffff;
    786 	cur->name = filename;
    787 	return(1);
    788 }
    789 
    790 void
    791 usage()
    792 {
    793 	(void)fprintf(stderr,
    794  "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
    795 	exit(1);
    796 }
    797