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"
43 #define LOG_TAG "NETCORE_INSTALLER_PLUGIN"
45 #ifndef DEVICE_API_DIR
46 #error "DEVICE_API_DIR is missed"
50 #error "RUNTIME_DIR is missed"
54 #error "CROSSGEN_PATH is missed"
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";
67 static std::string replace(std::string &str, const std::string& from, const std::string& to)
70 while ((startPos = str.find(from, startPos)) != std::string::npos) {
71 str.replace(startPos, from.length(), to);
72 startPos += to.length();
78 static void smack_(const char* dllPath, const char* label)
80 static const char* chsmack = "/usr/bin/chsmack";
87 waitpid(pid, &status, 0);
88 if (WIFEXITED(status))
91 const char* args[] = {
97 execv(chsmack, const_cast<char*const*>(args));
103 static void crossgen(const char* dllPath, const char* appPath)
105 //pid_t parent = getpid();
112 waitpid(pid, &status, 0);
113 if (WIFEXITED(status))
116 // search dlls in the application directory first, to use application dlls
117 // instead of system dlls when proceeding NI
118 std::vector<std::string> tpaDir;
119 if (appPath != NULL) {
120 std::string path(appPath);
121 std::string::size_type prevPos = 0, pos = 0;
122 while ((pos = path.find(':', pos)) != std::string::npos) {
123 std::string substring(path.substr(prevPos, pos - prevPos));
124 tpaDir.push_back(substring);
127 std::string substring(path.substr(prevPos, pos - prevPos));
128 tpaDir.push_back(substring);
130 tpaDir.push_back(__RUNTIME_DIR);
131 tpaDir.push_back(__DEVICE_API_DIR);
133 // get reference API directory ([DEVICE_API_DIR]/ref)
134 int len = strlen(__DEVICE_API_DIR);
135 char* refAPIDir = (char*)calloc(len + 5, 1);
137 printf("fail to allocate memory for reference API directory\n");
140 snprintf(refAPIDir, len + 5, "%s%s", __DEVICE_API_DIR, "/ref");
141 tpaDir.push_back(refAPIDir);
144 assembliesInDirectory(tpaDir, tpa);
146 std::vector<const char*> argv = {
148 "/Trusted_Platform_Assemblies", tpa.c_str(),
149 "/JITPath", __JIT_PATH,
150 "/FragileNonVersionable"
152 if (appPath != nullptr) {
153 argv.push_back("/App_Paths");
154 argv.push_back(appPath);
156 argv.push_back(dllPath);
157 argv.push_back(nullptr);
159 printf("+ %s\n", dllPath);
161 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
166 static int getRootPath(const char *pkgId, std::string& rootPath)
173 if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
174 _ERR("Failed to get UID");
178 _INFO("user id is %d", uid);
180 pkgmgrinfo_pkginfo_h handle;
182 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId, &handle);
183 if (ret != PMINFO_R_OK)
186 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId, uid, &handle);
187 if (ret != PMINFO_R_OK)
191 ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
192 if (ret != PMINFO_R_OK) {
193 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
197 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
202 static bool niExist(const std::string& path, std::string& ni)
204 // native image of System.Private.CoreLib.dll should have to overwrite
205 // original file to support new coreclr
206 if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
207 std::string coreLibBackup = path + ".Backup";
208 if (!fileNotExist(coreLibBackup)) {
215 static const char* possibleExts[] = {
216 ".ni.dll", ".NI.dll", ".NI.DLL", ".ni.DLL",
217 ".ni.exe", ".NI.exe", ".NI.EXE", ".ni.EXE"
219 std::string fName = path.substr(0, path.size() - 4);
223 for (const char* ext : possibleExts) {
224 std::string f = fName + ext;
225 if (stat(f.c_str(), &sb) == 0) {
234 static void createCoreLibNI()
236 std::string coreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.dll");
237 std::string niCoreLib = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.ni.dll");
238 std::string coreLibBackup = concatPath(__RUNTIME_DIR, "System.Private.CoreLib.dll.Backup");
240 if (!niExist(coreLib, niCoreLib)) {
241 crossgen(coreLib.c_str(), nullptr);
242 if (!fileNotExist(niCoreLib)) {
243 // change owner and groups for generated ni file.
245 if (!stat(coreLib.c_str(), &info)) {
246 if (chown(niCoreLib.c_str(), info.st_uid, info.st_gid) == -1)
247 _ERR("Failed to change owner and group name");
249 smack_(niCoreLib.c_str(), "_");
250 rename(coreLib.c_str(), coreLibBackup.c_str());
251 rename(niCoreLib.c_str(), coreLib.c_str());
256 void createNiPlatform()
260 const char* platformDirs[] = {__RUNTIME_DIR, __DEVICE_API_DIR, "/usr/bin"};
262 createNiUnderDirs(platformDirs, 3, [](const char* ni) {
267 void createNiSelect(const char* dllPath)
272 if (!fileNotExist(dllPath)) {
273 if (!niExist(dllPath, niPath)) {
274 crossgen(dllPath, nullptr);
275 if (niExist(dllPath, niPath)) {
276 // change owner and groups for generated ni file.
278 if (!stat(dllPath, &info)) {
279 if (chown(niPath.c_str(), info.st_uid, info.st_gid) == -1)
280 _ERR("Failed to change owner and group name");
282 smack_(niPath.c_str(), "_");
286 printf("Already [%s] file is exist\n", niPath.c_str());
290 void createNiUnderDirs(const char* rootPaths[], int count, const char* ignores[], int igcount, afterCreate cb)
292 std::string appPaths;
293 for (int i = 0; i < count; i++) {
294 appPaths += rootPaths[i];
298 if (appPaths.back() == ':')
301 auto convert = [&appPaths, ignores, igcount, &cb](const char* path, const char* name) {
302 for (int i = 0; i < igcount; i++) {
303 if (strcmp(path, ignores[i]) == 0)
307 if (isManagedAssembly(path) && !isNativeImage(path) && !niExist(path, ni)) {
308 crossgen(path, appPaths.c_str());
309 if (niExist(path, ni)) {
310 // change owner and groups for generated ni file.
312 if (!stat(path, &info)) {
313 if (chown(ni.c_str(), info.st_uid, info.st_gid) == -1)
314 _ERR("Failed to change owner and group name");
323 for (int i = 0; i < count; i++)
324 scanFilesInDir(rootPaths[i], convert, -1);
326 void createNiUnderDirs(const char* rootPaths[], int count, afterCreate cb)
328 createNiUnderDirs(rootPaths, count, nullptr, 0, cb);
330 void createNiUnderDirs(const char* rootPaths[], int count)
332 createNiUnderDirs(rootPaths, count, nullptr);
335 int createNiUnderPkgRoot(const char* pkgName)
338 if (getRootPath(pkgName, pkgRoot) < 0)
341 std::string binDir = concatPath(pkgRoot, "bin");
342 std::string libDir = concatPath(pkgRoot, "lib");
343 _INFO("bindir : %s", binDir.c_str());
344 _INFO("libdir : %s", libDir.c_str());
346 const char* paths[] = {
351 // change smack label for generated ni file.
352 std::string label = "User::Pkg::" + std::string(pkgName) + "::RO";
353 createNiUnderDirs(paths, 2, [label](const char* ni) {
354 smack_(ni, label.c_str());