Skip to content

Commit

Permalink
Reworked wait_for_exit, concerns #99 and #112
Browse files Browse the repository at this point in the history
  • Loading branch information
klemens-morgenstern committed Sep 25, 2018
1 parent e72127f commit 0938103
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion include/boost/process/detail/posix/wait_for_exit.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ inline void wait(const child_handle &p, int & exit_code, std::error_code &ec) no
{
ret = ::waitpid(p.pid, &status, 0);
}
while (((ret == -1) && (errno == EINTR)) || (ret != -1 && !WIFEXITED(status) && !WIFSIGNALED(status)));
while (((ret == -1) && (errno == EINTR)) ||
(ret != -1 && !WIFEXITED(status) && !WIFSIGNALED(status)));

if (ret == -1)
ec = boost::process::detail::get_last_error();
Expand All @@ -53,14 +54,43 @@ inline bool wait_until(
const std::chrono::time_point<Clock, Duration>& time_out,
std::error_code & ec) noexcept
{

::sigset_t sigset;

::sigemptyset(&sigset);
::sigaddset(&sigset, SIGCHLD);

auto get_timespec =
[](const Duration & dur)
{
::timespec ts;
ts.tv_sec = std::chrono::duration_cast<std::chrono::seconds>(dur).count();
ts.tv_nsec = std::chrono::duration_cast<std::chrono::nanoseconds>(dur).count() % 1000000000;
return ts;
};

pid_t ret;
int status;

struct ::sigaction old_sig;
if (-1 == ::sigaction(SIGCHLD, nullptr, &old_sig))
{
ec = get_last_error();
return false;
}

bool timed_out;

do
{
auto ts = get_timespec(time_out - Clock::now());
auto ret_sig = ::sigtimedwait(&sigset, nullptr, &ts);
errno = 0;
ret = ::waitpid(p.pid, &status, WNOHANG);

if ((ret_sig == SIGCHLD) && (old_sig.sa_handler != SIG_DFL) && (old_sig.sa_handler != SIG_IGN))
old_sig.sa_handler(ret);

if (ret == 0)
{
timed_out = Clock::now() >= time_out;
Expand Down

0 comments on commit 0938103

Please sign in to comment.