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 std::vector<std::string> tpaDir = {
169 RuntimeDir, DeviceAPIDir
172 AssembliesInDirectory(tpaDir, tpa);
174 std::vector<const char*> argv =
177 "/Trusted_Platform_Assemblies", tpa.c_str(),
179 "/FragileNonVersionable"
181 if (app_path != nullptr)
183 argv.push_back("/App_Paths");
184 argv.push_back(app_path);
186 argv.push_back(dll_path);
187 argv.push_back(nullptr);
190 for (const char* arg : argv)
196 printf("+ %s\n", dll_path);
198 execv(CrossgenPath, const_cast<char* const*>(argv.data()));
203 static int get_root_path(const char *pkgid, std::string& root_path)
210 if (pkgmgr_installer_info_get_target_uid(&uid) < 0)
212 _ERR("Failed to get UID");
216 _INFO("user id is %d", uid);
218 pkgmgrinfo_pkginfo_h handle;
221 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
222 if (ret != PMINFO_R_OK)
227 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
228 if (ret != PMINFO_R_OK)
232 ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
233 if (ret != PMINFO_R_OK) {
234 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
238 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
243 static bool NIExist(const std::string& path, std::string& ni)
245 static const char* possible_exts[] = {
246 ".ni.dll", ".NI.dll", ".NI.DLL", ".ni.DLL"
248 std::string fname = path.substr(0, path.size() - 4);
252 for (const char* ext : possible_exts)
254 std::string f = fname + ext;
255 if (stat(f.c_str(), &sb) == 0)
265 void create_ni_under_dirs(const char* root_paths[], int count, const char* ignores[], int igcount, after_create cb)
267 std::string app_paths;
268 for (int i=0; i<count; i++)
270 app_paths += root_paths[i];
273 if (app_paths.back() == ':')
274 app_paths.pop_back();
276 auto convert = [&app_paths, ignores, igcount, &cb](const char* path)
278 for (int i=0; i<igcount; i++)
280 if (strcmp(path, ignores[i]) == 0)
284 if (IsManagedAssembly(path) && !IsNativeImage(path) && !NIExist(path, ni))
286 crossgen(path, app_paths.c_str());
287 if (NIExist(path, ni))
297 for (int i=0; i<count; i++)
299 ScanFilesInDir(root_paths[i], convert, -1);
302 void create_ni_under_dirs(const char* root_paths[], int count, after_create cb)
304 create_ni_under_dirs(root_paths, count, nullptr, 0, cb);
306 void create_ni_under_dirs(const char* root_paths[], int count)
308 create_ni_under_dirs(root_paths, count, nullptr);
311 int create_ni_under_pkg_root(const char* pkg_name)
314 if (get_root_path(pkg_name, pkgroot) < 0)
319 //printf("pkgroot : %s\n", pkgroot.c_str());
321 std::string bindir = ConcatPath(pkgroot, "bin");
322 std::string libdir = ConcatPath(pkgroot, "lib");
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());
329 const char* paths[] = {
333 create_ni_under_dirs(paths, 2);