Merge branch 'vendor/BZIP'
[dragonfly.git] / lib / libc / gen / exec.3
1 .\" Copyright (c) 1991, 1993
2 .\"     The Regents of the University of California.  All rights reserved.
3 .\"
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
6 .\" are met:
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\"    notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\"    notice, this list of conditions and the following disclaimer in the
11 .\"    documentation and/or other materials provided with the distribution.
12 .\" 3. Neither the name of the University nor the names of its contributors
13 .\"    may be used to endorse or promote products derived from this software
14 .\"    without specific prior written permission.
15 .\"
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 .\" SUCH DAMAGE.
27 .\"
28 .\"     @(#)exec.3      8.3 (Berkeley) 1/24/94
29 .\" $FreeBSD: src/lib/libc/gen/exec.3,v 1.28 2008/06/23 05:22:06 ed Exp $
30 .\" $DragonFly: src/lib/libc/gen/exec.3,v 1.5 2007/12/21 22:14:04 swildner Exp $
31 .\"
32 .Dd December 21, 2007
33 .Dt EXEC 3
34 .Os
35 .Sh NAME
36 .Nm execl ,
37 .Nm execlp ,
38 .Nm execle ,
39 .Nm exect ,
40 .Nm execv ,
41 .Nm execvp ,
42 .Nm execvP
43 .Nd execute a file
44 .Sh LIBRARY
45 .Lb libc
46 .Sh SYNOPSIS
47 .In unistd.h
48 .Vt extern char **environ ;
49 .Ft int
50 .Fn execl "const char *path" "const char *arg" ...
51 .Ft int
52 .Fn execlp "const char *file" "const char *arg" ...
53 .Ft int
54 .Fo execle
55 .Fa "const char *path" "const char *arg" ...
56 .Fc
57 .Ft int
58 .Fn exect "const char *path" "char *const argv[]" "char *const envp[]"
59 .Ft int
60 .Fn execv "const char *path" "char *const argv[]"
61 .Ft int
62 .Fn execvp "const char *file" "char *const argv[]"
63 .Ft int
64 .Fn execvP "const char *file" "const char *search_path" "char *const argv[]"
65 .Sh DESCRIPTION
66 The
67 .Nm exec
68 family of functions replaces the current process image with a
69 new process image.
70 The functions described in this manual page are front-ends for the function
71 .Xr execve 2 .
72 (See the manual page for
73 .Xr execve 2
74 for detailed information about the replacement of the current process.
75 The
76 .Xr script 7
77 manual page provides detailed information about the execution of
78 interpreter scripts.)
79 .Pp
80 The initial argument for these functions is the pathname of a file which
81 is to be executed.
82 .Pp
83 The
84 .Fa "const char *arg"
85 and subsequent ellipses in the
86 .Fn execl ,
87 .Fn execlp ,
88 and
89 .Fn execle
90 functions can be thought of as
91 .Em arg0 ,
92 .Em arg1 ,
93 \&...,
94 .Em 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 file name associated
98 with the file being executed.
99 The list of arguments
100 .Em must
101 be terminated by a
102 .Dv NULL
103 pointer.
104 .Pp
105 The
106 .Fn exect ,
107 .Fn execv ,
108 .Fn execvp ,
109 and
110 .Fn execvP
111 functions provide 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 file name associated
114 with the file being executed.
115 The array of pointers
116 .Sy must
117 be terminated by a
118 .Dv NULL
119 pointer.
120 .Pp
121 The
122 .Fn execle
123 and
124 .Fn exect
125 functions also specify the environment of the executed process by following
126 the
127 .Dv NULL
128 pointer that terminates the list of arguments in the argument list
129 or the pointer to the argv array with an additional argument.
130 This additional argument is an array of pointers to null-terminated strings
131 and
132 .Em must
133 be terminated by a
134 .Dv NULL
135 pointer.
136 The other functions take the environment for the new process image from the
137 external variable
138 .Va environ
139 in the current process.
140 .Pp
141 Some of these functions have special semantics.
142 .Pp
143 The functions
144 .Fn execlp ,
145 .Fn execvp ,
146 and
147 .Fn execvP
148 will duplicate the actions of the shell in searching for an executable file
149 if the specified file name does not contain a slash
150 .Dq Li /
151 character.
152 For
153 .Fn execlp
154 and
155 .Fn execvp ,
156 search path is the path specified in the environment by
157 .Dq Ev PATH
158 variable.
159 If this variable is not specified,
160 the default path is set according to the
161 .Dv _PATH_DEFPATH
162 definition in
163 .In paths.h ,
164 which is set to
165 .Dq Ev /usr/bin:/bin .
166 For
167 .Fn execvP ,
168 the search path is specified as an argument to the function.
169 In addition, certain errors are treated specially.
170 .Pp
171 If an error is ambiguous (for simplicity, we shall consider all
172 errors except
173 .Er ENOEXEC
174 as being ambiguous here, although only the critical error
175 .Er EACCES
176 is really ambiguous),
177 then these functions will act as if they stat the file to determine
178 whether the file exists and has suitable execute permissions.
179 If it does, they will return immediately with the global variable
180 .Va errno
181 restored to the value set by
182 .Fn execve .
183 Otherwise, the search will be continued.
184 If the search completes without performing a successful
185 .Fn execve
186 or terminating due to an error,
187 these functions will return with the global variable
188 .Va errno
189 set to
190 .Er EACCES
191 or
192 .Er ENOENT
193 according to whether at least one file with suitable execute permissions
194 was found.
195 .Pp
196 If the header of a file is not recognized (the attempted
197 .Fn execve
198 returned
199 .Er ENOEXEC ) ,
200 these functions will execute the shell with the path of
201 the file as its first argument.
202 (If this attempt fails, no further searching is done.)
203 .Pp
204 The function
205 .Fn exect
206 executes a file with the program tracing facilities enabled (see
207 .Xr ptrace 2 ) .
208 .Sh RETURN VALUES
209 If any of the
210 .Fn exec
211 functions returns, an error will have occurred.
212 The return value is \-1, and the global variable
213 .Va errno
214 will be set to indicate the error.
215 .Sh FILES
216 .Bl -tag -width /bin/sh -compact
217 .It Pa /bin/sh
218 The shell.
219 .El
220 .Sh COMPATIBILITY
221 Historically, the default path for the
222 .Fn execlp
223 and
224 .Fn execvp
225 functions was
226 .Dq Pa :/bin:/usr/bin .
227 This was changed to place the current directory last to enhance system
228 security.
229 .Pp
230 The behavior of
231 .Fn execlp
232 and
233 .Fn execvp
234 when errors occur while attempting to execute the file is not quite historic
235 practice, and has not traditionally been documented and is not specified
236 by the
237 .Tn POSIX
238 standard.
239 .Pp
240 Traditionally, the functions
241 .Fn execlp
242 and
243 .Fn execvp
244 ignored all errors except for the ones described above and
245 .Er ETXTBSY ,
246 upon which they retried after sleeping for several seconds, and
247 .Er ENOMEM
248 and
249 .Er E2BIG ,
250 upon which they returned.
251 They now return for
252 .Er ETXTBSY ,
253 and determine existence and executability more carefully.
254 In particular,
255 .Er EACCES
256 for inaccessible directories in the path prefix is no longer
257 confused with
258 .Er EACCES
259 for files with unsuitable execute permissions.
260 In
261 .Bx 4.4 ,
262 they returned upon all errors except
263 .Er EACCES ,
264 .Er ENOENT ,
265 .Er ENOEXEC
266 and
267 .Er ETXTBSY .
268 This was inferior to the traditional error handling,
269 since it breaks the ignoring of errors for path prefixes
270 and only improves the handling of the unusual ambiguous error
271 .Er EFAULT
272 and the unusual error
273 .Er EIO .
274 The behaviour was changed to match the behaviour of
275 .Xr sh 1 .
276 .Sh ERRORS
277 The
278 .Fn execl ,
279 .Fn execle ,
280 .Fn execlp ,
281 .Fn execvp
282 and
283 .Fn execvP
284 functions
285 may fail and set
286 .Va errno
287 for any of the errors specified for the library functions
288 .Xr execve 2
289 and
290 .Xr malloc 3 .
291 .Pp
292 The
293 .Fn exect
294 and
295 .Fn execv
296 functions
297 may fail and set
298 .Va errno
299 for any of the errors specified for the library function
300 .Xr execve 2 .
301 .Sh SEE ALSO
302 .Xr sh 1 ,
303 .Xr execve 2 ,
304 .Xr fork 2 ,
305 .Xr ktrace 2 ,
306 .Xr ptrace 2 ,
307 .Xr environ 7 ,
308 .Xr script 7
309 .Sh STANDARDS
310 The
311 .Fn execl ,
312 .Fn execv ,
313 .Fn execle ,
314 .Fn execlp
315 and
316 .Fn execvp
317 functions
318 conform to
319 .St -p1003.1-88 .
320 The
321 .Fn execvP
322 function first appeared in
323 .Fx 5.2 .