#include <unistd.h>
#include <limits.h>
#include <strings.h>
-#include <pkgmgr-info.h>
#include <pkgmgr_installer_info.h>
#include <sys/smack.h>
#include <sys/prctl.h>
+#include <openssl/evp.h>
+#include <openssl/crypto.h>
+
+#include <mntent.h>
+
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <fstream>
#include <sstream>
#include <map>
+#include <iomanip>
#include "log.h"
#include "utils.h"
#include "path_manager.h"
+#include "r2r_checker.h"
static bool iCompare(const std::string& a, int aOffset, const std::string& b, int bOffset, int length)
{
bool isManagedAssembly(const std::string& fileName)
{
- return (iCompare(fileName, fileName.size()-4, ".dll", 0, 4) ||
- iCompare(fileName, fileName.size()-4, ".exe", 0, 4)) &&
- !isNativeImage(fileName);
-}
-
-bool isNativeImage(const std::string& fileName)
-{
- return iCompare(fileName, fileName.size()-7, ".ni", 0, 3);
+ return iCompare(fileName, fileName.size()-4, ".dll", 0, 4) && !isR2RImage(fileName);
}
std::string concatPath(const std::string& path1, const std::string& path2)
std::string token;
while (std::getline(ss, token, ':')) {
- out.push_back(token);
+ if (token != "") {
+ out.push_back(token);
+ }
}
}
{
int ret = 0;
char *path = 0;
- uid_t uid = 0;
std::string rootPath;
- if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
- _ERR("Failed to get UID");
+ pkgmgrinfo_pkginfo_h pkg_handle;
+ ret = pkgmgrGetPkgInfo(pkgId, &pkg_handle);
+ if (ret != 0) {
return rootPath;
}
- pkgmgrinfo_pkginfo_h handle;
- if (uid == 0) {
- ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &handle);
- if (ret != PMINFO_R_OK) {
- return rootPath;
- }
- } else {
- ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, &handle);
- if (ret != PMINFO_R_OK) {
- return rootPath;
- }
- }
-
- ret = pkgmgrinfo_pkginfo_get_root_path(handle, &path);
+ ret = pkgmgrinfo_pkginfo_get_root_path(pkg_handle, &path);
if (ret != PMINFO_R_OK) {
- pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+ pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
return rootPath;
}
rootPath = path;
- pkgmgrinfo_pkginfo_destroy_pkginfo(handle);
+ pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
return rootPath;
}
std::string execName;
pkgmgrinfo_pkginfo_h pkg_handle;
- int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle);
- if (ret != PMINFO_R_OK) {
+ int ret = pkgmgrGetPkgInfo(pkgId, &pkg_handle);
+ if (ret != 0) {
return execName;
}
+
ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
if (ret != PMINFO_R_OK) {
pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
}
pkgmgrinfo_appinfo_h app_handle;
- ret = pkgmgrinfo_appinfo_get_appinfo(appId, &app_handle);
- if (ret != PMINFO_R_OK) {
+ ret = pkgmgrGetAppInfo(appId, &app_handle);
+ if (ret != 0) {
pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
return execName;
}
+
ret = pkgmgrinfo_appinfo_get_exec(app_handle, &exec);
if (ret != PMINFO_R_OK) {
pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
std::string appType;
pkgmgrinfo_pkginfo_h pkg_handle;
- int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle);
- if (ret != PMINFO_R_OK) {
+ int ret = pkgmgrGetPkgInfo(pkgId, &pkg_handle);
+ if (ret != 0) {
return appType;
}
+
ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
if (ret != PMINFO_R_OK) {
pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
}
pkgmgrinfo_appinfo_h app_handle;
- ret = pkgmgrinfo_appinfo_get_appinfo(appId, &app_handle);
- if (ret != PMINFO_R_OK) {
+ ret = pkgmgrGetAppInfo(appId, &app_handle);
+ if (ret != 0) {
pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
return appType;
}
+
ret = pkgmgrinfo_appinfo_get_apptype(app_handle, &type);
if (ret != PMINFO_R_OK) {
pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
std::string metadataValue;
pkgmgrinfo_pkginfo_h pkg_handle;
- int ret = pkgmgrinfo_pkginfo_get_pkginfo(pkgId.c_str(), &pkg_handle);
- if (ret != PMINFO_R_OK) {
+ int ret = pkgmgrGetPkgInfo(pkgId, &pkg_handle);
+ if (ret != 0) {
return metadataValue;
}
+
ret = pkgmgrinfo_pkginfo_get_mainappid(pkg_handle, &appId);
if (ret != PMINFO_R_OK) {
pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
}
pkgmgrinfo_appinfo_h app_handle;
- ret = pkgmgrinfo_appinfo_get_appinfo(appId, &app_handle);
- if (ret != PMINFO_R_OK) {
+ ret = pkgmgrGetAppInfo(appId, &app_handle);
+ if (ret != 0) {
pkgmgrinfo_pkginfo_destroy_pkginfo(pkg_handle);
return metadataValue;
}
+
ret = pkgmgrinfo_appinfo_get_metadata_value(app_handle, key.c_str(), &value);
if (ret != PMINFO_R_OK) {
pkgmgrinfo_appinfo_destroy_appinfo(app_handle);
return metadataValue;
}
+bool isReadOnlyArea(const std::string& path)
+{
+ FILE *f = NULL;
+ struct mntent *m = NULL;
+
+ // "/opt/usr" is mounted to "RW" only
+ if (path.find("/opt/usr") != std::string::npos) {
+ return false;
+ }
+
+ // check whether "/" is mounted to RO or not
+ f = setmntent("/proc/mounts", "r");
+ if (!f) {
+ // return true for fail case to generate NI files under RW area.
+ return true;
+ }
+
+ while((m = getmntent(f))) {
+ if (m->mnt_dir != NULL && strcmp(m->mnt_dir, "/") == 0 &&
+ m->mnt_opts != NULL && strstr(m->mnt_opts, "ro,") != NULL) {
+ endmntent(f);
+ return true;
+ }
+ }
+ endmntent(f);
+ return false;
+
+}
+
std::string getBaseName(const std::string& path)
{
auto pos = path.find_last_of(PATH_SEPARATOR);
return result;
}
+std::string changeExtension(const std::string& path, const std::string& from, const std::string& to)
+{
+ return path.substr(0, path.rfind(from)) + to;
+}
+
bool isFile(const std::string& path)
{
struct stat sb;
return lstat(path.c_str(), &sb) == 0;
}
-bool isDirectory(const std::string& path)
+bool isSymlinkFile(const std::string& path)
{
struct stat sb;
- if (stat(path.c_str(), &sb) != 0) {
+ if (lstat(path.c_str(), &sb) != 0) {
return false;
}
- if (sb.st_mode & S_IFDIR) {
- return true;
- } else {
+ return (sb.st_mode & S_IFMT) == S_IFLNK;
+}
+
+bool isDirectory(const std::string& path)
+{
+ struct stat sb;
+ if (stat(path.c_str(), &sb) != 0) {
return false;
}
+ return (sb.st_mode & S_IFMT) == S_IFDIR;
}
std::string getAssemblyNameFromPath(const std::string& path)
std::unordered_map<std::string, std::string> assemPaths;
auto reader = [&assems, &assemPaths](const std::string& path, const std::string& filename) {
- if (isManagedAssembly(filename) || isNativeImage(filename)) {
+ if (isManagedAssembly(filename) || isR2RImage(filename)) {
std::string assem = getAssemblyNameFromPath(filename);
if (assemPaths.count(assem) == 0) {
assems.push_back(assem);
assemPaths[assem] = path;
- } else if (isManagedAssembly(assemPaths[assem]) && isNativeImage(filename)) {
+ } else if (isManagedAssembly(assemPaths[assem]) && isR2RImage(filename)) {
// Update only if a native image is found in the same directory.
// For example, if we have two directories = { X, Y } where X contains A.dll and
// Y contains both A.dll and A.ni.dll, always A.dll in X will be used.
// change smack label for symbolic link.
if (smack_lgetlabel(fromPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
if (smack_lsetlabel(toPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
- fprintf(stderr, "Fail to set smack label\n");
+ _SERR("Fail to set smack label");
}
free(label);
}
- // change owner and groups for symbolic link.
+ // change owner and groupsfor symbolic link.
+ // change mode is skipped for symlink because permission of symlink file is meaningless.
if (!lstat(fromPath.c_str(), &info)) {
if (lchown(toPath.c_str(), info.st_uid, info.st_gid) == -1)
- fprintf(stderr, "Failed to change owner and group name\n");
+ _SERR("Failed to change owner and group name");
}
} else {
// change smack label
if (smack_getlabel(fromPath.c_str(), &label, SMACK_LABEL_ACCESS) == 0) {
if (smack_setlabel(toPath.c_str(), label, SMACK_LABEL_ACCESS) < 0) {
- fprintf(stderr, "Fail to set smack label\n");
+ _SERR("Fail to set smack label");
}
free(label);
}
- // change owner and groups for generated ni file.
+ // change owner, groups and mode for generated ni file.
if (!stat(fromPath.c_str(), &info)) {
if (chown(toPath.c_str(), info.st_uid, info.st_gid) == -1)
- fprintf(stderr, "Failed to change owner and group name\n");
+ _SERR("Failed to change owner and group name");
+ if (chmod(toPath.c_str(), info.st_mode) == -1)
+ _SERR("Failed to change mode");
}
}
}
static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& path2)
{
- if (!bf::exists(path)) {
+ if (!exist(path)) {
_ERR("Failed to copy ownership and permissions from %s to %s", path.c_str(), path2.c_str());
return false;
}
- bf::perms permissions = bf::status(path).permissions();
+ bs::error_code error;
+ bf::perms permissions = bf::status(path, error).permissions();
+ if (error) {
+ _ERR("Failed to copy ownership and permissions : %s", error.message().c_str());
+ return false;
+ }
struct stat stats;
if (stat(path.c_str(), &stats) != 0) {
return false;
return true;
}
+bool exist(const bf::path& path)
+{
+ bs::error_code error;
+ int ret = bf::exists(path, error);
+ if (error) {
+ if ((error.value() != bs::errc::success) && (error.value() != bs::errc::no_such_file_or_directory)) {
+ _ERR("Failed to check %s exists : %s", path.c_str(), error.message().c_str());
+ }
+ }
+ return ret;
+}
+
bool createDir(const bf::path& path)
{
- if (bf::exists(path)) {
+ if (exist(path)) {
return true;
}
bs::error_code error;
{
try {
// Check whether the function call is valid
- if (!bf::exists(path1) || !bf::is_directory(path1)) {
+ if (!exist(path1) || !bf::is_directory(path1)) {
_ERR("Source directory %s does not exist or is not a directory", path1.c_str());
return false;
}
- if (!bf::exists(path2)) {
+ if (!exist(path2)) {
// Create the destination directory
if (!createDir(path2)) {
_ERR("Unable to create destination directory %s", path2.c_str());
}
// Iterate through the source directory
- for (bf::directory_iterator file(path1); file != bf::directory_iterator(); ++file) {
- try {
+ try {
+ for (bf::directory_iterator file(path1); file != bf::directory_iterator(); ++file) {
bf::path current(file->path());
bf::path target = path2 / current.filename();
- if (bf::is_symlink(symlink_status(current))) {
- if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) && bf::exists(target)) {
+ if (bf::is_symlink(bf::symlink_status(current))) {
+ if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) && exist(target)) {
continue;
}
bs::error_code error;
return false;
}
} else {
- if ((flags & FS_MERGE_SKIP) && bf::exists(target)) {
+ if ((flags & FS_MERGE_SKIP) && exist(target)) {
continue;
}
bf::path destination = target;
bf::rename(destination, target);
}
}
- } catch (const bf::filesystem_error& error) {
- _ERR("Failed to copy directory: %s", error.what());
- return false;
}
+ } catch (const bf::filesystem_error& error) {
+ _ERR("Failed to copy directory: %s", error.what());
+ return false;
}
+
return true;
}
bool copyFile(const bf::path& path1, const bf::path& path2)
{
bs::error_code error;
- if (!bf::exists(path1)) {
+ if (!exist(path1)) {
return false;
}
bf::copy_file(path1, path2, bf::copy_option::overwrite_if_exists, error);
bool moveFile(const bf::path& path1, const bf::path& path2)
{
- if (!bf::exists(path1) || bf::exists(path2)) {
+ if (!exist(path1) || exist(path2)) {
return false;
}
bs::error_code error;
bool removeFile(const bf::path& path)
{
- if (!bf::exists(path)) {
+ if (!exist(path)) {
return true;
}
bs::error_code error;
bool removeAll(const bf::path& path)
{
- if (!exists(path)) {
+ if (!exist(path)) {
return true;
}
bs::error_code error;
size_t index = ret.find_last_of(PATH_SEPARATOR);
return index == std::string::npos ? ret : ret.substr(index + 1);
}
+
+std::string SHA256(const std::string& path)
+{
+ int bytesRead = 0;
+ const int bufSize = 32768;
+
+ unsigned int digest_len = 0;
+ unsigned char* digest = NULL;
+
+ std::stringstream ss;
+ EVP_MD_CTX *mdctx = NULL;
+ std::string output = "";
+
+ FILE *file = fopen(path.c_str(), "rb");
+ if (!file) {
+ return output;
+ }
+
+ char *buffer = (char*)malloc(bufSize);
+ if (!buffer) {
+ goto cleanup4;
+ }
+
+ mdctx = EVP_MD_CTX_new();
+ if (mdctx == NULL) {
+ _ERR("Message Digest Context creation NULL");
+ goto cleanup3;
+ }
+
+ digest = (unsigned char*)OPENSSL_malloc(EVP_MD_size(EVP_sha256()));
+ if (!digest) {
+ _ERR("Memory Allocation for SHA256 failed");
+ goto cleanup2;
+ }
+
+ if (!EVP_DigestInit_ex(mdctx, EVP_sha256(), NULL)) {
+ _ERR("Message Digest init failed");
+ goto cleanup1;
+ }
+
+ while ((bytesRead = fread(buffer, 1, bufSize, file))) {
+ if (!EVP_DigestUpdate(mdctx, buffer, bytesRead)) {
+ _ERR("Message Digest update failed");
+ goto cleanup1;
+ }
+ }
+
+ if (!EVP_DigestFinal_ex(mdctx, digest, &digest_len)) {
+ _ERR("Message Digest Finalization falied");
+ goto cleanup1;
+ }
+
+ for (unsigned int i = 0; i < digest_len; i++) {
+ ss << std::hex << std::setw(2) << std::setfill('0') << (int)digest[i];
+ }
+ output = ss.str();
+
+cleanup1:
+ EVP_MD_CTX_free(mdctx);
+cleanup2:
+ OPENSSL_free(digest);
+cleanup3:
+ free(buffer);
+cleanup4:
+ fclose(file);
+
+ return output;
+}
+
+int pkgmgrGetPkgInfo(const std::string& pkgId, pkgmgrinfo_pkginfo_h* handle)
+{
+ uid_t uid = 0;
+ int ret = 0;
+
+ if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
+ _ERR("Failed to get UID");
+ return -1;
+ }
+
+ ret = pkgmgrinfo_pkginfo_get_usr_pkginfo(pkgId.c_str(), uid, handle);
+ if (ret != PMINFO_R_OK) {
+ _ERR("Failed to get pkginfo (%d)", ret);
+ return -1;
+ }
+
+ return 0;
+}
+
+int pkgmgrGetAppInfo(const std::string& appId, pkgmgrinfo_appinfo_h* handle)
+{
+ uid_t uid = 0;
+ int ret = 0;
+
+ if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
+ _ERR("Failed to get UID");
+ return -1;
+ }
+
+ ret = pkgmgrinfo_appinfo_get_usr_appinfo(appId.c_str(), uid, handle);
+ if (ret != PMINFO_R_OK) {
+ _ERR("Failed to get appinfo (%d)", ret);
+ return -1;
+ }
+
+ return 0;
+}
+
+int pkgmgrMDFilterForeach(pkgmgrinfo_appinfo_metadata_filter_h handle,
+ pkgmgrinfo_app_list_cb app_cb,
+ void *user_data)
+{
+ uid_t uid = 0;
+ int ret = 0;
+
+ if (pkgmgr_installer_info_get_target_uid(&uid) < 0) {
+ _ERR("Failed to get UID");
+ return -1;
+ }
+
+
+ ret = pkgmgrinfo_appinfo_usr_metadata_filter_foreach(handle, app_cb, user_data, uid);
+ if (ret != PMINFO_R_OK) {
+ _ERR("Failed to execute the metadata filter query (%d)", ret);
+ return -1;
+ }
+
+ return 0;
+}
+
+void printHWClockLog(const char* format, ...)
+{
+ char buf[1024] = {0,};
+ va_list ap;
+
+ va_start(ap, format);
+ vsnprintf(buf, sizeof(buf), format, ap);
+ va_end(ap);
+
+ prctl(PR_TASK_PERF_USER_TRACE, buf, strlen(buf));
+}
+
+const char* getNCDBStartupHook()
+{
+ return "/home/owner/share/tmp/sdk_tools/netcoredbg/ncdbhook.dll";
+}
+
+bool isNCDBStartupHookProvided()
+{
+ char *env = nullptr;
+ env = getenv("DOTNET_STARTUP_HOOKS");
+ if (env == nullptr)
+ return false;
+
+ // Note, `DOTNET_STARTUP_HOOKS` env could provide list of dlls with ':' delimiter,
+ // for example: "/path1/name1.dll:/path2/name2.dll"
+ while (*env != '\0')
+ {
+ const char *ncdbCur = getNCDBStartupHook();
+ while (*ncdbCur != '\0' && *env != '\0' && *env != ':')
+ {
+ if (*ncdbCur != *env)
+ break;
+
+ ncdbCur++;
+ env++;
+
+ if (*ncdbCur == '\0' && (*env == '\0' || *env == ':'))
+ return true;
+ }
+ while (*env != '\0' && *env != ':')
+ {
+ env++;
+ }
+ if (*env == ':')
+ env++;
+ }
+
+ return false;
+}