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