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