From af44951e77d6236c76c16645a285fbb0eceae0c5 Mon Sep 17 00:00:00 2001 From: Yuval Kashtan Date: Mon, 11 Aug 2014 09:10:48 +0300 Subject: [PATCH] add is_running support --- Foundation/include/Poco/Process.h | 9 +++++++++ Foundation/include/Poco/Process_UNIX.h | 2 ++ Foundation/include/Poco/Process_VMS.h | 2 ++ Foundation/include/Poco/Process_VX.h | 2 ++ Foundation/include/Poco/Process_WIN32.h | 2 ++ Foundation/include/Poco/Process_WIN32U.h | 2 ++ Foundation/include/Poco/Process_WINCE.h | 2 ++ Foundation/src/Process.cpp | 8 ++++++++ Foundation/src/Process_UNIX.cpp | 16 ++++++++++++++++ Foundation/src/Process_VMS.cpp | 8 ++++++++ Foundation/src/Process_VX.cpp | 9 +++++++++ Foundation/src/Process_WIN32.cpp | 21 +++++++++++++++++++++ Foundation/src/Process_WIN32U.cpp | 21 +++++++++++++++++++++ Foundation/src/Process_WINCE.cpp | 22 ++++++++++++++++++++++ 14 files changed, 126 insertions(+) diff --git a/Foundation/include/Poco/Process.h b/Foundation/include/Poco/Process.h index e3a9bbbee..fc843add6 100644 --- a/Foundation/include/Poco/Process.h +++ b/Foundation/include/Poco/Process.h @@ -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. /// diff --git a/Foundation/include/Poco/Process_UNIX.h b/Foundation/include/Poco/Process_UNIX.h index adcf87eb4..76d17c8a7 100644 --- a/Foundation/include/Poco/Process_UNIX.h +++ b/Foundation/include/Poco/Process_UNIX.h @@ -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: diff --git a/Foundation/include/Poco/Process_VMS.h b/Foundation/include/Poco/Process_VMS.h index 74079ae1d..4a963f9f3 100644 --- a/Foundation/include/Poco/Process_VMS.h +++ b/Foundation/include/Poco/Process_VMS.h @@ -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); }; diff --git a/Foundation/include/Poco/Process_VX.h b/Foundation/include/Poco/Process_VX.h index 14d6ba5d4..28e87669b 100644 --- a/Foundation/include/Poco/Process_VX.h +++ b/Foundation/include/Poco/Process_VX.h @@ -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); }; diff --git a/Foundation/include/Poco/Process_WIN32.h b/Foundation/include/Poco/Process_WIN32.h index ed29327e7..935f5e5a8 100644 --- a/Foundation/include/Poco/Process_WIN32.h +++ b/Foundation/include/Poco/Process_WIN32.h @@ -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); }; diff --git a/Foundation/include/Poco/Process_WIN32U.h b/Foundation/include/Poco/Process_WIN32U.h index f9277d73f..2e880c573 100644 --- a/Foundation/include/Poco/Process_WIN32U.h +++ b/Foundation/include/Poco/Process_WIN32U.h @@ -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); }; diff --git a/Foundation/include/Poco/Process_WINCE.h b/Foundation/include/Poco/Process_WINCE.h index a77ddb5ad..3d783e9c4 100644 --- a/Foundation/include/Poco/Process_WINCE.h +++ b/Foundation/include/Poco/Process_WINCE.h @@ -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); }; diff --git a/Foundation/src/Process.cpp b/Foundation/src/Process.cpp index aba114120..845611166 100644 --- a/Foundation/src/Process.cpp +++ b/Foundation/src/Process.cpp @@ -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) { diff --git a/Foundation/src/Process_UNIX.cpp b/Foundation/src/Process_UNIX.cpp index f118f9787..3957658ab 100644 --- a/Foundation/src/Process_UNIX.cpp +++ b/Foundation/src/Process_UNIX.cpp @@ -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) diff --git a/Foundation/src/Process_VMS.cpp b/Foundation/src/Process_VMS.cpp index 47fd118b3..5816796de 100644 --- a/Foundation/src/Process_VMS.cpp +++ b/Foundation/src/Process_VMS.cpp @@ -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) { diff --git a/Foundation/src/Process_VX.cpp b/Foundation/src/Process_VX.cpp index 5ec48b110..b403ed263 100644 --- a/Foundation/src/Process_VX.cpp +++ b/Foundation/src/Process_VX.cpp @@ -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) { diff --git a/Foundation/src/Process_WIN32.cpp b/Foundation/src/Process_WIN32.cpp index 79782cf2f..02cb94574 100644 --- a/Foundation/src/Process_WIN32.cpp +++ b/Foundation/src/Process_WIN32.cpp @@ -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) { diff --git a/Foundation/src/Process_WIN32U.cpp b/Foundation/src/Process_WIN32U.cpp index 52bafd478..dd4604377 100644 --- a/Foundation/src/Process_WIN32U.cpp +++ b/Foundation/src/Process_WIN32U.cpp @@ -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) { diff --git a/Foundation/src/Process_WINCE.cpp b/Foundation/src/Process_WINCE.cpp index a694986b4..79c9b1c8a 100644 --- a/Foundation/src/Process_WINCE.cpp +++ b/Foundation/src/Process_WINCE.cpp @@ -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) {