1 /*
2 * (MPSAFE)
3 *
4 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
5 *
6 * This code is derived from software contributed to The DragonFly Project
7 * by Matthew Dillon <dillon@backplane.com>
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
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
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * 3. Neither the name of The DragonFly Project nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific, prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
29 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
31 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/sysproto.h>
41 #include <sys/exec.h>
42 #include <sys/imgact.h>
43 #include <sys/imgact_aout.h>
44 #include <sys/mman.h>
45 #include <sys/proc.h>
46 #include <sys/priv.h>
47 #include <sys/resourcevar.h>
48 #include <sys/sysent.h>
49 #include <sys/stat.h>
50 #include <sys/vnode.h>
51 #include <sys/inflate.h>
52 #include <sys/sysctl.h>
53 #include <sys/lock.h>
54 #include <sys/resident.h>
55
56 #include <vm/vm.h>
57 #include <vm/vm_param.h>
58 #include <vm/pmap.h>
59 #include <vm/vm_map.h>
60 #include <vm/vm_kern.h>
61 #include <vm/vm_extern.h>
62
63 #include <sys/sysref2.h>
64
65 static int exec_res_id = 0;
66
67 static TAILQ_HEAD(,vmresident) exec_res_list;
68
69 static MALLOC_DEFINE(M_EXEC_RES, "vmresident", "resident execs");
70
71 /* lockmgr lock for protecting the exec_res_list */
72 static struct lock exec_list_lock;
73
74 static void
75 vm_resident_init(void *__dummy)
76 {
77 lockinit(&exec_list_lock, "vmres", 0, 0);
78 TAILQ_INIT(&exec_res_list);
79 }
80 SYSINIT(vmres, SI_BOOT1_LOCK, SI_ORDER_ANY, vm_resident_init, 0);
81
82 static int
83 fill_xresident(struct vmresident *vr, struct xresident *in, struct thread *td)
84 {
85 struct stat st;
86 struct vnode *vrtmp;
87 int error = 0;
88
89 vrtmp = vr->vr_vnode;
90
91 in->res_entry_addr = vr->vr_entry_addr;
92 in->res_id = vr->vr_id;
93 if (vrtmp) {
94 char *freepath, *fullpath;
95 error = vn_fullpath(td->td_proc, vrtmp, &fullpath, &freepath, 0);
96 if (error != 0) {
97 /* could not retrieve cached path, return zero'ed string */
98 bzero(in->res_file, MAXPATHLEN);
99 error = 0;
100 } else {
101 strlcpy(in->res_file, fullpath, sizeof(in->res_file));
102 kfree(freepath, M_TEMP);
103 }
104
105 /* indicate that we are using the vnode */
106 error = vget(vrtmp, LK_EXCLUSIVE);
107 if (error)
108 goto done;
109
110 /* retrieve underlying stat information and release vnode */
111 error = vn_stat(vrtmp, &st, td->td_ucred);
112 vput(vrtmp);
113 if (error)
114 goto done;
115
116 in->res_stat = st;
117 }
118
119 done:
120 if (error)
121 kprintf("fill_xresident, error = %d\n", error);
122 return (error);
123 }
124
125 static int
126 sysctl_vm_resident(SYSCTL_HANDLER_ARGS)
127 {
128 struct vmresident *vmres;
129 struct thread *td;
130 int error;
131 int count;
132
133 /* only super-user should call this sysctl */
134 td = req->td;
135 if ((priv_check(td, PRIV_VM_RESIDENT)) != 0)
136 return EPERM;
137
138 error = count = 0;
139
140 if (exec_res_id == 0)
141 return error;
142
143 /* client queried for number of resident binaries */
144 if (!req->oldptr)
145 return SYSCTL_OUT(req, 0, exec_res_id);
146
147 lockmgr(&exec_list_lock, LK_SHARED);
148
149 TAILQ_FOREACH(vmres, &exec_res_list, vr_link) {
150 struct xresident xres;
151 error = fill_xresident(vmres, &xres, td);
152 if (error != 0)
153 break;
154
155 error = SYSCTL_OUT(req, (void *)&xres,
156 sizeof(struct xresident));
157 if (error != 0)
158 break;
159 }
160 lockmgr(&exec_list_lock, LK_RELEASE);
161
162 return (error);
163 }
164 SYSCTL_PROC(_vm, OID_AUTO, resident, CTLTYPE_OPAQUE|CTLFLAG_RD, 0, 0,
165 sysctl_vm_resident, "S,xresident", "resident executables (sys/resident.h)");
166
167 int
168 exec_resident_imgact(struct image_params *imgp)
169 {
170 struct vmresident *vmres;
171
172 /*
173 * resident image activator
174 */
175 lockmgr(&exec_list_lock, LK_SHARED);
176 if ((vmres = imgp->vp->v_resident) == NULL) {
177 lockmgr(&exec_list_lock, LK_RELEASE);
178 return(-1);
179 }
180 atomic_add_int(&vmres->vr_refs, 1);
181 lockmgr(&exec_list_lock, LK_RELEASE);
182
183 /*
184 * We want to exec the new vmspace without holding the lock to
185 * improve concurrency.
186 */
187 exec_new_vmspace(imgp, vmres->vr_vmspace);
188 imgp->resident = 1;
189 imgp->interpreted = 0;
190 imgp->proc->p_sysent = vmres->vr_sysent;
191 imgp->entry_addr = vmres->vr_entry_addr;
192 atomic_subtract_int(&vmres->vr_refs, 1);
193
194 return(0);
195 }
196
197 /*
198 * exec_sys_register(entry)
199 *
200 * Register ourselves for resident execution. Only root (i.e. a process with
201 * PRIV_VM_RESIDENT credentials) can do this. This
202 * will snapshot the vmspace and cause future exec's of the specified binary
203 * to use the snapshot directly rather then load & relocate a new copy.
204 *
205 * MPALMOSTSAFE
206 */
207 int
208 sys_exec_sys_register(struct exec_sys_register_args *uap)
209 {
210 struct thread *td = curthread;
211 struct vmresident *vmres;
212 struct vnode *vp;
213 struct proc *p;
214 int error;
215
216 p = td->td_proc;
217 error = priv_check_cred(td->td_ucred, PRIV_VM_RESIDENT, 0);
218 if (error)
219 return(error);
220
221 if ((vp = p->p_textvp) == NULL)
222 return(ENOENT);
223
224 lockmgr(&exec_list_lock, LK_EXCLUSIVE);
225
226 if (vp->v_resident) {
227 lockmgr(&exec_list_lock, LK_RELEASE);
228 return(EEXIST);
229 }
230
231 vhold(vp);
232 vmres = kmalloc(sizeof(*vmres), M_EXEC_RES, M_WAITOK | M_ZERO);
233 vmres->vr_vnode = vp;
234 vmres->vr_sysent = p->p_sysent;
235 vmres->vr_id = ++exec_res_id;
236 vmres->vr_entry_addr = (intptr_t)uap->entry;
237 vmres->vr_vmspace = vmspace_fork(p->p_vmspace); /* XXX order */
238 pmap_pinit2(vmspace_pmap(vmres->vr_vmspace));
239 vp->v_resident = vmres;
240
241 TAILQ_INSERT_TAIL(&exec_res_list, vmres, vr_link);
242 lockmgr(&exec_list_lock, LK_RELEASE);
243
244 return(0);
245 }
246
247 /*
248 * exec_sys_unregister(id)
249 *
250 * Unregister the specified id. If an id of -1 is used unregister
251 * the registration associated with the current process. An id of -2
252 * unregisters everything.
253 *
254 * MPALMOSTSAFE
255 */
256 int
257 sys_exec_sys_unregister(struct exec_sys_unregister_args *uap)
258 {
259 struct thread *td = curthread;
260 struct vmresident *vmres;
261 struct proc *p;
262 int error;
263 int id;
264 int count;
265
266 p = td->td_proc;
267 error = priv_check_cred(td->td_ucred, PRIV_VM_RESIDENT, 0);
268 if (error)
269 return(error);
270
271 /*
272 * If id is -1, unregister ourselves
273 */
274 lockmgr(&exec_list_lock, LK_EXCLUSIVE);
275
276 if ((id = uap->id) == -1 && p->p_textvp && p->p_textvp->v_resident)
277 id = p->p_textvp->v_resident->vr_id;
278
279 /*
280 * Look for the registration
281 */
282 error = ENOENT;
283 count = 0;
284
285 restart:
286 TAILQ_FOREACH(vmres, &exec_res_list, vr_link) {
287 if (id == -2 || vmres->vr_id == id) {
288 /*
289 * Check race against exec
290 */
291 if (vmres->vr_refs) {
292 lockmgr(&exec_list_lock, LK_RELEASE);
293 tsleep(vmres, 0, "vmres", 1);
294 lockmgr(&exec_list_lock, LK_EXCLUSIVE);
295 goto restart;
296 }
297
298 /*
299 * Remove it
300 */
301 TAILQ_REMOVE(&exec_res_list, vmres, vr_link);
302 if (vmres->vr_vnode) {
303 vmres->vr_vnode->v_resident = NULL;
304 vdrop(vmres->vr_vnode);
305 vmres->vr_vnode = NULL;
306 }
307 if (vmres->vr_vmspace) {
308 vmspace_free(vmres->vr_vmspace);
309 vmres->vr_vmspace = NULL;
310 }
311 kfree(vmres, M_EXEC_RES);
312 exec_res_id--;
313 error = 0;
314 ++count;
315 goto restart;
316 }
317 }
318 lockmgr(&exec_list_lock, LK_RELEASE);
319
320 if (error == 0)
321 uap->sysmsg_result = count;
322 return(error);
323 }
324
Cache object: 2510fda3a1a7a9ec0764fa49a9328345
|