Exclude exe extension from target assembly
[platform/core/dotnet/launcher.git] / NativeLauncher / util / utils.cc
index 0dd7777..963cb7f 100644 (file)
@@ -24,6 +24,7 @@
 #include <sys/smack.h>
 #include <sys/prctl.h>
 #include <openssl/sha.h>
+#include <mntent.h>
 
 #include <cstdlib>
 #include <cstring>
@@ -51,9 +52,7 @@ static bool iCompare(const std::string& a, int aOffset, const std::string& b, in
 
 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);
+       return iCompare(fileName, fileName.size()-4, ".dll", 0, 4) && !isNativeImage(fileName);
 }
 
 bool isNativeImage(const std::string& fileName)
@@ -236,6 +235,35 @@ std::string getMetadataValue(const std::string& pkgId, const std::string& key)
        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);
@@ -260,23 +288,33 @@ std::string replaceAll(const std::string& str, const std::string& pattern, const
        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)
@@ -379,29 +417,32 @@ void copySmackAndOwnership(const std::string& fromPath, const std::string& toPat
                // 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");
                }
        }
 }
@@ -452,7 +493,12 @@ static bool copyOwnershipAndPermissions(const bf::path& path, const bf::path& pa
                _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;
@@ -522,11 +568,11 @@ bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags)
        }
 
        // 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 (bf::is_symlink(bf::symlink_status(current))) {
                                if ((flags & (FS_MERGE_SKIP | FS_MERGE_OVERWRITE)) && exist(target)) {
                                        continue;
                                }
@@ -564,11 +610,12 @@ bool copyDir(const bf::path& path1, const bf::path& path2, FSFlag flags)
                                        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;
 }
 
@@ -733,8 +780,8 @@ int pkgmgrGetAppInfo(const std::string& appId, pkgmgrinfo_appinfo_h* handle)
 }
 
 int pkgmgrMDFilterForeach(pkgmgrinfo_appinfo_metadata_filter_h handle,
-                                        pkgmgrinfo_app_list_cb app_cb,
-                                        void *user_data)
+                                                       pkgmgrinfo_app_list_cb app_cb,
+                                                       void *user_data)
 {
        uid_t uid = 0;
        int ret = 0;
@@ -754,3 +801,53 @@ int pkgmgrMDFilterForeach(pkgmgrinfo_appinfo_metadata_filter_h handle,
        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;
+}