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 291e9b3d91SPeter Wemm #include "opt_ktrace.h" 30339b79b9SRobert Watson #include "opt_mac.h" 311e9b3d91SPeter Wemm 32df8bae1dSRodney W. Grimes #include <sys/param.h> 3326f9a767SRodney W. Grimes #include <sys/systm.h> 34fb919e4dSMark Murray #include <sys/lock.h> 35fb919e4dSMark Murray #include <sys/mutex.h> 36d2d3e875SBruce Evans #include <sys/sysproto.h> 3726f9a767SRodney W. Grimes #include <sys/signalvar.h> 3826f9a767SRodney W. Grimes #include <sys/kernel.h> 39339b79b9SRobert Watson #include <sys/mac.h> 4026f9a767SRodney W. Grimes #include <sys/mount.h> 41797f2d22SPoul-Henning Kamp #include <sys/filedesc.h> 42079cc25bSDavid Greenman #include <sys/fcntl.h> 4326f9a767SRodney W. Grimes #include <sys/acct.h> 4426f9a767SRodney W. Grimes #include <sys/exec.h> 4526f9a767SRodney W. Grimes #include <sys/imgact.h> 46e1743d02SSøren Schmidt #include <sys/imgact_elf.h> 4726f9a767SRodney W. Grimes #include <sys/wait.h> 48333ea485SGuido van Rooij #include <sys/malloc.h> 49a794e791SBruce Evans #include <sys/proc.h> 502a024a2bSSean Eric Fagan #include <sys/pioctl.h> 51a794e791SBruce Evans #include <sys/namei.h> 521e1e0b44SSøren Schmidt #include <sys/sysent.h> 53797f2d22SPoul-Henning Kamp #include <sys/shm.h> 5499ac3bc8SPeter Wemm #include <sys/sysctl.h> 55333ea485SGuido van Rooij #include <sys/user.h> 56a794e791SBruce Evans #include <sys/vnode.h> 57c781aea8SPeter Wemm #ifdef KTRACE 58c781aea8SPeter Wemm #include <sys/ktrace.h> 59c781aea8SPeter Wemm #endif 6026f9a767SRodney W. Grimes 6126f9a767SRodney W. Grimes #include <vm/vm.h> 62efeaf95aSDavid Greenman #include <vm/vm_param.h> 63efeaf95aSDavid Greenman #include <vm/pmap.h> 641616db3cSJohn Dyson #include <vm/vm_page.h> 65efeaf95aSDavid Greenman #include <vm/vm_map.h> 6626f9a767SRodney W. Grimes #include <vm/vm_kern.h> 67efeaf95aSDavid Greenman #include <vm/vm_extern.h> 681ebd0c59SDavid Greenman #include <vm/vm_object.h> 691616db3cSJohn Dyson #include <vm/vm_pager.h> 7026f9a767SRodney W. Grimes 7126f9a767SRodney W. Grimes #include <machine/reg.h> 7226f9a767SRodney W. Grimes 73b9df5231SPoul-Henning Kamp MALLOC_DEFINE(M_PARGS, "proc-args", "Process arguments"); 74b9df5231SPoul-Henning Kamp 7521d56e9cSAlfred Perlstein static MALLOC_DEFINE(M_ATEXEC, "atexec", "atexec callback"); 7621d56e9cSAlfred Perlstein 7705ba50f5SJake Burkholder static int sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS); 7805ba50f5SJake Burkholder static int sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS); 79450ffb44SRobert Watson static int kern_execve(struct thread *td, char *fname, char **argv, 80450ffb44SRobert Watson char **envv); 8105ba50f5SJake Burkholder 8221d56e9cSAlfred Perlstein /* 8321d56e9cSAlfred Perlstein * callout list for things to do at exec time 8421d56e9cSAlfred Perlstein */ 8521d56e9cSAlfred Perlstein struct execlist { 8621d56e9cSAlfred Perlstein execlist_fn function; 8721d56e9cSAlfred Perlstein TAILQ_ENTRY(execlist) next; 8821d56e9cSAlfred Perlstein }; 8921d56e9cSAlfred Perlstein 9021d56e9cSAlfred Perlstein TAILQ_HEAD(exec_list_head, execlist); 9121d56e9cSAlfred Perlstein static struct exec_list_head exec_list = TAILQ_HEAD_INITIALIZER(exec_list); 9221d56e9cSAlfred Perlstein 939701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 9405ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD, 9505ba50f5SJake Burkholder NULL, 0, sysctl_kern_ps_strings, "LU", ""); 96486bddb0SDoug Rabson 979701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 9805ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD, 9905ba50f5SJake Burkholder NULL, 0, sysctl_kern_usrstack, "LU", ""); 10099ac3bc8SPeter Wemm 101b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16; 102c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 1039701cd40SJohn Baldwin &ps_arg_cache_limit, 0, ""); 104b9df5231SPoul-Henning Kamp 105a8704f89SPoul-Henning Kamp int ps_argsopen = 1; 106a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, ""); 107a8704f89SPoul-Henning Kamp 108911fc923SPeter Wemm #ifdef __ia64__ 109911fc923SPeter Wemm /* XXX HACK */ 110911fc923SPeter Wemm static int regstkpages = 256; 111911fc923SPeter Wemm SYSCTL_INT(_machdep, OID_AUTO, regstkpages, CTLFLAG_RW, ®stkpages, 0, ""); 112911fc923SPeter Wemm #endif 113911fc923SPeter Wemm 11405ba50f5SJake Burkholder static int 11505ba50f5SJake Burkholder sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS) 11605ba50f5SJake Burkholder { 11705ba50f5SJake Burkholder struct proc *p; 11805ba50f5SJake Burkholder 11905ba50f5SJake Burkholder p = curproc; 12005ba50f5SJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_psstrings, 12105ba50f5SJake Burkholder sizeof(p->p_sysent->sv_psstrings))); 12205ba50f5SJake Burkholder } 12305ba50f5SJake Burkholder 12405ba50f5SJake Burkholder static int 12505ba50f5SJake Burkholder sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS) 12605ba50f5SJake Burkholder { 12705ba50f5SJake Burkholder struct proc *p; 12805ba50f5SJake Burkholder 12905ba50f5SJake Burkholder p = curproc; 13005ba50f5SJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_usrstack, 13105ba50f5SJake Burkholder sizeof(p->p_sysent->sv_usrstack))); 13205ba50f5SJake Burkholder } 13305ba50f5SJake Burkholder 13499ac3bc8SPeter Wemm /* 135aa855a59SPeter Wemm * Each of the items is a pointer to a `const struct execsw', hence the 136aa855a59SPeter Wemm * double pointer here. 137df8bae1dSRodney W. Grimes */ 138aa855a59SPeter Wemm static const struct execsw **execsw; 13926f9a767SRodney W. Grimes 14026f9a767SRodney W. Grimes /* 141450ffb44SRobert Watson * In-kernel implementation of execve(). All arguments are assumed to be 142450ffb44SRobert Watson * userspace pointers from the passed thread. 143116734c4SMatthew Dillon * 144116734c4SMatthew Dillon * MPSAFE 14526f9a767SRodney W. Grimes */ 146450ffb44SRobert Watson static int 147450ffb44SRobert Watson kern_execve(td, fname, argv, envv) 148b40ce416SJulian Elischer struct thread *td; 149450ffb44SRobert Watson char *fname; 150450ffb44SRobert Watson char **argv; 151450ffb44SRobert Watson char **envv; 152df8bae1dSRodney W. Grimes { 153b40ce416SJulian Elischer struct proc *p = td->td_proc; 15426f9a767SRodney W. Grimes struct nameidata nd, *ndp; 1559b3b1c5fSJohn Baldwin struct ucred *newcred = NULL, *oldcred; 1561419eacbSAlfred Perlstein struct uidinfo *euip; 157654f6be1SBruce Evans register_t *stack_base; 158bb56ec4aSPoul-Henning Kamp int error, len, i; 159c52007c2SDavid Greenman struct image_params image_params, *imgp; 16026f9a767SRodney W. Grimes struct vattr attr; 1614d77a549SAlfred Perlstein int (*img_first)(struct image_params *); 16269be5db9SAlfred Perlstein struct pargs *oldargs = NULL, *newargs = NULL; 1639b3b1c5fSJohn Baldwin struct procsig *oldprocsig, *newprocsig; 1646c84de02SJohn Baldwin #ifdef KTRACE 1656c84de02SJohn Baldwin struct vnode *tracevp = NULL; 1666c84de02SJohn Baldwin #endif 1676c84de02SJohn Baldwin struct vnode *textvp = NULL; 168d06c0d4dSRobert Watson int credential_changing; 169619eb6e5SJeff Roberson int textset; 17026f9a767SRodney W. Grimes 171c52007c2SDavid Greenman imgp = &image_params; 172df8bae1dSRodney W. Grimes 1739ca45e81SDag-Erling Smørgrav /* 1749ca45e81SDag-Erling Smørgrav * Lock the process and set the P_INEXEC flag to indicate that 1759ca45e81SDag-Erling Smørgrav * it should be left alone until we're done here. This is 1769ca45e81SDag-Erling Smørgrav * necessary to avoid race conditions - e.g. in ptrace() - 1779ca45e81SDag-Erling Smørgrav * that might allow a local user to illicitly obtain elevated 1789ca45e81SDag-Erling Smørgrav * privileges. 1799ca45e81SDag-Erling Smørgrav */ 1809ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 1819ca45e81SDag-Erling Smørgrav KASSERT((p->p_flag & P_INEXEC) == 0, 18291f91617SDavid E. O'Brien ("%s(): process already has P_INEXEC flag", __func__)); 18349539972SJulian Elischer if (p->p_flag & P_KSES) { 1841279572aSDavid Xu if (thread_single(SINGLE_EXIT)) { 185e602ba25SJulian Elischer PROC_UNLOCK(p); 186e602ba25SJulian Elischer return (ERESTART); /* Try again later. */ 187e602ba25SJulian Elischer } 18849539972SJulian Elischer /* 18949539972SJulian Elischer * If we get here all other threads are dead, 19049539972SJulian Elischer * so unset the associated flags and lose KSE mode. 19149539972SJulian Elischer */ 19249539972SJulian Elischer p->p_flag &= ~P_KSES; 19349539972SJulian Elischer td->td_flags &= ~TDF_UNBOUND; 19449539972SJulian Elischer thread_single_end(); 19549539972SJulian Elischer } 1969ca45e81SDag-Erling Smørgrav p->p_flag |= P_INEXEC; 1979ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 1989ca45e81SDag-Erling Smørgrav 199df8bae1dSRodney W. Grimes /* 200c52007c2SDavid Greenman * Initialize part of the common data 201df8bae1dSRodney W. Grimes */ 202c52007c2SDavid Greenman imgp->proc = p; 203450ffb44SRobert Watson imgp->userspace_argv = argv; 204450ffb44SRobert Watson imgp->userspace_envv = envv; 205c52007c2SDavid Greenman imgp->attr = &attr; 206c52007c2SDavid Greenman imgp->argc = imgp->envc = 0; 2075cf3d12cSAndrey A. Chernov imgp->argv0 = NULL; 208c52007c2SDavid Greenman imgp->entry_addr = 0; 209c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 210c52007c2SDavid Greenman imgp->interpreted = 0; 211c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 212e1743d02SSøren Schmidt imgp->auxargs = NULL; 2131616db3cSJohn Dyson imgp->vp = NULL; 2140b2ed1aeSJeff Roberson imgp->object = NULL; 2151616db3cSJohn Dyson imgp->firstpage = NULL; 2164fe88fe6SJohn Polstra imgp->ps_strings = 0; 217b9a22da4STakanori Watanabe imgp->auxarg_size = 0; 21826f9a767SRodney W. Grimes 21926f9a767SRodney W. Grimes /* 22026f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 22126f9a767SRodney W. Grimes * environment strings 22226f9a767SRodney W. Grimes */ 223ca0387efSJake Burkholder imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + 224ca0387efSJake Burkholder PAGE_SIZE); 225c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 22626f9a767SRodney W. Grimes error = ENOMEM; 227b3afd20dSAlan Cox mtx_lock(&Giant); 22826f9a767SRodney W. Grimes goto exec_fail; 22926f9a767SRodney W. Grimes } 230c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 231c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 2321616db3cSJohn Dyson imgp->image_header = imgp->stringbase + ARG_MAX; 23326f9a767SRodney W. Grimes 23426f9a767SRodney W. Grimes /* 23526f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 23626f9a767SRodney W. Grimes * in ni_vp amoung other things. 23726f9a767SRodney W. Grimes */ 23826f9a767SRodney W. Grimes ndp = &nd; 239b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 240450ffb44SRobert Watson UIO_USERSPACE, fname, td); 24126f9a767SRodney W. Grimes 242b3afd20dSAlan Cox mtx_lock(&Giant); 24326f9a767SRodney W. Grimes interpret: 24426f9a767SRodney W. Grimes 24526f9a767SRodney W. Grimes error = namei(ndp); 24626f9a767SRodney W. Grimes if (error) { 2471616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 2481616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 24926f9a767SRodney W. Grimes goto exec_fail; 25026f9a767SRodney W. Grimes } 25126f9a767SRodney W. Grimes 252c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 253450ffb44SRobert Watson imgp->fname = fname; 25426f9a767SRodney W. Grimes 25526f9a767SRodney W. Grimes /* 2569022e4adSDavid Greenman * Check file permissions (also 'opens' file) 2579022e4adSDavid Greenman */ 258c52007c2SDavid Greenman error = exec_check_permissions(imgp); 259619eb6e5SJeff Roberson if (error) 26026f9a767SRodney W. Grimes goto exec_fail_dealloc; 261619eb6e5SJeff Roberson 262619eb6e5SJeff Roberson if (VOP_GETVOBJECT(imgp->vp, &imgp->object) == 0) 2630b2ed1aeSJeff Roberson vm_object_reference(imgp->object); 26426f9a767SRodney W. Grimes 265619eb6e5SJeff Roberson /* 266619eb6e5SJeff Roberson * Set VV_TEXT now so no one can write to the executable while we're 267619eb6e5SJeff Roberson * activating it. 268619eb6e5SJeff Roberson * 269619eb6e5SJeff Roberson * Remember if this was set before and unset it in case this is not 270619eb6e5SJeff Roberson * actually an executable image. 271619eb6e5SJeff Roberson */ 272619eb6e5SJeff Roberson textset = imgp->vp->v_vflag & VV_TEXT; 273619eb6e5SJeff Roberson imgp->vp->v_vflag |= VV_TEXT; 274619eb6e5SJeff Roberson 2751616db3cSJohn Dyson error = exec_map_first_page(imgp); 2766d5a0a8cSDavid Greenman if (error) 27726f9a767SRodney W. Grimes goto exec_fail_dealloc; 27826f9a767SRodney W. Grimes 27926f9a767SRodney W. Grimes /* 280d323ddf3SMatthew Dillon * If the current process has a special image activator it 281d323ddf3SMatthew Dillon * wants to try first, call it. For example, emulating shell 282d323ddf3SMatthew Dillon * scripts differently. 28326f9a767SRodney W. Grimes */ 284d323ddf3SMatthew Dillon error = -1; 285d323ddf3SMatthew Dillon if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 286d323ddf3SMatthew Dillon error = img_first(imgp); 287d323ddf3SMatthew Dillon 288d323ddf3SMatthew Dillon /* 289d323ddf3SMatthew Dillon * Loop through the list of image activators, calling each one. 290d323ddf3SMatthew Dillon * An activator returns -1 if there is no match, 0 on success, 291d323ddf3SMatthew Dillon * and an error otherwise. 292d323ddf3SMatthew Dillon */ 293d323ddf3SMatthew Dillon for (i = 0; error == -1 && execsw[i]; ++i) { 294d323ddf3SMatthew Dillon if (execsw[i]->ex_imgact == NULL || 295d323ddf3SMatthew Dillon execsw[i]->ex_imgact == img_first) { 296d323ddf3SMatthew Dillon continue; 297d323ddf3SMatthew Dillon } 298c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 299d323ddf3SMatthew Dillon } 300d323ddf3SMatthew Dillon 301d323ddf3SMatthew Dillon if (error) { 302619eb6e5SJeff Roberson if (error == -1) { 303619eb6e5SJeff Roberson if (textset == 0) 304619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 305d323ddf3SMatthew Dillon error = ENOEXEC; 306619eb6e5SJeff Roberson } 30726f9a767SRodney W. Grimes goto exec_fail_dealloc; 308d323ddf3SMatthew Dillon } 309d323ddf3SMatthew Dillon 310d323ddf3SMatthew Dillon /* 311d323ddf3SMatthew Dillon * Special interpreter operation, cleanup and loop up to try to 312d323ddf3SMatthew Dillon * activate the interpreter. 313d323ddf3SMatthew Dillon */ 314c52007c2SDavid Greenman if (imgp->interpreted) { 3151616db3cSJohn Dyson exec_unmap_first_page(imgp); 316619eb6e5SJeff Roberson /* 317619eb6e5SJeff Roberson * VV_TEXT needs to be unset for scripts. There is a short 318619eb6e5SJeff Roberson * period before we determine that something is a script where 319619eb6e5SJeff Roberson * VV_TEXT will be set. The vnode lock is held over this 320619eb6e5SJeff Roberson * entire period so nothing should illegitimately be blocked. 321619eb6e5SJeff Roberson */ 322619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 323762e6b85SEivind Eklund /* free name buffer and old vnode */ 324762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 325619eb6e5SJeff Roberson vput(ndp->ni_vp); 3260b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 3270b2ed1aeSJeff Roberson imgp->object = NULL; 32826f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 329b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 330b40ce416SJulian Elischer UIO_SYSSPACE, imgp->interpreter_name, td); 33126f9a767SRodney W. Grimes goto interpret; 33226f9a767SRodney W. Grimes } 33326f9a767SRodney W. Grimes 33426f9a767SRodney W. Grimes /* 33526f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 33626f9a767SRodney W. Grimes */ 3373ebc1248SPeter Wemm if (p->p_sysent->sv_copyout_strings) 3383ebc1248SPeter Wemm stack_base = (*p->p_sysent->sv_copyout_strings)(imgp); 3393ebc1248SPeter Wemm else 340c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 34126f9a767SRodney W. Grimes 34226f9a767SRodney W. Grimes /* 3431e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 3441e1e0b44SSøren Schmidt * let it do the stack setup. 3451e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 34626f9a767SRodney W. Grimes */ 3471e1e0b44SSøren Schmidt if (p->p_sysent->sv_fixup) 348c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 3491e1e0b44SSøren Schmidt else 350c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 35126f9a767SRodney W. Grimes 352a78e8d2aSDavid Greenman /* 353a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 354a78e8d2aSDavid Greenman * be shared after an exec. 355a78e8d2aSDavid Greenman */ 356426da3bcSAlfred Perlstein FILEDESC_LOCK(p->p_fd); 357a78e8d2aSDavid Greenman if (p->p_fd->fd_refcnt > 1) { 358a78e8d2aSDavid Greenman struct filedesc *tmp; 359a78e8d2aSDavid Greenman 360b40ce416SJulian Elischer tmp = fdcopy(td); 361426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 362b40ce416SJulian Elischer fdfree(td); 363a78e8d2aSDavid Greenman p->p_fd = tmp; 364426da3bcSAlfred Perlstein } else 365426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 366a78e8d2aSDavid Greenman 367333ea485SGuido van Rooij /* 3689b3b1c5fSJohn Baldwin * Malloc things before we need locks. 3699b3b1c5fSJohn Baldwin */ 3709b3b1c5fSJohn Baldwin newcred = crget(); 3711419eacbSAlfred Perlstein euip = uifind(attr.va_uid); 3729b3b1c5fSJohn Baldwin i = imgp->endargs - imgp->stringbase; 3739b3b1c5fSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) 3749b3b1c5fSJohn Baldwin newargs = pargs_alloc(i); 3759b3b1c5fSJohn Baldwin 3769b3b1c5fSJohn Baldwin /* close files on exec */ 3779b3b1c5fSJohn Baldwin fdcloseexec(td); 3789b3b1c5fSJohn Baldwin 379619eb6e5SJeff Roberson /* Get a reference to the vnode prior to locking the proc */ 380619eb6e5SJeff Roberson VREF(ndp->ni_vp); 381619eb6e5SJeff Roberson 3829b3b1c5fSJohn Baldwin /* 383333ea485SGuido van Rooij * For security and other reasons, signal handlers cannot 3848688bb93SJohn Baldwin * be shared after an exec. The new process gets a copy of the old 385b2c3fa70SDima Dorfman * handlers. In execsigs(), the new process will have its signals 386333ea485SGuido van Rooij * reset. 387333ea485SGuido van Rooij */ 3889b3b1c5fSJohn Baldwin PROC_LOCK(p); 3899b3b1c5fSJohn Baldwin mp_fixme("procsig needs a lock"); 390333ea485SGuido van Rooij if (p->p_procsig->ps_refcnt > 1) { 3919b3b1c5fSJohn Baldwin oldprocsig = p->p_procsig; 3929b3b1c5fSJohn Baldwin PROC_UNLOCK(p); 393333ea485SGuido van Rooij MALLOC(newprocsig, struct procsig *, sizeof(struct procsig), 394333ea485SGuido van Rooij M_SUBPROC, M_WAITOK); 3959b3b1c5fSJohn Baldwin bcopy(oldprocsig, newprocsig, sizeof(*newprocsig)); 3969b3b1c5fSJohn Baldwin newprocsig->ps_refcnt = 1; 3979b3b1c5fSJohn Baldwin oldprocsig->ps_refcnt--; 3989b3b1c5fSJohn Baldwin PROC_LOCK(p); 399333ea485SGuido van Rooij p->p_procsig = newprocsig; 400b40ce416SJulian Elischer if (p->p_sigacts == &p->p_uarea->u_sigacts) 401b2c3fa70SDima Dorfman panic("shared procsig but private sigacts?"); 402333ea485SGuido van Rooij 403b40ce416SJulian Elischer p->p_uarea->u_sigacts = *p->p_sigacts; 404b40ce416SJulian Elischer p->p_sigacts = &p->p_uarea->u_sigacts; 405333ea485SGuido van Rooij } 406fdf4e8b3SWarner Losh /* Stop profiling */ 407fdf4e8b3SWarner Losh stopprofclock(p); 408fdf4e8b3SWarner Losh 40926f9a767SRodney W. Grimes /* reset caught signals */ 41026f9a767SRodney W. Grimes execsigs(p); 41126f9a767SRodney W. Grimes 41226f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 41326f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 41426f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 41526f9a767SRodney W. Grimes p->p_comm[len] = 0; 41626f9a767SRodney W. Grimes 41726f9a767SRodney W. Grimes /* 41824b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 419dc733423SDag-Erling Smørgrav * it that it now has its own resources back 42026f9a767SRodney W. Grimes */ 42126f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 42226f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 42326f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 4247f05b035SAlfred Perlstein wakeup(p->p_pptr); 42526f9a767SRodney W. Grimes } 42626f9a767SRodney W. Grimes 42726f9a767SRodney W. Grimes /* 428a78e8d2aSDavid Greenman * Implement image setuid/setgid. 429a78e8d2aSDavid Greenman * 430a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 431a78e8d2aSDavid Greenman * the process is being traced. 432c52007c2SDavid Greenman */ 433b1fc0ec1SRobert Watson oldcred = p->p_ucred; 434d06c0d4dSRobert Watson credential_changing = 0; 435d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid != 436d06c0d4dSRobert Watson attr.va_uid; 437d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid != 438d06c0d4dSRobert Watson attr.va_gid; 439d06c0d4dSRobert Watson 440d06c0d4dSRobert Watson if (credential_changing && 441a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 442c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 443c52007c2SDavid Greenman /* 444c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 445b85db196SPeter Wemm * root. Record any set-id flags first to make sure that 446b85db196SPeter Wemm * we do not regain any tracing during a possible block. 44726f9a767SRodney W. Grimes */ 448b85db196SPeter Wemm setsugid(p); 4496c84de02SJohn Baldwin #ifdef KTRACE 45044731cabSJohn Baldwin if (p->p_tracep && suser_cred(oldcred, PRISON_ROOT)) { 4516c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 45279deba82SMatthew Dillon p->p_traceflag = 0; 4539b3b1c5fSJohn Baldwin tracevp = p->p_tracep; 4549b3b1c5fSJohn Baldwin p->p_tracep = NULL; 4556c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 45626f9a767SRodney W. Grimes } 4576c84de02SJohn Baldwin #endif 45828b325aaSDon Lewis /* 459c1e2d386SNate Lawson * Close any file descriptors 0..2 that reference procfs, 460c1e2d386SNate Lawson * then make sure file descriptors 0..2 are in use. 46128b325aaSDon Lewis * 462c1e2d386SNate Lawson * setugidsafety() may call closef() and then pfind() 463c1e2d386SNate Lawson * which may grab the process lock. 46428b325aaSDon Lewis * fdcheckstd() may call falloc() which may block to 46528b325aaSDon Lewis * allocate memory, so temporarily drop the process lock. 46628b325aaSDon Lewis */ 46728b325aaSDon Lewis PROC_UNLOCK(p); 468c1e2d386SNate Lawson setugidsafety(td); 469e983a376SJacques Vidrine error = fdcheckstd(td); 47063c9e754SJohn Baldwin if (error != 0) 47169be5db9SAlfred Perlstein goto done1; 472e1b1aa3bSJohn Baldwin PROC_LOCK(p); 473c52007c2SDavid Greenman /* 474c52007c2SDavid Greenman * Set the new credentials. 475c52007c2SDavid Greenman */ 4769b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 477c52007c2SDavid Greenman if (attr.va_mode & VSUID) 4781419eacbSAlfred Perlstein change_euid(newcred, euip); 479c52007c2SDavid Greenman if (attr.va_mode & VSGID) 480b1fc0ec1SRobert Watson change_egid(newcred, attr.va_gid); 4819b3b1c5fSJohn Baldwin /* 4829b3b1c5fSJohn Baldwin * Implement correct POSIX saved-id behavior. 4839b3b1c5fSJohn Baldwin */ 4849b3b1c5fSJohn Baldwin change_svuid(newcred, newcred->cr_uid); 4859b3b1c5fSJohn Baldwin change_svgid(newcred, newcred->cr_gid); 4869b3b1c5fSJohn Baldwin p->p_ucred = newcred; 4879b3b1c5fSJohn Baldwin newcred = NULL; 488c52007c2SDavid Greenman } else { 489b1fc0ec1SRobert Watson if (oldcred->cr_uid == oldcred->cr_ruid && 490b1fc0ec1SRobert Watson oldcred->cr_gid == oldcred->cr_rgid) 491c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 49226f9a767SRodney W. Grimes /* 493c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 494b1fc0ec1SRobert Watson * 495b1fc0ec1SRobert Watson * XXX: It's not clear that the existing behavior is 4969b3b1c5fSJohn Baldwin * POSIX-compliant. A number of sources indicate that the 4979b3b1c5fSJohn Baldwin * saved uid/gid should only be updated if the new ruid is 4989b3b1c5fSJohn Baldwin * not equal to the old ruid, or the new euid is not equal 4999b3b1c5fSJohn Baldwin * to the old euid and the new euid is not equal to the old 5009b3b1c5fSJohn Baldwin * ruid. The FreeBSD code always updates the saved uid/gid. 5019b3b1c5fSJohn Baldwin * Also, this code uses the new (replaced) euid and egid as 5029b3b1c5fSJohn Baldwin * the source, which may or may not be the right ones to use. 50326f9a767SRodney W. Grimes */ 504b1fc0ec1SRobert Watson if (oldcred->cr_svuid != oldcred->cr_uid || 505b1fc0ec1SRobert Watson oldcred->cr_svgid != oldcred->cr_gid) { 5069b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 507b1fc0ec1SRobert Watson change_svuid(newcred, newcred->cr_uid); 508b1fc0ec1SRobert Watson change_svgid(newcred, newcred->cr_gid); 509b1fc0ec1SRobert Watson p->p_ucred = newcred; 5109b3b1c5fSJohn Baldwin newcred = NULL; 5119b3b1c5fSJohn Baldwin } 512b1fc0ec1SRobert Watson } 51326f9a767SRodney W. Grimes 51426f9a767SRodney W. Grimes /* 515619eb6e5SJeff Roberson * Store the vp for use in procfs. This vnode was referenced prior 516619eb6e5SJeff Roberson * to locking the proc lock. 5172a531c80SDavid Greenman */ 5189b3b1c5fSJohn Baldwin textvp = p->p_textvp; 5192a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 5202a531c80SDavid Greenman 5212a531c80SDavid Greenman /* 5229ca45e81SDag-Erling Smørgrav * Notify others that we exec'd, and clear the P_INEXEC flag 5239ca45e81SDag-Erling Smørgrav * as we're now a bona fide freshly-execed process. 524cb679c38SJonathan Lemon */ 525cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_EXEC); 5269ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 527cb679c38SJonathan Lemon 528cb679c38SJonathan Lemon /* 52926f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 53026f9a767SRodney W. Grimes * can be set before the program executes. 53126f9a767SRodney W. Grimes */ 532e65897c3SJohn Baldwin _STOPEVENT(p, S_EXEC, 0); 5332a024a2bSSean Eric Fagan 53426f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 53526f9a767SRodney W. Grimes psignal(p, SIGTRAP); 53626f9a767SRodney W. Grimes 53726f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 53826f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 53926f9a767SRodney W. Grimes 540b9df5231SPoul-Henning Kamp /* Free any previous argument cache */ 5419b3b1c5fSJohn Baldwin oldargs = p->p_args; 542b9df5231SPoul-Henning Kamp p->p_args = NULL; 543b9df5231SPoul-Henning Kamp 544e1b1aa3bSJohn Baldwin /* Cache arguments if they fit inside our allowance */ 545e1b1aa3bSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 546e1b1aa3bSJohn Baldwin bcopy(imgp->stringbase, newargs->ar_args, i); 547e1b1aa3bSJohn Baldwin p->p_args = newargs; 548e1b1aa3bSJohn Baldwin newargs = NULL; 549e1b1aa3bSJohn Baldwin } 550e1b1aa3bSJohn Baldwin PROC_UNLOCK(p); 551e1b1aa3bSJohn Baldwin 552e913ca22SDoug Rabson /* Set values passed into the program in registers. */ 5533ebc1248SPeter Wemm if (p->p_sysent->sv_setregs) 5543ebc1248SPeter Wemm (*p->p_sysent->sv_setregs)(td, imgp->entry_addr, 5553ebc1248SPeter Wemm (u_long)(uintptr_t)stack_base, imgp->ps_strings); 5563ebc1248SPeter Wemm else 557bafbd492SJake Burkholder exec_setregs(td, imgp->entry_addr, 558bafbd492SJake Burkholder (u_long)(uintptr_t)stack_base, imgp->ps_strings); 559e913ca22SDoug Rabson 56069be5db9SAlfred Perlstein done1: 5619b3b1c5fSJohn Baldwin /* 5629b3b1c5fSJohn Baldwin * Free any resources malloc'd earlier that we didn't use. 5639b3b1c5fSJohn Baldwin */ 5641419eacbSAlfred Perlstein uifree(euip); 5659b3b1c5fSJohn Baldwin if (newcred == NULL) 5669b3b1c5fSJohn Baldwin crfree(oldcred); 5679b3b1c5fSJohn Baldwin else 5689b3b1c5fSJohn Baldwin crfree(newcred); 5699b3b1c5fSJohn Baldwin /* 5709b3b1c5fSJohn Baldwin * Handle deferred decrement of ref counts. 5719b3b1c5fSJohn Baldwin */ 5729b3b1c5fSJohn Baldwin if (textvp != NULL) 5739b3b1c5fSJohn Baldwin vrele(textvp); 574619eb6e5SJeff Roberson if (ndp->ni_vp && error != 0) 575619eb6e5SJeff Roberson vrele(ndp->ni_vp); 5766c84de02SJohn Baldwin #ifdef KTRACE 5779b3b1c5fSJohn Baldwin if (tracevp != NULL) 5789b3b1c5fSJohn Baldwin vrele(tracevp); 5796c84de02SJohn Baldwin #endif 58069be5db9SAlfred Perlstein if (oldargs != NULL) 5819b3b1c5fSJohn Baldwin pargs_drop(oldargs); 58269be5db9SAlfred Perlstein if (newargs != NULL) 58369be5db9SAlfred Perlstein pargs_drop(newargs); 584b9df5231SPoul-Henning Kamp 5851616db3cSJohn Dyson exec_fail_dealloc: 5861616db3cSJohn Dyson 58726f9a767SRodney W. Grimes /* 58826f9a767SRodney W. Grimes * free various allocated resources 58926f9a767SRodney W. Grimes */ 5901616db3cSJohn Dyson if (imgp->firstpage) 5911616db3cSJohn Dyson exec_unmap_first_page(imgp); 59226f9a767SRodney W. Grimes 593c2f9f36bSDavid Greenman if (imgp->stringbase != NULL) 5941616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 5951616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 5961616db3cSJohn Dyson 5979c0fed3dSDoug Rabson if (imgp->vp) { 598762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 599619eb6e5SJeff Roberson vput(imgp->vp); 600a3cf6ebaSDavid Greenman } 60126f9a767SRodney W. Grimes 6020b2ed1aeSJeff Roberson if (imgp->object) 6030b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 6040b2ed1aeSJeff Roberson 6051616db3cSJohn Dyson if (error == 0) 606116734c4SMatthew Dillon goto done2; 6071616db3cSJohn Dyson 60826f9a767SRodney W. Grimes exec_fail: 6099ca45e81SDag-Erling Smørgrav /* we're done here, clear P_INEXEC */ 6109ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 6119ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 6129ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 6139ca45e81SDag-Erling Smørgrav 614c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 61526f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 616b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, SIGABRT)); 61726f9a767SRodney W. Grimes /* NOT REACHED */ 618116734c4SMatthew Dillon error = 0; 61926f9a767SRodney W. Grimes } 620116734c4SMatthew Dillon done2: 621116734c4SMatthew Dillon mtx_unlock(&Giant); 622116734c4SMatthew Dillon return (error); 62326f9a767SRodney W. Grimes } 62426f9a767SRodney W. Grimes 625450ffb44SRobert Watson #ifndef _SYS_SYSPROTO_H_ 626450ffb44SRobert Watson struct execve_args { 627450ffb44SRobert Watson char *fname; 628450ffb44SRobert Watson char **argv; 629450ffb44SRobert Watson char **envv; 630450ffb44SRobert Watson }; 631450ffb44SRobert Watson #endif 632450ffb44SRobert Watson 633450ffb44SRobert Watson /* 634450ffb44SRobert Watson * MPSAFE 635450ffb44SRobert Watson */ 636450ffb44SRobert Watson int 637450ffb44SRobert Watson execve(td, uap) 638450ffb44SRobert Watson struct thread *td; 639450ffb44SRobert Watson struct execve_args /* { 640450ffb44SRobert Watson syscallarg(char *) fname; 641450ffb44SRobert Watson syscallarg(char **) argv; 642450ffb44SRobert Watson syscallarg(char **) envv; 643450ffb44SRobert Watson } */ *uap; 644450ffb44SRobert Watson { 645450ffb44SRobert Watson 646450ffb44SRobert Watson return (kern_execve(td, uap->fname, uap->argv, uap->envv)); 647450ffb44SRobert Watson } 648450ffb44SRobert Watson 6491616db3cSJohn Dyson int 6501616db3cSJohn Dyson exec_map_first_page(imgp) 6511616db3cSJohn Dyson struct image_params *imgp; 6521616db3cSJohn Dyson { 653d8aad40cSJohn Baldwin int rv, i; 65495461b45SJohn Dyson int initial_pagein; 65595461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 6561616db3cSJohn Dyson vm_object_t object; 6571616db3cSJohn Dyson 6580cddd8f0SMatthew Dillon GIANT_REQUIRED; 6591616db3cSJohn Dyson 6601616db3cSJohn Dyson if (imgp->firstpage) { 6611616db3cSJohn Dyson exec_unmap_first_page(imgp); 6621616db3cSJohn Dyson } 6631616db3cSJohn Dyson 6649ff5ce6bSBoris Popov VOP_GETVOBJECT(imgp->vp, &object); 6651616db3cSJohn Dyson 66695461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 6671616db3cSJohn Dyson 66895461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 66995461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 67095461b45SJohn Dyson if (initial_pagein > object->size) 67195461b45SJohn Dyson initial_pagein = object->size; 67295461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 673d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 67495461b45SJohn Dyson if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) 67595461b45SJohn Dyson break; 67695461b45SJohn Dyson if (ma[i]->valid) 67795461b45SJohn Dyson break; 678e69763a3SDoug Rabson vm_page_busy(ma[i]); 67995461b45SJohn Dyson } else { 680ca0387efSJake Burkholder ma[i] = vm_page_alloc(object, i, 681ca0387efSJake Burkholder VM_ALLOC_NORMAL); 68295461b45SJohn Dyson if (ma[i] == NULL) 68395461b45SJohn Dyson break; 68495461b45SJohn Dyson } 68595461b45SJohn Dyson } 68695461b45SJohn Dyson initial_pagein = i; 6871616db3cSJohn Dyson 68895461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 68995461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 69095461b45SJohn Dyson 691ca0387efSJake Burkholder if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || 692ca0387efSJake Burkholder (ma[0]->valid == 0)) { 693ecbb00a2SDoug Rabson if (ma[0]) { 69497646f56SAlan Cox vm_page_lock_queues(); 69595461b45SJohn Dyson vm_page_protect(ma[0], VM_PROT_NONE); 6968f9110f6SJohn Dyson vm_page_free(ma[0]); 69797646f56SAlan Cox vm_page_unlock_queues(); 698ecbb00a2SDoug Rabson } 699a7cddfedSJake Burkholder return (EIO); 7001616db3cSJohn Dyson } 7011616db3cSJohn Dyson } 70297646f56SAlan Cox vm_page_lock_queues(); 70395461b45SJohn Dyson vm_page_wire(ma[0]); 704e69763a3SDoug Rabson vm_page_wakeup(ma[0]); 70597646f56SAlan Cox vm_page_unlock_queues(); 7061616db3cSJohn Dyson 707ac59490bSJake Burkholder pmap_qenter((vm_offset_t)imgp->image_header, ma, 1); 70895461b45SJohn Dyson imgp->firstpage = ma[0]; 7091616db3cSJohn Dyson 710a7cddfedSJake Burkholder return (0); 7111616db3cSJohn Dyson } 7121616db3cSJohn Dyson 7131616db3cSJohn Dyson void 7141616db3cSJohn Dyson exec_unmap_first_page(imgp) 7151616db3cSJohn Dyson struct image_params *imgp; 7161616db3cSJohn Dyson { 7170cddd8f0SMatthew Dillon GIANT_REQUIRED; 71823955314SAlfred Perlstein 7191616db3cSJohn Dyson if (imgp->firstpage) { 720ac59490bSJake Burkholder pmap_qremove((vm_offset_t)imgp->image_header, 1); 72197646f56SAlan Cox vm_page_lock_queues(); 72273007561SDavid Greenman vm_page_unwire(imgp->firstpage, 1); 72397646f56SAlan Cox vm_page_unlock_queues(); 7241616db3cSJohn Dyson imgp->firstpage = NULL; 7251616db3cSJohn Dyson } 7261616db3cSJohn Dyson } 7271616db3cSJohn Dyson 72826f9a767SRodney W. Grimes /* 72926f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 73026f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 73126f9a767SRodney W. Grimes * automatically in trap.c. 73226f9a767SRodney W. Grimes */ 73326f9a767SRodney W. Grimes int 73405ba50f5SJake Burkholder exec_new_vmspace(imgp, sv) 735c52007c2SDavid Greenman struct image_params *imgp; 73605ba50f5SJake Burkholder struct sysentvec *sv; 73726f9a767SRodney W. Grimes { 73826f9a767SRodney W. Grimes int error; 7396f5dafeaSAlan Cox struct execlist *ep; 7405e20c11fSAlan Cox struct proc *p = imgp->proc; 7415e20c11fSAlan Cox struct vmspace *vmspace = p->p_vmspace; 74205ba50f5SJake Burkholder vm_offset_t stack_addr; 74305ba50f5SJake Burkholder vm_map_t map; 74426f9a767SRodney W. Grimes 7450cddd8f0SMatthew Dillon GIANT_REQUIRED; 7460cddd8f0SMatthew Dillon 74705ba50f5SJake Burkholder stack_addr = sv->sv_usrstack - maxssiz; 7483ebc1248SPeter Wemm 749c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 75026f9a767SRodney W. Grimes 751af9ec885SJohn Dyson /* 7526f5dafeaSAlan Cox * Perform functions registered with at_exec(). 7536f5dafeaSAlan Cox */ 7546f5dafeaSAlan Cox TAILQ_FOREACH(ep, &exec_list, next) 7555e20c11fSAlan Cox (*ep->function)(p); 7566f5dafeaSAlan Cox 7576f5dafeaSAlan Cox /* 758af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 759af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 760af9ec885SJohn Dyson * not disrupted 761af9ec885SJohn Dyson */ 76205ba50f5SJake Burkholder map = &vmspace->vm_map; 76305ba50f5SJake Burkholder if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser && 76405ba50f5SJake Burkholder vm_map_max(map) == sv->sv_maxuser) { 7653d903220SDoug Rabson if (vmspace->vm_shm) 7665e20c11fSAlan Cox shmexit(p); 76705ba50f5SJake Burkholder pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map), 76805ba50f5SJake Burkholder vm_map_max(map)); 76905ba50f5SJake Burkholder vm_map_remove(map, vm_map_min(map), vm_map_max(map)); 770af9ec885SJohn Dyson } else { 77105ba50f5SJake Burkholder vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser); 7725e20c11fSAlan Cox vmspace = p->p_vmspace; 77305ba50f5SJake Burkholder map = &vmspace->vm_map; 774af9ec885SJohn Dyson } 77526f9a767SRodney W. Grimes 77626f9a767SRodney W. Grimes /* Allocate a new stack */ 77705ba50f5SJake Burkholder error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz, 77805ba50f5SJake Burkholder sv->sv_stackprot, VM_PROT_ALL, 0); 7792267af78SJulian Elischer if (error) 7802267af78SJulian Elischer return (error); 7812267af78SJulian Elischer 78263c47a5cSDoug Rabson #ifdef __ia64__ 78363c47a5cSDoug Rabson { 78463c47a5cSDoug Rabson /* 78563c47a5cSDoug Rabson * Allocate backing store. We really need something 78663c47a5cSDoug Rabson * similar to vm_map_stack which can allow the backing 78763c47a5cSDoug Rabson * store to grow upwards. This will do for now. 78863c47a5cSDoug Rabson */ 78963c47a5cSDoug Rabson vm_offset_t bsaddr; 79005ba50f5SJake Burkholder bsaddr = p->p_sysent->sv_usrstack - 2 * maxssiz; 79105ba50f5SJake Burkholder error = vm_map_find(map, 0, 0, &bsaddr, 79281f223caSJake Burkholder regstkpages * PAGE_SIZE, 0, VM_PROT_ALL, VM_PROT_ALL, 0); 7935e20c11fSAlan Cox FIRST_THREAD_IN_PROC(p)->td_md.md_bspstore = bsaddr; 79463c47a5cSDoug Rabson } 79563c47a5cSDoug Rabson #endif 79663c47a5cSDoug Rabson 7972267af78SJulian Elischer /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 7982267af78SJulian Elischer * VM_STACK case, but they are still used to monitor the size of the 7992267af78SJulian Elischer * process stack so we can check the stack rlimit. 8002267af78SJulian Elischer */ 801cbc89bfbSPaul Saab vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 80205ba50f5SJake Burkholder vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz; 80326f9a767SRodney W. Grimes 80426f9a767SRodney W. Grimes return (0); 80526f9a767SRodney W. Grimes } 80626f9a767SRodney W. Grimes 80726f9a767SRodney W. Grimes /* 80826f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 80926f9a767SRodney W. Grimes * address space into the temporary string buffer. 81026f9a767SRodney W. Grimes */ 81126f9a767SRodney W. Grimes int 812c52007c2SDavid Greenman exec_extract_strings(imgp) 813c52007c2SDavid Greenman struct image_params *imgp; 81426f9a767SRodney W. Grimes { 81526f9a767SRodney W. Grimes char **argv, **envv; 81626f9a767SRodney W. Grimes char *argp, *envp; 817ecbb00a2SDoug Rabson int error; 818ecbb00a2SDoug Rabson size_t length; 81926f9a767SRodney W. Grimes 82026f9a767SRodney W. Grimes /* 82126f9a767SRodney W. Grimes * extract arguments first 82226f9a767SRodney W. Grimes */ 82326f9a767SRodney W. Grimes 824450ffb44SRobert Watson argv = imgp->userspace_argv; 82526f9a767SRodney W. Grimes 8260fd1a014SDavid Greenman if (argv) { 827aae0aa45SBruce Evans argp = (caddr_t)(intptr_t)fuword(argv); 8285cf3d12cSAndrey A. Chernov if (argp == (caddr_t)-1) 8295cf3d12cSAndrey A. Chernov return (EFAULT); 8305cf3d12cSAndrey A. Chernov if (argp) 8315cf3d12cSAndrey A. Chernov argv++; 8325cf3d12cSAndrey A. Chernov if (imgp->argv0) 8335cf3d12cSAndrey A. Chernov argp = imgp->argv0; 8345cf3d12cSAndrey A. Chernov if (argp) { 8355cf3d12cSAndrey A. Chernov do { 83626f9a767SRodney W. Grimes if (argp == (caddr_t)-1) 83726f9a767SRodney W. Grimes return (EFAULT); 838c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 839c52007c2SDavid Greenman imgp->stringspace, &length))) { 8400fd1a014SDavid Greenman if (error == ENAMETOOLONG) 84126f9a767SRodney W. Grimes return (E2BIG); 8420fd1a014SDavid Greenman return (error); 8430fd1a014SDavid Greenman } 844c52007c2SDavid Greenman imgp->stringspace -= length; 845c52007c2SDavid Greenman imgp->stringp += length; 846c52007c2SDavid Greenman imgp->argc++; 847aae0aa45SBruce Evans } while ((argp = (caddr_t)(intptr_t)fuword(argv++))); 84826f9a767SRodney W. Grimes } 8490fd1a014SDavid Greenman } 85026f9a767SRodney W. Grimes 851b9df5231SPoul-Henning Kamp imgp->endargs = imgp->stringp; 852b9df5231SPoul-Henning Kamp 85326f9a767SRodney W. Grimes /* 85426f9a767SRodney W. Grimes * extract environment strings 85526f9a767SRodney W. Grimes */ 85626f9a767SRodney W. Grimes 857450ffb44SRobert Watson envv = imgp->userspace_envv; 85826f9a767SRodney W. Grimes 8590fd1a014SDavid Greenman if (envv) { 860aae0aa45SBruce Evans while ((envp = (caddr_t)(intptr_t)fuword(envv++))) { 86126f9a767SRodney W. Grimes if (envp == (caddr_t)-1) 86226f9a767SRodney W. Grimes return (EFAULT); 863c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 864c52007c2SDavid Greenman imgp->stringspace, &length))) { 8650fd1a014SDavid Greenman if (error == ENAMETOOLONG) 86626f9a767SRodney W. Grimes return (E2BIG); 8670fd1a014SDavid Greenman return (error); 8680fd1a014SDavid Greenman } 869c52007c2SDavid Greenman imgp->stringspace -= length; 870c52007c2SDavid Greenman imgp->stringp += length; 871c52007c2SDavid Greenman imgp->envc++; 87226f9a767SRodney W. Grimes } 8730fd1a014SDavid Greenman } 87426f9a767SRodney W. Grimes 87526f9a767SRodney W. Grimes return (0); 87626f9a767SRodney W. Grimes } 87726f9a767SRodney W. Grimes 87826f9a767SRodney W. Grimes /* 87926f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 88026f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 88126f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 88226f9a767SRodney W. Grimes */ 883654f6be1SBruce Evans register_t * 884c52007c2SDavid Greenman exec_copyout_strings(imgp) 885c52007c2SDavid Greenman struct image_params *imgp; 88626f9a767SRodney W. Grimes { 88726f9a767SRodney W. Grimes int argc, envc; 88826f9a767SRodney W. Grimes char **vectp; 88926f9a767SRodney W. Grimes char *stringp, *destp; 890654f6be1SBruce Evans register_t *stack_base; 89193f6448cSDavid Greenman struct ps_strings *arginfo; 892f3bec5d7SJake Burkholder struct proc *p; 893d66a5066SPeter Wemm int szsigcode; 89426f9a767SRodney W. Grimes 89526f9a767SRodney W. Grimes /* 89626f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 897d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 89826f9a767SRodney W. Grimes */ 899f3bec5d7SJake Burkholder p = imgp->proc; 900f3bec5d7SJake Burkholder szsigcode = 0; 90105ba50f5SJake Burkholder arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 902f3bec5d7SJake Burkholder if (p->p_sysent->sv_szsigcode != NULL) 903f3bec5d7SJake Burkholder szsigcode = *(p->p_sysent->sv_szsigcode); 904d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 9051ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 9061ed012f9SPeter Wemm 90726f9a767SRodney W. Grimes /* 908d66a5066SPeter Wemm * install sigcode 909d66a5066SPeter Wemm */ 910d66a5066SPeter Wemm if (szsigcode) 911f3bec5d7SJake Burkholder copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo - 912f3bec5d7SJake Burkholder szsigcode), szsigcode); 913d66a5066SPeter Wemm 914d66a5066SPeter Wemm /* 915e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 916e1743d02SSøren Schmidt * on the stack. 917e1743d02SSøren Schmidt */ 918b9a22da4STakanori Watanabe if (imgp->auxargs) { 919e1743d02SSøren Schmidt /* 920b9a22da4STakanori Watanabe * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 921b9a22da4STakanori Watanabe * lower compatibility. 922b9a22da4STakanori Watanabe */ 923ca0387efSJake Burkholder imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size : 924ca0387efSJake Burkholder (AT_COUNT * 2); 925b9a22da4STakanori Watanabe /* 926b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 927b9a22da4STakanori Watanabe * the arg and env vector sets,and imgp->auxarg_size is room 928b9a22da4STakanori Watanabe * for argument of Runtime loader. 929e1743d02SSøren Schmidt */ 930e1743d02SSøren Schmidt vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 + 931b9a22da4STakanori Watanabe imgp->auxarg_size) * sizeof(char *)); 932b9a22da4STakanori Watanabe 933b9a22da4STakanori Watanabe } else 934e1743d02SSøren Schmidt /* 935b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 936b9a22da4STakanori Watanabe * the arg and env vector sets 93726f9a767SRodney W. Grimes */ 938ca0387efSJake Burkholder vectp = (char **)(destp - (imgp->argc + imgp->envc + 2) * 939ca0387efSJake Burkholder sizeof(char *)); 94026f9a767SRodney W. Grimes 94126f9a767SRodney W. Grimes /* 94226f9a767SRodney W. Grimes * vectp also becomes our initial stack base 94326f9a767SRodney W. Grimes */ 944654f6be1SBruce Evans stack_base = (register_t *)vectp; 94526f9a767SRodney W. Grimes 946c52007c2SDavid Greenman stringp = imgp->stringbase; 947c52007c2SDavid Greenman argc = imgp->argc; 948c52007c2SDavid Greenman envc = imgp->envc; 94926f9a767SRodney W. Grimes 95093f6448cSDavid Greenman /* 9513aed948bSDavid Greenman * Copy out strings - arguments and environment. 95293f6448cSDavid Greenman */ 953c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 95493f6448cSDavid Greenman 95593f6448cSDavid Greenman /* 9563aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 9573aed948bSDavid Greenman */ 958aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 9593aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 9603aed948bSDavid Greenman 9613aed948bSDavid Greenman /* 9623aed948bSDavid Greenman * Fill in argument portion of vector table. 96393f6448cSDavid Greenman */ 96426f9a767SRodney W. Grimes for (; argc > 0; --argc) { 965aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 9663aed948bSDavid Greenman while (*stringp++ != 0) 9673aed948bSDavid Greenman destp++; 9683aed948bSDavid Greenman destp++; 96926f9a767SRodney W. Grimes } 97026f9a767SRodney W. Grimes 9711a6e52d0SJeroen Ruigrok van der Werven /* a null vector table pointer separates the argp's from the envp's */ 9726ab46d52SBruce Evans suword(vectp++, 0); 97326f9a767SRodney W. Grimes 974aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 9753aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 97693f6448cSDavid Greenman 97793f6448cSDavid Greenman /* 9783aed948bSDavid Greenman * Fill in environment portion of vector table. 97993f6448cSDavid Greenman */ 98026f9a767SRodney W. Grimes for (; envc > 0; --envc) { 981aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 9823aed948bSDavid Greenman while (*stringp++ != 0) 9833aed948bSDavid Greenman destp++; 9843aed948bSDavid Greenman destp++; 98526f9a767SRodney W. Grimes } 98626f9a767SRodney W. Grimes 98726f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 9886ab46d52SBruce Evans suword(vectp, 0); 98926f9a767SRodney W. Grimes 99026f9a767SRodney W. Grimes return (stack_base); 99126f9a767SRodney W. Grimes } 99226f9a767SRodney W. Grimes 99326f9a767SRodney W. Grimes /* 99426f9a767SRodney W. Grimes * Check permissions of file to execute. 995cf64863aSRobert Watson * Called with imgp->vp locked. 99626f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 99726f9a767SRodney W. Grimes */ 998c8a79999SPeter Wemm int 999c52007c2SDavid Greenman exec_check_permissions(imgp) 1000c52007c2SDavid Greenman struct image_params *imgp; 100126f9a767SRodney W. Grimes { 1002c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 1003c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 1004a854ed98SJohn Baldwin struct thread *td; 100526f9a767SRodney W. Grimes int error; 100626f9a767SRodney W. Grimes 1007a854ed98SJohn Baldwin td = curthread; /* XXXKSE */ 1008339b79b9SRobert Watson 1009339b79b9SRobert Watson #ifdef MAC 1010339b79b9SRobert Watson error = mac_check_vnode_exec(td->td_ucred, imgp->vp); 1011339b79b9SRobert Watson if (error) 1012339b79b9SRobert Watson return (error); 1013339b79b9SRobert Watson #endif 1014339b79b9SRobert Watson 101526f9a767SRodney W. Grimes /* Get file attributes */ 1016a854ed98SJohn Baldwin error = VOP_GETATTR(vp, attr, td->td_ucred, td); 101726f9a767SRodney W. Grimes if (error) 101826f9a767SRodney W. Grimes return (error); 101926f9a767SRodney W. Grimes 102026f9a767SRodney W. Grimes /* 102126f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 102226f9a767SRodney W. Grimes * file resides on. 102326f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 102426f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 102526f9a767SRodney W. Grimes * file really is executable. 102626f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 102726f9a767SRodney W. Grimes */ 1028c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 102926f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 1030a854ed98SJohn Baldwin (attr->va_type != VREG)) 103126f9a767SRodney W. Grimes return (EACCES); 103226f9a767SRodney W. Grimes 103326f9a767SRodney W. Grimes /* 103426f9a767SRodney W. Grimes * Zero length files can't be exec'd 103526f9a767SRodney W. Grimes */ 103626f9a767SRodney W. Grimes if (attr->va_size == 0) 103726f9a767SRodney W. Grimes return (ENOEXEC); 103826f9a767SRodney W. Grimes 103926f9a767SRodney W. Grimes /* 104026f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 104126f9a767SRodney W. Grimes */ 1042a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 104326f9a767SRodney W. Grimes if (error) 104426f9a767SRodney W. Grimes return (error); 104526f9a767SRodney W. Grimes 10466d5a0a8cSDavid Greenman /* 10476d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 10486d5a0a8cSDavid Greenman * if there are any. 10496d5a0a8cSDavid Greenman */ 10506d5a0a8cSDavid Greenman if (vp->v_writecount) 10516d5a0a8cSDavid Greenman return (ETXTBSY); 10526d5a0a8cSDavid Greenman 10536d5a0a8cSDavid Greenman /* 10546d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 10556d5a0a8cSDavid Greenman * general case). 10566d5a0a8cSDavid Greenman */ 1057a854ed98SJohn Baldwin error = VOP_OPEN(vp, FREAD, td->td_ucred, td); 105826f9a767SRodney W. Grimes return (error); 1059df8bae1dSRodney W. Grimes } 1060aa855a59SPeter Wemm 1061aa855a59SPeter Wemm /* 1062aa855a59SPeter Wemm * Exec handler registration 1063aa855a59SPeter Wemm */ 1064aa855a59SPeter Wemm int 1065aa855a59SPeter Wemm exec_register(execsw_arg) 1066aa855a59SPeter Wemm const struct execsw *execsw_arg; 1067aa855a59SPeter Wemm { 1068aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1069aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 1070aa855a59SPeter Wemm 1071aa855a59SPeter Wemm if (execsw) 1072aa855a59SPeter Wemm for (es = execsw; *es; es++) 1073aa855a59SPeter Wemm count++; 1074aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1075aa855a59SPeter Wemm if (newexecsw == NULL) 1076a7cddfedSJake Burkholder return (ENOMEM); 1077aa855a59SPeter Wemm xs = newexecsw; 1078aa855a59SPeter Wemm if (execsw) 1079aa855a59SPeter Wemm for (es = execsw; *es; es++) 1080aa855a59SPeter Wemm *xs++ = *es; 1081aa855a59SPeter Wemm *xs++ = execsw_arg; 1082aa855a59SPeter Wemm *xs = NULL; 1083aa855a59SPeter Wemm if (execsw) 1084aa855a59SPeter Wemm free(execsw, M_TEMP); 1085aa855a59SPeter Wemm execsw = newexecsw; 1086a7cddfedSJake Burkholder return (0); 1087aa855a59SPeter Wemm } 1088aa855a59SPeter Wemm 1089aa855a59SPeter Wemm int 1090aa855a59SPeter Wemm exec_unregister(execsw_arg) 1091aa855a59SPeter Wemm const struct execsw *execsw_arg; 1092aa855a59SPeter Wemm { 1093aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1094aa855a59SPeter Wemm int count = 1; 1095aa855a59SPeter Wemm 1096aa855a59SPeter Wemm if (execsw == NULL) 1097aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 1098aa855a59SPeter Wemm 1099aa855a59SPeter Wemm for (es = execsw; *es; es++) { 1100aa855a59SPeter Wemm if (*es == execsw_arg) 1101aa855a59SPeter Wemm break; 1102aa855a59SPeter Wemm } 1103aa855a59SPeter Wemm if (*es == NULL) 1104a7cddfedSJake Burkholder return (ENOENT); 1105aa855a59SPeter Wemm for (es = execsw; *es; es++) 1106aa855a59SPeter Wemm if (*es != execsw_arg) 1107aa855a59SPeter Wemm count++; 1108aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1109aa855a59SPeter Wemm if (newexecsw == NULL) 1110a7cddfedSJake Burkholder return (ENOMEM); 1111aa855a59SPeter Wemm xs = newexecsw; 1112aa855a59SPeter Wemm for (es = execsw; *es; es++) 1113aa855a59SPeter Wemm if (*es != execsw_arg) 1114aa855a59SPeter Wemm *xs++ = *es; 1115aa855a59SPeter Wemm *xs = NULL; 1116aa855a59SPeter Wemm if (execsw) 1117aa855a59SPeter Wemm free(execsw, M_TEMP); 1118aa855a59SPeter Wemm execsw = newexecsw; 1119a7cddfedSJake Burkholder return (0); 1120aa855a59SPeter Wemm } 112121d56e9cSAlfred Perlstein 112221d56e9cSAlfred Perlstein int 112321d56e9cSAlfred Perlstein at_exec(function) 112421d56e9cSAlfred Perlstein execlist_fn function; 112521d56e9cSAlfred Perlstein { 112621d56e9cSAlfred Perlstein struct execlist *ep; 112721d56e9cSAlfred Perlstein 112821d56e9cSAlfred Perlstein #ifdef INVARIANTS 112921d56e9cSAlfred Perlstein /* Be noisy if the programmer has lost track of things */ 113021d56e9cSAlfred Perlstein if (rm_at_exec(function)) 113121d56e9cSAlfred Perlstein printf("WARNING: exec callout entry (%p) already present\n", 113221d56e9cSAlfred Perlstein function); 113321d56e9cSAlfred Perlstein #endif 113421d56e9cSAlfred Perlstein ep = malloc(sizeof(*ep), M_ATEXEC, M_NOWAIT); 113521d56e9cSAlfred Perlstein if (ep == NULL) 113621d56e9cSAlfred Perlstein return (ENOMEM); 113721d56e9cSAlfred Perlstein ep->function = function; 113821d56e9cSAlfred Perlstein TAILQ_INSERT_TAIL(&exec_list, ep, next); 113921d56e9cSAlfred Perlstein return (0); 114021d56e9cSAlfred Perlstein } 114121d56e9cSAlfred Perlstein 114221d56e9cSAlfred Perlstein /* 114321d56e9cSAlfred Perlstein * Scan the exec callout list for the given item and remove it. 114421d56e9cSAlfred Perlstein * Returns the number of items removed (0 or 1) 114521d56e9cSAlfred Perlstein */ 114621d56e9cSAlfred Perlstein int 114721d56e9cSAlfred Perlstein rm_at_exec(function) 114821d56e9cSAlfred Perlstein execlist_fn function; 114921d56e9cSAlfred Perlstein { 115021d56e9cSAlfred Perlstein struct execlist *ep; 115121d56e9cSAlfred Perlstein 115221d56e9cSAlfred Perlstein TAILQ_FOREACH(ep, &exec_list, next) { 115321d56e9cSAlfred Perlstein if (ep->function == function) { 115421d56e9cSAlfred Perlstein TAILQ_REMOVE(&exec_list, ep, next); 115521d56e9cSAlfred Perlstein free(ep, M_ATEXEC); 115621d56e9cSAlfred Perlstein return (1); 115721d56e9cSAlfred Perlstein } 115821d56e9cSAlfred Perlstein } 115921d56e9cSAlfred Perlstein return (0); 116021d56e9cSAlfred Perlstein } 1161