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);
115 * @brief create the directory including parents directory, and
116 * copy ownership and smack labels to the created directory.
117 * @param[in] target directory path
118 * @param[in] source directory path to get ownership and smack label
119 * @return if directory created successfully, return true otherwise false
121 static bool createDirsAndCopyOwnerShip(std::string& target_path, const std::string& source)
124 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
126 for (std::string::iterator iter = target_path.begin(); iter != target_path.end();) {
127 std::string::iterator newIter = std::find(iter, target_path.end(), '/');
128 std::string newPath = std::string(target_path.begin(), newIter);
130 if (!newPath.empty()) {
131 if (stat(newPath.c_str(), &st) != 0) {
132 if (mkdir(newPath.c_str(), mode) != 0 && errno != EEXIST) {
133 _SERR("Fail to create app ni directory (%s)", newPath.c_str());
136 if (!source.empty()) {
137 copySmackAndOwnership(source, newPath);
140 if (!S_ISDIR(st.st_mode)) {
141 _SERR("Fail. path is not a dir (%s)", newPath.c_str());
147 if(newIter != target_path.end()) {
155 static std::string getNIFilePath(const std::string& dllPath)
157 size_t index = dllPath.find_last_of(".");
158 if (index == std::string::npos) {
159 _SERR("File doesnot contain extension. fail to get NI file name");
162 std::string fName = dllPath.substr(0, index);
163 std::string fExt = dllPath.substr(index, dllPath.length());
165 // crossgen generate file with lower case extension only
166 std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
167 std::string niPath = fName + ".ni" + fExt;
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 checkAppNIExistence(const std::string& absDllPath, NIOption* opt)
218 std::string absNIPath = getAppNIFilePath(absDllPath, opt);
219 if (absNIPath.empty()) {
223 if (isFile(absNIPath)) {
230 static bool checkDllExistInDir(const std::string& path)
233 auto func = [&ret](const std::string& f_path, const std::string& f_name) {
234 if (isManagedAssembly(f_name) || isNativeImage(f_name)) {
239 scanFilesInDirectory(path, func, 0);
245 * Get the list of managed files in the specific directory
246 * Absolute paths of managed files are stored at the result list.
247 * If native image already exist in the same directory, managed file is ignored.
249 static ni_error_e getTargetDllList(const std::string& path, std::vector<std::string>& fileList)
251 if (!isDirectory(path)) {
252 return NI_ERROR_INVALID_PARAMETER;
255 auto func = [&fileList](const std::string& f_path, const std::string& f_name) {
256 if (isManagedAssembly(f_path) && !checkNIExistence(f_path)) {
257 fileList.push_back(getAbsolutePath(f_path));
261 scanFilesInDirectory(path, func, 0);
263 return NI_ERROR_NONE;
267 * Get the list of managed files in the specific directory of Application
268 * Absolute paths of managed files are stored at the result list.
269 * If native image already exist in the .native_image directory, managed file is ignored.
272 static ni_error_e getAppTargetDllList(const std::string& path, std::vector<std::string>& fileList, NIOption *opt)
274 if (!isDirectory(path)) {
275 return NI_ERROR_INVALID_PARAMETER;
278 auto func = [&fileList, opt](const std::string& f_path, const std::string& f_name) {
279 if (isManagedAssembly(f_path) && !checkAppNIExistence(f_path, opt)) {
280 fileList.push_back(getAbsolutePath(f_path));
284 scanFilesInDirectory(path, func, 0);
286 return NI_ERROR_NONE;
289 static void makeArgs(std::vector<const char*>& args, const std::vector<std::string>& refPaths, NIOption* opt)
291 args.push_back(CORERUN_CMD.c_str());
292 if (CROSSGEN2_PATH != "") {
293 args.push_back(CROSSGEN2_PATH.c_str());
295 args.push_back(CROSSGEN_OPT_JITPATH);
296 args.push_back(CLRJIT_PATH.c_str());
297 args.push_back(CROSSGEN_OPT_TARGET_ARCH);
298 args.push_back(ARCHITECTURE_IDENTIFIER);
299 if (!(opt->flags & NI_FLAGS_NO_PIPELINE)) {
300 args.push_back(CROSSGEN_OPT_OUT_NEAR_INPUT);
301 args.push_back(CROSSGEN_OPT_SINGLE_FILE_COMPILATION);
303 //args.push_back(OPT_PARALLELISM);
304 //args.push_back(OPT_PARALLELISM_COUNT);
305 args.push_back(CROSSGEN_OPT_RESILIENT);
307 args.push_back(CROSSGEN_OPT_OPTIMIZE);
309 if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
310 args.push_back(CROSSGEN_OPT_INPUTBUBBLE);
311 args.push_back(CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS);
313 if (opt->flags & NI_FLAGS_INPUT_BUBBLE_REF) {
314 INPUTBUBBLE_REF_VECTOR.clear();
315 // check inputbubbleref format.
316 for (const auto &path : opt->inputBubbleRefPath) {
317 if (checkDllExistInDir(path)) {
318 INPUTBUBBLE_REF_VECTOR.push_back("--inputbubbleref:" + path + "/*.dll");
321 // add ref path to inputbubble ref
322 for (const auto &path : refPaths) {
323 if (checkDllExistInDir(path)) {
324 INPUTBUBBLE_REF_VECTOR.push_back("--inputbubbleref:" + path + "/*.dll");
327 for (const auto &path : INPUTBUBBLE_REF_VECTOR) {
328 args.push_back(path.c_str());
333 if (opt->flags & NI_FLAGS_MIBC) {
335 for (const auto &path : opt->mibcPath) {
336 MIBC_VECTOR.push_back("--mibc:" + path);
338 for (const auto &path : MIBC_VECTOR) {
339 args.push_back(path.c_str());
343 if (opt->flags & NI_FLAGS_VERBOSE) {
344 args.push_back(CROSSGEN_OPT_VERBOSE);
349 // set reference path
350 if (opt->flags & NI_FLAGS_REF) {
351 for (const auto &path : opt->refPath) {
352 REF_VECTOR.push_back("-r:" + path + "/*.dll");
355 std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
356 for (const auto &path : paths) {
357 if (checkDllExistInDir(path)) {
358 REF_VECTOR.push_back("-r:" + path + "/*.dll");
363 if (opt->flags & NI_FLAGS_EXTRA_REF) {
364 for (const auto &erPath : opt->extraRefPath) {
365 std::string path = getAbsolutePath(erPath);
366 if (checkDllExistInDir(path)) {
367 REF_VECTOR.push_back("-r:" + path + "/*.dll");
372 for (const auto &path : refPaths) {
373 if (checkDllExistInDir(path)) {
374 REF_VECTOR.push_back("-r:" + path + "/*.dll");
378 for (const auto &path : REF_VECTOR) {
379 args.push_back(path.c_str());
383 static void clearArgs(std::vector<const char*>& args)
389 static ni_error_e makePdbSymlinkForNI(std::string dllPath, std::string niPath)
391 std::string pdbPath = changeExtension(dllPath, ".dll", ".pdb");
393 if (exist(pdbPath)) {
394 std::string targetPDBPath = changeExtension(niPath, ".ni.dll", ".pdb");
395 if (!exist(targetPDBPath)) {
396 bf::create_symlink(pdbPath, targetPDBPath);
397 copySmackAndOwnership(pdbPath, targetPDBPath, true);
400 } catch (const bf::filesystem_error& error) {
401 _SERR("Fail to create symlink for %s", pdbPath.c_str());
402 return NI_ERROR_UNKNOWN;
405 return NI_ERROR_NONE;
408 static ni_error_e crossgen2PostAction(const std::string& dllPath, const std::string& niPath, NIOption* opt) {
409 if (!exist(niPath)) {
410 removeFile(changeExtension(niPath, ".ni.dll", ".ni.dll.tmp"));
411 _SERR("Fail to create native image for %s", dllPath.c_str());
412 return NI_ERROR_NO_SUCH_FILE;
414 copySmackAndOwnership(dllPath, niPath);
415 // if AppNI then move ni.dll file to .native_image and copy pdb to .native_image
416 if (opt->flags & NI_FLAGS_APPNI) {
417 std::string appNIPath = getAppNIFilePath(dllPath, opt);
418 moveFile(niPath, appNIPath);
419 makePdbSymlinkForNI(dllPath, appNIPath);
420 _SOUT("Native image %s generated successfully.", appNIPath.c_str());
422 _SOUT("Native image %s generated successfully.", niPath.c_str());
424 return NI_ERROR_NONE;
427 static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
432 return NI_ERROR_UNKNOWN;
436 waitpid(pid, &status, 0);
437 if (WIFEXITED(status)) {
438 for (auto& dllPath: dllList) {
439 ni_error_e ret = crossgen2PostAction(dllPath, changeExtension(dllPath, ".dll", ".ni.dll"), opt);
440 if (ret != NI_ERROR_NONE) {
445 _SERR("Failed. Forked process terminated abnormally");
446 return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
449 std::vector<const char*> argv;
450 makeArgs(argv, refPaths, opt);
452 // add input files at the end of parameter
453 for (const auto &input : dllList) {
454 argv.push_back(input.c_str());
455 _SOUT("+ %s", input.c_str());
459 argv.push_back(nullptr);
462 if (opt->flags & NI_FLAGS_PRINT_CMD) {
463 _SOUT("==================== NI Commands =========================");
464 for (auto &arg: argv) _SOUT("+ %s", arg);
467 execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
473 return NI_ERROR_NONE;
476 static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
478 for (auto& dllPath : dllList) {
480 if (opt->flags & NI_FLAGS_APPNI) {
481 niPath = getAppNIFilePath(dllPath, opt);
483 niPath = getNIFilePath(dllPath);
489 return NI_ERROR_UNKNOWN;
493 waitpid(pid, &status, 0);
494 if (WIFEXITED(status)) {
495 ni_error_e ret = crossgen2PostAction(dllPath, niPath, opt);
496 if (ret != NI_ERROR_NONE) {
500 _SERR("Failed. Forked process terminated abnormally");
501 _SERR("Crossgen2 was terminated by the OOM killer. Please check the system.");
502 removeFile(changeExtension(niPath, ".ni.dll", ".ni.dll.tmp"));
503 return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
506 std::vector<const char*> argv;
507 makeArgs(argv, refPaths, opt);
509 argv.push_back("-o");
510 argv.push_back(niPath.c_str());
512 argv.push_back(dllPath.c_str());
513 _SOUT("+ %s", dllPath.c_str());
516 argv.push_back(nullptr);
519 if (opt->flags & NI_FLAGS_PRINT_CMD) {
520 _SOUT("==================== NI Commands =========================");
521 for (auto &arg: argv) _SOUT("+ %s", arg);
524 execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
533 return NI_ERROR_NONE;
536 static ni_error_e createCoreLibNI(NIOption* opt)
538 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
539 std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
540 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
542 std::vector<std::string> dllList;
543 std::vector<std::string> refPaths;
544 dllList.push_back(getAbsolutePath(coreLib));
546 if (!isFile(coreLibBackup) && !isR2RImage(coreLib)) {
547 if (crossgen2NoPipeLine(dllList, refPaths, opt) == NI_ERROR_NONE && exist(niCoreLib)) {
548 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
549 _SERR("Failed to rename System.Private.CoreLib.dll");
550 return NI_ERROR_CORE_NI_FILE;
552 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
553 _SERR("Failed to rename System.Private.CoreLib.ni.dll");
554 return NI_ERROR_CORE_NI_FILE;
557 _SERR("Failed to create native image for %s", coreLib.c_str());
558 return NI_ERROR_CORE_NI_FILE;
561 return NI_ERROR_NONE;
564 static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string& refPaths, NIOption* opt)
566 ni_error_e ret = NI_ERROR_NONE;
568 if (dllList.empty()) {
569 return NI_ERROR_INVALID_PARAMETER;
571 // When performing AOT for one Dll, an error is returned when an error occurs.
572 // However, when processing multiple dlls at once, only the log for errors is output and skipped.
574 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
577 std::vector<std::string> niList;
578 for (auto it = dllList.begin(); it != dllList.end(); it++) {
581 _SERR("dll file is not exist : %s", f.c_str());
584 if (!isManagedAssembly(f)) {
585 _SERR("Input file is not a dll file : %s", f.c_str());
588 // handle System.Private.CoreLib.dll separately.
589 // dllList and path manager contain absolute path. So, there is no need to change path to absolute path
594 niList.push_back(changeExtension(f, ".dll", ".ni.dll"));
598 // In the case of SPC, post-processing is required to change the name of the native image.
599 // In order to avoid repeatedly checking whether the generated native image is an SPC,
600 // the SPC native image generation is performed separately.
602 ret = createCoreLibNI(opt);
603 if (ret != NI_ERROR_NONE) {
608 // if there is no proper input after processing dll list
609 if (dllList.empty()) {
613 return NI_ERROR_INVALID_PARAMETER;
617 std::vector<std::string> paths;
618 splitPath(refPaths, paths);
620 if (opt->flags & NI_FLAGS_NO_PIPELINE) {
621 ret = crossgen2NoPipeLine(dllList, paths, opt);
623 // When the forked process in the pipeline state is terminated(WIFSIGNALED(status)),
624 // retry the generation of the native image
625 // if the number of .dll files and the number of .ni.dll files are different.
626 for (int callCnt = 0; callCnt < 2; callCnt++) {
627 // If an error occurs, perform it twice with the same option.
628 ret = crossgen2PipeLine(dllList, paths, opt);
629 if (ret != NI_ERROR_NONE) {
630 _SERR("Crossgen2 is abnormally terminated. Regenerate native images that failed while running crossgen2.");
632 for (auto it = niList.begin(); it != niList.end(); it++) {
633 std::string niPath = *it;
634 std::string dllPath = changeExtension(niPath, ".ni.dll", ".dll");
635 if (crossgen2PostAction(dllPath, niPath, opt) != NI_ERROR_NONE) {
636 dllList.push_back(dllPath);
645 // If an error occurs after two crossgen2PipeLine() attempts,
646 // try crossgen2NoPipeLine() for the last time.
647 if (ret != NI_ERROR_NONE) {
648 _SERR("Retry running crossgen2 with --no-pipeline mode to avoid termination by OOM.");
649 ret = crossgen2NoPipeLine(dllList, paths, opt);
656 static ni_error_e doAOTFile(const std::string& dllFile, const std::string& refPaths, NIOption* opt)
658 if (!isFile(dllFile)) {
659 _SERR("dll file is not exist : %s", dllFile.c_str());
660 return NI_ERROR_NO_SUCH_FILE;
663 if (!isManagedAssembly(dllFile)) {
664 _SERR("Failed. Input parameter is not managed dll (%s)\n", dllFile.c_str());
665 return NI_ERROR_INVALID_PARAMETER;
668 if (checkNIExistence(dllFile)) {
669 _SERR("Native image file is already exist : %s", dllFile.c_str());
670 return NI_ERROR_ALREADY_EXIST;
673 std::vector<std::string> dllList;
674 dllList.push_back(getAbsolutePath(dllFile));
675 return doAOTList(dllList, refPaths, opt);
678 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
679 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
683 NIOption **pOptions = (NIOption**)userData;
685 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
686 if (ret != PMINFO_R_OK) {
687 _SERR("Failed to get pkgid");
691 if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
692 _SERR("Failed to remove previous dlls from [%s]", pkgId);
696 if (createNIUnderPkgRoot(pkgId, *pOptions) != NI_ERROR_NONE) {
697 _SERR("Failed to generate NI file [%s]", pkgId);
700 _SOUT("Complete make application to native image");
706 ni_error_e initNICommon()
708 #if defined(__arm__) || defined(__aarch64__)
711 env = getenv("MIC_CROSSGEN2_ENABLED");
712 if (env != nullptr && !strcmp(env, "1")) {
713 CORERUN_CMD = std::string("/opt/usr/dotnet/mic/crossgen2");
715 CLRJIT_PATH = std::string("/opt/usr/dotnet/mic/libclrjit_unix_") + ARCHITECTURE_IDENTIFIER + std::string("_x64.so");
718 // get interval value
719 const static std::string intervalFile = concatPath(__NATIVE_LIB_DIR, "crossgen_interval.txt");
720 std::ifstream inFile(intervalFile);
722 _SOUT("crossgen_interval.txt is found");
723 inFile >> __interval;
726 if (initializePluginManager("normal")) {
727 _SERR("Fail to initialize PluginManager");
728 return NI_ERROR_UNKNOWN;
732 __pm = new PathManager();
733 } catch (const std::exception& e) {
734 _SERR("Failed to create PathManager");
735 return NI_ERROR_UNKNOWN;
738 char* pluginDllPaths = pluginGetDllPath();
739 if (pluginDllPaths && pluginDllPaths[0] != '\0') {
740 __pm->addPlatformAssembliesPaths(pluginDllPaths, true);
743 char* pluginNativePaths = pluginGetNativeDllSearchingPath();
744 if (pluginNativePaths && pluginNativePaths[0] != '\0') {
745 __pm->addNativeDllSearchingPaths(pluginNativePaths, true);
748 return NI_ERROR_NONE;
750 _SERR("crossgen supports arm/arm64 architecture only. skip ni file generation");
751 return NI_ERROR_NOT_SUPPORTED;
755 void finalizeNICommon()
759 finalizePluginManager();
766 __ni_option = nullptr;
770 ni_error_e createNIPlatform(NIOption* opt)
772 ni_error_e ret = createNIUnderDirs(__pm->getRuntimePath(), opt);
773 if (ret != NI_ERROR_NONE) {
777 return createNIUnderDirs(__pm->getTizenFXPath(), opt);
780 ni_error_e createNIDll(const std::string& dllPath, NIOption* opt)
782 return doAOTFile(dllPath, std::string(), opt);
785 ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& refPaths, NIOption* opt)
789 // get managed file list from targetPath
790 std::vector<std::string> dllList;
791 ret = getTargetDllList(targetPath, dllList);
792 if (ret != NI_ERROR_NONE) {
796 std::vector<std::string> needNIList;
797 std::vector<std::string> niList;
799 for (auto &dll : dllList) {
800 if (!checkNIExistence(dll)) {
801 needNIList.push_back(dll);
803 niList.push_back(getNIFilePath(dll));
806 if (!needNIList.empty()) {
807 // NI fils of TAC-related dlls under /opt/usr/dotnet should not be created under .native_image directory.
808 // So, unset NI_FLAGS_APPNI temporally and restore it after running AOT.
809 opt->flags &= ~NI_FLAGS_APPNI;
810 ret = doAOTList(needNIList, refPaths, opt);
811 opt->flags |= NI_FLAGS_APPNI;
812 if (ret != NI_ERROR_NONE) {
817 for (auto &niPath : niList) {
819 std::string symNIPath = concatPath(targetPath, getFileName(niPath));
820 if (!exist(symNIPath)) {
821 bf::create_symlink(niPath, symNIPath);
822 copySmackAndOwnership(targetPath.c_str(), symNIPath.c_str(), true);
823 _SOUT("%s symbolic link file generated successfully.", symNIPath.c_str());
824 _INFO("%s symbolic link file generated successfully.", symNIPath.c_str());
829 return NI_ERROR_NONE;
833 ni_error_e createNIUnderDirs(const std::string& rootPaths, NIOption* opt)
835 ni_error_e ret = NI_ERROR_NONE;
837 std::vector<std::string> fileList;
838 std::vector<std::string> paths;
839 splitPath(rootPaths, paths);
841 for (const auto &path : paths) {
846 if (path.find(TAC_SYMLINK_SUB_DIR) != std::string::npos) {
847 ret = createNIUnderTAC(path, rootPaths, opt);
848 if (ret != NI_ERROR_NONE) {
851 } else if (opt->flags & NI_FLAGS_APPNI) {
852 ret = getAppTargetDllList(path, fileList, opt);
853 if (ret != NI_ERROR_NONE) {
857 ret = getTargetDllList(path, fileList);
858 if (ret != NI_ERROR_NONE) {
864 if (fileList.empty()) {
865 return NI_ERROR_NONE;
868 return doAOTList(fileList, rootPaths, opt);
871 ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
873 if (!isR2RImage(concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll"))) {
874 _SERR("The native image of System.Private.CoreLib does not exist.\n"
875 "Run the command to create the native image\n"
876 "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll");
877 return NI_ERROR_CORE_NI_FILE;
880 std::string rootPath = getRootPath(pkgId);
881 if (rootPath.empty()) {
882 _SERR("Failed to get root path from [%s]", pkgId.c_str());
883 return NI_ERROR_INVALID_PACKAGE;
886 __pm->setAppRootPath(rootPath);
888 char* extraDllPaths = pluginGetExtraDllPath();
889 if (extraDllPaths && extraDllPaths[0] != '\0') {
890 opt->flags |= NI_FLAGS_EXTRA_REF;
891 splitPath(extraDllPaths, opt->extraRefPath);
894 opt->flags |= NI_FLAGS_APPNI;
896 if (isReadOnlyArea(rootPath)) {
897 opt->flags |= NI_FLAGS_APP_UNDER_RO_AREA;
898 opt->flags |= NI_FLAGS_NO_PIPELINE;
899 _SERR("Only no-pipeline mode supported for RO app. Set no-pipeline option forcibly");
901 opt->flags &= ~NI_FLAGS_APP_UNDER_RO_AREA;
904 // create native image under bin and lib directory
905 // tac directory is skipped in the createNIUnderDirs.
906 return createNIUnderDirs(__pm->getAppPaths(), opt);
909 void removeNIPlatform()
911 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
912 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
914 if (isR2RImage(coreLib)) {
915 if (!isFile(coreLibBackup)) {
919 if (remove(coreLib.c_str())) {
920 _SERR("Failed to remove System.Private.CoreLib native image file");
922 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
923 _SERR("Failed to rename System.Private.CoreLib.Backup to origin");
927 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
928 if (isFile(__SYSTEM_BASE_FILE)) {
929 if (remove(__SYSTEM_BASE_FILE)) {
930 _SERR("Failed to remove %s", __SYSTEM_BASE_FILE);
935 removeNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath());
938 void removeNIUnderDirs(const std::string& rootPaths)
940 auto convert = [](const std::string& path, const std::string& filename) {
941 if (isNativeImage(path)) {
942 if (remove(path.c_str())) {
943 _SERR("Failed to remove %s", path.c_str());
948 std::vector<std::string> paths;
949 splitPath(rootPaths, paths);
950 for (const auto &path : paths) {
951 scanFilesInDirectory(path, convert, -1);
955 ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
957 std::string rootPath = getRootPath(pkgId);
958 if (rootPath.empty()) {
959 _SERR("Failed to get root path from [%s]", pkgId.c_str());
960 return NI_ERROR_INVALID_PACKAGE;
963 __pm->setAppRootPath(rootPath);
965 // getAppNIPaths returns bin/.native_image, lib/.native_image and .tac_symlink.
966 std::string appNIPaths = __pm->getAppNIPaths();
967 std::vector<std::string> paths;
968 splitPath(appNIPaths, paths);
969 for (const auto &path : paths) {
970 if (!isReadOnlyArea(path)) {
971 // Only the native image inside the TAC should be removed.
972 if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
973 removeNIUnderDirs(path);
975 if (isDirectory(path)) {
976 if (!removeAll(path.c_str())) {
977 _SERR("Failed to remove app ni dir [%s]", path.c_str());
984 // In special cases, the ni file may exist in the dll location.
985 // The code below is to avoid this exceptional case.
986 std::string appPaths = __pm->getAppPaths();
987 splitPath(appPaths, paths);
988 for (const auto &path : paths) {
989 if (isDirectory(path)) {
990 removeNIUnderDirs(path);
994 return NI_ERROR_NONE;
997 ni_error_e regenerateAppNI(NIOption* opt)
1000 pkgmgrinfo_appinfo_metadata_filter_h handle;
1002 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
1003 if (ret != PMINFO_R_OK)
1004 return NI_ERROR_UNKNOWN;
1006 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
1007 if (ret != PMINFO_R_OK) {
1008 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1009 return NI_ERROR_UNKNOWN;
1012 ret = pkgmgrMDFilterForeach(handle, appAotCb, &opt);
1014 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1015 return NI_ERROR_UNKNOWN;
1018 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1019 return NI_ERROR_NONE;
1022 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
1023 static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
1026 NIOption **pOpt = (NIOption**)userData;
1028 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
1029 if (ret != PMINFO_R_OK || pkgId == NULL) {
1030 _SERR("Failed to get pkgid");
1034 sqlite3 *tac_db = openDB(TAC_APP_LIST_DB);
1036 _SERR("Sqlite open error");
1039 sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
1041 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
1042 std::vector<std::string> nugets = selectDB(tac_db, sql);
1050 std::string nugetPaths;
1051 for (const auto &nuget : nugets) {
1052 if (!nugetPaths.empty()) {
1055 nugetPaths += concatPath(__DOTNET_DIR, nuget);
1058 for (auto& nuget : nugets) {
1059 createNIUnderTAC(concatPath(__DOTNET_DIR, nuget), nugetPaths, *pOpt);
1065 ni_error_e regenerateTACNI(NIOption* opt)
1067 removeNIUnderDirs(__DOTNET_DIR);
1069 pkgmgrinfo_appinfo_metadata_filter_h handle;
1070 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
1071 if (ret != PMINFO_R_OK) {
1072 return NI_ERROR_UNKNOWN;
1075 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE);
1076 if (ret != PMINFO_R_OK) {
1077 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1078 return NI_ERROR_UNKNOWN;
1081 ret = pkgmgrMDFilterForeach(handle, regenTacCb, &opt);
1083 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1084 return NI_ERROR_UNKNOWN;
1087 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1089 return NI_ERROR_NONE;
1092 static std::vector<uid_t> getUserIds()
1094 std::vector<uid_t> list;
1097 errno = 0; // so we can distinguish errors from no more entries
1098 passwd* entry = getpwent();
1101 _SERR("Error while getting userIDs");
1107 list.push_back(entry->pw_uid);
1114 static std::string getAppDataPath(const std::string& pkgId, uid_t uid)
1116 std::string pDataFile;
1118 tzplatform_set_user(uid);
1120 const char* tzUserApp = tzplatform_getenv(TZ_USER_APP);
1121 if (tzUserApp != NULL) {
1122 pDataFile = std::string(tzUserApp) + "/" + pkgId + "/data/";
1125 tzplatform_reset_user();
1130 ni_error_e removeAppProfileData(const std::string& pkgId)
1132 if (pkgId.empty()) {
1133 return NI_ERROR_INVALID_PARAMETER;
1136 std::vector<uid_t> uidList = getUserIds();
1137 for (auto& uid : uidList) {
1138 // get data path from pkgid
1139 std::string dataPath = getAppDataPath(pkgId, uid);
1140 if (!dataPath.empty() && exist(dataPath)) {
1141 std::string pDataFile = dataPath + PROFILE_BASENAME;
1143 if (exist(pDataFile)) {
1144 if (!removeFile(pDataFile)) {
1145 _SERR("Fail to remove profile data file (%s).", pDataFile.c_str());
1146 return NI_ERROR_UNKNOWN;
1148 _SOUT("Profile data (%s) is removed successfully", pDataFile.c_str());
1153 return NI_ERROR_NONE;
1156 static int appTypeListCb(pkgmgrinfo_appinfo_h handle, void *user_data)
1159 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
1160 if (ret != PMINFO_R_OK || pkgId == NULL) {
1161 _SERR("Fail to get pkgid");
1165 if (removeAppProfileData(pkgId) != NI_ERROR_NONE) {
1166 _SERR("Fail to remove profile data for (%s)", pkgId);
1172 static ni_error_e removeAppProfileByAppType(const char* type)
1176 pkgmgrinfo_appinfo_filter_h filter;
1178 ret = pkgmgrinfo_appinfo_filter_create(&filter);
1179 if (ret != PMINFO_R_OK) {
1180 _SERR("Fail to create appinfo filter");
1181 return NI_ERROR_UNKNOWN;
1184 ret = pkgmgrinfo_appinfo_filter_add_string(filter, PMINFO_APPINFO_PROP_APP_TYPE, type);
1185 if (ret != PMINFO_R_OK) {
1186 pkgmgrinfo_appinfo_filter_destroy(filter);
1187 _SERR("Fail to add appinfo filter (%s)", type);
1188 return NI_ERROR_UNKNOWN;
1191 ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(filter, appTypeListCb, NULL);
1192 if (ret != PMINFO_R_OK) {
1193 _SERR("Fail to pkgmgrinfo_pkginfo_filter_foreach_pkginfo");
1194 pkgmgrinfo_appinfo_filter_destroy(filter);
1195 return NI_ERROR_UNKNOWN;
1198 pkgmgrinfo_appinfo_filter_destroy(filter);
1200 return NI_ERROR_NONE;
1203 void removeAllAppProfileData()
1205 std::vector<const char*> appTypeList = {"dotnet", "dotnet-nui", "dotnet-inhouse"};
1207 for (auto& type : appTypeList) {
1208 if (removeAppProfileByAppType(type) != NI_ERROR_NONE) {
1209 _SERR("Fail to removeAppProfileByAppType for type (%s)", type);