manual page distributed with 4.1BSD
[unix-history] / usr / src / lib / libc / sys / execve.2
CommitLineData
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
10execl, 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
29in all its forms
30overlays the calling process with the named file, then
31transfers to the
32entry point of the core image of the file.
33There can be no return from a successful exec; the calling
34core image is lost.
35.PP
36Files remain open across
37.I exec
38unless explicit arrangement has been made;
39see
40.IR ioctl (2).
41Ignored/held signals remain ignored/held across
42these calls, but
43signals that are caught (see
44.IR signal (2))
45are reset
46to their default values.
47.PP
48Each user has a
49.I real
50user ID and group ID and an
51.I effective
52user ID and group ID.
53The
54real
55ID
56identifies the person using the system;
57the
58effective
59ID
60determines his access privileges.
61.I Exec
62changes the effective user and group ID to
63the owner of the executed file if the file has the `set-user-ID'
64or `set-group-ID'
65modes.
66The
67real
68user ID is not affected.
69.PP
70The
71.I name
72argument
73is a pointer to the name of the file
74to be executed.
75The pointers
76.IR arg [ 0 ],
77.IR arg [ 1 "] ..."
78address null-terminated strings.
79Conventionally
80.IR arg [ 0 ]
81is the name of the
82file.
83.PP
84From C, two interfaces are available.
85.I execl
86is useful when a known file with known arguments is
87being called;
88the arguments to
89.I execl
90are the character strings
91constituting the file and the arguments;
92the first argument is conventionally
93the same as the file name (or its last component).
94A 0 argument must end the argument list.
95.PP
96The
97.I execv
98version is useful when the number of arguments is unknown
99in advance;
100the arguments to
101.I execv
102are the name of the file to be
103executed and a vector of strings containing
104the arguments.
105The last argument string must be followed
106by a 0 pointer.
107.PP
108When a C program is executed,
109it is called as follows:
110.PP
111.nf
112 main(argc, argv, envp)
113 int argc;
114 char **argv, **envp;
115.fi
116.PP
117where
118.IR argc ""
119is the argument count
120and
121.IR argv ""
122is an array of character pointers
123to the arguments themselves.
124As indicated,
125.IR argc ""
126is conventionally at least one
127and the first member of the array points to a
128string containing the name of the file.
129.PP
130.I Argv
131is directly usable in another
132.I execv
133because
134.IR argv [ argc ]
135is 0.
136.PP
137.I Envp
138is a pointer to an array of strings that constitute
139the
140.I environment
141of the process.
142Each string consists of a name, an \*(lq=\*(rq, and a null-terminated value.
143The array of pointers is terminated by a null pointer.
144The shell
145.IR sh (1)
146passes an environment entry for each global shell variable
147defined when the program is called.
148See
149.IR environ (5)
150for some conventionally
151used names.
152The C run-time start-off routine places a copy of
153.I envp
154in the global cell
155.I environ,
156which is used
157by
158.IR execv \ and \ execl
159to pass the environment to any subprograms executed by the
160current program.
161The
162.I exec
163routines use lower-level routines as follows
164to pass an environment explicitly:
165.RS
166.nf
167execve(file, argv, environ);
168execle(file, arg0, arg1, . . . , argn, 0, environ);
169.fi
170.RE
171.PP
172.I Execlp
173and
174.I execvp
175are called with the same arguments as
176.I execl
177and
178.I execv,
179but duplicate the shell's actions in searching for an executable
180file in a list of directories.
181The directory list is obtained from the environment.
182.PP
183To aid execution of command files of various programs,
184if the first two characters of the executable file are '#!' then
185.I exec
186attempts to read a pathname from the executable file and use
187that program as the command files command interpreter. For example, the
188following command file sequence would be used to begin a
189.I csh
190script:
191.RS
192.nf
193#! /bin/csh
194# This shell script computes the checksum on /dev/foobar
195#
196 ...
197.fi
198.RE
199A single parameter may be passed the interpreter, specified after the
200name of the interpreter; its length and the length of the name
201of the interpreter combined must not exceed 32 characters.
202The space (or tab) following the '#!' is mandatory, and the
203pathname 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
207by
208.I execlp
209or
210.I execvp
211.SH "SEE ALSO"
212fork(2), environ(5), csh(1)
213.SH DIAGNOSTICS
214If the file cannot be found,
215if it is not executable,
216if it does not start with a valid magic number (see
217.IR a.out (5)),
218if maximum memory is exceeded,
219or if the arguments require too much space,
220a return
221constitutes the diagnostic;
222the return value is \-1.
223Even for the super-user,
224at least one of the execute-permission bits must be set for
225a file to be executed.
226.SH BUGS
227If
228.I execvp
229is called to execute a file that turns out to be a shell
230command file,
231and if it is impossible to execute the shell,
232the values of
233.I argv[0]
234and
235.I argv[\-1]
236will 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
247Plain
248.I exec
249is obsoleted by
250.I exece,
251but remains for historical reasons.
252.PP
253When the called file starts execution on the PDP11,
254the stack pointer points to a word containing the number of arguments.
255Just above
256this number is a list of pointers to the argument strings,
257followed by a null pointer, followed by the pointers to
258the environment strings and then another null pointer.
259The strings themselves follow;
260a 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
288On the Interdata 8/32,
289the stack begins at a conventional place
290(currently 0xD0000)
291and grows upwards.
292After
293.I exec,
294the layout of data on the stack is as follows.
295.PP
296.nf
297 int 0
298 arg0: byte ...
299 ...
300argp0: int arg0
301 ...
302 int 0
303envp0: 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
313This arrangement happens to conform well to C calling conventions.
314.PP
315On a VAX-11, the stack begins at
316.lg 0
3170x7ffff000
318.lg 1
319and grows towards lower numbered addresses.
320After
321.IR exec ,
322the 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