xref: /freebsd/sys/kern/kern_exec.c (revision 2395531439bb140427dff4dfd6d67856f907c15e)
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 
29df8bae1dSRodney W. Grimes #include <sys/param.h>
3026f9a767SRodney W. Grimes #include <sys/systm.h>
31fb919e4dSMark Murray #include <sys/lock.h>
32fb919e4dSMark Murray #include <sys/mutex.h>
33d2d3e875SBruce Evans #include <sys/sysproto.h>
3426f9a767SRodney W. Grimes #include <sys/signalvar.h>
3526f9a767SRodney W. Grimes #include <sys/kernel.h>
3626f9a767SRodney W. Grimes #include <sys/mount.h>
37797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
38079cc25bSDavid Greenman #include <sys/fcntl.h>
3926f9a767SRodney W. Grimes #include <sys/acct.h>
4026f9a767SRodney W. Grimes #include <sys/exec.h>
4126f9a767SRodney W. Grimes #include <sys/imgact.h>
42e1743d02SSøren Schmidt #include <sys/imgact_elf.h>
4326f9a767SRodney W. Grimes #include <sys/wait.h>
44a794e791SBruce Evans #include <sys/proc.h>
452a024a2bSSean Eric Fagan #include <sys/pioctl.h>
46aa855a59SPeter Wemm #include <sys/malloc.h>
47a794e791SBruce Evans #include <sys/namei.h>
481e1e0b44SSøren Schmidt #include <sys/sysent.h>
49797f2d22SPoul-Henning Kamp #include <sys/shm.h>
5099ac3bc8SPeter Wemm #include <sys/sysctl.h>
51a794e791SBruce Evans #include <sys/vnode.h>
5226f9a767SRodney W. Grimes 
5326f9a767SRodney W. Grimes #include <vm/vm.h>
54efeaf95aSDavid Greenman #include <vm/vm_param.h>
55efeaf95aSDavid Greenman #include <vm/pmap.h>
561616db3cSJohn Dyson #include <vm/vm_page.h>
57efeaf95aSDavid Greenman #include <vm/vm_map.h>
5826f9a767SRodney W. Grimes #include <vm/vm_kern.h>
59efeaf95aSDavid Greenman #include <vm/vm_extern.h>
601ebd0c59SDavid Greenman #include <vm/vm_object.h>
611616db3cSJohn Dyson #include <vm/vm_pager.h>
6226f9a767SRodney W. Grimes 
6326f9a767SRodney W. Grimes #include <machine/reg.h>
6426f9a767SRodney W. Grimes 
65b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
66b9df5231SPoul-Henning Kamp 
67654f6be1SBruce Evans static register_t *exec_copyout_strings __P((struct image_params *));
68df8bae1dSRodney W. Grimes 
699701cd40SJohn Baldwin /* XXX This should be vm_size_t. */
709701cd40SJohn Baldwin static u_long ps_strings = PS_STRINGS;
719701cd40SJohn Baldwin SYSCTL_ULONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, 0, "");
72486bddb0SDoug Rabson 
739701cd40SJohn Baldwin /* XXX This should be vm_size_t. */
749701cd40SJohn Baldwin static u_long usrstack = USRSTACK;
759701cd40SJohn Baldwin SYSCTL_ULONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, 0, "");
7699ac3bc8SPeter Wemm 
77b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16;
78b9df5231SPoul-Henning Kamp SYSCTL_LONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
799701cd40SJohn Baldwin     &ps_arg_cache_limit, 0, "");
80b9df5231SPoul-Henning Kamp 
81a8704f89SPoul-Henning Kamp int ps_argsopen = 1;
82a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, "");
83a8704f89SPoul-Henning Kamp 
8499ac3bc8SPeter Wemm /*
85aa855a59SPeter Wemm  * Each of the items is a pointer to a `const struct execsw', hence the
86aa855a59SPeter Wemm  * double pointer here.
87df8bae1dSRodney W. Grimes  */
88aa855a59SPeter Wemm static const struct execsw **execsw;
8926f9a767SRodney W. Grimes 
90d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
91ad7507e2SSteven Wallace struct execve_args {
92ad7507e2SSteven Wallace         char    *fname;
93ad7507e2SSteven Wallace         char    **argv;
94ad7507e2SSteven Wallace         char    **envv;
95ad7507e2SSteven Wallace };
96d2d3e875SBruce Evans #endif
97ad7507e2SSteven Wallace 
9826f9a767SRodney W. Grimes /*
9926f9a767SRodney W. Grimes  * execve() system call.
10026f9a767SRodney W. Grimes  */
10126f9a767SRodney W. Grimes int
102cb226aaaSPoul-Henning Kamp execve(p, uap)
10326f9a767SRodney W. Grimes 	struct proc *p;
10426f9a767SRodney W. Grimes 	register struct execve_args *uap;
105df8bae1dSRodney W. Grimes {
10626f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
107654f6be1SBruce Evans 	register_t *stack_base;
108bb56ec4aSPoul-Henning Kamp 	int error, len, i;
109c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
11026f9a767SRodney W. Grimes 	struct vattr attr;
111d323ddf3SMatthew Dillon 	int (*img_first) __P((struct image_params *));
11226f9a767SRodney W. Grimes 
113c52007c2SDavid Greenman 	imgp = &image_params;
114df8bae1dSRodney W. Grimes 
115df8bae1dSRodney W. Grimes 	/*
116c52007c2SDavid Greenman 	 * Initialize part of the common data
117df8bae1dSRodney W. Grimes 	 */
118c52007c2SDavid Greenman 	imgp->proc = p;
119c52007c2SDavid Greenman 	imgp->uap = uap;
120c52007c2SDavid Greenman 	imgp->attr = &attr;
121c52007c2SDavid Greenman 	imgp->argc = imgp->envc = 0;
1225cf3d12cSAndrey A. Chernov 	imgp->argv0 = NULL;
123c52007c2SDavid Greenman 	imgp->entry_addr = 0;
124c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
125c52007c2SDavid Greenman 	imgp->interpreted = 0;
126c52007c2SDavid Greenman 	imgp->interpreter_name[0] = '\0';
127e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1281616db3cSJohn Dyson 	imgp->vp = NULL;
1291616db3cSJohn Dyson 	imgp->firstpage = NULL;
1304fe88fe6SJohn Polstra 	imgp->ps_strings = 0;
131b9a22da4STakanori Watanabe 	imgp->auxarg_size = 0;
13226f9a767SRodney W. Grimes 
13326f9a767SRodney W. Grimes 	/*
13426f9a767SRodney W. Grimes 	 * Allocate temporary demand zeroed space for argument and
13526f9a767SRodney W. Grimes 	 *	environment strings
13626f9a767SRodney W. Grimes 	 */
1371616db3cSJohn Dyson 	imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE);
138c2f9f36bSDavid Greenman 	if (imgp->stringbase == NULL) {
13926f9a767SRodney W. Grimes 		error = ENOMEM;
14026f9a767SRodney W. Grimes 		goto exec_fail;
14126f9a767SRodney W. Grimes 	}
142c52007c2SDavid Greenman 	imgp->stringp = imgp->stringbase;
143c52007c2SDavid Greenman 	imgp->stringspace = ARG_MAX;
1441616db3cSJohn Dyson 	imgp->image_header = imgp->stringbase + ARG_MAX;
14526f9a767SRodney W. Grimes 
14626f9a767SRodney W. Grimes 	/*
14726f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
14826f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
14926f9a767SRodney W. Grimes 	 */
15026f9a767SRodney W. Grimes 	ndp = &nd;
151b35ba931SDavid Greenman 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
1523ed8a403SDavid Greenman 	    UIO_USERSPACE, uap->fname, p);
15326f9a767SRodney W. Grimes 
15426f9a767SRodney W. Grimes interpret:
15526f9a767SRodney W. Grimes 
15626f9a767SRodney W. Grimes 	error = namei(ndp);
15726f9a767SRodney W. Grimes 	if (error) {
1581616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
1591616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
16026f9a767SRodney W. Grimes 		goto exec_fail;
16126f9a767SRodney W. Grimes 	}
16226f9a767SRodney W. Grimes 
163c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
1649c0fed3dSDoug Rabson 	imgp->fname = uap->fname;
16526f9a767SRodney W. Grimes 
16626f9a767SRodney W. Grimes 	/*
1679022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
1689022e4adSDavid Greenman 	 */
169c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
1708677f509SDavid Greenman 	if (error) {
1718677f509SDavid Greenman 		VOP_UNLOCK(imgp->vp, 0, p);
17226f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
1738677f509SDavid Greenman 	}
17426f9a767SRodney W. Grimes 
1751616db3cSJohn Dyson 	error = exec_map_first_page(imgp);
1769caaadb6SDavid Greenman 	VOP_UNLOCK(imgp->vp, 0, p);
1776d5a0a8cSDavid Greenman 	if (error)
17826f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
17926f9a767SRodney W. Grimes 
18026f9a767SRodney W. Grimes 	/*
181d323ddf3SMatthew Dillon 	 *	If the current process has a special image activator it
182d323ddf3SMatthew Dillon 	 *	wants to try first, call it.   For example, emulating shell
183d323ddf3SMatthew Dillon 	 *	scripts differently.
18426f9a767SRodney W. Grimes 	 */
185d323ddf3SMatthew Dillon 	error = -1;
186d323ddf3SMatthew Dillon 	if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL)
187d323ddf3SMatthew Dillon 		error = img_first(imgp);
188d323ddf3SMatthew Dillon 
189d323ddf3SMatthew Dillon 	/*
190d323ddf3SMatthew Dillon 	 *	Loop through the list of image activators, calling each one.
191d323ddf3SMatthew Dillon 	 *	An activator returns -1 if there is no match, 0 on success,
192d323ddf3SMatthew Dillon 	 *	and an error otherwise.
193d323ddf3SMatthew Dillon 	 */
194d323ddf3SMatthew Dillon 	for (i = 0; error == -1 && execsw[i]; ++i) {
195d323ddf3SMatthew Dillon 		if (execsw[i]->ex_imgact == NULL ||
196d323ddf3SMatthew Dillon 		    execsw[i]->ex_imgact == img_first) {
197d323ddf3SMatthew Dillon 			continue;
198d323ddf3SMatthew Dillon 		}
199c52007c2SDavid Greenman 		error = (*execsw[i]->ex_imgact)(imgp);
200d323ddf3SMatthew Dillon 	}
201d323ddf3SMatthew Dillon 
202d323ddf3SMatthew Dillon 	if (error) {
20326f9a767SRodney W. Grimes 		if (error == -1)
204d323ddf3SMatthew Dillon 			error = ENOEXEC;
20526f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
206d323ddf3SMatthew Dillon 	}
207d323ddf3SMatthew Dillon 
208d323ddf3SMatthew Dillon 	/*
209d323ddf3SMatthew Dillon 	 * Special interpreter operation, cleanup and loop up to try to
210d323ddf3SMatthew Dillon 	 * activate the interpreter.
211d323ddf3SMatthew Dillon 	 */
212c52007c2SDavid Greenman 	if (imgp->interpreted) {
2131616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
214762e6b85SEivind Eklund 		/* free name buffer and old vnode */
215762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
216f5277ae7SDavid Greenman 		vrele(ndp->ni_vp);
21726f9a767SRodney W. Grimes 		/* set new name to that of the interpreter */
218b35ba931SDavid Greenman 		NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
219c52007c2SDavid Greenman 		    UIO_SYSSPACE, imgp->interpreter_name, p);
22026f9a767SRodney W. Grimes 		goto interpret;
22126f9a767SRodney W. Grimes 	}
22226f9a767SRodney W. Grimes 
22326f9a767SRodney W. Grimes 	/*
22426f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
22526f9a767SRodney W. Grimes 	 */
226c52007c2SDavid Greenman 	stack_base = exec_copyout_strings(imgp);
22726f9a767SRodney W. Grimes 	p->p_vmspace->vm_minsaddr = (char *)stack_base;
22826f9a767SRodney W. Grimes 
22926f9a767SRodney W. Grimes 	/*
2301e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
2311e1e0b44SSøren Schmidt 	 * let it do the stack setup.
2321e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
23326f9a767SRodney W. Grimes 	 */
2341e1e0b44SSøren Schmidt 	if (p->p_sysent->sv_fixup)
235c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
2361e1e0b44SSøren Schmidt 	else
237c52007c2SDavid Greenman 		suword(--stack_base, imgp->argc);
23826f9a767SRodney W. Grimes 
239a78e8d2aSDavid Greenman 	/*
240a78e8d2aSDavid Greenman 	 * For security and other reasons, the file descriptor table cannot
241a78e8d2aSDavid Greenman 	 * be shared after an exec.
242a78e8d2aSDavid Greenman 	 */
243a78e8d2aSDavid Greenman 	if (p->p_fd->fd_refcnt > 1) {
244a78e8d2aSDavid Greenman 		struct filedesc *tmp;
245a78e8d2aSDavid Greenman 
246a78e8d2aSDavid Greenman 		tmp = fdcopy(p);
247a78e8d2aSDavid Greenman 		fdfree(p);
248a78e8d2aSDavid Greenman 		p->p_fd = tmp;
249a78e8d2aSDavid Greenman 	}
250a78e8d2aSDavid Greenman 
251fdf4e8b3SWarner Losh 	/* Stop profiling */
252fdf4e8b3SWarner Losh 	stopprofclock(p);
253fdf4e8b3SWarner Losh 
25426f9a767SRodney W. Grimes 	/* close files on exec */
25526f9a767SRodney W. Grimes 	fdcloseexec(p);
25626f9a767SRodney W. Grimes 
25726f9a767SRodney W. Grimes 	/* reset caught signals */
25826f9a767SRodney W. Grimes 	execsigs(p);
25926f9a767SRodney W. Grimes 
26026f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
26126f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
26226f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
26326f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
26426f9a767SRodney W. Grimes 
26526f9a767SRodney W. Grimes 	/*
26624b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
267dc733423SDag-Erling Smørgrav 	 * it that it now has its own resources back
26826f9a767SRodney W. Grimes 	 */
269e65897c3SJohn Baldwin 	PROC_LOCK(p);
27026f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
27126f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
27226f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
27326f9a767SRodney W. Grimes 		wakeup((caddr_t)p->p_pptr);
27426f9a767SRodney W. Grimes 	}
27526f9a767SRodney W. Grimes 
27626f9a767SRodney W. Grimes 	/*
277a78e8d2aSDavid Greenman 	 * Implement image setuid/setgid.
278a78e8d2aSDavid Greenman 	 *
279a78e8d2aSDavid Greenman 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
280a78e8d2aSDavid Greenman 	 * the process is being traced.
281c52007c2SDavid Greenman 	 */
2828aef1712SMatthew Dillon 	if ((((attr.va_mode & VSUID) && p->p_ucred->cr_uid != attr.va_uid) ||
2838aef1712SMatthew Dillon 	     ((attr.va_mode & VSGID) && p->p_ucred->cr_gid != attr.va_gid)) &&
284a78e8d2aSDavid Greenman 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
285c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
286e65897c3SJohn Baldwin 		PROC_UNLOCK(p);
287c52007c2SDavid Greenman 		/*
288c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
28926f9a767SRodney W. Grimes 		 * root.
29026f9a767SRodney W. Grimes 		 */
291f711d546SPoul-Henning Kamp 		if (p->p_tracep && suser(p)) {
29226f9a767SRodney W. Grimes 			p->p_traceflag = 0;
29326f9a767SRodney W. Grimes 			vrele(p->p_tracep);
294c52007c2SDavid Greenman 			p->p_tracep = NULL;
29526f9a767SRodney W. Grimes 		}
296c52007c2SDavid Greenman 		/*
297c52007c2SDavid Greenman 		 * Set the new credentials.
298c52007c2SDavid Greenman 		 */
29926f9a767SRodney W. Grimes 		p->p_ucred = crcopy(p->p_ucred);
300c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
301f535380cSDon Lewis 			change_euid(p, attr.va_uid);
302c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
303d021ae3dSGuido van Rooij 			p->p_ucred->cr_gid = attr.va_gid;
304d5f81602SSean Eric Fagan 		setsugid(p);
3055e266442SWarner Losh 		setugidsafety(p);
306c52007c2SDavid Greenman 	} else {
307e47bda07SDavid Greenman 		if (p->p_ucred->cr_uid == p->p_cred->p_ruid &&
308e47bda07SDavid Greenman 		    p->p_ucred->cr_gid == p->p_cred->p_rgid)
309c52007c2SDavid Greenman 			p->p_flag &= ~P_SUGID;
310e65897c3SJohn Baldwin 		PROC_UNLOCK(p);
31126f9a767SRodney W. Grimes 	}
31226f9a767SRodney W. Grimes 
31326f9a767SRodney W. Grimes 	/*
314c52007c2SDavid Greenman 	 * Implement correct POSIX saved-id behavior.
31526f9a767SRodney W. Grimes 	 */
31626f9a767SRodney W. Grimes 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
31726f9a767SRodney W. Grimes 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
31826f9a767SRodney W. Grimes 
31926f9a767SRodney W. Grimes 	/*
3202a531c80SDavid Greenman 	 * Store the vp for use in procfs
3212a531c80SDavid Greenman 	 */
3222a531c80SDavid Greenman 	if (p->p_textvp)		/* release old reference */
3232a531c80SDavid Greenman 		vrele(p->p_textvp);
3242a531c80SDavid Greenman 	VREF(ndp->ni_vp);
3252a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
3262a531c80SDavid Greenman 
3272a531c80SDavid Greenman 	/*
328cb679c38SJonathan Lemon 	 * notify others that we exec'd
329cb679c38SJonathan Lemon 	 */
330e65897c3SJohn Baldwin 	PROC_LOCK(p);
331cb679c38SJonathan Lemon 	KNOTE(&p->p_klist, NOTE_EXEC);
332cb679c38SJonathan Lemon 
333cb679c38SJonathan Lemon 	/*
33426f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
33526f9a767SRodney W. Grimes 	 * 	can be set before the program executes.
33626f9a767SRodney W. Grimes 	 */
337e65897c3SJohn Baldwin 	_STOPEVENT(p, S_EXEC, 0);
3382a024a2bSSean Eric Fagan 
33926f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
34026f9a767SRodney W. Grimes 		psignal(p, SIGTRAP);
34126f9a767SRodney W. Grimes 
34226f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
34326f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
34426f9a767SRodney W. Grimes 
3454fe88fe6SJohn Polstra 	/* Set values passed into the program in registers. */
3464fe88fe6SJohn Polstra 	setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base,
3474fe88fe6SJohn Polstra 	    imgp->ps_strings);
34826f9a767SRodney W. Grimes 
349b9df5231SPoul-Henning Kamp 	/* Free any previous argument cache */
350b9df5231SPoul-Henning Kamp 	if (p->p_args && --p->p_args->ar_ref == 0)
351b9df5231SPoul-Henning Kamp 		FREE(p->p_args, M_PARGS);
352b9df5231SPoul-Henning Kamp 	p->p_args = NULL;
353b9df5231SPoul-Henning Kamp 
354b9df5231SPoul-Henning Kamp 	/* Cache arguments if they fit inside our allowance */
355b9df5231SPoul-Henning Kamp 	i = imgp->endargs - imgp->stringbase;
356b9df5231SPoul-Henning Kamp 	if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
357e65897c3SJohn Baldwin 		PROC_UNLOCK(p);
358b9df5231SPoul-Henning Kamp 		MALLOC(p->p_args, struct pargs *, sizeof(struct pargs) + i,
359b9df5231SPoul-Henning Kamp 		    M_PARGS, M_WAITOK);
360e65897c3SJohn Baldwin 		KASSERT(p->p_args != NULL, ("malloc of p_args failed"));
361e65897c3SJohn Baldwin 		PROC_LOCK(p);
362b9df5231SPoul-Henning Kamp 		p->p_args->ar_ref = 1;
363b9df5231SPoul-Henning Kamp 		p->p_args->ar_length = i;
364b9df5231SPoul-Henning Kamp 		bcopy(imgp->stringbase, p->p_args->ar_args, i);
365b9df5231SPoul-Henning Kamp 	}
366e65897c3SJohn Baldwin 	PROC_UNLOCK(p);
367b9df5231SPoul-Henning Kamp 
3681616db3cSJohn Dyson exec_fail_dealloc:
3691616db3cSJohn Dyson 
37026f9a767SRodney W. Grimes 	/*
37126f9a767SRodney W. Grimes 	 * free various allocated resources
37226f9a767SRodney W. Grimes 	 */
3731616db3cSJohn Dyson 	if (imgp->firstpage)
3741616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
37526f9a767SRodney W. Grimes 
376c2f9f36bSDavid Greenman 	if (imgp->stringbase != NULL)
3771616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
3781616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
3791616db3cSJohn Dyson 
3809c0fed3dSDoug Rabson 	if (imgp->vp) {
381762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
3829c0fed3dSDoug Rabson 		vrele(imgp->vp);
383a3cf6ebaSDavid Greenman 	}
38426f9a767SRodney W. Grimes 
3851616db3cSJohn Dyson 	if (error == 0)
3861616db3cSJohn Dyson 		return (0);
3871616db3cSJohn Dyson 
38826f9a767SRodney W. Grimes exec_fail:
389c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
39026f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
39126f9a767SRodney W. Grimes 		exit1(p, W_EXITCODE(0, SIGABRT));
39226f9a767SRodney W. Grimes 		/* NOT REACHED */
39326f9a767SRodney W. Grimes 		return(0);
39426f9a767SRodney W. Grimes 	} else {
39526f9a767SRodney W. Grimes 		return(error);
39626f9a767SRodney W. Grimes 	}
39726f9a767SRodney W. Grimes }
39826f9a767SRodney W. Grimes 
3991616db3cSJohn Dyson int
4001616db3cSJohn Dyson exec_map_first_page(imgp)
4011616db3cSJohn Dyson 	struct image_params *imgp;
4021616db3cSJohn Dyson {
40395461b45SJohn Dyson 	int s, rv, i;
40495461b45SJohn Dyson 	int initial_pagein;
40595461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
4061616db3cSJohn Dyson 	vm_object_t object;
4071616db3cSJohn Dyson 
4081616db3cSJohn Dyson 
4091616db3cSJohn Dyson 	if (imgp->firstpage) {
4101616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
4111616db3cSJohn Dyson 	}
4121616db3cSJohn Dyson 
4139ff5ce6bSBoris Popov 	VOP_GETVOBJECT(imgp->vp, &object);
4141616db3cSJohn Dyson 	s = splvm();
41523955314SAlfred Perlstein 	mtx_lock(&vm_mtx);
4161616db3cSJohn Dyson 
41795461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
4181616db3cSJohn Dyson 
41995461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
42095461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
42195461b45SJohn Dyson 		if (initial_pagein > object->size)
42295461b45SJohn Dyson 			initial_pagein = object->size;
42395461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
424d254af07SMatthew Dillon 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
42595461b45SJohn Dyson 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
42695461b45SJohn Dyson 					break;
42795461b45SJohn Dyson 				if (ma[i]->valid)
42895461b45SJohn Dyson 					break;
429e69763a3SDoug Rabson 				vm_page_busy(ma[i]);
43095461b45SJohn Dyson 			} else {
43195461b45SJohn Dyson 				ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
43295461b45SJohn Dyson 				if (ma[i] == NULL)
43395461b45SJohn Dyson 					break;
43495461b45SJohn Dyson 			}
43595461b45SJohn Dyson 		}
43695461b45SJohn Dyson 		initial_pagein = i;
4371616db3cSJohn Dyson 
43895461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
43995461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
44095461b45SJohn Dyson 
441eed2412eSJohn Dyson 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) {
442ecbb00a2SDoug Rabson 			if (ma[0]) {
44395461b45SJohn Dyson 				vm_page_protect(ma[0], VM_PROT_NONE);
4448f9110f6SJohn Dyson 				vm_page_free(ma[0]);
445ecbb00a2SDoug Rabson 			}
4461616db3cSJohn Dyson 			splx(s);
44723955314SAlfred Perlstein 			mtx_unlock(&vm_mtx);
4481616db3cSJohn Dyson 			return EIO;
4491616db3cSJohn Dyson 		}
4501616db3cSJohn Dyson 	}
4511616db3cSJohn Dyson 
45295461b45SJohn Dyson 	vm_page_wire(ma[0]);
453e69763a3SDoug Rabson 	vm_page_wakeup(ma[0]);
4541616db3cSJohn Dyson 	splx(s);
4551616db3cSJohn Dyson 
45695461b45SJohn Dyson 	pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0]));
45795461b45SJohn Dyson 	imgp->firstpage = ma[0];
4581616db3cSJohn Dyson 
45923955314SAlfred Perlstein 	mtx_unlock(&vm_mtx);
4601616db3cSJohn Dyson 	return 0;
4611616db3cSJohn Dyson }
4621616db3cSJohn Dyson 
4631616db3cSJohn Dyson void
4641616db3cSJohn Dyson exec_unmap_first_page(imgp)
4651616db3cSJohn Dyson 	struct image_params *imgp;
4661616db3cSJohn Dyson {
46723955314SAlfred Perlstein 
4681616db3cSJohn Dyson 	if (imgp->firstpage) {
46923955314SAlfred Perlstein 		mtx_lock(&vm_mtx);
4701616db3cSJohn Dyson 		pmap_kremove((vm_offset_t) imgp->image_header);
47173007561SDavid Greenman 		vm_page_unwire(imgp->firstpage, 1);
47223955314SAlfred Perlstein 		mtx_unlock(&vm_mtx);
4731616db3cSJohn Dyson 		imgp->firstpage = NULL;
4741616db3cSJohn Dyson 	}
4751616db3cSJohn Dyson }
4761616db3cSJohn Dyson 
47726f9a767SRodney W. Grimes /*
47826f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
47926f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
48026f9a767SRodney W. Grimes  *	automatically in trap.c.
48126f9a767SRodney W. Grimes  */
48226f9a767SRodney W. Grimes int
483c52007c2SDavid Greenman exec_new_vmspace(imgp)
484c52007c2SDavid Greenman 	struct image_params *imgp;
48526f9a767SRodney W. Grimes {
48626f9a767SRodney W. Grimes 	int error;
487c52007c2SDavid Greenman 	struct vmspace *vmspace = imgp->proc->p_vmspace;
4882267af78SJulian Elischer 	caddr_t	stack_addr = (caddr_t) (USRSTACK - MAXSSIZ);
489af9ec885SJohn Dyson 	vm_map_t map = &vmspace->vm_map;
49026f9a767SRodney W. Grimes 
49123955314SAlfred Perlstein 	mtx_assert(&vm_mtx, MA_OWNED);
492c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
49326f9a767SRodney W. Grimes 
494af9ec885SJohn Dyson 	/*
495af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
496af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
497af9ec885SJohn Dyson 	 * not disrupted
498af9ec885SJohn Dyson 	 */
499af9ec885SJohn Dyson 	if (vmspace->vm_refcnt == 1) {
5003d903220SDoug Rabson 		if (vmspace->vm_shm)
501c52007c2SDavid Greenman 			shmexit(imgp->proc);
502b1028ad1SLuoqi Chen 		pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS);
5039c0fed3dSDoug Rabson 		vm_map_remove(map, 0, VM_MAXUSER_ADDRESS);
504af9ec885SJohn Dyson 	} else {
505492da96cSJohn Dyson 		vmspace_exec(imgp->proc);
506492da96cSJohn Dyson 		vmspace = imgp->proc->p_vmspace;
507af9ec885SJohn Dyson 		map = &vmspace->vm_map;
508af9ec885SJohn Dyson 	}
50926f9a767SRodney W. Grimes 
51026f9a767SRodney W. Grimes 	/* Allocate a new stack */
5112267af78SJulian Elischer 	error = vm_map_stack (&vmspace->vm_map, (vm_offset_t)stack_addr,
5122267af78SJulian Elischer 			      (vm_size_t)MAXSSIZ, VM_PROT_ALL, VM_PROT_ALL, 0);
5132267af78SJulian Elischer 	if (error)
5142267af78SJulian Elischer 		return (error);
5152267af78SJulian Elischer 
51663c47a5cSDoug Rabson #ifdef __ia64__
51763c47a5cSDoug Rabson 	{
51863c47a5cSDoug Rabson 		/*
51963c47a5cSDoug Rabson 		 * Allocate backing store. We really need something
52063c47a5cSDoug Rabson 		 * similar to vm_map_stack which can allow the backing
52163c47a5cSDoug Rabson 		 * store to grow upwards. This will do for now.
52263c47a5cSDoug Rabson 		 */
52363c47a5cSDoug Rabson 		vm_offset_t bsaddr;
52463c47a5cSDoug Rabson 		bsaddr = USRSTACK - 2*MAXSSIZ;
52563c47a5cSDoug Rabson 		error = vm_map_find(&vmspace->vm_map, 0, 0, &bsaddr,
52663c47a5cSDoug Rabson 				    4*PAGE_SIZE, 0,
52763c47a5cSDoug Rabson 				    VM_PROT_ALL, VM_PROT_ALL, 0);
52863c47a5cSDoug Rabson 		imgp->proc->p_md.md_bspstore = bsaddr;
52963c47a5cSDoug Rabson 	}
53063c47a5cSDoug Rabson #endif
53163c47a5cSDoug Rabson 
5322267af78SJulian Elischer 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
5332267af78SJulian Elischer 	 * VM_STACK case, but they are still used to monitor the size of the
5342267af78SJulian Elischer 	 * process stack so we can check the stack rlimit.
5352267af78SJulian Elischer 	 */
5362267af78SJulian Elischer 	vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT;
5372267af78SJulian Elischer 	vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ;
53826f9a767SRodney W. Grimes 
53926f9a767SRodney W. Grimes 	return(0);
54026f9a767SRodney W. Grimes }
54126f9a767SRodney W. Grimes 
54226f9a767SRodney W. Grimes /*
54326f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
54426f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
54526f9a767SRodney W. Grimes  */
54626f9a767SRodney W. Grimes int
547c52007c2SDavid Greenman exec_extract_strings(imgp)
548c52007c2SDavid Greenman 	struct image_params *imgp;
54926f9a767SRodney W. Grimes {
55026f9a767SRodney W. Grimes 	char	**argv, **envv;
55126f9a767SRodney W. Grimes 	char	*argp, *envp;
552ecbb00a2SDoug Rabson 	int	error;
553ecbb00a2SDoug Rabson 	size_t	length;
55426f9a767SRodney W. Grimes 
55526f9a767SRodney W. Grimes 	/*
55626f9a767SRodney W. Grimes 	 * extract arguments first
55726f9a767SRodney W. Grimes 	 */
55826f9a767SRodney W. Grimes 
559c52007c2SDavid Greenman 	argv = imgp->uap->argv;
56026f9a767SRodney W. Grimes 
5610fd1a014SDavid Greenman 	if (argv) {
562aae0aa45SBruce Evans 		argp = (caddr_t) (intptr_t) fuword(argv);
5635cf3d12cSAndrey A. Chernov 		if (argp == (caddr_t) -1)
5645cf3d12cSAndrey A. Chernov 			return (EFAULT);
5655cf3d12cSAndrey A. Chernov 		if (argp)
5665cf3d12cSAndrey A. Chernov 			argv++;
5675cf3d12cSAndrey A. Chernov 		if (imgp->argv0)
5685cf3d12cSAndrey A. Chernov 			argp = imgp->argv0;
5695cf3d12cSAndrey A. Chernov 		if (argp) {
5705cf3d12cSAndrey A. Chernov 			do {
57126f9a767SRodney W. Grimes 				if (argp == (caddr_t) -1)
57226f9a767SRodney W. Grimes 					return (EFAULT);
573c52007c2SDavid Greenman 				if ((error = copyinstr(argp, imgp->stringp,
574c52007c2SDavid Greenman 				    imgp->stringspace, &length))) {
5750fd1a014SDavid Greenman 					if (error == ENAMETOOLONG)
57626f9a767SRodney W. Grimes 						return(E2BIG);
5770fd1a014SDavid Greenman 					return (error);
5780fd1a014SDavid Greenman 				}
579c52007c2SDavid Greenman 				imgp->stringspace -= length;
580c52007c2SDavid Greenman 				imgp->stringp += length;
581c52007c2SDavid Greenman 				imgp->argc++;
582aae0aa45SBruce Evans 			} while ((argp = (caddr_t) (intptr_t) fuword(argv++)));
58326f9a767SRodney W. Grimes 		}
5840fd1a014SDavid Greenman 	}
58526f9a767SRodney W. Grimes 
586b9df5231SPoul-Henning Kamp 	imgp->endargs = imgp->stringp;
587b9df5231SPoul-Henning Kamp 
58826f9a767SRodney W. Grimes 	/*
58926f9a767SRodney W. Grimes 	 * extract environment strings
59026f9a767SRodney W. Grimes 	 */
59126f9a767SRodney W. Grimes 
592c52007c2SDavid Greenman 	envv = imgp->uap->envv;
59326f9a767SRodney W. Grimes 
5940fd1a014SDavid Greenman 	if (envv) {
595aae0aa45SBruce Evans 		while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
59626f9a767SRodney W. Grimes 			if (envp == (caddr_t) -1)
59726f9a767SRodney W. Grimes 				return (EFAULT);
598c52007c2SDavid Greenman 			if ((error = copyinstr(envp, imgp->stringp,
599c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
6000fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
60126f9a767SRodney W. Grimes 					return(E2BIG);
6020fd1a014SDavid Greenman 				return (error);
6030fd1a014SDavid Greenman 			}
604c52007c2SDavid Greenman 			imgp->stringspace -= length;
605c52007c2SDavid Greenman 			imgp->stringp += length;
606c52007c2SDavid Greenman 			imgp->envc++;
60726f9a767SRodney W. Grimes 		}
6080fd1a014SDavid Greenman 	}
60926f9a767SRodney W. Grimes 
61026f9a767SRodney W. Grimes 	return (0);
61126f9a767SRodney W. Grimes }
61226f9a767SRodney W. Grimes 
61326f9a767SRodney W. Grimes /*
61426f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
61526f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
61626f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
61726f9a767SRodney W. Grimes  */
618654f6be1SBruce Evans register_t *
619c52007c2SDavid Greenman exec_copyout_strings(imgp)
620c52007c2SDavid Greenman 	struct image_params *imgp;
62126f9a767SRodney W. Grimes {
62226f9a767SRodney W. Grimes 	int argc, envc;
62326f9a767SRodney W. Grimes 	char **vectp;
62426f9a767SRodney W. Grimes 	char *stringp, *destp;
625654f6be1SBruce Evans 	register_t *stack_base;
62693f6448cSDavid Greenman 	struct ps_strings *arginfo;
627d66a5066SPeter Wemm 	int szsigcode;
62826f9a767SRodney W. Grimes 
62926f9a767SRodney W. Grimes 	/*
63026f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
631d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
63226f9a767SRodney W. Grimes 	 */
6334c56fcdeSBruce Evans 	arginfo = (struct ps_strings *)PS_STRINGS;
634d66a5066SPeter Wemm 	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
635d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
6361ed012f9SPeter Wemm 		roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
6371ed012f9SPeter Wemm 
63826f9a767SRodney W. Grimes 	/*
639d66a5066SPeter Wemm 	 * install sigcode
640d66a5066SPeter Wemm 	 */
641d66a5066SPeter Wemm 	if (szsigcode)
642d66a5066SPeter Wemm 		copyout(imgp->proc->p_sysent->sv_sigcode,
643d66a5066SPeter Wemm 			((caddr_t)arginfo - szsigcode), szsigcode);
644d66a5066SPeter Wemm 
645d66a5066SPeter Wemm 	/*
646e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
647e1743d02SSøren Schmidt 	 * on the stack.
648e1743d02SSøren Schmidt 	 */
649b9a22da4STakanori Watanabe 	if (imgp->auxargs) {
650e1743d02SSøren Schmidt 		/*
651b9a22da4STakanori Watanabe 		 * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for
652b9a22da4STakanori Watanabe 		 * lower compatibility.
653b9a22da4STakanori Watanabe 		 */
654b9a22da4STakanori Watanabe 		imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size
655b9a22da4STakanori Watanabe 			: (AT_COUNT * 2);
656b9a22da4STakanori Watanabe 		/*
657b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
658b9a22da4STakanori Watanabe 		 * the arg and env vector sets,and imgp->auxarg_size is room
659b9a22da4STakanori Watanabe 		 * for argument of Runtime loader.
660e1743d02SSøren Schmidt 		 */
661e1743d02SSøren Schmidt 		vectp = (char **) (destp - (imgp->argc + imgp->envc + 2 +
662b9a22da4STakanori Watanabe 				       imgp->auxarg_size) * sizeof(char *));
663b9a22da4STakanori Watanabe 
664b9a22da4STakanori Watanabe 	} else
665e1743d02SSøren Schmidt 		/*
666b9a22da4STakanori Watanabe 		 * The '+ 2' is for the null pointers at the end of each of
667b9a22da4STakanori Watanabe 		 * the arg and env vector sets
66826f9a767SRodney W. Grimes 		 */
669e1743d02SSøren Schmidt 		vectp = (char **)
670e1743d02SSøren Schmidt 			(destp - (imgp->argc + imgp->envc + 2) * sizeof(char *));
67126f9a767SRodney W. Grimes 
67226f9a767SRodney W. Grimes 	/*
67326f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
67426f9a767SRodney W. Grimes 	 */
675654f6be1SBruce Evans 	stack_base = (register_t *)vectp;
67626f9a767SRodney W. Grimes 
677c52007c2SDavid Greenman 	stringp = imgp->stringbase;
678c52007c2SDavid Greenman 	argc = imgp->argc;
679c52007c2SDavid Greenman 	envc = imgp->envc;
68026f9a767SRodney W. Grimes 
68193f6448cSDavid Greenman 	/*
6823aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
68393f6448cSDavid Greenman 	 */
684c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
68593f6448cSDavid Greenman 
68693f6448cSDavid Greenman 	/*
6873aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
6883aed948bSDavid Greenman 	 */
689aae0aa45SBruce Evans 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
6903aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
6913aed948bSDavid Greenman 
6923aed948bSDavid Greenman 	/*
6933aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
69493f6448cSDavid Greenman 	 */
69526f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
696aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
6973aed948bSDavid Greenman 		while (*stringp++ != 0)
6983aed948bSDavid Greenman 			destp++;
6993aed948bSDavid Greenman 		destp++;
70026f9a767SRodney W. Grimes 	}
70126f9a767SRodney W. Grimes 
7021a6e52d0SJeroen Ruigrok van der Werven 	/* a null vector table pointer separates the argp's from the envp's */
7036ab46d52SBruce Evans 	suword(vectp++, 0);
70426f9a767SRodney W. Grimes 
705aae0aa45SBruce Evans 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
7063aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
70793f6448cSDavid Greenman 
70893f6448cSDavid Greenman 	/*
7093aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
71093f6448cSDavid Greenman 	 */
71126f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
712aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
7133aed948bSDavid Greenman 		while (*stringp++ != 0)
7143aed948bSDavid Greenman 			destp++;
7153aed948bSDavid Greenman 		destp++;
71626f9a767SRodney W. Grimes 	}
71726f9a767SRodney W. Grimes 
71826f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
7196ab46d52SBruce Evans 	suword(vectp, 0);
72026f9a767SRodney W. Grimes 
72126f9a767SRodney W. Grimes 	return (stack_base);
72226f9a767SRodney W. Grimes }
72326f9a767SRodney W. Grimes 
72426f9a767SRodney W. Grimes /*
72526f9a767SRodney W. Grimes  * Check permissions of file to execute.
726cf64863aSRobert Watson  *	Called with imgp->vp locked.
72726f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
72826f9a767SRodney W. Grimes  */
729c8a79999SPeter Wemm int
730c52007c2SDavid Greenman exec_check_permissions(imgp)
731c52007c2SDavid Greenman 	struct image_params *imgp;
73226f9a767SRodney W. Grimes {
733c52007c2SDavid Greenman 	struct proc *p = imgp->proc;
734c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
735c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
73626f9a767SRodney W. Grimes 	int error;
73726f9a767SRodney W. Grimes 
73826f9a767SRodney W. Grimes 	/* Get file attributes */
739c52007c2SDavid Greenman 	error = VOP_GETATTR(vp, attr, p->p_ucred, p);
74026f9a767SRodney W. Grimes 	if (error)
74126f9a767SRodney W. Grimes 		return (error);
74226f9a767SRodney W. Grimes 
74326f9a767SRodney W. Grimes 	/*
74426f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
74526f9a767SRodney W. Grimes 	 *	file resides on.
74626f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
74726f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
74826f9a767SRodney W. Grimes 	 *	file really is executable.
74926f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
75026f9a767SRodney W. Grimes 	 */
751c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
75226f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
75326f9a767SRodney W. Grimes 	    (attr->va_type != VREG)) {
75426f9a767SRodney W. Grimes 		return (EACCES);
75526f9a767SRodney W. Grimes 	}
75626f9a767SRodney W. Grimes 
75726f9a767SRodney W. Grimes 	/*
75826f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
75926f9a767SRodney W. Grimes 	 */
76026f9a767SRodney W. Grimes 	if (attr->va_size == 0)
76126f9a767SRodney W. Grimes 		return (ENOEXEC);
76226f9a767SRodney W. Grimes 
76326f9a767SRodney W. Grimes 	/*
76426f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
76526f9a767SRodney W. Grimes 	 */
766c52007c2SDavid Greenman 	error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
76726f9a767SRodney W. Grimes 	if (error)
76826f9a767SRodney W. Grimes 		return (error);
76926f9a767SRodney W. Grimes 
7706d5a0a8cSDavid Greenman 	/*
7716d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
7726d5a0a8cSDavid Greenman 	 * if there are any.
7736d5a0a8cSDavid Greenman 	 */
7746d5a0a8cSDavid Greenman 	if (vp->v_writecount)
7756d5a0a8cSDavid Greenman 		return (ETXTBSY);
7766d5a0a8cSDavid Greenman 
7776d5a0a8cSDavid Greenman 	/*
7786d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
7796d5a0a8cSDavid Greenman 	 * general case).
7806d5a0a8cSDavid Greenman 	 */
781c52007c2SDavid Greenman 	error = VOP_OPEN(vp, FREAD, p->p_ucred, p);
78226f9a767SRodney W. Grimes 	if (error)
78326f9a767SRodney W. Grimes 		return (error);
78426f9a767SRodney W. Grimes 
78526f9a767SRodney W. Grimes 	return (0);
786df8bae1dSRodney W. Grimes }
787aa855a59SPeter Wemm 
788aa855a59SPeter Wemm /*
789aa855a59SPeter Wemm  * Exec handler registration
790aa855a59SPeter Wemm  */
791aa855a59SPeter Wemm int
792aa855a59SPeter Wemm exec_register(execsw_arg)
793aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
794aa855a59SPeter Wemm {
795aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
796aa855a59SPeter Wemm 	int count = 2;	/* New slot and trailing NULL */
797aa855a59SPeter Wemm 
798aa855a59SPeter Wemm 	if (execsw)
799aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
800aa855a59SPeter Wemm 			count++;
801aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
802aa855a59SPeter Wemm 	if (newexecsw == NULL)
803aa855a59SPeter Wemm 		return ENOMEM;
804aa855a59SPeter Wemm 	xs = newexecsw;
805aa855a59SPeter Wemm 	if (execsw)
806aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
807aa855a59SPeter Wemm 			*xs++ = *es;
808aa855a59SPeter Wemm 	*xs++ = execsw_arg;
809aa855a59SPeter Wemm 	*xs = NULL;
810aa855a59SPeter Wemm 	if (execsw)
811aa855a59SPeter Wemm 		free(execsw, M_TEMP);
812aa855a59SPeter Wemm 	execsw = newexecsw;
813aa855a59SPeter Wemm 	return 0;
814aa855a59SPeter Wemm }
815aa855a59SPeter Wemm 
816aa855a59SPeter Wemm int
817aa855a59SPeter Wemm exec_unregister(execsw_arg)
818aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
819aa855a59SPeter Wemm {
820aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
821aa855a59SPeter Wemm 	int count = 1;
822aa855a59SPeter Wemm 
823aa855a59SPeter Wemm 	if (execsw == NULL)
824aa855a59SPeter Wemm 		panic("unregister with no handlers left?\n");
825aa855a59SPeter Wemm 
826aa855a59SPeter Wemm 	for (es = execsw; *es; es++) {
827aa855a59SPeter Wemm 		if (*es == execsw_arg)
828aa855a59SPeter Wemm 			break;
829aa855a59SPeter Wemm 	}
830aa855a59SPeter Wemm 	if (*es == NULL)
831aa855a59SPeter Wemm 		return ENOENT;
832aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
833aa855a59SPeter Wemm 		if (*es != execsw_arg)
834aa855a59SPeter Wemm 			count++;
835aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
836aa855a59SPeter Wemm 	if (newexecsw == NULL)
837aa855a59SPeter Wemm 		return ENOMEM;
838aa855a59SPeter Wemm 	xs = newexecsw;
839aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
840aa855a59SPeter Wemm 		if (*es != execsw_arg)
841aa855a59SPeter Wemm 			*xs++ = *es;
842aa855a59SPeter Wemm 	*xs = NULL;
843aa855a59SPeter Wemm 	if (execsw)
844aa855a59SPeter Wemm 		free(execsw, M_TEMP);
845aa855a59SPeter Wemm 	execsw = newexecsw;
846aa855a59SPeter Wemm 	return 0;
847aa855a59SPeter Wemm }
848