man-pages/man3/exec.3.html
2021-03-31 01:06:50 +01:00

432 lines
10 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of EXEC</TITLE>
</HEAD><BODY>
<H1>EXEC</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-08-02<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
execl, execlp, execle, execv, execvp, execvpe - execute a file
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/unistd.h">unistd.h</A>&gt;</B>
<B>extern char **environ;</B>
<B>int execl(const char *</B><I>pathname</I><B>, const char *</B><I>arg</I><B>, ...</B>
<B> /* (char *) NULL */);</B>
<B>int execlp(const char *</B><I>file</I><B>, const char *</B><I>arg</I><B>, ...</B>
<B> /* (char *) NULL */);</B>
<B>int execle(const char *</B><I>pathname</I><B>, const char *</B><I>arg</I><B>, ...</B>
<B> /*, (char *) NULL, char *const </B><I>envp</I><B>[] */);</B>
<B>int execv(const char *</B><I>pathname</I><B>, char *const </B><I>argv</I><B>[]);</B>
<B>int execvp(const char *</B><I>file</I><B>, char *const </B><I>argv</I><B>[]);</B>
<B>int execvpe(const char *</B><I>file</I><B>, char *const </B><I>argv</I><B>[],</B>
<B> char *const </B><I>envp</I><B>[]);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>execvpe</B>():
_GNU_SOURCE
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>exec</B>()
family of functions replaces the current process image with a new process
image.
The functions described in this manual page are front-ends for
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2).
(See the manual page for
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2)
for further details about the replacement of the current process image.)
<P>
The initial argument for these functions is the name of a file that is
to be executed.
<P>
The functions can be grouped based on the letters following the &quot;exec&quot; prefix.
<A NAME="lbAE">&nbsp;</A>
<H3>l - execl(), execlp(), execle()</H3>
<P>
The
<I>const char&nbsp;*arg</I>
and subsequent ellipses can be thought of as
<I>arg0</I>,
<I>arg1</I>,
...,
<I>argn</I>.
Together they describe a list of one or more pointers to null-terminated
strings that represent the argument list available to the executed program.
The first argument, by convention, should point to the filename associated
with the file being executed.
The list of arguments
<I>must</I>
be terminated by a null pointer,
and, since these are variadic functions, this pointer must be cast
<I>(char&nbsp;*) NULL</I>.
<P>
By contrast with the 'l' functions, the 'v' functions (below) specify the
command-line arguments of the executed program as a vector.
<A NAME="lbAF">&nbsp;</A>
<H3>v - execv(), execvp(), execvpe()</H3>
<P>
The
<I>char&nbsp;*const argv[]</I>
argument is an array of pointers to null-terminated strings that
represent the argument list available to the new program.
The first argument, by convention, should point to the filename
associated with the file being executed.
The array of pointers
<I>must</I>
be terminated by a null pointer.
<A NAME="lbAG">&nbsp;</A>
<H3>e - execle(), execvpe()</H3>
<P>
The environment of the caller is specified via the argument
<I>envp</I>.
The
<I>envp</I>
argument is an array of pointers to null-terminated strings and
<I>must</I>
be terminated by a null pointer.
<P>
All other
<B>exec</B>()
functions (which do not include 'e' in the suffix)
take the environment for the new process
image from the external variable
<I>environ</I>
in the calling process.
<A NAME="lbAH">&nbsp;</A>
<H3>p - execlp(), execvp(), execvpe()</H3>
<P>
These functions duplicate the actions of the shell in
searching for an executable file
if the specified filename does not contain a slash (/) character.
The file is sought in the colon-separated list of directory pathnames
specified in the
<B>PATH</B>
environment variable.
If this variable isn't defined, the path list defaults to
a list that includes the directories returned by
<I>confstr(_CS_PATH)</I>
(which typically returns the value &quot;/bin:/usr/bin&quot;)
and possibly also the current working directory;
see NOTES for further details.
<P>
If the specified filename includes a slash character, then
<B>PATH</B>
is ignored, and the file at the specified pathname is executed.
<P>
In addition, certain errors are treated specially.
<P>
If permission is denied for a file (the attempted
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2)
failed with the error
<B>EACCES</B>),
these functions will continue searching the rest of the search path.
If no other file is found, however,
they will return with
<I>errno</I>
set to
<B>EACCES</B>.
<P>
If the header of a file isn't recognized (the attempted
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2)
failed with the error
<B>ENOEXEC</B>),
these functions will execute the shell
(<I>/bin/sh</I>)
with the path of the file as its first argument.
(If this attempt fails, no further searching is done.)
<P>
All other
<B>exec</B>()
functions (which do not include 'p' in the suffix)
take as their first argument a (relative or absolute) pathname
that identifies the program to be executed.
<A NAME="lbAI">&nbsp;</A>
<H2>RETURN VALUE</H2>
The
<B>exec</B>()
functions return only if an error has occurred.
The return value is -1, and
<I>errno</I>
is set to indicate the error.
<A NAME="lbAJ">&nbsp;</A>
<H2>ERRORS</H2>
All of these functions may fail and set
<I>errno</I>
for any of the errors specified for
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2).
<A NAME="lbAK">&nbsp;</A>
<H2>VERSIONS</H2>
The
<B>execvpe</B>()
function first appeared in glibc 2.11.
<A NAME="lbAL">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>execl</B>(),
<B>execle</B>(),
<B>execv</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
<TR VALIGN=top><TD>
<B>execlp</B>(),
<B>execvp</B>(),
<B>execvpe</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe env<BR></TD></TR>
</TABLE>
<A NAME="lbAM">&nbsp;</A>
<H2>CONFORMING TO</H2>
POSIX.1-2001, POSIX.1-2008.
<P>
The
<B>execvpe</B>()
function is a GNU extension.
<A NAME="lbAN">&nbsp;</A>
<H2>NOTES</H2>
The default search path (used when the environment
does not contain the variable <B>PATH</B>)
shows some variation across systems.
It generally includes
<I>/bin</I>
and
<I>/usr/bin</I>
(in that order) and may also include the current working directory.
On some other systems, the current working is included after
<I>/bin</I>
and
<I>/usr/bin</I>,
as an anti-Trojan-horse measure.
The glibc implementation long followed the traditional default where
the current working directory is included at the start of the search path.
However, some code refactoring during the development of glibc 2.24
caused the current working directory to be dropped altogether
from the default search path.
This accidental behavior change is considered mildly beneficial,
and won't be reverted.
<P>
The behavior of
<B>execlp</B>()
and
<B>execvp</B>()
when errors occur while attempting to execute the file is historic
practice, but has not traditionally been documented and is not specified by
the POSIX standard.
BSD (and possibly other systems) do an automatic
sleep and retry if
<B>ETXTBSY</B>
is encountered.
Linux treats it as a hard
error and returns immediately.
<P>
Traditionally, the functions
<B>execlp</B>()
and
<B>execvp</B>()
ignored all errors except for the ones described above and
<B>ENOMEM</B>
and
<B>E2BIG</B>,
upon which they returned.
They now return if any error other than the ones
described above occurs.
<A NAME="lbAO">&nbsp;</A>
<H2>BUGS</H2>
Before glibc 2.24,
<B>execl</B>()
and
<B>execle</B>()
employed
<B><A HREF="/cgi-bin/man/man2html?3+realloc">realloc</A></B>(3)
internally and were consequently not async-signal-safe,
in violation of the requirements of POSIX.1.
This was fixed in glibc 2.24.
<A NAME="lbAP">&nbsp;</A>
<H3>Architecture-specific details</H3>
On sparc and sparc64,
<B>execv</B>()
is provided as a system call by the kernel
(with the prototype shown above)
for compatibility with SunOS.
This function is
<I>not</I>
employed by the
<B>execv</B>()
wrapper function on those architectures.
<A NAME="lbAQ">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+sh">sh</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?2+execve">execve</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+execveat">execveat</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+fork">fork</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?2+ptrace">ptrace</A></B>(2),
<B><A HREF="/cgi-bin/man/man2html?3+fexecve">fexecve</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+system">system</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?7+environ">environ</A></B>(7)
<A NAME="lbAR">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DL>
<DT id="4"><A HREF="#lbAE">l - execl(), execlp(), execle()</A><DD>
<DT id="5"><A HREF="#lbAF">v - execv(), execvp(), execvpe()</A><DD>
<DT id="6"><A HREF="#lbAG">e - execle(), execvpe()</A><DD>
<DT id="7"><A HREF="#lbAH">p - execlp(), execvp(), execvpe()</A><DD>
</DL>
<DT id="8"><A HREF="#lbAI">RETURN VALUE</A><DD>
<DT id="9"><A HREF="#lbAJ">ERRORS</A><DD>
<DT id="10"><A HREF="#lbAK">VERSIONS</A><DD>
<DT id="11"><A HREF="#lbAL">ATTRIBUTES</A><DD>
<DT id="12"><A HREF="#lbAM">CONFORMING TO</A><DD>
<DT id="13"><A HREF="#lbAN">NOTES</A><DD>
<DT id="14"><A HREF="#lbAO">BUGS</A><DD>
<DL>
<DT id="15"><A HREF="#lbAP">Architecture-specific details</A><DD>
</DL>
<DT id="16"><A HREF="#lbAQ">SEE ALSO</A><DD>
<DT id="17"><A HREF="#lbAR">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:39 GMT, March 31, 2021
</BODY>
</HTML>