]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libc/gen/exec.3
Change the samantic of struct link_map l_addr member.
[FreeBSD/FreeBSD.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$
30 .\"
31 .Dd March 22, 2020
32 .Dt EXEC 3
33 .Os
34 .Sh NAME
35 .Nm execl ,
36 .Nm execlp ,
37 .Nm execle ,
38 .Nm exect ,
39 .Nm execv ,
40 .Nm execvp ,
41 .Nm execvP
42 .Nd execute a file
43 .Sh LIBRARY
44 .Lb libc
45 .Sh SYNOPSIS
46 .In unistd.h
47 .Vt extern char **environ ;
48 .Ft int
49 .Fn execl "const char *path" "const char *arg" ... NULL
50 .Ft int
51 .Fn execlp "const char *file" "const char *arg" ... NULL
52 .Ft int
53 .Fn execle "const char *path" "const char *arg" ... NULL "char *const envp[]"
54 .Fc
55 .Ft int
56 .Fn exect "const char *path" "char *const argv[]" "char *const envp[]"
57 .Ft int
58 .Fn execv "const char *path" "char *const argv[]"
59 .Ft int
60 .Fn execvp "const char *file" "char *const argv[]"
61 .Ft int
62 .Fn execvP "const char *file" "const char *search_path" "char *const argv[]"
63 .Sh DESCRIPTION
64 The
65 .Nm exec
66 family of functions replaces the current process image with a
67 new process image.
68 The functions described in this manual page are front-ends for the function
69 .Xr execve 2 .
70 (See the manual page for
71 .Xr execve 2
72 for detailed information about the replacement of the current process.)
73 .Pp
74 The initial argument for these functions is the pathname of a file which
75 is to be executed.
76 .Pp
77 The
78 .Fa "const char *arg"
79 and subsequent ellipses in the
80 .Fn execl ,
81 .Fn execlp ,
82 and
83 .Fn execle
84 functions can be thought of as
85 .Em arg0 ,
86 .Em arg1 ,
87 \&...,
88 .Em argn .
89 Together they describe a list of one or more pointers to null-terminated
90 strings that represent the argument list available to the executed program.
91 The first argument, by convention, should point to the file name associated
92 with the file being executed.
93 The list of arguments
94 .Em must
95 be terminated by a
96 .Dv NULL
97 pointer.
98 .Pp
99 The
100 .Fn exect ,
101 .Fn execv ,
102 .Fn execvp ,
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 .Fn execvp ,
140 and
141 .Fn execvP
142 will duplicate the actions of the shell in searching for an executable file
143 if the specified file name does not contain a slash
144 .Dq Li /
145 character.
146 For
147 .Fn execlp
148 and
149 .Fn execvp ,
150 search path is the path specified in the environment by
151 .Dq Ev PATH
152 variable.
153 If this variable is not specified,
154 the default path is set according to the
155 .Dv _PATH_DEFPATH
156 definition in
157 .In paths.h ,
158 which is set to
159 .Dq Ev /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin .
160 For
161 .Fn execvP ,
162 the search path is specified as an argument to the function.
163 In addition, certain errors are treated specially.
164 .Pp
165 If an error is ambiguous (for simplicity, we shall consider all
166 errors except
167 .Er ENOEXEC
168 as being ambiguous here, although only the critical error
169 .Er EACCES
170 is really ambiguous),
171 then these functions will act as if they stat the file to determine
172 whether the file exists and has suitable execute permissions.
173 If it does, they will return immediately with the global variable
174 .Va errno
175 restored to the value set by
176 .Fn execve .
177 Otherwise, the search will be continued.
178 If the search completes without performing a successful
179 .Fn execve
180 or terminating due to an error,
181 these functions will return with the global variable
182 .Va errno
183 set to
184 .Er EACCES
185 or
186 .Er ENOENT
187 according to whether at least one file with suitable execute permissions
188 was found.
189 .Pp
190 If the header of a file is not recognized (the attempted
191 .Fn execve
192 returned
193 .Er ENOEXEC ) ,
194 these functions will execute the shell with the path of
195 the file as its first argument.
196 (If this attempt fails, no further searching is done.)
197 .Pp
198 The function
199 .Fn exect
200 executes a file with the program tracing facilities enabled (see
201 .Xr ptrace 2 ) .
202 .Sh RETURN VALUES
203 If any of the
204 .Fn exec
205 functions returns, an error will have occurred.
206 The return value is \-1, and the global variable
207 .Va errno
208 will be set to indicate the error.
209 .Sh FILES
210 .Bl -tag -width /bin/sh -compact
211 .It Pa /bin/sh
212 The shell.
213 .El
214 .Sh COMPATIBILITY
215 Historically, the default path for the
216 .Fn execlp
217 and
218 .Fn execvp
219 functions was
220 .Dq Pa :/bin:/usr/bin .
221 This was changed to remove the current directory to enhance system
222 security.
223 .Pp
224 The behavior of
225 .Fn execlp
226 and
227 .Fn execvp
228 when errors occur while attempting to execute the file is not quite historic
229 practice, and has not traditionally been documented and is not specified
230 by the
231 .Tn POSIX
232 standard.
233 .Pp
234 Traditionally, the functions
235 .Fn execlp
236 and
237 .Fn execvp
238 ignored all errors except for the ones described above and
239 .Er ETXTBSY ,
240 upon which they retried after sleeping for several seconds, and
241 .Er ENOMEM
242 and
243 .Er E2BIG ,
244 upon which they returned.
245 They now return for
246 .Er ETXTBSY ,
247 and determine existence and executability more carefully.
248 In particular,
249 .Er EACCES
250 for inaccessible directories in the path prefix is no longer
251 confused with
252 .Er EACCES
253 for files with unsuitable execute permissions.
254 In
255 .Bx 4.4 ,
256 they returned upon all errors except
257 .Er EACCES ,
258 .Er ENOENT ,
259 .Er ENOEXEC
260 and
261 .Er ETXTBSY .
262 This was inferior to the traditional error handling,
263 since it breaks the ignoring of errors for path prefixes
264 and only improves the handling of the unusual ambiguous error
265 .Er EFAULT
266 and the unusual error
267 .Er EIO .
268 The behaviour was changed to match the behaviour of
269 .Xr sh 1 .
270 .Sh ERRORS
271 The
272 .Fn execl ,
273 .Fn execle ,
274 .Fn execlp ,
275 .Fn execvp
276 and
277 .Fn execvP
278 functions
279 may fail and set
280 .Va errno
281 for any of the errors specified for the library functions
282 .Xr execve 2
283 and
284 .Xr malloc 3 .
285 .Pp
286 The
287 .Fn exect
288 and
289 .Fn execv
290 functions
291 may fail and set
292 .Va errno
293 for any of the errors specified for the library function
294 .Xr execve 2 .
295 .Sh SEE ALSO
296 .Xr sh 1 ,
297 .Xr execve 2 ,
298 .Xr fork 2 ,
299 .Xr ktrace 2 ,
300 .Xr ptrace 2 ,
301 .Xr environ 7
302 .Sh STANDARDS
303 The
304 .Fn execl ,
305 .Fn execv ,
306 .Fn execle ,
307 .Fn execlp
308 and
309 .Fn execvp
310 functions
311 conform to
312 .St -p1003.1-88 .
313 .Sh HISTORY
314 The
315 .Fn exec
316 function appeared in
317 .At v1 .
318 The
319 .Fn execl
320 and
321 .Fn execv
322 functions appeared in
323 .At v2 .
324 The
325 .Fn execlp ,
326 .Fn execle ,
327 .Fn execve ,
328 and
329 .Fn execvp
330 functions appeared in
331 .At v7 .
332 The
333 .Fn execvP
334 function first appeared in
335 .Fx 5.2 .
336 .Sh BUGS
337 The type of the
338 .Fa argv
339 and
340 .Fa envp
341 parameters to
342 .Fn execle ,
343 .Fn exect ,
344 .Fn execv ,
345 .Fn execvp ,
346 and
347 .Fn execvP
348 is a historical accident and no sane implementation should modify the provided
349 strings.
350 The bogus parameter types trigger false positives from
351 .Li const
352 correctness analyzers.
353 On
354 .Fx ,
355 the
356 .Fn __DECONST
357 macro may be used to work around this limitation.
358 .Pp
359 Due to a fluke of the C standard, on platforms other than
360 .Fx
361 the definition of
362 .Dv NULL
363 may be the untyped number zero, rather than a
364 .Ad (void *)0
365 expression.
366 To distinguish the concepts, they are referred to as a
367 .Dq null pointer constant
368 and a
369 .Dq null pointer ,
370 respectively.
371 On exotic computer architectures that
372 .Fx
373 does not support, the null pointer constant and null pointer may have a
374 different representation.
375 In general, where this document and others reference a
376 .Dv NULL
377 value, they actually imply a null pointer.
378 E.g., for portability to non-FreeBSD operating systems on exotic computer
379 architectures, one may use
380 .Li (char *)NULL
381 in place of
382 .Dv NULL
383 when invoking
384 .Fn execl ,
385 .Fn execle ,
386 and
387 .Fn execlp .