aboutsummaryrefslogtreecommitdiff
path: root/src/common/platform/unix
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/platform/unix')
-rw-r--r--src/common/platform/unix/PosixSpawnSubProcess.cpp44
-rw-r--r--src/common/platform/unix/UnixPipe.cpp12
2 files changed, 40 insertions, 16 deletions
diff --git a/src/common/platform/unix/PosixSpawnSubProcess.cpp b/src/common/platform/unix/PosixSpawnSubProcess.cpp
index 8b521a5e..b5d68845 100644
--- a/src/common/platform/unix/PosixSpawnSubProcess.cpp
+++ b/src/common/platform/unix/PosixSpawnSubProcess.cpp
@@ -17,15 +17,15 @@ namespace cru::platform::unix {
PosixSpawnSubProcessImpl::PosixSpawnSubProcessImpl()
: pid_(0),
exit_code_(0),
- stdin_pipe_(UnixPipe::Usage::Send),
- stdout_pipe_(UnixPipe::Usage::Receive),
- stderr_pipe_(UnixPipe::Usage::Receive) {
+ stdin_pipe_(UnixPipe::Usage::Send, false),
+ stdout_pipe_(UnixPipe::Usage::Receive, false),
+ stderr_pipe_(UnixPipe::Usage::Receive, false) {
stdin_stream_ = std::make_unique<UnixFileStream>(
- stdin_pipe_.GetSelfFileDescriptor(), false, false, true, false);
+ stdin_pipe_.GetSelfFileDescriptor(), false, false, true, true);
stdout_stream_ = std::make_unique<UnixFileStream>(
- stdout_pipe_.GetSelfFileDescriptor(), false, true, false, false);
+ stdout_pipe_.GetSelfFileDescriptor(), false, true, false, true);
stderr_stream_ = std::make_unique<UnixFileStream>(
- stderr_pipe_.GetSelfFileDescriptor(), false, true, false, false);
+ stderr_pipe_.GetSelfFileDescriptor(), false, true, false, true);
stdout_buffer_stream_ =
std::make_unique<io::AutoReadStream>(stdout_stream_.get(), false);
@@ -95,15 +95,35 @@ void PosixSpawnSubProcessImpl::PlatformCreateProcess(
error = posix_spawn_file_actions_adddup2(
&file_actions, stderr_pipe_.GetOtherFileDescriptor(), STDERR_FILENO);
check_error(u"Failed to call posix_spawn_file_actions_adddup2 on stderr.");
+
+ error = posix_spawn_file_actions_addclose(
+ &file_actions, stdin_pipe_.GetOtherFileDescriptor());
+ check_error(
+ u"Failed to call posix_spawn_file_actions_addclose on self fd of stdin.");
+ error = posix_spawn_file_actions_addclose(
+ &file_actions, stdout_pipe_.GetOtherFileDescriptor());
+ check_error(
+ u"Failed to call posix_spawn_file_actions_addclose on self fd stdout.");
+ error = posix_spawn_file_actions_addclose(
+ &file_actions, stderr_pipe_.GetOtherFileDescriptor());
+ check_error(
+ u"Failed to call posix_spawn_file_actions_addclose on self fd stderr.");
+
error = posix_spawn_file_actions_addclose(
&file_actions, stdin_pipe_.GetSelfFileDescriptor());
- check_error(u"Failed to call posix_spawn_file_actions_addclose on stdin.");
+ check_error(
+ u"Failed to call posix_spawn_file_actions_addclose on parent fd of "
+ u"stdin.");
error = posix_spawn_file_actions_addclose(
&file_actions, stdout_pipe_.GetSelfFileDescriptor());
- check_error(u"Failed to call posix_spawn_file_actions_addclose on stdout.");
+ check_error(
+ u"Failed to call posix_spawn_file_actions_addclose on parent fd of "
+ u"stdout.");
error = posix_spawn_file_actions_addclose(
&file_actions, stderr_pipe_.GetSelfFileDescriptor());
- check_error(u"Failed to call posix_spawn_file_actions_addclose on stderr.");
+ check_error(
+ u"Failed to call posix_spawn_file_actions_addclose on parent fd of "
+ u"stderr.");
posix_spawnattr_t attr;
error = posix_spawnattr_init(&attr);
@@ -130,11 +150,11 @@ void PosixSpawnSubProcessImpl::PlatformCreateProcess(
check_error(u"Failed to call posix_spawnp.");
error = ::close(stdin_pipe_.GetOtherFileDescriptor());
- check_error(u"Failed to close stdin.");
+ check_error(u"Failed to close child stdin.");
error = ::close(stdout_pipe_.GetOtherFileDescriptor());
- check_error(u"Failed to close stdout.");
+ check_error(u"Failed to close child stdout.");
error = ::close(stderr_pipe_.GetOtherFileDescriptor());
- check_error(u"Failed to close stderr.");
+ check_error(u"Failed to close child stderr.");
}
SubProcessExitResult PosixSpawnSubProcessImpl::PlatformWaitForProcess() {
diff --git a/src/common/platform/unix/UnixPipe.cpp b/src/common/platform/unix/UnixPipe.cpp
index 4d081d5e..f30c599e 100644
--- a/src/common/platform/unix/UnixPipe.cpp
+++ b/src/common/platform/unix/UnixPipe.cpp
@@ -7,8 +7,8 @@
#include <unistd.h>
namespace cru::platform::unix {
-UnixPipe::UnixPipe(Usage usage, UnixPipeFlag flags)
- : usage_(usage), flags_(flags) {
+UnixPipe::UnixPipe(Usage usage, bool auto_close, UnixPipeFlag flags)
+ : usage_(usage), auto_close_(auto_close), flags_(flags) {
int fds[2];
if (pipe(fds) != 0) {
throw ErrnoException(u"Failed to create unix pipe.");
@@ -40,8 +40,12 @@ int UnixPipe::GetOtherFileDescriptor() {
}
UnixPipe::~UnixPipe() {
- if (close(GetSelfFileDescriptor()) != 0) {
- CRU_LOG_ERROR(u"Failed to close unix pipe file descriptor.");
+ if (auto_close_) {
+ auto self_fd = GetSelfFileDescriptor();
+ if (::close(self_fd) != 0) {
+ CRU_LOG_ERROR(u"Failed to close unix pipe file descriptor {}, errno {}.",
+ self_fd, errno);
+ }
}
}
} // namespace cru::platform::unix