Salome HOME
USER or LOGNAME is not defined in a Docker container: on Linux get username with...
[modules/kernel.git] / src / Launcher / Launcher_Job.cxx
index 81fd5fc2cf94b407cd2cc0ceba7a4b5822b672de..b724958185a52363ba6549f74f733231b1e643f5 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2009-2019  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2009-2022  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -22,6 +22,9 @@
 //#define _DEBUG_
 #include "Launcher_Job.hxx"
 #include "Launcher.hxx"
+
+#include <Basics_DirUtils.hxx>
+
 #include <boost/filesystem.hpp>
 
 #ifdef WITH_LIBBATCH
@@ -34,6 +37,7 @@
 #include <process.h>
 #else
   static const char SEPARATOR = '/';
+#include <pwd.h>
 #endif
 
 Launcher::Job::Job()
@@ -188,7 +192,12 @@ Launcher::Job::setResourceDefinition(const ParserResourcesType & resource_defini
   if (resource_definition.UserName == "")
   {
 #ifndef WIN32
-    user_name = getenv("USER");
+    struct passwd *pwd = getpwuid(getuid());
+    if (pwd) {
+      user_name = std::string(pwd->pw_name);
+    }
+    if (user_name == "")
+      user_name = getenv("USER");
 #else
     user_name = getenv("USERNAME");
 #endif
@@ -667,11 +676,8 @@ Launcher::Job::common_job_params()
 
   // If result_directory is not defined, we use HOME environment
   if (_result_directory == "")
-#ifndef WIN32
-    _result_directory = getenv("HOME");
-#else
-    _result_directory = getenv("USERPROFILE");
-#endif
+    _result_directory = Kernel_Utils::HomePath();
+
   // _in_files
   std::list<std::string> in_files(_in_files);
   in_files.push_back(_job_file);