print HW clock log (#385)
[platform/core/dotnet/launcher.git] / NativeLauncher / util / utils.cc
index bb1ea62..64f4428 100644 (file)
 #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/sha.h>
+#include <mntent.h>
 
 #include <cstdlib>
 #include <cstring>
@@ -101,34 +101,21 @@ std::string getRootPath(const std::string& pkgId)
 {
        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;
 }
@@ -140,10 +127,11 @@ std::string getExecName(const std::string& pkgId)
        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);
@@ -151,11 +139,12 @@ std::string getExecName(const std::string& pkgId)
        }
 
        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);
@@ -177,10 +166,11 @@ std::string getAppType(const std::string& pkgId)
        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);
@@ -188,11 +178,12 @@ std::string getAppType(const std::string& pkgId)
        }
 
        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);
@@ -214,10 +205,11 @@ std::string getMetadataValue(const std::string& pkgId, const std::string& key)
        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);
@@ -225,11 +217,12 @@ std::string getMetadataValue(const std::string& pkgId, const std::string& key)
        }
 
        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);
@@ -244,6 +237,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);
@@ -268,23 +290,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)
@@ -387,29 +419,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");
                }
        }
 }
@@ -460,7 +495,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;
@@ -530,11 +570,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;
                                }
@@ -572,11 +612,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;
 }
 
@@ -700,4 +741,77 @@ std::string SHA256(const std::string& path)
        free(buffer);
 
        return output;
-}
\ No newline at end of file
+}
+
+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));
+}
+