1exec(3) Library Functions Manual exec(3)
2
3
4
6 execl, execlp, execle, execv, execvp, execvpe - execute a file
7
9 Standard C library (libc, -lc)
10
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
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
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
108 All of these functions may fail and set errno for any of the errors
109 specified for execve(2).
110
112 For an explanation of the terms used in this section, see at‐
113 tributes(7).
114
115 ┌────────────────────────────────────────┬───────────────┬─────────────┐
116 │Interface │ Attribute │ Value │
117 ├────────────────────────────────────────┼───────────────┼─────────────┤
118 │execl(), execle(), execv() │ Thread safety │ MT-Safe │
119 ├────────────────────────────────────────┼───────────────┼─────────────┤
120 │execlp(), execvp(), execvpe() │ Thread safety │ MT-Safe env │
121 └────────────────────────────────────────┴───────────────┴─────────────┘
122
124 The default search path (used when the environment does not contain the
125 variable PATH) shows some variation across systems. It generally
126 includes /bin and /usr/bin (in that order) and may also include the
127 current working directory. On some other systems, the current working
128 is included after /bin and /usr/bin, as an anti-Trojan-horse measure.
129 The glibc implementation long followed the traditional default where
130 the current working directory is included at the start of the search
131 path. However, some code refactoring during the development of glibc
132 2.24 caused the current working directory to be dropped altogether from
133 the 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
137 attempting to execute the file is historic practice, but has not
138 traditionally been documented and is not specified by the POSIX
139 standard. BSD (and possibly other systems) do an automatic sleep and
140 retry if ETXTBSY is encountered. Linux treats it as a hard error and
141 returns immediately.
142
143 Traditionally, the functions execlp() and execvp() ignored all errors
144 except for the ones described above and ENOMEM and E2BIG, upon which
145 they returned. They now return if any error other than the ones
146 described above occurs.
147
149 environ
150 execl()
151 execlp()
152 execle()
153 execv()
154 execvp()
155 POSIX.1-2008.
156
157 execvpe()
158 GNU.
159
161 environ
162 execl()
163 execlp()
164 execle()
165 execv()
166 execvp()
167 POSIX.1-2001.
168
169 execvpe()
170 glibc 2.11.
171
173 Before glibc 2.24, execl() and execle() employed realloc(3) internally
174 and were consequently not async-signal-safe, in violation of the
175 requirements of POSIX.1. This was fixed in glibc 2.24.
176
177 Architecture-specific details
178 On sparc and sparc64, execv() is provided as a system call by the
179 kernel (with the prototype shown above) for compatibility with SunOS.
180 This function is not employed by the execv() wrapper function on those
181 architectures.
182
184 sh(1), execve(2), execveat(2), fork(2), ptrace(2), fexecve(3),
185 system(3), environ(7)
186
187
188
189Linux man-pages 6.05 2023-07-20 exec(3)