Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
move dlopen tmp files to tmp dir instead of executable dir, which could be not writeable.
[simgrid.git] / src / smpi / internals / smpi_global.cpp
index 73413f5..4528d48 100644 (file)
@@ -14,6 +14,7 @@
 #include "src/simix/smx_private.hpp"
 #include "src/smpi/include/smpi_actor.hpp"
 #include "xbt/config.hpp"
+#include "xbt/file.hpp"
 
 #include <algorithm>
 #include <boost/algorithm/string.hpp> /* split */
@@ -195,7 +196,7 @@ void smpi_comm_copy_buffer_callback(simgrid::kernel::activity::CommImpl* comm, v
       (static_cast<char*>(buff) >= smpi_data_exe_start) &&
       (static_cast<char*>(buff) < smpi_data_exe_start + smpi_data_exe_size)) {
     XBT_DEBUG("Privatization : We are copying from a zone inside global memory... Saving data to temp buffer !");
-    smpi_switch_data_segment(comm->src_actor_->iface());
+    smpi_switch_data_segment(comm->src_actor_->get_iface());
     tmpbuff = xbt_malloc(buff_size);
     memcpy_private(tmpbuff, buff, private_blocks);
   }
@@ -204,7 +205,7 @@ void smpi_comm_copy_buffer_callback(simgrid::kernel::activity::CommImpl* comm, v
       ((char*)comm->dst_buff_ >= smpi_data_exe_start) &&
       ((char*)comm->dst_buff_ < smpi_data_exe_start + smpi_data_exe_size)) {
     XBT_DEBUG("Privatization : We are copying to a zone inside global memory - Switch data segment");
-    smpi_switch_data_segment(comm->dst_actor_->iface());
+    smpi_switch_data_segment(comm->dst_actor_->get_iface());
   }
   XBT_DEBUG("Copying %zu bytes from %p to %p", buff_size, tmpbuff, comm->dst_buff_);
   memcpy_private(comm->dst_buff_, tmpbuff, private_blocks);
@@ -404,13 +405,12 @@ static void smpi_copy_file(const std::string& src, const std::string& target, of
 #if not defined(__APPLE__) && not defined(__HAIKU__)
 static int visit_libs(struct dl_phdr_info* info, size_t, void* data)
 {
-  auto* libname    = static_cast<char*>(data);
-  const char *path = info->dlpi_name;
-  if(strstr(path, libname)){
-    strncpy(libname, path, 512);
+  auto* libname    = static_cast<std::string*>(data);
+  std::string path = info->dlpi_name;
+  if (path.find(*libname) != std::string::npos) {
+    *libname = std::move(path);
     return 1;
   }
-  
   return 0;
 }
 #endif
@@ -432,16 +432,15 @@ static void smpi_init_privatization_dlopen(const std::string& executable)
       // load the library once to add it to the local libs, to get the absolute path
       void* libhandle = dlopen(libname.c_str(), RTLD_LAZY);
       // get library name from path
-      char fullpath[512] = {'\0'};
-      strncpy(fullpath, libname.c_str(), 511);
+      std::string fullpath = libname;
 #if not defined(__APPLE__) && not defined(__HAIKU__)
-      xbt_assert(0 != dl_iterate_phdr(visit_libs, fullpath),
-                 "Can't find a linked %s - check your settings in smpi/privatize-libs", fullpath);
-      XBT_DEBUG("Extra lib to privatize '%s' found", fullpath);
+      xbt_assert(0 != dl_iterate_phdr(visit_libs, &fullpath),
+                 "Can't find a linked %s - check your settings in smpi/privatize-libs", fullpath.c_str());
+      XBT_DEBUG("Extra lib to privatize '%s' found", fullpath.c_str());
 #else
       xbt_die("smpi/privatize-libs is not (yet) compatible with OSX nor with Haiku");
 #endif
-      privatize_libs_paths.emplace_back(fullpath);
+      privatize_libs_paths.emplace_back(std::move(fullpath));
       dlclose(libhandle);
     }
   }
@@ -450,8 +449,9 @@ static void smpi_init_privatization_dlopen(const std::string& executable)
     return std::function<void()>([executable, fdin_size, args] {
       static std::size_t rank = 0;
       // Copy the dynamic library:
-      std::string target_executable =
-          executable + "_" + std::to_string(getpid()) + "_" + std::to_string(rank) + ".so";
+      simgrid::xbt::Path path(executable);
+      std::string target_executable = simgrid::config::get_value<std::string>("smpi/tmpdir") + "/" +
+          path.get_base_name() + "_" + std::to_string(getpid()) + "_" + std::to_string(rank) + ".so";
 
       smpi_copy_file(executable, target_executable, fdin_size);
       // if smpi/privatize-libs is set, duplicate pointed lib and link each executable copy to a different one.
@@ -474,7 +474,7 @@ static void smpi_init_privatization_dlopen(const std::string& executable)
           unsigned int pad = 7;
           if (libname.length() < pad)
             pad = libname.length();
-          std::string target_lib =
+          std::string target_lib = simgrid::config::get_value<std::string>("smpi/tmpdir") + "/" +
               std::string(pad - std::to_string(rank).length(), '0') + std::to_string(rank) + libname.substr(pad);
           target_libs.push_back(target_lib);
           XBT_DEBUG("copy lib %s to %s, with size %lld", libpath.c_str(), target_lib.c_str(), (long long)fdin_size2);