126f9a767SRodney W. Grimes /* 226f9a767SRodney W. Grimes * Copyright (c) 1993, David Greenman 326f9a767SRodney W. Grimes * All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 6df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 7df8bae1dSRodney W. Grimes * are met: 8df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 9df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 10df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 11df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 12df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 13df8bae1dSRodney W. Grimes * 1426f9a767SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1726f9a767SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24df8bae1dSRodney W. Grimes * SUCH DAMAGE. 25df8bae1dSRodney W. Grimes * 26c3aac50fSPeter Wemm * $FreeBSD$ 27df8bae1dSRodney W. Grimes */ 28df8bae1dSRodney W. Grimes 29df8bae1dSRodney W. Grimes #include <sys/param.h> 3026f9a767SRodney W. Grimes #include <sys/systm.h> 31d2d3e875SBruce Evans #include <sys/sysproto.h> 3226f9a767SRodney W. Grimes #include <sys/signalvar.h> 3326f9a767SRodney W. Grimes #include <sys/kernel.h> 3426f9a767SRodney W. Grimes #include <sys/mount.h> 35797f2d22SPoul-Henning Kamp #include <sys/filedesc.h> 36079cc25bSDavid Greenman #include <sys/fcntl.h> 3726f9a767SRodney W. Grimes #include <sys/acct.h> 3826f9a767SRodney W. Grimes #include <sys/exec.h> 3926f9a767SRodney W. Grimes #include <sys/imgact.h> 40e1743d02SSøren Schmidt #include <sys/imgact_elf.h> 4126f9a767SRodney W. Grimes #include <sys/wait.h> 42a794e791SBruce Evans #include <sys/proc.h> 432a024a2bSSean Eric Fagan #include <sys/pioctl.h> 44aa855a59SPeter Wemm #include <sys/malloc.h> 45a794e791SBruce Evans #include <sys/namei.h> 461e1e0b44SSøren Schmidt #include <sys/sysent.h> 47797f2d22SPoul-Henning Kamp #include <sys/shm.h> 4899ac3bc8SPeter Wemm #include <sys/sysctl.h> 49a794e791SBruce Evans #include <sys/vnode.h> 509caaadb6SDavid Greenman #include <sys/buf.h> 5126f9a767SRodney W. Grimes 5226f9a767SRodney W. Grimes #include <vm/vm.h> 53efeaf95aSDavid Greenman #include <vm/vm_param.h> 54996c772fSJohn Dyson #include <sys/lock.h> 55efeaf95aSDavid Greenman #include <vm/pmap.h> 561616db3cSJohn Dyson #include <vm/vm_page.h> 57efeaf95aSDavid Greenman #include <vm/vm_map.h> 5826f9a767SRodney W. Grimes #include <vm/vm_kern.h> 59efeaf95aSDavid Greenman #include <vm/vm_extern.h> 601ebd0c59SDavid Greenman #include <vm/vm_object.h> 61675ea6f0SBruce Evans #include <vm/vm_zone.h> 621616db3cSJohn Dyson #include <vm/vm_pager.h> 6326f9a767SRodney W. Grimes 6426f9a767SRodney W. Grimes #include <machine/reg.h> 6526f9a767SRodney W. Grimes 66b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments"); 67b9df5231SPoul-Henning Kamp 68ecbb00a2SDoug Rabson static long *exec_copyout_strings __P((struct image_params *)); 69df8bae1dSRodney W. Grimes 70486bddb0SDoug Rabson static long ps_strings = PS_STRINGS; 71486bddb0SDoug Rabson SYSCTL_LONG(_kern, KERN_PS_STRINGS, ps_strings, CTLFLAG_RD, &ps_strings, ""); 72486bddb0SDoug Rabson 73486bddb0SDoug Rabson static long usrstack = USRSTACK; 74486bddb0SDoug Rabson SYSCTL_LONG(_kern, KERN_USRSTACK, usrstack, CTLFLAG_RD, &usrstack, ""); 7599ac3bc8SPeter Wemm 76b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16; 77b9df5231SPoul-Henning Kamp SYSCTL_LONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 78b9df5231SPoul-Henning Kamp &ps_arg_cache_limit, ""); 79b9df5231SPoul-Henning Kamp 8099ac3bc8SPeter Wemm /* 81aa855a59SPeter Wemm * Each of the items is a pointer to a `const struct execsw', hence the 82aa855a59SPeter Wemm * double pointer here. 83df8bae1dSRodney W. Grimes */ 84aa855a59SPeter Wemm static const struct execsw **execsw; 8526f9a767SRodney W. Grimes 86d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 87ad7507e2SSteven Wallace struct execve_args { 88ad7507e2SSteven Wallace char *fname; 89ad7507e2SSteven Wallace char **argv; 90ad7507e2SSteven Wallace char **envv; 91ad7507e2SSteven Wallace }; 92d2d3e875SBruce Evans #endif 93ad7507e2SSteven Wallace 9426f9a767SRodney W. Grimes /* 9526f9a767SRodney W. Grimes * execve() system call. 9626f9a767SRodney W. Grimes */ 9726f9a767SRodney W. Grimes int 98cb226aaaSPoul-Henning Kamp execve(p, uap) 9926f9a767SRodney W. Grimes struct proc *p; 10026f9a767SRodney W. Grimes register struct execve_args *uap; 101df8bae1dSRodney W. Grimes { 10226f9a767SRodney W. Grimes struct nameidata nd, *ndp; 103ecbb00a2SDoug Rabson long *stack_base; 104bb56ec4aSPoul-Henning Kamp int error, len, i; 105c52007c2SDavid Greenman struct image_params image_params, *imgp; 10626f9a767SRodney W. Grimes struct vattr attr; 10726f9a767SRodney W. Grimes 108c52007c2SDavid Greenman imgp = &image_params; 109df8bae1dSRodney W. Grimes 110df8bae1dSRodney W. Grimes /* 111c52007c2SDavid Greenman * Initialize part of the common data 112df8bae1dSRodney W. Grimes */ 113c52007c2SDavid Greenman imgp->proc = p; 114c52007c2SDavid Greenman imgp->uap = uap; 115c52007c2SDavid Greenman imgp->attr = &attr; 116c52007c2SDavid Greenman imgp->argc = imgp->envc = 0; 1175cf3d12cSAndrey A. Chernov imgp->argv0 = NULL; 118c52007c2SDavid Greenman imgp->entry_addr = 0; 119c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 120c52007c2SDavid Greenman imgp->interpreted = 0; 121c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 122e1743d02SSøren Schmidt imgp->auxargs = NULL; 1231616db3cSJohn Dyson imgp->vp = NULL; 1241616db3cSJohn Dyson imgp->firstpage = NULL; 1254fe88fe6SJohn Polstra imgp->ps_strings = 0; 12626f9a767SRodney W. Grimes 12726f9a767SRodney W. Grimes /* 12826f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 12926f9a767SRodney W. Grimes * environment strings 13026f9a767SRodney W. Grimes */ 1311616db3cSJohn Dyson imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + PAGE_SIZE); 132c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 13326f9a767SRodney W. Grimes error = ENOMEM; 13426f9a767SRodney W. Grimes goto exec_fail; 13526f9a767SRodney W. Grimes } 136c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 137c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 1381616db3cSJohn Dyson imgp->image_header = imgp->stringbase + ARG_MAX; 13926f9a767SRodney W. Grimes 14026f9a767SRodney W. Grimes /* 14126f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 14226f9a767SRodney W. Grimes * in ni_vp amoung other things. 14326f9a767SRodney W. Grimes */ 14426f9a767SRodney W. Grimes ndp = &nd; 145b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 1463ed8a403SDavid Greenman UIO_USERSPACE, uap->fname, p); 14726f9a767SRodney W. Grimes 14826f9a767SRodney W. Grimes interpret: 14926f9a767SRodney W. Grimes 15026f9a767SRodney W. Grimes error = namei(ndp); 15126f9a767SRodney W. Grimes if (error) { 1521616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 1531616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 15426f9a767SRodney W. Grimes goto exec_fail; 15526f9a767SRodney W. Grimes } 15626f9a767SRodney W. Grimes 157c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 1589c0fed3dSDoug Rabson imgp->fname = uap->fname; 15926f9a767SRodney W. Grimes 16026f9a767SRodney W. Grimes /* 1619022e4adSDavid Greenman * Check file permissions (also 'opens' file) 1629022e4adSDavid Greenman */ 163c52007c2SDavid Greenman error = exec_check_permissions(imgp); 1648677f509SDavid Greenman if (error) { 1658677f509SDavid Greenman VOP_UNLOCK(imgp->vp, 0, p); 16626f9a767SRodney W. Grimes goto exec_fail_dealloc; 1678677f509SDavid Greenman } 16826f9a767SRodney W. Grimes 1691616db3cSJohn Dyson error = exec_map_first_page(imgp); 1709caaadb6SDavid Greenman VOP_UNLOCK(imgp->vp, 0, p); 1716d5a0a8cSDavid Greenman if (error) 17226f9a767SRodney W. Grimes goto exec_fail_dealloc; 17326f9a767SRodney W. Grimes 17426f9a767SRodney W. Grimes /* 17526f9a767SRodney W. Grimes * Loop through list of image activators, calling each one. 17626f9a767SRodney W. Grimes * If there is no match, the activator returns -1. If there 17726f9a767SRodney W. Grimes * is a match, but there was an error during the activation, 17826f9a767SRodney W. Grimes * the error is returned. Otherwise 0 means success. If the 17926f9a767SRodney W. Grimes * image is interpreted, loop back up and try activating 18026f9a767SRodney W. Grimes * the interpreter. 18126f9a767SRodney W. Grimes */ 18226f9a767SRodney W. Grimes for (i = 0; execsw[i]; ++i) { 18326f9a767SRodney W. Grimes if (execsw[i]->ex_imgact) 184c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 18526f9a767SRodney W. Grimes else 18626f9a767SRodney W. Grimes continue; 18726f9a767SRodney W. Grimes if (error == -1) 18826f9a767SRodney W. Grimes continue; 18926f9a767SRodney W. Grimes if (error) 19026f9a767SRodney W. Grimes goto exec_fail_dealloc; 191c52007c2SDavid Greenman if (imgp->interpreted) { 1921616db3cSJohn Dyson exec_unmap_first_page(imgp); 19326f9a767SRodney W. Grimes /* free old vnode and name buffer */ 194f5277ae7SDavid Greenman vrele(ndp->ni_vp); 19599448ed1SJohn Dyson zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 19626f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 197b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 198c52007c2SDavid Greenman UIO_SYSSPACE, imgp->interpreter_name, p); 19926f9a767SRodney W. Grimes goto interpret; 20026f9a767SRodney W. Grimes } 20126f9a767SRodney W. Grimes break; 20226f9a767SRodney W. Grimes } 20326f9a767SRodney W. Grimes /* If we made it through all the activators and none matched, exit. */ 20426f9a767SRodney W. Grimes if (error == -1) { 20526f9a767SRodney W. Grimes error = ENOEXEC; 20626f9a767SRodney W. Grimes goto exec_fail_dealloc; 20726f9a767SRodney W. Grimes } 20826f9a767SRodney W. Grimes 20926f9a767SRodney W. Grimes /* 21026f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 21126f9a767SRodney W. Grimes */ 212c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 21326f9a767SRodney W. Grimes p->p_vmspace->vm_minsaddr = (char *)stack_base; 21426f9a767SRodney W. Grimes 21526f9a767SRodney W. Grimes /* 2161e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 2171e1e0b44SSøren Schmidt * let it do the stack setup. 2181e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 21926f9a767SRodney W. Grimes */ 2201e1e0b44SSøren Schmidt if (p->p_sysent->sv_fixup) 221c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 2221e1e0b44SSøren Schmidt else 223c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 22426f9a767SRodney W. Grimes 225a78e8d2aSDavid Greenman /* 226a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 227a78e8d2aSDavid Greenman * be shared after an exec. 228a78e8d2aSDavid Greenman */ 229a78e8d2aSDavid Greenman if (p->p_fd->fd_refcnt > 1) { 230a78e8d2aSDavid Greenman struct filedesc *tmp; 231a78e8d2aSDavid Greenman 232a78e8d2aSDavid Greenman tmp = fdcopy(p); 233a78e8d2aSDavid Greenman fdfree(p); 234a78e8d2aSDavid Greenman p->p_fd = tmp; 235a78e8d2aSDavid Greenman } 236a78e8d2aSDavid Greenman 237fdf4e8b3SWarner Losh /* Stop profiling */ 238fdf4e8b3SWarner Losh stopprofclock(p); 239fdf4e8b3SWarner Losh 24026f9a767SRodney W. Grimes /* close files on exec */ 24126f9a767SRodney W. Grimes fdcloseexec(p); 24226f9a767SRodney W. Grimes 24326f9a767SRodney W. Grimes /* reset caught signals */ 24426f9a767SRodney W. Grimes execsigs(p); 24526f9a767SRodney W. Grimes 24626f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 24726f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 24826f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 24926f9a767SRodney W. Grimes p->p_comm[len] = 0; 25026f9a767SRodney W. Grimes 25126f9a767SRodney W. Grimes /* 25224b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 253dc733423SDag-Erling Smørgrav * it that it now has its own resources back 25426f9a767SRodney W. Grimes */ 25526f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 25626f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 25726f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 25826f9a767SRodney W. Grimes wakeup((caddr_t)p->p_pptr); 25926f9a767SRodney W. Grimes } 26026f9a767SRodney W. Grimes 26126f9a767SRodney W. Grimes /* 262a78e8d2aSDavid Greenman * Implement image setuid/setgid. 263a78e8d2aSDavid Greenman * 264a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 265a78e8d2aSDavid Greenman * the process is being traced. 266c52007c2SDavid Greenman */ 2678aef1712SMatthew Dillon if ((((attr.va_mode & VSUID) && p->p_ucred->cr_uid != attr.va_uid) || 2688aef1712SMatthew Dillon ((attr.va_mode & VSGID) && p->p_ucred->cr_gid != attr.va_gid)) && 269a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 270c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 271c52007c2SDavid Greenman /* 272c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 27326f9a767SRodney W. Grimes * root. 27426f9a767SRodney W. Grimes */ 275f711d546SPoul-Henning Kamp if (p->p_tracep && suser(p)) { 27626f9a767SRodney W. Grimes p->p_traceflag = 0; 27726f9a767SRodney W. Grimes vrele(p->p_tracep); 278c52007c2SDavid Greenman p->p_tracep = NULL; 27926f9a767SRodney W. Grimes } 280c52007c2SDavid Greenman /* 281c52007c2SDavid Greenman * Set the new credentials. 282c52007c2SDavid Greenman */ 28326f9a767SRodney W. Grimes p->p_ucred = crcopy(p->p_ucred); 284c52007c2SDavid Greenman if (attr.va_mode & VSUID) 28526f9a767SRodney W. Grimes p->p_ucred->cr_uid = attr.va_uid; 286c52007c2SDavid Greenman if (attr.va_mode & VSGID) 287d021ae3dSGuido van Rooij p->p_ucred->cr_gid = attr.va_gid; 288d5f81602SSean Eric Fagan setsugid(p); 289c52007c2SDavid Greenman } else { 290e47bda07SDavid Greenman if (p->p_ucred->cr_uid == p->p_cred->p_ruid && 291e47bda07SDavid Greenman p->p_ucred->cr_gid == p->p_cred->p_rgid) 292c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 29326f9a767SRodney W. Grimes } 29426f9a767SRodney W. Grimes 29526f9a767SRodney W. Grimes /* 296c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 29726f9a767SRodney W. Grimes */ 29826f9a767SRodney W. Grimes p->p_cred->p_svuid = p->p_ucred->cr_uid; 29926f9a767SRodney W. Grimes p->p_cred->p_svgid = p->p_ucred->cr_gid; 30026f9a767SRodney W. Grimes 30126f9a767SRodney W. Grimes /* 3022a531c80SDavid Greenman * Store the vp for use in procfs 3032a531c80SDavid Greenman */ 3042a531c80SDavid Greenman if (p->p_textvp) /* release old reference */ 3052a531c80SDavid Greenman vrele(p->p_textvp); 3062a531c80SDavid Greenman VREF(ndp->ni_vp); 3072a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 3082a531c80SDavid Greenman 3092a531c80SDavid Greenman /* 31026f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 31126f9a767SRodney W. Grimes * can be set before the program executes. 31226f9a767SRodney W. Grimes */ 3132a024a2bSSean Eric Fagan STOPEVENT(p, S_EXEC, 0); 3142a024a2bSSean Eric Fagan 31526f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 31626f9a767SRodney W. Grimes psignal(p, SIGTRAP); 31726f9a767SRodney W. Grimes 31826f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 31926f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 32026f9a767SRodney W. Grimes 3214fe88fe6SJohn Polstra /* Set values passed into the program in registers. */ 3224fe88fe6SJohn Polstra setregs(p, imgp->entry_addr, (u_long)(uintptr_t)stack_base, 3234fe88fe6SJohn Polstra imgp->ps_strings); 32426f9a767SRodney W. Grimes 325b9df5231SPoul-Henning Kamp /* Free any previous argument cache */ 326b9df5231SPoul-Henning Kamp if (p->p_args && --p->p_args->ar_ref == 0) 327b9df5231SPoul-Henning Kamp FREE(p->p_args, M_PARGS); 328b9df5231SPoul-Henning Kamp p->p_args = NULL; 329b9df5231SPoul-Henning Kamp 330b9df5231SPoul-Henning Kamp /* Cache arguments if they fit inside our allowance */ 331b9df5231SPoul-Henning Kamp i = imgp->endargs - imgp->stringbase; 332b9df5231SPoul-Henning Kamp if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 333b9df5231SPoul-Henning Kamp MALLOC(p->p_args, struct pargs *, sizeof(struct pargs) + i, 334b9df5231SPoul-Henning Kamp M_PARGS, M_WAITOK); 335b9df5231SPoul-Henning Kamp p->p_args->ar_ref = 1; 336b9df5231SPoul-Henning Kamp p->p_args->ar_length = i; 337b9df5231SPoul-Henning Kamp bcopy(imgp->stringbase, p->p_args->ar_args, i); 338b9df5231SPoul-Henning Kamp } 339b9df5231SPoul-Henning Kamp 3401616db3cSJohn Dyson exec_fail_dealloc: 3411616db3cSJohn Dyson 34226f9a767SRodney W. Grimes /* 34326f9a767SRodney W. Grimes * free various allocated resources 34426f9a767SRodney W. Grimes */ 3451616db3cSJohn Dyson if (imgp->firstpage) 3461616db3cSJohn Dyson exec_unmap_first_page(imgp); 34726f9a767SRodney W. Grimes 348c2f9f36bSDavid Greenman if (imgp->stringbase != NULL) 3491616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 3501616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 3511616db3cSJohn Dyson 3529c0fed3dSDoug Rabson if (imgp->vp) { 3539c0fed3dSDoug Rabson vrele(imgp->vp); 35499448ed1SJohn Dyson zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 355a3cf6ebaSDavid Greenman } 35626f9a767SRodney W. Grimes 3571616db3cSJohn Dyson if (error == 0) 3581616db3cSJohn Dyson return (0); 3591616db3cSJohn Dyson 36026f9a767SRodney W. Grimes exec_fail: 361c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 36226f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 36326f9a767SRodney W. Grimes exit1(p, W_EXITCODE(0, SIGABRT)); 36426f9a767SRodney W. Grimes /* NOT REACHED */ 36526f9a767SRodney W. Grimes return(0); 36626f9a767SRodney W. Grimes } else { 36726f9a767SRodney W. Grimes return(error); 36826f9a767SRodney W. Grimes } 36926f9a767SRodney W. Grimes } 37026f9a767SRodney W. Grimes 3711616db3cSJohn Dyson int 3721616db3cSJohn Dyson exec_map_first_page(imgp) 3731616db3cSJohn Dyson struct image_params *imgp; 3741616db3cSJohn Dyson { 37595461b45SJohn Dyson int s, rv, i; 37695461b45SJohn Dyson int initial_pagein; 37795461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 3781616db3cSJohn Dyson vm_object_t object; 3791616db3cSJohn Dyson 3801616db3cSJohn Dyson 3811616db3cSJohn Dyson if (imgp->firstpage) { 3821616db3cSJohn Dyson exec_unmap_first_page(imgp); 3831616db3cSJohn Dyson } 3841616db3cSJohn Dyson 3851616db3cSJohn Dyson object = imgp->vp->v_object; 3861616db3cSJohn Dyson s = splvm(); 3871616db3cSJohn Dyson 38895461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 3891616db3cSJohn Dyson 39095461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 39195461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 39295461b45SJohn Dyson if (initial_pagein > object->size) 39395461b45SJohn Dyson initial_pagein = object->size; 39495461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 395d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 39695461b45SJohn Dyson if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) 39795461b45SJohn Dyson break; 39895461b45SJohn Dyson if (ma[i]->valid) 39995461b45SJohn Dyson break; 400e69763a3SDoug Rabson vm_page_busy(ma[i]); 40195461b45SJohn Dyson } else { 40295461b45SJohn Dyson ma[i] = vm_page_alloc(object, i, VM_ALLOC_NORMAL); 40395461b45SJohn Dyson if (ma[i] == NULL) 40495461b45SJohn Dyson break; 40595461b45SJohn Dyson } 40695461b45SJohn Dyson } 40795461b45SJohn Dyson initial_pagein = i; 4081616db3cSJohn Dyson 40995461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 41095461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 41195461b45SJohn Dyson 412eed2412eSJohn Dyson if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || (ma[0]->valid == 0)) { 413ecbb00a2SDoug Rabson if (ma[0]) { 41495461b45SJohn Dyson vm_page_protect(ma[0], VM_PROT_NONE); 4158f9110f6SJohn Dyson vm_page_free(ma[0]); 416ecbb00a2SDoug Rabson } 4171616db3cSJohn Dyson splx(s); 4181616db3cSJohn Dyson return EIO; 4191616db3cSJohn Dyson } 4201616db3cSJohn Dyson } 4211616db3cSJohn Dyson 42295461b45SJohn Dyson vm_page_wire(ma[0]); 423e69763a3SDoug Rabson vm_page_wakeup(ma[0]); 4241616db3cSJohn Dyson splx(s); 4251616db3cSJohn Dyson 42695461b45SJohn Dyson pmap_kenter((vm_offset_t) imgp->image_header, VM_PAGE_TO_PHYS(ma[0])); 42795461b45SJohn Dyson imgp->firstpage = ma[0]; 4281616db3cSJohn Dyson 4291616db3cSJohn Dyson return 0; 4301616db3cSJohn Dyson } 4311616db3cSJohn Dyson 4321616db3cSJohn Dyson void 4331616db3cSJohn Dyson exec_unmap_first_page(imgp) 4341616db3cSJohn Dyson struct image_params *imgp; 4351616db3cSJohn Dyson { 4361616db3cSJohn Dyson if (imgp->firstpage) { 4371616db3cSJohn Dyson pmap_kremove((vm_offset_t) imgp->image_header); 43873007561SDavid Greenman vm_page_unwire(imgp->firstpage, 1); 4391616db3cSJohn Dyson imgp->firstpage = NULL; 4401616db3cSJohn Dyson } 4411616db3cSJohn Dyson } 4421616db3cSJohn Dyson 44326f9a767SRodney W. Grimes /* 44426f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 44526f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 44626f9a767SRodney W. Grimes * automatically in trap.c. 44726f9a767SRodney W. Grimes */ 44826f9a767SRodney W. Grimes int 449c52007c2SDavid Greenman exec_new_vmspace(imgp) 450c52007c2SDavid Greenman struct image_params *imgp; 45126f9a767SRodney W. Grimes { 45226f9a767SRodney W. Grimes int error; 453c52007c2SDavid Greenman struct vmspace *vmspace = imgp->proc->p_vmspace; 4542267af78SJulian Elischer caddr_t stack_addr = (caddr_t) (USRSTACK - MAXSSIZ); 455af9ec885SJohn Dyson vm_map_t map = &vmspace->vm_map; 45626f9a767SRodney W. Grimes 457c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 45826f9a767SRodney W. Grimes 459af9ec885SJohn Dyson /* 460af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 461af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 462af9ec885SJohn Dyson * not disrupted 463af9ec885SJohn Dyson */ 464af9ec885SJohn Dyson if (vmspace->vm_refcnt == 1) { 4653d903220SDoug Rabson if (vmspace->vm_shm) 466c52007c2SDavid Greenman shmexit(imgp->proc); 467b1028ad1SLuoqi Chen pmap_remove_pages(vmspace_pmap(vmspace), 0, VM_MAXUSER_ADDRESS); 4689c0fed3dSDoug Rabson vm_map_remove(map, 0, VM_MAXUSER_ADDRESS); 469af9ec885SJohn Dyson } else { 470492da96cSJohn Dyson vmspace_exec(imgp->proc); 471492da96cSJohn Dyson vmspace = imgp->proc->p_vmspace; 472af9ec885SJohn Dyson map = &vmspace->vm_map; 473af9ec885SJohn Dyson } 47426f9a767SRodney W. Grimes 47526f9a767SRodney W. Grimes /* Allocate a new stack */ 4762267af78SJulian Elischer error = vm_map_stack (&vmspace->vm_map, (vm_offset_t)stack_addr, 4772267af78SJulian Elischer (vm_size_t)MAXSSIZ, VM_PROT_ALL, VM_PROT_ALL, 0); 4782267af78SJulian Elischer if (error) 4792267af78SJulian Elischer return (error); 4802267af78SJulian Elischer 4812267af78SJulian Elischer /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 4822267af78SJulian Elischer * VM_STACK case, but they are still used to monitor the size of the 4832267af78SJulian Elischer * process stack so we can check the stack rlimit. 4842267af78SJulian Elischer */ 4852267af78SJulian Elischer vmspace->vm_ssize = SGROWSIZ >> PAGE_SHIFT; 4862267af78SJulian Elischer vmspace->vm_maxsaddr = (char *)USRSTACK - MAXSSIZ; 48726f9a767SRodney W. Grimes 48826f9a767SRodney W. Grimes return(0); 48926f9a767SRodney W. Grimes } 49026f9a767SRodney W. Grimes 49126f9a767SRodney W. Grimes /* 49226f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 49326f9a767SRodney W. Grimes * address space into the temporary string buffer. 49426f9a767SRodney W. Grimes */ 49526f9a767SRodney W. Grimes int 496c52007c2SDavid Greenman exec_extract_strings(imgp) 497c52007c2SDavid Greenman struct image_params *imgp; 49826f9a767SRodney W. Grimes { 49926f9a767SRodney W. Grimes char **argv, **envv; 50026f9a767SRodney W. Grimes char *argp, *envp; 501ecbb00a2SDoug Rabson int error; 502ecbb00a2SDoug Rabson size_t length; 50326f9a767SRodney W. Grimes 50426f9a767SRodney W. Grimes /* 50526f9a767SRodney W. Grimes * extract arguments first 50626f9a767SRodney W. Grimes */ 50726f9a767SRodney W. Grimes 508c52007c2SDavid Greenman argv = imgp->uap->argv; 50926f9a767SRodney W. Grimes 5100fd1a014SDavid Greenman if (argv) { 511aae0aa45SBruce Evans argp = (caddr_t) (intptr_t) fuword(argv); 5125cf3d12cSAndrey A. Chernov if (argp == (caddr_t) -1) 5135cf3d12cSAndrey A. Chernov return (EFAULT); 5145cf3d12cSAndrey A. Chernov if (argp) 5155cf3d12cSAndrey A. Chernov argv++; 5165cf3d12cSAndrey A. Chernov if (imgp->argv0) 5175cf3d12cSAndrey A. Chernov argp = imgp->argv0; 5185cf3d12cSAndrey A. Chernov if (argp) { 5195cf3d12cSAndrey A. Chernov do { 52026f9a767SRodney W. Grimes if (argp == (caddr_t) -1) 52126f9a767SRodney W. Grimes return (EFAULT); 522c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 523c52007c2SDavid Greenman imgp->stringspace, &length))) { 5240fd1a014SDavid Greenman if (error == ENAMETOOLONG) 52526f9a767SRodney W. Grimes return(E2BIG); 5260fd1a014SDavid Greenman return (error); 5270fd1a014SDavid Greenman } 528c52007c2SDavid Greenman imgp->stringspace -= length; 529c52007c2SDavid Greenman imgp->stringp += length; 530c52007c2SDavid Greenman imgp->argc++; 531aae0aa45SBruce Evans } while ((argp = (caddr_t) (intptr_t) fuword(argv++))); 53226f9a767SRodney W. Grimes } 5330fd1a014SDavid Greenman } 53426f9a767SRodney W. Grimes 535b9df5231SPoul-Henning Kamp imgp->endargs = imgp->stringp; 536b9df5231SPoul-Henning Kamp 53726f9a767SRodney W. Grimes /* 53826f9a767SRodney W. Grimes * extract environment strings 53926f9a767SRodney W. Grimes */ 54026f9a767SRodney W. Grimes 541c52007c2SDavid Greenman envv = imgp->uap->envv; 54226f9a767SRodney W. Grimes 5430fd1a014SDavid Greenman if (envv) { 544aae0aa45SBruce Evans while ((envp = (caddr_t) (intptr_t) fuword(envv++))) { 54526f9a767SRodney W. Grimes if (envp == (caddr_t) -1) 54626f9a767SRodney W. Grimes return (EFAULT); 547c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 548c52007c2SDavid Greenman imgp->stringspace, &length))) { 5490fd1a014SDavid Greenman if (error == ENAMETOOLONG) 55026f9a767SRodney W. Grimes return(E2BIG); 5510fd1a014SDavid Greenman return (error); 5520fd1a014SDavid Greenman } 553c52007c2SDavid Greenman imgp->stringspace -= length; 554c52007c2SDavid Greenman imgp->stringp += length; 555c52007c2SDavid Greenman imgp->envc++; 55626f9a767SRodney W. Grimes } 5570fd1a014SDavid Greenman } 55826f9a767SRodney W. Grimes 55926f9a767SRodney W. Grimes return (0); 56026f9a767SRodney W. Grimes } 56126f9a767SRodney W. Grimes 56226f9a767SRodney W. Grimes /* 56326f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 56426f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 56526f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 56626f9a767SRodney W. Grimes */ 567ecbb00a2SDoug Rabson long * 568c52007c2SDavid Greenman exec_copyout_strings(imgp) 569c52007c2SDavid Greenman struct image_params *imgp; 57026f9a767SRodney W. Grimes { 57126f9a767SRodney W. Grimes int argc, envc; 57226f9a767SRodney W. Grimes char **vectp; 57326f9a767SRodney W. Grimes char *stringp, *destp; 574ecbb00a2SDoug Rabson long *stack_base; 57593f6448cSDavid Greenman struct ps_strings *arginfo; 576d66a5066SPeter Wemm int szsigcode; 57726f9a767SRodney W. Grimes 57826f9a767SRodney W. Grimes /* 57926f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 580d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 58126f9a767SRodney W. Grimes */ 5824c56fcdeSBruce Evans arginfo = (struct ps_strings *)PS_STRINGS; 583d66a5066SPeter Wemm szsigcode = *(imgp->proc->p_sysent->sv_szsigcode); 584d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 5851ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 5861ed012f9SPeter Wemm 58726f9a767SRodney W. Grimes /* 588d66a5066SPeter Wemm * install sigcode 589d66a5066SPeter Wemm */ 590d66a5066SPeter Wemm if (szsigcode) 591d66a5066SPeter Wemm copyout(imgp->proc->p_sysent->sv_sigcode, 592d66a5066SPeter Wemm ((caddr_t)arginfo - szsigcode), szsigcode); 593d66a5066SPeter Wemm 594d66a5066SPeter Wemm /* 595e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 596e1743d02SSøren Schmidt * on the stack. 597e1743d02SSøren Schmidt */ 598e1743d02SSøren Schmidt if (imgp->auxargs) 599e1743d02SSøren Schmidt /* 600e1743d02SSøren Schmidt * The '+ 2' is for the null pointers at the end of each of the 601e1743d02SSøren Schmidt * arg and env vector sets, and 'AT_COUNT*2' is room for the 602e1743d02SSøren Schmidt * ELF Auxargs data. 603e1743d02SSøren Schmidt */ 604e1743d02SSøren Schmidt vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 + 605e1743d02SSøren Schmidt AT_COUNT*2) * sizeof(char*)); 606e1743d02SSøren Schmidt else 607e1743d02SSøren Schmidt /* 60826f9a767SRodney W. Grimes * The '+ 2' is for the null pointers at the end of each of the 60926f9a767SRodney W. Grimes * arg and env vector sets 61026f9a767SRodney W. Grimes */ 611e1743d02SSøren Schmidt vectp = (char **) 612e1743d02SSøren Schmidt (destp - (imgp->argc + imgp->envc + 2) * sizeof(char*)); 61326f9a767SRodney W. Grimes 61426f9a767SRodney W. Grimes /* 61526f9a767SRodney W. Grimes * vectp also becomes our initial stack base 61626f9a767SRodney W. Grimes */ 617ecbb00a2SDoug Rabson stack_base = (long *)vectp; 61826f9a767SRodney W. Grimes 619c52007c2SDavid Greenman stringp = imgp->stringbase; 620c52007c2SDavid Greenman argc = imgp->argc; 621c52007c2SDavid Greenman envc = imgp->envc; 62226f9a767SRodney W. Grimes 62393f6448cSDavid Greenman /* 6243aed948bSDavid Greenman * Copy out strings - arguments and environment. 62593f6448cSDavid Greenman */ 626c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 62793f6448cSDavid Greenman 62893f6448cSDavid Greenman /* 6293aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 6303aed948bSDavid Greenman */ 631aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 6323aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 6333aed948bSDavid Greenman 6343aed948bSDavid Greenman /* 6353aed948bSDavid Greenman * Fill in argument portion of vector table. 63693f6448cSDavid Greenman */ 63726f9a767SRodney W. Grimes for (; argc > 0; --argc) { 638aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 6393aed948bSDavid Greenman while (*stringp++ != 0) 6403aed948bSDavid Greenman destp++; 6413aed948bSDavid Greenman destp++; 64226f9a767SRodney W. Grimes } 64326f9a767SRodney W. Grimes 64426f9a767SRodney W. Grimes /* a null vector table pointer seperates the argp's from the envp's */ 6456ab46d52SBruce Evans suword(vectp++, 0); 64626f9a767SRodney W. Grimes 647aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 6483aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 64993f6448cSDavid Greenman 65093f6448cSDavid Greenman /* 6513aed948bSDavid Greenman * Fill in environment portion of vector table. 65293f6448cSDavid Greenman */ 65326f9a767SRodney W. Grimes for (; envc > 0; --envc) { 654aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 6553aed948bSDavid Greenman while (*stringp++ != 0) 6563aed948bSDavid Greenman destp++; 6573aed948bSDavid Greenman destp++; 65826f9a767SRodney W. Grimes } 65926f9a767SRodney W. Grimes 66026f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 6616ab46d52SBruce Evans suword(vectp, 0); 66226f9a767SRodney W. Grimes 66326f9a767SRodney W. Grimes return (stack_base); 66426f9a767SRodney W. Grimes } 66526f9a767SRodney W. Grimes 66626f9a767SRodney W. Grimes /* 66726f9a767SRodney W. Grimes * Check permissions of file to execute. 66826f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 66926f9a767SRodney W. Grimes */ 670c8a79999SPeter Wemm int 671c52007c2SDavid Greenman exec_check_permissions(imgp) 672c52007c2SDavid Greenman struct image_params *imgp; 67326f9a767SRodney W. Grimes { 674c52007c2SDavid Greenman struct proc *p = imgp->proc; 675c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 676c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 67726f9a767SRodney W. Grimes int error; 67826f9a767SRodney W. Grimes 67926f9a767SRodney W. Grimes /* Get file attributes */ 680c52007c2SDavid Greenman error = VOP_GETATTR(vp, attr, p->p_ucred, p); 68126f9a767SRodney W. Grimes if (error) 68226f9a767SRodney W. Grimes return (error); 68326f9a767SRodney W. Grimes 68426f9a767SRodney W. Grimes /* 68526f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 68626f9a767SRodney W. Grimes * file resides on. 68726f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 68826f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 68926f9a767SRodney W. Grimes * file really is executable. 69026f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 69126f9a767SRodney W. Grimes */ 692c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 69326f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 69426f9a767SRodney W. Grimes (attr->va_type != VREG)) { 69526f9a767SRodney W. Grimes return (EACCES); 69626f9a767SRodney W. Grimes } 69726f9a767SRodney W. Grimes 69826f9a767SRodney W. Grimes /* 69926f9a767SRodney W. Grimes * Zero length files can't be exec'd 70026f9a767SRodney W. Grimes */ 70126f9a767SRodney W. Grimes if (attr->va_size == 0) 70226f9a767SRodney W. Grimes return (ENOEXEC); 70326f9a767SRodney W. Grimes 70426f9a767SRodney W. Grimes /* 70526f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 70626f9a767SRodney W. Grimes */ 707c52007c2SDavid Greenman error = VOP_ACCESS(vp, VEXEC, p->p_ucred, p); 70826f9a767SRodney W. Grimes if (error) 70926f9a767SRodney W. Grimes return (error); 71026f9a767SRodney W. Grimes 7116d5a0a8cSDavid Greenman /* 7126d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 7136d5a0a8cSDavid Greenman * if there are any. 7146d5a0a8cSDavid Greenman */ 7156d5a0a8cSDavid Greenman if (vp->v_writecount) 7166d5a0a8cSDavid Greenman return (ETXTBSY); 7176d5a0a8cSDavid Greenman 7186d5a0a8cSDavid Greenman /* 7196d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 7206d5a0a8cSDavid Greenman * general case). 7216d5a0a8cSDavid Greenman */ 722c52007c2SDavid Greenman error = VOP_OPEN(vp, FREAD, p->p_ucred, p); 72326f9a767SRodney W. Grimes if (error) 72426f9a767SRodney W. Grimes return (error); 72526f9a767SRodney W. Grimes 72626f9a767SRodney W. Grimes return (0); 727df8bae1dSRodney W. Grimes } 728aa855a59SPeter Wemm 729aa855a59SPeter Wemm /* 730aa855a59SPeter Wemm * Exec handler registration 731aa855a59SPeter Wemm */ 732aa855a59SPeter Wemm int 733aa855a59SPeter Wemm exec_register(execsw_arg) 734aa855a59SPeter Wemm const struct execsw *execsw_arg; 735aa855a59SPeter Wemm { 736aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 737aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 738aa855a59SPeter Wemm 739aa855a59SPeter Wemm if (execsw) 740aa855a59SPeter Wemm for (es = execsw; *es; es++) 741aa855a59SPeter Wemm count++; 742aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 743aa855a59SPeter Wemm if (newexecsw == NULL) 744aa855a59SPeter Wemm return ENOMEM; 745aa855a59SPeter Wemm xs = newexecsw; 746aa855a59SPeter Wemm if (execsw) 747aa855a59SPeter Wemm for (es = execsw; *es; es++) 748aa855a59SPeter Wemm *xs++ = *es; 749aa855a59SPeter Wemm *xs++ = execsw_arg; 750aa855a59SPeter Wemm *xs = NULL; 751aa855a59SPeter Wemm if (execsw) 752aa855a59SPeter Wemm free(execsw, M_TEMP); 753aa855a59SPeter Wemm execsw = newexecsw; 754aa855a59SPeter Wemm return 0; 755aa855a59SPeter Wemm } 756aa855a59SPeter Wemm 757aa855a59SPeter Wemm int 758aa855a59SPeter Wemm exec_unregister(execsw_arg) 759aa855a59SPeter Wemm const struct execsw *execsw_arg; 760aa855a59SPeter Wemm { 761aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 762aa855a59SPeter Wemm int count = 1; 763aa855a59SPeter Wemm 764aa855a59SPeter Wemm if (execsw == NULL) 765aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 766aa855a59SPeter Wemm 767aa855a59SPeter Wemm for (es = execsw; *es; es++) { 768aa855a59SPeter Wemm if (*es == execsw_arg) 769aa855a59SPeter Wemm break; 770aa855a59SPeter Wemm } 771aa855a59SPeter Wemm if (*es == NULL) 772aa855a59SPeter Wemm return ENOENT; 773aa855a59SPeter Wemm for (es = execsw; *es; es++) 774aa855a59SPeter Wemm if (*es != execsw_arg) 775aa855a59SPeter Wemm count++; 776aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 777aa855a59SPeter Wemm if (newexecsw == NULL) 778aa855a59SPeter Wemm return ENOMEM; 779aa855a59SPeter Wemm xs = newexecsw; 780aa855a59SPeter Wemm for (es = execsw; *es; es++) 781aa855a59SPeter Wemm if (*es != execsw_arg) 782aa855a59SPeter Wemm *xs++ = *es; 783aa855a59SPeter Wemm *xs = NULL; 784aa855a59SPeter Wemm if (execsw) 785aa855a59SPeter Wemm free(execsw, M_TEMP); 786aa855a59SPeter Wemm execsw = newexecsw; 787aa855a59SPeter Wemm return 0; 788aa855a59SPeter Wemm } 789