mirror of
https://github.com/rakshasa/rtorrent.git
synced 2026-10-06 06:09:22 +00:00
Add support for posix_spawn_file_actions_addclosefrom_np by @fffe
This commit is contained in:
+16
-9
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user