Home | History | Annotate | Line # | Download | only in fstat
fstat.c revision 1.43
      1 /*	$NetBSD: fstat.c,v 1.43 2000/08/14 06:03:21 enami 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.43 2000/08/14 06:03:21 enami 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 _KERNEL
     71 #include <sys/mount.h>
     72 #undef _KERNEL
     73 #define NFS
     74 #include <nfs/nfsproto.h>
     75 #include <nfs/rpcv2.h>
     76 #include <nfs/nfs.h>
     77 #include <nfs/nfsnode.h>
     78 #undef NFS
     79 #include <msdosfs/denode.h>
     80 #include <msdosfs/bpb.h>
     81 #define	_KERNEL
     82 #include <msdosfs/msdosfsmount.h>
     83 #undef _KERNEL
     84 #define	_KERNEL
     85 #include <miscfs/genfs/layer.h>
     86 #undef _KERNEL
     87 
     88 #include <net/route.h>
     89 #include <netinet/in.h>
     90 #include <netinet/in_systm.h>
     91 #include <netinet/ip.h>
     92 #include <netinet/in_pcb.h>
     93 
     94 #ifdef INET6
     95 #include <netinet/ip6.h>
     96 #include <netinet6/ip6_var.h>
     97 #include <netinet6/in6_pcb.h>
     98 #endif
     99 
    100 #include <netdb.h>
    101 #include <arpa/inet.h>
    102 
    103 #include <ctype.h>
    104 #include <errno.h>
    105 #include <kvm.h>
    106 #include <limits.h>
    107 #include <nlist.h>
    108 #include <paths.h>
    109 #include <pwd.h>
    110 #include <stdio.h>
    111 #include <stdlib.h>
    112 #include <string.h>
    113 #include <unistd.h>
    114 #include <err.h>
    115 
    116 #include "fstat.h"
    117 
    118 #define	TEXT	-1
    119 #define	CDIR	-2
    120 #define	RDIR	-3
    121 #define	TRACE	-4
    122 
    123 typedef struct devs {
    124 	struct	devs *next;
    125 	long	fsid;
    126 	ino_t	ino;
    127 	char	*name;
    128 } DEVS;
    129 DEVS *devs;
    130 
    131 int 	fsflg,	/* show files on same filesystem as file(s) argument */
    132 	pflg,	/* show files open by a particular pid */
    133 	uflg;	/* show files open by a particular (effective) user */
    134 int 	checkfile; /* true if restricting to particular files or filesystems */
    135 int	nflg;	/* (numerical) display f.s. and rdev as dev_t */
    136 int	vflg;	/* display errors in locating kernel data objects etc... */
    137 
    138 struct file **ofiles;	/* buffer of pointers to file structures */
    139 int maxfiles;
    140 #define ALLOC_OFILES(d)	\
    141 	if ((d) > maxfiles) { \
    142 		free(ofiles); \
    143 		ofiles = malloc((d) * sizeof(struct file *)); \
    144 		if (ofiles == NULL) { \
    145 			err(1, "malloc(%u)", (d) *	\
    146 					(unsigned int)sizeof(struct file *)); \
    147 		} \
    148 		maxfiles = (d); \
    149 	}
    150 
    151 kvm_t *kd;
    152 
    153 void	dofiles __P((struct kinfo_proc *));
    154 int	ext2fs_filestat __P((struct vnode *, struct filestat *));
    155 int	getfname __P((char *));
    156 void	getinetproto __P((int));
    157 char   *getmnton __P((struct mount *));
    158 int	layer_filestat __P((struct vnode *, struct filestat *,
    159     struct vnode **));
    160 int	main __P((int, char **));
    161 int	msdosfs_filestat __P((struct vnode *, struct filestat *));
    162 int	nfs_filestat __P((struct vnode *, struct filestat *));
    163 #ifdef INET6
    164 static const char *inet6_addrstr __P((struct in6_addr *));
    165 #endif
    166 void	socktrans __P((struct socket *, int));
    167 int	ufs_filestat __P((struct vnode *, struct filestat *));
    168 void	usage __P((void));
    169 void	vtrans __P((struct vnode *, int, int));
    170 void	ftrans __P((struct file *, 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 {
    397 		dprintf("unknown file type %d for file %d of pid %d",
    398 		    file.f_type, i, Pid);
    399 	}
    400 }
    401 
    402 void
    403 vtrans(vp, i, flag)
    404 	struct vnode *vp;
    405 	int i;
    406 	int flag;
    407 {
    408 	struct vnode vn;
    409 	struct filestat fst;
    410 	char mode[15], rw[3];
    411 	char *badtype, *filename;
    412 
    413 again:
    414 	filename = badtype = NULL;
    415 	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
    416 		dprintf("can't read vnode at %p for pid %d", vp, Pid);
    417 		return;
    418 	}
    419 	if (vn.v_type == VNON || vn.v_tag == VT_NON)
    420 		badtype = "none";
    421 	else if (vn.v_type == VBAD)
    422 		badtype = "bad";
    423 	else
    424 		switch (vn.v_tag) {
    425 		case VT_UFS:
    426 			if (!ufs_filestat(&vn, &fst))
    427 				badtype = "error";
    428 			break;
    429 		case VT_MFS:
    430 			if (!ufs_filestat(&vn, &fst))
    431 				badtype = "error";
    432 			break;
    433 		case VT_MSDOSFS:
    434 			if (!msdosfs_filestat(&vn, &fst))
    435 				badtype = "error";
    436 			break;
    437 		case VT_NFS:
    438 			if (!nfs_filestat(&vn, &fst))
    439 				badtype = "error";
    440 			break;
    441 		case VT_EXT2FS:
    442 			if (!ext2fs_filestat(&vn, &fst))
    443 				badtype = "error";
    444 			break;
    445 		case VT_ISOFS:
    446 			if (!isofs_filestat(&vn, &fst))
    447 				badtype = "error";
    448 			break;
    449 		case VT_NTFS:
    450 			if (!ntfs_filestat(&vn, &fst))
    451 				badtype = "error";
    452 			break;
    453 		case VT_NULL:
    454 		case VT_OVERLAY:
    455 		case VT_UMAP:
    456 			if (!layer_filestat(&vn, &fst, &vp))
    457 				badtype = "error";
    458 			else
    459 				goto again;
    460 			break;
    461 		default: {
    462 			static char unknown[10];
    463 			(void)snprintf(badtype = unknown, sizeof unknown,
    464 			    "?(%x)", vn.v_tag);
    465 			break;;
    466 		}
    467 	}
    468 	if (checkfile) {
    469 		int fsmatch = 0;
    470 		DEVS *d;
    471 
    472 		if (badtype)
    473 			return;
    474 		for (d = devs; d != NULL; d = d->next)
    475 			if (d->fsid == fst.fsid) {
    476 				fsmatch = 1;
    477 				if (d->ino == fst.fileid) {
    478 					filename = d->name;
    479 					break;
    480 				}
    481 			}
    482 		if (fsmatch == 0 || (filename == NULL && fsflg == 0))
    483 			return;
    484 	}
    485 	PREFIX(i);
    486 	if (badtype) {
    487 		(void)printf(" -         -  %10s    -\n", badtype);
    488 		return;
    489 	}
    490 	if (nflg)
    491 		(void)printf(" %2d,%-2d", major(fst.fsid), minor(fst.fsid));
    492 	else
    493 		(void)printf(" %-8s", getmnton(vn.v_mount));
    494 	if (nflg)
    495 		(void)snprintf(mode, sizeof mode, "%o", fst.mode);
    496 	else
    497 		strmode(fst.mode, mode);
    498 	(void)printf(" %6ld %10s", (long)fst.fileid, mode);
    499 	switch (vn.v_type) {
    500 	case VBLK:
    501 	case VCHR: {
    502 		char *name;
    503 
    504 		if (nflg || ((name = devname(fst.rdev, vn.v_type == VCHR ?
    505 		    S_IFCHR : S_IFBLK)) == NULL))
    506 			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
    507 		else
    508 			printf(" %6s", name);
    509 		break;
    510 	}
    511 	default:
    512 		printf(" %6qd", (long long)fst.size);
    513 	}
    514 	rw[0] = '\0';
    515 	if (flag & FREAD)
    516 		strcat(rw, "r");
    517 	if (flag & FWRITE)
    518 		strcat(rw, "w");
    519 	printf(" %-2s", rw);
    520 	if (filename && !fsflg)
    521 		printf("  %s", filename);
    522 	putchar('\n');
    523 }
    524 
    525 int
    526 ufs_filestat(vp, fsp)
    527 	struct vnode *vp;
    528 	struct filestat *fsp;
    529 {
    530 	struct inode inode;
    531 
    532 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
    533 		dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
    534 		return 0;
    535 	}
    536 	fsp->fsid = inode.i_dev & 0xffff;
    537 	fsp->fileid = (long)inode.i_number;
    538 	fsp->mode = (mode_t)inode.i_ffs_mode;
    539 	fsp->size = inode.i_ffs_size;
    540 	fsp->rdev = inode.i_ffs_rdev;
    541 
    542 	return 1;
    543 }
    544 
    545 int
    546 ext2fs_filestat(vp, fsp)
    547 	struct vnode *vp;
    548 	struct filestat *fsp;
    549 {
    550 	struct inode inode;
    551 
    552 	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
    553 		dprintf("can't read inode at %p for pid %d", VTOI(vp), Pid);
    554 		return 0;
    555 	}
    556 	fsp->fsid = inode.i_dev & 0xffff;
    557 	fsp->fileid = (long)inode.i_number;
    558 	fsp->mode = (mode_t)inode.i_e2fs_mode;
    559 	fsp->size = inode.i_e2fs_size;
    560 	fsp->rdev = 0;  /* XXX */
    561 	return 1;
    562 }
    563 
    564 int
    565 nfs_filestat(vp, fsp)
    566 	struct vnode *vp;
    567 	struct filestat *fsp;
    568 {
    569 	struct nfsnode nfsnode;
    570 	struct vattr va;
    571 
    572 	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
    573 		dprintf("can't read nfsnode at %p for pid %d", VTONFS(vp),
    574 		    Pid);
    575 		return 0;
    576 	}
    577 	if (!KVM_READ(nfsnode.n_vattr, &va, sizeof(va))) {
    578 		dprintf("can't read vnode attributes at %p for pid %d",
    579 		    nfsnode.n_vattr, Pid);
    580 		return 0;
    581 	}
    582 	fsp->fsid = va.va_fsid;
    583 	fsp->fileid = va.va_fileid;
    584 	fsp->size = nfsnode.n_size;
    585 	fsp->rdev = va.va_rdev;
    586 	fsp->mode = (mode_t)va.va_mode | getftype(vp->v_type);
    587 
    588 	return 1;
    589 }
    590 
    591 int
    592 msdosfs_filestat(vp, fsp)
    593 	struct vnode *vp;
    594 	struct filestat *fsp;
    595 {
    596 	struct denode de;
    597 	struct msdosfsmount mp;
    598 
    599 	if (!KVM_READ(VTONFS(vp), &de, sizeof(de))) {
    600 		dprintf("can't read denode at %p for pid %d", VTONFS(vp),
    601 		    Pid);
    602 		return 0;
    603 	}
    604 	if (!KVM_READ(de.de_pmp, &mp, sizeof(mp))) {
    605 		dprintf("can't read mount struct at %p for pid %d", de.de_pmp,
    606 		    Pid);
    607 		return 0;
    608 	}
    609 
    610 	fsp->fsid = de.de_dev & 0xffff;
    611 	fsp->fileid = 0; /* XXX see msdosfs_vptofh() for more info */
    612 	fsp->size = de.de_FileSize;
    613 	fsp->rdev = 0;	/* msdosfs doesn't support device files */
    614 	fsp->mode = (0777 & mp.pm_mask) | getftype(vp->v_type);
    615 	return 1;
    616 }
    617 
    618 int
    619 layer_filestat(vp, fsp, nvp)
    620 	struct vnode *vp;
    621 	struct filestat *fsp;
    622 	struct vnode **nvp;
    623 {
    624 	struct layer_node layer_node;
    625 
    626 	if (!KVM_READ(VTOLAYER(vp), &layer_node, sizeof(layer_node))) {
    627 		dprintf("can't read layer_node at %p for pid %d",
    628 		    VTOLAYER(vp), Pid);
    629 		return (0);
    630 	}
    631 	*nvp = layer_node.layer_lowervp;
    632 	return (1);
    633 }
    634 
    635 char *
    636 getmnton(m)
    637 	struct mount *m;
    638 {
    639 	static struct mount mount;
    640 	static struct mtab {
    641 		struct mtab *next;
    642 		struct mount *m;
    643 		char mntonname[MNAMELEN];
    644 	} *mhead = NULL;
    645 	struct mtab *mt;
    646 
    647 	for (mt = mhead; mt != NULL; mt = mt->next)
    648 		if (m == mt->m)
    649 			return (mt->mntonname);
    650 	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
    651 		warnx("can't read mount table at %p", m);
    652 		return (NULL);
    653 	}
    654 	if ((mt = malloc(sizeof (struct mtab))) == NULL) {
    655 		err(1, "malloc(%u)", (unsigned int)sizeof(struct mtab));
    656 	}
    657 	mt->m = m;
    658 	memmove(&mt->mntonname[0], &mount.mnt_stat.f_mntonname[0], MNAMELEN);
    659 	mt->next = mhead;
    660 	mhead = mt;
    661 	return (mt->mntonname);
    662 }
    663 
    664 #ifdef INET6
    665 static const char *
    666 inet6_addrstr(p)
    667 	struct in6_addr *p;
    668 {
    669 	struct sockaddr_in6 sin6;
    670 	static char hbuf[NI_MAXHOST];
    671 #ifdef NI_WITHSCOPEID
    672 	const int niflags = NI_NUMERICHOST | NI_WITHSCOPEID;
    673 #else
    674 	const int niflags = NI_NUMERICHOST;
    675 #endif
    676 
    677 	memset(&sin6, 0, sizeof(sin6));
    678 	sin6.sin6_family = AF_INET6;
    679 	sin6.sin6_len = sizeof(struct sockaddr_in6);
    680 	sin6.sin6_addr = *p;
    681 	if (IN6_IS_ADDR_LINKLOCAL(p) &&
    682 	    *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) {
    683 		sin6.sin6_scope_id =
    684 			ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]);
    685 		sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0;
    686 	}
    687 
    688 	if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len,
    689 			hbuf, sizeof(hbuf), NULL, 0, niflags))
    690 		return "invalid";
    691 
    692 	return hbuf;
    693 }
    694 #endif
    695 
    696 void
    697 socktrans(sock, i)
    698 	struct socket *sock;
    699 	int i;
    700 {
    701 	static char *stypename[] = {
    702 		"unused",	/* 0 */
    703 		"stream", 	/* 1 */
    704 		"dgram",	/* 2 */
    705 		"raw",		/* 3 */
    706 		"rdm",		/* 4 */
    707 		"seqpak"	/* 5 */
    708 	};
    709 #define	STYPEMAX 5
    710 	struct socket	so;
    711 	struct protosw	proto;
    712 	struct domain	dom;
    713 	struct inpcb	inpcb;
    714 #ifdef INET6
    715 	struct in6pcb	in6pcb;
    716 #endif
    717 	struct unpcb	unpcb;
    718 	int len;
    719 	char dname[32];
    720 #ifdef INET6
    721 	char xaddrbuf[NI_MAXHOST + 2];
    722 #endif
    723 
    724 	PREFIX(i);
    725 
    726 	/* fill in socket */
    727 	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
    728 		dprintf("can't read sock at %p", sock);
    729 		goto bad;
    730 	}
    731 
    732 	/* fill in protosw entry */
    733 	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
    734 		dprintf("can't read protosw at %p", so.so_proto);
    735 		goto bad;
    736 	}
    737 
    738 	/* fill in domain */
    739 	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
    740 		dprintf("can't read domain at %p", proto.pr_domain);
    741 		goto bad;
    742 	}
    743 
    744 	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
    745 	    sizeof(dname) - 1)) != sizeof(dname) -1) {
    746 		dprintf("can't read domain name at %p", dom.dom_name);
    747 		dname[0] = '\0';
    748 	}
    749 	else
    750 		dname[len] = '\0';
    751 
    752 	if ((u_short)so.so_type > STYPEMAX)
    753 		printf("* %s ?%d", dname, so.so_type);
    754 	else
    755 		printf("* %s %s", dname, stypename[so.so_type]);
    756 
    757 	/*
    758 	 * protocol specific formatting
    759 	 *
    760 	 * Try to find interesting things to print.  For TCP, the interesting
    761 	 * thing is the address of the tcpcb, for UDP and others, just the
    762 	 * inpcb (socket pcb).  For UNIX domain, its the address of the socket
    763 	 * pcb and the address of the connected pcb (if connected).  Otherwise
    764 	 * just print the protocol number and address of the socket itself.
    765 	 * The idea is not to duplicate netstat, but to make available enough
    766 	 * information for further analysis.
    767 	 */
    768 	switch(dom.dom_family) {
    769 	case AF_INET:
    770 		getinetproto(proto.pr_protocol);
    771 		if (proto.pr_protocol == IPPROTO_TCP) {
    772 			if (so.so_pcb == NULL)
    773 				break;
    774 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
    775 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
    776 				dprintf("can't read inpcb at %p", so.so_pcb);
    777 				goto bad;
    778 			}
    779 			printf(" %lx", (long)inpcb.inp_ppcb);
    780 			printf(" %s:%d",
    781 			    inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
    782 			    inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
    783 			if (inpcb.inp_fport) {
    784 				printf(" <-> %s:%d",
    785 				    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
    786 				    inet_ntoa(inpcb.inp_faddr),
    787 				    ntohs(inpcb.inp_fport));
    788 			}
    789 		} else if (proto.pr_protocol == IPPROTO_UDP) {
    790 			if (so.so_pcb == NULL)
    791 				break;
    792 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&inpcb,
    793 			    sizeof(struct inpcb)) != sizeof(struct inpcb)) {
    794 				dprintf("can't read inpcb at %p", so.so_pcb);
    795 				goto bad;
    796 			}
    797 			printf(" %lx", (long)so.so_pcb);
    798 			printf(" %s:%d",
    799 			    inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" :
    800 			    inet_ntoa(inpcb.inp_laddr), ntohs(inpcb.inp_lport));
    801 			if (inpcb.inp_fport)
    802 				printf(" <-> %s:%d",
    803 				    inpcb.inp_faddr.s_addr == INADDR_ANY ? "*" :
    804 				    inet_ntoa(inpcb.inp_faddr),
    805 				    ntohs(inpcb.inp_fport));
    806 		} else if (so.so_pcb)
    807 			printf(" %lx", (long)so.so_pcb);
    808 		break;
    809 #ifdef INET6
    810 	case AF_INET6:
    811 		getinetproto(proto.pr_protocol);
    812 		if (proto.pr_protocol == IPPROTO_TCP) {
    813 			if (so.so_pcb == NULL)
    814 				break;
    815 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&in6pcb,
    816 			    sizeof(struct in6pcb)) != sizeof(struct in6pcb)) {
    817 				dprintf("can't read in6pcb at %p", so.so_pcb);
    818 				goto bad;
    819 			}
    820 			printf(" %lx", (long)in6pcb.in6p_ppcb);
    821 			sprintf(xaddrbuf, "[%s]",
    822 			    inet6_addrstr(&in6pcb.in6p_laddr));
    823 			printf(" %s:%d",
    824 			    IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_laddr) ? "*" :
    825 			    xaddrbuf,
    826 			    ntohs(in6pcb.in6p_lport));
    827 			if (in6pcb.in6p_fport) {
    828 				sprintf(xaddrbuf, "[%s]",
    829 				    inet6_addrstr(&in6pcb.in6p_faddr));
    830 				printf(" <-> %s:%d",
    831 			            IN6_IS_ADDR_UNSPECIFIED(&in6pcb.in6p_faddr) ? "*" :
    832 				    xaddrbuf,
    833 				    ntohs(in6pcb.in6p_fport));
    834 			}
    835 		} else if (proto.pr_protocol == IPPROTO_UDP) {
    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 inpcb at %p", so.so_pcb);
    841 				goto bad;
    842 			}
    843 			printf(" %lx", (long)so.so_pcb);
    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 (so.so_pcb)
    859 			printf(" %lx", (long)so.so_pcb);
    860 		break;
    861 #endif
    862 	case AF_LOCAL:
    863 		/* print address of pcb and connected pcb */
    864 		if (so.so_pcb) {
    865 			printf(" %lx", (long)so.so_pcb);
    866 			if (kvm_read(kd, (u_long)so.so_pcb, (char *)&unpcb,
    867 			    sizeof(struct unpcb)) != sizeof(struct unpcb)){
    868 				dprintf("can't read unpcb at %p", so.so_pcb);
    869 				goto bad;
    870 			}
    871 			if (unpcb.unp_conn) {
    872 				char shoconn[4], *cp;
    873 
    874 				cp = shoconn;
    875 				if (!(so.so_state & SS_CANTRCVMORE))
    876 					*cp++ = '<';
    877 				*cp++ = '-';
    878 				if (!(so.so_state & SS_CANTSENDMORE))
    879 					*cp++ = '>';
    880 				*cp = '\0';
    881 				printf(" %s %lx", shoconn,
    882 				    (long)unpcb.unp_conn);
    883 			}
    884 		}
    885 		break;
    886 	default:
    887 		/* print protocol number and socket address */
    888 		printf(" %d %lx", proto.pr_protocol, (long)sock);
    889 	}
    890 	printf("\n");
    891 	return;
    892 bad:
    893 	printf("* error\n");
    894 }
    895 
    896 /*
    897  * getinetproto --
    898  *	print name of protocol number
    899  */
    900 void
    901 getinetproto(number)
    902 	int number;
    903 {
    904 	char *cp;
    905 
    906 	switch (number) {
    907 	case IPPROTO_IP:
    908 		cp = "ip"; break;
    909 	case IPPROTO_ICMP:
    910 		cp ="icmp"; break;
    911 	case IPPROTO_GGP:
    912 		cp ="ggp"; break;
    913 	case IPPROTO_TCP:
    914 		cp ="tcp"; break;
    915 	case IPPROTO_EGP:
    916 		cp ="egp"; break;
    917 	case IPPROTO_PUP:
    918 		cp ="pup"; break;
    919 	case IPPROTO_UDP:
    920 		cp ="udp"; break;
    921 	case IPPROTO_IDP:
    922 		cp ="idp"; break;
    923 	case IPPROTO_RAW:
    924 		cp ="raw"; break;
    925 	case IPPROTO_ICMPV6:
    926 		cp ="icmp6"; break;
    927 	default:
    928 		printf(" %d", number);
    929 		return;
    930 	}
    931 	printf(" %s", cp);
    932 }
    933 
    934 int
    935 getfname(filename)
    936 	char *filename;
    937 {
    938 	struct stat statbuf;
    939 	DEVS *cur;
    940 
    941 	if (stat(filename, &statbuf)) {
    942 		warn("stat(%s)", filename);
    943 		return(0);
    944 	}
    945 	if ((cur = malloc(sizeof(DEVS))) == NULL) {
    946 		err(1, "malloc(%u)", (unsigned int)sizeof(DEVS));
    947 	}
    948 	cur->next = devs;
    949 	devs = cur;
    950 
    951 	cur->ino = statbuf.st_ino;
    952 	cur->fsid = statbuf.st_dev & 0xffff;
    953 	cur->name = filename;
    954 	return(1);
    955 }
    956 
    957 mode_t
    958 getftype(v_type)
    959 	enum vtype v_type;
    960 {
    961 	mode_t ftype;
    962 
    963 	switch (v_type) {
    964 	case VREG:
    965 		ftype = S_IFREG;
    966 		break;
    967 	case VDIR:
    968 		ftype = S_IFDIR;
    969 		break;
    970 	case VBLK:
    971 		ftype = S_IFBLK;
    972 		break;
    973 	case VCHR:
    974 		ftype = S_IFCHR;
    975 		break;
    976 	case VLNK:
    977 		ftype = S_IFLNK;
    978 		break;
    979 	case VSOCK:
    980 		ftype = S_IFSOCK;
    981 		break;
    982 	case VFIFO:
    983 		ftype = S_IFIFO;
    984 		break;
    985 	default:
    986 		ftype = 0;
    987 		break;
    988 	};
    989 
    990 	return ftype;
    991 }
    992 
    993 void
    994 usage()
    995 {
    996 	errx(1,
    997  "usage: fstat [-fnv] [-p pid] [-u user] [-N system] [-M core] [file ...]\n");
    998 }
    999