]> git.saurik.com Git - apple/libc.git/blame - gen/FreeBSD/exec.3
Libc-1439.100.3.tar.gz
[apple/libc.git] / gen / FreeBSD / exec.3
CommitLineData
5b2abdfb
A
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.
5b2abdfb
A
12.\" 4. 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
1f2f436a 29.\" $FreeBSD: src/lib/libc/gen/exec.3,v 1.28 2008/06/23 05:22:06 ed Exp $
5b2abdfb
A
30.\"
31.Dd January 24, 1994
32.Dt EXEC 3
33.Os
34.Sh NAME
35.Nm execl ,
5b2abdfb 36.Nm execle ,
ad3c9f2a 37.Nm execlp ,
5b2abdfb 38.Nm execv ,
3d9156a7
A
39.Nm execvp ,
40.Nm execvP
5b2abdfb
A
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
ad3c9f2a
A
48.Fo execl
49.Fa "const char *path"
50.Fa "const char *arg0"
51.Fa ... /* "(char *)0" */
52.Fc
5b2abdfb 53.Ft int
3d9156a7 54.Fo execle
ad3c9f2a
A
55.Fa "const char *path"
56.Fa "const char *arg0"
57.Fa ...
3d9156a7
A
58.Fa /*
59.Bk -words
60.Fa "(char *)0" "char *const envp[]" */
61.Ek
62.Fc
5b2abdfb 63.Ft int
ad3c9f2a
A
64.Fo execlp
65.Fa "const char *file"
66.Fa "const char *arg0"
67.Fa ... /* "(char *)0" */
68.Fc
5b2abdfb 69.Ft int
ad3c9f2a
A
70.Fo execv
71.Fa "const char *path"
72.Fa "char *const argv[]"
73.Fc
5b2abdfb 74.Ft int
ad3c9f2a
A
75.Fo execvp
76.Fa "const char *file"
77.Fa "char *const argv[]"
78.Fc
3d9156a7 79.Ft int
ad3c9f2a
A
80.Fo execvP
81.Fa "const char *file"
82.Fa "const char *search_path"
83.Fa "char *const argv[]"
84.Fc
5b2abdfb
A
85.Sh DESCRIPTION
86The
87.Nm exec
88family of functions replaces the current process image with a
89new process image.
90The functions described in this manual page are front-ends for the function
91.Xr execve 2 .
92(See the manual page for
93.Xr execve 2
94for detailed information about the replacement of the current process.)
95.Pp
96The initial argument for these functions is the pathname of a file which
97is to be executed.
98.Pp
99The
ad3c9f2a 100.Fa "const char *arg0"
5b2abdfb
A
101and subsequent ellipses in the
102.Fn execl ,
103.Fn execlp ,
104and
105.Fn execle
106functions can be thought of as
107.Em arg0 ,
108.Em arg1 ,
109\&...,
110.Em argn .
111Together they describe a list of one or more pointers to null-terminated
112strings that represent the argument list available to the executed program.
113The first argument, by convention, should point to the file name associated
114with the file being executed.
115The list of arguments
116.Em must
117be terminated by a
118.Dv NULL
119pointer.
120.Pp
121The
5b2abdfb 122.Fn execv ,
3d9156a7 123.Fn execvp ,
5b2abdfb 124and
3d9156a7 125.Fn execvP
5b2abdfb
A
126functions provide an array of pointers to null-terminated strings that
127represent the argument list available to the new program.
128The first argument, by convention, should point to the file name associated
129with the file being executed.
130The array of pointers
131.Sy must
132be terminated by a
133.Dv NULL
134pointer.
135.Pp
136The
137.Fn execle
ad3c9f2a
A
138function also specifies the environment of the executed process
139by following the
5b2abdfb 140.Dv NULL
9385eb3d
A
141pointer that terminates the list of arguments in the argument list
142or the pointer to the argv array with an additional argument.
143This additional argument is an array of pointers to null-terminated strings
5b2abdfb
A
144and
145.Em must
146be terminated by a
147.Dv NULL
148pointer.
149The other functions take the environment for the new process image from the
150external variable
151.Va environ
152in the current process.
153.Pp
154Some of these functions have special semantics.
155.Pp
156The functions
3d9156a7
A
157.Fn execlp ,
158.Fn execvp ,
5b2abdfb 159and
3d9156a7 160.Fn execvP
5b2abdfb
A
161will duplicate the actions of the shell in searching for an executable file
162if the specified file name does not contain a slash
163.Dq Li /
164character.
3d9156a7
A
165For
166.Fn execlp
167and
168.Fn execvp ,
169search path is the path specified in the environment by
5b2abdfb
A
170.Dq Ev PATH
171variable.
1f2f436a 172If this variable is not specified,
5b2abdfb
A
173the default path is set according to the
174.Dv _PATH_DEFPATH
175definition in
3d9156a7 176.In paths.h ,
5b2abdfb
A
177which is set to
178.Dq Ev /usr/bin:/bin .
3d9156a7
A
179For
180.Fn execvP ,
181the search path is specified as an argument to the function.
5b2abdfb
A
182In addition, certain errors are treated specially.
183.Pp
184If an error is ambiguous (for simplicity, we shall consider all
185errors except
186.Er ENOEXEC
187as being ambiguous here, although only the critical error
188.Er EACCES
189is really ambiguous),
190then these functions will act as if they stat the file to determine
191whether the file exists and has suitable execute permissions.
192If it does, they will return immediately with the global variable
193.Va errno
194restored to the value set by
195.Fn execve .
196Otherwise, the search will be continued.
197If the search completes without performing a successful
198.Fn execve
199or terminating due to an error,
200these functions will return with the global variable
201.Va errno
202set to
203.Er EACCES
204or
205.Er ENOENT
206according to whether at least one file with suitable execute permissions
207was found.
208.Pp
1f2f436a 209If the header of a file is not recognized (the attempted
5b2abdfb
A
210.Fn execve
211returned
212.Er ENOEXEC ) ,
213these functions will execute the shell with the path of
214the file as its first argument.
215(If this attempt fails, no further searching is done.)
5b2abdfb
A
216.Sh RETURN VALUES
217If any of the
218.Fn exec
219functions returns, an error will have occurred.
220The return value is \-1, and the global variable
221.Va errno
222will be set to indicate the error.
223.Sh FILES
224.Bl -tag -width /bin/sh -compact
225.It Pa /bin/sh
226The shell.
227.El
5b2abdfb
A
228.Sh COMPATIBILITY
229Historically, the default path for the
230.Fn execlp
231and
232.Fn execvp
233functions was
234.Dq Pa :/bin:/usr/bin .
235This was changed to place the current directory last to enhance system
236security.
237.Pp
238The behavior of
239.Fn execlp
240and
241.Fn execvp
242when errors occur while attempting to execute the file is not quite historic
243practice, and has not traditionally been documented and is not specified
244by the
245.Tn POSIX
246standard.
247.Pp
248Traditionally, the functions
249.Fn execlp
250and
251.Fn execvp
252ignored all errors except for the ones described above and
253.Er ETXTBSY ,
254upon which they retried after sleeping for several seconds, and
255.Er ENOMEM
256and
257.Er E2BIG ,
258upon which they returned.
259They now return for
260.Er ETXTBSY ,
261and determine existence and executability more carefully.
262In particular,
263.Er EACCES
264for inaccessible directories in the path prefix is no longer
265confused with
266.Er EACCES
267for files with unsuitable execute permissions.
268In
269.Bx 4.4 ,
270they returned upon all errors except
271.Er EACCES ,
272.Er ENOENT ,
273.Er ENOEXEC
274and
275.Er ETXTBSY .
276This was inferior to the traditional error handling,
277since it breaks the ignoring of errors for path prefixes
278and only improves the handling of the unusual ambiguous error
279.Er EFAULT
280and the unusual error
281.Er EIO .
282The behaviour was changed to match the behaviour of
283.Xr sh 1 .
1f2f436a
A
284.Sh ERRORS
285The
286.Fn execl ,
287.Fn execle ,
288.Fn execlp ,
ad3c9f2a 289.Fn execvp ,
1f2f436a
A
290and
291.Fn execvP
292functions
293may fail and set
294.Va errno
295for any of the errors specified for the library functions
296.Xr execve 2
297and
298.Xr malloc 3 .
299.Pp
300The
1f2f436a 301.Fn execv
ad3c9f2a 302function may fail and set
1f2f436a
A
303.Va errno
304for any of the errors specified for the library function
305.Xr execve 2 .
306.Sh SEE ALSO
307.Xr sh 1 ,
308.Xr execve 2 ,
309.Xr fork 2 ,
1f2f436a
A
310.Xr ptrace 2 ,
311.Xr environ 7
5b2abdfb 312.Sh STANDARDS
9385eb3d
A
313The
314.Fn execl ,
5b2abdfb
A
315.Fn execv ,
316.Fn execle ,
ad3c9f2a 317.Fn execlp ,
5b2abdfb
A
318and
319.Fn execvp
9385eb3d 320functions
5b2abdfb
A
321conform to
322.St -p1003.1-88 .
3d9156a7
A
323The
324.Fn execvP
325function first appeared in
326.Fx 5.2 .