Fix Preload DLL
[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 void create_ni_platform()
64 {
65   std::string corlib = ConcatPath(RuntimeDir, "System.Private.CoreLib.dll");
66   std::string nicorlib = ConcatPath(RuntimeDir, "System.Private.CoreLib.ni.dll");
67
68   if (FileNotExist(nicorlib))
69   {
70     crossgen(corlib.c_str(), nullptr);
71     smack_(nicorlib.c_str());
72   }
73
74   const char* platform_dirs[] = {RuntimeDir, DeviceAPIDir};
75   const char* ignores[] = {corlib.c_str()};
76
77   create_ni_under_dirs(platform_dirs, 2, ignores, 1, [](const char* ni){
78       smack_(ni);
79   });
80 }
81
82 static void smack_(const char* dll_path)
83 {
84   static const char* CHKSMACK = "/usr/bin/chsmack";
85   pid_t pid = fork();
86   if (pid == -1)
87   {
88     return;
89   }
90
91   if (pid > 0)
92   {
93     int status;
94     waitpid(pid, &status, 0);
95     if (WIFEXITED(status))
96     {
97       return;
98     }
99   }
100   else
101   {
102     const char* args[] = {
103       CHKSMACK,
104       "-a", "_",
105       dll_path,
106       nullptr
107     };
108     execv(CHKSMACK, const_cast<char*const*>(args));
109
110     exit(0);
111   }
112 }
113
114 static void crossgen(const char* dll_path, const char* app_path)
115 {
116   //pid_t parent = getpid();
117   pid_t pid = fork();
118   if (pid == -1)
119   {
120     return;
121   }
122
123   if (pid > 0)
124   {
125     int status;
126     waitpid(pid, &status, 0);
127     if (WIFEXITED(status))
128     {
129       return;
130     }
131   }
132   else
133   {
134     std::vector<std::string> tpaDir = {
135       RuntimeDir, DeviceAPIDir
136     };
137     std::string tpa;
138     AssembliesInDirectory(tpaDir, tpa);
139
140     std::vector<const char*> argv =
141     {
142       CrossgenPath,
143       "/Trusted_Platform_Assemblies", tpa.c_str(),
144       "/JITPath", JITPath,
145       "/FragileNonVersionable"
146     };
147     if (app_path != nullptr)
148     {
149       argv.push_back("/App_Paths");
150       argv.push_back(app_path);
151     }
152     argv.push_back(dll_path);
153     argv.push_back(nullptr);
154
155     /*
156     for (const char* arg : argv)
157     {
158       printf("%s ", arg);
159     }
160     printf("\n");
161     */
162     printf("+ %s\n", dll_path);
163
164     execv(CrossgenPath, const_cast<char* const*>(argv.data()));
165     exit(0);
166   }
167 }
168
169 static int get_root_path(const char *pkgid, std::string& root_path)
170 {
171   int ret = 0;
172   char *path = 0;
173
174   uid_t uid = 0;
175
176   if (pkgmgr_installer_info_get_target_uid(&uid) < 0)
177   {
178     _ERR("Failed to get UID");
179     return -1;
180   }
181
182   _INFO("user id is %d", uid);
183
184   pkgmgrinfo_pkginfo_h handle;
185   if (uid == 0)
186   {
187     ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
188     if (ret != PMINFO_R_OK)
189       return -1;
190   }
191   else
192   {
193     ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
194     if (ret != PMINFO_R_OK)
195       return -1;
196   }
197
198   ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
199   if (ret != PMINFO_R_OK) {
200     pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
201     return -1;
202   }
203   root_path = path;
204   pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
205
206   return 0;
207 }
208
209 static bool NIExist(const std::string& path, std::string& ni)
210 {
211   static const char* possible_exts[] = {
212     ".ni.dll", ".NI.dll", ".NI.DLL", ".ni.DLL"
213   };
214   std::string fname = path.substr(0, path.size() - 4);
215
216   struct stat sb;
217
218   for (const char* ext : possible_exts)
219   {
220     std::string f = fname + ext;
221     if (stat(f.c_str(), &sb) == 0)
222     {
223       ni = f;
224       return true;
225     }
226   }
227
228   return false;
229 }
230
231 void create_ni_under_dirs(const char* root_paths[], int count, const char* ignores[], int igcount, after_create cb)
232 {
233   std::string app_paths;
234   for (int i=0; i<count; i++)
235   {
236     app_paths += root_paths[i];
237     app_paths += ':';
238   }
239   if (app_paths.back() == ':')
240     app_paths.pop_back();
241
242   auto convert = [&app_paths, ignores, igcount, &cb](const char* path)
243   {
244     for (int i=0; i<igcount; i++)
245     {
246       if (strcmp(path, ignores[i]) == 0)
247         return;
248     }
249     std::string ni;
250     if (IsManagedAssembly(path) && !IsNativeImage(path) && !NIExist(path, ni))
251     {
252       crossgen(path, app_paths.c_str());
253       if (NIExist(path, ni))
254       {
255         if (cb != nullptr)
256         {
257           cb(ni.c_str());
258         }
259       }
260     }
261   };
262
263   for (int i=0; i<count; i++)
264   {
265     ScanFilesInDir(root_paths[i], convert, -1);
266   }
267 }
268 void create_ni_under_dirs(const char* root_paths[], int count, after_create cb)
269 {
270   create_ni_under_dirs(root_paths, count, nullptr, 0, cb);
271 }
272 void create_ni_under_dirs(const char* root_paths[], int count)
273 {
274   create_ni_under_dirs(root_paths, count, nullptr);
275 }
276
277 int create_ni_under_pkg_root(const char* pkg_name)
278 {
279   std::string pkgroot;
280   if (get_root_path(pkg_name, pkgroot) < 0)
281   {
282     return 1;
283   }
284
285   //printf("pkgroot : %s\n", pkgroot.c_str());
286
287   std::string bindir = ConcatPath(pkgroot, "bin");
288   std::string libdir = ConcatPath(pkgroot, "lib");
289   
290   //printf("bindir : %s\n", bindir.c_str());
291   //printf("libdir : %s\n", libdir.c_str());
292   _INFO("bindir : %s", bindir.c_str());
293   _INFO("libdir : %s", libdir.c_str());
294
295   const char* paths[] = {
296     bindir.c_str(),
297     libdir.c_str()
298   };
299   create_ni_under_dirs(paths, 2);
300
301   return 0;
302 }