Support hydra mode
[platform/core/dotnet/launcher.git] / NativeLauncher / launcher / dotnet / dotnet_launcher.cc
index 838a5e6..c13c406 100644 (file)
@@ -221,12 +221,6 @@ static void initEnvForSpecialFolder()
        }
 }
 
-static std::u16string utf8ToUtf16(char* str)
-{
-       std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
-       return convert.from_bytes(str);
-}
-
 void CoreRuntime::preloadTypes()
 {
        const static std::string initDllPath = "/usr/share/dotnet.tizen/framework/Tizen.Init.dll";
@@ -262,10 +256,18 @@ CoreRuntime::CoreRuntime(const char* mode) :
        __hostHandle(nullptr),
        __domainId(-1),
        fd(0),
-       __initialized(false)
+       __initialized(false),
+       __isProfileMode(false)
 {
        _INFO("Constructor called!!");
 
+       char *env = nullptr;
+       env = getenv("CORECLR_ENABLE_PROFILING");
+       if (env != nullptr && !strcmp(env, "1")) {
+               _INFO("profiling mode on");
+               __isProfileMode = true;
+       }
+
        // plugin initialize should be called before start loader mainloop.
        // In case of VD plugins, attaching secure zone is done in the plugin_initialize().
        // When attaching to a secure zone, if there is a created thread, it will failed.
@@ -283,10 +285,18 @@ CoreRuntime::CoreRuntime(const char* mode) :
 
 CoreRuntime::~CoreRuntime()
 {
+       // workaround : to prevent crash while process terminate on profiling mode,
+       //              kill process immediately.
+       // see https://github.com/dotnet/coreclr/issues/26687
+       if (__isProfileMode) {
+               _INFO("shutdown process immediately.");
+               _exit(0);
+       }
+
        dispose();
 }
 
-int CoreRuntime::initialize(bool standalone)
+int CoreRuntime::preinitialize(bool standalone)
 {
        // checkInjection checks dotnet-launcher run mode
        // At the moment, this mechanism is used only when the Memory Profiler is started.
@@ -326,6 +336,10 @@ int CoreRuntime::initialize(bool standalone)
        // Write Debug.WriteLine to stderr
        putenv(const_cast<char *>("COMPlus_DebugWriteToStdErr=1"));
 
+#ifdef USE_DEFAULT_BASE_ADDR
+       putenv(const_cast<char *>("COMPlus_UseDefaultBaseAddr=1"));
+#endif // USE_DEFAULT_BASE_ADDR
+
        // read string from external file and set them to environment value.
        setEnvFromFile();
 
@@ -337,29 +351,14 @@ int CoreRuntime::initialize(bool standalone)
                return -1;
        }
 
-       if (__enableLogManager) {
-               if (initializeLogManager() < 0) {
-                       _ERR("Failed to initnialize LogManager");
-                       return -1;
-               }
-
-               if (redirectFD() < 0) {
-                       _ERR("Failed to redirect FD");
-                       return -1;
-               }
-
-               if (runLoggingThread() < 0) {
-                       _ERR("Failed to create and run logging thread to redicrect log");
-                       return -1;
-               }
-       }
-
        std::string libCoreclr(concatPath(getRuntimeDir(), "libcoreclr.so"));
 
        __coreclrLib = dlopen(libCoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
        if (__coreclrLib == nullptr) {
                char *err = dlerror();
                _ERR("dlopen failed to open libcoreclr.so with error %s", err);
+               if (access(libCoreclr.c_str(), R_OK) == -1)
+                       _ERR("access '%s': %s\n", libCoreclr.c_str(), strerror(errno));
                return -1;
        }
 
@@ -376,12 +375,43 @@ int CoreRuntime::initialize(bool standalone)
        CORELIB_RETURN_IF_NOSYM(coreclr_execute_assembly_ptr, executeAssembly, "coreclr_execute_assembly");
        CORELIB_RETURN_IF_NOSYM(coreclr_shutdown_ptr, shutdown, "coreclr_shutdown");
        CORELIB_RETURN_IF_NOSYM(coreclr_create_delegate_ptr, createDelegate, "coreclr_create_delegate");
-       CORELIB_RETURN_IF_NOSYM(set_environment_variable_ptr, setEnvironmentVariable, "SetEnvironmentVariableW");
 
 #undef CORELIB_RETURN_IF_NOSYM
 
        _INFO("libcoreclr dlopen and dlsym success");
 
+       return 0;
+}
+
+int CoreRuntime::initialize(bool standalone)
+{
+#define __XSTR(x) #x
+#define __STR(x) __XSTR(x)
+
+#ifdef NATIVE_LIB_DIR
+       __nativeLibDirectory = __STR(NATIVE_LIB_DIR);
+#endif
+
+#undef __STR
+#undef __XSTR
+
+       if (__enableLogManager) {
+               if (initializeLogManager() < 0) {
+                       _ERR("Failed to initnialize LogManager");
+                       return -1;
+               }
+
+               if (redirectFD() < 0) {
+                       _ERR("Failed to redirect FD");
+                       return -1;
+               }
+
+               if (runLoggingThread() < 0) {
+                       _ERR("Failed to create and run logging thread to redicrect log");
+                       return -1;
+               }
+       }
+
        if (!standalone)
                pluginPreload();
 
@@ -389,10 +419,12 @@ int CoreRuntime::initialize(bool standalone)
        std::string appRoot = std::string("/proc/self/fd/") + std::to_string(fd);
        std::string appBin = concatPath(appRoot, "bin");
        std::string appLib = concatPath(appRoot, "lib");
-       std::string appTAC = concatPath(appBin, ".TAC.Release");
-       std::string NIprobePath = appBin + APP_NI_SUB_DIR + ":" + appLib + APP_NI_SUB_DIR + ":" + appTAC;
+       std::string appTac = concatPath(appBin, TAC_SYMLINK_SUB_DIR);
+       std::string probePath = appBin + ":" + appLib + ":" + appTac;
+       std::string NIprobePath = concatPath(appBin, APP_NI_SUB_DIR) + ":" + concatPath(appLib, APP_NI_SUB_DIR) + ":" + appTac;
        std::string tpa = getTPA();
-       std::string nativeLibPath = getExtraNativeLibDirs(appRoot) + ":" + appBin + ":" + appLib + ":" + __nativeLibDirectory;
+       std::string runtimeDir = getRuntimeDir();
+       std::string nativeLibPath = getExtraNativeLibDirs(appRoot) + ":" + appBin + ":" + appLib + ":" + __nativeLibDirectory + ":" + runtimeDir;
        std::string appName = std::string("dotnet-launcher-") + std::to_string(getpid());
 
        if (!initializeCoreClr(appName.c_str(), probePath.c_str(), NIprobePath.c_str(), nativeLibPath.c_str(), tpa.c_str())) {
@@ -400,6 +432,12 @@ int CoreRuntime::initialize(bool standalone)
                return -1;
        }
 
+       int st = createDelegate(__hostHandle, __domainId, "Dotnet.Launcher", "Dotnet.Launcher.Environment", "SetEnvironmentVariable", (void**)&setEnvironmentVariable);
+       if (st < 0 || setEnvironmentVariable == nullptr) {
+               _ERR("Create delegate for Dotnet.Launcher.dll -> Dotnet.Launcher.Environment -> SetEnvironmentVariable failed (0x%08x)", st);
+               return -1;
+       }
+
        __initialized = true;
 
        if (!standalone)
@@ -526,12 +564,7 @@ int CoreRuntime::launch(const char* appId, const char* root, const char* path, i
        // application data path can be changed by owner. So, we have to set data path just before launching.
        char* localDataPath = app_get_data_path();
        if (localDataPath != nullptr) {
-               std::u16string envval = utf8ToUtf16(localDataPath);
-
-               if (!setEnvironmentVariable(u"XDG_DATA_HOME", envval.c_str())) {
-                       _ERR("Failed to set XDG_DATA_HOME");
-               }
-
+               setEnvironmentVariable("XDG_DATA_HOME", localDataPath);
                free(localDataPath);
        }