xref: /freebsd/sys/kern/kern_exec.c (revision 762e6b856c64ee48f286a7f0b45d0067e556b252)
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>
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>
54996c772fSJohn Dyson #include <sys/lock.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>
61675ea6f0SBruce Evans #include <vm/vm_zone.h>
621616db3cSJohn Dyson #include <vm/vm_pager.h>
6326f9a767SRodney W. Grimes 
6426f9a767SRodney W. Grimes #include <machine/reg.h>
6526f9a767SRodney W. Grimes 
66b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments");
67b9df5231SPoul-Henning Kamp 
68ecbb00a2SDoug Rabson static long *exec_copyout_strings __P((struct image_params *));
69df8bae1dSRodney W. Grimes 
70486bddb0SDoug Rabson static long ps_strings = PS_STRINGS;
71486bddb0SDoug Rabson SYSCTL_LONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, "");
72486bddb0SDoug Rabson 
73486bddb0SDoug Rabson static long usrstack = USRSTACK;
74486bddb0SDoug Rabson SYSCTL_LONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, "");
7599ac3bc8SPeter Wemm 
76b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16;
77b9df5231SPoul-Henning Kamp SYSCTL_LONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW,
78b9df5231SPoul-Henning Kamp     &ps_arg_cache_limit, "");
79b9df5231SPoul-Henning Kamp 
80a8704f89SPoul-Henning Kamp int ps_argsopen = 1;
81a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, "");
82a8704f89SPoul-Henning Kamp 
8399ac3bc8SPeter Wemm /*
84aa855a59SPeter Wemm  * Each of the items is a pointer to a `const struct execsw', hence the
85aa855a59SPeter Wemm  * double pointer here.
86df8bae1dSRodney W. Grimes  */
87aa855a59SPeter Wemm static const struct execsw **execsw;
8826f9a767SRodney W. Grimes 
89d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_
90ad7507e2SSteven Wallace struct execve_args {
91ad7507e2SSteven Wallace         char    *fname;
92ad7507e2SSteven Wallace         char    **argv;
93ad7507e2SSteven Wallace         char    **envv;
94ad7507e2SSteven Wallace };
95d2d3e875SBruce Evans #endif
96ad7507e2SSteven Wallace 
9726f9a767SRodney W. Grimes /*
9826f9a767SRodney W. Grimes  * execve() system call.
9926f9a767SRodney W. Grimes  */
10026f9a767SRodney W. Grimes int
101cb226aaaSPoul-Henning Kamp execve(p, uap)
10226f9a767SRodney W. Grimes 	struct proc *p;
10326f9a767SRodney W. Grimes 	register struct execve_args *uap;
104df8bae1dSRodney W. Grimes {
10526f9a767SRodney W. Grimes 	struct nameidata nd, *ndp;
106ecbb00a2SDoug Rabson 	long *stack_base;
107bb56ec4aSPoul-Henning Kamp 	int error, len, i;
108c52007c2SDavid Greenman 	struct image_params image_params, *imgp;
10926f9a767SRodney W. Grimes 	struct vattr attr;
11026f9a767SRodney W. Grimes 
111c52007c2SDavid Greenman 	imgp = &image_params;
112df8bae1dSRodney W. Grimes 
113df8bae1dSRodney W. Grimes 	/*
114c52007c2SDavid Greenman 	 * Initialize part of the common data
115df8bae1dSRodney W. Grimes 	 */
116c52007c2SDavid Greenman 	imgp->proc = p;
117c52007c2SDavid Greenman 	imgp->uap = uap;
118c52007c2SDavid Greenman 	imgp->attr = &attr;
119c52007c2SDavid Greenman 	imgp->argc = imgp->envc = 0;
1205cf3d12cSAndrey A. Chernov 	imgp->argv0 = NULL;
121c52007c2SDavid Greenman 	imgp->entry_addr = 0;
122c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 0;
123c52007c2SDavid Greenman 	imgp->interpreted = 0;
124c52007c2SDavid Greenman 	imgp->interpreter_name[0] = '\0';
125e1743d02SSøren Schmidt 	imgp->auxargs = NULL;
1261616db3cSJohn Dyson 	imgp->vp = NULL;
1271616db3cSJohn Dyson 	imgp->firstpage = NULL;
1284fe88fe6SJohn Polstra 	imgp->ps_strings = 0;
12926f9a767SRodney W. Grimes 
13026f9a767SRodney W. Grimes 	/*
13126f9a767SRodney W. Grimes 	 * Allocate temporary demand zeroed space for argument and
13226f9a767SRodney W. Grimes 	 *	environment strings
13326f9a767SRodney W. Grimes 	 */
1341616db3cSJohn Dyson 	imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE);
135c2f9f36bSDavid Greenman 	if (imgp->stringbase == NULL) {
13626f9a767SRodney W. Grimes 		error = ENOMEM;
13726f9a767SRodney W. Grimes 		goto exec_fail;
13826f9a767SRodney W. Grimes 	}
139c52007c2SDavid Greenman 	imgp->stringp = imgp->stringbase;
140c52007c2SDavid Greenman 	imgp->stringspace = ARG_MAX;
1411616db3cSJohn Dyson 	imgp->image_header = imgp->stringbase + ARG_MAX;
14226f9a767SRodney W. Grimes 
14326f9a767SRodney W. Grimes 	/*
14426f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
14526f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
14626f9a767SRodney W. Grimes 	 */
14726f9a767SRodney W. Grimes 	ndp = &nd;
148b35ba931SDavid Greenman 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
1493ed8a403SDavid Greenman 	    UIO_USERSPACE, uap->fname, p);
15026f9a767SRodney W. Grimes 
15126f9a767SRodney W. Grimes interpret:
15226f9a767SRodney W. Grimes 
15326f9a767SRodney W. Grimes 	error = namei(ndp);
15426f9a767SRodney W. Grimes 	if (error) {
1551616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
1561616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
15726f9a767SRodney W. Grimes 		goto exec_fail;
15826f9a767SRodney W. Grimes 	}
15926f9a767SRodney W. Grimes 
160c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
1619c0fed3dSDoug Rabson 	imgp->fname = uap->fname;
16226f9a767SRodney W. Grimes 
16326f9a767SRodney W. Grimes 	/*
1649022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
1659022e4adSDavid Greenman 	 */
166c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
1678677f509SDavid Greenman 	if (error) {
1688677f509SDavid Greenman 		VOP_UNLOCK(imgp->vp, 0, p);
16926f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
1708677f509SDavid Greenman 	}
17126f9a767SRodney W. Grimes 
1721616db3cSJohn Dyson 	error = exec_map_first_page(imgp);
1739caaadb6SDavid Greenman 	VOP_UNLOCK(imgp->vp, 0, p);
1746d5a0a8cSDavid Greenman 	if (error)
17526f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
17626f9a767SRodney W. Grimes 
17726f9a767SRodney W. Grimes 	/*
17826f9a767SRodney W. Grimes 	 * Loop through list of image activators, calling each one.
17926f9a767SRodney W. Grimes 	 *	If there is no match, the activator returns -1. If there
18026f9a767SRodney W. Grimes 	 *	is a match, but there was an error during the activation,
18126f9a767SRodney W. Grimes 	 *	the error is returned. Otherwise 0 means success. If the
18226f9a767SRodney W. Grimes 	 *	image is interpreted, loop back up and try activating
18326f9a767SRodney W. Grimes 	 *	the interpreter.
18426f9a767SRodney W. Grimes 	 */
18526f9a767SRodney W. Grimes 	for (i = 0; execsw[i]; ++i) {
18626f9a767SRodney W. Grimes 		if (execsw[i]->ex_imgact)
187c52007c2SDavid Greenman 			error = (*execsw[i]->ex_imgact)(imgp);
18826f9a767SRodney W. Grimes 		else
18926f9a767SRodney W. Grimes 			continue;
19026f9a767SRodney W. Grimes 		if (error == -1)
19126f9a767SRodney W. Grimes 			continue;
19226f9a767SRodney W. Grimes 		if (error)
19326f9a767SRodney W. Grimes 			goto exec_fail_dealloc;
194c52007c2SDavid Greenman 		if (imgp->interpreted) {
1951616db3cSJohn Dyson 			exec_unmap_first_page(imgp);
196762e6b85SEivind Eklund 			/* free name buffer and old vnode */
197762e6b85SEivind Eklund 			NDFREE(ndp, NDF_ONLY_PNBUF);
198f5277ae7SDavid Greenman 			vrele(ndp->ni_vp);
19926f9a767SRodney W. Grimes 			/* set new name to that of the interpreter */
200b35ba931SDavid Greenman 			NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
201c52007c2SDavid Greenman 			    UIO_SYSSPACE, imgp->interpreter_name, p);
20226f9a767SRodney W. Grimes 			goto interpret;
20326f9a767SRodney W. Grimes 		}
20426f9a767SRodney W. Grimes 		break;
20526f9a767SRodney W. Grimes 	}
20626f9a767SRodney W. Grimes 	/* If we made it through all the activators and none matched, exit. */
20726f9a767SRodney W. Grimes 	if (error == -1) {
20826f9a767SRodney W. Grimes 		error = ENOEXEC;
20926f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
21026f9a767SRodney W. Grimes 	}
21126f9a767SRodney W. Grimes 
21226f9a767SRodney W. Grimes 	/*
21326f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
21426f9a767SRodney W. Grimes 	 */
215c52007c2SDavid Greenman 	stack_base = exec_copyout_strings(imgp);
21626f9a767SRodney W. Grimes 	p->p_vmspace->vm_minsaddr = (char *)stack_base;
21726f9a767SRodney W. Grimes 
21826f9a767SRodney W. Grimes 	/*
2191e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
2201e1e0b44SSøren Schmidt 	 * let it do the stack setup.
2211e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
22226f9a767SRodney W. Grimes 	 */
2231e1e0b44SSøren Schmidt 	if (p->p_sysent->sv_fixup)
224c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
2251e1e0b44SSøren Schmidt 	else
226c52007c2SDavid Greenman 		suword(--stack_base, imgp->argc);
22726f9a767SRodney W. Grimes 
228a78e8d2aSDavid Greenman 	/*
229a78e8d2aSDavid Greenman 	 * For security and other reasons, the file descriptor table cannot
230a78e8d2aSDavid Greenman 	 * be shared after an exec.
231a78e8d2aSDavid Greenman 	 */
232a78e8d2aSDavid Greenman 	if (p->p_fd->fd_refcnt > 1) {
233a78e8d2aSDavid Greenman 		struct filedesc *tmp;
234a78e8d2aSDavid Greenman 
235a78e8d2aSDavid Greenman 		tmp = fdcopy(p);
236a78e8d2aSDavid Greenman 		fdfree(p);
237a78e8d2aSDavid Greenman 		p->p_fd = tmp;
238a78e8d2aSDavid Greenman 	}
239a78e8d2aSDavid Greenman 
240fdf4e8b3SWarner Losh 	/* Stop profiling */
241fdf4e8b3SWarner Losh 	stopprofclock(p);
242fdf4e8b3SWarner Losh 
24326f9a767SRodney W. Grimes 	/* close files on exec */
24426f9a767SRodney W. Grimes 	fdcloseexec(p);
24526f9a767SRodney W. Grimes 
24626f9a767SRodney W. Grimes 	/* reset caught signals */
24726f9a767SRodney W. Grimes 	execsigs(p);
24826f9a767SRodney W. Grimes 
24926f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
25026f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
25126f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
25226f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
25326f9a767SRodney W. Grimes 
25426f9a767SRodney W. Grimes 	/*
25524b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
256dc733423SDag-Erling Smørgrav 	 * it that it now has its own resources back
25726f9a767SRodney W. Grimes 	 */
25826f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
25926f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
26026f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
26126f9a767SRodney W. Grimes 		wakeup((caddr_t)p->p_pptr);
26226f9a767SRodney W. Grimes 	}
26326f9a767SRodney W. Grimes 
26426f9a767SRodney W. Grimes 	/*
265a78e8d2aSDavid Greenman 	 * Implement image setuid/setgid.
266a78e8d2aSDavid Greenman 	 *
267a78e8d2aSDavid Greenman 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
268a78e8d2aSDavid Greenman 	 * the process is being traced.
269c52007c2SDavid Greenman 	 */
2708aef1712SMatthew Dillon 	if ((((attr.va_mode & VSUID) && p->p_ucred->cr_uid != attr.va_uid) ||
2718aef1712SMatthew Dillon 	     ((attr.va_mode & VSGID) && p->p_ucred->cr_gid != attr.va_gid)) &&
272a78e8d2aSDavid Greenman 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
273c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
274c52007c2SDavid Greenman 		/*
275c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
27626f9a767SRodney W. Grimes 		 * root.
27726f9a767SRodney W. Grimes 		 */
278f711d546SPoul-Henning Kamp 		if (p->p_tracep && suser(p)) {
27926f9a767SRodney W. Grimes 			p->p_traceflag = 0;
28026f9a767SRodney W. Grimes 			vrele(p->p_tracep);
281c52007c2SDavid Greenman 			p->p_tracep = NULL;
28226f9a767SRodney W. Grimes 		}
283c52007c2SDavid Greenman 		/*
284c52007c2SDavid Greenman 		 * Set the new credentials.
285c52007c2SDavid Greenman 		 */
28626f9a767SRodney W. Grimes 		p->p_ucred = crcopy(p->p_ucred);
287c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
28826f9a767SRodney W. Grimes 			p->p_ucred->cr_uid = attr.va_uid;
289c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
290d021ae3dSGuido van Rooij 			p->p_ucred->cr_gid = attr.va_gid;
291d5f81602SSean Eric Fagan 		setsugid(p);
292c52007c2SDavid Greenman 	} else {
293e47bda07SDavid Greenman 		if (p->p_ucred->cr_uid == p->p_cred->p_ruid &&
294e47bda07SDavid Greenman 		    p->p_ucred->cr_gid == p->p_cred->p_rgid)
295c52007c2SDavid Greenman 			p->p_flag &= ~P_SUGID;
29626f9a767SRodney W. Grimes 	}
29726f9a767SRodney W. Grimes 
29826f9a767SRodney W. Grimes 	/*
299c52007c2SDavid Greenman 	 * Implement correct POSIX saved-id behavior.
30026f9a767SRodney W. Grimes 	 */
30126f9a767SRodney W. Grimes 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
30226f9a767SRodney W. Grimes 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
30326f9a767SRodney W. Grimes 
30426f9a767SRodney W. Grimes 	/*
3052a531c80SDavid Greenman 	 * Store the vp for use in procfs
3062a531c80SDavid Greenman 	 */
3072a531c80SDavid Greenman 	if (p->p_textvp)		/* release old reference */
3082a531c80SDavid Greenman 		vrele(p->p_textvp);
3092a531c80SDavid Greenman 	VREF(ndp->ni_vp);
3102a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
3112a531c80SDavid Greenman 
3122a531c80SDavid Greenman 	/*
31326f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
31426f9a767SRodney W. Grimes 	 * 	can be set before the program executes.
31526f9a767SRodney W. Grimes 	 */
3162a024a2bSSean Eric Fagan 	STOPEVENT(p, S_EXEC, 0);
3172a024a2bSSean Eric Fagan 
31826f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
31926f9a767SRodney W. Grimes 		psignal(p, SIGTRAP);
32026f9a767SRodney W. Grimes 
32126f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
32226f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
32326f9a767SRodney W. Grimes 
3244fe88fe6SJohn Polstra 	/* Set values passed into the program in registers. */
3254fe88fe6SJohn Polstra 	setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base,
3264fe88fe6SJohn Polstra 	    imgp->ps_strings);
32726f9a767SRodney W. Grimes 
328b9df5231SPoul-Henning Kamp 	/* Free any previous argument cache */
329b9df5231SPoul-Henning Kamp 	if (p->p_args && --p->p_args->ar_ref == 0)
330b9df5231SPoul-Henning Kamp 		FREE(p->p_args, M_PARGS);
331b9df5231SPoul-Henning Kamp 	p->p_args = NULL;
332b9df5231SPoul-Henning Kamp 
333b9df5231SPoul-Henning Kamp 	/* Cache arguments if they fit inside our allowance */
334b9df5231SPoul-Henning Kamp 	i = imgp->endargs - imgp->stringbase;
335b9df5231SPoul-Henning Kamp 	if (ps_arg_cache_limit >= i + sizeof(struct pargs)) {
336b9df5231SPoul-Henning Kamp 		MALLOC(p->p_args, struct pargs *, sizeof(struct pargs) + i,
337b9df5231SPoul-Henning Kamp 		    M_PARGS, M_WAITOK);
338b9df5231SPoul-Henning Kamp 		p->p_args->ar_ref = 1;
339b9df5231SPoul-Henning Kamp 		p->p_args->ar_length = i;
340b9df5231SPoul-Henning Kamp 		bcopy(imgp->stringbase, p->p_args->ar_args, i);
341b9df5231SPoul-Henning Kamp 	}
342b9df5231SPoul-Henning Kamp 
3431616db3cSJohn Dyson exec_fail_dealloc:
3441616db3cSJohn Dyson 
34526f9a767SRodney W. Grimes 	/*
34626f9a767SRodney W. Grimes 	 * free various allocated resources
34726f9a767SRodney W. Grimes 	 */
3481616db3cSJohn Dyson 	if (imgp->firstpage)
3491616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
35026f9a767SRodney W. Grimes 
351c2f9f36bSDavid Greenman 	if (imgp->stringbase != NULL)
3521616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
3531616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
3541616db3cSJohn Dyson 
3559c0fed3dSDoug Rabson 	if (imgp->vp) {
356762e6b85SEivind Eklund 		NDFREE(ndp, NDF_ONLY_PNBUF);
3579c0fed3dSDoug Rabson 		vrele(imgp->vp);
358a3cf6ebaSDavid Greenman 	}
35926f9a767SRodney W. Grimes 
3601616db3cSJohn Dyson 	if (error == 0)
3611616db3cSJohn Dyson 		return (0);
3621616db3cSJohn Dyson 
36326f9a767SRodney W. Grimes exec_fail:
364c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
36526f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
36626f9a767SRodney W. Grimes 		exit1(p, W_EXITCODE(0, SIGABRT));
36726f9a767SRodney W. Grimes 		/* NOT REACHED */
36826f9a767SRodney W. Grimes 		return(0);
36926f9a767SRodney W. Grimes 	} else {
37026f9a767SRodney W. Grimes 		return(error);
37126f9a767SRodney W. Grimes 	}
37226f9a767SRodney W. Grimes }
37326f9a767SRodney W. Grimes 
3741616db3cSJohn Dyson int
3751616db3cSJohn Dyson exec_map_first_page(imgp)
3761616db3cSJohn Dyson 	struct image_params *imgp;
3771616db3cSJohn Dyson {
37895461b45SJohn Dyson 	int s, rv, i;
37995461b45SJohn Dyson 	int initial_pagein;
38095461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
3811616db3cSJohn Dyson 	vm_object_t object;
3821616db3cSJohn Dyson 
3831616db3cSJohn Dyson 
3841616db3cSJohn Dyson 	if (imgp->firstpage) {
3851616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
3861616db3cSJohn Dyson 	}
3871616db3cSJohn Dyson 
3881616db3cSJohn Dyson 	object = imgp->vp->v_object;
3891616db3cSJohn Dyson 	s = splvm();
3901616db3cSJohn Dyson 
39195461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3921616db3cSJohn Dyson 
39395461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
39495461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
39595461b45SJohn Dyson 		if (initial_pagein > object->size)
39695461b45SJohn Dyson 			initial_pagein = object->size;
39795461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
398d254af07SMatthew Dillon 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
39995461b45SJohn Dyson 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
40095461b45SJohn Dyson 					break;
40195461b45SJohn Dyson 				if (ma[i]->valid)
40295461b45SJohn Dyson 					break;
403e69763a3SDoug Rabson 				vm_page_busy(ma[i]);
40495461b45SJohn Dyson 			} else {
40595461b45SJohn Dyson 				ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
40695461b45SJohn Dyson 				if (ma[i] == NULL)
40795461b45SJohn Dyson 					break;
40895461b45SJohn Dyson 			}
40995461b45SJohn Dyson 		}
41095461b45SJohn Dyson 		initial_pagein = i;
4111616db3cSJohn Dyson 
41295461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
41395461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
41495461b45SJohn Dyson 
415eed2412eSJohn Dyson 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) {
416ecbb00a2SDoug Rabson 			if (ma[0]) {
41795461b45SJohn Dyson 				vm_page_protect(ma[0], VM_PROT_NONE);
4188f9110f6SJohn Dyson 				vm_page_free(ma[0]);
419ecbb00a2SDoug Rabson 			}
4201616db3cSJohn Dyson 			splx(s);
4211616db3cSJohn Dyson 			return EIO;
4221616db3cSJohn Dyson 		}
4231616db3cSJohn Dyson 	}
4241616db3cSJohn Dyson 
42595461b45SJohn Dyson 	vm_page_wire(ma[0]);
426e69763a3SDoug Rabson 	vm_page_wakeup(ma[0]);
4271616db3cSJohn Dyson 	splx(s);
4281616db3cSJohn Dyson 
42995461b45SJohn Dyson 	pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0]));
43095461b45SJohn Dyson 	imgp->firstpage = ma[0];
4311616db3cSJohn Dyson 
4321616db3cSJohn Dyson 	return 0;
4331616db3cSJohn Dyson }
4341616db3cSJohn Dyson 
4351616db3cSJohn Dyson void
4361616db3cSJohn Dyson exec_unmap_first_page(imgp)
4371616db3cSJohn Dyson 	struct image_params *imgp;
4381616db3cSJohn Dyson {
4391616db3cSJohn Dyson 	if (imgp->firstpage) {
4401616db3cSJohn Dyson 		pmap_kremove((vm_offset_t) imgp->image_header);
44173007561SDavid Greenman 		vm_page_unwire(imgp->firstpage, 1);
4421616db3cSJohn Dyson 		imgp->firstpage = NULL;
4431616db3cSJohn Dyson 	}
4441616db3cSJohn Dyson }
4451616db3cSJohn Dyson 
44626f9a767SRodney W. Grimes /*
44726f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
44826f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
44926f9a767SRodney W. Grimes  *	automatically in trap.c.
45026f9a767SRodney W. Grimes  */
45126f9a767SRodney W. Grimes int
452c52007c2SDavid Greenman exec_new_vmspace(imgp)
453c52007c2SDavid Greenman 	struct image_params *imgp;
45426f9a767SRodney W. Grimes {
45526f9a767SRodney W. Grimes 	int error;
456c52007c2SDavid Greenman 	struct vmspace *vmspace = imgp->proc->p_vmspace;
4572267af78SJulian Elischer 	caddr_t	stack_addr = (caddr_t) (USRSTACK - MAXSSIZ);
458af9ec885SJohn Dyson 	vm_map_t map = &vmspace->vm_map;
45926f9a767SRodney W. Grimes 
460c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
46126f9a767SRodney W. Grimes 
462af9ec885SJohn Dyson 	/*
463af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
464af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
465af9ec885SJohn Dyson 	 * not disrupted
466af9ec885SJohn Dyson 	 */
467af9ec885SJohn Dyson 	if (vmspace->vm_refcnt == 1) {
4683d903220SDoug Rabson 		if (vmspace->vm_shm)
469c52007c2SDavid Greenman 			shmexit(imgp->proc);
470b1028ad1SLuoqi Chen 		pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS);
4719c0fed3dSDoug Rabson 		vm_map_remove(map, 0, VM_MAXUSER_ADDRESS);
472af9ec885SJohn Dyson 	} else {
473492da96cSJohn Dyson 		vmspace_exec(imgp->proc);
474492da96cSJohn Dyson 		vmspace = imgp->proc->p_vmspace;
475af9ec885SJohn Dyson 		map = &vmspace->vm_map;
476af9ec885SJohn Dyson 	}
47726f9a767SRodney W. Grimes 
47826f9a767SRodney W. Grimes 	/* Allocate a new stack */
4792267af78SJulian Elischer 	error = vm_map_stack (&vmspace->vm_map, (vm_offset_t)stack_addr,
4802267af78SJulian Elischer 			      (vm_size_t)MAXSSIZ, VM_PROT_ALL, VM_PROT_ALL, 0);
4812267af78SJulian Elischer 	if (error)
4822267af78SJulian Elischer 		return (error);
4832267af78SJulian Elischer 
4842267af78SJulian Elischer 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
4852267af78SJulian Elischer 	 * VM_STACK case, but they are still used to monitor the size of the
4862267af78SJulian Elischer 	 * process stack so we can check the stack rlimit.
4872267af78SJulian Elischer 	 */
4882267af78SJulian Elischer 	vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT;
4892267af78SJulian Elischer 	vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ;
49026f9a767SRodney W. Grimes 
49126f9a767SRodney W. Grimes 	return(0);
49226f9a767SRodney W. Grimes }
49326f9a767SRodney W. Grimes 
49426f9a767SRodney W. Grimes /*
49526f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
49626f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
49726f9a767SRodney W. Grimes  */
49826f9a767SRodney W. Grimes int
499c52007c2SDavid Greenman exec_extract_strings(imgp)
500c52007c2SDavid Greenman 	struct image_params *imgp;
50126f9a767SRodney W. Grimes {
50226f9a767SRodney W. Grimes 	char	**argv, **envv;
50326f9a767SRodney W. Grimes 	char	*argp, *envp;
504ecbb00a2SDoug Rabson 	int	error;
505ecbb00a2SDoug Rabson 	size_t	length;
50626f9a767SRodney W. Grimes 
50726f9a767SRodney W. Grimes 	/*
50826f9a767SRodney W. Grimes 	 * extract arguments first
50926f9a767SRodney W. Grimes 	 */
51026f9a767SRodney W. Grimes 
511c52007c2SDavid Greenman 	argv = imgp->uap->argv;
51226f9a767SRodney W. Grimes 
5130fd1a014SDavid Greenman 	if (argv) {
514aae0aa45SBruce Evans 		argp = (caddr_t) (intptr_t) fuword(argv);
5155cf3d12cSAndrey A. Chernov 		if (argp == (caddr_t) -1)
5165cf3d12cSAndrey A. Chernov 			return (EFAULT);
5175cf3d12cSAndrey A. Chernov 		if (argp)
5185cf3d12cSAndrey A. Chernov 			argv++;
5195cf3d12cSAndrey A. Chernov 		if (imgp->argv0)
5205cf3d12cSAndrey A. Chernov 			argp = imgp->argv0;
5215cf3d12cSAndrey A. Chernov 		if (argp) {
5225cf3d12cSAndrey A. Chernov 			do {
52326f9a767SRodney W. Grimes 				if (argp == (caddr_t) -1)
52426f9a767SRodney W. Grimes 					return (EFAULT);
525c52007c2SDavid Greenman 				if ((error = copyinstr(argp, 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->argc++;
534aae0aa45SBruce Evans 			} while ((argp = (caddr_t) (intptr_t) fuword(argv++)));
53526f9a767SRodney W. Grimes 		}
5360fd1a014SDavid Greenman 	}
53726f9a767SRodney W. Grimes 
538b9df5231SPoul-Henning Kamp 	imgp->endargs = imgp->stringp;
539b9df5231SPoul-Henning Kamp 
54026f9a767SRodney W. Grimes 	/*
54126f9a767SRodney W. Grimes 	 * extract environment strings
54226f9a767SRodney W. Grimes 	 */
54326f9a767SRodney W. Grimes 
544c52007c2SDavid Greenman 	envv = imgp->uap->envv;
54526f9a767SRodney W. Grimes 
5460fd1a014SDavid Greenman 	if (envv) {
547aae0aa45SBruce Evans 		while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
54826f9a767SRodney W. Grimes 			if (envp == (caddr_t) -1)
54926f9a767SRodney W. Grimes 				return (EFAULT);
550c52007c2SDavid Greenman 			if ((error = copyinstr(envp, imgp->stringp,
551c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
5520fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
55326f9a767SRodney W. Grimes 					return(E2BIG);
5540fd1a014SDavid Greenman 				return (error);
5550fd1a014SDavid Greenman 			}
556c52007c2SDavid Greenman 			imgp->stringspace -= length;
557c52007c2SDavid Greenman 			imgp->stringp += length;
558c52007c2SDavid Greenman 			imgp->envc++;
55926f9a767SRodney W. Grimes 		}
5600fd1a014SDavid Greenman 	}
56126f9a767SRodney W. Grimes 
56226f9a767SRodney W. Grimes 	return (0);
56326f9a767SRodney W. Grimes }
56426f9a767SRodney W. Grimes 
56526f9a767SRodney W. Grimes /*
56626f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
56726f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
56826f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
56926f9a767SRodney W. Grimes  */
570ecbb00a2SDoug Rabson long *
571c52007c2SDavid Greenman exec_copyout_strings(imgp)
572c52007c2SDavid Greenman 	struct image_params *imgp;
57326f9a767SRodney W. Grimes {
57426f9a767SRodney W. Grimes 	int argc, envc;
57526f9a767SRodney W. Grimes 	char **vectp;
57626f9a767SRodney W. Grimes 	char *stringp, *destp;
577ecbb00a2SDoug Rabson 	long *stack_base;
57893f6448cSDavid Greenman 	struct ps_strings *arginfo;
579d66a5066SPeter Wemm 	int szsigcode;
58026f9a767SRodney W. Grimes 
58126f9a767SRodney W. Grimes 	/*
58226f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
583d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
58426f9a767SRodney W. Grimes 	 */
5854c56fcdeSBruce Evans 	arginfo = (struct ps_strings *)PS_STRINGS;
586d66a5066SPeter Wemm 	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
587d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
5881ed012f9SPeter Wemm 		roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
5891ed012f9SPeter Wemm 
59026f9a767SRodney W. Grimes 	/*
591d66a5066SPeter Wemm 	 * install sigcode
592d66a5066SPeter Wemm 	 */
593d66a5066SPeter Wemm 	if (szsigcode)
594d66a5066SPeter Wemm 		copyout(imgp->proc->p_sysent->sv_sigcode,
595d66a5066SPeter Wemm 			((caddr_t)arginfo - szsigcode), szsigcode);
596d66a5066SPeter Wemm 
597d66a5066SPeter Wemm 	/*
598e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
599e1743d02SSøren Schmidt 	 * on the stack.
600e1743d02SSøren Schmidt 	 */
601e1743d02SSøren Schmidt 	if (imgp->auxargs)
602e1743d02SSøren Schmidt 	/*
603e1743d02SSøren Schmidt 	 * The '+ 2' is for the null pointers at the end of each of the
604e1743d02SSøren Schmidt 	 * arg and env vector sets, and 'AT_COUNT*2' is room for the
605e1743d02SSøren Schmidt 	 * ELF Auxargs data.
606e1743d02SSøren Schmidt 	 */
607e1743d02SSøren Schmidt 		vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 +
608e1743d02SSøren Schmidt 				  AT_COUNT*2) * sizeof(char*));
609e1743d02SSøren Schmidt 	else
610e1743d02SSøren Schmidt 	/*
61126f9a767SRodney W. Grimes 	 * The '+ 2' is for the null pointers at the end of each of the
61226f9a767SRodney W. Grimes 	 * arg and env vector sets
61326f9a767SRodney W. Grimes 	 */
614e1743d02SSøren Schmidt 		vectp = (char **)
615e1743d02SSøren Schmidt 			(destp - (imgp->argc + imgp->envc + 2) * sizeof(char*));
61626f9a767SRodney W. Grimes 
61726f9a767SRodney W. Grimes 	/*
61826f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
61926f9a767SRodney W. Grimes 	 */
620ecbb00a2SDoug Rabson 	stack_base = (long *)vectp;
62126f9a767SRodney W. Grimes 
622c52007c2SDavid Greenman 	stringp = imgp->stringbase;
623c52007c2SDavid Greenman 	argc = imgp->argc;
624c52007c2SDavid Greenman 	envc = imgp->envc;
62526f9a767SRodney W. Grimes 
62693f6448cSDavid Greenman 	/*
6273aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
62893f6448cSDavid Greenman 	 */
629c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
63093f6448cSDavid Greenman 
63193f6448cSDavid Greenman 	/*
6323aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
6333aed948bSDavid Greenman 	 */
634aae0aa45SBruce Evans 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
6353aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
6363aed948bSDavid Greenman 
6373aed948bSDavid Greenman 	/*
6383aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
63993f6448cSDavid Greenman 	 */
64026f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
641aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
6423aed948bSDavid Greenman 		while (*stringp++ != 0)
6433aed948bSDavid Greenman 			destp++;
6443aed948bSDavid Greenman 		destp++;
64526f9a767SRodney W. Grimes 	}
64626f9a767SRodney W. Grimes 
64726f9a767SRodney W. Grimes 	/* a null vector table pointer seperates the argp's from the envp's */
6486ab46d52SBruce Evans 	suword(vectp++, 0);
64926f9a767SRodney W. Grimes 
650aae0aa45SBruce Evans 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
6513aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
65293f6448cSDavid Greenman 
65393f6448cSDavid Greenman 	/*
6543aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
65593f6448cSDavid Greenman 	 */
65626f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
657aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
6583aed948bSDavid Greenman 		while (*stringp++ != 0)
6593aed948bSDavid Greenman 			destp++;
6603aed948bSDavid Greenman 		destp++;
66126f9a767SRodney W. Grimes 	}
66226f9a767SRodney W. Grimes 
66326f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
6646ab46d52SBruce Evans 	suword(vectp, 0);
66526f9a767SRodney W. Grimes 
66626f9a767SRodney W. Grimes 	return (stack_base);
66726f9a767SRodney W. Grimes }
66826f9a767SRodney W. Grimes 
66926f9a767SRodney W. Grimes /*
67026f9a767SRodney W. Grimes  * Check permissions of file to execute.
67126f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
67226f9a767SRodney W. Grimes  */
673c8a79999SPeter Wemm int
674c52007c2SDavid Greenman exec_check_permissions(imgp)
675c52007c2SDavid Greenman 	struct image_params *imgp;
67626f9a767SRodney W. Grimes {
677c52007c2SDavid Greenman 	struct proc *p = imgp->proc;
678c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
679c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
68026f9a767SRodney W. Grimes 	int error;
68126f9a767SRodney W. Grimes 
68226f9a767SRodney W. Grimes 	/* Get file attributes */
683c52007c2SDavid Greenman 	error = VOP_GETATTR(vp, attr, p->p_ucred, p);
68426f9a767SRodney W. Grimes 	if (error)
68526f9a767SRodney W. Grimes 		return (error);
68626f9a767SRodney W. Grimes 
68726f9a767SRodney W. Grimes 	/*
68826f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
68926f9a767SRodney W. Grimes 	 *	file resides on.
69026f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
69126f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
69226f9a767SRodney W. Grimes 	 *	file really is executable.
69326f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
69426f9a767SRodney W. Grimes 	 */
695c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
69626f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
69726f9a767SRodney W. Grimes 	    (attr->va_type != VREG)) {
69826f9a767SRodney W. Grimes 		return (EACCES);
69926f9a767SRodney W. Grimes 	}
70026f9a767SRodney W. Grimes 
70126f9a767SRodney W. Grimes 	/*
70226f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
70326f9a767SRodney W. Grimes 	 */
70426f9a767SRodney W. Grimes 	if (attr->va_size == 0)
70526f9a767SRodney W. Grimes 		return (ENOEXEC);
70626f9a767SRodney W. Grimes 
70726f9a767SRodney W. Grimes 	/*
70826f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
70926f9a767SRodney W. Grimes 	 */
710c52007c2SDavid Greenman 	error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
71126f9a767SRodney W. Grimes 	if (error)
71226f9a767SRodney W. Grimes 		return (error);
71326f9a767SRodney W. Grimes 
7146d5a0a8cSDavid Greenman 	/*
7156d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
7166d5a0a8cSDavid Greenman 	 * if there are any.
7176d5a0a8cSDavid Greenman 	 */
7186d5a0a8cSDavid Greenman 	if (vp->v_writecount)
7196d5a0a8cSDavid Greenman 		return (ETXTBSY);
7206d5a0a8cSDavid Greenman 
7216d5a0a8cSDavid Greenman 	/*
7226d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
7236d5a0a8cSDavid Greenman 	 * general case).
7246d5a0a8cSDavid Greenman 	 */
725c52007c2SDavid Greenman 	error = VOP_OPEN(vp, FREAD, p->p_ucred, p);
72626f9a767SRodney W. Grimes 	if (error)
72726f9a767SRodney W. Grimes 		return (error);
72826f9a767SRodney W. Grimes 
72926f9a767SRodney W. Grimes 	return (0);
730df8bae1dSRodney W. Grimes }
731aa855a59SPeter Wemm 
732aa855a59SPeter Wemm /*
733aa855a59SPeter Wemm  * Exec handler registration
734aa855a59SPeter Wemm  */
735aa855a59SPeter Wemm int
736aa855a59SPeter Wemm exec_register(execsw_arg)
737aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
738aa855a59SPeter Wemm {
739aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
740aa855a59SPeter Wemm 	int count = 2;	/* New slot and trailing NULL */
741aa855a59SPeter Wemm 
742aa855a59SPeter Wemm 	if (execsw)
743aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
744aa855a59SPeter Wemm 			count++;
745aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
746aa855a59SPeter Wemm 	if (newexecsw == NULL)
747aa855a59SPeter Wemm 		return ENOMEM;
748aa855a59SPeter Wemm 	xs = newexecsw;
749aa855a59SPeter Wemm 	if (execsw)
750aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
751aa855a59SPeter Wemm 			*xs++ = *es;
752aa855a59SPeter Wemm 	*xs++ = execsw_arg;
753aa855a59SPeter Wemm 	*xs = NULL;
754aa855a59SPeter Wemm 	if (execsw)
755aa855a59SPeter Wemm 		free(execsw, M_TEMP);
756aa855a59SPeter Wemm 	execsw = newexecsw;
757aa855a59SPeter Wemm 	return 0;
758aa855a59SPeter Wemm }
759aa855a59SPeter Wemm 
760aa855a59SPeter Wemm int
761aa855a59SPeter Wemm exec_unregister(execsw_arg)
762aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
763aa855a59SPeter Wemm {
764aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
765aa855a59SPeter Wemm 	int count = 1;
766aa855a59SPeter Wemm 
767aa855a59SPeter Wemm 	if (execsw == NULL)
768aa855a59SPeter Wemm 		panic("unregister with no handlers left?\n");
769aa855a59SPeter Wemm 
770aa855a59SPeter Wemm 	for (es = execsw; *es; es++) {
771aa855a59SPeter Wemm 		if (*es == execsw_arg)
772aa855a59SPeter Wemm 			break;
773aa855a59SPeter Wemm 	}
774aa855a59SPeter Wemm 	if (*es == NULL)
775aa855a59SPeter Wemm 		return ENOENT;
776aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
777aa855a59SPeter Wemm 		if (*es != execsw_arg)
778aa855a59SPeter Wemm 			count++;
779aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
780aa855a59SPeter Wemm 	if (newexecsw == NULL)
781aa855a59SPeter Wemm 		return ENOMEM;
782aa855a59SPeter Wemm 	xs = newexecsw;
783aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
784aa855a59SPeter Wemm 		if (*es != execsw_arg)
785aa855a59SPeter Wemm 			*xs++ = *es;
786aa855a59SPeter Wemm 	*xs = NULL;
787aa855a59SPeter Wemm 	if (execsw)
788aa855a59SPeter Wemm 		free(execsw, M_TEMP);
789aa855a59SPeter Wemm 	execsw = newexecsw;
790aa855a59SPeter Wemm 	return 0;
791aa855a59SPeter Wemm }
792