return uid;
}
+static const std::string global_parser_memory_db_path =
+ "file:parserdb?mode=memory&cache=shared";
+
+static const std::string cert_memory_db_path =
+ "file:certdb?mode=memory&cache=shared";
+
} // namespace
namespace pkgmgr_server {
nullptr, sqlite3_close_v2);
std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
DBHandleProvider::cert_memory_db_handle_(nullptr, sqlite3_close_v2);
-std::string DBHandleProvider::global_parser_memory_db_path_ =
- "file:parserdb?mode=memory&cache=shared";
std::string DBHandleProvider::global_parser_file_db_path_;
-std::string DBHandleProvider::cert_memory_db_path_ =
- "file:certdb?mode=memory&cache=shared";
std::string DBHandleProvider::cert_file_db_path_;
std::unordered_set<pid_t> DBHandleProvider::pid_list_;
std::recursive_mutex DBHandleProvider::lock_;
DBHandleProvider::DBHandleProvider(uid_t uid) : uid_(uid),
is_memory_(false), parser_memory_db_handle_(nullptr, sqlite3_close_v2) {
- char* tmp_path = nullptr;
+ char* tmp_path;
+
if (global_parser_file_db_path_.empty()) {
tmp_path = getUserPkgParserDBPathUID(GetGlobalUID());
global_parser_file_db_path_ = tmp_path;
DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
static std::mutex singleton_lock;
std::unique_lock<std::mutex> u(singleton_lock);
+
uid = ConvertUID(uid);
auto& prov = provider_[uid];
if (prov == nullptr)
bool DBHandleProvider::IsCrashedWriteRequest() {
std::unique_lock<std::recursive_mutex> u(lock_);
+
if (pid_list_.empty())
return false;
+
bool ret = true;
LOGD("Check process count : %zu", pid_list_.size());
std::vector<pid_t> remove_pids;
pid_t pid, bool write) {
std::unique_lock<std::recursive_mutex> u(lock_);
std::vector<std::pair<std::string, uid_t>> db_path_list;
+
if (is_memory_ != is_memory_global_)
- SetMemoryMode(pid, is_memory_global_);
+ is_memory_global_ ? SetMemoryMode(pid) : UnsetMemoryMode(pid);
- if (is_memory_ && pid_list_.find(pid) == pid_list_.end() && !write) {
+ if (IsMemoryDBActive(pid, write)) {
if (uid_ > REGULAR_USER)
db_path_list.emplace_back(std::make_pair(parser_memory_db_path_, uid_));
db_path_list.emplace_back(
- std::make_pair(global_parser_memory_db_path_, GetGlobalUID()));
+ std::make_pair(global_parser_memory_db_path, GetGlobalUID()));
} else {
if (uid_ > REGULAR_USER)
db_path_list.emplace_back(std::make_pair(parser_file_db_path_, uid_));
std::string DBHandleProvider::GetCertDBPath(pid_t pid, bool write) {
std::unique_lock<std::recursive_mutex> u(lock_);
if (is_memory_ != is_memory_global_)
- SetMemoryMode(pid, is_memory_global_);
+ is_memory_global_ ?
+ SetMemoryMode(pid) : UnsetMemoryMode(is_memory_global_);
- if ((is_memory_ && pid_list_.find(pid) == pid_list_.end()) && !write)
- return cert_memory_db_path_;
+ if (IsMemoryDBActive(pid, write))
+ return cert_memory_db_path;
else
return cert_file_db_path_;
}
sqlite3_close_v2(memorydb);
return nullptr;
}
+
sqlite3_backup* backup = sqlite3_backup_init(memorydb, "main",
filedb, "main");
if (backup == nullptr) {
return memorydb;
}
-void DBHandleProvider::SetMemoryMode(pid_t pid, bool flag) {
+void DBHandleProvider::SetMemoryMode(pid_t pid) {
std::unique_lock<std::recursive_mutex> u(lock_);
- if (flag == is_memory_global_ && flag == is_memory_)
+ if (is_memory_global_ && is_memory_)
return;
- if (flag == true) {
- sqlite3* parser_db = GetMemoryDBHandle(parser_file_db_path_,
- parser_memory_db_path_);
- if (parser_db != nullptr)
- parser_memory_db_handle_.reset(parser_db);
-
- if (is_memory_ == is_memory_global_) { /* first call */
- sqlite3* global_parser_file_db = GetMemoryDBHandle(
- global_parser_file_db_path_, global_parser_memory_db_path_);
- if (global_parser_file_db)
- global_parser_memory_db_handle_.reset(global_parser_file_db);
- sqlite3* cert_db = GetMemoryDBHandle(cert_file_db_path_,
- cert_memory_db_path_);
- if (cert_db != nullptr)
- cert_memory_db_handle_.reset(cert_db);
-
- if (pid_list_.find(pid) == pid_list_.end())
- pid_list_.insert(pid);
- }
- } else {
- parser_memory_db_handle_.reset(nullptr);
- cert_memory_db_handle_.reset(nullptr);
- global_parser_memory_db_handle_.reset(nullptr);
-
- auto it = pid_list_.find(pid);
- if (it != pid_list_.end())
- pid_list_.erase(it);
- else
- LOGE("Given pid is not exists in pid list : %d", pid);
+ sqlite3* parser_db = GetMemoryDBHandle(parser_file_db_path_,
+ parser_memory_db_path_);
+ if (parser_db != nullptr)
+ parser_memory_db_handle_.reset(parser_db);
+
+ if (is_memory_ == is_memory_global_) { /* first call */
+ sqlite3* global_parser_file_db = GetMemoryDBHandle(
+ global_parser_file_db_path_, global_parser_memory_db_path);
+ if (global_parser_file_db)
+ global_parser_memory_db_handle_.reset(global_parser_file_db);
+ sqlite3* cert_db = GetMemoryDBHandle(cert_file_db_path_,
+ cert_memory_db_path);
+ if (cert_db != nullptr)
+ cert_memory_db_handle_.reset(cert_db);
+
+ if (pid_list_.find(pid) == pid_list_.end())
+ pid_list_.insert(pid);
}
- is_memory_ = flag;
- is_memory_global_ = flag;
- LOGD("Set Memory mode : %s", flag ? "Memory" : "File");
+
+ is_memory_ = true;
+ is_memory_global_ = true;
+ LOGD("Set Memory mode : Memory");
+}
+
+void DBHandleProvider::UnsetMemoryMode(pid_t pid) {
+ std::unique_lock<std::recursive_mutex> u(lock_);
+ if (!is_memory_global_ && !is_memory_)
+ return;
+
+ parser_memory_db_handle_.reset(nullptr);
+ cert_memory_db_handle_.reset(nullptr);
+ global_parser_memory_db_handle_.reset(nullptr);
+
+ auto it = pid_list_.find(pid);
+ if (it != pid_list_.end())
+ pid_list_.erase(it);
+ else
+ LOGE("Given pid is not exists in pid list : %d", pid);
+
+ is_memory_ = false;
+ is_memory_global_ = false;
+ LOGD("Set Memory mode : File");
+}
+
+bool DBHandleProvider::IsMemoryDBActive(pid_t pid, bool write) {
+ return (is_memory_ && pid_list_.find(pid) == pid_list_.end() && !write);
}
} // namespace database