svn commit: r195473 - projects/libprocstat/usr.bin/fstat

Stanislav Sedov stas at FreeBSD.org
Wed Jul 8 17:10:17 UTC 2009


Author: stas
Date: Wed Jul  8 17:10:16 2009
New Revision: 195473
URL: http://svn.freebsd.org/changeset/base/195473

Log:
  - Move common interest functions into separate files so they can be reused by
    other modules (e.g. fuser).

Added:
  projects/libprocstat/usr.bin/fstat/common.c
  projects/libprocstat/usr.bin/fstat/common.h
Modified:
  projects/libprocstat/usr.bin/fstat/Makefile
  projects/libprocstat/usr.bin/fstat/cd9660.c
  projects/libprocstat/usr.bin/fstat/fstat.c
  projects/libprocstat/usr.bin/fstat/fstat.h
  projects/libprocstat/usr.bin/fstat/msdosfs.c
  projects/libprocstat/usr.bin/fstat/zfs.c

Modified: projects/libprocstat/usr.bin/fstat/Makefile
==============================================================================
--- projects/libprocstat/usr.bin/fstat/Makefile	Wed Jul  8 17:01:07 2009	(r195472)
+++ projects/libprocstat/usr.bin/fstat/Makefile	Wed Jul  8 17:10:16 2009	(r195473)
@@ -4,7 +4,7 @@
 .include <bsd.own.mk>
 
 PROG=	fstat
-SRCS=	cd9660.c fstat.c msdosfs.c
+SRCS=	cd9660.c common.c fstat.c msdosfs.c
 DPADD=	${LIBKVM}
 LDADD=	-lkvm
 BINGRP=	kmem

Modified: projects/libprocstat/usr.bin/fstat/cd9660.c
==============================================================================
--- projects/libprocstat/usr.bin/fstat/cd9660.c	Wed Jul  8 17:01:07 2009	(r195472)
+++ projects/libprocstat/usr.bin/fstat/cd9660.c	Wed Jul  8 17:10:16 2009	(r195473)
@@ -54,16 +54,18 @@ __FBSDID("$FreeBSD$");
 #include <kvm.h>
 #include <stdio.h>
 
+#include "common.h"
 #include "fstat.h"
 
 int
