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