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 bool isCoreLibPrepared()
107 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
108 if (isFile(coreLibBackup)) {
111 _SERR("The native image of System.Private.CoreLib does not exist\n"
112 "Run the command to create the native image\n"
113 "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll\n");
118 static bool hasCoreLibNI()
120 std::string ildasm = concatPath(__pm->getRuntimePath(), "ildasm");
121 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
122 std::string cmd = ildasm + " " + coreLib + " -noil -stats | grep '.rsrc'";
125 fp = popen(cmd.c_str(), "r");
128 if (fgets(buff, sizeof(buff), fp) != NULL) {
137 static void waitInterval()
139 // by the recommand, ignore small value for performance.
140 if (__interval > 10000) {
141 _SOUT("sleep %d usec", __interval);
146 static std::string getNIFilePath(const std::string& dllPath)
148 size_t index = dllPath.find_last_of(".");
149 if (index == std::string::npos) {
150 _SERR("File doesnot contain extension. fail to get NI file name");
153 std::string fName = dllPath.substr(0, index);
154 std::string fExt = dllPath.substr(index, dllPath.length());
156 // crossgen generate file with lower case extension only
157 std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
158 std::string niPath = fName + ".ni" + fExt;
164 * @brief create the directory including parents directory, and
165 * copy ownership and smack labels to the created directory.
166 * @param[in] target directory path
167 * @param[in] source directory path to get ownership and smack label
168 * @return if directory created successfully, return true otherwise false
170 static bool createDirsAndCopyOwnerShip(std::string& target_path, const std::string& source)
173 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
175 for (std::string::iterator iter = target_path.begin(); iter != target_path.end();) {
176 std::string::iterator newIter = std::find(iter, target_path.end(), '/');
177 std::string newPath = std::string(target_path.begin(), newIter);
179 if (!newPath.empty()) {
180 if (stat(newPath.c_str(), &st) != 0) {
181 if (mkdir(newPath.c_str(), mode) != 0 && errno != EEXIST) {
182 _SERR("Fail to create app ni directory (%s)", newPath.c_str());
185 if (!source.empty()) {
186 copySmackAndOwnership(source, newPath);
189 if (!S_ISDIR(st.st_mode)) {
190 _SERR("Fail. path is not a dir (%s)", newPath.c_str());
196 if(newIter != target_path.end()) {
204 static std::string getAppNIFilePath(const std::string& absDllPath, NIOption* opt)
206 std::string niDirPath;
207 std::string prevPath;
209 prevPath = getBaseName(absDllPath);
210 niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
212 if (opt->flags & NI_FLAGS_APP_UNDER_RO_AREA) {
213 niDirPath = replaceAll(niDirPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
214 _SERR("App is installed in RO area. Change NI path to RW area(%s).", niDirPath.c_str());
215 _ERR("App is installed in RO area. Change NI path to RW area(%s).", niDirPath.c_str());
218 if (!isDirectory(niDirPath)) {
219 if (!createDirsAndCopyOwnerShip(niDirPath, prevPath)) {
220 niDirPath = prevPath;
221 _SERR("fail to create dir (%s)", niDirPath.c_str());
225 return getNIFilePath(concatPath(niDirPath, getFileName(absDllPath)));
228 static bool checkNIExistence(const std::string& absDllPath)
230 std::string absNIPath = getNIFilePath(absDllPath);
231 if (absNIPath.empty()) {
235 if (isFile(absNIPath)) {
239 // native image of System.Private.CoreLib.dll should have to overwrite
240 // original file to support new coreclr
241 if (absDllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
242 if (hasCoreLibNI()) {
250 static bool checkDllExistInDir(const std::string& path)
253 auto func = [&ret](const std::string& f_path, const std::string& f_name) {
254 if (isManagedAssembly(f_name) || isNativeImage(f_name)) {
259 scanFilesInDirectory(path, func, 0);
265 * Get the list of managed files in the specific directory
266 * Absolute paths of managed files are stored at the result list.
267 * If native image already exist in the same directory, managed file is ignored.
269 * Note: System.Private.CoreLib is excluded from the result list.
271 static ni_error_e getTargetDllList(const std::string& path, std::vector<std::string>& fileList)
273 if (!isDirectory(path)) {
274 return NI_ERROR_INVALID_PARAMETER;
277 auto func = [&fileList](const std::string& f_path, const std::string& f_name) {
278 if (isManagedAssembly(f_path) && !checkNIExistence(f_path)) {
279 if (f_path.find("System.Private.CoreLib") == std::string::npos) {
280 fileList.push_back(getAbsolutePath(f_path));
285 scanFilesInDirectory(path, func, 0);
287 return NI_ERROR_NONE;
290 static void makeArgs(std::vector<const char*>& args, const std::vector<std::string>& refPaths, NIOption* opt)
292 args.push_back(CORERUN_CMD.c_str());
293 if (CROSSGEN2_PATH != "") {
294 args.push_back(CROSSGEN2_PATH.c_str());
296 args.push_back(CROSSGEN_OPT_JITPATH);
297 args.push_back(CLRJIT_PATH.c_str());
298 args.push_back(CROSSGEN_OPT_TARGET_ARCH);
299 args.push_back(ARCHITECTURE_IDENTIFIER);
300 if (!(opt->flags & NI_FLAGS_NO_PIPELINE)) {
301 args.push_back(CROSSGEN_OPT_OUT_NEAR_INPUT);
302 args.push_back(CROSSGEN_OPT_SINGLE_FILE_COMPILATION);
304 //args.push_back(OPT_PARALLELISM);
305 //args.push_back(OPT_PARALLELISM_COUNT);
306 args.push_back(CROSSGEN_OPT_RESILIENT);
308 args.push_back(CROSSGEN_OPT_OPTIMIZE);
310 if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
311 args.push_back(CROSSGEN_OPT_INPUTBUBBLE);
312 args.push_back(CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS);
314 if (opt->flags & NI_FLAGS_INPUT_BUBBLE_REF) {
315 INPUTBUBBLE_REF_VECTOR.clear();
316 // check inputbubbleref format.
317 for (const auto &path : opt->inputBubbleRefPath) {
318 if (checkDllExistInDir(path)) {
319 INPUTBUBBLE_REF_VECTOR.push_back("--inputbubbleref:" + path + "/*.dll");
322 // add ref path to inputbubble ref
323 for (const auto &path : refPaths) {
324 if (checkDllExistInDir(path)) {
325 INPUTBUBBLE_REF_VECTOR.push_back("--inputbubbleref:" + path + "/*.dll");
328 for (const auto &path : INPUTBUBBLE_REF_VECTOR) {
329 args.push_back(path.c_str());
334 if (opt->flags & NI_FLAGS_MIBC) {
336 for (const auto &path : opt->mibcPath) {
337 MIBC_VECTOR.push_back("--mibc:" + path);
339 for (const auto &path : MIBC_VECTOR) {
340 args.push_back(path.c_str());
344 if (opt->flags & NI_FLAGS_VERBOSE) {
345 args.push_back(CROSSGEN_OPT_VERBOSE);
350 // set reference path
351 if (opt->flags & NI_FLAGS_REF) {
352 for (const auto &path : opt->refPath) {
353 REF_VECTOR.push_back("-r:" + path + "/*.dll");
356 std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
357 for (const auto &path : paths) {
358 if (checkDllExistInDir(path)) {
359 REF_VECTOR.push_back("-r:" + path + "/*.dll");
364 for (const auto &path : refPaths) {
365 if (checkDllExistInDir(path)) {
366 REF_VECTOR.push_back("-r:" + path + "/*.dll");
370 for (const auto &path : REF_VECTOR) {
371 args.push_back(path.c_str());
375 static void clearArgs(std::vector<const char*>& args)
381 static ni_error_e makePdbSymlinkForNI(std::string dllPath, std::string niPath)
383 std::string pdbPath = changeExtension(dllPath, ".dll", ".pdb");
385 if (exist(pdbPath)) {
386 std::string targetPDBPath = changeExtension(niPath, ".ni.dll", ".pdb");
387 if (!exist(targetPDBPath)) {
388 bf::create_symlink(pdbPath, targetPDBPath);
389 copySmackAndOwnership(pdbPath, targetPDBPath, true);
392 } catch (const bf::filesystem_error& error) {
393 _SERR("Fail to create symlink for %s", pdbPath.c_str());
394 return NI_ERROR_UNKNOWN;
397 return NI_ERROR_NONE;
400 static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
405 return NI_ERROR_UNKNOWN;
409 waitpid(pid, &status, 0);
410 if (WIFEXITED(status)) {
411 for (auto& dllPath: dllList) {
412 std::string niPath = changeExtension(dllPath, ".dll", ".ni.dll");
414 if (!exist(niPath)) {
415 _SERR("Fail to create native image for %s", dllPath.c_str());
416 return NI_ERROR_NO_SUCH_FILE;
419 copySmackAndOwnership(dllPath, niPath);
420 // if AppNI then move ni.dll file to .native_image and copy pdb to .native_image
421 if (opt->flags & NI_FLAGS_APPNI) {
422 std::string appNIPath = getAppNIFilePath(dllPath, opt);
423 moveFile(niPath, appNIPath);
424 makePdbSymlinkForNI(dllPath, appNIPath);
428 _SOUT("Native image %s generated successfully.", niPath.c_str());
431 _SERR("Failed. Forked process terminated abnormally");
434 std::vector<const char*> argv;
435 makeArgs(argv, refPaths, opt);
437 // add input files at the end of parameter
438 for (const auto &input : dllList) {
439 argv.push_back(input.c_str());
440 _SOUT("+ %s", input.c_str());
444 argv.push_back(nullptr);
447 if (opt->flags & NI_FLAGS_PRINT_CMD) {
448 _SOUT("==================== NI Commands =========================");
449 for (auto &arg: argv) _SOUT("+ %s", arg);
452 execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
458 return NI_ERROR_NONE;
461 static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
463 for (auto& dllPath : dllList) {
465 if (opt->flags & NI_FLAGS_APPNI) {
466 niPath = getAppNIFilePath(dllPath, opt);
468 niPath = getNIFilePath(dllPath);
474 return NI_ERROR_UNKNOWN;
478 waitpid(pid, &status, 0);
479 if (WIFEXITED(status)) {
480 if (!exist(niPath)) {
481 _SERR("Fail to create native image for %s", dllPath.c_str());
482 return NI_ERROR_NO_SUCH_FILE;
485 copySmackAndOwnership(dllPath, niPath);
486 if (opt->flags & NI_FLAGS_APPNI) {
487 makePdbSymlinkForNI(dllPath, niPath);
490 _SOUT("Native image %s generated successfully.", niPath.c_str());
492 _SERR("Failed. Forked process terminated abnormally");
495 std::vector<const char*> argv;
496 makeArgs(argv, refPaths, opt);
498 argv.push_back("-o");
499 argv.push_back(niPath.c_str());
501 argv.push_back(dllPath.c_str());
502 _SOUT("+ %s", dllPath.c_str());
505 argv.push_back(nullptr);
508 if (opt->flags & NI_FLAGS_PRINT_CMD) {
509 _SOUT("==================== NI Commands =========================");
510 for (auto &arg: argv) _SOUT("+ %s", arg);
513 execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
522 return NI_ERROR_NONE;
525 static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string& refPaths, NIOption* opt)
527 if (dllList.empty()) {
528 return NI_ERROR_INVALID_PARAMETER;
530 // When performing AOT for one Dll, an error is returned when an error occurs.
531 // However, when processing multiple dlls at once, only the log for errors is output and skipped.
533 for (auto it = dllList.begin(); it != dllList.end(); it++) {
536 _SERR("dll file is not exist : %s", f.c_str());
539 if (!isManagedAssembly(f)) {
540 _SERR("Input file is not a dll file : %s", f.c_str());
545 std::vector<std::string> paths;
546 splitPath(refPaths, paths);
548 if (opt->flags & NI_FLAGS_NO_PIPELINE) {
549 crossgen2NoPipeLine(dllList, paths, opt);
551 crossgen2PipeLine(dllList, paths, opt);
554 return NI_ERROR_NONE;
557 static ni_error_e doAOTFile(const std::string& dllFile, const std::string& refPaths, NIOption* opt)
559 if (!isFile(dllFile)) {
560 _SERR("dll file is not exist : %s", dllFile.c_str());
561 return NI_ERROR_NO_SUCH_FILE;
564 if (!isManagedAssembly(dllFile)) {
565 _SERR("Failed. Input parameter is not managed dll (%s)\n", dllFile.c_str());
566 return NI_ERROR_INVALID_PARAMETER;
569 std::vector<std::string> dllList;
570 dllList.push_back(getAbsolutePath(dllFile));
571 return doAOTList(dllList, refPaths, opt);
574 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
575 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
579 NIOption **pOptions = (NIOption**)userData;
581 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
582 if (ret != PMINFO_R_OK) {
583 _SERR("Failed to get pkgid");
587 if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
588 _SERR("Failed to remove previous dlls from [%s]", pkgId);
592 if (createNIUnderPkgRoot(pkgId, *pOptions) != NI_ERROR_NONE) {
593 _SERR("Failed to generate NI file [%s]", pkgId);
596 _SOUT("Complete make application to native image");
602 static ni_error_e createCoreLibNI(NIOption* opt)
604 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
605 std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
606 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
608 if (!isFile(coreLibBackup) && !hasCoreLibNI()) {
609 if (doAOTFile(coreLib, std::string(), opt) == NI_ERROR_NONE && exist(niCoreLib)) {
610 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
611 _SERR("Failed to rename System.Private.CoreLib.dll");
612 return NI_ERROR_CORE_NI_FILE;
614 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
615 _SERR("Failed to rename System.Private.CoreLib.ni.dll");
616 return NI_ERROR_CORE_NI_FILE;
619 _SERR("Failed to create native image for %s", coreLib.c_str());
620 return NI_ERROR_CORE_NI_FILE;
623 return NI_ERROR_NONE;
626 ni_error_e initNICommon()
628 #if defined(__arm__) || defined(__aarch64__)
631 env = getenv("MIC_CROSSGEN2_ENABLED");
632 if (env != nullptr && !strcmp(env, "1")) {
633 CORERUN_CMD = std::string("/opt/usr/dotnet/mic/crossgen2");
635 CLRJIT_PATH = std::string("/opt/usr/dotnet/mic/libclrjit_unix_") + ARCHITECTURE_IDENTIFIER + std::string("_x64.so");
638 // get interval value
639 const static std::string intervalFile = concatPath(__NATIVE_LIB_DIR, "crossgen_interval.txt");
640 std::ifstream inFile(intervalFile);
642 _SOUT("crossgen_interval.txt is found");
643 inFile >> __interval;
646 if (initializePluginManager("normal")) {
647 _SERR("Fail to initialize PluginManager");
648 return NI_ERROR_UNKNOWN;
652 __pm = new PathManager();
653 } catch (const std::exception& e) {
654 _SERR("Failed to create PathManager");
655 return NI_ERROR_UNKNOWN;
658 char* pluginDllPaths = pluginGetDllPath();
659 if (pluginDllPaths && pluginDllPaths[0] != '\0') {
660 __pm->addPlatformAssembliesPaths(pluginDllPaths, true);
663 char* pluginNativePaths = pluginGetNativeDllSearchingPath();
664 if (pluginNativePaths && pluginNativePaths[0] != '\0') {
665 __pm->addNativeDllSearchingPaths(pluginNativePaths, true);
668 return NI_ERROR_NONE;
670 _SERR("crossgen supports arm/arm64 architecture only. skip ni file generation");
671 return NI_ERROR_NOT_SUPPORTED;
675 void finalizeNICommon()
679 finalizePluginManager();
686 __ni_option = nullptr;
690 ni_error_e createNIPlatform(NIOption* opt)
692 ni_error_e ret = createCoreLibNI(opt);
693 if (ret != NI_ERROR_NONE) {
697 ret = createNIUnderDirs(__pm->getRuntimePath(), opt);
698 if (ret != NI_ERROR_NONE) {
702 return createNIUnderDirs(__pm->getTizenFXPath(), opt);
705 ni_error_e createNIDll(const std::string& dllPath, NIOption* opt)
707 if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
708 return createCoreLibNI(opt);
711 if (!isCoreLibPrepared()) {
712 return NI_ERROR_CORE_NI_FILE;
715 return doAOTFile(dllPath, std::string(), opt);
718 ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& refPaths, NIOption* opt)
722 // get managed file list from targetPath
723 std::vector<std::string> dllList;
724 ret = getTargetDllList(targetPath, dllList);
725 if (ret != NI_ERROR_NONE) {
729 std::vector<std::string> needNIList;
730 std::vector<std::string> niList;
732 for (auto &dll : dllList) {
733 if (!checkNIExistence(dll)) {
734 needNIList.push_back(dll);
736 niList.push_back(getNIFilePath(dll));
739 if (!needNIList.empty()) {
740 // NI fils of TAC-related dlls under /opt/usr/dotnet should not be created under .native_image directory.
741 // So, unset NI_FLAGS_APPNI temporally and restore it after running AOT.
742 opt->flags &= ~NI_FLAGS_APPNI;
743 ret = doAOTList(needNIList, refPaths, opt);
744 opt->flags |= NI_FLAGS_APPNI;
745 if (ret != NI_ERROR_NONE) {
750 for (auto &niPath : niList) {
752 std::string symNIPath = concatPath(targetPath, getFileName(niPath));
753 if (!exist(symNIPath)) {
754 bf::create_symlink(niPath, symNIPath);
755 copySmackAndOwnership(targetPath.c_str(), symNIPath.c_str(), true);
756 _SOUT("%s symbolic link file generated successfully.", symNIPath.c_str());
757 _INFO("%s symbolic link file generated successfully.", symNIPath.c_str());
762 return NI_ERROR_NONE;
766 ni_error_e createNIUnderDirs(const std::string& rootPaths, NIOption* opt)
768 ni_error_e ret = NI_ERROR_NONE;
769 if (!isCoreLibPrepared()) {
770 return NI_ERROR_CORE_NI_FILE;
773 std::vector<std::string> fileList;
774 std::vector<std::string> paths;
775 splitPath(rootPaths, paths);
777 for (const auto &path : paths) {
782 if (path.find(TAC_SYMLINK_SUB_DIR) != std::string::npos) {
783 ret = createNIUnderTAC(path, rootPaths, opt);
784 if (ret != NI_ERROR_NONE) {
788 ret = getTargetDllList(path, fileList);
789 if (ret != NI_ERROR_NONE) {
795 if (fileList.empty()) {
796 return NI_ERROR_NONE;
799 return doAOTList(fileList, rootPaths, opt);
802 ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
804 std::string rootPath = getRootPath(pkgId);
805 if (rootPath.empty()) {
806 _SERR("Failed to get root path from [%s]", pkgId.c_str());
807 return NI_ERROR_INVALID_PACKAGE;
810 __pm->setAppRootPath(rootPath);
812 char* extraDllPaths = pluginGetExtraDllPath();
813 if (extraDllPaths && extraDllPaths[0] != '\0') {
814 __pm->setExtraDllPaths(extraDllPaths);
817 opt->flags |= NI_FLAGS_APPNI;
819 if (isReadOnlyArea(rootPath)) {
820 opt->flags |= NI_FLAGS_APP_UNDER_RO_AREA;
821 opt->flags |= NI_FLAGS_NO_PIPELINE;
822 _SERR("Only no-pipeline mode supported for RO app. Set no-pipeline option forcibly");
824 opt->flags &= ~NI_FLAGS_APP_UNDER_RO_AREA;
827 // create native image under bin and lib directory
828 // tac directory is skipped in the createNIUnderDirs.
829 return createNIUnderDirs(__pm->getAppPaths(), opt);
832 void removeNIPlatform()
834 std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
835 std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
837 if (hasCoreLibNI()) {
838 if (!isFile(coreLibBackup)) {
842 if (remove(coreLib.c_str())) {
843 _SERR("Failed to remove System.Private.CoreLib native image file");
845 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
846 _SERR("Failed to rename System.Private.CoreLib.Backup to origin");
850 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
851 if (isFile(__SYSTEM_BASE_FILE)) {
852 if (remove(__SYSTEM_BASE_FILE)) {
853 _SERR("Failed to remove %s", __SYSTEM_BASE_FILE);
858 removeNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath());
861 void removeNIUnderDirs(const std::string& rootPaths)
863 auto convert = [](const std::string& path, const std::string& filename) {
864 if (isNativeImage(path)) {
865 if (remove(path.c_str())) {
866 _SERR("Failed to remove %s", path.c_str());
871 std::vector<std::string> paths;
872 splitPath(rootPaths, paths);
873 for (const auto &path : paths) {
874 scanFilesInDirectory(path, convert, -1);
878 ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
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 // getAppNIPaths returns bin/.native_image, lib/.native_image and .tac_symlink.
889 std::string appNIPaths = __pm->getAppNIPaths();
890 std::vector<std::string> paths;
891 splitPath(appNIPaths, paths);
892 for (const auto &path : paths) {
893 if (!isReadOnlyArea(path)) {
894 // Only the native image inside the TAC should be removed.
895 if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
896 removeNIUnderDirs(path);
898 if (isDirectory(path)) {
899 if (!removeAll(path.c_str())) {
900 _SERR("Failed to remove app ni dir [%s]", path.c_str());
907 // In special cases, the ni file may exist in the dll location.
908 // The code below is to avoid this exceptional case.
909 std::string appPaths = __pm->getAppPaths();
910 splitPath(appPaths, paths);
911 for (const auto &path : paths) {
912 if (isDirectory(path)) {
913 removeNIUnderDirs(path);
917 return NI_ERROR_NONE;
920 ni_error_e regenerateAppNI(NIOption* opt)
922 if (!isCoreLibPrepared()) {
923 return NI_ERROR_CORE_NI_FILE;
927 pkgmgrinfo_appinfo_metadata_filter_h handle;
929 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
930 if (ret != PMINFO_R_OK)
931 return NI_ERROR_UNKNOWN;
933 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
934 if (ret != PMINFO_R_OK) {
935 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
936 return NI_ERROR_UNKNOWN;
939 ret = pkgmgrMDFilterForeach(handle, appAotCb, &opt);
941 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
942 return NI_ERROR_UNKNOWN;
945 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
946 return NI_ERROR_NONE;
949 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
950 static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
953 NIOption **pOpt = (NIOption**)userData;
955 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
956 if (ret != PMINFO_R_OK || pkgId == NULL) {
957 _SERR("Failed to get pkgid");
961 sqlite3 *tac_db = openDB(TAC_APP_LIST_DB);
963 _SERR("Sqlite open error");
966 sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
968 char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
969 std::vector<std::string> nugets = selectDB(tac_db, sql);
977 std::string nugetPaths;
978 for (const auto &nuget : nugets) {
979 if (!nugetPaths.empty()) {
982 nugetPaths += concatPath(__DOTNET_DIR, nuget);
985 for (auto& nuget : nugets) {
986 createNIUnderTAC(concatPath(__DOTNET_DIR, nuget), nugetPaths, *pOpt);
992 ni_error_e regenerateTACNI(NIOption* opt)
994 if (!isCoreLibPrepared()) {
995 return NI_ERROR_CORE_NI_FILE;
998 removeNIUnderDirs(__DOTNET_DIR);
1000 pkgmgrinfo_appinfo_metadata_filter_h handle;
1001 int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
1002 if (ret != PMINFO_R_OK) {
1003 return NI_ERROR_UNKNOWN;
1006 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_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, regenTacCb, &opt);
1014 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1015 return NI_ERROR_UNKNOWN;
1018 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1020 return NI_ERROR_NONE;
1023 static std::vector<uid_t> getUserIds()
1025 std::vector<uid_t> list;
1028 errno = 0; // so we can distinguish errors from no more entries
1029 passwd* entry = getpwent();
1032 _SERR("Error while getting userIDs");
1038 list.push_back(entry->pw_uid);
1045 static std::string getAppDataPath(const std::string& pkgId, uid_t uid)
1047 std::string pDataFile;
1049 tzplatform_set_user(uid);
1051 const char* tzUserApp = tzplatform_getenv(TZ_USER_APP);
1052 if (tzUserApp != NULL) {
1053 pDataFile = std::string(tzUserApp) + "/" + pkgId + "/data/";
1056 tzplatform_reset_user();
1061 ni_error_e removeAppProfileData(const std::string& pkgId)
1063 if (pkgId.empty()) {
1064 return NI_ERROR_INVALID_PARAMETER;
1067 std::vector<uid_t> uidList = getUserIds();
1068 for (auto& uid : uidList) {
1069 // get data path from pkgid
1070 std::string dataPath = getAppDataPath(pkgId, uid);
1071 if (!dataPath.empty() && exist(dataPath)) {
1072 std::string pDataFile = dataPath + PROFILE_BASENAME;
1074 if (exist(pDataFile)) {
1075 if (!removeFile(pDataFile)) {
1076 _SERR("Fail to remove profile data file (%s).", pDataFile.c_str());
1077 return NI_ERROR_UNKNOWN;
1079 _SOUT("Profile data (%s) is removed successfully", pDataFile.c_str());
1084 return NI_ERROR_NONE;
1087 static int appTypeListCb(pkgmgrinfo_appinfo_h handle, void *user_data)
1090 int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
1091 if (ret != PMINFO_R_OK || pkgId == NULL) {
1092 _SERR("Fail to get pkgid");
1096 if (removeAppProfileData(pkgId) != NI_ERROR_NONE) {
1097 _SERR("Fail to remove profile data for (%s)", pkgId);
1103 static ni_error_e removeAppProfileByAppType(const char* type)
1107 pkgmgrinfo_appinfo_filter_h filter;
1109 ret = pkgmgrinfo_appinfo_filter_create(&filter);
1110 if (ret != PMINFO_R_OK) {
1111 _SERR("Fail to create appinfo filter");
1112 return NI_ERROR_UNKNOWN;
1115 ret = pkgmgrinfo_appinfo_filter_add_string(filter, PMINFO_APPINFO_PROP_APP_TYPE, type);
1116 if (ret != PMINFO_R_OK) {
1117 pkgmgrinfo_appinfo_filter_destroy(filter);
1118 _SERR("Fail to add appinfo filter (%s)", type);
1119 return NI_ERROR_UNKNOWN;
1122 ret = pkgmgrinfo_appinfo_filter_foreach_appinfo(filter, appTypeListCb, NULL);
1123 if (ret != PMINFO_R_OK) {
1124 _SERR("Fail to pkgmgrinfo_pkginfo_filter_foreach_pkginfo");
1125 pkgmgrinfo_appinfo_filter_destroy(filter);
1126 return NI_ERROR_UNKNOWN;
1129 pkgmgrinfo_appinfo_filter_destroy(filter);
1131 return NI_ERROR_NONE;
1134 void removeAllAppProfileData()
1136 std::vector<const char*> appTypeList = {"dotnet", "dotnet-nui", "dotnet-inhouse"};
1138 for (auto& type : appTypeList) {
1139 if (removeAppProfileByAppType(type) != NI_ERROR_NONE) {
1140 _SERR("Fail to removeAppProfileByAppType for type (%s)", type);