1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
4 .\" and Copyright (c) 2006 Michael Kerrisk <mtk-manpages@gmx.net>
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date. The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein. The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
26 .\" Modified by Michael Haardt <michael@moria.de>
27 .\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu>
28 .\" Modified 1994-08-21 by Michael Chastain <mec@shell.portal.com>:
29 .\" Modified 1997-01-31 by Eric S. Raymond <esr@thyrsus.com>
30 .\" Modified 1999-11-12 by Urs Thuermann <urs@isnogud.escape.de>
31 .\" Modified 2004-06-23 by Michael Kerrisk <mtk-manpages@gmx.net>
32 .\" 2006-09-04 Michael Kerrisk <mtk-manpages@gmx.net>
33 .\" Added list of process attributes that are not preserved on exec().
35 .TH EXECVE 2 2006-09-04 "Linux" "Linux Programmer's Manual"
37 execve \- execute program
39 .B #include <unistd.h>
41 .BI "int execve(const char *" filename ", char *const " argv "[], "
43 .BI " char *const " envp []);
46 executes the program pointed to by \fIfilename\fP.
47 \fIfilename\fP must be either a binary executable, or a script
48 starting with a line of the form:
52 \fB#! \fIinterpreter \fR[optional-arg]
56 For details of the latter case, see "Interpreter scripts" below.
58 \fIargv\fP is an array of argument strings passed to the new program.
59 \fIenvp\fP is an array of strings, conventionally of the form
60 \fBkey=value\fR, which are passed as environment to the new program.
61 Both \fIargv\fP and \fIenvp\fP must be terminated by a null pointer.
62 The argument vector and environment can be accessed by the
63 called program's main function, when it is defined as:
67 int main(int argc, char *argv[], char *envp[])\fR.
72 does not return on success, and the text, data, bss, and
73 stack of the calling process are overwritten by that of the program
76 If the current program is being ptraced, a \fBSIGTRAP\fP is sent to it
80 If the set-user-ID bit is set on the program file pointed to by
82 and the underlying file system is not mounted
88 and the calling process is not being ptraced,
89 then the effective user ID of the calling process is changed
90 to that of the owner of the program file.
91 Similarly, when the set-group-ID
92 bit of the program file is set the effective group ID of the calling
93 process is set to the group of the program file.
95 The effective user ID of the process is copied to the saved set-user-ID;
96 similarly, the effective group ID is copied to the saved set-group-ID.
97 This copying takes place after any effective ID changes that occur
98 because of the set-user-ID and set-group-ID permission bits.
100 If the executable is an a.out dynamically-linked
101 binary executable containing
102 shared-library stubs, the Linux dynamic linker
104 is called at the start of execution to bring
105 needed shared libraries into memory
106 and link the executable with them.
108 If the executable is a dynamically-linked ELF executable, the
109 interpreter named in the PT_INTERP segment is used to load the needed
111 This interpreter is typically
112 \fI/lib/ld-linux.so.1\fR for binaries linked with the Linux libc
113 version 5, or \fI/lib/ld-linux.so.2\fR for binaries linked with the
116 All process attributes are preserved during an
118 except the following:
120 The set of pending signals is cleared
121 .RB ( sigpending (2)).
123 The dispositions of any signals that are being caught are
124 reset to being ignored.
126 Any alternate signal stack is not preserved
127 .RB ( sigaltstack (2)).
129 Memory mappings are not preserved
132 Attached System V shared memory segments are detached
135 POSIX shared memory regions are unmapped
138 Open POSIX message queue descriptors are closed
139 .RB ( mq_overview (7)).
141 Any open POSIX named semaphores are closed
142 .RB ( sem_overview (7)).
144 POSIX timers are not preserved
145 .RB ( timer_create (3)).
147 Any open directory streams are closed
150 Memory locks are not preserved
154 Exit handlers are not preserved
158 The process attributes in the preceding list are all specified
160 The following Linux-specific process attributes are also
161 not preserved during an
168 unless a set-user-ID or set-group ID program is being executed,
169 in which case it is cleared.
176 The process name, as set by
181 is reset to the name of the new executable file.
183 The termination signal is reset to
188 Note the following further points:
190 All threads other than the calling thread are destroyed during an
192 Mutexes, condition variables, and other pthreads objects are not preserved.
194 The equivalent of \fIsetlocale(LC_ALL, "C")\fP
195 is executed at program start-up.
197 POSIX.1-2001 specifies that the dispositions of any signals that
198 are ignored or set to the default are left unchanged.
199 POSIX.1-2001 specifies one exception: if
202 then an implementation may leave the disposition unchanged or
203 reset it to the default; Linux does the former.
205 Any outstanding asynchronous I/O operations are canceled
209 For the handling of capabilities during
212 .BR capabilities (7).
214 By default, file descriptors remain open across an
216 File descriptors that are marked close-on-exec are closed
217 ; see the description of
221 (If a file descriptor is closed, this will cause the release
222 of all record locks obtained on the underlying file by this process.
226 POSIX.1-2001 says that if file descriptors 0, 1, and 2 would
227 otherwise be closed after a successful
229 and the process would gain privilege because the set-user_ID or
230 set-group_ID permission bit was set on the executed file,
231 then the system may open an unspecified file for each of these
233 As a general principle, no portable program, whether privileged or not,
234 can assume that these three file descriptors will remain
237 .\" On Linux it appears that these file descriptors are
238 .\" always open after an execve(), and it looks like
239 .\" Solaris 8 and FreeBSD 6.1 are the same. -- mtk, 30 Apr 2007
240 .SS Interpreter scripts
241 An interpreter script is a text file that has execute
242 permission enabled and whose first line is of the form:
246 \fB#! \fIinterpreter \fR[optional-arg]
252 must be a valid pathname for an
253 executable which is not itself a script.
258 specifies an interpreter script, then
260 will be invoked with the following arguments:
264 \fIinterpreter\fR [optional-arg] \fIfilename\fR arg...
270 is the series of words pointed to by the
277 should either be absent, or be specified as a single word (i.e., it
278 should not contain white space); see NOTES below.
282 does not return, on error \-1 is returned, and
284 is set appropriately.
288 The total number of bytes in the environment
295 Search permission is denied on a component of the path prefix of
297 or the name of a script interpreter.
299 .BR path_resolution (7).)
302 The file or a script interpreter is not a regular file.
305 Execute permission is denied for the file or a script or ELF interpreter.
308 The file system is mounted
313 points outside your accessible address space.
316 An ELF executable had more than one PT_INTERP segment (i.e., tried to
317 name more than one interpreter).
320 An I/O error occurred.
323 An ELF interpreter was a directory.
326 An ELF interpreter was not in a recognized format.
329 Too many symbolic links were encountered in resolving
331 or the name of a script or ELF interpreter.
334 The process has the maximum number of files open.
341 The system limit on the total number of open files has been reached.
346 or a script or ELF interpreter does not exist, or a shared library
347 needed for file or interpreter cannot be found.
350 An executable is not in a recognized format, is for the wrong
351 architecture, or has some other format error that means it cannot be
355 Insufficient kernel memory was available.
358 A component of the path prefix of
360 or a script or ELF interpreter is not a directory.
363 The file system is mounted
365 the user is not the superuser,
366 and the file has the set-user-ID or set-group-ID bit set.
369 The process is being traced, the user is not the superuser and the
370 file has the set-user-ID or set-group-ID bit set.
373 Executable was open for writing by one or more processes.
375 SVr4, 4.3BSD, POSIX.1-2001.
376 POSIX.1-2001 does not document the #! behavior
377 but is otherwise compatible.
378 .\" SVr4 documents additional error
379 .\" conditions EAGAIN, EINTR, ELIBACC, ENOLINK, EMULTIHOP; POSIX does not
380 .\" document ETXTBSY, EPERM, EFAULT, ELOOP, EIO, ENFILE, EMFILE, EINVAL,
381 .\" EISDIR or ELIBBAD error conditions.
383 Set-user-ID and set-group-ID processes can not be
386 Linux ignores the set-user-ID and set-group-ID bits on scripts.
388 The result of mounting a filesystem
390 varies across Linux kernel versions:
391 some will refuse execution of set-user-ID and set-group-ID
392 executables when this would
393 give the user powers she did not have already (and return EPERM),
394 some will just ignore the set-user-ID and set-group-ID bits and
398 A maximum line length of 127 characters is allowed for the first line in
399 a #! executable shell script.
403 argument of an interpreter script vary across implementations.
404 On Linux, the entire string following the
406 name is passed as a single argument to the interpreter,
407 and this string can include white space.
408 However, behavior differs on some other systems.
411 use the first white space to terminate
414 .\" e.g., FreeBSD before 6.0, but not FreeBSD 6.0 onwards
415 an interpreter script can have multiple arguments,
418 are used to delimit the arguments.
424 can be specified as NULL,
425 which has the same effect as specifying these arguments
426 as pointers to lists containing a single NULL pointer.
427 .BR "Do not take advantage of this misfeature!"
428 It is non-standard and non-portable:
429 on most other Unix systems doing this will result in an error.
430 .\" e.g., EFAULT on Solaris 8 and FreeBSD 6.1; but
431 .\" HP-UX 11 is like Linux -- mtk, Apr 2007
433 .\" Bug filed 30 Apr 2007: http://bugzilla.kernel.org/show_bug.cgi?id=8408
434 .\" Maybe this will get fixed (but it would constitute an ABI change).
437 .\" Some Linux versions have failed to check permissions on ELF
438 .\" interpreters. This is a security hole, because it allows users to
439 .\" open any file, such as a rewinding tape device, for reading. Some
440 .\" Linux versions have also had other security holes in
442 .\" that could be exploited for denial of service by a suitably crafted
443 .\" ELF binary. There are no known problems with 2.0.34 or 2.2.15.
445 With Unix V6 the argument list of an
448 while the argument list of
451 Thus, this argument list was not directly usable in a further
454 Since Unix V7 both are NULL.
456 The following program is designed to execed by the second program below.
457 It just echoes its command-line one per line.
467 main(int argc, char *argv[])
471 for (j = 0; j < argc; j++)
472 printf("argv[%d]: %s\\n", j, argv[j]);
479 This program can be used to exec the program named in its command-line
492 main(int argc, char *argv[])
494 char *newargv[] = { NULL, "hello", "world", NULL };
495 char *newenviron[] = { NULL };
497 assert(argc == 2); /* argv[1] identifies
499 newargv[0] = argv[1];
501 execve(argv[1], newargv, newenviron);
502 perror("execve"); /* execve() only returns on error */
508 We can use the second program to exec the first as follows:
512 $ cc myecho.c -o myecho
513 $ cc execve.c -o execve
521 We can also use these programs to demonstrate the use of a script
523 To do this we create a script whose "interpreter" is our
530 #! ./myecho script-arg
536 We can then use our program to exec the script:
540 $ ./execve ./script.sh
556 .BR path_resolution (7),