Exec(3): execute file – Linux man page

Name

execl, execlp, execle,

execv, execvp, execvpe – run a

file

Synopsis

#include <unistd.h>

extern char **environ;

int execl(const char *path, const char *arg, …); int execlp(const char *file, const char *arg, …

); int execle(const char *path, const char *arg, … , char * const envp[]); int execv(const char *path, char *const argv[]); int execvp(const char *file, char *const argv[]); int execvpe(const char *file, char *const argv[], char *const envp[]);

Feature test macro requirements for glibc (see feature_test_macros(7

)): execvpe():

_GNU_SOURCE Description

The exec() family of functions replaces the current process image with a new process image. The functions described on this page of the manual are front-ends for execve(2). (See the manual page for execve(2) for details on replacing the current process image.)

The initial argument for these functions is the name of a file to run

.

The const char *arg and subsequent ellipses in the execl(), execlp(), and execle() functions can be thought of as arg0, arg1, …, argn. Together they describe a list of one or more pointers to null-terminated strings that represent the list of arguments available to the executed program. The first argument, by convention, should point to the file name associated with the file being executed. The argument list must end with a NULL pointer, and because these are variadic functions, this pointer must be NULL (char *).

The execv(), execvp(), and execvpe() functions provide an array of pointers to null-terminated strings that represent the list of arguments available to the new program. The first argument, by convention, should point to the file name associated with the file being executed. The array of pointers must end with a NULL pointer.

The execle() and execvpe() functions allow the caller to specify the environment of the executed program through the envp argument. The envp argument is an array of pointers to null-terminated strings and must end with a null pointer. The other functions take the environment for the new process image from the external variable environment in the calling process.

Special semantics for

execlp() and execvp() The execlp(), execvp(), and execvpe() functions duplicate shell actions when searching for an executable file if the specified file name does not contain a forward slash (/) character. The file is searched in the two-point list of directory access addresses specified in the PATH environment variable. If this variable is not defined, the path list defaults to the current directory followed by the directory list returned by confstr(_CS_PATH). (This confstr(3) call usually returns the value “/bin:/usr/bin”.) If the specified file name

includes a slash character, PATH is ignored and

the file with the specified path name is executed.

In addition, certain errors are specially handled

.

If permission is denied for a file (the attempt to execve(2) failed with the EACCES error), these functions will continue to look for the rest of the search path. However, if no other files are found, they will return with errno set to EACCES.

If a file’s header is not recognized (the execve(2) attempt failed with the ENOEXEC error), these functions will execute the shell (/bin/sh) with the file path as the first argument. (If this attempt fails, no additional search is performed.)

Return value

exec() functions return only if an error has occurred. The return value is -1 and errno is set to indicate the error.

Errors

All of these functions can fail and set errno for any of the errors specified for execve(2). Versions The execvpe() function first appeared in glibc 2.11. Pursuant to POSIX.1-2001, POSIX.1-2008.

The execvpe

()

function is an extension of GNU.

Notes

On some other systems, the default path (used when the environment does not contain the PATH variable) ) has the current working directory listed after /bin and /usr/bin, as an anti-Trojan measure. Linux here uses the traditional default path “current directory first”.

The behavior of execlp() and execvp() when errors occur when attempting to execute the file is a historical practice, but has not traditionally been documented and is not specified by the POSIX standard. BSD (and possibly other systems) do an automatic suspend and try again if ETXTBSY is found. Linux treats it as a hard mistake and returns immediately.

Traditionally, the execlp() and execvp() functions ignored all errors except those described above and ENOMEM and E2BIG, over which they returned. They are now returned if any errors other than those described above occur.

See also sh

(1), execve(2), fork(2),

ptrace(2), fexecve(3), environ(7) Referenced by atexit(3), collectd-exec(5), environ(5), getpid(2), glob(3), ibv_fork_init(3), libssh2_sftp_statvfs(3), on_exit(3), statvfs(2), stdin(3), sysconf(3), system(3), vfork(2), x11vnc(1)

Contact US