EXECVE(2)

๐Ÿ“ฆ
version 15.1 ยท 2 versions
, in freebsd 15.1

๐Ÿ’ซ EXECVE(2) ๐Ÿ’ซ

okay so bestie, buckle up โ€” this is the execve / fexecve page from the System Calls Manual, and it's genuinely one of the most iconic syscalls out there. it literally replaces your whole self. main character energy fr fr ๐ŸŽ€

โœจ NAME โœจ

execve, fexecve โ€” execute a file ๐Ÿฆ‹

๐ŸŒธ LIBRARY ๐ŸŒธ

Standard C Library (libc, -lc) ๐Ÿ’–

โญ SYNOPSIS โญ

#include <unistd.h>

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

int
fexecve(int fd, char *const argv[], char *const envp[]);

๐Ÿ’… DESCRIPTION ๐Ÿ’…

okay so the execve() system call transforms the calling process into a new process. like, full glow-up, no going back. the new process is constructed from an ordinary file, whose name is pointed to by path, and we call it the new process file. ๐Ÿซถ

the fexecve() system call is equivalent to execve() except that the file to be executed is determined by the file descriptor fd instead of a path. iykyk.

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 elf(5) and a.out(5). ๐Ÿ’€ (yes bestie, a.out is still in here, vintage icon behaviour)

an interpreter file begins with a line of the form:

#! interpreter [arg]

when an interpreter file is execve'd, the system actually execve's the specified interpreter. if the optional arg is specified, it becomes the first argument to the interpreter, and the name of the originally execve'd file becomes the second argument; otherwise, the name of the originally execve'd file becomes the first argument. the original arguments are shifted over to become the subsequent arguments. the zeroth argument is set to the specified interpreter. ๐Ÿงš


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)). ๐Ÿ‘‘

๐Ÿ”ฅ file descriptors ๐Ÿ”ฅ

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) and fcntl(2)). descriptors that remain open are unaffected by execve(), except those with the close-on-fork flag FD_CLOFORK which is cleared from all file descriptors. ๐Ÿซง

ok so this next bit is important: if any of the standard descriptors (0, 1, and/or 2) are closed at the time execve() is called, and the process will gain privilege as a result of set-id semantics, those descriptors will be re-opened automatically. no programs, whether privileged or not, should assume that these descriptors will remain closed across a call to execve(). like, don't build your whole personality on that assumption, it will not hold. ๐Ÿ˜ญ

๐Ÿฆ‹ signals ๐Ÿฆ‹

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) for more information). ๐Ÿ’œ

๐Ÿ‘‘ set-user-ID / set-group-ID tea ๐Ÿ‘‘

if the set-user-ID mode bit of the new process image file is set (see chmod(2)), 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)). ๐ŸŽ€

the set-ID bits are not honored if the respective file system has the nosuid option enabled, or if the new process file is an interpreter file. syscall tracing is disabled if effective IDs are changed. no cap, that's just how it is.

๐ŸŒŸ inherited attributes ๐ŸŒŸ

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

๐Ÿ“ how the program gets entered ๐Ÿ“

when a program is executed as a result of an execve() system call, it is entered 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 points to the array of character pointers to the arguments themselves. periodt. ๐Ÿ’…

๐Ÿ’– fexecve() specifics ๐Ÿ’–

the fexecve() ignores the file offset of fd. since execute permission is checked by fexecve(), the file descriptor fd need not have been opened with the O_EXEC flag. however, if the file to be executed denies read permission for the process preparing to do the exec, the only way to provide the fd to fexecve() is to use the O_EXEC flag when opening fd. note that the file to be executed can not be open for writing. ๐Ÿ™ˆ

๐ŸŒˆ RETURN VALUES ๐ŸŒˆ

as the execve() system call overlays the current process image with a new process image, the successful call has no process to return to. like, you're gone, you're somebody new now, slay. โœจ

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 ๐Ÿ”ฅ

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

  • [ENOTDIR] โ€” a component of the path prefix is not a directory.
  • [ENAMETOOLONG] โ€” a component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters.
  • [ENOEXEC] โ€” when invoking an interpreted script, the length of the first line, inclusive of the #! prefix and terminating newline, exceeds MAXSHELLCMDLEN characters.
  • [ENOENT] โ€” the new process file does not exist.
  • [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.
  • [EINVAL] โ€” argv did not contain at least one element.
  • [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 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 is larger than the system-imposed limit. this limit is specified by the sysctl(3) MIB variable KERN_ARGMAX.
  • [EFAULT] โ€” the new process file is not as long as indicated by the size values in its header.
  • [EFAULT] โ€” the path, argv, or envp arguments point to an illegal address.
  • [EIO] โ€” an I/O error occurred while reading from the file system.
  • [EINTEGRITY] โ€” corrupted data was detected while reading from the file system.

in addition, the fexecve() will fail and return to the calling process if:

  • [EBADF] โ€” the fd argument is not a valid file descriptor open for executing.

๐Ÿฆ‹ SEE ALSO ๐Ÿฆ‹

ktrace(1), _exit(2), fork(2), open(2), execl(3), exit(3), sysctl(3), fdescfs(4), a.out(5), elf(5), environ(7), mount(8)

๐Ÿ‘‘ STANDARDS ๐Ÿ‘‘

the execve() system call conforms to IEEE Std 1003.1-2001 ("POSIX.1"), with the exception of reopening descriptors 0, 1, and/or 2 in certain circumstances. a future update of the Standard is expected to require this behavior, and it may become the default for non-privileged processes as well. the support for executing interpreted programs is an extension. the fexecve() system call conforms to The Open Group Extended API Set 2 specification. ๐Ÿ’œ

๐Ÿงš HISTORY ๐Ÿงš

the execve() system call appeared in Version 7 AT&T UNIX. the fexecve() system call appeared in FreeBSD 8.0. vintage lineage bestie, we love a documented origin story. ๐ŸŽ€

๐Ÿ’€ 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. not me warning you about this, but like, be aware. ๐Ÿ™ˆ
  • when executing an interpreted program through fexecve(), kernel supplies /dev/fd/n as a second argument to the interpreter, where n is the file descriptor passed in the fd argument to fexecve(). for this construction to work correctly, the fdescfs(4) filesystem shall be mounted on /dev/fd. important!! ๐Ÿซถ

and that's the whole page, bestie ๐ŸŒธ โ€” this one's from Debian, dated July 02, 2025. go forth and exec, but like, responsibly ๐Ÿ’–โœจ๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from
  • freebsd 15.1: base 15.1
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms