dlvsym manpage: cleanup
[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 .\" 4. 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" ... /* "(char *)0" */
51 .Ft int
52 .Fn execlp "const char *file" "const char *arg" ... /* "(char *)0" */
53 .Ft int
54 .Fo execle
55 .Fa "const char *path" "const char *arg" ...
56 .Fa /*
57 .Bk -words
58 .Fa "(char *)0" "char *const envp[]" */
59 .Ek
60 .Fc
61 .Ft int
62 .Fn exect "const char *path" "char *const argv[]" "char *const envp[]"
63 .Ft int
64 .Fn execv "const char *path" "char *const argv[]"
65 .Ft int
66 .Fn execvp "const char *file" "char *const argv[]"
67 .Ft int
68 .Fn execvP "const char *file" "const char *search_path" "char *const argv[]"
69 .Sh DESCRIPTION
70 The
71 .Nm exec
72 family of functions replaces the current process image with a
73 new process image.
74 The functions described in this manual page are front-ends for the function
75 .Xr execve 2 .
76 (See the manual page for
77 .Xr execve 2
78 for detailed information about the replacement of the current process.
79 The
80 .Xr script 7
81 manual page provides detailed information about the execution of
82 interpreter scripts.)
83 .Pp
84 The initial argument for these functions is the pathname of a file which
85 is to be executed.
86 .Pp
87 The
88 .Fa "const char *arg"
89 and subsequent ellipses in the
90 .Fn execl ,
91 .Fn execlp ,
92 and
93 .Fn execle
94 functions can be thought of as
95 .Em arg0 ,
96 .Em arg1 ,
97 \&...,
98 .Em argn .
99 Together they describe a list of one or more pointers to null-terminated
100 strings that represent the argument list available to the executed program.
101 The first argument, by convention, should point to the file name associated
102 with the file being executed.
103 The list of arguments
104 .Em must
105 be terminated by a
106 .Dv NULL
107 pointer.
108 .Pp
109 The
110 .Fn exect ,
111 .Fn execv ,
112 .Fn execvp ,
113 and
114 .Fn execvP
115 functions provide an array of pointers to null-terminated strings that
116 represent the argument list available to the new program.
117 The first argument, by convention, should point to the file name associated
118 with the file being executed.
119 The array of pointers
120 .Sy must
121 be terminated by a
122 .Dv NULL
123 pointer.
124 .Pp
125 The
126 .Fn execle
127 and
128 .Fn exect
129 functions also specify the environment of the executed process by following
130 the
131 .Dv NULL
132 pointer that terminates the list of arguments in the argument list
133 or the pointer to the argv array with an additional argument.
134 This additional argument is an array of pointers to null-terminated strings
135 and
136 .Em must
137 be terminated by a
138 .Dv NULL
139 pointer.
140 The other functions take the environment for the new process image from the
141 external variable
142 .Va environ
143 in the current process.
144 .Pp
145 Some of these functions have special semantics.
146 .Pp
147 The functions
148 .Fn execlp ,
149 .Fn execvp ,
150 and
151 .Fn execvP
152 will duplicate the actions of the shell in searching for an executable file
153 if the specified file name does not contain a slash
154 .Dq Li /
155 character.
156 For
157 .Fn execlp
158 and
159 .Fn execvp ,
160 search path is the path specified in the environment by
161 .Dq Ev PATH
162 variable.
163 If this variable is not specified,
164 the default path is set according to the
165 .Dv _PATH_DEFPATH
166 definition in
167 .In paths.h ,
168 which is set to
169 .Dq Ev /usr/bin:/bin .
170 For
171 .Fn execvP ,
172 the search path is specified as an argument to the function.
173 In addition, certain errors are treated specially.
174 .Pp
175 If an error is ambiguous (for simplicity, we shall consider all
176 errors except
177 .Er ENOEXEC
178 as being ambiguous here, although only the critical error
179 .Er EACCES
180 is really ambiguous),
181 then these functions will act as if they stat the file to determine
182 whether the file exists and has suitable execute permissions.
183 If it does, they will return immediately with the global variable
184 .Va errno
185 restored to the value set by
186 .Fn execve .
187 Otherwise, the search will be continued.
188 If the search completes without performing a successful
189 .Fn execve
190 or terminating due to an error,
191 these functions will return with the global variable
192 .Va errno
193 set to
194 .Er EACCES
195 or
196 .Er ENOENT
197 according to whether at least one file with suitable execute permissions
198 was found.
199 .Pp
200 If the header of a file is not recognized (the attempted
201 .Fn execve
202 returned
203 .Er ENOEXEC ) ,
204 these functions will execute the shell with the path of
205 the file as its first argument.
206 (If this attempt fails, no further searching is done.)
207 .Pp
208 The function
209 .Fn exect
210 executes a file with the program tracing facilities enabled (see
211 .Xr ptrace 2 ) .
212 .Sh RETURN VALUES
213 If any of the
214 .Fn exec
215 functions returns, an error will have occurred.
216 The return value is \-1, and the global variable
217 .Va errno
218 will be set to indicate the error.
219 .Sh FILES
220 .Bl -tag -width /bin/sh -compact
221 .It Pa /bin/sh
222 The shell.
223 .El
224 .Sh COMPATIBILITY
225 Historically, the default path for the
226 .Fn execlp
227 and
228 .Fn execvp
229 functions was
230 .Dq Pa :/bin:/usr/bin .
231 This was changed to place the current directory last to enhance system
232 security.
233 .Pp
234 The behavior of
235 .Fn execlp
236 and
237 .Fn execvp
238 when errors occur while attempting to execute the file is not quite historic
239 practice, and has not traditionally been documented and is not specified
240 by the
241 .Tn POSIX
242 standard.
243 .Pp
244 Traditionally, the functions
245 .Fn execlp
246 and
247 .Fn execvp
248 ignored all errors except for the ones described above and
249 .Er ETXTBSY ,
250 upon which they retried after sleeping for several seconds, and
251 .Er ENOMEM
252 and
253 .Er E2BIG ,
254 upon which they returned.
255 They now return for
256 .Er ETXTBSY ,
257 and determine existence and executability more carefully.
258 In particular,
259 .Er EACCES
260 for inaccessible directories in the path prefix is no longer
261 confused with
262 .Er EACCES
263 for files with unsuitable execute permissions.
264 In
265 .Bx 4.4 ,
266 they returned upon all errors except
267 .Er EACCES ,
268 .Er ENOENT ,
269 .Er ENOEXEC
270 and
271 .Er ETXTBSY .
272 This was inferior to the traditional error handling,
273 since it breaks the ignoring of errors for path prefixes
274 and only improves the handling of the unusual ambiguous error
275 .Er EFAULT
276 and the unusual error
277 .Er EIO .
278 The behaviour was changed to match the behaviour of
279 .Xr sh 1 .
280 .Sh ERRORS
281 The
282 .Fn execl ,
283 .Fn execle ,
284 .Fn execlp ,
285 .Fn execvp
286 and
287 .Fn execvP
288 functions
289 may fail and set
290 .Va errno
291 for any of the errors specified for the library functions
292 .Xr execve 2
293 and
294 .Xr malloc 3 .
295 .Pp
296 The
297 .Fn exect
298 and
299 .Fn execv
300 functions
301 may fail and set
302 .Va errno
303 for any of the errors specified for the library function
304 .Xr execve 2 .
305 .Sh SEE ALSO
306 .Xr sh 1 ,
307 .Xr execve 2 ,
308 .Xr fork 2 ,
309 .Xr ktrace 2 ,
310 .Xr ptrace 2 ,
311 .Xr environ 7 ,
312 .Xr script 7
313 .Sh STANDARDS
314 The
315 .Fn execl ,
316 .Fn execv ,
317 .Fn execle ,
318 .Fn execlp
319 and
320 .Fn execvp
321 functions
322 conform to
323 .St -p1003.1-88 .
324 The
325 .Fn execvP
326 function first appeared in
327 .Fx 5.2 .