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