diff --git a/Foundation/include/Poco/Process_UNIX.h b/Foundation/include/Poco/Process_UNIX.h index b33dd08210..ee6771e81b 100644 --- a/Foundation/include/Poco/Process_UNIX.h +++ b/Foundation/include/Poco/Process_UNIX.h @@ -19,6 +19,9 @@ #include "Poco/Foundation.h" +#include "Poco/Event.h" +#include "Poco/Mutex.h" +#include "Poco/Optional.h" #include "Poco/RefCountedObject.h" #include #include @@ -39,9 +42,13 @@ class Foundation_API ProcessHandleImpl: public RefCountedObject pid_t id() const; int wait() const; + int wait(int options) const; private: - pid_t _pid; + const pid_t _pid; + mutable FastMutex _mutex; + mutable Event _event; + mutable Optional _status; }; diff --git a/Foundation/src/Process_UNIX.cpp b/Foundation/src/Process_UNIX.cpp index 4671278e1d..edd0881ddf 100644 --- a/Foundation/src/Process_UNIX.cpp +++ b/Foundation/src/Process_UNIX.cpp @@ -16,6 +16,8 @@ #include "Poco/Exception.h" #include "Poco/NumberFormatter.h" #include "Poco/Pipe.h" +#include "Poco/Thread.h" +#include #include #include #include @@ -39,7 +41,10 @@ namespace Poco { // ProcessHandleImpl // ProcessHandleImpl::ProcessHandleImpl(pid_t pid): - _pid(pid) + _pid(pid), + _mutex(), + _event(Event::EVENT_MANUALRESET), + _status() { } @@ -57,16 +62,51 @@ pid_t ProcessHandleImpl::id() const int ProcessHandleImpl::wait() const { + if (wait(0) != _pid) + throw SystemException("Cannot wait for process", NumberFormatter::format(_pid)); + + const int status = _status.value(); + return WEXITSTATUS(status); +} + + +int ProcessHandleImpl::wait(int options) const +{ + { + FastMutex::ScopedLock lock(_mutex); + if (_status.isSpecified()) + { + return _pid; + } + } + int status; int rc; do { - rc = waitpid(_pid, &status, 0); + rc = waitpid(_pid, &status, options); } while (rc < 0 && errno == EINTR); - if (rc != _pid) - throw SystemException("Cannot wait for process", NumberFormatter::format(_pid)); - return WEXITSTATUS(status); + + if (rc == _pid) + { + FastMutex::ScopedLock lock(_mutex); + _status = status; + _event.set(); + } + else if (rc < 0 && errno == ECHILD) + { + // Looks like another thread was lucky and it should update the status for us shortly + _event.wait(); + + FastMutex::ScopedLock lock(_mutex); + if (_status.isSpecified()) + { + rc = _pid; + } + } + + return rc; } @@ -243,7 +283,7 @@ void ProcessImpl::killImpl(PIDImpl pid) bool ProcessImpl::isRunningImpl(const ProcessHandleImpl& handle) { - return isRunningImpl(handle.id()); + return handle.wait(WNOHANG) == 0; } diff --git a/Foundation/testsuite/src/ProcessTest.cpp b/Foundation/testsuite/src/ProcessTest.cpp index 1681a774f0..8708126264 100644 --- a/Foundation/testsuite/src/ProcessTest.cpp +++ b/Foundation/testsuite/src/ProcessTest.cpp @@ -14,6 +14,8 @@ #include "Poco/Process.h" #include "Poco/Pipe.h" #include "Poco/PipeStream.h" +#include "Poco/Thread.h" +#include using Poco::Process; @@ -239,6 +241,45 @@ void ProcessTest::testIsRunning() } +void ProcessTest::testIsRunningAllowsForTermination() +{ +#if !defined(_WIN32_WCE) + std::string name("TestApp"); + std::string cmd; + +#if defined(POCO_OS_FAMILY_UNIX) + cmd = "./"; + cmd += name; +#else + cmd = name; +#endif + + std::vector args; + ProcessHandle ph = Process::launch(cmd, args, 0, 0, 0); + while (Process::isRunning(ph)) + Poco::Thread::sleep(100); +#endif // !defined(_WIN32_WCE) +} + + +void ProcessTest::testSignalExitCode() +{ +#if defined(POCO_OS_FAMILY_UNIX) + std::string name("TestApp"); + std::string cmd; + + cmd = "./"; + cmd += name; + + std::vector args; + args.push_back("-raise-int"); + ProcessHandle ph = Process::launch(cmd, args, 0, 0, 0); + int rc = ph.wait(); + assert (rc == -SIGINT); +#endif // defined(POCO_OS_FAMILY_UNIX) +} + + void ProcessTest::setUp() { } @@ -259,6 +300,8 @@ CppUnit::Test* ProcessTest::suite() CppUnit_addTest(pSuite, ProcessTest, testLaunchEnv); CppUnit_addTest(pSuite, ProcessTest, testLaunchArgs); CppUnit_addTest(pSuite, ProcessTest, testIsRunning); + CppUnit_addTest(pSuite, ProcessTest, testIsRunningAllowsForTermination); + CppUnit_addTest(pSuite, ProcessTest, testSignalExitCode); return pSuite; } diff --git a/Foundation/testsuite/src/ProcessTest.h b/Foundation/testsuite/src/ProcessTest.h index 130f4ad228..e6f508ebd1 100644 --- a/Foundation/testsuite/src/ProcessTest.h +++ b/Foundation/testsuite/src/ProcessTest.h @@ -30,6 +30,8 @@ class ProcessTest: public CppUnit::TestCase void testLaunchEnv(); void testLaunchArgs(); void testIsRunning(); + void testIsRunningAllowsForTermination(); + void testSignalExitCode(); void setUp(); void tearDown();