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 "db_handle_provider.hh"
21 #include <sys/types.h>
23 #include <tzplatform_config.h>
29 #include "cache_flag.hh"
30 #include "utils/logging.hh"
32 #include "pkgmgr-info.h"
33 #include "pkgmgrinfo_debug.h"
34 #include "pkgmgrinfo_internal.h"
35 #include "pkgmgrinfo_private.h"
40 #define LOG_TAG "PKGMGR_INFO"
44 uid_t globaluser_uid = -1;
46 uid_t GetGlobalUID() {
47 if (globaluser_uid == (uid_t)-1)
48 globaluser_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
50 return globaluser_uid;
53 uid_t ConvertUID(uid_t uid) {
54 if (uid < REGULAR_USER)
55 return GetGlobalUID();
60 static const std::string global_parser_memdb_path =
61 "file:parserdb?mode=memory&cache=shared";
63 static const std::string cert_memdb_path =
64 "file:certdb?mode=memory&cache=shared";
68 namespace pkgmgr_server {
71 std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>>
72 DBHandleProvider::provider_map_;
73 bool DBHandleProvider::is_global_memdb_set_ = false;
74 std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
75 DBHandleProvider::global_parser_memdb_handle_(nullptr,
77 std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
78 DBHandleProvider::cert_memdb_handle_(nullptr, sqlite3_close_v2);
79 std::string DBHandleProvider::global_parser_filedb_path_;
80 std::string DBHandleProvider::cert_filedb_path_;
81 std::unordered_set<pid_t> DBHandleProvider::writer_pid_list_;
82 std::recursive_mutex DBHandleProvider::lock_;
83 std::mutex DBHandleProvider::pid_list_lock_;
85 DBHandleProvider::DBHandleProvider(uid_t uid)
87 is_user_memdb_set_(false),
88 parser_memdb_handle_(nullptr, sqlite3_close_v2) {
91 if (global_parser_filedb_path_.empty()) {
92 tmp_path = getUserPkgParserDBPathUID(GetGlobalUID());
93 global_parser_filedb_path_ = tmp_path;
97 if (cert_filedb_path_.empty()) {
98 tmp_path = getUserPkgCertDBPath();
99 cert_filedb_path_ = tmp_path;
103 tmp_path = getUserPkgParserDBPathUID(uid_);
104 user_parser_filedb_path_ = tmp_path;
107 user_parser_memdb_path_ = "file:parserdb" +
108 std::to_string(static_cast<int>(uid_)) +
109 "?mode=memory&cache=shared";
112 DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
113 static std::mutex singleton_lock_;
114 std::unique_lock<std::mutex> u(singleton_lock_);
116 uid = ConvertUID(uid);
117 auto& prov = provider_map_[uid];
119 prov.reset(new DBHandleProvider(uid));
124 bool DBHandleProvider::IsCrashedWriteRequest() {
125 std::unique_lock<std::mutex> u(pid_list_lock_);
127 if (writer_pid_list_.empty())
131 LOG(DEBUG) << "Check process count : " << writer_pid_list_.size();
132 std::vector<pid_t> remove_pids;
133 for (pid_t pid : writer_pid_list_) {
134 std::string status_path = "/proc/" + std::to_string(pid) + "/status";
136 int fd = open(status_path.c_str(), O_RDONLY);
138 LOG(ERROR) << "Process is crashed : " << pid;
139 remove_pids.push_back(pid);
146 for (pid_t pid : remove_pids)
147 writer_pid_list_.erase(pid);
152 std::vector<std::pair<std::string, uid_t>> DBHandleProvider::GetParserDBPath(
153 pid_t pid, bool write) {
154 std::unique_lock<std::recursive_mutex> u(lock_);
155 std::vector<std::pair<std::string, uid_t>> db_path_list;
157 if (is_user_memdb_set_ != is_global_memdb_set_)
158 is_global_memdb_set_ ? SetMemoryMode(pid) : UnsetMemoryMode(pid);
160 if (IsMemoryDBActive(pid, write)) {
161 if (uid_ > REGULAR_USER)
162 db_path_list.emplace_back(std::make_pair(user_parser_memdb_path_, uid_));
164 db_path_list.emplace_back(
165 std::make_pair(global_parser_memdb_path, GetGlobalUID()));
167 if (uid_ > REGULAR_USER)
168 db_path_list.emplace_back(std::make_pair(user_parser_filedb_path_, uid_));
170 db_path_list.emplace_back(
171 std::make_pair(global_parser_filedb_path_, GetGlobalUID()));
174 if (db_path_list.size() == 1) {
175 LOG(DEBUG) << "global db path : " << db_path_list[0].first;
177 LOG(DEBUG) << "local db path : " << db_path_list[0].first;
178 LOG(DEBUG) << "global db path : " << db_path_list[1].first;
184 std::string DBHandleProvider::GetCertDBPath(pid_t pid, bool write) {
185 std::unique_lock<std::recursive_mutex> u(lock_);
186 if (is_user_memdb_set_ != is_global_memdb_set_)
187 is_global_memdb_set_ ? SetMemoryMode(pid) : UnsetMemoryMode(pid);
189 if (IsMemoryDBActive(pid, write))
190 return cert_memdb_path;
192 return cert_filedb_path_;
195 sqlite3* DBHandleProvider::CreateMemoryDBHandle(const std::string& filedb_path,
196 const std::string& memorydb_path) {
197 sqlite3* memorydb = nullptr;
198 sqlite3* filedb = nullptr;
199 int ret = sqlite3_open_v2(memorydb_path.c_str(), &memorydb,
200 SQLITE_OPEN_READONLY | SQLITE_OPEN_URI, nullptr);
201 if (ret != SQLITE_OK) {
202 LOG(ERROR) << "Failed to open memory DB " << ret << ": " << memorydb_path;
206 ret = sqlite3_open_v2(filedb_path.c_str(), &filedb, SQLITE_OPEN_READONLY,
208 if (ret != SQLITE_OK) {
209 LOG(ERROR) << "Failed to open file DB " << ret << ": " << filedb_path;
210 sqlite3_close_v2(memorydb);
214 sqlite3_backup* backup =
215 sqlite3_backup_init(memorydb, "main", filedb, "main");
216 if (backup == nullptr) {
217 LOG(ERROR) << "Failed to backup for memory DB";
218 sqlite3_close_v2(memorydb);
219 sqlite3_close_v2(filedb);
223 sqlite3_backup_step(backup, -1);
224 sqlite3_backup_finish(backup);
225 sqlite3_close_v2(filedb);
229 void DBHandleProvider::SetMemoryMode(pid_t pid) {
230 std::unique_lock<std::recursive_mutex> u(lock_);
231 if (is_global_memdb_set_ && is_user_memdb_set_)
235 CreateMemoryDBHandle(user_parser_filedb_path_, user_parser_memdb_path_);
236 if (parser_db != nullptr)
237 parser_memdb_handle_.reset(parser_db);
239 if (is_user_memdb_set_ == is_global_memdb_set_) {
240 sqlite3* global_parser_file_db = CreateMemoryDBHandle(
241 global_parser_filedb_path_, global_parser_memdb_path);
242 if (global_parser_file_db)
243 global_parser_memdb_handle_.reset(global_parser_file_db);
246 CreateMemoryDBHandle(cert_filedb_path_, cert_memdb_path);
247 if (cert_db != nullptr)
248 cert_memdb_handle_.reset(cert_db);
253 is_user_memdb_set_ = true;
254 is_global_memdb_set_ = true;
255 LOG(DEBUG) << "Set Memory mode : Memory";
258 void DBHandleProvider::UnsetMemoryMode(pid_t pid) {
259 std::unique_lock<std::recursive_mutex> u(lock_);
260 if (!is_global_memdb_set_ && !is_user_memdb_set_)
263 parser_memdb_handle_.reset(nullptr);
264 cert_memdb_handle_.reset(nullptr);
265 global_parser_memdb_handle_.reset(nullptr);
268 LOG(ERROR) << "Given pid is not exists in pid list : " << pid;
270 is_user_memdb_set_ = false;
271 is_global_memdb_set_ = false;
273 auto lock = CacheFlag::GetWriterLock();
274 CacheFlag::SetStatus(CacheFlag::Status::PREPARING);
278 CacheFlag::SetStatus(CacheFlag::Status::UNPREPARED);
279 LOG(DEBUG) << "Set Memory mode : File";
282 bool DBHandleProvider::IsMemoryDBActive(pid_t pid, bool write) {
283 std::unique_lock<std::mutex> u(pid_list_lock_);
284 if (!is_user_memdb_set_)
290 if (writer_pid_list_.find(pid) != writer_pid_list_.end())
296 void DBHandleProvider::TrimCache() {
297 std::unique_lock<std::recursive_mutex> u(lock_);
302 void DBHandleProvider::ReleaseCache() {
303 auto lock = CacheFlag::GetWriterLock();
304 CacheFlag::SetStatus(CacheFlag::Status::PREPARING);
308 CacheFlag::SetStatus(CacheFlag::Status::UNPREPARED);
313 bool DBHandleProvider::IsWriter(pid_t pid) {
314 std::unique_lock<std::mutex> u(pid_list_lock_);
315 return writer_pid_list_.find(pid) != writer_pid_list_.end();
318 int DBHandleProvider::UpdateCache(sqlite3* db, pid_t pid, uid_t uid, bool write,
319 const std::string& locale) {
323 GHashTable* list = g_hash_table_new(g_str_hash, g_str_equal);
324 if (list == nullptr) {
325 LOG(ERROR) << "Out of memory";
326 return PMINFO_R_ERROR;
329 auto tmp_filter = reinterpret_cast<pkgmgrinfo_filter_x*>(
330 calloc(1, sizeof(pkgmgrinfo_filter_x)));
331 if (tmp_filter == nullptr) {
332 LOG(ERROR) << "Out of memory";
333 g_hash_table_destroy(list);
334 return PMINFO_R_ERROR;
337 int ret = pkginfo_internal_filter_get_list(db, tmp_filter, uid_,
338 locale.c_str(), list);
339 if (ret == PMINFO_R_OK) {
342 g_hash_table_iter_init(&iter, list);
343 while (g_hash_table_iter_next(&iter, nullptr, &value)) {
344 auto* pkg = reinterpret_cast<package_x*>(value);
345 AddPackage(pkg->package, pkg);
349 g_hash_table_destroy(list);
350 if (ret == PMINFO_R_ERROR) {
355 list = g_hash_table_new(g_str_hash, g_str_equal);
356 ret = appinfo_internal_filter_get_list(db, tmp_filter, uid_, uid,
357 locale.c_str(), list);
359 if (ret == PMINFO_R_OK) {
362 g_hash_table_iter_init(&iter, list);
363 while (g_hash_table_iter_next(&iter, nullptr, &value)) {
364 auto* app = reinterpret_cast<application_x*>(value);
365 app->privileges = pkg_map_[app->package].front()->privileges;
366 AddApplication(app->appid, app);
369 g_hash_table_destroy(list);
375 std::vector<std::shared_ptr<package_x>> DBHandleProvider::GetPackages(
376 pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
377 const std::string& package) {
378 std::vector<std::shared_ptr<package_x>> ret;
379 for (auto& info : pkg_map_[package]) {
381 for (auto* it = filter->list; it != nullptr; it = g_slist_next(it)) {
382 auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
383 auto* checker = FilterCheckerProvider::GetInst().
384 GetPkgFilterChecker(node->prop);
385 if (!checker->CheckFilter(node, info.get())) {
397 void DBHandleProvider::AddPackage(std::string package, package_x* info) {
398 auto ptr = std::shared_ptr<package_x>(info, pkgmgrinfo_basic_free_package);
399 pkg_map_[package].push_back(ptr);
400 pkg_map_[""].push_back(std::move(ptr));
403 std::vector<std::shared_ptr<application_x>> DBHandleProvider::GetApplications(
404 pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
405 const std::string& app) {
406 /* make metadata filter map */
407 std::unordered_map<std::string, std::string> metadata_map;
408 for (auto* it = filter->list_metadata; it != nullptr; it = g_slist_next(it)) {
409 auto node = reinterpret_cast<pkgmgrinfo_metadata_node_x*>(it->data);
410 if (node->key == nullptr)
413 metadata_map[node->key] = (node->value ? node->value : "");
416 std::vector<std::shared_ptr<application_x>> ret;
417 for (auto& info : app_map_[app]) {
419 for (auto* it = filter->list; it != nullptr; it = g_slist_next(it)) {
420 auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
421 auto* checker = FilterCheckerProvider::GetInst().
422 GetAppFilterChecker(node->prop);
423 if (!checker->CheckFilter(node, info.get())) {
431 if (!metadata_map.empty()) {
433 for (auto* it = info->metadata; it != nullptr; it = g_list_next(it)) {
434 auto* node = reinterpret_cast<metadata_x*>(it->data);
435 if (node->key != nullptr) {
436 auto metadata = metadata_map.find(node->key);
437 if (metadata != metadata_map.end() &&
438 strstr(node->value ? node->value : "",
439 metadata->second.c_str()) != nullptr) {
453 void DBHandleProvider::AddApplication(std::string app, application_x* info) {
455 std::shared_ptr<application_x>(info, pkgmgrinfo_basic_free_application);
456 app_map_[app].push_back(ptr);
457 app_map_[""].push_back(std::move(ptr));
460 void DBHandleProvider::InsertPID(pid_t pid) {
461 std::unique_lock<std::mutex> u(pid_list_lock_);
463 writer_pid_list_.insert(pid);
466 bool DBHandleProvider::ErasePID(pid_t pid) {
467 std::unique_lock<std::mutex> u(pid_list_lock_);
469 return writer_pid_list_.erase(pid) == 1;
472 } // namespace database
473 } // namespace pkgmgr_server