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
|
/// Waits for the process to terminate
|
||||||
/// and returns the exit code of the process.
|
/// 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:
|
protected:
|
||||||
ProcessHandle(ProcessHandleImpl* pImpl);
|
ProcessHandle(ProcessHandleImpl* pImpl);
|
||||||
|
|
||||||
@@ -211,6 +216,10 @@ public:
|
|||||||
/// Waits for the process specified by handle to terminate
|
/// Waits for the process specified by handle to terminate
|
||||||
/// and returns the exit code of the process.
|
/// 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);
|
static bool isRunning(const ProcessHandle& handle);
|
||||||
/// check if the process specified by handle is running or not
|
/// check if the process specified by handle is running or not
|
||||||
///
|
///
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ public:
|
|||||||
|
|
||||||
pid_t id() const;
|
pid_t id() const;
|
||||||
int wait() const;
|
int wait() const;
|
||||||
|
int tryWait() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
pid_t _pid;
|
pid_t _pid;
|
||||||
|
|||||||
@@ -41,6 +41,7 @@ public:
|
|||||||
|
|
||||||
int id() const;
|
int id() const;
|
||||||
int wait() const;
|
int wait() const;
|
||||||
|
int tryWait() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int _pid;
|
int _pid;
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ public:
|
|||||||
UInt32 id() const;
|
UInt32 id() const;
|
||||||
HANDLE process() const;
|
HANDLE process() const;
|
||||||
int wait() const;
|
int wait() const;
|
||||||
|
int tryWait() const;
|
||||||
void closeHandle();
|
void closeHandle();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ public:
|
|||||||
UInt32 id() const;
|
UInt32 id() const;
|
||||||
HANDLE process() const;
|
HANDLE process() const;
|
||||||
int wait() const;
|
int wait() const;
|
||||||
|
int tryWait() const;
|
||||||
void closeHandle();
|
void closeHandle();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ public:
|
|||||||
UInt32 id() const;
|
UInt32 id() const;
|
||||||
HANDLE process() const;
|
HANDLE process() const;
|
||||||
int wait() const;
|
int wait() const;
|
||||||
|
int tryWait() const;
|
||||||
void closeHandle();
|
void closeHandle();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -110,6 +110,12 @@ int ProcessHandle::wait() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int ProcessHandle::tryWait() const
|
||||||
|
{
|
||||||
|
return _pImpl->tryWait();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Process
|
// 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)
|
void Process::kill(ProcessHandle& handle)
|
||||||
{
|
{
|
||||||
killImpl(*handle._pImpl);
|
killImpl(*handle._pImpl);
|
||||||
|
|||||||
@@ -66,7 +66,31 @@ int ProcessHandleImpl::wait() const
|
|||||||
while (rc < 0 && errno == EINTR);
|
while (rc < 0 && errno == EINTR);
|
||||||
if (rc != _pid)
|
if (rc != _pid)
|
||||||
throw SystemException("Cannot wait for process", NumberFormatter::format(_pid));
|
throw SystemException("Cannot wait for process", NumberFormatter::format(_pid));
|
||||||
|
|
||||||
|
if (WIFEXITED(status)) // normal termination
|
||||||
return WEXITSTATUS(status);
|
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
|
// 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
|
// 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
|
// 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
|
// ProcessImpl
|
||||||
//
|
//
|
||||||
|
|||||||
Reference in New Issue
Block a user