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