execve(2)
๐ execve(2) ๐
okay bestie, welcome to the System Calls Manual, section 2, and we're talking about execve today. this one is a whole journey, so grab a snack ๐
โจ NAME โจ
execve โ execute program
๐ LIBRARY ๐
Standard C library (libc, -lc)
๐ธ SYNOPSIS ๐ธ
#include <unistd.h>
int execve(const char *path, char *const _Nullable argv[],
char *const _Nullable envp[]);
๐ฆ DESCRIPTION ๐ฆ
so. execve() executes the program referred to by path. and when we say "executes", we mean it replaces โ the program your calling process is currently running gets swapped out for a brand new program, with a freshly initialized stack, heap, and (initialized and uninitialized) data segments. it's giving glow up, but for processes ๐
path has to be either a binary executable, or a script that starts with a line shaped like this:
#!interpreter
[optional-arg]
for the details on that whole script situation, go look at "Interpreter scripts" down below ๐
argv is an array of pointers to strings that get passed to the new program as its command-line arguments. by convention, the first one of those strings (i.e., argv[0]) should hold the filename associated with the file being executed. the argv array must be terminated by a null pointer. (so yeah, in the new program, argv[argc] will be a null pointer.)
envp is an array of pointers to strings, conventionally in key=value form, which get passed as the environment of the new program. the envp array must also be terminated by a null pointer.
this manual page describes the Linux system call in full detail, bestie. if you want an overview of the nomenclature and the many โ often preferable โ standardised variants of this function that libc gives you, including ones that search the PATH environment variable, then you want exec(3).
the argument vector and the environment can be accessed by the new program's main function, when it's defined like this:
int main(int argc, char *argv[], char *envp[])
but like, heads up ๐ โ using a third argument to the main function is not specified in POSIX.1. according to POSIX.1, the environment should be accessed via the external variable environ(7).
execve() does not return on success, and the text, initialized data, uninitialized data (bss), and stack of the calling process all get overwritten according to the contents of the newly loaded program. gone. replaced. iconic behavior honestly.
if the current program is being ptraced, a SIGTRAP signal is sent to it after a successful execve().
if the set-user-ID bit is set on the program file referred to by path, then the effective user ID of the calling process is changed to that of the owner of the program file. similarly, if the set-group-ID bit is set on the program file, then the effective group ID of the calling process is set to the group of the program file. ๐
those transformations of the effective IDs are not performed (meaning: the set-user-ID and set-group-ID bits are just ignored, no cap) if any of these is true:
- the
no_new_privsattribute is set for the calling thread (seeprctl(2)); - the underlying filesystem is mounted
nosuid(theMS_NOSUIDflag formount(2)); or - the calling process is being ptraced.
the capabilities of the program file (see capabilities(7)) are also ignored if any of the above are true.
the effective user ID of the process is copied to the saved set-user-ID; similarly, the effective group ID is copied to the saved set-group-ID. this copying takes place after any effective ID changes that happen because of the set-user-ID and set-group-ID mode bits.
the process's real UID and real GID, along with its supplementary group IDs, are totally unchanged by a call to execve(). not touched. left alone. ๐ซถ
if the executable is an a.out dynamically linked binary executable containing shared-library stubs, the Linux dynamic linker ld.so(8) is called at the start of execution to bring the needed shared objects into memory and link the executable with them.
if the executable is a dynamically linked ELF executable, the interpreter named in the PT_INTERP segment is used to load the needed shared objects. this interpreter is typically /lib/ld-linux.so.2 for binaries linked with glibc (see ld-linux.so(8)).
๐ฅ Effect on process attributes ๐ฅ
all process attributes are preserved during an execve(), EXCEPT the following, bestie, pay attention:
- The dispositions of any signals that are being caught are reset to the default (
signal(7)). - Any alternate signal stack is not preserved (
sigaltstack(2)). - Memory mappings are not preserved (
mmap(2)). - Attached System V shared memory segments are detached (
shmat(2)). - POSIX shared memory regions are unmapped (
shm_open(3)). - Open POSIX message queue descriptors are closed (
mq_overview(7)). - Any open POSIX named semaphores are closed (
sem_overview(7)). - POSIX timers are not preserved (
timer_create(2)). - Any open directory streams are closed (
opendir(3)). - Memory locks are not preserved (
mlock(2),mlockall(2)). - Exit handlers are not preserved (
atexit(3),on_exit(3)). - The floating-point environment is reset to the default (see
fenv(3)).
everything in that list above is specified in POSIX.1, slay. but the following Linux-specific process attributes are also not preserved during an execve():
- The process's "dumpable" attribute is set to the value
1, unless a set-user-ID program, a set-group-ID program, or a program with capabilities is being executed, in which case the dumpable flag may instead be reset to the value in/proc/sys/fs/suid_dumpable, in the circumstances described underPR_SET_DUMPABLEinprctl(2). Note that changes to the "dumpable" attribute may cause ownership of files in the process's/proc/piddirectory to change toroot:root, as described inproc(5). - The
prctl(2)PR_SET_KEEPCAPSflag is cleared. - (Since Linux 2.4.36 / 2.6.23) If a set-user-ID or set-group-ID program is being executed, then the parent death signal set by
prctl(2)PR_SET_PDEATHSIGflag is cleared. - The process name, as set by
prctl(2)PR_SET_NAME(and displayed byps -o comm), is reset to the name of the new executable file. - The
SECBIT_KEEP_CAPSsecurebits flag is cleared. Seecapabilities(7). - The termination signal is reset to
SIGCHLD(seeclone(2)). - The file descriptor table is unshared, undoing the effect of the
CLONE_FILESflag ofclone(2).
also, note the following further points, bestie, don't skip these:
- All threads other than the calling thread are destroyed during an
execve(). Mutexes, condition variables, and other pthreads objects are not preserved. yeah. literally all of them ๐ - The equivalent of
setlocale(LC_ALL, "C")is executed at program start-up. - POSIX.1 specifies that the dispositions of any signals that are ignored or set to the default are left unchanged. POSIX.1 specifies one exception: if
SIGCHLDis being ignored, then an implementation may leave the disposition unchanged or reset it to the default; Linux does the former. - Any outstanding asynchronous I/O operations are canceled (
aio_read(3),aio_write(3)). - For the handling of capabilities during
execve(), seecapabilities(7). - By default, file descriptors remain open across an
execve(). File descriptors that are marked close-on-exec are closed; see the description ofFD_CLOEXECinfcntl(2). (If a file descriptor is closed, this will cause the release of all record locks obtained on the underlying file by this process. Seefcntl(2)for details.) POSIX.1 says that if file descriptors0,1, and2would otherwise be closed after a successfulexecve(), and the process would gain privilege because the set-user-ID or set-group-ID mode bit was set on the executed file, then the system may open an unspecified file for each of these file descriptors. As a general principle, no portable program, whether privileged or not, can assume that these three file descriptors will remain closed across anexecve().
๐จ bestie this matters: NO portable program โ privileged or not โ can assume those three fds stay closed across an execve(). periodt.
๐ง Interpreter scripts ๐ง
an interpreter script is a text file that has execute permission enabled and whose first line is of the form:
#!interpreter [optional-arg]
the interpreter must be a valid pathname for an executable file.
interpreter will be invoked with the following arguments:
interpreter [optional-arg] path arg...
where arg... is the series of words pointed to by the argv argument of execve(), starting at argv[1]. note that there is no way to get the argv[0] that was passed to the execve() call. it's just gone, bb ๐ญ
for portable use, optional-arg should either be absent, or be specified as a single word (i.e., it should not contain white space); see VERSIONS below.
since Linux 2.6.28, the kernel permits the interpreter of a script to itself be a script. this permission is recursive, up to a limit of four recursions, so that the interpreter may be a script which is interpreted by a script, and so on. inception vibes, truly ๐
๐ Limits on size of arguments and environment ๐
most UNIX implementations impose some limit on the total size of the command-line argument (argv) and environment (envp) strings that may be passed to a new program. POSIX.1 allows an implementation to advertise this limit using the ARG_MAX constant (either defined in <limits.h> or available at run time using the call sysconf(_SC_ARG_MAX)).
before Linux 2.6.23, the memory used to store the environment and argument strings was limited to 32 pages (defined by the kernel constant MAX_ARG_PAGES). on architectures with a 4-kB page size, this yields a maximum size of 128 kB.
on Linux 2.6.23 and later, most architectures support a size limit derived from the soft RLIMIT_STACK resource limit (see getrlimit(2)) that is in force at the time of the execve() call. (Architectures with no memory management unit are excepted: they maintain the limit that was in effect before Linux 2.6.23.) this change allows programs to have a much larger argument and/or environment list. for these architectures, the total size is limited to 1/4 of the allowed stack size. (imposing the 1/4-limit ensures that the new program always has some stack space.) additionally, the total size is limited to 3/4 of the value of the kernel constant _STK_LIM (8 MiB). since Linux 2.6.25, the kernel also places a floor of 32 pages on this size limit, so that, even when RLIMIT_STACK is set very low, applications are guaranteed to have at least as much argument and environment space as was provided by Linux 2.6.22 and earlier. (this guarantee was not provided in Linux 2.6.23 and 2.6.24.) additionally, the limit per string is 32 pages (the kernel constant MAX_ARG_STRLEN), and the maximum number of strings is 0x7FFFFFFF.
โญ RETURN VALUE โญ
on success, execve() does not return. on error, -1 is returned, and errno is set to indicate the error.
๐ ERRORS ๐
okay bestie, here's where it gets real โ every single way execve() can justโฆ not. and there are so many. buckle up ๐
E2BIGโ too many bytes, periodt. the combined size of the environment (envp) and the argument list (argv) is too big, or one single argument or environment string is too long, or the whole path of the executable is too long. and yes, the terminating null byte counts toward the string length. every byte counts, no cap.EACCESโ search permission is denied on a component of the path prefix of path, or on the name of a script interpreter. (go peek atpath_resolution(7)if you wanna know more about how that walking-the-path thing works.)EACCESโ the file, or a script interpreter, is not a regular file. like, it's not that kind of file. sorry bestie.EACCESโ execute permission is denied for the file, or for a script interpreter, or for an ELF interpreter.EACCESโ the filesystem is mountednoexec. that's a whole vibe-killer right there.EAGAINโ (since Linux 3.1) so the caller changed its real UID using one of theset*uid()calls and was โ and still is โ above itsRLIMIT_NPROCresource limit (that'ssetrlimit(2), go look). want the full lore on this one? it's all down in NOTES, bestie.EFAULTโ path, or one of the pointers inside theargvorenvpvectors, points outside your accessible address space. rude of it.EINVALโ an ELF executable had more than onePT_INTERPsegment. i.e., it tried to name more than one interpreter. greedy much?EIOโ an I/O error happened. ๐EISDIRโ an ELF interpreter was a directory. that's not a program bestie that's a folder.ELIBBADโ an ELF interpreter was not in a recognized format.ELOOPโ too many symbolic links were hit while resolving path, or the name of a script or ELF interpreter.ELOOPโ the maximum recursion limit was reached during recursive script interpretation (see "Interpreter scripts", up above ๐). before Linux 3.8, the error for this exact case wasENOEXEC. glow-up.EMFILEโ the per-process limit on the number of open file descriptors has been reached. too many tabs open, metaphorically.ENAMETOOLONGโ path is too long.ENFILEโ the system-wide limit on the total number of open files has been reached. not just you โ everyone.ENOENTโ path, or a script or ELF interpreter, does not exist. it's giving ghosted. ๐ปENOEXECโ an executable is not in a recognized format, or it's for the wrong architecture, or it has some other format error meaning it simply cannot be executed.ENOMEMโ not enough kernel memory was available.ENOTDIRโ a component of the path prefix of path, or of a script or ELF interpreter, is not a directory.EPERMโ the filesystem is mountednosuid, the user is not the superuser, and the file has the set-user-ID or set-group-ID bit set.EPERMโ the process is being traced, the user is not the superuser, and the file has the set-user-ID or set-group-ID bit set.EPERMโ a "capability-dumb" application would not get the full set of permitted capabilities granted by the executable file. seecapabilities(7)for the deets.ETXTBSYโ the specified executable was open for writing by one or more processes. somebody's editing it live, iconic behavior, but no.
๐ VERSIONS ๐
POSIX does not document the #! behavior at all, bestie, but it does exist โ with some variations โ on other UNIX systems. so it's a vibe, just not an officially standardized one.
On Linux, argv and envp can be specified as NULL. and in both cases that has the same effect as specifying the argument as a pointer to a list containing a single null pointer. but listen:
Do NOT take advantage of this nonstandard and nonportable misfeature! ๐ On many other UNIX systems, passingargvasNULLwill just straight-up error out withEFAULT. some other UNIX systems do treat theenvp == NULLcase the same way Linux does. so likeโฆ don't build your whole personality around it.
POSIX.1 says values returned by sysconf(3) should be invariant over the lifetime of a process. however โ plot twist โ since Linux 2.6.23, if the RLIMIT_STACK resource limit changes, then the value reported by _SC_ARG_MAX changes too, reflecting the fact that the limit on space for holding command-line arguments and environment variables has changed. she's dynamic now. ๐
๐ง Interpreter scripts
The kernel imposes a max length on the text that follows the #! characters at the start of a script; anything past the limit gets ignored, sorry. before Linux 5.1, the limit is 127 characters. since Linux 5.1, the limit is 255 characters. glow-up era. โจ
The semantics of the optional-arg argument of an interpreter script vary across implementations. on Linux, the entire string following the interpreter name gets passed as a single argument to the interpreter, and this string can include white space. however, behavior differs on some other systems:
- some systems use the first white space to terminate optional-arg.
- on some systems, an interpreter script can have multiple arguments, and white spaces in optional-arg are used to delimit those arguments.
Linux (like most other modern UNIX systems) ignores the set-user-ID and set-group-ID bits on scripts. yes, on scripts specifically. iykyk.
๐ STANDARDS ๐
POSIX.1-2024. slay, certified.
๐ฆ HISTORY ๐ฆ
4.3BSD, SVr4, POSIX.1-1988. the ancestral lore fr fr.
With UNIX V6, the argument list of an exec() call was ended by 0, while the argument list of main was ended by -1. so that argument list was not directly usable in a further exec() call. messy. since UNIX V7, both are NULL. we fixed it. growth. ๐ฑ
๐ NOTES ๐
One sometimes sees execve() (and the related functions in exec(3)) described as "executing a new process" (or similar). this is a highly misleading description. there is no new process. many attributes of the calling process remain unchanged โ in particular, its PID. all execve() actually does is arrange for an existing process (the calling process) to execute a new program. same process, new fit. ๐
Set-user-ID and set-group-ID processes can not be ptrace(2)d. periodt.
The result of mounting a filesystem nosuid varies across Linux kernel versions:
- some will refuse execution of set-user-ID and set-group-ID executables when this would give the user powers they did not have already โ and return
EPERM. - some will just ignore the set-user-ID and set-group-ID bits and
exec()successfully.
either way, we move.
In most cases where execve() fails, control returns to the original executable image, and the caller of execve() can then handle the error. but โ in rare cases, typically caused by resource exhaustion โ failure may occur past the point of no return: the original executable image has been torn down, but the new image could not be completely built. in those cases, the kernel kills the process with a SIGSEGV signal (SIGKILL until Linux 3.17). yeah. no coming back from that one bestie. ๐ญ
๐ญ execve() and EAGAIN ๐ญ
A more detailed explanation of the EAGAIN error that can occur (since Linux 3.1) when calling execve() goes a little like this:
The EAGAIN error can occur when a preceding call to setuid(2), setreuid(2), or setresuid(2) caused the real user ID of the process to change, and that change caused the process to exceed its RLIMIT_NPROC resource limit โ i.e., the number of processes belonging to the new real UID exceeds the resource limit. from Linux 2.6.0 to Linux 3.0, this caused the set*uid() call to fail. (and before Linux 2.6, the resource limit was not imposed on processes that changed their user IDs. different times.)
Since Linux 3.1, the scenario just described no longer causes the set*uid() call to fail โ because it too often led to security holes where buggy applications didn't check the return status and assumed that, if the caller had root privileges, the call would always succeed. instead, the set*uid() calls now successfully change the real UID, but the kernel sets an internal flag named PF_NPROC_EXCEEDED to note that the RLIMIT_NPROC resource limit has been exceeded. if the PF_NPROC_EXCEEDED flag is set and the resource limit is still exceeded at the time of a subsequent execve() call, that call fails with the error EAGAIN. this kernel logic ensures that the RLIMIT_NPROC resource limit is still enforced for the common privileged daemon workflow โ namely, fork(2) + set*uid() + execve().
If the resource limit was not still exceeded at the time of the execve() call (because other processes belonging to this real UID terminated between the set*uid() call and the execve() call), then the execve() call succeeds and the kernel clears the PF_NPROC_EXCEEDED process flag. the flag is also cleared if a subsequent call to fork(2) by this process succeeds. we love a clean slate. ๐ซถ
๐ EXAMPLES ๐
The following program is designed to be execed by the second program below. it just echoes its command-line arguments, one per line. simple. elegant. iconic.
/* myecho.c */
#include <stdio.h>
#include <stdlib.h>
int
main(int argc, char *argv[])
{
for (size_t j = 0; j < argc; j++)
printf("argv[%zu]: %s\n", j, argv[j]);
exit(EXIT_SUCCESS);
}
This program can be used to exec the program named in its command-line argument:
/* execve.c */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
static char *newargv[] = { NULL, "hello", "world", NULL };
static char *newenviron[] = { NULL };
if (argc != 2) {
fprintf(stderr, "Usage: %s <file-to-exec>\n", argv[0]);
exit(EXIT_FAILURE);
}
newargv[0] = argv[1];
execve(argv[1], newargv, newenviron);
perror("execve"); /* execve() returns only on error */
exit(EXIT_FAILURE);
}
We can use the second program to exec the first as follows:
$ cc myecho.c -o myecho
$ cc execve.c -o execve
$ ./execve ./myecho
argv[0]: ./myecho
argv[1]: hello
argv[2]: world
We can also use these programs to demonstrate the use of a script interpreter. to do this we create a script whose "interpreter" is our myecho program:
$ cat > script
#!./myecho script-arg
^D
$ chmod +x script
We can then use our program to exec the script:
$ ./execve ./script
argv[0]: ./myecho
argv[1]: script-arg
argv[2]: ./script
argv[3]: hello
argv[4]: world
see how hello and world slid all the way down? that's the interpreter + optional-arg + script + real args stack stacking up. chef's kiss. ๐ฅ
๐ธ SEE ALSO ๐ธ
chmod(2), execveat(2), fork(2), get_robust_list(2), ptrace(2), exec(3), fexecve(3), getauxval(3), getopt(3), system(3), capabilities(7), credentials(7), environ(7), path_resolution(7), ld.so(8)
and that's the whole page bestie!! ๐ this lil masterpiece is from Linux man-pages 6.20, dated 2026-02-08, and the page is of course execve(2). go forth and execve() responsibly. mwah. ๐
โจ๐
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1992, Drew Eckhardt <drew@cs.colorado.edu>
- Copyright 2006, Michael Kerrisk <tk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms