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