VM Resident Executables update:
[dragonfly.git] / sys / kern / imgact_resident.c
1 /*
2  * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $DragonFly: src/sys/kern/imgact_resident.c,v 1.3 2004/06/03 16:28:15 hmp Exp $
27  */
28
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/sysproto.h>
33 #include <sys/exec.h>
34 #include <sys/imgact.h>
35 #include <sys/imgact_aout.h>
36 #include <sys/mman.h>
37 #include <sys/proc.h>
38 #include <sys/resourcevar.h>
39 #include <sys/sysent.h>
40 #include <sys/systm.h>
41 #include <sys/stat.h>
42 #include <sys/vnode.h>
43 #include <sys/inflate.h>
44 #include <sys/sysctl.h>
45 #include <sys/lock.h>
46 #include <sys/resident.h>
47
48 #include <vm/vm.h>
49 #include <vm/vm_param.h>
50 #include <vm/pmap.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_kern.h>
53 #include <vm/vm_extern.h>
54
55 static int exec_res_id = 0;
56 static TAILQ_HEAD(,vmresident) exec_res_list = TAILQ_HEAD_INITIALIZER(exec_res_list);
57
58 static MALLOC_DEFINE(M_EXEC_RES, "vmresident", "resident execs");
59
60 static int
61 fill_xresident(struct vmresident *vr, struct xresident *in, struct thread *td)
62 {
63         struct stat st;
64         struct vnode *vrtmp;
65         int error = 0;
66
67         vrtmp = vr->vr_vnode;
68
69         in->res_entry_addr = vr->vr_entry_addr;
70         in->res_id = vr->vr_id;
71         if (vrtmp) {
72                 char *freepath, *fullpath;
73                 error = vn_fullpath(td->td_proc, vrtmp, &fullpath, &freepath);
74                 if (error != 0) {
75                         /* could not retrieve cached path, return zero'ed string */
76                         bzero(in->res_file, MAXPATHLEN);
77                         error = 0;
78                 } else {
79                         bcopy(fullpath, in->res_file, MAXPATHLEN);
80                         free(freepath, M_TEMP);
81                 }
82
83                 /* indicate that we are using the vnode */
84                 error = vget(vrtmp, NULL, LK_EXCLUSIVE, td);
85                 if (error)
86                         goto done;
87         
88                 /* retrieve underlying stat information and release vnode */
89                 error = vn_stat(vrtmp, &st, td);
90                 vput(vrtmp);
91                 if (error)
92                         goto done;
93
94                 in->res_stat = st;
95         }
96
97 done:
98         if (error)
99                 printf("fill_xresident, error = %d\n", error);
100         return (error);
101 }
102
103 static int
104 sysctl_vm_resident(SYSCTL_HANDLER_ARGS)
105 {
106         struct vmresident *vmres;
107         struct thread *td;
108         int error;
109         int count;
110
111         /* only super-user should call this sysctl */
112         td = req->td;
113         if ((suser(td)) != 0)
114                 return EPERM;
115
116         error = count = 0;
117
118         if (exec_res_id == 0)
119             return error;
120         
121         /* client queried for number of resident binaries */
122         if (!req->oldptr)
123             return SYSCTL_OUT(req, 0, exec_res_id);
124
125         TAILQ_FOREACH(vmres, &exec_res_list, vr_link) {
126                 struct xresident xres;
127                 error = fill_xresident(vmres, &xres, td);
128                 if (error != 0)
129                         break;
130                 
131                 error = SYSCTL_OUT(req, (void *)&xres,
132                                 sizeof(struct xresident));
133                 if (error != 0)
134                         break;
135         }
136
137         return (error);
138 }
139 SYSCTL_PROC(_vm, OID_AUTO, resident, CTLTYPE_OPAQUE|CTLFLAG_RD, 0, 0,
140   sysctl_vm_resident, "S,xresident", "resident executables (sys/resident.h)");
141
142 int
143 exec_resident_imgact(struct image_params *imgp)
144 {
145         struct vmresident *vmres;
146
147         /*
148          * resident image activator
149          */
150         if ((vmres = imgp->vp->v_resident) == NULL)
151             return(-1);
152         exec_new_vmspace(imgp, vmres->vr_vmspace);
153         imgp->resident = 1;
154         imgp->interpreted = 0;
155         imgp->proc->p_sysent = vmres->vr_sysent;
156         imgp->entry_addr = vmres->vr_entry_addr;
157         return(0);
158 }
159
160 /*
161  * exec_sys_register(entry)
162  *
163  * Register ourselves for resident execution.  Only root can do this.  This
164  * will snapshot the vmspace and cause future exec's of the specified binary
165  * to use the snapshot directly rather then load & relocate a new copy.
166  */
167 int
168 exec_sys_register(struct exec_sys_register_args *uap)
169 {
170     struct vmresident *vmres;
171     struct vnode *vp;
172     struct proc *p;
173     int error;
174
175     p = curproc;
176     if ((error = suser(p->p_thread)) != 0)
177         return(error);
178     if ((vp = p->p_textvp) == NULL)
179         return(ENOENT);
180     if (vp->v_resident)
181         return(EEXIST);
182     vhold(vp);
183     vmres = malloc(sizeof(*vmres), M_EXEC_RES, M_WAITOK);
184     vp->v_resident = vmres;
185     vmres->vr_vnode = vp;
186     vmres->vr_sysent = p->p_sysent;
187     vmres->vr_id = ++exec_res_id;
188     vmres->vr_entry_addr = (intptr_t)uap->entry;
189     vmres->vr_vmspace = vmspace_fork(p->p_vmspace); /* XXX order */
190     TAILQ_INSERT_TAIL(&exec_res_list, vmres, vr_link);
191     return(0);
192 }
193
194 /*
195  * exec_sys_unregister(id)
196  *
197  *      Unregister the specified id.  If an id of -1 is used unregister
198  *      the registration associated with the current process.  An id of -2
199  *      unregisters everything.
200  */
201 int
202 exec_sys_unregister(struct exec_sys_unregister_args *uap)
203 {
204     struct vmresident *vmres;
205     struct proc *p;
206     int error;
207     int id;
208     int count;
209
210     p = curproc;
211     if ((error = suser(p->p_thread)) != 0)
212         return(error);
213
214     /*
215      * If id is -1, unregister ourselves
216      */
217     if ((id = uap->id) == -1 && p->p_textvp && p->p_textvp->v_resident)
218         id = p->p_textvp->v_resident->vr_id;
219
220     /*
221      * Look for the registration
222      */
223     error = ENOENT;
224     count = 0;
225 restart:
226     TAILQ_FOREACH(vmres, &exec_res_list, vr_link) {
227         if (id == -2 || vmres->vr_id == id) {
228             TAILQ_REMOVE(&exec_res_list, vmres, vr_link);
229             if (vmres->vr_vnode) {
230                 vmres->vr_vnode->v_resident = NULL;
231                 vdrop(vmres->vr_vnode);
232                 vmres->vr_vnode = NULL;
233             }
234             if (vmres->vr_vmspace) {
235                 vmspace_free(vmres->vr_vmspace);
236                 vmres->vr_vmspace = NULL;
237             }
238             free(vmres, M_EXEC_RES);
239             exec_res_id--;
240             error = 0;
241             ++count;
242             goto restart;
243         }
244     }
245     if (error == 0)
246         uap->sysmsg_result = count;
247     return(error);
248 }
249