#include <pkgmgr-info.h>
#include <pkgmgr_installer_info.h>
#include <aul.h>
+#include <tzplatform_config.h>
#include "log.h"
#include "utils.h"
#include "tac_common.h"
#include "path_manager.h"
#include "plugin_manager.h"
+#include "r2r_checker.h"
#ifdef LOG_TAG
#undef LOG_TAG
#endif
#define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
-#ifndef CROSSGEN_PATH
-#error "CROSSGEN_PATH is missed"
-#endif
-
#define __XSTR(x) #x
#define __STR(x) __XSTR(x)
#if defined(__arm__) || defined(__aarch64__)
static const char* __NATIVE_LIB_DIR = __STR(NATIVE_LIB_DIR);
#endif
-static const char* __CROSSGEN_PATH = __STR(CROSSGEN_PATH);
static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
static const char* __READ_ONLY_APP_UPDATE_DIR = __STR(READ_ONLY_APP_UPDATE_DIR);
#undef __STR
#undef __XSTR
+static std::string CORERUN_CMD = "/usr/share/dotnet.tizen/netcoreapp/corerun";
+static std::string CROSSGEN2_PATH = "/usr/share/dotnet.tizen/netcoreapp/crossgen2/crossgen2.dll";
+static std::string CLRJIT_PATH = "/usr/share/dotnet.tizen/netcoreapp/libclrjit.so";
+static const char* CROSSGEN_OPT_JITPATH = "--jitpath";
+static const char* CROSSGEN_OPT_TARGET_ARCH = "--targetarch";
+static const char* CROSSGEN_OPT_OUT_NEAR_INPUT = "--out-near-input";
+static const char* CROSSGEN_OPT_SINGLE_FILE_COMPILATION = "--single-file-compilation";
+//static const char* CROSSGEN_OPT_PARALLELISM = "--parallelism";
+//static const char* CROSSGEN_OPT_PARALLELISM_COUNT = "5";
+static const char* CROSSGEN_OPT_RESILIENT = "--resilient";
+static const char* CROSSGEN_OPT_OPTIMIZE = "-O";
+static const char* CROSSGEN_OPT_INPUTBUBBLE = "--inputbubble";
+static const char* CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS = "--compilebubblegenerics";
+static const char* CROSSGEN_OPT_VERBOSE = "--verbose";
+static std::vector<std::string> REF_VECTOR;
+static std::vector<std::string> INPUTBUBBLE_REF_VECTOR;
+static std::vector<std::string> MIBC_VECTOR;
+
static int __interval = 0;
static PathManager* __pm = nullptr;
-static void waitInterval()
+static NIOption* __ni_option = nullptr;
+
+// singleton
+NIOption* getNIOption()
{
- // by the recommand, ignore small value for performance.
- if (__interval > 10000) {
- fprintf(stdout, "sleep %d usec\n", __interval);
- usleep(__interval);
+ if (__ni_option == nullptr) {
+ __ni_option = (NIOption*)calloc(sizeof(NIOption), 1);
+ if (__ni_option == nullptr) {
+ _SERR("Fail to create NIOption");
+ }
}
+ return __ni_option;
}
-static std::string getNIFilePath(const std::string& dllPath)
+static void waitInterval()
{
- size_t index = dllPath.find_last_of(".");
- if (index == std::string::npos) {
- fprintf(stderr, "File doesnot contain extension. fail to get NI file name\n");
- return "";
+ // by the recommand, ignore small value for performance.
+ if (__interval > 10000) {
+ _SOUT("sleep %d usec", __interval);
+ usleep(__interval);
}
- std::string fName = dllPath.substr(0, index);
- std::string fExt = dllPath.substr(index, dllPath.length());
-
- // crossgen generate file with lower case extension only
- std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
- std::string niPath = fName + ".ni" + fExt;
-
- return niPath;
}
/**
if (!newPath.empty()) {
if (stat(newPath.c_str(), &st) != 0) {
if (mkdir(newPath.c_str(), mode) != 0 && errno != EEXIST) {
- fprintf(stderr, "Fail to create app ni directory (%s)\n", newPath.c_str());
+ _SERR("Fail to create app ni directory (%s)", newPath.c_str());
return false;
}
if (!source.empty()) {
}
} else {
if (!S_ISDIR(st.st_mode)) {
- fprintf(stderr, "Fail. path is not a dir (%s)\n", newPath.c_str());
+ _SERR("Fail. path is not a dir (%s)", newPath.c_str());
return false;
}
}
return true;
}
-static std::string getAppNIFilePath(const std::string& absDllPath, DWORD flags)
+static std::string getNIFilePath(const std::string& dllPath)
+{
+ size_t index = dllPath.find_last_of(".");
+ if (index == std::string::npos) {
+ _SERR("File doesnot contain extension. fail to get NI file name");
+ return "";
+ }
+ std::string fName = dllPath.substr(0, index);
+ std::string fExt = dllPath.substr(index, dllPath.length());
+
+ // crossgen generate file with lower case extension only
+ std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
+ std::string niPath = fName + ".ni" + fExt;
+
+ return niPath;
+}
+
+static std::string getAppNIFilePath(const std::string& absDllPath, NIOption* opt)
{
std::string niDirPath;
std::string prevPath;
prevPath = getBaseName(absDllPath);
niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
- if (flags & NI_FLAGS_READONLY_APP) {
+ if (opt->flags & NI_FLAGS_APP_UNDER_RO_AREA) {
niDirPath = replaceAll(niDirPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
+ _SERR("App is installed in RO area. Change NI path to RW area(%s).", niDirPath.c_str());
+ _ERR("App is installed in RO area. Change NI path to RW area(%s).", niDirPath.c_str());
}
if (!isDirectory(niDirPath)) {
if (!createDirsAndCopyOwnerShip(niDirPath, prevPath)) {
niDirPath = prevPath;
- fprintf(stderr, "fail to create dir (%s)\n", niDirPath.c_str());
+ _SERR("fail to create dir (%s)", niDirPath.c_str());
}
}
return getNIFilePath(concatPath(niDirPath, getFileName(absDllPath)));
}
-static bool checkNIExistence(const std::string& path)
+static bool checkNIExistence(const std::string& absDllPath)
{
- std::string f = getNIFilePath(path);
- if (f.empty()) {
+ std::string absNIPath = getNIFilePath(absDllPath);
+ if (absNIPath.empty()) {
return false;
}
- if (isFile(f)) {
+ if (isFile(absNIPath)) {
return true;
}
// native image of System.Private.CoreLib.dll should have to overwrite
// original file to support new coreclr
- if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
- std::string coreLibBackup = path + ".Backup";
- if (isFile(coreLibBackup)) {
- return true;
- }
+ if (absDllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
+ return isR2RImage(absDllPath);
}
return false;
}
-#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
-static uintptr_t getFileSize(const std::string& path)
+static bool checkAppNIExistence(const std::string& absDllPath, NIOption* opt)
{
- struct stat sb;
+ std::string absNIPath = getAppNIFilePath(absDllPath, opt);
+ if (absNIPath.empty()) {
+ return false;
+ }
- if (stat(path.c_str(), &sb) == 0) {
- return sb.st_size;
+ if (isFile(absNIPath)) {
+ return true;
}
- return 0;
+ return false;
}
-// Get next base address to be used for system ni image from file
-// __SYSTEM_BASE_FILE should be checked for existance before calling this function
-static uintptr_t getNextBaseAddrFromFile()
+static bool checkDllExistInDir(const std::string& path)
{
- FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
- if (pFile == NULL) {
- fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
- return 0;
- }
-
- uintptr_t addr = 0;
- uintptr_t size = 0;
-
- while (fscanf(pFile, "%" SCNxPTR " %" SCNuPTR "", &addr, &size) != EOF) {
- }
+ bool ret = false;
+ auto func = [&ret](const std::string& f_path, const std::string& f_name) {
+ if (isManagedAssembly(f_name) || isNativeImage(f_name)) {
+ ret = true;
+ }
+ };
- fclose(pFile);
+ scanFilesInDirectory(path, func, 0);
- return addr + size;
+ return ret;
}
-// Get next base address to be used for system ni image
-static uintptr_t getNextBaseAddr()
+/*
+ * Get the list of managed files in the specific directory
+ * Absolute paths of managed files are stored at the result list.
+ * If native image already exist in the same directory, managed file is ignored.
+ */
+static ni_error_e getTargetDllList(const std::string& path, std::vector<std::string>& fileList)
{
- uintptr_t baseAddr = 0;
+ if (!isDirectory(path)) {
+ return NI_ERROR_INVALID_PARAMETER;
+ }
- if (!isFile(__SYSTEM_BASE_FILE)) {
- // This is the starting address for all default base addresses
- baseAddr = DEFAULT_BASE_ADDR_START;
- } else {
- baseAddr = getNextBaseAddrFromFile();
+ auto func = [&fileList](const std::string& f_path, const std::string& f_name) {
+ if (isManagedAssembly(f_path) && !checkNIExistence(f_path)) {
+ fileList.push_back(getAbsolutePath(f_path));
+ }
+ };
- // Round to a multple of 64K (see ZapImage::CalculateZapBaseAddress in CoreCLR)
- uintptr_t BASE_ADDRESS_ALIGNMENT = 0xffff;
- baseAddr = (baseAddr + BASE_ADDRESS_ALIGNMENT) & ~BASE_ADDRESS_ALIGNMENT;
- }
+ scanFilesInDirectory(path, func, 0);
- return baseAddr;
+ return NI_ERROR_NONE;
}
-// Save base address of system ni image to file
-static void updateBaseAddrFile(const std::string& absNIPath, uintptr_t baseAddr)
+/*
+ * Get the list of managed files in the specific directory of Application
+ * Absolute paths of managed files are stored at the result list.
+ * If native image already exist in the .native_image directory, managed file is ignored.
+ *
+ */
+static ni_error_e getAppTargetDllList(const std::string& path, std::vector<std::string>& fileList, NIOption *opt)
{
- uintptr_t niSize = getFileSize(absNIPath);
- if (niSize == 0) {
- fprintf(stderr, "File %s doesn't exist\n", absNIPath.c_str());
- return;
+ if (!isDirectory(path)) {
+ return NI_ERROR_INVALID_PARAMETER;
}
- // Write new entry to the file
- FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
- if (pFile == NULL) {
- fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
- return;
- }
+ auto func = [&fileList, opt](const std::string& f_path, const std::string& f_name) {
+ if (isManagedAssembly(f_path) && !checkAppNIExistence(f_path, opt)) {
+ fileList.push_back(getAbsolutePath(f_path));
+ }
+ };
+
+ scanFilesInDirectory(path, func, 0);
- fprintf(pFile, "%" PRIxPTR " %" PRIuPTR "\n", baseAddr, niSize);
- fclose(pFile);
+ return NI_ERROR_NONE;
}
-// check if dll is listed in TPA
-static bool isTPADll(const std::string& dllPath)
+static void makeArgs(std::vector<const char*>& args, const std::vector<std::string>& refPaths, NIOption* opt)
{
- std::string absPath = getBaseName(getAbsolutePath(dllPath));
-
- std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
- for (unsigned int i = 0; i < paths.size(); i++) {
- if (paths[i].find(getBaseName(absPath)) != std::string::npos) {
- return true;
+ args.push_back(CORERUN_CMD.c_str());
+ if (CROSSGEN2_PATH != "") {
+ args.push_back(CROSSGEN2_PATH.c_str());
+ }
+ args.push_back(CROSSGEN_OPT_JITPATH);
+ args.push_back(CLRJIT_PATH.c_str());
+ args.push_back(CROSSGEN_OPT_TARGET_ARCH);
+ args.push_back(ARCHITECTURE_IDENTIFIER);
+ if (!(opt->flags & NI_FLAGS_NO_PIPELINE)) {
+ args.push_back(CROSSGEN_OPT_OUT_NEAR_INPUT);
+ args.push_back(CROSSGEN_OPT_SINGLE_FILE_COMPILATION);
+ }
+ //args.push_back(OPT_PARALLELISM);
+ //args.push_back(OPT_PARALLELISM_COUNT);
+ args.push_back(CROSSGEN_OPT_RESILIENT);
+
+ args.push_back(CROSSGEN_OPT_OPTIMIZE);
+
+ if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
+ args.push_back(CROSSGEN_OPT_INPUTBUBBLE);
+ args.push_back(CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS);
+
+ if (opt->flags & NI_FLAGS_INPUT_BUBBLE_REF) {
+ INPUTBUBBLE_REF_VECTOR.clear();
+ // check inputbubbleref format.
+ for (const auto &path : opt->inputBubbleRefPath) {
+ if (checkDllExistInDir(path)) {
+ INPUTBUBBLE_REF_VECTOR.push_back("--inputbubbleref:" + path + "/*.dll");
+ }
+ }
+ // add ref path to inputbubble ref
+ for (const auto &path : refPaths) {
+ if (checkDllExistInDir(path)) {
+ INPUTBUBBLE_REF_VECTOR.push_back("--inputbubbleref:" + path + "/*.dll");
+ }
+ }
+ for (const auto &path : INPUTBUBBLE_REF_VECTOR) {
+ args.push_back(path.c_str());
+ }
}
}
- return false;
-}
-#endif
-
-// baseAddr should be checked in file before getting here
-static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, DWORD flags)
-{
- if (!isFile(dllPath)) {
- fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
- return NI_ERROR_NO_SUCH_FILE;
+ if (opt->flags & NI_FLAGS_MIBC) {
+ MIBC_VECTOR.clear();
+ for (const auto &path : opt->mibcPath) {
+ MIBC_VECTOR.push_back("--mibc:" + path);
+ }
+ for (const auto &path : MIBC_VECTOR) {
+ args.push_back(path.c_str());
+ }
}
- if (!isManagedAssembly(dllPath)) {
- //fprintf(stderr, "Input file is not a dll file : %s\n", dllPath.c_str());
- return NI_ERROR_INVALID_PARAMETER;
+ if (opt->flags & NI_FLAGS_VERBOSE) {
+ args.push_back(CROSSGEN_OPT_VERBOSE);
}
- if (checkNIExistence(dllPath)) {
- //fprintf(stderr, "Already ni file is exist for %s\n", dllPath.c_str());
- return NI_ERROR_ALREADY_EXIST;
+ REF_VECTOR.clear();
+
+ // set reference path
+ if (opt->flags & NI_FLAGS_REF) {
+ for (const auto &path : opt->refPath) {
+ REF_VECTOR.push_back("-r:" + path + "/*.dll");
+ }
+ } else {
+ std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
+ for (const auto &path : paths) {
+ if (checkDllExistInDir(path)) {
+ REF_VECTOR.push_back("-r:" + path + "/*.dll");
+ }
+ }
}
- std::string absDllPath = getAbsolutePath(dllPath);
- std::string absNIPath;
+ for (const auto &path : refPaths) {
+ if (checkDllExistInDir(path)) {
+ REF_VECTOR.push_back("-r:" + path + "/*.dll");
+ }
+ }
- bool isAppNI = flags & NI_FLAGS_APPNI;
- if (isAppNI && strstr(absDllPath.c_str(), __DOTNET_DIR) == NULL) {
- absNIPath = getAppNIFilePath(absDllPath, flags);
- } else {
- absNIPath = getNIFilePath(absDllPath);
+ for (const auto &path : REF_VECTOR) {
+ args.push_back(path.c_str());
}
+}
- if (absNIPath.empty()) {
- fprintf(stderr, "Fail to get ni file name\n");
+static void clearArgs(std::vector<const char*>& args)
+{
+ REF_VECTOR.clear();
+ args.clear();
+}
+
+static ni_error_e makePdbSymlinkForNI(std::string dllPath, std::string niPath)
+{
+ std::string pdbPath = changeExtension(dllPath, ".dll", ".pdb");
+ try {
+ if (exist(pdbPath)) {
+ std::string targetPDBPath = changeExtension(niPath, ".ni.dll", ".pdb");
+ if (!exist(targetPDBPath)) {
+ bf::create_symlink(pdbPath, targetPDBPath);
+ copySmackAndOwnership(pdbPath, targetPDBPath, true);
+ }
+ }
+ } catch (const bf::filesystem_error& error) {
+ _SERR("Fail to create symlink for %s", pdbPath.c_str());
return NI_ERROR_UNKNOWN;
}
-#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
- uintptr_t baseAddr = 0;
+ return NI_ERROR_NONE;
+}
- if (isTPADll(dllPath)) {
- baseAddr = getNextBaseAddr();
+static ni_error_e crossgen2PostAction(const std::string& dllPath, const std::string& niPath, NIOption* opt) {
+ if (!exist(niPath)) {
+ removeFile(changeExtension(niPath, ".ni.dll", ".ni.dll.tmp"));
+ _SERR("Fail to create native image for %s", dllPath.c_str());
+ return NI_ERROR_NO_SUCH_FILE;
}
-#endif
+ copySmackAndOwnership(dllPath, niPath);
+ // if AppNI then move ni.dll file to .native_image and copy pdb to .native_image
+ if (opt->flags & NI_FLAGS_APPNI) {
+ std::string appNIPath = getAppNIFilePath(dllPath, opt);
+ moveFile(niPath, appNIPath);
+ makePdbSymlinkForNI(dllPath, niPath);
+ _SOUT("Native image %s generated successfully.", appNIPath.c_str());
+ } else {
+ _SOUT("Native image %s generated successfully.", niPath.c_str());
+ }
+ return NI_ERROR_NONE;
+}
+static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
+{
+ // fork crossgen2
pid_t pid = fork();
if (pid == -1)
return NI_ERROR_UNKNOWN;
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status)) {
- // Do not use checkNIExistence() function to check whether ni file created or not.
- // checkNIExistence() return false for System.Private.Corelib.dll
- if (isFile(absNIPath)) {
- copySmackAndOwnership(absDllPath, absNIPath);
- std::string absPdbPath = replaceAll(absDllPath, ".dll", ".pdb");
- std::string pdbFilePath = replaceAll(absNIPath, ".ni.dll", ".pdb");
- if (isFile(absPdbPath) && (absPdbPath != pdbFilePath)) {
- if (!copyFile(absPdbPath, pdbFilePath)) {
- fprintf(stderr, "Failed to copy a .pdb file\n");
- } else {
- copySmackAndOwnership(absPdbPath, pdbFilePath);
- }
- }
-#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
- if (baseAddr != 0) {
- updateBaseAddrFile(absNIPath, baseAddr);
+ for (auto& dllPath: dllList) {
+ ni_error_e ret = crossgen2PostAction(dllPath, changeExtension(dllPath, ".dll", ".ni.dll"), opt);
+ if (ret != NI_ERROR_NONE) {
+ return ret;
}
-#endif
- return NI_ERROR_NONE;
- } else {
- fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
- return NI_ERROR_NO_SUCH_FILE;
}
+ } else {
+ _SERR("Failed. Forked process terminated abnormally");
+ return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
}
} else {
- std::string jitPath = __pm->getRuntimePath() + "/libclrjit.so";
- std::vector<const char*> argv = {
- __CROSSGEN_PATH,
- "/nologo",
- "/JITPath", jitPath.c_str()
- };
-
- bool compat = flags & NI_FLAGS_COMPATIBILITY;
- argv.push_back(compat ? "/Platform_Assemblies_Pathes" : "/p");
- std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
- std::string platformAssembliesPaths;
- for (const auto &path : paths) {
- if (!platformAssembliesPaths.empty()) {
- platformAssembliesPaths += ":";
- }
- platformAssembliesPaths += path;
- }
- argv.push_back(platformAssembliesPaths.c_str());
+ std::vector<const char*> argv;
+ makeArgs(argv, refPaths, opt);
- bool enableR2R = flags & NI_FLAGS_ENABLER2R;
- if (!enableR2R) {
- argv.push_back("/FragileNonVersionable");
+ // add input files at the end of parameter
+ for (const auto &input : dllList) {
+ argv.push_back(input.c_str());
+ _SOUT("+ %s", input.c_str());
}
- if (flags & NI_FLAGS_VERBOSE) {
- argv.push_back("/verbose");
- }
+ // end param
+ argv.push_back(nullptr);
- if (flags & NI_FLAGS_INSTRUMENT) {
- argv.push_back("/Tuning");
+ // print cmd
+ if (opt->flags & NI_FLAGS_PRINT_CMD) {
+ _SOUT("==================== NI Commands =========================");
+ for (auto &arg: argv) _SOUT("+ %s", arg);
}
-#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
- std::string baseAddrString;
- if (baseAddr != 0) {
- argv.push_back("/BaseAddress");
- std::stringstream ss;
- ss << "0x" << std::hex << baseAddr;
- baseAddrString = ss.str();
- argv.push_back(baseAddrString.c_str());
- }
-#endif
+ execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
+
+ clearArgs(argv);
+ exit(0);
+ }
- argv.push_back("/App_Paths");
- std::string absAppPath;
- if (!appPath.empty()) {
- absAppPath = appPath;
+ return NI_ERROR_NONE;
+}
+
+static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
+{
+ for (auto& dllPath : dllList) {
+ std::string niPath;
+ if (opt->flags & NI_FLAGS_APPNI) {
+ niPath = getAppNIFilePath(dllPath, opt);
} else {
- absAppPath = getBaseName(absDllPath);
+ niPath = getNIFilePath(dllPath);
}
- argv.push_back(absAppPath.c_str());
- argv.push_back("/out");
- argv.push_back(absNIPath.c_str());
+ // fork crossgen2
+ pid_t pid = fork();
+ if (pid == -1)
+ return NI_ERROR_UNKNOWN;
+
+ if (pid > 0) {
+ int status;
+ waitpid(pid, &status, 0);
+ if (WIFEXITED(status)) {
+ ni_error_e ret = crossgen2PostAction(dllPath, niPath, opt);
+ if (ret != NI_ERROR_NONE) {
+ return ret;
+ }
+ } else {
+ _SERR("Failed. Forked process terminated abnormally");
+ _SERR("Crossgen2 was terminated by the OOM killer. Please check the system.");
+ removeFile(changeExtension(niPath, ".ni.dll", ".ni.dll.tmp"));
+ return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
+ }
+ } else {
+ std::vector<const char*> argv;
+ makeArgs(argv, refPaths, opt);
+
+ argv.push_back("-o");
+ argv.push_back(niPath.c_str());
- argv.push_back(absDllPath.c_str());
- argv.push_back(nullptr);
+ argv.push_back(dllPath.c_str());
+ _SOUT("+ %s", dllPath.c_str());
- fprintf(stdout, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
+ // end param
+ argv.push_back(nullptr);
- execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
- exit(0);
+ // print cmd
+ if (opt->flags & NI_FLAGS_PRINT_CMD) {
+ _SOUT("==================== NI Commands =========================");
+ for (auto &arg: argv) _SOUT("+ %s", arg);
+ }
+
+ execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
+
+ clearArgs(argv);
+ exit(0);
+ }
+
+ waitInterval();
}
return NI_ERROR_NONE;
}
-// callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
-static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
+static ni_error_e createCoreLibNI(NIOption* opt)
{
- char *pkgId = NULL;
- int ret = 0;
- DWORD *pFlags = (DWORD*)userData;
+ std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
+ std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
+ std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
- ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
- if (ret != PMINFO_R_OK) {
- fprintf(stderr, "Failed to get pkgid\n");
- return -1;
+ std::vector<std::string> dllList;
+ std::vector<std::string> refPaths;
+ dllList.push_back(getAbsolutePath(coreLib));
+
+ if (!isFile(coreLibBackup) && !isR2RImage(coreLib)) {
+ if (crossgen2NoPipeLine(dllList, refPaths, opt) == NI_ERROR_NONE && exist(niCoreLib)) {
+ if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
+ _SERR("Failed to rename System.Private.CoreLib.dll");
+ return NI_ERROR_CORE_NI_FILE;
+ }
+ if (rename(niCoreLib.c_str(), coreLib.c_str())) {
+ _SERR("Failed to rename System.Private.CoreLib.ni.dll");
+ return NI_ERROR_CORE_NI_FILE;
+ }
+ } else {
+ _SERR("Failed to create native image for %s", coreLib.c_str());
+ return NI_ERROR_CORE_NI_FILE;
+ }
}
+ return NI_ERROR_NONE;
+}
- bool readOnlyApp = isReadOnlyApp(pkgId);
+static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string& refPaths, NIOption* opt)
+{
+ ni_error_e ret = NI_ERROR_NONE;
- // read-only and readonly flag set
- if (readOnlyApp && (*pFlags & NI_FLAGS_READONLY_APP)) {
- fprintf(stderr, "try to regenerate read-only pkg [%s]\n", pkgId);
- }
- // not rad-only and readonly flag doesnot set
- else if (!readOnlyApp && !(*pFlags & NI_FLAGS_READONLY_APP)) {
- if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
- fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
- return -1;
- }
+ if (dllList.empty()) {
+ return NI_ERROR_INVALID_PARAMETER;
}
- // skip regeneration
- else {
- fprintf(stderr, "skip regeneration. pkg-type(read-only) doesnot match the configuration [%s]\n", pkgId);
- return 0;
+ // When performing AOT for one Dll, an error is returned when an error occurs.
+ // However, when processing multiple dlls at once, only the log for errors is output and skipped.
+
+ std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
+ bool hasSPC = false;
+
+ std::vector<std::string> niList;
+ for (auto it = dllList.begin(); it != dllList.end(); it++) {
+ std::string f = *it;
+ if (!isFile(f)) {
+ _SERR("dll file is not exist : %s", f.c_str());
+ dllList.erase(it--);
+ }
+ if (!isManagedAssembly(f)) {
+ _SERR("Input file is not a dll file : %s", f.c_str());
+ dllList.erase(it--);
+ }
+ // handle System.Private.CoreLib.dll separately.
+ // dllList and path manager contain absolute path. So, there is no need to change path to absolute path
+ if (f == coreLib) {
+ hasSPC = true;
+ dllList.erase(it--);
+ } else {
+ niList.push_back(changeExtension(f, ".dll", ".ni.dll"));
+ }
}
- if (createNIUnderPkgRoot(pkgId, *pFlags) != NI_ERROR_NONE) {
- fprintf(stderr, "Failed to generate NI file [%s]\n", pkgId);
- return -1;
+ // In the case of SPC, post-processing is required to change the name of the native image.
+ // In order to avoid repeatedly checking whether the generated native image is an SPC,
+ // the SPC native image generation is performed separately.
+ if (hasSPC) {
+ ret = createCoreLibNI(opt);
+ if (ret != NI_ERROR_NONE) {
+ return ret;
+ }
+ }
+
+ // if there is no proper input after processing dll list
+ if (dllList.empty()) {
+ if (hasSPC) {
+ return ret;
+ } else {
+ return NI_ERROR_INVALID_PARAMETER;
+ }
+ }
+
+ std::vector<std::string> paths;
+ splitPath(refPaths, paths);
+
+ if (opt->flags & NI_FLAGS_NO_PIPELINE) {
+ ret = crossgen2NoPipeLine(dllList, paths, opt);
} else {
- fprintf(stdout, "Complete make application to native image\n");
+ // When the forked process in the pipeline state is terminated(WIFSIGNALED(status)),
+ // retry the generation of the native image
+ // if the number of .dll files and the number of .ni.dll files are different.
+ for (int callCnt = 0; callCnt < 2; callCnt++) {
+ // If an error occurs, perform it twice with the same option.
+ ret = crossgen2PipeLine(dllList, paths, opt);
+ if (ret != NI_ERROR_NONE) {
+ _SERR("Crossgen2 is abnormally terminated. Regenerate native images that failed while running crossgen2.");
+ dllList.clear();
+ for (auto it = niList.begin(); it != niList.end(); it++) {
+ std::string niPath = *it;
+ std::string dllPath = changeExtension(niPath, ".ni.dll", ".dll");
+ if (crossgen2PostAction(dllPath, niPath, opt) != NI_ERROR_NONE) {
+ dllList.push_back(dllPath);
+ } else {
+ niList.erase(it--);
+ }
+ }
+ } else {
+ break;
+ }
+ }
+ // If an error occurs after two crossgen2PipeLine() attempts,
+ // try crossgen2NoPipeLine() for the last time.
+ if (ret != NI_ERROR_NONE) {
+ _SERR("Retry running crossgen2 with --no-pipeline mode to avoid termination by OOM.");
+ ret = crossgen2NoPipeLine(dllList, paths, opt);
+ }
}
- return 0;
+ return ret;
}
-static bool isCoreLibPrepared(DWORD flags)
+static ni_error_e doAOTFile(const std::string& dllFile, const std::string& refPaths, NIOption* opt)
{
- if (flags & NI_FLAGS_ENABLER2R) {
- return true;
+ if (!isFile(dllFile)) {
+ _SERR("dll file is not exist : %s", dllFile.c_str());
+ return NI_ERROR_NO_SUCH_FILE;
}
- std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
- if (isFile(coreLibBackup)) {
- return true;
- } else {
- fprintf(stderr, "The native image of System.Private.CoreLib does not exist\n"
- "Run the command to create the native image\n"
- "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll\n\n");
- return false;
+ if (!isManagedAssembly(dllFile)) {
+ _SERR("Failed. Input parameter is not managed dll (%s)\n", dllFile.c_str());
+ return NI_ERROR_INVALID_PARAMETER;
+ }
+
+ if (checkNIExistence(dllFile)) {
+ _SERR("Native image file is already exist : %s", dllFile.c_str());
+ return NI_ERROR_ALREADY_EXIST;
}
+
+ std::vector<std::string> dllList;
+ dllList.push_back(getAbsolutePath(dllFile));
+ return doAOTList(dllList, refPaths, opt);
}
-static bool hasCoreLibNI()
+// callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
+static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
{
- std::string ildasm = concatPath(__pm->getRuntimePath(), "ildasm");
- std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
- std::string cmd = ildasm + " " + coreLib + " -noil -stats | grep '\\.xdata'";
+ char *pkgId = NULL;
+ int ret = 0;
+ NIOption **pOptions = (NIOption**)userData;
- FILE *fp;
- fp = popen(cmd.c_str(), "r");
- if (fp != NULL) {
- char buff[1024];
- if (fgets(buff, sizeof(buff), fp) != NULL) {
- pclose(fp);
- return true;
- }
- pclose(fp);
+ ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
+ if (ret != PMINFO_R_OK) {
+ _SERR("Failed to get pkgid");
+ return -1;
}
- return false;
-}
-static ni_error_e createCoreLibNI(DWORD flags)
-{
- std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
- std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
- std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
+ if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
+ _SERR("Failed to remove previous dlls from [%s]", pkgId);
+ return -1;
+ }
- if (!isFile(coreLibBackup) && !hasCoreLibNI()) {
- if (!crossgen(coreLib, std::string(), flags)) {
- if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
- fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
- return NI_ERROR_CORE_NI_FILE;
- }
- if (rename(niCoreLib.c_str(), coreLib.c_str())) {
- fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
- return NI_ERROR_CORE_NI_FILE;
- }
- } else {
- fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
- return NI_ERROR_CORE_NI_FILE;
- }
+ if (createNIUnderPkgRoot(pkgId, *pOptions) != NI_ERROR_NONE) {
+ _SERR("Failed to generate NI file [%s]", pkgId);
+ return -1;
+ } else {
+ _SOUT("Complete make application to native image");
}
- return NI_ERROR_NONE;
+
+ return 0;
}
ni_error_e initNICommon()
{
#if defined(__arm__) || defined(__aarch64__)
+
+ char *env = nullptr;
+ env = getenv("MIC_CROSSGEN2_ENABLED");
+ if (env != nullptr && !strcmp(env, "1")) {
+ CORERUN_CMD = std::string("/opt/usr/dotnet/mic/crossgen2");
+ CROSSGEN2_PATH = "";
+ CLRJIT_PATH = std::string("/opt/usr/dotnet/mic/libclrjit_unix_") + ARCHITECTURE_IDENTIFIER + std::string("_x64.so");
+ }
+
// get interval value
const static std::string intervalFile = concatPath(__NATIVE_LIB_DIR, "crossgen_interval.txt");
std::ifstream inFile(intervalFile);
if (inFile) {
- fprintf(stdout, "crossgen_interval.txt is found\n");
+ _SOUT("crossgen_interval.txt is found");
inFile >> __interval;
}
if (initializePluginManager("normal")) {
- fprintf(stderr, "Fail to initialize PluginManager\n");
+ _SERR("Fail to initialize PluginManager");
return NI_ERROR_UNKNOWN;
}
try {
__pm = new PathManager();
} catch (const std::exception& e) {
- fprintf(stderr, "Failed to create PathManager");
+ _SERR("Failed to create PathManager");
return NI_ERROR_UNKNOWN;
}
char* pluginDllPaths = pluginGetDllPath();
- if (pluginDllPaths) {
- __pm->addPlatformAssembliesPaths(pluginDllPaths);
+ if (pluginDllPaths && pluginDllPaths[0] != '\0') {
+ __pm->addPlatformAssembliesPaths(pluginDllPaths, true);
+ }
+
+ char* pluginNativePaths = pluginGetNativeDllSearchingPath();
+ if (pluginNativePaths && pluginNativePaths[0] != '\0') {
+ __pm->addNativeDllSearchingPaths(pluginNativePaths, true);
}
return NI_ERROR_NONE;
#else
- fprintf(stderr, "crossgen supports arm/arm64 architecture only. skip ni file generation\n");
+ _SERR("crossgen supports arm/arm64 architecture only. skip ni file generation");
return NI_ERROR_NOT_SUPPORTED;
#endif
}
delete(__pm);
__pm = nullptr;
+
+ if (__ni_option) {
+ free(__ni_option);
+ __ni_option = nullptr;
+ }
}
-ni_error_e createNIPlatform(DWORD flags)
+ni_error_e createNIPlatform(NIOption* opt)
{
- if (createCoreLibNI(flags) != NI_ERROR_NONE) {
- return NI_ERROR_CORE_NI_FILE;
+ ni_error_e ret = createNIUnderDirs(__pm->getRuntimePath(), opt);
+ if (ret != NI_ERROR_NONE) {
+ return ret;
}
- return createNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath(), flags);
+ return createNIUnderDirs(__pm->getTizenFXPath(), opt);
}
-ni_error_e createNIDll(const std::string& dllPath, DWORD flags)
+ni_error_e createNIDll(const std::string& dllPath, NIOption* opt)
{
- if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
- return createCoreLibNI(flags);
- }
+ return doAOTFile(dllPath, std::string(), opt);
+}
- if (!isCoreLibPrepared(flags)) {
- return NI_ERROR_CORE_NI_FILE;
+ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& refPaths, NIOption* opt)
+{
+ ni_error_e ret;
+
+ // get managed file list from targetPath
+ std::vector<std::string> dllList;
+ ret = getTargetDllList(targetPath, dllList);
+ if (ret != NI_ERROR_NONE) {
+ return ret;
}
- return crossgen(dllPath, std::string(), flags);
-}
+ std::vector<std::string> needNIList;
+ std::vector<std::string> niList;
-ni_error_e createNIUnderDirs(const std::string& rootPaths, DWORD flags)
-{
- if (!isCoreLibPrepared(flags)) {
- return NI_ERROR_CORE_NI_FILE;
+ for (auto &dll : dllList) {
+ if (!checkNIExistence(dll)) {
+ needNIList.push_back(dll);
+ }
+ niList.push_back(getNIFilePath(dll));
}
- auto convert = [&rootPaths, flags](const std::string& path, const std::string& filename) {
- // if path is symlink, donot generate crossgen
- if (!crossgen(path, rootPaths.c_str(), flags)) {
- waitInterval();
+ if (!needNIList.empty()) {
+ // NI fils of TAC-related dlls under /opt/usr/dotnet should not be created under .native_image directory.
+ // So, unset NI_FLAGS_APPNI temporally and restore it after running AOT.
+ opt->flags &= ~NI_FLAGS_APPNI;
+ ret = doAOTList(needNIList, refPaths, opt);
+ opt->flags |= NI_FLAGS_APPNI;
+ if (ret != NI_ERROR_NONE) {
+ return ret;
}
- };
+ }
- std::vector<std::string> targetPaths;
- splitPath(rootPaths, targetPaths);
- for (const auto &path : targetPaths) {
- // TAC directory should be handled specially because that contains symlink of native image file.
- if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
- if (!isDirectory(path)) {
- continue;
+ for (auto &niPath : niList) {
+ if (exist(niPath)) {
+ std::string symNIPath = concatPath(targetPath, getFileName(niPath));
+ if (!exist(symNIPath)) {
+ bf::create_symlink(niPath, symNIPath);
+ copySmackAndOwnership(targetPath.c_str(), symNIPath.c_str(), true);
+ _SOUT("%s symbolic link file generated successfully.", symNIPath.c_str());
+ _INFO("%s symbolic link file generated successfully.", symNIPath.c_str());
}
- // make native image symlink if not exist under tac directory
- try {
- for (auto& symlinkAssembly : bf::recursive_directory_iterator(path)) {
- std::string symPath = symlinkAssembly.path().string();
- if (!isManagedAssembly(symPath)) {
- continue;
- }
+ }
+ }
- // if there is symlink and original file for native image, skip generation
- std::string symNIPath = symPath.substr(0, symPath.rfind(".dll")) + ".ni.dll";
- if (isFile(symNIPath)) {
- continue;
- }
+ return NI_ERROR_NONE;
+}
- // if original native image not exist, generate native image
- std::string originPath = bf::read_symlink(symPath).string();
- std::string originNIPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
- if (!isFile(originNIPath)) {
- if (!crossgen(originPath, path.c_str(), flags)) {
- waitInterval();
- }
- }
- // if no symlink file exist, create symlink
- if (!isFile(symNIPath)) {
- bf::create_symlink(originNIPath, symNIPath);
- copySmackAndOwnership(symPath.c_str(), symNIPath.c_str(), true);
- fprintf(stdout, "%s symbolic link file generated successfully.\n", symNIPath.c_str());
- _INFO("%s symbolic link file generated successfully.", symNIPath.c_str());
- }
- }
- } catch (const bf::filesystem_error& error) {
- fprintf(stderr, "Failed to recursive directory: %s\n", error.what());
- return NI_ERROR_UNKNOWN;
+ni_error_e createNIUnderDirs(const std::string& rootPaths, NIOption* opt)
+{
+ ni_error_e ret = NI_ERROR_NONE;
+
+ std::vector<std::string> fileList;
+ std::vector<std::string> paths;
+ splitPath(rootPaths, paths);
+
+ for (const auto &path : paths) {
+ if (!exist(path)) {
+ continue;
+ }
+
+ if (path.find(TAC_SYMLINK_SUB_DIR) != std::string::npos) {
+ ret = createNIUnderTAC(path, rootPaths, opt);
+ if (ret != NI_ERROR_NONE) {
+ return ret;
+ }
+ } else if (opt->flags & NI_FLAGS_APPNI) {
+ ret = getAppTargetDllList(path, fileList, opt);
+ if (ret != NI_ERROR_NONE) {
+ return ret;
}
} else {
- scanFilesInDirectory(path, convert, 0);
+ ret = getTargetDllList(path, fileList);
+ if (ret != NI_ERROR_NONE) {
+ return ret;
+ }
}
}
- return NI_ERROR_NONE;
+ if (fileList.empty()) {
+ return NI_ERROR_NONE;
+ }
+
+ return doAOTList(fileList, rootPaths, opt);
}
-ni_error_e createNIUnderPkgRoot(const std::string& pkgId, DWORD flags)
+ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
{
+ if (!isR2RImage(concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll"))) {
+ _SERR("The native image of System.Private.CoreLib does not exist.\n"
+ "Run the command to create the native image\n"
+ "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll");
+ return NI_ERROR_CORE_NI_FILE;
+ }
+
std::string rootPath = getRootPath(pkgId);
if (rootPath.empty()) {
- fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
+ _SERR("Failed to get root path from [%s]", pkgId.c_str());
return NI_ERROR_INVALID_PACKAGE;
}
__pm->setAppRootPath(rootPath);
- flags |= NI_FLAGS_APPNI;
+ char* extraDllPaths = pluginGetExtraDllPath();
+ if (extraDllPaths && extraDllPaths[0] != '\0') {
+ __pm->setExtraDllPaths(extraDllPaths);
+ }
+
+ opt->flags |= NI_FLAGS_APPNI;
+
+ if (isReadOnlyArea(rootPath)) {
+ opt->flags |= NI_FLAGS_APP_UNDER_RO_AREA;
+ opt->flags |= NI_FLAGS_NO_PIPELINE;
+ _SERR("Only no-pipeline mode supported for RO app. Set no-pipeline option forcibly");
+ } else {
+ opt->flags &= ~NI_FLAGS_APP_UNDER_RO_AREA;
+ }
// create native image under bin and lib directory
// tac directory is skipped in the createNIUnderDirs.
- return createNIUnderDirs(__pm->getAppPaths(), flags);
+ return createNIUnderDirs(__pm->getAppPaths(), opt);
}
void removeNIPlatform()
std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
- if (hasCoreLibNI()) {
+ if (isR2RImage(coreLib)) {
if (!isFile(coreLibBackup)) {
return;
}
if (remove(coreLib.c_str())) {
- fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
+ _SERR("Failed to remove System.Private.CoreLib native image file");
}
if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
- fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
+ _SERR("Failed to rename System.Private.CoreLib.Backup to origin");
}
}
#ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
if (isFile(__SYSTEM_BASE_FILE)) {
if (remove(__SYSTEM_BASE_FILE)) {
- fprintf(stderr, "Failed to remove %s\n", __SYSTEM_BASE_FILE);
+ _SERR("Failed to remove %s", __SYSTEM_BASE_FILE);
}
}
#endif
auto convert = [](const std::string& path, const std::string& filename) {
if (isNativeImage(path)) {
if (remove(path.c_str())) {
- fprintf(stderr, "Failed to remove %s\n", path.c_str());
+ _SERR("Failed to remove %s", path.c_str());
}
}
};
{
std::string rootPath = getRootPath(pkgId);
if (rootPath.empty()) {
- fprintf(stderr, "Failed to get root path from [%s]\n", pkgId.c_str());
+ _SERR("Failed to get root path from [%s]", pkgId.c_str());
return NI_ERROR_INVALID_PACKAGE;
}
std::vector<std::string> paths;
splitPath(appNIPaths, paths);
for (const auto &path : paths) {
- // Only the native image inside the TAC should be removed.
- if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
- removeNIUnderDirs(path);
- } else {
- if (isDirectory(path)) {
- if (!removeAll(path.c_str())) {
- fprintf(stderr, "Failed to remove app ni dir [%s]\n", path.c_str());
+ if (!isReadOnlyArea(path)) {
+ // Only the native image inside the TAC should be removed.
+ if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
+ removeNIUnderDirs(path);
+ } else {
+ if (isDirectory(path)) {
+ if (!removeAll(path.c_str())) {
+ _SERR("Failed to remove app ni dir [%s]", path.c_str());
+ }
}
}
}
}
+ // In special cases, the ni file may exist in the dll location.
+ // The code below is to avoid this exceptional case.
+ std::string appPaths = __pm->getAppPaths();
+ splitPath(appPaths, paths);
+ for (const auto &path : paths) {
+ if (isDirectory(path)) {
+ removeNIUnderDirs(path);
+ }
+ }
+
return NI_ERROR_NONE;
}
-ni_error_e regenerateAppNI(DWORD flags)
+ni_error_e regenerateAppNI(NIOption* opt)
{
- if (!isCoreLibPrepared(flags)) {
- return NI_ERROR_CORE_NI_FILE;
- }
-
int ret = 0;
pkgmgrinfo_appinfo_metadata_filter_h handle;
return NI_ERROR_UNKNOWN;
}
- ret = pkgmgrMDFilterForeach(handle, appAotCb, &flags);
+ ret = pkgmgrMDFilterForeach(handle, appAotCb, &opt);
if (ret != 0) {
pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
return NI_ERROR_UNKNOWN;
static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
{
char *pkgId = NULL;
- DWORD *pFlags = (DWORD*)userData;
+ NIOption **pOpt = (NIOption**)userData;
int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
if (ret != PMINFO_R_OK || pkgId == NULL) {
- fprintf(stderr, "Failed to get pkgid\n");
+ _SERR("Failed to get pkgid");
return -1;
}
sqlite3 *tac_db = openDB(TAC_APP_LIST_DB);
if (!tac_db) {
- fprintf(stderr, "Sqlite open error\n");
+ _SERR("Sqlite open error");
return -1;
}
sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
nugetPaths += concatPath(__DOTNET_DIR, nuget);
}
- auto convert = [&nugetPaths, pFlags](const std::string& path, const std::string& filename) {
- if (strstr(path.c_str(), TAC_SHA_256_INFO) != NULL)
- return;
- if (!crossgen(path, nugetPaths.c_str(), *pFlags)) {
- waitInterval();
- }
- };
-
for (auto& nuget : nugets) {
- scanFilesInDirectory(concatPath(__DOTNET_DIR, nuget), convert, -1);
+ createNIUnderTAC(concatPath(__DOTNET_DIR, nuget), nugetPaths, *pOpt);
}
return 0;
}
-ni_error_e regenerateTACNI(DWORD flags)
+ni_error_e regenerateTACNI(NIOption* opt)
{
- if (!isCoreLibPrepared(flags)) {
- return NI_ERROR_CORE_NI_FILE;
- }
-
removeNIUnderDirs(__DOTNET_DIR);
pkgmgrinfo_appinfo_metadata_filter_h handle;
return NI_ERROR_UNKNOWN;
}
- ret = pkgmgrMDFilterForeach(handle, regenTacCb, &flags);
+ ret = pkgmgrMDFilterForeach(handle, regenTacCb, &opt);
if (ret != 0) {
pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
return NI_ERROR_UNKNOWN;
return NI_ERROR_NONE;
}
+
+static std::vector<uid_t> getUserIds()
+{
+ std::vector<uid_t> list;
+
+ while (true) {
+ errno = 0; // so we can distinguish errors from no more entries
+ passwd* entry = getpwent();
+ if (!entry) {
+ if (errno) {
+ _SERR("Error while getting userIDs");
+ list.clear();
+ return list;
+ }
+ break;
+ }
+ list.push_back(entry->pw_uid);
+ }
+ endpwent();
+
+ return list;
+}
+
+static std::string getAppDataPath(const std::string& pkgId, uid_t uid)
+{
+ std::string pDataFile;
+
+ tzplatform_set_user(uid);
+
+ const char* tzUserApp = tzplatform_getenv(TZ_USER_APP);
+ if (tzUserApp != NULL) {
+ pDataFile = std::string(tzUserApp) + "/" + pkgId + "/data/";
+ }
+
+ tzplatform_reset_user();
+
+ return pDataFile;
+}
+
+ni_error_e removeAppProfileData(const std::string& pkgId)
+{
+ if (pkgId.empty()) {
+ return NI_ERROR_INVALID_PARAMETER;
+ }
+
+ std::vector<uid_t> uidList = getUserIds();
+ for (auto& uid : uidList) {
+ // get data path from pkgid
+ std::string dataPath = getAppDataPath(pkgId, uid);
+ if (!dataPath.empty() && exist(dataPath)) {
+ std::string pDataFile = dataPath + PROFILE_BASENAME;
+
+ if (exist(pDataFile)) {
+ if (!removeFile(pDataFile)) {
+ _SERR("Fail to remove profile data file (%s).", pDataFile.c_str());
+ return NI_ERROR_UNKNOWN;
+ }
+ _SOUT("Profile data (%s) is removed successfully", pDataFile.c_str());
+ }
+ }
+ }
+
+ return NI_ERROR_NONE;
+}
+
+static int appTypeListCb(pkgmgrinfo_appinfo_h handle, void *user_data)
+{
+ char *pkgId = NULL;
+ int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
+ if (ret != PMINFO_R_OK || pkgId == NULL) {
+ _SERR("Fail to get pkgid");
+ return 0;
+ }
+
+ if (removeAppProfileData(pkgId) != NI_ERROR_NONE) {
+ _SERR("Fail to remove profile data for (%s)", pkgId);
+ }
+
+ return 0;
+}
+
+static ni_error_e removeAppProfileByAppType(const char* type)
+{
+ int ret;
+
+ pkgmgrinfo_appinfo_filter_h filter;
+
+ ret = pkgmgrinfo_appinfo_filter_create(&filter);
+ if (ret != PMINFO_R_OK) {
+ _SERR("Fail to create appinfo filter");
+ return NI_ERROR_UNKNOWN;
+ }
+
+ ret = pkgmgrinfo_appinfo_filter_add_string(filter, PMINFO_APPINFO_PROP_APP_TYPE, type);
+ if (ret != PMINFO_R_OK) {
+ pkgmgrinfo_appinfo_filter_destroy(filter);
+ _SERR("Fail to add appinfo filter (%s)", type);
+ return NI_ERROR_UNKNOWN;
+ }
+
+ ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(filter, appTypeListCb, NULL);
+ if (ret != PMINFO_R_OK) {
+ _SERR("Fail to pkgmgrinfo_pkginfo_filter_foreach_pkginfo");
+ pkgmgrinfo_appinfo_filter_destroy(filter);
+ return NI_ERROR_UNKNOWN;
+ }
+
+ pkgmgrinfo_appinfo_filter_destroy(filter);
+
+ return NI_ERROR_NONE;
+}
+
+void removeAllAppProfileData()
+{
+ std::vector<const char*> appTypeList = {"dotnet", "dotnet-nui", "dotnet-inhouse"};
+
+ for (auto& type : appTypeList) {
+ if (removeAppProfileByAppType(type) != NI_ERROR_NONE) {
+ _SERR("Fail to removeAppProfileByAppType for type (%s)", type);
+ }
+ }
+}