---
name: "execve"
section: "2"
platform: "macos"
package: "xnu"
version: "12377.121.6"
release: "macOS 26.5"
platforms: ["linux", "freebsd", "macos"]
url: "https://new.carta.tech/execve"
---

# execve(2)

execute a file

## NAME

**execve** – execute a file

## SYNOPSIS

**#include \<unistd.h\>**

*int*\

**execve**(*const char \*path*, *char \*const argv\[\]*, *char \*const envp\[\]*);

## DESCRIPTION

**execve**() transforms the calling process into a new process. The new process is constructed from an ordinary file, whose name is pointed to by *path*, 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)](/a.out/5.md).

An interpreter file begins with a line of the form:

**#!** *interpreter* \[*arg* *...*\]

When an interpreter file is **execve**(*Ap*, *d*), the system runs the specified *interpreter*. If any optional *args* are specified, they become the first (second, ...) argument to the *interpreter.* The name of the originally **execve**(*Ap*, *d*) file becomes the subsequent argument; otherwise, the name of the originally **execve**(*Ap*, *d*) file is the first argument. The original arguments to the invocation of the interpreter are shifted over to become the final arguments. The zeroth argument, normally the name of the **execve**(*Ap*, *d*) file, is left unchanged.

The argument *argv* is a pointer to a null-terminated array of character pointers to null-terminated character strings. These strings construct the argument list to be made available to the new process.  At least one argument must be present in the array; by custom, the first element should be the name of the executed program (for example, the last component of *path*).

The argument *envp* is also a pointer to a null-terminated array of character pointers to null-terminated strings. A pointer to this array is normally stored in the global variable *environ.* These strings pass information to the new process that is not directly an argument to the command (see [environ(7)](/environ/7.md)).

File descriptors open in the calling process image remain open in the new process image, except for those for which the close-on-exec flag is set (see [close(2)](/close/2.md) and [fcntl(2)](/fcntl/2.md)). Descriptors that remain open are unaffected by **execve**().

Signals set to be ignored in the calling process are set to be ignored in the new process. Signals which are set to be caught in the calling process image are set to default action in the new process image. Blocked signals remain blocked regardless of changes to the signal action. The signal stack is reset to be undefined (see [sigaction(2)](/sigaction/2.md) for more information).

If the set-user-ID mode bit of the new process image file is set (see [chmod(2)](/chmod/2.md)), the effective user ID of the new process image is set to the owner ID of the new process image file. If the set-group-ID mode bit of the new process image file is set, the effective group ID of the new process image is set to the group ID of the new process image file. (The effective group ID is the first element of the group list.) The real user ID, real group ID and other group IDs of the new process image remain the same as the calling process image. After any set-user-ID and set-group-ID processing, the effective user ID is recorded as the saved set-user-ID, and the effective group ID is recorded as the saved set-group-ID. These values may be used in changing the effective IDs later (see [setuid(2)](/setuid/2.md)).

The new process also inherits the following attributes from the calling process:

| | |
| --- | --- |
| process ID | see [getpid(2)](/getpid/2.md) |
| parent process ID | see [getppid(2)](/getppid/2.md) |
| process group ID | see [getpgrp(2)](/getpgrp/2.md) |
| access groups | see [getgroups(2)](/getgroups/2.md) |
| working directory | see [chdir(2)](/chdir/2.md) |
| root directory | see [chroot(2)](/chroot/2.md) |
| control terminal | see [termios(4)](/termios/4.md) |
| resource usages | see [getrusage(2)](/getrusage/2.md) |
| interval timers | see [getitimer(2)](/getitimer/2.md) |
| resource limits | see [getrlimit(2)](/getrlimit/2.md) |
| file mode mask | see [umask(2)](/umask/2.md) |
| signal mask | see [sigaction(2)](/sigaction/2.md), |

When a program is executed as a result of an **execve**() call, it is entered as follows:

```
int
main(int argc, char **argv, char **envp);
```

where *argc* is the number of elements in *argv* (the \`\`arg count'') and *argv* points to the array of character pointers to the arguments themselves.

## RETURN VALUES

As the **execve**() function overlays the current process image  with a new process image, the successful call has no process to return to. If **execve**() does return to the calling process, an error has occurred; the return value will be -1 and the global variable *errno* is set to indicate the error.

## ERRORS

**execve**() will fail and return to the calling process if:

- \[E2BIG\]

  The number of bytes in the new process's argument list is larger than the system-imposed limit. This limit is specified by the [sysctl(3)](/sysctl/3.md) MIB variable KERN\_ARGMAX.

- \[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.

- \[EACCES\]

  The new process file is on a filesystem mounted with execution disabled (MNT\_NOEXEC in ⟨*sys/mount.h*⟩).

- \[EFAULT\]

  The new process file is not as long as indicated by the size values in its header.

- \[EFAULT\]

  *Path*, *argv*, or *envp* point to an illegal address.

- \[EIO\]

  An I/O error occurred while reading from the file system.

- \[ELOOP\]

  Too many symbolic links were encountered in translating the pathname. This is taken to be indicative of a looping symbolic link.

- \[ENAMETOOLONG\]

  A component of a pathname exceeded  {NAME\_MAX} characters, or an entire path name exceeded  {PATH\_MAX} characters.

- \[ENOENT\]

  The new process file does not exist.

- \[ENOEXEC\]

  The new process file has the appropriate access permission, but has an unrecognized format (e.g., an invalid magic number in its header).

- \[ENOMEM\]

  The new process requires more virtual memory than is allowed by the imposed maximum ([getrlimit(2)](/getrlimit/2.md)).

- \[ENOTDIR\]

  A component of the path prefix is not a directory.

- \[ETXTBSY\]

  The new process file is a pure procedure (shared text) file that is currently open for writing or reading by some process.

## CAVEAT

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.

## SEE ALSO

[exit(2)](/exit/2.md), [fork(2)](/fork/2.md), [execl(3)](/execl/3.md), [sysctl(3)](/sysctl/3.md), [environ(7)](/environ/7.md)

## HISTORY

The **execve**() function call appeared in 4.2BSD.
