1exec(3)                    Library Functions Manual                    exec(3)
2
3
4

NAME

6       execl, execlp, execle, execv, execvp, execvpe - execute a file
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <unistd.h>
13
14       extern char **environ;
15
16       int execl(const char *pathname, const char *arg, ...
17                       /*, (char *) NULL */);
18       int execlp(const char *file, const char *arg, ...
19                       /*, (char *) NULL */);
20       int execle(const char *pathname, const char *arg, ...
21                       /*, (char *) NULL, char *const envp[] */);
22       int execv(const char *pathname, char *const argv[]);
23       int execvp(const char *file, char *const argv[]);
24       int execvpe(const char *file, char *const argv[], char *const envp[]);
25
26   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
27
28       execvpe():
29           _GNU_SOURCE
30

DESCRIPTION

32       The  exec() family of functions replaces the current process image with
33       a new process image.  The functions described in this manual  page  are
34       layered  on  top  of execve(2).  (See the manual page for execve(2) for
35       further details about the replacement of the current process image.)
36
37       The initial argument for these functions is the name of a file that  is
38       to be executed.
39
40       The  functions can be grouped based on the letters following the "exec"
41       prefix.
42
43   l - execl(), execlp(), execle()
44       The const char *arg and subsequent ellipses can be thought of as  arg0,
45       arg1, ..., argn.  Together they describe a list of one or more pointers
46       to null-terminated strings that represent the argument  list  available
47       to  the  executed  program.   The first argument, by convention, should
48       point to the filename associated with the  file  being  executed.   The
49       list  of  arguments  must  be  terminated by a null pointer, and, since
50       these are variadic functions, this pointer must be cast (char *) NULL.
51
52       By contrast with the 'l' functions, the 'v' functions  (below)  specify
53       the command-line arguments of the executed program as a vector.
54
55   v - execv(), execvp(), execvpe()
56       The  char *const argv[] argument is an array of pointers to null-termi‐
57       nated strings that represent the argument list  available  to  the  new
58       program.   The first argument, by convention, should point to the file‐
59       name associated with the file being executed.  The  array  of  pointers
60       must be terminated by a null pointer.
61
62   e - execle(), execvpe()
63       The  environment of the new process image is specified via the argument
64       envp.  The envp argument is an array  of  pointers  to  null-terminated
65       strings and must be terminated by a null pointer.
66
67       All  other  exec()  functions  (which do not include 'e' in the suffix)
68       take the environment for the new process image from the external  vari‐
69       able environ in the calling process.
70
71   p - execlp(), execvp(), execvpe()
72       These  functions duplicate the actions of the shell in searching for an
73       executable file if the specified filename does not contain a slash  (/)
74       character.  The file is sought in the colon-separated list of directory
75       pathnames specified in the PATH environment variable.  If this variable
76       isn't  defined,  the path list defaults to a list that includes the di‐
77       rectories returned by confstr(_CS_PATH) (which  typically  returns  the
78       value "/bin:/usr/bin") and possibly also the current working directory;
79       see NOTES for further details.
80
81       execvpe() searches for the program using the value  of  PATH  from  the
82       caller's environment, not from the envp argument.
83
84       If  the specified filename includes a slash character, then PATH is ig‐
85       nored, and the file at the specified pathname is executed.
86
87       In addition, certain errors are treated specially.
88
89       If permission is denied for a file (the attempted execve(2) failed with
90       the  error EACCES), these functions will continue searching the rest of
91       the search path.  If no other file is found, however, they will  return
92       with errno set to EACCES.
93
94       If  the  header  of  a  file  isn't recognized (the attempted execve(2)
95       failed with the error ENOEXEC), these functions will execute the  shell
96       (/bin/sh)  with  the  path of the file as its first argument.  (If this
97       attempt fails, no further searching is done.)
98
99       All other exec() functions (which do not include  'p'  in  the  suffix)
100       take  as  their  first  argument a (relative or absolute) pathname that
101       identifies the program to be executed.
102

RETURN VALUE

104       The exec() functions return only if an error has occurred.  The  return
105       value is -1, and errno is set to indicate the error.
106

ERRORS

108       All  of  these  functions  may fail and set errno for any of the errors
109       specified for execve(2).
110

ATTRIBUTES

112       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
113       tributes(7).
114
115       ┌────────────────────────────────────────┬───────────────┬─────────────┐
116Interface                               Attribute     Value       
117       ├────────────────────────────────────────┼───────────────┼─────────────┤
118execl(), execle(), execv()              │ Thread safety │ MT-Safe     │
119       ├────────────────────────────────────────┼───────────────┼─────────────┤
120execlp(), execvp(), execvpe()           │ Thread safety │ MT-Safe env │
121       └────────────────────────────────────────┴───────────────┴─────────────┘
122

VERSIONS

124       The default search path (used when the environment does not contain the
125       variable PATH) shows some variation across systems.  It  generally  in‐
126       cludes  /bin and /usr/bin (in that order) and may also include the cur‐
127       rent working directory.  On some other systems, the current working  is
128       included after /bin and /usr/bin, as an anti-Trojan-horse measure.  The
129       glibc implementation long followed the traditional  default  where  the
130       current  working directory is included at the start of the search path.
131       However, some code refactoring during the  development  of  glibc  2.24
132       caused  the current working directory to be dropped altogether from the
133       default search path.  This accidental  behavior  change  is  considered
134       mildly beneficial, and won't be reverted.
135
136       The  behavior of execlp() and execvp() when errors occur while attempt‐
137       ing to execute the file is historic practice, but has not traditionally
138       been  documented  and is not specified by the POSIX standard.  BSD (and
139       possibly other systems) do an automatic sleep and retry if  ETXTBSY  is
140       encountered.  Linux treats it as a hard error and returns immediately.
141
142       Traditionally,  the  functions execlp() and execvp() ignored all errors
143       except for the ones described above and ENOMEM and  E2BIG,  upon  which
144       they  returned.   They  now return if any error other than the ones de‐
145       scribed above occurs.
146

STANDARDS

148       environ
149       execl()
150       execlp()
151       execle()
152       execv()
153       execvp()
154              POSIX.1-2008.
155
156       execvpe()
157              GNU.
158

HISTORY

160       environ
161       execl()
162       execlp()
163       execle()
164       execv()
165       execvp()
166              POSIX.1-2001.
167
168       execvpe()
169              glibc 2.11.
170

BUGS

172       Before glibc 2.24, execl() and execle() employed realloc(3)  internally
173       and  were  consequently  not async-signal-safe, in violation of the re‐
174       quirements of POSIX.1.  This was fixed in glibc 2.24.
175
176   Architecture-specific details
177       On sparc and sparc64, execv() is provided as a system call by the  ker‐
178       nel  (with  the  prototype  shown  above) for compatibility with SunOS.
179       This function is not employed by the execv() wrapper function on  those
180       architectures.
181

SEE ALSO

183       sh(1),  execve(2),  execveat(2),  fork(2),  ptrace(2), fexecve(3), sys‐
184       tem(3), environ(7)
185
186
187
188Linux man-pages 6.04              2023-03-30                           exec(3)
Impressum