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