Home | History | Annotate | Line # | Download | only in fstat
fstat.c revision 1.47
      1 /*	$NetBSD: fstat.c,v 1.47 2001/06/16 12:08: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.47 2001/06/16 12:08: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 accessable */
    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 	if (file.f_type == DTYPE_VNODE)
    392 		vtrans((struct vnode *)file.f_data, i, file.f_flag);
    393 	else if (file.f_type == DTYPE_SOCKET) {
    394 		if (checkfile == 0)
    395 			socktrans((struct socket *)file.f_data, i);
    396 	} else if (file.f_type == DTYPE_PIPE) {
    397 		if (checkfile == 0)
    398 			ptrans(&file, (struct pipe *)file.f_data, i);
    399 	} else {
    400 		dprintf("unknown file type %d for file %d of pid %d",
    401 		    file.f_type, i, Pid);
    402 	}
    403 }
    404 
    405 char *
    406 vfilestat(vp, fsp)
    407 	struct vnode *vp;
    408 	struct filestat *fsp;
    409 {
    410 	char *badtype = NULL;
    411 
    412 	if (vp->v_type == VNON || vp->v_tag == VT_NON)
    413 		badtype = "none";
    414 	else if (vp->v_type == VBAD)
    415 		badtype = "bad";
    416 	else
    417 		switch (vp->v_tag) {
    418 		case VT_UFS:
    419 			if (!ufs_filestat(vp, fsp))
    420 				badtype = "error";
    421 			break;
    422 		case VT_MFS:
    423 			if (!ufs_filestat(vp, fsp))
    424 				badtype = "error";
    425 			break;
    426 		case VT_MSDOSFS:
    427 			if (!msdosfs_filestat(vp, fsp))
    428 				badtype = "error";
    429 			break;
    430 		case VT_NFS:
    431 			if (!nfs_filestat(vp, fsp))
    432 				badtype = "error";
    433 			break;
    434 		case VT_EXT2FS:
    435 			if (!ext2fs_filestat(vp, fsp))
    436 				badtype = "error";
    437 			break;
    438 		case VT_ISOFS:
    439 			if (!isofs_filestat(vp, fsp))
    440 				badtype = "error";
    441 			break;
    442 		case VT_NTFS:
    443 			if (!ntfs_filestat(vp, fsp))
    444 				badtype = "error";
    445 			break;
    446 		case VT_NULL:
    447 		case VT_OVERLAY:
    448 		case VT_UMAP:
    449 			badtype = layer_filestat(vp, fsp);
    450 			break;
    451 		default: {
    452 			static char unknown[10];
    453 			(void)snprintf(badtype = unknown, sizeof unknown,
    454 			    "?(%x)", vp->v_tag);
    455 			break;
    456 		}
    457 	}
    458 	return (badtype);
    459 }
    460 
    461 void
    462 vtrans(vp, i, flag)
    463 	struct vnode *vp;
    464 	int i;
    465 	int flag;
    466 {
    467 	struct vnode vn;
    468 	struct filestat fst;
    469 	char mode[15], rw[3];
    470 	char *badtype, *filename;
    471 
    472 	filename = NULL;
    473 	if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
    474 		dprintf("can't read vnode at %p for pid %d", vp, Pid);
    475 		return;
    476 	}
    477 	badtype = vfilestat(&vn, &fst);
    478 	if (checkfile) {
    479 		int fsmatch = 0;
    480 		DEVS *d;
    481 
    482 		if (badtype)
    483 			return;
    484 		for (d = devs; d != NULL; d = d->next)
    485 			if (d->fsid == fst.fsid) {
    486 				fsmatch = 1;
    487 				if (d->ino == fst.fileid) {
    488 					filename = d->name;
    489 					break;
    490 				}
    491 			}
    492 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
    493 			return;
    494 	}
    495 	PREFIX(i);
    496 	if (badtype) {
    497 		(void)printf(" -         -  %10s    -\n", badtype);
    498 		return;
    499 	}
    500 	if (nflg)
    501 		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
    502 	else
    503 		(void)printf(" %-8s", getmnton(vn.v_mount));
    504 	if (nflg)
    505 		(void)snprintf(mode, sizeof mode, "%o", fst.mode);
    506 	else
    507 		strmode(fst.mode, mode);
    508 	(void)printf(" %6ld %10s", (long)fst.fileid, mode);
    509 	switch (vn.v_type) {
    510 	case VBLK:
    511 	case VCHR: {
    512 		char *name;
    513 
    514 		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
    515 		    S_IFCHR : S_IFBLK)) == NULL))
    516 			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
    517 		else
    518 			printf(" %6s", name);
    519 		break;
    520 	}
    521 	default:
    522 		printf(" %6lld", (long long)fst.size);
    523 	}
    524 	rw[0] = '\0';
    525 	if (flag & FREAD)
    526 		strcat(rw, "r");
    527 	if (flag & FWRITE)
    528 		strcat(rw, "w");
    529 	printf(" %-2s", rw);
    530 	if (filename && !fsflg)
    531 		printf("  %s", filename);
    532 	putchar('\n');
    533 }
    534 
    535 int
    536 ufs_filestat(vp, fsp)
    537 	struct vnode *vp;
    538 	struct filestat *fsp;
    539 {
    540 	struct inode inode;
    541 
    542 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
    543 		dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
    544 		return 0;
    545 	}
    546 	fsp->fsid = inode.i_dev & 0xffff;
    547 	fsp->fileid = (long)inode.i_number;
    548 	fsp->mode = (mode_t)inode.i_ffs_mode;
    549 	fsp->size = inode.i_ffs_size;
    550 	fsp->rdev = inode.i_ffs_rdev;
    551 
    552 	return 1;
    553 }
    554 
    555 int
    556 ext2fs_filestat(vp, fsp)
    557 	struct vnode *vp;
    558 	struct filestat *fsp;
    559 {
    560 	struct inode inode;
    561 
    562 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
    563 		dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
    564 		return 0;
    565 	}
    566 	fsp->fsid = inode.i_dev & 0xffff;
    567 	fsp->fileid = (long)inode.i_number;
    568 	fsp->mode = (mode_t)inode.i_e2fs_mode;
    569 	fsp->size = inode.i_e2fs_size;
    570 	fsp->rdev = 0;  /* XXX */
    571 	return 1;
    572 }
    573 
    574 int
    575 nfs_filestat(vp, fsp)
    576 	struct vnode *vp;
    577 	struct filestat *fsp;
    578 {
    579 	struct nfsnode nfsnode;
    580 	struct vattr va;
    581 
    582 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
    583 		dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp),
    584 		    Pid);
    585 		return 0;
    586 	}
    587 	if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
    588 		dprintf("can't read vnode attributes at %p for pid %d",
    589 		    nfsnode.n_vattr, Pid);
    590 		return 0;
    591 	}
    592 	fsp->fsid = va.va_fsid;
    593 	fsp->fileid = va.va_fileid;
    594 	fsp->size = nfsnode.n_size;
    595 	fsp->rdev = va.va_rdev;
    596 	fsp->mode = (mode_t)va.va_mode | getftype(vp->v_type);
    597 
    598 	return 1;
    599 }
    600 
    601 int
    602 msdosfs_filestat(vp, fsp)
    603 	struct vnode *vp;
    604 	struct filestat *fsp;
    605 {
    606 	struct denode de;
    607 	struct msdosfsmount mp;
    608 
    609 	if (!KVM_READ(VTONFS(vp), &de, sizeof(de))) {
    610 		dprintf("can't read denode at %p for pid %d", VTONFS(vp),
    611 		    Pid);
    612 		return 0;
    613 	}
    614 	if (!KVM_READ(de.de_pmp, &mp, sizeof(mp))) {
    615 		dprintf("can't read mount struct at %p for pid %d", de.de_pmp,
    616 		    Pid);
    617 		return 0;
    618 	}
    619 
    620 	fsp->fsid = de.de_dev & 0xffff;
    621 	fsp->fileid = 0; /* XXX see msdosfs_vptofh() for more info */
    622 	fsp->size = de.de_FileSize;
    623 	fsp->rdev = 0;	/* msdosfs doesn't support device files */
    624 	fsp->mode = (0777 & mp.pm_mask) | getftype(vp->v_type);
    625 	return 1;
    626 }
    627 
    628 char *
    629 layer_filestat(vp, fsp)
    630 	struct vnode *vp;
    631 	struct filestat *fsp;
    632 {
    633 	struct layer_node layer_node;
    634 	struct mount mount;
    635 	struct vnode vn;
    636 	char *badtype;
    637 
    638 	if (!KVM_READ(VTOLAYER(vp), &layer_node, sizeof(layer_node))) {
    639 		dprintf("can't read layer_node at %p for pid %d",
    640 		    VTOLAYER(vp), Pid);
    641 		return ("error");
    642 	}
    643 	if (!KVM_READ(vp->v_mount, &mount, sizeof(struct mount))) {
    644 		dprintf("can't read mount struct at %p for pid %d",
    645 		    vp->v_mount, Pid);
    646 		return ("error");
    647 	}
    648 	vp = layer_node.layer_lowervp;
    649 	if (!KVM_READ(vp, &vn, sizeof(struct vnode))) {
    650 		dprintf("can't read vnode at %p for pid %d", vp, Pid);
    651 		return ("error");
    652 	}
    653 	if ((badtype = vfilestat(&vn, fsp)) == NULL)
    654 		fsp->fsid = mount.mnt_stat.f_fsid.val[0];
    655 	return (badtype);
    656 }
    657 
    658 char *
    659 getmnton(m)
    660 	struct mount *m;
    661 {
    662 	static struct mount mount;
    663 	static struct mtab {
    664 		struct mtab *next;
    665 		struct mount *m;
    666 		char mntonname[MNAMELEN];
    667 	} *mhead = NULL;
    668 	struct mtab *mt;
    669 
    670 	for (mt = mhead; mt != NULL; mt = mt->next)
    671 		if (m == mt->m)
    672 			return (mt->mntonname);
    673 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
    674 		warnx("can't read mount table at %p", m);
    675 		return (NULL);
    676 	}
    677 	if ((mt = malloc(sizeof (struct mtab))) == NULL) {
    678 		err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab));
    679 	}
    680 	mt->m = m;
    681 	memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0], MNAMELEN);
    682 	mt->next = mhead;
    683 	mhead = mt;
    684 	return (mt->mntonname);
    685 }
    686 
    687 #ifdef INET6
    688 static const char *
    689 inet6_addrstr(p)
    690 	struct in6_addr *p;
    691 {
    692 	struct sockaddr_in6 sin6;
    693 	static char hbuf[NI_MAXHOST];
    694 #ifdef NI_WITHSCOPEID
    695 	const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
    696 #else
    697 	const int niflags = NI_NUMERICHOST;
    698 #endif
    699 
    700 	memset(&sin6, 0, sizeof(sin6));
    701 	sin6.sin6_family = AF_INET6;
    702 	sin6.sin6_len = sizeof(struct sockaddr_in6);
    703 	sin6.sin6_addr = *p;
    704 	if (IN6_IS_ADDR_LINKLOCAL(p) &&
    705 	    *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
    706 		sin6.sin6_scope_id =
    707 			ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
    708 		sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0;
    709 	}
    710 
    711 	if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
    712 			hbuf, sizeof(hbuf), NULL, 0, niflags))
    713 		return "invalid";
    714 
    715 	return hbuf;
    716 }
    717 #endif
    718 
    719 void
    720 socktrans(sock, i)
    721 	struct socket *sock;
    722 	int i;
    723 {
    724 	static char *stypename[] = {
    725 		"unused",	/* 0 */
    726 		"stream", 	/* 1 */
    727 		"dgram",	/* 2 */
    728 		"raw",		/* 3 */
    729 		"rdm",		/* 4 */
    730 		"seqpak"	/* 5 */
    731 	};
    732 #define	STYPEMAX 5
    733 	struct socket	so;
    734 	struct protosw	proto;
    735 	struct domain	dom;
    736 	struct inpcb	inpcb;
    737 #ifdef INET6
    738 	struct in6pcb	in6pcb;
    739 #endif
    740 	struct unpcb	unpcb;
    741 	int len;
    742 	char dname[32];
    743 #ifdef INET6
    744 	char xaddrbuf[NI_MAXHOST + 2];
    745 #endif
    746 
    747 	PREFIX(i);
    748 
    749 	/* fill in socket */
    750 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
    751 		dprintf("can't read sock at %p", sock);
    752 		goto bad;
    753 	}
    754 
    755 	/* fill in protosw entry */
    756 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
    757 		dprintf("can't read protosw at %p", so.so_proto);
    758 		goto bad;
    759 	}
    760 
    761 	/* fill in domain */
    762 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
    763 		dprintf("can't read domain at %p", proto.pr_domain);
    764 		goto bad;
    765 	}
    766 
    767 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
    768 	    sizeof(dname) - 1)) != sizeof(dname) -1) {
    769 		dprintf("can't read domain name at %p", dom.dom_name);
    770 		dname[0] = '\0';
    771 	}
    772 	else
    773 		dname[len] = '\0';
    774 
    775 	if ((u_short)so.so_type > STYPEMAX)
    776 		printf("* %s ?%d", dname, so.so_type);
    777 	else
    778 		printf("* %s %s", dname, stypename[so.so_type]);
    779 
    780 	/*
    781 	 * protocol specific formatting
    782 	 *
    783 	 * Try to find interesting things to print.  For TCP, the interesting
    784 	 * thing is the address of the tcpcb, for UDP and others, just the
    785 	 * inpcb (socket pcb).  For UNIX domain, its the address of the socket
    786 	 * pcb and the address of the connected pcb (if connected).  Otherwise
    787 	 * just print the protocol number and address of the socket itself.
    788 	 * The idea is not to duplicate netstat, but to make available enough
    789 	 * information for further analysis.
    790 	 */
    791 	switch(dom.dom_family) {
    792 	case AF_INET:
    793 		getinetproto(proto.pr_protocol);
    794 		if (proto.pr_protocol == IPPROTO_TCP) {
    795 			if (so.so_pcb == NULL)
    796 				break;
    797 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
    798 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
    799 				dprintf("can't read inpcb at %p", so.so_pcb);
    800 				goto bad;
    801 			}
    802 			printf(" %lx", (long)inpcb.inp_ppcb);
    803 			printf(" %s:%d",
    804 			    inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
    805 			    inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
    806 			if (inpcb.inp_fport) {
    807 				printf(" <-> %s:%d",
    808 				    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
    809 				    inet_ntoa(inpcb.inp_faddr),
    810 				    ntohs(inpcb.inp_fport));
    811 			}
    812 		} else if (proto.pr_protocol == IPPROTO_UDP) {
    813 			if (so.so_pcb == NULL)
    814 				break;
    815 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
    816 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
    817 				dprintf("can't read inpcb at %p", so.so_pcb);
    818 				goto bad;
    819 			}
    820 			printf(" %lx", (long)so.so_pcb);
    821 			printf(" %s:%d",
    822 			    inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
    823 			    inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
    824 			if (inpcb.inp_fport)
    825 				printf(" <-> %s:%d",
    826 				    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
    827 				    inet_ntoa(inpcb.inp_faddr),
    828 				    ntohs(inpcb.inp_fport));
    829 		} else if (so.so_pcb)
    830 			printf(" %lx", (long)so.so_pcb);
    831 		break;
    832 #ifdef INET6
    833 	case AF_INET6:
    834 		getinetproto(proto.pr_protocol);
    835 		if (proto.pr_protocol == IPPROTO_TCP) {
    836 			if (so.so_pcb == NULL)
    837 				break;
    838 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb,
    839 			    sizeof(struct in6pcb)) != sizeof(struct in6pcb)) {
    840 				dprintf("can't read in6pcb at %p", so.so_pcb);
    841 				goto bad;
    842 			}
    843 			printf(" %lx", (long)in6pcb.in6p_ppcb);
    844 			sprintf(xaddrbuf, "[%s]",
    845 			    inet6_addrstr(&in6pcb.in6p_laddr));
    846 			printf(" %s:%d",
    847 			    IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" :
    848 			    xaddrbuf,
    849 			    ntohs(in6pcb.in6p_lport));
    850 			if (in6pcb.in6p_fport) {
    851 				sprintf(xaddrbuf, "[%s]",
    852 				    inet6_addrstr(&in6pcb.in6p_faddr));
    853 				printf(" <-> %s:%d",
    854 			            IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" :
    855 				    xaddrbuf,
    856 				    ntohs(in6pcb.in6p_fport));
    857 			}
    858 		} else if (proto.pr_protocol == IPPROTO_UDP) {
    859 			if (so.so_pcb == NULL)
    860 				break;
    861 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb,
    862 			    sizeof(struct in6pcb)) != sizeof(struct in6pcb)) {
    863 				dprintf("can't read inpcb at %p", so.so_pcb);
    864 				goto bad;
    865 			}
    866 			printf(" %lx", (long)so.so_pcb);
    867 			sprintf(xaddrbuf, "[%s]",
    868 			    inet6_addrstr(&in6pcb.in6p_laddr));
    869 			printf(" %s:%d",
    870 		            IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" :
    871 			    xaddrbuf,
    872 			    ntohs(in6pcb.in6p_lport));
    873 			if (in6pcb.in6p_fport) {
    874 				sprintf(xaddrbuf, "[%s]",
    875 				    inet6_addrstr(&in6pcb.in6p_faddr));
    876 				printf(" <-> %s:%d",
    877 			            IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" :
    878 				    xaddrbuf,
    879 				    ntohs(in6pcb.in6p_fport));
    880 			}
    881 		} else if (so.so_pcb)
    882 			printf(" %lx", (long)so.so_pcb);
    883 		break;
    884 #endif
    885 	case AF_LOCAL:
    886 		/* print address of pcb and connected pcb */
    887 		if (so.so_pcb) {
    888 			printf(" %lx", (long)so.so_pcb);
    889 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
    890 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
    891 				dprintf("can't read unpcb at %p", so.so_pcb);
    892 				goto bad;
    893 			}
    894 			if (unpcb.unp_conn) {
    895 				char shoconn[4], *cp;
    896 
    897 				cp = shoconn;
    898 				if (!(so.so_state & SS_CANTRCVMORE))
    899 					*cp++ = '<';
    900 				*cp++ = '-';
    901 				if (!(so.so_state & SS_CANTSENDMORE))
    902 					*cp++ = '>';
    903 				*cp = '\0';
    904 				printf(" %s %lx", shoconn,
    905 				    (long)unpcb.unp_conn);
    906 			}
    907 		}
    908 		break;
    909 	default:
    910 		/* print protocol number and socket address */
    911 		printf(" %d %lx", proto.pr_protocol, (long)sock);
    912 	}
    913 	printf("\n");
    914 	return;
    915 bad:
    916 	printf("* error\n");
    917 }
    918 
    919 void
    920 ptrans(fp, cpipe, i)
    921 	struct file *fp;
    922 	struct pipe *cpipe;
    923 	int i;
    924 {
    925 	struct pipe cp;
    926 
    927 	PREFIX(i);
    928 
    929 	/* fill in socket */
    930 	if (!KVM_READ(cpipe, &cp, sizeof(struct pipe))) {
    931 		dprintf("can't read pipe at %p", cpipe);
    932 		goto bad;
    933 	}
    934 
    935 	printf("* pipe %p %s %p", cpipe,
    936 		(fp->f_flag == FWRITE) ? "->" : "<-",
    937 		cp.pipe_peer);
    938 	printf("\n");
    939 	return;
    940 bad:
    941 	printf("* error\n");
    942 }
    943 
    944 /*
    945  * getinetproto --
    946  *	print name of protocol number
    947  */
    948 void
    949 getinetproto(number)
    950 	int number;
    951 {
    952 	char *cp;
    953 
    954 	switch (number) {
    955 	case IPPROTO_IP:
    956 		cp = "ip"; break;
    957 	case IPPROTO_ICMP:
    958 		cp ="icmp"; break;
    959 	case IPPROTO_GGP:
    960 		cp ="ggp"; break;
    961 	case IPPROTO_TCP:
    962 		cp ="tcp"; break;
    963 	case IPPROTO_EGP:
    964 		cp ="egp"; break;
    965 	case IPPROTO_PUP:
    966 		cp ="pup"; break;
    967 	case IPPROTO_UDP:
    968 		cp ="udp"; break;
    969 	case IPPROTO_IDP:
    970 		cp ="idp"; break;
    971 	case IPPROTO_RAW:
    972 		cp ="raw"; break;
    973 	case IPPROTO_ICMPV6:
    974 		cp ="icmp6"; break;
    975 	default:
    976 		printf(" %d", number);
    977 		return;
    978 	}
    979 	printf(" %s", cp);
    980 }
    981 
    982 int
    983 getfname(filename)
    984 	char *filename;
    985 {
    986 	struct stat statbuf;
    987 	DEVS *cur;
    988 
    989 	if (stat(filename, &statbuf)) {
    990 		warn("stat(%s)", filename);
    991 		return(0);
    992 	}
    993 	if ((cur = malloc(sizeof(DEVS))) == NULL) {
    994 		err(1, "malloc(%u)", (unsigned int)sizeof(DEVS));
    995 	}
    996 	cur->next = devs;
    997 	devs = cur;
    998 
    999 	cur->ino = statbuf.st_ino;
   1000 	cur->fsid = statbuf.st_dev & 0xffff;
   1001 	cur->name = filename;
   1002 	return(1);
   1003 }
   1004 
   1005 mode_t
   1006 getftype(v_type)
   1007 	enum vtype v_type;
   1008 {
   1009 	mode_t ftype;
   1010 
   1011 	switch (v_type) {
   1012 	case VREG:
   1013 		ftype = S_IFREG;
   1014 		break;
   1015 	case VDIR:
   1016 		ftype = S_IFDIR;
   1017 		break;
   1018 	case VBLK:
   1019 		ftype = S_IFBLK;
   1020 		break;
   1021 	case VCHR:
   1022 		ftype = S_IFCHR;
   1023 		break;
   1024 	case VLNK:
   1025 		ftype = S_IFLNK;
   1026 		break;
   1027 	case VSOCK:
   1028 		ftype = S_IFSOCK;
   1029 		break;
   1030 	case VFIFO:
   1031 		ftype = S_IFIFO;
   1032 		break;
   1033 	default:
   1034 		ftype = 0;
   1035 		break;
   1036 	};
   1037 
   1038 	return ftype;
   1039 }
   1040 
   1041 void
   1042 usage()
   1043 {
   1044 	errx(1,
   1045  "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
   1046 }
   1047