xref: /freebsd/sys/kern/kern_exec.c (revision a7cddfed7f9900e4c50e8ae1c6ec792f808b74de)
126f9a767SRodney W. Grimes /*
226f9a767SRodney W. Grimes  * Copyright (c) 1993, David Greenman
326f9a767SRodney W. Grimes  * All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
6df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
7df8bae1dSRodney W. Grimes  * are met:
8df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
9df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
10df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
11df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
12df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13df8bae1dSRodney W. Grimes  *
1426f9a767SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1726f9a767SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
25df8bae1dSRodney W. Grimes  *
26c3aac50fSPeter Wemm  * $FreeBSD$
27df8bae1dSRodney W. Grimes  */
28df8bae1dSRodney W. Grimes 
291e9b3d91SPeter Wemm #include "opt_ktrace.h"
30339b79b9SRobert Watson #include "opt_mac.h"
311e9b3d91SPeter Wemm 
32df8bae1dSRodney W. Grimes #include <sys/param.h>
3326f9a767SRodney W. Grimes #include <sys/systm.h>
34fb919e4dSMark Murray #include <sys/lock.h>
35fb919e4dSMark Murray #include <sys/mutex.h>
36d2d3e875SBruce Evans #include <sys/sysproto.h>
3726f9a767SRodney W. Grimes #include <sys/signalvar.h>
3826f9a767SRodney W. Grimes #include <sys/kernel.h>
39339b79b9SRobert Watson #include <sys/mac.h>
4026f9a767SRodney W. Grimes #include <sys/mount.h>
41797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
42079cc25bSDavid Greenman #include <sys/fcntl.h>
4326f9a767SRodney W. Grimes #include <sys/acct.h>
4426f9a767SRodney W. Grimes #include <sys/exec.h>
4526f9a767SRodney W. Grimes #include <sys/imgact.h>
46e1743d02SSøren Schmidt #include <sys/imgact_elf.h>
4726f9a767SRodney W. Grimes #include <sys/wait.h>
48333ea485SGuido van Rooij #include <sys/malloc.h>
49a794e791SBruce Evans #include <sys/proc.h>
502a024a2bSSean Eric Fagan #include <sys/pioctl.h>
51a794e791SBruce Evans #include <sys/namei.h>
521e1e0b44SSøren Schmidt #include <sys/sysent.h>
53797f2d22SPoul-Henning Kamp #include <sys/shm.h>
5499ac3bc8SPeter Wemm #include <sys/sysctl.h>
55333ea485SGuido van Rooij #include <sys/user.h>
56a794e791SBruce Evans #include <sys/vnode.h>
57c781aea8SPeter Wemm #ifdef KTRACE
58c781aea8SPeter Wemm #include <sys/ktrace.h>
59c781aea8SPeter Wemm #endif
6026f9a767SRodney W. Grimes 
6126f9a767SRodney W. Grimes #include <vm/vm.h>
62efeaf95aSDavid Greenman #include <vm/vm_param.h>
63efeaf95aSDavid Greenman #include <vm/pmap.h>
641616db3cSJohn Dyson #include <vm/vm_page.h>
65efeaf95aSDavid Greenman #include <vm/vm_map.h>
6626f9a767SRodney W. Grimes #include <vm/vm_kern.h>
67efeaf95aSDavid Greenman #include <vm/vm_extern.h>
681ebd0c59SDavid Greenman #include <vm/vm_object.h>
691616db3cSJohn Dyson #include <vm/vm_pager.h>
7026f9a767SRodney W. Grimes 
7126f9a767SRodney W. Grimes #include <machine/reg.h>
7226f9a767SRodney W. Grimes 
73b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
74b9df5231SPoul-Henning Kamp 
7521d56e9cSAlfred Perlstein static MALLOC_DEFINE(M_ATEXEC, "atexec", "atexec callback");
7621d56e9cSAlfred Perlstein 
7721d56e9cSAlfred Perlstein /*
7821d56e9cSAlfred Perlstein  * callout list for things to do at exec time
7921d56e9cSAlfred Perlstein  */
8021d56e9cSAlfred Perlstein struct execlist {
8121d56e9cSAlfred Perlstein 	execlist_fn function;
8221d56e9cSAlfred Perlstein 	TAILQ_ENTRY(execlist) next;
8321d56e9cSAlfred Perlstein };
8421d56e9cSAlfred Perlstein 
8521d56e9cSAlfred Perlstein TAILQ_HEAD(exec_list_head, execlist);
8621d56e9cSAlfred Perlstein static struct exec_list_head exec_list = TAILQ_HEAD_INITIALIZER(exec_list);
8721d56e9cSAlfred Perlstein 
884d77a549SAlfred Perlstein static register_t *exec_copyout_strings(struct image_params *);
89df8bae1dSRodney W. Grimes 
909701cd40SJohn Baldwin /* XXX This should be vm_size_t. */
919701cd40SJohn Baldwin static u_long ps_strings = PS_STRINGS;
929701cd40SJohn Baldwin SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, 0, "");
93486bddb0SDoug Rabson 
949701cd40SJohn Baldwin /* XXX This should be vm_size_t. */
959701cd40SJohn Baldwin static u_long usrstack = USRSTACK;
969701cd40SJohn Baldwin SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, "");
9799ac3bc8SPeter Wemm 
98b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16;
99c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
1009701cd40SJohn Baldwin     &ps_arg_cache_limit, 0, "");
101b9df5231SPoul-Henning Kamp 
102a8704f89SPoul-Henning Kamp int ps_argsopen = 1;
103a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, "");
104a8704f89SPoul-Henning Kamp 
105911fc923SPeter Wemm #ifdef __ia64__
106911fc923SPeter Wemm /* XXX HACK */
107911fc923SPeter Wemm static int regstkpages = 256;
108911fc923SPeter Wemm SYSCTL_INT(_machdep, OID_AUTO, regstkpages, CTLFLAG_RW, &regstkpages, 0, "");
109911fc923SPeter Wemm #endif
110911fc923SPeter Wemm 
11199ac3bc8SPeter Wemm /*
112aa855a59SPeter Wemm  * Each of the items is a pointer to a `const struct execsw', hence the
113aa855a59SPeter Wemm  * double pointer here.
114df8bae1dSRodney W. Grimes  */
115aa855a59SPeter Wemm static const struct execsw **execsw;
11626f9a767SRodney W. Grimes 
117d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
118ad7507e2SSteven Wallace struct execve_args {
119ad7507e2SSteven Wallace         char    *fname;
120ad7507e2SSteven Wallace         char    **argv;
121ad7507e2SSteven Wallace         char    **envv;
122ad7507e2SSteven Wallace };
123d2d3e875SBruce Evans #endif
124ad7507e2SSteven Wallace 
12526f9a767SRodney W. Grimes /*
12626f9a767SRodney W. Grimes  * execve() system call.
127116734c4SMatthew Dillon  *
128116734c4SMatthew Dillon  * MPSAFE
12926f9a767SRodney W. Grimes  */
13026f9a767SRodney W. Grimes int
131b40ce416SJulian Elischer execve(td, uap)
132b40ce416SJulian Elischer 	struct thread *td;
13326f9a767SRodney W. Grimes 	register struct execve_args *uap;
134df8bae1dSRodney W. Grimes {
135b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
13626f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
1379b3b1c5fSJohn Baldwin 	struct ucred *newcred = NULL, *oldcred;
1381419eacbSAlfred Perlstein 	struct uidinfo *euip;
139654f6be1SBruce Evans 	register_t *stack_base;
140bb56ec4aSPoul-Henning Kamp 	int error, len, i;
141c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
14226f9a767SRodney W. Grimes 	struct vattr attr;
1434d77a549SAlfred Perlstein 	int (*img_first)(struct image_params *);
14469be5db9SAlfred Perlstein 	struct pargs *oldargs = NULL, *newargs = NULL;
1459b3b1c5fSJohn Baldwin 	struct procsig *oldprocsig, *newprocsig;
1466c84de02SJohn Baldwin #ifdef KTRACE
1476c84de02SJohn Baldwin 	struct vnode *tracevp = NULL;
1486c84de02SJohn Baldwin #endif
1496c84de02SJohn Baldwin 	struct vnode *textvp = NULL;
150d06c0d4dSRobert Watson 	int credential_changing;
151619eb6e5SJeff Roberson 	int textset;
15226f9a767SRodney W. Grimes 
153c52007c2SDavid Greenman 	imgp = &image_params;
154df8bae1dSRodney W. Grimes 
1559ca45e81SDag-Erling Smørgrav 	/*
1569ca45e81SDag-Erling Smørgrav 	 * Lock the process and set the P_INEXEC flag to indicate that
1579ca45e81SDag-Erling Smørgrav 	 * it should be left alone until we're done here.  This is
1589ca45e81SDag-Erling Smørgrav 	 * necessary to avoid race conditions - e.g. in ptrace() -
1599ca45e81SDag-Erling Smørgrav 	 * that might allow a local user to illicitly obtain elevated
1609ca45e81SDag-Erling Smørgrav 	 * privileges.
1619ca45e81SDag-Erling Smørgrav 	 */
1629ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
1639ca45e81SDag-Erling Smørgrav 	KASSERT((p->p_flag & P_INEXEC) == 0,
16491f91617SDavid E. O'Brien 	    ("%s(): process already has P_INEXEC flag", __func__));
16549539972SJulian Elischer 	if (p->p_flag & P_KSES) {
16649539972SJulian Elischer 		if (thread_single(SNGLE_EXIT)) {
167e602ba25SJulian Elischer 			PROC_UNLOCK(p);
168e602ba25SJulian Elischer 			return (ERESTART);	/* Try again later. */
169e602ba25SJulian Elischer 		}
17049539972SJulian Elischer 		/*
17149539972SJulian Elischer 		 * If we get here all other threads are dead,
17249539972SJulian Elischer 		 * so unset the associated flags and lose KSE mode.
17349539972SJulian Elischer 		 */
17449539972SJulian Elischer 		p->p_flag &= ~P_KSES;
17549539972SJulian Elischer 		td->td_flags &= ~TDF_UNBOUND;
17649539972SJulian Elischer 		thread_single_end();
17749539972SJulian Elischer 	}
1789ca45e81SDag-Erling Smørgrav 	p->p_flag |= P_INEXEC;
1799ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
1809ca45e81SDag-Erling Smørgrav 
181df8bae1dSRodney W. Grimes 	/*
182c52007c2SDavid Greenman 	 * Initialize part of the common data
183df8bae1dSRodney W. Grimes 	 */
184c52007c2SDavid Greenman 	imgp->proc = p;
185c52007c2SDavid Greenman 	imgp->uap = uap;
186c52007c2SDavid Greenman 	imgp->attr = &attr;
187c52007c2SDavid Greenman 	imgp->argc = imgp->envc = 0;
1885cf3d12cSAndrey A. Chernov 	imgp->argv0 = NULL;
189c52007c2SDavid Greenman 	imgp->entry_addr = 0;
190c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
191c52007c2SDavid Greenman 	imgp->interpreted = 0;
192c52007c2SDavid Greenman 	imgp->interpreter_name[0] = '\0';
193e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1941616db3cSJohn Dyson 	imgp->vp = NULL;
1950b2ed1aeSJeff Roberson 	imgp->object = NULL;
1961616db3cSJohn Dyson 	imgp->firstpage = NULL;
1974fe88fe6SJohn Polstra 	imgp->ps_strings = 0;
198b9a22da4STakanori Watanabe 	imgp->auxarg_size = 0;
19926f9a767SRodney W. Grimes 
20026f9a767SRodney W. Grimes 	/*
20126f9a767SRodney W. Grimes 	 * Allocate temporary demand zeroed space for argument and
20226f9a767SRodney W. Grimes 	 *	environment strings
20326f9a767SRodney W. Grimes 	 */
2041616db3cSJohn Dyson 	imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE);
205c2f9f36bSDavid Greenman 	if (imgp->stringbase == NULL) {
20626f9a767SRodney W. Grimes 		error = ENOMEM;
207b3afd20dSAlan Cox 		mtx_lock(&Giant);
20826f9a767SRodney W. Grimes 		goto exec_fail;
20926f9a767SRodney W. Grimes 	}
210c52007c2SDavid Greenman 	imgp->stringp = imgp->stringbase;
211c52007c2SDavid Greenman 	imgp->stringspace = ARG_MAX;
2121616db3cSJohn Dyson 	imgp->image_header = imgp->stringbase + ARG_MAX;
21326f9a767SRodney W. Grimes 
21426f9a767SRodney W. Grimes 	/*
21526f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
21626f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
21726f9a767SRodney W. Grimes 	 */
21826f9a767SRodney W. Grimes 	ndp = &nd;
219b35ba931SDavid Greenman 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
220b40ce416SJulian Elischer 	    UIO_USERSPACE, uap->fname, td);
22126f9a767SRodney W. Grimes 
222b3afd20dSAlan Cox 	mtx_lock(&Giant);
22326f9a767SRodney W. Grimes interpret:
22426f9a767SRodney W. Grimes 
22526f9a767SRodney W. Grimes 	error = namei(ndp);
22626f9a767SRodney W. Grimes 	if (error) {
2271616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
2281616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
22926f9a767SRodney W. Grimes 		goto exec_fail;
23026f9a767SRodney W. Grimes 	}
23126f9a767SRodney W. Grimes 
232c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
2339c0fed3dSDoug Rabson 	imgp->fname = uap->fname;
23426f9a767SRodney W. Grimes 
23526f9a767SRodney W. Grimes 	/*
2369022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
2379022e4adSDavid Greenman 	 */
238c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
239619eb6e5SJeff Roberson 	if (error)
24026f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
241619eb6e5SJeff Roberson 
242619eb6e5SJeff Roberson 	if (VOP_GETVOBJECT(imgp->vp, &imgp->object) == 0)
2430b2ed1aeSJeff Roberson 		vm_object_reference(imgp->object);
24426f9a767SRodney W. Grimes 
245619eb6e5SJeff Roberson 	/*
246619eb6e5SJeff Roberson 	 * Set VV_TEXT now so no one can write to the executable while we're
247619eb6e5SJeff Roberson 	 * activating it.
248619eb6e5SJeff Roberson 	 *
249619eb6e5SJeff Roberson 	 * Remember if this was set before and unset it in case this is not
250619eb6e5SJeff Roberson 	 * actually an executable image.
251619eb6e5SJeff Roberson 	 */
252619eb6e5SJeff Roberson 	textset = imgp->vp->v_vflag & VV_TEXT;
253619eb6e5SJeff Roberson 	imgp->vp->v_vflag |= VV_TEXT;
254619eb6e5SJeff Roberson 
2551616db3cSJohn Dyson 	error = exec_map_first_page(imgp);
2566d5a0a8cSDavid Greenman 	if (error)
25726f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
25826f9a767SRodney W. Grimes 
25926f9a767SRodney W. Grimes 	/*
260d323ddf3SMatthew Dillon 	 *	If the current process has a special image activator it
261d323ddf3SMatthew Dillon 	 *	wants to try first, call it.   For example, emulating shell
262d323ddf3SMatthew Dillon 	 *	scripts differently.
26326f9a767SRodney W. Grimes 	 */
264d323ddf3SMatthew Dillon 	error = -1;
265d323ddf3SMatthew Dillon 	if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
266d323ddf3SMatthew Dillon 		error = img_first(imgp);
267d323ddf3SMatthew Dillon 
268d323ddf3SMatthew Dillon 	/*
269d323ddf3SMatthew Dillon 	 *	Loop through the list of image activators, calling each one.
270d323ddf3SMatthew Dillon 	 *	An activator returns -1 if there is no match, 0 on success,
271d323ddf3SMatthew Dillon 	 *	and an error otherwise.
272d323ddf3SMatthew Dillon 	 */
273d323ddf3SMatthew Dillon 	for (i = 0; error == -1 && execsw[i]; ++i) {
274d323ddf3SMatthew Dillon 		if (execsw[i]->ex_imgact == NULL ||
275d323ddf3SMatthew Dillon 		    execsw[i]->ex_imgact == img_first) {
276d323ddf3SMatthew Dillon 			continue;
277d323ddf3SMatthew Dillon 		}
278c52007c2SDavid Greenman 		error = (*execsw[i]->ex_imgact)(imgp);
279d323ddf3SMatthew Dillon 	}
280d323ddf3SMatthew Dillon 
281d323ddf3SMatthew Dillon 	if (error) {
282619eb6e5SJeff Roberson 		if (error == -1) {
283619eb6e5SJeff Roberson 			if (textset == 0)
284619eb6e5SJeff Roberson 				imgp->vp->v_vflag &= ~VV_TEXT;
285d323ddf3SMatthew Dillon 			error = ENOEXEC;
286619eb6e5SJeff Roberson 		}
28726f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
288d323ddf3SMatthew Dillon 	}
289d323ddf3SMatthew Dillon 
290d323ddf3SMatthew Dillon 	/*
291d323ddf3SMatthew Dillon 	 * Special interpreter operation, cleanup and loop up to try to
292d323ddf3SMatthew Dillon 	 * activate the interpreter.
293d323ddf3SMatthew Dillon 	 */
294c52007c2SDavid Greenman 	if (imgp->interpreted) {
2951616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
296619eb6e5SJeff Roberson 		/*
297619eb6e5SJeff Roberson 		 * VV_TEXT needs to be unset for scripts.  There is a short
298619eb6e5SJeff Roberson 		 * period before we determine that something is a script where
299619eb6e5SJeff Roberson 		 * VV_TEXT will be set. The vnode lock is held over this
300619eb6e5SJeff Roberson 		 * entire period so nothing should illegitimately be blocked.
301619eb6e5SJeff Roberson 		 */
302619eb6e5SJeff Roberson 		imgp->vp->v_vflag &= ~VV_TEXT;
303762e6b85SEivind Eklund 		/* free name buffer and old vnode */
304762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
305619eb6e5SJeff Roberson 		vput(ndp->ni_vp);
3060b2ed1aeSJeff Roberson 		vm_object_deallocate(imgp->object);
3070b2ed1aeSJeff Roberson 		imgp->object = NULL;
30826f9a767SRodney W. Grimes 		/* set new name to that of the interpreter */
309b35ba931SDavid Greenman 		NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
310b40ce416SJulian Elischer 		    UIO_SYSSPACE, imgp->interpreter_name, td);
31126f9a767SRodney W. Grimes 		goto interpret;
31226f9a767SRodney W. Grimes 	}
31326f9a767SRodney W. Grimes 
31426f9a767SRodney W. Grimes 	/*
31526f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
31626f9a767SRodney W. Grimes 	 */
3173ebc1248SPeter Wemm 	if (p->p_sysent->sv_copyout_strings)
3183ebc1248SPeter Wemm 		stack_base = (*p->p_sysent->sv_copyout_strings)(imgp);
3193ebc1248SPeter Wemm 	else
320c52007c2SDavid Greenman 		stack_base = exec_copyout_strings(imgp);
32126f9a767SRodney W. Grimes 
32226f9a767SRodney W. Grimes 	/*
3231e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
3241e1e0b44SSøren Schmidt 	 * let it do the stack setup.
3251e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
32626f9a767SRodney W. Grimes 	 */
3271e1e0b44SSøren Schmidt 	if (p->p_sysent->sv_fixup)
328c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
3291e1e0b44SSøren Schmidt 	else
330c52007c2SDavid Greenman 		suword(--stack_base, imgp->argc);
33126f9a767SRodney W. Grimes 
332a78e8d2aSDavid Greenman 	/*
333a78e8d2aSDavid Greenman 	 * For security and other reasons, the file descriptor table cannot
334a78e8d2aSDavid Greenman 	 * be shared after an exec.
335a78e8d2aSDavid Greenman 	 */
336426da3bcSAlfred Perlstein 	FILEDESC_LOCK(p->p_fd);
337a78e8d2aSDavid Greenman 	if (p->p_fd->fd_refcnt > 1) {
338a78e8d2aSDavid Greenman 		struct filedesc *tmp;
339a78e8d2aSDavid Greenman 
340b40ce416SJulian Elischer 		tmp = fdcopy(td);
341426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(p->p_fd);
342b40ce416SJulian Elischer 		fdfree(td);
343a78e8d2aSDavid Greenman 		p->p_fd = tmp;
344426da3bcSAlfred Perlstein 	} else
345426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(p->p_fd);
346a78e8d2aSDavid Greenman 
347333ea485SGuido van Rooij 	/*
3489b3b1c5fSJohn Baldwin 	 * Malloc things before we need locks.
3499b3b1c5fSJohn Baldwin 	 */
3509b3b1c5fSJohn Baldwin 	newcred = crget();
3511419eacbSAlfred Perlstein 	euip = uifind(attr.va_uid);
3529b3b1c5fSJohn Baldwin 	i = imgp->endargs - imgp->stringbase;
3539b3b1c5fSJohn Baldwin 	if (ps_arg_cache_limit >= i + sizeof(struct pargs))
3549b3b1c5fSJohn Baldwin 		newargs = pargs_alloc(i);
3559b3b1c5fSJohn Baldwin 
3569b3b1c5fSJohn Baldwin 	/* close files on exec */
3579b3b1c5fSJohn Baldwin 	fdcloseexec(td);
3589b3b1c5fSJohn Baldwin 
359619eb6e5SJeff Roberson 	/* Get a reference to the vnode prior to locking the proc */
360619eb6e5SJeff Roberson 	VREF(ndp->ni_vp);
361619eb6e5SJeff Roberson 
3629b3b1c5fSJohn Baldwin 	/*
363333ea485SGuido van Rooij 	 * For security and other reasons, signal handlers cannot
3648688bb93SJohn Baldwin 	 * be shared after an exec. The new process gets a copy of the old
365b2c3fa70SDima Dorfman 	 * handlers. In execsigs(), the new process will have its signals
366333ea485SGuido van Rooij 	 * reset.
367333ea485SGuido van Rooij 	 */
3689b3b1c5fSJohn Baldwin 	PROC_LOCK(p);
3699b3b1c5fSJohn Baldwin 	mp_fixme("procsig needs a lock");
370333ea485SGuido van Rooij 	if (p->p_procsig->ps_refcnt > 1) {
3719b3b1c5fSJohn Baldwin 		oldprocsig = p->p_procsig;
3729b3b1c5fSJohn Baldwin 		PROC_UNLOCK(p);
373333ea485SGuido van Rooij 		MALLOC(newprocsig, struct procsig *, sizeof(struct procsig),
374333ea485SGuido van Rooij 		    M_SUBPROC, M_WAITOK);
3759b3b1c5fSJohn Baldwin 		bcopy(oldprocsig, newprocsig, sizeof(*newprocsig));
3769b3b1c5fSJohn Baldwin 		newprocsig->ps_refcnt = 1;
3779b3b1c5fSJohn Baldwin 		oldprocsig->ps_refcnt--;
3789b3b1c5fSJohn Baldwin 		PROC_LOCK(p);
379333ea485SGuido van Rooij 		p->p_procsig = newprocsig;
380b40ce416SJulian Elischer 		if (p->p_sigacts == &p->p_uarea->u_sigacts)
381b2c3fa70SDima Dorfman 			panic("shared procsig but private sigacts?");
382333ea485SGuido van Rooij 
383b40ce416SJulian Elischer 		p->p_uarea->u_sigacts = *p->p_sigacts;
384b40ce416SJulian Elischer 		p->p_sigacts = &p->p_uarea->u_sigacts;
385333ea485SGuido van Rooij 	}
386fdf4e8b3SWarner Losh 	/* Stop profiling */
387fdf4e8b3SWarner Losh 	stopprofclock(p);
388fdf4e8b3SWarner Losh 
38926f9a767SRodney W. Grimes 	/* reset caught signals */
39026f9a767SRodney W. Grimes 	execsigs(p);
39126f9a767SRodney W. Grimes 
39226f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
39326f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
39426f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
39526f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
39626f9a767SRodney W. Grimes 
39726f9a767SRodney W. Grimes 	/*
39824b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
399dc733423SDag-Erling Smørgrav 	 * it that it now has its own resources back
40026f9a767SRodney W. Grimes 	 */
40126f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
40226f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
40326f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
4047f05b035SAlfred Perlstein 		wakeup(p->p_pptr);
40526f9a767SRodney W. Grimes 	}
40626f9a767SRodney W. Grimes 
40726f9a767SRodney W. Grimes 	/*
408a78e8d2aSDavid Greenman 	 * Implement image setuid/setgid.
409a78e8d2aSDavid Greenman 	 *
410a78e8d2aSDavid Greenman 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
411a78e8d2aSDavid Greenman 	 * the process is being traced.
412c52007c2SDavid Greenman 	 */
413b1fc0ec1SRobert Watson 	oldcred = p->p_ucred;
414d06c0d4dSRobert Watson 	credential_changing = 0;
415d06c0d4dSRobert Watson 	credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid !=
416d06c0d4dSRobert Watson 	    attr.va_uid;
417d06c0d4dSRobert Watson 	credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid !=
418d06c0d4dSRobert Watson 	    attr.va_gid;
419d06c0d4dSRobert Watson 
420d06c0d4dSRobert Watson 	if (credential_changing &&
421a78e8d2aSDavid Greenman 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
422c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
423c52007c2SDavid Greenman 		/*
424c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
425b85db196SPeter Wemm 		 * root.  Record any set-id flags first to make sure that
426b85db196SPeter Wemm 		 * we do not regain any tracing during a possible block.
42726f9a767SRodney W. Grimes 		 */
428b85db196SPeter Wemm 		setsugid(p);
4296c84de02SJohn Baldwin #ifdef KTRACE
43044731cabSJohn Baldwin 		if (p->p_tracep && suser_cred(oldcred, PRISON_ROOT)) {
4316c84de02SJohn Baldwin 			mtx_lock(&ktrace_mtx);
43279deba82SMatthew Dillon 			p->p_traceflag = 0;
4339b3b1c5fSJohn Baldwin 			tracevp = p->p_tracep;
4349b3b1c5fSJohn Baldwin 			p->p_tracep = NULL;
4356c84de02SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
43626f9a767SRodney W. Grimes 		}
4376c84de02SJohn Baldwin #endif
43889ab9307SJacques Vidrine 		/* Close any file descriptors 0..2 that reference procfs */
43989ab9307SJacques Vidrine 		setugidsafety(td);
440e983a376SJacques Vidrine 		/* Make sure file descriptors 0..2 are in use.  */
441e983a376SJacques Vidrine 		error = fdcheckstd(td);
44263c9e754SJohn Baldwin 		if (error != 0)
44369be5db9SAlfred Perlstein 			goto done1;
444c52007c2SDavid Greenman 		/*
445c52007c2SDavid Greenman 		 * Set the new credentials.
446c52007c2SDavid Greenman 		 */
4479b3b1c5fSJohn Baldwin 		crcopy(newcred, oldcred);
448c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
4491419eacbSAlfred Perlstein 			change_euid(newcred, euip);
450c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
451b1fc0ec1SRobert Watson 			change_egid(newcred, attr.va_gid);
4529b3b1c5fSJohn Baldwin 		/*
4539b3b1c5fSJohn Baldwin 		 * Implement correct POSIX saved-id behavior.
4549b3b1c5fSJohn Baldwin 		 */
4559b3b1c5fSJohn Baldwin 		change_svuid(newcred, newcred->cr_uid);
4569b3b1c5fSJohn Baldwin 		change_svgid(newcred, newcred->cr_gid);
4579b3b1c5fSJohn Baldwin 		p->p_ucred = newcred;
4589b3b1c5fSJohn Baldwin 		newcred = NULL;
459c52007c2SDavid Greenman 	} else {
460b1fc0ec1SRobert Watson 		if (oldcred->cr_uid == oldcred->cr_ruid &&
461b1fc0ec1SRobert Watson 		    oldcred->cr_gid == oldcred->cr_rgid)
462c52007c2SDavid Greenman 			p->p_flag &= ~P_SUGID;
46326f9a767SRodney W. Grimes 		/*
464c52007c2SDavid Greenman 		 * Implement correct POSIX saved-id behavior.
465b1fc0ec1SRobert Watson 		 *
466b1fc0ec1SRobert Watson 		 * XXX: It's not clear that the existing behavior is
4679b3b1c5fSJohn Baldwin 		 * POSIX-compliant.  A number of sources indicate that the
4689b3b1c5fSJohn Baldwin 		 * saved uid/gid should only be updated if the new ruid is
4699b3b1c5fSJohn Baldwin 		 * not equal to the old ruid, or the new euid is not equal
4709b3b1c5fSJohn Baldwin 		 * to the old euid and the new euid is not equal to the old
4719b3b1c5fSJohn Baldwin 		 * ruid.  The FreeBSD code always updates the saved uid/gid.
4729b3b1c5fSJohn Baldwin 		 * Also, this code uses the new (replaced) euid and egid as
4739b3b1c5fSJohn Baldwin 		 * the source, which may or may not be the right ones to use.
47426f9a767SRodney W. Grimes 		 */
475b1fc0ec1SRobert Watson 		if (oldcred->cr_svuid != oldcred->cr_uid ||
476b1fc0ec1SRobert Watson 		    oldcred->cr_svgid != oldcred->cr_gid) {
4779b3b1c5fSJohn Baldwin 			crcopy(newcred, oldcred);
478b1fc0ec1SRobert Watson 			change_svuid(newcred, newcred->cr_uid);
479b1fc0ec1SRobert Watson 			change_svgid(newcred, newcred->cr_gid);
480b1fc0ec1SRobert Watson 			p->p_ucred = newcred;
4819b3b1c5fSJohn Baldwin 			newcred = NULL;
4829b3b1c5fSJohn Baldwin 		}
483b1fc0ec1SRobert Watson 	}
48426f9a767SRodney W. Grimes 
48526f9a767SRodney W. Grimes 	/*
486619eb6e5SJeff Roberson 	 * Store the vp for use in procfs.  This vnode was referenced prior
487619eb6e5SJeff Roberson 	 * to locking the proc lock.
4882a531c80SDavid Greenman 	 */
4899b3b1c5fSJohn Baldwin 	textvp = p->p_textvp;
4902a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
4912a531c80SDavid Greenman 
4922a531c80SDavid Greenman 	/*
4939ca45e81SDag-Erling Smørgrav 	 * Notify others that we exec'd, and clear the P_INEXEC flag
4949ca45e81SDag-Erling Smørgrav 	 * as we're now a bona fide freshly-execed process.
495cb679c38SJonathan Lemon 	 */
496cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_EXEC);
4979ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
498cb679c38SJonathan Lemon 
499cb679c38SJonathan Lemon 	/*
50026f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
50126f9a767SRodney W. Grimes 	 * can be set before the program executes.
50226f9a767SRodney W. Grimes 	 */
503e65897c3SJohn Baldwin 	_STOPEVENT(p, S_EXEC, 0);
5042a024a2bSSean Eric Fagan 
50526f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
50626f9a767SRodney W. Grimes 		psignal(p, SIGTRAP);
50726f9a767SRodney W. Grimes 
50826f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
50926f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
51026f9a767SRodney W. Grimes 
511b9df5231SPoul-Henning Kamp 	/* Free any previous argument cache */
5129b3b1c5fSJohn Baldwin 	oldargs = p->p_args;
513b9df5231SPoul-Henning Kamp 	p->p_args = NULL;
514b9df5231SPoul-Henning Kamp 
515e913ca22SDoug Rabson 	/* Set values passed into the program in registers. */
5163ebc1248SPeter Wemm 	if (p->p_sysent->sv_setregs)
5173ebc1248SPeter Wemm 		(*p->p_sysent->sv_setregs)(td, imgp->entry_addr,
5183ebc1248SPeter Wemm 		    (u_long)(uintptr_t)stack_base, imgp->ps_strings);
5193ebc1248SPeter Wemm 	else
520e913ca22SDoug Rabson 		setregs(td, imgp->entry_addr, (u_long)(uintptr_t)stack_base,
521e913ca22SDoug Rabson 		    imgp->ps_strings);
522e913ca22SDoug Rabson 
523b9df5231SPoul-Henning Kamp 	/* Cache arguments if they fit inside our allowance */
524b9df5231SPoul-Henning Kamp 	if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
5259b3b1c5fSJohn Baldwin 		bcopy(imgp->stringbase, newargs->ar_args, i);
5269b3b1c5fSJohn Baldwin 		p->p_args = newargs;
5279b3b1c5fSJohn Baldwin 		newargs = NULL;
528fbd26f75SJohn Baldwin 	}
52969be5db9SAlfred Perlstein done1:
5309b3b1c5fSJohn Baldwin 	PROC_UNLOCK(p);
5319b3b1c5fSJohn Baldwin 
532619eb6e5SJeff Roberson 
5339b3b1c5fSJohn Baldwin 	/*
5349b3b1c5fSJohn Baldwin 	 * Free any resources malloc'd earlier that we didn't use.
5359b3b1c5fSJohn Baldwin 	 */
5361419eacbSAlfred Perlstein 	uifree(euip);
5379b3b1c5fSJohn Baldwin 	if (newcred == NULL)
5389b3b1c5fSJohn Baldwin 		crfree(oldcred);
5399b3b1c5fSJohn Baldwin 	else
5409b3b1c5fSJohn Baldwin 		crfree(newcred);
5419b3b1c5fSJohn Baldwin 	/*
5429b3b1c5fSJohn Baldwin 	 * Handle deferred decrement of ref counts.
5439b3b1c5fSJohn Baldwin 	 */
5449b3b1c5fSJohn Baldwin 	if (textvp != NULL)
5459b3b1c5fSJohn Baldwin 		vrele(textvp);
546619eb6e5SJeff Roberson 	if (ndp->ni_vp && error != 0)
547619eb6e5SJeff Roberson 		vrele(ndp->ni_vp);
5486c84de02SJohn Baldwin #ifdef KTRACE
5499b3b1c5fSJohn Baldwin 	if (tracevp != NULL)
5509b3b1c5fSJohn Baldwin 		vrele(tracevp);
5516c84de02SJohn Baldwin #endif
55269be5db9SAlfred Perlstein 	if (oldargs != NULL)
5539b3b1c5fSJohn Baldwin 		pargs_drop(oldargs);
55469be5db9SAlfred Perlstein 	if (newargs != NULL)
55569be5db9SAlfred Perlstein 		pargs_drop(newargs);
556b9df5231SPoul-Henning Kamp 
5571616db3cSJohn Dyson exec_fail_dealloc:
5581616db3cSJohn Dyson 
55926f9a767SRodney W. Grimes 	/*
56026f9a767SRodney W. Grimes 	 * free various allocated resources
56126f9a767SRodney W. Grimes 	 */
5621616db3cSJohn Dyson 	if (imgp->firstpage)
5631616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
56426f9a767SRodney W. Grimes 
565c2f9f36bSDavid Greenman 	if (imgp->stringbase != NULL)
5661616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
5671616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
5681616db3cSJohn Dyson 
5699c0fed3dSDoug Rabson 	if (imgp->vp) {
570762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
571619eb6e5SJeff Roberson 		vput(imgp->vp);
572a3cf6ebaSDavid Greenman 	}
57326f9a767SRodney W. Grimes 
5740b2ed1aeSJeff Roberson 	if (imgp->object)
5750b2ed1aeSJeff Roberson 		vm_object_deallocate(imgp->object);
5760b2ed1aeSJeff Roberson 
5771616db3cSJohn Dyson 	if (error == 0)
578116734c4SMatthew Dillon 		goto done2;
5791616db3cSJohn Dyson 
58026f9a767SRodney W. Grimes exec_fail:
5819ca45e81SDag-Erling Smørgrav 	/* we're done here, clear P_INEXEC */
5829ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
5839ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
5849ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
5859ca45e81SDag-Erling Smørgrav 
586c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
58726f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
588b40ce416SJulian Elischer 		exit1(td, W_EXITCODE(0, SIGABRT));
58926f9a767SRodney W. Grimes 		/* NOT REACHED */
590116734c4SMatthew Dillon 		error = 0;
59126f9a767SRodney W. Grimes 	}
592116734c4SMatthew Dillon done2:
593116734c4SMatthew Dillon 	mtx_unlock(&Giant);
594116734c4SMatthew Dillon 	return (error);
59526f9a767SRodney W. Grimes }
59626f9a767SRodney W. Grimes 
5971616db3cSJohn Dyson int
5981616db3cSJohn Dyson exec_map_first_page(imgp)
5991616db3cSJohn Dyson 	struct image_params *imgp;
6001616db3cSJohn Dyson {
601d8aad40cSJohn Baldwin 	int rv, i;
60295461b45SJohn Dyson 	int initial_pagein;
60395461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
6041616db3cSJohn Dyson 	vm_object_t object;
6051616db3cSJohn Dyson 
6060cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
6071616db3cSJohn Dyson 
6081616db3cSJohn Dyson 	if (imgp->firstpage) {
6091616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
6101616db3cSJohn Dyson 	}
6111616db3cSJohn Dyson 
6129ff5ce6bSBoris Popov 	VOP_GETVOBJECT(imgp->vp, &object);
6131616db3cSJohn Dyson 
61495461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
6151616db3cSJohn Dyson 
61695461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
61795461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
61895461b45SJohn Dyson 		if (initial_pagein > object->size)
61995461b45SJohn Dyson 			initial_pagein = object->size;
62095461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
621d254af07SMatthew Dillon 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
62295461b45SJohn Dyson 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
62395461b45SJohn Dyson 					break;
62495461b45SJohn Dyson 				if (ma[i]->valid)
62595461b45SJohn Dyson 					break;
626e69763a3SDoug Rabson 				vm_page_busy(ma[i]);
62795461b45SJohn Dyson 			} else {
62895461b45SJohn Dyson 				ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
62995461b45SJohn Dyson 				if (ma[i] == NULL)
63095461b45SJohn Dyson 					break;
63195461b45SJohn Dyson 			}
63295461b45SJohn Dyson 		}
63395461b45SJohn Dyson 		initial_pagein = i;
6341616db3cSJohn Dyson 
63595461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
63695461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
63795461b45SJohn Dyson 
638eed2412eSJohn Dyson 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) {
639ecbb00a2SDoug Rabson 			if (ma[0]) {
64097646f56SAlan Cox 				vm_page_lock_queues();
64195461b45SJohn Dyson 				vm_page_protect(ma[0], VM_PROT_NONE);
6428f9110f6SJohn Dyson 				vm_page_free(ma[0]);
64397646f56SAlan Cox 				vm_page_unlock_queues();
644ecbb00a2SDoug Rabson 			}
645a7cddfedSJake Burkholder 			return (EIO);
6461616db3cSJohn Dyson 		}
6471616db3cSJohn Dyson 	}
64897646f56SAlan Cox 	vm_page_lock_queues();
64995461b45SJohn Dyson 	vm_page_wire(ma[0]);
650e69763a3SDoug Rabson 	vm_page_wakeup(ma[0]);
65197646f56SAlan Cox 	vm_page_unlock_queues();
6521616db3cSJohn Dyson 
653ac59490bSJake Burkholder 	pmap_qenter((vm_offset_t)imgp->image_header, ma, 1);
65495461b45SJohn Dyson 	imgp->firstpage = ma[0];
6551616db3cSJohn Dyson 
656a7cddfedSJake Burkholder 	return (0);
6571616db3cSJohn Dyson }
6581616db3cSJohn Dyson 
6591616db3cSJohn Dyson void
6601616db3cSJohn Dyson exec_unmap_first_page(imgp)
6611616db3cSJohn Dyson 	struct image_params *imgp;
6621616db3cSJohn Dyson {
6630cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
66423955314SAlfred Perlstein 
6651616db3cSJohn Dyson 	if (imgp->firstpage) {
666ac59490bSJake Burkholder 		pmap_qremove((vm_offset_t)imgp->image_header, 1);
66797646f56SAlan Cox 		vm_page_lock_queues();
66873007561SDavid Greenman 		vm_page_unwire(imgp->firstpage, 1);
66997646f56SAlan Cox 		vm_page_unlock_queues();
6701616db3cSJohn Dyson 		imgp->firstpage = NULL;
6711616db3cSJohn Dyson 	}
6721616db3cSJohn Dyson }
6731616db3cSJohn Dyson 
67426f9a767SRodney W. Grimes /*
67526f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
67626f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
67726f9a767SRodney W. Grimes  *	automatically in trap.c.
67826f9a767SRodney W. Grimes  */
67926f9a767SRodney W. Grimes int
6803ebc1248SPeter Wemm exec_new_vmspace(imgp, minuser, maxuser, stack_addr)
681c52007c2SDavid Greenman 	struct image_params *imgp;
6823ebc1248SPeter Wemm 	vm_offset_t minuser, maxuser, stack_addr;
68326f9a767SRodney W. Grimes {
68426f9a767SRodney W. Grimes 	int error;
6856f5dafeaSAlan Cox 	struct execlist *ep;
6865e20c11fSAlan Cox 	struct proc *p = imgp->proc;
6875e20c11fSAlan Cox 	struct vmspace *vmspace = p->p_vmspace;
68826f9a767SRodney W. Grimes 
6890cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
6900cddd8f0SMatthew Dillon 
6913ebc1248SPeter Wemm 	stack_addr = stack_addr - maxssiz;
6923ebc1248SPeter Wemm 
693c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
69426f9a767SRodney W. Grimes 
695af9ec885SJohn Dyson 	/*
6966f5dafeaSAlan Cox 	 * Perform functions registered with at_exec().
6976f5dafeaSAlan Cox 	 */
6986f5dafeaSAlan Cox 	TAILQ_FOREACH(ep, &exec_list, next)
6995e20c11fSAlan Cox 		(*ep->function)(p);
7006f5dafeaSAlan Cox 
7016f5dafeaSAlan Cox 	/*
702af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
703af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
704af9ec885SJohn Dyson 	 * not disrupted
705af9ec885SJohn Dyson 	 */
7063ebc1248SPeter Wemm 	if (vmspace->vm_refcnt == 1
7073ebc1248SPeter Wemm 	    && vm_map_min(&vmspace->vm_map) == minuser
7083ebc1248SPeter Wemm 	    && vm_map_max(&vmspace->vm_map) == maxuser) {
7093d903220SDoug Rabson 		if (vmspace->vm_shm)
7105e20c11fSAlan Cox 			shmexit(p);
7113ebc1248SPeter Wemm 		pmap_remove_pages(vmspace_pmap(vmspace), minuser, maxuser);
7123ebc1248SPeter Wemm 		vm_map_remove(&vmspace->vm_map, minuser, maxuser);
713af9ec885SJohn Dyson 	} else {
7143ebc1248SPeter Wemm 		vmspace_exec(p, minuser, maxuser);
7155e20c11fSAlan Cox 		vmspace = p->p_vmspace;
716af9ec885SJohn Dyson 	}
71726f9a767SRodney W. Grimes 
71826f9a767SRodney W. Grimes 	/* Allocate a new stack */
7199a024fc5SRobert Drehmel 	error = vm_map_stack(&vmspace->vm_map, stack_addr, (vm_size_t)maxssiz,
7209a024fc5SRobert Drehmel 	    VM_PROT_ALL, VM_PROT_ALL, 0);
7212267af78SJulian Elischer 	if (error)
7222267af78SJulian Elischer 		return (error);
7232267af78SJulian Elischer 
72463c47a5cSDoug Rabson #ifdef __ia64__
72563c47a5cSDoug Rabson 	{
72663c47a5cSDoug Rabson 		/*
72763c47a5cSDoug Rabson 		 * Allocate backing store. We really need something
72863c47a5cSDoug Rabson 		 * similar to vm_map_stack which can allow the backing
72963c47a5cSDoug Rabson 		 * store to grow upwards. This will do for now.
73063c47a5cSDoug Rabson 		 */
73163c47a5cSDoug Rabson 		vm_offset_t bsaddr;
732cbc89bfbSPaul Saab 		bsaddr = USRSTACK - 2*maxssiz;
73363c47a5cSDoug Rabson 		error = vm_map_find(&vmspace->vm_map, 0, 0, &bsaddr,
734911fc923SPeter Wemm 				    regstkpages * PAGE_SIZE, 0,
73563c47a5cSDoug Rabson 				    VM_PROT_ALL, VM_PROT_ALL, 0);
7365e20c11fSAlan Cox 		FIRST_THREAD_IN_PROC(p)->td_md.md_bspstore = bsaddr;
73763c47a5cSDoug Rabson 	}
73863c47a5cSDoug Rabson #endif
73963c47a5cSDoug Rabson 
7402267af78SJulian Elischer 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
7412267af78SJulian Elischer 	 * VM_STACK case, but they are still used to monitor the size of the
7422267af78SJulian Elischer 	 * process stack so we can check the stack rlimit.
7432267af78SJulian Elischer 	 */
744cbc89bfbSPaul Saab 	vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
745cbc89bfbSPaul Saab 	vmspace->vm_maxsaddr = (char *)USRSTACK - maxssiz;
74626f9a767SRodney W. Grimes 
74726f9a767SRodney W. Grimes 	return (0);
74826f9a767SRodney W. Grimes }
74926f9a767SRodney W. Grimes 
75026f9a767SRodney W. Grimes /*
75126f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
75226f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
75326f9a767SRodney W. Grimes  */
75426f9a767SRodney W. Grimes int
755c52007c2SDavid Greenman exec_extract_strings(imgp)
756c52007c2SDavid Greenman 	struct image_params *imgp;
75726f9a767SRodney W. Grimes {
75826f9a767SRodney W. Grimes 	char	**argv, **envv;
75926f9a767SRodney W. Grimes 	char	*argp, *envp;
760ecbb00a2SDoug Rabson 	int	error;
761ecbb00a2SDoug Rabson 	size_t	length;
76226f9a767SRodney W. Grimes 
76326f9a767SRodney W. Grimes 	/*
76426f9a767SRodney W. Grimes 	 * extract arguments first
76526f9a767SRodney W. Grimes 	 */
76626f9a767SRodney W. Grimes 
767c52007c2SDavid Greenman 	argv = imgp->uap->argv;
76826f9a767SRodney W. Grimes 
7690fd1a014SDavid Greenman 	if (argv) {
770aae0aa45SBruce Evans 		argp = (caddr_t) (intptr_t) fuword(argv);
7715cf3d12cSAndrey A. Chernov 		if (argp == (caddr_t) -1)
7725cf3d12cSAndrey A. Chernov 			return (EFAULT);
7735cf3d12cSAndrey A. Chernov 		if (argp)
7745cf3d12cSAndrey A. Chernov 			argv++;
7755cf3d12cSAndrey A. Chernov 		if (imgp->argv0)
7765cf3d12cSAndrey A. Chernov 			argp = imgp->argv0;
7775cf3d12cSAndrey A. Chernov 		if (argp) {
7785cf3d12cSAndrey A. Chernov 			do {
77926f9a767SRodney W. Grimes 				if (argp == (caddr_t) -1)
78026f9a767SRodney W. Grimes 					return (EFAULT);
781c52007c2SDavid Greenman 				if ((error = copyinstr(argp, imgp->stringp,
782c52007c2SDavid Greenman 				    imgp->stringspace, &length))) {
7830fd1a014SDavid Greenman 					if (error == ENAMETOOLONG)
78426f9a767SRodney W. Grimes 						return (E2BIG);
7850fd1a014SDavid Greenman 					return (error);
7860fd1a014SDavid Greenman 				}
787c52007c2SDavid Greenman 				imgp->stringspace -= length;
788c52007c2SDavid Greenman 				imgp->stringp += length;
789c52007c2SDavid Greenman 				imgp->argc++;
790aae0aa45SBruce Evans 			} while ((argp = (caddr_t) (intptr_t) fuword(argv++)));
79126f9a767SRodney W. Grimes 		}
7920fd1a014SDavid Greenman 	}
79326f9a767SRodney W. Grimes 
794b9df5231SPoul-Henning Kamp 	imgp->endargs = imgp->stringp;
795b9df5231SPoul-Henning Kamp 
79626f9a767SRodney W. Grimes 	/*
79726f9a767SRodney W. Grimes 	 * extract environment strings
79826f9a767SRodney W. Grimes 	 */
79926f9a767SRodney W. Grimes 
800c52007c2SDavid Greenman 	envv = imgp->uap->envv;
80126f9a767SRodney W. Grimes 
8020fd1a014SDavid Greenman 	if (envv) {
803aae0aa45SBruce Evans 		while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
80426f9a767SRodney W. Grimes 			if (envp == (caddr_t) -1)
80526f9a767SRodney W. Grimes 				return (EFAULT);
806c52007c2SDavid Greenman 			if ((error = copyinstr(envp, imgp->stringp,
807c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
8080fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
80926f9a767SRodney W. Grimes 					return (E2BIG);
8100fd1a014SDavid Greenman 				return (error);
8110fd1a014SDavid Greenman 			}
812c52007c2SDavid Greenman 			imgp->stringspace -= length;
813c52007c2SDavid Greenman 			imgp->stringp += length;
814c52007c2SDavid Greenman 			imgp->envc++;
81526f9a767SRodney W. Grimes 		}
8160fd1a014SDavid Greenman 	}
81726f9a767SRodney W. Grimes 
81826f9a767SRodney W. Grimes 	return (0);
81926f9a767SRodney W. Grimes }
82026f9a767SRodney W. Grimes 
82126f9a767SRodney W. Grimes /*
82226f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
82326f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
82426f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
82526f9a767SRodney W. Grimes  */
826654f6be1SBruce Evans register_t *
827c52007c2SDavid Greenman exec_copyout_strings(imgp)
828c52007c2SDavid Greenman 	struct image_params *imgp;
82926f9a767SRodney W. Grimes {
83026f9a767SRodney W. Grimes 	int argc, envc;
83126f9a767SRodney W. Grimes 	char **vectp;
83226f9a767SRodney W. Grimes 	char *stringp, *destp;
833654f6be1SBruce Evans 	register_t *stack_base;
83493f6448cSDavid Greenman 	struct ps_strings *arginfo;
835d66a5066SPeter Wemm 	int szsigcode;
83626f9a767SRodney W. Grimes 
83726f9a767SRodney W. Grimes 	/*
83826f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
839d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
84026f9a767SRodney W. Grimes 	 */
8414c56fcdeSBruce Evans 	arginfo = (struct ps_strings *)PS_STRINGS;
842d66a5066SPeter Wemm 	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
843d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
8441ed012f9SPeter Wemm 		roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
8451ed012f9SPeter Wemm 
84626f9a767SRodney W. Grimes 	/*
847d66a5066SPeter Wemm 	 * install sigcode
848d66a5066SPeter Wemm 	 */
849d66a5066SPeter Wemm 	if (szsigcode)
850d66a5066SPeter Wemm 		copyout(imgp->proc->p_sysent->sv_sigcode,
851d66a5066SPeter Wemm 			((caddr_t)arginfo - szsigcode), szsigcode);
852d66a5066SPeter Wemm 
853d66a5066SPeter Wemm 	/*
854e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
855e1743d02SSøren Schmidt 	 * on the stack.
856e1743d02SSøren Schmidt 	 */
857b9a22da4STakanori Watanabe 	if (imgp->auxargs) {
858e1743d02SSøren Schmidt 		/*
859b9a22da4STakanori Watanabe 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
860b9a22da4STakanori Watanabe 		 * lower compatibility.
861b9a22da4STakanori Watanabe 		 */
862b9a22da4STakanori Watanabe 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
863b9a22da4STakanori Watanabe 			: (AT_COUNT * 2);
864b9a22da4STakanori Watanabe 		/*
865b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
866b9a22da4STakanori Watanabe 		 * the arg and env vector sets,and imgp->auxarg_size is room
867b9a22da4STakanori Watanabe 		 * for argument of Runtime loader.
868e1743d02SSøren Schmidt 		 */
869e1743d02SSøren Schmidt 		vectp = (char **) (destp - (imgp->argc + imgp->envc + 2 +
870b9a22da4STakanori Watanabe 				       imgp->auxarg_size) * sizeof(char *));
871b9a22da4STakanori Watanabe 
872b9a22da4STakanori Watanabe 	} else
873e1743d02SSøren Schmidt 		/*
874b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
875b9a22da4STakanori Watanabe 		 * the arg and env vector sets
87626f9a767SRodney W. Grimes 		 */
877e1743d02SSøren Schmidt 		vectp = (char **)
878e1743d02SSøren Schmidt 			(destp - (imgp->argc + imgp->envc + 2) * sizeof(char *));
87926f9a767SRodney W. Grimes 
88026f9a767SRodney W. Grimes 	/*
88126f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
88226f9a767SRodney W. Grimes 	 */
883654f6be1SBruce Evans 	stack_base = (register_t *)vectp;
88426f9a767SRodney W. Grimes 
885c52007c2SDavid Greenman 	stringp = imgp->stringbase;
886c52007c2SDavid Greenman 	argc = imgp->argc;
887c52007c2SDavid Greenman 	envc = imgp->envc;
88826f9a767SRodney W. Grimes 
88993f6448cSDavid Greenman 	/*
8903aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
89193f6448cSDavid Greenman 	 */
892c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
89393f6448cSDavid Greenman 
89493f6448cSDavid Greenman 	/*
8953aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
8963aed948bSDavid Greenman 	 */
897aae0aa45SBruce Evans 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
8983aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
8993aed948bSDavid Greenman 
9003aed948bSDavid Greenman 	/*
9013aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
90293f6448cSDavid Greenman 	 */
90326f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
904aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
9053aed948bSDavid Greenman 		while (*stringp++ != 0)
9063aed948bSDavid Greenman 			destp++;
9073aed948bSDavid Greenman 		destp++;
90826f9a767SRodney W. Grimes 	}
90926f9a767SRodney W. Grimes 
9101a6e52d0SJeroen Ruigrok van der Werven 	/* a null vector table pointer separates the argp's from the envp's */
9116ab46d52SBruce Evans 	suword(vectp++, 0);
91226f9a767SRodney W. Grimes 
913aae0aa45SBruce Evans 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
9143aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
91593f6448cSDavid Greenman 
91693f6448cSDavid Greenman 	/*
9173aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
91893f6448cSDavid Greenman 	 */
91926f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
920aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
9213aed948bSDavid Greenman 		while (*stringp++ != 0)
9223aed948bSDavid Greenman 			destp++;
9233aed948bSDavid Greenman 		destp++;
92426f9a767SRodney W. Grimes 	}
92526f9a767SRodney W. Grimes 
92626f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
9276ab46d52SBruce Evans 	suword(vectp, 0);
92826f9a767SRodney W. Grimes 
92926f9a767SRodney W. Grimes 	return (stack_base);
93026f9a767SRodney W. Grimes }
93126f9a767SRodney W. Grimes 
93226f9a767SRodney W. Grimes /*
93326f9a767SRodney W. Grimes  * Check permissions of file to execute.
934cf64863aSRobert Watson  *	Called with imgp->vp locked.
93526f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
93626f9a767SRodney W. Grimes  */
937c8a79999SPeter Wemm int
938c52007c2SDavid Greenman exec_check_permissions(imgp)
939c52007c2SDavid Greenman 	struct image_params *imgp;
94026f9a767SRodney W. Grimes {
941c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
942c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
943a854ed98SJohn Baldwin 	struct thread *td;
94426f9a767SRodney W. Grimes 	int error;
94526f9a767SRodney W. Grimes 
946a854ed98SJohn Baldwin 	td = curthread;			/* XXXKSE */
947339b79b9SRobert Watson 
948339b79b9SRobert Watson #ifdef MAC
949339b79b9SRobert Watson 	error = mac_check_vnode_exec(td->td_ucred, imgp->vp);
950339b79b9SRobert Watson 	if (error)
951339b79b9SRobert Watson 		return (error);
952339b79b9SRobert Watson #endif
953339b79b9SRobert Watson 
95426f9a767SRodney W. Grimes 	/* Get file attributes */
955a854ed98SJohn Baldwin 	error = VOP_GETATTR(vp, attr, td->td_ucred, td);
95626f9a767SRodney W. Grimes 	if (error)
95726f9a767SRodney W. Grimes 		return (error);
95826f9a767SRodney W. Grimes 
95926f9a767SRodney W. Grimes 	/*
96026f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
96126f9a767SRodney W. Grimes 	 *	file resides on.
96226f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
96326f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
96426f9a767SRodney W. Grimes 	 *	file really is executable.
96526f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
96626f9a767SRodney W. Grimes 	 */
967c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
96826f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
969a854ed98SJohn Baldwin 	    (attr->va_type != VREG))
97026f9a767SRodney W. Grimes 		return (EACCES);
97126f9a767SRodney W. Grimes 
97226f9a767SRodney W. Grimes 	/*
97326f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
97426f9a767SRodney W. Grimes 	 */
97526f9a767SRodney W. Grimes 	if (attr->va_size == 0)
97626f9a767SRodney W. Grimes 		return (ENOEXEC);
97726f9a767SRodney W. Grimes 
97826f9a767SRodney W. Grimes 	/*
97926f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
98026f9a767SRodney W. Grimes 	 */
981a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
98226f9a767SRodney W. Grimes 	if (error)
98326f9a767SRodney W. Grimes 		return (error);
98426f9a767SRodney W. Grimes 
9856d5a0a8cSDavid Greenman 	/*
9866d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
9876d5a0a8cSDavid Greenman 	 * if there are any.
9886d5a0a8cSDavid Greenman 	 */
9896d5a0a8cSDavid Greenman 	if (vp->v_writecount)
9906d5a0a8cSDavid Greenman 		return (ETXTBSY);
9916d5a0a8cSDavid Greenman 
9926d5a0a8cSDavid Greenman 	/*
9936d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
9946d5a0a8cSDavid Greenman 	 * general case).
9956d5a0a8cSDavid Greenman 	 */
996a854ed98SJohn Baldwin 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td);
99726f9a767SRodney W. Grimes 	return (error);
998df8bae1dSRodney W. Grimes }
999aa855a59SPeter Wemm 
1000aa855a59SPeter Wemm /*
1001aa855a59SPeter Wemm  * Exec handler registration
1002aa855a59SPeter Wemm  */
1003aa855a59SPeter Wemm int
1004aa855a59SPeter Wemm exec_register(execsw_arg)
1005aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
1006aa855a59SPeter Wemm {
1007aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
1008aa855a59SPeter Wemm 	int count = 2;	/* New slot and trailing NULL */
1009aa855a59SPeter Wemm 
1010aa855a59SPeter Wemm 	if (execsw)
1011aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
1012aa855a59SPeter Wemm 			count++;
1013aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1014aa855a59SPeter Wemm 	if (newexecsw == NULL)
1015a7cddfedSJake Burkholder 		return (ENOMEM);
1016aa855a59SPeter Wemm 	xs = newexecsw;
1017aa855a59SPeter Wemm 	if (execsw)
1018aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
1019aa855a59SPeter Wemm 			*xs++ = *es;
1020aa855a59SPeter Wemm 	*xs++ = execsw_arg;
1021aa855a59SPeter Wemm 	*xs = NULL;
1022aa855a59SPeter Wemm 	if (execsw)
1023aa855a59SPeter Wemm 		free(execsw, M_TEMP);
1024aa855a59SPeter Wemm 	execsw = newexecsw;
1025a7cddfedSJake Burkholder 	return (0);
1026aa855a59SPeter Wemm }
1027aa855a59SPeter Wemm 
1028aa855a59SPeter Wemm int
1029aa855a59SPeter Wemm exec_unregister(execsw_arg)
1030aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
1031aa855a59SPeter Wemm {
1032aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
1033aa855a59SPeter Wemm 	int count = 1;
1034aa855a59SPeter Wemm 
1035aa855a59SPeter Wemm 	if (execsw == NULL)
1036aa855a59SPeter Wemm 		panic("unregister with no handlers left?\n");
1037aa855a59SPeter Wemm 
1038aa855a59SPeter Wemm 	for (es = execsw; *es; es++) {
1039aa855a59SPeter Wemm 		if (*es == execsw_arg)
1040aa855a59SPeter Wemm 			break;
1041aa855a59SPeter Wemm 	}
1042aa855a59SPeter Wemm 	if (*es == NULL)
1043a7cddfedSJake Burkholder 		return (ENOENT);
1044aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
1045aa855a59SPeter Wemm 		if (*es != execsw_arg)
1046aa855a59SPeter Wemm 			count++;
1047aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1048aa855a59SPeter Wemm 	if (newexecsw == NULL)
1049a7cddfedSJake Burkholder 		return (ENOMEM);
1050aa855a59SPeter Wemm 	xs = newexecsw;
1051aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
1052aa855a59SPeter Wemm 		if (*es != execsw_arg)
1053aa855a59SPeter Wemm 			*xs++ = *es;
1054aa855a59SPeter Wemm 	*xs = NULL;
1055aa855a59SPeter Wemm 	if (execsw)
1056aa855a59SPeter Wemm 		free(execsw, M_TEMP);
1057aa855a59SPeter Wemm 	execsw = newexecsw;
1058a7cddfedSJake Burkholder 	return (0);
1059aa855a59SPeter Wemm }
106021d56e9cSAlfred Perlstein 
106121d56e9cSAlfred Perlstein int
106221d56e9cSAlfred Perlstein at_exec(function)
106321d56e9cSAlfred Perlstein 	execlist_fn function;
106421d56e9cSAlfred Perlstein {
106521d56e9cSAlfred Perlstein 	struct execlist *ep;
106621d56e9cSAlfred Perlstein 
106721d56e9cSAlfred Perlstein #ifdef INVARIANTS
106821d56e9cSAlfred Perlstein 	/* Be noisy if the programmer has lost track of things */
106921d56e9cSAlfred Perlstein 	if (rm_at_exec(function))
107021d56e9cSAlfred Perlstein 		printf("WARNING: exec callout entry (%p) already present\n",
107121d56e9cSAlfred Perlstein 		    function);
107221d56e9cSAlfred Perlstein #endif
107321d56e9cSAlfred Perlstein 	ep = malloc(sizeof(*ep), M_ATEXEC, M_NOWAIT);
107421d56e9cSAlfred Perlstein 	if (ep == NULL)
107521d56e9cSAlfred Perlstein 		return (ENOMEM);
107621d56e9cSAlfred Perlstein 	ep->function = function;
107721d56e9cSAlfred Perlstein 	TAILQ_INSERT_TAIL(&exec_list, ep, next);
107821d56e9cSAlfred Perlstein 	return (0);
107921d56e9cSAlfred Perlstein }
108021d56e9cSAlfred Perlstein 
108121d56e9cSAlfred Perlstein /*
108221d56e9cSAlfred Perlstein  * Scan the exec callout list for the given item and remove it.
108321d56e9cSAlfred Perlstein  * Returns the number of items removed (0 or 1)
108421d56e9cSAlfred Perlstein  */
108521d56e9cSAlfred Perlstein int
108621d56e9cSAlfred Perlstein rm_at_exec(function)
108721d56e9cSAlfred Perlstein 	execlist_fn function;
108821d56e9cSAlfred Perlstein {
108921d56e9cSAlfred Perlstein 	struct execlist *ep;
109021d56e9cSAlfred Perlstein 
109121d56e9cSAlfred Perlstein 	TAILQ_FOREACH(ep, &exec_list, next) {
109221d56e9cSAlfred Perlstein 		if (ep->function == function) {
109321d56e9cSAlfred Perlstein 			TAILQ_REMOVE(&exec_list, ep, next);
109421d56e9cSAlfred Perlstein 			free(ep, M_ATEXEC);
109521d56e9cSAlfred Perlstein 			return (1);
109621d56e9cSAlfred Perlstein 		}
109721d56e9cSAlfred Perlstein 	}
109821d56e9cSAlfred Perlstein 	return (0);
109921d56e9cSAlfred Perlstein }
110021d56e9cSAlfred Perlstein 
1101