2 * Copyright (c) 2003 Matthew Dillon <dillon@backplane.com>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $DragonFly: src/sys/kern/imgact_resident.c,v 1.3 2004/06/03 16:28:15 hmp Exp $
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/sysproto.h>
34 #include <sys/imgact.h>
35 #include <sys/imgact_aout.h>
38 #include <sys/resourcevar.h>
39 #include <sys/sysent.h>
40 #include <sys/systm.h>
42 #include <sys/vnode.h>
43 #include <sys/inflate.h>
44 #include <sys/sysctl.h>
46 #include <sys/resident.h>
49 #include <vm/vm_param.h>
51 #include <vm/vm_map.h>
52 #include <vm/vm_kern.h>
53 #include <vm/vm_extern.h>
55 static int exec_res_id = 0;
56 static TAILQ_HEAD(,vmresident) exec_res_list = TAILQ_HEAD_INITIALIZER(exec_res_list);
58 static MALLOC_DEFINE(M_EXEC_RES, "vmresident", "resident execs");
61 fill_xresident(struct vmresident *vr, struct xresident *in, struct thread *td)
69 in->res_entry_addr = vr->vr_entry_addr;
70 in->res_id = vr->vr_id;
72 char *freepath, *fullpath;
73 error = vn_fullpath(td->td_proc, vrtmp, &fullpath, &freepath);
75 /* could not retrieve cached path, return zero'ed string */
76 bzero(in->res_file, MAXPATHLEN);
79 bcopy(fullpath, in->res_file, MAXPATHLEN);
80 free(freepath, M_TEMP);
83 /* indicate that we are using the vnode */
84 error = vget(vrtmp, NULL, LK_EXCLUSIVE, td);
88 /* retrieve underlying stat information and release vnode */
89 error = vn_stat(vrtmp, &st, td);
99 printf("fill_xresident, error = %d\n", error);
104 sysctl_vm_resident(SYSCTL_HANDLER_ARGS)
106 struct vmresident *vmres;
111 /* only super-user should call this sysctl */
113 if ((suser(td)) != 0)
118 if (exec_res_id == 0)
121 /* client queried for number of resident binaries */
123 return SYSCTL_OUT(req, 0, exec_res_id);
125 TAILQ_FOREACH(vmres, &exec_res_list, vr_link) {
126 struct xresident xres;
127 error = fill_xresident(vmres, &xres, td);
131 error = SYSCTL_OUT(req, (void *)&xres,
132 sizeof(struct xresident));
139 SYSCTL_PROC(_vm, OID_AUTO, resident, CTLTYPE_OPAQUE|CTLFLAG_RD, 0, 0,
140 sysctl_vm_resident, "S,xresident", "resident executables (sys/resident.h)");
143 exec_resident_imgact(struct image_params *imgp)
145 struct vmresident *vmres;
148 * resident image activator
150 if ((vmres = imgp->vp->v_resident) == NULL)
152 exec_new_vmspace(imgp, vmres->vr_vmspace);
154 imgp->interpreted = 0;
155 imgp->proc->p_sysent = vmres->vr_sysent;
156 imgp->entry_addr = vmres->vr_entry_addr;
161 * exec_sys_register(entry)
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.
168 exec_sys_register(struct exec_sys_register_args *uap)
170 struct vmresident *vmres;
176 if ((error = suser(p->p_thread)) != 0)
178 if ((vp = p->p_textvp) == NULL)
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);
195 * exec_sys_unregister(id)
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.
202 exec_sys_unregister(struct exec_sys_unregister_args *uap)
204 struct vmresident *vmres;
211 if ((error = suser(p->p_thread)) != 0)
215 * If id is -1, unregister ourselves
217 if ((id = uap->id) == -1 && p->p_textvp && p->p_textvp->v_resident)
218 id = p->p_textvp->v_resident->vr_id;
221 * Look for the registration
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;
234 if (vmres->vr_vmspace) {
235 vmspace_free(vmres->vr_vmspace);
236 vmres->vr_vmspace = NULL;
238 free(vmres, M_EXEC_RES);
246 uap->sysmsg_result = count;