xref: /freebsd/sys/kern/kern_exec.c (revision fdf4e8b30c692a3abf7d5f13def2d5ce12c8174c)
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  *
26fdf4e8b3SWarner Losh  *	$Id: kern_exec.c,v 1.99 1999/04/27 11:15:55 phk 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 
69486bddb0SDoug Rabson static long ps_strings = PS_STRINGS;
70486bddb0SDoug Rabson SYSCTL_LONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, "");
71486bddb0SDoug Rabson 
72486bddb0SDoug Rabson static long usrstack = USRSTACK;
73486bddb0SDoug Rabson SYSCTL_LONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, "");
7499ac3bc8SPeter Wemm 
7599ac3bc8SPeter Wemm /*
76aa855a59SPeter Wemm  * Each of the items is a pointer to a `const struct execsw', hence the
77aa855a59SPeter Wemm  * double pointer here.
78df8bae1dSRodney W. Grimes  */
79aa855a59SPeter Wemm static const struct execsw **execsw;
8026f9a767SRodney W. Grimes 
81d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
82ad7507e2SSteven Wallace struct execve_args {
83ad7507e2SSteven Wallace         char    *fname;
84ad7507e2SSteven Wallace         char    **argv;
85ad7507e2SSteven Wallace         char    **envv;
86ad7507e2SSteven Wallace };
87d2d3e875SBruce Evans #endif
88ad7507e2SSteven Wallace 
8926f9a767SRodney W. Grimes /*
9026f9a767SRodney W. Grimes  * execve() system call.
9126f9a767SRodney W. Grimes  */
9226f9a767SRodney W. Grimes int
93cb226aaaSPoul-Henning Kamp execve(p, uap)
9426f9a767SRodney W. Grimes 	struct proc *p;
9526f9a767SRodney W. Grimes 	register struct execve_args *uap;
96df8bae1dSRodney W. Grimes {
9726f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
98ecbb00a2SDoug Rabson 	long *stack_base;
99bb56ec4aSPoul-Henning Kamp 	int error, len, i;
100c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
10126f9a767SRodney W. Grimes 	struct vattr attr;
10226f9a767SRodney W. Grimes 
103c52007c2SDavid Greenman 	imgp = &image_params;
104df8bae1dSRodney W. Grimes 
105df8bae1dSRodney W. Grimes 	/*
106c52007c2SDavid Greenman 	 * Initialize part of the common data
107df8bae1dSRodney W. Grimes 	 */
108c52007c2SDavid Greenman 	imgp->proc = p;
109c52007c2SDavid Greenman 	imgp->uap = uap;
110c52007c2SDavid Greenman 	imgp->attr = &attr;
111c52007c2SDavid Greenman 	imgp->argc = imgp->envc = 0;
1125cf3d12cSAndrey A. Chernov 	imgp->argv0 = NULL;
113c52007c2SDavid Greenman 	imgp->entry_addr = 0;
114c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
115c52007c2SDavid Greenman 	imgp->interpreted = 0;
116c52007c2SDavid Greenman 	imgp->interpreter_name[0] = '\0';
117e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1181616db3cSJohn Dyson 	imgp->vp = NULL;
1191616db3cSJohn Dyson 	imgp->firstpage = NULL;
1204fe88fe6SJohn Polstra 	imgp->ps_strings = 0;
12126f9a767SRodney W. Grimes 
12226f9a767SRodney W. Grimes 	/*
12326f9a767SRodney W. Grimes 	 * Allocate temporary demand zeroed space for argument and
12426f9a767SRodney W. Grimes 	 *	environment strings
12526f9a767SRodney W. Grimes 	 */
1261616db3cSJohn Dyson 	imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE);
127c2f9f36bSDavid Greenman 	if (imgp->stringbase == NULL) {
12826f9a767SRodney W. Grimes 		error = ENOMEM;
12926f9a767SRodney W. Grimes 		goto exec_fail;
13026f9a767SRodney W. Grimes 	}
131c52007c2SDavid Greenman 	imgp->stringp = imgp->stringbase;
132c52007c2SDavid Greenman 	imgp->stringspace = ARG_MAX;
1331616db3cSJohn Dyson 	imgp->image_header = imgp->stringbase + ARG_MAX;
13426f9a767SRodney W. Grimes 
13526f9a767SRodney W. Grimes 	/*
13626f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
13726f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
13826f9a767SRodney W. Grimes 	 */
13926f9a767SRodney W. Grimes 	ndp = &nd;
140b35ba931SDavid Greenman 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
1413ed8a403SDavid Greenman 	    UIO_USERSPACE, uap->fname, p);
14226f9a767SRodney W. Grimes 
14326f9a767SRodney W. Grimes interpret:
14426f9a767SRodney W. Grimes 
14526f9a767SRodney W. Grimes 	error = namei(ndp);
14626f9a767SRodney W. Grimes 	if (error) {
1471616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
1481616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
14926f9a767SRodney W. Grimes 		goto exec_fail;
15026f9a767SRodney W. Grimes 	}
15126f9a767SRodney W. Grimes 
152c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
1539c0fed3dSDoug Rabson 	imgp->fname = uap->fname;
15426f9a767SRodney W. Grimes 
15526f9a767SRodney W. Grimes 	/*
1569022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
1579022e4adSDavid Greenman 	 */
158c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
1598677f509SDavid Greenman 	if (error) {
1608677f509SDavid Greenman 		VOP_UNLOCK(imgp->vp, 0, p);
16126f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
1628677f509SDavid Greenman 	}
16326f9a767SRodney W. Grimes 
1641616db3cSJohn Dyson 	error = exec_map_first_page(imgp);
1659caaadb6SDavid Greenman 	VOP_UNLOCK(imgp->vp, 0, p);
1666d5a0a8cSDavid Greenman 	if (error)
16726f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
16826f9a767SRodney W. Grimes 
16926f9a767SRodney W. Grimes 	/*
17026f9a767SRodney W. Grimes 	 * Loop through list of image activators, calling each one.
17126f9a767SRodney W. Grimes 	 *	If there is no match, the activator returns -1. If there
17226f9a767SRodney W. Grimes 	 *	is a match, but there was an error during the activation,
17326f9a767SRodney W. Grimes 	 *	the error is returned. Otherwise 0 means success. If the
17426f9a767SRodney W. Grimes 	 *	image is interpreted, loop back up and try activating
17526f9a767SRodney W. Grimes 	 *	the interpreter.
17626f9a767SRodney W. Grimes 	 */
17726f9a767SRodney W. Grimes 	for (i = 0; execsw[i]; ++i) {
17826f9a767SRodney W. Grimes 		if (execsw[i]->ex_imgact)
179c52007c2SDavid Greenman 			error = (*execsw[i]->ex_imgact)(imgp);
18026f9a767SRodney W. Grimes 		else
18126f9a767SRodney W. Grimes 			continue;
18226f9a767SRodney W. Grimes 		if (error == -1)
18326f9a767SRodney W. Grimes 			continue;
18426f9a767SRodney W. Grimes 		if (error)
18526f9a767SRodney W. Grimes 			goto exec_fail_dealloc;
186c52007c2SDavid Greenman 		if (imgp->interpreted) {
1871616db3cSJohn Dyson 			exec_unmap_first_page(imgp);
18826f9a767SRodney W. Grimes 			/* free old vnode and name buffer */
189f5277ae7SDavid Greenman 			vrele(ndp->ni_vp);
19099448ed1SJohn Dyson 			zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
19126f9a767SRodney W. Grimes 			/* set new name to that of the interpreter */
192b35ba931SDavid Greenman 			NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
193c52007c2SDavid Greenman 			    UIO_SYSSPACE, imgp->interpreter_name, p);
19426f9a767SRodney W. Grimes 			goto interpret;
19526f9a767SRodney W. Grimes 		}
19626f9a767SRodney W. Grimes 		break;
19726f9a767SRodney W. Grimes 	}
19826f9a767SRodney W. Grimes 	/* If we made it through all the activators and none matched, exit. */
19926f9a767SRodney W. Grimes 	if (error == -1) {
20026f9a767SRodney W. Grimes 		error = ENOEXEC;
20126f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
20226f9a767SRodney W. Grimes 	}
20326f9a767SRodney W. Grimes 
20426f9a767SRodney W. Grimes 	/*
20526f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
20626f9a767SRodney W. Grimes 	 */
207c52007c2SDavid Greenman 	stack_base = exec_copyout_strings(imgp);
20826f9a767SRodney W. Grimes 	p->p_vmspace->vm_minsaddr = (char *)stack_base;
20926f9a767SRodney W. Grimes 
21026f9a767SRodney W. Grimes 	/*
2111e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
2121e1e0b44SSøren Schmidt 	 * let it do the stack setup.
2131e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
21426f9a767SRodney W. Grimes 	 */
2151e1e0b44SSøren Schmidt 	if (p->p_sysent->sv_fixup)
216c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
2171e1e0b44SSøren Schmidt 	else
218c52007c2SDavid Greenman 		suword(--stack_base, imgp->argc);
21926f9a767SRodney W. Grimes 
220a78e8d2aSDavid Greenman 	/*
221a78e8d2aSDavid Greenman 	 * For security and other reasons, the file descriptor table cannot
222a78e8d2aSDavid Greenman 	 * be shared after an exec.
223a78e8d2aSDavid Greenman 	 */
224a78e8d2aSDavid Greenman 	if (p->p_fd->fd_refcnt > 1) {
225a78e8d2aSDavid Greenman 		struct filedesc *tmp;
226a78e8d2aSDavid Greenman 
227a78e8d2aSDavid Greenman 		tmp = fdcopy(p);
228a78e8d2aSDavid Greenman 		fdfree(p);
229a78e8d2aSDavid Greenman 		p->p_fd = tmp;
230a78e8d2aSDavid Greenman 	}
231a78e8d2aSDavid Greenman 
232fdf4e8b3SWarner Losh 	/* Stop profiling */
233fdf4e8b3SWarner Losh 	stopprofclock(p);
234fdf4e8b3SWarner Losh 
23526f9a767SRodney W. Grimes 	/* close files on exec */
23626f9a767SRodney W. Grimes 	fdcloseexec(p);
23726f9a767SRodney W. Grimes 
23826f9a767SRodney W. Grimes 	/* reset caught signals */
23926f9a767SRodney W. Grimes 	execsigs(p);
24026f9a767SRodney W. Grimes 
24126f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
24226f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
24326f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
24426f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
24526f9a767SRodney W. Grimes 
24626f9a767SRodney W. Grimes 	/*
24724b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
248dc733423SDag-Erling Smørgrav 	 * it that it now has its own resources back
24926f9a767SRodney W. Grimes 	 */
25026f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
25126f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
25226f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
25326f9a767SRodney W. Grimes 		wakeup((caddr_t)p->p_pptr);
25426f9a767SRodney W. Grimes 	}
25526f9a767SRodney W. Grimes 
25626f9a767SRodney W. Grimes 	/*
257a78e8d2aSDavid Greenman 	 * Implement image setuid/setgid.
258a78e8d2aSDavid Greenman 	 *
259a78e8d2aSDavid Greenman 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
260a78e8d2aSDavid Greenman 	 * the process is being traced.
261c52007c2SDavid Greenman 	 */
2628aef1712SMatthew Dillon 	if ((((attr.va_mode & VSUID) && p->p_ucred->cr_uid != attr.va_uid) ||
2638aef1712SMatthew Dillon 	     ((attr.va_mode & VSGID) && p->p_ucred->cr_gid != attr.va_gid)) &&
264a78e8d2aSDavid Greenman 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
265c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
266c52007c2SDavid Greenman 		/*
267c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
26826f9a767SRodney W. Grimes 		 * root.
26926f9a767SRodney W. Grimes 		 */
270f711d546SPoul-Henning Kamp 		if (p->p_tracep && suser(p)) {
27126f9a767SRodney W. Grimes 			p->p_traceflag = 0;
27226f9a767SRodney W. Grimes 			vrele(p->p_tracep);
273c52007c2SDavid Greenman 			p->p_tracep = NULL;
27426f9a767SRodney W. Grimes 		}
275c52007c2SDavid Greenman 		/*
276c52007c2SDavid Greenman 		 * Set the new credentials.
277c52007c2SDavid Greenman 		 */
27826f9a767SRodney W. Grimes 		p->p_ucred = crcopy(p->p_ucred);
279c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
28026f9a767SRodney W. Grimes 			p->p_ucred->cr_uid = attr.va_uid;
281c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
282d021ae3dSGuido van Rooij 			p->p_ucred->cr_gid = attr.va_gid;
283d5f81602SSean Eric Fagan 		setsugid(p);
284c52007c2SDavid Greenman 	} else {
285e47bda07SDavid Greenman 		if (p->p_ucred->cr_uid == p->p_cred->p_ruid &&
286e47bda07SDavid Greenman 		    p->p_ucred->cr_gid == p->p_cred->p_rgid)
287c52007c2SDavid Greenman 			p->p_flag &= ~P_SUGID;
28826f9a767SRodney W. Grimes 	}
28926f9a767SRodney W. Grimes 
29026f9a767SRodney W. Grimes 	/*
291c52007c2SDavid Greenman 	 * Implement correct POSIX saved-id behavior.
29226f9a767SRodney W. Grimes 	 */
29326f9a767SRodney W. Grimes 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
29426f9a767SRodney W. Grimes 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
29526f9a767SRodney W. Grimes 
29626f9a767SRodney W. Grimes 	/*
2972a531c80SDavid Greenman 	 * Store the vp for use in procfs
2982a531c80SDavid Greenman 	 */
2992a531c80SDavid Greenman 	if (p->p_textvp)		/* release old reference */
3002a531c80SDavid Greenman 		vrele(p->p_textvp);
3012a531c80SDavid Greenman 	VREF(ndp->ni_vp);
3022a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
3032a531c80SDavid Greenman 
3042a531c80SDavid Greenman 	/*
30526f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
30626f9a767SRodney W. Grimes 	 * 	can be set before the program executes.
30726f9a767SRodney W. Grimes 	 */
3082a024a2bSSean Eric Fagan 	STOPEVENT(p, S_EXEC, 0);
3092a024a2bSSean Eric Fagan 
31026f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
31126f9a767SRodney W. Grimes 		psignal(p, SIGTRAP);
31226f9a767SRodney W. Grimes 
31326f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
31426f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
31526f9a767SRodney W. Grimes 
3164fe88fe6SJohn Polstra 	/* Set values passed into the program in registers. */
3174fe88fe6SJohn Polstra 	setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base,
3184fe88fe6SJohn Polstra 	    imgp->ps_strings);
31926f9a767SRodney W. Grimes 
3201616db3cSJohn Dyson exec_fail_dealloc:
3211616db3cSJohn Dyson 
32226f9a767SRodney W. Grimes 	/*
32326f9a767SRodney W. Grimes 	 * free various allocated resources
32426f9a767SRodney W. Grimes 	 */
3251616db3cSJohn Dyson 	if (imgp->firstpage)
3261616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
32726f9a767SRodney W. Grimes 
328c2f9f36bSDavid Greenman 	if (imgp->stringbase != NULL)
3291616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
3301616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
3311616db3cSJohn Dyson 
3329c0fed3dSDoug Rabson 	if (imgp->vp) {
3339c0fed3dSDoug Rabson 		vrele(imgp->vp);
33499448ed1SJohn Dyson 		zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
335a3cf6ebaSDavid Greenman 	}
33626f9a767SRodney W. Grimes 
3371616db3cSJohn Dyson 	if (error == 0)
3381616db3cSJohn Dyson 		return (0);
3391616db3cSJohn Dyson 
34026f9a767SRodney W. Grimes exec_fail:
341c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
34226f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
34326f9a767SRodney W. Grimes 		exit1(p, W_EXITCODE(0, SIGABRT));
34426f9a767SRodney W. Grimes 		/* NOT REACHED */
34526f9a767SRodney W. Grimes 		return(0);
34626f9a767SRodney W. Grimes 	} else {
34726f9a767SRodney W. Grimes 		return(error);
34826f9a767SRodney W. Grimes 	}
34926f9a767SRodney W. Grimes }
35026f9a767SRodney W. Grimes 
3511616db3cSJohn Dyson int
3521616db3cSJohn Dyson exec_map_first_page(imgp)
3531616db3cSJohn Dyson 	struct image_params *imgp;
3541616db3cSJohn Dyson {
35595461b45SJohn Dyson 	int s, rv, i;
35695461b45SJohn Dyson 	int initial_pagein;
35795461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
3581616db3cSJohn Dyson 	vm_object_t object;
3591616db3cSJohn Dyson 
3601616db3cSJohn Dyson 
3611616db3cSJohn Dyson 	if (imgp->firstpage) {
3621616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
3631616db3cSJohn Dyson 	}
3641616db3cSJohn Dyson 
3651616db3cSJohn Dyson 	object = imgp->vp->v_object;
3661616db3cSJohn Dyson 	s = splvm();
3671616db3cSJohn Dyson 
36895461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3691616db3cSJohn Dyson 
37095461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
37195461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
37295461b45SJohn Dyson 		if (initial_pagein > object->size)
37395461b45SJohn Dyson 			initial_pagein = object->size;
37495461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
375d254af07SMatthew Dillon 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
37695461b45SJohn Dyson 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
37795461b45SJohn Dyson 					break;
37895461b45SJohn Dyson 				if (ma[i]->valid)
37995461b45SJohn Dyson 					break;
380e69763a3SDoug Rabson 				vm_page_busy(ma[i]);
38195461b45SJohn Dyson 			} else {
38295461b45SJohn Dyson 				ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
38395461b45SJohn Dyson 				if (ma[i] == NULL)
38495461b45SJohn Dyson 					break;
38595461b45SJohn Dyson 			}
38695461b45SJohn Dyson 		}
38795461b45SJohn Dyson 		initial_pagein = i;
3881616db3cSJohn Dyson 
38995461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
39095461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
39195461b45SJohn Dyson 
392eed2412eSJohn Dyson 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) {
393ecbb00a2SDoug Rabson 			if (ma[0]) {
39495461b45SJohn Dyson 				vm_page_protect(ma[0], VM_PROT_NONE);
3958f9110f6SJohn Dyson 				vm_page_free(ma[0]);
396ecbb00a2SDoug Rabson 			}
3971616db3cSJohn Dyson 			splx(s);
3981616db3cSJohn Dyson 			return EIO;
3991616db3cSJohn Dyson 		}
4001616db3cSJohn Dyson 	}
4011616db3cSJohn Dyson 
40295461b45SJohn Dyson 	vm_page_wire(ma[0]);
403e69763a3SDoug Rabson 	vm_page_wakeup(ma[0]);
4041616db3cSJohn Dyson 	splx(s);
4051616db3cSJohn Dyson 
40695461b45SJohn Dyson 	pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0]));
40795461b45SJohn Dyson 	imgp->firstpage = ma[0];
4081616db3cSJohn Dyson 
4091616db3cSJohn Dyson 	return 0;
4101616db3cSJohn Dyson }
4111616db3cSJohn Dyson 
4121616db3cSJohn Dyson void
4131616db3cSJohn Dyson exec_unmap_first_page(imgp)
4141616db3cSJohn Dyson 	struct image_params *imgp;
4151616db3cSJohn Dyson {
4161616db3cSJohn Dyson 	if (imgp->firstpage) {
4171616db3cSJohn Dyson 		pmap_kremove((vm_offset_t) imgp->image_header);
41873007561SDavid Greenman 		vm_page_unwire(imgp->firstpage, 1);
4191616db3cSJohn Dyson 		imgp->firstpage = NULL;
4201616db3cSJohn Dyson 	}
4211616db3cSJohn Dyson }
4221616db3cSJohn Dyson 
42326f9a767SRodney W. Grimes /*
42426f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
42526f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
42626f9a767SRodney W. Grimes  *	automatically in trap.c.
42726f9a767SRodney W. Grimes  */
42826f9a767SRodney W. Grimes int
429c52007c2SDavid Greenman exec_new_vmspace(imgp)
430c52007c2SDavid Greenman 	struct image_params *imgp;
43126f9a767SRodney W. Grimes {
43226f9a767SRodney W. Grimes 	int error;
433c52007c2SDavid Greenman 	struct vmspace *vmspace = imgp->proc->p_vmspace;
4342267af78SJulian Elischer 	caddr_t	stack_addr = (caddr_t) (USRSTACK - MAXSSIZ);
435af9ec885SJohn Dyson 	vm_map_t map = &vmspace->vm_map;
43626f9a767SRodney W. Grimes 
437c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
43826f9a767SRodney W. Grimes 
439af9ec885SJohn Dyson 	/*
440af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
441af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
442af9ec885SJohn Dyson 	 * not disrupted
443af9ec885SJohn Dyson 	 */
444af9ec885SJohn Dyson 	if (vmspace->vm_refcnt == 1) {
4453d903220SDoug Rabson 		if (vmspace->vm_shm)
446c52007c2SDavid Greenman 			shmexit(imgp->proc);
447b1028ad1SLuoqi Chen 		pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS);
4489c0fed3dSDoug Rabson 		vm_map_remove(map, 0, VM_MAXUSER_ADDRESS);
449af9ec885SJohn Dyson 	} else {
450492da96cSJohn Dyson 		vmspace_exec(imgp->proc);
451492da96cSJohn Dyson 		vmspace = imgp->proc->p_vmspace;
452af9ec885SJohn Dyson 		map = &vmspace->vm_map;
453af9ec885SJohn Dyson 	}
45426f9a767SRodney W. Grimes 
45526f9a767SRodney W. Grimes 	/* Allocate a new stack */
4562267af78SJulian Elischer 	error = vm_map_stack (&vmspace->vm_map, (vm_offset_t)stack_addr,
4572267af78SJulian Elischer 			      (vm_size_t)MAXSSIZ, VM_PROT_ALL, VM_PROT_ALL, 0);
4582267af78SJulian Elischer 	if (error)
4592267af78SJulian Elischer 		return (error);
4602267af78SJulian Elischer 
4612267af78SJulian Elischer 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
4622267af78SJulian Elischer 	 * VM_STACK case, but they are still used to monitor the size of the
4632267af78SJulian Elischer 	 * process stack so we can check the stack rlimit.
4642267af78SJulian Elischer 	 */
4652267af78SJulian Elischer 	vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT;
4662267af78SJulian Elischer 	vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ;
46726f9a767SRodney W. Grimes 
46826f9a767SRodney W. Grimes 	return(0);
46926f9a767SRodney W. Grimes }
47026f9a767SRodney W. Grimes 
47126f9a767SRodney W. Grimes /*
47226f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
47326f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
47426f9a767SRodney W. Grimes  */
47526f9a767SRodney W. Grimes int
476c52007c2SDavid Greenman exec_extract_strings(imgp)
477c52007c2SDavid Greenman 	struct image_params *imgp;
47826f9a767SRodney W. Grimes {
47926f9a767SRodney W. Grimes 	char	**argv, **envv;
48026f9a767SRodney W. Grimes 	char	*argp, *envp;
481ecbb00a2SDoug Rabson 	int	error;
482ecbb00a2SDoug Rabson 	size_t	length;
48326f9a767SRodney W. Grimes 
48426f9a767SRodney W. Grimes 	/*
48526f9a767SRodney W. Grimes 	 * extract arguments first
48626f9a767SRodney W. Grimes 	 */
48726f9a767SRodney W. Grimes 
488c52007c2SDavid Greenman 	argv = imgp->uap->argv;
48926f9a767SRodney W. Grimes 
4900fd1a014SDavid Greenman 	if (argv) {
491aae0aa45SBruce Evans 		argp = (caddr_t) (intptr_t) fuword(argv);
4925cf3d12cSAndrey A. Chernov 		if (argp == (caddr_t) -1)
4935cf3d12cSAndrey A. Chernov 			return (EFAULT);
4945cf3d12cSAndrey A. Chernov 		if (argp)
4955cf3d12cSAndrey A. Chernov 			argv++;
4965cf3d12cSAndrey A. Chernov 		if (imgp->argv0)
4975cf3d12cSAndrey A. Chernov 			argp = imgp->argv0;
4985cf3d12cSAndrey A. Chernov 		if (argp) {
4995cf3d12cSAndrey A. Chernov 			do {
50026f9a767SRodney W. Grimes 				if (argp == (caddr_t) -1)
50126f9a767SRodney W. Grimes 					return (EFAULT);
502c52007c2SDavid Greenman 				if ((error = copyinstr(argp, imgp->stringp,
503c52007c2SDavid Greenman 				    imgp->stringspace, &length))) {
5040fd1a014SDavid Greenman 					if (error == ENAMETOOLONG)
50526f9a767SRodney W. Grimes 						return(E2BIG);
5060fd1a014SDavid Greenman 					return (error);
5070fd1a014SDavid Greenman 				}
508c52007c2SDavid Greenman 				imgp->stringspace -= length;
509c52007c2SDavid Greenman 				imgp->stringp += length;
510c52007c2SDavid Greenman 				imgp->argc++;
511aae0aa45SBruce Evans 			} while ((argp = (caddr_t) (intptr_t) fuword(argv++)));
51226f9a767SRodney W. Grimes 		}
5130fd1a014SDavid Greenman 	}
51426f9a767SRodney W. Grimes 
51526f9a767SRodney W. Grimes 	/*
51626f9a767SRodney W. Grimes 	 * extract environment strings
51726f9a767SRodney W. Grimes 	 */
51826f9a767SRodney W. Grimes 
519c52007c2SDavid Greenman 	envv = imgp->uap->envv;
52026f9a767SRodney W. Grimes 
5210fd1a014SDavid Greenman 	if (envv) {
522aae0aa45SBruce Evans 		while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
52326f9a767SRodney W. Grimes 			if (envp == (caddr_t) -1)
52426f9a767SRodney W. Grimes 				return (EFAULT);
525c52007c2SDavid Greenman 			if ((error = copyinstr(envp, imgp->stringp,
526c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
5270fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
52826f9a767SRodney W. Grimes 					return(E2BIG);
5290fd1a014SDavid Greenman 				return (error);
5300fd1a014SDavid Greenman 			}
531c52007c2SDavid Greenman 			imgp->stringspace -= length;
532c52007c2SDavid Greenman 			imgp->stringp += length;
533c52007c2SDavid Greenman 			imgp->envc++;
53426f9a767SRodney W. Grimes 		}
5350fd1a014SDavid Greenman 	}
53626f9a767SRodney W. Grimes 
53726f9a767SRodney W. Grimes 	return (0);
53826f9a767SRodney W. Grimes }
53926f9a767SRodney W. Grimes 
54026f9a767SRodney W. Grimes /*
54126f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
54226f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
54326f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
54426f9a767SRodney W. Grimes  */
545ecbb00a2SDoug Rabson long *
546c52007c2SDavid Greenman exec_copyout_strings(imgp)
547c52007c2SDavid Greenman 	struct image_params *imgp;
54826f9a767SRodney W. Grimes {
54926f9a767SRodney W. Grimes 	int argc, envc;
55026f9a767SRodney W. Grimes 	char **vectp;
55126f9a767SRodney W. Grimes 	char *stringp, *destp;
552ecbb00a2SDoug Rabson 	long *stack_base;
55393f6448cSDavid Greenman 	struct ps_strings *arginfo;
554d66a5066SPeter Wemm 	int szsigcode;
55526f9a767SRodney W. Grimes 
55626f9a767SRodney W. Grimes 	/*
55726f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
558d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
55926f9a767SRodney W. Grimes 	 */
5604c56fcdeSBruce Evans 	arginfo = (struct ps_strings *)PS_STRINGS;
561d66a5066SPeter Wemm 	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
562d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
5631ed012f9SPeter Wemm 		roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
5641ed012f9SPeter Wemm 
56526f9a767SRodney W. Grimes 	/*
566d66a5066SPeter Wemm 	 * install sigcode
567d66a5066SPeter Wemm 	 */
568d66a5066SPeter Wemm 	if (szsigcode)
569d66a5066SPeter Wemm 		copyout(imgp->proc->p_sysent->sv_sigcode,
570d66a5066SPeter Wemm 			((caddr_t)arginfo - szsigcode), szsigcode);
571d66a5066SPeter Wemm 
572d66a5066SPeter Wemm 	/*
573e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
574e1743d02SSøren Schmidt 	 * on the stack.
575e1743d02SSøren Schmidt 	 */
576e1743d02SSøren Schmidt 	if (imgp->auxargs)
577e1743d02SSøren Schmidt 	/*
578e1743d02SSøren Schmidt 	 * The '+ 2' is for the null pointers at the end of each of the
579e1743d02SSøren Schmidt 	 * arg and env vector sets, and 'AT_COUNT*2' is room for the
580e1743d02SSøren Schmidt 	 * ELF Auxargs data.
581e1743d02SSøren Schmidt 	 */
582e1743d02SSøren Schmidt 		vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 +
583e1743d02SSøren Schmidt 				  AT_COUNT*2) * sizeof(char*));
584e1743d02SSøren Schmidt 	else
585e1743d02SSøren Schmidt 	/*
58626f9a767SRodney W. Grimes 	 * The '+ 2' is for the null pointers at the end of each of the
58726f9a767SRodney W. Grimes 	 * arg and env vector sets
58826f9a767SRodney W. Grimes 	 */
589e1743d02SSøren Schmidt 		vectp = (char **)
590e1743d02SSøren Schmidt 			(destp - (imgp->argc + imgp->envc + 2) * sizeof(char*));
59126f9a767SRodney W. Grimes 
59226f9a767SRodney W. Grimes 	/*
59326f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
59426f9a767SRodney W. Grimes 	 */
595ecbb00a2SDoug Rabson 	stack_base = (long *)vectp;
59626f9a767SRodney W. Grimes 
597c52007c2SDavid Greenman 	stringp = imgp->stringbase;
598c52007c2SDavid Greenman 	argc = imgp->argc;
599c52007c2SDavid Greenman 	envc = imgp->envc;
60026f9a767SRodney W. Grimes 
60193f6448cSDavid Greenman 	/*
6023aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
60393f6448cSDavid Greenman 	 */
604c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
60593f6448cSDavid Greenman 
60693f6448cSDavid Greenman 	/*
6073aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
6083aed948bSDavid Greenman 	 */
609aae0aa45SBruce Evans 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
6103aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
6113aed948bSDavid Greenman 
6123aed948bSDavid Greenman 	/*
6133aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
61493f6448cSDavid Greenman 	 */
61526f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
616aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
6173aed948bSDavid Greenman 		while (*stringp++ != 0)
6183aed948bSDavid Greenman 			destp++;
6193aed948bSDavid Greenman 		destp++;
62026f9a767SRodney W. Grimes 	}
62126f9a767SRodney W. Grimes 
62226f9a767SRodney W. Grimes 	/* a null vector table pointer seperates the argp's from the envp's */
6236ab46d52SBruce Evans 	suword(vectp++, 0);
62426f9a767SRodney W. Grimes 
625aae0aa45SBruce Evans 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
6263aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
62793f6448cSDavid Greenman 
62893f6448cSDavid Greenman 	/*
6293aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
63093f6448cSDavid Greenman 	 */
63126f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
632aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
6333aed948bSDavid Greenman 		while (*stringp++ != 0)
6343aed948bSDavid Greenman 			destp++;
6353aed948bSDavid Greenman 		destp++;
63626f9a767SRodney W. Grimes 	}
63726f9a767SRodney W. Grimes 
63826f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
6396ab46d52SBruce Evans 	suword(vectp, 0);
64026f9a767SRodney W. Grimes 
64126f9a767SRodney W. Grimes 	return (stack_base);
64226f9a767SRodney W. Grimes }
64326f9a767SRodney W. Grimes 
64426f9a767SRodney W. Grimes /*
64526f9a767SRodney W. Grimes  * Check permissions of file to execute.
64626f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
64726f9a767SRodney W. Grimes  */
648c8a79999SPeter Wemm int
649c52007c2SDavid Greenman exec_check_permissions(imgp)
650c52007c2SDavid Greenman 	struct image_params *imgp;
65126f9a767SRodney W. Grimes {
652c52007c2SDavid Greenman 	struct proc *p = imgp->proc;
653c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
654c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
65526f9a767SRodney W. Grimes 	int error;
65626f9a767SRodney W. Grimes 
65726f9a767SRodney W. Grimes 	/* Get file attributes */
658c52007c2SDavid Greenman 	error = VOP_GETATTR(vp, attr, p->p_ucred, p);
65926f9a767SRodney W. Grimes 	if (error)
66026f9a767SRodney W. Grimes 		return (error);
66126f9a767SRodney W. Grimes 
66226f9a767SRodney W. Grimes 	/*
66326f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
66426f9a767SRodney W. Grimes 	 *	file resides on.
66526f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
66626f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
66726f9a767SRodney W. Grimes 	 *	file really is executable.
66826f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
66926f9a767SRodney W. Grimes 	 */
670c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
67126f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
67226f9a767SRodney W. Grimes 	    (attr->va_type != VREG)) {
67326f9a767SRodney W. Grimes 		return (EACCES);
67426f9a767SRodney W. Grimes 	}
67526f9a767SRodney W. Grimes 
67626f9a767SRodney W. Grimes 	/*
67726f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
67826f9a767SRodney W. Grimes 	 */
67926f9a767SRodney W. Grimes 	if (attr->va_size == 0)
68026f9a767SRodney W. Grimes 		return (ENOEXEC);
68126f9a767SRodney W. Grimes 
68226f9a767SRodney W. Grimes 	/*
68326f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
68426f9a767SRodney W. Grimes 	 */
685c52007c2SDavid Greenman 	error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
68626f9a767SRodney W. Grimes 	if (error)
68726f9a767SRodney W. Grimes 		return (error);
68826f9a767SRodney W. Grimes 
6896d5a0a8cSDavid Greenman 	/*
6906d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
6916d5a0a8cSDavid Greenman 	 * if there are any.
6926d5a0a8cSDavid Greenman 	 */
6936d5a0a8cSDavid Greenman 	if (vp->v_writecount)
6946d5a0a8cSDavid Greenman 		return (ETXTBSY);
6956d5a0a8cSDavid Greenman 
6966d5a0a8cSDavid Greenman 	/*
6976d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
6986d5a0a8cSDavid Greenman 	 * general case).
6996d5a0a8cSDavid Greenman 	 */
700c52007c2SDavid Greenman 	error = VOP_OPEN(vp, FREAD, p->p_ucred, p);
70126f9a767SRodney W. Grimes 	if (error)
70226f9a767SRodney W. Grimes 		return (error);
70326f9a767SRodney W. Grimes 
70426f9a767SRodney W. Grimes 	return (0);
705df8bae1dSRodney W. Grimes }
706aa855a59SPeter Wemm 
707aa855a59SPeter Wemm /*
708aa855a59SPeter Wemm  * Exec handler registration
709aa855a59SPeter Wemm  */
710aa855a59SPeter Wemm int
711aa855a59SPeter Wemm exec_register(execsw_arg)
712aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
713aa855a59SPeter Wemm {
714aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
715aa855a59SPeter Wemm 	int count = 2;	/* New slot and trailing NULL */
716aa855a59SPeter Wemm 
717aa855a59SPeter Wemm 	if (execsw)
718aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
719aa855a59SPeter Wemm 			count++;
720aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
721aa855a59SPeter Wemm 	if (newexecsw == NULL)
722aa855a59SPeter Wemm 		return ENOMEM;
723aa855a59SPeter Wemm 	xs = newexecsw;
724aa855a59SPeter Wemm 	if (execsw)
725aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
726aa855a59SPeter Wemm 			*xs++ = *es;
727aa855a59SPeter Wemm 	*xs++ = execsw_arg;
728aa855a59SPeter Wemm 	*xs = NULL;
729aa855a59SPeter Wemm 	if (execsw)
730aa855a59SPeter Wemm 		free(execsw, M_TEMP);
731aa855a59SPeter Wemm 	execsw = newexecsw;
732aa855a59SPeter Wemm 	return 0;
733aa855a59SPeter Wemm }
734aa855a59SPeter Wemm 
735aa855a59SPeter Wemm int
736aa855a59SPeter Wemm exec_unregister(execsw_arg)
737aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
738aa855a59SPeter Wemm {
739aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
740aa855a59SPeter Wemm 	int count = 1;
741aa855a59SPeter Wemm 
742aa855a59SPeter Wemm 	if (execsw == NULL)
743aa855a59SPeter Wemm 		panic("unregister with no handlers left?\n");
744aa855a59SPeter Wemm 
745aa855a59SPeter Wemm 	for (es = execsw; *es; es++) {
746aa855a59SPeter Wemm 		if (*es == execsw_arg)
747aa855a59SPeter Wemm 			break;
748aa855a59SPeter Wemm 	}
749aa855a59SPeter Wemm 	if (*es == NULL)
750aa855a59SPeter Wemm 		return ENOENT;
751aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
752aa855a59SPeter Wemm 		if (*es != execsw_arg)
753aa855a59SPeter Wemm 			count++;
754aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
755aa855a59SPeter Wemm 	if (newexecsw == NULL)
756aa855a59SPeter Wemm 		return ENOMEM;
757aa855a59SPeter Wemm 	xs = newexecsw;
758aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
759aa855a59SPeter Wemm 		if (*es != execsw_arg)
760aa855a59SPeter Wemm 			*xs++ = *es;
761aa855a59SPeter Wemm 	*xs = NULL;
762aa855a59SPeter Wemm 	if (execsw)
763aa855a59SPeter Wemm 		free(execsw, M_TEMP);
764aa855a59SPeter Wemm 	execsw = newexecsw;
765aa855a59SPeter Wemm 	return 0;
766aa855a59SPeter Wemm }
767