xref: /freebsd/lib/libprocstat/common_kvm.c (revision 076ad2f8)
1 /*-
2  * Copyright (c) 2009 Stanislav Sedov <stas@FreeBSD.org>
3  * Copyright (c) 1988, 1993
4  *	The Regents of the University of California.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. All advertising materials mentioning features or use of this software
15  *    must display the following acknowledgement:
16  *	This product includes software developed by the University of
17  *	California, Berkeley and its contributors.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include <sys/param.h>
39 #include <sys/user.h>
40 #include <sys/stat.h>
41 #include <sys/vnode.h>
42 #include <sys/conf.h>
43 #define	_KERNEL
44 #include <sys/pipe.h>
45 #include <sys/mount.h>
46 #include <ufs/ufs/quota.h>
47 #include <ufs/ufs/inode.h>
48 #include <ufs/ufs/extattr.h>
49 #include <ufs/ufs/ufsmount.h>
50 #include <fs/devfs/devfs.h>
51 #include <fs/devfs/devfs_int.h>
52 #undef _KERNEL
53 #include <nfs/nfsproto.h>
54 #include <nfsclient/nfs.h>
55 #include <nfsclient/nfsnode.h>
56 
57 #include <assert.h>
58 #include <err.h>
59 #include <kvm.h>
60 #include <stddef.h>
61 #include <string.h>
62 
63 #include <libprocstat.h>
64 #include "common_kvm.h"
65 
66 int
67 kvm_read_all(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes)
68 {
69 	ssize_t error;
70 
71 	if (nbytes >= SSIZE_MAX)
72 		return (0);
73 	error = kvm_read(kd, addr, buf, nbytes);
74 	return (error == (ssize_t)(nbytes));
75 }
76 
77 int
78 kdevtoname(kvm_t *kd, struct cdev *dev, char *buf)
79 {
80 	struct cdev si;
81 
82 	assert(buf);
83 	if (!kvm_read_all(kd, (unsigned long)dev, &si, sizeof(si)))
84 		return (1);
85 	strlcpy(buf, si.si_name, SPECNAMELEN + 1);
86 	return (0);
87 }
88 
89 int
90 ufs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
91 {
92 	struct inode inode;
93 	struct ufsmount um;
94 
95 	if (!kvm_read_all(kd, (unsigned long)VTOI(vp), &inode, sizeof(inode))) {
96 		warnx("can't read inode at %p", (void *)VTOI(vp));
97 		return (1);
98 	}
99 	if (!kvm_read_all(kd, (unsigned long)inode.i_ump, &um, sizeof(um))) {
100 		warnx("can't read ufsmount at %p", (void *)inode.i_ump);
101 		return (1);
102 	}
103 	/*
104 	 * The st_dev from stat(2) is a dev_t. These kernel structures
105 	 * contain cdev pointers. We need to convert to dev_t to make
106 	 * comparisons
107 	 */
108 	vn->vn_fsid = dev2udev(kd, um.um_dev);
109 	vn->vn_fileid = inode.i_number;
110 	vn->vn_mode = (mode_t)inode.i_mode;
111 	vn->vn_size = inode.i_size;
112 	return (0);
113 }
114 
115 int
116 devfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
117 {
118 	struct devfs_dirent devfs_dirent;
119 	struct mount mount;
120 
121 	if (!kvm_read_all(kd, (unsigned long)getvnodedata(vp), &devfs_dirent,
122 	    sizeof(devfs_dirent))) {
123 		warnx("can't read devfs_dirent at %p",
124 		    (void *)vp->v_data);
125 		return (1);
126 	}
127 	if (!kvm_read_all(kd, (unsigned long)getvnodemount(vp), &mount,
128 	    sizeof(mount))) {
129 		warnx("can't read mount at %p",
130 		    (void *)getvnodemount(vp));
131 		return (1);
132 	}
133 	vn->vn_fsid = mount.mnt_stat.f_fsid.val[0];
134 	vn->vn_fileid = devfs_dirent.de_inode;
135 	vn->vn_mode = (devfs_dirent.de_mode & ~S_IFMT) | S_IFCHR;
136 	vn->vn_size = 0;
137 	return (0);
138 }
139 
140 int
141 nfs_filestat(kvm_t *kd, struct vnode *vp, struct vnstat *vn)
142 {
143 	struct nfsnode nfsnode;
144 	mode_t mode;
145 
146 	if (!kvm_read_all(kd, (unsigned long)VTONFS(vp), &nfsnode,
147 	    sizeof(nfsnode))) {
148 		warnx("can't read nfsnode at %p",
149 		    (void *)VTONFS(vp));
150 		return (1);
151 	}
152 	vn->vn_fsid = nfsnode.n_vattr.va_fsid;
153 	vn->vn_fileid = nfsnode.n_vattr.va_fileid;
154 	vn->vn_size = nfsnode.n_size;
155 	mode = (mode_t)nfsnode.n_vattr.va_mode;
156 	switch (vp->v_type) {
157 	case VREG:
158 		mode |= S_IFREG;
159 		break;
160 	case VDIR:
161 		mode |= S_IFDIR;
162 		break;
163 	case VBLK:
164 		mode |= S_IFBLK;
165 		break;
166 	case VCHR:
167 		mode |= S_IFCHR;
168 		break;
169 	case VLNK:
170 		mode |= S_IFLNK;
171 		break;
172 	case VSOCK:
173 		mode |= S_IFSOCK;
174 		break;
175 	case VFIFO:
176 		mode |= S_IFIFO;
177 		break;
178 	default:
179 		break;
180 	};
181 	vn->vn_mode = mode;
182 	return (0);
183 }
184 
185 /*
186  * Read the cdev structure in the kernel in order to work out the
187  * associated dev_t
188  */
189 dev_t
190 dev2udev(kvm_t *kd, struct cdev *dev)
191 {
192 	struct cdev_priv priv;
193 
194 	assert(kd);
195 	if (kvm_read_all(kd, (unsigned long)cdev2priv(dev), &priv,
196 	    sizeof(priv))) {
197 		return ((dev_t)priv.cdp_inode);
198 	} else {
199 		warnx("can't convert cdev *%p to a dev_t\n", dev);
200 		return (-1);
201 	}
202 }
203 
204 void *
205 getvnodedata(struct vnode *vp)
206 {
207 	return (vp->v_data);
208 }
209 
210 struct mount *
211 getvnodemount(struct vnode *vp)
212 {
213 	return (vp->v_mount);
214 }
215