From 0401832d402601cf7d63ce51644a4ba379396cf8 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Mon, 28 Jun 2021 17:15:07 +0900 Subject: [PATCH] Fix aul_svc_get_appid_by_alias_appid_for_uid() function If the amd ready file is not existed, the caller process accesses to the database directly to get the appid using the alias appid. Change-Id: I964e3b0b3d82ac699214a1b963b109e081bce619 Signed-off-by: Hwankyu Jhun --- src/aul_svc.cc | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/src/aul_svc.cc b/src/aul_svc.cc index 4f98330..252d029 100644 --- a/src/aul_svc.cc +++ b/src/aul_svc.cc @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -26,6 +27,7 @@ #include #include +#include #include #include #include @@ -60,6 +62,11 @@ namespace { +constexpr const char kPathAmdReady[] = "/run/.amd_ready"; +constexpr const char kPathLibAulServer[] = "/usr/lib/libaul-server.so.0"; +constexpr const char kAulServiceForeachUsrAliasInfo[] = + "aul_service_foreach_usr_alias_info"; + class CbInfo { public: CbInfo(int request_code, aul_svc_res_fn res_fn, @@ -76,6 +83,16 @@ class CbInfo { void* user_data_; }; +class AliasInfo { + public: + AliasInfo(std::string alias_appid) + : alias_appid_(std::move(alias_appid)) { + } + + std::string alias_appid_; + std::string appid_; +}; + int SetBundle(bundle* b, const char* key, const char* value) { if (bundle_get_type(b, key) != BUNDLE_TYPE_NONE) { if (bundle_del(b, key) != BUNDLE_ERROR_NONE) @@ -316,6 +333,68 @@ int SendAndReceive(int cmd, uid_t uid, bundle* request, bundle** response) { return AUL_SVC_RET_OK; } +std::atomic amd_ready { false }; +bool IsAmdReady() { + if (amd_ready) + return amd_ready; + + if (access(kPathAmdReady, F_OK) == 0) { + amd_ready.exchange(true); + return amd_ready; + } + + return false; +} + +using AulServiceAliasInfoCb = + bool (*)(const char*, const char*, void*); +using AulServiceForeachUsrAliasInfoFunc = + int (*)(AulServiceAliasInfoCb, uid_t, void*); + +int GetAppIdByAliasAppIdFromDB(const char* alias_appid, char** app_id, + uid_t uid) { + void* handle = dlopen(kPathLibAulServer, RTLD_LAZY | RTLD_GLOBAL); + if (handle == nullptr) { + _E("dlopen() is failed. path(%s), error(%s)", kPathLibAulServer, dlerror()); + return AUL_SVC_RET_ERROR; + } + + std::unique_ptr handle_auto(handle, dlclose); + auto* func = reinterpret_cast( + dlsym(handle, kAulServiceForeachUsrAliasInfo)); + if (func == nullptr) { + _E("dlsym() is failed. error(%s)", dlerror()); + return AUL_SVC_RET_ERROR; + } + + AliasInfo info(alias_appid); + int ret = func( + [](const char* alias_appid, const char* appid, void* user_data) -> bool { + auto* info = static_cast(user_data); + if (info->alias_appid_ == alias_appid) { + info->appid_ = appid; + return false; + } + + return true; + }, uid, &info); + if (ret != 0) { + _E("%s() is failed. error(%d)", kAulServiceForeachUsrAliasInfo, ret); + return AUL_SVC_RET_ERROR; + } + + if (info.appid_.empty()) + return AUL_SVC_RET_ERROR; + + *app_id = strdup(info.appid_.c_str()); + if (*app_id == nullptr) { + _E("strdup() is failed"); + return AUL_SVC_RET_ENOMEM; + } + + return AUL_SVC_RET_OK; +} + } // namespace extern "C" API int aul_svc_set_operation(bundle* b, const char* operation) { @@ -684,6 +763,9 @@ extern "C" API int aul_svc_get_appid_by_alias_appid_for_uid( return AUL_SVC_RET_EINVAL; } + if (!IsAmdReady()) + return GetAppIdByAliasAppIdFromDB(alias_appid, appid, uid); + bundle* response; tizen_base::Bundle request; request.Add(AUL_K_ALIAS_APPID, alias_appid); -- 2.7.4