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