Skip to content

Commit

Permalink
Improve error reporting for process component on POSIX systems
Browse files Browse the repository at this point in the history
  • Loading branch information
hkaiser committed Sep 20, 2017
1 parent 0bc6e4e commit 0f4b982
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions hpx/components/process/util/posix/initializers/throw_on_error.hpp
Expand Up @@ -16,32 +16,46 @@
#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 <string>

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

namespace initializers {

class throw_on_error : public initializer_base
{
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 +66,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 +80,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 0f4b982

Please sign in to comment.