call ecore_init() in the CoreRuntime constructor.
[platform/core/dotnet/launcher.git] / NativeLauncher / launcher / dotnet / dotnet_launcher.cc
index 58092f7..e6d3bc7 100644 (file)
 #include <linux/limits.h>
 
 #include <storage.h>
+#include <vconf.h>
 #include <app_common.h>
 
+#include <Ecore.h>
+
 #include "injection.h"
 #include "utils.h"
 #include "log.h"
 #include "path_manager.h"
 #include "log_manager.h"
 
+#define __XSTR(x) #x
+#define __STR(x) __XSTR(x)
+static const char* __FRAMEWORK_DIR = __STR(FRAMEWORK_DIR);
+#undef __STR
+#undef __XSTR
+
 namespace tizen {
 namespace runtime {
 namespace dotnetcore {
 
 #if defined (__aarch64__)
 #define ARCHITECTURE_IDENTIFIER "arm64"
-const static std::vector<std::string> RID_FALLBACK_GRAPH =
-       {"linux-arm64", "linux", "unix-arm64", "unix", "any", "base"};
 
 #elif defined (__arm__)
-#define ARCHITECTURE_IDENTIFIER "arm"
-const static std::vector<std::string> RID_FALLBACK_GRAPH =
-       {"tizen.5.0.0-armel", "tizen.5.0.0", "tizen.4.0.0-armel", "tizen.4.0.0", "tizen-armel", "tizen", "linux-armel", "linux", "unix-armel", "unix", "any", "base"};
+#define ARCHITECTURE_IDENTIFIER "armel"
 
 #elif defined (__x86_64__)
 #define ARCHITECTURE_IDENTIFIER "x64"
-const static std::vector<std::string> RID_FALLBACK_GRAPH =
-       {"linux-x64", "linux", "unix-x64", "unix", "any", "base"};
 
 #elif defined (__i386__)
 #define ARCHITECTURE_IDENTIFIER "x86"
-const static std::vector<std::string> RID_FALLBACK_GRAPH =
-       {"tizen.5.0.0-x86", "tizen.5.0.0", "tizen.4.0.0-x86", "tizen.4.0.0", "tizen-x86", "tizen", "linux-x86", "linux", "unix-x86", "unix", "any", "base"};
 
 #else
 #error "Unknown target"
 #endif
 
+static const char* __TIZEN_RID_VERSION_KEY = "db/dotnet/tizen_rid_version";
+
+// The sequence of RID_FALLBACK graphs must be:
+// 1. Tizen + Version + Architecture
+// 2. Tizen + Version
+// 3. OS(tizen, linux, unix) + Architecture
+// 4. OS(tizen, linux, unix)
+// 5. any, base
 static std::string getExtraNativeLibDirs(const std::string& appRoot)
 {
+       std::vector<std::string> RID_FALLBACK_GRAPH;
+       std::vector<std::string> RID_FALLBACK_TIZEN;
+       char* tizen_rid = vconf_get_str(__TIZEN_RID_VERSION_KEY);
+       if (tizen_rid) {
+               std::vector<std::string> version;
+               splitPath(tizen_rid, version);
+               std::reverse(std::begin(version), std::end(version));
+               for (unsigned int i = 0; i < version.size(); i++) {
+                       RID_FALLBACK_TIZEN.push_back(std::string("tizen." + version[i] + "-" + ARCHITECTURE_IDENTIFIER));
+                       RID_FALLBACK_TIZEN.push_back(std::string("tizen." + version[i]));
+               }
+               free(tizen_rid);
+       }
+
+       std::vector<std::string> RID_FALLBACK_OS = {"tizen", "linux", "unix"};
+       for (unsigned int i = 0; i < RID_FALLBACK_OS.size(); i++) {
+               RID_FALLBACK_GRAPH.push_back(std::string(RID_FALLBACK_OS[i] + "-" +  ARCHITECTURE_IDENTIFIER));
+               RID_FALLBACK_GRAPH.push_back(std::string(RID_FALLBACK_OS[i]));
+       }
+       RID_FALLBACK_GRAPH.push_back("any");
+       RID_FALLBACK_GRAPH.push_back("base");
+
        std::string candidate;
        for (unsigned int i = 0; i < RID_FALLBACK_GRAPH.size(); i++) {
-               if(!candidate.empty()) {
+               if (!candidate.empty()) {
                        candidate += ":";
                }
                candidate += concatPath(appRoot, "bin/runtimes/" + RID_FALLBACK_GRAPH[i] + "/native");
@@ -86,6 +117,8 @@ static std::string getExtraNativeLibDirs(const std::string& appRoot)
        candidate = candidate + ":" + concatPath(appRoot, "lib/" ARCHITECTURE_IDENTIFIER);
        if (!strncmp(ARCHITECTURE_IDENTIFIER, "arm64", 5)) {
                candidate = candidate + ":" + concatPath(appRoot, "lib/aarch64");
+       } else if (!strncmp(ARCHITECTURE_IDENTIFIER, "armel", 5)) {
+               candidate = candidate + ":" + concatPath(appRoot, "lib/arm");
        }
 
        return candidate;
@@ -103,12 +136,9 @@ static void setEnvFromFile()
 
        if (inFile) {
                _INFO("coreclr_env.list is found");
-               inFile >> envList;
 
-               std::istringstream ss(envList);
                std::string token;
-
-               while (std::getline(ss, token, ':')) {
+               while (std::getline(inFile, token, '\n')) {
                        if (!token.empty()) {
                                __envList.push_back(token);
                        }
@@ -147,12 +177,6 @@ static void onSigabrt(int signum)
                exit(0);
        }
 
-       if (hasException()) {
-               ret = write(STDERR_FILENO, "******************************************************\n", 55);
-               ret = write(STDERR_FILENO, "Unhandled exception is occured. check application code\n", 55);
-               ret = write(STDERR_FILENO, "******************************************************\n", 55);
-       }
-
        checkOnSigabrt = true;
        if (sigaction(SIGABRT, &sig_abrt_old, NULL) == 0) {
                if (raise(signum) < 0) {
@@ -223,7 +247,7 @@ static void initEnvForSpecialFolder()
 
 void CoreRuntime::preloadTypes()
 {
-       const static std::string initDllPath = "/usr/share/dotnet.tizen/framework/Tizen.Init.dll";
+       const static std::string initDllPath = concatPath(__FRAMEWORK_DIR, "Tizen.Init.dll");
        if (!isFileExist(initDllPath)) {
                _ERR("Failed to locate Tizen.Init.dll");
                return;
@@ -252,7 +276,6 @@ CoreRuntime::CoreRuntime(const char* mode) :
        shutdown(nullptr),
        createDelegate(nullptr),
        setEnvironmentVariable(nullptr),
-       preloadAssembly(nullptr),
        __coreclrLib(nullptr),
        __hostHandle(nullptr),
        __domainId(-1),
@@ -262,6 +285,9 @@ CoreRuntime::CoreRuntime(const char* mode) :
 {
        _INFO("Constructor called!!");
 
+       // Intiailize ecore first (signal handlers, etc.) before runtime init.
+       ecore_init();
+
        char *env = nullptr;
        env = getenv("CORECLR_ENABLE_PROFILING");
        if (env != nullptr && !strcmp(env, "1")) {
@@ -276,12 +302,6 @@ CoreRuntime::CoreRuntime(const char* mode) :
        if (initializePluginManager(mode) < 0) {
                _ERR("Failed to initialize PluginManager");
        }
-
-       if (pluginHasLogControl()) {
-               __enableLogManager = false;
-       } else {
-               __enableLogManager = true;
-       }
 }
 
 CoreRuntime::~CoreRuntime()
@@ -297,7 +317,7 @@ CoreRuntime::~CoreRuntime()
        dispose();
 }
 
-int CoreRuntime::preinitialize(bool standalone)
+int CoreRuntime::initialize(bool standalone, bool useDlog)
 {
        // checkInjection checks dotnet-launcher run mode
        // At the moment, this mechanism is used only when the Memory Profiler is started.
@@ -349,6 +369,13 @@ int CoreRuntime::preinitialize(bool standalone)
                return -1;
        }
 
+       if (useDlog && !pluginHasLogControl()) {
+               if (initializeLogManager() < 0) {
+                       _ERR("Failed to initnialize LogManager");
+                       return -1;
+               }
+       }
+
        std::string libCoreclr(concatPath(getRuntimeDir(), "libcoreclr.so"));
 
        __coreclrLib = dlopen(libCoreclr.c_str(), RTLD_NOW | RTLD_LOCAL);
@@ -373,68 +400,14 @@ int CoreRuntime::preinitialize(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(coreclr_preload_assembly_ptr, preloadAssembly, "coreclr_preload_assembly");
 
 #undef CORELIB_RETURN_IF_NOSYM
 
        _INFO("libcoreclr dlopen and dlsym success");
 
-#define PRELOAD_IMAGE(path) \
-       do { \
-               int st = preloadAssembly(path); \
-               if (st != 0) { \
-                       _ERR("preload of %s failed! (0x%08x)", path, st); \
-               } else { \
-                       _DBG("preload of %s succeded", path); \
-               } \
-       } while (0)
-
-       if(!standalone) {
-               std::ifstream preloadList(AOT_PRELOAD_PATH);
-               if (preloadList) {
-                       std::string path;
-                       while (getline(preloadList, path)) {
-                               PRELOAD_IMAGE(absolutePath(path).c_str());
-                       }
-               }
-       }
-
-#undef PRELOAD_IMAGE
-
-       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)
+       if (!standalone) {
                pluginPreload();
+       }
 
        // Set environment for System.Environment.SpecialFolder
        // Below function creates dbus connection by callging storage API.
@@ -467,13 +440,8 @@ int CoreRuntime::initialize(bool standalone)
 
        __initialized = true;
 
-       if (!standalone)
-       {
-               // Preload common managed code.
-               preloadTypes();
-
-               // This redundant call effectively reduces JIT overhead at launch time.
-               setEnvironmentVariable("XDG_DATA_HOME", "/");
+       if (!standalone) {
+               preloadTypes();         // Preload common managed code
        }
 
        _INFO("CoreRuntime initialize success");
@@ -573,14 +541,16 @@ int CoreRuntime::launch(const char* appId, const char* root, const char* path, i
                return -1;
        }
 
-       if (__enableLogManager) {
-               // launchpad override stdout and stderr to journalctl before launch application.
-               // we have to re-override that to input pipe for logging thread.
-               if (redirectFD() < 0) {
-                       _ERR("Failed to redirect FD");
-                       return -1;
-               }
+       // launchpad override stdout and stderr to journalctl before launch application.
+       // we have to re-override that to input pipe for logging thread.
+       // if LogManager is not initialized, below redirectFD will return 0;
+       if (redirectFD() < 0) {
+               _ERR("Failed to redirect FD");
+               return -1;
+       }
 
+       // VD has their own signal handler.
+       if (!pluginHasLogControl()) {
                registerSigHandler();
        }