1 /* 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 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 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 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 32 * SUCH DAMAGE. 33 * 34 * $DragonFly: src/sys/kern/imgact_resident.c,v 1.17 2007/04/30 07:18:53 dillon Exp $ 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/resourcevar.h> 47 #include <sys/sysent.h> 48 #include <sys/systm.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 #include <sys/sysref2.h> 64 65 static int exec_res_id = 0; 66 67 static TAILQ_HEAD(,vmresident) exec_res_list; 68 69 static MALLOC_DEFINE(M_EXEC_RES, "vmresident", "resident execs"); 70 71 /* lockmgr lock for protecting the exec_res_list */ 72 static struct lock exec_list_lock; 73 74 static void 75 vm_resident_init(void *__dummy) 76 { 77 lockinit(&exec_list_lock, "vmres", 0, 0); 78 TAILQ_INIT(&exec_res_list); 79 } 80 SYSINIT(vmres, SI_BOOT1_LOCK, SI_ORDER_ANY, vm_resident_init, 0); 81 82 static int 83 fill_xresident(struct vmresident *vr, struct xresident *in, struct thread *td) 84 { 85 struct stat st; 86 struct vnode *vrtmp; 87 int error = 0; 88 89 vrtmp = vr->vr_vnode; 90 91 in->res_entry_addr = vr->vr_entry_addr; 92 in->res_id = vr->vr_id; 93 if (vrtmp) { 94 char *freepath, *fullpath; 95 error = vn_fullpath(td->td_proc, vrtmp, &fullpath, &freepath); 96 if (error != 0) { 97 /* could not retrieve cached path, return zero'ed string */ 98 bzero(in->res_file, MAXPATHLEN); 99 error = 0; 100 } else { 101 strlcpy(in->res_file, fullpath, sizeof(in->res_file)); 102 kfree(freepath, M_TEMP); 103 } 104 105 /* indicate that we are using the vnode */ 106 error = vget(vrtmp, LK_EXCLUSIVE); 107 if (error) 108 goto done; 109 110 /* retrieve underlying stat information and release vnode */ 111 error = vn_stat(vrtmp, &st, td->td_proc->p_ucred); 112 vput(vrtmp); 113 if (error) 114 goto done; 115 116 in->res_stat = st; 117 } 118 119 done: 120 if (error) 121 kprintf("fill_xresident, error = %d\n", error); 122 return (error); 123 } 124 125 static int 126 sysctl_vm_resident(SYSCTL_HANDLER_ARGS) 127 { 128 struct vmresident *vmres; 129 struct thread *td; 130 int error; 131 int count; 132 133 /* only super-user should call this sysctl */ 134 td = req->td; 135 if ((suser(td)) != 0) 136 return EPERM; 137 138 error = count = 0; 139 140 if (exec_res_id == 0) 141 return error; 142 143 /* client queried for number of resident binaries */ 144 if (!req->oldptr) 145 return SYSCTL_OUT(req, 0, exec_res_id); 146 147 lockmgr(&exec_list_lock, LK_SHARED); 148 TAILQ_FOREACH(vmres, &exec_res_list, vr_link) { 149 struct xresident xres; 150 error = fill_xresident(vmres, &xres, td); 151 if (error != 0) 152 break; 153 154 error = SYSCTL_OUT(req, (void *)&xres, 155 sizeof(struct xresident)); 156 if (error != 0) 157 break; 158 } 159 lockmgr(&exec_list_lock, LK_RELEASE); 160 161 return (error); 162 } 163 SYSCTL_PROC(_vm, OID_AUTO, resident, CTLTYPE_OPAQUE|CTLFLAG_RD, 0, 0, 164 sysctl_vm_resident, "S,xresident", "resident executables (sys/resident.h)"); 165 166 int 167 exec_resident_imgact(struct image_params *imgp) 168 { 169 struct vmresident *vmres; 170 171 /* 172 * resident image activator 173 */ 174 if ((vmres = imgp->vp->v_resident) == NULL) 175 return(-1); 176 exec_new_vmspace(imgp, vmres->vr_vmspace); 177 imgp->resident = 1; 178 imgp->interpreted = 0; 179 imgp->proc->p_sysent = vmres->vr_sysent; 180 imgp->entry_addr = vmres->vr_entry_addr; 181 return(0); 182 } 183 184 /* 185 * exec_sys_register(entry) 186 * 187 * Register ourselves for resident execution. Only root can do this. This 188 * will snapshot the vmspace and cause future exec's of the specified binary 189 * to use the snapshot directly rather then load & relocate a new copy. 190 */ 191 int 192 sys_exec_sys_register(struct exec_sys_register_args *uap) 193 { 194 struct vmresident *vmres; 195 struct vnode *vp; 196 struct proc *p; 197 int error; 198 199 p = curproc; 200 if ((error = suser_cred(p->p_ucred, 0)) != 0) 201 return(error); 202 if ((vp = p->p_textvp) == NULL) 203 return(ENOENT); 204 if (vp->v_resident) 205 return(EEXIST); 206 vhold(vp); 207 vmres = kmalloc(sizeof(*vmres), M_EXEC_RES, M_WAITOK); 208 vp->v_resident = vmres; 209 vmres->vr_vnode = vp; 210 vmres->vr_sysent = p->p_sysent; 211 vmres->vr_id = ++exec_res_id; 212 vmres->vr_entry_addr = (intptr_t)uap->entry; 213 vmres->vr_vmspace = vmspace_fork(p->p_vmspace); /* XXX order */ 214 215 lockmgr(&exec_list_lock, LK_EXCLUSIVE); 216 TAILQ_INSERT_TAIL(&exec_res_list, vmres, vr_link); 217 lockmgr(&exec_list_lock, LK_RELEASE); 218 219 return(0); 220 } 221 222 /* 223 * exec_sys_unregister(id) 224 * 225 * Unregister the specified id. If an id of -1 is used unregister 226 * the registration associated with the current process. An id of -2 227 * unregisters everything. 228 */ 229 int 230 sys_exec_sys_unregister(struct exec_sys_unregister_args *uap) 231 { 232 struct vmresident *vmres; 233 struct proc *p; 234 int error; 235 int id; 236 int count; 237 238 p = curproc; 239 if ((error = suser_cred(p->p_ucred, 0)) != 0) 240 return(error); 241 242 /* 243 * If id is -1, unregister ourselves 244 */ 245 if ((id = uap->id) == -1 && p->p_textvp && p->p_textvp->v_resident) 246 id = p->p_textvp->v_resident->vr_id; 247 248 /* 249 * Look for the registration 250 */ 251 error = ENOENT; 252 count = 0; 253 254 lockmgr(&exec_list_lock, LK_EXCLUSIVE); 255 restart: 256 TAILQ_FOREACH(vmres, &exec_res_list, vr_link) { 257 if (id == -2 || vmres->vr_id == id) { 258 TAILQ_REMOVE(&exec_res_list, vmres, vr_link); 259 if (vmres->vr_vnode) { 260 vmres->vr_vnode->v_resident = NULL; 261 vdrop(vmres->vr_vnode); 262 vmres->vr_vnode = NULL; 263 } 264 if (vmres->vr_vmspace) { 265 sysref_put(&vmres->vr_vmspace->vm_sysref); 266 vmres->vr_vmspace = NULL; 267 } 268 kfree(vmres, M_EXEC_RES); 269 exec_res_id--; 270 error = 0; 271 ++count; 272 goto restart; 273 } 274 } 275 lockmgr(&exec_list_lock, LK_RELEASE); 276 if (error == 0) 277 uap->sysmsg_result = count; 278 return(error); 279 } 280 281