]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/exec.3
pldd.1, bpf.2, chdir.2, clone.2, fanotify_init.2, fanotify_mark.2, intro.2, ipc.2...
[thirdparty/man-pages.git] / man3 / exec.3
1 .\" Copyright (c) 1991 The Regents of the University of California.
2 .\" All rights reserved.
3 .\"
4 .\" %%%LICENSE_START(BSD_4_CLAUSE_UCB)
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\" 3. All advertising materials mentioning features or use of this software
14 .\" must display the following acknowledgement:
15 .\" This product includes software developed by the University of
16 .\" California, Berkeley and its contributors.
17 .\" 4. Neither the name of the University nor the names of its contributors
18 .\" may be used to endorse or promote products derived from this software
19 .\" without specific prior written permission.
20 .\"
21 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .\" SUCH DAMAGE.
32 .\" %%%LICENSE_END
33 .\"
34 .\" @(#)exec.3 6.4 (Berkeley) 4/19/91
35 .\"
36 .\" Converted for Linux, Mon Nov 29 11:12:48 1993, faith@cs.unc.edu
37 .\" Updated more for Linux, Tue Jul 15 11:54:18 1997, pacman@cqc.com
38 .\" Modified, 24 Jun 2004, Michael Kerrisk <mtk.manpages@gmail.com>
39 .\" Added note on casting NULL
40 .\"
41 .TH EXEC 3 2019-08-02 "GNU" "Linux Programmer's Manual"
42 .SH NAME
43 execl, execlp, execle, execv, execvp, execvpe \- execute a file
44 .SH SYNOPSIS
45 .nf
46 .B #include <unistd.h>
47 .PP
48 .B extern char **environ;
49 .PP
50 .BI "int execl(const char *" pathname ", const char *" arg ", ..."
51 .B " /* (char *) NULL */);"
52 .BI "int execlp(const char *" file ", const char *" arg ", ..."
53 .B " /* (char *) NULL */);"
54 .BI "int execle(const char *" pathname ", const char *" arg ", ..."
55 .BI " /*, (char *) NULL, char * const " envp "[] */);"
56 .BI "int execv(const char *" pathname ", char *const " argv "[]);"
57 .BI "int execvp(const char *" file ", char *const " argv "[]);"
58 .BI "int execvpe(const char *" file ", char *const " argv "[],"
59 .BI " char *const " envp "[]);"
60 .fi
61 .PP
62 .in -4n
63 Feature Test Macro Requirements for glibc (see
64 .BR feature_test_macros (7)):
65 .in
66 .PP
67 .BR execvpe ():
68 _GNU_SOURCE
69 .SH DESCRIPTION
70 The
71 .BR exec ()
72 family of functions replaces the current process image with a new process
73 image.
74 The functions described in this manual page are front-ends for
75 .BR execve (2).
76 (See the manual page for
77 .BR execve (2)
78 for further details about the replacement of the current process image.)
79 .PP
80 The initial argument for these functions is the name of a file that is
81 to be executed.
82 .PP
83 The functions can be grouped based on the letters following the "exec" prefix.
84 .\"
85 .SS l - execl(), execlp(), execle()
86 .PP
87 The
88 .I "const char\ *arg"
89 and subsequent ellipses can be thought of as
90 .IR arg0 ,
91 .IR arg1 ,
92 \&...,
93 .IR argn .
94 Together they describe a list of one or more pointers to null-terminated
95 strings that represent the argument list available to the executed program.
96 The first argument, by convention, should point to the filename associated
97 with the file being executed.
98 The list of arguments
99 .I must
100 be terminated by a null pointer,
101 and, since these are variadic functions, this pointer must be cast
102 .IR "(char\ *) NULL" .
103 .PP
104 By contrast with the 'l' functions, the 'v' functions (below) specify the
105 command-line arguments of the executed program as a vector.
106 .\"
107 .SS v - execv(), execvp(), execvpe()
108 .PP
109 The
110 .I "char\ *const argv[]"
111 argument is an array of pointers to null-terminated strings that
112 represent the argument list available to the new program.
113 The first argument, by convention, should point to the filename
114 associated with the file being executed.
115 The array of pointers
116 .I must
117 be terminated by a null pointer.
118 .SS e - execle(), execvpe()
119 .PP
120 The environment of the caller is specified via the argument
121 .IR envp .
122 The
123 .I envp
124 argument is an array of pointers to null-terminated strings and
125 .I must
126 be terminated by a null pointer.
127 .PP
128 All other
129 .BR exec ()
130 functions (which do not include 'e' in the suffix)
131 take the environment for the new process
132 image from the external variable
133 .I environ
134 in the calling process.
135 .SS p - execlp(), execvp(), execvpe()
136 .PP
137 These functions duplicate the actions of the shell in
138 searching for an executable file
139 if the specified filename does not contain a slash (/) character.
140 The file is sought in the colon-separated list of directory pathnames
141 specified in the
142 .B PATH
143 environment variable.
144 If this variable isn't defined, the path list defaults to
145 a list that includes the directories returned by
146 .IR confstr(_CS_PATH)
147 (which typically returns the value "/bin:/usr/bin")
148 and possibly also the current working directory;
149 see NOTES for further details.
150 .PP
151 If the specified filename includes a slash character, then
152 .B PATH
153 is ignored, and the file at the specified pathname is executed.
154 .PP
155 In addition, certain errors are treated specially.
156 .PP
157 If permission is denied for a file (the attempted
158 .BR execve (2)
159 failed with the error
160 .BR EACCES ),
161 these functions will continue searching the rest of the search path.
162 If no other file is found, however,
163 they will return with
164 .I errno
165 set to
166 .BR EACCES .
167 .PP
168 If the header of a file isn't recognized (the attempted
169 .BR execve (2)
170 failed with the error
171 .BR ENOEXEC ),
172 these functions will execute the shell
173 .RI ( /bin/sh )
174 with the path of the file as its first argument.
175 (If this attempt fails, no further searching is done.)
176 .PP
177 All other
178 .BR exec ()
179 functions (which do not include 'p' in the suffix)
180 take as their first argument a (relative or absolute) pathname
181 that identifies the program to be executed.
182 .SH RETURN VALUE
183 The
184 .BR exec ()
185 functions return only if an error has occurred.
186 The return value is \-1, and
187 .I errno
188 is set to indicate the error.
189 .SH ERRORS
190 All of these functions may fail and set
191 .I errno
192 for any of the errors specified for
193 .BR execve (2).
194 .SH VERSIONS
195 The
196 .BR execvpe ()
197 function first appeared in glibc 2.11.
198 .SH ATTRIBUTES
199 For an explanation of the terms used in this section, see
200 .BR attributes (7).
201 .TS
202 allbox;
203 lbw29 lb lb
204 l l l.
205 Interface Attribute Value
206 T{
207 .BR execl (),
208 .BR execle (),
209 .BR execv ()
210 T} Thread safety MT-Safe
211 T{
212 .BR execlp (),
213 .BR execvp (),
214 .BR execvpe ()
215 T} Thread safety MT-Safe env
216 .TE
217 .SH CONFORMING TO
218 POSIX.1-2001, POSIX.1-2008.
219 .PP
220 The
221 .BR execvpe ()
222 function is a GNU extension.
223 .SH NOTES
224 The default search path (used when the environment
225 does not contain the variable \fBPATH\fR)
226 shows some variation across systems.
227 It generally includes
228 .I /bin
229 and
230 .IR /usr/bin
231 (in that order) and may also include the current working directory.
232 On some other systems, the current working is included after
233 .I /bin
234 and
235 .IR /usr/bin ,
236 as an anti-Trojan-horse measure.
237 The glibc implementation long followed the traditional default where
238 the current working directory is included at the start of the search path.
239 However, some code refactoring during the development of glibc 2.24
240 .\" glibc commit 1eb8930608705702d5746e5491bab4e4429fcb83
241 caused the current working directory to be dropped altogether
242 from the default search path.
243 This accidental behavior change is considered mildly beneficial,
244 and won't be reverted.
245 .PP
246 The behavior of
247 .BR execlp ()
248 and
249 .BR execvp ()
250 when errors occur while attempting to execute the file is historic
251 practice, but has not traditionally been documented and is not specified by
252 the POSIX standard.
253 BSD (and possibly other systems) do an automatic
254 sleep and retry if
255 .B ETXTBSY
256 is encountered.
257 Linux treats it as a hard
258 error and returns immediately.
259 .PP
260 Traditionally, the functions
261 .BR execlp ()
262 and
263 .BR execvp ()
264 ignored all errors except for the ones described above and
265 .B ENOMEM
266 and
267 .BR E2BIG ,
268 upon which they returned.
269 They now return if any error other than the ones
270 described above occurs.
271 .SH BUGS
272 Before glibc 2.24,
273 .BR execl ()
274 and
275 .BR execle ()
276 employed
277 .BR realloc (3)
278 internally and were consequently not async-signal-safe,
279 in violation of the requirements of POSIX.1.
280 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=19534
281 This was fixed in glibc 2.24.
282 .\"
283 .SS Architecture-specific details
284 On sparc and sparc64,
285 .BR execv ()
286 is provided as a system call by the kernel
287 (with the prototype shown above)
288 for compatibility with SunOS.
289 This function is
290 .I not
291 employed by the
292 .BR execv ()
293 wrapper function on those architectures.
294 .SH SEE ALSO
295 .BR sh (1),
296 .BR execve (2),
297 .BR execveat (2),
298 .BR fork (2),
299 .BR ptrace (2),
300 .BR fexecve (3),
301 .BR system (3),
302 .BR environ (7)