In 24c5e57fddec43c3e46fff36f93fe5f9f35d05bd util: Clear FD_CLOEXEC on child socket before exec: to not make things more non-fork-safe:
diff --git a/src/mp/util.cpp b/src/mp/util.cpp
index 15400215e4..b2a545ef5f 100644
--- a/src/mp/util.cpp
+++ b/src/mp/util.cpp
@@ -62,4 +62,12 @@ size_t MaxFd()
}
+template <size_t N>
+[[noreturn]] void ChildProcessError(const char (&msg)[N])
+{
+ const ssize_t writeResult = ::write(STDERR_FILENO, msg, N - 1);
+ (void)writeResult;
+ _exit(126);
+}
+
} // namespace
@@ -144,8 +152,5 @@ std::tuple<ProcessId, SocketId> SpawnProcess(ConnectInfoToArgsFn&& connect_info_
throw std::system_error(errno, std::system_category(), "close");
}
- static constexpr char msg[] = "SpawnProcess(child): close(fds[1]) failed\n";
- const ssize_t writeResult = ::write(STDERR_FILENO, msg, sizeof(msg) - 1);
- (void)writeResult;
- _exit(126);
+ ChildProcessError("SpawnProcess(child): close(fds[1]) failed\n");
}
@@ -164,8 +169,12 @@ std::tuple<ProcessId, SocketId> SpawnProcess(ConnectInfoToArgsFn&& connect_info_
// the socket was created.
int flags = fcntl(fds[0], F_GETFD);
- if (flags == -1) throw std::system_error(errno, std::system_category(), "fcntl F_GETFD");
+ if (flags == -1) {
+ ChildProcessError("SpawnProcess(child): fcntl(fds[0], F_GETFD) failed\n");
+ }
if (flags & FD_CLOEXEC) {
flags &= ~FD_CLOEXEC;
- if (fcntl(fds[0], F_SETFD, flags) == -1) throw std::system_error(errno, std::system_category(), "fcntl F_SETFD");
+ if (fcntl(fds[0], F_SETFD, flags) == -1) {
+ ChildProcessError("SpawnProcess(child): fcntl(fds[0], F_SETFD) failed\n");
+ }
}