xref: /freebsd/sys/kern/kern_exec.c (revision f3bec5d7469aef003ff633680e9caa7d7daa9a63)
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;
92ca0387efSJake Burkholder SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings,
93ca0387efSJake Burkholder     0, "");
94486bddb0SDoug Rabson 
959701cd40SJohn Baldwin /* XXX This should be vm_size_t. */
969701cd40SJohn Baldwin static u_long usrstack = USRSTACK;
979701cd40SJohn Baldwin SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, "");
9899ac3bc8SPeter Wemm 
99b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16;
100c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
1019701cd40SJohn Baldwin     &ps_arg_cache_limit, 0, "");
102b9df5231SPoul-Henning Kamp 
103a8704f89SPoul-Henning Kamp int ps_argsopen = 1;
104a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, "");
105a8704f89SPoul-Henning Kamp 
106911fc923SPeter Wemm #ifdef __ia64__
107911fc923SPeter Wemm /* XXX HACK */
108911fc923SPeter Wemm static int regstkpages = 256;
109911fc923SPeter Wemm SYSCTL_INT(_machdep, OID_AUTO, regstkpages, CTLFLAG_RW, &regstkpages, 0, "");
110911fc923SPeter Wemm #endif
111911fc923SPeter Wemm 
11299ac3bc8SPeter Wemm /*
113aa855a59SPeter Wemm  * Each of the items is a pointer to a `const struct execsw', hence the
114aa855a59SPeter Wemm  * double pointer here.
115df8bae1dSRodney W. Grimes  */
116aa855a59SPeter Wemm static const struct execsw **execsw;
11726f9a767SRodney W. Grimes 
118d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
119ad7507e2SSteven Wallace struct execve_args {
120ad7507e2SSteven Wallace         char    *fname;
121ad7507e2SSteven Wallace         char    **argv;
122ad7507e2SSteven Wallace         char    **envv;
123ad7507e2SSteven Wallace };
124d2d3e875SBruce Evans #endif
125ad7507e2SSteven Wallace 
12626f9a767SRodney W. Grimes /*
12726f9a767SRodney W. Grimes  * execve() system call.
128116734c4SMatthew Dillon  *
129116734c4SMatthew Dillon  * MPSAFE
13026f9a767SRodney W. Grimes  */
13126f9a767SRodney W. Grimes int
132b40ce416SJulian Elischer execve(td, uap)
133b40ce416SJulian Elischer 	struct thread *td;
13426f9a767SRodney W. Grimes 	register struct execve_args *uap;
135df8bae1dSRodney W. Grimes {
136b40ce416SJulian Elischer 	struct proc *p = td->td_proc;
13726f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
1389b3b1c5fSJohn Baldwin 	struct ucred *newcred = NULL, *oldcred;
1391419eacbSAlfred Perlstein 	struct uidinfo *euip;
140654f6be1SBruce Evans 	register_t *stack_base;
141bb56ec4aSPoul-Henning Kamp 	int error, len, i;
142c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
14326f9a767SRodney W. Grimes 	struct vattr attr;
1444d77a549SAlfred Perlstein 	int (*img_first)(struct image_params *);
14569be5db9SAlfred Perlstein 	struct pargs *oldargs = NULL, *newargs = NULL;
1469b3b1c5fSJohn Baldwin 	struct procsig *oldprocsig, *newprocsig;
1476c84de02SJohn Baldwin #ifdef KTRACE
1486c84de02SJohn Baldwin 	struct vnode *tracevp = NULL;
1496c84de02SJohn Baldwin #endif
1506c84de02SJohn Baldwin 	struct vnode *textvp = NULL;
151d06c0d4dSRobert Watson 	int credential_changing;
152619eb6e5SJeff Roberson 	int textset;
15326f9a767SRodney W. Grimes 
154c52007c2SDavid Greenman 	imgp = &image_params;
155df8bae1dSRodney W. Grimes 
1569ca45e81SDag-Erling Smørgrav 	/*
1579ca45e81SDag-Erling Smørgrav 	 * Lock the process and set the P_INEXEC flag to indicate that
1589ca45e81SDag-Erling Smørgrav 	 * it should be left alone until we're done here.  This is
1599ca45e81SDag-Erling Smørgrav 	 * necessary to avoid race conditions - e.g. in ptrace() -
1609ca45e81SDag-Erling Smørgrav 	 * that might allow a local user to illicitly obtain elevated
1619ca45e81SDag-Erling Smørgrav 	 * privileges.
1629ca45e81SDag-Erling Smørgrav 	 */
1639ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
1649ca45e81SDag-Erling Smørgrav 	KASSERT((p->p_flag & P_INEXEC) == 0,
16591f91617SDavid E. O'Brien 	    ("%s(): process already has P_INEXEC flag", __func__));
16649539972SJulian Elischer 	if (p->p_flag & P_KSES) {
16749539972SJulian Elischer 		if (thread_single(SNGLE_EXIT)) {
168e602ba25SJulian Elischer 			PROC_UNLOCK(p);
169e602ba25SJulian Elischer 			return (ERESTART);	/* Try again later. */
170e602ba25SJulian Elischer 		}
17149539972SJulian Elischer 		/*
17249539972SJulian Elischer 		 * If we get here all other threads are dead,
17349539972SJulian Elischer 		 * so unset the associated flags and lose KSE mode.
17449539972SJulian Elischer 		 */
17549539972SJulian Elischer 		p->p_flag &= ~P_KSES;
17649539972SJulian Elischer 		td->td_flags &= ~TDF_UNBOUND;
17749539972SJulian Elischer 		thread_single_end();
17849539972SJulian Elischer 	}
1799ca45e81SDag-Erling Smørgrav 	p->p_flag |= P_INEXEC;
1809ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
1819ca45e81SDag-Erling Smørgrav 
182df8bae1dSRodney W. Grimes 	/*
183c52007c2SDavid Greenman 	 * Initialize part of the common data
184df8bae1dSRodney W. Grimes 	 */
185c52007c2SDavid Greenman 	imgp->proc = p;
186c52007c2SDavid Greenman 	imgp->uap = uap;
187c52007c2SDavid Greenman 	imgp->attr = &attr;
188c52007c2SDavid Greenman 	imgp->argc = imgp->envc = 0;
1895cf3d12cSAndrey A. Chernov 	imgp->argv0 = NULL;
190c52007c2SDavid Greenman 	imgp->entry_addr = 0;
191c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
192c52007c2SDavid Greenman 	imgp->interpreted = 0;
193c52007c2SDavid Greenman 	imgp->interpreter_name[0] = '\0';
194e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1951616db3cSJohn Dyson 	imgp->vp = NULL;
1960b2ed1aeSJeff Roberson 	imgp->object = NULL;
1971616db3cSJohn Dyson 	imgp->firstpage = NULL;
1984fe88fe6SJohn Polstra 	imgp->ps_strings = 0;
199b9a22da4STakanori Watanabe 	imgp->auxarg_size = 0;
20026f9a767SRodney W. Grimes 
20126f9a767SRodney W. Grimes 	/*
20226f9a767SRodney W. Grimes 	 * Allocate temporary demand zeroed space for argument and
20326f9a767SRodney W. Grimes 	 *	environment strings
20426f9a767SRodney W. Grimes 	 */
205ca0387efSJake Burkholder 	imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX +
206ca0387efSJake Burkholder 	    PAGE_SIZE);
207c2f9f36bSDavid Greenman 	if (imgp->stringbase == NULL) {
20826f9a767SRodney W. Grimes 		error = ENOMEM;
209b3afd20dSAlan Cox 		mtx_lock(&Giant);
21026f9a767SRodney W. Grimes 		goto exec_fail;
21126f9a767SRodney W. Grimes 	}
212c52007c2SDavid Greenman 	imgp->stringp = imgp->stringbase;
213c52007c2SDavid Greenman 	imgp->stringspace = ARG_MAX;
2141616db3cSJohn Dyson 	imgp->image_header = imgp->stringbase + ARG_MAX;
21526f9a767SRodney W. Grimes 
21626f9a767SRodney W. Grimes 	/*
21726f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
21826f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
21926f9a767SRodney W. Grimes 	 */
22026f9a767SRodney W. Grimes 	ndp = &nd;
221b35ba931SDavid Greenman 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
222b40ce416SJulian Elischer 	    UIO_USERSPACE, uap->fname, td);
22326f9a767SRodney W. Grimes 
224b3afd20dSAlan Cox 	mtx_lock(&Giant);
22526f9a767SRodney W. Grimes interpret:
22626f9a767SRodney W. Grimes 
22726f9a767SRodney W. Grimes 	error = namei(ndp);
22826f9a767SRodney W. Grimes 	if (error) {
2291616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
2301616db3cSJohn Dyson 		    ARG_MAX + PAGE_SIZE);
23126f9a767SRodney W. Grimes 		goto exec_fail;
23226f9a767SRodney W. Grimes 	}
23326f9a767SRodney W. Grimes 
234c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
2359c0fed3dSDoug Rabson 	imgp->fname = uap->fname;
23626f9a767SRodney W. Grimes 
23726f9a767SRodney W. Grimes 	/*
2389022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
2399022e4adSDavid Greenman 	 */
240c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
241619eb6e5SJeff Roberson 	if (error)
24226f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
243619eb6e5SJeff Roberson 
244619eb6e5SJeff Roberson 	if (VOP_GETVOBJECT(imgp->vp, &imgp->object) == 0)
2450b2ed1aeSJeff Roberson 		vm_object_reference(imgp->object);
24626f9a767SRodney W. Grimes 
247619eb6e5SJeff Roberson 	/*
248619eb6e5SJeff Roberson 	 * Set VV_TEXT now so no one can write to the executable while we're
249619eb6e5SJeff Roberson 	 * activating it.
250619eb6e5SJeff Roberson 	 *
251619eb6e5SJeff Roberson 	 * Remember if this was set before and unset it in case this is not
252619eb6e5SJeff Roberson 	 * actually an executable image.
253619eb6e5SJeff Roberson 	 */
254619eb6e5SJeff Roberson 	textset = imgp->vp->v_vflag & VV_TEXT;
255619eb6e5SJeff Roberson 	imgp->vp->v_vflag |= VV_TEXT;
256619eb6e5SJeff Roberson 
2571616db3cSJohn Dyson 	error = exec_map_first_page(imgp);
2586d5a0a8cSDavid Greenman 	if (error)
25926f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
26026f9a767SRodney W. Grimes 
26126f9a767SRodney W. Grimes 	/*
262d323ddf3SMatthew Dillon 	 *	If the current process has a special image activator it
263d323ddf3SMatthew Dillon 	 *	wants to try first, call it.   For example, emulating shell
264d323ddf3SMatthew Dillon 	 *	scripts differently.
26526f9a767SRodney W. Grimes 	 */
266d323ddf3SMatthew Dillon 	error = -1;
267d323ddf3SMatthew Dillon 	if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
268d323ddf3SMatthew Dillon 		error = img_first(imgp);
269d323ddf3SMatthew Dillon 
270d323ddf3SMatthew Dillon 	/*
271d323ddf3SMatthew Dillon 	 *	Loop through the list of image activators, calling each one.
272d323ddf3SMatthew Dillon 	 *	An activator returns -1 if there is no match, 0 on success,
273d323ddf3SMatthew Dillon 	 *	and an error otherwise.
274d323ddf3SMatthew Dillon 	 */
275d323ddf3SMatthew Dillon 	for (i = 0; error == -1 && execsw[i]; ++i) {
276d323ddf3SMatthew Dillon 		if (execsw[i]->ex_imgact == NULL ||
277d323ddf3SMatthew Dillon 		    execsw[i]->ex_imgact == img_first) {
278d323ddf3SMatthew Dillon 			continue;
279d323ddf3SMatthew Dillon 		}
280c52007c2SDavid Greenman 		error = (*execsw[i]->ex_imgact)(imgp);
281d323ddf3SMatthew Dillon 	}
282d323ddf3SMatthew Dillon 
283d323ddf3SMatthew Dillon 	if (error) {
284619eb6e5SJeff Roberson 		if (error == -1) {
285619eb6e5SJeff Roberson 			if (textset == 0)
286619eb6e5SJeff Roberson 				imgp->vp->v_vflag &= ~VV_TEXT;
287d323ddf3SMatthew Dillon 			error = ENOEXEC;
288619eb6e5SJeff Roberson 		}
28926f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
290d323ddf3SMatthew Dillon 	}
291d323ddf3SMatthew Dillon 
292d323ddf3SMatthew Dillon 	/*
293d323ddf3SMatthew Dillon 	 * Special interpreter operation, cleanup and loop up to try to
294d323ddf3SMatthew Dillon 	 * activate the interpreter.
295d323ddf3SMatthew Dillon 	 */
296c52007c2SDavid Greenman 	if (imgp->interpreted) {
2971616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
298619eb6e5SJeff Roberson 		/*
299619eb6e5SJeff Roberson 		 * VV_TEXT needs to be unset for scripts.  There is a short
300619eb6e5SJeff Roberson 		 * period before we determine that something is a script where
301619eb6e5SJeff Roberson 		 * VV_TEXT will be set. The vnode lock is held over this
302619eb6e5SJeff Roberson 		 * entire period so nothing should illegitimately be blocked.
303619eb6e5SJeff Roberson 		 */
304619eb6e5SJeff Roberson 		imgp->vp->v_vflag &= ~VV_TEXT;
305762e6b85SEivind Eklund 		/* free name buffer and old vnode */
306762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
307619eb6e5SJeff Roberson 		vput(ndp->ni_vp);
3080b2ed1aeSJeff Roberson 		vm_object_deallocate(imgp->object);
3090b2ed1aeSJeff Roberson 		imgp->object = NULL;
31026f9a767SRodney W. Grimes 		/* set new name to that of the interpreter */
311b35ba931SDavid Greenman 		NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
312b40ce416SJulian Elischer 		    UIO_SYSSPACE, imgp->interpreter_name, td);
31326f9a767SRodney W. Grimes 		goto interpret;
31426f9a767SRodney W. Grimes 	}
31526f9a767SRodney W. Grimes 
31626f9a767SRodney W. Grimes 	/*
31726f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
31826f9a767SRodney W. Grimes 	 */
3193ebc1248SPeter Wemm 	if (p->p_sysent->sv_copyout_strings)
3203ebc1248SPeter Wemm 		stack_base = (*p->p_sysent->sv_copyout_strings)(imgp);
3213ebc1248SPeter Wemm 	else
322c52007c2SDavid Greenman 		stack_base = exec_copyout_strings(imgp);
32326f9a767SRodney W. Grimes 
32426f9a767SRodney W. Grimes 	/*
3251e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
3261e1e0b44SSøren Schmidt 	 * let it do the stack setup.
3271e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
32826f9a767SRodney W. Grimes 	 */
3291e1e0b44SSøren Schmidt 	if (p->p_sysent->sv_fixup)
330c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
3311e1e0b44SSøren Schmidt 	else
332c52007c2SDavid Greenman 		suword(--stack_base, imgp->argc);
33326f9a767SRodney W. Grimes 
334a78e8d2aSDavid Greenman 	/*
335a78e8d2aSDavid Greenman 	 * For security and other reasons, the file descriptor table cannot
336a78e8d2aSDavid Greenman 	 * be shared after an exec.
337a78e8d2aSDavid Greenman 	 */
338426da3bcSAlfred Perlstein 	FILEDESC_LOCK(p->p_fd);
339a78e8d2aSDavid Greenman 	if (p->p_fd->fd_refcnt > 1) {
340a78e8d2aSDavid Greenman 		struct filedesc *tmp;
341a78e8d2aSDavid Greenman 
342b40ce416SJulian Elischer 		tmp = fdcopy(td);
343426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(p->p_fd);
344b40ce416SJulian Elischer 		fdfree(td);
345a78e8d2aSDavid Greenman 		p->p_fd = tmp;
346426da3bcSAlfred Perlstein 	} else
347426da3bcSAlfred Perlstein 		FILEDESC_UNLOCK(p->p_fd);
348a78e8d2aSDavid Greenman 
349333ea485SGuido van Rooij 	/*
3509b3b1c5fSJohn Baldwin 	 * Malloc things before we need locks.
3519b3b1c5fSJohn Baldwin 	 */
3529b3b1c5fSJohn Baldwin 	newcred = crget();
3531419eacbSAlfred Perlstein 	euip = uifind(attr.va_uid);
3549b3b1c5fSJohn Baldwin 	i = imgp->endargs - imgp->stringbase;
3559b3b1c5fSJohn Baldwin 	if (ps_arg_cache_limit >= i + sizeof(struct pargs))
3569b3b1c5fSJohn Baldwin 		newargs = pargs_alloc(i);
3579b3b1c5fSJohn Baldwin 
3589b3b1c5fSJohn Baldwin 	/* close files on exec */
3599b3b1c5fSJohn Baldwin 	fdcloseexec(td);
3609b3b1c5fSJohn Baldwin 
361619eb6e5SJeff Roberson 	/* Get a reference to the vnode prior to locking the proc */
362619eb6e5SJeff Roberson 	VREF(ndp->ni_vp);
363619eb6e5SJeff Roberson 
3649b3b1c5fSJohn Baldwin 	/*
365333ea485SGuido van Rooij 	 * For security and other reasons, signal handlers cannot
3668688bb93SJohn Baldwin 	 * be shared after an exec. The new process gets a copy of the old
367b2c3fa70SDima Dorfman 	 * handlers. In execsigs(), the new process will have its signals
368333ea485SGuido van Rooij 	 * reset.
369333ea485SGuido van Rooij 	 */
3709b3b1c5fSJohn Baldwin 	PROC_LOCK(p);
3719b3b1c5fSJohn Baldwin 	mp_fixme("procsig needs a lock");
372333ea485SGuido van Rooij 	if (p->p_procsig->ps_refcnt > 1) {
3739b3b1c5fSJohn Baldwin 		oldprocsig = p->p_procsig;
3749b3b1c5fSJohn Baldwin 		PROC_UNLOCK(p);
375333ea485SGuido van Rooij 		MALLOC(newprocsig, struct procsig *, sizeof(struct procsig),
376333ea485SGuido van Rooij 		    M_SUBPROC, M_WAITOK);
3779b3b1c5fSJohn Baldwin 		bcopy(oldprocsig, newprocsig, sizeof(*newprocsig));
3789b3b1c5fSJohn Baldwin 		newprocsig->ps_refcnt = 1;
3799b3b1c5fSJohn Baldwin 		oldprocsig->ps_refcnt--;
3809b3b1c5fSJohn Baldwin 		PROC_LOCK(p);
381333ea485SGuido van Rooij 		p->p_procsig = newprocsig;
382b40ce416SJulian Elischer 		if (p->p_sigacts == &p->p_uarea->u_sigacts)
383b2c3fa70SDima Dorfman 			panic("shared procsig but private sigacts?");
384333ea485SGuido van Rooij 
385b40ce416SJulian Elischer 		p->p_uarea->u_sigacts = *p->p_sigacts;
386b40ce416SJulian Elischer 		p->p_sigacts = &p->p_uarea->u_sigacts;
387333ea485SGuido van Rooij 	}
388fdf4e8b3SWarner Losh 	/* Stop profiling */
389fdf4e8b3SWarner Losh 	stopprofclock(p);
390fdf4e8b3SWarner Losh 
39126f9a767SRodney W. Grimes 	/* reset caught signals */
39226f9a767SRodney W. Grimes 	execsigs(p);
39326f9a767SRodney W. Grimes 
39426f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
39526f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
39626f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
39726f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
39826f9a767SRodney W. Grimes 
39926f9a767SRodney W. Grimes 	/*
40024b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
401dc733423SDag-Erling Smørgrav 	 * it that it now has its own resources back
40226f9a767SRodney W. Grimes 	 */
40326f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
40426f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
40526f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
4067f05b035SAlfred Perlstein 		wakeup(p->p_pptr);
40726f9a767SRodney W. Grimes 	}
40826f9a767SRodney W. Grimes 
40926f9a767SRodney W. Grimes 	/*
410a78e8d2aSDavid Greenman 	 * Implement image setuid/setgid.
411a78e8d2aSDavid Greenman 	 *
412a78e8d2aSDavid Greenman 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
413a78e8d2aSDavid Greenman 	 * the process is being traced.
414c52007c2SDavid Greenman 	 */
415b1fc0ec1SRobert Watson 	oldcred = p->p_ucred;
416d06c0d4dSRobert Watson 	credential_changing = 0;
417d06c0d4dSRobert Watson 	credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid !=
418d06c0d4dSRobert Watson 	    attr.va_uid;
419d06c0d4dSRobert Watson 	credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid !=
420d06c0d4dSRobert Watson 	    attr.va_gid;
421d06c0d4dSRobert Watson 
422d06c0d4dSRobert Watson 	if (credential_changing &&
423a78e8d2aSDavid Greenman 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
424c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
425c52007c2SDavid Greenman 		/*
426c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
427b85db196SPeter Wemm 		 * root.  Record any set-id flags first to make sure that
428b85db196SPeter Wemm 		 * we do not regain any tracing during a possible block.
42926f9a767SRodney W. Grimes 		 */
430b85db196SPeter Wemm 		setsugid(p);
4316c84de02SJohn Baldwin #ifdef KTRACE
43244731cabSJohn Baldwin 		if (p->p_tracep && suser_cred(oldcred, PRISON_ROOT)) {
4336c84de02SJohn Baldwin 			mtx_lock(&ktrace_mtx);
43479deba82SMatthew Dillon 			p->p_traceflag = 0;
4359b3b1c5fSJohn Baldwin 			tracevp = p->p_tracep;
4369b3b1c5fSJohn Baldwin 			p->p_tracep = NULL;
4376c84de02SJohn Baldwin 			mtx_unlock(&ktrace_mtx);
43826f9a767SRodney W. Grimes 		}
4396c84de02SJohn Baldwin #endif
44089ab9307SJacques Vidrine 		/* Close any file descriptors 0..2 that reference procfs */
44189ab9307SJacques Vidrine 		setugidsafety(td);
442e983a376SJacques Vidrine 		/* Make sure file descriptors 0..2 are in use.  */
443e983a376SJacques Vidrine 		error = fdcheckstd(td);
44463c9e754SJohn Baldwin 		if (error != 0)
44569be5db9SAlfred Perlstein 			goto done1;
446c52007c2SDavid Greenman 		/*
447c52007c2SDavid Greenman 		 * Set the new credentials.
448c52007c2SDavid Greenman 		 */
4499b3b1c5fSJohn Baldwin 		crcopy(newcred, oldcred);
450c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
4511419eacbSAlfred Perlstein 			change_euid(newcred, euip);
452c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
453b1fc0ec1SRobert Watson 			change_egid(newcred, attr.va_gid);
4549b3b1c5fSJohn Baldwin 		/*
4559b3b1c5fSJohn Baldwin 		 * Implement correct POSIX saved-id behavior.
4569b3b1c5fSJohn Baldwin 		 */
4579b3b1c5fSJohn Baldwin 		change_svuid(newcred, newcred->cr_uid);
4589b3b1c5fSJohn Baldwin 		change_svgid(newcred, newcred->cr_gid);
4599b3b1c5fSJohn Baldwin 		p->p_ucred = newcred;
4609b3b1c5fSJohn Baldwin 		newcred = NULL;
461c52007c2SDavid Greenman 	} else {
462b1fc0ec1SRobert Watson 		if (oldcred->cr_uid == oldcred->cr_ruid &&
463b1fc0ec1SRobert Watson 		    oldcred->cr_gid == oldcred->cr_rgid)
464c52007c2SDavid Greenman 			p->p_flag &= ~P_SUGID;
46526f9a767SRodney W. Grimes 		/*
466c52007c2SDavid Greenman 		 * Implement correct POSIX saved-id behavior.
467b1fc0ec1SRobert Watson 		 *
468b1fc0ec1SRobert Watson 		 * XXX: It's not clear that the existing behavior is
4699b3b1c5fSJohn Baldwin 		 * POSIX-compliant.  A number of sources indicate that the
4709b3b1c5fSJohn Baldwin 		 * saved uid/gid should only be updated if the new ruid is
4719b3b1c5fSJohn Baldwin 		 * not equal to the old ruid, or the new euid is not equal
4729b3b1c5fSJohn Baldwin 		 * to the old euid and the new euid is not equal to the old
4739b3b1c5fSJohn Baldwin 		 * ruid.  The FreeBSD code always updates the saved uid/gid.
4749b3b1c5fSJohn Baldwin 		 * Also, this code uses the new (replaced) euid and egid as
4759b3b1c5fSJohn Baldwin 		 * the source, which may or may not be the right ones to use.
47626f9a767SRodney W. Grimes 		 */
477b1fc0ec1SRobert Watson 		if (oldcred->cr_svuid != oldcred->cr_uid ||
478b1fc0ec1SRobert Watson 		    oldcred->cr_svgid != oldcred->cr_gid) {
4799b3b1c5fSJohn Baldwin 			crcopy(newcred, oldcred);
480b1fc0ec1SRobert Watson 			change_svuid(newcred, newcred->cr_uid);
481b1fc0ec1SRobert Watson 			change_svgid(newcred, newcred->cr_gid);
482b1fc0ec1SRobert Watson 			p->p_ucred = newcred;
4839b3b1c5fSJohn Baldwin 			newcred = NULL;
4849b3b1c5fSJohn Baldwin 		}
485b1fc0ec1SRobert Watson 	}
48626f9a767SRodney W. Grimes 
48726f9a767SRodney W. Grimes 	/*
488619eb6e5SJeff Roberson 	 * Store the vp for use in procfs.  This vnode was referenced prior
489619eb6e5SJeff Roberson 	 * to locking the proc lock.
4902a531c80SDavid Greenman 	 */
4919b3b1c5fSJohn Baldwin 	textvp = p->p_textvp;
4922a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
4932a531c80SDavid Greenman 
4942a531c80SDavid Greenman 	/*
4959ca45e81SDag-Erling Smørgrav 	 * Notify others that we exec'd, and clear the P_INEXEC flag
4969ca45e81SDag-Erling Smørgrav 	 * as we're now a bona fide freshly-execed process.
497cb679c38SJonathan Lemon 	 */
498cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_EXEC);
4999ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
500cb679c38SJonathan Lemon 
501cb679c38SJonathan Lemon 	/*
50226f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
50326f9a767SRodney W. Grimes 	 * can be set before the program executes.
50426f9a767SRodney W. Grimes 	 */
505e65897c3SJohn Baldwin 	_STOPEVENT(p, S_EXEC, 0);
5062a024a2bSSean Eric Fagan 
50726f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
50826f9a767SRodney W. Grimes 		psignal(p, SIGTRAP);
50926f9a767SRodney W. Grimes 
51026f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
51126f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
51226f9a767SRodney W. Grimes 
513b9df5231SPoul-Henning Kamp 	/* Free any previous argument cache */
5149b3b1c5fSJohn Baldwin 	oldargs = p->p_args;
515b9df5231SPoul-Henning Kamp 	p->p_args = NULL;
516b9df5231SPoul-Henning Kamp 
517e913ca22SDoug Rabson 	/* Set values passed into the program in registers. */
5183ebc1248SPeter Wemm 	if (p->p_sysent->sv_setregs)
5193ebc1248SPeter Wemm 		(*p->p_sysent->sv_setregs)(td, imgp->entry_addr,
5203ebc1248SPeter Wemm 		    (u_long)(uintptr_t)stack_base, imgp->ps_strings);
5213ebc1248SPeter Wemm 	else
522e913ca22SDoug Rabson 		setregs(td, imgp->entry_addr, (u_long)(uintptr_t)stack_base,
523e913ca22SDoug Rabson 		    imgp->ps_strings);
524e913ca22SDoug Rabson 
525b9df5231SPoul-Henning Kamp 	/* Cache arguments if they fit inside our allowance */
526b9df5231SPoul-Henning Kamp 	if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
5279b3b1c5fSJohn Baldwin 		bcopy(imgp->stringbase, newargs->ar_args, i);
5289b3b1c5fSJohn Baldwin 		p->p_args = newargs;
5299b3b1c5fSJohn Baldwin 		newargs = NULL;
530fbd26f75SJohn Baldwin 	}
53169be5db9SAlfred Perlstein done1:
5329b3b1c5fSJohn Baldwin 	PROC_UNLOCK(p);
5339b3b1c5fSJohn Baldwin 
534619eb6e5SJeff Roberson 
5359b3b1c5fSJohn Baldwin 	/*
5369b3b1c5fSJohn Baldwin 	 * Free any resources malloc'd earlier that we didn't use.
5379b3b1c5fSJohn Baldwin 	 */
5381419eacbSAlfred Perlstein 	uifree(euip);
5399b3b1c5fSJohn Baldwin 	if (newcred == NULL)
5409b3b1c5fSJohn Baldwin 		crfree(oldcred);
5419b3b1c5fSJohn Baldwin 	else
5429b3b1c5fSJohn Baldwin 		crfree(newcred);
5439b3b1c5fSJohn Baldwin 	/*
5449b3b1c5fSJohn Baldwin 	 * Handle deferred decrement of ref counts.
5459b3b1c5fSJohn Baldwin 	 */
5469b3b1c5fSJohn Baldwin 	if (textvp != NULL)
5479b3b1c5fSJohn Baldwin 		vrele(textvp);
548619eb6e5SJeff Roberson 	if (ndp->ni_vp && error != 0)
549619eb6e5SJeff Roberson 		vrele(ndp->ni_vp);
5506c84de02SJohn Baldwin #ifdef KTRACE
5519b3b1c5fSJohn Baldwin 	if (tracevp != NULL)
5529b3b1c5fSJohn Baldwin 		vrele(tracevp);
5536c84de02SJohn Baldwin #endif
55469be5db9SAlfred Perlstein 	if (oldargs != NULL)
5559b3b1c5fSJohn Baldwin 		pargs_drop(oldargs);
55669be5db9SAlfred Perlstein 	if (newargs != NULL)
55769be5db9SAlfred Perlstein 		pargs_drop(newargs);
558b9df5231SPoul-Henning Kamp 
5591616db3cSJohn Dyson exec_fail_dealloc:
5601616db3cSJohn Dyson 
56126f9a767SRodney W. Grimes 	/*
56226f9a767SRodney W. Grimes 	 * free various allocated resources
56326f9a767SRodney W. Grimes 	 */
5641616db3cSJohn Dyson 	if (imgp->firstpage)
5651616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
56626f9a767SRodney W. Grimes 
567c2f9f36bSDavid Greenman 	if (imgp->stringbase != NULL)
5681616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
5691616db3cSJohn Dyson 		    ARG_MAX + PAGE_SIZE);
5701616db3cSJohn Dyson 
5719c0fed3dSDoug Rabson 	if (imgp->vp) {
572762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
573619eb6e5SJeff Roberson 		vput(imgp->vp);
574a3cf6ebaSDavid Greenman 	}
57526f9a767SRodney W. Grimes 
5760b2ed1aeSJeff Roberson 	if (imgp->object)
5770b2ed1aeSJeff Roberson 		vm_object_deallocate(imgp->object);
5780b2ed1aeSJeff Roberson 
5791616db3cSJohn Dyson 	if (error == 0)
580116734c4SMatthew Dillon 		goto done2;
5811616db3cSJohn Dyson 
58226f9a767SRodney W. Grimes exec_fail:
5839ca45e81SDag-Erling Smørgrav 	/* we're done here, clear P_INEXEC */
5849ca45e81SDag-Erling Smørgrav 	PROC_LOCK(p);
5859ca45e81SDag-Erling Smørgrav 	p->p_flag &= ~P_INEXEC;
5869ca45e81SDag-Erling Smørgrav 	PROC_UNLOCK(p);
5879ca45e81SDag-Erling Smørgrav 
588c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
58926f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
590b40ce416SJulian Elischer 		exit1(td, W_EXITCODE(0, SIGABRT));
59126f9a767SRodney W. Grimes 		/* NOT REACHED */
592116734c4SMatthew Dillon 		error = 0;
59326f9a767SRodney W. Grimes 	}
594116734c4SMatthew Dillon done2:
595116734c4SMatthew Dillon 	mtx_unlock(&Giant);
596116734c4SMatthew Dillon 	return (error);
59726f9a767SRodney W. Grimes }
59826f9a767SRodney W. Grimes 
5991616db3cSJohn Dyson int
6001616db3cSJohn Dyson exec_map_first_page(imgp)
6011616db3cSJohn Dyson 	struct image_params *imgp;
6021616db3cSJohn Dyson {
603d8aad40cSJohn Baldwin 	int rv, i;
60495461b45SJohn Dyson 	int initial_pagein;
60595461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
6061616db3cSJohn Dyson 	vm_object_t object;
6071616db3cSJohn Dyson 
6080cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
6091616db3cSJohn Dyson 
6101616db3cSJohn Dyson 	if (imgp->firstpage) {
6111616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
6121616db3cSJohn Dyson 	}
6131616db3cSJohn Dyson 
6149ff5ce6bSBoris Popov 	VOP_GETVOBJECT(imgp->vp, &object);
6151616db3cSJohn Dyson 
61695461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
6171616db3cSJohn Dyson 
61895461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
61995461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
62095461b45SJohn Dyson 		if (initial_pagein > object->size)
62195461b45SJohn Dyson 			initial_pagein = object->size;
62295461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
623d254af07SMatthew Dillon 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
62495461b45SJohn Dyson 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
62595461b45SJohn Dyson 					break;
62695461b45SJohn Dyson 				if (ma[i]->valid)
62795461b45SJohn Dyson 					break;
628e69763a3SDoug Rabson 				vm_page_busy(ma[i]);
62995461b45SJohn Dyson 			} else {
630ca0387efSJake Burkholder 				ma[i] = vm_page_alloc(object, i,
631ca0387efSJake Burkholder 				    VM_ALLOC_NORMAL);
63295461b45SJohn Dyson 				if (ma[i] == NULL)
63395461b45SJohn Dyson 					break;
63495461b45SJohn Dyson 			}
63595461b45SJohn Dyson 		}
63695461b45SJohn Dyson 		initial_pagein = i;
6371616db3cSJohn Dyson 
63895461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
63995461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
64095461b45SJohn Dyson 
641ca0387efSJake Burkholder 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) ||
642ca0387efSJake Burkholder 		    (ma[0]->valid == 0)) {
643ecbb00a2SDoug Rabson 			if (ma[0]) {
64497646f56SAlan Cox 				vm_page_lock_queues();
64595461b45SJohn Dyson 				vm_page_protect(ma[0], VM_PROT_NONE);
6468f9110f6SJohn Dyson 				vm_page_free(ma[0]);
64797646f56SAlan Cox 				vm_page_unlock_queues();
648ecbb00a2SDoug Rabson 			}
649a7cddfedSJake Burkholder 			return (EIO);
6501616db3cSJohn Dyson 		}
6511616db3cSJohn Dyson 	}
65297646f56SAlan Cox 	vm_page_lock_queues();
65395461b45SJohn Dyson 	vm_page_wire(ma[0]);
654e69763a3SDoug Rabson 	vm_page_wakeup(ma[0]);
65597646f56SAlan Cox 	vm_page_unlock_queues();
6561616db3cSJohn Dyson 
657ac59490bSJake Burkholder 	pmap_qenter((vm_offset_t)imgp->image_header, ma, 1);
65895461b45SJohn Dyson 	imgp->firstpage = ma[0];
6591616db3cSJohn Dyson 
660a7cddfedSJake Burkholder 	return (0);
6611616db3cSJohn Dyson }
6621616db3cSJohn Dyson 
6631616db3cSJohn Dyson void
6641616db3cSJohn Dyson exec_unmap_first_page(imgp)
6651616db3cSJohn Dyson 	struct image_params *imgp;
6661616db3cSJohn Dyson {
6670cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
66823955314SAlfred Perlstein 
6691616db3cSJohn Dyson 	if (imgp->firstpage) {
670ac59490bSJake Burkholder 		pmap_qremove((vm_offset_t)imgp->image_header, 1);
67197646f56SAlan Cox 		vm_page_lock_queues();
67273007561SDavid Greenman 		vm_page_unwire(imgp->firstpage, 1);
67397646f56SAlan Cox 		vm_page_unlock_queues();
6741616db3cSJohn Dyson 		imgp->firstpage = NULL;
6751616db3cSJohn Dyson 	}
6761616db3cSJohn Dyson }
6771616db3cSJohn Dyson 
67826f9a767SRodney W. Grimes /*
67926f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
68026f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
68126f9a767SRodney W. Grimes  *	automatically in trap.c.
68226f9a767SRodney W. Grimes  */
68326f9a767SRodney W. Grimes int
6843ebc1248SPeter Wemm exec_new_vmspace(imgp, minuser, maxuser, stack_addr)
685c52007c2SDavid Greenman 	struct image_params *imgp;
6863ebc1248SPeter Wemm 	vm_offset_t minuser, maxuser, stack_addr;
68726f9a767SRodney W. Grimes {
68826f9a767SRodney W. Grimes 	int error;
6896f5dafeaSAlan Cox 	struct execlist *ep;
6905e20c11fSAlan Cox 	struct proc *p = imgp->proc;
6915e20c11fSAlan Cox 	struct vmspace *vmspace = p->p_vmspace;
69226f9a767SRodney W. Grimes 
6930cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
6940cddd8f0SMatthew Dillon 
6953ebc1248SPeter Wemm 	stack_addr = stack_addr - maxssiz;
6963ebc1248SPeter Wemm 
697c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
69826f9a767SRodney W. Grimes 
699af9ec885SJohn Dyson 	/*
7006f5dafeaSAlan Cox 	 * Perform functions registered with at_exec().
7016f5dafeaSAlan Cox 	 */
7026f5dafeaSAlan Cox 	TAILQ_FOREACH(ep, &exec_list, next)
7035e20c11fSAlan Cox 		(*ep->function)(p);
7046f5dafeaSAlan Cox 
7056f5dafeaSAlan Cox 	/*
706af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
707af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
708af9ec885SJohn Dyson 	 * not disrupted
709af9ec885SJohn Dyson 	 */
710ca0387efSJake Burkholder 	if (vmspace->vm_refcnt == 1 &&
711ca0387efSJake Burkholder 	    vm_map_min(&vmspace->vm_map) == minuser &&
712ca0387efSJake Burkholder 	    vm_map_max(&vmspace->vm_map) == maxuser) {
7133d903220SDoug Rabson 		if (vmspace->vm_shm)
7145e20c11fSAlan Cox 			shmexit(p);
7153ebc1248SPeter Wemm 		pmap_remove_pages(vmspace_pmap(vmspace), minuser, maxuser);
7163ebc1248SPeter Wemm 		vm_map_remove(&vmspace->vm_map, minuser, maxuser);
717af9ec885SJohn Dyson 	} else {
7183ebc1248SPeter Wemm 		vmspace_exec(p, minuser, maxuser);
7195e20c11fSAlan Cox 		vmspace = p->p_vmspace;
720af9ec885SJohn Dyson 	}
72126f9a767SRodney W. Grimes 
72226f9a767SRodney W. Grimes 	/* Allocate a new stack */
7239a024fc5SRobert Drehmel 	error = vm_map_stack(&vmspace->vm_map, stack_addr, (vm_size_t)maxssiz,
7249a024fc5SRobert Drehmel 	    VM_PROT_ALL, VM_PROT_ALL, 0);
7252267af78SJulian Elischer 	if (error)
7262267af78SJulian Elischer 		return (error);
7272267af78SJulian Elischer 
72863c47a5cSDoug Rabson #ifdef __ia64__
72963c47a5cSDoug Rabson 	{
73063c47a5cSDoug Rabson 		/*
73163c47a5cSDoug Rabson 		 * Allocate backing store. We really need something
73263c47a5cSDoug Rabson 		 * similar to vm_map_stack which can allow the backing
73363c47a5cSDoug Rabson 		 * store to grow upwards. This will do for now.
73463c47a5cSDoug Rabson 		 */
73563c47a5cSDoug Rabson 		vm_offset_t bsaddr;
736cbc89bfbSPaul Saab 		bsaddr = USRSTACK - 2 * maxssiz;
73763c47a5cSDoug Rabson 		error = vm_map_find(&vmspace->vm_map, 0, 0, &bsaddr,
73881f223caSJake Burkholder 		    regstkpages * PAGE_SIZE, 0, VM_PROT_ALL, VM_PROT_ALL, 0);
7395e20c11fSAlan Cox 		FIRST_THREAD_IN_PROC(p)->td_md.md_bspstore = bsaddr;
74063c47a5cSDoug Rabson 	}
74163c47a5cSDoug Rabson #endif
74263c47a5cSDoug Rabson 
7432267af78SJulian Elischer 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
7442267af78SJulian Elischer 	 * VM_STACK case, but they are still used to monitor the size of the
7452267af78SJulian Elischer 	 * process stack so we can check the stack rlimit.
7462267af78SJulian Elischer 	 */
747cbc89bfbSPaul Saab 	vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT;
748cbc89bfbSPaul Saab 	vmspace->vm_maxsaddr = (char *)USRSTACK - maxssiz;
74926f9a767SRodney W. Grimes 
75026f9a767SRodney W. Grimes 	return (0);
75126f9a767SRodney W. Grimes }
75226f9a767SRodney W. Grimes 
75326f9a767SRodney W. Grimes /*
75426f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
75526f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
75626f9a767SRodney W. Grimes  */
75726f9a767SRodney W. Grimes int
758c52007c2SDavid Greenman exec_extract_strings(imgp)
759c52007c2SDavid Greenman 	struct image_params *imgp;
76026f9a767SRodney W. Grimes {
76126f9a767SRodney W. Grimes 	char	**argv, **envv;
76226f9a767SRodney W. Grimes 	char	*argp, *envp;
763ecbb00a2SDoug Rabson 	int	error;
764ecbb00a2SDoug Rabson 	size_t	length;
76526f9a767SRodney W. Grimes 
76626f9a767SRodney W. Grimes 	/*
76726f9a767SRodney W. Grimes 	 * extract arguments first
76826f9a767SRodney W. Grimes 	 */
76926f9a767SRodney W. Grimes 
770c52007c2SDavid Greenman 	argv = imgp->uap->argv;
77126f9a767SRodney W. Grimes 
7720fd1a014SDavid Greenman 	if (argv) {
773aae0aa45SBruce Evans 		argp = (caddr_t)(intptr_t)fuword(argv);
7745cf3d12cSAndrey A. Chernov 		if (argp == (caddr_t)-1)
7755cf3d12cSAndrey A. Chernov 			return (EFAULT);
7765cf3d12cSAndrey A. Chernov 		if (argp)
7775cf3d12cSAndrey A. Chernov 			argv++;
7785cf3d12cSAndrey A. Chernov 		if (imgp->argv0)
7795cf3d12cSAndrey A. Chernov 			argp = imgp->argv0;
7805cf3d12cSAndrey A. Chernov 		if (argp) {
7815cf3d12cSAndrey A. Chernov 			do {
78226f9a767SRodney W. Grimes 				if (argp == (caddr_t)-1)
78326f9a767SRodney W. Grimes 					return (EFAULT);
784c52007c2SDavid Greenman 				if ((error = copyinstr(argp, imgp->stringp,
785c52007c2SDavid Greenman 				    imgp->stringspace, &length))) {
7860fd1a014SDavid Greenman 					if (error == ENAMETOOLONG)
78726f9a767SRodney W. Grimes 						return (E2BIG);
7880fd1a014SDavid Greenman 					return (error);
7890fd1a014SDavid Greenman 				}
790c52007c2SDavid Greenman 				imgp->stringspace -= length;
791c52007c2SDavid Greenman 				imgp->stringp += length;
792c52007c2SDavid Greenman 				imgp->argc++;
793aae0aa45SBruce Evans 			} while ((argp = (caddr_t)(intptr_t)fuword(argv++)));
79426f9a767SRodney W. Grimes 		}
7950fd1a014SDavid Greenman 	}
79626f9a767SRodney W. Grimes 
797b9df5231SPoul-Henning Kamp 	imgp->endargs = imgp->stringp;
798b9df5231SPoul-Henning Kamp 
79926f9a767SRodney W. Grimes 	/*
80026f9a767SRodney W. Grimes 	 * extract environment strings
80126f9a767SRodney W. Grimes 	 */
80226f9a767SRodney W. Grimes 
803c52007c2SDavid Greenman 	envv = imgp->uap->envv;
80426f9a767SRodney W. Grimes 
8050fd1a014SDavid Greenman 	if (envv) {
806aae0aa45SBruce Evans 		while ((envp = (caddr_t)(intptr_t)fuword(envv++))) {
80726f9a767SRodney W. Grimes 			if (envp == (caddr_t)-1)
80826f9a767SRodney W. Grimes 				return (EFAULT);
809c52007c2SDavid Greenman 			if ((error = copyinstr(envp, imgp->stringp,
810c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
8110fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
81226f9a767SRodney W. Grimes 					return (E2BIG);
8130fd1a014SDavid Greenman 				return (error);
8140fd1a014SDavid Greenman 			}
815c52007c2SDavid Greenman 			imgp->stringspace -= length;
816c52007c2SDavid Greenman 			imgp->stringp += length;
817c52007c2SDavid Greenman 			imgp->envc++;
81826f9a767SRodney W. Grimes 		}
8190fd1a014SDavid Greenman 	}
82026f9a767SRodney W. Grimes 
82126f9a767SRodney W. Grimes 	return (0);
82226f9a767SRodney W. Grimes }
82326f9a767SRodney W. Grimes 
82426f9a767SRodney W. Grimes /*
82526f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
82626f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
82726f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
82826f9a767SRodney W. Grimes  */
829654f6be1SBruce Evans register_t *
830c52007c2SDavid Greenman exec_copyout_strings(imgp)
831c52007c2SDavid Greenman 	struct image_params *imgp;
83226f9a767SRodney W. Grimes {
83326f9a767SRodney W. Grimes 	int argc, envc;
83426f9a767SRodney W. Grimes 	char **vectp;
83526f9a767SRodney W. Grimes 	char *stringp, *destp;
836654f6be1SBruce Evans 	register_t *stack_base;
83793f6448cSDavid Greenman 	struct ps_strings *arginfo;
838f3bec5d7SJake Burkholder 	struct proc *p;
839d66a5066SPeter Wemm 	int szsigcode;
84026f9a767SRodney W. Grimes 
84126f9a767SRodney W. Grimes 	/*
84226f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
843d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
84426f9a767SRodney W. Grimes 	 */
845f3bec5d7SJake Burkholder 	p = imgp->proc;
846f3bec5d7SJake Burkholder 	szsigcode = 0;
8474c56fcdeSBruce Evans 	arginfo = (struct ps_strings *)PS_STRINGS;
848f3bec5d7SJake Burkholder 	if (p->p_sysent->sv_szsigcode != NULL)
849f3bec5d7SJake Burkholder 		szsigcode = *(p->p_sysent->sv_szsigcode);
850d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
8511ed012f9SPeter Wemm 	    roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
8521ed012f9SPeter Wemm 
85326f9a767SRodney W. Grimes 	/*
854d66a5066SPeter Wemm 	 * install sigcode
855d66a5066SPeter Wemm 	 */
856d66a5066SPeter Wemm 	if (szsigcode)
857f3bec5d7SJake Burkholder 		copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo -
858f3bec5d7SJake Burkholder 		    szsigcode), szsigcode);
859d66a5066SPeter Wemm 
860d66a5066SPeter Wemm 	/*
861e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
862e1743d02SSøren Schmidt 	 * on the stack.
863e1743d02SSøren Schmidt 	 */
864b9a22da4STakanori Watanabe 	if (imgp->auxargs) {
865e1743d02SSøren Schmidt 		/*
866b9a22da4STakanori Watanabe 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
867b9a22da4STakanori Watanabe 		 * lower compatibility.
868b9a22da4STakanori Watanabe 		 */
869ca0387efSJake Burkholder 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size :
870ca0387efSJake Burkholder 		    (AT_COUNT * 2);
871b9a22da4STakanori Watanabe 		/*
872b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
873b9a22da4STakanori Watanabe 		 * the arg and env vector sets,and imgp->auxarg_size is room
874b9a22da4STakanori Watanabe 		 * for argument of Runtime loader.
875e1743d02SSøren Schmidt 		 */
876e1743d02SSøren Schmidt 		vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 +
877b9a22da4STakanori Watanabe 		    imgp->auxarg_size) * sizeof(char *));
878b9a22da4STakanori Watanabe 
879b9a22da4STakanori Watanabe 	} else
880e1743d02SSøren Schmidt 		/*
881b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
882b9a22da4STakanori Watanabe 		 * the arg and env vector sets
88326f9a767SRodney W. Grimes 		 */
884ca0387efSJake Burkholder 		vectp = (char **)(destp - (imgp->argc + imgp->envc + 2) *
885ca0387efSJake Burkholder 		    sizeof(char *));
88626f9a767SRodney W. Grimes 
88726f9a767SRodney W. Grimes 	/*
88826f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
88926f9a767SRodney W. Grimes 	 */
890654f6be1SBruce Evans 	stack_base = (register_t *)vectp;
89126f9a767SRodney W. Grimes 
892c52007c2SDavid Greenman 	stringp = imgp->stringbase;
893c52007c2SDavid Greenman 	argc = imgp->argc;
894c52007c2SDavid Greenman 	envc = imgp->envc;
89526f9a767SRodney W. Grimes 
89693f6448cSDavid Greenman 	/*
8973aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
89893f6448cSDavid Greenman 	 */
899c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
90093f6448cSDavid Greenman 
90193f6448cSDavid Greenman 	/*
9023aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
9033aed948bSDavid Greenman 	 */
904aae0aa45SBruce Evans 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
9053aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
9063aed948bSDavid Greenman 
9073aed948bSDavid Greenman 	/*
9083aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
90993f6448cSDavid Greenman 	 */
91026f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
911aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
9123aed948bSDavid Greenman 		while (*stringp++ != 0)
9133aed948bSDavid Greenman 			destp++;
9143aed948bSDavid Greenman 		destp++;
91526f9a767SRodney W. Grimes 	}
91626f9a767SRodney W. Grimes 
9171a6e52d0SJeroen Ruigrok van der Werven 	/* a null vector table pointer separates the argp's from the envp's */
9186ab46d52SBruce Evans 	suword(vectp++, 0);
91926f9a767SRodney W. Grimes 
920aae0aa45SBruce Evans 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
9213aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
92293f6448cSDavid Greenman 
92393f6448cSDavid Greenman 	/*
9243aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
92593f6448cSDavid Greenman 	 */
92626f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
927aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
9283aed948bSDavid Greenman 		while (*stringp++ != 0)
9293aed948bSDavid Greenman 			destp++;
9303aed948bSDavid Greenman 		destp++;
93126f9a767SRodney W. Grimes 	}
93226f9a767SRodney W. Grimes 
93326f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
9346ab46d52SBruce Evans 	suword(vectp, 0);
93526f9a767SRodney W. Grimes 
93626f9a767SRodney W. Grimes 	return (stack_base);
93726f9a767SRodney W. Grimes }
93826f9a767SRodney W. Grimes 
93926f9a767SRodney W. Grimes /*
94026f9a767SRodney W. Grimes  * Check permissions of file to execute.
941cf64863aSRobert Watson  *	Called with imgp->vp locked.
94226f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
94326f9a767SRodney W. Grimes  */
944c8a79999SPeter Wemm int
945c52007c2SDavid Greenman exec_check_permissions(imgp)
946c52007c2SDavid Greenman 	struct image_params *imgp;
94726f9a767SRodney W. Grimes {
948c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
949c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
950a854ed98SJohn Baldwin 	struct thread *td;
95126f9a767SRodney W. Grimes 	int error;
95226f9a767SRodney W. Grimes 
953a854ed98SJohn Baldwin 	td = curthread;			/* XXXKSE */
954339b79b9SRobert Watson 
955339b79b9SRobert Watson #ifdef MAC
956339b79b9SRobert Watson 	error = mac_check_vnode_exec(td->td_ucred, imgp->vp);
957339b79b9SRobert Watson 	if (error)
958339b79b9SRobert Watson 		return (error);
959339b79b9SRobert Watson #endif
960339b79b9SRobert Watson 
96126f9a767SRodney W. Grimes 	/* Get file attributes */
962a854ed98SJohn Baldwin 	error = VOP_GETATTR(vp, attr, td->td_ucred, td);
96326f9a767SRodney W. Grimes 	if (error)
96426f9a767SRodney W. Grimes 		return (error);
96526f9a767SRodney W. Grimes 
96626f9a767SRodney W. Grimes 	/*
96726f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
96826f9a767SRodney W. Grimes 	 *	file resides on.
96926f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
97026f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
97126f9a767SRodney W. Grimes 	 *	file really is executable.
97226f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
97326f9a767SRodney W. Grimes 	 */
974c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
97526f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
976a854ed98SJohn Baldwin 	    (attr->va_type != VREG))
97726f9a767SRodney W. Grimes 		return (EACCES);
97826f9a767SRodney W. Grimes 
97926f9a767SRodney W. Grimes 	/*
98026f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
98126f9a767SRodney W. Grimes 	 */
98226f9a767SRodney W. Grimes 	if (attr->va_size == 0)
98326f9a767SRodney W. Grimes 		return (ENOEXEC);
98426f9a767SRodney W. Grimes 
98526f9a767SRodney W. Grimes 	/*
98626f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
98726f9a767SRodney W. Grimes 	 */
988a854ed98SJohn Baldwin 	error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td);
98926f9a767SRodney W. Grimes 	if (error)
99026f9a767SRodney W. Grimes 		return (error);
99126f9a767SRodney W. Grimes 
9926d5a0a8cSDavid Greenman 	/*
9936d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
9946d5a0a8cSDavid Greenman 	 * if there are any.
9956d5a0a8cSDavid Greenman 	 */
9966d5a0a8cSDavid Greenman 	if (vp->v_writecount)
9976d5a0a8cSDavid Greenman 		return (ETXTBSY);
9986d5a0a8cSDavid Greenman 
9996d5a0a8cSDavid Greenman 	/*
10006d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
10016d5a0a8cSDavid Greenman 	 * general case).
10026d5a0a8cSDavid Greenman 	 */
1003a854ed98SJohn Baldwin 	error = VOP_OPEN(vp, FREAD, td->td_ucred, td);
100426f9a767SRodney W. Grimes 	return (error);
1005df8bae1dSRodney W. Grimes }
1006aa855a59SPeter Wemm 
1007aa855a59SPeter Wemm /*
1008aa855a59SPeter Wemm  * Exec handler registration
1009aa855a59SPeter Wemm  */
1010aa855a59SPeter Wemm int
1011aa855a59SPeter Wemm exec_register(execsw_arg)
1012aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
1013aa855a59SPeter Wemm {
1014aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
1015aa855a59SPeter Wemm 	int count = 2;	/* New slot and trailing NULL */
1016aa855a59SPeter Wemm 
1017aa855a59SPeter Wemm 	if (execsw)
1018aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
1019aa855a59SPeter Wemm 			count++;
1020aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1021aa855a59SPeter Wemm 	if (newexecsw == NULL)
1022a7cddfedSJake Burkholder 		return (ENOMEM);
1023aa855a59SPeter Wemm 	xs = newexecsw;
1024aa855a59SPeter Wemm 	if (execsw)
1025aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
1026aa855a59SPeter Wemm 			*xs++ = *es;
1027aa855a59SPeter Wemm 	*xs++ = execsw_arg;
1028aa855a59SPeter Wemm 	*xs = NULL;
1029aa855a59SPeter Wemm 	if (execsw)
1030aa855a59SPeter Wemm 		free(execsw, M_TEMP);
1031aa855a59SPeter Wemm 	execsw = newexecsw;
1032a7cddfedSJake Burkholder 	return (0);
1033aa855a59SPeter Wemm }
1034aa855a59SPeter Wemm 
1035aa855a59SPeter Wemm int
1036aa855a59SPeter Wemm exec_unregister(execsw_arg)
1037aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
1038aa855a59SPeter Wemm {
1039aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
1040aa855a59SPeter Wemm 	int count = 1;
1041aa855a59SPeter Wemm 
1042aa855a59SPeter Wemm 	if (execsw == NULL)
1043aa855a59SPeter Wemm 		panic("unregister with no handlers left?\n");
1044aa855a59SPeter Wemm 
1045aa855a59SPeter Wemm 	for (es = execsw; *es; es++) {
1046aa855a59SPeter Wemm 		if (*es == execsw_arg)
1047aa855a59SPeter Wemm 			break;
1048aa855a59SPeter Wemm 	}
1049aa855a59SPeter Wemm 	if (*es == NULL)
1050a7cddfedSJake Burkholder 		return (ENOENT);
1051aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
1052aa855a59SPeter Wemm 		if (*es != execsw_arg)
1053aa855a59SPeter Wemm 			count++;
1054aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
1055aa855a59SPeter Wemm 	if (newexecsw == NULL)
1056a7cddfedSJake Burkholder 		return (ENOMEM);
1057aa855a59SPeter Wemm 	xs = newexecsw;
1058aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
1059aa855a59SPeter Wemm 		if (*es != execsw_arg)
1060aa855a59SPeter Wemm 			*xs++ = *es;
1061aa855a59SPeter Wemm 	*xs = NULL;
1062aa855a59SPeter Wemm 	if (execsw)
1063aa855a59SPeter Wemm 		free(execsw, M_TEMP);
1064aa855a59SPeter Wemm 	execsw = newexecsw;
1065a7cddfedSJake Burkholder 	return (0);
1066aa855a59SPeter Wemm }
106721d56e9cSAlfred Perlstein 
106821d56e9cSAlfred Perlstein int
106921d56e9cSAlfred Perlstein at_exec(function)
107021d56e9cSAlfred Perlstein 	execlist_fn function;
107121d56e9cSAlfred Perlstein {
107221d56e9cSAlfred Perlstein 	struct execlist *ep;
107321d56e9cSAlfred Perlstein 
107421d56e9cSAlfred Perlstein #ifdef INVARIANTS
107521d56e9cSAlfred Perlstein 	/* Be noisy if the programmer has lost track of things */
107621d56e9cSAlfred Perlstein 	if (rm_at_exec(function))
107721d56e9cSAlfred Perlstein 		printf("WARNING: exec callout entry (%p) already present\n",
107821d56e9cSAlfred Perlstein 		    function);
107921d56e9cSAlfred Perlstein #endif
108021d56e9cSAlfred Perlstein 	ep = malloc(sizeof(*ep), M_ATEXEC, M_NOWAIT);
108121d56e9cSAlfred Perlstein 	if (ep == NULL)
108221d56e9cSAlfred Perlstein 		return (ENOMEM);
108321d56e9cSAlfred Perlstein 	ep->function = function;
108421d56e9cSAlfred Perlstein 	TAILQ_INSERT_TAIL(&exec_list, ep, next);
108521d56e9cSAlfred Perlstein 	return (0);
108621d56e9cSAlfred Perlstein }
108721d56e9cSAlfred Perlstein 
108821d56e9cSAlfred Perlstein /*
108921d56e9cSAlfred Perlstein  * Scan the exec callout list for the given item and remove it.
109021d56e9cSAlfred Perlstein  * Returns the number of items removed (0 or 1)
109121d56e9cSAlfred Perlstein  */
109221d56e9cSAlfred Perlstein int
109321d56e9cSAlfred Perlstein rm_at_exec(function)
109421d56e9cSAlfred Perlstein 	execlist_fn function;
109521d56e9cSAlfred Perlstein {
109621d56e9cSAlfred Perlstein 	struct execlist *ep;
109721d56e9cSAlfred Perlstein 
109821d56e9cSAlfred Perlstein 	TAILQ_FOREACH(ep, &exec_list, next) {
109921d56e9cSAlfred Perlstein 		if (ep->function == function) {
110021d56e9cSAlfred Perlstein 			TAILQ_REMOVE(&exec_list, ep, next);
110121d56e9cSAlfred Perlstein 			free(ep, M_ATEXEC);
110221d56e9cSAlfred Perlstein 			return (1);
110321d56e9cSAlfred Perlstein 		}
110421d56e9cSAlfred Perlstein 	}
110521d56e9cSAlfred Perlstein 	return (0);
110621d56e9cSAlfred Perlstein }
1107