Fix "ls: not found" problem during buildworld. mdate.sh script
[dragonfly.git] / sys / kern / imgact_resident.c
blobb56b6c562cb6c0af86cb757d5b9fc1463c771521
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:
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.
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.
34 * $DragonFly: src/sys/kern/imgact_resident.c,v 1.8 2006/03/02 19:07:59 dillon Exp $
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>
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>
63 static int exec_res_id = 0;
65 static TAILQ_HEAD(,vmresident) exec_res_list;
67 static MALLOC_DEFINE(M_EXEC_RES, "vmresident", "resident execs");
69 /* lockmgr lock for protecting the exec_res_list */
70 static struct lock exec_list_lock;
72 static void
73 vm_resident_init(void *__dummy)
75 lockinit(&exec_list_lock, "vmres", 0, 0);
76 TAILQ_INIT(&exec_res_list);
78 SYSINIT(vmres, SI_SUB_VM, SI_ORDER_ANY, vm_resident_init, 0);
80 static int
81 fill_xresident(struct vmresident *vr, struct xresident *in, struct thread *td)
83 struct stat st;
84 struct vnode *vrtmp;
85 int error = 0;
87 vrtmp = vr->vr_vnode;
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);
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 free(freepath, M_TEMP);
103 /* indicate that we are using the vnode */
104 error = vget(vrtmp, LK_EXCLUSIVE, td);
105 if (error)
106 goto done;
108 /* retrieve underlying stat information and release vnode */
109 error = vn_stat(vrtmp, &st, td);
110 vput(vrtmp);
111 if (error)
112 goto done;
114 in->res_stat = st;
117 done:
118 if (error)
119 printf("fill_xresident, error = %d\n", error);
120 return (error);
123 static int
124 sysctl_vm_resident(SYSCTL_HANDLER_ARGS)
126 struct vmresident *vmres;
127 struct thread *td;
128 int error;
129 int count;
131 /* only super-user should call this sysctl */
132 td = req->td;
133 if ((suser(td)) != 0)
134 return EPERM;
136 error = count = 0;
138 if (exec_res_id == 0)
139 return error;
141 /* client queried for number of resident binaries */
142 if (!req->oldptr)
143 return SYSCTL_OUT(req, 0, exec_res_id);
145 lockmgr(&exec_list_lock, LK_SHARED, NULL, td);
146 TAILQ_FOREACH(vmres, &exec_res_list, vr_link) {
147 struct xresident xres;
148 error = fill_xresident(vmres, &xres, td);
149 if (error != 0)
150 break;
152 error = SYSCTL_OUT(req, (void *)&xres,
153 sizeof(struct xresident));
154 if (error != 0)
155 break;
157 lockmgr(&exec_list_lock, LK_RELEASE, NULL, td);
159 return (error);
161 SYSCTL_PROC(_vm, OID_AUTO, resident, CTLTYPE_OPAQUE|CTLFLAG_RD, 0, 0,
162 sysctl_vm_resident, "S,xresident", "resident executables (sys/resident.h)");
165 exec_resident_imgact(struct image_params *imgp)
167 struct vmresident *vmres;
170 * resident image activator
172 if ((vmres = imgp->vp->v_resident) == NULL)
173 return(-1);
174 exec_new_vmspace(imgp, vmres->vr_vmspace);
175 imgp->resident = 1;
176 imgp->interpreted = 0;
177 imgp->proc->p_sysent = vmres->vr_sysent;
178 imgp->entry_addr = vmres->vr_entry_addr;
179 return(0);
183 * exec_sys_register(entry)
185 * Register ourselves for resident execution. Only root can do this. This
186 * will snapshot the vmspace and cause future exec's of the specified binary
187 * to use the snapshot directly rather then load & relocate a new copy.
190 exec_sys_register(struct exec_sys_register_args *uap)
192 struct vmresident *vmres;
193 struct vnode *vp;
194 struct proc *p;
195 int error;
197 p = curproc;
198 if ((error = suser(p->p_thread)) != 0)
199 return(error);
200 if ((vp = p->p_textvp) == NULL)
201 return(ENOENT);
202 if (vp->v_resident)
203 return(EEXIST);
204 vhold(vp);
205 vmres = malloc(sizeof(*vmres), M_EXEC_RES, M_WAITOK);
206 vp->v_resident = vmres;
207 vmres->vr_vnode = vp;
208 vmres->vr_sysent = p->p_sysent;
209 vmres->vr_id = ++exec_res_id;
210 vmres->vr_entry_addr = (intptr_t)uap->entry;
211 vmres->vr_vmspace = vmspace_fork(p->p_vmspace); /* XXX order */
213 lockmgr(&exec_list_lock, LK_EXCLUSIVE, NULL, curthread);
214 TAILQ_INSERT_TAIL(&exec_res_list, vmres, vr_link);
215 lockmgr(&exec_list_lock, LK_RELEASE, NULL, curthread);
217 return(0);
221 * exec_sys_unregister(id)
223 * Unregister the specified id. If an id of -1 is used unregister
224 * the registration associated with the current process. An id of -2
225 * unregisters everything.
228 exec_sys_unregister(struct exec_sys_unregister_args *uap)
230 struct vmresident *vmres;
231 struct proc *p;
232 int error;
233 int id;
234 int count;
236 p = curproc;
237 if ((error = suser(p->p_thread)) != 0)
238 return(error);
241 * If id is -1, unregister ourselves
243 if ((id = uap->id) == -1 && p->p_textvp && p->p_textvp->v_resident)
244 id = p->p_textvp->v_resident->vr_id;
247 * Look for the registration
249 error = ENOENT;
250 count = 0;
252 lockmgr(&exec_list_lock, LK_EXCLUSIVE, NULL, curthread);
253 restart:
254 TAILQ_FOREACH(vmres, &exec_res_list, vr_link) {
255 if (id == -2 || vmres->vr_id == id) {
256 TAILQ_REMOVE(&exec_res_list, vmres, vr_link);
257 if (vmres->vr_vnode) {
258 vmres->vr_vnode->v_resident = NULL;
259 vdrop(vmres->vr_vnode);
260 vmres->vr_vnode = NULL;
262 if (vmres->vr_vmspace) {
263 vmspace_free(vmres->vr_vmspace);
264 vmres->vr_vmspace = NULL;
266 free(vmres, M_EXEC_RES);
267 exec_res_id--;
268 error = 0;
269 ++count;
270 goto restart;
273 lockmgr(&exec_list_lock, LK_RELEASE, NULL, curthread);
274 if (error == 0)
275 uap->sysmsg_result = count;
276 return(error);