2 * Copyright (c) 2003,2004 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
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>
42 #include <sys/imgact.h>
43 #include <sys/imgact_aout.h>
46 #include <sys/resourcevar.h>
47 #include <sys/sysent.h>
48 #include <sys/systm.h>
50 #include <sys/vnode.h>
51 #include <sys/inflate.h>
52 #include <sys/sysctl.h>
54 #include <sys/resident.h>
57 #include <vm/vm_param.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
;
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);
81 fill_xresident(struct vmresident
*vr
, struct xresident
*in
, struct thread
*td
)
89 in
->res_entry_addr
= vr
->vr_entry_addr
;
90 in
->res_id
= vr
->vr_id
;
92 char *freepath
, *fullpath
;
93 error
= vn_fullpath(td
->td_proc
, vrtmp
, &fullpath
, &freepath
);
95 /* could not retrieve cached path, return zero'ed string */
96 bzero(in
->res_file
, MAXPATHLEN
);
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
);
108 /* retrieve underlying stat information and release vnode */
109 error
= vn_stat(vrtmp
, &st
, td
);
119 printf("fill_xresident, error = %d\n", error
);
124 sysctl_vm_resident(SYSCTL_HANDLER_ARGS
)
126 struct vmresident
*vmres
;
131 /* only super-user should call this sysctl */
133 if ((suser(td
)) != 0)
138 if (exec_res_id
== 0)
141 /* client queried for number of resident binaries */
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
);
152 error
= SYSCTL_OUT(req
, (void *)&xres
,
153 sizeof(struct xresident
));
157 lockmgr(&exec_list_lock
, LK_RELEASE
, NULL
, td
);
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
)
174 exec_new_vmspace(imgp
, vmres
->vr_vmspace
);
176 imgp
->interpreted
= 0;
177 imgp
->proc
->p_sysent
= vmres
->vr_sysent
;
178 imgp
->entry_addr
= vmres
->vr_entry_addr
;
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
;
198 if ((error
= suser(p
->p_thread
)) != 0)
200 if ((vp
= p
->p_textvp
) == NULL
)
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
);
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
;
237 if ((error
= suser(p
->p_thread
)) != 0)
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
252 lockmgr(&exec_list_lock
, LK_EXCLUSIVE
, NULL
, curthread
);
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
);
273 lockmgr(&exec_list_lock
, LK_RELEASE
, NULL
, curthread
);
275 uap
->sysmsg_result
= count
;