From: Gabriel Corona Date: Mon, 18 Apr 2016 12:53:48 +0000 (+0200) Subject: Clear errno when throwing an errno exception X-Git-Tag: v3_13~73 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/4bd2cbe6c40e96efdbf49550cc66bb9e35df8b94 Clear errno when throwing an errno exception --- diff --git a/include/xbt/system_error.hpp b/include/xbt/system_error.hpp index 4a2ae7ae44..4b0bbf21fd 100644 --- a/include/xbt/system_error.hpp +++ b/include/xbt/system_error.hpp @@ -43,6 +43,15 @@ std::error_code errno_code(int errnum) return std::error_code(errnum, errno_category()); } +/** Create an `error_code` from `errno` (and clear it) */ +inline +std::error_code errno_code() +{ + int errnum = errno; + errno = 0; + return errno_code(errnum); +} + /** Create a `system_error` from an `errno` value * * This is expected to to whatever is right to create a @@ -60,6 +69,19 @@ std::system_error errno_error(int errnum, const char* what) return std::system_error(errno_code(errnum), what); } +/** Create a `system_code` from `errno` (and clear it) */ +inline +std::system_error errno_error() +{ + return std::system_error(errno_code()); +} + +inline +std::system_error errno_error(const char* what) +{ + return std::system_error(errno_code(), what); +} + } } diff --git a/src/mc/ModelChecker.cpp b/src/mc/ModelChecker.cpp index 0b7eadf16a..956f5f13d1 100644 --- a/src/mc/ModelChecker.cpp +++ b/src/mc/ModelChecker.cpp @@ -82,7 +82,7 @@ void ModelChecker::start() sigemptyset(&set); sigaddset(&set, SIGCHLD); if (sigprocmask(SIG_BLOCK, &set, nullptr) == -1) - throw simgrid::xbt::errno_error(errno); + throw simgrid::xbt::errno_error(); sigset_t full_set; sigfillset(&full_set); @@ -96,7 +96,7 @@ void ModelChecker::start() int signal_fd = signalfd(-1, &set, 0); if (signal_fd == -1) - throw simgrid::xbt::errno_error(errno); + throw simgrid::xbt::errno_error(); struct pollfd* signalfd_pollfd = &fds_[SIGNAL_FD_INDEX]; signalfd_pollfd->fd = signal_fd; @@ -170,7 +170,7 @@ void ModelChecker::resume(simgrid::mc::Process& process) { int res = process.getChannel().send(MC_MESSAGE_CONTINUE); if (res) - throw simgrid::xbt::errno_error(res); + throw simgrid::xbt::errno_error(); process.clear_cache(); } @@ -181,7 +181,7 @@ void throw_socket_error(int fd) socklen_t errlen = sizeof(error); if (getsockopt(fd, SOL_SOCKET, SO_ERROR, (void *)&error, &errlen) == -1) error = errno; - throw simgrid::xbt::errno_error(errno); + throw simgrid::xbt::errno_error(); } static void MC_report_crash(int status) @@ -333,7 +333,7 @@ bool ModelChecker::handle_events() case EINTR: continue; default: - throw simgrid::xbt::errno_error(errno); + throw simgrid::xbt::errno_error(); } } @@ -341,7 +341,7 @@ bool ModelChecker::handle_events() if (socket_pollfd->revents & POLLIN) { ssize_t size = process_->getChannel().receive(buffer, sizeof(buffer), false); if (size == -1 && errno != EAGAIN) - throw simgrid::xbt::errno_error(errno); + throw simgrid::xbt::errno_error(); return handle_message(buffer, size); } if (socket_pollfd->revents & POLLERR) @@ -380,7 +380,7 @@ void ModelChecker::handle_signals() if (errno == EINTR) continue; else - throw simgrid::xbt::errno_error(errno); + throw simgrid::xbt::errno_error(); } else if (size != sizeof(info)) return throw std::runtime_error( "Bad communication with model-checked application"); @@ -405,7 +405,7 @@ void ModelChecker::handle_waitpid() break; } else { XBT_ERROR("Could not wait for pid"); - throw simgrid::xbt::errno_error(errno); + throw simgrid::xbt::errno_error(); } } diff --git a/src/mc/Session.cpp b/src/mc/Session.cpp index 99696652ab..095d4b05dc 100644 --- a/src/mc/Session.cpp +++ b/src/mc/Session.cpp @@ -32,9 +32,9 @@ static void setup_child_environment(int socket) sigset_t mask; sigemptyset (&mask); if (sigprocmask(SIG_SETMASK, &mask, nullptr) < 0) - throw simgrid::xbt::errno_error(errno, "Could not unblock signals"); + throw simgrid::xbt::errno_error("Could not unblock signals"); if (prctl(PR_SET_PDEATHSIG, SIGHUP) != 0) - throw simgrid::xbt::errno_error(errno, "Could not PR_SET_PDEATHSIG"); + throw simgrid::xbt::errno_error("Could not PR_SET_PDEATHSIG"); #endif int res; @@ -42,7 +42,7 @@ static void setup_child_environment(int socket) // Remove CLOEXEC in order to pass the socket to the exec-ed program: int fdflags = fcntl(socket, F_GETFD, 0); if (fdflags == -1 || fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) == -1) - throw simgrid::xbt::errno_error(errno, "Could not remove CLOEXEC for socket"); + throw simgrid::xbt::errno_error("Could not remove CLOEXEC for socket"); // Set environment: setenv(MC_ENV_VARIABLE, "1", 1); @@ -66,7 +66,7 @@ pid_t do_fork(F code) { pid_t pid = fork(); if (pid < 0) - throw simgrid::xbt::errno_error(errno, "Could not fork model-checked process"); + throw simgrid::xbt::errno_error("Could not fork model-checked process"); if (pid != 0) return pid; @@ -141,7 +141,7 @@ Session* Session::fork(std::function code) int sockets[2]; res = socketpair(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockets); if (res == -1) - throw simgrid::xbt::errno_error(errno, "Could not create socketpair"); + throw simgrid::xbt::errno_error("Could not create socketpair"); pid_t pid = do_fork([&] { ::close(sockets[1]);