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