poll.2: Remove <signal.h>
[man-pages.git] / man3 / exec.3
blob6292949b68361fac6e0ea32e553750dcb09d1839
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  2021-03-22 "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 "[], char *const " envp "[]);"
60 .fi
61 .PP
62 .RS -4
63 Feature Test Macro Requirements for glibc (see
64 .BR feature_test_macros (7)):
65 .RE
66 .PP
67 .BR execvpe ():
68 .nf
69     _GNU_SOURCE
70 .fi
71 .SH DESCRIPTION
72 The
73 .BR exec ()
74 family of functions replaces the current process image with a new process
75 image.
76 The functions described in this manual page are layered on top of
77 .BR execve (2).
78 (See the manual page for
79 .BR execve (2)
80 for further details about the replacement of the current process image.)
81 .PP
82 The initial argument for these functions is the name of a file that is
83 to be executed.
84 .PP
85 The functions can be grouped based on the letters following the "exec" prefix.
86 .\"
87 .SS l - execl(), execlp(), execle()
88 The
89 .I "const char\ *arg"
90 and subsequent ellipses can be thought of as
91 .IR arg0 ,
92 .IR arg1 ,
93 \&...,
94 .IR argn .
95 Together they describe a list of one or more pointers to null-terminated
96 strings that represent the argument list available to the executed program.
97 The first argument, by convention, should point to the filename associated
98 with the file being executed.
99 The list of arguments
100 .I must
101 be terminated by a null pointer,
102 and, since these are variadic functions, this pointer must be cast
103 .IR "(char\ *) NULL" .
105 By contrast with the 'l' functions, the 'v' functions (below) specify the
106 command-line arguments of the executed program as a vector.
108 .SS v - execv(), execvp(), execvpe()
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 The environment of the new process image is specified via the argument
120 .IR envp .
122 .I envp
123 argument is an array of pointers to null-terminated strings and
124 .I must
125 be terminated by a null pointer.
127 All other
128 .BR exec ()
129 functions (which do not include 'e' in the suffix)
130 take the environment for the new process
131 image from the external variable
132 .I environ
133 in the calling process.
134 .SS p - execlp(), execvp(), execvpe()
135 These functions duplicate the actions of the shell in
136 searching for an executable file
137 if the specified filename does not contain a slash (/) character.
138 The file is sought in the colon-separated list of directory pathnames
139 specified in the
140 .B PATH
141 environment variable.
142 If this variable isn't defined, the path list defaults to
143 a list that includes the directories returned by
144 .IR confstr(_CS_PATH)
145 (which typically returns the value "/bin:/usr/bin")
146 and possibly also the current working directory;
147 see NOTES for further details.
149 .BR execvpe ()
150 searches for the program using the value of
151 .B PATH
152 from the caller's environment, not from the
153 .I envp
154 argument.
156 If the specified filename includes a slash character, then
157 .B PATH
158 is ignored, and the file at the specified pathname is executed.
160 In addition, certain errors are treated specially.
162 If permission is denied for a file (the attempted
163 .BR execve (2)
164 failed with the error
165 .BR EACCES ),
166 these functions will continue searching the rest of the search path.
167 If no other file is found, however,
168 they will return with
169 .I errno
170 set to
171 .BR EACCES .
173 If the header of a file isn't recognized (the attempted
174 .BR execve (2)
175 failed with the error
176 .BR ENOEXEC ),
177 these functions will execute the shell
178 .RI ( /bin/sh )
179 with the path of the file as its first argument.
180 (If this attempt fails, no further searching is done.)
182 All other
183 .BR exec ()
184 functions (which do not include 'p' in the suffix)
185 take as their first argument a (relative or absolute) pathname
186 that identifies the program to be executed.
187 .SH RETURN VALUE
189 .BR exec ()
190 functions return only if an error has occurred.
191 The return value is \-1, and
192 .I errno
193 is set to indicate the error.
194 .SH ERRORS
195 All of these functions may fail and set
196 .I errno
197 for any of the errors specified for
198 .BR execve (2).
199 .SH VERSIONS
201 .BR execvpe ()
202 function first appeared in glibc 2.11.
203 .SH ATTRIBUTES
204 For an explanation of the terms used in this section, see
205 .BR attributes (7).
206 .ad l
209 allbox;
210 lbx lb lb
211 l l l.
212 Interface       Attribute       Value
214 .BR execl (),
215 .BR execle (),
216 .BR execv ()
217 T}      Thread safety   MT-Safe
219 .BR execlp (),
220 .BR execvp (),
221 .BR execvpe ()
222 T}      Thread safety   MT-Safe env
226 .sp 1
227 .SH CONFORMING TO
228 POSIX.1-2001, POSIX.1-2008.
231 .BR execvpe ()
232 function is a GNU extension.
233 .SH NOTES
234 The default search path (used when the environment
235 does not contain the variable \fBPATH\fR)
236 shows some variation across systems.
237 It generally includes
238 .I /bin
240 .IR /usr/bin
241 (in that order) and may also include the current working directory.
242 On some other systems, the current working is included after
243 .I /bin
245 .IR /usr/bin ,
246 as an anti-Trojan-horse measure.
247 The glibc implementation long followed the traditional default where
248 the current working directory is included at the start of the search path.
249 However, some code refactoring during the development of glibc 2.24
250 .\" glibc commit 1eb8930608705702d5746e5491bab4e4429fcb83
251 caused the current working directory to be dropped altogether
252 from the default search path.
253 This accidental behavior change is considered mildly beneficial,
254 and won't be reverted.
256 The behavior of
257 .BR execlp ()
259 .BR execvp ()
260 when errors occur while attempting to execute the file is historic
261 practice, but has not traditionally been documented and is not specified by
262 the POSIX standard.
263 BSD (and possibly other systems) do an automatic
264 sleep and retry if
265 .B ETXTBSY
266 is encountered.
267 Linux treats it as a hard
268 error and returns immediately.
270 Traditionally, the functions
271 .BR execlp ()
273 .BR execvp ()
274 ignored all errors except for the ones described above and
275 .B ENOMEM
277 .BR E2BIG ,
278 upon which they returned.
279 They now return if any error other than the ones
280 described above occurs.
281 .SH BUGS
282 Before glibc 2.24,
283 .BR execl ()
285 .BR execle ()
286 employed
287 .BR realloc (3)
288 internally and were consequently not async-signal-safe,
289 in violation of the requirements of POSIX.1.
290 .\" https://sourceware.org/bugzilla/show_bug.cgi?id=19534
291 This was fixed in glibc 2.24.
293 .SS Architecture-specific details
294 On sparc and sparc64,
295 .BR execv ()
296 is provided as a system call by the kernel
297 (with the prototype shown above)
298 for compatibility with SunOS.
299 This function is
300 .I not
301 employed by the
302 .BR execv ()
303 wrapper function on those architectures.
304 .SH SEE ALSO
305 .BR sh (1),
306 .BR execve (2),
307 .BR execveat (2),
308 .BR fork (2),
309 .BR ptrace (2),
310 .BR fexecve (3),
311 .BR system (3),
312 .BR environ (7)