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