2 * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <sys/types.h>
19 #include <tzplatform_config.h>
21 #include "db_handle_provider.hh"
22 #include "pkgmgr-info.h"
23 #include "pkgmgrinfo_debug.h"
31 #define LOG_TAG "PKGMGR_INFO"
35 constexpr uid_t REGULAR_USER = 5000;
37 uid_t GetGlobalUID() {
38 return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
41 bool IsUserRequest(uid_t uid) {
42 return (uid > REGULAR_USER);
45 uid_t ConvertUID(uid_t uid) {
46 if (uid < REGULAR_USER)
47 return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
54 namespace pkgmgr_common {
56 std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>>
57 DBHandleProvider::provider_;
58 bool DBHandleProvider::is_memory_ = false;
59 std::vector<pid_t> DBHandleProvider::pid_list_;
61 DBHandleProvider::DBHandleProvider(uid_t uid) : uid_(uid),
62 global_parser_memory_db_handle_(nullptr, sqlite3_close_v2),
63 parser_memory_db_handle_(nullptr, sqlite3_close_v2),
64 cert_memory_db_handle_(nullptr, sqlite3_close_v2) {
65 char *tmp_path = getUserPkgParserDBPathUID(GetGlobalUID());
66 global_parser_file_db_path_ = tmp_path;
69 tmp_path = getUserPkgParserDBPathUID(uid_);
70 parser_file_db_path_ = tmp_path;
73 tmp_path = getUserPkgCertDBPath();
74 cert_file_db_path_ = tmp_path;
77 global_parser_memory_db_path_ = "file:parserdb?mode=memory&cache=shared";
78 parser_memory_db_path_ = "file:parserdb" +
79 std::to_string(static_cast<int>(uid_)) + "?mode=memory&cache=shared";
80 cert_memory_db_path_ = "file:certdb" +
81 std::to_string(static_cast<int>(uid_)) + "?mode=memory&cache=shared";
84 DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
85 static std::mutex singleton_lock;
86 std::unique_lock<std::mutex> u(singleton_lock);
87 uid = ConvertUID(uid);
88 auto& prov = provider_[uid];
89 if (prov == nullptr) {
90 prov.reset(new DBHandleProvider(uid));
96 std::vector<std::string> DBHandleProvider::GetParserDBPath(pid_t pid) {
97 std::unique_lock<std::mutex> u(lock_);
98 std::vector<std::string> db_path_list;
99 bool tmp_is_memory = is_memory_;
102 std::vector<pid_t>::iterator it = std::find(pid_list_.begin(),
103 pid_list_.end(), pid);
104 if (it != pid_list_.end())
105 tmp_is_memory = false;
110 db_path_list.emplace_back(parser_memory_db_path_);
111 if (IsUserRequest(uid_))
112 db_path_list.emplace_back(global_parser_memory_db_path_);
114 db_path_list.emplace_back(parser_file_db_path_);
115 if (IsUserRequest(uid_))
116 db_path_list.emplace_back(global_parser_file_db_path_);
121 std::string DBHandleProvider::GetCertDBPath(pid_t pid) {
122 std::unique_lock<std::mutex> u(lock_);
123 bool tmp_is_memory = is_memory_;
126 std::vector<pid_t>::iterator it = std::find(pid_list_.begin(),
127 pid_list_.end(), pid);
128 if (it != pid_list_.end())
129 tmp_is_memory = false;
133 return cert_memory_db_path_;
135 return cert_file_db_path_;
138 sqlite3* DBHandleProvider::GetMemoryDBHandle(const std::string& filedb_path,
139 const std::string& memorydb_path) {
140 sqlite3* memorydb = nullptr;
141 sqlite3* filedb = nullptr;
142 int ret = sqlite3_open_v2(memorydb_path.c_str(), &memorydb,
143 SQLITE_OPEN_READONLY | SQLITE_OPEN_URI, nullptr);
144 if (ret != SQLITE_OK) {
145 LOGE("Failed to open memory DB %d(%s)", ret, memorydb_path.c_str());
149 ret = sqlite3_open_v2(filedb_path.c_str(), &filedb,
150 SQLITE_OPEN_READONLY, nullptr);
151 if (ret != SQLITE_OK) {
152 LOGE("Failed to open file DB %d(%s)", ret, filedb_path.c_str());
153 sqlite3_close_v2(memorydb);
156 sqlite3_backup* backup = sqlite3_backup_init(memorydb, "main",
158 if (backup == nullptr) {
159 LOGE("Failed to backup for memory DB");
160 sqlite3_close_v2(memorydb);
161 sqlite3_close_v2(filedb);
165 sqlite3_backup_step(backup, -1);
166 sqlite3_backup_finish(backup);
167 LOGD("Set memory DB(%s)", memorydb_path.c_str());
168 sqlite3_close_v2(filedb);
172 void DBHandleProvider::SetMemoryMode(pid_t pid, bool flag) {
173 std::unique_lock<std::mutex> u(lock_);
174 if (is_memory_ == flag)
178 if (is_memory_ == true) {
179 sqlite3* parser_db = GetMemoryDBHandle(parser_file_db_path_,
180 parser_memory_db_path_);
181 if (parser_db != nullptr)
182 parser_memory_db_handle_.reset(parser_db);
183 sqlite3* global_parser_file_db = GetMemoryDBHandle(global_parser_file_db_path_,
184 global_parser_memory_db_path_);
185 if (global_parser_file_db)
186 global_parser_memory_db_handle_.reset(global_parser_file_db);
187 sqlite3* cert_db = GetMemoryDBHandle(cert_file_db_path_,
188 cert_memory_db_path_);
189 if (cert_db != nullptr)
190 cert_memory_db_handle_.reset(cert_db);
191 std::vector<pid_t>::iterator it =
192 std::find(pid_list_.begin(), pid_list_.end(), pid);
194 if (it == pid_list_.end())
195 pid_list_.emplace_back(pid);
197 parser_memory_db_handle_.reset(nullptr);
198 cert_memory_db_handle_.reset(nullptr);
199 global_parser_memory_db_handle_.reset(nullptr);
200 std::vector<pid_t>::iterator it =
201 std::find(pid_list_.begin(), pid_list_.end(), pid);
202 if (it != pid_list_.end())
205 LOGE("Given pid is not exists in pid list : %d", pid);
208 LOGD("Set Memory mode : %s", flag ? "Memory" : "File");
211 } // namespace pkgmgr_common