2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <pkgmgr-info.h>
18 #include <pkgmgr_installer_info.h>
20 #include <tzplatform_config.h>
24 #include "pkgmgr_parser_plugin_interface.h"
43 #include "ni_common.h"
44 #include "db_manager.h"
45 #include "tac_common.h"
46 #include "path_manager.h"
47 #include "plugin_manager.h"
48 #include "r2r_checker.h"
53 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
56 #define __STR(x) __XSTR(x)
57 #if defined(__arm__) || defined(__aarch64__)
58 static const char* __NATIVE_LIB_DIR = __STR(NATIVE_LIB_DIR);
60 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
61 static const char* __READ_ONLY_APP_UPDATE_DIR = __STR(READ_ONLY_APP_UPDATE_DIR);
63 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
64 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
70 static std::string CORERUN_CMD = "/usr/share/dotnet.tizen/netcoreapp/corerun";
71 static std::string CROSSGEN2_PATH = "/usr/share/dotnet.tizen/netcoreapp/crossgen2/crossgen2.dll";
72 static std::string CLRJIT_PATH = "/usr/share/dotnet.tizen/netcoreapp/libclrjit.so";
73 static const char* CROSSGEN_OPT_JITPATH = "--jitpath";
74 static const char* CROSSGEN_OPT_TARGET_ARCH = "--targetarch";
75 static const char* CROSSGEN_OPT_OUT_NEAR_INPUT = "--out-near-input";
76 static const char* CROSSGEN_OPT_SINGLE_FILE_COMPILATION = "--single-file-compilation";
77 //static const char* CROSSGEN_OPT_PARALLELISM = "--parallelism";
78 //static const char* CROSSGEN_OPT_PARALLELISM_COUNT = "5";
79 static const char* CROSSGEN_OPT_RESILIENT = "--resilient";
80 static const char* CROSSGEN_OPT_OPTIMIZE = "-O";
81 static const char* CROSSGEN_OPT_INPUTBUBBLE = "--inputbubble";
82 static const char* CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS = "--compilebubblegenerics";
83 static const char* CROSSGEN_OPT_VERBOSE = "--verbose";
84 static std::vector<std::string> REF_VECTOR;
85 static std::vector<std::string> INPUTBUBBLE_REF_VECTOR;
86 static std::vector<std::string> MIBC_VECTOR;
88 static int __interval = 0;
89 static PathManager* __pm = nullptr;
91 static NIOption* __ni_option = nullptr;
94 NIOption* getNIOption()
96 if (__ni_option == nullptr) {
97 __ni_option = (NIOption*)calloc(sizeof(NIOption), 1);
98 if (__ni_option == nullptr) {
99 _SERR("Fail to create NIOption");
105 static void waitInterval()
107 // by the recommand, ignore small value for performance.
108 if (__interval > 10000) {
109 _SOUT("sleep %d usec", __interval);
114 static std::string getNIFilePath(const std::string& dllPath)
116 size_t index = dllPath.find_last_of(".");
117 if (index == std::string::npos) {
118 _SERR("File doesnot contain extension. fail to get NI file name");
121 std::string fName = dllPath.substr(0, index);
122 std::string fExt = dllPath.substr(index, dllPath.length());
124 // crossgen generate file with lower case extension only
125 std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
126 std::string niPath = fName + ".ni" + fExt;
132 * @brief create the directory including parents directory, and
133 * copy ownership and smack labels to the created directory.
134 * @param[in] target directory path
135 * @param[in] source directory path to get ownership and smack label
136 * @return if directory created successfully, return true otherwise false
138 static bool createDirsAndCopyOwnerShip(std::string& target_path, const std::string& source)
141 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
143 for (std::string::iterator iter = target_path.begin(); iter != target_path.end();) {
144 std::string::iterator newIter = std::find(iter, target_path.end(), '/');
145 std::string newPath = std::string(target_path.begin(), newIter);
147 if (!newPath.empty()) {
148 if (stat(newPath.c_str(), &st) != 0) {
149 if (mkdir(newPath.c_str(), mode) != 0 && errno != EEXIST) {
150 _SERR("Fail to create app ni directory (%s)", newPath.c_str());
153 if (!source.empty()) {
154 copySmackAndOwnership(source, newPath);
157 if (!S_ISDIR(st.st_mode)) {
158 _SERR("Fail. path is not a dir (%s)", newPath.c_str());
164 if(newIter != target_path.end()) {
172 static std::string getAppNIFilePath(const std::string& absDllPath, NIOption* opt)
174 std::string niDirPath;
175 std::string prevPath;
177 prevPath = getBaseName(absDllPath);
178 niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
180 if (opt->flags & NI_FLAGS_APP_UNDER_RO_AREA) {
181 niDirPath = replaceAll(niDirPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
182 _SERR("App is installed in RO area. Change NI path to RW area(%s).", niDirPath.c_str());
183 _ERR("App is installed in RO area. Change NI path to RW area(%s).", niDirPath.c_str());
186 if (!isDirectory(niDirPath)) {
187 if (!createDirsAndCopyOwnerShip(niDirPath, prevPath)) {
188 niDirPath = prevPath;
189 _SERR("fail to create dir (%s)", niDirPath.c_str());
193 return getNIFilePath(concatPath(niDirPath, getFileName(absDllPath)));
196 static bool checkNIExistence(const std::string& absDllPath)
198 std::string absNIPath = getNIFilePath(absDllPath);
199 if (absNIPath.empty()) {
203 if (isFile(absNIPath)) {
207 // native image of System.Private.CoreLib.dll should have to overwrite
208 // original file to support new coreclr
209 if (absDllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
210 return isR2RImage(absDllPath);
216 static bool checkDllExistInDir(const std::string& path)
219 auto func = [&ret](const std::string& f_path, const std::string& f_name) {
220 if (isManagedAssembly(f_name) || isNativeImage(f_name)) {
225 scanFilesInDirectory(path, func, 0);
231 * Get the list of managed files in the specific directory
232 * Absolute paths of managed files are stored at the result list.
233 * If native image already exist in the same directory, managed file is ignored.
235 static ni_error_e getTargetDllList(const std::string& path, std::vector<std::string>& fileList)
237 if (!isDirectory(path)) {
238 return NI_ERROR_INVALID_PARAMETER;
241 auto func = [&fileList](const std::string& f_path, const std::string& f_name) {
242 if (isManagedAssembly(f_path) && !checkNIExistence(f_path)) {
243 fileList.push_back(getAbsolutePath(f_path));
247 scanFilesInDirectory(path, func, 0);
249 return NI_ERROR_NONE;
252 static void makeArgs(std::vector<const char*>& args, const std::vector<std::string>& refPaths, NIOption* opt)
254 args.push_back(CORERUN_CMD.c_str());
255 if (CROSSGEN2_PATH != "") {
256 args.push_back(CROSSGEN2_PATH.c_str());
258 args.push_back(CROSSGEN_OPT_JITPATH);
259 args.push_back(CLRJIT_PATH.c_str());
260 args.push_back(CROSSGEN_OPT_TARGET_ARCH);
261 args.push_back(ARCHITECTURE_IDENTIFIER);
262 if (!(opt->flags & NI_FLAGS_NO_PIPELINE)) {
263 args.push_back(CROSSGEN_OPT_OUT_NEAR_INPUT);
264 args.push_back(CROSSGEN_OPT_SINGLE_FILE_COMPILATION);
266 //args.push_back(OPT_PARALLELISM);
267 //args.push_back(OPT_PARALLELISM_COUNT);
268 args.push_back(CROSSGEN_OPT_RESILIENT);
270 args.push_back(CROSSGEN_OPT_OPTIMIZE);
272 if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
273 args.push_back(CROSSGEN_OPT_INPUTBUBBLE);
274 args.push_back(CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS);
276 if (opt->flags & NI_FLAGS_INPUT_BUBBLE_REF) {
277 INPUTBUBBLE_REF_VECTOR.clear();
278 // check inputbubbleref format.
279 for (const auto &path : opt->inputBubbleRefPath) {
280 if (checkDllExistInDir(path)) {
281 INPUTBUBBLE_REF_VECTOR.push_back("--inputbubbleref:" + path + "/*.dll");
284 // add ref path to inputbubble ref
285 for (const auto &path : refPaths) {
286 if (checkDllExistInDir(path)) {
287 INPUTBUBBLE_REF_VECTOR.push_back("--inputbubbleref:" + path + "/*.dll");
290 for (const auto &path : INPUTBUBBLE_REF_VECTOR) {
291 args.push_back(path.c_str());
296 if (opt->flags & NI_FLAGS_MIBC) {
298 for (const auto &path : opt->mibcPath) {
299 MIBC_VECTOR.push_back("--mibc:" + path);
301 for (const auto &path : MIBC_VECTOR) {
302 args.push_back(path.c_str());
306 if (opt->flags & NI_FLAGS_VERBOSE) {
307 args.push_back(CROSSGEN_OPT_VERBOSE);
312 // set reference path
313 if (opt->flags & NI_FLAGS_REF) {
314 for (const auto &path : opt->refPath) {
315 REF_VECTOR.push_back("-r:" + path + "/*.dll");
318 std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
319 for (const auto &path : paths) {
320 if (checkDllExistInDir(path)) {
321 REF_VECTOR.push_back("-r:" + path + "/*.dll");
326 for (const auto &path : refPaths) {
327 if (checkDllExistInDir(path)) {
328 REF_VECTOR.push_back("-r:" + path + "/*.dll");
332 for (const auto &path : REF_VECTOR) {
333 args.push_back(path.c_str());
337 static void clearArgs(std::vector<const char*>& args)
343 static ni_error_e makePdbSymlinkForNI(std::string dllPath, std::string niPath)
345 std::string pdbPath = changeExtension(dllPath, ".dll", ".pdb");
347 if (exist(pdbPath)) {
348 std::string targetPDBPath = changeExtension(niPath, ".ni.dll", ".pdb");
349 if (!exist(targetPDBPath)) {
350 bf::create_symlink(pdbPath, targetPDBPath);
351 copySmackAndOwnership(pdbPath, targetPDBPath, true);
354 } catch (const bf::filesystem_error& error) {
355 _SERR("Fail to create symlink for %s", pdbPath.c_str());
356 return NI_ERROR_UNKNOWN;
359 return NI_ERROR_NONE;
362 static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
367 return NI_ERROR_UNKNOWN;
371 waitpid(pid, &status, 0);
372 if (WIFEXITED(status)) {
373 for (auto& dllPath: dllList) {
374 std::string niPath = changeExtension(dllPath, ".dll", ".ni.dll");
376 if (!exist(niPath)) {
377 _SERR("Fail to create native image for %s", dllPath.c_str());
378 return NI_ERROR_NO_SUCH_FILE;
381 copySmackAndOwnership(dllPath, niPath);
382 // if AppNI then move ni.dll file to .native_image and copy pdb to .native_image
383 if (opt->flags & NI_FLAGS_APPNI) {
384 std::string appNIPath = getAppNIFilePath(dllPath, opt);
385 moveFile(niPath, appNIPath);
386 makePdbSymlinkForNI(dllPath, appNIPath);
390 _SOUT("Native image %s generated successfully.", niPath.c_str());
393 _SERR("Failed. Forked process terminated abnormally");
396 std::vector<const char*> argv;
397 makeArgs(argv, refPaths, opt);
399 // add input files at the end of parameter
400 for (const auto &input : dllList) {
401 argv.push_back(input.c_str());
402 _SOUT("+ %s", input.c_str());
406 argv.push_back(nullptr);
409 if (opt->flags & NI_FLAGS_PRINT_CMD) {
410 _SOUT("==================== NI Commands =========================");
411 for (auto &arg: argv) _SOUT("+ %s", arg);
414 execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
420 return NI_ERROR_NONE;
423 static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
425 for (auto& dllPath : dllList) {
427 if (opt->flags & NI_FLAGS_APPNI) {
428 niPath = getAppNIFilePath(dllPath, opt);
430 niPath = getNIFilePath(dllPath);
436 return NI_ERROR_UNKNOWN;
440 waitpid(pid, &status, 0);
441 if (WIFEXITED(status)) {
442 if (!exist(niPath)) {
443 _SERR("Fail to create native image for %s", dllPath.c_str());
444 return NI_ERROR_NO_SUCH_FILE;
447 copySmackAndOwnership(dllPath, niPath);
448 if (opt->flags & NI_FLAGS_APPNI) {
449 makePdbSymlinkForNI(dllPath, niPath);
452 _SOUT("Native image %s generated successfully.", niPath.c_str());
454 _SERR("Failed. Forked process terminated abnormally");
457 std::vector<const char*> argv;
458 makeArgs(argv, refPaths, opt);
460 argv.push_back("-o");
461 argv.push_back(niPath.c_str());
463 argv.push_back(dllPath.c_str());
464 _SOUT("+ %s", dllPath.c_str());
467 argv.push_back(nullptr);
470 if (opt->flags & NI_FLAGS_PRINT_CMD) {
471 _SOUT("==================== NI Commands =========================");
472 for (auto &arg: argv) _SOUT("+ %s", arg);
475 execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
484 return NI_ERROR_NONE;
487 static ni_error_e createCoreLibNI(NIOption* opt)
489 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
490 std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
491 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
493 std::vector<std::string> dllList;
494 std::vector<std::string> refPaths;
495 dllList.push_back(getAbsolutePath(coreLib));
497 if (!isFile(coreLibBackup) && !isR2RImage(coreLib)) {
498 if (crossgen2NoPipeLine(dllList, refPaths, opt) == NI_ERROR_NONE && exist(niCoreLib)) {
499 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
500 _SERR("Failed to rename System.Private.CoreLib.dll");
501 return NI_ERROR_CORE_NI_FILE;
503 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
504 _SERR("Failed to rename System.Private.CoreLib.ni.dll");
505 return NI_ERROR_CORE_NI_FILE;
508 _SERR("Failed to create native image for %s", coreLib.c_str());
509 return NI_ERROR_CORE_NI_FILE;
512 return NI_ERROR_NONE;
515 static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string& refPaths, NIOption* opt)
517 ni_error_e ret = NI_ERROR_NONE;
519 if (dllList.empty()) {
520 return NI_ERROR_INVALID_PARAMETER;
522 // When performing AOT for one Dll, an error is returned when an error occurs.
523 // However, when processing multiple dlls at once, only the log for errors is output and skipped.
525 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
528 for (auto it = dllList.begin(); it != dllList.end(); it++) {
531 _SERR("dll file is not exist : %s", f.c_str());
534 if (!isManagedAssembly(f)) {
535 _SERR("Input file is not a dll file : %s", f.c_str());
538 // handle System.Private.CoreLib.dll separately.
539 // dllList and path manager contain absolute path. So, there is no need to change path to absolute path
546 // In the case of SPC, post-processing is required to change the name of the native image.
547 // In order to avoid repeatedly checking whether the generated native image is an SPC,
548 // the SPC native image generation is performed separately.
550 ret = createCoreLibNI(opt);
551 if (ret != NI_ERROR_NONE) {
556 // if there is no proper input after processing dll list
557 if (dllList.empty()) {
561 return NI_ERROR_INVALID_PARAMETER;
565 std::vector<std::string> paths;
566 splitPath(refPaths, paths);
568 if (opt->flags & NI_FLAGS_NO_PIPELINE) {
569 ret = crossgen2NoPipeLine(dllList, paths, opt);
571 ret = crossgen2PipeLine(dllList, paths, opt);
577 static ni_error_e doAOTFile(const std::string& dllFile, const std::string& refPaths, NIOption* opt)
579 if (!isFile(dllFile)) {
580 _SERR("dll file is not exist : %s", dllFile.c_str());
581 return NI_ERROR_NO_SUCH_FILE;
584 if (!isManagedAssembly(dllFile)) {
585 _SERR("Failed. Input parameter is not managed dll (%s)\n", dllFile.c_str());
586 return NI_ERROR_INVALID_PARAMETER;
589 if (checkNIExistence(dllFile)) {
590 _SERR("Native image file is already exist : %s", dllFile.c_str());
591 return NI_ERROR_ALREADY_EXIST;
594 std::vector<std::string> dllList;
595 dllList.push_back(getAbsolutePath(dllFile));
596 return doAOTList(dllList, refPaths, opt);
599 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
600 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
604 NIOption **pOptions = (NIOption**)userData;
606 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
607 if (ret != PMINFO_R_OK) {
608 _SERR("Failed to get pkgid");
612 if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
613 _SERR("Failed to remove previous dlls from [%s]", pkgId);
617 if (createNIUnderPkgRoot(pkgId, *pOptions) != NI_ERROR_NONE) {
618 _SERR("Failed to generate NI file [%s]", pkgId);
621 _SOUT("Complete make application to native image");
627 ni_error_e initNICommon()
629 #if defined(__arm__) || defined(__aarch64__)
632 env = getenv("MIC_CROSSGEN2_ENABLED");
633 if (env != nullptr && !strcmp(env, "1")) {
634 CORERUN_CMD = std::string("/opt/usr/dotnet/mic/crossgen2");
636 CLRJIT_PATH = std::string("/opt/usr/dotnet/mic/libclrjit_unix_") + ARCHITECTURE_IDENTIFIER + std::string("_x64.so");
639 // get interval value
640 const static std::string intervalFile = concatPath(__NATIVE_LIB_DIR, "crossgen_interval.txt");
641 std::ifstream inFile(intervalFile);
643 _SOUT("crossgen_interval.txt is found");
644 inFile >> __interval;
647 if (initializePluginManager("normal")) {
648 _SERR("Fail to initialize PluginManager");
649 return NI_ERROR_UNKNOWN;
653 __pm = new PathManager();
654 } catch (const std::exception& e) {
655 _SERR("Failed to create PathManager");
656 return NI_ERROR_UNKNOWN;
659 char* pluginDllPaths = pluginGetDllPath();
660 if (pluginDllPaths && pluginDllPaths[0] != '\0') {
661 __pm->addPlatformAssembliesPaths(pluginDllPaths, true);
664 char* pluginNativePaths = pluginGetNativeDllSearchingPath();
665 if (pluginNativePaths && pluginNativePaths[0] != '\0') {
666 __pm->addNativeDllSearchingPaths(pluginNativePaths, true);
669 return NI_ERROR_NONE;
671 _SERR("crossgen supports arm/arm64 architecture only. skip ni file generation");
672 return NI_ERROR_NOT_SUPPORTED;
676 void finalizeNICommon()
680 finalizePluginManager();
687 __ni_option = nullptr;
691 ni_error_e createNIPlatform(NIOption* opt)
693 ni_error_e ret = createNIUnderDirs(__pm->getRuntimePath(), opt);
694 if (ret != NI_ERROR_NONE) {
698 return createNIUnderDirs(__pm->getTizenFXPath(), opt);
701 ni_error_e createNIDll(const std::string& dllPath, NIOption* opt)
703 return doAOTFile(dllPath, std::string(), opt);
706 ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& refPaths, NIOption* opt)
710 // get managed file list from targetPath
711 std::vector<std::string> dllList;
712 ret = getTargetDllList(targetPath, dllList);
713 if (ret != NI_ERROR_NONE) {
717 std::vector<std::string> needNIList;
718 std::vector<std::string> niList;
720 for (auto &dll : dllList) {
721 if (!checkNIExistence(dll)) {
722 needNIList.push_back(dll);
724 niList.push_back(getNIFilePath(dll));
727 if (!needNIList.empty()) {
728 // NI fils of TAC-related dlls under /opt/usr/dotnet should not be created under .native_image directory.
729 // So, unset NI_FLAGS_APPNI temporally and restore it after running AOT.
730 opt->flags &= ~NI_FLAGS_APPNI;
731 ret = doAOTList(needNIList, refPaths, opt);
732 opt->flags |= NI_FLAGS_APPNI;
733 if (ret != NI_ERROR_NONE) {
738 for (auto &niPath : niList) {
740 std::string symNIPath = concatPath(targetPath, getFileName(niPath));
741 if (!exist(symNIPath)) {
742 bf::create_symlink(niPath, symNIPath);
743 copySmackAndOwnership(targetPath.c_str(), symNIPath.c_str(), true);
744 _SOUT("%s symbolic link file generated successfully.", symNIPath.c_str());
745 _INFO("%s symbolic link file generated successfully.", symNIPath.c_str());
750 return NI_ERROR_NONE;
754 ni_error_e createNIUnderDirs(const std::string& rootPaths, NIOption* opt)
756 ni_error_e ret = NI_ERROR_NONE;
758 std::vector<std::string> fileList;
759 std::vector<std::string> paths;
760 splitPath(rootPaths, paths);
762 for (const auto &path : paths) {
767 if (path.find(TAC_SYMLINK_SUB_DIR) != std::string::npos) {
768 ret = createNIUnderTAC(path, rootPaths, opt);
769 if (ret != NI_ERROR_NONE) {
773 ret = getTargetDllList(path, fileList);
774 if (ret != NI_ERROR_NONE) {
780 if (fileList.empty()) {
781 return NI_ERROR_NONE;
784 return doAOTList(fileList, rootPaths, opt);
787 ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
789 std::string rootPath = getRootPath(pkgId);
790 if (rootPath.empty()) {
791 _SERR("Failed to get root path from [%s]", pkgId.c_str());
792 return NI_ERROR_INVALID_PACKAGE;
795 __pm->setAppRootPath(rootPath);
797 char* extraDllPaths = pluginGetExtraDllPath();
798 if (extraDllPaths && extraDllPaths[0] != '\0') {
799 __pm->setExtraDllPaths(extraDllPaths);
802 opt->flags |= NI_FLAGS_APPNI;
804 if (isReadOnlyArea(rootPath)) {
805 opt->flags |= NI_FLAGS_APP_UNDER_RO_AREA;
806 opt->flags |= NI_FLAGS_NO_PIPELINE;
807 _SERR("Only no-pipeline mode supported for RO app. Set no-pipeline option forcibly");
809 opt->flags &= ~NI_FLAGS_APP_UNDER_RO_AREA;
812 // create native image under bin and lib directory
813 // tac directory is skipped in the createNIUnderDirs.
814 return createNIUnderDirs(__pm->getAppPaths(), opt);
817 void removeNIPlatform()
819 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
820 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
822 if (isR2RImage(coreLib)) {
823 if (!isFile(coreLibBackup)) {
827 if (remove(coreLib.c_str())) {
828 _SERR("Failed to remove System.Private.CoreLib native image file");
830 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
831 _SERR("Failed to rename System.Private.CoreLib.Backup to origin");
835 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
836 if (isFile(__SYSTEM_BASE_FILE)) {
837 if (remove(__SYSTEM_BASE_FILE)) {
838 _SERR("Failed to remove %s", __SYSTEM_BASE_FILE);
843 removeNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath());
846 void removeNIUnderDirs(const std::string& rootPaths)
848 auto convert = [](const std::string& path, const std::string& filename) {
849 if (isNativeImage(path)) {
850 if (remove(path.c_str())) {
851 _SERR("Failed to remove %s", path.c_str());
856 std::vector<std::string> paths;
857 splitPath(rootPaths, paths);
858 for (const auto &path : paths) {
859 scanFilesInDirectory(path, convert, -1);
863 ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
865 std::string rootPath = getRootPath(pkgId);
866 if (rootPath.empty()) {
867 _SERR("Failed to get root path from [%s]", pkgId.c_str());
868 return NI_ERROR_INVALID_PACKAGE;
871 __pm->setAppRootPath(rootPath);
873 // getAppNIPaths returns bin/.native_image, lib/.native_image and .tac_symlink.
874 std::string appNIPaths = __pm->getAppNIPaths();
875 std::vector<std::string> paths;
876 splitPath(appNIPaths, paths);
877 for (const auto &path : paths) {
878 if (!isReadOnlyArea(path)) {
879 // Only the native image inside the TAC should be removed.
880 if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
881 removeNIUnderDirs(path);
883 if (isDirectory(path)) {
884 if (!removeAll(path.c_str())) {
885 _SERR("Failed to remove app ni dir [%s]", path.c_str());
892 // In special cases, the ni file may exist in the dll location.
893 // The code below is to avoid this exceptional case.
894 std::string appPaths = __pm->getAppPaths();
895 splitPath(appPaths, paths);
896 for (const auto &path : paths) {
897 if (isDirectory(path)) {
898 removeNIUnderDirs(path);
902 return NI_ERROR_NONE;
905 ni_error_e regenerateAppNI(NIOption* opt)
908 pkgmgrinfo_appinfo_metadata_filter_h handle;
910 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
911 if (ret != PMINFO_R_OK)
912 return NI_ERROR_UNKNOWN;
914 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
915 if (ret != PMINFO_R_OK) {
916 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
917 return NI_ERROR_UNKNOWN;
920 ret = pkgmgrMDFilterForeach(handle, appAotCb, &opt);
922 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
923 return NI_ERROR_UNKNOWN;
926 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
927 return NI_ERROR_NONE;
930 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
931 static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
934 NIOption **pOpt = (NIOption**)userData;
936 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
937 if (ret != PMINFO_R_OK || pkgId == NULL) {
938 _SERR("Failed to get pkgid");
942 sqlite3 *tac_db = openDB(TAC_APP_LIST_DB);
944 _SERR("Sqlite open error");
947 sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
949 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
950 std::vector<std::string> nugets = selectDB(tac_db, sql);
958 std::string nugetPaths;
959 for (const auto &nuget : nugets) {
960 if (!nugetPaths.empty()) {
963 nugetPaths += concatPath(__DOTNET_DIR, nuget);
966 for (auto& nuget : nugets) {
967 createNIUnderTAC(concatPath(__DOTNET_DIR, nuget), nugetPaths, *pOpt);
973 ni_error_e regenerateTACNI(NIOption* opt)
975 removeNIUnderDirs(__DOTNET_DIR);
977 pkgmgrinfo_appinfo_metadata_filter_h handle;
978 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
979 if (ret != PMINFO_R_OK) {
980 return NI_ERROR_UNKNOWN;
983 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
984 if (ret != PMINFO_R_OK) {
985 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
986 return NI_ERROR_UNKNOWN;
989 ret = pkgmgrMDFilterForeach(handle, regenTacCb, &opt);
991 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
992 return NI_ERROR_UNKNOWN;
995 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
997 return NI_ERROR_NONE;
1000 static std::vector<uid_t> getUserIds()
1002 std::vector<uid_t> list;
1005 errno = 0; // so we can distinguish errors from no more entries
1006 passwd* entry = getpwent();
1009 _SERR("Error while getting userIDs");
1015 list.push_back(entry->pw_uid);
1022 static std::string getAppDataPath(const std::string& pkgId, uid_t uid)
1024 std::string pDataFile;
1026 tzplatform_set_user(uid);
1028 const char* tzUserApp = tzplatform_getenv(TZ_USER_APP);
1029 if (tzUserApp != NULL) {
1030 pDataFile = std::string(tzUserApp) + "/" + pkgId + "/data/";
1033 tzplatform_reset_user();
1038 ni_error_e removeAppProfileData(const std::string& pkgId)
1040 if (pkgId.empty()) {
1041 return NI_ERROR_INVALID_PARAMETER;
1044 std::vector<uid_t> uidList = getUserIds();
1045 for (auto& uid : uidList) {
1046 // get data path from pkgid
1047 std::string dataPath = getAppDataPath(pkgId, uid);
1048 if (!dataPath.empty() && exist(dataPath)) {
1049 std::string pDataFile = dataPath + PROFILE_BASENAME;
1051 if (exist(pDataFile)) {
1052 if (!removeFile(pDataFile)) {
1053 _SERR("Fail to remove profile data file (%s).", pDataFile.c_str());
1054 return NI_ERROR_UNKNOWN;
1056 _SOUT("Profile data (%s) is removed successfully", pDataFile.c_str());
1061 return NI_ERROR_NONE;
1064 static int appTypeListCb(pkgmgrinfo_appinfo_h handle, void *user_data)
1067 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
1068 if (ret != PMINFO_R_OK || pkgId == NULL) {
1069 _SERR("Fail to get pkgid");
1073 if (removeAppProfileData(pkgId) != NI_ERROR_NONE) {
1074 _SERR("Fail to remove profile data for (%s)", pkgId);
1080 static ni_error_e removeAppProfileByAppType(const char* type)
1084 pkgmgrinfo_appinfo_filter_h filter;
1086 ret = pkgmgrinfo_appinfo_filter_create(&filter);
1087 if (ret != PMINFO_R_OK) {
1088 _SERR("Fail to create appinfo filter");
1089 return NI_ERROR_UNKNOWN;
1092 ret = pkgmgrinfo_appinfo_filter_add_string(filter, PMINFO_APPINFO_PROP_APP_TYPE, type);
1093 if (ret != PMINFO_R_OK) {
1094 pkgmgrinfo_appinfo_filter_destroy(filter);
1095 _SERR("Fail to add appinfo filter (%s)", type);
1096 return NI_ERROR_UNKNOWN;
1099 ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(filter, appTypeListCb, NULL);
1100 if (ret != PMINFO_R_OK) {
1101 _SERR("Fail to pkgmgrinfo_pkginfo_filter_foreach_pkginfo");
1102 pkgmgrinfo_appinfo_filter_destroy(filter);
1103 return NI_ERROR_UNKNOWN;
1106 pkgmgrinfo_appinfo_filter_destroy(filter);
1108 return NI_ERROR_NONE;
1111 void removeAllAppProfileData()
1113 std::vector<const char*> appTypeList = {"dotnet", "dotnet-nui", "dotnet-inhouse"};
1115 for (auto& type : appTypeList) {
1116 if (removeAppProfileByAppType(type) != NI_ERROR_NONE) {
1117 _SERR("Fail to removeAppProfileByAppType for type (%s)", type);