From 111f7f5f3bf39ba2687141242824e055538ec324 Mon Sep 17 00:00:00 2001 From: ilho kim Date: Fri, 13 Oct 2023 15:02:38 +0900 Subject: [PATCH] Handle abnormally terminated installer process If there is an abnormally terminated installer process apply the pending package info written by the process to the cache Change-Id: Idc128d9dcddec3ba209996e2a09eea3ea687c0ce Signed-off-by: ilho kim --- src/server/database/db_handle_provider.cc | 35 ++++++++++++---------- src/server/database/db_handle_provider.hh | 8 ++--- src/server/database/pkg_set_db_handler.cc | 3 +- src/server/database/remove_cache_db_handler.cc | 6 ++-- src/server/database/remove_cache_db_handler.hh | 5 +++- .../request_handler/command_request_handler.cc | 2 +- src/server/worker_thread.cc | 9 ++++++ 7 files changed, 42 insertions(+), 26 deletions(-) diff --git a/src/server/database/db_handle_provider.cc b/src/server/database/db_handle_provider.cc index 626a88e..516f3b0 100644 --- a/src/server/database/db_handle_provider.cc +++ b/src/server/database/db_handle_provider.cc @@ -126,13 +126,12 @@ DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) { return *prov; } -bool DBHandleProvider::IsCrashedWriteRequest() { +std::vector DBHandleProvider::CrashedWriteRequestPIDs() { std::unique_lock u(pid_list_lock_); if (writer_pid_list_.empty()) - return false; + return {}; - bool ret = true; LOG(DEBUG) << "Check process count : " << writer_pid_list_.size(); std::vector remove_pids; for (pid_t pid : writer_pid_list_) { @@ -143,7 +142,6 @@ bool DBHandleProvider::IsCrashedWriteRequest() { LOG(ERROR) << "Process is crashed : " << pid; remove_pids.push_back(pid); } else { - ret = false; close(fd); } } @@ -151,7 +149,7 @@ bool DBHandleProvider::IsCrashedWriteRequest() { for (pid_t pid : remove_pids) writer_pid_list_.erase(pid); - return ret; + return remove_pids; } std::vector> DBHandleProvider::GetParserDBPath() { @@ -425,15 +423,17 @@ bool DBHandleProvider::ErasePID(pid_t pid) { return writer_pid_list_.erase(pid) == 1; } -void DBHandleProvider::RegisterPendingPackageInfo(package_x* info) { +void DBHandleProvider::RegisterPendingPackageInfo(package_x* info, pid_t pid) { if (!info || !info->package) return; - pending_pkg_.emplace(info->package); + InsertPID(pid); + + pending_pkg_[pid].emplace(info->package); } void DBHandleProvider::UpdatePendingPackageInfo(const tizen_base::Database& db, - pid_t pid, uid_t uid, const std::string& locale) { + const std::vector& pids, uid_t uid, const std::string& locale) { pkgmgrinfo_filter_x tmp_filter = { 0, }; pkgmgrinfo_node_x node = { .prop = E_PMINFO_PKGINFO_PROP_PACKAGE_ID @@ -441,15 +441,19 @@ void DBHandleProvider::UpdatePendingPackageInfo(const tizen_base::Database& db, tmp_filter.cache_flag = true; tmp_filter.list = g_slist_append(tmp_filter.list, (gpointer)&node); std::map> pkgs; - for (const auto& pkg : pending_pkg_) { - pkg_map_.erase(pkg); - for (auto& appid : pkg_app_map_[pkg]) { - app_map_.erase(appid); + for (const pid_t& pid : pids) { + for (const auto& pkg : pending_pkg_[pid]) { + pkg_map_.erase(pkg); + for (auto& appid : pkg_app_map_[pkg]) { + app_map_.erase(appid); + } + + pkg_app_map_.erase(pkg); + node.value = const_cast(pkg.c_str()); + internal::GetPkgInfo(db, &tmp_filter, uid_, locale, pkgs); } - pkg_app_map_.erase(pkg); - node.value = const_cast(pkg.c_str()); - internal::GetPkgInfo(db, &tmp_filter, uid_, locale, pkgs); + pending_pkg_.erase(pid); } for (auto& [key, val] : pkgs) @@ -469,7 +473,6 @@ void DBHandleProvider::UpdatePendingPackageInfo(const tizen_base::Database& db, } g_slist_free(tmp_filter.list); - pending_pkg_.clear(); } bool DBHandleProvider::UpdateCachePkg(const tizen_base::Database& db, uid_t uid, diff --git a/src/server/database/db_handle_provider.hh b/src/server/database/db_handle_provider.hh index e1a470b..e99e1c7 100644 --- a/src/server/database/db_handle_provider.hh +++ b/src/server/database/db_handle_provider.hh @@ -45,7 +45,7 @@ class EXPORT_API DBHandleProvider { public: ~DBHandleProvider() = default; static DBHandleProvider& GetInst(uid_t uid); - static bool IsCrashedWriteRequest(); + static std::vector CrashedWriteRequestPIDs(); static bool IsWriter(pid_t pid); std::vector> GetParserDBPath(); std::string GetCertDBPath(); @@ -58,9 +58,9 @@ class EXPORT_API DBHandleProvider { pid_t pid, pkgmgrinfo_filter_x* filter, const std::string& app); void TrimCache(); - void RegisterPendingPackageInfo(package_x* info); + void RegisterPendingPackageInfo(package_x* info, pid_t pid); void UpdatePendingPackageInfo(const tizen_base::Database& db, - pid_t pid, uid_t uid, const std::string& locale); + const std::vector& pids, uid_t uid, const std::string& locale); bool UpdateCachePkg(const tizen_base::Database& db, uid_t uid, const std::string& pkgid, const std::string& locale); bool UpdateCacheApp(const tizen_base::Database& db, uid_t uid, @@ -92,7 +92,7 @@ class EXPORT_API DBHandleProvider { std::unordered_map> pkg_map_; std::unordered_map> app_map_; std::unordered_map> pkg_app_map_; - std::unordered_set pending_pkg_; + std::unordered_map> pending_pkg_; }; } // namespace database diff --git a/src/server/database/pkg_set_db_handler.cc b/src/server/database/pkg_set_db_handler.cc index 445828d..90aa2ae 100644 --- a/src/server/database/pkg_set_db_handler.cc +++ b/src/server/database/pkg_set_db_handler.cc @@ -74,7 +74,8 @@ int PkgSetDBHandler::Execute() { auto lock = CacheFlag::GetWriterLock(); if (CacheFlag::GetStatus() == CacheFlag::Status::PREPARED) - DBHandleProvider::GetInst(uid_).RegisterPendingPackageInfo(package_); + DBHandleProvider::GetInst(uid_) + .RegisterPendingPackageInfo(package_, GetPID()); return ret; } diff --git a/src/server/database/remove_cache_db_handler.cc b/src/server/database/remove_cache_db_handler.cc index c37e410..ab3279a 100644 --- a/src/server/database/remove_cache_db_handler.cc +++ b/src/server/database/remove_cache_db_handler.cc @@ -22,8 +22,8 @@ namespace pkgmgr_server { namespace database { -RemoveCacheDBHandler::RemoveCacheDBHandler(uid_t uid, int pid) - : AbstractDBHandler(uid, pid), uid_(uid) {} +RemoveCacheDBHandler::RemoveCacheDBHandler(uid_t uid, std::vector pids) + : AbstractDBHandler(uid, 0), uid_(uid), pids_(std::move(pids)) {} int RemoveCacheDBHandler::Execute() { std::unique_lock u(lock_); @@ -43,7 +43,7 @@ int RemoveCacheDBHandler::Execute() { CacheFlag::SetStatus(CacheFlag::Status::PREPARING); for (const auto& [db, uid] : conn_list) { auto& provider = database::DBHandleProvider::GetInst(uid); - provider.UpdatePendingPackageInfo(db, GetPID(), uid_, GetLocale()); + provider.UpdatePendingPackageInfo(db, pids_, uid_, GetLocale()); } CacheFlag::SetStatus(CacheFlag::Status::PREPARED); diff --git a/src/server/database/remove_cache_db_handler.hh b/src/server/database/remove_cache_db_handler.hh index b33dcea..1019755 100644 --- a/src/server/database/remove_cache_db_handler.hh +++ b/src/server/database/remove_cache_db_handler.hh @@ -19,6 +19,8 @@ #include +#include + #include "abstract_db_handler.hh" namespace pkgmgr_server { @@ -30,11 +32,12 @@ namespace database { class EXPORT_API RemoveCacheDBHandler : public AbstractDBHandler { public: - RemoveCacheDBHandler(uid_t uid, int pid); + RemoveCacheDBHandler(uid_t uid, std::vector pids); int Execute() override; private: uid_t uid_; + std::vector pids_; }; } // namespace database diff --git a/src/server/request_handler/command_request_handler.cc b/src/server/request_handler/command_request_handler.cc index 458b3a8..1577349 100644 --- a/src/server/request_handler/command_request_handler.cc +++ b/src/server/request_handler/command_request_handler.cc @@ -42,7 +42,7 @@ bool CommandRequestHandler::HandleRequest(unsigned char* data, size_t size, } if (parcel->GetCmd() == CommandType::RemoveCache) { - database::RemoveCacheDBHandler db(parcel->GetUid(), GetPID()); + database::RemoveCacheDBHandler db(parcel->GetUid(), {GetPID()}); db.SetLocale(locale); int ret = db.Execute(); result_ = std::make_shared( diff --git a/src/server/worker_thread.cc b/src/server/worker_thread.cc index ab9b637..129ff1d 100644 --- a/src/server/worker_thread.cc +++ b/src/server/worker_thread.cc @@ -26,6 +26,7 @@ #include "cynara_checker.hh" #include "request_handler_factory.hh" #include "server/database/db_handle_provider.hh" +#include "server/database/remove_cache_db_handler.hh" #include "utils/logging.hh" #include "pkgmgrinfo_debug.h" @@ -181,6 +182,14 @@ gboolean WorkerThread::TrimMemory(void* data) { h->timer_ = 0; } + auto crashed_writer_pids = + database::DBHandleProvider::CrashedWriteRequestPIDs(); + if (!crashed_writer_pids.empty()) { + database::RemoveCacheDBHandler db(getuid(), std::move(crashed_writer_pids)); + db.SetLocale(h->locale_.GetObject()); + db.Execute(); + } + sqlite3_release_memory(-1); malloc_trim(0); return G_SOURCE_REMOVE; -- 2.7.4