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