ddeabdc014246c95b518cae73d026c3f9184cf2a
[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     std::vector<std::string> tpaDir = {
169       RuntimeDir, DeviceAPIDir
170     };
171     std::string tpa;
172     AssembliesInDirectory(tpaDir, tpa);
173
174     std::vector<const char*> argv =
175     {
176       CrossgenPath,
177       "/Trusted_Platform_Assemblies", tpa.c_str(),
178       "/JITPath", JITPath,
179       "/FragileNonVersionable"
180     };
181     if (app_path != nullptr)
182     {
183       argv.push_back("/App_Paths");
184       argv.push_back(app_path);
185     }
186     argv.push_back(dll_path);
187     argv.push_back(nullptr);
188
189     /*
190     for (const char* arg : argv)
191     {
192       printf("%s ", arg);
193     }
194     printf("\n");
195     */
196     printf("+ %s\n", dll_path);
197
198     execv(CrossgenPath, const_cast<char* const*>(argv.data()));
199     exit(0);
200   }
201 }
202
203 static int get_root_path(const char *pkgid, std::string& root_path)
204 {
205   int ret = 0;
206   char *path = 0;
207
208   uid_t uid = 0;
209
210   if (pkgmgr_installer_info_get_target_uid(&uid) < 0)
211   {
212     _ERR("Failed to get UID");
213     return -1;
214   }
215
216   _INFO("user id is %d", uid);
217
218   pkgmgrinfo_pkginfo_h handle;
219   if (uid == 0)
220   {
221     ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
222     if (ret != PMINFO_R_OK)
223       return -1;
224   }
225   else
226   {
227     ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
228     if (ret != PMINFO_R_OK)
229       return -1;
230   }
231
232   ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
233   if (ret != PMINFO_R_OK) {
234     pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
235     return -1;
236   }
237   root_path = path;
238   pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
239
240   return 0;
241 }
242
243 static bool NIExist(const std::string& path, std::string& ni)
244 {
245   static const char* possible_exts[] = {
246     ".ni.dll", ".NI.dll", ".NI.DLL", ".ni.DLL"
247   };
248   std::string fname = path.substr(0, path.size() - 4);
249
250   struct stat sb;
251
252   for (const char* ext : possible_exts)
253   {
254     std::string f = fname + ext;
255     if (stat(f.c_str(), &sb) == 0)
256     {
257       ni = f;
258       return true;
259     }
260   }
261
262   return false;
263 }
264
265 void create_ni_under_dirs(const char* root_paths[], int count, const char* ignores[], int igcount, after_create cb)
266 {
267   std::string app_paths;
268   for (int i=0; i<count; i++)
269   {
270     app_paths += root_paths[i];
271     app_paths += ':';
272   }
273   if (app_paths.back() == ':')
274     app_paths.pop_back();
275
276   auto convert = [&app_paths, ignores, igcount, &cb](const char* path)
277   {
278     for (int i=0; i<igcount; i++)
279     {
280       if (strcmp(path, ignores[i]) == 0)
281         return;
282     }
283     std::string ni;
284     if (IsManagedAssembly(path) && !IsNativeImage(path) && !NIExist(path, ni))
285     {
286       crossgen(path, app_paths.c_str());
287       if (NIExist(path, ni))
288       {
289         if (cb != nullptr)
290         {
291           cb(ni.c_str());
292         }
293       }
294     }
295   };
296
297   for (int i=0; i<count; i++)
298   {
299     ScanFilesInDir(root_paths[i], convert, -1);
300   }
301 }
302 void create_ni_under_dirs(const char* root_paths[], int count, after_create cb)
303 {
304   create_ni_under_dirs(root_paths, count, nullptr, 0, cb);
305 }
306 void create_ni_under_dirs(const char* root_paths[], int count)
307 {
308   create_ni_under_dirs(root_paths, count, nullptr);
309 }
310
311 int create_ni_under_pkg_root(const char* pkg_name)
312 {
313   std::string pkgroot;
314   if (get_root_path(pkg_name, pkgroot) < 0)
315   {
316     return 1;
317   }
318
319   //printf("pkgroot : %s\n", pkgroot.c_str());
320
321   std::string bindir = ConcatPath(pkgroot, "bin");
322   std::string libdir = ConcatPath(pkgroot, "lib");
323   
324   //printf("bindir : %s\n", bindir.c_str());
325   //printf("libdir : %s\n", libdir.c_str());
326   _INFO("bindir : %s", bindir.c_str());
327   _INFO("libdir : %s", libdir.c_str());
328
329   const char* paths[] = {
330     bindir.c_str(),
331     libdir.c_str()
332   };
333   create_ni_under_dirs(paths, 2);
334
335   return 0;
336 }