Merge pull request #514 from yuvalk/develop

add is_running support
This commit is contained in:
Aleksandar Fabijanic 2014-08-12 18:35:27 -05:00
commit 828732f774
16 changed files with 157 additions and 0 deletions

View File

@ -217,6 +217,15 @@ public:
/// Waits for the process specified by handle to terminate
/// and returns the exit code of the process.
static bool isRunning(const ProcessHandle& handle);
/// check if the process specified by handle is running or not
///
/// This is preferable on Windows where process IDs
/// may be reused.
static bool isRunning(PID pid);
/// Check if the process specified by given pid is running or not.
static void kill(ProcessHandle& handle);
/// Kills the process specified by handle.
///

View File

@ -66,6 +66,8 @@ public:
const EnvImpl& env);
static void killImpl(ProcessHandleImpl& handle);
static void killImpl(PIDImpl pid);
static bool isRunningImpl(const ProcessHandleImpl& handle);
static bool isRunningImpl(PIDImpl pid);
static void requestTerminationImpl(PIDImpl pid);
private:

View File

@ -67,6 +67,8 @@ public:
static int waitImpl(PIDImpl pid);
static void killImpl(ProcessHandleImpl& handle);
static void killImpl(PIDImpl pid);
static bool isRunningImpl(const ProcessHandleImpl& handle);
static bool isRunningImpl(PIDImpl pid);
static void requestTerminationImpl(PIDImpl pid);
};

View File

@ -68,6 +68,8 @@ public:
const EnvImpl& env);
static void killImpl(ProcessHandleImpl& handle);
static void killImpl(PIDImpl pid);
static bool isRunningImpl(const ProcessHandleImpl& handle);
static bool isRunningImpl(PIDImpl pid);
static void requestTerminationImpl(PIDImpl pid);
};

View File

@ -72,6 +72,8 @@ public:
const EnvImpl& env);
static void killImpl(ProcessHandleImpl& handle);
static void killImpl(PIDImpl pid);
static bool isRunningImpl(const ProcessHandleImpl& handle);
static bool isRunningImpl(PIDImpl pid);
static void requestTerminationImpl(PIDImpl pid);
static std::string terminationEventName(PIDImpl pid);
};

View File

@ -72,6 +72,8 @@ public:
const EnvImpl& env);
static void killImpl(ProcessHandleImpl& handle);
static void killImpl(PIDImpl pid);
static bool isRunningImpl(const ProcessHandleImpl& handle);
static bool isRunningImpl(PIDImpl pid);
static void requestTerminationImpl(PIDImpl pid);
static std::string terminationEventName(PIDImpl pid);
};

View File

@ -72,6 +72,8 @@ public:
const EnvImpl& env);
static void killImpl(ProcessHandleImpl& handle);
static void killImpl(PIDImpl pid);
static bool isRunningImpl(const ProcessHandleImpl& handle);
static bool isRunningImpl(PIDImpl pid);
static void requestTerminationImpl(PIDImpl pid);
static std::string terminationEventName(PIDImpl pid);
};

View File

