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