Merge "find dlls in the app directory first for app NI" into tizen
[platform/core/dotnet/launcher.git] / NativeLauncher / installer-plugin / 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 "common.h"
33
34 #ifdef  LOG_TAG
35 #undef  LOG_TAG
36 #endif
37 #define LOG_TAG "NETCORE_INSTALLER_PLUGIN"
38
39 #ifndef DEVICE_API_DIR
40 #error "DEVICE_API_DIR is missed"
41 #endif
42
43 #ifndef RUNTIME_DIR
44 #error "RUNTIME_DIR is missed"
45 #endif
46
47 #ifndef CROSSGEN_PATH
48 #error "CROSSGEN_PATH is missed"
49 #endif
50
51 #define __XSTR(x) #x
52 #define __STR(x) __XSTR(x)
53 static const char* DeviceAPIDir = __STR(DEVICE_API_DIR);
54 static const char* RuntimeDir = __STR(RUNTIME_DIR);
55 static const char* CrossgenPath = __STR(CROSSGEN_PATH);
56 static const char* JITPath = __STR(RUNTIME_DIR)"/libclrjit.so";
57 #undef __STR
58 #undef __XSTR
59
60 static void crossgen(const char* dll_path, const char* app_path);
61 static void smack_(const char* dll_path);
62
63 std::string Replace(std::string &str, const std::string& from, const std::string& to)
64 {
65   size_t start_pos = 0;
66   while((start_pos = str.find(from, start_pos)) != std::string::npos)
67   {
68     str.replace(start_pos, from.length(), to);
69     start_pos += to.length();
70   }
71   return str;
72 }
73
74 void create_ni_platform()
75 {
76   std::string corlib = ConcatPath(RuntimeDir, "System.Private.CoreLib.dll");
77   std::string nicorlib = ConcatPath(RuntimeDir, "System.Private.CoreLib.ni.dll");
78
79   if (FileNotExist(nicorlib))
80   {
81     crossgen(corlib.c_str(), nullptr);
82     smack_(nicorlib.c_str());
83   }
84
85   const char* platform_dirs[] = {RuntimeDir, DeviceAPIDir, "/usr/bin"};
86   const char* ignores[] = {corlib.c_str()};
87
88   create_ni_under_dirs(platform_dirs, 3, ignores, 1, [](const char* ni){
89       smack_(ni);
90   });
91 }
92
93 void create_ni_select(const char* dll_path)
94 {
95   std::string corlib = ConcatPath(RuntimeDir, "System.Private.CoreLib.dll");
96   std::string nicorlib = ConcatPath(RuntimeDir, "System.Private.CoreLib.ni.dll");
97
98   if (FileNotExist(nicorlib))
99   {
100     crossgen(corlib.c_str(), nullptr);
101     smack_(nicorlib.c_str());
102   }
103
104   if (!FileNotExist(dll_path))
105   {
106     std::string str_path = dll_path;
107     std::string ni_path = Replace(str_path, std::string(".dll"), std::string(".ni.dll"));
108     if (FileNotExist(ni_path))
109       crossgen(dll_path, nullptr);
110     else
111       printf("Already [%s] file is exist\n", ni_path.c_str());
112     smack_(ni_path.c_str());
113   }
114 }
115
116 static void smack_(const char* dll_path)
117 {
118   static const char* CHKSMACK = "/usr/bin/chsmack";
119   pid_t pid = fork();
120   if (pid == -1)
121   {
122     return;
123   }
124
125   if (pid > 0)
126   {
127     int status;
128     waitpid(pid, &status, 0);
129     if (WIFEXITED(status))
130     {
131       return;
132     }
133   }
134   else
135   {
136     const char* args[] = {
137       CHKSMACK,
138       "-a", "_",
139       dll_path,
140       nullptr
141     };
142     execv(CHKSMACK, const_cast<char*const*>(args));
143
144     exit(0);
145   }
146 }
147
148 static void crossgen(const char* dll_path, const char* app_path)
149 {
150   //pid_t parent = getpid();
151   pid_t pid = fork();
152   if (pid == -1)
153   {
154     return;
155   }
156
157   if (pid > 0)
158   {
159     int status;
160     waitpid(pid, &status, 0);
161     if (WIFEXITED(status))
162     {
163       return;
164     }
165   }
166   else
167   {
168     // search dlls in the application directory first, to use application dlls
169     // instead of system dlls when proceeding NI
170     std::vector<std::string> tpaDir;
171     if (app_path != NULL)
172     {
173       std::string path(app_path);
174       std::string::size_type prev_pos = 0, pos = 0;
175       while((pos = path.find(':', pos)) != std::string::npos)
176       {
177         std::string substring(path.substr(prev_pos, pos - prev_pos));
178         tpaDir.push_back(substring);
179         prev_pos = ++pos;
180       }
181       std::string substring(path.substr(prev_pos, pos - prev_pos));
182       tpaDir.push_back(substring);
183     }
184     tpaDir.push_back(RuntimeDir);
185     tpaDir.push_back(DeviceAPIDir);
186
187     std::string tpa;
188     AssembliesInDirectory(tpaDir, tpa);
189
190     std::vector<const char*> argv =
191     {
192       CrossgenPath,
193       "/Trusted_Platform_Assemblies", tpa.c_str(),
194       "/JITPath", JITPath,
195       "/FragileNonVersionable"
196     };
197     if (app_path != nullptr)
198     {
199       argv.push_back("/App_Paths");
200       argv.push_back(app_path);
201     }
202     argv.push_back(dll_path);
203     argv.push_back(nullptr);
204
205     /*
206     for (const char* arg : argv)
207     {
208       printf("%s ", arg);
209     }
210     printf("\n");
211     */
212     printf("+ %s\n", dll_path);
213
214     execv(CrossgenPath, const_cast<char* const*>(argv.data()));
215     exit(0);
216   }
217 }
218
219 static int get_root_path(const char *pkgid, std::string& root_path)
220 {
221   int ret = 0;
222   char *path = 0;
223
224   uid_t uid = 0;
225
226   if (pkgmgr_installer_info_get_target_uid(&uid) < 0)
227   {
228     _ERR("Failed to get UID");
229     return -1;
230   }
231
232   _INFO("user id is %d", uid);
233
234   pkgmgrinfo_pkginfo_h handle;
235   if (uid == 0)
236   {
237     ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
238     if (ret != PMINFO_R_OK)
239       return -1;
240   }
241   else
242   {
243     ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
244     if (ret != PMINFO_R_OK)
245       return -1;
246   }
247
248   ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
249   if (ret != PMINFO_R_OK) {
250     pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
251     return -1;
252   }
253   root_path = path;
254   pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
255
256   return 0;
257 }
258
259 static bool NIExist(const std::string& path, std::string& ni)
260 {
261   static const char* possible_exts[] = {
262     ".ni.dll", ".NI.dll", ".NI.DLL", ".ni.DLL"
263   };
264   std::string fname = path.substr(0, path.size() - 4);
265
266   struct stat sb;
267
268   for (const char* ext : possible_exts)
269   {
270     std::string f = fname + ext;
271     if (stat(f.c_str(), &sb) == 0)
272     {
273       ni = f;
274       return true;
275     }
276   }
277
278   return false;
279 }
280
281 void create_ni_under_dirs(const char* root_paths[], int count, const char* ignores[], int igcount, after_create cb)
282 {
283   std::string app_paths;
284   for (int i=0; i<count; i++)
285   {
286     app_paths += root_paths[i];
287     app_paths += ':';
288   }
289   if (app_paths.back() == ':')
290     app_paths.pop_back();
291
292   auto convert = [&app_paths, ignores, igcount, &cb](const char* path, const char* name)
293   {
294     for (int i=0; i<igcount; i++)
295     {
296       if (strcmp(path, ignores[i]) == 0)
297         return;
298     }
299     std::string ni;
300     if (IsManagedAssembly(path) && !IsNativeImage(path) && !NIExist(path, ni))
301     {
302       crossgen(path, app_paths.c_str());
303       if (NIExist(path, ni))
304       {
305         if (cb != nullptr)
306         {
307           cb(ni.c_str());
308         }
309       }
310     }
311   };
312
313   for (int i=0; i<count; i++)
314   {
315     ScanFilesInDir(root_paths[i], convert, -1);
316   }
317 }
318 void create_ni_under_dirs(const char* root_paths[], int count, after_create cb)
319 {
320   create_ni_under_dirs(root_paths, count, nullptr, 0, cb);
321 }
322 void create_ni_under_dirs(const char* root_paths[], int count)
323 {
324   create_ni_under_dirs(root_paths, count, nullptr);
325 }
326
327 int create_ni_under_pkg_root(const char* pkg_name)
328 {
329   std::string pkgroot;
330   if (get_root_path(pkg_name, pkgroot) < 0)
331   {
332     return 1;
333   }
334
335   //printf("pkgroot : %s\n", pkgroot.c_str());
336
337   std::string bindir = ConcatPath(pkgroot, "bin");
338   std::string libdir = ConcatPath(pkgroot, "lib");
339   
340   //printf("bindir : %s\n", bindir.c_str());
341   //printf("libdir : %s\n", libdir.c_str());
342   _INFO("bindir : %s", bindir.c_str());
343   _INFO("libdir : %s", libdir.c_str());
344
345   const char* paths[] = {
346     bindir.c_str(),
347     libdir.c_str()
348   };
349   create_ni_under_dirs(paths, 2);
350
351   return 0;
352 }