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