Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use std::min.
[simgrid.git] / src / smpi / internals / smpi_global.cpp
index 741fc10..ce1c397 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (c) 2007-2020. The SimGrid Team. All rights reserved.          */
+/* Copyright (c) 2007-2021. 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 <array>
 #include <boost/algorithm/string.hpp> /* split */
 #include <boost/tokenizer.hpp>
+#include <cerrno>
 #include <cinttypes>
 #include <cstdint> /* intmax_t */
+#include <cstring> /* strerror */
 #include <dlfcn.h>
 #include <fcntl.h>
 #include <fstream>
@@ -360,8 +362,10 @@ static void smpi_copy_file(const std::string& src, const std::string& target, of
 {
   int fdin = open(src.c_str(), O_RDONLY);
   xbt_assert(fdin >= 0, "Cannot read from %s. Please make sure that the file exists and is executable.", src.c_str());
-  int fdout = open(target.c_str(), O_CREAT | O_RDWR, S_IRWXU);
-  xbt_assert(fdout >= 0, "Cannot write into %s", target.c_str());
+  XBT_ATTRIB_UNUSED int unlink_status = unlink(target.c_str());
+  xbt_assert(unlink_status == 0 || errno == ENOENT, "Failed to unlink file %s: %s", target.c_str(), strerror(errno));
+  int fdout = open(target.c_str(), O_CREAT | O_RDWR | O_EXCL, S_IRWXU);
+  xbt_assert(fdout >= 0, "Cannot write into %s: %s", target.c_str(), strerror(errno));
 
   XBT_DEBUG("Copy %" PRIdMAX " bytes into %s", static_cast<intmax_t>(fdin_size), target.c_str());
 #if SG_HAVE_SENDFILE
@@ -469,9 +473,7 @@ static void smpi_init_privatization_dlopen(const std::string& executable)
 
           // Copy the dynamic library, the new name must be the same length as the old one
           // just replace the name with 7 digits for the rank and the rest of the name.
-          unsigned int pad = 7;
-          if (libname.length() < pad)
-            pad = libname.length();
+          auto pad                   = std::min<unsigned>(7, libname.length());
           std::string target_libname = std::string(pad - std::to_string(rank).length(), '0') + std::to_string(rank) + libname.substr(pad);
           std::string target_lib = simgrid::config::get_value<std::string>("smpi/tmpdir") + "/" + target_libname;
           target_libs.push_back(target_lib);
@@ -493,8 +495,10 @@ static void smpi_init_privatization_dlopen(const std::string& executable)
         for (const std::string& target_lib : target_libs)
           unlink(target_lib.c_str());
       }
-      xbt_assert(handle != nullptr, "dlopen failed: %s (errno: %d -- %s)", dlerror(), saved_errno,
-                 strerror(saved_errno));
+      xbt_assert(handle != nullptr,
+                 "dlopen failed: %s (errno: %d -- %s).\nError: Did you compile the program with a SMPI-specific "
+                 "compiler (spmicc or friends)?",
+                 dlerror(), saved_errno, strerror(saved_errno));
 
       smpi_entry_point_type entry_point = smpi_resolve_function(handle);
       xbt_assert(entry_point, "Could not resolve entry point");