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