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