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

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 the O_PATH flag, 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 โ€” fd is not a valid file descriptor, or argv is NULL, or envp is NULL. one of those three is being messy.
  • ENOENT โ€” The close-on-exec flag is set on fd, and fd refers to a script. See BUGS. (spoiler: it's giving tragic. ๐Ÿ™ˆ)
  • ENOSYS โ€” The kernel does not provide the execveat(2) system call, and the /proc filesystem 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 to fexecve(). 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:

  1. 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 ๐Ÿงพ
  2. 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms