xref: /freebsd/lib/libc/gen/exec.3 (revision 23f282aa31e9b6fceacd449020e936e98d6f2298)
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.Fd #include <unistd.h>
50.Vt extern char **environ;
51.Ft int
52.Fn execl "const char *path" "const char *arg" ...
53.Ft int
54.Fn execlp "const char *file" "const char *arg" ...
55.Ft int
56.Fn execle "const char *path" "const char *arg" ...
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 begin 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 parameter list
122or the pointer to the argv array with an additional parameter.
123This additional parameter 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, the default path
148.Dq Ev /bin:/usr/bin:
149is
150used.
151In addition, certain errors are treated specially.
152.Pp
153If an error is ambiguous (for simplicity, we shall consider all
154errors except
155.Er ENOEXEC
156as being ambiguous here, although only the critical error
157.Er EACCES
158is really ambiguous),
159then these functions will act as if they stat the file to determine
160whether the file exists and has suitable execute permissions.
161If it does, they will return immediately with the global variable
162.Va errno
163restored to the value set by
164.Fn execve .
165Otherwise, the search will be continued.
166If the search completes without performing a successful
167.Fn execve
168or terminating due to an error,
169these functions will return with the global variable
170.Va errno
171set to
172.Er EACCES
173or
174.Er ENOENT
175according to whether at least one file with suitable execute permissions
176was found.
177.Pp
178If the header of a file isn't recognized (the attempted
179.Fn execve
180returned
181.Er ENOEXEC ) ,
182these functions will execute the shell with the path of
183the file as its first argument.
184(If this attempt fails, no further searching is done.)
185.Pp
186The function
187.Fn exect
188executes a file with the program tracing facilities enabled (see
189.Xr ptrace 2 ) .
190.Sh RETURN VALUES
191If any of the
192.Fn exec
193functions returns, an error will have occurred.
194The return value is \-1, and the global variable
195.Va errno
196will be set to indicate the error.
197.Sh FILES
198.Bl -tag -width /bin/sh -compact
199.It Pa /bin/sh
200The shell.
201.El
202.Sh ERRORS
203.Fn Execl ,
204.Fn execle ,
205.Fn execlp
206and
207.Fn execvp
208may fail and set
209.Va errno
210for any of the errors specified for the library functions
211.Xr execve 2
212and
213.Xr malloc 3 .
214.Pp
215.Fn Exect
216and
217.Fn execv
218may fail and set
219.Va errno
220for any of the errors specified for the library function
221.Xr execve 2 .
222.Sh SEE ALSO
223.Xr sh 1 ,
224.Xr execve 2 ,
225.Xr fork 2 ,
226.Xr ktrace 2 ,
227.Xr ptrace 2 ,
228.Xr environ 7 .
229.Sh COMPATIBILITY
230Historically, the default path for the
231.Fn execlp
232and
233.Fn execvp
234functions was
235.Dq Pa :/bin:/usr/bin .
236This was changed to place the current directory last to enhance system
237security.
238.Pp
239The behavior of
240.Fn execlp
241and
242.Fn execvp
243when errors occur while attempting to execute the file is not quite historic
244practice, and has not traditionally been documented and is not specified
245by the
246.Tn POSIX
247standard.
248.Pp
249Traditionally, the functions
250.Fn execlp
251and
252.Fn execvp
253ignored all errors except for the ones described above and
254.Er ETXTBSY ,
255upon which they retried after sleeping for several seconds, and
256.Er ENOMEM
257and
258.Er E2BIG ,
259upon which they returned.
260They now return for
261.Er ETXTBSY ,
262and determine existence and executability more carefully.
263In particular,
264.Er EACCES
265for inaccessible directories in the path prefix is no longer
266confused with
267.Er EACCES
268for files with unsuitable execute permissions.
269In
270.Bx 4.4 ,
271they returned upon all errors except
272.Er EACCES ,
273.Er ENOENT ,
274.Er ENOEXEC
275and
276.Er ETXTBSY .
277This was inferior to the traditional error handling,
278since it it breaks the ignoring of errors for path prefixes
279and only improves the handling of the unusual ambiguous error
280.Er EFAULT
281and the unusual error
282.Er EIO .
283The behaviour was changed to match the behaviour of
284.Xr sh 1 .
285.Sh STANDARDS
286.Fn Execl ,
287.Fn execv ,
288.Fn execle ,
289.Fn execlp
290and
291.Fn execvp
292conform to
293.St -p1003.1-88 .
294