Merge branch 'vendor/LESS'
[dragonfly.git] / sys / kern / imgact_resident.c
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  * $DragonFly: src/sys/kern/imgact_resident.c,v 1.17 2007/04/30 07:18:53 dillon Exp $
37  */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/sysproto.h>
43 #include <sys/exec.h>
44 #include <sys/imgact.h>
45 #include <sys/imgact_aout.h>
46 #include <sys/mman.h>
47 #include <sys/proc.h>
48 #include <sys/priv.h>
49 #include <sys/resourcevar.h>
50 #include <sys/sysent.h>
51 #include <sys/systm.h>
52 #include <sys/stat.h>
53 #include <sys/vnode.h>
54 #include <sys/inflate.h>
55 #include <sys/sysctl.h>
56 #include <sys/lock.h>
57 #include <sys/resident.h>
58
59 #include <vm/vm.h>
60 #include <vm/vm_param.h>
61 #include <vm/pmap.h>
62 #include <vm/vm_map.h>
63 #include <vm/vm_kern.h>
64 #include <vm/vm_extern.h>
65
66 #include <sys/sysref2.h>
67
68 static int exec_res_id = 0;
69
70 static TAILQ_HEAD(,vmresident) exec_res_list;
71
72 static MALLOC_DEFINE(M_EXEC_RES, "vmresident", "resident execs");
73
74 /* lockmgr lock for protecting the exec_res_list */
75 static struct lock exec_list_lock;
76
77 static void
78 vm_resident_init(void *__dummy)
79 {
80         lockinit(&exec_list_lock, "vmres", 0, 0);
81         TAILQ_INIT(&exec_res_list);
82 }
83 SYSINIT(vmres, SI_BOOT1_LOCK, SI_ORDER_ANY, vm_resident_init, 0);
84
85 static int
86 fill_xresident(struct vmresident *vr, struct xresident *in, struct thread *td)
87 {
88         struct stat st;
89         struct vnode *vrtmp;
90         int error = 0;
91
92         vrtmp = vr->vr_vnode;
93
94         in->res_entry_addr = vr->vr_entry_addr;
95         in->res_id = vr->vr_id;
96         if (vrtmp) {
97                 char *freepath, *fullpath;
98                 error = vn_fullpath(td->td_proc, vrtmp, &fullpath, &freepath, 0);
99                 if (error != 0) {
100                         /* could not retrieve cached path, return zero'ed string */
101                         bzero(in->res_file, MAXPATHLEN);
102                         error = 0;
103                 } else {
104                         strlcpy(in->res_file, fullpath, sizeof(in->res_file));
105                         kfree(freepath, M_TEMP);
106                 }
107
108                 /* indicate that we are using the vnode */
109                 error = vget(vrtmp, LK_EXCLUSIVE);
110                 if (error)
111                         goto done;
112         
113                 /* retrieve underlying stat information and release vnode */
114                 error = vn_stat(vrtmp, &st, td->td_ucred);
115                 vput(vrtmp);
116                 if (error)
117                         goto done;
118
119                 in->res_stat = st;
120         }
121
122 done:
123         if (error)
124                 kprintf("fill_xresident, error = %d\n", error);
125         return (error);
126 }
127
128 static int
129 sysctl_vm_resident(SYSCTL_HANDLER_ARGS)
130 {
131         struct vmresident *vmres;
132         struct thread *td;
133         int error;
134         int count;
135
136         /* only super-user should call this sysctl */
137         td = req->td;
138         if ((priv_check(td, PRIV_VM_RESIDENT)) != 0)
139                 return EPERM;
140
141         error = count = 0;
142
143         if (exec_res_id == 0)
144             return error;
145         
146         /* client queried for number of resident binaries */
147         if (!req->oldptr)
148             return SYSCTL_OUT(req, 0, exec_res_id);
149
150         lockmgr(&exec_list_lock, LK_SHARED);
151
152         TAILQ_FOREACH(vmres, &exec_res_list, vr_link) {
153                 struct xresident xres;
154                 error = fill_xresident(vmres, &xres, td);
155                 if (error != 0)
156                         break;
157                 
158                 error = SYSCTL_OUT(req, (void *)&xres,
159                                 sizeof(struct xresident));
160                 if (error != 0)
161                         break;
162         }
163         lockmgr(&exec_list_lock, LK_RELEASE);
164
165         return (error);
166 }
167 SYSCTL_PROC(_vm, OID_AUTO, resident, CTLTYPE_OPAQUE|CTLFLAG_RD, 0, 0,
168   sysctl_vm_resident, "S,xresident", "resident executables (sys/resident.h)");
169
170 int
171 exec_resident_imgact(struct image_params *imgp)
172 {
173         struct vmresident *vmres;
174
175         /*
176          * resident image activator
177          */
178         lockmgr(&exec_list_lock, LK_SHARED);
179         if ((vmres = imgp->vp->v_resident) == NULL) {
180             lockmgr(&exec_list_lock, LK_RELEASE);
181             return(-1);
182         }
183         atomic_add_int(&vmres->vr_refs, 1);
184         lockmgr(&exec_list_lock, LK_RELEASE);
185
186         /*
187          * We want to exec the new vmspace without holding the lock to
188          * improve concurrency.
189          */
190         exec_new_vmspace(imgp, vmres->vr_vmspace);
191         imgp->resident = 1;
192         imgp->interpreted = 0;
193         imgp->proc->p_sysent = vmres->vr_sysent;
194         imgp->entry_addr = vmres->vr_entry_addr;
195         atomic_subtract_int(&vmres->vr_refs, 1);
196
197         return(0);
198 }
199
200 /*
201  * exec_sys_register(entry)
202  *
203  * Register ourselves for resident execution.  Only root (i.e. a process with
204  * PRIV_VM_RESIDENT credentials) can do this.  This
205  * will snapshot the vmspace and cause future exec's of the specified binary
206  * to use the snapshot directly rather then load & relocate a new copy.
207  *
208  * MPALMOSTSAFE
209  */
210 int
211 sys_exec_sys_register(struct exec_sys_register_args *uap)
212 {
213     struct thread *td = curthread;
214     struct vmresident *vmres;
215     struct vnode *vp;
216     struct proc *p;
217     int error;
218
219     p = td->td_proc;
220     error = priv_check_cred(td->td_ucred, PRIV_VM_RESIDENT, 0);
221     if (error)
222         return(error);
223
224     if ((vp = p->p_textvp) == NULL)
225         return(ENOENT);
226
227     lockmgr(&exec_list_lock, LK_EXCLUSIVE);
228
229     if (vp->v_resident) {
230         lockmgr(&exec_list_lock, LK_RELEASE);
231         return(EEXIST);
232     }
233
234     vhold(vp);
235     vmres = kmalloc(sizeof(*vmres), M_EXEC_RES, M_WAITOK | M_ZERO);
236     vmres->vr_vnode = vp;
237     vmres->vr_sysent = p->p_sysent;
238     vmres->vr_id = ++exec_res_id;
239     vmres->vr_entry_addr = (intptr_t)uap->entry;
240     vmres->vr_vmspace = vmspace_fork(p->p_vmspace); /* XXX order */
241     pmap_pinit2(vmspace_pmap(vmres->vr_vmspace));
242     vp->v_resident = vmres;
243
244     TAILQ_INSERT_TAIL(&exec_res_list, vmres, vr_link);
245     lockmgr(&exec_list_lock, LK_RELEASE);
246
247     return(0);
248 }
249
250 /*
251  * exec_sys_unregister(id)
252  *
253  *      Unregister the specified id.  If an id of -1 is used unregister
254  *      the registration associated with the current process.  An id of -2
255  *      unregisters everything.
256  *
257  * MPALMOSTSAFE
258  */
259 int
260 sys_exec_sys_unregister(struct exec_sys_unregister_args *uap)
261 {
262     struct thread *td = curthread;
263     struct vmresident *vmres;
264     struct proc *p;
265     int error;
266     int id;
267     int count;
268
269     p = td->td_proc;
270     error = priv_check_cred(td->td_ucred, PRIV_VM_RESIDENT, 0);
271     if (error)
272         return(error);
273
274     /*
275      * If id is -1, unregister ourselves
276      */
277     lockmgr(&exec_list_lock, LK_EXCLUSIVE);
278
279     if ((id = uap->id) == -1 && p->p_textvp && p->p_textvp->v_resident)
280         id = p->p_textvp->v_resident->vr_id;
281
282     /*
283      * Look for the registration
284      */
285     error = ENOENT;
286     count = 0;
287
288 restart:
289     TAILQ_FOREACH(vmres, &exec_res_list, vr_link) {
290         if (id == -2 || vmres->vr_id == id) {
291             /*
292              * Check race against exec
293              */
294             if (vmres->vr_refs) {
295                 lockmgr(&exec_list_lock, LK_RELEASE);
296                 tsleep(vmres, 0, "vmres", 1);
297                 lockmgr(&exec_list_lock, LK_EXCLUSIVE);
298                 goto restart;
299             }
300
301             /*
302              * Remove it
303              */
304             TAILQ_REMOVE(&exec_res_list, vmres, vr_link);
305             if (vmres->vr_vnode) {
306                 vmres->vr_vnode->v_resident = NULL;
307                 vdrop(vmres->vr_vnode);
308                 vmres->vr_vnode = NULL;
309             }
310             if (vmres->vr_vmspace) {
311                 sysref_put(&vmres->vr_vmspace->vm_sysref);
312                 vmres->vr_vmspace = NULL;
313             }
314             kfree(vmres, M_EXEC_RES);
315             exec_res_id--;
316             error = 0;
317             ++count;
318             goto restart;
319         }
320     }
321     lockmgr(&exec_list_lock, LK_RELEASE);
322
323     if (error == 0)
324         uap->sysmsg_result = count;
325     return(error);
326 }
327