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 void create_ni_platform()
65 std::string corlib = ConcatPath(RuntimeDir, "System.Private.CoreLib.dll");
66 std::string nicorlib = ConcatPath(RuntimeDir, "System.Private.CoreLib.ni.dll");
68 if (FileNotExist(nicorlib))
70 crossgen(corlib.c_str(), nullptr);
71 smack_(nicorlib.c_str());
74 const char* platform_dirs[] = {RuntimeDir, DeviceAPIDir};
75 const char* ignores[] = {corlib.c_str()};
77 create_ni_under_dirs(platform_dirs, 2, ignores, 1, [](const char* ni){
82 static void smack_(const char* dll_path)
84 static const char* CHKSMACK = "/usr/bin/chsmack";
94 waitpid(pid, &status, 0);
95 if (WIFEXITED(status))
102 const char* args[] = {
108 execv(CHKSMACK, const_cast<char*const*>(args));
114 static void crossgen(const char* dll_path, const char* app_path)
116 //pid_t parent = getpid();
126 waitpid(pid, &status, 0);
127 if (WIFEXITED(status))
134 std::vector<std::string> tpaDir = {
135 RuntimeDir, DeviceAPIDir
138 AssembliesInDirectory(tpaDir, tpa);
140 std::vector<const char*> argv =
143 "/Trusted_Platform_Assemblies", tpa.c_str(),
145 "/FragileNonVersionable"
147 if (app_path != nullptr)
149 argv.push_back("/App_Paths");
150 argv.push_back(app_path);
152 argv.push_back(dll_path);
153 argv.push_back(nullptr);
156 for (const char* arg : argv)
162 printf("+ %s\n", dll_path);
164 execv(CrossgenPath, const_cast<char* const*>(argv.data()));
169 static int get_root_path(const char *pkgid, std::string& root_path)
176 if (pkgmgr_installer_info_get_target_uid(&uid) < 0)
178 _ERR("Failed to get UID");
182 _INFO("user id is %d", uid);
184 pkgmgrinfo_pkginfo_h handle;
187 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgid, &handle);
188 if (ret != PMINFO_R_OK)
193 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgid, uid, &handle);
194 if (ret != PMINFO_R_OK)
198 ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
199 if (ret != PMINFO_R_OK) {
200 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
204 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
209 static bool NIExist(const std::string& path, std::string& ni)
211 static const char* possible_exts[] = {
212 ".ni.dll", ".NI.dll", ".NI.DLL", ".ni.DLL"
214 std::string fname = path.substr(0, path.size() - 4);
218 for (const char* ext : possible_exts)
220 std::string f = fname + ext;
221 if (stat(f.c_str(), &sb) == 0)
231 void create_ni_under_dirs(const char* root_paths[], int count, const char* ignores[], int igcount, after_create cb)
233 std::string app_paths;
234 for (int i=0; i<count; i++)
236 app_paths += root_paths[i];
239 if (app_paths.back() == ':')
240 app_paths.pop_back();
242 auto convert = [&app_paths, ignores, igcount, &cb](const char* path)
244 for (int i=0; i<igcount; i++)
246 if (strcmp(path, ignores[i]) == 0)
250 if (IsManagedAssembly(path) && !IsNativeImage(path) && !NIExist(path, ni))
252 crossgen(path, app_paths.c_str());
253 if (NIExist(path, ni))
263 for (int i=0; i<count; i++)
265 ScanFilesInDir(root_paths[i], convert, -1);
268 void create_ni_under_dirs(const char* root_paths[], int count, after_create cb)
270 create_ni_under_dirs(root_paths, count, nullptr, 0, cb);
272 void create_ni_under_dirs(const char* root_paths[], int count)
274 create_ni_under_dirs(root_paths, count, nullptr);
277 int create_ni_under_pkg_root(const char* pkg_name)
280 if (get_root_path(pkg_name, pkgroot) < 0)
285 //printf("pkgroot : %s\n", pkgroot.c_str());
287 std::string bindir = ConcatPath(pkgroot, "bin");
288 std::string libdir = ConcatPath(pkgroot, "lib");
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());
295 const char* paths[] = {
299 create_ni_under_dirs(paths, 2);