2 * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <pkgmgr-info.h>
18 #include <pkgmgr_installer_info.h>
23 #include "pkgmgr_parser_plugin_interface.h"
37 #define LOG_TAG "NETCORE_INSTALLER_PLUGIN"
39 #ifndef DEVICE_API_DIR
40 #error "DEVICE_API_DIR is missed"
44 #error "RUNTIME_DIR is missed"
48 #error "CROSSGEN_PATH is missed"
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";
60 static void crossgen(const char* dll_path, const char* app_path);
61 static void smack_(const char* dll_path);
63 std::string Replace(std::string &str, const std::string& from, const std::string& to)
66 while((start_pos = str.find(from, start_pos)) != std::string::npos)
68 str.replace(start_pos, from.length(), to);
69 start_pos += to.length();
74 void create_ni_platform()
76 std::string corlib = ConcatPath(RuntimeDir, "System.Private.CoreLib.dll");
77 std::string nicorlib = ConcatPath(RuntimeDir, "System.Private.CoreLib.ni.dll");
79 if (FileNotExist(nicorlib))
81 crossgen(corlib.c_str(), nullptr);
82 smack_(nicorlib.c_str());
85 const char* platform_dirs[] = {RuntimeDir, DeviceAPIDir, "/usr/bin"};
86 const char* ignores[] = {corlib.c_str()};
88 create_ni_under_dirs(platform_dirs, 3, ignores, 1, [](const char* ni){
93 void create_ni_select(const char* dll_path)
95 std::string corlib = ConcatPath(RuntimeDir, "System.Private.CoreLib.dll");
96 std::string nicorlib = ConcatPath(RuntimeDir, "System.Private.CoreLib.ni.dll");
98 if (FileNotExist(nicorlib))
100 crossgen(corlib.c_str(), nullptr);
101 smack_(nicorlib.c_str());
104 if (!FileNotExist(dll_path))
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);
111 printf("Already [%s] file is exist\n", ni_path.c_str());
112 smack_(ni_path.c_str());
116 static void smack_(const char* dll_path)
118 static const char* CHKSMACK = "/usr/bin/chsmack";
128 waitpid(pid, &status, 0);
129 if (WIFEXITED(status))
136 const char* args[] = {
142 execv(CHKSMACK, const_cast<char*const*>(args));
148 static void crossgen(const char* dll_path, const char* app_path)
150 //pid_t parent = getpid();
160 waitpid(pid, &status, 0);
161 if (WIFEXITED(status))
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)
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)
177 std::string substring(path.substr(prev_pos, pos - prev_pos));
178 tpaDir.push_back(substring);
181 std::string substring(path.substr(prev_pos, pos - prev_pos));
182 tpaDir.push_back(substring);
184 tpaDir.push_back(RuntimeDir);
185 tpaDir.push_back(DeviceAPIDir);
188 AssembliesInDirectory(tpaDir, tpa);
190 std::vector<const char*> argv =
193 "/Trusted_Platform_Assemblies", tpa.c_str(),
195 "/FragileNonVersionable"
197 if (app_path != nullptr)
199 argv.push_back("/App_Paths");
200 argv.push_back(app_path);
202 argv.push_back(dll_path);
203 argv.push_back(nullptr);
206 for (const char* arg : argv)
212 printf("+ %s\n", dll_path);
214 execv(CrossgenPath, const_cast<char* const*>(argv.data()));
219 static int get_root_path(const char *pkgid, std::string& root_path)
226 if (pkgmgr_installer_info_get_target_uid(&uid) < 0)
228 _ERR("Failed to get UID");
232 _INFO("user id is %d", uid);
234 pkgmgrinfo_pkginfo_h handle;
237 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
238 if (ret != PMINFO_R_OK)
243 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
244 if (ret != PMINFO_R_OK)
248 ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
249 if (ret != PMINFO_R_OK) {
250 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
254 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
259 static bool NIExist(const std::string& path, std::string& ni)
261 static const char* possible_exts[] = {
262 ".ni.dll", ".NI.dll", ".NI.DLL", ".ni.DLL"
264 std::string fname = path.substr(0, path.size() - 4);
268 for (const char* ext : possible_exts)
270 std::string f = fname + ext;
271 if (stat(f.c_str(), &sb) == 0)
281 void create_ni_under_dirs(const char* root_paths[], int count, const char* ignores[], int igcount, after_create cb)
283 std::string app_paths;
284 for (int i=0; i<count; i++)
286 app_paths += root_paths[i];
289 if (app_paths.back() == ':')
290 app_paths.pop_back();
292 auto convert = [&app_paths, ignores, igcount, &cb](const char* path, const char* name)
294 for (int i=0; i<igcount; i++)
296 if (strcmp(path, ignores[i]) == 0)
300 if (IsManagedAssembly(path) && !IsNativeImage(path) && !NIExist(path, ni))
302 crossgen(path, app_paths.c_str());
303 if (NIExist(path, ni))
313 for (int i=0; i<count; i++)
315 ScanFilesInDir(root_paths[i], convert, -1);
318 void create_ni_under_dirs(const char* root_paths[], int count, after_create cb)
320 create_ni_under_dirs(root_paths, count, nullptr, 0, cb);
322 void create_ni_under_dirs(const char* root_paths[], int count)
324 create_ni_under_dirs(root_paths, count, nullptr);
327 int create_ni_under_pkg_root(const char* pkg_name)
330 if (get_root_path(pkg_name, pkgroot) < 0)
335 //printf("pkgroot : %s\n", pkgroot.c_str());
337 std::string bindir = ConcatPath(pkgroot, "bin");
338 std::string libdir = ConcatPath(pkgroot, "lib");
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());
345 const char* paths[] = {
349 create_ni_under_dirs(paths, 2);