C Reference ยท POSIX and Systems
C exec family: execl, execv, execvp and more
Reference for the exec family (execl, execlp, execv, execvp, execve): replace the running program with another, how the letters differ, and how to handle failure.
The exec functions replace the program running in the current process with a different executable. The process ID stays the same, but code, stack and heap are replaced, so on success the call never returns. Open file descriptors survive unless they are marked close-on-exec, which is how shells wire up redirections and pipelines.
Because a successful exec destroys the caller, programs usually combine it with fork: the child calls exec, the parent waits with waitpid. If exec returns at all, it failed, and errno says why.
#include <unistd.h> execlp("echo", "echo", "hello", (char *)NULL); /* only returns on error */
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.
Syntax
#include <unistd.h> int execl (const char *path, const char *arg0, ... /*, (char *)NULL */); int execlp(const char *file, const char *arg0, ... /*, (char *)NULL */); int execle(const char *path, const char *arg0, ... /*, (char *)NULL, char *const envp[] */); int execv (const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]); int execve(const char *path, char *const argv[], char *const envp[]);
| Name | Description |
|---|---|
path |
Path of the executable (absolute or relative to the working directory). |
file |
Program name; if it contains no slash, the directories in PATH are searched. |
arg0, ... |
The argument list as separate parameters, ending with a null pointer cast to char *. |
argv |
A null-terminated array of argument strings; argv[0] is the program name by convention. |
envp |
A null-terminated array of "NAME=value" strings for the new environment. |
Returns: nothing on success. -1 on failure, with errno set (ENOENT no such file, EACCES not executable or permission denied, ENOEXEC bad format, E2BIG arguments plus environment too large, ENOMEM).
Reading the suffix letters
| Letter | Meaning |
|---|---|
l |
Arguments are passed as a list of parameters |
v |
Arguments are passed as a vector (array) |
p |
The PATH is searched for the file name |
e |
An explicit environment array is supplied (otherwise the caller's is inherited) |
execve is the real system call; the others are library wrappers around it (execlp and execvp add the PATH search).
Usage notes
- Terminate the list with
(char *)NULL. A bareNULLcan be defined as plain0, which is anintand breaks the variadic call. In C23nullptralso works but the cast is portable everywhere. - Repeat the program name as
arg0. The program sees it asargv[0]; omitting it shifts every argument by one. - Flush stdio first. Unflushed buffered output is lost when the image is replaced, because no
atexithandlers or flushes run. - A failed
execreturns in the child: report the error and call_exit(notexit) so the child does not flush the parent's inherited buffers. - With
pvariants a name without a slash is searched alongPATH. Do not use them with untrusted names in privileged programs; use an absolute path. - Never pass user input to
system()or throughsh -cunless you quote it. Callingexecvwith a separate argument array avoids the shell and its injection problems. - Descriptors stay open across
execunless opened withO_CLOEXECor markedFD_CLOEXEC. Close or mark everything the new program should not inherit. - An
execof a script with a#!first line starts the named interpreter; a file with no such line may fail withENOEXEC(execlp/execvpretry throughshin that case). posix_spawncombines fork and exec and is cheaper in large processes.
Related
- fork, wait and waitpid, pipe
- system(), getenv() and unistd.h
- Tutorial: System calls, Command-line arguments