Skip to content

Commit

Permalink
Fix win64 child process checks
Browse files Browse the repository at this point in the history
Signed-off-by: falkTX <[email protected]>
  • Loading branch information
falkTX committed May 3, 2024
1 parent 3680c36 commit 42d9058
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 40 deletions.
2 changes: 1 addition & 1 deletion src/mod-host
59 changes: 28 additions & 31 deletions src/plugin/ChildProcess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <string>
# include <winsock2.h>
Expand All @@ -22,16 +17,14 @@
# include <sys/wait.h>
#endif

// #include <sys/time.h>

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
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -90,7 +83,7 @@ class ChildProcess
const_cast<LPWSTR>(envp), // lpEnvironment
nullptr, // lpCurrentDirectory
&si, // lpStartupInfo
&process) != FALSE;
&pinfo) != FALSE;
#else
const pid_t ret = pid = vfork();

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand Down
17 changes: 9 additions & 8 deletions src/plugin/SharedMemory.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit 42d9058

Please sign in to comment.