8c200cea01ba618efc09d2a75c0a6ec2bb8466d0
[platform/core/dotnet/launcher.git] / NativeLauncher / installer-plugin / 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
32 #include <pwd.h>
33 #include <grp.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <string.h>
37
38 #include <fstream>
39 #include <sys/smack.h>
40
41 #include "ni_common.h"
42 #include "path_manager.h"
43 #include "plugin_manager.h"
44
45 #ifdef  LOG_TAG
46 #undef  LOG_TAG
47 #endif
48 #define LOG_TAG "NETCORE_INSTALLER_PLUGIN"
49
50 #ifndef CROSSGEN_PATH
51 #error "CROSSGEN_PATH is missed"
52 #endif
53
54 #define __XSTR(x) #x
55 #define __STR(x) __XSTR(x)
56 static const char* __CROSSGEN_PATH = __STR(CROSSGEN_PATH);
57 #undef __STR
58 #undef __XSTR
59
60 static int __interval = 0;
61
62 static void waitInterval()
63 {
64         // by the recommand, ignore small value for performance.
65         if (__interval > 10000) {
66                 fprintf(stderr, "sleep %d usec\n", __interval);
67                 usleep(__interval);
68         }
69 }
70
71 static bool niExist(const std::string& path, std::string& ni)
72 {
73         static std::string possibleExts[] = {
74                 ".ni.dll", ".NI.dll", ".NI.DLL", ".ni.DLL",
75                 ".ni.exe", ".NI.exe", ".NI.EXE", ".ni.EXE"
76         };
77         std::string fName = path.substr(0, path.size() - 4);
78
79         struct stat sb;
80
81         for (std::string ext : possibleExts) {
82                 std::string f = fName + ext;
83                 if (stat(f.c_str(), &sb) == 0) {
84                         ni = f;
85                         return true;
86                 }
87         }
88
89         // native image of System.Private.CoreLib.dll should have to overwrite
90         // original file to support new coreclr
91         if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
92                 std::string coreLibBackup = path + ".Backup";
93                 if (isFileExist(coreLibBackup)) {
94                         ni = path;
95                         return true;
96                 }
97         }
98
99         return false;
100 }
101
102 static void updateNiFileInfo(const std::string& path)
103 {
104         char* label = NULL;
105         std::string niPath;
106
107         if (niExist(path, niPath)) {
108                 // change smack label
109                 if (smack_getlabel(path.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
110                         if (smack_setlabel(niPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
111                                 fprintf(stderr, "Fail to set smack label\n");
112                         }
113                         free(label);
114                 }
115
116                 // change owner and groups for generated ni file.
117                 struct stat info;
118                 if (!stat(path.c_str(), &info)) {
119                         if (chown(niPath.c_str(), info.st_uid, info.st_gid) == -1)
120                                 fprintf(stderr, "Failed to change owner and group name\n");
121                 }
122         }
123 }
124
125 static void crossgen(const std::string& dllPath, const std::string& appPath, bool enableR2R)
126 {
127         std::string absDllPath = absolutePath(dllPath);
128
129         pid_t pid = fork();
130         if (pid == -1)
131                 return;
132
133         if (pid > 0) {
134                 int status;
135                 waitpid(pid, &status, 0);
136                 if (WIFEXITED(status)) {
137                         updateNiFileInfo(absDllPath);
138                         return;
139                 }
140         } else {
141                 std::string tpa = getTPA();
142                 std::string jitPath = getRuntimeDir() + "/libclrjit.so";
143                 std::vector<const char*> argv = {
144                         __CROSSGEN_PATH,
145                         "/nologo",
146                         "/Trusted_Platform_Assemblies", tpa.c_str(),
147                         "/JITPath", jitPath.c_str()
148                 };
149
150                 if (!enableR2R) {
151                         argv.push_back("/FragileNonVersionable");
152                 }
153
154                 argv.push_back("/App_Paths");
155                 std::string absAppPath;
156                 if (!appPath.empty()) {
157                         absAppPath = appPath;
158                 } else {
159                         absAppPath = baseName(absDllPath);
160                 }
161                 argv.push_back(absAppPath.c_str());
162
163                 argv.push_back(absDllPath.c_str());
164                 argv.push_back(nullptr);
165
166                 fprintf(stderr, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
167
168                 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
169                 exit(0);
170         }
171 }
172
173 static int getRootPath(std::string pkgId, std::string& rootPath)
174 {
175         int ret = 0;
176         char *path = 0;
177
178         uid_t uid = 0;
179
180         if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
181                 _ERR("Failed to get UID");
182                 return -1;
183         }
184
185         pkgmgrinfo_pkginfo_h handle;
186         if (uid == 0) {
187                 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
188                 if (ret != PMINFO_R_OK)
189                         return -1;
190         } else {
191                 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
192                 if (ret != PMINFO_R_OK)
193                         return -1;
194         }
195
196         ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
197         if (ret != PMINFO_R_OK) {
198                 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
199                 return -1;
200         }
201         rootPath = path;
202         pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
203
204         return 0;
205 }
206
207
208 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
209 {
210         char *pkgId = NULL;
211         int ret = 0;
212
213         ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
214         if (ret != PMINFO_R_OK) {
215                 fprintf(stderr, "Failed to get pkgid\n");
216                 return -1;
217         }
218
219         if (removeNiUnderPkgRoot(pkgId) != 0) {
220                 fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
221                 return -1;
222         }
223
224         // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
225         if (createNiUnderPkgRoot(pkgId, true) != 0) {
226                 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId);
227                 return -1;
228         } else {
229                 fprintf(stderr, "Complete make application to native image\n");
230         }
231
232         return 0;
233 }
234
235 static void createCoreLibNI(bool enableR2R)
236 {
237         std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
238         std::string niCoreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.ni.dll");
239         std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
240
241         if (!niExist(coreLib, niCoreLib)) {
242                 crossgen(coreLib, std::string(), enableR2R);
243                 if (isFileExist(niCoreLib)) {
244                         if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
245                                 fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
246                         }
247                         if (rename(niCoreLib.c_str(), coreLib.c_str())) {
248                                 fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
249                         }
250                 } else {
251                         fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
252                 }
253         }
254 }
255
256 int initNICommon(NiCommonOption* option)
257 {
258         // get interval value
259         const char* intervalFile = "/usr/share/dotnet.tizen/lib/crossgen_interval.txt";
260         std::ifstream inFile(intervalFile);
261         if (inFile) {
262                 fprintf(stderr, "crossgen_interval.txt is found\n");
263                 inFile >> __interval;
264         }
265
266         if (initializePluginManager("normal")) {
267                 fprintf(stderr, "Fail to initialize plugin manager\n");
268                 return -1;
269         }
270         if (initializePathManager(option->runtimeDir, option->tizenFXDir, option->extraDirs)) {
271                 fprintf(stderr, "Fail to initialize path manager\n");
272                 return -1;
273         }
274
275         return 0;
276 }
277
278 void finalizeNICommon()
279 {
280         __interval = 0;
281
282         finalizePluginManager();
283         finalizePathManager();
284 }
285
286
287 void createNiPlatform(bool enableR2R)
288 {
289         const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
290
291         createNiUnderDirs(platformDirs, 2, enableR2R);
292 }
293
294 void createNiDll(const std::string& dllPath, bool enableR2R)
295 {
296         createCoreLibNI(enableR2R);
297
298         if (!isFileExist(dllPath)) {
299                 fprintf(stderr, "Failed to find dll : %s\n", dllPath.c_str());
300                 return;
301         }
302
303         std::string niPath;
304         if (niExist(dllPath, niPath)) {
305                 fprintf(stderr, "Already [%s] file is exist\n", niPath.c_str());
306                 return;
307         }
308
309         crossgen(dllPath, std::string(), enableR2R);
310         if (!niExist(dllPath, niPath)) {
311                 fprintf(stderr, "Failed to create native image for %s\n", dllPath.c_str());
312         }
313 }
314
315 void createNiUnderDirs(const std::string rootPaths[], int count, const std::string ignores[], int igcount, afterCreate cb, bool enableR2R)
316 {
317         createCoreLibNI(enableR2R);
318
319         std::string appPaths;
320         for (int i = 0; i < count; i++) {
321                 appPaths += rootPaths[i];
322                 appPaths += ':';
323         }
324
325         if (appPaths.back() == ':')
326                 appPaths.pop_back();
327
328         auto convert = [&appPaths, ignores, igcount, &cb, enableR2R](const std::string& path, const char* name) {
329                 for (int i = 0; i < igcount; i++) {
330                         if (path == ignores[i])
331                                 return;
332                 }
333                 std::string niPath;
334                 if (isManagedAssembly(path)) {
335                         if (niExist(path, niPath)) {
336                                 fprintf(stderr, "Already [%s] file is exist\n", niPath.c_str());
337                                 return;
338                         }
339                         crossgen(path, appPaths.c_str(), enableR2R);
340                         if (niExist(path, niPath)) {
341                                 if (cb != nullptr)
342                                         cb(niPath.c_str());
343                         } else {
344                                 fprintf(stderr, "Failed to create native image for %s\n", path.c_str());
345                         }
346                         waitInterval();
347                 }
348         };
349
350         for (int i = 0; i < count; i++)
351                 scanFilesInDir(rootPaths[i], convert, 1);
352 }
353 void createNiUnderDirs(const std::string rootPaths[], int count, afterCreate cb, bool enableR2R)
354 {
355         createNiUnderDirs(rootPaths, count, nullptr, 0, cb, enableR2R);
356 }
357 void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R)
358 {
359         createNiUnderDirs(rootPaths, count, nullptr, enableR2R);
360 }
361
362 int createNiUnderPkgRoot(const std::string& pkgName, bool enableR2R)
363 {
364         std::string pkgRoot;
365         if (getRootPath(pkgName, pkgRoot) < 0)
366                 return -1;
367
368         std::string binDir = concatPath(pkgRoot, "bin");
369         std::string libDir = concatPath(pkgRoot, "lib");
370         std::string paths[] = {binDir, libDir};
371
372         createNiUnderDirs(paths, 2, enableR2R);
373
374         return 0;
375 }
376
377 int createNiDllUnderPkgRoot(const std::string& pkgName, const std::string& dllPath, bool enableR2R)
378 {
379         std::string pkgRoot;
380         if (getRootPath(pkgName, pkgRoot) < 0)
381                 return -1;
382
383         std::string binDir = concatPath(pkgRoot, "bin");
384         std::string libDir = concatPath(pkgRoot, "lib");
385         std::string appPaths = binDir + ":" + libDir;
386
387         if (!isFileExist(dllPath)) {
388                 fprintf(stderr, "Failed to find dll : %s\n", dllPath.c_str());
389                 return -1;
390         }
391
392         std::string niPath;
393         if (niExist(dllPath, niPath)) {
394                 fprintf(stderr, "Already [%s] file is exist\n", niPath.c_str());
395                 return -1;
396         }
397
398         crossgen(dllPath, appPaths, enableR2R);
399         if (!niExist(dllPath, niPath)) {
400                 fprintf(stderr, "Failed to create native image for %s\n", dllPath.c_str());
401                 return -1;
402         }
403
404         return 0;
405 }
406
407 void removeNiPlatform()
408 {
409         std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
410         std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
411
412         if (!isFileExist(coreLibBackup)) {
413                 return;
414         }
415
416         if (remove(coreLib.c_str())) {
417                 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
418         }
419
420         if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
421                 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
422         }
423
424         const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
425
426         removeNiUnderDirs(platformDirs, 2);
427 }
428
429 void removeNiUnderDirs(const std::string rootPaths[], int count)
430 {
431         auto convert = [](const std::string& path, std::string name) {
432                 std::string ni;
433                 if (isNativeImage(path)) {
434                         if (remove(path.c_str())) {
435                                 fprintf(stderr, "Failed to remove %s\n", path.c_str());
436                         }
437                 }
438         };
439
440         for (int i = 0; i < count; i++)
441                 scanFilesInDir(rootPaths[i], convert, -1);
442 }
443
444 int removeNiUnderPkgRoot(const std::string& pkgName)
445 {
446         std::string pkgRoot;
447         if (getRootPath(pkgName, pkgRoot) < 0)
448                 return -1;
449
450         std::string binDir = concatPath(pkgRoot, "bin");
451         std::string libDir = concatPath(pkgRoot, "lib");
452         std::string paths[] = {binDir, libDir};
453
454         removeNiUnderDirs(paths, 2);
455
456         return 0;
457 }
458
459 int regenerateAppNI()
460 {
461         int ret = 0;
462         pkgmgrinfo_appinfo_metadata_filter_h handle;
463
464         ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
465         if (ret != PMINFO_R_OK)
466                 return -1;
467
468         ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, "http://tizen.org/metadata/prefer_dotnet_aot", "true");
469         if (ret != PMINFO_R_OK) {
470                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
471                 return -1;
472         }
473
474         ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, NULL);
475         if (ret != PMINFO_R_OK) {
476                 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
477                 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
478                 return -1;
479         }
480
481         fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
482
483         pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
484         return 0;
485 }
486