#include <dlfcn.h>
+#include <signal.h>
#include <string>
#include <fstream>
#include <vector>
+#include <sstream>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
+#include <linux/limits.h>
+
+#include <storage.h>
+#include <app_common.h>
#include "utils.h"
#include "log.h"
#include "launcher.h"
#include "dotnet_launcher.h"
+#include "plugin_manager.h"
+#include "path_manager.h"
+#include "log_manager.h"
#define PLUGIN_PATH "/usr/share/dotnet.tizen/lib/libdotnet_plugin.so"
+#define ENV_FILE_PATH "/usr/share/dotnet.tizen/lib/coreclr_env.list"
namespace tizen {
namespace runtime {
#elif defined (__arm__)
#define ARCHITECTURE_IDENTIFIER "arm"
const static std::vector<std::string> RID_FALLBACK_GRAPH =
- {"tizen.4.0.0-armel", "tizen.4.0.0", "tizen-armel", "tizen", "linux-armel", "linux", "unix-armel", "unix", "any", "base"};
+ {"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"};
#elif defined (__x86_64__)
#define ARCHITECTURE_IDENTIFIER "x64"
#elif defined (__i386__)
#define ARCHITECTURE_IDENTIFIER "x86"
const static std::vector<std::string> RID_FALLBACK_GRAPH =
- {"linux-x86", "linux", "unix-x86", "unix", "any", "base"};
+ {"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"
return candidate;
}
+
+static std::vector<std::string> __envList;
+
+static void setEnvFromFile()
+{
+ std::string envList;
+ std::ifstream inFile(ENV_FILE_PATH);
+
+ __envList.clear();
+
+ if (inFile) {
+ _INFO("coreclr_env.list is found");
+ inFile >> envList;
+
+ std::istringstream ss(envList);
+ std::string token;
+
+ while (std::getline(ss, token, ':')) {
+ if (!token.empty()) {
+ __envList.push_back(token);
+ }
+ }
+
+ for (unsigned int i = 0; i < __envList.size(); i++) {
+ putenv(const_cast<char *>(__envList[i].c_str()));
+ }
+ } else {
+ _INFO("coreclr_env.list file is not found. skip");
+ }
+}
+
+#define _unused(x) ((void)(x))
+
+struct sigaction sig_abrt_new;
+struct sigaction sig_abrt_old;
+
+static bool checkOnSigabrt = false;
+static bool checkOnTerminate = false;
+
+static void onSigabrt(int signum)
+{
+ // use unused variable to avoid build warning
+ ssize_t ret = write(STDERR_FILENO, "onSigabrt called\n", 17);
+
+ if (checkOnTerminate) {
+ ret = write(STDERR_FILENO, "onSigabrt called while terminate. go to exit\n", 45);
+ _unused(ret);
+ exit(0);
+ }
+
+ if (checkOnSigabrt) {
+ ret = write(STDERR_FILENO, "onSigabrt called again. go to exit\n", 35);
+ _unused(ret);
+ 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) {
+ ret = write(STDERR_FILENO, "Fail to raise SIGABRT\n", 22);
+ }
+ } else {
+ ret = write(STDERR_FILENO, "Fail to set original SIGABRT handler\n", 37);
+ }
+ _unused(ret);
+}
+
+static void registerSigHandler()
+{
+ sig_abrt_new.sa_handler = onSigabrt;
+ if (sigemptyset(&sig_abrt_new.sa_mask) != 0) {
+ _ERR("Fail to sigemptyset");
+ }
+
+ if (sigaction(SIGABRT, &sig_abrt_new, &sig_abrt_old) < 0) {
+ _ERR("Fail to add sig handler");
+ }
+}
+
+static bool storage_cb(int id, storage_type_e type, storage_state_e state, const char *path, void *user_data)
+{
+ int* tmp = (int*)user_data;
+ if (type == STORAGE_TYPE_INTERNAL)
+ {
+ *tmp = id;
+ return false;
+ }
+
+ return true;
+}
+
+static void initEnvForSpecialFolder()
+{
+ int storageId;
+ int error;
+ char *path = NULL;
+
+ error = storage_foreach_device_supported(storage_cb, &storageId);
+ if (error != STORAGE_ERROR_NONE) {
+ return;
+ }
+
+ error = storage_get_directory(storageId, STORAGE_DIRECTORY_IMAGES, &path);
+ if (error == STORAGE_ERROR_NONE && path != NULL) {
+ setenv("XDG_PICTURES_DIR", const_cast<char *>(path), 1);
+ free(path);
+ path = NULL;
+ }
+
+ error = storage_get_directory(storageId, STORAGE_DIRECTORY_MUSIC, &path);
+ if (error == STORAGE_ERROR_NONE && path != NULL) {
+ setenv("XDG_MUSIC_DIR", const_cast<char *>(path), 1);
+ free(path);
+ path = NULL;
+ }
+
+ error = storage_get_directory(storageId, STORAGE_DIRECTORY_VIDEOS, &path);
+ if (error == STORAGE_ERROR_NONE && path != NULL) {
+ setenv("XDG_VIDEOS_DIR", const_cast<char *>(path), 1);
+ free(path);
+ path = NULL;
+ }
+}
+
CoreRuntime::CoreRuntime(const char* mode) :
initializeClr(nullptr),
executeAssembly(nullptr),
shutdown(nullptr),
createDelegate(nullptr),
+ setEnvironmentVariable(nullptr),
+ multiByteToWideChar(nullptr),
__coreclrLib(nullptr),
__hostHandle(nullptr),
__domainId(-1),
- preparedFunction(nullptr),
- launchFunction(nullptr),
- __pluginLib(nullptr),
- pluginInitialize(nullptr),
- pluginPreload(nullptr),
- pluginSetAppInfo(nullptr),
- pluginSetCoreclrInfo(nullptr),
- pluginGetDllPath(nullptr),
- pluginBeforeExecute(nullptr),
- pluginFinalize(nullptr),
fd(0),
- __mode(mode)
+ __initialized(false)
{
-#define __XSTR(x) #x
-#define __STR(x) __XSTR(x)
-
-#ifdef DEVICE_API_DIR
- __deviceAPIDirectory = __STR(DEVICE_API_DIR);
-#endif
-#ifdef RUNTIME_DIR
- __runtimeDirectory = __STR(RUNTIME_DIR);
-#endif
-#ifdef NATIVE_LIB_DIR
- __nativeLibDirectory = __STR(NATIVE_LIB_DIR);
-#endif
-
-#undef __STR
-#undef __XSTR
-
- // support launcher plugin
- if (!fileNotExist(PLUGIN_PATH)) {
- __pluginLib = dlopen(PLUGIN_PATH, RTLD_NOW | RTLD_LOCAL);
- if (__pluginLib) {
- pluginInitialize = (plugin_initialize_ptr)dlsym(__pluginLib, "plugin_initialize");
- pluginPreload = (plugin_preload_ptr)dlsym(__pluginLib, "plugin_preload");
- pluginSetAppInfo = (plugin_set_app_info_ptr)dlsym(__pluginLib, "plugin_set_app_info");
- pluginSetCoreclrInfo = (plugin_set_coreclr_info_ptr)dlsym(__pluginLib, "plugin_set_coreclr_info");
- pluginGetDllPath = (plugin_get_dll_path_ptr)dlsym(__pluginLib, "plugin_get_dll_path");
- pluginBeforeExecute = (plugin_before_execute_ptr)dlsym(__pluginLib, "plugin_before_execute");
- pluginFinalize = (plugin_finalize_ptr)dlsym(__pluginLib, "plugin_finalize");
- }
+ _INFO("Constructor called!!");
+
+ // 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.
+ // So, plugin initialize should be called before mainloop start.
+ if (initializePluginManager(mode) < 0) {
+ _ERR("Failed to initialize PluginManager");
}
- if (pluginInitialize)
- pluginInitialize(mode);
-
- _DBG("Constructor called!!");
+ if (pluginHasLogControl()) {
+ __enableLogManager = false;
+ } else {
+ __enableLogManager = true;
+ }
}
CoreRuntime::~CoreRuntime()
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
+
#ifdef __arm__
// libunwind library is used to unwind stack frame, but libunwind for ARM
// does not support ARM vfpv3/NEON registers in DWARF format correctly.
putenv(const_cast<char *>("UNW_ARM_UNWIND_METHOD=6"));
#endif // __arm__
- if (__deviceAPIDirectory.empty()) {
- _ERR("Empty Device API Directory");
- return 1;
- } else {
- __deviceAPIDirectory = absolutePath(__deviceAPIDirectory);
- }
+ // Disable debug pipes and semaphores creation in case of non-standlone mode
+ if (!standalone)
+ putenv(const_cast<char *>("COMPlus_EnableDiagnostics=0"));
- if (__runtimeDirectory.empty()) {
- _ERR("Empty Runtime Directory");
- return 1;
- } else {
- __runtimeDirectory = absolutePath(__runtimeDirectory);
+ // read string from external file and set them to environment value.
+ setEnvFromFile();
+
+ // Set environment for System.Environment.SpecialFolder
+ initEnvForSpecialFolder();
+
+ if (initializePathManager(std::string(), std::string(), std::string()) < 0) {
+ _ERR("Failed to initialize PathManager");
+ return -1;
}
- // set Reference API directory
- __refAPIDirectory = __deviceAPIDirectory + "/ref";
+ if (__enableLogManager) {
+ if (initializeLogManager() < 0) {
+ _ERR("Failed to initnialize LogManager");
+ return -1;
+ }
+
+ if (redirectFD() < 0) {
+ _ERR("Failed to redirect FD");
+ return -1;
+ }
- std::string libCoreclr(concatPath(__runtimeDirectory, "libcoreclr.so"));
+ if (runLoggingThread() < 0) {
+ _ERR("Failed to create and run logging thread to redicrect log");
+ return -1;
+ }
+ }
- _DBG("libcoreclr : %s", libCoreclr.c_str());
+ 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);
- return 1;
+ return -1;
}
#define CORELIB_RETURN_IF_NOSYM(type, variable, name) \
variable = (type)dlsym(__coreclrLib, name); \
if (variable == nullptr) { \
_ERR(name " is not found in the libcoreclr.so"); \
- return 1; \
+ return -1; \
} \
} while (0)
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");
+ CORELIB_RETURN_IF_NOSYM(multi_byte_to_wide_char_ptr, multiByteToWideChar, "MultiByteToWideChar");
#undef CORELIB_RETURN_IF_NOSYM
- _DBG("libcoreclr dlopen and dlsym success");
+ _INFO("libcoreclr dlopen and dlsym success");
- if (!standalone && pluginPreload)
+ if (!standalone)
pluginPreload();
fd = open("/proc/self", O_DIRECTORY);
- std::string path_tmp = std::string("/proc/self/fd/") + std::to_string(fd);
-
- std::string appRoot = path_tmp;
+ 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 probePath = appBin + ":" + appLib;
-
- std::string tpa;
- std::vector<std::string> searchDirectories;
- searchDirectories.push_back(__runtimeDirectory);
- searchDirectories.push_back(__deviceAPIDirectory);
- searchDirectories.push_back(__refAPIDirectory);
-
- if (pluginGetDllPath) {
- std::string pluginPath = pluginGetDllPath();
- if (!pluginPath.empty()) {
- splitPath(pluginPath, searchDirectories);
- }
- }
-
- assembliesInDirectory(searchDirectories, tpa);
-
- std::string nativeLibPath;
- nativeLibPath = getExtraNativeLibDirs(appRoot) + ":" + appBin + ":" + appLib + ":" + __nativeLibDirectory;
-
+ std::string tpa = getTPA();
+ std::string nativeLibPath = getExtraNativeLibDirs(appRoot) + ":" + appBin + ":" + appLib + ":" + __nativeLibDirectory;
std::string appName = std::string("dotnet-launcher-") + std::to_string(getpid());
+
if (!initializeCoreClr(appName.c_str(), probePath.c_str(), nativeLibPath.c_str(), tpa.c_str())) {
_ERR("Failed to initialize coreclr");
- return 1;
+ return -1;
}
+ __initialized = true;
+
+ _INFO("CoreRuntime initialize success");
+
return 0;
}
return false;
}
- if (pluginSetCoreclrInfo)
- pluginSetCoreclrInfo(__hostHandle, __domainId, createDelegate);
+ pluginSetCoreclrInfo(__hostHandle, __domainId, createDelegate);
- _DBG("Initialize core clr success");
+ _INFO("Initialize core clr success");
return true;
}
void CoreRuntime::dispose()
{
+ // call plugin finalize function to notify finalize to plugin
+ // dlclose shoud be done after coreclr shutdown to avoid breaking signal chain
+ pluginFinalize();
+
+ // ignore the signal generated by an exception that occurred during shutdown
+ checkOnTerminate = true;
+
if (__hostHandle != nullptr) {
int st = shutdown(__hostHandle, __domainId);
if (st < 0)
_ERR("shutdown core clr fail! (0x%08x)", st);
+ __hostHandle = nullptr;
}
- if (dlclose(__coreclrLib) != 0)
- _ERR("libcoreclr.so close failed");
-
- __coreclrLib = nullptr;
+ if (__coreclrLib != nullptr) {
+ if (dlclose(__coreclrLib) != 0) {
+ _ERR("libcoreclr.so close failed");
+ }
- if (pluginFinalize)
- pluginFinalize();
+ __coreclrLib = nullptr;
+ }
- if (__pluginLib != nullptr) {
- if (dlclose(__pluginLib) != 0)
- _ERR("libdotnet_plugin.so close failed");
+ finalizePluginManager();
+ finalizePathManager();
- __pluginLib = nullptr;
- pluginInitialize = nullptr;
- pluginPreload = nullptr;
- pluginSetAppInfo = nullptr;
- pluginSetCoreclrInfo = nullptr;
- pluginGetDllPath = nullptr;
- pluginBeforeExecute = nullptr;
- pluginFinalize = nullptr;
- }
+ __envList.clear();
- _DBG("Dotnet runtime disposed");
+ _INFO("Dotnet runtime disposed");
}
int CoreRuntime::launch(const char* appId, const char* root, const char* path, int argc, char* argv[])
{
- if (runLoggingThread() < 0) {
- _ERR("Failed to create logging thread");
+ if (!__initialized) {
+ _ERR("Runtime is not initialized");
+ return -1;
}
if (path == nullptr) {
_ERR("executable path is null");
- return 1;
+ return -1;
}
- if (fileNotExist(path)) {
+ if (!isFileExist(path)) {
_ERR("File not exist : %s", path);
- return 1;
+ 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;
+ }
+
+ registerSigHandler();
}
- if (pluginSetAppInfo)
- pluginSetAppInfo(appId, path);
+ pluginSetAppInfo(appId, path);
int fd2 = open(root, O_DIRECTORY);
dup3(fd2, fd, O_CLOEXEC);
if (fd2 >= 0)
close(fd2);
- if (pluginBeforeExecute)
- pluginBeforeExecute();
+ // set application data path to coreclr environment.
+ // 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) {
+ char16_t envval[PATH_MAX] = {0};
+ int copied = multiByteToWideChar(0 /* CP_ACP */, 0, localDataPath, -1, envval, PATH_MAX);
+ if (copied >= PATH_MAX) {
+ _ERR("Data Path is bigger than PATH_MAX");
+ }
- if (runLoggingThread() < 0) {
- _ERR("Failed to create logging thread");
+ if (!setEnvironmentVariable(u"XDG_DATA_HOME", envval)) {
+ _ERR("Failed to set XDG_DATA_HOME");
+ }
}
+ pluginBeforeExecute();
+
+ _INFO("execute assembly : %s", path);
+
unsigned int ret = 0;
int st = executeAssembly(__hostHandle, __domainId, argc, (const char**)argv, path, &ret);
if (st < 0)