[Runner] Workaround if we can't open the parent process

This commit is contained in:
momo5502 2017-01-30 17:47:29 +01:00
parent bef2cbfdcc
commit 5931c42cc6
3 changed files with 39 additions and 11 deletions

View File

@ -17,6 +17,7 @@
#include <Wininet.h> #include <Wininet.h>
#include <d3d9.h> #include <d3d9.h>
#include <Aclapi.h> #include <Aclapi.h>
#include <Psapi.h>
#pragma warning(push) #pragma warning(push)
#pragma warning(disable: 4996) #pragma warning(disable: 4996)

View File

@ -18,32 +18,58 @@ namespace Worker
{ {
printf("Attaching to parent process %d...\n", this->processId); printf("Attaching to parent process %d...\n", this->processId);
HANDLE processHandle = OpenProcess(SYNCHRONIZE, FALSE, this->processId); HANDLE processHandle = OpenProcess(SYNCHRONIZE, FALSE, this->processId);
if (!processHandle || processHandle == INVALID_HANDLE_VALUE)
if (!processHandle || processHandle == INVALID_HANDLE_VALUE && !Runner::isProcessAlive())
{ {
printf("Unable to attach to parent process\n"); printf("Unable to attach to parent process\n");
return;
} }
else
{
printf("Successfully attached to parent process\n"); printf("Successfully attached to parent process\n");
printf("Starting worker...\n"); printf("Starting worker...\n");
std::thread workerThread(&Runner::worker, this); std::thread workerThread(&Runner::worker, this);
if (!processHandle || processHandle == INVALID_HANDLE_VALUE)
{
while(this->isProcessAlive())
{
std::this_thread::sleep_for(100ms);
}
}
else
{
WaitForSingleObject(processHandle, INFINITE); WaitForSingleObject(processHandle, INFINITE);
CloseHandle(processHandle); CloseHandle(processHandle);
}
printf("Awaiting worker termination...\n"); printf("Awaiting worker termination...\n");
this->terminate = true; this->terminate = true;
if (workerThread.joinable()) workerThread.join(); if (workerThread.joinable()) workerThread.join();
printf("Worker terminated\n"); printf("Worker terminated\n");
} }
}
void Runner::attachHandler(Runner::Handler* handler) void Runner::attachHandler(Runner::Handler* handler)
{ {
this->handlers[handler->getCommand()] = std::shared_ptr<Runner::Handler>(handler); this->handlers[handler->getCommand()] = std::shared_ptr<Runner::Handler>(handler);
} }
bool Runner::isProcessAlive()
{
DWORD aProcesses[1024], cbNeeded;
if (!EnumProcesses(aProcesses, sizeof(aProcesses), &cbNeeded)) return false;
for(DWORD i = 0; i < cbNeeded / sizeof(DWORD); ++i)
{
if(aProcesses[i] == static_cast<DWORD>(this->processId))
{
return true;
}
}
return false;
}
void Runner::worker() void Runner::worker()
{ {
printf("Worker started\n"); printf("Worker started\n");

View File

@ -47,6 +47,7 @@ namespace Worker
private: private:
void worker(); void worker();
bool isProcessAlive();
int processId; int processId;
bool terminate; bool terminate;