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); 7905ba50f5SJake Burkholder 8021d56e9cSAlfred Perlstein /* 8121d56e9cSAlfred Perlstein * callout list for things to do at exec time 8221d56e9cSAlfred Perlstein */ 8321d56e9cSAlfred Perlstein struct execlist { 8421d56e9cSAlfred Perlstein execlist_fn function; 8521d56e9cSAlfred Perlstein TAILQ_ENTRY(execlist) next; 8621d56e9cSAlfred Perlstein }; 8721d56e9cSAlfred Perlstein 8821d56e9cSAlfred Perlstein TAILQ_HEAD(exec_list_head, execlist); 8921d56e9cSAlfred Perlstein static struct exec_list_head exec_list = TAILQ_HEAD_INITIALIZER(exec_list); 9021d56e9cSAlfred Perlstein 919701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 9205ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_PS_STRINGS, ps_strings, CTLTYPE_ULONG|CTLFLAG_RD, 9305ba50f5SJake Burkholder NULL, 0, sysctl_kern_ps_strings, "LU", ""); 94486bddb0SDoug Rabson 959701cd40SJohn Baldwin /* XXX This should be vm_size_t. */ 9605ba50f5SJake Burkholder SYSCTL_PROC(_kern, KERN_USRSTACK, usrstack, CTLTYPE_ULONG|CTLFLAG_RD, 9705ba50f5SJake Burkholder NULL, 0, sysctl_kern_usrstack, "LU", ""); 9899ac3bc8SPeter Wemm 99b9df5231SPoul-Henning Kamp u_long ps_arg_cache_limit = PAGE_SIZE / 16; 100c3699b5fSPeter Wemm SYSCTL_ULONG(_kern, OID_AUTO, ps_arg_cache_limit, CTLFLAG_RW, 1019701cd40SJohn Baldwin &ps_arg_cache_limit, 0, ""); 102b9df5231SPoul-Henning Kamp 103a8704f89SPoul-Henning Kamp int ps_argsopen = 1; 104a8704f89SPoul-Henning Kamp SYSCTL_INT(_kern, OID_AUTO, ps_argsopen, CTLFLAG_RW, &ps_argsopen, 0, ""); 105a8704f89SPoul-Henning Kamp 106911fc923SPeter Wemm #ifdef __ia64__ 107911fc923SPeter Wemm /* XXX HACK */ 108911fc923SPeter Wemm static int regstkpages = 256; 109911fc923SPeter Wemm SYSCTL_INT(_machdep, OID_AUTO, regstkpages, CTLFLAG_RW, ®stkpages, 0, ""); 110911fc923SPeter Wemm #endif 111911fc923SPeter Wemm 11205ba50f5SJake Burkholder static int 11305ba50f5SJake Burkholder sysctl_kern_ps_strings(SYSCTL_HANDLER_ARGS) 11405ba50f5SJake Burkholder { 11505ba50f5SJake Burkholder struct proc *p; 11605ba50f5SJake Burkholder 11705ba50f5SJake Burkholder p = curproc; 11805ba50f5SJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_psstrings, 11905ba50f5SJake Burkholder sizeof(p->p_sysent->sv_psstrings))); 12005ba50f5SJake Burkholder } 12105ba50f5SJake Burkholder 12205ba50f5SJake Burkholder static int 12305ba50f5SJake Burkholder sysctl_kern_usrstack(SYSCTL_HANDLER_ARGS) 12405ba50f5SJake Burkholder { 12505ba50f5SJake Burkholder struct proc *p; 12605ba50f5SJake Burkholder 12705ba50f5SJake Burkholder p = curproc; 12805ba50f5SJake Burkholder return (SYSCTL_OUT(req, &p->p_sysent->sv_usrstack, 12905ba50f5SJake Burkholder sizeof(p->p_sysent->sv_usrstack))); 13005ba50f5SJake Burkholder } 13105ba50f5SJake Burkholder 13299ac3bc8SPeter Wemm /* 133aa855a59SPeter Wemm * Each of the items is a pointer to a `const struct execsw', hence the 134aa855a59SPeter Wemm * double pointer here. 135df8bae1dSRodney W. Grimes */ 136aa855a59SPeter Wemm static const struct execsw **execsw; 13726f9a767SRodney W. Grimes 138d2d3e875SBruce Evans #ifndef _SYS_SYSPROTO_H_ 139ad7507e2SSteven Wallace struct execve_args { 140ad7507e2SSteven Wallace char *fname; 141ad7507e2SSteven Wallace char **argv; 142ad7507e2SSteven Wallace char **envv; 143ad7507e2SSteven Wallace }; 144d2d3e875SBruce Evans #endif 145ad7507e2SSteven Wallace 14626f9a767SRodney W. Grimes /* 14726f9a767SRodney W. Grimes * execve() system call. 148116734c4SMatthew Dillon * 149116734c4SMatthew Dillon * MPSAFE 15026f9a767SRodney W. Grimes */ 15126f9a767SRodney W. Grimes int 152b40ce416SJulian Elischer execve(td, uap) 153b40ce416SJulian Elischer struct thread *td; 15426f9a767SRodney W. Grimes register struct execve_args *uap; 155df8bae1dSRodney W. Grimes { 156b40ce416SJulian Elischer struct proc *p = td->td_proc; 15726f9a767SRodney W. Grimes struct nameidata nd, *ndp; 1589b3b1c5fSJohn Baldwin struct ucred *newcred = NULL, *oldcred; 1591419eacbSAlfred Perlstein struct uidinfo *euip; 160654f6be1SBruce Evans register_t *stack_base; 161bb56ec4aSPoul-Henning Kamp int error, len, i; 162c52007c2SDavid Greenman struct image_params image_params, *imgp; 16326f9a767SRodney W. Grimes struct vattr attr; 1644d77a549SAlfred Perlstein int (*img_first)(struct image_params *); 16569be5db9SAlfred Perlstein struct pargs *oldargs = NULL, *newargs = NULL; 1669b3b1c5fSJohn Baldwin struct procsig *oldprocsig, *newprocsig; 1676c84de02SJohn Baldwin #ifdef KTRACE 1686c84de02SJohn Baldwin struct vnode *tracevp = NULL; 1696c84de02SJohn Baldwin #endif 1706c84de02SJohn Baldwin struct vnode *textvp = NULL; 171d06c0d4dSRobert Watson int credential_changing; 172619eb6e5SJeff Roberson int textset; 17326f9a767SRodney W. Grimes 174c52007c2SDavid Greenman imgp = &image_params; 175df8bae1dSRodney W. Grimes 1769ca45e81SDag-Erling Smørgrav /* 1779ca45e81SDag-Erling Smørgrav * Lock the process and set the P_INEXEC flag to indicate that 1789ca45e81SDag-Erling Smørgrav * it should be left alone until we're done here. This is 1799ca45e81SDag-Erling Smørgrav * necessary to avoid race conditions - e.g. in ptrace() - 1809ca45e81SDag-Erling Smørgrav * that might allow a local user to illicitly obtain elevated 1819ca45e81SDag-Erling Smørgrav * privileges. 1829ca45e81SDag-Erling Smørgrav */ 1839ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 1849ca45e81SDag-Erling Smørgrav KASSERT((p->p_flag & P_INEXEC) == 0, 18591f91617SDavid E. O'Brien ("%s(): process already has P_INEXEC flag", __func__)); 18649539972SJulian Elischer if (p->p_flag & P_KSES) { 1871279572aSDavid Xu if (thread_single(SINGLE_EXIT)) { 188e602ba25SJulian Elischer PROC_UNLOCK(p); 189e602ba25SJulian Elischer return (ERESTART); /* Try again later. */ 190e602ba25SJulian Elischer } 19149539972SJulian Elischer /* 19249539972SJulian Elischer * If we get here all other threads are dead, 19349539972SJulian Elischer * so unset the associated flags and lose KSE mode. 19449539972SJulian Elischer */ 19549539972SJulian Elischer p->p_flag &= ~P_KSES; 19649539972SJulian Elischer td->td_flags &= ~TDF_UNBOUND; 19749539972SJulian Elischer thread_single_end(); 19849539972SJulian Elischer } 1999ca45e81SDag-Erling Smørgrav p->p_flag |= P_INEXEC; 2009ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 2019ca45e81SDag-Erling Smørgrav 202df8bae1dSRodney W. Grimes /* 203c52007c2SDavid Greenman * Initialize part of the common data 204df8bae1dSRodney W. Grimes */ 205c52007c2SDavid Greenman imgp->proc = p; 206c52007c2SDavid Greenman imgp->uap = uap; 207c52007c2SDavid Greenman imgp->attr = &attr; 208c52007c2SDavid Greenman imgp->argc = imgp->envc = 0; 2095cf3d12cSAndrey A. Chernov imgp->argv0 = NULL; 210c52007c2SDavid Greenman imgp->entry_addr = 0; 211c52007c2SDavid Greenman imgp->vmspace_destroyed = 0; 212c52007c2SDavid Greenman imgp->interpreted = 0; 213c52007c2SDavid Greenman imgp->interpreter_name[0] = '\0'; 214e1743d02SSøren Schmidt imgp->auxargs = NULL; 2151616db3cSJohn Dyson imgp->vp = NULL; 2160b2ed1aeSJeff Roberson imgp->object = NULL; 2171616db3cSJohn Dyson imgp->firstpage = NULL; 2184fe88fe6SJohn Polstra imgp->ps_strings = 0; 219b9a22da4STakanori Watanabe imgp->auxarg_size = 0; 22026f9a767SRodney W. Grimes 22126f9a767SRodney W. Grimes /* 22226f9a767SRodney W. Grimes * Allocate temporary demand zeroed space for argument and 22326f9a767SRodney W. Grimes * environment strings 22426f9a767SRodney W. Grimes */ 225ca0387efSJake Burkholder imgp->stringbase = (char *)kmem_alloc_wait(exec_map, ARG_MAX + 226ca0387efSJake Burkholder PAGE_SIZE); 227c2f9f36bSDavid Greenman if (imgp->stringbase == NULL) { 22826f9a767SRodney W. Grimes error = ENOMEM; 229b3afd20dSAlan Cox mtx_lock(&Giant); 23026f9a767SRodney W. Grimes goto exec_fail; 23126f9a767SRodney W. Grimes } 232c52007c2SDavid Greenman imgp->stringp = imgp->stringbase; 233c52007c2SDavid Greenman imgp->stringspace = ARG_MAX; 2341616db3cSJohn Dyson imgp->image_header = imgp->stringbase + ARG_MAX; 23526f9a767SRodney W. Grimes 23626f9a767SRodney W. Grimes /* 23726f9a767SRodney W. Grimes * Translate the file name. namei() returns a vnode pointer 23826f9a767SRodney W. Grimes * in ni_vp amoung other things. 23926f9a767SRodney W. Grimes */ 24026f9a767SRodney W. Grimes ndp = &nd; 241b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 242b40ce416SJulian Elischer UIO_USERSPACE, uap->fname, td); 24326f9a767SRodney W. Grimes 244b3afd20dSAlan Cox mtx_lock(&Giant); 24526f9a767SRodney W. Grimes interpret: 24626f9a767SRodney W. Grimes 24726f9a767SRodney W. Grimes error = namei(ndp); 24826f9a767SRodney W. Grimes if (error) { 2491616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 2501616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 25126f9a767SRodney W. Grimes goto exec_fail; 25226f9a767SRodney W. Grimes } 25326f9a767SRodney W. Grimes 254c52007c2SDavid Greenman imgp->vp = ndp->ni_vp; 2559c0fed3dSDoug Rabson imgp->fname = uap->fname; 25626f9a767SRodney W. Grimes 25726f9a767SRodney W. Grimes /* 2589022e4adSDavid Greenman * Check file permissions (also 'opens' file) 2599022e4adSDavid Greenman */ 260c52007c2SDavid Greenman error = exec_check_permissions(imgp); 261619eb6e5SJeff Roberson if (error) 26226f9a767SRodney W. Grimes goto exec_fail_dealloc; 263619eb6e5SJeff Roberson 264619eb6e5SJeff Roberson if (VOP_GETVOBJECT(imgp->vp, &imgp->object) == 0) 2650b2ed1aeSJeff Roberson vm_object_reference(imgp->object); 26626f9a767SRodney W. Grimes 267619eb6e5SJeff Roberson /* 268619eb6e5SJeff Roberson * Set VV_TEXT now so no one can write to the executable while we're 269619eb6e5SJeff Roberson * activating it. 270619eb6e5SJeff Roberson * 271619eb6e5SJeff Roberson * Remember if this was set before and unset it in case this is not 272619eb6e5SJeff Roberson * actually an executable image. 273619eb6e5SJeff Roberson */ 274619eb6e5SJeff Roberson textset = imgp->vp->v_vflag & VV_TEXT; 275619eb6e5SJeff Roberson imgp->vp->v_vflag |= VV_TEXT; 276619eb6e5SJeff Roberson 2771616db3cSJohn Dyson error = exec_map_first_page(imgp); 2786d5a0a8cSDavid Greenman if (error) 27926f9a767SRodney W. Grimes goto exec_fail_dealloc; 28026f9a767SRodney W. Grimes 28126f9a767SRodney W. Grimes /* 282d323ddf3SMatthew Dillon * If the current process has a special image activator it 283d323ddf3SMatthew Dillon * wants to try first, call it. For example, emulating shell 284d323ddf3SMatthew Dillon * scripts differently. 28526f9a767SRodney W. Grimes */ 286d323ddf3SMatthew Dillon error = -1; 287d323ddf3SMatthew Dillon if ((img_first = imgp->proc->p_sysent->sv_imgact_try) != NULL) 288d323ddf3SMatthew Dillon error = img_first(imgp); 289d323ddf3SMatthew Dillon 290d323ddf3SMatthew Dillon /* 291d323ddf3SMatthew Dillon * Loop through the list of image activators, calling each one. 292d323ddf3SMatthew Dillon * An activator returns -1 if there is no match, 0 on success, 293d323ddf3SMatthew Dillon * and an error otherwise. 294d323ddf3SMatthew Dillon */ 295d323ddf3SMatthew Dillon for (i = 0; error == -1 && execsw[i]; ++i) { 296d323ddf3SMatthew Dillon if (execsw[i]->ex_imgact == NULL || 297d323ddf3SMatthew Dillon execsw[i]->ex_imgact == img_first) { 298d323ddf3SMatthew Dillon continue; 299d323ddf3SMatthew Dillon } 300c52007c2SDavid Greenman error = (*execsw[i]->ex_imgact)(imgp); 301d323ddf3SMatthew Dillon } 302d323ddf3SMatthew Dillon 303d323ddf3SMatthew Dillon if (error) { 304619eb6e5SJeff Roberson if (error == -1) { 305619eb6e5SJeff Roberson if (textset == 0) 306619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 307d323ddf3SMatthew Dillon error = ENOEXEC; 308619eb6e5SJeff Roberson } 30926f9a767SRodney W. Grimes goto exec_fail_dealloc; 310d323ddf3SMatthew Dillon } 311d323ddf3SMatthew Dillon 312d323ddf3SMatthew Dillon /* 313d323ddf3SMatthew Dillon * Special interpreter operation, cleanup and loop up to try to 314d323ddf3SMatthew Dillon * activate the interpreter. 315d323ddf3SMatthew Dillon */ 316c52007c2SDavid Greenman if (imgp->interpreted) { 3171616db3cSJohn Dyson exec_unmap_first_page(imgp); 318619eb6e5SJeff Roberson /* 319619eb6e5SJeff Roberson * VV_TEXT needs to be unset for scripts. There is a short 320619eb6e5SJeff Roberson * period before we determine that something is a script where 321619eb6e5SJeff Roberson * VV_TEXT will be set. The vnode lock is held over this 322619eb6e5SJeff Roberson * entire period so nothing should illegitimately be blocked. 323619eb6e5SJeff Roberson */ 324619eb6e5SJeff Roberson imgp->vp->v_vflag &= ~VV_TEXT; 325762e6b85SEivind Eklund /* free name buffer and old vnode */ 326762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 327619eb6e5SJeff Roberson vput(ndp->ni_vp); 3280b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 3290b2ed1aeSJeff Roberson imgp->object = NULL; 33026f9a767SRodney W. Grimes /* set new name to that of the interpreter */ 331b35ba931SDavid Greenman NDINIT(ndp, LOOKUP, LOCKLEAF | FOLLOW | SAVENAME, 332b40ce416SJulian Elischer UIO_SYSSPACE, imgp->interpreter_name, td); 33326f9a767SRodney W. Grimes goto interpret; 33426f9a767SRodney W. Grimes } 33526f9a767SRodney W. Grimes 33626f9a767SRodney W. Grimes /* 33726f9a767SRodney W. Grimes * Copy out strings (args and env) and initialize stack base 33826f9a767SRodney W. Grimes */ 3393ebc1248SPeter Wemm if (p->p_sysent->sv_copyout_strings) 3403ebc1248SPeter Wemm stack_base = (*p->p_sysent->sv_copyout_strings)(imgp); 3413ebc1248SPeter Wemm else 342c52007c2SDavid Greenman stack_base = exec_copyout_strings(imgp); 34326f9a767SRodney W. Grimes 34426f9a767SRodney W. Grimes /* 3451e1e0b44SSøren Schmidt * If custom stack fixup routine present for this process 3461e1e0b44SSøren Schmidt * let it do the stack setup. 3471e1e0b44SSøren Schmidt * Else stuff argument count as first item on stack 34826f9a767SRodney W. Grimes */ 3491e1e0b44SSøren Schmidt if (p->p_sysent->sv_fixup) 350c52007c2SDavid Greenman (*p->p_sysent->sv_fixup)(&stack_base, imgp); 3511e1e0b44SSøren Schmidt else 352c52007c2SDavid Greenman suword(--stack_base, imgp->argc); 35326f9a767SRodney W. Grimes 354a78e8d2aSDavid Greenman /* 355a78e8d2aSDavid Greenman * For security and other reasons, the file descriptor table cannot 356a78e8d2aSDavid Greenman * be shared after an exec. 357a78e8d2aSDavid Greenman */ 358426da3bcSAlfred Perlstein FILEDESC_LOCK(p->p_fd); 359a78e8d2aSDavid Greenman if (p->p_fd->fd_refcnt > 1) { 360a78e8d2aSDavid Greenman struct filedesc *tmp; 361a78e8d2aSDavid Greenman 362b40ce416SJulian Elischer tmp = fdcopy(td); 363426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 364b40ce416SJulian Elischer fdfree(td); 365a78e8d2aSDavid Greenman p->p_fd = tmp; 366426da3bcSAlfred Perlstein } else 367426da3bcSAlfred Perlstein FILEDESC_UNLOCK(p->p_fd); 368a78e8d2aSDavid Greenman 369333ea485SGuido van Rooij /* 3709b3b1c5fSJohn Baldwin * Malloc things before we need locks. 3719b3b1c5fSJohn Baldwin */ 3729b3b1c5fSJohn Baldwin newcred = crget(); 3731419eacbSAlfred Perlstein euip = uifind(attr.va_uid); 3749b3b1c5fSJohn Baldwin i = imgp->endargs - imgp->stringbase; 3759b3b1c5fSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) 3769b3b1c5fSJohn Baldwin newargs = pargs_alloc(i); 3779b3b1c5fSJohn Baldwin 3789b3b1c5fSJohn Baldwin /* close files on exec */ 3799b3b1c5fSJohn Baldwin fdcloseexec(td); 3809b3b1c5fSJohn Baldwin 381619eb6e5SJeff Roberson /* Get a reference to the vnode prior to locking the proc */ 382619eb6e5SJeff Roberson VREF(ndp->ni_vp); 383619eb6e5SJeff Roberson 3849b3b1c5fSJohn Baldwin /* 385333ea485SGuido van Rooij * For security and other reasons, signal handlers cannot 3868688bb93SJohn Baldwin * be shared after an exec. The new process gets a copy of the old 387b2c3fa70SDima Dorfman * handlers. In execsigs(), the new process will have its signals 388333ea485SGuido van Rooij * reset. 389333ea485SGuido van Rooij */ 3909b3b1c5fSJohn Baldwin PROC_LOCK(p); 3919b3b1c5fSJohn Baldwin mp_fixme("procsig needs a lock"); 392333ea485SGuido van Rooij if (p->p_procsig->ps_refcnt > 1) { 3939b3b1c5fSJohn Baldwin oldprocsig = p->p_procsig; 3949b3b1c5fSJohn Baldwin PROC_UNLOCK(p); 395333ea485SGuido van Rooij MALLOC(newprocsig, struct procsig *, sizeof(struct procsig), 396333ea485SGuido van Rooij M_SUBPROC, M_WAITOK); 3979b3b1c5fSJohn Baldwin bcopy(oldprocsig, newprocsig, sizeof(*newprocsig)); 3989b3b1c5fSJohn Baldwin newprocsig->ps_refcnt = 1; 3999b3b1c5fSJohn Baldwin oldprocsig->ps_refcnt--; 4009b3b1c5fSJohn Baldwin PROC_LOCK(p); 401333ea485SGuido van Rooij p->p_procsig = newprocsig; 402b40ce416SJulian Elischer if (p->p_sigacts == &p->p_uarea->u_sigacts) 403b2c3fa70SDima Dorfman panic("shared procsig but private sigacts?"); 404333ea485SGuido van Rooij 405b40ce416SJulian Elischer p->p_uarea->u_sigacts = *p->p_sigacts; 406b40ce416SJulian Elischer p->p_sigacts = &p->p_uarea->u_sigacts; 407333ea485SGuido van Rooij } 408fdf4e8b3SWarner Losh /* Stop profiling */ 409fdf4e8b3SWarner Losh stopprofclock(p); 410fdf4e8b3SWarner Losh 41126f9a767SRodney W. Grimes /* reset caught signals */ 41226f9a767SRodney W. Grimes execsigs(p); 41326f9a767SRodney W. Grimes 41426f9a767SRodney W. Grimes /* name this process - nameiexec(p, ndp) */ 41526f9a767SRodney W. Grimes len = min(ndp->ni_cnd.cn_namelen,MAXCOMLEN); 41626f9a767SRodney W. Grimes bcopy(ndp->ni_cnd.cn_nameptr, p->p_comm, len); 41726f9a767SRodney W. Grimes p->p_comm[len] = 0; 41826f9a767SRodney W. Grimes 41926f9a767SRodney W. Grimes /* 42024b34f09SSujal Patel * mark as execed, wakeup the process that vforked (if any) and tell 421dc733423SDag-Erling Smørgrav * it that it now has its own resources back 42226f9a767SRodney W. Grimes */ 42326f9a767SRodney W. Grimes p->p_flag |= P_EXEC; 42426f9a767SRodney W. Grimes if (p->p_pptr && (p->p_flag & P_PPWAIT)) { 42526f9a767SRodney W. Grimes p->p_flag &= ~P_PPWAIT; 4267f05b035SAlfred Perlstein wakeup(p->p_pptr); 42726f9a767SRodney W. Grimes } 42826f9a767SRodney W. Grimes 42926f9a767SRodney W. Grimes /* 430a78e8d2aSDavid Greenman * Implement image setuid/setgid. 431a78e8d2aSDavid Greenman * 432a78e8d2aSDavid Greenman * Don't honor setuid/setgid if the filesystem prohibits it or if 433a78e8d2aSDavid Greenman * the process is being traced. 434c52007c2SDavid Greenman */ 435b1fc0ec1SRobert Watson oldcred = p->p_ucred; 436d06c0d4dSRobert Watson credential_changing = 0; 437d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSUID) && oldcred->cr_uid != 438d06c0d4dSRobert Watson attr.va_uid; 439d06c0d4dSRobert Watson credential_changing |= (attr.va_mode & VSGID) && oldcred->cr_gid != 440d06c0d4dSRobert Watson attr.va_gid; 441d06c0d4dSRobert Watson 442d06c0d4dSRobert Watson if (credential_changing && 443a78e8d2aSDavid Greenman (imgp->vp->v_mount->mnt_flag & MNT_NOSUID) == 0 && 444c52007c2SDavid Greenman (p->p_flag & P_TRACED) == 0) { 445c52007c2SDavid Greenman /* 446c52007c2SDavid Greenman * Turn off syscall tracing for set-id programs, except for 447b85db196SPeter Wemm * root. Record any set-id flags first to make sure that 448b85db196SPeter Wemm * we do not regain any tracing during a possible block. 44926f9a767SRodney W. Grimes */ 450b85db196SPeter Wemm setsugid(p); 4516c84de02SJohn Baldwin #ifdef KTRACE 45244731cabSJohn Baldwin if (p->p_tracep && suser_cred(oldcred, PRISON_ROOT)) { 4536c84de02SJohn Baldwin mtx_lock(&ktrace_mtx); 45479deba82SMatthew Dillon p->p_traceflag = 0; 4559b3b1c5fSJohn Baldwin tracevp = p->p_tracep; 4569b3b1c5fSJohn Baldwin p->p_tracep = NULL; 4576c84de02SJohn Baldwin mtx_unlock(&ktrace_mtx); 45826f9a767SRodney W. Grimes } 4596c84de02SJohn Baldwin #endif 46028b325aaSDon Lewis /* 461c1e2d386SNate Lawson * Close any file descriptors 0..2 that reference procfs, 462c1e2d386SNate Lawson * then make sure file descriptors 0..2 are in use. 46328b325aaSDon Lewis * 464c1e2d386SNate Lawson * setugidsafety() may call closef() and then pfind() 465c1e2d386SNate Lawson * which may grab the process lock. 46628b325aaSDon Lewis * fdcheckstd() may call falloc() which may block to 46728b325aaSDon Lewis * allocate memory, so temporarily drop the process lock. 46828b325aaSDon Lewis */ 46928b325aaSDon Lewis PROC_UNLOCK(p); 470c1e2d386SNate Lawson setugidsafety(td); 471e983a376SJacques Vidrine error = fdcheckstd(td); 47228b325aaSDon Lewis PROC_LOCK(p); 47363c9e754SJohn Baldwin if (error != 0) 47469be5db9SAlfred Perlstein goto done1; 475c52007c2SDavid Greenman /* 476c52007c2SDavid Greenman * Set the new credentials. 477c52007c2SDavid Greenman */ 4789b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 479c52007c2SDavid Greenman if (attr.va_mode & VSUID) 4801419eacbSAlfred Perlstein change_euid(newcred, euip); 481c52007c2SDavid Greenman if (attr.va_mode & VSGID) 482b1fc0ec1SRobert Watson change_egid(newcred, attr.va_gid); 4839b3b1c5fSJohn Baldwin /* 4849b3b1c5fSJohn Baldwin * Implement correct POSIX saved-id behavior. 4859b3b1c5fSJohn Baldwin */ 4869b3b1c5fSJohn Baldwin change_svuid(newcred, newcred->cr_uid); 4879b3b1c5fSJohn Baldwin change_svgid(newcred, newcred->cr_gid); 4889b3b1c5fSJohn Baldwin p->p_ucred = newcred; 4899b3b1c5fSJohn Baldwin newcred = NULL; 490c52007c2SDavid Greenman } else { 491b1fc0ec1SRobert Watson if (oldcred->cr_uid == oldcred->cr_ruid && 492b1fc0ec1SRobert Watson oldcred->cr_gid == oldcred->cr_rgid) 493c52007c2SDavid Greenman p->p_flag &= ~P_SUGID; 49426f9a767SRodney W. Grimes /* 495c52007c2SDavid Greenman * Implement correct POSIX saved-id behavior. 496b1fc0ec1SRobert Watson * 497b1fc0ec1SRobert Watson * XXX: It's not clear that the existing behavior is 4989b3b1c5fSJohn Baldwin * POSIX-compliant. A number of sources indicate that the 4999b3b1c5fSJohn Baldwin * saved uid/gid should only be updated if the new ruid is 5009b3b1c5fSJohn Baldwin * not equal to the old ruid, or the new euid is not equal 5019b3b1c5fSJohn Baldwin * to the old euid and the new euid is not equal to the old 5029b3b1c5fSJohn Baldwin * ruid. The FreeBSD code always updates the saved uid/gid. 5039b3b1c5fSJohn Baldwin * Also, this code uses the new (replaced) euid and egid as 5049b3b1c5fSJohn Baldwin * the source, which may or may not be the right ones to use. 50526f9a767SRodney W. Grimes */ 506b1fc0ec1SRobert Watson if (oldcred->cr_svuid != oldcred->cr_uid || 507b1fc0ec1SRobert Watson oldcred->cr_svgid != oldcred->cr_gid) { 5089b3b1c5fSJohn Baldwin crcopy(newcred, oldcred); 509b1fc0ec1SRobert Watson change_svuid(newcred, newcred->cr_uid); 510b1fc0ec1SRobert Watson change_svgid(newcred, newcred->cr_gid); 511b1fc0ec1SRobert Watson p->p_ucred = newcred; 5129b3b1c5fSJohn Baldwin newcred = NULL; 5139b3b1c5fSJohn Baldwin } 514b1fc0ec1SRobert Watson } 51526f9a767SRodney W. Grimes 51626f9a767SRodney W. Grimes /* 517619eb6e5SJeff Roberson * Store the vp for use in procfs. This vnode was referenced prior 518619eb6e5SJeff Roberson * to locking the proc lock. 5192a531c80SDavid Greenman */ 5209b3b1c5fSJohn Baldwin textvp = p->p_textvp; 5212a531c80SDavid Greenman p->p_textvp = ndp->ni_vp; 5222a531c80SDavid Greenman 5232a531c80SDavid Greenman /* 5249ca45e81SDag-Erling Smørgrav * Notify others that we exec'd, and clear the P_INEXEC flag 5259ca45e81SDag-Erling Smørgrav * as we're now a bona fide freshly-execed process. 526cb679c38SJonathan Lemon */ 527cb679c38SJonathan Lemon KNOTE(&p->p_klist, NOTE_EXEC); 5289ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 529cb679c38SJonathan Lemon 530cb679c38SJonathan Lemon /* 53126f9a767SRodney W. Grimes * If tracing the process, trap to debugger so breakpoints 53226f9a767SRodney W. Grimes * can be set before the program executes. 53326f9a767SRodney W. Grimes */ 534e65897c3SJohn Baldwin _STOPEVENT(p, S_EXEC, 0); 5352a024a2bSSean Eric Fagan 53626f9a767SRodney W. Grimes if (p->p_flag & P_TRACED) 53726f9a767SRodney W. Grimes psignal(p, SIGTRAP); 53826f9a767SRodney W. Grimes 53926f9a767SRodney W. Grimes /* clear "fork but no exec" flag, as we _are_ execing */ 54026f9a767SRodney W. Grimes p->p_acflag &= ~AFORK; 54126f9a767SRodney W. Grimes 542b9df5231SPoul-Henning Kamp /* Free any previous argument cache */ 5439b3b1c5fSJohn Baldwin oldargs = p->p_args; 544b9df5231SPoul-Henning Kamp p->p_args = NULL; 545b9df5231SPoul-Henning Kamp 546e913ca22SDoug Rabson /* Set values passed into the program in registers. */ 5473ebc1248SPeter Wemm if (p->p_sysent->sv_setregs) 5483ebc1248SPeter Wemm (*p->p_sysent->sv_setregs)(td, imgp->entry_addr, 5493ebc1248SPeter Wemm (u_long)(uintptr_t)stack_base, imgp->ps_strings); 5503ebc1248SPeter Wemm else 551bafbd492SJake Burkholder exec_setregs(td, imgp->entry_addr, 552bafbd492SJake Burkholder (u_long)(uintptr_t)stack_base, imgp->ps_strings); 553e913ca22SDoug Rabson 554b9df5231SPoul-Henning Kamp /* Cache arguments if they fit inside our allowance */ 555b9df5231SPoul-Henning Kamp if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 5569b3b1c5fSJohn Baldwin bcopy(imgp->stringbase, newargs->ar_args, i); 5579b3b1c5fSJohn Baldwin p->p_args = newargs; 5589b3b1c5fSJohn Baldwin newargs = NULL; 559fbd26f75SJohn Baldwin } 56069be5db9SAlfred Perlstein done1: 5619b3b1c5fSJohn Baldwin PROC_UNLOCK(p); 5629b3b1c5fSJohn Baldwin 563619eb6e5SJeff Roberson 5649b3b1c5fSJohn Baldwin /* 5659b3b1c5fSJohn Baldwin * Free any resources malloc'd earlier that we didn't use. 5669b3b1c5fSJohn Baldwin */ 5671419eacbSAlfred Perlstein uifree(euip); 5689b3b1c5fSJohn Baldwin if (newcred == NULL) 5699b3b1c5fSJohn Baldwin crfree(oldcred); 5709b3b1c5fSJohn Baldwin else 5719b3b1c5fSJohn Baldwin crfree(newcred); 5729b3b1c5fSJohn Baldwin /* 5739b3b1c5fSJohn Baldwin * Handle deferred decrement of ref counts. 5749b3b1c5fSJohn Baldwin */ 5759b3b1c5fSJohn Baldwin if (textvp != NULL) 5769b3b1c5fSJohn Baldwin vrele(textvp); 577619eb6e5SJeff Roberson if (ndp->ni_vp && error != 0) 578619eb6e5SJeff Roberson vrele(ndp->ni_vp); 5796c84de02SJohn Baldwin #ifdef KTRACE 5809b3b1c5fSJohn Baldwin if (tracevp != NULL) 5819b3b1c5fSJohn Baldwin vrele(tracevp); 5826c84de02SJohn Baldwin #endif 58369be5db9SAlfred Perlstein if (oldargs != NULL) 5849b3b1c5fSJohn Baldwin pargs_drop(oldargs); 58569be5db9SAlfred Perlstein if (newargs != NULL) 58669be5db9SAlfred Perlstein pargs_drop(newargs); 587b9df5231SPoul-Henning Kamp 5881616db3cSJohn Dyson exec_fail_dealloc: 5891616db3cSJohn Dyson 59026f9a767SRodney W. Grimes /* 59126f9a767SRodney W. Grimes * free various allocated resources 59226f9a767SRodney W. Grimes */ 5931616db3cSJohn Dyson if (imgp->firstpage) 5941616db3cSJohn Dyson exec_unmap_first_page(imgp); 59526f9a767SRodney W. Grimes 596c2f9f36bSDavid Greenman if (imgp->stringbase != NULL) 5971616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 5981616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 5991616db3cSJohn Dyson 6009c0fed3dSDoug Rabson if (imgp->vp) { 601762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 602619eb6e5SJeff Roberson vput(imgp->vp); 603a3cf6ebaSDavid Greenman } 60426f9a767SRodney W. Grimes 6050b2ed1aeSJeff Roberson if (imgp->object) 6060b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 6070b2ed1aeSJeff Roberson 6081616db3cSJohn Dyson if (error == 0) 609116734c4SMatthew Dillon goto done2; 6101616db3cSJohn Dyson 61126f9a767SRodney W. Grimes exec_fail: 6129ca45e81SDag-Erling Smørgrav /* we're done here, clear P_INEXEC */ 6139ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 6149ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 6159ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 6169ca45e81SDag-Erling Smørgrav 617c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 61826f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 619b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, SIGABRT)); 62026f9a767SRodney W. Grimes /* NOT REACHED */ 621116734c4SMatthew Dillon error = 0; 62226f9a767SRodney W. Grimes } 623116734c4SMatthew Dillon done2: 624116734c4SMatthew Dillon mtx_unlock(&Giant); 625116734c4SMatthew Dillon return (error); 62626f9a767SRodney W. Grimes } 62726f9a767SRodney W. Grimes 6281616db3cSJohn Dyson int 6291616db3cSJohn Dyson exec_map_first_page(imgp) 6301616db3cSJohn Dyson struct image_params *imgp; 6311616db3cSJohn Dyson { 632d8aad40cSJohn Baldwin int rv, i; 63395461b45SJohn Dyson int initial_pagein; 63495461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 6351616db3cSJohn Dyson vm_object_t object; 6361616db3cSJohn Dyson 6370cddd8f0SMatthew Dillon GIANT_REQUIRED; 6381616db3cSJohn Dyson 6391616db3cSJohn Dyson if (imgp->firstpage) { 6401616db3cSJohn Dyson exec_unmap_first_page(imgp); 6411616db3cSJohn Dyson } 6421616db3cSJohn Dyson 6439ff5ce6bSBoris Popov VOP_GETVOBJECT(imgp->vp, &object); 6441616db3cSJohn Dyson 64595461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 6461616db3cSJohn Dyson 64795461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 64895461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 64995461b45SJohn Dyson if (initial_pagein > object->size) 65095461b45SJohn Dyson initial_pagein = object->size; 65195461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 652d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 65395461b45SJohn Dyson if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) 65495461b45SJohn Dyson break; 65595461b45SJohn Dyson if (ma[i]->valid) 65695461b45SJohn Dyson break; 657e69763a3SDoug Rabson vm_page_busy(ma[i]); 65895461b45SJohn Dyson } else { 659ca0387efSJake Burkholder ma[i] = vm_page_alloc(object, i, 660ca0387efSJake Burkholder VM_ALLOC_NORMAL); 66195461b45SJohn Dyson if (ma[i] == NULL) 66295461b45SJohn Dyson break; 66395461b45SJohn Dyson } 66495461b45SJohn Dyson } 66595461b45SJohn Dyson initial_pagein = i; 6661616db3cSJohn Dyson 66795461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 66895461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 66995461b45SJohn Dyson 670ca0387efSJake Burkholder if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || 671ca0387efSJake Burkholder (ma[0]->valid == 0)) { 672ecbb00a2SDoug Rabson if (ma[0]) { 67397646f56SAlan Cox vm_page_lock_queues(); 67495461b45SJohn Dyson vm_page_protect(ma[0], VM_PROT_NONE); 6758f9110f6SJohn Dyson vm_page_free(ma[0]); 67697646f56SAlan Cox vm_page_unlock_queues(); 677ecbb00a2SDoug Rabson } 678a7cddfedSJake Burkholder return (EIO); 6791616db3cSJohn Dyson } 6801616db3cSJohn Dyson } 68197646f56SAlan Cox vm_page_lock_queues(); 68295461b45SJohn Dyson vm_page_wire(ma[0]); 683e69763a3SDoug Rabson vm_page_wakeup(ma[0]); 68497646f56SAlan Cox vm_page_unlock_queues(); 6851616db3cSJohn Dyson 686ac59490bSJake Burkholder pmap_qenter((vm_offset_t)imgp->image_header, ma, 1); 68795461b45SJohn Dyson imgp->firstpage = ma[0]; 6881616db3cSJohn Dyson 689a7cddfedSJake Burkholder return (0); 6901616db3cSJohn Dyson } 6911616db3cSJohn Dyson 6921616db3cSJohn Dyson void 6931616db3cSJohn Dyson exec_unmap_first_page(imgp) 6941616db3cSJohn Dyson struct image_params *imgp; 6951616db3cSJohn Dyson { 6960cddd8f0SMatthew Dillon GIANT_REQUIRED; 69723955314SAlfred Perlstein 6981616db3cSJohn Dyson if (imgp->firstpage) { 699ac59490bSJake Burkholder pmap_qremove((vm_offset_t)imgp->image_header, 1); 70097646f56SAlan Cox vm_page_lock_queues(); 70173007561SDavid Greenman vm_page_unwire(imgp->firstpage, 1); 70297646f56SAlan Cox vm_page_unlock_queues(); 7031616db3cSJohn Dyson imgp->firstpage = NULL; 7041616db3cSJohn Dyson } 7051616db3cSJohn Dyson } 7061616db3cSJohn Dyson 70726f9a767SRodney W. Grimes /* 70826f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 70926f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 71026f9a767SRodney W. Grimes * automatically in trap.c. 71126f9a767SRodney W. Grimes */ 71226f9a767SRodney W. Grimes int 71305ba50f5SJake Burkholder exec_new_vmspace(imgp, sv) 714c52007c2SDavid Greenman struct image_params *imgp; 71505ba50f5SJake Burkholder struct sysentvec *sv; 71626f9a767SRodney W. Grimes { 71726f9a767SRodney W. Grimes int error; 7186f5dafeaSAlan Cox struct execlist *ep; 7195e20c11fSAlan Cox struct proc *p = imgp->proc; 7205e20c11fSAlan Cox struct vmspace *vmspace = p->p_vmspace; 72105ba50f5SJake Burkholder vm_offset_t stack_addr; 72205ba50f5SJake Burkholder vm_map_t map; 72326f9a767SRodney W. Grimes 7240cddd8f0SMatthew Dillon GIANT_REQUIRED; 7250cddd8f0SMatthew Dillon 72605ba50f5SJake Burkholder stack_addr = sv->sv_usrstack - maxssiz; 7273ebc1248SPeter Wemm 728c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 72926f9a767SRodney W. Grimes 730af9ec885SJohn Dyson /* 7316f5dafeaSAlan Cox * Perform functions registered with at_exec(). 7326f5dafeaSAlan Cox */ 7336f5dafeaSAlan Cox TAILQ_FOREACH(ep, &exec_list, next) 7345e20c11fSAlan Cox (*ep->function)(p); 7356f5dafeaSAlan Cox 7366f5dafeaSAlan Cox /* 737af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 738af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 739af9ec885SJohn Dyson * not disrupted 740af9ec885SJohn Dyson */ 74105ba50f5SJake Burkholder map = &vmspace->vm_map; 74205ba50f5SJake Burkholder if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser && 74305ba50f5SJake Burkholder vm_map_max(map) == sv->sv_maxuser) { 7443d903220SDoug Rabson if (vmspace->vm_shm) 7455e20c11fSAlan Cox shmexit(p); 74605ba50f5SJake Burkholder pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map), 74705ba50f5SJake Burkholder vm_map_max(map)); 74805ba50f5SJake Burkholder vm_map_remove(map, vm_map_min(map), vm_map_max(map)); 749af9ec885SJohn Dyson } else { 75005ba50f5SJake Burkholder vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser); 7515e20c11fSAlan Cox vmspace = p->p_vmspace; 75205ba50f5SJake Burkholder map = &vmspace->vm_map; 753af9ec885SJohn Dyson } 75426f9a767SRodney W. Grimes 75526f9a767SRodney W. Grimes /* Allocate a new stack */ 75605ba50f5SJake Burkholder error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz, 75705ba50f5SJake Burkholder sv->sv_stackprot, VM_PROT_ALL, 0); 7582267af78SJulian Elischer if (error) 7592267af78SJulian Elischer return (error); 7602267af78SJulian Elischer 76163c47a5cSDoug Rabson #ifdef __ia64__ 76263c47a5cSDoug Rabson { 76363c47a5cSDoug Rabson /* 76463c47a5cSDoug Rabson * Allocate backing store. We really need something 76563c47a5cSDoug Rabson * similar to vm_map_stack which can allow the backing 76663c47a5cSDoug Rabson * store to grow upwards. This will do for now. 76763c47a5cSDoug Rabson */ 76863c47a5cSDoug Rabson vm_offset_t bsaddr; 76905ba50f5SJake Burkholder bsaddr = p->p_sysent->sv_usrstack - 2 * maxssiz; 77005ba50f5SJake Burkholder error = vm_map_find(map, 0, 0, &bsaddr, 77181f223caSJake Burkholder regstkpages * PAGE_SIZE, 0, VM_PROT_ALL, VM_PROT_ALL, 0); 7725e20c11fSAlan Cox FIRST_THREAD_IN_PROC(p)->td_md.md_bspstore = bsaddr; 77363c47a5cSDoug Rabson } 77463c47a5cSDoug Rabson #endif 77563c47a5cSDoug Rabson 7762267af78SJulian Elischer /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 7772267af78SJulian Elischer * VM_STACK case, but they are still used to monitor the size of the 7782267af78SJulian Elischer * process stack so we can check the stack rlimit. 7792267af78SJulian Elischer */ 780cbc89bfbSPaul Saab vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 78105ba50f5SJake Burkholder vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz; 78226f9a767SRodney W. Grimes 78326f9a767SRodney W. Grimes return (0); 78426f9a767SRodney W. Grimes } 78526f9a767SRodney W. Grimes 78626f9a767SRodney W. Grimes /* 78726f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 78826f9a767SRodney W. Grimes * address space into the temporary string buffer. 78926f9a767SRodney W. Grimes */ 79026f9a767SRodney W. Grimes int 791c52007c2SDavid Greenman exec_extract_strings(imgp) 792c52007c2SDavid Greenman struct image_params *imgp; 79326f9a767SRodney W. Grimes { 79426f9a767SRodney W. Grimes char **argv, **envv; 79526f9a767SRodney W. Grimes char *argp, *envp; 796ecbb00a2SDoug Rabson int error; 797ecbb00a2SDoug Rabson size_t length; 79826f9a767SRodney W. Grimes 79926f9a767SRodney W. Grimes /* 80026f9a767SRodney W. Grimes * extract arguments first 80126f9a767SRodney W. Grimes */ 80226f9a767SRodney W. Grimes 803c52007c2SDavid Greenman argv = imgp->uap->argv; 80426f9a767SRodney W. Grimes 8050fd1a014SDavid Greenman if (argv) { 806aae0aa45SBruce Evans argp = (caddr_t)(intptr_t)fuword(argv); 8075cf3d12cSAndrey A. Chernov if (argp == (caddr_t)-1) 8085cf3d12cSAndrey A. Chernov return (EFAULT); 8095cf3d12cSAndrey A. Chernov if (argp) 8105cf3d12cSAndrey A. Chernov argv++; 8115cf3d12cSAndrey A. Chernov if (imgp->argv0) 8125cf3d12cSAndrey A. Chernov argp = imgp->argv0; 8135cf3d12cSAndrey A. Chernov if (argp) { 8145cf3d12cSAndrey A. Chernov do { 81526f9a767SRodney W. Grimes if (argp == (caddr_t)-1) 81626f9a767SRodney W. Grimes return (EFAULT); 817c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 818c52007c2SDavid Greenman imgp->stringspace, &length))) { 8190fd1a014SDavid Greenman if (error == ENAMETOOLONG) 82026f9a767SRodney W. Grimes return (E2BIG); 8210fd1a014SDavid Greenman return (error); 8220fd1a014SDavid Greenman } 823c52007c2SDavid Greenman imgp->stringspace -= length; 824c52007c2SDavid Greenman imgp->stringp += length; 825c52007c2SDavid Greenman imgp->argc++; 826aae0aa45SBruce Evans } while ((argp = (caddr_t)(intptr_t)fuword(argv++))); 82726f9a767SRodney W. Grimes } 8280fd1a014SDavid Greenman } 82926f9a767SRodney W. Grimes 830b9df5231SPoul-Henning Kamp imgp->endargs = imgp->stringp; 831b9df5231SPoul-Henning Kamp 83226f9a767SRodney W. Grimes /* 83326f9a767SRodney W. Grimes * extract environment strings 83426f9a767SRodney W. Grimes */ 83526f9a767SRodney W. Grimes 836c52007c2SDavid Greenman envv = imgp->uap->envv; 83726f9a767SRodney W. Grimes 8380fd1a014SDavid Greenman if (envv) { 839aae0aa45SBruce Evans while ((envp = (caddr_t)(intptr_t)fuword(envv++))) { 84026f9a767SRodney W. Grimes if (envp == (caddr_t)-1) 84126f9a767SRodney W. Grimes return (EFAULT); 842c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 843c52007c2SDavid Greenman imgp->stringspace, &length))) { 8440fd1a014SDavid Greenman if (error == ENAMETOOLONG) 84526f9a767SRodney W. Grimes return (E2BIG); 8460fd1a014SDavid Greenman return (error); 8470fd1a014SDavid Greenman } 848c52007c2SDavid Greenman imgp->stringspace -= length; 849c52007c2SDavid Greenman imgp->stringp += length; 850c52007c2SDavid Greenman imgp->envc++; 85126f9a767SRodney W. Grimes } 8520fd1a014SDavid Greenman } 85326f9a767SRodney W. Grimes 85426f9a767SRodney W. Grimes return (0); 85526f9a767SRodney W. Grimes } 85626f9a767SRodney W. Grimes 85726f9a767SRodney W. Grimes /* 85826f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 85926f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 86026f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 86126f9a767SRodney W. Grimes */ 862654f6be1SBruce Evans register_t * 863c52007c2SDavid Greenman exec_copyout_strings(imgp) 864c52007c2SDavid Greenman struct image_params *imgp; 86526f9a767SRodney W. Grimes { 86626f9a767SRodney W. Grimes int argc, envc; 86726f9a767SRodney W. Grimes char **vectp; 86826f9a767SRodney W. Grimes char *stringp, *destp; 869654f6be1SBruce Evans register_t *stack_base; 87093f6448cSDavid Greenman struct ps_strings *arginfo; 871f3bec5d7SJake Burkholder struct proc *p; 872d66a5066SPeter Wemm int szsigcode; 87326f9a767SRodney W. Grimes 87426f9a767SRodney W. Grimes /* 87526f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 876d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 87726f9a767SRodney W. Grimes */ 878f3bec5d7SJake Burkholder p = imgp->proc; 879f3bec5d7SJake Burkholder szsigcode = 0; 88005ba50f5SJake Burkholder arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 881f3bec5d7SJake Burkholder if (p->p_sysent->sv_szsigcode != NULL) 882f3bec5d7SJake Burkholder szsigcode = *(p->p_sysent->sv_szsigcode); 883d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 8841ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 8851ed012f9SPeter Wemm 88626f9a767SRodney W. Grimes /* 887d66a5066SPeter Wemm * install sigcode 888d66a5066SPeter Wemm */ 889d66a5066SPeter Wemm if (szsigcode) 890f3bec5d7SJake Burkholder copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo - 891f3bec5d7SJake Burkholder szsigcode), szsigcode); 892d66a5066SPeter Wemm 893d66a5066SPeter Wemm /* 894e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 895e1743d02SSøren Schmidt * on the stack. 896e1743d02SSøren Schmidt */ 897b9a22da4STakanori Watanabe if (imgp->auxargs) { 898e1743d02SSøren Schmidt /* 899b9a22da4STakanori Watanabe * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 900b9a22da4STakanori Watanabe * lower compatibility. 901b9a22da4STakanori Watanabe */ 902ca0387efSJake Burkholder imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size : 903ca0387efSJake Burkholder (AT_COUNT * 2); 904b9a22da4STakanori Watanabe /* 905b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 906b9a22da4STakanori Watanabe * the arg and env vector sets,and imgp->auxarg_size is room 907b9a22da4STakanori Watanabe * for argument of Runtime loader. 908e1743d02SSøren Schmidt */ 909e1743d02SSøren Schmidt vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 + 910b9a22da4STakanori Watanabe imgp->auxarg_size) * sizeof(char *)); 911b9a22da4STakanori Watanabe 912b9a22da4STakanori Watanabe } else 913e1743d02SSøren Schmidt /* 914b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 915b9a22da4STakanori Watanabe * the arg and env vector sets 91626f9a767SRodney W. Grimes */ 917ca0387efSJake Burkholder vectp = (char **)(destp - (imgp->argc + imgp->envc + 2) * 918ca0387efSJake Burkholder sizeof(char *)); 91926f9a767SRodney W. Grimes 92026f9a767SRodney W. Grimes /* 92126f9a767SRodney W. Grimes * vectp also becomes our initial stack base 92226f9a767SRodney W. Grimes */ 923654f6be1SBruce Evans stack_base = (register_t *)vectp; 92426f9a767SRodney W. Grimes 925c52007c2SDavid Greenman stringp = imgp->stringbase; 926c52007c2SDavid Greenman argc = imgp->argc; 927c52007c2SDavid Greenman envc = imgp->envc; 92826f9a767SRodney W. Grimes 92993f6448cSDavid Greenman /* 9303aed948bSDavid Greenman * Copy out strings - arguments and environment. 93193f6448cSDavid Greenman */ 932c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 93393f6448cSDavid Greenman 93493f6448cSDavid Greenman /* 9353aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 9363aed948bSDavid Greenman */ 937aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 9383aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 9393aed948bSDavid Greenman 9403aed948bSDavid Greenman /* 9413aed948bSDavid Greenman * Fill in argument portion of vector table. 94293f6448cSDavid Greenman */ 94326f9a767SRodney W. Grimes for (; argc > 0; --argc) { 944aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 9453aed948bSDavid Greenman while (*stringp++ != 0) 9463aed948bSDavid Greenman destp++; 9473aed948bSDavid Greenman destp++; 94826f9a767SRodney W. Grimes } 94926f9a767SRodney W. Grimes 9501a6e52d0SJeroen Ruigrok van der Werven /* a null vector table pointer separates the argp's from the envp's */ 9516ab46d52SBruce Evans suword(vectp++, 0); 95226f9a767SRodney W. Grimes 953aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 9543aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 95593f6448cSDavid Greenman 95693f6448cSDavid Greenman /* 9573aed948bSDavid Greenman * Fill in environment portion of vector table. 95893f6448cSDavid Greenman */ 95926f9a767SRodney W. Grimes for (; envc > 0; --envc) { 960aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 9613aed948bSDavid Greenman while (*stringp++ != 0) 9623aed948bSDavid Greenman destp++; 9633aed948bSDavid Greenman destp++; 96426f9a767SRodney W. Grimes } 96526f9a767SRodney W. Grimes 96626f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 9676ab46d52SBruce Evans suword(vectp, 0); 96826f9a767SRodney W. Grimes 96926f9a767SRodney W. Grimes return (stack_base); 97026f9a767SRodney W. Grimes } 97126f9a767SRodney W. Grimes 97226f9a767SRodney W. Grimes /* 97326f9a767SRodney W. Grimes * Check permissions of file to execute. 974cf64863aSRobert Watson * Called with imgp->vp locked. 97526f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 97626f9a767SRodney W. Grimes */ 977c8a79999SPeter Wemm int 978c52007c2SDavid Greenman exec_check_permissions(imgp) 979c52007c2SDavid Greenman struct image_params *imgp; 98026f9a767SRodney W. Grimes { 981c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 982c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 983a854ed98SJohn Baldwin struct thread *td; 98426f9a767SRodney W. Grimes int error; 98526f9a767SRodney W. Grimes 986a854ed98SJohn Baldwin td = curthread; /* XXXKSE */ 987339b79b9SRobert Watson 988339b79b9SRobert Watson #ifdef MAC 989339b79b9SRobert Watson error = mac_check_vnode_exec(td->td_ucred, imgp->vp); 990339b79b9SRobert Watson if (error) 991339b79b9SRobert Watson return (error); 992339b79b9SRobert Watson #endif 993339b79b9SRobert Watson 99426f9a767SRodney W. Grimes /* Get file attributes */ 995a854ed98SJohn Baldwin error = VOP_GETATTR(vp, attr, td->td_ucred, td); 99626f9a767SRodney W. Grimes if (error) 99726f9a767SRodney W. Grimes return (error); 99826f9a767SRodney W. Grimes 99926f9a767SRodney W. Grimes /* 100026f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 100126f9a767SRodney W. Grimes * file resides on. 100226f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 100326f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 100426f9a767SRodney W. Grimes * file really is executable. 100526f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 100626f9a767SRodney W. Grimes */ 1007c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 100826f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 1009a854ed98SJohn Baldwin (attr->va_type != VREG)) 101026f9a767SRodney W. Grimes return (EACCES); 101126f9a767SRodney W. Grimes 101226f9a767SRodney W. Grimes /* 101326f9a767SRodney W. Grimes * Zero length files can't be exec'd 101426f9a767SRodney W. Grimes */ 101526f9a767SRodney W. Grimes if (attr->va_size == 0) 101626f9a767SRodney W. Grimes return (ENOEXEC); 101726f9a767SRodney W. Grimes 101826f9a767SRodney W. Grimes /* 101926f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 102026f9a767SRodney W. Grimes */ 1021a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 102226f9a767SRodney W. Grimes if (error) 102326f9a767SRodney W. Grimes return (error); 102426f9a767SRodney W. Grimes 10256d5a0a8cSDavid Greenman /* 10266d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 10276d5a0a8cSDavid Greenman * if there are any. 10286d5a0a8cSDavid Greenman */ 10296d5a0a8cSDavid Greenman if (vp->v_writecount) 10306d5a0a8cSDavid Greenman return (ETXTBSY); 10316d5a0a8cSDavid Greenman 10326d5a0a8cSDavid Greenman /* 10336d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 10346d5a0a8cSDavid Greenman * general case). 10356d5a0a8cSDavid Greenman */ 1036a854ed98SJohn Baldwin error = VOP_OPEN(vp, FREAD, td->td_ucred, td); 103726f9a767SRodney W. Grimes return (error); 1038df8bae1dSRodney W. Grimes } 1039aa855a59SPeter Wemm 1040aa855a59SPeter Wemm /* 1041aa855a59SPeter Wemm * Exec handler registration 1042aa855a59SPeter Wemm */ 1043aa855a59SPeter Wemm int 1044aa855a59SPeter Wemm exec_register(execsw_arg) 1045aa855a59SPeter Wemm const struct execsw *execsw_arg; 1046aa855a59SPeter Wemm { 1047aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1048aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 1049aa855a59SPeter Wemm 1050aa855a59SPeter Wemm if (execsw) 1051aa855a59SPeter Wemm for (es = execsw; *es; es++) 1052aa855a59SPeter Wemm count++; 1053aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1054aa855a59SPeter Wemm if (newexecsw == NULL) 1055a7cddfedSJake Burkholder return (ENOMEM); 1056aa855a59SPeter Wemm xs = newexecsw; 1057aa855a59SPeter Wemm if (execsw) 1058aa855a59SPeter Wemm for (es = execsw; *es; es++) 1059aa855a59SPeter Wemm *xs++ = *es; 1060aa855a59SPeter Wemm *xs++ = execsw_arg; 1061aa855a59SPeter Wemm *xs = NULL; 1062aa855a59SPeter Wemm if (execsw) 1063aa855a59SPeter Wemm free(execsw, M_TEMP); 1064aa855a59SPeter Wemm execsw = newexecsw; 1065a7cddfedSJake Burkholder return (0); 1066aa855a59SPeter Wemm } 1067aa855a59SPeter Wemm 1068aa855a59SPeter Wemm int 1069aa855a59SPeter Wemm exec_unregister(execsw_arg) 1070aa855a59SPeter Wemm const struct execsw *execsw_arg; 1071aa855a59SPeter Wemm { 1072aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1073aa855a59SPeter Wemm int count = 1; 1074aa855a59SPeter Wemm 1075aa855a59SPeter Wemm if (execsw == NULL) 1076aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 1077aa855a59SPeter Wemm 1078aa855a59SPeter Wemm for (es = execsw; *es; es++) { 1079aa855a59SPeter Wemm if (*es == execsw_arg) 1080aa855a59SPeter Wemm break; 1081aa855a59SPeter Wemm } 1082aa855a59SPeter Wemm if (*es == NULL) 1083a7cddfedSJake Burkholder return (ENOENT); 1084aa855a59SPeter Wemm for (es = execsw; *es; es++) 1085aa855a59SPeter Wemm if (*es != execsw_arg) 1086aa855a59SPeter Wemm count++; 1087aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1088aa855a59SPeter Wemm if (newexecsw == NULL) 1089a7cddfedSJake Burkholder return (ENOMEM); 1090aa855a59SPeter Wemm xs = newexecsw; 1091aa855a59SPeter Wemm for (es = execsw; *es; es++) 1092aa855a59SPeter Wemm if (*es != execsw_arg) 1093aa855a59SPeter Wemm *xs++ = *es; 1094aa855a59SPeter Wemm *xs = NULL; 1095aa855a59SPeter Wemm if (execsw) 1096aa855a59SPeter Wemm free(execsw, M_TEMP); 1097aa855a59SPeter Wemm execsw = newexecsw; 1098a7cddfedSJake Burkholder return (0); 1099aa855a59SPeter Wemm } 110021d56e9cSAlfred Perlstein 110121d56e9cSAlfred Perlstein int 110221d56e9cSAlfred Perlstein at_exec(function) 110321d56e9cSAlfred Perlstein execlist_fn function; 110421d56e9cSAlfred Perlstein { 110521d56e9cSAlfred Perlstein struct execlist *ep; 110621d56e9cSAlfred Perlstein 110721d56e9cSAlfred Perlstein #ifdef INVARIANTS 110821d56e9cSAlfred Perlstein /* Be noisy if the programmer has lost track of things */ 110921d56e9cSAlfred Perlstein if (rm_at_exec(function)) 111021d56e9cSAlfred Perlstein printf("WARNING: exec callout entry (%p) already present\n", 111121d56e9cSAlfred Perlstein function); 111221d56e9cSAlfred Perlstein #endif 111321d56e9cSAlfred Perlstein ep = malloc(sizeof(*ep), M_ATEXEC, M_NOWAIT); 111421d56e9cSAlfred Perlstein if (ep == NULL) 111521d56e9cSAlfred Perlstein return (ENOMEM); 111621d56e9cSAlfred Perlstein ep->function = function; 111721d56e9cSAlfred Perlstein TAILQ_INSERT_TAIL(&exec_list, ep, next); 111821d56e9cSAlfred Perlstein return (0); 111921d56e9cSAlfred Perlstein } 112021d56e9cSAlfred Perlstein 112121d56e9cSAlfred Perlstein /* 112221d56e9cSAlfred Perlstein * Scan the exec callout list for the given item and remove it. 112321d56e9cSAlfred Perlstein * Returns the number of items removed (0 or 1) 112421d56e9cSAlfred Perlstein */ 112521d56e9cSAlfred Perlstein int 112621d56e9cSAlfred Perlstein rm_at_exec(function) 112721d56e9cSAlfred Perlstein execlist_fn function; 112821d56e9cSAlfred Perlstein { 112921d56e9cSAlfred Perlstein struct execlist *ep; 113021d56e9cSAlfred Perlstein 113121d56e9cSAlfred Perlstein TAILQ_FOREACH(ep, &exec_list, next) { 113221d56e9cSAlfred Perlstein if (ep->function == function) { 113321d56e9cSAlfred Perlstein TAILQ_REMOVE(&exec_list, ep, next); 113421d56e9cSAlfred Perlstein free(ep, M_ATEXEC); 113521d56e9cSAlfred Perlstein return (1); 113621d56e9cSAlfred Perlstein } 113721d56e9cSAlfred Perlstein } 113821d56e9cSAlfred Perlstein return (0); 113921d56e9cSAlfred Perlstein } 1140