-isofs_filestat(struct vnode *vp, struct filestat *fsp)
+isofs_filestat(kvm_t *kd, struct vnode *vp, struct filestat *fsp)
 {
 	struct iso_node isonode;
 
-	if (!KVM_READ(VTOI(vp), &isonode, sizeof (isonode))) {
-		dprintf(stderr, "can't read iso_node at %p for pid %d\n",
-		    (void *)VTOI(vp), Pid);
+	if (!kvm_read_all(kd, (unsigned long)VTOI(vp), &isonode,
+	    sizeof(isonode))) {
+		dprintf(stderr, "can't read iso_node at %p\n",
+		    (void *)VTOI(vp));
 		return 0;
 	}
 #if 0
@@ -76,4 +78,3 @@ isofs_filestat(struct vnode *vp, struct 
 	fsp->size = (u_long)isonode.i_size;
 	return 1;
 }
-

Added: projects/libprocstat/usr.bin/fstat/common.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ projects/libprocstat/usr.bin/fstat/common.c	Wed Jul  8 17:10:16 2009	(r195473)
@@ -0,0 +1,275 @@
+/*-
+ * Copyright (c) 1988, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by the University of
+ *	California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include <sys/param.h>
+#include <sys/time.h>
+#include <sys/proc.h>
+#include <sys/user.h>
+#include <sys/stat.h>
+#include <sys/vnode.h>
+#include <sys/socket.h>
+#include <sys/socketvar.h>
+#include <sys/domain.h>
+#include <sys/protosw.h>
+#include <sys/un.h>
+#include <sys/unpcb.h>
+#include <sys/sysctl.h>
+#include <sys/tty.h>
+#include <sys/filedesc.h>
+#include <sys/queue.h>
+#define	_WANT_FILE
+#include <sys/file.h>
+#include <sys/conf.h>
+#define	_KERNEL
+#include <sys/pipe.h>
+#include <sys/mount.h>
+#include <ufs/ufs/quota.h>
+#include <ufs/ufs/inode.h>
+#include <fs/devfs/devfs.h>
+#include <fs/devfs/devfs_int.h>
+#undef _KERNEL
+#include <nfs/nfsproto.h>
+#include <nfsclient/nfs.h>
+#include <nfsclient/nfsnode.h>
+
+
+#include <vm/vm.h>
+#include <vm/vm_map.h>
+#include <vm/vm_object.h>
+
+#include <net/route.h>
+#include <netinet/in.h>
+#include <netinet/in_systm.h>
+#include <netinet/ip.h>
+#include <netinet/in_pcb.h>
+
+#include <ctype.h>
+#include <err.h>
+#include <fcntl.h>
+#include <kvm.h>
+#include <limits.h>
+#include <nlist.h>
+#include <paths.h>
+#include <pwd.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <stddef.h>
+#include <string.h>
+#include <unistd.h>
+#include <netdb.h>
+
+#include "common.h"
+
+int vflg = 0;
+
+void
+dprintf(FILE *file, const char *fmt, ...) {
+	va_list ap;
+
+	if (vflg != 0) {
+		va_start(ap, fmt);
+		vfprintf(file, fmt, ap);
+		va_end(ap);
+	}
+}
+
+int
+kvm_read_all(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes)
+{
+	ssize_t error;
+
+	if (nbytes >= SSIZE_MAX)
+		return (0);
+	error = kvm_read(kd, addr, buf, nbytes);
+	return (error == (ssize_t)(nbytes));
+}
+
+char *
+kdevtoname(kvm_t *kd, struct cdev *dev)
+{
+	struct cdev si;
+
+	if (!kvm_read_all(kd, (unsigned long)dev, &si, sizeof(si)))
+		return (NULL);
+	return (strdup(si.__si_namebuf));
+}
+
+int
+ufs_filestat(kvm_t *kd, struct vnode *vp, struct filestat *fsp)
+{
+	struct inode inode;
+
+	if (!kvm_read_all(kd, (unsigned long)VTOI(vp), &inode, sizeof(inode))) {
+		dprintf(stderr, "can't read inode at %p\n", (void *)VTOI(vp));
+		return 0;
+	}
+	/*
+	 * The st_dev from stat(2) is a dev_t. These kernel structures
+	 * contain cdev pointers. We need to convert to dev_t to make
+	 * comparisons
+	 */
+	fsp->fsid = dev2udev(kd, inode.i_dev);
+	fsp->fileid = (long)inode.i_number;
+	fsp->mode = (mode_t)inode.i_mode;
+	fsp->size = (u_long)inode.i_size;
+#if should_be_but_is_hard
+	/* XXX - need to load i_ump and i_din[12] from kernel memory */
+	if (inode.i_ump->um_fstype == UFS1)
+		fsp->rdev = inode.i_din1->di_rdev;
+	else
+		fsp->rdev = inode.i_din2->di_rdev;
+#else
+	fsp->rdev = 0;
+#endif
+
+	return 1;
+}
+
+int
+devfs_filestat(kvm_t *kd, struct vnode *vp, struct filestat *fsp)
+{
+	struct devfs_dirent devfs_dirent;
+	struct mount mount;
+	struct vnode vnode;
+
+	if (!kvm_read_all(kd, (unsigned long)vp->v_data, &devfs_dirent,
+	    sizeof(devfs_dirent))) {
+		dprintf(stderr, "can't read devfs_dirent at %p\n",
+		    (void *)vp->v_data);
+		return 0;
+	}
+	if (!kvm_read_all(kd, (unsigned long)vp->v_mount, &mount,
+	    sizeof(mount))) {
+		dprintf(stderr, "can't read mount at %p\n",
+		    (void *)vp->v_mount);
+		return 0;
+	}
+	if (!kvm_read_all(kd, (unsigned long)devfs_dirent.de_vnode, &vnode,
+	    sizeof(vnode))) {
+		dprintf(stderr, "can't read vnode at %p\n",
+		    (void *)devfs_dirent.de_vnode);
+		return 0;
+	}
+	fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0];
+	fsp->fileid = devfs_dirent.de_inode;
+	fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR;
+	fsp->size = 0;
+	fsp->rdev = dev2udev(kd, vnode.v_rdev);
+
+	return 1;
+}
+
+int
+nfs_filestat(kvm_t *kd, struct vnode *vp, struct filestat *fsp)
+{
+	struct nfsnode nfsnode;
+	mode_t mode;
+
+	if (!kvm_read_all(kd, (unsigned long)VTONFS(vp), &nfsnode,
+	    sizeof(nfsnode))) {
+		dprintf(stderr, "can't read nfsnode at %p\n",
+		    (void *)VTONFS(vp));
+		return 0;
+	}
+	fsp->fsid = nfsnode.n_vattr.va_fsid;
+	fsp->fileid = nfsnode.n_vattr.va_fileid;
+	fsp->size = nfsnode.n_size;
+	fsp->rdev = nfsnode.n_vattr.va_rdev;
+	mode = (mode_t)nfsnode.n_vattr.va_mode;
+	switch (vp->v_type) {
+	case VREG:
+		mode |= S_IFREG;
+		break;
+	case VDIR:
+		mode |= S_IFDIR;
+		break;
+	case VBLK:
+		mode |= S_IFBLK;
+		break;
+	case VCHR:
+		mode |= S_IFCHR;
+		break;
+	case VLNK:
+		mode |= S_IFLNK;
+		break;
+	case VSOCK:
+		mode |= S_IFSOCK;
+		break;
+	case VFIFO:
+		mode |= S_IFIFO;
+		break;
+	case VNON:
+	case VBAD:
+	case VMARKER:
+		return 0;
+	};
+	fsp->mode = mode;
+
+	return 1;
+}
+
+/*
+ * Read the cdev structure in the kernel in order to work out the
+ * associated dev_t
+ */
+dev_t
+dev2udev(kvm_t *kd, struct cdev *dev)
+{
+	struct cdev_priv priv;
+
+	if (kvm_read_all(kd, (unsigned long)cdev2priv(dev), &priv,
+	    sizeof(priv))) {
+		return ((dev_t)priv.cdp_inode);
+	} else {
+		dprintf(stderr, "can't convert cdev *%p to a dev_t\n", dev);
+		return -1;
+	}
+}
+
+#ifdef ZFS
+void *
+getvnodedata(struct vnode *vp)
+{
+	return (vp->v_data);
+}
+
+struct mount *
+getvnodemount(struct vnode *vp)
+{
+	return (vp->v_mount);
+}
+#endif

