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