EXEC(3)                    POSIX Programmer's Manual                   EXEC(3)



PPRROOLLOOGG
       This manual page is part of the POSIX Programmer's Manual.  The OS
       implementation of this interface may differ (consult the corresponding
       OS manual page for details of OS behavior), or the interface may not be
       implemented on OS.

NNAAMMEE
       environ, execl, execle, execlp, execv, execve, execvp, fexecve --
       execute a file

SSYYNNOOPPSSIISS
       #include <unistd.h>

       extern char **environ;
       int execl(const char *_p_a_t_h, const char *_a_r_g_0, ... /*, (char *)0 */);
       int execle(const char *_p_a_t_h, const char *_a_r_g_0, ... /*,
           (char *)0, char *const _e_n_v_p[]*/);
       int execlp(const char *_f_i_l_e, const char *_a_r_g_0, ... /*, (char *)0 */);
       int execv(const char *_p_a_t_h, char *const _a_r_g_v[]);
       int execve(const char *_p_a_t_h, char *const _a_r_g_v[], char *const _e_n_v_p[]);
       int execvp(const char *_f_i_l_e, char *const _a_r_g_v[]);
       int fexecve(int _f_d, char *const _a_r_g_v[], char *const _e_n_v_p[]);

DDEESSCCRRIIPPTTIIOONN
       The _e_x_e_c family of functions shall replace the current process image
       with a new process image. The new image shall be constructed from a
       regular, executable file called the _n_e_w _p_r_o_c_e_s_s _i_m_a_g_e _f_i_l_e.  There
       shall be no return from a successful _e_x_e_c, because the calling process
       image is overlaid by the new process image.

       The _f_e_x_e_c_v_e() function shall be equivalent to the _e_x_e_c_v_e() function
       except that the file to be executed is determined by the file
       descriptor _f_d instead of a pathname. The file offset of _f_d is ignored.

       When a C-language program is executed as a result of a call to one of
       the _e_x_e_c family of functions, it shall be entered as a C-language
       function call as follows:


           int main (_i_n_t _a_r_g_c_, _c_h_a_r _*_a_r_g_v[]);

       where _a_r_g_c is the argument count and _a_r_g_v is an array of character
       pointers to the arguments themselves.  In addition, the following
       variable, which must be declared by the user if it is to be used
       directly:


           extern char **environ;

       is initialized as a pointer to an array of character pointers to the
       environment strings. The _a_r_g_v and _e_n_v_i_r_o_n arrays are each terminated by
       a null pointer. The null pointer terminating the _a_r_g_v array is not
       counted in _a_r_g_c.

       Applications can change the entire environment in a single operation by
       assigning the _e_n_v_i_r_o_n variable to point to an array of character
       pointers to the new environment strings. After assigning a new value to
       _e_n_v_i_r_o_n, applications should not rely on the new environment strings
       remaining part of the environment, as a call to _g_e_t_e_n_v(), _p_u_t_e_n_v(),
       _s_e_t_e_n_v(), _u_n_s_e_t_e_n_v(), or any function that is dependent on an
       environment variable may, on noticing that _e_n_v_i_r_o_n has changed, copy
       the environment strings to a new array and assign _e_n_v_i_r_o_n to point to
       it.

       Any application that directly modifies the pointers to which the
       _e_n_v_i_r_o_n variable points has undefined behavior.

       Conforming multi-threaded applications shall not use the _e_n_v_i_r_o_n
       variable to access or modify any environment variable while any other
       thread is concurrently modifying any environment variable. A call to
       any function dependent on any environment variable shall be considered
       a use of the _e_n_v_i_r_o_n variable to access that environment variable.

       The arguments specified by a program with one of the _e_x_e_c functions
       shall be passed on to the new process image in the corresponding _m_a_i_n()
       arguments.

       The argument _p_a_t_h points to a pathname that identifies the new process
       image file.

       The argument _f_i_l_e is used to construct a pathname that identifies the
       new process image file. If the _f_i_l_e argument contains a <slash>
       character, the _f_i_l_e argument shall be used as the pathname for this
       file. Otherwise, the path prefix for this file is obtained by a search
       of the directories passed as the environment variable _P_A_T_H (see the
       Base Definitions volume of POSIX.1-2017, _C_h_a_p_t_e_r _8, _E_n_v_i_r_o_n_m_e_n_t
       _V_a_r_i_a_b_l_e_s).  If this environment variable is not present, the results
       of the search are implementation-defined.

       There are two distinct ways in which the contents of the process image
       file may cause the execution to fail, distinguished by the setting of
       _e_r_r_n_o to either [[EENNOOEEXXEECC]] or [[EEIINNVVAALL]] (see the ERRORS section). In the
       cases where the other members of the _e_x_e_c family of functions would
       fail and set _e_r_r_n_o to [[EENNOOEEXXEECC]], the _e_x_e_c_l_p() and _e_x_e_c_v_p() functions
       shall execute a command interpreter and the environment of the executed
       command shall be as if the process invoked the _s_h utility using _e_x_e_c_l()
       as follows:


           execl(<shell path>, arg0, file, arg1, ..., (char *)0);

       where <_s_h_e_l_l _p_a_t_h> is an unspecified pathname for the _s_h utility, _f_i_l_e
       is the process image file, and for _e_x_e_c_v_p(), where _a_r_g0, _a_r_g1, and so
       on correspond to the values passed to _e_x_e_c_v_p() in _a_r_g_v[0], _a_r_g_v[1], and
       so on.

       The arguments represented by _a_r_g_0,...  are pointers to null-terminated
       character strings. These strings shall constitute the argument list
       available to the new process image. The list is terminated by a null
       pointer. The argument _a_r_g_0 should point to a filename string that is
       associated with the process being started by one of the _e_x_e_c functions.

       The argument _a_r_g_v is an array of character pointers to null-terminated
       strings. The application shall ensure that the last member of this
       array is a null pointer. These strings shall constitute the argument
       list available to the new process image. The value in _a_r_g_v[0] should
       point to a filename string that is associated with the process being
       started by one of the _e_x_e_c functions.

       The argument _e_n_v_p is an array of character pointers to null-terminated
       strings. These strings shall constitute the environment for the new
       process image.  The _e_n_v_p array is terminated by a null pointer.

       For those forms not containing an _e_n_v_p pointer (_e_x_e_c_l(), _e_x_e_c_v(),
       _e_x_e_c_l_p(), and _e_x_e_c_v_p()), the environment for the new process image
       shall be taken from the external variable _e_n_v_i_r_o_n in the calling
       process.

       The number of bytes available for the new process' combined argument
       and environment lists is {ARG_MAX}.  It is implementation-defined
       whether null terminators, pointers, and/or any alignment bytes are
       included in this total.

       File descriptors open in the calling process image shall remain open in
       the new process image, except for those whose close-on-_e_x_e_c flag
       FD_CLOEXEC is set.  For those file descriptors that remain open, all
       attributes of the open file description remain unchanged. For any file
       descriptor that is closed for this reason, file locks are removed as a
       result of the close as described in _c_l_o_s_e().  Locks that are not
       removed by closing of file descriptors remain unchanged.

       If file descriptor 0, 1, or 2 would otherwise be closed after a
       successful call to one of the _e_x_e_c family of functions, implementations
       may open an unspecified file for the file descriptor in the new process
       image. If a standard utility or a conforming application is executed
       with file descriptor 0 not open for reading or with file descriptor 1
       or 2 not open for writing, the environment in which the utility or
       application is executed shall be deemed non-conforming, and
       consequently the utility or application might not behave as described
       in this standard.

       Directory streams open in the calling process image shall be closed in
       the new process image.

       The state of the floating-point environment in the initial thread of
       the new process image shall be set to the default.

       The state of conversion descriptors and message catalog descriptors in
       the new process image is undefined.

       For the new process image, the equivalent of:


           setlocale(LC_ALL, "C")

       shall be executed at start-up.

       Signals set to the default action (SIG_DFL) in the calling process
       image shall be set to the default action in the new process image.
       Except for SIGCHLD, signals set to be ignored (SIG_IGN) by the calling
       process image shall be set to be ignored by the new process image.
       Signals set to be caught by the calling process image shall be set to
       the default action in the new process image (see _<_s_i_g_n_a_l_._h_>).

       If the SIGCHLD signal is set to be ignored by the calling process
       image, it is unspecified whether the SIGCHLD signal is set to be
       ignored or to the default action in the new process image.

       After a successful call to any of the _e_x_e_c functions, alternate signal
       stacks are not preserved and the SA_ONSTACK flag shall be cleared for
       all signals.

       After a successful call to any of the _e_x_e_c functions, any functions
       previously registered by the _a_t_e_x_i_t() or _p_t_h_r_e_a_d___a_t_f_o_r_k() functions are
       no longer registered.

       If the ST_NOSUID bit is set for the file system containing the new
       process image file, then the effective user ID, effective group ID,
       saved set-user-ID, and saved set-group-ID are unchanged in the new
       process image. Otherwise, if the set-user-ID mode bit of the new
       process image file is set, the effective user ID of the new process
       image shall be set to the user ID of the new process image file.
       Similarly, if the set-group-ID mode bit of the new process image file
       is set, the effective group ID of the new process image shall be set to
       the group ID of the new process image file. The real user ID, real
       group ID, and supplementary group IDs of the new process image shall
       remain the same as those of the calling process image. The effective
       user ID and effective group ID of the new process image shall be saved
       (as the saved set-user-ID and the saved set-group-ID) for use by
       _s_e_t_u_i_d().

       Any shared memory segments attached to the calling process image shall
       not be attached to the new process image.

       Any named semaphores open in the calling process shall be closed as if
       by appropriate calls to _s_e_m___c_l_o_s_e().

       Any blocks of typed memory that were mapped in the calling process are
       unmapped, as if _m_u_n_m_a_p() was implicitly called to unmap them.

       Memory locks established by the calling process via calls to _m_l_o_c_k_a_l_l()
       or _m_l_o_c_k() shall be removed. If locked pages in the address space of
       the calling process are also mapped into the address spaces of other
       processes and are locked by those processes, the locks established by
       the other processes shall be unaffected by the call by this process to
       the _e_x_e_c function. If the _e_x_e_c function fails, the effect on memory
       locks is unspecified.

       Memory mappings created in the process are unmapped before the address
       space is rebuilt for the new process image.

       When the calling process image does not use the SCHED_FIFO, SCHED_RR,
       or SCHED_SPORADIC scheduling policies, the scheduling policy and
       parameters of the new process image and the initial thread in that new
       process image are implementation-defined.

       When the calling process image uses the SCHED_FIFO, SCHED_RR, or
       SCHED_SPORADIC scheduling policies, the process policy and scheduling
       parameter settings shall not be changed by a call to an _e_x_e_c function.
       The initial thread in the new process image shall inherit the process
       scheduling policy and parameters. It shall have the default system
       contention scope, but shall inherit its allocation domain from the
       calling process image.

       Per-process timers created by the calling process shall be deleted
       before replacing the current process image with the new process image.

       All open message queue descriptors in the calling process shall be
       closed, as described in _m_q___c_l_o_s_e().

       Any outstanding asynchronous I/O operations may be canceled. Those
       asynchronous I/O operations that are not canceled shall complete as if
       the _e_x_e_c function had not yet occurred, but any associated signal
       notifications shall be suppressed. It is unspecified whether the _e_x_e_c
       function itself blocks awaiting such I/O completion. In no event,
       however, shall the new process image created by the _e_x_e_c function be
       affected by the presence of outstanding asynchronous I/O operations at
       the time the _e_x_e_c function is called. Whether any I/O is canceled, and
       which I/O may be canceled upon _e_x_e_c, is implementation-defined.

       The new process image shall inherit the CPU-time clock of the calling
       process image. This inheritance means that the process CPU-time clock
       of the process being _e_x_e_c-ed shall not be reinitialized or altered as a
       result of the _e_x_e_c function other than to reflect the time spent by the
       process executing the _e_x_e_c function itself.

       The initial value of the CPU-time clock of the initial thread of the
       new process image shall be set to zero.

       If the calling process is being traced, the new process image shall
       continue to be traced into the same trace stream as the original
       process image, but the new process image shall not inherit the mapping
       of trace event names to trace event type identifiers that was defined
       by calls to the _p_o_s_i_x___t_r_a_c_e___e_v_e_n_t_i_d___o_p_e_n() or the
       _p_o_s_i_x___t_r_a_c_e___t_r_i_d___e_v_e_n_t_i_d___o_p_e_n() functions in the calling process image.

       If the calling process is a trace controller process, any trace streams
       that were created by the calling process shall be shut down as
       described in the _p_o_s_i_x___t_r_a_c_e___s_h_u_t_d_o_w_n() function.

       The thread ID of the initial thread in the new process image is
       unspecified.

       The size and location of the stack on which the initial thread in the
       new process image runs is unspecified.

       The initial thread in the new process image shall have its cancellation
       type set to PTHREAD_CANCEL_DEFERRED and its cancellation state set to
       PTHREAD_CANCEL_ENABLED.

       The initial thread in the new process image shall have all thread-
       specific data values set to NULL and all thread-specific data keys
       shall be removed by the call to _e_x_e_c without running destructors.

       The initial thread in the new process image shall be joinable, as if
       created with the _d_e_t_a_c_h_s_t_a_t_e attribute set to PTHREAD_CREATE_JOINABLE.

       The new process shall inherit at least the following attributes from
       the calling process image:

        *  Nice value (see _n_i_c_e())

        *  _s_e_m_a_d_j values (see _s_e_m_o_p())

        *  Process ID

        *  Parent process ID

        *  Process group ID

        *  Session membership

        *  Real user ID

        *  Real group ID

        *  Supplementary group IDs

        *  Time left until an alarm clock signal (see _a_l_a_r_m())

        *  Current working directory

        *  Root directory

        *  File mode creation mask (see _u_m_a_s_k())

        *  File size limit (see _g_e_t_r_l_i_m_i_t() and _s_e_t_r_l_i_m_i_t())

        *  Process signal mask (see _p_t_h_r_e_a_d___s_i_g_m_a_s_k())

        *  Pending signal (see _s_i_g_p_e_n_d_i_n_g())

        *  _t_m_s___u_t_i_m_e, _t_m_s___s_t_i_m_e, _t_m_s___c_u_t_i_m_e, and _t_m_s___c_s_t_i_m_e (see _t_i_m_e_s())

        *  Resource limits

        *  Controlling terminal

        *  Interval timers

       The initial thread of the new process shall inherit at least the
       following attributes from the calling thread:

        *  Signal mask (see _s_i_g_p_r_o_c_m_a_s_k() and _p_t_h_r_e_a_d___s_i_g_m_a_s_k())

        *  Pending signals (see _s_i_g_p_e_n_d_i_n_g())

       All other process attributes defined in this volume of POSIX.1-2017
       shall be inherited in the new process image from the old process image.
       All other thread attributes defined in this volume of POSIX.1-2017
       shall be inherited in the initial thread in the new process image from
       the calling thread in the old process image.  The inheritance of
       process or thread attributes not defined by this volume of POSIX.1-2017
       is implementation-defined.

       A call to any _e_x_e_c function from a process with more than one thread
       shall result in all threads being terminated and the new executable
       image being loaded and executed. No destructor functions or cleanup
       handlers shall be called.

       Upon successful completion, the _e_x_e_c functions shall mark for update
       the last data access timestamp of the file. If an _e_x_e_c function failed
       but was able to locate the process image file, whether the last data
       access timestamp is marked for update is unspecified. Should the _e_x_e_c
       function succeed, the process image file shall be considered to have
       been opened with _o_p_e_n().  The corresponding _c_l_o_s_e() shall be considered
       to occur at a time after this open, but before process termination or
       successful completion of a subsequent call to one of the _e_x_e_c
       functions, _p_o_s_i_x___s_p_a_w_n(), or _p_o_s_i_x___s_p_a_w_n_p().  The _a_r_g_v[] and _e_n_v_p[]
       arrays of pointers and the strings to which those arrays point shall
       not be modified by a call to one of the _e_x_e_c functions, except as a
       consequence of replacing the process image.

       The saved resource limits in the new process image are set to be a copy
       of the process' corresponding hard and soft limits.

