Skip to content

Commit

Permalink
Merge pull request #2910 from STEllAR-GROUP/process_error_reporting
Browse files Browse the repository at this point in the history
Improve error reporting for process component on POSIX systems
  • Loading branch information
hkaiser committed Sep 24, 2017
2 parents 95a3e7d + c2ccf42 commit c17edc8
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions hpx/components/process/util/posix/initializers/throw_on_error.hpp
Expand Up @@ -16,32 +16,47 @@
#include <hpx/components/process/util/posix/initializers/initializer_base.hpp>
#include <hpx/runtime/serialization/serialization_fwd.hpp>
#include <hpx/throw_exception.hpp>

#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

#include <cstddef>
#include <string>

namespace hpx { namespace components { namespace process { namespace posix {

namespace initializers {

class throw_on_error : public initializer_base
{
static std::string extract_error_string(int code)
{
constexpr std::size_t const buffer_len = 256;
char buffer[buffer_len+1];
strerror_r(code, buffer, buffer_len);
return buffer;
}

public:
template <class PosixExecutor>
void on_fork_setup(PosixExecutor&) const
{
if (::pipe(fds_) == -1)
{
HPX_THROW_EXCEPTION(kernel_error,
"throw_on_error::on_fork_setup", "pipe(2) failed");
"throw_on_error::on_fork_setup",
"pipe(2) failed: " + extract_error_string(errno));
}
if (::fcntl(fds_[1], F_SETFD, FD_CLOEXEC) == -1)
{
::close(fds_[0]);
::close(fds_[1]);

HPX_THROW_EXCEPTION(kernel_error,
"throw_on_error::on_fork_setup", "fcntl(2) failed");
"throw_on_error::on_fork_setup",
"fcntl(2) failed: " + extract_error_string(errno));
}
}

Expand All @@ -52,7 +67,8 @@ class throw_on_error : public initializer_base
::close(fds_[1]);

HPX_THROW_EXCEPTION(kernel_error,
"throw_on_error::on_fork_error", "fork(2) failed");
"throw_on_error::on_fork_error",
"fork(2) failed: " + extract_error_string(errno));
}

template <class PosixExecutor>
Expand All @@ -65,7 +81,8 @@ class throw_on_error : public initializer_base
::close(fds_[0]);

HPX_THROW_EXCEPTION(kernel_error,
"throw_on_error::on_fork_success", "execve(2) failed");
"throw_on_error::on_fork_success",
"execve(2) failed: " + extract_error_string(code));
}
::close(fds_[0]);
}
Expand Down

0 comments on commit c17edc8

Please sign in to comment.