+
+/*
+ * :WARNING: Do not directly convert returned value to std::string
+ * This function may return NULL if env variable is not defined.
+ * And std::string(NULL) causes undefined behavior.
+ * Use GetenvThreadSafeAsString to properly get a std::string.
+*/
+char *SALOME_ContainerManager::GetenvThreadSafe(const char *name)
+{// getenv is not thread safe. See man 7 pthread.
+ Utils_Locker lock (&_getenvMutex);
+ return getenv(name);
+}
+
+/*
+ * Return env variable as a std::string.
+ * Return empty string if env variable is not set.
+ */
+std::string SALOME_ContainerManager::GetenvThreadSafeAsString(const char *name)
+{
+ char* var = GetenvThreadSafe(name);
+ return var ? std::string(var) : std::string();
+}
+
+int SALOME_ContainerManager::SystemThreadSafe(const char *command)
+{
+ Utils_Locker lock (&_systemMutex);
+ return system(command);
+}
+
+long SALOME_ContainerManager::SystemWithPIDThreadSafe(const std::vector<std::string>& command)
+{
+ Utils_Locker lock(&_systemMutex);
+ if(command.size()<1)
+ throw SALOME_Exception("SystemWithPIDThreadSafe : command is expected to have a length of size 1 at least !");
+#ifndef WIN32
+ pid_t pid ( fork() ) ; // spawn a child process, following code is executed in both processes
+#else
+ pid_t pid = -1; //Throw SALOME_Exception on Windows
+#endif
+ if ( pid == 0 ) // I'm a child, replace myself with a new ompi-server
+ {
+ std::size_t sz(command.size());
+ char **args = new char *[sz+1];
+ for(std::size_t i=0;i<sz;i++)
+ args[i] = strdup(command[i].c_str());
+ args[sz] = nullptr;
+ execvp( command[0].c_str() , args );
+ std::ostringstream oss;
+ oss << "Error when launching " << command[0];
+ throw SALOME_Exception(oss.str().c_str()); // execvp failed
+ }
+ else if ( pid < 0 )
+ {
+ throw SALOME_Exception("fork() failed");
+ }
+ else // I'm a parent
+ {
+ return pid;
+ }
+}
+