#ifndef CACHE_FLAG_HH_
#define CACHE_FLAG_HH_
-#include <mutex>
#include <shared_mutex>
namespace pkgmgr_server {
static bool SetPreparing() {
bool ret = false;
- std::unique_lock<std::mutex> u(status_lock_);
+ std::unique_lock<std::shared_mutex> u(status_lock_);
if (status_ == Status::UNPREPARED) {
status_ = Status::PREPARING;
ret = true;
}
static void SetStatus(Status status) {
- std::unique_lock<std::mutex> u(status_lock_);
+ std::unique_lock<std::shared_mutex> u(status_lock_);
status_ = status;
}
static Status GetStatus() {
- std::unique_lock<std::mutex> u(status_lock_);
+ std::shared_lock<std::shared_mutex> s(status_lock_);
return status_;
}
- static std::unique_lock<std::shared_timed_mutex> GetWriterLock() {
- return std::unique_lock<std::shared_timed_mutex>(lock_);
+ static std::unique_lock<std::shared_mutex> GetWriterLock() {
+ return std::unique_lock<std::shared_mutex>(lock_);
}
static void WriterUnLock() {
lock_.unlock();
}
- static std::shared_lock<std::shared_timed_mutex> GetReaderLock() {
- return std::shared_lock<std::shared_timed_mutex>(lock_, std::defer_lock);
+ static std::shared_lock<std::shared_mutex> GetReaderLock() {
+ return std::shared_lock<std::shared_mutex>(lock_, std::defer_lock);
}
static void ReaderUnLock() {
private:
static inline Status status_;
- static inline std::mutex status_lock_;
- static inline std::shared_timed_mutex lock_;
+ static inline std::shared_mutex status_lock_;
+ static inline std::shared_mutex lock_;
};
} // namespace pkgmgr_server
namespace pkgmgr_server {
namespace database {
-std::shared_timed_mutex AbstractDBHandler::lock_;
+std::shared_mutex AbstractDBHandler::lock_;
AbstractDBHandler::~AbstractDBHandler() {
for (auto& db_handle : db_handle_list_)
const std::string& GetLocale();
static uid_t GetDefaultUser();
- static std::shared_timed_mutex lock_;
+ static std::shared_mutex lock_;
private:
pkgmgr_common::DBType db_type_;
#include "pkgmgrinfo_internal.h"
#include "pkgmgrinfo_internal.hh"
+namespace {
+
+uid_t globaluser_uid = -1;
+
+uid_t GetGlobalUID() {
+ if (globaluser_uid == (uid_t)-1)
+ globaluser_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
+
+ return globaluser_uid;
+}
+
+} // namespace
+
namespace pkgmgr_server {
namespace database {
filter_ = filter;
}
-int AppInfoDBHandler::GetHandleFromDB(
- std::vector<std::pair<sqlite3*, uid_t>>& conn_list) {
+int AppInfoDBHandler::GetHandleFromDB() {
+ if (!Connect()) {
+ LOG(ERROR) << "Failed to connect database";
+ return PMINFO_R_ERROR;
+ }
+
+ std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
std::vector<std::shared_ptr<application_x>> list;
int ret = PMINFO_R_OK;
for (auto& conn : conn_list) {
return ret;
}
-int AppInfoDBHandler::GetHandleFromCache(
- std::vector<std::pair<sqlite3*, uid_t>>& conn_list) {
- bool is_write_op =
- GetOpType() == pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE;
- int ret = PMINFO_R_OK;
+void AppInfoDBHandler::GetApplicationFromCache(uid_t uid,
+ const std::string& application) {
+ std::vector<std::shared_ptr<application_x>> app_list;
+ app_list = DBHandleProvider::GetInst(uid).GetApplications(GetPID(),
+ filter_, application);
+
+ handle_list_.reserve(app_list.size() + handle_list_.size());
+ std::move(std::begin(app_list), std::end(app_list),
+ std::back_inserter(handle_list_));
+}
+
+int AppInfoDBHandler::GetHandleFromCache() {
std::string application;
for (auto* it = filter_->list; it != nullptr; it = g_slist_next(it)) {
}
}
- std::vector<std::shared_ptr<application_x>> app_list;
- for (auto& conn : conn_list) {
- app_list = DBHandleProvider::GetInst(conn.second)
- .GetApplications(
- GetPID(), is_write_op, filter_, application);
+ if (uid_ > REGULAR_USER)
+ GetApplicationFromCache(uid_, application);
- handle_list_.reserve(app_list.size() + handle_list_.size());
- std::move(std::begin(app_list), std::end(app_list),
- std::back_inserter(handle_list_));
- }
+ GetApplicationFromCache(GetGlobalUID(), application);
if (handle_list_.empty())
- ret = PMINFO_R_ENOENT;
+ return PMINFO_R_ENOENT;
- return ret;
+ return PMINFO_R_OK;
}
int AppInfoDBHandler::Execute() {
- std::shared_lock<std::shared_timed_mutex> s(lock_);
+ std::shared_lock<std::shared_mutex> s(lock_);
SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
- if (!Connect())
- return PMINFO_R_ERROR;
-
- std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
-
- bool is_writer = false;
- for (auto& conn : conn_list) {
- if (DBHandleProvider::GetInst(conn.second).IsWriter(GetPID()))
- is_writer = true;
- }
- if (is_writer)
- return GetHandleFromDB(conn_list);
+ if (DBHandleProvider::IsWriter(GetPID()))
+ return GetHandleFromDB();
if (uid_ <= GetDefaultUser() &&
CacheFlag::GetStatus() == CacheFlag::Status::PREPARED) {
auto cache_lock = CacheFlag::GetReaderLock();
if (cache_lock.try_lock() &&
CacheFlag::GetStatus() == CacheFlag::Status::PREPARED)
- return GetHandleFromCache(conn_list);
+ return GetHandleFromCache();
}
- return GetHandleFromDB(conn_list);
+ return GetHandleFromDB();
}
} // namespace database
virtual int Execute();
protected:
- int GetHandleFromDB(std::vector<std::pair<sqlite3*, uid_t>>& conn_list);
- int GetHandleFromCache(std::vector<std::pair<sqlite3*, uid_t>>& conn_list);
+ int GetHandleFromDB();
+ int GetHandleFromCache();
+ void GetApplicationFromCache(uid_t uid, const std::string& application);
pkgmgrinfo_filter_x* filter_;
std::vector<std::shared_ptr<application_x>> handle_list_;
uid_t uid_;
CacheDBHandler::~CacheDBHandler() {}
int CacheDBHandler::Execute() {
- std::shared_lock<std::shared_timed_mutex> s(lock_);
+ std::shared_lock<std::shared_mutex> s(lock_);
SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
}
int CertGetDBHandler::Execute() {
- std::shared_lock<std::shared_timed_mutex> s(lock_);
+ std::shared_lock<std::shared_mutex> s(lock_);
SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_CERTDB);
}
int CertSetDBHandler::Execute() {
- std::unique_lock<std::shared_timed_mutex> u(lock_);
+ std::unique_lock<std::shared_mutex> u(lock_);
SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE);
SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_CERTDB);
}
int CreateDBHandler::Execute() {
- std::unique_lock<std::shared_timed_mutex> u(lock_);
+ std::unique_lock<std::shared_mutex> u(lock_);
if (CreateParserDB() < 0) {
LOG(ERROR) << "Failed to create parser db for uid : " << GetUID();
std::string DBHandleProvider::cert_filedb_path_;
std::unordered_set<pid_t> DBHandleProvider::writer_pid_list_;
std::recursive_mutex DBHandleProvider::lock_;
-std::mutex DBHandleProvider::pid_list_lock_;
+std::shared_mutex DBHandleProvider::pid_list_lock_;
DBHandleProvider::DBHandleProvider(uid_t uid)
: uid_(uid),
}
DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
- static std::mutex singleton_lock_;
- std::unique_lock<std::mutex> u(singleton_lock_);
+ static std::shared_mutex singleton_lock_;
+ std::shared_lock<std::shared_mutex> s(singleton_lock_);
uid = ConvertUID(uid);
+ auto it = provider_map_.find(uid);
+ if (it != provider_map_.end())
+ return *(it->second);
+
+ s.unlock();
+ std::unique_lock<std::shared_mutex> u(singleton_lock_);
auto& prov = provider_map_[uid];
if (prov == nullptr)
prov.reset(new DBHandleProvider(uid));
}
bool DBHandleProvider::IsCrashedWriteRequest() {
- std::unique_lock<std::mutex> u(pid_list_lock_);
+ std::unique_lock<std::shared_mutex> u(pid_list_lock_);
if (writer_pid_list_.empty())
return false;
}
bool DBHandleProvider::IsMemoryDBActive(pid_t pid, bool write) {
- std::unique_lock<std::mutex> u(pid_list_lock_);
+ std::unique_lock<std::shared_mutex> u(pid_list_lock_);
if (!is_user_memdb_set_)
return false;
}
bool DBHandleProvider::IsWriter(pid_t pid) {
- std::unique_lock<std::mutex> u(pid_list_lock_);
+ std::unique_lock<std::shared_mutex> s(pid_list_lock_);
return writer_pid_list_.find(pid) != writer_pid_list_.end();
}
}
std::vector<std::shared_ptr<package_x>> DBHandleProvider::GetPackages(
- pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
+ pid_t pid, pkgmgrinfo_filter_x* filter,
const std::string& package) {
std::vector<std::shared_ptr<package_x>> ret;
auto map_it = pkg_map_.find(package);
}
std::vector<std::shared_ptr<application_x>> DBHandleProvider::GetApplications(
- pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
+ pid_t pid, pkgmgrinfo_filter_x* filter,
const std::string& app) {
/* make metadata filter map */
std::unordered_map<std::string, std::string> metadata_map;
}
void DBHandleProvider::InsertPID(pid_t pid) {
- std::unique_lock<std::mutex> u(pid_list_lock_);
+ std::unique_lock<std::shared_mutex> u(pid_list_lock_);
writer_pid_list_.insert(pid);
}
bool DBHandleProvider::ErasePID(pid_t pid) {
- std::unique_lock<std::mutex> u(pid_list_lock_);
+ std::unique_lock<std::shared_mutex> u(pid_list_lock_);
return writer_pid_list_.erase(pid) == 1;
}
#include <sys/types.h>
#include <memory>
-#include <mutex>
+#include <shared_mutex>
#include <string>
#include <unordered_map>
#include <unordered_set>
~DBHandleProvider() = default;
static DBHandleProvider& GetInst(uid_t uid);
static bool IsCrashedWriteRequest();
+ static bool IsWriter(pid_t pid);
std::vector<std::pair<std::string, uid_t>> GetParserDBPath(int pid,
bool write);
std::string GetCertDBPath(int pid, bool write);
int UpdateCache(sqlite3* db, pid_t pid, uid_t uid, bool write,
const std::string& locale);
std::vector<std::shared_ptr<package_x>> GetPackages(
- pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
+ pid_t pid, pkgmgrinfo_filter_x* filter,
const std::string& package);
std::vector<std::shared_ptr<application_x>> GetApplications(
- pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
+ pid_t pid, pkgmgrinfo_filter_x* filter,
const std::string& app);
void TrimCache();
- bool IsWriter(pid_t pid);
private:
explicit DBHandleProvider(uid_t uid);
static std::string cert_filedb_path_;
static std::unordered_set<pid_t> writer_pid_list_;
static std::recursive_mutex lock_;
- static std::mutex pid_list_lock_;
+ static std::shared_mutex pid_list_lock_;
uid_t uid_;
bool is_user_memdb_set_;
}
int DepInfoGetDBHandler::Execute() {
- std::shared_lock<std::shared_timed_mutex> s(lock_);
+ std::shared_lock<std::shared_mutex> s(lock_);
SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
return true;
}
+uid_t globaluser_uid = -1;
+
+uid_t GetGlobalUID() {
+ if (globaluser_uid == (uid_t)-1)
+ globaluser_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
+
+ return globaluser_uid;
+}
+
} // namespace
namespace pkgmgr_server {
filter_ = filter;
}
-int PkgGetDBHandler::GetHandleFromDB(
- std::vector<std::pair<sqlite3*, uid_t>>& conn_list) {
+int PkgGetDBHandler::GetHandleFromDB() {
+ if (!Connect()) {
+ LOG(ERROR) << "Failed to connect database";
+ return PMINFO_R_ERROR;
+ }
+
+ std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
GHashTable* list =
g_hash_table_new_full(g_str_hash, g_str_equal, nullptr, nullptr);
int ret = PMINFO_R_OK;
return ret;
}
-int PkgGetDBHandler::GetHandleFromCache(
- std::vector<std::pair<sqlite3*, uid_t>>& conn_list) {
- int ret = PMINFO_R_OK;
+void PkgGetDBHandler::GetPackageFromCache(uid_t uid,
+ const std::string& package) {
+ std::vector<std::shared_ptr<package_x>> pkg_list =
+ DBHandleProvider::GetInst(uid).GetPackages(GetPID(), filter_, package);
+
+ handle_list_.reserve(pkg_list.size() + handle_list_.size());
+ std::move(std::begin(pkg_list), std::end(pkg_list),
+ std::back_inserter(handle_list_));
+}
+
+int PkgGetDBHandler::GetHandleFromCache() {
std::string package;
for (auto* it = filter_->list; it != nullptr; it = g_slist_next(it)) {
}
}
- std::vector<std::shared_ptr<package_x>> pkg_list;
- for (auto& conn : conn_list) {
- pkg_list = DBHandleProvider::GetInst(conn.second)
- .GetPackages(GetPID(), false, filter_, package);
+ if (uid_ > REGULAR_USER)
+ GetPackageFromCache(uid_, package);
- handle_list_.reserve(pkg_list.size() + handle_list_.size());
- std::move(std::begin(pkg_list), std::end(pkg_list),
- std::back_inserter(handle_list_));
- }
+ GetPackageFromCache(GetGlobalUID(), package);
if (handle_list_.empty())
return PMINFO_R_ENOENT;
- return ret;
+ return PMINFO_R_OK;
}
int PkgGetDBHandler::Execute() {
- std::shared_lock<std::shared_timed_mutex> s(lock_);
+ std::shared_lock<std::shared_mutex> s(lock_);
SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
- if (!Connect()) {
- LOG(ERROR) << "Failed to connect database";
- return PMINFO_R_ERROR;
- }
-
- std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
- bool is_writer = false;
- for (auto& conn : conn_list) {
- if (DBHandleProvider::GetInst(conn.second).IsWriter(GetPID()))
- is_writer = true;
- }
- if (is_writer)
- return GetHandleFromDB(conn_list);
+ if (DBHandleProvider::IsWriter(GetPID()))
+ return GetHandleFromDB();
if (uid_ <= GetDefaultUser() &&
CacheFlag::GetStatus() == CacheFlag::Status::PREPARED) {
auto cache_lock = CacheFlag::GetReaderLock();
if (cache_lock.try_lock() &&
CacheFlag::GetStatus() == CacheFlag::Status::PREPARED)
- return GetHandleFromCache(conn_list);
+ return GetHandleFromCache();
}
- return GetHandleFromDB(conn_list);
+ return GetHandleFromDB();
}
} // namespace database
virtual int Execute();
protected:
- int GetHandleFromDB(std::vector<std::pair<sqlite3*, uid_t>>& conn_list);
- int GetHandleFromCache(std::vector<std::pair<sqlite3*, uid_t>>& conn_list);
+ int GetHandleFromDB();
+ int GetHandleFromCache();
+ void GetPackageFromCache(uid_t uid, const std::string& package);
pkgmgrinfo_filter_x* filter_ = nullptr;
std::vector<std::shared_ptr<package_x>> handle_list_;
uid_t uid_;
}
int PkgSetDBHandler::Execute() {
- std::unique_lock<std::shared_timed_mutex> u(lock_);
+ std::unique_lock<std::shared_mutex> u(lock_);
SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE);
SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
}
int QueryHandler::Execute() {
- std::shared_lock<std::shared_timed_mutex> s(lock_);
+ std::shared_lock<std::shared_mutex> s(lock_);
if (!Connect()) {
LOG(ERROR) << "Failed to connect database";
return PMINFO_R_ERROR;
: AbstractDBHandler(uid, pid) {}
int RemoveCacheDBHandler::Execute() {
- std::unique_lock<std::shared_timed_mutex> u(lock_);
+ std::unique_lock<std::shared_mutex> u(lock_);
database::DBHandleProvider::GetInst(GetUID()).UnsetMemoryMode(GetPID());
database::DBHandleProvider::GetInst(GetUID()).TrimCache();