xref: /freebsd/sys/kern/kern_exec.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
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  *	$Id: kern_exec.c,v 1.98 1999/04/19 14:14:09 peter Exp $
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 	/* close files on exec */
233 	fdcloseexec(p);
234 
235 	/* reset caught signals */
236 	execsigs(p);
237 
238 	/* name this process - nameiexec(p, ndp) */
239 	len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN);
240 	bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len);
241 	p->p_comm[len] = 0;
242 
243 	/*
244 	 * mark as execed, wakeup the process that vforked (if any) and tell
245 	 * it that it now has its own resources back
246 	 */
247 	p->p_flag |= P_EXEC;
248 	if (p->p_pptr && (p->p_flag & P_PPWAIT)) {
249 		p->p_flag &= ~P_PPWAIT;
250 		wakeup((caddr_t)p->p_pptr);
251 	}
252 
253 	/*
254 	 * Implement image setuid/setgid.
255 	 *
256 	 * Don't honor setuid/setgid if the filesystem prohibits it or if
257 	 * the process is being traced.
258 	 */
259 	if ((((attr.va_mode & VSUID) && p->p_ucred->cr_uid != attr.va_uid) ||
260 	     ((attr.va_mode & VSGID) && p->p_ucred->cr_gid != attr.va_gid)) &&
261 	    (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 &&
262 	    (p->p_flag & P_TRACED) == 0) {
263 		/*
264 		 * Turn off syscall tracing for set-id programs, except for
265 		 * root.
266 		 */
267 		if (p->p_tracep && suser(p)) {
268 			p->p_traceflag = 0;
269 			vrele(p->p_tracep);
270 			p->p_tracep = NULL;
271 		}
272 		/*
273 		 * Set the new credentials.
274 		 */
275 		p->p_ucred = crcopy(p->p_ucred);
276 		if (attr.va_mode & VSUID)
277 			p->p_ucred->cr_uid = attr.va_uid;
278 		if (attr.va_mode & VSGID)
279 			p->p_ucred->cr_gid = attr.va_gid;
280 		setsugid(p);
281 	} else {
282 		if (p->p_ucred->cr_uid == p->p_cred->p_ruid &&
283 		    p->p_ucred->cr_gid == p->p_cred->p_rgid)
284 			p->p_flag &= ~P_SUGID;
285 	}
286 
287 	/*
288 	 * Implement correct POSIX saved-id behavior.
289 	 */
290 	p->p_cred->p_svuid = p->p_ucred->cr_uid;
291 	p->p_cred->p_svgid = p->p_ucred->cr_gid;
292 
293 	/*
294 	 * Store the vp for use in procfs
295 	 */
296 	if (p->p_textvp)		/* release old reference */
297 		vrele(p->p_textvp);
298 	VREF(ndp->ni_vp);
299 	p->p_textvp = ndp->ni_vp;
300 
301 	/*
302 	 * If tracing the process, trap to debugger so breakpoints
303 	 * 	can be set before the program executes.
304 	 */
305 	STOPEVENT(p, S_EXEC, 0);
306 
307 	if (p->p_flag & P_TRACED)
308 		psignal(p, SIGTRAP);
309 
310 	/* clear "fork but no exec" flag, as we _are_ execing */
311 	p->p_acflag &= ~AFORK;
312 
313 	/* Set values passed into the program in registers. */
314 	setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base,
315 	    imgp->ps_strings);
316 
317 exec_fail_dealloc:
318 
319 	/*
320 	 * free various allocated resources
321 	 */
322 	if (imgp->firstpage)
323 		exec_unmap_first_page(imgp);
324 
325 	if (imgp->stringbase != NULL)
326 		kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase,
327 			ARG_MAX + PAGE_SIZE);
328 
329 	if (imgp->vp) {
330 		vrele(imgp->vp);
331 		zfree(namei_zone, ndp->ni_cnd.cn_pnbuf);
332 	}
333 
334 	if (error == 0)
335 		return (0);
336 
337 exec_fail:
338 	if (imgp->vmspace_destroyed) {
339 		/* sorry, no more process anymore. exit gracefully */
340 		exit1(p, W_EXITCODE(0, SIGABRT));
341 		/* NOT REACHED */
342 		return(0);
343 	} else {
344 		return(error);
345 	}
346 }
347 
348 int
349 exec_map_first_page(imgp)
350 	struct image_params *imgp;
351 {
352 	int s, rv, i;
353 	int initial_pagein;
354 	vm_page_t ma[VM_INITIAL_PAGEIN];
355 	vm_object_t object;
356 
357 
358 	if (imgp->firstpage) {
359 		exec_unmap_first_page(imgp);
360 	}
361 
362 	object = imgp->vp->v_object;
363 	s = splvm();
364 
365 	ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY);
366 
367 	if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) {
368 		initial_pagein = VM_INITIAL_PAGEIN;
369 		if (initial_pagein > object->size)
370 			initial_pagein = object->size;
371 		for (i = 1; i < initial_pagein; i++) {
372 			if ((ma[i] = vm_page_lookup(object, i)) != NULL) {
373 				if ((ma[i]->flags & PG_BUSY) || ma[i]->busy)
374 					break;
375 				if (ma[i]->valid)
376 					break;
377 				vm_page_busy(ma[i]);
378 			} else {
379 				ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL);
380 				if (ma[i] == NULL)
381 					break;
382 			}
383 		}
384 		initial_pagein = i;
385 
386 		rv = vm_pager_get_pages(object, ma, initial_pagein, 0);
387 		ma[0] = vm_page_lookup(object, 0);
388 
389 		if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) {
390 			if (ma[0]) {
391 				vm_page_protect(ma[0], VM_PROT_NONE);
392 				vm_page_free(ma[0]);
393 			}
394 			splx(s);
395 			return EIO;
396 		}
397 	}
398 
399 	vm_page_wire(ma[0]);
400 	vm_page_wakeup(ma[0]);
401 	splx(s);
402 
403 	pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0]));
404 	imgp->firstpage = ma[0];
405 
406 	return 0;
407 }
408 
409 void
410 exec_unmap_first_page(imgp)
411 	struct image_params *imgp;
412 {
413 	if (imgp->firstpage) {
414 		pmap_kremove((vm_offset_t) imgp->image_header);
415 		vm_page_unwire(imgp->firstpage, 1);
416 		imgp->firstpage = NULL;
417 	}
418 }
419 
420 /*
421  * Destroy old address space, and allocate a new stack
422  *	The new stack is only SGROWSIZ large because it is grown
423  *	automatically in trap.c.
424  */
425 int
426 exec_new_vmspace(imgp)
427 	struct image_params *imgp;
428 {
429 	int error;
430 	struct vmspace *vmspace = imgp->proc->p_vmspace;
431 	caddr_t	stack_addr = (caddr_t) (USRSTACK - MAXSSIZ);
432 	vm_map_t map = &vmspace->vm_map;
433 
434 	imgp->vmspace_destroyed = 1;
435 
436 	/*
437 	 * Blow away entire process VM, if address space not shared,
438 	 * otherwise, create a new VM space so that other threads are
439 	 * not disrupted
440 	 */
441 	if (vmspace->vm_refcnt == 1) {
442 		if (vmspace->vm_shm)
443 			shmexit(imgp->proc);
444 		pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS);
445 		vm_map_remove(map, 0, VM_MAXUSER_ADDRESS);
446 	} else {
447 		vmspace_exec(imgp->proc);
448 		vmspace = imgp->proc->p_vmspace;
449 		map = &vmspace->vm_map;
450 	}
451 
452 	/* Allocate a new stack */
453 	error = vm_map_stack (&vmspace->vm_map, (vm_offset_t)stack_addr,
454 			      (vm_size_t)MAXSSIZ, VM_PROT_ALL, VM_PROT_ALL, 0);
455 	if (error)
456 		return (error);
457 
458 	/* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the
459 	 * VM_STACK case, but they are still used to monitor the size of the
460 	 * process stack so we can check the stack rlimit.
461 	 */
462 	vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT;
463 	vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ;
464 
465 	return(0);
466 }
467 
468 /*
469  * Copy out argument and environment strings from the old process
470  *	address space into the temporary string buffer.
471  */
472 int
473 exec_extract_strings(imgp)
474 	struct image_params *imgp;
475 {
476 	char	**argv, **envv;
477 	char	*argp, *envp;
478 	int	error;
479 	size_t	length;
480 
481 	/*
482 	 * extract arguments first
483 	 */
484 
485 	argv = imgp->uap->argv;
486 
487 	if (argv) {
488 		argp = (caddr_t) (intptr_t) fuword(argv);
489 		if (argp == (caddr_t) -1)
490 			return (EFAULT);
491 		if (argp)
492 			argv++;
493 		if (imgp->argv0)
494 			argp = imgp->argv0;
495 		if (argp) {
496 			do {
497 				if (argp == (caddr_t) -1)
498 					return (EFAULT);
499 				if ((error = copyinstr(argp, imgp->stringp,
500 				    imgp->stringspace, &length))) {
501 					if (error == ENAMETOOLONG)
502 						return(E2BIG);
503 					return (error);
504 				}
505 				imgp->stringspace -= length;
506 				imgp->stringp += length;
507 				imgp->argc++;
508 			} while ((argp = (caddr_t) (intptr_t) fuword(argv++)));
509 		}
510 	}
511 
512 	/*
513 	 * extract environment strings
514 	 */
515 
516 	envv = imgp->uap->envv;
517 
518 	if (envv) {
519 		while ((envp = (caddr_t) (intptr_t) fuword(envv++))) {
520 			if (envp == (caddr_t) -1)
521 				return (EFAULT);
522 			if ((error = copyinstr(envp, imgp->stringp,
523 			    imgp->stringspace, &length))) {
524 				if (error == ENAMETOOLONG)
525 					return(E2BIG);
526 				return (error);
527 			}
528 			imgp->stringspace -= length;
529 			imgp->stringp += length;
530 			imgp->envc++;
531 		}
532 	}
533 
534 	return (0);
535 }
536 
537 /*
538  * Copy strings out to the new process address space, constructing
539  *	new arg and env vector tables. Return a pointer to the base
540  *	so that it can be used as the initial stack pointer.
541  */
542 long *
543 exec_copyout_strings(imgp)
544 	struct image_params *imgp;
545 {
546 	int argc, envc;
547 	char **vectp;
548 	char *stringp, *destp;
549 	long *stack_base;
550 	struct ps_strings *arginfo;
551 	int szsigcode;
552 
553 	/*
554 	 * Calculate string base and vector table pointers.
555 	 * Also deal with signal trampoline code for this exec type.
556 	 */
557 	arginfo = (struct ps_strings *)PS_STRINGS;
558 	szsigcode = *(imgp->proc->p_sysent->sv_szsigcode);
559 	destp =	(caddr_t)arginfo - szsigcode - SPARE_USRSPACE -
560 		roundup((ARG_MAX - imgp->stringspace), sizeof(char *));
561 
562 	/*
563 	 * install sigcode
564 	 */
565 	if (szsigcode)
566 		copyout(imgp->proc->p_sysent->sv_sigcode,
567 			((caddr_t)arginfo - szsigcode), szsigcode);
568 
569 	/*
570 	 * If we have a valid auxargs ptr, prepare some room
571 	 * on the stack.
572 	 */
573 	if (imgp->auxargs)
574 	/*
575 	 * The '+ 2' is for the null pointers at the end of each of the
576 	 * arg and env vector sets, and 'AT_COUNT*2' is room for the
577 	 * ELF Auxargs data.
578 	 */
579 		vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 +
580 				  AT_COUNT*2) * sizeof(char*));
581 	else
582 	/*
583 	 * The '+ 2' is for the null pointers at the end of each of the
584 	 * arg and env vector sets
585 	 */
586 		vectp = (char **)
587 			(destp - (imgp->argc + imgp->envc + 2) * sizeof(char*));
588 
589 	/*
590 	 * vectp also becomes our initial stack base
591 	 */
592 	stack_base = (long *)vectp;
593 
594 	stringp = imgp->stringbase;
595 	argc = imgp->argc;
596 	envc = imgp->envc;
597 
598 	/*
599 	 * Copy out strings - arguments and environment.
600 	 */
601 	copyout(stringp, destp, ARG_MAX - imgp->stringspace);
602 
603 	/*
604 	 * Fill in "ps_strings" struct for ps, w, etc.
605 	 */
606 	suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp);
607 	suword(&arginfo->ps_nargvstr, argc);
608 
609 	/*
610 	 * Fill in argument portion of vector table.
611 	 */
612 	for (; argc > 0; --argc) {
613 		suword(vectp++, (long)(intptr_t)destp);
614 		while (*stringp++ != 0)
615 			destp++;
616 		destp++;
617 	}
618 
619 	/* a null vector table pointer seperates the argp's from the envp's */
620 	suword(vectp++, 0);
621 
622 	suword(&arginfo->ps_envstr, (long)(intptr_t)vectp);
623 	suword(&arginfo->ps_nenvstr, envc);
624 
625 	/*
626 	 * Fill in environment portion of vector table.
627 	 */
628 	for (; envc > 0; --envc) {
629 		suword(vectp++, (long)(intptr_t)destp);
630 		while (*stringp++ != 0)
631 			destp++;
632 		destp++;
633 	}
634 
635 	/* end of vector table is a null pointer */
636 	suword(vectp, 0);
637 
638 	return (stack_base);
639 }
640 
641 /*
642  * Check permissions of file to execute.
643  *	Return 0 for success or error code on failure.
644  */
645 int
646 exec_check_permissions(imgp)
647 	struct image_params *imgp;
648 {
649 	struct proc *p = imgp->proc;
650 	struct vnode *vp = imgp->vp;
651 	struct vattr *attr = imgp->attr;
652 	int error;
653 
654 	/* Get file attributes */
655 	error = VOP_GETATTR(vp, attr, p->p_ucred, p);
656 	if (error)
657 		return (error);
658 
659 	/*
660 	 * 1) Check if file execution is disabled for the filesystem that this
661 	 *	file resides on.
662 	 * 2) Insure that at least one execute bit is on - otherwise root
663 	 *	will always succeed, and we don't want to happen unless the
664 	 *	file really is executable.
665 	 * 3) Insure that the file is a regular file.
666 	 */
667 	if ((vp->v_mount->mnt_flag & MNT_NOEXEC) ||
668 	    ((attr->va_mode & 0111) == 0) ||
669 	    (attr->va_type != VREG)) {
670 		return (EACCES);
671 	}
672 
673 	/*
674 	 * Zero length files can't be exec'd
675 	 */
676 	if (attr->va_size == 0)
677 		return (ENOEXEC);
678 
679 	/*
680 	 *  Check for execute permission to file based on current credentials.
681 	 */
682 	error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p);
683 	if (error)
684 		return (error);
685 
686 	/*
687 	 * Check number of open-for-writes on the file and deny execution
688 	 * if there are any.
689 	 */
690 	if (vp->v_writecount)
691 		return (ETXTBSY);
692 
693 	/*
694 	 * Call filesystem specific open routine (which does nothing in the
695 	 * general case).
696 	 */
697 	error = VOP_OPEN(vp, FREAD, p->p_ucred, p);
698 	if (error)
699 		return (error);
700 
701 	return (0);
702 }
703 
704 /*
705  * Exec handler registration
706  */
707 int
708 exec_register(execsw_arg)
709 	const struct execsw *execsw_arg;
710 {
711 	const struct execsw **es, **xs, **newexecsw;
712 	int count = 2;	/* New slot and trailing NULL */
713 
714 	if (execsw)
715 		for (es = execsw; *es; es++)
716 			count++;
717 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
718 	if (newexecsw == NULL)
719 		return ENOMEM;
720 	xs = newexecsw;
721 	if (execsw)
722 		for (es = execsw; *es; es++)
723 			*xs++ = *es;
724 	*xs++ = execsw_arg;
725 	*xs = NULL;
726 	if (execsw)
727 		free(execsw, M_TEMP);
728 	execsw = newexecsw;
729 	return 0;
730 }
731 
732 int
733 exec_unregister(execsw_arg)
734 	const struct execsw *execsw_arg;
735 {
736 	const struct execsw **es, **xs, **newexecsw;
737 	int count = 1;
738 
739 	if (execsw == NULL)
740 		panic("unregister with no handlers left?\n");
741 
742 	for (es = execsw; *es; es++) {
743 		if (*es == execsw_arg)
744 			break;
745 	}
746 	if (*es == NULL)
747 		return ENOENT;
748 	for (es = execsw; *es; es++)
749 		if (*es != execsw_arg)
750 			count++;
751 	newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK);
752 	if (newexecsw == NULL)
753 		return ENOMEM;
754 	xs = newexecsw;
755 	for (es = execsw; *es; es++)
756 		if (*es != execsw_arg)
757 			*xs++ = *es;
758 	*xs = NULL;
759 	if (execsw)
760 		free(execsw, M_TEMP);
761 	execsw = newexecsw;
762 	return 0;
763 }
764