EXECVE(2) BSD EXECVE(2)
NAME
execve - execute a file
SYNOPSIS
execve(name, argv, envp)
const char *name, *argv[], *envp[];
DESCRIPTION
execve transforms the calling process into a new process. The new
process is constructed from an ordinary file called the "new process
file". This file is either an executable object file or a file of data
for an interpreter. An executable object file consists of an identifying
header followed by pages of data representing the initial program (text)
and initialized data pages. Additional pages may be specified by the
header to be initialized with zero data. See a.out(5).
An interpreter file begins with a line of the form "#! interpreter". When
an interpreter file is execved, the system execve's the specified
interpreter, giving it the name of the originally exec'd file as an
argument and shifting over the rest of the original arguments.
There can be no return from a successful execve because the calling image
is lost. This is the mechanism whereby different process images become
active.
The argument argv is a null terminated array of character pointers to
null terminated character strings. These strings constitute the argument
list to be made available to the new process. By convention, at least
one argument must be present in this array, and the first element of this
array should be the name of the executed program (that is, the last
component of name).
The argument envp is also a null terminated array of character pointers
to null terminated strings. These strings pass information to the new
process that is not directly an argument to the command (see environ(7)).
Descriptors open in the calling process remain open in the new process,
except for those for which the close-on-exec flag is set (see close(2)).
Descriptors that remain open are unaffected by execve.
Ignored signals remain ignored across an execve, but signals that are
caught are reset to their default values. Blocked signals remain blocked
regardless of changes to the signal action. The signal stack is reset to
be undefined (see sigvec(2) for more information).
Each process has "real" user and group IDs and an "effective" user and
group IDs. The real ID identifies the person using the system; the
effective ID determines his access privileges. execve changes the
effective user and group ID to the owner of the executed file if the file
has the "set-user-ID" or "set-group-ID" modes. The real user ID is not
affected.
The new process also inherits the following attributes from the calling
process:
process ID see getpid(2)
parent process ID see getppid(2)
process group ID see getpgrp(2)
access groups see getgroups(2)
working directory see chdir(2)
root directory see chroot(2)
control terminal see tty(4)
resource usages see getrusage(2)
interval timers see getitimer(2)
resource limits see getrlimit(2)
file mode mask see umask(2)
signal mask see sigvec(2), sigmask(2)
When the executed program begins, it is called as follows:
main(argc, argv, envp)
int argc;
char **argv, **envp;
where argc is the number of elements in argv (the "arg count") and argv
is the array of character pointers to the arguments themselves.
envp is a pointer to an array of strings that constitute the
"environment" of the process. A pointer to this array is also stored in
the global variable "environ". Each string consists of a name, an equal
sign (=), and a null terminated value. The array of pointers is
terminated by a null pointer. The shell sh(1) passes an environment
entry for each global shell variable defined when the program is called.
See environ(7) for some conventionally used names.
The number of bytes available for the new process' combined argument and
environment lists is ARG_MAX (26624). There is a restriction on the
number of bytes of argument and environment that can be handled. This
limit has been increased from roughly 10240 bytes to 26620 bytes (10-KB
to 26-KB) at SR10.4. Attempts to pass more than this much will result in
an [E2BIG] errno.
This limit is defined in the header file <sys/param.h> as NCARGS and
<limits.h> as ARG_MAX.
The required space is computed from the specified (or implicit) arguments
and Environment as follows:
For each argument: the size of that argument + 6, rounded up to a
longword boundary. For example:
((strlen(argv[i]) + 6) + 3) & (~3)
For each Environment Variable: the size of that Environment Variable
+ 1 (for a terminating null) + 4 (for a pointer to it), rounded up
to a longword boundary. For example:
((strlen(environ[i]) + 5) + 3) & (~3)
4 for a terminating null in the Environment list.
The execve system call will no longer establish a SYSTYPE environment
variable if the binary to be executed is stamped such that it expects an
XPG3, AES or POSIX environment and there isn't such an environment
variable already. That is: if an application removes the SYSTYPE
environment variable and then uses an execve call to execute an XPG3,
AES, or POSIX binary, the environment variable will not re-defined.
ERRORS
execve will fail and return to the calling process if one or more of the
following are true:
[ENOTDIR] A component of the path prefix is not a directory.
[ENAMETOOLONG] A component of a pathname exceeded 255 characters, or an
entire pathname exceeded 1023 characters.
[ENOENT] The new process file does not exist or path points to an
empty string.
[ELOOP] Too many symbolic links were encountered in translating
the pathname.
[EACCES] Search permission is denied for a component of the path
prefix.
[EACCES] The new process file is not an ordinary file.
[EACCES] The new process file mode denies execute permission.
[ENOEXEC] The new process file has the appropriate access
permission, but has an invalid magic number in its
header.
[ETXTBSY] The new process file is a pure procedure (shared text)
file that is currently open for writing or reading by
some process.
[ENOMEM] The new process requires more virtual memory than is
allowed by the imposed maximum (getrlimit(2)).
[E2BIG] The number of bytes in the new process' argument list and
Environment list is larger than the system-imposed limit
of 26624 bytes. (NCARGS in <sys/param.h>.
[EIO] An I/O error occurred while reading from the file system.
SEE ALSO
exit(2), fork(2), execl(3), environ(7)
DIAGNOSTICS
A successful execve never returns. A failed call returns -1 and sets
errno.
NOTES
Some implementations also define the following error:
[EINVAL] The pathname contains a character with the high-order
bit set.
CAVEATS
If a program is setuid to a non-super-user, but is executed when the real
uid is "root," then the program has some of the powers of a super-user as
well.