1 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
2 .\" and Copyright (c) 2006 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" 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@gmail.com>
32 .\" 2006-09-04 Michael Kerrisk <mtk.manpages@gmail.com>
33 .\" Added list of process attributes that are not preserved on exec().
34 .\" 2007-09-14 Ollie Wild <aaw@google.com>, mtk
35 .\" Add text describing limits on command-line arguments + environment
37 .TH EXECVE 2 2015-05-07 "Linux" "Linux Programmer's Manual"
39 execve \- execute program
41 .B #include <unistd.h>
43 .BI "int execve(const char *" filename ", char *const " argv "[], "
45 .BI " char *const " envp []);
48 executes the program pointed to by \fIfilename\fP.
49 \fIfilename\fP must be either a binary executable, or a script
50 starting with a line of the form:
54 \fB#!\fP \fIinterpreter \fP[optional-arg]
58 For details of the latter case, see "Interpreter scripts" below.
60 \fIargv\fP is an array of argument strings passed to the new program.
61 By convention, the first of these strings should contain the filename
62 associated with the file being executed.
63 \fIenvp\fP is an array of strings, conventionally of the form
64 \fBkey=value\fP, which are passed as environment to the new program.
65 Both \fIargv\fP and \fIenvp\fP must be terminated by a null pointer.
66 The argument vector and environment can be accessed by the
67 called program's main function, when it is defined as:
71 int main(int argc, char *argv[], char *envp[])
76 does not return on success, and the text, data, bss, and
77 stack of the calling process are overwritten by that of the program
80 If the current program is being ptraced, a \fBSIGTRAP\fP is sent to it
84 If the set-user-ID bit is set on the program file pointed to by
86 and the underlying filesystem is not mounted
92 and the calling process is not being ptraced,
93 then the effective user ID of the calling process is changed
94 to that of the owner of the program file.
95 Similarly, when the set-group-ID
96 bit of the program file is set the effective group ID of the calling
97 process is set to the group of the program file.
99 The effective user ID of the process is copied to the saved set-user-ID;
100 similarly, the effective group ID is copied to the saved set-group-ID.
101 This copying takes place after any effective ID changes that occur
102 because of the set-user-ID and set-group-ID mode bits.
104 If the executable is an a.out dynamically linked
105 binary executable containing
106 shared-library stubs, the Linux dynamic linker
108 is called at the start of execution to bring
109 needed shared libraries into memory
110 and link the executable with them.
112 If the executable is a dynamically linked ELF executable, the
113 interpreter named in the PT_INTERP segment is used to load the needed
115 This interpreter is typically
116 .I /lib/ld-linux.so.2
117 for binaries linked with glibc.
119 All process attributes are preserved during an
121 except the following:
123 The dispositions of any signals that are being caught are
127 Any alternate signal stack is not preserved
128 .RB ( sigaltstack (2)).
130 Memory mappings are not preserved
133 Attached System\ V shared memory segments are detached
136 POSIX shared memory regions are unmapped
139 Open POSIX message queue descriptors are closed
140 .RB ( mq_overview (7)).
142 Any open POSIX named semaphores are closed
143 .RB ( sem_overview (7)).
145 POSIX timers are not preserved
146 .RB ( timer_create (2)).
148 Any open directory streams are closed
151 Memory locks are not preserved
155 Exit handlers are not preserved
159 The floating-point environment is reset to the default (see
162 The process attributes in the preceding list are all specified
164 The following Linux-specific process attributes are also
165 not preserved during an
172 unless a set-user-ID or set-group ID program is being executed,
173 in which case it is cleared.
180 (Since Linux 2.4.36 / 2.6.23)
181 If a set-user-ID or set-group-ID program is being executed,
182 then the parent death signal set by
187 The process name, as set by
191 .IR "ps\ \-o comm" ),
192 is reset to the name of the new executable file.
199 .BR capabilities (7).
201 The termination signal is reset to
206 The file descriptor table is unshared, undoing the effect of the
211 Note the following further points:
213 All threads other than the calling thread are destroyed during an
215 Mutexes, condition variables, and other pthreads objects are not preserved.
217 The equivalent of \fIsetlocale(LC_ALL, "C")\fP
218 is executed at program start-up.
220 POSIX.1 specifies that the dispositions of any signals that
221 are ignored or set to the default are left unchanged.
222 POSIX.1 specifies one exception: if
225 then an implementation may leave the disposition unchanged or
226 reset it to the default; Linux does the former.
228 Any outstanding asynchronous I/O operations are canceled
232 For the handling of capabilities during
235 .BR capabilities (7).
237 By default, file descriptors remain open across an
239 File descriptors that are marked close-on-exec are closed;
240 see the description of
244 (If a file descriptor is closed, this will cause the release
245 of all record locks obtained on the underlying file by this process.
249 POSIX.1 says that if file descriptors 0, 1, and 2 would
250 otherwise be closed after a successful
252 and the process would gain privilege because the set-user_ID or
253 set-group_ID mode bit was set on the executed file,
254 then the system may open an unspecified file for each of these
256 As a general principle, no portable program, whether privileged or not,
257 can assume that these three file descriptors will remain
260 .\" On Linux it appears that these file descriptors are
261 .\" always open after an execve(), and it looks like
262 .\" Solaris 8 and FreeBSD 6.1 are the same. -- mtk, 30 Apr 2007
263 .SS Interpreter scripts
264 An interpreter script is a text file that has execute
265 permission enabled and whose first line is of the form:
269 \fB#!\fP \fIinterpreter \fP[optional-arg]
275 must be a valid pathname for an
276 executable which is not itself a script.
281 specifies an interpreter script, then
283 will be invoked with the following arguments:
287 \fIinterpreter\fP [optional-arg] \fIfilename\fP arg...
293 is the series of words pointed to by the
302 should either be absent, or be specified as a single word (i.e., it
303 should not contain white space); see NOTES below.
304 .SS Limits on size of arguments and environment
305 Most UNIX implementations impose some limit on the total size
306 of the command-line argument
310 strings that may be passed to a new program.
311 POSIX.1 allows an implementation to advertise this limit using the
313 constant (either defined in
315 or available at run time using the call
316 .IR "sysconf(_SC_ARG_MAX)" ).
318 On Linux prior to kernel 2.6.23, the memory used to store the
319 environment and argument strings was limited to 32 pages
320 (defined by the kernel constant
322 On architectures with a 4-kB page size,
323 this yields a maximum size of 128 kB.
325 On kernel 2.6.23 and later, most architectures support a size limit
326 derived from the soft
330 that is in force at the time of the
333 (Architectures with no memory management unit are excepted:
334 they maintain the limit that was in effect before kernel 2.6.23.)
335 This change allows programs to have a much larger
336 argument and/or environment list.
337 .\" For some background on the changes to ARG_MAX in kernels 2.6.23 and
339 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=5786
340 .\" http://bugzilla.kernel.org/show_bug.cgi?id=10095
341 .\" http://thread.gmane.org/gmane.linux.kernel/646709/focus=648101,
342 .\" checked into 2.6.25 as commit a64e715fc74b1a7dcc5944f848acc38b2c4d4ee2.
343 For these architectures, the total size is limited to 1/4 of the allowed
345 (Imposing the 1/4-limit
346 ensures that the new program always has some stack space.)
347 .\" Ollie: That doesn't include the lists of pointers, though,
348 .\" so the actual usage is a bit higher (1 pointer per argument).
350 the kernel places a floor of 32 pages on this size limit,
354 applications are guaranteed to have at least as much argument and
355 environment space as was provided by Linux 2.6.23 and earlier.
356 (This guarantee was not provided in Linux 2.6.23 and 2.6.24.)
357 Additionally, the limit per string is 32 pages (the kernel constant
358 .BR MAX_ARG_STRLEN ),
359 and the maximum number of strings is 0x7FFFFFFF.
363 does not return, on error \-1 is returned, and
365 is set appropriately.
369 The total number of bytes in the environment
376 Search permission is denied on a component of the path prefix of
378 or the name of a script interpreter.
380 .BR path_resolution (7).)
383 The file or a script interpreter is not a regular file.
386 Execute permission is denied for the file or a script or ELF interpreter.
389 The filesystem is mounted
392 .BR EAGAIN " (since Linux 3.1)"
393 .\" commit 72fa59970f8698023045ab0713d66f3f4f96945c
394 Having changed its real UID using one of the
396 calls, the caller was\(emand is now still\(emabove its
400 For a more detailed explanation of this error, see NOTES.
404 or one of the pointers in the vectors
408 points outside your accessible address space.
411 An ELF executable had more than one PT_INTERP segment (i.e., tried to
412 name more than one interpreter).
415 An I/O error occurred.
418 An ELF interpreter was a directory.
421 An ELF interpreter was not in a recognized format.
424 Too many symbolic links were encountered in resolving
426 or the name of a script or ELF interpreter.
429 The process has the maximum number of files open.
436 The system limit on the total number of open files has been reached.
441 or a script or ELF interpreter does not exist, or a shared library
442 .\" FIXME but see http://sourceware.org/bugzilla/show_bug.cgi?id=12241
443 needed for file or interpreter cannot be found.
446 An executable is not in a recognized format, is for the wrong
447 architecture, or has some other format error that means it cannot be
451 Insufficient kernel memory was available.
454 A component of the path prefix of
456 or a script or ELF interpreter is not a directory.
459 The filesystem is mounted
461 the user is not the superuser,
462 and the file has the set-user-ID or set-group-ID bit set.
465 The process is being traced, the user is not the superuser and the
466 file has the set-user-ID or set-group-ID bit set.
469 Executable was open for writing by one or more processes.
471 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
472 POSIX.1-2001 does not document the #! behavior
473 but is otherwise compatible.
474 .\" SVr4 documents additional error
475 .\" conditions EAGAIN, EINTR, ELIBACC, ENOLINK, EMULTIHOP; POSIX does not
476 .\" document ETXTBSY, EPERM, EFAULT, ELOOP, EIO, ENFILE, EMFILE, EINVAL,
477 .\" EISDIR or ELIBBAD error conditions.
479 Set-user-ID and set-group-ID processes can not be
482 The result of mounting a filesystem
484 varies across Linux kernel versions:
485 some will refuse execution of set-user-ID and set-group-ID
486 executables when this would
487 give the user powers she did not have already (and return
489 some will just ignore the set-user-ID and set-group-ID bits and
497 can be specified as NULL.
498 In both cases, this has the same effect as specifying the argument
499 as a pointer to a list containing a single null pointer.
500 .B "Do not take advantage of this nonstandard and nonportable misfeature!"
501 On many other UNIX systems, specifying
503 as NULL will result in an error
506 other UNIX systems treat the
508 case the same as Linux.
509 .\" e.g., EFAULT on Solaris 8 and FreeBSD 6.1; but
510 .\" HP-UX 11 is like Linux -- mtk, Apr 2007
511 .\" Bug filed 30 Apr 2007: http://bugzilla.kernel.org/show_bug.cgi?id=8408
512 .\" Bug rejected (because fix would constitute an ABI change).
515 POSIX.1 says that values returned by
517 should be invariant over the lifetime of a process.
518 However, since Linux 2.6.23, if the
520 resource limit changes, then the value reported by
523 to reflect the fact that the limit on space for holding
524 command-line arguments and environment variables has changed.
528 fails, control returns to the original executable image,
531 can then handle the error.
532 However, in (rare) cases (typically caused by resource exhaustion),
533 failure may occur past the point of no return:
534 the original executable image has been torn down,
535 but the new image could not be completely built.
536 In such cases, the kernel kills the process with a
540 .SS Interpreter scripts
541 A maximum line length of 127 characters is allowed for the first line in
542 an interpreter scripts.
546 argument of an interpreter script vary across implementations.
547 On Linux, the entire string following the
549 name is passed as a single argument to the interpreter,
550 and this string can include white space.
551 However, behavior differs on some other systems.
554 use the first white space to terminate
557 .\" e.g., FreeBSD before 6.0, but not FreeBSD 6.0 onward
558 an interpreter script can have multiple arguments,
561 are used to delimit the arguments.
563 Linux ignores the set-user-ID and set-group-ID bits on scripts.
566 .\" Some Linux versions have failed to check permissions on ELF
567 .\" interpreters. This is a security hole, because it allows users to
568 .\" open any file, such as a rewinding tape device, for reading. Some
569 .\" Linux versions have also had other security holes in
571 .\" that could be exploited for denial of service by a suitably crafted
572 .\" ELF binary. There are no known problems with 2.0.34 or 2.2.15.
573 .SS execve() and EAGAIN
574 A more detailed explanation of the
576 error that can occur (since Linux 3.1) when calling
582 error can occur when a
589 caused the real user ID of the process to change,
590 and that change caused the process to exceed its
592 resource limit (i.e., the number of processes belonging
593 to the new real UID exceeds the resource limit).
594 From Linux 2.6.0 to 3.0, this caused the
598 .\" commit 909cc4ae86f3380152a18e2a3c44523893ee11c4
599 the resource limit was not imposed on processes that
600 changed their user IDs.)
602 Since Linux 3.1, the scenario just described no longer causes the
605 because it too often led to security holes where buggy applications
606 didn't check the return status and assumed
607 that\(emif the caller had root privileges\(emthe call would always succeed.
610 calls now successfully change the real UID,
611 but the kernel sets an internal flag, named
612 .BR PF_NPROC_EXCEEDED ,
615 resource limit has been exceeded.
617 .BR PF_NPROC_EXCEEDED
618 flag is set and the resource limit is still
619 exceeded at the time of a subsequent
621 call, that call fails with the error
623 This kernel logic ensures that the
625 resource limit is still enforced for the
626 common privileged daemon workflow\(emnamely,
633 If the resource limit was not still exceeded at the time of the
636 (because other processes belonging to this real UID terminated between the
642 call succeeds and the kernel clears the
643 .BR PF_NPROC_EXCEEDED
645 The flag is also cleared if a subsequent call to
647 by this process succeeds.
649 With UNIX\ V6, the argument list of an
652 while the argument list of
655 Thus, this argument list was not directly usable in a further
658 Since UNIX\ V7, both are NULL.
661 .\" Some Linux versions have failed to check permissions on ELF
662 .\" interpreters. This is a security hole, because it allows users to
663 .\" open any file, such as a rewinding tape device, for reading. Some
664 .\" Linux versions have also had other security holes in
666 .\" that could be exploited for denial of service by a suitably crafted
667 .\" ELF binary. There are no known problems with 2.0.34 or 2.2.15.
669 The following program is designed to be execed by the second program below.
670 It just echoes its command-line arguments, one per line.
680 main(int argc, char *argv[])
684 for (j = 0; j < argc; j++)
685 printf("argv[%d]: %s\\n", j, argv[j]);
692 This program can be used to exec the program named in its command-line
704 main(int argc, char *argv[])
706 char *newargv[] = { NULL, "hello", "world", NULL };
707 char *newenviron[] = { NULL };
710 fprintf(stderr, "Usage: %s <file\-to\-exec>\\n", argv[0]);
714 newargv[0] = argv[1];
716 execve(argv[1], newargv, newenviron);
717 perror("execve"); /* execve() returns only on error */
723 We can use the second program to exec the first as follows:
727 .RB "$" " cc myecho.c \-o myecho"
728 .RB "$" " cc execve.c \-o execve"
729 .RB "$" " ./execve ./myecho"
736 We can also use these programs to demonstrate the use of a script
738 To do this we create a script whose "interpreter" is our
744 .RB "$" " cat > script"
745 .B #!./myecho script-arg
747 .RB "$" " chmod +x script"
751 We can then use our program to exec the script:
755 .RB "$" " ./execve ./script"
773 .BR path_resolution (7),