bacf9238789b80aa798f82ec3496ac77f9639027
[platform/core/dotnet/launcher.git] / NativeLauncher / tool / ni_common.cc
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <pkgmgr-info.h>
18 #include <pkgmgr_installer_info.h>
19 #include <aul.h>
20
21 #include "log.h"
22 #include "utils.h"
23 #include "pkgmgr_parser_plugin_interface.h"
24
25 #include <wait.h>
26 #include <dirent.h>
27 #include <sys/stat.h>
28 #include <sys/time.h>
29 #include <sys/resource.h>
30
31 #include <algorithm>
32 #include <string>
33 #include <fstream>
34 #include <sstream>
35
36 #include <grp.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <sqlite3.h>
40 #include <inttypes.h>
41 #include <errno.h>
42
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"
49
50 #ifdef  LOG_TAG
51 #undef  LOG_TAG
52 #endif
53 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
54
55 #define __XSTR(x) #x
56 #define __STR(x) __XSTR(x)
57 #if defined(__arm__) || defined(__aarch64__)
58 static const char* __NATIVE_LIB_DIR = __STR(NATIVE_LIB_DIR);
59 #endif
60 static const char* __DOTNET_DIR = __STR(DOTNET_DIR);
61 static const char* __READ_ONLY_APP_UPDATE_DIR = __STR(READ_ONLY_APP_UPDATE_DIR);
62
63 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
64 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
65 #endif
66
67 #undef __STR
68 #undef __XSTR
69
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_OPTIMIZE_TIME = "--Ot";
82 static const char* CROSSGEN_OPT_INPUTBUBBLE = "--inputbubble";
83 static const char* CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS = "--compilebubblegenerics";
84 static const char* CROSSGEN_OPT_VERBOSE = "--verbose";
85 static std::vector<std::string> REF_VECTOR;
86 static std::vector<std::string> INPUTBUBBLE_REF_VECTOR;
87 static std::vector<std::string> MIBC_VECTOR;
88
89 static int __interval = 0;
90 static PathManager* __pm = nullptr;
91
92 static NIOption* __ni_option = nullptr;
93
94 // singleton
95 NIOption* getNIOption()
96 {
97         if (__ni_option == nullptr) {
98                 __ni_option = (NIOption*)calloc(sizeof(NIOption), 1);
99                 if (__ni_option == nullptr) {
100                         _SERR("Fail to create NIOption");
101                 }
102         }
103         return __ni_option;
104 }
105
106 static void waitInterval()
107 {
108         // by the recommand, ignore small value for performance.
109         if (__interval > 10000) {
110                 _SOUT("sleep %d usec", __interval);
111                 usleep(__interval);
112         }
113 }
114
115 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
116 // Get next base address to be used for system ni image from file
117 // __SYSTEM_BASE_FILE should be checked for existance before calling this function
118 static uintptr_t getNextBaseAddrFromFile()
119 {
120         FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
121         if (pFile == NULL) {
122                 _SERR("Failed to open %s", __SYSTEM_BASE_FILE);
123                 return 0;
124         }
125
126         uintptr_t addr = 0;
127         uintptr_t size = 0;
128
129         while (fscanf(pFile, "%" SCNxPTR " %" SCNuPTR "", &addr, &size) != EOF) {
130         }
131
132         fclose(pFile);
133
134         return addr + size;
135 }
136
137 // Get next base address to be used for system ni image
138 static uintptr_t getNextBaseAddr()
139 {
140         uintptr_t baseAddr = 0;
141
142         if (!isFile(__SYSTEM_BASE_FILE)) {
143                 // This is the starting address for all default base addresses
144                 baseAddr = DEFAULT_BASE_ADDR_START;
145         } else {
146                 baseAddr = getNextBaseAddrFromFile();
147
148                 // Round to a multple of 64K (see ZapImage::CalculateZapBaseAddress in CoreCLR)
149                 uintptr_t BASE_ADDRESS_ALIGNMENT = 0xffff;
150                 baseAddr = (baseAddr + BASE_ADDRESS_ALIGNMENT) & ~BASE_ADDRESS_ALIGNMENT;
151         }
152
153         return baseAddr;
154 }
155
156 // Save base address of system ni image to file
157 static void updateBaseAddrFile(const std::string& absNIPath, uintptr_t baseAddr)
158 {
159         uintptr_t niSize = getSizeOfImage(absNIPath);
160         if (niSize == 0) {
161                 _SERR("File %s doesn't exist", absNIPath.c_str());
162                 return;
163         }
164
165         // Write new entry to the file
166         FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
167         if (pFile == NULL) {
168                 _SERR("Failed to open %s", __SYSTEM_BASE_FILE);
169                 return;
170         }
171
172         fprintf(pFile, "%" PRIxPTR " %" PRIuPTR "\n", baseAddr, niSize);
173         fclose(pFile);
174 }
175
176 // check if dll is listed in TPA
177 static bool isTPADll(const std::string& dllPath)
178 {
179         std::string absPath = getBaseName(getAbsolutePath(dllPath));
180
181         std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
182         for (unsigned int i = 0; i < paths.size(); i++) {
183                 if (paths[i].find(getBaseName(absPath)) != std::string::npos) {
184                         return true;
185                 }
186         }
187
188         return false;
189 }
190 #endif
191
192 /**
193  * @brief create the directory including parents directory, and
194  *          copy ownership and smack labels to the created directory.
195  * @param[in] target directory path
196  * @param[in] source directory path to get ownership and smack label
197  * @return if directory created successfully, return true otherwise false
198  */
199 static bool createDirsAndCopyOwnerShip(std::string& target_path, const std::string& source)
200 {
201         struct stat st;
202         mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
203
204         for (std::string::iterator iter = target_path.begin(); iter != target_path.end();) {
205                 std::string::iterator newIter = std::find(iter, target_path.end(), '/');
206                 std::string newPath = std::string(target_path.begin(), newIter);
207
208                 if (!newPath.empty()) {
209                         if (stat(newPath.c_str(), &st) != 0) {
210                                 if (mkdir(newPath.c_str(), mode) != 0 && errno != EEXIST) {
211                                         _SERR("Fail to create app ni directory (%s)", newPath.c_str());
212                                         return false;
213                                 }
214                                 if (!source.empty()) {
215                                         copySmackAndOwnership(source, newPath);
216                                 }
217                         } else {
218                                 if (!S_ISDIR(st.st_mode)) {
219                                         _SERR("Fail. path is not a dir (%s)", newPath.c_str());
220                                         return false;
221                                 }
222                         }
223                 }
224                 iter = newIter;
225                 if(newIter != target_path.end()) {
226                         ++iter;
227                 }
228         }
229
230         return true;
231 }
232
233 static std::string getNIFilePath(const std::string& absDllPath, NIOption* opt)
234 {
235         std::string dllPath = absDllPath;
236         std::string fileName = getFileName(absDllPath);
237         if (opt->flags & NI_FLAGS_APPNI) {
238                 std::string niDirPath;
239                 std::string niTmpDirPath;
240                 std::string prevPath;
241
242                 prevPath = getBaseName(absDllPath);
243                 niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
244                 niTmpDirPath = concatPath(prevPath, APP_NI_SUB_TMP_DIR);
245
246                 if (opt->flags & NI_FLAGS_APP_UNDER_RO_AREA) {
247                         niTmpDirPath = replaceAll(niTmpDirPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
248                         niDirPath = replaceAll(niDirPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
249                         _INFO("App is installed in RO area. Change NI path to RW area(%s).", niTmpDirPath.c_str());
250                 }
251
252                 if (!isDirectory(niDirPath)) {
253                         if (!createDirsAndCopyOwnerShip(niTmpDirPath, prevPath)) {
254                                 niTmpDirPath = prevPath;
255                                 _SERR("fail to create dir (%s)", niTmpDirPath.c_str());
256                         }
257                         dllPath = concatPath(niTmpDirPath, fileName);
258                 } else {
259                         dllPath = concatPath(niDirPath, fileName);
260                 }
261         } else if (opt->flags & NI_FLAGS_RESOURCE_NI) {
262                 std::string rpkDir = concatPath(__pm->getAppRootPath(), APP_NI_SUB_DIR);
263                 dllPath = createDir(rpkDir) ? concatPath(rpkDir, fileName) : concatPath(__pm->getAppRootPath(), fileName);
264         }
265
266         size_t index = dllPath.find_last_of(".");
267         if (index == std::string::npos) {
268                 _SERR("File doesnot contain extension. fail to get NI file name");
269                 return "";
270         }
271         std::string fName = dllPath.substr(0, index);
272         std::string fExt = dllPath.substr(index, dllPath.length());
273
274         // crossgen generate file with lower case extension only
275         std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
276         std::string niPath = fName + ".ni" + fExt;
277
278         return niPath;
279 }
280
281 static bool checkNIExistence(const std::string& absDllPath, NIOption* opt)
282 {
283         std::string absNIPath = getNIFilePath(absDllPath, opt);
284         if (absNIPath.empty()) {
285                 return false;
286         }
287
288         if (isFile(absNIPath)) {
289                 return true;
290         }
291
292         // native image of System.Private.CoreLib.dll should have to overwrite
293         // original file to support new coreclr
294         if (absDllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
295                 return isR2RImage(absDllPath);
296         }
297
298         return false;
299 }
300
301 static bool checkDllExistInDir(const std::string& path)
302 {
303         bool ret = false;
304         auto func = [&ret](const std::string& f_path, const std::string& f_name) {
305                 if (isManagedAssembly(f_name) || isR2RImage(f_name)) {
306                         ret = true;
307                 }
308         };
309
310         scanFilesInDirectory(path, func, 0);
311
312         return ret;
313 }
314
315 /*
316  * Get the list of managed files in the specific directory (of Application)
317  * Absolute paths of managed files are stored at the result list.
318  * If native image already exist in the (same / .native_image) directory, managed file is ignored.
319  */
320 static ni_error_e getTargetDllList(const std::string& path, std::vector<std::string>& fileList, NIOption *opt)
321 {
322         if (!isDirectory(path)) {
323                 return NI_ERROR_INVALID_PARAMETER;
324         }
325
326         auto func = [&fileList, opt](const std::string& f_path, const std::string& f_name) {
327                 if (isManagedAssembly(f_path) && !checkNIExistence(f_path, opt)) {
328                         fileList.push_back(getAbsolutePath(f_path));
329                 }
330         };
331
332         scanFilesInDirectory(path, func, 0);
333
334         return NI_ERROR_NONE;
335 }
336
337 static void makeArgs(std::vector<const char*>& args, const std::vector<std::string>& refPaths, NIOption* opt)
338 {
339         args.push_back(CORERUN_CMD.c_str());
340         if (CROSSGEN2_PATH != "") {
341                 args.push_back(CROSSGEN2_PATH.c_str());
342         }
343         args.push_back(CROSSGEN_OPT_JITPATH);
344         args.push_back(CLRJIT_PATH.c_str());
345         args.push_back(CROSSGEN_OPT_TARGET_ARCH);
346         args.push_back(ARCHITECTURE_IDENTIFIER);
347
348         //args.push_back(OPT_PARALLELISM);
349         //args.push_back(OPT_PARALLELISM_COUNT);
350         args.push_back(CROSSGEN_OPT_RESILIENT);
351
352         args.push_back(CROSSGEN_OPT_OPTIMIZE_TIME);
353
354         if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
355                 args.push_back(CROSSGEN_OPT_INPUTBUBBLE);
356                 args.push_back(CROSSGEN_OPT_COMPILE_BUBBLE_GENERICS);
357
358                 INPUTBUBBLE_REF_VECTOR.clear();
359                 for (const auto &path : opt->inputBubbleRefFiles) {
360                         INPUTBUBBLE_REF_VECTOR.push_back("--inputbubbleref:" + path);
361                 }
362                 for (const auto &path : INPUTBUBBLE_REF_VECTOR) {
363                         if (find(args.begin(), args.end(), path) == args.end()) {
364                                 args.push_back(path.c_str());
365                         }
366                 }
367         }
368
369         if (opt->flags & NI_FLAGS_MIBC) {
370                 MIBC_VECTOR.clear();
371                 for (const auto &path : opt->mibcPath) {
372                         MIBC_VECTOR.push_back("--mibc:" + path);
373                 }
374                 for (const auto &path : MIBC_VECTOR) {
375                         if (find(args.begin(), args.end(), path) == args.end()) {
376                                 args.push_back(path.c_str());
377                         }
378                 }
379         }
380
381         if (opt->flags & NI_FLAGS_VERBOSE) {
382                 args.push_back(CROSSGEN_OPT_VERBOSE);
383         }
384
385         REF_VECTOR.clear();
386
387         // set reference path
388         for (const auto &path : opt->refFiles) {
389                 REF_VECTOR.push_back("-r:" + path);
390         }
391
392         std::vector<std::string> paths = __pm->getPlatformAssembliesPaths();
393         for (const auto &path : paths) {
394                 if (checkDllExistInDir(path)) {
395                         REF_VECTOR.push_back("-r:" + path + "/*.dll");
396                 }
397         }
398
399         if (opt->flags & NI_FLAGS_EXTRA_REF) {
400                 for (const auto &erPath : opt->extraRefPath) {
401                         std::string path = getAbsolutePath(erPath);
402                         if (checkDllExistInDir(path)) {
403                                 REF_VECTOR.push_back("-r:" + path + "/*.dll");
404                         }
405                 }
406         }
407
408         for (const auto &path : refPaths) {
409                 if (checkDllExistInDir(path)) {
410                         REF_VECTOR.push_back("-r:" + path + "/*.dll");
411                 }
412         }
413
414         for (const auto &path : REF_VECTOR) {
415                 if (find(args.begin(), args.end(), path) == args.end()) {
416                         args.push_back(path.c_str());
417                 }
418         }
419 }
420
421 static void clearArgs(std::vector<const char*>& args)
422 {
423         REF_VECTOR.clear();
424         args.clear();
425 }
426
427 static ni_error_e makePdbSymlinkForNI(std::string dllPath, std::string niPath)
428 {
429         std::string pdbPath = changeExtension(dllPath, ".dll", ".pdb");
430         try {
431                 if (exist(pdbPath)) {
432                         std::string targetPDBPath = changeExtension(niPath, ".ni.dll", ".pdb");
433                         if (!exist(targetPDBPath)) {
434                                 bf::create_symlink(pdbPath, targetPDBPath);
435                                 copySmackAndOwnership(pdbPath, targetPDBPath, true);
436                         }
437                 }
438         } catch (const bf::filesystem_error& error) {
439                 _SERR("Fail to create symlink for %s", pdbPath.c_str());
440                 return NI_ERROR_UNKNOWN;
441         }
442
443         return NI_ERROR_NONE;
444 }
445
446 static ni_error_e crossgen2PostAction(const std::string& dllPath, const std::string& niPath, NIOption* opt) {
447         std::string outFile = niPath;
448         if (!exist(outFile)) {
449                 return NI_ERROR_NO_SUCH_FILE;
450         }
451         copySmackAndOwnership(dllPath, outFile);
452
453         // if AppNI then move ni.dll file to .native_image and copy pdb to .native_image
454         if (opt->flags & NI_FLAGS_APPNI) {
455                 outFile = getNIFilePath(dllPath, opt);
456                 makePdbSymlinkForNI(dllPath, outFile);
457
458                 if (opt->flags & NI_FLAGS_INPUT_BUBBLE && opt->flags & NI_FLAGS_NO_PIPELINE) {
459                         outFile = outFile + ".tmp";
460                 }
461
462                 if (niPath != outFile) {
463                         moveFile(niPath, outFile);
464                 }
465         }
466
467         if (opt->flags & NI_FLAGS_RM_ORIGIN_AFTER_NI) {
468                 if (!removeFile(dllPath)) {
469                         _SERR("Fail to remove original file : %s", dllPath.c_str());
470                 }
471         }
472
473         if (opt->flags & NI_FLAGS_RESOURCE_NI) {
474                 moveFile(niPath, changeExtension(dllPath, ".dll", ".ni.dll"));
475                 removeAll(concatPath(__pm->getAppRootPath(), APP_NI_SUB_DIR));
476         }
477
478         if (!(opt->flags & NI_FLAGS_INPUT_BUBBLE && opt->flags & NI_FLAGS_NO_PIPELINE)) {
479                 _SOUT("Native image %s generated successfully.", outFile.c_str());
480         }
481
482         return NI_ERROR_NONE;
483 }
484
485 void setPriority(NIOption* opt)
486 {
487         pid_t pid = getpid();
488         if (setpriority(PRIO_PROCESS, pid, opt->priority) == 0) {
489                 std::string str = " ";
490                 if (opt->priority <= -20) {
491                         str = " highest ";
492                 } else if (opt->priority >= 19) {
493                         str = " lowest ";
494                 }
495                 _SOUT("Success to set the%spriority of the process. pid : [%d], priority : [%d]", str.c_str(), pid, getpriority(PRIO_PROCESS, pid));
496         } else {
497                 _SERR("Failed to set the priority of the process. pid : [%d], priority : [%d]", pid, getpriority(PRIO_PROCESS, pid));
498         }
499 }
500
501 static ni_error_e crossgen2PipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
502 {
503         // fork crossgen2
504         pid_t pid = fork();
505         if (pid == -1)
506                 return NI_ERROR_UNKNOWN;
507
508         if (pid > 0) {
509                 int status;
510                 waitpid(pid, &status, 0);
511                 if (WIFEXITED(status)) {
512                         for (auto& dllPath: dllList) {
513                                 ni_error_e ret = crossgen2PostAction(dllPath, changeExtension(dllPath, ".dll", ".ni.dll"), opt);
514                                 if (ret != NI_ERROR_NONE) {
515                                         return ret;
516                                 }
517                         }
518                 } else {
519                         _SERR("Failed. Forked process terminated abnormally");
520                         return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
521                 }
522         } else {
523                 if (opt->flags & NI_FLAGS_SET_PRIORITY) {
524                         setPriority(opt);
525                 }
526                 std::vector<const char*> argv;
527                 makeArgs(argv, refPaths, opt);
528                 argv.push_back(CROSSGEN_OPT_OUT_NEAR_INPUT);
529                 argv.push_back(CROSSGEN_OPT_SINGLE_FILE_COMPILATION);
530
531                 // add input files at the end of parameter
532                 for (const auto &input : dllList) {
533                         argv.push_back(input.c_str());
534                         _SOUT("+ %s", input.c_str());
535                 }
536
537                 // end param
538                 argv.push_back(nullptr);
539
540                 // print cmd
541                 if (opt->flags & NI_FLAGS_PRINT_CMD) {
542                         _SOUT("==================== NI Commands =========================");
543                         for (auto &arg: argv) _SOUT("+ %s", arg);
544                 }
545
546                 execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
547
548                 clearArgs(argv);
549                 exit(0);
550         }
551
552         return NI_ERROR_NONE;
553 }
554
555 static ni_error_e crossgen2NoPipeLine(const std::vector<std::string>& dllList, const std::vector<std::string>& refPaths, NIOption* opt)
556 {
557         for (auto& dllPath : dllList) {
558                 std::string niPath;
559                 niPath = getNIFilePath(dllPath, opt);
560
561 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
562                 uintptr_t baseAddr = 0;
563                 if (isTPADll(dllPath)) {
564                         baseAddr = getNextBaseAddr();
565                 }
566 #endif
567                 if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
568                         niPath += ".tmp";
569                 }
570
571                 // fork crossgen2
572                 pid_t pid = fork();
573                 if (pid == -1)
574                         return NI_ERROR_UNKNOWN;
575
576                 if (pid > 0) {
577                         int status;
578                         waitpid(pid, &status, 0);
579                         if (WIFEXITED(status)) {
580                                 ni_error_e ret = crossgen2PostAction(dllPath, niPath, opt);
581                                 if (ret != NI_ERROR_NONE) {
582                                         return ret;
583                                 }
584 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
585                                 if (baseAddr != 0) {
586                                         updateBaseAddrFile(niPath, baseAddr);
587                                 }
588 #endif
589                         } else {
590                                 _SERR("Failed. Forked process terminated abnormally");
591                                 _SERR("Crossgen2 was terminated by the OOM killer. Please check the system.");
592                                 removeFile(changeExtension(niPath, ".ni.dll", ".ni.dll.tmp"));
593                                 return NI_ERROR_ABNORMAL_PROCESS_TERMINATION;
594                         }
595                 } else {
596                         if (opt->flags & NI_FLAGS_SET_PRIORITY) {
597                                 setPriority(opt);
598                         }
599                         std::vector<const char*> argv;
600                         makeArgs(argv, refPaths, opt);
601
602 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
603                         std::string baseAddrString;
604                         if (baseAddr != 0) {
605                                 argv.push_back("--imagebase");
606                                 std::stringstream ss;
607                                 ss << "0x" << std::hex << baseAddr;
608                                 baseAddrString = ss.str();
609                                 argv.push_back(baseAddrString.c_str());
610                         }
611 #endif
612                         argv.push_back("-o");
613                         argv.push_back(niPath.c_str());
614
615                         argv.push_back(dllPath.c_str());
616                         _SOUT("+ %s", dllPath.c_str());
617
618                         // end param
619                         argv.push_back(nullptr);
620
621                         // print cmd
622                         if (opt->flags & NI_FLAGS_PRINT_CMD) {
623                                 _SOUT("==================== NI Commands =========================");
624                                 for (auto &arg: argv) _SOUT("+ %s", arg);
625                         }
626
627                         execv(CORERUN_CMD.c_str(), const_cast<char* const*>(argv.data()));
628
629                         clearArgs(argv);
630                         exit(0);
631                 }
632
633                 waitInterval();
634         }
635
636         return NI_ERROR_NONE;
637 }
638
639 static ni_error_e createCoreLibNI(NIOption* opt)
640 {
641         std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
642         std::string niCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll");
643         std::string niTmpCoreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.ni.dll.tmp");
644         std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
645
646         std::vector<std::string> dllList;
647         std::vector<std::string> refPaths;
648         dllList.push_back(getAbsolutePath(coreLib));
649
650         if (!isFile(coreLibBackup) && !isR2RImage(coreLib)) {
651                 if (crossgen2NoPipeLine(dllList, refPaths, opt) == NI_ERROR_NONE) {
652                         if (opt->flags & NI_FLAGS_RM_ORIGIN_AFTER_NI) {
653                                 std::ofstream output(coreLibBackup);
654                                 if (!exist(coreLibBackup)) {
655                                         _SERR("Failed to create System.Private.CoreLib.dll.Backup");
656                                         return NI_ERROR_CORE_NI_FILE;
657                                 }
658                                 copySmackAndOwnership(__pm->getRuntimePath(), coreLibBackup, false);
659                                 output.close();
660                         } else if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
661                                 _SERR("Failed to rename from System.Private.CoreLib.dll to System.Private.CoreLib.dll.Backup");
662                                 return NI_ERROR_CORE_NI_FILE;
663                         }
664                         if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
665                                 if (rename(niTmpCoreLib.c_str(), coreLib.c_str())) {
666                                         _SERR("Failed to rename from System.Private.CoreLib.ni.dll.tmp to Private.CoreLib.dll");
667                                         return NI_ERROR_CORE_NI_FILE;
668                                 }
669                         } else {
670                                 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
671                                         _SERR("Failed to rename from System.Private.CoreLib.ni.dll to Private.CoreLib.dll");
672                                         return NI_ERROR_CORE_NI_FILE;
673                                 }
674                         }
675                 } else {
676                         _SERR("Failed to create native image for %s", coreLib.c_str());
677                         return NI_ERROR_CORE_NI_FILE;
678                 }
679         }
680         return NI_ERROR_NONE;
681 }
682
683 static void renameAppNITmpPath(NIOption* opt)
684 {
685         std::string niTmpPath = __pm->getAppRootPath() + "/bin/" + APP_NI_SUB_TMP_DIR;
686         std::string niPath = __pm->getAppRootPath() + "/bin/" + APP_NI_SUB_DIR;
687
688         if (opt->flags & NI_FLAGS_APP_UNDER_RO_AREA) {
689                 niTmpPath = replaceAll(niTmpPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
690                 niPath = replaceAll(niPath, getBaseName(__pm->getAppRootPath()), __READ_ONLY_APP_UPDATE_DIR);
691         }
692
693         if (isDirectory(niTmpPath)) {
694                 if (rename(niTmpPath.c_str(), niPath.c_str())) {
695                         _SERR("Fail to rename from .native_image_tmp to .native_image");
696                 } else {
697                         _SOUT("Success to rename from %s to %s", niTmpPath.c_str(), niPath.c_str());
698                 }
699         }
700 }
701
702 static ni_error_e doAOTList(std::vector<std::string>& dllList, const std::string& refPaths, NIOption* opt)
703 {
704         ni_error_e ret = NI_ERROR_NONE;
705
706         if (dllList.empty()) {
707                 return NI_ERROR_INVALID_PARAMETER;
708         }
709         // When performing AOT for one Dll, an error is returned when an error occurs.
710         // However, when processing multiple dlls at once, only the log for errors is output and skipped.
711
712         std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
713         bool hasSPC = false;
714
715         for (auto it = dllList.begin(); it != dllList.end(); it++) {
716                 std::string f = *it;
717                 if (!isFile(f)) {
718                         _SERR("dll file is not exist : %s", f.c_str());
719                         dllList.erase(it--);
720                 }
721                 else if (!isManagedAssembly(f)) {
722                         _SERR("Input file is not a dll file : %s", f.c_str());
723                         dllList.erase(it--);
724                 }
725                 // handle System.Private.CoreLib.dll separately.
726                 // dllList and path manager contain absolute path. So, there is no need to change path to absolute path
727                 else if (f == coreLib) {
728                         hasSPC = true;
729                         dllList.erase(it--);
730                 }
731         }
732
733         // Error : Multiple input files matching same simple name
734         // So, Remove dulicate files from dll list
735         std::sort(dllList.begin(), dllList.end());
736         dllList.erase(unique(dllList.begin(), dllList.end()), dllList.end());
737
738         // In the case of SPC, post-processing is required to change the name of the native image.
739         // In order to avoid repeatedly checking whether the generated native image is an SPC,
740         // the SPC native image generation is performed separately.
741         if (hasSPC) {
742                 ret = createCoreLibNI(opt);
743                 if (ret != NI_ERROR_NONE) {
744                         return ret;
745                 }
746         }
747
748         // if there is no proper input after processing dll list
749         if (dllList.empty()) {
750                 if (hasSPC) {
751                         return ret;
752                 } else {
753                         return NI_ERROR_INVALID_PARAMETER;
754                 }
755         }
756
757         std::vector<std::string> paths;
758         splitPath(refPaths, paths);
759
760         if (opt->flags & NI_FLAGS_NO_PIPELINE) {
761                 ret = crossgen2NoPipeLine(dllList, paths, opt);
762         } else {
763                 std::vector<std::string> notCompiled;
764                 ret = crossgen2PipeLine(dllList, paths, opt);
765                 if (ret != NI_ERROR_NONE) {
766                         _SERR("Crossgen2 is abnormally terminated. Regenerate native images that failed while running crossgen2.");
767                         for (auto &dll : dllList) {
768                                 std::string tFile = changeExtension(dll, ".dll", ".ni.dll");
769                                 if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
770                                         tFile += ".tmp";
771                                 }
772                                 if (!exist(tFile)) {
773                                         notCompiled.push_back(dll);
774                                 }
775                         }
776                         _SERR("Retry running crossgen2 with --no-pipeline mode to avoid termination by OOM.");
777                         ret = crossgen2NoPipeLine(notCompiled, paths, opt);
778                 }
779         }
780
781         if (ret == NI_ERROR_NONE) {
782                 if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
783                         for (auto &dll : dllList) {
784                                 std::string tmpFile;
785                                 std::string niFile = getNIFilePath(dll, opt);
786                                 tmpFile = niFile + ".tmp";
787
788                                 if (exist(tmpFile)) {
789                                         moveFile(tmpFile, niFile);
790                                         _SOUT("Native image %s generated successfully.", niFile.c_str());
791                                 }
792                         }
793                 }
794                 if (opt->flags & NI_FLAGS_APPNI) {
795                         renameAppNITmpPath(opt);
796                 }
797         }
798
799         return ret;
800 }
801
802 static ni_error_e doAOTFile(const std::string& dllFile, const std::string& refPaths, NIOption* opt)
803 {
804         if (!isFile(dllFile)) {
805                 _SERR("dll file is not exist : %s", dllFile.c_str());
806                 return NI_ERROR_NO_SUCH_FILE;
807         }
808
809         if (checkNIExistence(dllFile, opt)) {
810                 _SERR("Native image file is already exist : %s", dllFile.c_str());
811                 return NI_ERROR_ALREADY_EXIST;
812         }
813
814         if (!isManagedAssembly(dllFile)) {
815                 _SERR("Failed. Input parameter is not managed dll (%s)\n", dllFile.c_str());
816                 return NI_ERROR_INVALID_PARAMETER;
817         }
818
819         std::vector<std::string> dllList;
820         dllList.push_back(getAbsolutePath(dllFile));
821         return doAOTList(dllList, refPaths, opt);
822 }
823
824 static ni_error_e removeAndCreateNI(const char* pkgId, NIOption* pOptions)
825 {
826         if (removeNIUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
827                 _SERR("Failed to remove previous dlls from [%s]", pkgId);
828                 return NI_ERROR_UNKNOWN;
829         }
830
831         if (createNIUnderPkgRoot(pkgId, pOptions) != NI_ERROR_NONE) {
832                 _SERR("Failed to generate NI file [%s]", pkgId);
833                 return NI_ERROR_UNKNOWN;
834         }
835
836         _SOUT("Complete make native image for pkg (%s)", pkgId);
837         return NI_ERROR_NONE;
838 }
839
840 static bool isReadOnlyPkg(std::string pkgId)
841 {
842         int ret = 0;
843         bool readonly = false;
844         pkgmgrinfo_pkginfo_h handle;
845
846         ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
847         if (ret != PMINFO_R_OK) {
848                 _ERR("Fail to get pkginfo");
849                 return false;
850         }
851
852         ret = pkgmgrinfo_pkginfo_is_readonly(handle, &readonly);
853         if (ret != PMINFO_R_OK) {
854                 _ERR("Fail to get is_readonly");
855                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
856                 return false;
857         }
858
859         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
860         return readonly;
861 }
862
863 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
864 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
865 {
866         char *pkgId = NULL;
867         int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
868         if (ret != PMINFO_R_OK) {
869                 _SERR("Failed to get pkgid");
870                 return -1;
871         }
872
873         std::vector<std::string> *pkgList = (std::vector<std::string> *)userData;
874         pkgList->push_back(pkgId);
875
876         return 0;
877 }
878
879 // callback function of "pkgmgrinfo_pkginfo_metadata_filter_foreach"
880 static int pkgAotCb(pkgmgrinfo_pkginfo_h handle, void *userData)
881 {
882         char *pkgId = NULL;
883         int ret = pkgmgrinfo_pkginfo_get_pkgid(handle, &pkgId);
884         if (ret != PMINFO_R_OK) {
885                 _SERR("Failed to get pkgid");
886                 return -1;
887         }
888
889         std::vector<std::string> *pkgList = (std::vector<std::string> *)userData;
890         pkgList->push_back(pkgId);
891
892         return 0;
893 }
894
895 ni_error_e initNICommon()
896 {
897 #if defined(__arm__) || defined(__aarch64__)
898
899         char *env = nullptr;
900         env = getenv("MIC_CROSSGEN2_ENABLED");
901         if (env != nullptr && !strcmp(env, "1")) {
902                 CORERUN_CMD = std::string("/opt/usr/dotnet/mic/crossgen2");
903                 CROSSGEN2_PATH = "";
904                 CLRJIT_PATH = std::string("/opt/usr/dotnet/mic/libclrjit_unix_") + ARCHITECTURE_IDENTIFIER + std::string("_x64.so");
905         }
906
907         // get interval value
908         const static std::string intervalFile = concatPath(__NATIVE_LIB_DIR, "crossgen_interval.txt");
909         std::ifstream inFile(intervalFile);
910         if (inFile) {
911                 _SOUT("crossgen_interval.txt is found");
912                 inFile >> __interval;
913         }
914
915         if (initializePluginManager("normal")) {
916                 _SERR("Fail to initialize PluginManager");
917                 return NI_ERROR_UNKNOWN;
918         }
919
920         try {
921                 __pm = new PathManager();
922         } catch (const std::exception& e) {
923                 _SERR("Failed to create PathManager");
924                 return NI_ERROR_UNKNOWN;
925         }
926
927         char* pluginDllPaths = pluginGetDllPath();
928         if (pluginDllPaths && pluginDllPaths[0] != '\0') {
929                 __pm->addPlatformAssembliesPaths(pluginDllPaths, true);
930         }
931
932         char* pluginNativePaths = pluginGetNativeDllSearchingPath();
933         if (pluginNativePaths && pluginNativePaths[0] != '\0') {
934                 __pm->addNativeDllSearchingPaths(pluginNativePaths, true);
935         }
936
937         return NI_ERROR_NONE;
938 #else
939         _SERR("crossgen supports arm/arm64 architecture only. skip ni file generation");
940         return NI_ERROR_NOT_SUPPORTED;
941 #endif
942 }
943
944 void finalizeNICommon()
945 {
946         __interval = 0;
947
948         finalizePluginManager();
949
950         delete(__pm);
951         __pm = nullptr;
952
953         if (__ni_option) {
954                 free(__ni_option);
955                 __ni_option = nullptr;
956         }
957 }
958
959 ni_error_e createNIPlatform(std::string& extraInputs, NIOption* opt)
960 {
961         extraInputs += ":" + __pm->getRuntimePath();
962         extraInputs += ":" + __pm->getTizenFXPath();
963
964         return createNIUnderDirs(extraInputs, opt);
965 }
966
967 ni_error_e createNIDll(const std::string& dllPath, NIOption* opt)
968 {
969         return doAOTFile(dllPath, std::string(), opt);
970 }
971
972 ni_error_e createNIUnderTAC(const std::string& targetPath, const std::string& refPaths, NIOption* opt)
973 {
974         ni_error_e ret;
975
976         bool isAppNI = false;
977         if (opt->flags & NI_FLAGS_APPNI) {
978                 isAppNI = true;
979         }
980
981         if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
982                 std::vector<std::string> refs;
983                 splitPath(refPaths, refs);
984                 for (auto &p: refs) {
985                         if (isDirectory(p) && checkDllExistInDir(p)) {
986                                 opt->inputBubbleRefFiles.push_back(p + "/*.dll");
987                         }
988                 }
989         }
990
991         // get managed file list from targetPath
992         std::vector<std::string> dllList;
993         ret = getTargetDllList(targetPath, dllList, opt);
994         if (ret != NI_ERROR_NONE) {
995                 return ret;
996         }
997
998         std::vector<std::string> needNIList;
999         std::vector<std::string> niList;
1000
1001         for (auto &dll : dllList) {
1002                 if (!checkNIExistence(dll, opt)) {
1003                         needNIList.push_back(dll);
1004                 }
1005                 niList.push_back(getNIFilePath(dll, opt));
1006         }
1007
1008         if (!needNIList.empty()) {
1009                 // NI fils of TAC-related dlls under /opt/usr/dotnet should not be created under .native_image directory.
1010                 // So, unset NI_FLAGS_APPNI temporally and restore it after running AOT.
1011                 opt->flags &= ~NI_FLAGS_APPNI;
1012                 ret = doAOTList(needNIList, refPaths, opt);
1013                 if (isAppNI) {
1014                         opt->flags |= NI_FLAGS_APPNI;
1015                 }
1016                 if (ret != NI_ERROR_NONE) {
1017                         return ret;
1018                 }
1019         }
1020
1021         if (isAppNI) {
1022                 for (auto &niPath : niList) {
1023                         if (exist(niPath)) {
1024                                 std::string symNIPath = concatPath(targetPath, getFileName(niPath));
1025                                 if (!exist(symNIPath)) {
1026                                         bf::create_symlink(niPath, symNIPath);
1027                                         copySmackAndOwnership(targetPath.c_str(), symNIPath.c_str(), true);
1028                                         _SOUT("%s symbolic link file generated successfully.", symNIPath.c_str());
1029                                         _INFO("%s symbolic link file generated successfully.", symNIPath.c_str());
1030                                 }
1031                         }
1032                 }
1033         }
1034
1035         return NI_ERROR_NONE;
1036 }
1037
1038 ni_error_e createNIUnderDirs(const std::string& rootPaths, NIOption* opt)
1039 {
1040         ni_error_e ret = NI_ERROR_NONE;
1041
1042         std::vector<std::string> fileList;
1043         std::vector<std::string> paths;
1044         splitPath(rootPaths, paths);
1045
1046         if (opt->flags & NI_FLAGS_INPUT_BUBBLE) {
1047                 for (auto &p: paths) {
1048                         if (isDirectory(p) && checkDllExistInDir(p)) {
1049                                 opt->inputBubbleRefFiles.push_back(p + "/*.dll");
1050                         }
1051                 }
1052         }
1053
1054         for (const auto &path : paths) {
1055                 if (!exist(path)) {
1056                         continue;
1057                 }
1058
1059                 if (path.find(TAC_SYMLINK_SUB_DIR) != std::string::npos) {
1060                         ret = createNIUnderTAC(path, rootPaths, opt);
1061                         if (ret != NI_ERROR_NONE) {
1062                                 return ret;
1063                         }
1064                 } else {
1065                         ret = getTargetDllList(path, fileList, opt);
1066                         if (ret != NI_ERROR_NONE) {
1067                                 return ret;
1068                         }
1069                 }
1070         }
1071
1072         if (fileList.empty()) {
1073                 return NI_ERROR_NONE;
1074         }
1075
1076         return doAOTList(fileList, rootPaths, opt);
1077 }
1078
1079 ni_error_e createNIUnderPkgRoot(const std::string& pkgId, NIOption* opt)
1080 {
1081         if (!isR2RImage(concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll"))) {
1082                 _SERR("The native image of System.Private.CoreLib does not exist.\n"
1083                                 "Run the command to create the native image\n"
1084                                 "# dotnettool --ni-dll /usr/share/dotnet.tizen/netcoreapp/System.Private.CoreLib.dll");
1085                 return NI_ERROR_CORE_NI_FILE;
1086         }
1087
1088         std::string rootPath = getRootPath(pkgId);
1089         if (rootPath.empty()) {
1090                 _SERR("Failed to get root path from [%s]", pkgId.c_str());
1091                 return NI_ERROR_INVALID_PACKAGE;
1092         }
1093         __pm->setAppRootPath(rootPath);
1094
1095         char* extraDllPaths = pluginGetExtraDllPath();
1096         if (extraDllPaths && extraDllPaths[0] != '\0') {
1097                 opt->flags |= NI_FLAGS_EXTRA_REF;
1098                 splitPath(extraDllPaths, opt->extraRefPath);
1099         }
1100
1101         std::string targetDirs;
1102         if (isRPK(pkgId)) {
1103                 opt->flags &= ~NI_FLAGS_APPNI; // added to exclude logic of APP_NI
1104                 opt->flags |= NI_FLAGS_RESOURCE_NI; // added flag for RPK type
1105                 opt->flags |= NI_FLAGS_NO_PIPELINE; // added the flag to set the output path
1106
1107                 std::string paths = getResourcePaths(rootPath);
1108                 if (paths.empty()) {
1109                         _SERR("Failed to get rpk paths from [%s]", pkgId.c_str());
1110                         return NI_ERROR_UNKNOWN;
1111                 }
1112                 targetDirs = paths;
1113         } else {
1114                 opt->flags |= NI_FLAGS_APPNI;
1115                 opt->flags &= ~NI_FLAGS_RESOURCE_NI; // added to exclude logic of RESOURCE_NI
1116
1117                 if (isReadOnlyArea(rootPath)) {
1118                         opt->flags |= NI_FLAGS_APP_UNDER_RO_AREA;
1119                         opt->flags |= NI_FLAGS_NO_PIPELINE;
1120                         _SERR("Only no-pipeline mode supported for RO app. Set no-pipeline option forcibly");
1121                 } else {
1122                         opt->flags &= ~NI_FLAGS_APP_UNDER_RO_AREA;
1123                 }
1124
1125                 targetDirs = __pm->getAppPaths();
1126         }
1127
1128         return createNIUnderDirs(targetDirs, opt);
1129 }
1130
1131 void removeNIPlatform()
1132 {
1133         std::string coreLib = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll");
1134         std::string coreLibBackup = concatPath(__pm->getRuntimePath(), "System.Private.CoreLib.dll.Backup");
1135
1136         if (isR2RImage(coreLib)) {
1137                 if (!isFile(coreLibBackup)) {
1138                         return;
1139                 }
1140
1141                 if (remove(coreLib.c_str())) {
1142                         _SERR("Failed to remove System.Private.CoreLib native image file");
1143                 }
1144                 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
1145                         _SERR("Failed to rename System.Private.CoreLib.Backup to origin");
1146                 }
1147         }
1148
1149 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
1150         if (isFile(__SYSTEM_BASE_FILE)) {
1151                 if (remove(__SYSTEM_BASE_FILE)) {
1152                         _SERR("Failed to remove %s", __SYSTEM_BASE_FILE);
1153                 }
1154         }
1155 #endif
1156
1157         removeNIUnderDirs(__pm->getRuntimePath() + ":" + __pm->getTizenFXPath());
1158 }
1159
1160 void removeNIUnderDirs(const std::string& rootPaths)
1161 {
1162         auto convert = [](const std::string& path, const std::string& filename) {
1163                 if (isR2RImage(path)) {
1164                         std::string assemblyPath = changeExtension(path, ".ni.dll", ".dll");
1165                         if (exist(assemblyPath)) {
1166                                 if (remove(path.c_str())) {
1167                                         _SERR("Failed to remove %s", path.c_str());
1168                                 }
1169                         } else {
1170                                 _SOUT("%s cannot be removed because there is no %s", path.c_str(), assemblyPath.c_str());
1171                         }
1172                 }
1173         };
1174
1175         std::vector<std::string> paths;
1176         splitPath(rootPaths, paths);
1177         for (const auto &path : paths) {
1178                 scanFilesInDirectory(path, convert, -1);
1179         }
1180 }
1181
1182 ni_error_e removeNIUnderPkgRoot(const std::string& pkgId)
1183 {
1184         std::vector<std::string> paths;
1185
1186         std::string rootPath = getRootPath(pkgId);
1187         if (rootPath.empty()) {
1188                 _SERR("Failed to get root path from [%s]", pkgId.c_str());
1189                 return NI_ERROR_INVALID_PACKAGE;
1190         }
1191
1192         if (isRPK(pkgId)) {
1193                 std::string rpkPaths = getResourcePaths(rootPath);
1194                 if (rpkPaths.empty()) {
1195                         _SERR("Failed to get rpk path from [%s]", pkgId.c_str());
1196                         return NI_ERROR_UNKNOWN;
1197                 }
1198                 splitPath(rpkPaths, paths);
1199                 for (const auto &path : paths) {
1200                         if (isDirectory(path)) {
1201                                 removeNIUnderDirs(path);
1202                         }
1203                 }
1204         } else {
1205                 __pm->setAppRootPath(rootPath);
1206
1207                 // getAppNIPaths returns bin/.native_image, lib/.native_image and .tac_symlink.
1208                 std::string appNIPaths = __pm->getAppNIPaths();
1209                 splitPath(appNIPaths, paths);
1210                 for (const auto &path : paths) {
1211                         if (!isReadOnlyArea(path)) {
1212                                 // Only the native image inside the TAC should be removed.
1213                                 if (strstr(path.c_str(), TAC_SYMLINK_SUB_DIR) != NULL) {
1214                                         removeNIUnderDirs(path);
1215                                 } else {
1216                                         if (isDirectory(path)) {
1217                                                 if (!removeAll(path.c_str())) {
1218                                                         _SERR("Failed to remove app ni dir [%s]", path.c_str());
1219                                                 }
1220                                         }
1221                                 }
1222                         }
1223                 }
1224
1225                 // In special cases, the ni file may exist in the dll location.
1226                 // The code below is to avoid this exceptional case.
1227                 std::string appPaths = __pm->getAppPaths();
1228                 splitPath(appPaths, paths);
1229                 for (const auto &path : paths) {
1230                         if (isDirectory(path)) {
1231                                 removeNIUnderDirs(path);
1232                         }
1233                 }
1234         }
1235
1236         return NI_ERROR_NONE;
1237 }
1238
1239 ni_error_e regeneratePkgNI(NIOption* opt)
1240 {
1241         std::vector<std::string> pkgList;
1242         ni_error_e ret = NI_ERROR_NONE;
1243
1244         // iterates for Packages's metadata (RPK)
1245         pkgmgrinfo_pkginfo_metadata_filter_h pkgHandle;
1246         if (pkgmgrinfo_pkginfo_metadata_filter_create(&pkgHandle) == PMINFO_R_OK) {
1247                 if (pkgmgrinfo_pkginfo_metadata_filter_add(pkgHandle, AOT_METADATA_KEY, METADATA_VALUE_TRUE) == PMINFO_R_OK) {
1248                         if (pkgmgrPkgMDFilterForeach(pkgHandle, pkgAotCb, &pkgList) != 0) {
1249                                 ret = NI_ERROR_UNKNOWN;
1250                                 _ERR("pkgmgrPkgMDFilterForeach failed");
1251                         }
1252                 } else {
1253                         ret = NI_ERROR_UNKNOWN;
1254                         _ERR("pkgmgrinfo_pkginfo_metadata_filter_add failed");
1255                 }
1256                 pkgmgrinfo_pkginfo_metadata_filter_destroy(pkgHandle);
1257         } else {
1258                 ret = NI_ERROR_UNKNOWN;
1259                 _ERR("pkgmgrinfo_pkginfo_metadata_filter_create failed");
1260         }
1261
1262         // iterate for App's metadata
1263         pkgmgrinfo_appinfo_metadata_filter_h appHandle;
1264         if (pkgmgrinfo_appinfo_metadata_filter_create(&appHandle) == PMINFO_R_OK) {
1265                 if (pkgmgrinfo_appinfo_metadata_filter_add(appHandle, AOT_METADATA_KEY, METADATA_VALUE_TRUE) == PMINFO_R_OK) {
1266                         if (pkgmgrAppMDFilterForeach(appHandle, appAotCb, &pkgList) != 0) {
1267                                 ret = NI_ERROR_UNKNOWN;
1268                                 _ERR("pkgmgrAppMDFilterForeach failed");
1269                         }
1270                 } else {
1271                         ret = NI_ERROR_UNKNOWN;
1272                         _ERR("pkgmgrinfo_appinfo_metadata_filter_add failed");
1273                 }
1274                 pkgmgrinfo_appinfo_metadata_filter_destroy(appHandle);
1275         } else {
1276                 ret = NI_ERROR_UNKNOWN;
1277                 _ERR("pkgmgrinfo_appinfo_metadata_filter_create failed");
1278         }
1279
1280         // remove duplicated pkg in the list.
1281         // If one package has multiple apps, there can be duplicate values.
1282         std::sort(pkgList.begin(), pkgList.end());
1283         pkgList.erase(unique(pkgList.begin(), pkgList.end()), pkgList.end());
1284
1285         for (auto pkg : pkgList) {
1286                 if (isReadOnlyPkg(pkg) && opt->flags & NI_FLAGS_SKIP_RO_APP) {
1287                         continue;
1288                 }
1289
1290                 if (removeAndCreateNI(pkg.c_str(), opt) != NI_ERROR_NONE) {
1291                         _SERR("Failed to remove previous dlls from [%s]", pkg.c_str());
1292                         ret = NI_ERROR_UNKNOWN;
1293                 } else {
1294                         _SOUT("Complete make application to native image");
1295                 }
1296         }
1297
1298         return ret;
1299 }
1300
1301 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
1302 static int regenTacCb(pkgmgrinfo_appinfo_h handle, void *userData)
1303 {
1304         char *pkgId = NULL;
1305         char *root = NULL;
1306         NIOption **pOpt = (NIOption**)userData;
1307
1308         int ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
1309         if (ret != PMINFO_R_OK || pkgId == NULL) {
1310                 _SERR("Failed to get pkgid");
1311                 return -1;
1312         }
1313
1314         ret = pkgmgrinfo_appinfo_get_root_path(handle, &root);
1315         if (ret != PMINFO_R_OK) {
1316                 _SERR("Failed to get root path");
1317                 return -1;
1318         }
1319
1320         std::string binPath = concatPath(std::string(root), "bin");
1321         if (exist(concatPath(binPath, PRE_COMPILED_PACKAGE_FILE))) {
1322                 _INFO("The %s is a Pre-Compiled package. So, skip the TAC", pkgId);
1323                 return 0;
1324         }
1325
1326         sqlite3 *tac_db = openDB(TAC_APP_LIST_DB);
1327         if (!tac_db) {
1328                 _SERR("Sqlite open error");
1329                 return -1;
1330         }
1331         sqlite3_exec(tac_db, "BEGIN;", NULL, NULL, NULL);
1332
1333         char *sql = sqlite3_mprintf("SELECT * FROM TAC WHERE PKGID = %Q;", pkgId);
1334         std::vector<std::string> nugets = selectDB(tac_db, sql);
1335         sqlite3_free(sql);
1336
1337         if (tac_db) {
1338                 closeDB(tac_db);
1339                 tac_db = NULL;
1340         }
1341
1342         std::string nugetPaths;
1343         for (const auto &nuget : nugets) {
1344                 if (!nugetPaths.empty()) {
1345                         nugetPaths += ":";
1346                 }
1347                 nugetPaths += concatPath(__DOTNET_DIR, nuget);
1348         }
1349
1350         for (auto& nuget : nugets) {
1351                 createNIUnderTAC(concatPath(__DOTNET_DIR, nuget), nugetPaths, *pOpt);
1352         }
1353
1354         return 0;
1355 }
1356
1357 ni_error_e regenerateTACNI(NIOption* opt)
1358 {
1359         removeNIUnderDirs(__DOTNET_DIR);
1360
1361         pkgmgrinfo_appinfo_metadata_filter_h handle;
1362         int ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
1363         if (ret != PMINFO_R_OK) {
1364                 return NI_ERROR_UNKNOWN;
1365         }
1366
1367         ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, TAC_METADATA_KEY, METADATA_VALUE_TRUE);
1368         if (ret != PMINFO_R_OK) {
1369                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1370                 return NI_ERROR_UNKNOWN;
1371         }
1372
1373         ret = pkgmgrAppMDFilterForeach(handle, regenTacCb, &opt);
1374         if (ret != 0) {
1375                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1376                 return NI_ERROR_UNKNOWN;
1377         }
1378
1379         pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
1380
1381         return NI_ERROR_NONE;
1382 }
1383