mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-26 18:42:41 +01:00
GH #2689: Added tryWait() into Process and ProcessHandle. Handle kill()-ed UNIX process exit codes.
This commit is contained in:
@@ -66,6 +66,11 @@ public:
|
||||
/// Waits for the process to terminate
|
||||
/// and returns the exit code of the process.
|
||||
|
||||
int tryWait() const;
|
||||
/// Checks that process is terminated
|
||||
/// and returns the exit code of the process.
|
||||
/// If the process is still running, returns -1.
|
||||
|
||||
protected:
|
||||
ProcessHandle(ProcessHandleImpl* pImpl);
|
||||
|
||||
@@ -211,6 +216,10 @@ public:
|
||||
/// Waits for the process specified by handle to terminate
|
||||
/// and returns the exit code of the process.
|
||||
|
||||
static int tryWait(const ProcessHandle& handle);
|
||||
/// Checks that process is finished and returns the exit code of the
|
||||
/// process. If the process is still running, returns -1.
|
||||
|
||||
static bool isRunning(const ProcessHandle& handle);
|
||||
/// check if the process specified by handle is running or not
|
||||
///
|
||||
|
||||
@@ -39,6 +39,7 @@ public:
|
||||
|
||||
pid_t id() const;
|
||||
int wait() const;
|
||||
int tryWait() const;
|
||||
|
||||
private:
|
||||
pid_t _pid;
|
||||
|
||||
@@ -41,6 +41,7 @@ public:
|
||||
|
||||
int id() const;
|
||||
int wait() const;
|
||||
int tryWait() const;
|
||||
|
||||
private:
|
||||
int _pid;
|
||||
|
||||
@@ -40,6 +40,7 @@ public:
|
||||
UInt32 id() const;
|
||||
HANDLE process() const;
|
||||
int wait() const;
|
||||
int tryWait() const;
|
||||
void closeHandle();
|
||||
|
||||
private:
|
||||
|
||||
@@ -40,6 +40,7 @@ public:
|
||||
UInt32 id() const;
|
||||
HANDLE process() const;
|
||||
int wait() const;
|
||||
int tryWait() const;
|
||||
void closeHandle();
|
||||
|
||||
private:
|
||||
|
||||
@@ -40,6 +40,7 @@ public:
|
||||
UInt32 id() const;
|
||||
HANDLE process() const;
|
||||
int wait() const;
|
||||
int tryWait() const;
|
||||
void closeHandle();
|
||||
|
||||
private:
|
||||
|
||||
@@ -110,6 +110,12 @@ int ProcessHandle::wait() const
|
||||
}
|
||||
|
||||
|
||||
int ProcessHandle::tryWait() const
|
||||
{
|
||||
return _pImpl->tryWait();
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// Process
|
||||
//
|
||||
@@ -166,6 +172,12 @@ int Process::wait(const ProcessHandle& handle)
|
||||
}
|
||||
|
||||
|
||||
int Process::tryWait(const ProcessHandle& handle)
|
||||
{
|
||||
return handle.tryWait();
|
||||
}
|
||||
|
||||
|
||||
void Process::kill(ProcessHandle& handle)
|
||||
{
|
||||
killImpl(*handle._pImpl);
|
||||
|
||||
@@ -66,7 +66,31 @@ int ProcessHandleImpl::wait() const
|
||||
while (rc < 0 && errno == EINTR);
|
||||
if (rc != _pid)
|
||||
throw SystemException("Cannot wait for process", NumberFormatter::format(_pid));
|
||||
return WEXITSTATUS(status);
|
||||
|
||||
if (WIFEXITED(status)) // normal termination
|
||||
return WEXITSTATUS(status);
|
||||
else // termination by a signal
|
||||
return 256 + WTERMSIG(status);
|
||||
}
|
||||
|
||||
|
||||
int ProcessHandleImpl::tryWait() const
|
||||
{
|
||||
int status;
|
||||
int rc;
|
||||
do
|
||||
{
|
||||
rc = waitpid(_pid, &status, WNOHANG);
|
||||
}
|
||||
while (rc < 0 && errno == EINTR);
|
||||
if (rc == 0)
|
||||
return -1;
|
||||
if (rc != _pid)
|
||||
throw SystemException("Cannot wait for process", NumberFormatter::format(_pid));
|
||||
if (WIFEXITED(status)) // normal termination
|
||||
return WEXITSTATUS(status);
|
||||
else // termination by a signal
|
||||
return 256 + WTERMSIG(status);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -45,6 +45,12 @@ int ProcessHandleImpl::wait() const
|
||||
}
|
||||
|
||||
|
||||
int ProcessHandleImpl::tryWait() const
|
||||
{
|
||||
throw Poco::NotImplementedException("Process::tryWait()");
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// ProcessImpl
|
||||
//
|
||||
|
||||
@@ -74,6 +74,18 @@ int ProcessHandleImpl::wait() const
|
||||
}
|
||||
|
||||
|
||||
int ProcessHandleImpl::tryWait() const
|
||||
{
|
||||
DWORD exitCode;
|
||||
if (GetExitCodeProcess(_hProcess, &exitCode) == 0)
|
||||
throw SystemException("Cannot get exit code for process", NumberFormatter::format(_pid));
|
||||
if (exitCode == STILL_ACTIVE)
|
||||
return -1;
|
||||
else
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// ProcessImpl
|
||||
//
|
||||
|
||||
@@ -78,6 +78,18 @@ int ProcessHandleImpl::wait() const
|
||||
}
|
||||
|
||||
|
||||
int ProcessHandleImpl::tryWait() const
|
||||
{
|
||||
DWORD exitCode;
|
||||
if (GetExitCodeProcess(_hProcess, &exitCode) == 0)
|
||||
throw SystemException("Cannot get exit code for process", NumberFormatter::format(_pid));
|
||||
if (exitCode == STILL_ACTIVE)
|
||||
return -1;
|
||||
else
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// ProcessImpl
|
||||
//
|
||||
|
||||
@@ -73,6 +73,18 @@ int ProcessHandleImpl::wait() const
|
||||
}
|
||||
|
||||
|
||||
int ProcessHandleImpl::tryWait() const
|
||||
{
|
||||
DWORD exitCode;
|
||||
if (GetExitCodeProcess(_hProcess, &exitCode) == 0)
|
||||
throw SystemException("Cannot get exit code for process", NumberFormatter::format(_pid));
|
||||
if (exitCode == STILL_ACTIVE)
|
||||
return -1;
|
||||
else
|
||||
return exitCode;
|
||||
}
|
||||
|
||||
|
||||
//
|
||||
// ProcessImpl
|
||||
//
|
||||
|
||||
Reference in New Issue
Block a user