fexecve(3)
โจ fexecve(3) โจ โ execute program specified via file descriptor ๐
okay so bestie, you know execve(2)? yeah. this is that but the โจcoolโจ version where you point at an fd instead of typing out a whole pathname. it lives in the Standard C library (libc, link it with -lc). she's been around a minute, but she ate, no cap. ๐ซถ
๐ธ SYNOPSIS ๐ธ
#include <unistd.h>
int fexecve(int fd, char *const argv[], char *const envp[]);
๐ Feature Test Macro Requirements for glibc (see feature_test_macros(7)): ๐
fexecve():- Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L
- Before glibc 2.10:
_GNU_SOURCE
- Since glibc 2.10:
like pls define these or the compiler is gonna be like "who?" ๐ญ
๐ DESCRIPTION ๐
So fexecve() does literally the exact same task as execve(2) โ same energy, same vibe โ except the file you're executing is specified via a file descriptor, fd, rather than via a pathname. that's the whole plot twist right there.
And the rules for that fd? bestie, listen closely:
- it must be opened read-only (
O_RDONLY) or with theO_PATHflag, and - the caller must have permission to execute the file that it refers to.
no shortcuts, no vibes-based access control. ๐ซ
๐ฅ RETURN VALUE ๐ฅ
A successful call to fexecve() never returns. like, literally never. she's gone. the function is the program now. main character energy, periodt. ๐
On error, the function does return, with a result value of -1, and errno is set to indicate the error. so at least she tells you why she's not coming back. ๐
โจ ERRORS โจ
Errors are as for execve(2), with the following additions โ the bonus round, if you will:
EINVALโfdis not a valid file descriptor, orargvisNULL, orenvpisNULL. one of those three is being messy.ENOENTโ The close-on-exec flag is set onfd, andfdrefers to a script. See BUGS. (spoiler: it's giving tragic. ๐)ENOSYSโ The kernel does not provide theexecveat(2)system call, and the/procfilesystem could not be accessed.
๐ ATTRIBUTES ๐
For an explanation of the terms used in this section, see attributes(7).
- Interface:
fexecve()- Attribute: Thread safety
- Value:
MT-Safe
she's MT-Safe babes, she can hang in threads. ๐
โญ STANDARDS โญ
POSIX.1-2008.
official, certified, standardized. slay. ๐
๐ฆ HISTORY ๐ฆ
glibc 2.3.2. she's an elder icon, honestly.
On Linux with glibc versions 2.26 and earlier, fexecve() is implemented using the proc(5) filesystem, so /proc needs to be mounted and available at the time of the call. (mount your procs, bestie.)
Since glibc 2.27, if the underlying kernel supports the execveat(2) system call, then fexecve() is implemented using that system call โ with the benefit that /proc does not need to be mounted. glow-up fr fr. โจ
๐ NOTES ๐
The idea behind fexecve() is to allow the caller to verify (checksum) the contents of an executable before executing it. like, check the ingredients before you eat the snack, yk?
Simply opening the file, checksumming the contents, and then doing an execve(2) would not suffice โ because between the two steps, the filename, or a directory prefix of the pathname, could have been exchanged (by, for example, modifying the target of a symbolic link). sneaky sneaky. ๐
real talk though:fexecve()does not mitigate the problem that the contents of a file could be changed between the checksumming and the call tofexecve(). for that, the solution is to ensure that the permissions on the file prevent it from being modified by malicious users. lock the file down, bestie, that's the only fix.
The natural idiom when using fexecve() is to set the close-on-exec flag on fd, so that the file descriptor does not leak through to the program that is executed. This approach is natural for two reasons:
- First, it prevents file descriptors being consumed unnecessarily. (The executed program normally has no need of a file descriptor that refers to the program itself.) like girl, why are you holding your own receipt ๐งพ
- Second, if
fexecve()is used recursively, employing the close-on-exec flag prevents the file descriptor exhaustion that would result from the fact that each step in the recursion would cause one more file descriptor to be passed to the new program. recursion + leaking fds = doom spiral. ๐
(But see BUGS.) ๐
๐ญ BUGS ๐ญ
If fd refers to a script (i.e., it is an executable text file that names a script interpreter with a first line that begins with the characters #!) and the close-on-exec flag has been set for fd, then fexecve() fails with the error ENOENT.
This error occurs because, by the time the script interpreter is executed, fd has already been closed because of the close-on-exec flag. yeah. she closed the door before the guest walked in. ๐ช
Thus, the close-on-exec flag can't be set on fd if it refers to a script โ leading to the problems described in NOTES. it's giving catch-22, ngl. ๐
๐ SEE ALSO ๐
okay that's the whole page bestie, you're so welcome ๐ซถ๐ this was written from Linux man-pages 6.20, dated 2026-02-08, and yeah she's filed under fexecve(3) in the Library Functions Manual. now go execute something responsibly, mwah ๐โจ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 2006-2014, Michael Kerrisk <mtk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms