Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Homogeneise throwned system_error
authorGabriel Corona <gabriel.corona@loria.fr>
Fri, 26 Feb 2016 11:12:20 +0000 (12:12 +0100)
committerGabriel Corona <gabriel.corona@loria.fr>
Fri, 26 Feb 2016 11:15:40 +0000 (12:15 +0100)
 * the same category was not used in all calls;

 * sometimes a pointer to a `new`-ed `system_error` was wrongly used.

include/xbt/system_error.hpp [new file with mode: 0644]
src/mc/ModelChecker.cpp
src/mc/simgrid_mc.cpp
tools/cmake/DefinePackages.cmake

diff --git a/include/xbt/system_error.hpp b/include/xbt/system_error.hpp
new file mode 100644 (file)
index 0000000..02f5089
--- /dev/null
@@ -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 <cerrno>
+
+#include <system_error>
+
+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);
+}
+
+}
+}
index 1bbf9e6..da9a245 100644 (file)
@@ -19,6 +19,7 @@
 #include <xbt/log.h>
 #include <xbt/automaton.h>
 #include <xbt/automaton.hpp>
+#include <xbt/system_error.hpp>
 
 #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);
       }
     }
 
index f6e998d..99a6abd 100644 (file)
@@ -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<pid_t, int> 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]);
index 5f512d4..726c247 100644 (file)
@@ -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