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); 47263c9e754SJohn Baldwin if (error != 0) 47369be5db9SAlfred Perlstein goto done1; 474e1b1aa3bSJohn Baldwin PROC_LOCK(p); 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 546e1b1aa3bSJohn Baldwin /* Cache arguments if they fit inside our allowance */ 547e1b1aa3bSJohn Baldwin if (ps_arg_cache_limit >= i + sizeof(struct pargs)) { 548e1b1aa3bSJohn Baldwin bcopy(imgp->stringbase, newargs->ar_args, i); 549e1b1aa3bSJohn Baldwin p->p_args = newargs; 550e1b1aa3bSJohn Baldwin newargs = NULL; 551e1b1aa3bSJohn Baldwin } 552e1b1aa3bSJohn Baldwin PROC_UNLOCK(p); 553e1b1aa3bSJohn Baldwin 554e913ca22SDoug Rabson /* Set values passed into the program in registers. */ 5553ebc1248SPeter Wemm if (p->p_sysent->sv_setregs) 5563ebc1248SPeter Wemm (*p->p_sysent->sv_setregs)(td, imgp->entry_addr, 5573ebc1248SPeter Wemm (u_long)(uintptr_t)stack_base, imgp->ps_strings); 5583ebc1248SPeter Wemm else 559bafbd492SJake Burkholder exec_setregs(td, imgp->entry_addr, 560bafbd492SJake Burkholder (u_long)(uintptr_t)stack_base, imgp->ps_strings); 561e913ca22SDoug Rabson 56269be5db9SAlfred Perlstein done1: 5639b3b1c5fSJohn Baldwin /* 5649b3b1c5fSJohn Baldwin * Free any resources malloc'd earlier that we didn't use. 5659b3b1c5fSJohn Baldwin */ 5661419eacbSAlfred Perlstein uifree(euip); 5679b3b1c5fSJohn Baldwin if (newcred == NULL) 5689b3b1c5fSJohn Baldwin crfree(oldcred); 5699b3b1c5fSJohn Baldwin else 5709b3b1c5fSJohn Baldwin crfree(newcred); 5719b3b1c5fSJohn Baldwin /* 5729b3b1c5fSJohn Baldwin * Handle deferred decrement of ref counts. 5739b3b1c5fSJohn Baldwin */ 5749b3b1c5fSJohn Baldwin if (textvp != NULL) 5759b3b1c5fSJohn Baldwin vrele(textvp); 576619eb6e5SJeff Roberson if (ndp->ni_vp && error != 0) 577619eb6e5SJeff Roberson vrele(ndp->ni_vp); 5786c84de02SJohn Baldwin #ifdef KTRACE 5799b3b1c5fSJohn Baldwin if (tracevp != NULL) 5809b3b1c5fSJohn Baldwin vrele(tracevp); 5816c84de02SJohn Baldwin #endif 58269be5db9SAlfred Perlstein if (oldargs != NULL) 5839b3b1c5fSJohn Baldwin pargs_drop(oldargs); 58469be5db9SAlfred Perlstein if (newargs != NULL) 58569be5db9SAlfred Perlstein pargs_drop(newargs); 586b9df5231SPoul-Henning Kamp 5871616db3cSJohn Dyson exec_fail_dealloc: 5881616db3cSJohn Dyson 58926f9a767SRodney W. Grimes /* 59026f9a767SRodney W. Grimes * free various allocated resources 59126f9a767SRodney W. Grimes */ 5921616db3cSJohn Dyson if (imgp->firstpage) 5931616db3cSJohn Dyson exec_unmap_first_page(imgp); 59426f9a767SRodney W. Grimes 595c2f9f36bSDavid Greenman if (imgp->stringbase != NULL) 5961616db3cSJohn Dyson kmem_free_wakeup(exec_map, (vm_offset_t)imgp->stringbase, 5971616db3cSJohn Dyson ARG_MAX + PAGE_SIZE); 5981616db3cSJohn Dyson 5999c0fed3dSDoug Rabson if (imgp->vp) { 600762e6b85SEivind Eklund NDFREE(ndp, NDF_ONLY_PNBUF); 601619eb6e5SJeff Roberson vput(imgp->vp); 602a3cf6ebaSDavid Greenman } 60326f9a767SRodney W. Grimes 6040b2ed1aeSJeff Roberson if (imgp->object) 6050b2ed1aeSJeff Roberson vm_object_deallocate(imgp->object); 6060b2ed1aeSJeff Roberson 6071616db3cSJohn Dyson if (error == 0) 608116734c4SMatthew Dillon goto done2; 6091616db3cSJohn Dyson 61026f9a767SRodney W. Grimes exec_fail: 6119ca45e81SDag-Erling Smørgrav /* we're done here, clear P_INEXEC */ 6129ca45e81SDag-Erling Smørgrav PROC_LOCK(p); 6139ca45e81SDag-Erling Smørgrav p->p_flag &= ~P_INEXEC; 6149ca45e81SDag-Erling Smørgrav PROC_UNLOCK(p); 6159ca45e81SDag-Erling Smørgrav 616c52007c2SDavid Greenman if (imgp->vmspace_destroyed) { 61726f9a767SRodney W. Grimes /* sorry, no more process anymore. exit gracefully */ 618b40ce416SJulian Elischer exit1(td, W_EXITCODE(0, SIGABRT)); 61926f9a767SRodney W. Grimes /* NOT REACHED */ 620116734c4SMatthew Dillon error = 0; 62126f9a767SRodney W. Grimes } 622116734c4SMatthew Dillon done2: 623116734c4SMatthew Dillon mtx_unlock(&Giant); 624116734c4SMatthew Dillon return (error); 62526f9a767SRodney W. Grimes } 62626f9a767SRodney W. Grimes 6271616db3cSJohn Dyson int 6281616db3cSJohn Dyson exec_map_first_page(imgp) 6291616db3cSJohn Dyson struct image_params *imgp; 6301616db3cSJohn Dyson { 631d8aad40cSJohn Baldwin int rv, i; 63295461b45SJohn Dyson int initial_pagein; 63395461b45SJohn Dyson vm_page_t ma[VM_INITIAL_PAGEIN]; 6341616db3cSJohn Dyson vm_object_t object; 6351616db3cSJohn Dyson 6360cddd8f0SMatthew Dillon GIANT_REQUIRED; 6371616db3cSJohn Dyson 6381616db3cSJohn Dyson if (imgp->firstpage) { 6391616db3cSJohn Dyson exec_unmap_first_page(imgp); 6401616db3cSJohn Dyson } 6411616db3cSJohn Dyson 6429ff5ce6bSBoris Popov VOP_GETVOBJECT(imgp->vp, &object); 6431616db3cSJohn Dyson 64495461b45SJohn Dyson ma[0] = vm_page_grab(object, 0, VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 6451616db3cSJohn Dyson 64695461b45SJohn Dyson if ((ma[0]->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) { 64795461b45SJohn Dyson initial_pagein = VM_INITIAL_PAGEIN; 64895461b45SJohn Dyson if (initial_pagein > object->size) 64995461b45SJohn Dyson initial_pagein = object->size; 65095461b45SJohn Dyson for (i = 1; i < initial_pagein; i++) { 651d254af07SMatthew Dillon if ((ma[i] = vm_page_lookup(object, i)) != NULL) { 65295461b45SJohn Dyson if ((ma[i]->flags & PG_BUSY) || ma[i]->busy) 65395461b45SJohn Dyson break; 65495461b45SJohn Dyson if (ma[i]->valid) 65595461b45SJohn Dyson break; 656e69763a3SDoug Rabson vm_page_busy(ma[i]); 65795461b45SJohn Dyson } else { 658ca0387efSJake Burkholder ma[i] = vm_page_alloc(object, i, 659ca0387efSJake Burkholder VM_ALLOC_NORMAL); 66095461b45SJohn Dyson if (ma[i] == NULL) 66195461b45SJohn Dyson break; 66295461b45SJohn Dyson } 66395461b45SJohn Dyson } 66495461b45SJohn Dyson initial_pagein = i; 6651616db3cSJohn Dyson 66695461b45SJohn Dyson rv = vm_pager_get_pages(object, ma, initial_pagein, 0); 66795461b45SJohn Dyson ma[0] = vm_page_lookup(object, 0); 66895461b45SJohn Dyson 669ca0387efSJake Burkholder if ((rv != VM_PAGER_OK) || (ma[0] == NULL) || 670ca0387efSJake Burkholder (ma[0]->valid == 0)) { 671ecbb00a2SDoug Rabson if (ma[0]) { 67297646f56SAlan Cox vm_page_lock_queues(); 67395461b45SJohn Dyson vm_page_protect(ma[0], VM_PROT_NONE); 6748f9110f6SJohn Dyson vm_page_free(ma[0]); 67597646f56SAlan Cox vm_page_unlock_queues(); 676ecbb00a2SDoug Rabson } 677a7cddfedSJake Burkholder return (EIO); 6781616db3cSJohn Dyson } 6791616db3cSJohn Dyson } 68097646f56SAlan Cox vm_page_lock_queues(); 68195461b45SJohn Dyson vm_page_wire(ma[0]); 682e69763a3SDoug Rabson vm_page_wakeup(ma[0]); 68397646f56SAlan Cox vm_page_unlock_queues(); 6841616db3cSJohn Dyson 685ac59490bSJake Burkholder pmap_qenter((vm_offset_t)imgp->image_header, ma, 1); 68695461b45SJohn Dyson imgp->firstpage = ma[0]; 6871616db3cSJohn Dyson 688a7cddfedSJake Burkholder return (0); 6891616db3cSJohn Dyson } 6901616db3cSJohn Dyson 6911616db3cSJohn Dyson void 6921616db3cSJohn Dyson exec_unmap_first_page(imgp) 6931616db3cSJohn Dyson struct image_params *imgp; 6941616db3cSJohn Dyson { 6950cddd8f0SMatthew Dillon GIANT_REQUIRED; 69623955314SAlfred Perlstein 6971616db3cSJohn Dyson if (imgp->firstpage) { 698ac59490bSJake Burkholder pmap_qremove((vm_offset_t)imgp->image_header, 1); 69997646f56SAlan Cox vm_page_lock_queues(); 70073007561SDavid Greenman vm_page_unwire(imgp->firstpage, 1); 70197646f56SAlan Cox vm_page_unlock_queues(); 7021616db3cSJohn Dyson imgp->firstpage = NULL; 7031616db3cSJohn Dyson } 7041616db3cSJohn Dyson } 7051616db3cSJohn Dyson 70626f9a767SRodney W. Grimes /* 70726f9a767SRodney W. Grimes * Destroy old address space, and allocate a new stack 70826f9a767SRodney W. Grimes * The new stack is only SGROWSIZ large because it is grown 70926f9a767SRodney W. Grimes * automatically in trap.c. 71026f9a767SRodney W. Grimes */ 71126f9a767SRodney W. Grimes int 71205ba50f5SJake Burkholder exec_new_vmspace(imgp, sv) 713c52007c2SDavid Greenman struct image_params *imgp; 71405ba50f5SJake Burkholder struct sysentvec *sv; 71526f9a767SRodney W. Grimes { 71626f9a767SRodney W. Grimes int error; 7176f5dafeaSAlan Cox struct execlist *ep; 7185e20c11fSAlan Cox struct proc *p = imgp->proc; 7195e20c11fSAlan Cox struct vmspace *vmspace = p->p_vmspace; 72005ba50f5SJake Burkholder vm_offset_t stack_addr; 72105ba50f5SJake Burkholder vm_map_t map; 72226f9a767SRodney W. Grimes 7230cddd8f0SMatthew Dillon GIANT_REQUIRED; 7240cddd8f0SMatthew Dillon 72505ba50f5SJake Burkholder stack_addr = sv->sv_usrstack - maxssiz; 7263ebc1248SPeter Wemm 727c52007c2SDavid Greenman imgp->vmspace_destroyed = 1; 72826f9a767SRodney W. Grimes 729af9ec885SJohn Dyson /* 7306f5dafeaSAlan Cox * Perform functions registered with at_exec(). 7316f5dafeaSAlan Cox */ 7326f5dafeaSAlan Cox TAILQ_FOREACH(ep, &exec_list, next) 7335e20c11fSAlan Cox (*ep->function)(p); 7346f5dafeaSAlan Cox 7356f5dafeaSAlan Cox /* 736af9ec885SJohn Dyson * Blow away entire process VM, if address space not shared, 737af9ec885SJohn Dyson * otherwise, create a new VM space so that other threads are 738af9ec885SJohn Dyson * not disrupted 739af9ec885SJohn Dyson */ 74005ba50f5SJake Burkholder map = &vmspace->vm_map; 74105ba50f5SJake Burkholder if (vmspace->vm_refcnt == 1 && vm_map_min(map) == sv->sv_minuser && 74205ba50f5SJake Burkholder vm_map_max(map) == sv->sv_maxuser) { 7433d903220SDoug Rabson if (vmspace->vm_shm) 7445e20c11fSAlan Cox shmexit(p); 74505ba50f5SJake Burkholder pmap_remove_pages(vmspace_pmap(vmspace), vm_map_min(map), 74605ba50f5SJake Burkholder vm_map_max(map)); 74705ba50f5SJake Burkholder vm_map_remove(map, vm_map_min(map), vm_map_max(map)); 748af9ec885SJohn Dyson } else { 74905ba50f5SJake Burkholder vmspace_exec(p, sv->sv_minuser, sv->sv_maxuser); 7505e20c11fSAlan Cox vmspace = p->p_vmspace; 75105ba50f5SJake Burkholder map = &vmspace->vm_map; 752af9ec885SJohn Dyson } 75326f9a767SRodney W. Grimes 75426f9a767SRodney W. Grimes /* Allocate a new stack */ 75505ba50f5SJake Burkholder error = vm_map_stack(map, stack_addr, (vm_size_t)maxssiz, 75605ba50f5SJake Burkholder sv->sv_stackprot, VM_PROT_ALL, 0); 7572267af78SJulian Elischer if (error) 7582267af78SJulian Elischer return (error); 7592267af78SJulian Elischer 76063c47a5cSDoug Rabson #ifdef __ia64__ 76163c47a5cSDoug Rabson { 76263c47a5cSDoug Rabson /* 76363c47a5cSDoug Rabson * Allocate backing store. We really need something 76463c47a5cSDoug Rabson * similar to vm_map_stack which can allow the backing 76563c47a5cSDoug Rabson * store to grow upwards. This will do for now. 76663c47a5cSDoug Rabson */ 76763c47a5cSDoug Rabson vm_offset_t bsaddr; 76805ba50f5SJake Burkholder bsaddr = p->p_sysent->sv_usrstack - 2 * maxssiz; 76905ba50f5SJake Burkholder error = vm_map_find(map, 0, 0, &bsaddr, 77081f223caSJake Burkholder regstkpages * PAGE_SIZE, 0, VM_PROT_ALL, VM_PROT_ALL, 0); 7715e20c11fSAlan Cox FIRST_THREAD_IN_PROC(p)->td_md.md_bspstore = bsaddr; 77263c47a5cSDoug Rabson } 77363c47a5cSDoug Rabson #endif 77463c47a5cSDoug Rabson 7752267af78SJulian Elischer /* vm_ssize and vm_maxsaddr are somewhat antiquated concepts in the 7762267af78SJulian Elischer * VM_STACK case, but they are still used to monitor the size of the 7772267af78SJulian Elischer * process stack so we can check the stack rlimit. 7782267af78SJulian Elischer */ 779cbc89bfbSPaul Saab vmspace->vm_ssize = sgrowsiz >> PAGE_SHIFT; 78005ba50f5SJake Burkholder vmspace->vm_maxsaddr = (char *)sv->sv_usrstack - maxssiz; 78126f9a767SRodney W. Grimes 78226f9a767SRodney W. Grimes return (0); 78326f9a767SRodney W. Grimes } 78426f9a767SRodney W. Grimes 78526f9a767SRodney W. Grimes /* 78626f9a767SRodney W. Grimes * Copy out argument and environment strings from the old process 78726f9a767SRodney W. Grimes * address space into the temporary string buffer. 78826f9a767SRodney W. Grimes */ 78926f9a767SRodney W. Grimes int 790c52007c2SDavid Greenman exec_extract_strings(imgp) 791c52007c2SDavid Greenman struct image_params *imgp; 79226f9a767SRodney W. Grimes { 79326f9a767SRodney W. Grimes char **argv, **envv; 79426f9a767SRodney W. Grimes char *argp, *envp; 795ecbb00a2SDoug Rabson int error; 796ecbb00a2SDoug Rabson size_t length; 79726f9a767SRodney W. Grimes 79826f9a767SRodney W. Grimes /* 79926f9a767SRodney W. Grimes * extract arguments first 80026f9a767SRodney W. Grimes */ 80126f9a767SRodney W. Grimes 802c52007c2SDavid Greenman argv = imgp->uap->argv; 80326f9a767SRodney W. Grimes 8040fd1a014SDavid Greenman if (argv) { 805aae0aa45SBruce Evans argp = (caddr_t)(intptr_t)fuword(argv); 8065cf3d12cSAndrey A. Chernov if (argp == (caddr_t)-1) 8075cf3d12cSAndrey A. Chernov return (EFAULT); 8085cf3d12cSAndrey A. Chernov if (argp) 8095cf3d12cSAndrey A. Chernov argv++; 8105cf3d12cSAndrey A. Chernov if (imgp->argv0) 8115cf3d12cSAndrey A. Chernov argp = imgp->argv0; 8125cf3d12cSAndrey A. Chernov if (argp) { 8135cf3d12cSAndrey A. Chernov do { 81426f9a767SRodney W. Grimes if (argp == (caddr_t)-1) 81526f9a767SRodney W. Grimes return (EFAULT); 816c52007c2SDavid Greenman if ((error = copyinstr(argp, imgp->stringp, 817c52007c2SDavid Greenman imgp->stringspace, &length))) { 8180fd1a014SDavid Greenman if (error == ENAMETOOLONG) 81926f9a767SRodney W. Grimes return (E2BIG); 8200fd1a014SDavid Greenman return (error); 8210fd1a014SDavid Greenman } 822c52007c2SDavid Greenman imgp->stringspace -= length; 823c52007c2SDavid Greenman imgp->stringp += length; 824c52007c2SDavid Greenman imgp->argc++; 825aae0aa45SBruce Evans } while ((argp = (caddr_t)(intptr_t)fuword(argv++))); 82626f9a767SRodney W. Grimes } 8270fd1a014SDavid Greenman } 82826f9a767SRodney W. Grimes 829b9df5231SPoul-Henning Kamp imgp->endargs = imgp->stringp; 830b9df5231SPoul-Henning Kamp 83126f9a767SRodney W. Grimes /* 83226f9a767SRodney W. Grimes * extract environment strings 83326f9a767SRodney W. Grimes */ 83426f9a767SRodney W. Grimes 835c52007c2SDavid Greenman envv = imgp->uap->envv; 83626f9a767SRodney W. Grimes 8370fd1a014SDavid Greenman if (envv) { 838aae0aa45SBruce Evans while ((envp = (caddr_t)(intptr_t)fuword(envv++))) { 83926f9a767SRodney W. Grimes if (envp == (caddr_t)-1) 84026f9a767SRodney W. Grimes return (EFAULT); 841c52007c2SDavid Greenman if ((error = copyinstr(envp, imgp->stringp, 842c52007c2SDavid Greenman imgp->stringspace, &length))) { 8430fd1a014SDavid Greenman if (error == ENAMETOOLONG) 84426f9a767SRodney W. Grimes return (E2BIG); 8450fd1a014SDavid Greenman return (error); 8460fd1a014SDavid Greenman } 847c52007c2SDavid Greenman imgp->stringspace -= length; 848c52007c2SDavid Greenman imgp->stringp += length; 849c52007c2SDavid Greenman imgp->envc++; 85026f9a767SRodney W. Grimes } 8510fd1a014SDavid Greenman } 85226f9a767SRodney W. Grimes 85326f9a767SRodney W. Grimes return (0); 85426f9a767SRodney W. Grimes } 85526f9a767SRodney W. Grimes 85626f9a767SRodney W. Grimes /* 85726f9a767SRodney W. Grimes * Copy strings out to the new process address space, constructing 85826f9a767SRodney W. Grimes * new arg and env vector tables. Return a pointer to the base 85926f9a767SRodney W. Grimes * so that it can be used as the initial stack pointer. 86026f9a767SRodney W. Grimes */ 861654f6be1SBruce Evans register_t * 862c52007c2SDavid Greenman exec_copyout_strings(imgp) 863c52007c2SDavid Greenman struct image_params *imgp; 86426f9a767SRodney W. Grimes { 86526f9a767SRodney W. Grimes int argc, envc; 86626f9a767SRodney W. Grimes char **vectp; 86726f9a767SRodney W. Grimes char *stringp, *destp; 868654f6be1SBruce Evans register_t *stack_base; 86993f6448cSDavid Greenman struct ps_strings *arginfo; 870f3bec5d7SJake Burkholder struct proc *p; 871d66a5066SPeter Wemm int szsigcode; 87226f9a767SRodney W. Grimes 87326f9a767SRodney W. Grimes /* 87426f9a767SRodney W. Grimes * Calculate string base and vector table pointers. 875d66a5066SPeter Wemm * Also deal with signal trampoline code for this exec type. 87626f9a767SRodney W. Grimes */ 877f3bec5d7SJake Burkholder p = imgp->proc; 878f3bec5d7SJake Burkholder szsigcode = 0; 87905ba50f5SJake Burkholder arginfo = (struct ps_strings *)p->p_sysent->sv_psstrings; 880f3bec5d7SJake Burkholder if (p->p_sysent->sv_szsigcode != NULL) 881f3bec5d7SJake Burkholder szsigcode = *(p->p_sysent->sv_szsigcode); 882d66a5066SPeter Wemm destp = (caddr_t)arginfo - szsigcode - SPARE_USRSPACE - 8831ed012f9SPeter Wemm roundup((ARG_MAX - imgp->stringspace), sizeof(char *)); 8841ed012f9SPeter Wemm 88526f9a767SRodney W. Grimes /* 886d66a5066SPeter Wemm * install sigcode 887d66a5066SPeter Wemm */ 888d66a5066SPeter Wemm if (szsigcode) 889f3bec5d7SJake Burkholder copyout(p->p_sysent->sv_sigcode, ((caddr_t)arginfo - 890f3bec5d7SJake Burkholder szsigcode), szsigcode); 891d66a5066SPeter Wemm 892d66a5066SPeter Wemm /* 893e1743d02SSøren Schmidt * If we have a valid auxargs ptr, prepare some room 894e1743d02SSøren Schmidt * on the stack. 895e1743d02SSøren Schmidt */ 896b9a22da4STakanori Watanabe if (imgp->auxargs) { 897e1743d02SSøren Schmidt /* 898b9a22da4STakanori Watanabe * 'AT_COUNT*2' is size for the ELF Auxargs data. This is for 899b9a22da4STakanori Watanabe * lower compatibility. 900b9a22da4STakanori Watanabe */ 901ca0387efSJake Burkholder imgp->auxarg_size = (imgp->auxarg_size) ? imgp->auxarg_size : 902ca0387efSJake Burkholder (AT_COUNT * 2); 903b9a22da4STakanori Watanabe /* 904b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 905b9a22da4STakanori Watanabe * the arg and env vector sets,and imgp->auxarg_size is room 906b9a22da4STakanori Watanabe * for argument of Runtime loader. 907e1743d02SSøren Schmidt */ 908e1743d02SSøren Schmidt vectp = (char **)(destp - (imgp->argc + imgp->envc + 2 + 909b9a22da4STakanori Watanabe imgp->auxarg_size) * sizeof(char *)); 910b9a22da4STakanori Watanabe 911b9a22da4STakanori Watanabe } else 912e1743d02SSøren Schmidt /* 913b9a22da4STakanori Watanabe * The '+ 2' is for the null pointers at the end of each of 914b9a22da4STakanori Watanabe * the arg and env vector sets 91526f9a767SRodney W. Grimes */ 916ca0387efSJake Burkholder vectp = (char **)(destp - (imgp->argc + imgp->envc + 2) * 917ca0387efSJake Burkholder sizeof(char *)); 91826f9a767SRodney W. Grimes 91926f9a767SRodney W. Grimes /* 92026f9a767SRodney W. Grimes * vectp also becomes our initial stack base 92126f9a767SRodney W. Grimes */ 922654f6be1SBruce Evans stack_base = (register_t *)vectp; 92326f9a767SRodney W. Grimes 924c52007c2SDavid Greenman stringp = imgp->stringbase; 925c52007c2SDavid Greenman argc = imgp->argc; 926c52007c2SDavid Greenman envc = imgp->envc; 92726f9a767SRodney W. Grimes 92893f6448cSDavid Greenman /* 9293aed948bSDavid Greenman * Copy out strings - arguments and environment. 93093f6448cSDavid Greenman */ 931c52007c2SDavid Greenman copyout(stringp, destp, ARG_MAX - imgp->stringspace); 93293f6448cSDavid Greenman 93393f6448cSDavid Greenman /* 9343aed948bSDavid Greenman * Fill in "ps_strings" struct for ps, w, etc. 9353aed948bSDavid Greenman */ 936aae0aa45SBruce Evans suword(&arginfo->ps_argvstr, (long)(intptr_t)vectp); 9373aed948bSDavid Greenman suword(&arginfo->ps_nargvstr, argc); 9383aed948bSDavid Greenman 9393aed948bSDavid Greenman /* 9403aed948bSDavid Greenman * Fill in argument portion of vector table. 94193f6448cSDavid Greenman */ 94226f9a767SRodney W. Grimes for (; argc > 0; --argc) { 943aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 9443aed948bSDavid Greenman while (*stringp++ != 0) 9453aed948bSDavid Greenman destp++; 9463aed948bSDavid Greenman destp++; 94726f9a767SRodney W. Grimes } 94826f9a767SRodney W. Grimes 9491a6e52d0SJeroen Ruigrok van der Werven /* a null vector table pointer separates the argp's from the envp's */ 9506ab46d52SBruce Evans suword(vectp++, 0); 95126f9a767SRodney W. Grimes 952aae0aa45SBruce Evans suword(&arginfo->ps_envstr, (long)(intptr_t)vectp); 9533aed948bSDavid Greenman suword(&arginfo->ps_nenvstr, envc); 95493f6448cSDavid Greenman 95593f6448cSDavid Greenman /* 9563aed948bSDavid Greenman * Fill in environment portion of vector table. 95793f6448cSDavid Greenman */ 95826f9a767SRodney W. Grimes for (; envc > 0; --envc) { 959aae0aa45SBruce Evans suword(vectp++, (long)(intptr_t)destp); 9603aed948bSDavid Greenman while (*stringp++ != 0) 9613aed948bSDavid Greenman destp++; 9623aed948bSDavid Greenman destp++; 96326f9a767SRodney W. Grimes } 96426f9a767SRodney W. Grimes 96526f9a767SRodney W. Grimes /* end of vector table is a null pointer */ 9666ab46d52SBruce Evans suword(vectp, 0); 96726f9a767SRodney W. Grimes 96826f9a767SRodney W. Grimes return (stack_base); 96926f9a767SRodney W. Grimes } 97026f9a767SRodney W. Grimes 97126f9a767SRodney W. Grimes /* 97226f9a767SRodney W. Grimes * Check permissions of file to execute. 973cf64863aSRobert Watson * Called with imgp->vp locked. 97426f9a767SRodney W. Grimes * Return 0 for success or error code on failure. 97526f9a767SRodney W. Grimes */ 976c8a79999SPeter Wemm int 977c52007c2SDavid Greenman exec_check_permissions(imgp) 978c52007c2SDavid Greenman struct image_params *imgp; 97926f9a767SRodney W. Grimes { 980c52007c2SDavid Greenman struct vnode *vp = imgp->vp; 981c52007c2SDavid Greenman struct vattr *attr = imgp->attr; 982a854ed98SJohn Baldwin struct thread *td; 98326f9a767SRodney W. Grimes int error; 98426f9a767SRodney W. Grimes 985a854ed98SJohn Baldwin td = curthread; /* XXXKSE */ 986339b79b9SRobert Watson 987339b79b9SRobert Watson #ifdef MAC 988339b79b9SRobert Watson error = mac_check_vnode_exec(td->td_ucred, imgp->vp); 989339b79b9SRobert Watson if (error) 990339b79b9SRobert Watson return (error); 991339b79b9SRobert Watson #endif 992339b79b9SRobert Watson 99326f9a767SRodney W. Grimes /* Get file attributes */ 994a854ed98SJohn Baldwin error = VOP_GETATTR(vp, attr, td->td_ucred, td); 99526f9a767SRodney W. Grimes if (error) 99626f9a767SRodney W. Grimes return (error); 99726f9a767SRodney W. Grimes 99826f9a767SRodney W. Grimes /* 99926f9a767SRodney W. Grimes * 1) Check if file execution is disabled for the filesystem that this 100026f9a767SRodney W. Grimes * file resides on. 100126f9a767SRodney W. Grimes * 2) Insure that at least one execute bit is on - otherwise root 100226f9a767SRodney W. Grimes * will always succeed, and we don't want to happen unless the 100326f9a767SRodney W. Grimes * file really is executable. 100426f9a767SRodney W. Grimes * 3) Insure that the file is a regular file. 100526f9a767SRodney W. Grimes */ 1006c52007c2SDavid Greenman if ((vp->v_mount->mnt_flag & MNT_NOEXEC) || 100726f9a767SRodney W. Grimes ((attr->va_mode & 0111) == 0) || 1008a854ed98SJohn Baldwin (attr->va_type != VREG)) 100926f9a767SRodney W. Grimes return (EACCES); 101026f9a767SRodney W. Grimes 101126f9a767SRodney W. Grimes /* 101226f9a767SRodney W. Grimes * Zero length files can't be exec'd 101326f9a767SRodney W. Grimes */ 101426f9a767SRodney W. Grimes if (attr->va_size == 0) 101526f9a767SRodney W. Grimes return (ENOEXEC); 101626f9a767SRodney W. Grimes 101726f9a767SRodney W. Grimes /* 101826f9a767SRodney W. Grimes * Check for execute permission to file based on current credentials. 101926f9a767SRodney W. Grimes */ 1020a854ed98SJohn Baldwin error = VOP_ACCESS(vp, VEXEC, td->td_ucred, td); 102126f9a767SRodney W. Grimes if (error) 102226f9a767SRodney W. Grimes return (error); 102326f9a767SRodney W. Grimes 10246d5a0a8cSDavid Greenman /* 10256d5a0a8cSDavid Greenman * Check number of open-for-writes on the file and deny execution 10266d5a0a8cSDavid Greenman * if there are any. 10276d5a0a8cSDavid Greenman */ 10286d5a0a8cSDavid Greenman if (vp->v_writecount) 10296d5a0a8cSDavid Greenman return (ETXTBSY); 10306d5a0a8cSDavid Greenman 10316d5a0a8cSDavid Greenman /* 10326d5a0a8cSDavid Greenman * Call filesystem specific open routine (which does nothing in the 10336d5a0a8cSDavid Greenman * general case). 10346d5a0a8cSDavid Greenman */ 1035a854ed98SJohn Baldwin error = VOP_OPEN(vp, FREAD, td->td_ucred, td); 103626f9a767SRodney W. Grimes return (error); 1037df8bae1dSRodney W. Grimes } 1038aa855a59SPeter Wemm 1039aa855a59SPeter Wemm /* 1040aa855a59SPeter Wemm * Exec handler registration 1041aa855a59SPeter Wemm */ 1042aa855a59SPeter Wemm int 1043aa855a59SPeter Wemm exec_register(execsw_arg) 1044aa855a59SPeter Wemm const struct execsw *execsw_arg; 1045aa855a59SPeter Wemm { 1046aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1047aa855a59SPeter Wemm int count = 2; /* New slot and trailing NULL */ 1048aa855a59SPeter Wemm 1049aa855a59SPeter Wemm if (execsw) 1050aa855a59SPeter Wemm for (es = execsw; *es; es++) 1051aa855a59SPeter Wemm count++; 1052aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1053aa855a59SPeter Wemm if (newexecsw == NULL) 1054a7cddfedSJake Burkholder return (ENOMEM); 1055aa855a59SPeter Wemm xs = newexecsw; 1056aa855a59SPeter Wemm if (execsw) 1057aa855a59SPeter Wemm for (es = execsw; *es; es++) 1058aa855a59SPeter Wemm *xs++ = *es; 1059aa855a59SPeter Wemm *xs++ = execsw_arg; 1060aa855a59SPeter Wemm *xs = NULL; 1061aa855a59SPeter Wemm if (execsw) 1062aa855a59SPeter Wemm free(execsw, M_TEMP); 1063aa855a59SPeter Wemm execsw = newexecsw; 1064a7cddfedSJake Burkholder return (0); 1065aa855a59SPeter Wemm } 1066aa855a59SPeter Wemm 1067aa855a59SPeter Wemm int 1068aa855a59SPeter Wemm exec_unregister(execsw_arg) 1069aa855a59SPeter Wemm const struct execsw *execsw_arg; 1070aa855a59SPeter Wemm { 1071aa855a59SPeter Wemm const struct execsw **es, **xs, **newexecsw; 1072aa855a59SPeter Wemm int count = 1; 1073aa855a59SPeter Wemm 1074aa855a59SPeter Wemm if (execsw == NULL) 1075aa855a59SPeter Wemm panic("unregister with no handlers left?\n"); 1076aa855a59SPeter Wemm 1077aa855a59SPeter Wemm for (es = execsw; *es; es++) { 1078aa855a59SPeter Wemm if (*es == execsw_arg) 1079aa855a59SPeter Wemm break; 1080aa855a59SPeter Wemm } 1081aa855a59SPeter Wemm if (*es == NULL) 1082a7cddfedSJake Burkholder return (ENOENT); 1083aa855a59SPeter Wemm for (es = execsw; *es; es++) 1084aa855a59SPeter Wemm if (*es != execsw_arg) 1085aa855a59SPeter Wemm count++; 1086aa855a59SPeter Wemm newexecsw = malloc(count * sizeof(*es), M_TEMP, M_WAITOK); 1087aa855a59SPeter Wemm if (newexecsw == NULL) 1088a7cddfedSJake Burkholder return (ENOMEM); 1089aa855a59SPeter Wemm xs = newexecsw; 1090aa855a59SPeter Wemm for (es = execsw; *es; es++) 1091aa855a59SPeter Wemm if (*es != execsw_arg) 1092aa855a59SPeter Wemm *xs++ = *es; 1093aa855a59SPeter Wemm *xs = NULL; 1094aa855a59SPeter Wemm if (execsw) 1095aa855a59SPeter Wemm free(execsw, M_TEMP); 1096aa855a59SPeter Wemm execsw = newexecsw; 1097a7cddfedSJake Burkholder return (0); 1098aa855a59SPeter Wemm } 109921d56e9cSAlfred Perlstein 110021d56e9cSAlfred Perlstein int 110121d56e9cSAlfred Perlstein at_exec(function) 110221d56e9cSAlfred Perlstein execlist_fn function; 110321d56e9cSAlfred Perlstein { 110421d56e9cSAlfred Perlstein struct execlist *ep; 110521d56e9cSAlfred Perlstein 110621d56e9cSAlfred Perlstein #ifdef INVARIANTS 110721d56e9cSAlfred Perlstein /* Be noisy if the programmer has lost track of things */ 110821d56e9cSAlfred Perlstein if (rm_at_exec(function)) 110921d56e9cSAlfred Perlstein printf("WARNING: exec callout entry (%p) already present\n", 111021d56e9cSAlfred Perlstein function); 111121d56e9cSAlfred Perlstein #endif 111221d56e9cSAlfred Perlstein ep = malloc(sizeof(*ep), M_ATEXEC, M_NOWAIT); 111321d56e9cSAlfred Perlstein if (ep == NULL) 111421d56e9cSAlfred Perlstein return (ENOMEM); 111521d56e9cSAlfred Perlstein ep->function = function; 111621d56e9cSAlfred Perlstein TAILQ_INSERT_TAIL(&exec_list, ep, next); 111721d56e9cSAlfred Perlstein return (0); 111821d56e9cSAlfred Perlstein } 111921d56e9cSAlfred Perlstein 112021d56e9cSAlfred Perlstein /* 112121d56e9cSAlfred Perlstein * Scan the exec callout list for the given item and remove it. 112221d56e9cSAlfred Perlstein * Returns the number of items removed (0 or 1) 112321d56e9cSAlfred Perlstein */ 112421d56e9cSAlfred Perlstein int 112521d56e9cSAlfred Perlstein rm_at_exec(function) 112621d56e9cSAlfred Perlstein execlist_fn function; 112721d56e9cSAlfred Perlstein { 112821d56e9cSAlfred Perlstein struct execlist *ep; 112921d56e9cSAlfred Perlstein 113021d56e9cSAlfred Perlstein TAILQ_FOREACH(ep, &exec_list, next) { 113121d56e9cSAlfred Perlstein if (ep->function == function) { 113221d56e9cSAlfred Perlstein TAILQ_REMOVE(&exec_list, ep, next); 113321d56e9cSAlfred Perlstein free(ep, M_ATEXEC); 113421d56e9cSAlfred Perlstein return (1); 113521d56e9cSAlfred Perlstein } 113621d56e9cSAlfred Perlstein } 113721d56e9cSAlfred Perlstein return (0); 113821d56e9cSAlfred Perlstein } 1139