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