procfs_map.c revision 1.30 1 /* $NetBSD: procfs_map.c,v 1.30 2007/02/18 20:03:44 ad Exp $ */
2
3 /*
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Jan-Simon Pendry.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. 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 * @(#)procfs_status.c 8.3 (Berkeley) 2/17/94
35 *
36 * $FreeBSD: procfs_map.c,v 1.18 1998/12/04 22:54:51 archie Exp $
37 */
38
39 /*
40 * Copyright (c) 1993 Jan-Simon Pendry
41 *
42 * This code is derived from software contributed to Berkeley by
43 * Jan-Simon Pendry.
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by the University of
56 * California, Berkeley and its contributors.
57 * 4. Neither the name of the University nor the names of its contributors
58 * may be used to endorse or promote products derived from this software
59 * without specific prior written permission.
60 *
61 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
62 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
63 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
64 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
65 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
66 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
67 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
68 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
69 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
70 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
71 * SUCH DAMAGE.
72 *
73 * @(#)procfs_status.c 8.3 (Berkeley) 2/17/94
74 *
75 * $FreeBSD: procfs_map.c,v 1.18 1998/12/04 22:54:51 archie Exp $
76 */
77
78 #include <sys/cdefs.h>
79 __KERNEL_RCSID(0, "$NetBSD: procfs_map.c,v 1.30 2007/02/18 20:03:44 ad Exp $");
80
81 #include <sys/param.h>
82 #include <sys/systm.h>
83 #include <sys/proc.h>
84 #include <sys/vnode.h>
85 #include <sys/malloc.h>
86 #include <sys/namei.h>
87 #include <miscfs/procfs/procfs.h>
88
89 #include <sys/lock.h>
90
91 #include <uvm/uvm.h>
92
93 #define MEBUFFERSIZE 256
94
95 extern int getcwd_common(struct vnode *, struct vnode *,
96 char **, char *, int, int, struct lwp *);
97
98 static int procfs_vnode_to_path(struct vnode *vp, char *path, int len,
99 struct lwp *curl, struct proc *p);
100
101 /*
102 * The map entries can *almost* be read with programs like cat. However,
103 * large maps need special programs to read. It is not easy to implement
104 * a program that can sense the required size of the buffer, and then
105 * subsequently do a read with the appropriate size. This operation cannot
106 * be atomic. The best that we can do is to allow the program to do a read
107 * with an arbitrarily large buffer, and return as much as we can. We can
108 * return an error code if the buffer is too small (EFBIG), then the program
109 * can try a bigger buffer.
110 */
111 int
112 procfs_domap(struct lwp *curl, struct proc *p, struct pfsnode *pfs,
113 struct uio *uio, int linuxmode)
114 {
115 size_t len;
116 int error, retries;
117 struct vmspace *vm;
118 struct vm_map *map;
119 struct vm_map_entry *entry;
120 char mebuffer[MEBUFFERSIZE];
121 char *path;
122 struct vnode *vp;
123 struct vattr va;
124 dev_t dev;
125 long fileid;
126 unsigned timestamp;
127 struct uio savuio;
128
129 if (uio->uio_rw != UIO_READ)
130 return (EOPNOTSUPP);
131
132 if (uio->uio_offset != 0)
133 return (0);
134
135 error = 0;
136 path = NULL;
137
138 if (linuxmode != 0) {
139 path = (char *)malloc(MAXPATHLEN * 4, M_TEMP, M_WAITOK);
140 if (path == NULL)
141 return ENOMEM;
142 }
143
144 if ((error = proc_vmspace_getref(p, &vm)) != 0) {
145 if (path != NULL)
146 free(path, M_TEMP);
147 return (error);
148 }
149
150 map = &vm->vm_map;
151 memcpy(&savuio, uio, sizeof(savuio));
152 retries = 0;
153 vm_map_lock_read(map);
154
155 restart:
156 for (entry = map->header.next;
157 ((uio->uio_resid > 0) && (entry != &map->header));
158 entry = entry->next) {
159
160 if (retries > 250) {
161 error = EWOULDBLOCK;
162 break;
163 }
164
165 if (UVM_ET_ISSUBMAP(entry))
166 continue;
167
168 if (linuxmode != 0) {
169 *path = 0;
170 dev = (dev_t)0;
171 fileid = 0;
172 if (UVM_ET_ISOBJ(entry) &&
173 UVM_OBJ_IS_VNODE(entry->object.uvm_obj)) {
174 vp = (struct vnode *)entry->object.uvm_obj;
175 error = VOP_GETATTR(vp, &va, curl->l_cred,
176 curl);
177 if (error == 0 && vp != pfs->pfs_vnode) {
178 fileid = va.va_fileid;
179 dev = va.va_fsid;
180 error = procfs_vnode_to_path(vp, path,
181 MAXPATHLEN * 4, curl, p);
182 }
183 }
184 snprintf(mebuffer, sizeof(mebuffer),
185 "%0*lx-%0*lx %c%c%c%c %0*lx %02x:%02x %ld %s\n",
186 (int)sizeof(void *) * 2,(unsigned long)entry->start,
187 (int)sizeof(void *) * 2,(unsigned long)entry->end,
188 (entry->protection & VM_PROT_READ) ? 'r' : '-',
189 (entry->protection & VM_PROT_WRITE) ? 'w' : '-',
190 (entry->protection & VM_PROT_EXECUTE) ? 'x' : '-',
191 (entry->etype & UVM_ET_COPYONWRITE) ? 'p' : 's',
192 (int)sizeof(void *) * 2,
193 (unsigned long)entry->offset,
194 major(dev), minor(dev), fileid, path);
195 } else {
196 snprintf(mebuffer, sizeof(mebuffer),
197 "0x%lx 0x%lx %c%c%c %c%c%c %s %s %d %d %d\n",
198 entry->start, entry->end,
199 (entry->protection & VM_PROT_READ) ? 'r' : '-',
200 (entry->protection & VM_PROT_WRITE) ? 'w' : '-',
201 (entry->protection & VM_PROT_EXECUTE) ? 'x' : '-',
202 (entry->max_protection & VM_PROT_READ) ? 'r' : '-',
203 (entry->max_protection & VM_PROT_WRITE) ? 'w' : '-',
204 (entry->max_protection & VM_PROT_EXECUTE) ?
205 'x' : '-',
206 (entry->etype & UVM_ET_COPYONWRITE) ?
207 "COW" : "NCOW",
208 (entry->etype & UVM_ET_NEEDSCOPY) ? "NC" : "NNC",
209 entry->inheritance, entry->wired_count,
210 entry->advice);
211 }
212
213 len = strlen(mebuffer);
214 if (len > uio->uio_resid) {
215 error = EFBIG;
216 break;
217 }
218
219 timestamp = map->timestamp;
220 vm_map_unlock_read(map);
221 error = uiomove(mebuffer, len, uio);
222 vm_map_lock_read(map);
223 if (error)
224 break;
225
226 if (timestamp != map->timestamp) {
227 /*
228 * The map may have changed, so restart. We
229 * make an ugly assumption about uiomove()
230 * and the vm_map timestamp: it will never
231 * fall back to copyout_vmspace() because
232 * we are copying out to curproc.
233 */
234 KASSERT(uio->uio_vmspace == curproc->p_vmspace);
235 retries++;
236 memcpy(uio, &savuio, sizeof(*uio));
237 goto restart;
238 }
239 }
240
241 vm_map_unlock_read(map);
242 uvmspace_free(vm);
243 if (path != NULL)
244 free(path, M_TEMP);
245
246 return error;
247 }
248
249 int
250 procfs_validmap(struct lwp *l, struct mount *mp)
251 {
252 return ((l->l_flag & LW_SYSTEM) == 0);
253 }
254
255 /*
256 * Try to find a pathname for a vnode. Since there is no mapping
257 * vnode -> parent directory, this needs the NAMECACHE_ENTER_REVERSE
258 * option to work (to make cache_revlookup succeed).
259 */
260 static int procfs_vnode_to_path(struct vnode *vp, char *path, int len,
261 struct lwp *curl, struct proc *p)
262 {
263 struct proc *curp = curl->l_proc;
264 int error, lenused, elen;
265 char *bp, *bend;
266 struct vnode *dvp;
267
268 bp = bend = &path[len];
269 *(--bp) = '\0';
270
271 error = vget(vp, LK_EXCLUSIVE | LK_RETRY);
272 if (error != 0)
273 return error;
274 error = cache_revlookup(vp, &dvp, &bp, path);
275 vput(vp);
276 if (error != 0)
277 return (error == -1 ? ENOENT : error);
278
279 error = vget(dvp, 0);
280 if (error != 0)
281 return error;
282 *(--bp) = '/';
283 /* XXX GETCWD_CHECK_ACCESS == 0x0001 */
284 error = getcwd_common(dvp, NULL, &bp, path, len / 2, 1, curl);
285
286 /*
287 * Strip off emulation path for emulated processes looking at
288 * the maps file of a process of the same emulation. (Won't
289 * work if /emul/xxx is a symlink..)
290 */
291 if (curp->p_emul == p->p_emul && curp->p_emul->e_path != NULL) {
292 elen = strlen(curp->p_emul->e_path);
293 if (!strncmp(bp, curp->p_emul->e_path, elen))
294 bp = &bp[elen];
295 }
296
297 lenused = bend - bp;
298
299 memcpy(path, bp, lenused);
300 path[lenused] = 0;
301
302 return 0;
303 }
304