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