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