RREETTUURRNN VVAALLUUEE
       If one of the _e_x_e_c functions returns to the calling process image, an
       error has occurred; the return value shall be -1, and _e_r_r_n_o shall be
       set to indicate the error.

EERRRROORRSS
       The _e_x_e_c functions shall fail if:

       EE22BBIIGG  The number of bytes used by the new process image's argument
              list and environment list is greater than the system-imposed
              limit of {ARG_MAX} bytes.

       EEAACCCCEESS The new process image file is not a regular file and the
              implementation does not support execution of files of its type.

       EEIINNVVAALL The new process image file has appropriate privileges and has a
              recognized executable binary format, but the system does not
              support execution of a file with this format.

       The _e_x_e_c functions, except for _f_e_x_e_c_v_e(), shall fail if:

       EEAACCCCEESS Search permission is denied for a directory listed in the new
              process image file's path prefix, or the new process image file
              denies execution permission.

       EELLOOOOPP  A loop exists in symbolic links encountered during resolution of
              the _p_a_t_h or _f_i_l_e argument.

       EENNAAMMEETTOOOOLLOONNGG
              The length of a component of a pathname is longer than
              {NAME_MAX}.

       EENNOOEENNTT A component of _p_a_t_h or _f_i_l_e does not name an existing file or
              _p_a_t_h or _f_i_l_e is an empty string.

       EENNOOTTDDIIRR
              A component of the new process image file's path prefix names an
              existing file that is neither a directory nor a symbolic link to
              a directory, or the new process image file's pathname contains
              at least one non-<slash> character and ends with one or more
              trailing <slash> characters and the last pathname component
              names an existing file that is neither a directory nor a
              symbolic link to a directory.

       The _e_x_e_c functions, except for _e_x_e_c_l_p() and _e_x_e_c_v_p(), shall fail if:

       EENNOOEEXXEECC
              The new process image file has the appropriate access permission
              but has an unrecognized format.

       The _f_e_x_e_c_v_e() function shall fail if:

       EEBBAADDFF  The _f_d argument is not a valid file descriptor open for
              executing.

       The _e_x_e_c functions may fail if:

       EENNOOMMEEMM The new process image requires more memory than is allowed by
              the hardware or system-imposed memory management constraints.

       The _e_x_e_c functions, except for _f_e_x_e_c_v_e(), may fail if:

       EELLOOOOPP  More than {SYMLOOP_MAX} symbolic links were encountered during
              resolution of the _p_a_t_h or _f_i_l_e argument.

       EENNAAMMEETTOOOOLLOONNGG
              The length of the _p_a_t_h argument or the length of the pathname
              constructed from the _f_i_l_e argument exceeds {PATH_MAX}, or
              pathname resolution of a symbolic link produced an intermediate
              result with a length that exceeds {PATH_MAX}.

       EETTXXTTBBSSYY
              The new process image file is a pure procedure (shared text)
              file that is currently open for writing by some process.

       _T_h_e _f_o_l_l_o_w_i_n_g _s_e_c_t_i_o_n_s _a_r_e _i_n_f_o_r_m_a_t_i_v_e_.

