Commit | Line | Data |
---|---|---|
2fe0358e KM |
1 | .\" Copyright (c) 1980 Regents of the University of California. |
2 | .\" All rights reserved. The Berkeley software License Agreement | |
3 | .\" specifies the terms and conditions for redistribution. | |
4 | .\" | |
5 | .\" @(#)execve.2 4.1 (Berkeley) %G% | |
6 | .\" | |
7 | .TH EXEC 2 4/1/81 | |
8 | .UC 4 | |
9 | .SH NAME | |
10 | execl, execv, execle, execve, execlp, execvp, exec, exece, environ \- execute a file | |
11 | .SH SYNOPSIS | |
12 | .nf | |
13 | .B execl(name, arg0, arg1, ..., argn, 0) | |
14 | .B char *name, *arg0, *arg1, ..., *argn; | |
15 | .PP | |
16 | .B execv(name, argv) | |
17 | .B char *name, *argv[]; | |
18 | .PP | |
19 | .B "execle(name, arg0, arg1, ..., argn, 0, envp)" | |
20 | .B "char *name, *arg0, *arg1, ..., *argn, *envp[];" | |
21 | .PP | |
22 | .B execve(name, argv, envp) | |
23 | .B char *name, *argv[], *envp[]; | |
24 | .PP | |
25 | .B extern char **environ; | |
26 | .fi | |
27 | .SH DESCRIPTION | |
28 | .I Exec | |
29 | in all its forms | |
30 | overlays the calling process with the named file, then | |
31 | transfers to the | |
32 | entry point of the core image of the file. | |
33 | There can be no return from a successful exec; the calling | |
34 | core image is lost. | |
35 | .PP | |
36 | Files remain open across | |
37 | .I exec | |
38 | unless explicit arrangement has been made; | |
39 | see | |
40 | .IR ioctl (2). | |
41 | Ignored/held signals remain ignored/held across | |
42 | these calls, but | |
43 | signals that are caught (see | |
44 | .IR signal (2)) | |
45 | are reset | |
46 | to their default values. | |
47 | .PP | |
48 | Each user has a | |
49 | .I real | |
50 | user ID and group ID and an | |
51 | .I effective | |
52 | user ID and group ID. | |
53 | The | |
54 | real | |
55 | ID | |
56 | identifies the person using the system; | |
57 | the | |
58 | effective | |
59 | ID | |
60 | determines his access privileges. | |
61 | .I Exec | |
62 | changes the effective user and group ID to | |
63 | the owner of the executed file if the file has the `set-user-ID' | |
64 | or `set-group-ID' | |
65 | modes. | |
66 | The | |
67 | real | |
68 | user ID is not affected. | |
69 | .PP | |
70 | The | |
71 | .I name | |
72 | argument | |
73 | is a pointer to the name of the file | |
74 | to be executed. | |
75 | The pointers | |
76 | .IR arg [ 0 ], | |
77 | .IR arg [ 1 "] ..." | |
78 | address null-terminated strings. | |
79 | Conventionally | |
80 | .IR arg [ 0 ] | |
81 | is the name of the | |
82 | file. | |
83 | .PP | |
84 | From C, two interfaces are available. | |
85 | .I execl | |
86 | is useful when a known file with known arguments is | |
87 | being called; | |
88 | the arguments to | |
89 | .I execl | |
90 | are the character strings | |
91 | constituting the file and the arguments; | |
92 | the first argument is conventionally | |
93 | the same as the file name (or its last component). | |
94 | A 0 argument must end the argument list. | |
95 | .PP | |
96 | The | |
97 | .I execv | |
98 | version is useful when the number of arguments is unknown | |
99 | in advance; | |
100 | the arguments to | |
101 | .I execv | |
102 | are the name of the file to be | |
103 | executed and a vector of strings containing | |
104 | the arguments. | |
105 | The last argument string must be followed | |
106 | by a 0 pointer. | |
107 | .PP | |
108 | When a C program is executed, | |
109 | it is called as follows: | |
110 | .PP | |
111 | .nf | |
112 | main(argc, argv, envp) | |
113 | int argc; | |
114 | char **argv, **envp; | |
115 | .fi | |
116 | .PP | |
117 | where | |
118 | .IR argc "" | |
119 | is the argument count | |
120 | and | |
121 | .IR argv "" | |
122 | is an array of character pointers | |
123 | to the arguments themselves. | |
124 | As indicated, | |
125 | .IR argc "" | |
126 | is conventionally at least one | |
127 | and the first member of the array points to a | |
128 | string containing the name of the file. | |
129 | .PP | |
130 | .I Argv | |
131 | is directly usable in another | |
132 | .I execv | |
133 | because | |
134 | .IR argv [ argc ] | |
135 | is 0. | |
136 | .PP | |
137 | .I Envp | |
138 | is a pointer to an array of strings that constitute | |
139 | the | |
140 | .I environment | |
141 | of the process. | |
142 | Each string consists of a name, an \*(lq=\*(rq, and a null-terminated value. | |
143 | The array of pointers is terminated by a null pointer. | |
144 | The shell | |
145 | .IR sh (1) | |
146 | passes an environment entry for each global shell variable | |
147 | defined when the program is called. | |
148 | See | |
149 | .IR environ (5) | |
150 | for some conventionally | |
151 | used names. | |
152 | The C run-time start-off routine places a copy of | |
153 | .I envp | |
154 | in the global cell | |
155 | .I environ, | |
156 | which is used | |
157 | by | |
158 | .IR execv \ and \ execl | |
159 | to pass the environment to any subprograms executed by the | |
160 | current program. | |
161 | The | |
162 | .I exec | |
163 | routines use lower-level routines as follows | |
164 | to pass an environment explicitly: | |
165 | .RS | |
166 | .nf | |
167 | execve(file, argv, environ); | |
168 | execle(file, arg0, arg1, . . . , argn, 0, environ); | |
169 | .fi | |
170 | .RE | |
171 | .PP | |
172 | .I Execlp | |
173 | and | |
174 | .I execvp | |
175 | are called with the same arguments as | |
176 | .I execl | |
177 | and | |
178 | .I execv, | |
179 | but duplicate the shell's actions in searching for an executable | |
180 | file in a list of directories. | |
181 | The directory list is obtained from the environment. | |
182 | .PP | |
183 | To aid execution of command files of various programs, | |
184 | if the first two characters of the executable file are '#!' then | |
185 | .I exec | |
186 | attempts to read a pathname from the executable file and use | |
187 | that program as the command files command interpreter. For example, the | |
188 | following command file sequence would be used to begin a | |
189 | .I csh | |
190 | script: | |
191 | .RS | |
192 | .nf | |
193 | #! /bin/csh | |
194 | # This shell script computes the checksum on /dev/foobar | |
195 | # | |
196 | ... | |
197 | .fi | |
198 | .RE | |
199 | A single parameter may be passed the interpreter, specified after the | |
200 | name of the interpreter; its length and the length of the name | |
201 | of the interpreter combined must not exceed 32 characters. | |
202 | The space (or tab) following the '#!' is mandatory, and the | |
203 | pathname must be explicit (no paths are searched). | |
204 | .SH FILES | |
205 | .ta \w'/bin/sh 'u | |
206 | /bin/sh shell, invoked if command file found | |
207 | by | |
208 | .I execlp | |
209 | or | |
210 | .I execvp | |
211 | .SH "SEE ALSO" | |
212 | fork(2), environ(5), csh(1) | |
213 | .SH DIAGNOSTICS | |
214 | If the file cannot be found, | |
215 | if it is not executable, | |
216 | if it does not start with a valid magic number (see | |
217 | .IR a.out (5)), | |
218 | if maximum memory is exceeded, | |
219 | or if the arguments require too much space, | |
220 | a return | |
221 | constitutes the diagnostic; | |
222 | the return value is \-1. | |
223 | Even for the super-user, | |
224 | at least one of the execute-permission bits must be set for | |
225 | a file to be executed. | |
226 | .SH BUGS | |
227 | If | |
228 | .I execvp | |
229 | is called to execute a file that turns out to be a shell | |
230 | command file, | |
231 | and if it is impossible to execute the shell, | |
232 | the values of | |
233 | .I argv[0] | |
234 | and | |
235 | .I argv[\-1] | |
236 | will be modified before return. | |
237 | .SH "ASSEMBLER (PDP-11)" | |
238 | .DT | |
239 | (exec = 11.) | |
240 | .br | |
241 | .B sys exec; name; argv | |
242 | .PP | |
243 | (exece = 59.) | |
244 | .br | |
245 | .B sys exece; name; argv; envp | |
246 | .PP | |
247 | Plain | |
248 | .I exec | |
249 | is obsoleted by | |
250 | .I exece, | |
251 | but remains for historical reasons. | |
252 | .PP | |
253 | When the called file starts execution on the PDP11, | |
254 | the stack pointer points to a word containing the number of arguments. | |
255 | Just above | |
256 | this number is a list of pointers to the argument strings, | |
257 | followed by a null pointer, followed by the pointers to | |
258 | the environment strings and then another null pointer. | |
259 | The strings themselves follow; | |
260 | a 0 word is left at the very top of memory. | |
261 | .PP | |
262 | sp\(-> nargs | |
263 | .br | |
264 | arg0 | |
265 | .br | |
266 | ... | |
267 | .br | |
268 | argn | |
269 | .br | |
270 | 0 | |
271 | .br | |
272 | env0 | |
273 | .br | |
274 | ... | |
275 | .br | |
276 | envm | |
277 | .br | |
278 | 0 | |
279 | .PP | |
280 | arg0: <arg0\e0> | |
281 | .br | |
282 | ... | |
283 | .br | |
284 | env0: <env0\e0> | |
285 | .br | |
286 | 0 | |
287 | .PP | |
288 | On the Interdata 8/32, | |
289 | the stack begins at a conventional place | |
290 | (currently 0xD0000) | |
291 | and grows upwards. | |
292 | After | |
293 | .I exec, | |
294 | the layout of data on the stack is as follows. | |
295 | .PP | |
296 | .nf | |
297 | int 0 | |
298 | arg0: byte ... | |
299 | ... | |
300 | argp0: int arg0 | |
301 | ... | |
302 | int 0 | |
303 | envp0: int env0 | |
304 | ... | |
305 | int 0 | |
306 | %2\(-> space 40 | |
307 | int nargs | |
308 | int argp0 | |
309 | int envp0 | |
310 | %3\(-> | |
311 | .fi | |
312 | .PP | |
313 | This arrangement happens to conform well to C calling conventions. | |
314 | .PP | |
315 | On a VAX-11, the stack begins at | |
316 | .lg 0 | |
317 | 0x7ffff000 | |
318 | .lg 1 | |
319 | and grows towards lower numbered addresses. | |
320 | After | |
321 | .IR exec , | |
322 | the layout of data on the stack is as follows. | |
323 | .PP | |
324 | .nf | |
325 | .ta \w' arg0: 'u | |
326 | ap \(-> | |
327 | fp \(-> | |
328 | sp \(-> .long nargs | |
329 | .long arg0 | |
330 | ... | |
331 | .long argn | |
332 | .long 0 | |
333 | .long env0 | |
334 | ... | |
335 | .long envn | |
336 | .long 0 | |
337 | arg0: .byte "arg0\e0" | |
338 | ... | |
339 | envn: .byte "envn\e0" | |
340 | .long 0 |