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 void updateNiFileInfo(const std::string& dllPath, const std::string& niPath)
77 if (smack_getlabel(dllPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
78 if (smack_setlabel(niPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
79 fprintf(stderr, "Fail to set smack label\n");
84 // change owner and groups for generated ni file.
86 if (!stat(dllPath.c_str(), &info)) {
87 if (chown(niPath.c_str(), info.st_uid, info.st_gid) == -1)
88 fprintf(stderr, "Failed to change owner and group name\n");
92 static std::string getNiFilePath(const std::string& dllPath)
94 size_t index = dllPath.find_last_of(".");
95 if (index == std::string::npos) {
96 fprintf(stderr, "File doesnot contain extension. fail to get NI file name\n");
99 std::string fName = dllPath.substr(0, index);
100 std::string fExt = dllPath.substr(index, dllPath.length());
102 // crossgen generate file with lower case extension only
103 std::transform(fExt.begin(), fExt.end(), fExt.begin(), ::tolower);
104 std::string niPath = fName + ".ni" + fExt;
109 static std::string getAppNIPath(const std::string& niPath)
111 size_t index = niPath.find_last_of("/");
112 if (index == std::string::npos) {
113 fprintf(stderr, "dllPath doesnot contains path info\n");
117 std::string prevPath = niPath.substr(0, index);
118 std::string fileName = niPath.substr(index, niPath.length());
119 std::string niDirPath = prevPath + APP_NI_SUB_DIR;
121 if (!isFileExist(niDirPath)) {
122 if (mkdir(niDirPath.c_str(), 0755) == 0) {
123 updateNiFileInfo(prevPath, niDirPath);
125 fprintf(stderr, "Fail to create app ni directory (%s)\n", niDirPath.c_str());
129 return niDirPath + fileName;
132 static bool niExist(const std::string& path)
134 std::string f = getNiFilePath(path);
139 if (isFileExist(f)) {
143 // native image of System.Private.CoreLib.dll should have to overwrite
144 // original file to support new coreclr
145 if (path.find("System.Private.CoreLib.dll") != std::string::npos) {
146 std::string coreLibBackup = path + ".Backup";
147 if (isFileExist(coreLibBackup)) {
155 static ni_error_e crossgen(const std::string& dllPath, const std::string& appPath, bool enableR2R, bool isAppNI = false)
157 if (!isFileExist(dllPath)) {
158 fprintf(stderr, "dll file is not exist : %s\n", dllPath.c_str());
159 return NI_ERROR_NO_SUCH_FILE;
162 if (!isManagedAssembly(dllPath)) {
163 fprintf(stderr, "Input file is not a dll file : %s\n", dllPath.c_str());
164 return NI_ERROR_INVALID_PARAMETER;
167 if (niExist(dllPath)) {
168 fprintf(stderr, "Already ni file is exist for %s\n", dllPath.c_str());
169 return NI_ERROR_ALREADY_EXIST;
172 std::string absDllPath = absolutePath(dllPath);
173 std::string absNiPath = getNiFilePath(dllPath);
174 if (absNiPath.empty()) {
175 fprintf(stderr, "Fail to get ni file name\n");
176 return NI_ERROR_UNKNOWN;
180 absNiPath = getAppNIPath(absNiPath);
185 return NI_ERROR_UNKNOWN;
189 waitpid(pid, &status, 0);
190 if (WIFEXITED(status)) {
191 // Do not use niExist() function to check whether ni file created or not.
192 // niEixst() return false for System.Private.Corelib.dll
193 if (isFileExist(absNiPath)) {
194 updateNiFileInfo(absDllPath, absNiPath);
195 return NI_ERROR_NONE;
197 fprintf(stderr, "Fail to create native image for %s\n", dllPath.c_str());
198 return NI_ERROR_NO_SUCH_FILE;
202 std::string jitPath = getRuntimeDir() + "/libclrjit.so";
203 std::vector<const char*> argv = {
206 "/Trusted_Platform_Assemblies", __tpa.c_str(),
207 "/JITPath", jitPath.c_str()
211 argv.push_back("/FragileNonVersionable");
214 argv.push_back("/App_Paths");
215 std::string absAppPath;
216 if (!appPath.empty()) {
217 absAppPath = appPath;
219 absAppPath = baseName(absDllPath);
221 argv.push_back(absAppPath.c_str());
223 argv.push_back("/out");
224 argv.push_back(absNiPath.c_str());
226 argv.push_back(absDllPath.c_str());
227 argv.push_back(nullptr);
229 fprintf(stderr, "+ %s (%s)\n", absDllPath.c_str(), enableR2R ? "R2R" : "FNV");
231 execv(__CROSSGEN_PATH, const_cast<char* const*>(argv.data()));
235 return NI_ERROR_NONE;
238 static ni_error_e getRootPath(std::string pkgId, std::string& rootPath)
245 if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
246 _ERR("Failed to get UID");
247 return NI_ERROR_UNKNOWN;
250 pkgmgrinfo_pkginfo_h handle;
252 ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
253 if (ret != PMINFO_R_OK)
254 return NI_ERROR_UNKNOWN;
256 ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
257 if (ret != PMINFO_R_OK)
258 return NI_ERROR_UNKNOWN;
261 ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
262 if (ret != PMINFO_R_OK) {
263 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
264 return NI_ERROR_UNKNOWN;
267 pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
269 return NI_ERROR_NONE;
272 // callback function of "pkgmgrinfo_appinfo_metadata_filter_foreach"
273 static int appAotCb(pkgmgrinfo_appinfo_h handle, void *userData)
277 bool* enableR2R = (bool*)userData;
279 ret = pkgmgrinfo_appinfo_get_pkgid(handle, &pkgId);
280 if (ret != PMINFO_R_OK) {
281 fprintf(stderr, "Failed to get pkgid\n");
285 if (removeNiUnderPkgRoot(pkgId) != 0) {
286 fprintf(stderr, "Failed to remove previous dlls from [%s]\n", pkgId);
290 // Regenerate ni files with R2R mode forcibiliy. (there is no way to now which option is used)
291 if (createNiUnderPkgRoot(pkgId, *enableR2R) != 0) {
292 fprintf(stderr, "Failed to get root path from [%s]\n", pkgId);
295 fprintf(stderr, "Complete make application to native image\n");
301 static void createCoreLibNI(bool enableR2R)
303 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
304 std::string niCoreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.ni.dll");
305 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
307 if (!isFileExist(coreLibBackup)) {
308 if (!crossgen(coreLib, std::string(), enableR2R)) {
309 if (rename(coreLib.c_str(), coreLibBackup.c_str())) {
310 fprintf(stderr, "Failed to rename System.Private.CoreLib.dll\n");
312 if (rename(niCoreLib.c_str(), coreLib.c_str())) {
313 fprintf(stderr, "Failed to rename System.Private.CoreLib.ni.dll\n");
316 fprintf(stderr, "Failed to create native image for %s\n", coreLib.c_str());
321 ni_error_e initNICommon(NiCommonOption* option)
324 // get interval value
325 const char* intervalFile = "/usr/share/dotnet.tizen/lib/crossgen_interval.txt";
326 std::ifstream inFile(intervalFile);
328 fprintf(stderr, "crossgen_interval.txt is found\n");
329 inFile >> __interval;
332 if (initializePluginManager("normal")) {
333 fprintf(stderr, "Fail to initialize plugin manager\n");
334 return NI_ERROR_UNKNOWN;
336 if (initializePathManager(option->runtimeDir, option->tizenFXDir, option->extraDirs)) {
337 fprintf(stderr, "Fail to initialize path manager\n");
338 return NI_ERROR_UNKNOWN;
343 return NI_ERROR_NONE;
345 fprintf(stderr, "crossgen supports arm architecture only. skip ni file generation\n");
346 return NI_ERROR_NOT_SUPPORTED;
350 void finalizeNICommon()
354 finalizePluginManager();
355 finalizePathManager();
361 void createNiPlatform(bool enableR2R)
363 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
364 createNiUnderDirs(platformDirs, 2, enableR2R);
367 ni_error_e createNiDll(const std::string& dllPath, bool enableR2R)
369 createCoreLibNI(enableR2R);
370 // System.Private.CoreLib.dll is generated in the createCoreLibNI function.
371 // Skip if input dll is System.Private.CoreLib.dll
372 if (dllPath.find("System.Private.CoreLib.dll") != std::string::npos) {
373 return NI_ERROR_NONE;
376 return crossgen(dllPath, std::string(), enableR2R);
379 void createNiUnderDirs(const std::string rootPaths[], int count, bool enableR2R, bool isAppNI)
381 createCoreLibNI(enableR2R);
383 std::string appPaths;
384 for (int i = 0; i < count; i++) {
385 appPaths += rootPaths[i];
389 if (appPaths.back() == ':')
392 auto convert = [&appPaths, enableR2R, isAppNI](const std::string& path, const char* name) {
393 if (!crossgen(path, appPaths.c_str(), enableR2R, isAppNI)) {
398 for (int i = 0; i < count; i++) {
399 scanFilesInDir(rootPaths[i], convert, 1);
403 ni_error_e createNiUnderPkgRoot(const std::string& pkgName, bool enableR2R)
406 if (getRootPath(pkgName, pkgRoot) != NI_ERROR_NONE) {
407 fprintf(stderr, "Failed to get root path from [%s]\n", pkgName.c_str());
408 return NI_ERROR_INVALID_PACKAGE;
411 std::string binDir = concatPath(pkgRoot, "bin");
412 std::string libDir = concatPath(pkgRoot, "lib");
413 std::string paths[] = {binDir, libDir};
415 createNiUnderDirs(paths, 2, enableR2R, true);
417 return NI_ERROR_NONE;
420 ni_error_e createNiDllUnderPkgRoot(const std::string& pkgName, const std::string& dllPath, bool enableR2R)
423 if (getRootPath(pkgName, pkgRoot) < 0) {
424 fprintf(stderr, "Failed to get root path from [%s]\n", pkgName.c_str());
425 return NI_ERROR_INVALID_PACKAGE;
428 std::string binDir = concatPath(pkgRoot, "bin");
429 std::string libDir = concatPath(pkgRoot, "lib");
430 std::string paths = binDir + ":" + libDir;
432 return crossgen(dllPath, paths, enableR2R, true);
435 void removeNiPlatform()
437 std::string coreLib = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll");
438 std::string coreLibBackup = concatPath(getRuntimeDir(), "System.Private.CoreLib.dll.Backup");
440 if (!isFileExist(coreLibBackup)) {
444 if (remove(coreLib.c_str())) {
445 fprintf(stderr, "Failed to remove System.Private.CoreLib native image file\n");
448 if (rename(coreLibBackup.c_str(), coreLib.c_str())) {
449 fprintf(stderr, "Failed to rename System.Private.CoreLib.Backup to origin\n");
452 const std::string platformDirs[] = {getRuntimeDir(), getTizenFXDir()};
454 removeNiUnderDirs(platformDirs, 2);
457 void removeNiUnderDirs(const std::string rootPaths[], int count)
459 auto convert = [](const std::string& path, std::string name) {
461 if (isNativeImage(path)) {
462 if (remove(path.c_str())) {
463 fprintf(stderr, "Failed to remove %s\n", path.c_str());
468 for (int i = 0; i < count; i++)
469 scanFilesInDir(rootPaths[i], convert, -1);
472 ni_error_e removeNiUnderPkgRoot(const std::string& pkgName)
475 if (getRootPath(pkgName, pkgRoot) < 0) {
476 fprintf(stderr, "Failed to get root path from [%s]\n", pkgName.c_str());
477 return NI_ERROR_INVALID_PACKAGE;
480 std::string binDir = concatPath(pkgRoot, "bin");
481 std::string libDir = concatPath(pkgRoot, "lib");
482 std::string paths[] = {binDir, libDir};
484 removeNiUnderDirs(paths, 2);
486 std::string binNIDir = binDir + APP_NI_SUB_DIR;
487 if (isFileExist(binNIDir)) {
488 if (rmdir(binNIDir.c_str()) != 0) {
489 fprintf(stderr, "Failed to remove app ni dir [%s]\n", binNIDir.c_str());
493 std::string libNIDir = libDir + APP_NI_SUB_DIR;
494 if (isFileExist(libNIDir)) {
495 if (rmdir(libNIDir.c_str()) != 0) {
496 fprintf(stderr, "Failed to remove app ni dir [%s]\n", libNIDir.c_str());
500 return NI_ERROR_NONE;
503 ni_error_e regenerateAppNI(bool enableR2R)
506 pkgmgrinfo_appinfo_metadata_filter_h handle;
508 ret = pkgmgrinfo_appinfo_metadata_filter_create(&handle);
509 if (ret != PMINFO_R_OK)
510 return NI_ERROR_UNKNOWN;
512 ret = pkgmgrinfo_appinfo_metadata_filter_add(handle, AOT_METADATA_KEY, AOT_METADATA_VALUE);
513 if (ret != PMINFO_R_OK) {
514 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
515 return NI_ERROR_UNKNOWN;
518 ret = pkgmgrinfo_appinfo_metadata_filter_foreach(handle, appAotCb, &enableR2R);
519 if (ret != PMINFO_R_OK) {
520 fprintf(stderr, "Failed pkgmgrinfo_appinfo_metadata_filter_foreach\n");
521 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
522 return NI_ERROR_UNKNOWN;
525 fprintf(stderr, "Success pkgmgrinfo_appinfo_metadata_filter_foreach\n");
527 pkgmgrinfo_appinfo_metadata_filter_destroy(handle);
528 return NI_ERROR_NONE;