xref: /freebsd/sys/kern/kern_exec.c (revision 4cf49a43559ed9fdad601bdcccd2c55963008675)
1 /*
2  * Copyright (c) 1993, David Greenman
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/sysproto.h>
32 #include <sys/signalvar.h>
33 #include <sys/kernel.h>
34 #include <sys/mount.h>
35 #include <sys/filedesc.h>
36 #include <sys/fcntl.h>
37 #include <sys/acct.h>
38 #include <sys/exec.h>
39 #include <sys/imgact.h>
40 #include <sys/imgact_elf.h>
41 #include <sys/wait.h>
42 #include <sys/proc.h>
43 #include <sys/pioctl.h>
44 #include <sys/malloc.h>
45 #include <sys/namei.h>
46 #include <sys/sysent.h>
47 #include <sys/shm.h>
48 #include <sys/sysctl.h>
49 #include <sys/vnode.h>
50 #include <sys/buf.h>
51 
52 #include <vm/vm.h>
53 #include <vm/vm_param.h>
54 #include <vm/vm_prot.h>
55 #include <sys/lock.h>
56 #include <vm/pmap.h>
57 #include <vm/vm_page.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_kern.h>
60 #include <vm/vm_extern.h>
61 #include <vm/vm_object.h>
62 #include <vm/vm_zone.h>
63 #include <vm/vm_pager.h>
64 
65 #include <machine/reg.h>
66 
67 static long *exec_copyout_strings __P((struct image_params *));
68 
69 static long ps_strings = PS_STRINGS;
70 SYSCTL_LONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, "");
71 
72 static long usrstack = USRSTACK;
73 SYSCTL_LONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, "");
74 
75 /*
76  * Each of the items is a pointer to a `const struct execsw', hence the
77  * double pointer here.
78  */
79 static const struct execsw **execsw;
80 
81 #ifndef _SYS_SYSPROTO_H_
82 struct execve_args {
83         char    *fname;
84         char    **argv;
85         char    **envv;
86 };
87 #endif
88 
89 /*
90  * execve() system call.
91  */
92 int
93 execve(p, uap)
94 	struct proc *p;
95 	register struct execve_args *uap;
96 {
97 	struct nameidata nd, *ndp;
98 	long *stack_base;
99 	int error, len, i;
100 	struct image_params image_params, *imgp;
101 	struct vattr attr;
102 
103 	imgp = &image_params;
104 
105 	/*
106 	 * Initialize part of the common data
107 	 */
108 	imgp->proc = p;
109 	imgp->uap = uap;
110 	imgp->attr = &attr;
111 	imgp->argc = imgp->envc = 0;
112 	imgp->argv0 = NULL;
113 	imgp->entry_addr = 0;
114 	imgp->vmspace_destroyed = 0;
115 	imgp->interpreted = 0;
116 	imgp->interpreter_name[0] = '\0';
117 	imgp->auxargs = NULL;
118 	imgp->vp = NULL;
119 	imgp->firstpage = NULL;
120 	imgp->ps_strings = 0;
121 
122 	/*
123 	 * Allocate temporary demand zeroed space for argument and
124 	 *	environment strings
125 	 */
126 	imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE);
127 	if (imgp->stringbase == NULL) {
128 		error = ENOMEM;
129 		goto exec_fail;
130 	}
131 	imgp->stringp = imgp->stringbase;
132 	imgp->stringspace = ARG_MAX;
133 	imgp->image_header = imgp->stringbase + ARG_MAX;
134 
135 	/*
136 	 * Translate the file name. namei() returns a vnode pointer
137 	 *	in ni_vp amoung other things.
138 	 */
139 	ndp = &nd;
140 	NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
141 	    UIO_USERSPACE, uap->fname, p);
142 
143 interpret:
144 
145 	error = namei(ndp);
146 	if (error) {
147 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
148 			ARG_MAX + PAGE_SIZE);
149 		goto exec_fail;
150 	}
151 
152 	imgp->vp = ndp->ni_vp;
153 	imgp->fname = uap->fname;
154 
155 	/*
156 	 * Check file permissions (also 'opens' file)
157 	 */
158 	error = exec_check_permissions(imgp);
159 	if (error) {
160 		VOP_UNLOCK(imgp->vp, 0, p);
161 		goto exec_fail_dealloc;
162 	}
163 
164 	error = exec_map_first_page(imgp);
165 	VOP_UNLOCK(imgp->vp, 0, p);
166 	if (error)
167 		goto exec_fail_dealloc;
168 
169 	/*
170 	 * Loop through list of image activators, calling each one.
171 	 *	If there is no match, the activator returns -1. If there
172 	 *	is a match, but there was an error during the activation,
173 	 *	the error is returned. Otherwise 0 means success. If the
174 	 *	image is interpreted, loop back up and try activating
175 	 *	the interpreter.
176 	 */
177 	for (i = 0; execsw[i]; ++i) {
178 		if (execsw[i]->ex_imgact)
179 			error = (*execsw[i]->ex_imgact)(imgp);
180 		else
181 			continue;
182 		if (error == -1)
183 			continue;
184 		if (error)
185 			goto exec_fail_dealloc;
186 		if (imgp->interpreted) {
187 			exec_unmap_first_page(imgp);
188 			/* free old vnode and name buffer */
189 			vrele(ndp->ni_vp);
190 			zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
191 			/* set new name to that of the interpreter */
192 			NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME,
193 			    UIO_SYSSPACE, imgp->interpreter_name, p);
194 			goto interpret;
195 		}
196 		break;
197 	}
198 	/* If we made it through all the activators and none matched, exit. */
199 	if (error == -1) {
200 		error = ENOEXEC;
201 		goto exec_fail_dealloc;
202 	}
203 
204 	/*
205 	 * Copy out strings (args and env) and initialize stack base
206 	 */
207 	stack_base = exec_copyout_strings(imgp);
208 	p->p_vmspace->vm_minsaddr = (char *)stack_base;
209 
210 	/*
211 	 * If custom stack fixup routine present for this process
212 	 * let it do the stack setup.
213 	 * Else stuff argument count as first item on stack
214 	 */
215 	if (p->p_sysent->sv_fixup)
216 		(*p->p_sysent->sv_fixup)(&stack_base, imgp);
217 	else
218 		suword(--stack_base, imgp->argc);
219 
220 	/*
221 	 * For security and other reasons, the file descriptor table cannot
222 	 * be shared after an exec.
223 	 */
224 	if (p->p_fd->fd_refcnt > 1) {
225 		struct filedesc *tmp;
226 
227 		tmp = fdcopy(p);
228 		fdfree(p);
229 		p->p_fd = tmp;
230 	}
231 
232 	/* Stop profiling */
233 	stopprofclock(p);
234 
235 	/* close files on exec */
236 	fdcloseexec(p);
237 
238 	/* reset caught signals */
239 	execsigs(p);
240 
241 	/* name this process - nameiexec(p, ndp) */
242 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
243 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
244 	p->p_comm[len] = 0;
245 
246 	/*
247 	 * mark as execed, wakeup the process that vforked (if any) and tell
248 	 * it that it now has its own resources back
249 	 */
250 	p->p_flag |= P_EXEC;
251 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
252 		p->p_flag &= ~P_PPWAIT;
253 		wakeup((caddr_t)p->p_pptr);
254 	}
255 
256 	/*
257 	 * Implement image setuid/setgid.
258 	 *
259 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
260 	 * the process is being traced.
261 	 */
262 	if ((((attr.va_mode & VSUID) && p->p_ucred->cr_uid != attr.va_uid) ||
263 	     ((attr.va_mode & VSGID) && p->p_ucred->cr_gid != attr.va_gid)) &&
264 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
265 	    (p->p_flag & P_TRACED) == 0) {
266 		/*
267 		 * Turn off syscall tracing for set-id programs, except for
268 		 * root.
269 		 */
270 		if (p->p_tracep && suser(p)) {
271 			p->p_traceflag = 0;
272 			vrele(p->p_tracep);
273 			p->p_tracep = NULL;
274 		}
275 		/*
276 		 * Set the new credentials.
277 		 */
278 		p->p_ucred = crcopy(p->p_ucred);
279 		if (attr.va_mode & VSUID)
280 			p->p_ucred->cr_uid = attr.va_uid;
281 		if (attr.va_mode & VSGID)
282 			p->p_ucred->cr_gid = attr.va_gid;
283 		setsugid(p);
284 	} else {
285 		if (p->p_ucred->cr_uid == p->p_cred->p_ruid &&
286 		    p->p_ucred->cr_gid == p->p_cred->p_rgid)
287 			p->p_flag &= ~P_SUGID;
288 	}
289 
290 	/*
291 	 * Implement correct POSIX saved-id behavior.
292 	 */
293 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
294 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
295 
296 	/*
297 	 * Store the vp for use in procfs
298 	 */
299 	if (p->p_textvp)		/* release old reference */
300 		vrele(p->p_textvp);
301 	VREF(ndp->ni_vp);
302 	p->p_textvp = ndp->ni_vp;
303 
304 	/*
305 	 * If tracing the process, trap to debugger so breakpoints
306 	 * 	can be set before the program executes.
307 	 */
308 	STOPEVENT(p, S_EXEC, 0);
309 
310 	if (p->p_flag & P_TRACED)
311 		psignal(p, SIGTRAP);
312 
313 	/* clear "fork but no exec" flag, as we _are_ execing */
314 	p->p_acflag &= ~AFORK;
315 
316 	/* Set values passed into the program in registers. */
317 	setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base,
318 	    imgp->ps_strings);
319 
320 exec_fail_dealloc:
321 
322 	/*
323 	 * free various allocated resources
324 	 */
325 	if (imgp->firstpage)
326 		exec_unmap_first_page(imgp);
327 
328 	if (imgp->stringbase != NULL)
329 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
330 			ARG_MAX + PAGE_SIZE);
331 
332 	if (imgp->vp) {
333 		vrele(imgp->vp);
334 		zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
335 	}
336 
337 	if (error == 0)
338 		return (0);
339 
340 exec_fail:
341 	if (imgp->vmspace_destroyed) {
342 		/* sorry, no more process anymore. exit gracefully */
343 		exit1(p, W_EXITCODE(0, SIGABRT));
344 		/* NOT REACHED */
345 		return(0);
346 	} else {
347 		return(error);
348 	}
349 }
350 
351 int
352 exec_map_first_page(imgp)
353 	struct image_params *imgp;
354 {
355 	int s, rv, i;
356 	int initial_pagein;
357 	vm_page_t ma[VM_INITIAL_PAGEIN];
358 	vm_object_t object;
359 
360 
361 	if (imgp->firstpage) {
362 		exec_unmap_first_page(imgp);
363 	}
364 
365 	object = imgp->vp->v_object;
366 	s = splvm();
367 
368 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
369 
370 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
371 		initial_pagein = VM_INITIAL_PAGEIN;
372 		if (initial_pagein > object->size)
373 			initial_pagein = object->size;
374 		for (i = 1; i < initial_pagein; i++) {
375 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
376 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
377 					break;
378 				if (ma[i]->valid)
379 					break;
380 				vm_page_busy(ma[i]);
381 			} else {
382 				ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
383 				if (ma[i] == NULL)
384 					break;
385 			}
386 		}
387 		initial_pagein = i;
388 
389 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
390 		ma[0] = vm_page_lookup(object, 0);
391 
392 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) {
393 			if (ma[0]) {
394 				vm_page_protect(ma[0], VM_PROT_NONE);
395 				vm_page_free(ma[0]);
396 			}
397 			splx(s);
398 			return EIO;
399 		}
400 	}
401 
402 	vm_page_wire(ma[0]);
403 	vm_page_wakeup(ma[0]);
404 	splx(s);
405 
406 	pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0]));
407 	imgp->firstpage = ma[0];
408 
409 	return 0;
410 }
411 
412 void
413 exec_unmap_first_page(imgp)
414 	struct image_params *imgp;
415 {
416 	if (imgp->firstpage) {
417 		pmap_kremove((vm_offset_t) imgp->image_header);
418 		vm_page_unwire(imgp->firstpage, 1);
419 		imgp->firstpage = NULL;
420 	}
421 }
422 
423 /*
424  * Destroy old address space, and allocate a new stack
425  *	The new stack is only SGROWSIZ large because it is grown
426  *	automatically in trap.c.
427  */
428 int
429 exec_new_vmspace(imgp)
430 	struct image_params *imgp;
431 {
432 	int error;
433 	struct vmspace *vmspace = imgp->proc->p_vmspace;
434 	caddr_t	stack_addr = (caddr_t) (USRSTACK - MAXSSIZ);
435 	vm_map_t map = &vmspace->vm_map;
436 
437 	imgp->vmspace_destroyed = 1;
438 
439 	/*
440 	 * Blow away entire process VM, if address space not shared,
441 	 * otherwise, create a new VM space so that other threads are
442 	 * not disrupted
443 	 */
444 	if (vmspace->vm_refcnt == 1) {
445 		if (vmspace->vm_shm)
446 			shmexit(imgp->proc);
447 		pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS);
448 		vm_map_remove(map, 0, VM_MAXUSER_ADDRESS);
449 	} else {
450 		vmspace_exec(imgp->proc);
451 		vmspace = imgp->proc->p_vmspace;
452 		map = &vmspace->vm_map;
453 	}
454 
455 	/* Allocate a new stack */
456 	error = vm_map_stack (&vmspace->vm_map, (vm_offset_t)stack_addr,
457 			      (vm_size_t)MAXSSIZ, VM_PROT_ALL, VM_PROT_ALL, 0);
458 	if (error)
459 		return (error);
460 
461 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
462 	 * VM_STACK case, but they are still used to monitor the size of the
463 	 * process stack so we can check the stack rlimit.
464 	 */
465 	vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT;
466 	vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ;
467 
468 	return(0);
469 }
470 
471 /*
472  * Copy out argument and environment strings from the old process
473  *	address space into the temporary string buffer.
474  */
475 int
476 exec_extract_strings(imgp)
477 	struct image_params *imgp;
478 {
479 	char	**argv, **envv;
480 	char	*argp, *envp;
481 	int	error;
482 	size_t	length;
483 
484 	/*
485 	 * extract arguments first
486 	 */
487 
488 	argv = imgp->uap->argv;
489 
490 	if (argv) {
491 		argp = (caddr_t) (intptr_t) fuword(argv);
492 		if (argp == (caddr_t) -1)
493 			return (EFAULT);
494 		if (argp)
495 			argv++;
496 		if (imgp->argv0)
497 			argp = imgp->argv0;
498 		if (argp) {
499 			do {
500 				if (argp == (caddr_t) -1)
501 					return (EFAULT);
502 				if ((error = copyinstr(argp, imgp->stringp,
503 				    imgp->stringspace, &length))) {
504 					if (error == ENAMETOOLONG)
505 						return(E2BIG);
506 					return (error);
507 				}
508 				imgp->stringspace -= length;
509 				imgp->stringp += length;
510 				imgp->argc++;
511 			} while ((argp = (caddr_t) (intptr_t) fuword(argv++)));
512 		}
513 	}
514 
515 	/*
516 	 * extract environment strings
517 	 */
518 
519 	envv = imgp->uap->envv;
520 
521 	if (envv) {
522 		while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
523 			if (envp == (caddr_t) -1)
524 				return (EFAULT);
525 			if ((error = copyinstr(envp, imgp->stringp,
526 			    imgp->stringspace, &length))) {
527 				if (error == ENAMETOOLONG)
528 					return(E2BIG);
529 				return (error);
530 			}
531 			imgp->stringspace -= length;
532 			imgp->stringp += length;
533 			imgp->envc++;
534 		}
535 	}
536 
537 	return (0);
538 }
539 
540 /*
541  * Copy strings out to the new process address space, constructing
542  *	new arg and env vector tables. Return a pointer to the base
543  *	so that it can be used as the initial stack pointer.
544  */
545 long *
546 exec_copyout_strings(imgp)
547 	struct image_params *imgp;
548 {
549 	int argc, envc;
550 	char **vectp;
551 	char *stringp, *destp;
552 	long *stack_base;
553 	struct ps_strings *arginfo;
554 	int szsigcode;
555 
556 	/*
557 	 * Calculate string base and vector table pointers.
558 	 * Also deal with signal trampoline code for this exec type.
559 	 */
560 	arginfo = (struct ps_strings *)PS_STRINGS;
561 	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
562 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
563 		roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
564 
565 	/*
566 	 * install sigcode
567 	 */
568 	if (szsigcode)
569 		copyout(imgp->proc->p_sysent->sv_sigcode,
570 			((caddr_t)arginfo - szsigcode), szsigcode);
571 
572 	/*
573 	 * If we have a valid auxargs ptr, prepare some room
574 	 * on the stack.
575 	 */
576 	if (imgp->auxargs)
577 	/*
578 	 * The '+ 2' is for the null pointers at the end of each of the
579 	 * arg and env vector sets, and 'AT_COUNT*2' is room for the
580 	 * ELF Auxargs data.
581 	 */
582 		vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 +
583 				  AT_COUNT*2) * sizeof(char*));
584 	else
585 	/*
586 	 * The '+ 2' is for the null pointers at the end of each of the
587 	 * arg and env vector sets
588 	 */
589 		vectp = (char **)
590 			(destp - (imgp->argc + imgp->envc + 2) * sizeof(char*));
591 
592 	/*
593 	 * vectp also becomes our initial stack base
594 	 */
595 	stack_base = (long *)vectp;
596 
597 	stringp = imgp->stringbase;
598 	argc = imgp->argc;
599 	envc = imgp->envc;
600 
601 	/*
602 	 * Copy out strings - arguments and environment.
603 	 */
604 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
605 
606 	/*
607 	 * Fill in "ps_strings" struct for ps, w, etc.
608 	 */
609 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
610 	suword(&arginfo->ps_nargvstr, argc);
611 
612 	/*
613 	 * Fill in argument portion of vector table.
614 	 */
615 	for (; argc > 0; --argc) {
616 		suword(vectp++, (long)(intptr_t)destp);
617 		while (*stringp++ != 0)
618 			destp++;
619 		destp++;
620 	}
621 
622 	/* a null vector table pointer seperates the argp's from the envp's */
623 	suword(vectp++, 0);
624 
625 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
626 	suword(&arginfo->ps_nenvstr, envc);
627 
628 	/*
629 	 * Fill in environment portion of vector table.
630 	 */
631 	for (; envc > 0; --envc) {
632 		suword(vectp++, (long)(intptr_t)destp);
633 		while (*stringp++ != 0)
634 			destp++;
635 		destp++;
636 	}
637 
638 	/* end of vector table is a null pointer */
639 	suword(vectp, 0);
640 
641 	return (stack_base);
642 }
643 
644 /*
645  * Check permissions of file to execute.
646  *	Return 0 for success or error code on failure.
647  */
648 int
649 exec_check_permissions(imgp)
650 	struct image_params *imgp;
651 {
652 	struct proc *p = imgp->proc;
653 	struct vnode *vp = imgp->vp;
654 	struct vattr *attr = imgp->attr;
655 	int error;
656 
657 	/* Get file attributes */
658 	error = VOP_GETATTR(vp, attr, p->p_ucred, p);
659 	if (error)
660 		return (error);
661 
662 	/*
663 	 * 1) Check if file execution is disabled for the filesystem that this
664 	 *	file resides on.
665 	 * 2) Insure that at least one execute bit is on - otherwise root
666 	 *	will always succeed, and we don't want to happen unless the
667 	 *	file really is executable.
668 	 * 3) Insure that the file is a regular file.
669 	 */
670 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
671 	    ((attr->va_mode & 0111) == 0) ||
672 	    (attr->va_type != VREG)) {
673 		return (EACCES);
674 	}
675 
676 	/*
677 	 * Zero length files can't be exec'd
678 	 */
679 	if (attr->va_size == 0)
680 		return (ENOEXEC);
681 
682 	/*
683 	 *  Check for execute permission to file based on current credentials.
684 	 */
685 	error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
686 	if (error)
687 		return (error);
688 
689 	/*
690 	 * Check number of open-for-writes on the file and deny execution
691 	 * if there are any.
692 	 */
693 	if (vp->v_writecount)
694 		return (ETXTBSY);
695 
696 	/*
697 	 * Call filesystem specific open routine (which does nothing in the
698 	 * general case).
699 	 */
700 	error = VOP_OPEN(vp, FREAD, p->p_ucred, p);
701 	if (error)
702 		return (error);
703 
704 	return (0);
705 }
706 
707 /*
708  * Exec handler registration
709  */
710 int
711 exec_register(execsw_arg)
712 	const struct execsw *execsw_arg;
713 {
714 	const struct execsw **es, **xs, **newexecsw;
715 	int count = 2;	/* New slot and trailing NULL */
716 
717 	if (execsw)
718 		for (es = execsw; *es; es++)
719 			count++;
720 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
721 	if (newexecsw == NULL)
722 		return ENOMEM;
723 	xs = newexecsw;
724 	if (execsw)
725 		for (es = execsw; *es; es++)
726 			*xs++ = *es;
727 	*xs++ = execsw_arg;
728 	*xs = NULL;
729 	if (execsw)
730 		free(execsw, M_TEMP);
731 	execsw = newexecsw;
732 	return 0;
733 }
734 
735 int
736 exec_unregister(execsw_arg)
737 	const struct execsw *execsw_arg;
738 {
739 	const struct execsw **es, **xs, **newexecsw;
740 	int count = 1;
741 
742 	if (execsw == NULL)
743 		panic("unregister with no handlers left?\n");
744 
745 	for (es = execsw; *es; es++) {
746 		if (*es == execsw_arg)
747 			break;
748 	}
749 	if (*es == NULL)
750 		return ENOENT;
751 	for (es = execsw; *es; es++)
752 		if (*es != execsw_arg)
753 			count++;
754 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
755 	if (newexecsw == NULL)
756 		return ENOMEM;
757 	xs = newexecsw;
758 	for (es = execsw; *es; es++)
759 		if (*es != execsw_arg)
760 			*xs++ = *es;
761 	*xs = NULL;
762 	if (execsw)
763 		free(execsw, M_TEMP);
764 	execsw = newexecsw;
765 	return 0;
766 }
767