GH #2689: Added tryWait() into Process and ProcessHandle. Handle kill()-ed UNIX process exit codes.

This commit is contained in:
Günter Obiltschnig
2020-01-22 08:23:36 +01:00
parent 5f0e0a0374
commit 924cdf235c
12 changed files with 187 additions and 95 deletions

View File

@@ -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
///

View File

@@ -39,6 +39,7 @@ public:
pid_t id() const;
int wait() const;
int tryWait() const;
private:
pid_t _pid;

View File

@@ -41,6 +41,7 @@ public:
int id() const;
int wait() const;
int tryWait() const;
private:
int _pid;

View File

@@ -40,6 +40,7 @@ public:
UInt32 id() const;
HANDLE process() const;
int wait() const;
int tryWait() const;
void closeHandle();
private:

View File

@@ -40,6 +40,7 @@ public:
UInt32 id() const;
HANDLE process() const;
int wait() const;
int tryWait() const;
void closeHandle();
private:

View File

@@ -40,6 +40,7 @@ public:
UInt32 id() const;
HANDLE process() const;
int wait() const;
int tryWait() const;
void closeHandle();
private:

View File

@@ -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);

View File

@@ -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);
}

View File

@@ -45,6 +45,12 @@ int ProcessHandleImpl::wait() const
}
int ProcessHandleImpl::tryWait() const
{
throw Poco::NotImplementedException("Process::tryWait()");
}
//
// ProcessImpl
//

View File

@@ -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
//

View File

@@ -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
//

View File

@@ -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
//