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 2018-04-30 "Linux" "Linux Programmer's Manual"
39 execve \- execute program
41 .B #include <unistd.h>
43 .BI "int execve(const char *" pathname ", char *const " argv "[], "
45 .BI " char *const " envp []);
48 executes the program referred to by \fIpathname\fP.
49 This causes the program that is currently being run by the calling process
50 to be replaced with a new program, with newly initialized stack, heap,
51 and (initialized and uninitialized) data segments.
53 \fIpathname\fP must be either a binary executable, or a script
54 starting with a line of the form:
58 \fB#!\fP \fIinterpreter \fP[optional-arg]
62 For details of the latter case, see "Interpreter scripts" below.
64 \fIargv\fP is an array of argument strings passed to the new program.
65 By convention, the first of these strings (i.e.,
67 should contain the filename associated with the file being executed.
68 \fIenvp\fP is an array of strings, conventionally of the form
69 \fBkey=value\fP, which are passed as environment to the new program.
70 The \fIargv\fP and \fIenvp\fP arrays must each include a null pointer
71 at the end of the array.
73 The argument vector and environment can be accessed by the
74 called program's main function, when it is defined as:
78 int main(int argc, char *argv[], char *envp[])
82 Note, however, that the use of a third argument to the main function
83 is not specified in POSIX.1;
85 the environment should be accessed via the external variable
89 does not return on success, and the text, initialized data,
90 uninitialized data (bss), and stack of the calling process are overwritten
91 according to the contents of the newly loaded program.
93 If the current program is being ptraced, a \fBSIGTRAP\fP signal is sent to it
97 If the set-user-ID bit is set on the program file referred to by
99 then the effective user ID of the calling process is changed
100 to that of the owner of the program file.
101 Similarly, when the set-group-ID
102 bit of the program file is set the effective group ID of the calling
103 process is set to the group of the program file.
105 The aforementioned transformations of the effective IDs are
107 performed (i.e., the set-user-ID and set-group-ID bits are ignored)
108 if any of the following is true:
112 attribute is set for the calling thread (see
115 the underlying filesystem is mounted
123 the calling process is being ptraced.
125 The capabilities of the program file (see
126 .BR capabilities (7))
127 are also ignored if any of the above are true.
129 The effective user ID of the process is copied to the saved set-user-ID;
130 similarly, the effective group ID is copied to the saved set-group-ID.
131 This copying takes place after any effective ID changes that occur
132 because of the set-user-ID and set-group-ID mode bits.
134 The process's real UID and real GID, as well its supplementary group IDs,
135 are unchanged by a call to
138 If the executable is an a.out dynamically linked
139 binary executable containing
140 shared-library stubs, the Linux dynamic linker
142 is called at the start of execution to bring
143 needed shared objects into memory
144 and link the executable with them.
146 If the executable is a dynamically linked ELF executable, the
147 interpreter named in the PT_INTERP segment is used to load the needed
149 This interpreter is typically
150 .I /lib/ld-linux.so.2
151 for binaries linked with glibc (see
152 .BR ld-linux.so (8)).
154 All process attributes are preserved during an
156 except the following:
158 The dispositions of any signals that are being caught are
162 Any alternate signal stack is not preserved
163 .RB ( sigaltstack (2)).
165 Memory mappings are not preserved
168 Attached System\ V shared memory segments are detached
171 POSIX shared memory regions are unmapped
174 Open POSIX message queue descriptors are closed
175 .RB ( mq_overview (7)).
177 Any open POSIX named semaphores are closed
178 .RB ( sem_overview (7)).
180 POSIX timers are not preserved
181 .RB ( timer_create (2)).
183 Any open directory streams are closed
186 Memory locks are not preserved
190 Exit handlers are not preserved
194 The floating-point environment is reset to the default (see
197 The process attributes in the preceding list are all specified
199 The following Linux-specific process attributes are also
200 not preserved during an
207 unless a set-user-ID or set-group ID program is being executed,
208 in which case it is cleared.
215 (Since Linux 2.4.36 / 2.6.23)
216 If a set-user-ID or set-group-ID program is being executed,
217 then the parent death signal set by
222 The process name, as set by
226 .IR "ps\ \-o comm" ),
227 is reset to the name of the new executable file.
234 .BR capabilities (7).
236 The termination signal is reset to
241 The file descriptor table is unshared, undoing the effect of the
246 Note the following further points:
248 All threads other than the calling thread are destroyed during an
250 Mutexes, condition variables, and other pthreads objects are not preserved.
252 The equivalent of \fIsetlocale(LC_ALL, "C")\fP
253 is executed at program start-up.
255 POSIX.1 specifies that the dispositions of any signals that
256 are ignored or set to the default are left unchanged.
257 POSIX.1 specifies one exception: if
260 then an implementation may leave the disposition unchanged or
261 reset it to the default; Linux does the former.
263 Any outstanding asynchronous I/O operations are canceled
267 For the handling of capabilities during
270 .BR capabilities (7).
272 By default, file descriptors remain open across an
274 File descriptors that are marked close-on-exec are closed;
275 see the description of
279 (If a file descriptor is closed, this will cause the release
280 of all record locks obtained on the underlying file by this process.
284 POSIX.1 says that if file descriptors 0, 1, and 2 would
285 otherwise be closed after a successful
287 and the process would gain privilege because the set-user-ID or
288 set-group_ID mode bit was set on the executed file,
289 then the system may open an unspecified file for each of these
291 As a general principle, no portable program, whether privileged or not,
292 can assume that these three file descriptors will remain
295 .\" On Linux it appears that these file descriptors are
296 .\" always open after an execve(), and it looks like
297 .\" Solaris 8 and FreeBSD 6.1 are the same. -- mtk, 30 Apr 2007
298 .SS Interpreter scripts
299 An interpreter script is a text file that has execute
300 permission enabled and whose first line is of the form:
304 \fB#!\fP \fIinterpreter \fP[optional-arg]
310 must be a valid pathname for an executable file.
315 specifies an interpreter script, then
317 will be invoked with the following arguments:
321 \fIinterpreter\fP [optional-arg] \fIpathname\fP arg...
327 is the series of words pointed to by the
336 should either be absent, or be specified as a single word (i.e., it
337 should not contain white space); see NOTES below.
340 .\" commit bf2a9a39639b8b51377905397a5005f444e9a892
341 the kernel permits the interpreter of a script to itself be a script.
342 This permission is recursive, up to a limit of four recursions,
343 so that the interpreter may be a script which is interpreted by a script,
345 .SS Limits on size of arguments and environment
346 Most UNIX implementations impose some limit on the total size
347 of the command-line argument
351 strings that may be passed to a new program.
352 POSIX.1 allows an implementation to advertise this limit using the
354 constant (either defined in
356 or available at run time using the call
357 .IR "sysconf(_SC_ARG_MAX)" ).
359 On Linux prior to kernel 2.6.23, the memory used to store the
360 environment and argument strings was limited to 32 pages
361 (defined by the kernel constant
363 On architectures with a 4-kB page size,
364 this yields a maximum size of 128\ kB.
366 On kernel 2.6.23 and later, most architectures support a size limit
367 derived from the soft
371 that is in force at the time of the
374 (Architectures with no memory management unit are excepted:
375 they maintain the limit that was in effect before kernel 2.6.23.)
376 This change allows programs to have a much larger
377 argument and/or environment list.
378 .\" For some background on the changes to ARG_MAX in kernels 2.6.23 and
380 .\" http://sourceware.org/bugzilla/show_bug.cgi?id=5786
381 .\" http://bugzilla.kernel.org/show_bug.cgi?id=10095
382 .\" http://thread.gmane.org/gmane.linux.kernel/646709/focus=648101,
383 .\" checked into 2.6.25 as commit a64e715fc74b1a7dcc5944f848acc38b2c4d4ee2.
384 For these architectures, the total size is limited to 1/4 of the allowed
386 (Imposing the 1/4-limit
387 ensures that the new program always has some stack space.)
388 .\" Ollie: That doesn't include the lists of pointers, though,
389 .\" so the actual usage is a bit higher (1 pointer per argument).
390 Additionally, the total size is limited to 3/4 of the value
391 of the kernel constant
395 the kernel also places a floor of 32 pages on this size limit,
399 applications are guaranteed to have at least as much argument and
400 environment space as was provided by Linux 2.6.23 and earlier.
401 (This guarantee was not provided in Linux 2.6.23 and 2.6.24.)
402 Additionally, the limit per string is 32 pages (the kernel constant
403 .BR MAX_ARG_STRLEN ),
404 and the maximum number of strings is 0x7FFFFFFF.
408 does not return, on error \-1 is returned, and
410 is set appropriately.
414 The total number of bytes in the environment
421 Search permission is denied on a component of the path prefix of
423 or the name of a script interpreter.
425 .BR path_resolution (7).)
428 The file or a script interpreter is not a regular file.
431 Execute permission is denied for the file or a script or ELF interpreter.
434 The filesystem is mounted
437 .BR EAGAIN " (since Linux 3.1)"
438 .\" commit 72fa59970f8698023045ab0713d66f3f4f96945c
439 Having changed its real UID using one of the
441 calls, the caller was\(emand is now still\(emabove its
445 For a more detailed explanation of this error, see NOTES.
449 or one of the pointers in the vectors
453 points outside your accessible address space.
456 An ELF executable had more than one PT_INTERP segment (i.e., tried to
457 name more than one interpreter).
460 An I/O error occurred.
463 An ELF interpreter was a directory.
466 An ELF interpreter was not in a recognized format.
469 Too many symbolic links were encountered in resolving
471 or the name of a script or ELF interpreter.
474 The maximum recursion limit was reached during recursive script
475 interpretation (see "Interpreter scripts", above).
477 .\" commit d740269867021faf4ce38a449353d2b986c34a67
478 the error produced for this case was
482 The per-process limit on the number of open file descriptors has been reached.
489 The system-wide limit on the total number of open files has been reached.
494 or a script or ELF interpreter does not exist, or a shared library
495 .\" FIXME but see http://sourceware.org/bugzilla/show_bug.cgi?id=12241
496 needed for the file or interpreter cannot be found.
499 An executable is not in a recognized format, is for the wrong
500 architecture, or has some other format error that means it cannot be
504 Insufficient kernel memory was available.
507 A component of the path prefix of
509 or a script or ELF interpreter is not a directory.
512 The filesystem is mounted
514 the user is not the superuser,
515 and the file has the set-user-ID or set-group-ID bit set.
518 The process is being traced, the user is not the superuser and the
519 file has the set-user-ID or set-group-ID bit set.
522 A "capability-dumb" applications would not obtain the full set of
523 permitted capabilities granted by the executable file.
525 .BR capabilities (7).
528 The specified executable was open for writing by one or more processes.
530 POSIX.1-2001, POSIX.1-2008, SVr4, 4.3BSD.
531 POSIX does not document the #! behavior, but it exists
532 (with some variations) on other UNIX systems.
533 .\" SVr4 documents additional error
534 .\" conditions EAGAIN, EINTR, ELIBACC, ENOLINK, EMULTIHOP; POSIX does not
535 .\" document ETXTBSY, EPERM, EFAULT, ELOOP, EIO, ENFILE, EMFILE, EINVAL,
536 .\" EISDIR or ELIBBAD error conditions.
540 (and the related functions described in
542 described as "executing a
544 process" (or similar).
545 This is a highly misleading description:
546 there is no new process;
547 many attributes of the calling process remain unchanged
548 (in particular, its PID).
551 does is arrange for an existing process (the calling process)
552 to execute a new program.
554 Set-user-ID and set-group-ID processes can not be
557 The result of mounting a filesystem
559 varies across Linux kernel versions:
560 some will refuse execution of set-user-ID and set-group-ID
561 executables when this would
562 give the user powers they did not have already (and return
564 some will just ignore the set-user-ID and set-group-ID bits and
572 can be specified as NULL.
573 In both cases, this has the same effect as specifying the argument
574 as a pointer to a list containing a single null pointer.
575 .B "Do not take advantage of this nonstandard and nonportable misfeature!"
576 On many other UNIX systems, specifying
578 as NULL will result in an error
581 other UNIX systems treat the
583 case the same as Linux.
584 .\" e.g., EFAULT on Solaris 8 and FreeBSD 6.1; but
585 .\" HP-UX 11 is like Linux -- mtk, Apr 2007
586 .\" Bug filed 30 Apr 2007: http://bugzilla.kernel.org/show_bug.cgi?id=8408
587 .\" Bug rejected (because fix would constitute an ABI change).
590 POSIX.1 says that values returned by
592 should be invariant over the lifetime of a process.
593 However, since Linux 2.6.23, if the
595 resource limit changes, then the value reported by
598 to reflect the fact that the limit on space for holding
599 command-line arguments and environment variables has changed.
603 fails, control returns to the original executable image,
606 can then handle the error.
607 However, in (rare) cases (typically caused by resource exhaustion),
608 failure may occur past the point of no return:
609 the original executable image has been torn down,
610 but the new image could not be completely built.
611 In such cases, the kernel kills the process with a
615 .SS Interpreter scripts
616 A maximum line length of 127 characters is allowed for the first line in
617 an interpreter script.
621 argument of an interpreter script vary across implementations.
622 On Linux, the entire string following the
624 name is passed as a single argument to the interpreter,
625 and this string can include white space.
626 However, behavior differs on some other systems.
629 use the first white space to terminate
632 .\" e.g., FreeBSD before 6.0, but not FreeBSD 6.0 onward
633 an interpreter script can have multiple arguments,
636 are used to delimit the arguments.
638 Linux ignores the set-user-ID and set-group-ID bits on scripts.
641 .\" Some Linux versions have failed to check permissions on ELF
642 .\" interpreters. This is a security hole, because it allows users to
643 .\" open any file, such as a rewinding tape device, for reading. Some
644 .\" Linux versions have also had other security holes in
646 .\" that could be exploited for denial of service by a suitably crafted
647 .\" ELF binary. There are no known problems with 2.0.34 or 2.2.15.
648 .SS execve() and EAGAIN
649 A more detailed explanation of the
651 error that can occur (since Linux 3.1) when calling
657 error can occur when a
664 caused the real user ID of the process to change,
665 and that change caused the process to exceed its
667 resource limit (i.e., the number of processes belonging
668 to the new real UID exceeds the resource limit).
669 From Linux 2.6.0 to 3.0, this caused the
673 .\" commit 909cc4ae86f3380152a18e2a3c44523893ee11c4
674 the resource limit was not imposed on processes that
675 changed their user IDs.)
677 Since Linux 3.1, the scenario just described no longer causes the
680 because it too often led to security holes where buggy applications
681 didn't check the return status and assumed
682 that\(emif the caller had root privileges\(emthe call would always succeed.
685 calls now successfully change the real UID,
686 but the kernel sets an internal flag, named
687 .BR PF_NPROC_EXCEEDED ,
690 resource limit has been exceeded.
692 .BR PF_NPROC_EXCEEDED
693 flag is set and the resource limit is still
694 exceeded at the time of a subsequent
696 call, that call fails with the error
698 This kernel logic ensures that the
700 resource limit is still enforced for the
701 common privileged daemon workflow\(emnamely,
708 If the resource limit was not still exceeded at the time of the
711 (because other processes belonging to this real UID terminated between the
717 call succeeds and the kernel clears the
718 .BR PF_NPROC_EXCEEDED
720 The flag is also cleared if a subsequent call to
722 by this process succeeds.
724 With UNIX\ V6, the argument list of an
727 while the argument list of
730 Thus, this argument list was not directly usable in a further
733 Since UNIX\ V7, both are NULL.
736 .\" Some Linux versions have failed to check permissions on ELF
737 .\" interpreters. This is a security hole, because it allows users to
738 .\" open any file, such as a rewinding tape device, for reading. Some
739 .\" Linux versions have also had other security holes in
741 .\" that could be exploited for denial of service by a suitably crafted
742 .\" ELF binary. There are no known problems with 2.0.34 or 2.2.15.
744 The following program is designed to be execed by the second program below.
745 It just echoes its command-line arguments, one per line.
755 main(int argc, char *argv[])
759 for (j = 0; j < argc; j++)
760 printf("argv[%d]: %s\en", j, argv[j]);
767 This program can be used to exec the program named in its command-line
779 main(int argc, char *argv[])
781 char *newargv[] = { NULL, "hello", "world", NULL };
782 char *newenviron[] = { NULL };
785 fprintf(stderr, "Usage: %s <file\-to\-exec>\en", argv[0]);
789 newargv[0] = argv[1];
791 execve(argv[1], newargv, newenviron);
792 perror("execve"); /* execve() returns only on error */
798 We can use the second program to exec the first as follows:
802 .RB "$" " cc myecho.c \-o myecho"
803 .RB "$" " cc execve.c \-o execve"
804 .RB "$" " ./execve ./myecho"
811 We can also use these programs to demonstrate the use of a script
813 To do this we create a script whose "interpreter" is our
819 .RB "$" " cat > script"
820 .B #!./myecho script-arg
822 .RB "$" " chmod +x script"
826 We can then use our program to exec the script:
830 .RB "$" " ./execve ./script"
842 .BR get_robust_list (2),
850 .BR path_resolution (7),