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