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