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