xref: /freebsd/sys/kern/kern_exec.c (revision 2267af789e8723fa3e608b6bd0ca8e9156b958c4)
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  *
262267af78SJulian Elischer  *	$Id: kern_exec.c,v 1.92 1998/12/30 10:38:59 dfr Exp $
27df8bae1dSRodney W. Grimes  */
28df8bae1dSRodney W. Grimes 
29df8bae1dSRodney W. Grimes #include <sys/param.h>
3026f9a767SRodney W. Grimes #include <sys/systm.h>
31d2d3e875SBruce Evans #include <sys/sysproto.h>
3226f9a767SRodney W. Grimes #include <sys/signalvar.h>
3326f9a767SRodney W. Grimes #include <sys/kernel.h>
3426f9a767SRodney W. Grimes #include <sys/mount.h>
35797f2d22SPoul-Henning Kamp #include <sys/filedesc.h>
36079cc25bSDavid Greenman #include <sys/fcntl.h>
3726f9a767SRodney W. Grimes #include <sys/acct.h>
3826f9a767SRodney W. Grimes #include <sys/exec.h>
3926f9a767SRodney W. Grimes #include <sys/imgact.h>
40e1743d02SSøren Schmidt #include <sys/imgact_elf.h>
4126f9a767SRodney W. Grimes #include <sys/wait.h>
42a794e791SBruce Evans #include <sys/proc.h>
432a024a2bSSean Eric Fagan #include <sys/pioctl.h>
44aa855a59SPeter Wemm #include <sys/malloc.h>
45a794e791SBruce Evans #include <sys/namei.h>
461e1e0b44SSøren Schmidt #include <sys/sysent.h>
47797f2d22SPoul-Henning Kamp #include <sys/shm.h>
4899ac3bc8SPeter Wemm #include <sys/sysctl.h>
49a794e791SBruce Evans #include <sys/vnode.h>
509caaadb6SDavid Greenman #include <sys/buf.h>
5126f9a767SRodney W. Grimes 
5226f9a767SRodney W. Grimes #include <vm/vm.h>
53efeaf95aSDavid Greenman #include <vm/vm_param.h>
54efeaf95aSDavid Greenman #include <vm/vm_prot.h>
55996c772fSJohn Dyson #include <sys/lock.h>
56efeaf95aSDavid Greenman #include <vm/pmap.h>
571616db3cSJohn Dyson #include <vm/vm_page.h>
58efeaf95aSDavid Greenman #include <vm/vm_map.h>
5926f9a767SRodney W. Grimes #include <vm/vm_kern.h>
60efeaf95aSDavid Greenman #include <vm/vm_extern.h>
611ebd0c59SDavid Greenman #include <vm/vm_object.h>
62675ea6f0SBruce Evans #include <vm/vm_zone.h>
631616db3cSJohn Dyson #include <vm/vm_pager.h>
6426f9a767SRodney W. Grimes 
6526f9a767SRodney W. Grimes #include <machine/reg.h>
6626f9a767SRodney W. Grimes 
67ecbb00a2SDoug Rabson static long *exec_copyout_strings __P((struct image_params *));
68df8bae1dSRodney W. Grimes 
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;
12026f9a767SRodney W. Grimes 
12126f9a767SRodney W. Grimes 	/*
12226f9a767SRodney W. Grimes 	 * Allocate temporary demand zeroed space for argument and
12326f9a767SRodney W. Grimes 	 *	environment strings
12426f9a767SRodney W. Grimes 	 */
1251616db3cSJohn Dyson 	imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE);
126c2f9f36bSDavid Greenman 	if (imgp->stringbase == NULL) {
12726f9a767SRodney W. Grimes 		error = ENOMEM;
12826f9a767SRodney W. Grimes 		goto exec_fail;
12926f9a767SRodney W. Grimes 	}
130c52007c2SDavid Greenman 	imgp->stringp = imgp->stringbase;
131c52007c2SDavid Greenman 	imgp->stringspace = ARG_MAX;
1321616db3cSJohn Dyson 	imgp->image_header = imgp->stringbase + ARG_MAX;
13326f9a767SRodney W. Grimes 
13426f9a767SRodney W. Grimes 	/*
13526f9a767SRodney W. Grimes 	 * Translate the file name. namei() returns a vnode pointer
13626f9a767SRodney W. Grimes 	 *	in ni_vp amoung other things.
13726f9a767SRodney W. Grimes 	 */
13826f9a767SRodney W. Grimes 	ndp = &nd;
139b35ba931SDavid Greenman 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
1403ed8a403SDavid Greenman 	    UIO_USERSPACE, uap->fname, p);
14126f9a767SRodney W. Grimes 
14226f9a767SRodney W. Grimes interpret:
14326f9a767SRodney W. Grimes 
14426f9a767SRodney W. Grimes 	error = namei(ndp);
14526f9a767SRodney W. Grimes 	if (error) {
1461616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
1471616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
14826f9a767SRodney W. Grimes 		goto exec_fail;
14926f9a767SRodney W. Grimes 	}
15026f9a767SRodney W. Grimes 
151c52007c2SDavid Greenman 	imgp->vp = ndp->ni_vp;
1529c0fed3dSDoug Rabson 	imgp->fname = uap->fname;
15326f9a767SRodney W. Grimes 
15426f9a767SRodney W. Grimes 	/*
1559022e4adSDavid Greenman 	 * Check file permissions (also 'opens' file)
1569022e4adSDavid Greenman 	 */
157c52007c2SDavid Greenman 	error = exec_check_permissions(imgp);
1588677f509SDavid Greenman 	if (error) {
1598677f509SDavid Greenman 		VOP_UNLOCK(imgp->vp, 0, p);
16026f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
1618677f509SDavid Greenman 	}
16226f9a767SRodney W. Grimes 
1631616db3cSJohn Dyson 	error = exec_map_first_page(imgp);
1649caaadb6SDavid Greenman 	VOP_UNLOCK(imgp->vp, 0, p);
1656d5a0a8cSDavid Greenman 	if (error)
16626f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
16726f9a767SRodney W. Grimes 
16826f9a767SRodney W. Grimes 	/*
16926f9a767SRodney W. Grimes 	 * Loop through list of image activators, calling each one.
17026f9a767SRodney W. Grimes 	 *	If there is no match, the activator returns -1. If there
17126f9a767SRodney W. Grimes 	 *	is a match, but there was an error during the activation,
17226f9a767SRodney W. Grimes 	 *	the error is returned. Otherwise 0 means success. If the
17326f9a767SRodney W. Grimes 	 *	image is interpreted, loop back up and try activating
17426f9a767SRodney W. Grimes 	 *	the interpreter.
17526f9a767SRodney W. Grimes 	 */
17626f9a767SRodney W. Grimes 	for (i = 0; execsw[i]; ++i) {
17726f9a767SRodney W. Grimes 		if (execsw[i]->ex_imgact)
178c52007c2SDavid Greenman 			error = (*execsw[i]->ex_imgact)(imgp);
17926f9a767SRodney W. Grimes 		else
18026f9a767SRodney W. Grimes 			continue;
18126f9a767SRodney W. Grimes 		if (error == -1)
18226f9a767SRodney W. Grimes 			continue;
18326f9a767SRodney W. Grimes 		if (error)
18426f9a767SRodney W. Grimes 			goto exec_fail_dealloc;
185c52007c2SDavid Greenman 		if (imgp->interpreted) {
1861616db3cSJohn Dyson 			exec_unmap_first_page(imgp);
18726f9a767SRodney W. Grimes 			/* free old vnode and name buffer */
188f5277ae7SDavid Greenman 			vrele(ndp->ni_vp);
18999448ed1SJohn Dyson 			zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
19026f9a767SRodney W. Grimes 			/* set new name to that of the interpreter */
191b35ba931SDavid Greenman 			NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
192c52007c2SDavid Greenman 			    UIO_SYSSPACE, imgp->interpreter_name, p);
19326f9a767SRodney W. Grimes 			goto interpret;
19426f9a767SRodney W. Grimes 		}
19526f9a767SRodney W. Grimes 		break;
19626f9a767SRodney W. Grimes 	}
19726f9a767SRodney W. Grimes 	/* If we made it through all the activators and none matched, exit. */
19826f9a767SRodney W. Grimes 	if (error == -1) {
19926f9a767SRodney W. Grimes 		error = ENOEXEC;
20026f9a767SRodney W. Grimes 		goto exec_fail_dealloc;
20126f9a767SRodney W. Grimes 	}
20226f9a767SRodney W. Grimes 
20326f9a767SRodney W. Grimes 	/*
20426f9a767SRodney W. Grimes 	 * Copy out strings (args and env) and initialize stack base
20526f9a767SRodney W. Grimes 	 */
206c52007c2SDavid Greenman 	stack_base = exec_copyout_strings(imgp);
20726f9a767SRodney W. Grimes 	p->p_vmspace->vm_minsaddr = (char *)stack_base;
20826f9a767SRodney W. Grimes 
20926f9a767SRodney W. Grimes 	/*
2101e1e0b44SSøren Schmidt 	 * If custom stack fixup routine present for this process
2111e1e0b44SSøren Schmidt 	 * let it do the stack setup.
2121e1e0b44SSøren Schmidt 	 * Else stuff argument count as first item on stack
21326f9a767SRodney W. Grimes 	 */
2141e1e0b44SSøren Schmidt 	if (p->p_sysent->sv_fixup)
215c52007c2SDavid Greenman 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
2161e1e0b44SSøren Schmidt 	else
217c52007c2SDavid Greenman 		suword(--stack_base, imgp->argc);
21826f9a767SRodney W. Grimes 
219a78e8d2aSDavid Greenman 	/*
220a78e8d2aSDavid Greenman 	 * For security and other reasons, the file descriptor table cannot
221a78e8d2aSDavid Greenman 	 * be shared after an exec.
222a78e8d2aSDavid Greenman 	 */
223a78e8d2aSDavid Greenman 	if (p->p_fd->fd_refcnt > 1) {
224a78e8d2aSDavid Greenman 		struct filedesc *tmp;
225a78e8d2aSDavid Greenman 
226a78e8d2aSDavid Greenman 		tmp = fdcopy(p);
227a78e8d2aSDavid Greenman 		fdfree(p);
228a78e8d2aSDavid Greenman 		p->p_fd = tmp;
229a78e8d2aSDavid Greenman 	}
230a78e8d2aSDavid Greenman 
23126f9a767SRodney W. Grimes 	/* close files on exec */
23226f9a767SRodney W. Grimes 	fdcloseexec(p);
23326f9a767SRodney W. Grimes 
23426f9a767SRodney W. Grimes 	/* reset caught signals */
23526f9a767SRodney W. Grimes 	execsigs(p);
23626f9a767SRodney W. Grimes 
23726f9a767SRodney W. Grimes 	/* name this process - nameiexec(p, ndp) */
23826f9a767SRodney W. Grimes 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
23926f9a767SRodney W. Grimes 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
24026f9a767SRodney W. Grimes 	p->p_comm[len] = 0;
24126f9a767SRodney W. Grimes 
24226f9a767SRodney W. Grimes 	/*
24324b34f09SSujal Patel 	 * mark as execed, wakeup the process that vforked (if any) and tell
244dc733423SDag-Erling Smørgrav 	 * it that it now has its own resources back
24526f9a767SRodney W. Grimes 	 */
24626f9a767SRodney W. Grimes 	p->p_flag |= P_EXEC;
24726f9a767SRodney W. Grimes 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
24826f9a767SRodney W. Grimes 		p->p_flag &= ~P_PPWAIT;
24926f9a767SRodney W. Grimes 		wakeup((caddr_t)p->p_pptr);
25026f9a767SRodney W. Grimes 	}
25126f9a767SRodney W. Grimes 
25226f9a767SRodney W. Grimes 	/*
253a78e8d2aSDavid Greenman 	 * Implement image setuid/setgid.
254a78e8d2aSDavid Greenman 	 *
255a78e8d2aSDavid Greenman 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
256a78e8d2aSDavid Greenman 	 * the process is being traced.
257c52007c2SDavid Greenman 	 */
258d021ae3dSGuido van Rooij 	if ((attr.va_mode & VSUID && p->p_ucred->cr_uid != attr.va_uid ||
259d021ae3dSGuido van Rooij 	     attr.va_mode & VSGID && p->p_ucred->cr_gid != attr.va_gid) &&
260a78e8d2aSDavid Greenman 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
261c52007c2SDavid Greenman 	    (p->p_flag & P_TRACED) == 0) {
262c52007c2SDavid Greenman 		/*
263c52007c2SDavid Greenman 		 * Turn off syscall tracing for set-id programs, except for
26426f9a767SRodney W. Grimes 		 * root.
26526f9a767SRodney W. Grimes 		 */
266c52007c2SDavid Greenman 		if (p->p_tracep && suser(p->p_ucred, &p->p_acflag)) {
26726f9a767SRodney W. Grimes 			p->p_traceflag = 0;
26826f9a767SRodney W. Grimes 			vrele(p->p_tracep);
269c52007c2SDavid Greenman 			p->p_tracep = NULL;
27026f9a767SRodney W. Grimes 		}
271c52007c2SDavid Greenman 		/*
272c52007c2SDavid Greenman 		 * Set the new credentials.
273c52007c2SDavid Greenman 		 */
27426f9a767SRodney W. Grimes 		p->p_ucred = crcopy(p->p_ucred);
275c52007c2SDavid Greenman 		if (attr.va_mode & VSUID)
27626f9a767SRodney W. Grimes 			p->p_ucred->cr_uid = attr.va_uid;
277c52007c2SDavid Greenman 		if (attr.va_mode & VSGID)
278d021ae3dSGuido van Rooij 			p->p_ucred->cr_gid = attr.va_gid;
279d5f81602SSean Eric Fagan 		setsugid(p);
280c52007c2SDavid Greenman 	} else {
281e47bda07SDavid Greenman 		if (p->p_ucred->cr_uid == p->p_cred->p_ruid &&
282e47bda07SDavid Greenman 		    p->p_ucred->cr_gid == p->p_cred->p_rgid)
283c52007c2SDavid Greenman 			p->p_flag &= ~P_SUGID;
28426f9a767SRodney W. Grimes 	}
28526f9a767SRodney W. Grimes 
28626f9a767SRodney W. Grimes 	/*
287c52007c2SDavid Greenman 	 * Implement correct POSIX saved-id behavior.
28826f9a767SRodney W. Grimes 	 */
28926f9a767SRodney W. Grimes 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
29026f9a767SRodney W. Grimes 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
29126f9a767SRodney W. Grimes 
29226f9a767SRodney W. Grimes 	/*
2932a531c80SDavid Greenman 	 * Store the vp for use in procfs
2942a531c80SDavid Greenman 	 */
2952a531c80SDavid Greenman 	if (p->p_textvp)		/* release old reference */
2962a531c80SDavid Greenman 		vrele(p->p_textvp);
2972a531c80SDavid Greenman 	VREF(ndp->ni_vp);
2982a531c80SDavid Greenman 	p->p_textvp = ndp->ni_vp;
2992a531c80SDavid Greenman 
3002a531c80SDavid Greenman 	/*
30126f9a767SRodney W. Grimes 	 * If tracing the process, trap to debugger so breakpoints
30226f9a767SRodney W. Grimes 	 * 	can be set before the program executes.
30326f9a767SRodney W. Grimes 	 */
3042a024a2bSSean Eric Fagan 	STOPEVENT(p, S_EXEC, 0);
3052a024a2bSSean Eric Fagan 
30626f9a767SRodney W. Grimes 	if (p->p_flag & P_TRACED)
30726f9a767SRodney W. Grimes 		psignal(p, SIGTRAP);
30826f9a767SRodney W. Grimes 
30926f9a767SRodney W. Grimes 	/* clear "fork but no exec" flag, as we _are_ execing */
31026f9a767SRodney W. Grimes 	p->p_acflag &= ~AFORK;
31126f9a767SRodney W. Grimes 
31226f9a767SRodney W. Grimes 	/* Set entry address */
313aae0aa45SBruce Evans 	setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base);
31426f9a767SRodney W. Grimes 
3151616db3cSJohn Dyson exec_fail_dealloc:
3161616db3cSJohn Dyson 
31726f9a767SRodney W. Grimes 	/*
31826f9a767SRodney W. Grimes 	 * free various allocated resources
31926f9a767SRodney W. Grimes 	 */
3201616db3cSJohn Dyson 	if (imgp->firstpage)
3211616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
32226f9a767SRodney W. Grimes 
323c2f9f36bSDavid Greenman 	if (imgp->stringbase != NULL)
3241616db3cSJohn Dyson 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
3251616db3cSJohn Dyson 			ARG_MAX + PAGE_SIZE);
3261616db3cSJohn Dyson 
3279c0fed3dSDoug Rabson 	if (imgp->vp) {
3289c0fed3dSDoug Rabson 		vrele(imgp->vp);
32999448ed1SJohn Dyson 		zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
330a3cf6ebaSDavid Greenman 	}
33126f9a767SRodney W. Grimes 
3321616db3cSJohn Dyson 	if (error == 0)
3331616db3cSJohn Dyson 		return (0);
3341616db3cSJohn Dyson 
33526f9a767SRodney W. Grimes exec_fail:
336c52007c2SDavid Greenman 	if (imgp->vmspace_destroyed) {
33726f9a767SRodney W. Grimes 		/* sorry, no more process anymore. exit gracefully */
33826f9a767SRodney W. Grimes 		exit1(p, W_EXITCODE(0, SIGABRT));
33926f9a767SRodney W. Grimes 		/* NOT REACHED */
34026f9a767SRodney W. Grimes 		return(0);
34126f9a767SRodney W. Grimes 	} else {
34226f9a767SRodney W. Grimes 		return(error);
34326f9a767SRodney W. Grimes 	}
34426f9a767SRodney W. Grimes }
34526f9a767SRodney W. Grimes 
3461616db3cSJohn Dyson int
3471616db3cSJohn Dyson exec_map_first_page(imgp)
3481616db3cSJohn Dyson 	struct image_params *imgp;
3491616db3cSJohn Dyson {
35095461b45SJohn Dyson 	int s, rv, i;
35195461b45SJohn Dyson 	int initial_pagein;
35295461b45SJohn Dyson 	vm_page_t ma[VM_INITIAL_PAGEIN];
3531616db3cSJohn Dyson 	vm_object_t object;
3541616db3cSJohn Dyson 
3551616db3cSJohn Dyson 
3561616db3cSJohn Dyson 	if (imgp->firstpage) {
3571616db3cSJohn Dyson 		exec_unmap_first_page(imgp);
3581616db3cSJohn Dyson 	}
3591616db3cSJohn Dyson 
3601616db3cSJohn Dyson 	object = imgp->vp->v_object;
3611616db3cSJohn Dyson 	s = splvm();
3621616db3cSJohn Dyson 
36395461b45SJohn Dyson 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
3641616db3cSJohn Dyson 
36595461b45SJohn Dyson 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
36695461b45SJohn Dyson 		initial_pagein = VM_INITIAL_PAGEIN;
36795461b45SJohn Dyson 		if (initial_pagein > object->size)
36895461b45SJohn Dyson 			initial_pagein = object->size;
36995461b45SJohn Dyson 		for (i = 1; i < initial_pagein; i++) {
37095461b45SJohn Dyson 			if (ma[i] = vm_page_lookup(object, i)) {
37195461b45SJohn Dyson 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
37295461b45SJohn Dyson 					break;
37395461b45SJohn Dyson 				if (ma[i]->valid)
37495461b45SJohn Dyson 					break;
375e69763a3SDoug Rabson 				vm_page_busy(ma[i]);
37695461b45SJohn Dyson 			} else {
37795461b45SJohn Dyson 				ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
37895461b45SJohn Dyson 				if (ma[i] == NULL)
37995461b45SJohn Dyson 					break;
38095461b45SJohn Dyson 			}
38195461b45SJohn Dyson 		}
38295461b45SJohn Dyson 		initial_pagein = i;
3831616db3cSJohn Dyson 
38495461b45SJohn Dyson 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
38595461b45SJohn Dyson 		ma[0] = vm_page_lookup(object, 0);
38695461b45SJohn Dyson 
387eed2412eSJohn Dyson 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) {
388ecbb00a2SDoug Rabson 			if (ma[0]) {
38995461b45SJohn Dyson 				vm_page_protect(ma[0], VM_PROT_NONE);
3908f9110f6SJohn Dyson 				vm_page_free(ma[0]);
391ecbb00a2SDoug Rabson 			}
3921616db3cSJohn Dyson 			splx(s);
3931616db3cSJohn Dyson 			return EIO;
3941616db3cSJohn Dyson 		}
3951616db3cSJohn Dyson 	}
3961616db3cSJohn Dyson 
39795461b45SJohn Dyson 	vm_page_wire(ma[0]);
398e69763a3SDoug Rabson 	vm_page_wakeup(ma[0]);
3991616db3cSJohn Dyson 	splx(s);
4001616db3cSJohn Dyson 
40195461b45SJohn Dyson 	pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0]));
40295461b45SJohn Dyson 	imgp->firstpage = ma[0];
4031616db3cSJohn Dyson 
4041616db3cSJohn Dyson 	return 0;
4051616db3cSJohn Dyson }
4061616db3cSJohn Dyson 
4071616db3cSJohn Dyson void
4081616db3cSJohn Dyson exec_unmap_first_page(imgp)
4091616db3cSJohn Dyson 	struct image_params *imgp;
4101616db3cSJohn Dyson {
4111616db3cSJohn Dyson 	if (imgp->firstpage) {
4121616db3cSJohn Dyson 		pmap_kremove((vm_offset_t) imgp->image_header);
41373007561SDavid Greenman 		vm_page_unwire(imgp->firstpage, 1);
4141616db3cSJohn Dyson 		imgp->firstpage = NULL;
4151616db3cSJohn Dyson 	}
4161616db3cSJohn Dyson }
4171616db3cSJohn Dyson 
41826f9a767SRodney W. Grimes /*
41926f9a767SRodney W. Grimes  * Destroy old address space, and allocate a new stack
42026f9a767SRodney W. Grimes  *	The new stack is only SGROWSIZ large because it is grown
42126f9a767SRodney W. Grimes  *	automatically in trap.c.
42226f9a767SRodney W. Grimes  */
42326f9a767SRodney W. Grimes int
424c52007c2SDavid Greenman exec_new_vmspace(imgp)
425c52007c2SDavid Greenman 	struct image_params *imgp;
42626f9a767SRodney W. Grimes {
42726f9a767SRodney W. Grimes 	int error;
428c52007c2SDavid Greenman 	struct vmspace *vmspace = imgp->proc->p_vmspace;
4292267af78SJulian Elischer #ifdef VM_STACK
4302267af78SJulian Elischer 	caddr_t	stack_addr = (caddr_t) (USRSTACK - MAXSSIZ);
4312267af78SJulian Elischer #else
43226f9a767SRodney W. Grimes 	caddr_t	stack_addr = (caddr_t) (USRSTACK - SGROWSIZ);
4332267af78SJulian Elischer #endif
434af9ec885SJohn Dyson 	vm_map_t map = &vmspace->vm_map;
43526f9a767SRodney W. Grimes 
436c52007c2SDavid Greenman 	imgp->vmspace_destroyed = 1;
43726f9a767SRodney W. Grimes 
438af9ec885SJohn Dyson 	/*
439af9ec885SJohn Dyson 	 * Blow away entire process VM, if address space not shared,
440af9ec885SJohn Dyson 	 * otherwise, create a new VM space so that other threads are
441af9ec885SJohn Dyson 	 * not disrupted
442af9ec885SJohn Dyson 	 */
443af9ec885SJohn Dyson 	if (vmspace->vm_refcnt == 1) {
4443d903220SDoug Rabson 		if (vmspace->vm_shm)
445c52007c2SDavid Greenman 			shmexit(imgp->proc);
4469c0fed3dSDoug Rabson 		pmap_remove_pages(&vmspace->vm_pmap, 0, VM_MAXUSER_ADDRESS);
4479c0fed3dSDoug Rabson 		vm_map_remove(map, 0, VM_MAXUSER_ADDRESS);
448af9ec885SJohn Dyson 	} else {
449492da96cSJohn Dyson 		vmspace_exec(imgp->proc);
450492da96cSJohn Dyson 		vmspace = imgp->proc->p_vmspace;
451af9ec885SJohn Dyson 		map = &vmspace->vm_map;
452af9ec885SJohn Dyson 	}
45326f9a767SRodney W. Grimes 
45426f9a767SRodney W. Grimes 	/* Allocate a new stack */
4552267af78SJulian Elischer #ifdef VM_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;
4672267af78SJulian Elischer #else
4681616db3cSJohn Dyson 	error = vm_map_insert(&vmspace->vm_map, NULL, 0,
4691616db3cSJohn Dyson 		(vm_offset_t) stack_addr, (vm_offset_t) USRSTACK,
4701616db3cSJohn Dyson 		VM_PROT_ALL, VM_PROT_ALL, 0);
47126f9a767SRodney W. Grimes 	if (error)
47226f9a767SRodney W. Grimes 		return (error);
47326f9a767SRodney W. Grimes 
47426f9a767SRodney W. Grimes 	vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT;
47526f9a767SRodney W. Grimes 
47626f9a767SRodney W. Grimes 	/* Initialize maximum stack address */
47726f9a767SRodney W. Grimes 	vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ;
4782267af78SJulian Elischer #endif
47926f9a767SRodney W. Grimes 
48026f9a767SRodney W. Grimes 	return(0);
48126f9a767SRodney W. Grimes }
48226f9a767SRodney W. Grimes 
48326f9a767SRodney W. Grimes /*
48426f9a767SRodney W. Grimes  * Copy out argument and environment strings from the old process
48526f9a767SRodney W. Grimes  *	address space into the temporary string buffer.
48626f9a767SRodney W. Grimes  */
48726f9a767SRodney W. Grimes int
488c52007c2SDavid Greenman exec_extract_strings(imgp)
489c52007c2SDavid Greenman 	struct image_params *imgp;
49026f9a767SRodney W. Grimes {
49126f9a767SRodney W. Grimes 	char	**argv, **envv;
49226f9a767SRodney W. Grimes 	char	*argp, *envp;
493ecbb00a2SDoug Rabson 	int	error;
494ecbb00a2SDoug Rabson 	size_t	length;
49526f9a767SRodney W. Grimes 
49626f9a767SRodney W. Grimes 	/*
49726f9a767SRodney W. Grimes 	 * extract arguments first
49826f9a767SRodney W. Grimes 	 */
49926f9a767SRodney W. Grimes 
500c52007c2SDavid Greenman 	argv = imgp->uap->argv;
50126f9a767SRodney W. Grimes 
5020fd1a014SDavid Greenman 	if (argv) {
503aae0aa45SBruce Evans 		argp = (caddr_t) (intptr_t) fuword(argv);
5045cf3d12cSAndrey A. Chernov 		if (argp == (caddr_t) -1)
5055cf3d12cSAndrey A. Chernov 			return (EFAULT);
5065cf3d12cSAndrey A. Chernov 		if (argp)
5075cf3d12cSAndrey A. Chernov 			argv++;
5085cf3d12cSAndrey A. Chernov 		if (imgp->argv0)
5095cf3d12cSAndrey A. Chernov 			argp = imgp->argv0;
5105cf3d12cSAndrey A. Chernov 		if (argp) {
5115cf3d12cSAndrey A. Chernov 			do {
51226f9a767SRodney W. Grimes 				if (argp == (caddr_t) -1)
51326f9a767SRodney W. Grimes 					return (EFAULT);
514c52007c2SDavid Greenman 				if ((error = copyinstr(argp, imgp->stringp,
515c52007c2SDavid Greenman 				    imgp->stringspace, &length))) {
5160fd1a014SDavid Greenman 					if (error == ENAMETOOLONG)
51726f9a767SRodney W. Grimes 						return(E2BIG);
5180fd1a014SDavid Greenman 					return (error);
5190fd1a014SDavid Greenman 				}
520c52007c2SDavid Greenman 				imgp->stringspace -= length;
521c52007c2SDavid Greenman 				imgp->stringp += length;
522c52007c2SDavid Greenman 				imgp->argc++;
523aae0aa45SBruce Evans 			} while ((argp = (caddr_t) (intptr_t) fuword(argv++)));
52426f9a767SRodney W. Grimes 		}
5250fd1a014SDavid Greenman 	}
52626f9a767SRodney W. Grimes 
52726f9a767SRodney W. Grimes 	/*
52826f9a767SRodney W. Grimes 	 * extract environment strings
52926f9a767SRodney W. Grimes 	 */
53026f9a767SRodney W. Grimes 
531c52007c2SDavid Greenman 	envv = imgp->uap->envv;
53226f9a767SRodney W. Grimes 
5330fd1a014SDavid Greenman 	if (envv) {
534aae0aa45SBruce Evans 		while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
53526f9a767SRodney W. Grimes 			if (envp == (caddr_t) -1)
53626f9a767SRodney W. Grimes 				return (EFAULT);
537c52007c2SDavid Greenman 			if ((error = copyinstr(envp, imgp->stringp,
538c52007c2SDavid Greenman 			    imgp->stringspace, &length))) {
5390fd1a014SDavid Greenman 				if (error == ENAMETOOLONG)
54026f9a767SRodney W. Grimes 					return(E2BIG);
5410fd1a014SDavid Greenman 				return (error);
5420fd1a014SDavid Greenman 			}
543c52007c2SDavid Greenman 			imgp->stringspace -= length;
544c52007c2SDavid Greenman 			imgp->stringp += length;
545c52007c2SDavid Greenman 			imgp->envc++;
54626f9a767SRodney W. Grimes 		}
5470fd1a014SDavid Greenman 	}
54826f9a767SRodney W. Grimes 
54926f9a767SRodney W. Grimes 	return (0);
55026f9a767SRodney W. Grimes }
55126f9a767SRodney W. Grimes 
55226f9a767SRodney W. Grimes /*
55326f9a767SRodney W. Grimes  * Copy strings out to the new process address space, constructing
55426f9a767SRodney W. Grimes  *	new arg and env vector tables. Return a pointer to the base
55526f9a767SRodney W. Grimes  *	so that it can be used as the initial stack pointer.
55626f9a767SRodney W. Grimes  */
557ecbb00a2SDoug Rabson long *
558c52007c2SDavid Greenman exec_copyout_strings(imgp)
559c52007c2SDavid Greenman 	struct image_params *imgp;
56026f9a767SRodney W. Grimes {
56126f9a767SRodney W. Grimes 	int argc, envc;
56226f9a767SRodney W. Grimes 	char **vectp;
56326f9a767SRodney W. Grimes 	char *stringp, *destp;
564ecbb00a2SDoug Rabson 	long *stack_base;
56593f6448cSDavid Greenman 	struct ps_strings *arginfo;
566d66a5066SPeter Wemm 	int szsigcode;
56726f9a767SRodney W. Grimes 
56826f9a767SRodney W. Grimes 	/*
56926f9a767SRodney W. Grimes 	 * Calculate string base and vector table pointers.
570d66a5066SPeter Wemm 	 * Also deal with signal trampoline code for this exec type.
57126f9a767SRodney W. Grimes 	 */
5724c56fcdeSBruce Evans 	arginfo = (struct ps_strings *)PS_STRINGS;
573d66a5066SPeter Wemm 	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
574d66a5066SPeter Wemm 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
5751ed012f9SPeter Wemm 		roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
5761ed012f9SPeter Wemm 
57726f9a767SRodney W. Grimes 	/*
578d66a5066SPeter Wemm 	 * install sigcode
579d66a5066SPeter Wemm 	 */
580d66a5066SPeter Wemm 	if (szsigcode)
581d66a5066SPeter Wemm 		copyout(imgp->proc->p_sysent->sv_sigcode,
582d66a5066SPeter Wemm 			((caddr_t)arginfo - szsigcode), szsigcode);
583d66a5066SPeter Wemm 
584d66a5066SPeter Wemm 	/*
585e1743d02SSøren Schmidt 	 * If we have a valid auxargs ptr, prepare some room
586e1743d02SSøren Schmidt 	 * on the stack.
587e1743d02SSøren Schmidt 	 */
588e1743d02SSøren Schmidt 	if (imgp->auxargs)
589e1743d02SSøren Schmidt 	/*
590e1743d02SSøren Schmidt 	 * The '+ 2' is for the null pointers at the end of each of the
591e1743d02SSøren Schmidt 	 * arg and env vector sets, and 'AT_COUNT*2' is room for the
592e1743d02SSøren Schmidt 	 * ELF Auxargs data.
593e1743d02SSøren Schmidt 	 */
594e1743d02SSøren Schmidt 		vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 +
595e1743d02SSøren Schmidt 				  AT_COUNT*2) * sizeof(char*));
596e1743d02SSøren Schmidt 	else
597e1743d02SSøren Schmidt 	/*
59826f9a767SRodney W. Grimes 	 * The '+ 2' is for the null pointers at the end of each of the
59926f9a767SRodney W. Grimes 	 * arg and env vector sets
60026f9a767SRodney W. Grimes 	 */
601e1743d02SSøren Schmidt 		vectp = (char **)
602e1743d02SSøren Schmidt 			(destp - (imgp->argc + imgp->envc + 2) * sizeof(char*));
60326f9a767SRodney W. Grimes 
60426f9a767SRodney W. Grimes 	/*
60526f9a767SRodney W. Grimes 	 * vectp also becomes our initial stack base
60626f9a767SRodney W. Grimes 	 */
607ecbb00a2SDoug Rabson 	stack_base = (long *)vectp;
60826f9a767SRodney W. Grimes 
609c52007c2SDavid Greenman 	stringp = imgp->stringbase;
610c52007c2SDavid Greenman 	argc = imgp->argc;
611c52007c2SDavid Greenman 	envc = imgp->envc;
61226f9a767SRodney W. Grimes 
61393f6448cSDavid Greenman 	/*
6143aed948bSDavid Greenman 	 * Copy out strings - arguments and environment.
61593f6448cSDavid Greenman 	 */
616c52007c2SDavid Greenman 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
61793f6448cSDavid Greenman 
61893f6448cSDavid Greenman 	/*
6193aed948bSDavid Greenman 	 * Fill in "ps_strings" struct for ps, w, etc.
6203aed948bSDavid Greenman 	 */
621aae0aa45SBruce Evans 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
6223aed948bSDavid Greenman 	suword(&arginfo->ps_nargvstr, argc);
6233aed948bSDavid Greenman 
6243aed948bSDavid Greenman 	/*
6253aed948bSDavid Greenman 	 * Fill in argument portion of vector table.
62693f6448cSDavid Greenman 	 */
62726f9a767SRodney W. Grimes 	for (; argc > 0; --argc) {
628aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
6293aed948bSDavid Greenman 		while (*stringp++ != 0)
6303aed948bSDavid Greenman 			destp++;
6313aed948bSDavid Greenman 		destp++;
63226f9a767SRodney W. Grimes 	}
63326f9a767SRodney W. Grimes 
63426f9a767SRodney W. Grimes 	/* a null vector table pointer seperates the argp's from the envp's */
6356ab46d52SBruce Evans 	suword(vectp++, 0);
63626f9a767SRodney W. Grimes 
637aae0aa45SBruce Evans 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
6383aed948bSDavid Greenman 	suword(&arginfo->ps_nenvstr, envc);
63993f6448cSDavid Greenman 
64093f6448cSDavid Greenman 	/*
6413aed948bSDavid Greenman 	 * Fill in environment portion of vector table.
64293f6448cSDavid Greenman 	 */
64326f9a767SRodney W. Grimes 	for (; envc > 0; --envc) {
644aae0aa45SBruce Evans 		suword(vectp++, (long)(intptr_t)destp);
6453aed948bSDavid Greenman 		while (*stringp++ != 0)
6463aed948bSDavid Greenman 			destp++;
6473aed948bSDavid Greenman 		destp++;
64826f9a767SRodney W. Grimes 	}
64926f9a767SRodney W. Grimes 
65026f9a767SRodney W. Grimes 	/* end of vector table is a null pointer */
6516ab46d52SBruce Evans 	suword(vectp, 0);
65226f9a767SRodney W. Grimes 
65326f9a767SRodney W. Grimes 	return (stack_base);
65426f9a767SRodney W. Grimes }
65526f9a767SRodney W. Grimes 
65626f9a767SRodney W. Grimes /*
65726f9a767SRodney W. Grimes  * Check permissions of file to execute.
65826f9a767SRodney W. Grimes  *	Return 0 for success or error code on failure.
65926f9a767SRodney W. Grimes  */
660c8a79999SPeter Wemm int
661c52007c2SDavid Greenman exec_check_permissions(imgp)
662c52007c2SDavid Greenman 	struct image_params *imgp;
66326f9a767SRodney W. Grimes {
664c52007c2SDavid Greenman 	struct proc *p = imgp->proc;
665c52007c2SDavid Greenman 	struct vnode *vp = imgp->vp;
666c52007c2SDavid Greenman 	struct vattr *attr = imgp->attr;
66726f9a767SRodney W. Grimes 	int error;
66826f9a767SRodney W. Grimes 
66926f9a767SRodney W. Grimes 	/* Get file attributes */
670c52007c2SDavid Greenman 	error = VOP_GETATTR(vp, attr, p->p_ucred, p);
67126f9a767SRodney W. Grimes 	if (error)
67226f9a767SRodney W. Grimes 		return (error);
67326f9a767SRodney W. Grimes 
67426f9a767SRodney W. Grimes 	/*
67526f9a767SRodney W. Grimes 	 * 1) Check if file execution is disabled for the filesystem that this
67626f9a767SRodney W. Grimes 	 *	file resides on.
67726f9a767SRodney W. Grimes 	 * 2) Insure that at least one execute bit is on - otherwise root
67826f9a767SRodney W. Grimes 	 *	will always succeed, and we don't want to happen unless the
67926f9a767SRodney W. Grimes 	 *	file really is executable.
68026f9a767SRodney W. Grimes 	 * 3) Insure that the file is a regular file.
68126f9a767SRodney W. Grimes 	 */
682c52007c2SDavid Greenman 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
68326f9a767SRodney W. Grimes 	    ((attr->va_mode & 0111) == 0) ||
68426f9a767SRodney W. Grimes 	    (attr->va_type != VREG)) {
68526f9a767SRodney W. Grimes 		return (EACCES);
68626f9a767SRodney W. Grimes 	}
68726f9a767SRodney W. Grimes 
68826f9a767SRodney W. Grimes 	/*
68926f9a767SRodney W. Grimes 	 * Zero length files can't be exec'd
69026f9a767SRodney W. Grimes 	 */
69126f9a767SRodney W. Grimes 	if (attr->va_size == 0)
69226f9a767SRodney W. Grimes 		return (ENOEXEC);
69326f9a767SRodney W. Grimes 
69426f9a767SRodney W. Grimes 	/*
69526f9a767SRodney W. Grimes 	 *  Check for execute permission to file based on current credentials.
69626f9a767SRodney W. Grimes 	 */
697c52007c2SDavid Greenman 	error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
69826f9a767SRodney W. Grimes 	if (error)
69926f9a767SRodney W. Grimes 		return (error);
70026f9a767SRodney W. Grimes 
7016d5a0a8cSDavid Greenman 	/*
7026d5a0a8cSDavid Greenman 	 * Check number of open-for-writes on the file and deny execution
7036d5a0a8cSDavid Greenman 	 * if there are any.
7046d5a0a8cSDavid Greenman 	 */
7056d5a0a8cSDavid Greenman 	if (vp->v_writecount)
7066d5a0a8cSDavid Greenman 		return (ETXTBSY);
7076d5a0a8cSDavid Greenman 
7086d5a0a8cSDavid Greenman 	/*
7096d5a0a8cSDavid Greenman 	 * Call filesystem specific open routine (which does nothing in the
7106d5a0a8cSDavid Greenman 	 * general case).
7116d5a0a8cSDavid Greenman 	 */
712c52007c2SDavid Greenman 	error = VOP_OPEN(vp, FREAD, p->p_ucred, p);
71326f9a767SRodney W. Grimes 	if (error)
71426f9a767SRodney W. Grimes 		return (error);
71526f9a767SRodney W. Grimes 
71626f9a767SRodney W. Grimes 	return (0);
717df8bae1dSRodney W. Grimes }
718aa855a59SPeter Wemm 
719aa855a59SPeter Wemm /*
720aa855a59SPeter Wemm  * Exec handler registration
721aa855a59SPeter Wemm  */
722aa855a59SPeter Wemm int
723aa855a59SPeter Wemm exec_register(execsw_arg)
724aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
725aa855a59SPeter Wemm {
726aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
727aa855a59SPeter Wemm 	int count = 2;	/* New slot and trailing NULL */
728aa855a59SPeter Wemm 
729aa855a59SPeter Wemm 	if (execsw)
730aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
731aa855a59SPeter Wemm 			count++;
732aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
733aa855a59SPeter Wemm 	if (newexecsw == NULL)
734aa855a59SPeter Wemm 		return ENOMEM;
735aa855a59SPeter Wemm 	xs = newexecsw;
736aa855a59SPeter Wemm 	if (execsw)
737aa855a59SPeter Wemm 		for (es = execsw; *es; es++)
738aa855a59SPeter Wemm 			*xs++ = *es;
739aa855a59SPeter Wemm 	*xs++ = execsw_arg;
740aa855a59SPeter Wemm 	*xs = NULL;
741aa855a59SPeter Wemm 	if (execsw)
742aa855a59SPeter Wemm 		free(execsw, M_TEMP);
743aa855a59SPeter Wemm 	execsw = newexecsw;
744aa855a59SPeter Wemm 	return 0;
745aa855a59SPeter Wemm }
746aa855a59SPeter Wemm 
747aa855a59SPeter Wemm int
748aa855a59SPeter Wemm exec_unregister(execsw_arg)
749aa855a59SPeter Wemm 	const struct execsw *execsw_arg;
750aa855a59SPeter Wemm {
751aa855a59SPeter Wemm 	const struct execsw **es, **xs, **newexecsw;
752aa855a59SPeter Wemm 	int count = 1;
753aa855a59SPeter Wemm 
754aa855a59SPeter Wemm 	if (execsw == NULL)
755aa855a59SPeter Wemm 		panic("unregister with no handlers left?\n");
756aa855a59SPeter Wemm 
757aa855a59SPeter Wemm 	for (es = execsw; *es; es++) {
758aa855a59SPeter Wemm 		if (*es == execsw_arg)
759aa855a59SPeter Wemm 			break;
760aa855a59SPeter Wemm 	}
761aa855a59SPeter Wemm 	if (*es == NULL)
762aa855a59SPeter Wemm 		return ENOENT;
763aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
764aa855a59SPeter Wemm 		if (*es != execsw_arg)
765aa855a59SPeter Wemm 			count++;
766aa855a59SPeter Wemm 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
767aa855a59SPeter Wemm 	if (newexecsw == NULL)
768aa855a59SPeter Wemm 		return ENOMEM;
769aa855a59SPeter Wemm 	xs = newexecsw;
770aa855a59SPeter Wemm 	for (es = execsw; *es; es++)
771aa855a59SPeter Wemm 		if (*es != execsw_arg)
772aa855a59SPeter Wemm 			*xs++ = *es;
773aa855a59SPeter Wemm 	*xs = NULL;
774aa855a59SPeter Wemm 	if (execsw)
775aa855a59SPeter Wemm 		free(execsw, M_TEMP);
776aa855a59SPeter Wemm 	execsw = newexecsw;
777aa855a59SPeter Wemm 	return 0;
778aa855a59SPeter Wemm }
779