From: Gabriel Corona Date: Fri, 26 Feb 2016 11:12:20 +0000 (+0100) Subject: [mc] Homogeneise throwned system_error X-Git-Tag: v3_13~657 X-Git-Url: http://info.iut-bm.univ-fcomte.fr/pub/gitweb/simgrid.git/commitdiff_plain/9f9eb7fc2dcaeb4e7bdf6b0aaf76fe713e370746 [mc] Homogeneise throwned system_error * the same category was not used in all calls; * sometimes a pointer to a `new`-ed `system_error` was wrongly used. --- diff --git a/include/xbt/system_error.hpp b/include/xbt/system_error.hpp new file mode 100644 index 0000000000..02f508990e --- /dev/null +++ b/include/xbt/system_error.hpp @@ -0,0 +1,33 @@ +/* Copyright (c) 2016. The SimGrid Team. + * All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include + +#include + +namespace simgrid { +namespace xbt { + +inline +const std::error_category& errno_category() noexcept +{ + return std::generic_category(); +} + +inline +std::system_error errno_error(int errnum) noexcept +{ + return std::system_error(errnum, errno_category()); +} + +inline +std::system_error errno_error(int errno, const char* what) +{ + return std::system_error(errnum, errno_category(), what); +} + +} +} diff --git a/src/mc/ModelChecker.cpp b/src/mc/ModelChecker.cpp index 1bbf9e6440..da9a2451af 100644 --- a/src/mc/ModelChecker.cpp +++ b/src/mc/ModelChecker.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include "simgrid/sg_config.h" @@ -83,7 +84,7 @@ void ModelChecker::start() sigemptyset(&set); sigaddset(&set, SIGCHLD); if (sigprocmask(SIG_BLOCK, &set, nullptr) == -1) - throw std::system_error(errno, std::system_category()); + throw simgrid::xbt::errno_error(errno); sigset_t full_set; sigfillset(&full_set); @@ -97,7 +98,7 @@ void ModelChecker::start() int signal_fd = signalfd(-1, &set, 0); if (signal_fd == -1) - throw std::system_error(errno, std::system_category()); + throw simgrid::xbt::errno_error(errno); struct pollfd* signalfd_pollfd = &fds_[SIGNAL_FD_INDEX]; signalfd_pollfd->fd = signal_fd; @@ -178,7 +179,7 @@ void ModelChecker::resume(simgrid::mc::Process& process) { int res = process.send_message(MC_MESSAGE_CONTINUE); if (res) - throw std::system_error(res, std::system_category()); + throw simgrid::xbt::errno_error(res); process.cache_flags = (mc_process_cache_flags_t) 0; } @@ -189,7 +190,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 std::system_error(error, std::system_category()); + throw simgrid::xbt::errno_error(errno); } bool ModelChecker::handle_message(char* buffer, ssize_t size) @@ -307,7 +308,7 @@ bool ModelChecker::handle_events() case EINTR: continue; default: - throw std::system_error(errno, std::system_category()); + throw simgrid::mc::errno_error(errno); } } @@ -315,7 +316,7 @@ bool ModelChecker::handle_events() if (socket_pollfd->revents & POLLIN) { ssize_t size = MC_receive_message(socket_pollfd->fd, buffer, sizeof(buffer), MSG_DONTWAIT); if (size == -1 && errno != EAGAIN) - throw std::system_error(errno, std::system_category()); + throw simgrid::mc::errno_error(errno); return handle_message(buffer, size); } if (socket_pollfd->revents & POLLERR) { @@ -356,7 +357,7 @@ void ModelChecker::handle_signals() if (errno == EINTR) continue; else - throw std::system_error(errno, std::system_category()); + throw simgrid::mc::errno_error(errno); } else if (size != sizeof(info)) return throw std::runtime_error( "Bad communication with model-checked application"); @@ -381,7 +382,7 @@ void ModelChecker::handle_waitpid() break; } else { XBT_ERROR("Could not wait for pid"); - throw std::system_error(errno, std::system_category()); + throw simgrid::mc::errno_error(errno); } } diff --git a/src/mc/simgrid_mc.cpp b/src/mc/simgrid_mc.cpp index f6e998d825..99a6abdc7e 100644 --- a/src/mc/simgrid_mc.cpp +++ b/src/mc/simgrid_mc.cpp @@ -48,7 +48,7 @@ pid_t do_fork(F f) { pid_t pid = fork(); if (pid < 0) - throw new std::system_error(errno, std::generic_category()); + throw simgrid::mc::errno_error(errno, "Could not fork model-checked process"); if (pid != 0) return pid; @@ -73,19 +73,17 @@ int exec_model_checked(int socket, char** argv) sigset_t mask; sigemptyset (&mask); if (sigprocmask(SIG_SETMASK, &mask, nullptr) < 0) - throw new std::system_error(errno, std::generic_category(), "sigprocmask"); + throw simgrid::mc::errno_error(errno, "Could not unblock signals"); if (prctl(PR_SET_PDEATHSIG, SIGHUP) != 0) - throw new std::system_error(errno, std::generic_category(), "PR_SET_PDEATHSIG"); + throw simgrid::mc::errno_error(errno, "Could not PR_SET_PDEATHSIG"); #endif int res; // Remove CLOEXEC in order to pass the socket to the exec-ed program: int fdflags = fcntl(socket, F_GETFD, 0); - if (fdflags == -1) - throw new std::system_error(errno, std::generic_category(), "F_GETFD"); - if (fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) == -1) - throw new std::system_error(errno, std::generic_category(), "Remove FD_CLOEXEC"); + if (fdflags == -1 || fcntl(socket, F_SETFD, fdflags & ~FD_CLOEXEC) == -1) + throw simgrid::xbt::system_error(errno, "Could not remove CLOEXEC for socket"); // Set environment: setenv(MC_ENV_VARIABLE, "1", 1); @@ -118,7 +116,7 @@ std::pair create_model_checked(char** argv) int sockets[2]; res = socketpair(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0, sockets); if (res == -1) - throw new std::system_error(errno, std::generic_category(), "socketpair"); + throw simgrid::xbt::system_error(errno, "Could not create socketpair"); pid_t pid = do_fork([&] { close(sockets[1]); diff --git a/tools/cmake/DefinePackages.cmake b/tools/cmake/DefinePackages.cmake index 5f512d4db8..726c2476e7 100644 --- a/tools/cmake/DefinePackages.cmake +++ b/tools/cmake/DefinePackages.cmake @@ -702,6 +702,7 @@ set(headers_to_install include/xbt/swag.h include/xbt/synchro_core.h include/xbt/sysdep.h + include/xbt/system_error.hpp include/xbt/virtu.h include/xbt/xbt_os_thread.h include/xbt/xbt_os_time.h