From e817d54cdd1853f55820d7e291e68b1070a94e0d Mon Sep 17 00:00:00 2001 From: Michael Carroll Date: Tue, 2 Apr 2024 16:24:51 +0000 Subject: [PATCH] More Windows fixes --- include/gz/utils/Subprocess.hh | 18 ++------------ include/gz/utils/detail/subprocess.h | 35 +++++++++++++++++++--------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/include/gz/utils/Subprocess.hh b/include/gz/utils/Subprocess.hh index 9128b24..87ae874 100644 --- a/include/gz/utils/Subprocess.hh +++ b/include/gz/utils/Subprocess.hh @@ -40,20 +40,6 @@ namespace gz namespace utils { -enum class Signals -{ -#if defined(_WIN32) - CTRL_C_EVENT = 0, - CTRL_BREAK_EVENT = 1, - CTRL_CLOSE_EVENT = 2, - CTRL_LOGOFF_EVENT = 5, - CTRL_SHUTDOWN_EVENT = 6, -#else - SIGNAL_SIGINT = 2, - SIGNAL_SIGQUIT = 3, -#endif -}; - /// \brief Create a RAII-type object that encapsulates a subprocess. class Subprocess { @@ -214,9 +200,9 @@ class Subprocess { int signal = 0; #if defined(_WIN32) - signal = static_cast(Signals::CTRL_C_EVENT); + signal = static_cast(CTRL_C_EVENT); #else - signal = static_cast(Signals::SIGNAL_SIGINT); + signal = static_cast(SIGINT); #endif return this->Signal(signal); } diff --git a/include/gz/utils/detail/subprocess.h b/include/gz/utils/detail/subprocess.h index 08e64ec..4d2aef4 100644 --- a/include/gz/utils/detail/subprocess.h +++ b/include/gz/utils/detail/subprocess.h @@ -170,6 +170,17 @@ subprocess_weak int subprocess_join(struct subprocess_s *const process, /// the parent process. subprocess_weak int subprocess_destroy(struct subprocess_s *const process); +/// @brief Signal a previously created process. +/// @param process The process to signal. +/// @param signal The signal number to send to the process. +/// @return On success zero is returned. +/// +/// Allows to send signals to the subprocess. For unix-based systems, +/// this includes any signal that appears in `kill -l`. For Windows +/// systems, this includes events available to GenerateConsoleCtrlEvent. +subprocess_weak int subprocess_signal(struct subprocess_s *const process, + int signum); + /// @brief Terminate a previously created process. /// @param process The process to terminate. /// @return On success zero is returned. @@ -923,17 +934,6 @@ FILE *subprocess_stderr(const struct subprocess_s *const process) { } } -int subprocess_signal(const subprocess_s *const process, - int signum) -{ -#if defined(_WIN32) - return GenerateConsoleCtrlEvent(signum, process->hProcess); -#else - return kill(process->child, signum); -#endif - -} - int subprocess_join(struct subprocess_s *const process, int *const out_return_code) { #if defined(_WIN32) @@ -1033,6 +1033,19 @@ int subprocess_destroy(struct subprocess_s *const process) { return 0; } +int subprocess_signal(const subprocess_s *const process, + int signum) +{ + int result; +#if defined(_WIN32) + auto processId = GetProcessId(process->hProcess); + result = GenerateConsoleCtrlEvent(signum, processId); +#else + result = kill(process->child, signum); +#endif + return result; +} + int subprocess_terminate(struct subprocess_s *const process) { #if defined(_WIN32) unsigned int killed_process_exit_code;