xref: /freebsd/lib/libc/gen/exec.3 (revision b52f49a9a0f22207ad5130ad8faba08de3ed23d8)
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$
34.\"
35.Dd January 24, 1994
36.Dt EXEC 3
37.Os
38.Sh NAME
39.Nm execl ,
40.Nm execlp ,
41.Nm execle ,
42.Nm exect ,
43.Nm execv ,
44.Nm execvp
45.Nd execute a file
46.Sh LIBRARY
47.Lb libc
48.Sh SYNOPSIS
49.In unistd.h
50.Vt extern char **environ ;
51.Ft int
52.Fn execl "const char *path" "const char *arg" "... /*" "(char *)0*/"
53.Ft int
54.Fn execlp "const char *file" "const char *arg" "... /*" "(char *)0*/"
55.Ft int
56.Fn execle "const char *path" "const char *arg" "... /*" "(char *)0" "char *const envp[]*/"
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.Sh DESCRIPTION
64The
65.Nm exec
66family of functions replaces the current process image with a
67new process image.
68The 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
72for detailed information about the replacement of the current process.)
73.Pp
74The initial argument for these functions is the pathname of a file which
75is to be executed.
76.Pp
77The
78.Fa "const char *arg"
79and subsequent ellipses in the
80.Fn execl ,
81.Fn execlp ,
82and
83.Fn execle
84functions can be thought of as
85.Em arg0 ,
86.Em arg1 ,
87\&...,
88.Em argn .
89Together they describe a list of one or more pointers to null-terminated
90strings that represent the argument list available to the executed program.
91The first argument, by convention, should point to the file name associated
92with the file being executed.
93The list of arguments
94.Em must
95be terminated by a
96.Dv NULL
97pointer.
98.Pp
99The
100.Fn exect ,
101.Fn execv ,
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
138and
139.Fn execvp
140will duplicate the actions of the shell in searching for an executable file
141if the specified file name does not contain a slash
142.Dq Li /
143character.
144The search path is the path specified in the environment by
145.Dq Ev PATH
146variable.
147If this variable isn't specified,
148the default path is set according to the
149.Dv _PATH_DEFPATH
150definition in
151.Aq paths.h ,
152which is set to
153.Dq Ev /usr/bin:/bin .
154In addition, certain errors are treated specially.
155.Pp
156If an error is ambiguous (for simplicity, we shall consider all
157errors except
158.Er ENOEXEC
159as being ambiguous here, although only the critical error
160.Er EACCES
161is really ambiguous),
162then these functions will act as if they stat the file to determine
163whether the file exists and has suitable execute permissions.
164If it does, they will return immediately with the global variable
165.Va errno
166restored to the value set by
167.Fn execve .
168Otherwise, the search will be continued.
169If the search completes without performing a successful
170.Fn execve
171or terminating due to an error,
172these functions will return with the global variable
173.Va errno
174set to
175.Er EACCES
176or
177.Er ENOENT
178according to whether at least one file with suitable execute permissions
179was found.
180.Pp
181If the header of a file isn't recognized (the attempted
182.Fn execve
183returned
184.Er ENOEXEC ) ,
185these functions will execute the shell with the path of
186the file as its first argument.
187(If this attempt fails, no further searching is done.)
188.Pp
189The function
190.Fn exect
191executes a file with the program tracing facilities enabled (see
192.Xr ptrace 2 ) .
193.Sh RETURN VALUES
194If any of the
195.Fn exec
196functions returns, an error will have occurred.
197The return value is \-1, and the global variable
198.Va errno
199will be set to indicate the error.
200.Sh FILES
201.Bl -tag -width /bin/sh -compact
202.It Pa /bin/sh
203The shell.
204.El
205.Sh ERRORS
206The
207.Fn execl ,
208.Fn execle ,
209.Fn execlp
210and
211.Fn execvp
212functions
213may fail and set
214.Va errno
215for any of the errors specified for the library functions
216.Xr execve 2
217and
218.Xr malloc 3 .
219.Pp
220The
221.Fn exect
222and
223.Fn execv
224functions
225may fail and set
226.Va errno
227for any of the errors specified for the library function
228.Xr execve 2 .
229.Sh SEE ALSO
230.Xr sh 1 ,
231.Xr execve 2 ,
232.Xr fork 2 ,
233.Xr ktrace 2 ,
234.Xr ptrace 2 ,
235.Xr environ 7
236.Sh COMPATIBILITY
237Historically, the default path for the
238.Fn execlp
239and
240.Fn execvp
241functions was
242.Dq Pa :/bin:/usr/bin .
243This was changed to place the current directory last to enhance system
244security.
245.Pp
246The behavior of
247.Fn execlp
248and
249.Fn execvp
250when errors occur while attempting to execute the file is not quite historic
251practice, and has not traditionally been documented and is not specified
252by the
253.Tn POSIX
254standard.
255.Pp
256Traditionally, the functions
257.Fn execlp
258and
259.Fn execvp
260ignored all errors except for the ones described above and
261.Er ETXTBSY ,
262upon which they retried after sleeping for several seconds, and
263.Er ENOMEM
264and
265.Er E2BIG ,
266upon which they returned.
267They now return for
268.Er ETXTBSY ,
269and determine existence and executability more carefully.
270In particular,
271.Er EACCES
272for inaccessible directories in the path prefix is no longer
273confused with
274.Er EACCES
275for files with unsuitable execute permissions.
276In
277.Bx 4.4 ,
278they returned upon all errors except
279.Er EACCES ,
280.Er ENOENT ,
281.Er ENOEXEC
282and
283.Er ETXTBSY .
284This was inferior to the traditional error handling,
285since it breaks the ignoring of errors for path prefixes
286and only improves the handling of the unusual ambiguous error
287.Er EFAULT
288and the unusual error
289.Er EIO .
290The behaviour was changed to match the behaviour of
291.Xr sh 1 .
292.Sh STANDARDS
293The
294.Fn execl ,
295.Fn execv ,
296.Fn execle ,
297.Fn execlp
298and
299.Fn execvp
300functions
301conform to
302.St -p1003.1-88 .
303