@ -191,6 +191,14 @@ void Process::kill(PID pid)
killImpl(pid);
}
bool Process::isRunning(const ProcessHandle& handle)
{
return isRunningImpl(*handle._pImpl);
}
bool Process::isRunning(PID pid)
{
return isRunningImpl(pid);
}
void Process::requestTermination(PID pid)
{

View File

@ -220,7 +220,23 @@ void ProcessImpl::killImpl(PIDImpl pid)
}
}
bool ProcessImpl::isRunningImpl(const ProcessHandleImpl& handle)
{
return isRunningImpl(handle.id());
}
bool ProcessImpl::isRunningImpl(PIDImpl pid)
{
if (kill(pid, 0) == 0)
{
return true;
}
else
{
return false;
}
}
void ProcessImpl::requestTerminationImpl(PIDImpl pid)
{
if (kill(pid, SIGINT) != 0)

View File

@ -139,6 +139,14 @@ void ProcessImpl::killImpl(PIDImpl pid)
}
}
bool ProcessImpl::isRunningImpl(const ProcessHandleImpl& handle)
{
throw Poco::NotImplementedException("Process::is_running()");
}
bool ProcessImpl::isRunningImpl(PIDImpl pid)
{
throw Poco::NotImplementedException("Process::is_running()");
}
void ProcessImpl::requestTerminationImpl(PIDImpl pid)
{

View File

@ -80,6 +80,15 @@ void ProcessImpl::killImpl(PIDImpl pid)
throw Poco::NotImplementedException("Process::kill()");
}
bool ProcessImpl::isRunningImpl(const ProcessHandleImpl& handle)
{
throw Poco::NotImplementedException("Process::is_running()");
}
bool ProcessImpl::isRunningImpl(PIDImpl pid)
{
throw Poco::NotImplementedException("Process::is_running()");
}
void ProcessImpl::requestTerminationImpl(PIDImpl pid)
{

View File

@ -255,6 +255,27 @@ void ProcessImpl::killImpl(PIDImpl pid)
}
}
bool ProcessImpl::isRunningImpl(const ProcessHandleImpl& handle)
{
BOOL fRC = true;
DWORD exit_code;
GetExitCodeProcess(handle.process(), &exit_code);
if (exit_code != STILL_ACTIVE) fRC = false;
return fRC;
}
bool ProcessImpl::isRunningImpl(PIDImpl pid)
{
HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
BOOL fRC = true;
DWORD exit_code;
GetExitCodeProcess(hProc, &exit_code);
if (exit_code != STILL_ACTIVE) fRC = false;
return fRC;
}
void ProcessImpl::requestTerminationImpl(PIDImpl pid)
{

View File

@ -261,6 +261,27 @@ void ProcessImpl::killImpl(PIDImpl pid)
}
}
bool ProcessImpl::isRunningImpl(const ProcessHandleImpl& handle)
{
BOOL fRC = true;
DWORD exit_code;
GetExitCodeProcess(handle.process(), &exit_code);
if (exit_code != STILL_ACTIVE) fRC = false;
return fRC;
}
bool ProcessImpl::isRunningImpl(PIDImpl pid)
{
HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
BOOL fRC = true;
DWORD exit_code;
GetExitCodeProcess(hProc, &exit_code);
if (exit_code != STILL_ACTIVE) fRC = false;
return fRC;
}
void ProcessImpl::requestTerminationImpl(PIDImpl pid)
{

View File

@ -186,6 +186,28 @@ void ProcessImpl::killImpl(PIDImpl pid)
}
}
bool ProcessImpl::isRunningImpl(const ProcessHandleImpl& handle)
{
BOOL fRC = true;
DWORD exit_code;
GetExitCodeProcess(handle.process(), &exit_code);
if (exit_code != STILL_ACTIVE) fRC = false;
return fRC;
}
bool ProcessImpl::isRunningImpl(PIDImpl pid)
{
HANDLE hProc = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
BOOL fRC = true;
DWORD exit_code;
GetExitCodeProcess(hProc, &exit_code);
if (exit_code != STILL_ACTIVE) fRC = false;
return fRC;
}
void ProcessImpl::requestTerminationImpl(PIDImpl pid)
{

View File

@ -92,6 +92,35 @@ void ProcessTest::testLaunchRedirectIn()
#endif // !defined(_WIN32_WCE)
}
void ProcessTest::testIsRunning()
{
std::string name("TestApp");
std::string cmd;
#if defined(_DEBUG)
name += "d";
#endif
#if defined(POCO_OS_FAMILY_UNIX)
cmd = "./";
cmd += name;
#else
cmd = name;
#endif
std::vector<std::string> args;
args.push_back("-count");
Pipe inPipe;
ProcessHandle ph = Process::launch(cmd, args, &inPipe, 0, 0);
Process::PID id = ph.id();
assert (Process::isRunning(ph));
assert (Process::isRunning(id));
PipeOutputStream ostr(inPipe);
ostr << std::string(100, 'x');
ostr.close();
int rc = ph.wait();
assert (!Process::isRunning(ph));
assert (!Process::isRunning(id));
}
void ProcessTest::testLaunchRedirectOut()
{
@ -175,6 +204,7 @@ CppUnit::Test* ProcessTest::suite()
CppUnit_addTest(pSuite, ProcessTest, testLaunchRedirectIn);
CppUnit_addTest(pSuite, ProcessTest, testLaunchRedirectOut);
CppUnit_addTest(pSuite, ProcessTest, testLaunchEnv);
CppUnit_addTest(pSuite, ProcessTest, testIsRunning);
return pSuite;
}

View File

@ -30,6 +30,7 @@ public:
void testLaunchRedirectIn();
void testLaunchRedirectOut();
void testLaunchEnv();
void testIsRunning();
void setUp();
void tearDown();