Added: projects/libprocstat/usr.bin/fstat/common.h
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ projects/libprocstat/usr.bin/fstat/common.h	Wed Jul  8 17:10:16 2009	(r195473)
@@ -0,0 +1,69 @@
+/*-
+ * Copyright (c) 1988, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *	This product includes software developed by the University of
+ *	California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * $FreeBSD$
+ */
+
+#ifndef	__COMMON_H__
+#define	__COMMON_H__
+
+struct  filestat {
+	long	fsid;
+	long	fileid;
+	mode_t	mode;
+	u_long	size;
+	dev_t	rdev;
+};
+
+extern int vflg;
+
+dev_t	dev2udev(kvm_t *kd, struct cdev *dev);
+void	dprintf(FILE *file, const char *fmt, ...);
+char	*kdevtoname(kvm_t *kd, struct cdev *dev);
+int	kvm_read_all(kvm_t *kd, unsigned long addr, void *buf,
+    size_t nbytes);
+
+/*
+ * Filesystems specific access routines.
+ */
+int	devfs_filestat(kvm_t *kd, struct vnode *vp, struct filestat *fsp);
+int	isofs_filestat(kvm_t *kd, struct vnode *vp, struct filestat *fsp);
+int	msdosfs_filestat(kvm_t *kd, struct vnode *vp, struct filestat *fsp);
+int	nfs_filestat(kvm_t *kd, struct vnode *vp, struct filestat *fsp);
+int	ufs_filestat(kvm_t *kd, struct vnode *vp, struct filestat *fsp);
+#ifdef ZFS
+int	zfs_filestat(kvm_t *kd, struct vnode *vp, struct filestat *fsp);
+void	*getvnodedata(struct vnode *vp);
+struct mount	*getvnodemount(struct vnode *vp);
+#endif
+
+#endif /* __COMMON_H__ */

Modified: projects/libprocstat/usr.bin/fstat/fstat.c
==============================================================================
--- projects/libprocstat/usr.bin/fstat/fstat.c	Wed Jul  8 17:01:07 2009	(r195472)
+++ projects/libprocstat/usr.bin/fstat/fstat.c	Wed Jul  8 17:10:16 2009	(r195473)
@@ -102,6 +102,7 @@ __FBSDID("$FreeBSD$");
 #include <unistd.h>
 #include <netdb.h>
 
+#include "common.h"
 #include "fstat.h"
 
 #define	TEXT	-1
@@ -124,7 +125,6 @@ int 	fsflg,	/* show files on same filesy
 	uflg;	/* show files open by a particular (effective) user */
 int 	checkfile; /* true if restricting to particular files or filesystems */
 int	nflg;	/* (numerical) display f.s. and rdev as dev_t */
-int	vflg;	/* display errors in locating kernel data objects etc... */
 int	mflg;	/* include memory-mapped files */
 
 
@@ -148,9 +148,6 @@ static void fstat_sysctl(int, int);
 void dofiles(struct kinfo_proc *kp);
 void dommap(struct kinfo_proc *kp);
 void vtrans(struct vnode *vp, int i, int flag);
