diff options
author | crupest <crupest@outlook.com> | 2024-02-12 15:47:31 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2024-02-14 22:58:11 +0800 |
commit | f9c404510584faab71b9e9d911d9b396b0f420b0 (patch) | |
tree | a46280ef09459e1f2997ad4a15f2d4053519e634 | |
parent | 96ed44c31b92f1492be68c084e8b18972d549743 (diff) | |
download | cru-f9c404510584faab71b9e9d911d9b396b0f420b0.tar.gz cru-f9c404510584faab71b9e9d911d9b396b0f420b0.tar.bz2 cru-f9c404510584faab71b9e9d911d9b396b0f420b0.zip |
WORKING: add non-block flag for unix pipe.
-rw-r--r-- | include/cru/common/platform/unix/UnixPipe.h | 12 | ||||
-rw-r--r-- | src/common/platform/unix/PosixSpawnSubProcess.cpp | 15 | ||||
-rw-r--r-- | src/common/platform/unix/UnixPipe.cpp | 11 |
3 files changed, 35 insertions, 3 deletions
diff --git a/include/cru/common/platform/unix/UnixPipe.h b/include/cru/common/platform/unix/UnixPipe.h index 153c52bf..2ea80bde 100644 --- a/include/cru/common/platform/unix/UnixPipe.h +++ b/include/cru/common/platform/unix/UnixPipe.h @@ -5,8 +5,17 @@ #ifdef CRU_PLATFORM_UNIX #include "../../Base.h" +#include "../../Bitmask.h" namespace cru::platform::unix { +namespace details { +struct UnixPipeFlagTag; +} +using UnixPipeFlag = Bitmask<details::UnixPipeFlagTag>; +struct UnixPipeFlags { + constexpr static auto NonBlock = UnixPipeFlag::FromOffset(1); +}; + /** * @brief an unix pipe, commonly for communication of parent process and child * process. @@ -32,7 +41,7 @@ class UnixPipe : public Object { Receive, }; - explicit UnixPipe(Usage usage); + explicit UnixPipe(Usage usage, UnixPipeFlag flags = {}); CRU_DELETE_COPY(UnixPipe) CRU_DELETE_MOVE(UnixPipe) @@ -51,6 +60,7 @@ class UnixPipe : public Object { private: Usage usage_; + UnixPipeFlag flags_; int read_fd_; int write_fd_; }; diff --git a/src/common/platform/unix/PosixSpawnSubProcess.cpp b/src/common/platform/unix/PosixSpawnSubProcess.cpp index 6a8a8d0f..a356de77 100644 --- a/src/common/platform/unix/PosixSpawnSubProcess.cpp +++ b/src/common/platform/unix/PosixSpawnSubProcess.cpp @@ -2,11 +2,24 @@ #include "cru/common/SubProcess.h" #include <spawn.h> +#include <memory> namespace cru::platform::unix { PosixSpawnSubProcess::PosixSpawnSubProcess( const PlatformSubProcessStartInfo& start_info) - : PlatformSubProcessBase(start_info), pid_(0), exit_code_(0) {} + : PlatformSubProcessBase(start_info), + pid_(0), + exit_code_(0), + stdin_pipe_(UnixPipe::Usage::Send), + stdout_pipe_(UnixPipe::Usage::Receive), + stderr_pipe_(UnixPipe::Usage::Receive) { + stdin_stream_ = std::make_unique<UnixFileStream>( + stdin_pipe_.GetSelfFileDescriptor(), false, false, true, false); + stdout_stream_ = std::make_unique<UnixFileStream>( + stdout_pipe_.GetSelfFileDescriptor(), false, true, false, false); + stderr_stream_ = std::make_unique<UnixFileStream>( + stderr_pipe_.GetSelfFileDescriptor(), false, true, false, false); +} PosixSpawnSubProcess::~PosixSpawnSubProcess() {} diff --git a/src/common/platform/unix/UnixPipe.cpp b/src/common/platform/unix/UnixPipe.cpp index 8393f022..4d081d5e 100644 --- a/src/common/platform/unix/UnixPipe.cpp +++ b/src/common/platform/unix/UnixPipe.cpp @@ -2,14 +2,23 @@ #include "cru/common/Exception.h" #include "cru/common/log/Logger.h" +#include <fcntl.h> +#include <sys/fcntl.h> #include <unistd.h> namespace cru::platform::unix { -UnixPipe::UnixPipe(Usage usage) : usage_(usage) { +UnixPipe::UnixPipe(Usage usage, UnixPipeFlag flags) + : usage_(usage), flags_(flags) { int fds[2]; if (pipe(fds) != 0) { throw ErrnoException(u"Failed to create unix pipe."); } + + if (flags & UnixPipeFlags::NonBlock) { + fcntl(fds[0], F_SETFL, O_NONBLOCK); + fcntl(fds[1], F_SETFL, O_NONBLOCK); + } + read_fd_ = fds[0]; write_fd_ = fds[1]; } |