Add support for posix_spawn_file_actions_addclosefrom_np by @fffe

This commit is contained in:
fffe
2026-07-03 04:52:26 -07:00
committed by GitHub
parent d99cd5eb73
commit 9884c9dd9b
3 changed files with 35 additions and 9 deletions
+16 -9
View File
@@ -43,6 +43,22 @@ ExecFile::execute(const char* file, char* const* argv, int flags) {
if (posix_spawn_file_actions_init(&actions) != 0)
throw torrent::internal_error("ExecFile::execute(...) posix_spawn_file_actions_init failed.");
posix_spawnattr_t attr;
posix_spawnattr_init(&attr);
short spawn_flags = 0;
// Try to avoid leaking open fds to the spawned process. Prefer POSIX_SPAWN_CLOEXEC_DEFAULT
// (macOS-only) or posix_spawn_file_actions_addclosefrom_np (glibc >= 2.34, FreeBSD >= 13.1).
//
// Other platforms like musl libc, OpenBSD and NetBSD must rely on explicit O_CLOEXEC.
#if defined(POSIX_SPAWN_CLOEXEC_DEFAULT)
spawn_flags |= POSIX_SPAWN_CLOEXEC_DEFAULT;
#elif defined(HAVE_POSIX_SPAWN_FILE_ACTIONS_ADDCLOSEFROM_NP)
posix_spawn_file_actions_addclosefrom_np(&actions, 3);
#endif
// Handle standard input redirection (/dev/null), posix_spawn_file_actions_addopen handles opening
// and dup2 natively
if (posix_spawn_file_actions_addopen(&actions, 0, "/dev/null", O_RDWR, 0) != 0) {
@@ -76,15 +92,6 @@ ExecFile::execute(const char* file, char* const* argv, int flags) {
posix_spawn_file_actions_addopen(&actions, 2, "/dev/null", O_RDWR, 0);
}
posix_spawnattr_t attr;
posix_spawnattr_init(&attr);
short spawn_flags = 0;
#ifdef POSIX_SPAWN_CLOEXEC_DEFAULT
spawn_flags |= POSIX_SPAWN_CLOEXEC_DEFAULT;
#endif
if (flags & flag_background) {
#ifdef POSIX_SPAWN_SETSID
spawn_flags |= POSIX_SPAWN_SETSID;