-int  ufs_filestat(struct vnode *vp, struct filestat *fsp);
-int  nfs_filestat(struct vnode *vp, struct filestat *fsp);
-int  devfs_filestat(struct vnode *vp, struct filestat *fsp);
 char *getmnton(struct mount *m);
 void pipetrans(struct pipe *pi, int i, int flag);
 void socktrans(struct socket *sock, int i);
@@ -158,7 +155,6 @@ void ptstrans(struct tty *tp, int i, int
 void getinetproto(int number);
 int  getfname(const char *filename);
 void usage(void);
-char *kdevtoname(struct cdev *dev);
 
 int
 main(int argc, char **argv)
@@ -338,7 +334,8 @@ dofiles(struct kinfo_proc *kp)
 
 	if (kp->ki_fd == NULL)
 		return;
-	if (!KVM_READ(kp->ki_fd, &filed, sizeof (filed))) {
+	if (!kvm_read_all(kd, (unsigned long)kp->ki_fd, &filed,
+	    sizeof(filed))) {
 		dprintf(stderr, "can't read filedesc at %p for pid %d\n",
 		    (void *)kp->ki_fd, Pid);
 		return;
@@ -379,7 +376,7 @@ dofiles(struct kinfo_proc *kp)
 		return;
 
 	ALLOC_OFILES(filed.fd_lastfile+1);
-	if (!KVM_READ(filed.fd_ofiles, ofiles,
+	if (!kvm_read_all(kd, (unsigned long)filed.fd_ofiles, ofiles,
 	    (filed.fd_lastfile+1) * FPSIZE)) {
 		dprintf(stderr,
 		    "can't read file structures at %p for pid %d\n",
@@ -389,7 +386,8 @@ dofiles(struct kinfo_proc *kp)
 	for (i = 0; i <= filed.fd_lastfile; i++) {
 		if (ofiles[i] == NULL)
 			continue;
-		if (!KVM_READ(ofiles[i], &file, sizeof (struct file))) {
+		if (!kvm_read_all(kd, (unsigned long)ofiles[i], &file,
+		    sizeof(struct file))) {
 			dprintf(stderr, "can't read file %d at %p for pid %d\n",
 			    i, (void *)ofiles[i], Pid);
 			continue;
@@ -437,7 +435,8 @@ dommap(struct kinfo_proc *kp)
 	vm_object_t objp;
 	int prot, fflags;
 
-	if (!KVM_READ(kp->ki_vmspace, &vmspace, sizeof(vmspace))) {
+	if (!kvm_read_all(kd, (unsigned long)kp->ki_vmspace, &vmspace,
+	    sizeof(vmspace))) {
 		dprintf(stderr,
 		    "can't read vmspace at %p for pid %d\n",
 		    (void *)kp->ki_vmspace, Pid);
@@ -447,7 +446,8 @@ dommap(struct kinfo_proc *kp)
 
 	for (entryp = map->header.next;
 	    entryp != &kp->ki_vmspace->vm_map.header; entryp = entry.next) {
-		if (!KVM_READ(entryp, &entry, sizeof(entry))) {
+		if (!kvm_read_all(kd, (unsigned long)entryp, &entry,
+		    sizeof(entry))) {
 			dprintf(stderr,
 			    "can't read vm_map_entry at %p for pid %d\n",
 			    (void *)entryp, Pid);
@@ -461,7 +461,8 @@ dommap(struct kinfo_proc *kp)
 			continue;
 
 		for (; objp; objp = object.backing_object) {
-			if (!KVM_READ(objp, &object, sizeof(object))) {
+			if (!kvm_read_all(kd, (unsigned long)objp, &object,
+			    sizeof(object))) {
 				dprintf(stderr,
 				    "can't read vm_object at %p for pid %d\n",
 				    (void *)objp, Pid);
@@ -483,16 +484,6 @@ dommap(struct kinfo_proc *kp)
 	}
 }
 
-char *
-kdevtoname(struct cdev *dev)
-{
-	struct cdev si;
-
-	if (!KVM_READ(dev, &si, sizeof si))
-		return (NULL);
-	return (strdup(si.__si_namebuf));
-}
-
 void
 vtrans(struct vnode *vp, int i, int flag)
 {
@@ -502,13 +493,14 @@ vtrans(struct vnode *vp, int i, int flag
 	const char *badtype, *filename;
 
 	filename = badtype = NULL;
-	if (!KVM_READ(vp, &vn, sizeof (struct vnode))) {
+	if (!kvm_read_all(kd, (unsigned long)vp, &vn, sizeof(struct vnode))) {
 		dprintf(stderr, "can't read vnode at %p for pid %d\n",
 		    (void *)vp, Pid);
 		return;
 	}
-	if (!KVM_READ(&vp->v_tag, &tagptr, sizeof tagptr) ||
-	    !KVM_READ(tagptr, tagstr, sizeof tagstr)) {
+	if (!kvm_read_all(kd, (unsigned long)&vp->v_tag, &tagptr,
+	    sizeof(tagptr)) || !kvm_read_all(kd, (unsigned long)tagptr, tagstr,
+	    sizeof(tagstr))) {
 		dprintf(stderr, "can't read v_tag at %p for pid %d\n",
 		    (void *)vp, Pid);
 		return;
@@ -520,23 +512,23 @@ vtrans(struct vnode *vp, int i, int flag
 		badtype = "bad";
 	else {
 		if (!strcmp("ufs", tagstr)) {
-			if (!ufs_filestat(&vn, &fst))
+			if (!ufs_filestat(kd, &vn, &fst))
 				badtype = "error";
 		} else if (!strcmp("devfs", tagstr)) {
-			if (!devfs_filestat(&vn, &fst))
+			if (!devfs_filestat(kd, &vn, &fst))
 				badtype = "error";
 		} else if (!strcmp("nfs", tagstr)) {
-			if (!nfs_filestat(&vn, &fst))
+			if (!nfs_filestat(kd, &vn, &fst))
 				badtype = "error";
 		} else if (!strcmp("msdosfs", tagstr)) {
-			if (!msdosfs_filestat(&vn, &fst))
+			if (!msdosfs_filestat(kd, &vn, &fst))
 				badtype = "error";
 		} else if (!strcmp("isofs", tagstr)) {
-			if (!isofs_filestat(&vn, &fst))
+			if (!isofs_filestat(kd, &vn, &fst))
 				badtype = "error";
 #ifdef ZFS
 		} else if (!strcmp("zfs", tagstr)) {
-			if (!zfs_filestat(&vn, &fst))
+			if (!zfs_filestat(kd, &vn, &fst))
 				badtype = "error";
 #endif
 		} else {
@@ -581,7 +573,7 @@ vtrans(struct vnode *vp, int i, int flag
 	case VCHR: {
 		char *name;
 
-		name = kdevtoname(vn.v_rdev);
+		name = kdevtoname(kd, vn.v_rdev);
 		if (nflg || !name)
 			printf("  %2d,%-2d", major(fst.rdev), minor(fst.rdev));
 		else {
@@ -604,118 +596,6 @@ vtrans(struct vnode *vp, int i, int flag
 	putchar('\n');
 }
 
-int
-ufs_filestat(struct vnode *vp, struct filestat *fsp)
-{
-	struct inode inode;
-
-	if (!KVM_READ(VTOI(vp), &inode, sizeof (inode))) {
-		dprintf(stderr, "can't read inode at %p for pid %d\n",
-		    (void *)VTOI(vp), Pid);
-		return 0;
-	}
-	/*
-	 * The st_dev from stat(2) is a dev_t. These kernel structures
-	 * contain cdev pointers. We need to convert to dev_t to make
-	 * comparisons
-	 */
-	fsp->fsid = dev2udev(inode.i_dev);
-	fsp->fileid = (long)inode.i_number;
-	fsp->mode = (mode_t)inode.i_mode;
-	fsp->size = (u_long)inode.i_size;
-#if should_be_but_is_hard
-	/* XXX - need to load i_ump and i_din[12] from kernel memory */
-	if (inode.i_ump->um_fstype == UFS1)
-		fsp->rdev = inode.i_din1->di_rdev;
-	else
-		fsp->rdev = inode.i_din2->di_rdev;
-#else
-	fsp->rdev = 0;
-#endif
-
-	return 1;
-}
-
-int
-devfs_filestat(struct vnode *vp, struct filestat *fsp)
-{
-	struct devfs_dirent devfs_dirent;
-	struct mount mount;
-	struct vnode vnode;
-
-	if (!KVM_READ(vp->v_data, &devfs_dirent, sizeof (devfs_dirent))) {
-		dprintf(stderr, "can't read devfs_dirent at %p for pid %d\n",
-		    (void *)vp->v_data, Pid);
-		return 0;
-	}
-	if (!KVM_READ(vp->v_mount, &mount, sizeof (mount))) {
-		dprintf(stderr, "can't read mount at %p for pid %d\n",
-		    (void *)vp->v_mount, Pid);
-		return 0;
-	}
-	if (!KVM_READ(devfs_dirent.de_vnode, &vnode, sizeof (vnode))) {
-		dprintf(stderr, "can't read vnode at %p for pid %d\n",
-		    (void *)devfs_dirent.de_vnode, Pid);
-		return 0;
-	}
-	fsp->fsid = (long)mount.mnt_stat.f_fsid.val[0];
-	fsp->fileid = devfs_dirent.de_inode;
-	fsp->mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR;
-	fsp->size = 0;
-	fsp->rdev = dev2udev(vnode.v_rdev);
-
-	return 1;
-}
-
-int
-nfs_filestat(struct vnode *vp, struct filestat *fsp)
-{
-	struct nfsnode nfsnode;
-	mode_t mode;
-
-	if (!KVM_READ(VTONFS(vp), &nfsnode, sizeof (nfsnode))) {
-		dprintf(stderr, "can't read nfsnode at %p for pid %d\n",
-		    (void *)VTONFS(vp), Pid);
-		return 0;
-	}
-	fsp->fsid = nfsnode.n_vattr.va_fsid;
-	fsp->fileid = nfsnode.n_vattr.va_fileid;
-	fsp->size = nfsnode.n_size;
-	fsp->rdev = nfsnode.n_vattr.va_rdev;
-	mode = (mode_t)nfsnode.n_vattr.va_mode;
-	switch (vp->v_type) {
-	case VREG:
-		mode |= S_IFREG;
-		break;
-	case VDIR:
-		mode |= S_IFDIR;
-		break;
-	case VBLK:
-		mode |= S_IFBLK;
-		break;
-	case VCHR:
-		mode |= S_IFCHR;
-		break;
-	case VLNK:
-		mode |= S_IFLNK;
-		break;
-	case VSOCK:
-		mode |= S_IFSOCK;
-		break;
-	case VFIFO:
-		mode |= S_IFIFO;
-		break;
-	case VNON:
-	case VBAD:
-	case VMARKER:
-		return 0;
-	};
-	fsp->mode = mode;
-
-	return 1;
-}
-
-
 char *
 getmnton(struct mount *m)
 {
@@ -730,7 +610,7 @@ getmnton(struct mount *m)
 	for (mt = mhead; mt != NULL; mt = mt->next)
 		if (m == mt->m)
 			return (mt->mntonname);
-	if (!KVM_READ(m, &mount, sizeof(struct mount))) {
+	if (!kvm_read_all(kd, (unsigned long)m, &mount, sizeof(struct mount))) {
 		warnx("can't read mount table at %p", (void *)m);
 		return (NULL);
 	}
@@ -752,7 +632,7 @@ pipetrans(struct pipe *pi, int i, int fl
 	PREFIX(i);
 
 	/* fill in socket */
-	if (!KVM_READ(pi, &pip, sizeof(struct pipe))) {
+	if (!kvm_read_all(kd, (unsigned long)pi, &pip, sizeof(struct pipe))) {
 		dprintf(stderr, "can't read pipe at %p\n", (void *)pi);
 		goto bad;
 	}
@@ -795,26 +675,29 @@ socktrans(struct socket *sock, int i)
 	PREFIX(i);
 
 	/* fill in socket */
-	if (!KVM_READ(sock, &so, sizeof(struct socket))) {
+	if (!kvm_read_all(kd, (unsigned long)sock, &so,
+	    sizeof(struct socket))) {
 		dprintf(stderr, "can't read sock at %p\n", (void *)sock);
 		goto bad;
 	}
 
 	/* fill in protosw entry */
-	if (!KVM_READ(so.so_proto, &proto, sizeof(struct protosw))) {
+	if (!kvm_read_all(kd, (unsigned long)so.so_proto, &proto,
+	    sizeof(struct protosw))) {
 		dprintf(stderr, "can't read protosw at %p",
 		    (void *)so.so_proto);
 		goto bad;
 	}
 
 	/* fill in domain */
-	if (!KVM_READ(proto.pr_domain, &dom, sizeof(struct domain))) {
+	if (!kvm_read_all(kd, (unsigned long)proto.pr_domain, &dom,
+	    sizeof(struct domain))) {
 		dprintf(stderr, "can't read domain at %p\n",
 		    (void *)proto.pr_domain);
 		goto bad;
 	}
 
-	if ((len = kvm_read(kd, (u_long)dom.dom_name, dname,
+	if ((len = kvm_read(kd, (unsigned long)dom.dom_name, dname,
 	    sizeof(dname) - 1)) < 0) {
 		dprintf(stderr, "can't read domain name at %p\n",
 		    (void *)dom.dom_name);
@@ -905,13 +788,13 @@ ptstrans(struct tty *tp, int i, int flag
 	PREFIX(i);
 
 	/* Obtain struct tty. */
-	if (!KVM_READ(tp, &tty, sizeof(struct tty))) {
+	if (!kvm_read_all(kd, (unsigned long)tp, &tty, sizeof(struct tty))) {
 		dprintf(stderr, "can't read tty at %p\n", (void *)tp);
 		goto bad;
 	}
 
 	/* Figure out the device name. */
-	name = kdevtoname(tty.t_dev);
+	name = kdevtoname(kd, tty.t_dev);
 	if (name == NULL) {
 		dprintf(stderr, "can't determine tty name at %p\n", (void *)tp);
 		goto bad;
@@ -925,7 +808,7 @@ ptstrans(struct tty *tp, int i, int flag
 
 	printf("* pseudo-terminal master ");
 	if (nflg || !name) {
-		rdev = dev2udev(tty.t_dev);
+		rdev = dev2udev(kd, tty.t_dev);
 		printf("%10d,%-2d", major(rdev), minor(rdev));
 	} else {
 		printf("%10s", name);
@@ -940,23 +823,6 @@ bad:
 }
 
 /*
- * Read the cdev structure in the kernel in order to work out the
- * associated dev_t
- */
-dev_t
-dev2udev(struct cdev *dev)
-{
-	struct cdev_priv priv;
-
-	if (KVM_READ(cdev2priv(dev), &priv, sizeof priv)) {
-		return ((dev_t)priv.cdp_inode);
-	} else {
-		dprintf(stderr, "can't convert cdev *%p to a dev_t\n", dev);
-		return -1;
-	}
-}
-
-/*
  * getinetproto --
  *	print name of protocol number
  */
@@ -995,20 +861,6 @@ getfname(const char *filename)
 	return(1);
 }
 
-#ifdef ZFS
-void *
-getvnodedata(struct vnode *vp)
-{
-	return (vp->v_data);
-}
-
-struct mount *
-getvnodemount(struct vnode *vp)
-{
-	return (vp->v_mount);
-}
-#endif
-
 void
 usage(void)
 {

Modified: projects/libprocstat/usr.bin/fstat/fstat.h
==============================================================================
--- projects/libprocstat/usr.bin/fstat/fstat.h	Wed Jul  8 17:01:07 2009	(r195472)
+++ projects/libprocstat/usr.bin/fstat/fstat.h	Wed Jul  8 17:10:16 2009	(r195473)
@@ -36,15 +36,6 @@
 #ifndef	__FSTAT_H__
 #define	__FSTAT_H__
 
-/*
- * a kvm_read that returns true if everything is read
- */
-#define KVM_READ(kaddr, paddr, len) \
-	((len) < SSIZE_MAX && \
-	kvm_read(kd, (u_long)(kaddr), (char *)(paddr), (len)) == (ssize_t)(len))
-
-#define dprintf	if (vflg) fprintf
-
 typedef struct devs {
 	struct	devs *next;
 	long	fsid;
@@ -52,29 +43,4 @@ typedef struct devs {
 	const char	*name;
 } DEVS;
 
-struct  filestat {
-	long	fsid;
-	long	fileid;
-	mode_t	mode;
-	u_long	size;
-	dev_t	rdev;
-};
-
-/* Ugh */
-extern kvm_t *kd;
-extern int vflg;
-extern int Pid;
-
-dev_t dev2udev(struct cdev *dev);
-
-/* Additional filesystem types */
-int isofs_filestat(struct vnode *vp, struct filestat *fsp);
-int msdosfs_filestat(struct vnode *vp, struct filestat *fsp);
-
-#ifdef ZFS
-int zfs_filestat(struct vnode *vp, struct filestat *fsp);
-void *getvnodedata(struct vnode *vp);
-struct mount *getvnodemount(struct vnode *vp);
-#endif
-
 #endif /* __FSTAT_H__ */

Modified: projects/libprocstat/usr.bin/fstat/msdosfs.c
==============================================================================
--- projects/libprocstat/usr.bin/fstat/msdosfs.c	Wed Jul  8 17:01:07 2009	(r195472)
+++ projects/libprocstat/usr.bin/fstat/msdosfs.c	Wed Jul  8 17:10:16 2009	(r195473)
@@ -64,6 +64,7 @@ __FBSDID("$FreeBSD$");
  */
 #define VTODE(vp) ((struct denode *)(vp)->v_data)
 
+#include "common.h"
 #include "fstat.h"
 
 struct dosmount {
@@ -73,7 +74,7 @@ struct dosmount {
 };
 
 int
-msdosfs_filestat(struct vnode *vp, struct filestat *fsp)
+msdosfs_filestat(kvm_t *kd, struct vnode *vp, struct filestat *fsp)
 {
 	struct denode denode;
 	static struct dosmount *mounts;
@@ -81,9 +82,9 @@ msdosfs_filestat(struct vnode *vp, struc
 	u_long dirsperblk;
 	int fileid;
 
-	if (!KVM_READ(VTODE(vp), &denode, sizeof (denode))) {
-		dprintf(stderr, "can't read denode at %p for pid %d\n",
-		    (void *)VTODE(vp), Pid);
+	if (!kvm_read_all(kd, (unsigned long)VTODE(vp), &denode,
+	    sizeof(denode))) {
+		dprintf(stderr, "can't read denode at %p\n", (void *)VTODE(vp));
 		return 0;
 	}
 
@@ -98,11 +99,12 @@ msdosfs_filestat(struct vnode *vp, struc
 	if (!mnt) {
 		if ((mnt = malloc(sizeof(struct dosmount))) == NULL)
 			err(1, NULL);
-		if (!KVM_READ(denode.de_pmp, &mnt->data, sizeof mnt->data)) {
+		if (!kvm_read_all(kd, (unsigned long)denode.de_pmp,
+		    &mnt->data, sizeof(mnt->data))) {
 			free(mnt);
 			dprintf(stderr,
-			    "can't read mount info at %p for pid %d\n",
-			    (void *)denode.de_pmp, Pid);
+			    "can't read mount info at %p\n",
+			    (void *)denode.de_pmp);
 			return 0;
 		}
 		mnt->next = mounts;
@@ -110,7 +112,7 @@ msdosfs_filestat(struct vnode *vp, struc
 		mnt->kptr = denode.de_pmp;
 	}
 
-	fsp->fsid = dev2udev(mnt->data.pm_dev);
+	fsp->fsid = dev2udev(kd, mnt->data.pm_dev);
 	fsp->mode = 0555;
 	fsp->mode |= denode.de_Attributes & ATTR_READONLY ? 0 : 0222;
 	fsp->mode &= mnt->data.pm_mask;

Modified: projects/libprocstat/usr.bin/fstat/zfs.c
==============================================================================
--- projects/libprocstat/usr.bin/fstat/zfs.c	Wed Jul  8 17:01:07 2009	(r195472)
+++ projects/libprocstat/usr.bin/fstat/zfs.c	Wed Jul  8 17:10:16 2009	(r195473)
@@ -51,7 +51,8 @@
 
 #define ZFS
 #undef dprintf
-#include <fstat.h>
+#include "common.h"
+#include "fstat.h"
 
 /* 
  * Offset calculations that are used to get data from znode without having the
@@ -61,7 +62,7 @@
 #define LOCATION_ZPHYS(zsize) ((zsize) - (2 * sizeof(void *) + sizeof(struct task)))
 
 int
-zfs_filestat(struct vnode *vp, struct filestat *fsp)
+zfs_filestat(kvm_t *kd, struct vnode *vp, struct filestat *fsp)
 {
 
 	znode_phys_t zphys;
@@ -86,9 +87,9 @@ zfs_filestat(struct vnode *vp, struct fi
 
 	/* Since we have problems including vnode.h, we'll use the wrappers. */
 	vnodeptr = getvnodedata(vp);
-	if (!KVM_READ(vnodeptr, znodeptr, (size_t)size)) {
-		dprintf(stderr, "can't read znode at %p for pid %d\n",
-		    (void *)vnodeptr, Pid);
+	if (!kvm_read_all(kd, (unsigned long)vnodeptr, znodeptr,
+	    (size_t)size)) {
+		dprintf(stderr, "can't read znode at %p\n", (void *)vnodeptr);
 		goto bad;
 	}
 
@@ -103,17 +104,16 @@ zfs_filestat(struct vnode *vp, struct fi
 	zid = (uint64_t *)(dataptr + LOCATION_ZID);
 	zphys_addr = *(void **)(dataptr + LOCATION_ZPHYS(size));
 
-	if (!KVM_READ(zphys_addr, &zphys, sizeof(zphys))) {
-		dprintf(stderr, "can't read znode_phys at %p for pid %d\n",
-		    zphys_addr, Pid);
+	if (!kvm_read_all(kd, (unsigned long)zphys_addr, &zphys,
+	    sizeof(zphys))) {
+		dprintf(stderr, "can't read znode_phys at %p\n", zphys_addr);
 		goto bad;
 	}
 
 	/* Get the mount pointer, and read from the address. */
 	mountptr = getvnodemount(vp);
-	if (!KVM_READ(mountptr, &mount, sizeof(mount))) {
-		dprintf(stderr, "can't read mount at %p for pid %d\n",
-		    (void *)mountptr, Pid);
+	if (!kvm_read_all(kd, (unsigned long)mountptr, &mount, sizeof(mount))) {
+		dprintf(stderr, "can't read mount at %p\n", (void *)mountptr);
 		goto bad;
 	}
 


More information about the svn-src-projects mailing list