From b165ebcfb9c278dc7481abb5287e85672731c005 Mon Sep 17 00:00:00 2001 From: crupest Date: Sat, 1 Jun 2024 17:09:44 +0800 Subject: HALF WORK: finish implementing PosixSpawnSubProcess. --- include/cru/common/SubProcess.h | 47 +++++++++++++++++----- .../common/platform/unix/PosixSpawnSubProcess.h | 7 +++- 2 files changed, 41 insertions(+), 13 deletions(-) (limited to 'include') diff --git a/include/cru/common/SubProcess.h b/include/cru/common/SubProcess.h index ba46d049..9a56b726 100644 --- a/include/cru/common/SubProcess.h +++ b/include/cru/common/SubProcess.h @@ -13,7 +13,7 @@ #include namespace cru { -enum class PlatformSubProcessStatus { +enum class SubProcessStatus { /** * @brief The process has not been created and started. */ @@ -43,14 +43,40 @@ class CRU_BASE_API SubProcessFailedToStartException using SubProcessException::SubProcessException; }; -struct PlatformSubProcessStartInfo { +class CRU_BASE_API SubProcessInternalException : public SubProcessException { + public: + using SubProcessException::SubProcessException; +}; + +struct SubProcessStartInfo { String program; std::vector arguments; std::unordered_map environments; }; -struct PlatformSubProcessExitResult { +enum class SubProcessExitType { + Unknown, + Normal, + Signal, +}; + +struct SubProcessExitResult { + SubProcessExitType exit_type; int exit_code; + int exit_signal; + bool has_core_dump; + + static SubProcessExitResult Unknown() { + return {SubProcessExitType::Unknown, 0, 0, false}; + } + + static SubProcessExitResult Normal(int exit_code) { + return {SubProcessExitType::Normal, exit_code, 0, false}; + } + + static SubProcessExitResult Signal(int exit_signal, bool has_core_dump) { + return {SubProcessExitType::Normal, 0, exit_signal, has_core_dump}; + } }; /** @@ -62,8 +88,7 @@ struct PlatformSubProcessExitResult { */ class PlatformSubProcessBase : public Object { public: - explicit PlatformSubProcessBase( - const PlatformSubProcessStartInfo& start_info); + explicit PlatformSubProcessBase(const SubProcessStartInfo& start_info); ~PlatformSubProcessBase() override; @@ -104,13 +129,13 @@ class PlatformSubProcessBase : public Object { * actually running. Because there might be a window that the process exits * already but status is not updated. */ - PlatformSubProcessStatus GetStatus(); + SubProcessStatus GetStatus(); /** * @brief Get the exit result. If the process is not started, failed to start * or running, `SubProcessException` will be thrown. */ - PlatformSubProcessExitResult GetExitResult(); + SubProcessExitResult GetExitResult(); virtual io::Stream* GetStdinStream() = 0; virtual io::Stream* GetStdoutStream() = 0; @@ -137,7 +162,7 @@ class PlatformSubProcessBase : public Object { * This method will be called only once on another thread after * `PlatformCreateProcess` returns successfully */ - virtual PlatformSubProcessExitResult PlatformWaitForProcess() = 0; + virtual SubProcessExitResult PlatformWaitForProcess() = 0; /** * @brief Kill the process immediately. @@ -150,11 +175,11 @@ class PlatformSubProcessBase : public Object { virtual void PlatformKillProcess() = 0; protected: - PlatformSubProcessStartInfo start_info_; - PlatformSubProcessExitResult exit_result_; + SubProcessStartInfo start_info_; + SubProcessExitResult exit_result_; private: - PlatformSubProcessStatus status_; + SubProcessStatus status_; std::thread process_thread_; std::mutex process_mutex_; diff --git a/include/cru/common/platform/unix/PosixSpawnSubProcess.h b/include/cru/common/platform/unix/PosixSpawnSubProcess.h index 9a08daa6..e8e23738 100644 --- a/include/cru/common/platform/unix/PosixSpawnSubProcess.h +++ b/include/cru/common/platform/unix/PosixSpawnSubProcess.h @@ -4,6 +4,7 @@ #ifdef CRU_PLATFORM_UNIX +#include "../../Base.h" #include "../../SubProcess.h" #include "../../io/AutoReadStream.h" @@ -14,8 +15,10 @@ namespace cru::platform::unix { class PosixSpawnSubProcess : public PlatformSubProcessBase { + CRU_DEFINE_CLASS_LOG_TAG(u"PosixSpawnSubProcess") + public: - explicit PosixSpawnSubProcess(const PlatformSubProcessStartInfo& start_info); + explicit PosixSpawnSubProcess(const SubProcessStartInfo& start_info); ~PosixSpawnSubProcess(); io::Stream* GetStdinStream() override; @@ -24,7 +27,7 @@ class PosixSpawnSubProcess : public PlatformSubProcessBase { protected: void PlatformCreateProcess() override; - PlatformSubProcessExitResult PlatformWaitForProcess() override; + SubProcessExitResult PlatformWaitForProcess() override; void PlatformKillProcess() override; private: -- cgit v1.2.3