Support 3.0-preview7
[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
29 #include <algorithm>
30 #include <string>
31 #include <fstream>
32
33 #include <pwd.h>
34 #include <grp.h>
35 #include <unistd.h>
36 #include <string.h>
37
38 #include "ni_common.h"
39 #include "tac_common.h"
40 #include "path_manager.h"
41 #include "plugin_manager.h"
42
43 #ifdef  LOG_TAG
44 #undef  LOG_TAG
45 #endif
46 #define LOG_TAG "DOTNET_INSTALLER_PLUGIN"
47
48 #ifndef CROSSGEN_PATH
49 #error "CROSSGEN_PATH is missed"
50 #endif
51
52 #define __XSTR(x) #x
53 #define __STR(x) __XSTR(x)
54 static const char* __CROSSGEN_PATH = __STR(CROSSGEN_PATH);
55 static const char* __TAC_DIR = __STR(TAC_DIR);
56
57 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
58 static const char* __SYSTEM_BASE_FILE = __STR(SYSTEM_BASE_FILE);
59 #endif
60
61 #undef __STR
62 #undef __XSTR
63
64 static int __interval = 0;
65 static std::string __tpa;
66
67 static void waitInterval()
68 {
69         // by the recommand, ignore small value for performance.
70         if (__interval > 10000) {
71                 fprintf(stderr, "sleep %d usec\n", __interval);
72                 usleep(__interval);
73         }
74 }
75
76 static std::string getNiFilePath(const std::string& dllPath)
77 {
78         size_t index = dllPath.find_last_of(".");
79         if (index == std::string::npos) {
80                 fprintf(stderr, "File doesnot contain extension. fail to get NI file name\n");
81                 return "";
82         }
83         std::string fName = dllPath.substr(0, index);
84         std::string fExt = dllPath.substr(index, dllPath.length());
85
86         // crossgen generate file with lower case extension only
87         std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
88         std::string niPath = fName + ".ni" + fExt;
89
90         return niPath;
91 }
92
93 static std::string getAppNIPath(const std::string& niPath)
94 {
95         std::string fileName;
96         std::string niDirPath;
97         std::string prevPath;
98
99         size_t index = niPath.find_last_of("/");
100         if (index != std::string::npos) {
101                 prevPath = niPath.substr(0, index);
102                 fileName = niPath.substr(index + 1, niPath.length());
103         } else {
104                 prevPath = ".";
105                 fileName = niPath;
106         }
107
108         niDirPath = concatPath(prevPath, APP_NI_SUB_DIR);
109
110         if (!isFileExist(niDirPath)) {
111                 if (mkdir(niDirPath.c_str(), 0755) == 0) {
112                         updateAssemblyInfo(prevPath, niDirPath);
113                 } else {
114                         fprintf(stderr, "Fail to create app ni directory (%s)\n", niDirPath.c_str());
115                 }
116         }
117
118         return concatPath(niDirPath, fileName);
119 }
120
121 static bool niExist(const std::string& path)
122 {
123         std::string f = getNiFilePath(path);
124         if (f.empty()) {
125                 return false;
126         }
127
128         if (isFileExist(f)) {
129                 return true;
130         }
131
132         // native image of System.Private.CoreLib.dll should have to overwrite
133         // original file to support new coreclr
134         if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
135                 std::string coreLibBackup = path + ".Backup";
136                 if (isFileExist(coreLibBackup)) {
137                         return true;
138                 }
139         }
140
141         return false;
142 }
143
144 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
145 // Get next base address to be used for system ni image from file
146 // __SYSTEM_BASE_FILE should be checked for existance before calling this function
147 static uintptr_t getNextBaseAddrFromFile()
148 {
149         FILE *pFile = fopen(__SYSTEM_BASE_FILE, "r");
150         if (pFile == NULL) {
151                 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
152                 return 0;
153         }
154
155         uintptr_t addr = 0;
156         uintptr_t size = 0;
157
158         while (fscanf(pFile, "%u %u", &addr, &size) != EOF) {
159         }
160
161         fclose(pFile);
162
163         return addr + size;
164 }
165
166 // Get next base address to be used for system ni image
167 static uintptr_t getNextBaseAddr()
168 {
169         uintptr_t baseAddr = 0;
170
171         if (!isFileExist(__SYSTEM_BASE_FILE)) {
172                 // This is the starting address for all default base addresses
173                 baseAddr = DEFAULT_BASE_ADDR_START;
174         } else {
175                 baseAddr = getNextBaseAddrFromFile();
176
177                 // Round to a multple of 64K (see ZapImage::CalculateZapBaseAddress in CoreCLR)
178                 uintptr_t BASE_ADDRESS_ALIGNMENT = 0xffff;
179                 baseAddr = (baseAddr + BASE_ADDRESS_ALIGNMENT) & ~BASE_ADDRESS_ALIGNMENT;
180         }
181
182         return baseAddr;
183 }
184
185 // Save base address of system ni image to file
186 static void updateBaseAddrFile(const std::string &absNiPath, uintptr_t baseAddr)
187 {
188         uintptr_t niSize = getFileSize(absNiPath);
189         if (niSize == 0) {
190                 fprintf(stderr, "File %s doesn't exist\n", absNiPath.c_str());
191                 return;
192         }
193
194         // Write new entry to the file
195         FILE *pFile = fopen(__SYSTEM_BASE_FILE, "a");
196         if (pFile == NULL) {
197                 fprintf(stderr, "Failed to open %s\n", __SYSTEM_BASE_FILE);
198                 return;
199         }
200
201         fprintf(pFile, "%u %u\n", baseAddr, niSize);
202         fclose(pFile);
203 }
204
205 // check if dll is listed in TPA
206 static bool isTPADll(const std::string &dllPath)
207 {
208         std::string absDllPath = absolutePath(dllPath);
209
210         if (__tpa.find(absDllPath) != std::string::npos) {
211                 return true;
212         }
213
214         return false;
215 }
216 #endif
217
218 // baseAddr should be checked in file before getting here
219 static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, bool enableR2R, bool isAppNI = false)
220 {
221         if (!isFileExist(dllPath)) {
222                 fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
223                 return NI_ERROR_NO_SUCH_FILE;
224         }
225
226         if (!isManagedAssembly(dllPath)) {
227                 fprintf(stderr, "Input file is not a dll file : %s\n", dllPath.c_str());
228                 return NI_ERROR_INVALID_PARAMETER;
229         }
230
231         if (niExist(dllPath)) {
232                 fprintf(stderr, "Already ni file is exist for %s\n", dllPath.c_str());
233                 return NI_ERROR_ALREADY_EXIST;
234         }
235
236         std::string absDllPath = absolutePath(dllPath);
237         std::string absNiPath = getNiFilePath(dllPath);
238         if (absNiPath.empty()) {
239                 fprintf(stderr, "Fail to get ni file name\n");
240                 return NI_ERROR_UNKNOWN;
241         }
242
243         if (isAppNI && strstr(absNiPath.c_str(), __TAC_DIR) == NULL) {
244                 absNiPath = getAppNIPath(absNiPath);
245         }
246
247 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
248         uintptr_t baseAddr = 0;
249
250         if (isTPADll(dllPath)) {
251                 baseAddr = getNextBaseAddr();
252         }
253 #endif
254
255         pid_t pid = fork();
256         if (pid == -1)
257                 return NI_ERROR_UNKNOWN;
258
259         if (pid > 0) {
260                 int status;
261                 waitpid(pid, &status, 0);
262                 if (WIFEXITED(status)) {
263                         // Do not use niExist() function to check whether ni file created or not.
264                         // niEixst() return false for System.Private.Corelib.dll
265                         if (isFileExist(absNiPath)) {
266                                 updateAssemblyInfo(absDllPath, absNiPath);
267 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
268                                 if (baseAddr != 0) {
269                                         updateBaseAddrFile(absNiPath, baseAddr);
270                                 }
271 #endif
272                                 return NI_ERROR_NONE;
273                         } else {
274                                 fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
275                                 return NI_ERROR_NO_SUCH_FILE;
276                         }
277                 }
278         } else {
279                 std::string jitPath = getRuntimeDir() + "/libclrjit.so";
280                 std::vector<const char*> argv = {
281                         __CROSSGEN_PATH,
282                         "/nologo",
283                         "/r", __tpa.c_str(),
284                         "/JITPath", jitPath.c_str()
285                 };
286
287                 if (!enableR2R) {
288                         argv.push_back("/FragileNonVersionable");
289                 }
290
291 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
292                 if (baseAddr != 0) {
293                         argv.push_back("/BaseAddress");
294                         argv.push_back(std::to_string(baseAddr).c_str());
295                 }
296 #endif
297
298                 argv.push_back("/App_Paths");
299                 std::string absAppPath;
300                 if (!appPath.empty()) {
301                         absAppPath = appPath;
302                 } else {
303                         absAppPath = baseName(absDllPath);
304                 }
305                 argv.push_back(absAppPath.c_str());
306
307                 argv.push_back("/out");
308                 argv.push_back(absNiPath.c_str());
309
310                 argv.push_back(absDllPath.c_str());
311                 argv.push_back(nullptr);
312
313                 fprintf(stderr, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
314
315                 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
316                 exit(0);
317         }
318
319         return NI_ERROR_NONE;
320 }
321
322 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
323 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
324 {
325         char *pkgId = NULL;
326         int ret = 0;
327         bool* enableR2R = (bool*)userData;
328
329         ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
330         if (ret != PMINFO_R_OK) {
331                 fprintf(stderr, "Failed to get pkgid\n");
332                 return -1;
333         }
334
335         if (removeNiUnderPkgRoot(pkgId) != NI_ERROR_NONE) {
336                 fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
337                 return -1;
338         }
339
340         if (resetTACPackage(pkgId) != TAC_ERROR_NONE) {
341                 fprintf(stderr, "Failed to remove symlink for given package [%s]\n", pkgId);
342                 return -1;
343         }
344
345         // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
346         if (createNiUnderPkgRoot(pkgId, *enableR2R) != NI_ERROR_NONE) {
347                 fprintf(stderr, "Failed to generate NI file [%s]\n", pkgId);
348                 return -1;
349         } else {
350                 fprintf(stderr, "Complete make application to native image\n");
351         }
352
353         if (createTACPackage(pkgId) != TAC_ERROR_NONE) {
354                 fprintf(stderr, "Failed to generate symbolic link file [%s]\n", pkgId);
355                 return -1;
356         }else {
357                 fprintf(stderr, "Complete make symbolic link file to tac\n");
358         }
359
360         return 0;
361 }
362
363 static void createCoreLibNI(bool enableR2R)
364 {
365         std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
366         std::string niCoreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.ni.dll");
367         std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
368
369         if (!isFileExist(coreLibBackup)) {
370
371                 if (!crossgen(coreLib, std::string(), enableR2R)) {
372                         if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
373                                 fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
374                         }
375                         if (rename(niCoreLib.c_str(), coreLib.c_str())) {
376                                 fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
377                         }
378                 } else {
379                         fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
380                 }
381         }
382 }
383
384 ni_error_e initNICommon(NiCommonOption* option)
385 {
386 #if defined(__arm__)
387         // get interval value
388         const char* intervalFile = "/usr/share/dotnet.tizen/lib/crossgen_interval.txt";
389         std::ifstream inFile(intervalFile);
390         if (inFile) {
391                 fprintf(stderr, "crossgen_interval.txt is found\n");
392                 inFile >> __interval;
393         }
394
395         if (initializePluginManager("normal")) {
396                 fprintf(stderr, "Fail to initialize PluginManager\n");
397                 return NI_ERROR_UNKNOWN;
398         }
399         if (initializePathManager(option->runtimeDir, option->tizenFXDir, option->extraDirs)) {
400                 fprintf(stderr, "Fail to initialize PathManager\n");
401                 return NI_ERROR_UNKNOWN;
402         }
403
404         __tpa = getTPA();
405
406         return NI_ERROR_NONE;
407 #else
408         fprintf(stderr, "crossgen supports arm architecture only. skip ni file generation\n");
409         return NI_ERROR_NOT_SUPPORTED;
410 #endif
411 }
412
413 void finalizeNICommon()
414 {
415         __interval = 0;
416
417         finalizePluginManager();
418         finalizePathManager();
419
420         __tpa.clear();
421 }
422
423
424 void createNiPlatform(bool enableR2R)
425 {
426         const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
427         createNiUnderDirs(platformDirs, 2, enableR2R);
428 }
429
430 ni_error_e createNiDll(const std::string& dllPath, bool enableR2R)
431 {
432         createCoreLibNI(enableR2R);
433         // System.Private.CoreLib.dll is generated in the createCoreLibNI function.
434         // Skip if input dll is System.Private.CoreLib.dll
435         if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
436                 return NI_ERROR_NONE;
437         }
438
439         ni_error_e status = crossgen(dllPath, std::string(), enableR2R);
440
441         return status;
442 }
443
444 void createNiUnderTAC(const std::string rootPaths[], int count)
445 {
446         std::string appPaths;
447         try {
448                 for (auto& nuget : bf::recursive_directory_iterator(__TAC_DIR)) {
449                         std::string nugetPath = nuget.path().string();
450                         if (bf::is_directory(nugetPath)) {
451                                 appPaths += nugetPath;
452                                 appPaths += ':';
453                         }
454                 }
455                 if (appPaths.back() == ':') {
456                         appPaths.pop_back();
457                 }
458
459                 auto convert = [&appPaths](const std::string& path, const char* name) {
460                         if (strstr(path.c_str(), TAC_APP_LIST_DB) != NULL ||
461                                 strstr(path.c_str(), TAC_APP_LIST_RESTORE_DB) != NULL ||
462                                 strstr(path.c_str(), TAC_SHA_256_INFO) != NULL)
463                                 return;
464                         if (!crossgen(path, appPaths.c_str(), false)) {
465                                 waitInterval();
466                         }
467                 };
468
469                 for (int i = 0; i < count; i++) {
470                         scanFilesInDir(rootPaths[i], convert, -1);
471                 }
472         } catch (const bf::filesystem_error& error) {
473                 fprintf(stderr, "Failed to recursive directory: %s", error.what());
474                 return;
475         }
476 }
477
478 void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R, bool isAppNI)
479 {
480         createCoreLibNI(enableR2R);
481
482         std::string appPaths;
483         for (int i = 0; i < count; i++) {
484                 appPaths += rootPaths[i];
485                 appPaths += ':';
486         }
487
488         if (appPaths.back() == ':')
489                 appPaths.pop_back();
490
491         std::vector<std::string> tpaAssemblies;
492         splitPath(__tpa, tpaAssemblies);
493
494         auto convert = [&appPaths, enableR2R, isAppNI, tpaAssemblies](const std::string& path, const char* name) {
495                 if (isAppNI) {
496                         std::string assembly = path.substr(path.rfind('/') + 1);
497                         bool isExist = false;
498                         for (auto& tpa : tpaAssemblies) {
499                                 if (!strcmp(replaceAll(tpa, ".ni.dll", ".dll").c_str(), assembly.c_str())) {
500                                         isExist = true;
501                                         break;
502                                 }
503                         }
504                         if (isExist) {
505                                 fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", path.c_str());
506                                 return;
507                         }
508                 }
509                 if (!crossgen(path, appPaths.c_str(), enableR2R, isAppNI)) {
510                         waitInterval();
511                 }
512         };
513
514         for (int i = 0; i < count; i++) {
515                 scanFilesInDir(rootPaths[i], convert, 1);
516         }
517
518         tpaAssemblies.clear();
519 }
520
521 ni_error_e createNiUnderPkgRoot(const std::string& pkgId, bool enableR2R)
522 {
523         std::string pkgRoot;
524         if (getRootPath(pkgId, pkgRoot) < 0) {
525                 return NI_ERROR_INVALID_PACKAGE;
526         }
527
528         std::string binDir = concatPath(pkgRoot, "bin");
529         std::string libDir = concatPath(pkgRoot, "lib");
530         std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
531         std::string paths[] = {binDir, libDir, tacDir};
532
533         createNiUnderDirs(paths, 3, enableR2R, true);
534
535         return NI_ERROR_NONE;
536 }
537
538 ni_error_e createNiDllUnderPkgRoot(const std::string& pkgId, const std::string& dllPath, bool enableR2R)
539 {
540         std::string pkgRoot;
541         if (getRootPath(pkgId, pkgRoot) < 0) {
542                 return NI_ERROR_INVALID_PACKAGE;
543         }
544
545         std::string binDir = concatPath(pkgRoot, "bin");
546         std::string libDir = concatPath(pkgRoot, "lib");
547         std::string tacDir = concatPath(binDir, TAC_SYMLINK_SUB_DIR);
548         std::string paths = binDir + ":" + libDir + ":" + tacDir;
549
550         if (bf::is_symlink(dllPath)) {
551                 if (bf::exists(tacDir)) {
552                         if (!isNativeImage(dllPath)) {
553                                 std::string originPath = bf::read_symlink(dllPath).string();
554                                 std::string originNiPath = originPath.substr(0, originPath.rfind(".dll")) + ".ni.dll";
555                                 if (!bf::exists(originNiPath)) {
556                                         if(createNiDll(originPath, false) != NI_ERROR_NONE) {
557                                                 fprintf(stderr, "Failed to create NI file [%s]\n", originPath.c_str());
558                                                 return NI_ERROR_UNKNOWN;
559                                         }
560                                 }
561                                 std::string setNiPath = dllPath.substr(0, dllPath.rfind(".dll")) + ".ni.dll";
562                                 if (!bf::exists(setNiPath)) {
563                                         bf::create_symlink(originNiPath, setNiPath);
564                                         fprintf(stderr, "%s symbolic link file generated successfully.\n", setNiPath.c_str());
565                                         updateAssemblyInfo(tacDir.c_str(), setNiPath.c_str(), true);
566                                 }
567                         }
568                 }
569                 return NI_ERROR_NONE;
570         } else {
571                 std::string assembly = dllPath.substr(dllPath.rfind('/') + 1);
572                 std::vector<std::string> tpaAssemblies;
573                 splitPath(__tpa, tpaAssemblies);
574                 bool isExist = false;
575                 for (auto& tpa : tpaAssemblies) {
576                         if (!strcmp(replaceAll(tpa, ".ni.dll", ".dll").c_str(), assembly.c_str())) {
577                                 isExist = true;
578                                 break;
579                         }
580                 }
581                 tpaAssemblies.clear();
582                 if (isExist) {
583                         fprintf(stderr, "%s present in the TPA list skips generation of NI file.\n", dllPath.c_str());
584                         return NI_ERROR_NONE;
585                 } else {
586                         return crossgen(dllPath, paths, enableR2R, true);
587                 }
588         }
589 }
590
591 void removeNiPlatform()
592 {
593         std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
594         std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
595
596         if (!isFileExist(coreLibBackup)) {
597                 return;
598         }
599
600 #ifdef UNIQUE_DEFAULT_BASE_ADDR_SUPPORT
601         if (isFileExist(__SYSTEM_BASE_FILE)) {
602                 if (remove(__SYSTEM_BASE_FILE)) {
603                         fprintf(stderr, "Failed to remove %s\n", __SYSTEM_BASE_FILE);
604                 }
605         }
606 #endif
607
608         if (remove(coreLib.c_str())) {
609                 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
610         }
611
612         if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
613                 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
614         }
615
616         const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
617
618         removeNiUnderDirs(platformDirs, 2);
619 }
620
621 void removeNiUnderDirs(const std::string rootPaths[], int count)
622 {
623         auto convert = [](const std::string& path, std::string name) {
624                 if (isNativeImage(path)) {
625                         if (remove(path.c_str())) {
626                                 fprintf(stderr, "Failed to remove %s\n", path.c_str());
627                         }
628                 }
629         };
630
631         for (int i = 0; i < count; i++)
632                 scanFilesInDir(rootPaths[i], convert, -1);
633 }
634
635 ni_error_e removeNiUnderPkgRoot(const std::string& pkgId)
636 {
637         std::string pkgRoot;
638         if (getRootPath(pkgId, pkgRoot) < 0) {
639                 return NI_ERROR_INVALID_PACKAGE;
640         }
641
642         std::string binDir = concatPath(pkgRoot, "bin");
643         std::string libDir = concatPath(pkgRoot, "lib");
644         std::string paths[] = {binDir, libDir};
645
646         removeNiUnderDirs(paths, 2);
647
648         std::string binNIDir = concatPath(binDir, APP_NI_SUB_DIR);
649         if (isFileExist(binNIDir)) {
650                 if (rmdir(binNIDir.c_str()) != 0) {
651                         fprintf(stderr, "Failed to remove app ni dir [%s]\n", binNIDir.c_str());
652                 }
653         }
654
655         std::string libNIDir = concatPath(libDir, APP_NI_SUB_DIR);
656         if (isFileExist(libNIDir)) {
657                 if (rmdir(libNIDir.c_str()) != 0) {
658                         fprintf(stderr, "Failed to remove app ni dir [%s]\n", libNIDir.c_str());
659                 }
660         }
661
662         return NI_ERROR_NONE;
663 }
664
665 ni_error_e regenerateAppNI(bool enableR2R)
666 {
667         int ret = 0;
668         pkgmgrinfo_appinfo_metadata_filter_h handle;
669
670         ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
671         if (ret != PMINFO_R_OK)
672                 return NI_ERROR_UNKNOWN;
673
674         ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, METADATA_VALUE);
675         if (ret != PMINFO_R_OK) {
676                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
677                 return NI_ERROR_UNKNOWN;
678         }
679
680         ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, &enableR2R);
681         if (ret != PMINFO_R_OK) {
682                 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
683                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
684                 return NI_ERROR_UNKNOWN;
685         }
686
687         fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
688
689         pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
690         return NI_ERROR_NONE;
691 }