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"
39 #include <sys/smack.h>
41 #include "ni_common.h"
42 #include "path_manager.h"
43 #include "plugin_manager.h"
48 #define LOG_TAG "NETCORE_INSTALLER_PLUGIN"
51 #error "CROSSGEN_PATH is missed"
55 #define __STR(x) __XSTR(x)
56 static const char* __CROSSGEN_PATH = __STR(CROSSGEN_PATH);
60 static int __interval = 0;
61 static std::string __tpa;
63 static void waitInterval()
65 // by the recommand, ignore small value for performance.
66 if (__interval > 10000) {
67 fprintf(stderr, "sleep %d usec\n", __interval);
72 static bool niExist(const std::string& path, std::string& ni)
74 static std::string possibleExts[] = {
75 ".ni.dll", ".NI.dll", ".NI.DLL", ".ni.DLL",
76 ".ni.exe", ".NI.exe", ".NI.EXE", ".ni.EXE"
78 std::string fName = path.substr(0, path.size() - 4);
82 for (std::string ext : possibleExts) {
83 std::string f = fName + ext;
84 if (stat(f.c_str(), &sb) == 0) {
90 // native image of System.Private.CoreLib.dll should have to overwrite
91 // original file to support new coreclr
92 if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
93 std::string coreLibBackup = path + ".Backup";
94 if (isFileExist(coreLibBackup)) {
103 static void updateNiFileInfo(const std::string& path)
108 if (niExist(path, niPath)) {
109 // change smack label
110 if (smack_getlabel(path.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
111 if (smack_setlabel(niPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
112 fprintf(stderr, "Fail to set smack label\n");
117 // change owner and groups for generated ni file.
119 if (!stat(path.c_str(), &info)) {
120 if (chown(niPath.c_str(), info.st_uid, info.st_gid) == -1)
121 fprintf(stderr, "Failed to change owner and group name\n");
126 static void crossgen(const std::string& dllPath, const std::string& appPath, bool enableR2R)
128 std::string absDllPath = absolutePath(dllPath);
136 waitpid(pid, &status, 0);
137 if (WIFEXITED(status)) {
138 updateNiFileInfo(absDllPath);
142 std::string jitPath = getRuntimeDir() + "/libclrjit.so";
143 std::vector<const char*> argv = {
146 "/Trusted_Platform_Assemblies", __tpa.c_str(),
147 "/JITPath", jitPath.c_str()
151 argv.push_back("/FragileNonVersionable");
154 argv.push_back("/App_Paths");
155 std::string absAppPath;
156 if (!appPath.empty()) {
157 absAppPath = appPath;
159 absAppPath = baseName(absDllPath);
161 argv.push_back(absAppPath.c_str());
163 argv.push_back(absDllPath.c_str());
164 argv.push_back(nullptr);
166 fprintf(stderr, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
168 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
173 static int getRootPath(std::string pkgId, std::string& rootPath)
180 if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
181 _ERR("Failed to get UID");
185 pkgmgrinfo_pkginfo_h handle;
187 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
188 if (ret != PMINFO_R_OK)
191 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
192 if (ret != PMINFO_R_OK)
196 ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
197 if (ret != PMINFO_R_OK) {
198 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
202 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
208 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
213 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
214 if (ret != PMINFO_R_OK) {
215 fprintf(stderr, "Failed to get pkgid\n");
219 if (removeNiUnderPkgRoot(pkgId) != 0) {
220 fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
224 // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
225 if (createNiUnderPkgRoot(pkgId, true) != 0) {
226 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId);
229 fprintf(stderr, "Complete make application to native image\n");
235 static void createCoreLibNI(bool enableR2R)
237 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
238 std::string niCoreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.ni.dll");
239 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
241 if (!niExist(coreLib, niCoreLib)) {
242 crossgen(coreLib, std::string(), enableR2R);
243 if (isFileExist(niCoreLib)) {
244 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
245 fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
247 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
248 fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
251 fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
256 int initNICommon(NiCommonOption* option)
258 // get interval value
259 const char* intervalFile = "/usr/share/dotnet.tizen/lib/crossgen_interval.txt";
260 std::ifstream inFile(intervalFile);
262 fprintf(stderr, "crossgen_interval.txt is found\n");
263 inFile >> __interval;
266 if (initializePluginManager("normal")) {
267 fprintf(stderr, "Fail to initialize plugin manager\n");
270 if (initializePathManager(option->runtimeDir, option->tizenFXDir, option->extraDirs)) {
271 fprintf(stderr, "Fail to initialize path manager\n");
280 void finalizeNICommon()
284 finalizePluginManager();
285 finalizePathManager();
291 void createNiPlatform(bool enableR2R)
293 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
295 createNiUnderDirs(platformDirs, 2, enableR2R);
298 void createNiDll(const std::string& dllPath, bool enableR2R)
300 createCoreLibNI(enableR2R);
302 if (!isFileExist(dllPath)) {
303 fprintf(stderr, "Failed to find dll : %s\n", dllPath.c_str());
308 if (niExist(dllPath, niPath)) {
309 fprintf(stderr, "Already [%s] file is exist\n", niPath.c_str());
313 crossgen(dllPath, std::string(), enableR2R);
314 if (!niExist(dllPath, niPath)) {
315 fprintf(stderr, "Failed to create native image for %s\n", dllPath.c_str());
319 void createNiUnderDirs(const std::string rootPaths[], int count, const std::string ignores[], int igcount, afterCreate cb, bool enableR2R)
321 createCoreLibNI(enableR2R);
323 std::string appPaths;
324 for (int i = 0; i < count; i++) {
325 appPaths += rootPaths[i];
329 if (appPaths.back() == ':')
332 auto convert = [&appPaths, ignores, igcount, &cb, enableR2R](const std::string& path, const char* name) {
333 for (int i = 0; i < igcount; i++) {
334 if (path == ignores[i])
338 if (isManagedAssembly(path)) {
339 if (niExist(path, niPath)) {
340 fprintf(stderr, "Already [%s] file is exist\n", niPath.c_str());
343 crossgen(path, appPaths.c_str(), enableR2R);
344 if (niExist(path, niPath)) {
348 fprintf(stderr, "Failed to create native image for %s\n", path.c_str());
354 for (int i = 0; i < count; i++)
355 scanFilesInDir(rootPaths[i], convert, 1);
357 void createNiUnderDirs(const std::string rootPaths[], int count, afterCreate cb, bool enableR2R)
359 createNiUnderDirs(rootPaths, count, nullptr, 0, cb, enableR2R);
361 void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R)
363 createNiUnderDirs(rootPaths, count, nullptr, enableR2R);
366 int createNiUnderPkgRoot(const std::string& pkgName, bool enableR2R)
369 if (getRootPath(pkgName, pkgRoot) < 0)
372 std::string binDir = concatPath(pkgRoot, "bin");
373 std::string libDir = concatPath(pkgRoot, "lib");
374 std::string paths[] = {binDir, libDir};
376 createNiUnderDirs(paths, 2, enableR2R);
381 int createNiDllUnderPkgRoot(const std::string& pkgName, const std::string& dllPath, bool enableR2R)
384 if (getRootPath(pkgName, pkgRoot) < 0)
387 std::string binDir = concatPath(pkgRoot, "bin");
388 std::string libDir = concatPath(pkgRoot, "lib");
389 std::string appPaths = binDir + ":" + libDir;
391 if (!isFileExist(dllPath)) {
392 fprintf(stderr, "Failed to find dll : %s\n", dllPath.c_str());
397 if (niExist(dllPath, niPath)) {
398 fprintf(stderr, "Already [%s] file is exist\n", niPath.c_str());
402 crossgen(dllPath, appPaths, enableR2R);
403 if (!niExist(dllPath, niPath)) {
404 fprintf(stderr, "Failed to create native image for %s\n", dllPath.c_str());
411 void removeNiPlatform()
413 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
414 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
416 if (!isFileExist(coreLibBackup)) {
420 if (remove(coreLib.c_str())) {
421 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
424 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
425 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
428 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
430 removeNiUnderDirs(platformDirs, 2);
433 void removeNiUnderDirs(const std::string rootPaths[], int count)
435 auto convert = [](const std::string& path, std::string name) {
437 if (isNativeImage(path)) {
438 if (remove(path.c_str())) {
439 fprintf(stderr, "Failed to remove %s\n", path.c_str());
444 for (int i = 0; i < count; i++)
445 scanFilesInDir(rootPaths[i], convert, -1);
448 int removeNiUnderPkgRoot(const std::string& pkgName)
451 if (getRootPath(pkgName, pkgRoot) < 0)
454 std::string binDir = concatPath(pkgRoot, "bin");
455 std::string libDir = concatPath(pkgRoot, "lib");
456 std::string paths[] = {binDir, libDir};
458 removeNiUnderDirs(paths, 2);
463 int regenerateAppNI()
466 pkgmgrinfo_appinfo_metadata_filter_h handle;
468 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
469 if (ret != PMINFO_R_OK)
472 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, "http://tizen.org/metadata/prefer_dotnet_aot", "true");
473 if (ret != PMINFO_R_OK) {
474 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
478 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, NULL);
479 if (ret != PMINFO_R_OK) {
480 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
481 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
485 fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
487 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);