Skip to content

Commit

Permalink
More Windows fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mjcarroll committed Apr 2, 2024
1 parent 9c28ca4 commit e817d54
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
18 changes: 2 additions & 16 deletions include/gz/utils/Subprocess.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -214,9 +200,9 @@ class Subprocess
{
int signal = 0;
#if defined(_WIN32)
signal = static_cast<int>(Signals::CTRL_C_EVENT);
signal = static_cast<int>(CTRL_C_EVENT);
#else
signal = static_cast<int>(Signals::SIGNAL_SIGINT);
signal = static_cast<int>(SIGINT);
#endif
return this->Signal(signal);
}
Expand Down
35 changes: 24 additions & 11 deletions include/gz/utils/detail/subprocess.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e817d54

Please sign in to comment.