xref: /freebsd/sys/kern/kern_exec.c (revision aa855a598d9a933daa5facd559803ef280326028)
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  *
26aa855a59SPeter Wemm  *	$Id: kern_exec.c,v 1.86 1998/09/04 08:06:55 dfr Exp $
27df8bae1dSRodney W. Grimes  */
28df8bae1dSRodney W. Grimes 
29df8bae1dSRodney W. Grimes #include <sys/param.h>
3026f9a767SRodney W. Grimes #include <sys/systm.h>
31d2d3e875SBruce Evans #include <sys/sysproto.h>
3226f9a767SRodney W. Grimes #include <sys/signalvar.h>
3326f9a767SRodney W. Grimes #include <sys/kernel.h>
3426f9a767SRodney W. Grimes #include <sys/mount.h>
35797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
36079cc25bSDavid Greenman #include <sys/fcntl.h>
3726f9a767SRodney W. Grimes #include <sys/acct.h>
3826f9a767SRodney W. Grimes #include <sys/exec.h>
3926f9a767SRodney W. Grimes #include <sys/imgact.h>
40e1743d02SSøren Schmidt #include <sys/imgact_elf.h>
4126f9a767SRodney W. Grimes #include <sys/wait.h>
42a794e791SBruce Evans #include <sys/proc.h>
432a024a2bSSean Eric Fagan #include <sys/pioctl.h>
44aa855a59SPeter Wemm #include <sys/malloc.h>
45a794e791SBruce Evans #include <sys/namei.h>
461e1e0b44SSøren Schmidt #include <sys/sysent.h>
47797f2d22SPoul-Henning Kamp #include <sys/shm.h>
4899ac3bc8SPeter Wemm #include <sys/sysctl.h>
49a794e791SBruce Evans #include <sys/vnode.h>
509caaadb6SDavid Greenman #include <sys/buf.h>
5126f9a767SRodney W. Grimes 
5226f9a767SRodney W. Grimes #include <vm/vm.h>
53efeaf95aSDavid Greenman #include <vm/vm_param.h>
54efeaf95aSDavid Greenman #include <vm/vm_prot.h>
55996c772fSJohn Dyson #include <sys/lock.h>
56efeaf95aSDavid Greenman #include <vm/pmap.h>
571616db3cSJohn Dyson #include <vm/vm_page.h>
58efeaf95aSDavid Greenman #include <vm/vm_map.h>
5926f9a767SRodney W. Grimes #include <vm/vm_kern.h>
60efeaf95aSDavid Greenman #include <vm/vm_extern.h>
611ebd0c59SDavid Greenman #include <vm/vm_object.h>
62675ea6f0SBruce Evans #include <vm/vm_zone.h>
631616db3cSJohn Dyson #include <vm/vm_pager.h>
6426f9a767SRodney W. Grimes 
6526f9a767SRodney W. Grimes #include <machine/reg.h>
6626f9a767SRodney W. Grimes 
67ecbb00a2SDoug Rabson static long *exec_copyout_strings __P((struct image_params *));
68df8bae1dSRodney W. Grimes 
696120fef1SDavid Greenman static struct ps_strings *ps_strings = PS_STRINGS;
70069e9bc1SDoug Rabson SYSCTL_INTPTR(_kern, KERN_PS_STRINGS, ps_strings, 0, &ps_strings, 0, "");
7199ac3bc8SPeter Wemm 
7299ac3bc8SPeter Wemm static caddr_t usrstack = (caddr_t)USRSTACK;
73069e9bc1SDoug Rabson SYSCTL_INTPTR(_kern, KERN_USRSTACK, usrstack, 0, &usrstack, 0, "");
7499ac3bc8SPeter Wemm /*
75aa855a59SPeter Wemm  * Each of the items is a pointer to a `const struct execsw', hence the
76aa855a59SPeter Wemm  * double pointer here.
77df8bae1dSRodney W. Grimes  */
78aa855a59SPeter Wemm static const struct execsw **execsw;
7926f9a767SRodney W. Grimes 
80d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
81ad7507e2SSteven Wallace struct execve_args {
82ad7507e2SSteven Wallace         char    *fname;
83ad7507e2SSteven Wallace         char    **argv;
84ad7507e2SSteven Wallace         char    **envv;
85ad7507e2SSteven Wallace };
86d2d3e875SBruce Evans #endif
87ad7507e2SSteven Wallace 
8826f9a767SRodney W. Grimes /*
8926f9a767SRodney W. Grimes  * execve() system call.
9026f9a767SRodney W. Grimes  */
9126f9a767SRodney W. Grimes int
92cb226aaaSPoul-Henning Kamp execve(p, uap)
9326f9a767SRodney W. Grimes 	struct proc *p;
9426f9a767SRodney W. Grimes 	register struct execve_args *uap;
95df8bae1dSRodney W. Grimes {
9626f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
97ecbb00a2SDoug Rabson 	long *stack_base;
98bb56ec4aSPoul-Henning Kamp 	int error, len, i;
99c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
10026f9a767SRodney W. Grimes 	struct vattr attr;
10126f9a767SRodney W. Grimes 
102c52007c2SDavid Greenman 	imgp = &image_params;
103df8bae1dSRodney W. Grimes 
104df8bae1dSRodney W. Grimes 	/*
105c52007c2SDavid Greenman 	 * Initialize part of the common data
106df8bae1dSRodney W. Grimes 	 */
107c52007c2SDavid Greenman 	imgp->proc = p;
108c52007c2SDavid Greenman 	imgp->uap = uap;
109c52007c2SDavid Greenman 	imgp->attr = &attr;
110c52007c2SDavid Greenman 	imgp->argc = imgp->envc = 0;
1115cf3d12cSAndrey A. Chernov 	imgp->argv0 = NULL;
112c52007c2SDavid Greenman 	imgp->entry_addr = 0;
113c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
114c52007c2SDavid Greenman 	imgp->interpreted = 0;
115c52007c2SDavid Greenman 	imgp->interpreter_name[0] = '\0';
116e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1171616db3cSJohn Dyson 	imgp->vp = NULL;
1181616db3cSJohn Dyson 	imgp->firstpage = NULL;
11926f9a767SRodney W. Grimes 
12026f9a767SRodney W. Grimes 	/*
12126f9a767SRodney W. Grimes 	 * Allocate temporary demand zeroed space for argument and
12226f9a767SRodney W. Grimes 	 *	environment strings
12326f9a767SRodney W. Grimes 	 */
1241616db3cSJohn Dyson 	imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE);
125c2f9f36bSDavid Greenman 	if (imgp->stringbase == NULL) {
12626f9a767SRodney W. Grimes 		error = ENOMEM;
12726f9a767SRodney W. Grimes 		goto exec_fail;
12826f9a767SRodney W. Grimes 	}
129c52007c2SDavid Greenman 	imgp->stringp = imgp->stringbase;
130c52007c2SDavid Greenman 	imgp->stringspace = ARG_MAX;
1311616db3cSJohn Dyson 	imgp->image_header = imgp->stringbase + ARG_MAX;
13226f9a767SRodney W. Grimes 
13326f9a767SRodney W. Grimes 	/*
13426f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
13526f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
13626f9a767SRodney W. Grimes 	 */
13726f9a767SRodney W. Grimes 	ndp = &nd;
138b35ba931SDavid Greenman 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
1393ed8a403SDavid Greenman 	    UIO_USERSPACE, uap->fname, p);
14026f9a767SRodney W. Grimes 
14126f9a767SRodney W. Grimes interpret:
14226f9a767SRodney W. Grimes 
14326f9a767SRodney W. Grimes 	error = namei(ndp);
14426f9a767SRodney W. Grimes 	if (error) {
1451616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
1461616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
14726f9a767SRodney W. Grimes 		goto exec_fail;
14826f9a767SRodney W. Grimes 	}
14926f9a767SRodney W. Grimes 
150c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
15126f9a767SRodney W. Grimes 
15226f9a767SRodney W. Grimes 	/*
1539022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
1549022e4adSDavid Greenman 	 */
155c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
1568677f509SDavid Greenman 	if (error) {
1578677f509SDavid Greenman 		VOP_UNLOCK(imgp->vp, 0, p);
15826f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
1598677f509SDavid Greenman 	}
16026f9a767SRodney W. Grimes 
1611616db3cSJohn Dyson 	error = exec_map_first_page(imgp);
1629caaadb6SDavid Greenman 	VOP_UNLOCK(imgp->vp, 0, p);
1636d5a0a8cSDavid Greenman 	if (error)
16426f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
16526f9a767SRodney W. Grimes 
16626f9a767SRodney W. Grimes 	/*
16726f9a767SRodney W. Grimes 	 * Loop through list of image activators, calling each one.
16826f9a767SRodney W. Grimes 	 *	If there is no match, the activator returns -1. If there
16926f9a767SRodney W. Grimes 	 *	is a match, but there was an error during the activation,
17026f9a767SRodney W. Grimes 	 *	the error is returned. Otherwise 0 means success. If the
17126f9a767SRodney W. Grimes 	 *	image is interpreted, loop back up and try activating
17226f9a767SRodney W. Grimes 	 *	the interpreter.
17326f9a767SRodney W. Grimes 	 */
17426f9a767SRodney W. Grimes 	for (i = 0; execsw[i]; ++i) {
17526f9a767SRodney W. Grimes 		if (execsw[i]->ex_imgact)
176c52007c2SDavid Greenman 			error = (*execsw[i]->ex_imgact)(imgp);
17726f9a767SRodney W. Grimes 		else
17826f9a767SRodney W. Grimes 			continue;
17926f9a767SRodney W. Grimes 		if (error == -1)
18026f9a767SRodney W. Grimes 			continue;
18126f9a767SRodney W. Grimes 		if (error)
18226f9a767SRodney W. Grimes 			goto exec_fail_dealloc;
183c52007c2SDavid Greenman 		if (imgp->interpreted) {
1841616db3cSJohn Dyson 			exec_unmap_first_page(imgp);
18526f9a767SRodney W. Grimes 			/* free old vnode and name buffer */
186f5277ae7SDavid Greenman 			vrele(ndp->ni_vp);
18799448ed1SJohn Dyson 			zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
18826f9a767SRodney W. Grimes 			/* set new name to that of the interpreter */
189b35ba931SDavid Greenman 			NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
190c52007c2SDavid Greenman 			    UIO_SYSSPACE, imgp->interpreter_name, p);
19126f9a767SRodney W. Grimes 			goto interpret;
19226f9a767SRodney W. Grimes 		}
19326f9a767SRodney W. Grimes 		break;
19426f9a767SRodney W. Grimes 	}
19526f9a767SRodney W. Grimes 	/* If we made it through all the activators and none matched, exit. */
19626f9a767SRodney W. Grimes 	if (error == -1) {
19726f9a767SRodney W. Grimes 		error = ENOEXEC;
19826f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
19926f9a767SRodney W. Grimes 	}
20026f9a767SRodney W. Grimes 
20126f9a767SRodney W. Grimes 	/*
20226f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
20326f9a767SRodney W. Grimes 	 */
204c52007c2SDavid Greenman 	stack_base = exec_copyout_strings(imgp);
20526f9a767SRodney W. Grimes 	p->p_vmspace->vm_minsaddr = (char *)stack_base;
20626f9a767SRodney W. Grimes 
20726f9a767SRodney W. Grimes 	/*
2081e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
2091e1e0b44SSøren Schmidt 	 * let it do the stack setup.
2101e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
21126f9a767SRodney W. Grimes 	 */
2121e1e0b44SSøren Schmidt 	if (p->p_sysent->sv_fixup)
213c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
2141e1e0b44SSøren Schmidt 	else
215c52007c2SDavid Greenman 		suword(--stack_base, imgp->argc);
21626f9a767SRodney W. Grimes 
217a78e8d2aSDavid Greenman 	/*
218a78e8d2aSDavid Greenman 	 * For security and other reasons, the file descriptor table cannot
219a78e8d2aSDavid Greenman 	 * be shared after an exec.
220a78e8d2aSDavid Greenman 	 */
221a78e8d2aSDavid Greenman 	if (p->p_fd->fd_refcnt > 1) {
222a78e8d2aSDavid Greenman 		struct filedesc *tmp;
223a78e8d2aSDavid Greenman 
224a78e8d2aSDavid Greenman 		tmp = fdcopy(p);
225a78e8d2aSDavid Greenman 		fdfree(p);
226a78e8d2aSDavid Greenman 		p->p_fd = tmp;
227a78e8d2aSDavid Greenman 	}
228a78e8d2aSDavid Greenman 
22926f9a767SRodney W. Grimes 	/* close files on exec */
23026f9a767SRodney W. Grimes 	fdcloseexec(p);
23126f9a767SRodney W. Grimes 
23226f9a767SRodney W. Grimes 	/* reset caught signals */
23326f9a767SRodney W. Grimes 	execsigs(p);
23426f9a767SRodney W. Grimes 
23526f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
23626f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
23726f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
23826f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
23926f9a767SRodney W. Grimes 
24026f9a767SRodney W. Grimes 	/*
24124b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
242dc733423SDag-Erling Smørgrav 	 * it that it now has its own resources back
24326f9a767SRodney W. Grimes 	 */
24426f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
24526f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
24626f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
24726f9a767SRodney W. Grimes 		wakeup((caddr_t)p->p_pptr);
24826f9a767SRodney W. Grimes 	}
24926f9a767SRodney W. Grimes 
25026f9a767SRodney W. Grimes 	/*
251a78e8d2aSDavid Greenman 	 * Implement image setuid/setgid.
252a78e8d2aSDavid Greenman 	 *
253a78e8d2aSDavid Greenman 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
254a78e8d2aSDavid Greenman 	 * the process is being traced.
255c52007c2SDavid Greenman 	 */
256d021ae3dSGuido van Rooij 	if ((attr.va_mode & VSUID && p->p_ucred->cr_uid != attr.va_uid ||
257d021ae3dSGuido van Rooij 	     attr.va_mode & VSGID && p->p_ucred->cr_gid != attr.va_gid) &&
258a78e8d2aSDavid Greenman 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
259c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
260c52007c2SDavid Greenman 		/*
261c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
26226f9a767SRodney W. Grimes 		 * root.
26326f9a767SRodney W. Grimes 		 */
264c52007c2SDavid Greenman 		if (p->p_tracep && suser(p->p_ucred, &p->p_acflag)) {
26526f9a767SRodney W. Grimes 			p->p_traceflag = 0;
26626f9a767SRodney W. Grimes 			vrele(p->p_tracep);
267c52007c2SDavid Greenman 			p->p_tracep = NULL;
26826f9a767SRodney W. Grimes 		}
269c52007c2SDavid Greenman 		/*
270c52007c2SDavid Greenman 		 * Set the new credentials.
271c52007c2SDavid Greenman 		 */
27226f9a767SRodney W. Grimes 		p->p_ucred = crcopy(p->p_ucred);
273c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
27426f9a767SRodney W. Grimes 			p->p_ucred->cr_uid = attr.va_uid;
275c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
276d021ae3dSGuido van Rooij 			p->p_ucred->cr_gid = attr.va_gid;
277d5f81602SSean Eric Fagan 		setsugid(p);
278c52007c2SDavid Greenman 	} else {
279e47bda07SDavid Greenman 		if (p->p_ucred->cr_uid == p->p_cred->p_ruid &&
280e47bda07SDavid Greenman 		    p->p_ucred->cr_gid == p->p_cred->p_rgid)
281c52007c2SDavid Greenman 			p->p_flag &= ~P_SUGID;
28226f9a767SRodney W. Grimes 	}
28326f9a767SRodney W. Grimes 
28426f9a767SRodney W. Grimes 	/*
285c52007c2SDavid Greenman 	 * Implement correct POSIX saved-id behavior.
28626f9a767SRodney W. Grimes 	 */
28726f9a767SRodney W. Grimes 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
28826f9a767SRodney W. Grimes 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
28926f9a767SRodney W. Grimes 
29026f9a767SRodney W. Grimes 	/*
2912a531c80SDavid Greenman 	 * Store the vp for use in procfs
2922a531c80SDavid Greenman 	 */
2932a531c80SDavid Greenman 	if (p->p_textvp)		/* release old reference */
2942a531c80SDavid Greenman 		vrele(p->p_textvp);
2952a531c80SDavid Greenman 	VREF(ndp->ni_vp);
2962a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
2972a531c80SDavid Greenman 
2982a531c80SDavid Greenman 	/*
29926f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
30026f9a767SRodney W. Grimes 	 * 	can be set before the program executes.
30126f9a767SRodney W. Grimes 	 */
3022a024a2bSSean Eric Fagan 	STOPEVENT(p, S_EXEC, 0);
3032a024a2bSSean Eric Fagan 
30426f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
30526f9a767SRodney W. Grimes 		psignal(p, SIGTRAP);
30626f9a767SRodney W. Grimes 
30726f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
30826f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
30926f9a767SRodney W. Grimes 
31026f9a767SRodney W. Grimes 	/* Set entry address */
311aae0aa45SBruce Evans 	setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base);
31226f9a767SRodney W. Grimes 
3131616db3cSJohn Dyson exec_fail_dealloc:
3141616db3cSJohn Dyson 
31526f9a767SRodney W. Grimes 	/*
31626f9a767SRodney W. Grimes 	 * free various allocated resources
31726f9a767SRodney W. Grimes 	 */
3181616db3cSJohn Dyson 	if (imgp->firstpage)
3191616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
32026f9a767SRodney W. Grimes 
321c2f9f36bSDavid Greenman 	if (imgp->stringbase != NULL)
3221616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
3231616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
3241616db3cSJohn Dyson 
325a3cf6ebaSDavid Greenman 	if (ndp->ni_vp) {
326f5277ae7SDavid Greenman 		vrele(ndp->ni_vp);
32799448ed1SJohn Dyson 		zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
328a3cf6ebaSDavid Greenman 	}
32926f9a767SRodney W. Grimes 
3301616db3cSJohn Dyson 	if (error == 0)
3311616db3cSJohn Dyson 		return (0);
3321616db3cSJohn Dyson 
33326f9a767SRodney W. Grimes exec_fail:
334c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
33526f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
33626f9a767SRodney W. Grimes 		exit1(p, W_EXITCODE(0, SIGABRT));
33726f9a767SRodney W. Grimes 		/* NOT REACHED */
33826f9a767SRodney W. Grimes 		return(0);
33926f9a767SRodney W. Grimes 	} else {
34026f9a767SRodney W. Grimes 		return(error);
34126f9a767SRodney W. Grimes 	}
34226f9a767SRodney W. Grimes }
34326f9a767SRodney W. Grimes 
3441616db3cSJohn Dyson int
3451616db3cSJohn Dyson exec_map_first_page(imgp)
3461616db3cSJohn Dyson 	struct image_params *imgp;
3471616db3cSJohn Dyson {
34895461b45SJohn Dyson 	int s, rv, i;
34995461b45SJohn Dyson 	int initial_pagein;
35095461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
3511616db3cSJohn Dyson 	vm_object_t object;
3521616db3cSJohn Dyson 
3531616db3cSJohn Dyson 
3541616db3cSJohn Dyson 	if (imgp->firstpage) {
3551616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
3561616db3cSJohn Dyson 	}
3571616db3cSJohn Dyson 
3581616db3cSJohn Dyson 	object = imgp->vp->v_object;
3591616db3cSJohn Dyson 	s = splvm();
3601616db3cSJohn Dyson 
36195461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3621616db3cSJohn Dyson 
36395461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
36495461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
36595461b45SJohn Dyson 		if (initial_pagein > object->size)
36695461b45SJohn Dyson 			initial_pagein = object->size;
36795461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
36895461b45SJohn Dyson 			if (ma[i] = vm_page_lookup(object, i)) {
36995461b45SJohn Dyson 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
37095461b45SJohn Dyson 					break;
37195461b45SJohn Dyson 				if (ma[i]->valid)
37295461b45SJohn Dyson 					break;
373e69763a3SDoug Rabson 				vm_page_busy(ma[i]);
37495461b45SJohn Dyson 			} else {
37595461b45SJohn Dyson 				ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
37695461b45SJohn Dyson 				if (ma[i] == NULL)
37795461b45SJohn Dyson 					break;
37895461b45SJohn Dyson 			}
37995461b45SJohn Dyson 		}
38095461b45SJohn Dyson 		initial_pagein = i;
3811616db3cSJohn Dyson 
38295461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
38395461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
38495461b45SJohn Dyson 
385eed2412eSJohn Dyson 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) {
386ecbb00a2SDoug Rabson 			if (ma[0]) {
38795461b45SJohn Dyson 				vm_page_protect(ma[0], VM_PROT_NONE);
3888f9110f6SJohn Dyson 				vm_page_free(ma[0]);
389ecbb00a2SDoug Rabson 			}
3901616db3cSJohn Dyson 			splx(s);
3911616db3cSJohn Dyson 			return EIO;
3921616db3cSJohn Dyson 		}
3931616db3cSJohn Dyson 	}
3941616db3cSJohn Dyson 
39595461b45SJohn Dyson 	vm_page_wire(ma[0]);
396e69763a3SDoug Rabson 	vm_page_wakeup(ma[0]);
3971616db3cSJohn Dyson 	splx(s);
3981616db3cSJohn Dyson 
39995461b45SJohn Dyson 	pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0]));
40095461b45SJohn Dyson 	imgp->firstpage = ma[0];
4011616db3cSJohn Dyson 
4021616db3cSJohn Dyson 	return 0;
4031616db3cSJohn Dyson }
4041616db3cSJohn Dyson 
4051616db3cSJohn Dyson void
4061616db3cSJohn Dyson exec_unmap_first_page(imgp)
4071616db3cSJohn Dyson 	struct image_params *imgp;
4081616db3cSJohn Dyson {
4091616db3cSJohn Dyson 	if (imgp->firstpage) {
4101616db3cSJohn Dyson 		pmap_kremove((vm_offset_t) imgp->image_header);
4111616db3cSJohn Dyson 		vm_page_unwire(imgp->firstpage);
4121616db3cSJohn Dyson 		imgp->firstpage = NULL;
4131616db3cSJohn Dyson 	}
4141616db3cSJohn Dyson }
4151616db3cSJohn Dyson 
41626f9a767SRodney W. Grimes /*
41726f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
41826f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
41926f9a767SRodney W. Grimes  *	automatically in trap.c.
42026f9a767SRodney W. Grimes  */
42126f9a767SRodney W. Grimes int
422c52007c2SDavid Greenman exec_new_vmspace(imgp)
423c52007c2SDavid Greenman 	struct image_params *imgp;
42426f9a767SRodney W. Grimes {
42526f9a767SRodney W. Grimes 	int error;
426c52007c2SDavid Greenman 	struct vmspace *vmspace = imgp->proc->p_vmspace;
42726f9a767SRodney W. Grimes 	caddr_t	stack_addr = (caddr_t) (USRSTACK - SGROWSIZ);
428af9ec885SJohn Dyson 	vm_map_t map = &vmspace->vm_map;
42926f9a767SRodney W. Grimes 
430c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
43126f9a767SRodney W. Grimes 
432af9ec885SJohn Dyson 	/*
433af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
434af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
435af9ec885SJohn Dyson 	 * not disrupted
436af9ec885SJohn Dyson 	 */
437af9ec885SJohn Dyson 	if (vmspace->vm_refcnt == 1) {
4383d903220SDoug Rabson 		if (vmspace->vm_shm)
439c52007c2SDavid Greenman 			shmexit(imgp->proc);
4409d3fbbb5SJohn Dyson 		pmap_remove_pages(&vmspace->vm_pmap, 0, USRSTACK);
441af9ec885SJohn Dyson 		vm_map_remove(map, 0, USRSTACK);
442af9ec885SJohn Dyson 	} else {
443492da96cSJohn Dyson 		vmspace_exec(imgp->proc);
444492da96cSJohn Dyson 		vmspace = imgp->proc->p_vmspace;
445af9ec885SJohn Dyson 		map = &vmspace->vm_map;
446af9ec885SJohn Dyson 	}
44726f9a767SRodney W. Grimes 
44826f9a767SRodney W. Grimes 	/* Allocate a new stack */
4491616db3cSJohn Dyson 	error = vm_map_insert(&vmspace->vm_map, NULL, 0,
4501616db3cSJohn Dyson 		(vm_offset_t) stack_addr, (vm_offset_t) USRSTACK,
4511616db3cSJohn Dyson 		VM_PROT_ALL, VM_PROT_ALL, 0);
45226f9a767SRodney W. Grimes 	if (error)
45326f9a767SRodney W. Grimes 		return (error);
45426f9a767SRodney W. Grimes 
45526f9a767SRodney W. Grimes 	vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT;
45626f9a767SRodney W. Grimes 
45726f9a767SRodney W. Grimes 	/* Initialize maximum stack address */
45826f9a767SRodney W. Grimes 	vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ;
45926f9a767SRodney W. Grimes 
46026f9a767SRodney W. Grimes 	return(0);
46126f9a767SRodney W. Grimes }
46226f9a767SRodney W. Grimes 
46326f9a767SRodney W. Grimes /*
46426f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
46526f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
46626f9a767SRodney W. Grimes  */
46726f9a767SRodney W. Grimes int
468c52007c2SDavid Greenman exec_extract_strings(imgp)
469c52007c2SDavid Greenman 	struct image_params *imgp;
47026f9a767SRodney W. Grimes {
47126f9a767SRodney W. Grimes 	char	**argv, **envv;
47226f9a767SRodney W. Grimes 	char	*argp, *envp;
473ecbb00a2SDoug Rabson 	int	error;
474ecbb00a2SDoug Rabson 	size_t	length;
47526f9a767SRodney W. Grimes 
47626f9a767SRodney W. Grimes 	/*
47726f9a767SRodney W. Grimes 	 * extract arguments first
47826f9a767SRodney W. Grimes 	 */
47926f9a767SRodney W. Grimes 
480c52007c2SDavid Greenman 	argv = imgp->uap->argv;
48126f9a767SRodney W. Grimes 
4820fd1a014SDavid Greenman 	if (argv) {
483aae0aa45SBruce Evans 		argp = (caddr_t) (intptr_t) fuword(argv);
4845cf3d12cSAndrey A. Chernov 		if (argp == (caddr_t) -1)
4855cf3d12cSAndrey A. Chernov 			return (EFAULT);
4865cf3d12cSAndrey A. Chernov 		if (argp)
4875cf3d12cSAndrey A. Chernov 			argv++;
4885cf3d12cSAndrey A. Chernov 		if (imgp->argv0)
4895cf3d12cSAndrey A. Chernov 			argp = imgp->argv0;
4905cf3d12cSAndrey A. Chernov 		if (argp) {
4915cf3d12cSAndrey A. Chernov 			do {
49226f9a767SRodney W. Grimes 				if (argp == (caddr_t) -1)
49326f9a767SRodney W. Grimes 					return (EFAULT);
494c52007c2SDavid Greenman 				if ((error = copyinstr(argp, imgp->stringp,
495c52007c2SDavid Greenman 				    imgp->stringspace, &length))) {
4960fd1a014SDavid Greenman 					if (error == ENAMETOOLONG)
49726f9a767SRodney W. Grimes 						return(E2BIG);
4980fd1a014SDavid Greenman 					return (error);
4990fd1a014SDavid Greenman 				}
500c52007c2SDavid Greenman 				imgp->stringspace -= length;
501c52007c2SDavid Greenman 				imgp->stringp += length;
502c52007c2SDavid Greenman 				imgp->argc++;
503aae0aa45SBruce Evans 			} while ((argp = (caddr_t) (intptr_t) fuword(argv++)));
50426f9a767SRodney W. Grimes 		}
5050fd1a014SDavid Greenman 	}
50626f9a767SRodney W. Grimes 
50726f9a767SRodney W. Grimes 	/*
50826f9a767SRodney W. Grimes 	 * extract environment strings
50926f9a767SRodney W. Grimes 	 */
51026f9a767SRodney W. Grimes 
511c52007c2SDavid Greenman 	envv = imgp->uap->envv;
51226f9a767SRodney W. Grimes 
5130fd1a014SDavid Greenman 	if (envv) {
514aae0aa45SBruce Evans 		while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
51526f9a767SRodney W. Grimes 			if (envp == (caddr_t) -1)
51626f9a767SRodney W. Grimes 				return (EFAULT);
517c52007c2SDavid Greenman 			if ((error = copyinstr(envp, imgp->stringp,
518c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
5190fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
52026f9a767SRodney W. Grimes 					return(E2BIG);
5210fd1a014SDavid Greenman 				return (error);
5220fd1a014SDavid Greenman 			}
523c52007c2SDavid Greenman 			imgp->stringspace -= length;
524c52007c2SDavid Greenman 			imgp->stringp += length;
525c52007c2SDavid Greenman 			imgp->envc++;
52626f9a767SRodney W. Grimes 		}
5270fd1a014SDavid Greenman 	}
52826f9a767SRodney W. Grimes 
52926f9a767SRodney W. Grimes 	return (0);
53026f9a767SRodney W. Grimes }
53126f9a767SRodney W. Grimes 
53226f9a767SRodney W. Grimes /*
53326f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
53426f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
53526f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
53626f9a767SRodney W. Grimes  */
537ecbb00a2SDoug Rabson long *
538c52007c2SDavid Greenman exec_copyout_strings(imgp)
539c52007c2SDavid Greenman 	struct image_params *imgp;
54026f9a767SRodney W. Grimes {
54126f9a767SRodney W. Grimes 	int argc, envc;
54226f9a767SRodney W. Grimes 	char **vectp;
54326f9a767SRodney W. Grimes 	char *stringp, *destp;
544ecbb00a2SDoug Rabson 	long *stack_base;
54593f6448cSDavid Greenman 	struct ps_strings *arginfo;
546d66a5066SPeter Wemm 	int szsigcode;
54726f9a767SRodney W. Grimes 
54826f9a767SRodney W. Grimes 	/*
54926f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
550d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
55126f9a767SRodney W. Grimes 	 */
55293f6448cSDavid Greenman 	arginfo = PS_STRINGS;
553d66a5066SPeter Wemm 	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
554d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
5551ed012f9SPeter Wemm 		roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
5561ed012f9SPeter Wemm 
55726f9a767SRodney W. Grimes 	/*
558d66a5066SPeter Wemm 	 * install sigcode
559d66a5066SPeter Wemm 	 */
560d66a5066SPeter Wemm 	if (szsigcode)
561d66a5066SPeter Wemm 		copyout(imgp->proc->p_sysent->sv_sigcode,
562d66a5066SPeter Wemm 			((caddr_t)arginfo - szsigcode), szsigcode);
563d66a5066SPeter Wemm 
564d66a5066SPeter Wemm 	/*
565e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
566e1743d02SSøren Schmidt 	 * on the stack.
567e1743d02SSøren Schmidt 	 */
568e1743d02SSøren Schmidt 	if (imgp->auxargs)
569e1743d02SSøren Schmidt 	/*
570e1743d02SSøren Schmidt 	 * The '+ 2' is for the null pointers at the end of each of the
571e1743d02SSøren Schmidt 	 * arg and env vector sets, and 'AT_COUNT*2' is room for the
572e1743d02SSøren Schmidt 	 * ELF Auxargs data.
573e1743d02SSøren Schmidt 	 */
574e1743d02SSøren Schmidt 		vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 +
575e1743d02SSøren Schmidt 				  AT_COUNT*2) * sizeof(char*));
576e1743d02SSøren Schmidt 	else
577e1743d02SSøren Schmidt 	/*
57826f9a767SRodney W. Grimes 	 * The '+ 2' is for the null pointers at the end of each of the
57926f9a767SRodney W. Grimes 	 * arg and env vector sets
58026f9a767SRodney W. Grimes 	 */
581e1743d02SSøren Schmidt 		vectp = (char **)
582e1743d02SSøren Schmidt 			(destp - (imgp->argc + imgp->envc + 2) * sizeof(char*));
58326f9a767SRodney W. Grimes 
58426f9a767SRodney W. Grimes 	/*
58526f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
58626f9a767SRodney W. Grimes 	 */
587ecbb00a2SDoug Rabson 	stack_base = (long *)vectp;
58826f9a767SRodney W. Grimes 
589c52007c2SDavid Greenman 	stringp = imgp->stringbase;
590c52007c2SDavid Greenman 	argc = imgp->argc;
591c52007c2SDavid Greenman 	envc = imgp->envc;
59226f9a767SRodney W. Grimes 
59393f6448cSDavid Greenman 	/*
5943aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
59593f6448cSDavid Greenman 	 */
596c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
59793f6448cSDavid Greenman 
59893f6448cSDavid Greenman 	/*
5993aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
6003aed948bSDavid Greenman 	 */
601aae0aa45SBruce Evans 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
6023aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
6033aed948bSDavid Greenman 
6043aed948bSDavid Greenman 	/*
6053aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
60693f6448cSDavid Greenman 	 */
60726f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
608aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
6093aed948bSDavid Greenman 		while (*stringp++ != 0)
6103aed948bSDavid Greenman 			destp++;
6113aed948bSDavid Greenman 		destp++;
61226f9a767SRodney W. Grimes 	}
61326f9a767SRodney W. Grimes 
61426f9a767SRodney W. Grimes 	/* a null vector table pointer seperates the argp's from the envp's */
6156ab46d52SBruce Evans 	suword(vectp++, 0);
61626f9a767SRodney W. Grimes 
617aae0aa45SBruce Evans 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
6183aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
61993f6448cSDavid Greenman 
62093f6448cSDavid Greenman 	/*
6213aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
62293f6448cSDavid Greenman 	 */
62326f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
624aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
6253aed948bSDavid Greenman 		while (*stringp++ != 0)
6263aed948bSDavid Greenman 			destp++;
6273aed948bSDavid Greenman 		destp++;
62826f9a767SRodney W. Grimes 	}
62926f9a767SRodney W. Grimes 
63026f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
6316ab46d52SBruce Evans 	suword(vectp, 0);
63226f9a767SRodney W. Grimes 
63326f9a767SRodney W. Grimes 	return (stack_base);
63426f9a767SRodney W. Grimes }
63526f9a767SRodney W. Grimes 
63626f9a767SRodney W. Grimes /*
63726f9a767SRodney W. Grimes  * Check permissions of file to execute.
63826f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
63926f9a767SRodney W. Grimes  */
640c8a79999SPeter Wemm int
641c52007c2SDavid Greenman exec_check_permissions(imgp)
642c52007c2SDavid Greenman 	struct image_params *imgp;
64326f9a767SRodney W. Grimes {
644c52007c2SDavid Greenman 	struct proc *p = imgp->proc;
645c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
646c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
64726f9a767SRodney W. Grimes 	int error;
64826f9a767SRodney W. Grimes 
64926f9a767SRodney W. Grimes 	/* Get file attributes */
650c52007c2SDavid Greenman 	error = VOP_GETATTR(vp, attr, p->p_ucred, p);
65126f9a767SRodney W. Grimes 	if (error)
65226f9a767SRodney W. Grimes 		return (error);
65326f9a767SRodney W. Grimes 
65426f9a767SRodney W. Grimes 	/*
65526f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
65626f9a767SRodney W. Grimes 	 *	file resides on.
65726f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
65826f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
65926f9a767SRodney W. Grimes 	 *	file really is executable.
66026f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
66126f9a767SRodney W. Grimes 	 */
662c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
66326f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
66426f9a767SRodney W. Grimes 	    (attr->va_type != VREG)) {
66526f9a767SRodney W. Grimes 		return (EACCES);
66626f9a767SRodney W. Grimes 	}
66726f9a767SRodney W. Grimes 
66826f9a767SRodney W. Grimes 	/*
66926f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
67026f9a767SRodney W. Grimes 	 */
67126f9a767SRodney W. Grimes 	if (attr->va_size == 0)
67226f9a767SRodney W. Grimes 		return (ENOEXEC);
67326f9a767SRodney W. Grimes 
67426f9a767SRodney W. Grimes 	/*
67526f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
67626f9a767SRodney W. Grimes 	 */
677c52007c2SDavid Greenman 	error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
67826f9a767SRodney W. Grimes 	if (error)
67926f9a767SRodney W. Grimes 		return (error);
68026f9a767SRodney W. Grimes 
6816d5a0a8cSDavid Greenman 	/*
6826d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
6836d5a0a8cSDavid Greenman 	 * if there are any.
6846d5a0a8cSDavid Greenman 	 */
6856d5a0a8cSDavid Greenman 	if (vp->v_writecount)
6866d5a0a8cSDavid Greenman 		return (ETXTBSY);
6876d5a0a8cSDavid Greenman 
6886d5a0a8cSDavid Greenman 	/*
6896d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
6906d5a0a8cSDavid Greenman 	 * general case).
6916d5a0a8cSDavid Greenman 	 */
692c52007c2SDavid Greenman 	error = VOP_OPEN(vp, FREAD, p->p_ucred, p);
69326f9a767SRodney W. Grimes 	if (error)
69426f9a767SRodney W. Grimes 		return (error);
69526f9a767SRodney W. Grimes 
69626f9a767SRodney W. Grimes 	return (0);
697df8bae1dSRodney W. Grimes }
698aa855a59SPeter Wemm 
699aa855a59SPeter Wemm /*
700aa855a59SPeter Wemm  * Exec handler registration
701aa855a59SPeter Wemm  */
702aa855a59SPeter Wemm int
703aa855a59SPeter Wemm exec_register(execsw_arg)
704aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
705aa855a59SPeter Wemm {
706aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
707aa855a59SPeter Wemm 	int count = 2;	/* New slot and trailing NULL */
708aa855a59SPeter Wemm 
709aa855a59SPeter Wemm 	if (execsw)
710aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
711aa855a59SPeter Wemm 			count++;
712aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
713aa855a59SPeter Wemm 	if (newexecsw == NULL)
714aa855a59SPeter Wemm 		return ENOMEM;
715aa855a59SPeter Wemm 	xs = newexecsw;
716aa855a59SPeter Wemm 	if (execsw)
717aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
718aa855a59SPeter Wemm 			*xs++ = *es;
719aa855a59SPeter Wemm 	*xs++ = execsw_arg;
720aa855a59SPeter Wemm 	*xs = NULL;
721aa855a59SPeter Wemm 	if (execsw)
722aa855a59SPeter Wemm 		free(execsw, M_TEMP);
723aa855a59SPeter Wemm 	execsw = newexecsw;
724aa855a59SPeter Wemm 	return 0;
725aa855a59SPeter Wemm }
726aa855a59SPeter Wemm 
727aa855a59SPeter Wemm int
728aa855a59SPeter Wemm exec_unregister(execsw_arg)
729aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
730aa855a59SPeter Wemm {
731aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
732aa855a59SPeter Wemm 	int count = 1;
733aa855a59SPeter Wemm 
734aa855a59SPeter Wemm 	if (execsw == NULL)
735aa855a59SPeter Wemm 		panic("unregister with no handlers left?\n");
736aa855a59SPeter Wemm 
737aa855a59SPeter Wemm 	for (es = execsw; *es; es++) {
738aa855a59SPeter Wemm 		if (*es == execsw_arg)
739aa855a59SPeter Wemm 			break;
740aa855a59SPeter Wemm 	}
741aa855a59SPeter Wemm 	if (*es == NULL)
742aa855a59SPeter Wemm 		return ENOENT;
743aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
744aa855a59SPeter Wemm 		if (*es != execsw_arg)
745aa855a59SPeter Wemm 			count++;
746aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
747aa855a59SPeter Wemm 	if (newexecsw == NULL)
748aa855a59SPeter Wemm 		return ENOMEM;
749aa855a59SPeter Wemm 	xs = newexecsw;
750aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
751aa855a59SPeter Wemm 		if (*es != execsw_arg)
752aa855a59SPeter Wemm 			*xs++ = *es;
753aa855a59SPeter Wemm 	*xs = NULL;
754aa855a59SPeter Wemm 	if (execsw)
755aa855a59SPeter Wemm 		free(execsw, M_TEMP);
756aa855a59SPeter Wemm 	execsw = newexecsw;
757aa855a59SPeter Wemm 	return 0;
758aa855a59SPeter Wemm }
759