EEXXAAMMPPLLEESS
   UUssiinngg eexxeeccll(())
       The following example executes the _l_s command, specifying the pathname
       of the executable (//bbiinn//llss) and using arguments supplied directly to
       the command to produce single-column output.


           #include <unistd.h>

           int ret;
           ...
           ret = execl ("/bin/ls", "ls", "-1", (char *)0);

   UUssiinngg eexxeeccllee(())
       The following example is similar to _U_s_i_n_g _e_x_e_c_l_(_).  In addition, it
       specifies the environment for the new process image using the _e_n_v
       argument.


           #include <unistd.h>

           int ret;
           char *env[] = { "HOME=/usr/home", "LOGNAME=home", (char *)0 };
           ...
           ret = execle ("/bin/ls", "ls", "-l", (char *)0, env);

   UUssiinngg eexxeeccllpp(())
       The following example searches for the location of the _l_s command among
       the directories specified by the _P_A_T_H environment variable.


           #include <unistd.h>

           int ret;
           ...
           ret = execlp ("ls", "ls", "-l", (char *)0);

   UUssiinngg eexxeeccvv(())
       The following example passes arguments to the _l_s command in the _c_m_d
       array.


           #include <unistd.h>

           int ret;
           char *cmd[] = { "ls", "-l", (char *)0 };
           ...
           ret = execv ("/bin/ls", cmd);

   UUssiinngg eexxeeccvvee(())
       The following example passes arguments to the _l_s command in the _c_m_d
       array, and specifies the environment for the new process image using
       the _e_n_v argument.


           #include <unistd.h>

           int ret;
           char *cmd[] = { "ls", "-l", (char *)0 };
           char *env[] = { "HOME=/usr/home", "LOGNAME=home", (char *)0 };
           ...
           ret = execve ("/bin/ls", cmd, env);

   UUssiinngg eexxeeccvvpp(())
       The following example searches for the location of the _l_s command among
       the directories specified by the _P_A_T_H environment variable, and passes
       arguments to the _l_s command in the _c_m_d array.


           #include <unistd.h>

           int ret;
           char *cmd[] = { "ls", "-l", (char *)0 };
           ...
           ret = execvp ("ls", cmd);

AAPPPPLLIICCAATTIIOONN UUSSAAGGEE
       As the state of conversion descriptors and message catalog descriptors
       in the new process image is undefined, conforming applications should
       not rely on their use and should close them prior to calling one of the
       _e_x_e_c functions.

       Applications that require other than the default POSIX locale as the
       global locale in the new process image should call _s_e_t_l_o_c_a_l_e() with the
       appropriate parameters.

       When assigning a new value to the _e_n_v_i_r_o_n variable, applications should
       ensure that the environment to which it will point contains at least
       the following:

        1. Any implementation-defined variables required by the implementation
           to provide a conforming environment. See the _CS_V7_ENV entry in
           _<_u_n_i_s_t_d_._h_> and _c_o_n_f_s_t_r() for details.

        2. A value for _P_A_T_H which finds conforming versions of all standard
           utilities before any other versions.

       The same constraint applies to the _e_n_v_p array passed to _e_x_e_c_l_e() or
       _e_x_e_c_v_e(), in order to ensure that the new process image is invoked in a
       conforming environment.

       Applications should not execute programs with file descriptor 0 not
       open for reading or with file descriptor 1 or 2 not open for writing,
       as this might cause the executed program to misbehave. In order not to
       pass on these file descriptors to an executed program, applications
       should not just close them but should reopen them on, for example,
       //ddeevv//nnuullll.  Some implementations may reopen them automatically, but
       applications should not rely on this being done.

       If an application wants to perform a checksum test of the file being
       executed before executing it, the file will need to be opened with read
       permission to perform the checksum test.

       Since execute permission is checked by _f_e_x_e_c_v_e(), the file description
       _f_d need not have been opened with the O_EXEC flag. However, if the file
       to be executed denies read and write permission for the process
       preparing to do the _e_x_e_c, the only way to provide the _f_d to _f_e_x_e_c_v_e()
       will be to use the O_EXEC flag when opening _f_d.  In this case, the
       application will not be able to perform a checksum test since it will
       not be able to read the contents of the file.

       Note that when a file descriptor is opened with O_RDONLY, O_RDWR, or
       O_WRONLY mode, the file descriptor can be used to read, read and write,
       or write the file, respectively, even if the mode of the file changes
       after the file was opened. Using the O_EXEC open mode is different;
       _f_e_x_e_c_v_e() will ignore the mode that was used when the file descriptor
       was opened and the _e_x_e_c will fail if the mode of the file associated
       with _f_d does not grant execute permission to the calling process at the
       time _f_e_x_e_c_v_e() is called.

RRAATTIIOONNAALLEE
       Early proposals required that the value of _a_r_g_c passed to _m_a_i_n() be
       ``one or greater''. This was driven by the same requirement in drafts
       of the ISO C standard.  In fact, historical implementations have passed
       a value of zero when no arguments are supplied to the caller of the
       _e_x_e_c functions. This requirement was removed from the ISO C standard
       and subsequently removed from this volume of POSIX.1-2017 as well. The
       wording, in particular the use of the word _s_h_o_u_l_d, requires a Strictly
       Conforming POSIX Application to pass at least one argument to the _e_x_e_c
       function, thus guaranteeing that _a_r_g_c be one or greater when invoked by
       such an application. In fact, this is good practice, since many
       existing applications reference _a_r_g_v[0] without first checking the
       value of _a_r_g_c.

       The requirement on a Strictly Conforming POSIX Application also states
       that the value passed as the first argument be a filename string
       associated with the process being started. Although some existing
       applications pass a pathname rather than a filename string in some
       circumstances, a filename string is more generally useful, since the
       common usage of _a_r_g_v[0] is in printing diagnostics. In some cases the
       filename passed is not the actual filename of the file; for example,
       many implementations of the _l_o_g_i_n utility use a convention of prefixing
       a <hyphen-minus> (''--'') to the actual filename, which indicates to the
       command interpreter being invoked that it is a ``login shell''.

       Also, note that the _t_e_s_t and _[ utilities require specific strings for
       the _a_r_g_v[0] argument to have deterministic behavior across all
       implementations.

       Historically, there have been two ways that implementations can _e_x_e_c
       shell scripts.

       One common historical implementation is that the _e_x_e_c_l(), _e_x_e_c_v(),
       _e_x_e_c_l_e(), and _e_x_e_c_v_e() functions return an [[EENNOOEEXXEECC]] error for any file
       not recognizable as executable, including a shell script. When the
       _e_x_e_c_l_p() and _e_x_e_c_v_p() functions encounter such a file, they assume the
       file to be a shell script and invoke a known command interpreter to
       interpret such files.  This is now required by POSIX.1-2008. These
       implementations of _e_x_e_c_v_p() and _e_x_e_c_l_p() only give the [[EENNOOEEXXEECC]] error
       in the rare case of a problem with the command interpreter's executable
       file. Because of these implementations, the [[EENNOOEEXXEECC]] error is not
       mentioned for _e_x_e_c_l_p() or _e_x_e_c_v_p(), although implementations can still
       give it.

       Another way that some historical implementations handle shell scripts
       is by recognizing the first two bytes of the file as the character
       string ""##!!"" and using the remainder of the first line of the file as
       the name of the command interpreter to execute.

       One potential source of confusion noted by the standard developers is
       over how the contents of a process image file affect the behavior of
       the _e_x_e_c family of functions. The following is a description of the
       actions taken:

        1. If the process image file is a valid executable (in a format that
           is executable and valid and having appropriate privileges) for this
           system, then the system executes the file.

        2. If the process image file has appropriate privileges and is in a
           format that is executable but not valid for this system (such as a
           recognized binary for another architecture), then this is an error
           and _e_r_r_n_o is set to [[EEIINNVVAALL]] (see later RATIONALE on [[EEIINNVVAALL]]).

        3. If the process image file has appropriate privileges but is not
           otherwise recognized:

            a. If this is a call to _e_x_e_c_l_p() or _e_x_e_c_v_p(), then they invoke a
               command interpreter assuming that the process image file is a
               shell script.

            b. If this is not a call to _e_x_e_c_l_p() or _e_x_e_c_v_p(), then an error
               occurs and _e_r_r_n_o is set to [[EENNOOEEXXEECC]].

       Applications that do not require to access their arguments may use the
       form:


           main(void)

       as specified in the ISO C standard. However, the implementation will
       always provide the two arguments _a_r_g_c and _a_r_g_v, even if they are not
       used.

       Some implementations provide a third argument to _m_a_i_n() called _e_n_v_p.
       This is defined as a pointer to the environment. The ISO C standard
       specifies invoking _m_a_i_n() with two arguments, so implementations must
       support applications written this way. Since this volume of
       POSIX.1-2017 defines the global variable _e_n_v_i_r_o_n, which is also
       provided by historical implementations and can be used anywhere that
       _e_n_v_p could be used, there is no functional need for the _e_n_v_p argument.
       Applications should use the _g_e_t_e_n_v() function rather than accessing the
       environment directly via either _e_n_v_p or _e_n_v_i_r_o_n.  Implementations are
       required to support the two-argument calling sequence, but this does
       not prohibit an implementation from supporting _e_n_v_p as an optional
       third argument.

       This volume of POSIX.1-2017 specifies that signals set to SIG_IGN
       remain set to SIG_IGN, and that the new process image inherits the
       signal mask of the thread that called _e_x_e_c in the old process image.
       This is consistent with historical implementations, and it permits some
       useful functionality, such as the _n_o_h_u_p command. However, it should be
       noted that many existing applications wrongly assume that they start
       with certain signals set to the default action and/or unblocked. In
       particular, applications written with a simpler signal model that does
       not include blocking of signals, such as the one in the ISO C standard,
       may not behave properly if invoked with some signals blocked.
       Therefore, it is best not to block or ignore signals across _e_x_e_cs
       without explicit reason to do so, and especially not to block signals
       across _e_x_e_cs of arbitrary (not closely cooperating) programs.

       The _e_x_e_c functions always save the value of the effective user ID and
       effective group ID of the process at the completion of the _e_x_e_c,
       whether or not the set-user-ID or the set-group-ID bit of the process
       image file is set.

       The statement about _a_r_g_v[] and _e_n_v_p[] being constants is included to
       make explicit to future writers of language bindings that these objects
       are completely constant. Due to a limitation of the ISO C standard, it
       is not possible to state that idea in standard C. Specifying two levels
       of _c_o_n_s_t-_q_u_a_l_i_f_i_c_a_t_i_o_n for the _a_r_g_v[] and _e_n_v_p[] parameters for the
       _e_x_e_c functions may seem to be the natural choice, given that these
       functions do not modify either the array of pointers or the characters
       to which the function points, but this would disallow existing correct
       code.  Instead, only the array of pointers is noted as constant. The
       table of assignment compatibility for _d_s_t=_s_r_c derived from the ISO C
       standard summarizes the compatibility:

+--------------------+----------+----------------+---------------+---------------------+
|               _d_s_t: | cchhaarr **[[]] | ccoonnsstt cchhaarr **[[]] | cchhaarr **ccoonnsstt[[]] | ccoonnsstt cchhaarr **ccoonnsstt[[]] |
+--------------------+----------+----------------+---------------+---------------------+
|_s_r_c::                |          |                |               |                     |
|cchhaarr **[[]]            |  VALID   |       --       |     VALID     |         --          |
|ccoonnsstt cchhaarr **[[]]      |    --    |     VALID      |      --       |        VALID        |
|cchhaarr ** ccoonnsstt [[]]     |    --    |       --       |     VALID     |         --          |
|ccoonnsstt cchhaarr **ccoonnsstt[[]] |    --    |       --       |      --       |        VALID        |
+--------------------+----------+----------------+---------------+---------------------+
       Since all existing code has a source type matching the first row, the
       column that gives the most valid combinations is the third column. The
       only other possibility is the fourth column, but using it would require
       a cast on the _a_r_g_v or _e_n_v_p arguments. It is unfortunate that the fourth
       column cannot be used, because the declaration a non-expert would
       naturally use would be that in the second row.

       The ISO C standard and this volume of POSIX.1-2017 do not conflict on
       the use of _e_n_v_i_r_o_n, but some historical implementations of _e_n_v_i_r_o_n may
       cause a conflict. As long as _e_n_v_i_r_o_n is treated in the same way as an
       entry point (for example, _f_o_r_k()), it conforms to both standards. A
       library can contain _f_o_r_k(), but if there is a user-provided _f_o_r_k(),
       that _f_o_r_k() is given precedence and no problem ensues. The situation is
       similar for _e_n_v_i_r_o_n: the definition in this volume of POSIX.1-2017 is
       to be used if there is no user-provided _e_n_v_i_r_o_n to take precedence. At
       least three implementations are known to exist that solve this problem.

       EE22BBIIGG  The limit {ARG_MAX} applies not just to the size of the argument
              list, but to the sum of that and the size of the environment
              list.

       EEFFAAUULLTT Some historical systems return [[EEFFAAUULLTT]] rather than [[EENNOOEEXXEECC]]
              when the new process image file is corrupted. They are non-
              conforming.

       EEIINNVVAALL This error condition was added to POSIX.1-2008 to allow an
              implementation to detect executable files generated for
              different architectures, and indicate this situation to the
              application. Historical implementations of shells, _e_x_e_c_v_p(), and
              _e_x_e_c_l_p() that encounter an [[EENNOOEEXXEECC]] error will execute a shell
              on the assumption that the file is a shell script. This will not
              produce the desired effect when the file is a valid executable
              for a different architecture. An implementation may now choose
              to avoid this problem by returning [[EEIINNVVAALL]] when a valid
              executable for a different architecture is encountered.  Some
              historical implementations return [[EEIINNVVAALL]] to indicate that the
              _p_a_t_h argument contains a character with the high order bit set.
              The standard developers chose to deviate from historical
              practice for the following reasons:

                    1. The new utilization of [[EEIINNVVAALL]] will provide some
                       measure of utility to the user community.

                    2. Historical use of [[EEIINNVVAALL]] is not acceptable in an
                       internationalized operating environment.

       EENNAAMMEETTOOOOLLOONNGG
              Since the file pathname may be constructed by taking elements in
              the _P_A_T_H variable and putting them together with the filename,
              the [[EENNAAMMEETTOOOOLLOONNGG]] error condition could also be reached this
              way.

       EETTXXTTBBSSYY
              System V returns this error when the executable file is
              currently open for writing by some process. This volume of
              POSIX.1-2017 neither requires nor prohibits this behavior.

       Other systems (such as System V) may return [[EEIINNTTRR]] from _e_x_e_c.  This is
       not addressed by this volume of POSIX.1-2017, but implementations may
       have a window between the call to _e_x_e_c and the time that a signal could
       cause one of the _e_x_e_c calls to return with [[EEIINNTTRR]].

       An explicit statement regarding the floating-point environment (as
       defined in the _<_f_e_n_v_._h_> header) was added to make it clear that the
       floating-point environment is set to its default when a call to one of
       the _e_x_e_c functions succeeds. The requirements for inheritance or
       setting to the default for other process and thread start-up functions
       is covered by more generic statements in their descriptions and can be
       summarized as follows:

       _p_o_s_i_x___s_p_a_w_n() Set to default.

       _f_o_r_k()        Inherit.

       _p_t_h_r_e_a_d___c_r_e_a_t_e()
                     Inherit.

       The purpose of the _f_e_x_e_c_v_e() function is to enable executing a file
       which has been verified to be the intended file. It is possible to
       actively check the file by reading from the file descriptor and be sure
       that the file is not exchanged for another between the reading and the
       execution. Alternatively, a function like _o_p_e_n_a_t() can be used to open
       a file which has been found by reading the content of a directory using
       _r_e_a_d_d_i_r().

FFUUTTUURREE DDIIRREECCTTIIOONNSS
       None.

SSEEEE AALLSSOO
       _a_l_a_r_m(), _a_t_e_x_i_t(), _c_h_m_o_d(), _c_l_o_s_e(), _c_o_n_f_s_t_r(), _e_x_i_t(), _f_c_n_t_l(),
       _f_o_r_k(), _f_s_t_a_t_v_f_s(), _g_e_t_e_n_v(), _g_e_t_i_t_i_m_e_r(), _g_e_t_r_l_i_m_i_t(), _m_k_n_o_d(),
       _m_m_a_p(), _n_i_c_e(), _o_p_e_n(), _p_o_s_i_x___s_p_a_w_n(), _p_o_s_i_x___t_r_a_c_e___c_r_e_a_t_e(),
       _p_o_s_i_x___t_r_a_c_e___e_v_e_n_t(), _p_o_s_i_x___t_r_a_c_e___e_v_e_n_t_i_d___e_q_u_a_l(), _p_t_h_r_e_a_d___a_t_f_o_r_k(),
       _p_t_h_r_e_a_d___s_i_g_m_a_s_k(), _p_u_t_e_n_v(), _r_e_a_d_d_i_r(), _s_e_m_o_p(), _s_e_t_l_o_c_a_l_e(), _s_h_m_a_t(),
       _s_i_g_a_c_t_i_o_n(), _s_i_g_a_l_t_s_t_a_c_k(), _s_i_g_p_e_n_d_i_n_g(), _s_y_s_t_e_m(), _t_i_m_e_s(), _u_l_i_m_i_t(),
       _u_m_a_s_k()

       The Base Definitions volume of POSIX.1-2017, _C_h_a_p_t_e_r _8, _E_n_v_i_r_o_n_m_e_n_t
       _V_a_r_i_a_b_l_e_s, <<uunniissttdd..hh>>

       The Shell and Utilities volume of POSIX.1-2017, _t_e_s_t

CCOOPPYYRRIIGGHHTT
       Portions of this text are reprinted and reproduced in electronic form
       from IEEE Std 1003.1-2017, Standard for Information Technology --
       Portable Operating System Interface (POSIX), The Open Group Base
       Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the
       Institute of Electrical and Electronics Engineers, Inc and The Open
       Group.  In the event of any discrepancy between this version and the
       original IEEE and The Open Group Standard, the original IEEE and The
       Open Group Standard is the referee document. The original Standard can
       be obtained online at http://www.opengroup.org/unix/online.html .

       Any typographical or formatting errors that appear in this page are
       most likely to have been introduced during the conversion of the source
       files to man page format. To report such errors, see
       https://www.kernel.org/doc/man-pages/reporting_bugs.html .



IEEE/The Open Group                  2017                              EXEC(3)
