From 42d90585d6680f6122495c77780f98fb05bc8b63 Mon Sep 17 00:00:00 2001 From: falkTX Date: Fri, 3 May 2024 18:37:16 +0200 Subject: [PATCH] Fix win64 child process checks Signed-off-by: falkTX --- src/mod-host | 2 +- src/plugin/ChildProcess.hpp | 59 ++++++++++++++++++------------------- src/plugin/SharedMemory.hpp | 17 ++++++----- 3 files changed, 38 insertions(+), 40 deletions(-) diff --git a/src/mod-host b/src/mod-host index 7d0b7a2..b6dc4e0 160000 --- a/src/mod-host +++ b/src/mod-host @@ -1 +1 @@ -Subproject commit 7d0b7a2d7d58df3068eecfcf2594d4524b9ebeb6 +Subproject commit b6dc4e0651cb8d2905856b298cee337944179d73 diff --git a/src/plugin/ChildProcess.hpp b/src/plugin/ChildProcess.hpp index 6b771f1..16f76ab 100644 --- a/src/plugin/ChildProcess.hpp +++ b/src/plugin/ChildProcess.hpp @@ -6,11 +6,6 @@ #include "extra/Sleep.hpp" #include "Time.hpp" -#if defined(DISTRHO_OS_MAC) -#elif defined(DISTRHO_OS_WINDOWS) -#else -#endif - #ifdef DISTRHO_OS_WINDOWS # include # include @@ -22,8 +17,6 @@ # include #endif -// #include - START_NAMESPACE_DISTRHO // ----------------------------------------------------------------------------------------------------------- @@ -31,7 +24,7 @@ START_NAMESPACE_DISTRHO class ChildProcess { #ifdef _WIN32 - PROCESS_INFORMATION process = { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, 0, 0 }; + PROCESS_INFORMATION pinfo = { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, 0, 0 }; #else pid_t pid = -1; #endif @@ -60,7 +53,7 @@ class ChildProcess if (i != 0) cmd += " "; - if (std::strchr(args[i], ' ') != nullptr) + if (args[i][0] != '"' && std::strchr(args[i], ' ') != nullptr) { cmd += "\""; cmd += args[i]; @@ -90,7 +83,7 @@ class ChildProcess const_cast(envp), // lpEnvironment nullptr, // lpCurrentDirectory &si, // lpStartupInfo - &process) != FALSE; + &pinfo) != FALSE; #else const pid_t ret = pid = vfork(); @@ -123,38 +116,39 @@ class ChildProcess bool sendTerminate = true; #ifdef _WIN32 - if (process.hProcess == INVALID_HANDLE_VALUE) + if (pinfo.hProcess == INVALID_HANDLE_VALUE) return; - const PROCESS_INFORMATION oprocess = process; - process = { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, 0, 0 }; + const PROCESS_INFORMATION opinfo = pinfo; + pinfo = { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, 0, 0 }; - for (;;) + for (DWORD exitCode;;) { - switch (WaitForSingleObject(oprocess.hProcess, 0)) + if (GetExitCodeProcess(opinfo.hProcess, &exitCode) == FALSE || + exitCode != STILL_ACTIVE || + WaitForSingleObject(opinfo.hProcess, 0) != WAIT_TIMEOUT) { - case WAIT_OBJECT_0: - case WAIT_FAILED: - CloseHandle(oprocess.hThread); - CloseHandle(oprocess.hProcess); + CloseHandle(opinfo.hThread); + CloseHandle(opinfo.hProcess); return; } if (sendTerminate) { sendTerminate = false; - TerminateProcess(oprocess.hProcess, 15); + TerminateProcess(opinfo.hProcess, ERROR_BROKEN_PIPE); } + if (d_gettime_ms() < timeout) { d_msleep(5); continue; } d_stderr("ChildProcess::stop() - timed out"); - TerminateProcess(oprocess.hProcess, 9); + TerminateProcess(opinfo.hProcess, 9); d_msleep(5); - CloseHandle(oprocess.hThread); - CloseHandle(oprocess.hProcess); + CloseHandle(opinfo.hThread); + CloseHandle(opinfo.hProcess); break; } #else @@ -223,15 +217,18 @@ class ChildProcess bool isRunning() { #ifdef _WIN32 - if (process.hProcess == INVALID_HANDLE_VALUE) + if (pinfo.hProcess == INVALID_HANDLE_VALUE) return false; - if (WaitForSingleObject(process.hProcess, 0) == WAIT_FAILED) + DWORD exitCode; + if (GetExitCodeProcess(pinfo.hProcess, &exitCode) == FALSE || + exitCode != STILL_ACTIVE || + WaitForSingleObject(pinfo.hProcess, 0) != WAIT_TIMEOUT) { - const PROCESS_INFORMATION oprocess = process; - process = { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, 0, 0 }; - CloseHandle(oprocess.hThread); - CloseHandle(oprocess.hProcess); + const PROCESS_INFORMATION opinfo = pinfo; + pinfo = { INVALID_HANDLE_VALUE, INVALID_HANDLE_VALUE, 0, 0 }; + CloseHandle(opinfo.hThread); + CloseHandle(opinfo.hProcess); return false; } @@ -263,8 +260,8 @@ class ChildProcess void terminate() { #ifdef _WIN32 - if (process.hProcess != INVALID_HANDLE_VALUE) - TerminateProcess(process.hProcess, 15); + if (pinfo.hProcess != INVALID_HANDLE_VALUE) + TerminateProcess(pinfo.hProcess, 15); #else if (pid > 0) kill(pid, SIGTERM); diff --git a/src/plugin/SharedMemory.hpp b/src/plugin/SharedMemory.hpp index c0e6f8e..70b9cce 100644 --- a/src/plugin/SharedMemory.hpp +++ b/src/plugin/SharedMemory.hpp @@ -253,17 +253,18 @@ class SharedMemory void post() { - #if defined(DISTRHO_OS_WINDOWS) + #if defined(DISTRHO_OS_WINDOWS) ReleaseSemaphore(data->sem1, 1, nullptr); - #else - const bool unlocked = __sync_bool_compare_and_swap(&data->sem1, 0, 1); - DISTRHO_SAFE_ASSERT_RETURN(unlocked,); - #ifdef DISTRHO_OS_MAC - __ulock_wake(0x1000003, &data->sem1, 0); #else - syscall(__NR_futex, &data->sem1, FUTEX_WAKE, 1, nullptr, nullptr, 0); + if (__sync_bool_compare_and_swap(&data->sem1, 0, 1)) + { + #ifdef DISTRHO_OS_MAC + __ulock_wake(0x1000003, &data->sem1, 0); + #else + syscall(__NR_futex, &data->sem1, FUTEX_WAKE, 1, nullptr, nullptr, 0); + #endif + } #endif - #endif } bool wait()