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"
22 #include <sys/types.h>
24 #include <tzplatform_config.h>
30 #include "cache_flag.hh"
31 #include "utils/logging.hh"
33 #include "pkgmgr-info.h"
34 #include "pkgmgrinfo_debug.h"
35 #include "pkgmgrinfo_internal.h"
36 #include "pkgmgrinfo_internal.hh"
37 #include "pkgmgrinfo_private.h"
42 #define LOG_TAG "PKGMGR_INFO"
46 uid_t globaluser_uid = -1;
48 uid_t GetGlobalUID() {
49 if (globaluser_uid == (uid_t)-1)
50 globaluser_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
52 return globaluser_uid;
55 uid_t ConvertUID(uid_t uid) {
56 if (uid < REGULAR_USER)
57 return GetGlobalUID();
62 bool GetModifiedTime(const char* dbpath, timespec* t) {
63 if (dbpath == nullptr || t == nullptr) {
64 LOG(ERROR) << "Invalid argument";
69 if (stat(dbpath, &attr)) {
70 LOG(ERROR) << "Fail to get status from file "
71 << dbpath << " errno : " << errno;
80 static const std::string global_parser_memdb_path =
81 "file:parserdb?mode=memory&cache=shared";
83 static const std::string cert_memdb_path =
84 "file:certdb?mode=memory&cache=shared";
88 namespace pkgmgr_server {
91 std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>>
92 DBHandleProvider::provider_map_;
93 bool DBHandleProvider::is_global_memdb_set_ = false;
94 std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
95 DBHandleProvider::global_parser_memdb_handle_(nullptr,
97 std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
98 DBHandleProvider::cert_memdb_handle_(nullptr, sqlite3_close_v2);
99 std::string DBHandleProvider::global_parser_filedb_path_;
100 std::string DBHandleProvider::cert_filedb_path_;
101 std::unordered_set<pid_t> DBHandleProvider::writer_pid_list_;
102 std::recursive_mutex DBHandleProvider::lock_;
103 std::mutex DBHandleProvider::pid_list_lock_;
105 DBHandleProvider::DBHandleProvider(uid_t uid)
107 is_user_memdb_set_(false),
108 parser_memdb_handle_(nullptr, sqlite3_close_v2) {
111 if (global_parser_filedb_path_.empty()) {
112 tmp_path = getUserPkgParserDBPathUID(GetGlobalUID());
113 global_parser_filedb_path_ = tmp_path;
117 if (cert_filedb_path_.empty()) {
118 tmp_path = getUserPkgCertDBPath();
119 cert_filedb_path_ = tmp_path;
123 tmp_path = getUserPkgParserDBPathUID(uid_);
124 user_parser_filedb_path_ = tmp_path;
127 user_parser_memdb_path_ = "file:parserdb" +
128 std::to_string(static_cast<int>(uid_)) +
129 "?mode=memory&cache=shared";
132 DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
133 static std::mutex singleton_lock_;
134 std::unique_lock<std::mutex> u(singleton_lock_);
136 uid = ConvertUID(uid);
137 auto& prov = provider_map_[uid];
139 prov.reset(new DBHandleProvider(uid));
144 bool DBHandleProvider::IsCrashedWriteRequest() {
145 std::unique_lock<std::mutex> u(pid_list_lock_);
147 if (writer_pid_list_.empty())
151 LOG(DEBUG) << "Check process count : " << writer_pid_list_.size();
152 std::vector<pid_t> remove_pids;
153 for (pid_t pid : writer_pid_list_) {
154 std::string status_path = "/proc/" + std::to_string(pid) + "/status";
156 int fd = open(status_path.c_str(), O_RDONLY);
158 LOG(ERROR) << "Process is crashed : " << pid;
159 remove_pids.push_back(pid);
166 for (pid_t pid : remove_pids)
167 writer_pid_list_.erase(pid);
172 std::vector<std::pair<std::string, uid_t>> DBHandleProvider::GetParserDBPath(
173 pid_t pid, bool write) {
174 std::unique_lock<std::recursive_mutex> u(lock_);
175 std::vector<std::pair<std::string, uid_t>> db_path_list;
177 if (is_user_memdb_set_ != is_global_memdb_set_)
178 is_global_memdb_set_ ? SetMemoryMode(pid) : UnsetMemoryMode(pid);
180 if (IsMemoryDBActive(pid, write)) {
181 if (uid_ > REGULAR_USER)
182 db_path_list.emplace_back(std::make_pair(user_parser_memdb_path_, uid_));
184 db_path_list.emplace_back(
185 std::make_pair(global_parser_memdb_path, GetGlobalUID()));
187 if (uid_ > REGULAR_USER)
188 db_path_list.emplace_back(std::make_pair(user_parser_filedb_path_, uid_));
190 db_path_list.emplace_back(
191 std::make_pair(global_parser_filedb_path_, GetGlobalUID()));
194 if (db_path_list.size() == 1) {
195 LOG(DEBUG) << "global db path : " << db_path_list[0].first;
197 LOG(DEBUG) << "local db path : " << db_path_list[0].first;
198 LOG(DEBUG) << "global db path : " << db_path_list[1].first;
204 std::string DBHandleProvider::GetCertDBPath(pid_t pid, bool write) {
205 std::unique_lock<std::recursive_mutex> u(lock_);
206 if (is_user_memdb_set_ != is_global_memdb_set_)
207 is_global_memdb_set_ ? SetMemoryMode(pid) : UnsetMemoryMode(pid);
209 if (IsMemoryDBActive(pid, write))
210 return cert_memdb_path;
212 return cert_filedb_path_;
215 sqlite3* DBHandleProvider::CreateMemoryDBHandle(const std::string& filedb_path,
216 const std::string& memorydb_path) {
217 sqlite3* memorydb = nullptr;
218 sqlite3* filedb = nullptr;
219 int ret = sqlite3_open_v2(memorydb_path.c_str(), &memorydb,
220 SQLITE_OPEN_READONLY | SQLITE_OPEN_URI, nullptr);
221 if (ret != SQLITE_OK) {
222 LOG(ERROR) << "Failed to open memory DB " << ret << ": " << memorydb_path;
226 ret = sqlite3_open_v2(filedb_path.c_str(), &filedb, SQLITE_OPEN_READONLY,
228 if (ret != SQLITE_OK) {
229 LOG(ERROR) << "Failed to open file DB " << ret << ": " << filedb_path;
230 sqlite3_close_v2(memorydb);
234 sqlite3_backup* backup =
235 sqlite3_backup_init(memorydb, "main", filedb, "main");
236 if (backup == nullptr) {
237 LOG(ERROR) << "Failed to backup for memory DB";
238 sqlite3_close_v2(memorydb);
239 sqlite3_close_v2(filedb);
243 sqlite3_backup_step(backup, -1);
244 sqlite3_backup_finish(backup);
245 sqlite3_close_v2(filedb);
249 void DBHandleProvider::SetMemoryMode(pid_t pid) {
250 std::unique_lock<std::recursive_mutex> u(lock_);
251 if (is_global_memdb_set_ && is_user_memdb_set_)
255 CreateMemoryDBHandle(user_parser_filedb_path_, user_parser_memdb_path_);
256 if (parser_db != nullptr)
257 parser_memdb_handle_.reset(parser_db);
259 if (is_user_memdb_set_ == is_global_memdb_set_) {
260 sqlite3* global_parser_file_db = CreateMemoryDBHandle(
261 global_parser_filedb_path_, global_parser_memdb_path);
262 if (global_parser_file_db)
263 global_parser_memdb_handle_.reset(global_parser_file_db);
266 CreateMemoryDBHandle(cert_filedb_path_, cert_memdb_path);
267 if (cert_db != nullptr)
268 cert_memdb_handle_.reset(cert_db);
273 is_user_memdb_set_ = true;
274 is_global_memdb_set_ = true;
275 LOG(DEBUG) << "Set Memory mode : Memory";
278 void DBHandleProvider::UnsetMemoryMode(pid_t pid) {
279 std::unique_lock<std::recursive_mutex> u(lock_);
280 if (!is_global_memdb_set_ && !is_user_memdb_set_)
283 parser_memdb_handle_.reset(nullptr);
284 cert_memdb_handle_.reset(nullptr);
285 global_parser_memdb_handle_.reset(nullptr);
288 LOG(ERROR) << "Given pid is not exists in pid list : " << pid;
290 is_user_memdb_set_ = false;
291 is_global_memdb_set_ = false;
295 LOG(DEBUG) << "Set Memory mode : File";
298 bool DBHandleProvider::IsMemoryDBActive(pid_t pid, bool write) {
299 std::unique_lock<std::mutex> u(pid_list_lock_);
300 if (!is_user_memdb_set_)
306 if (writer_pid_list_.find(pid) != writer_pid_list_.end())
312 void DBHandleProvider::TrimCache() {
313 std::unique_lock<std::recursive_mutex> u(lock_);
318 void DBHandleProvider::ReleaseCache() {
319 auto lock = CacheFlag::GetWriterLock();
320 CacheFlag::SetStatus(CacheFlag::Status::PREPARING);
324 CacheFlag::SetStatus(CacheFlag::Status::UNPREPARED);
329 bool DBHandleProvider::IsWriter(pid_t pid) {
330 std::unique_lock<std::mutex> u(pid_list_lock_);
331 return writer_pid_list_.find(pid) != writer_pid_list_.end();
334 int DBHandleProvider::UpdateCache(sqlite3* db, pid_t pid, uid_t uid, bool write,
335 const std::string& locale) {
339 const char* dbpath = sqlite3_db_filename(db, "main");
340 bool is_inmemory_db = false;
341 if (dbpath == nullptr || strlen(dbpath) == 0) {
342 LOG(INFO) << "database is inmemory db";
343 is_inmemory_db = true;
346 timespec start_time = { 0, };
347 timespec end_time = { 0, };
348 if (!is_inmemory_db && !GetModifiedTime(dbpath, &start_time))
349 return PMINFO_R_ERROR;
351 GHashTable* list = g_hash_table_new(g_str_hash, g_str_equal);
352 if (list == nullptr) {
353 LOG(ERROR) << "Out of memory";
354 return PMINFO_R_ERROR;
357 pkgmgrinfo_filter_x tmp_filter = { 0, };
358 tmp_filter.cache_flag = true;
359 int ret = pkginfo_internal_filter_get_list(db, &tmp_filter, uid_,
360 locale.c_str(), list);
361 if (ret == PMINFO_R_OK) {
364 g_hash_table_iter_init(&iter, list);
365 while (g_hash_table_iter_next(&iter, nullptr, &value)) {
366 auto* pkg = reinterpret_cast<package_x*>(value);
367 std::string pkgid = pkg->package;
368 AddPackage(std::move(pkgid), pkg);
372 g_hash_table_destroy(list);
373 if (ret == PMINFO_R_ERROR)
376 if (!is_inmemory_db && !GetModifiedTime(dbpath, &end_time))
377 return PMINFO_R_ERROR;
379 if (start_time.tv_sec != end_time.tv_sec ||
380 start_time.tv_nsec != end_time.tv_nsec) {
381 LOG(ERROR) << "Database(" << dbpath << ") modification was detected";
382 return PMINFO_R_ERROR;
385 std::vector<std::shared_ptr<application_x>> app_list;
386 ret = pkgmgr_server::internal::appinfo_internal_filter_get_list(db,
387 &tmp_filter, uid_, uid, locale.c_str(), app_list);
389 if (!is_inmemory_db && !GetModifiedTime(dbpath, &end_time))
390 return PMINFO_R_ERROR;
392 if (start_time.tv_sec != end_time.tv_sec ||
393 start_time.tv_nsec != end_time.tv_nsec) {
394 LOG(ERROR) << "Database(" << dbpath << ") modification was detected";
395 return PMINFO_R_ERROR;
398 if (ret == PMINFO_R_OK) {
399 for (auto& app : app_list) {
400 if (pkg_map_.find(app->package) == pkg_map_.end()) {
401 LOG(ERROR) << "Can not find package from pkg_map";
402 return PMINFO_R_ERROR;
405 app->privileges = pkg_map_[app->package].front()->privileges;
406 std::string appid = app->appid;
407 AddApplication(std::move(appid), std::move(app));
416 std::vector<std::shared_ptr<package_x>> DBHandleProvider::GetPackages(
417 pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
418 const std::string& package) {
419 std::vector<std::shared_ptr<package_x>> ret;
420 auto map_it = pkg_map_.find(package);
421 if (map_it == pkg_map_.end())
424 if (__check_package_storage_status(filter)) {
425 if (pkgmgrinfo_pkginfo_filter_add_bool(filter,
426 PMINFO_PKGINFO_PROP_PACKAGE_CHECK_STORAGE, true) != PMINFO_R_OK) {
427 LOG(ERROR) << "Fail to add check storage value to filter";
432 for (auto& info : map_it->second) {
434 for (auto* it = filter->list; it != nullptr; it = g_slist_next(it)) {
435 auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
436 auto* checker = FilterCheckerProvider::GetInst().
437 GetPkgFilterChecker(node->prop);
438 if (!checker->CheckFilter(node, info.get())) {
450 void DBHandleProvider::AddPackage(std::string package, package_x* info) {
451 auto ptr = std::shared_ptr<package_x>(info, pkgmgrinfo_basic_free_package);
452 pkg_map_[package].push_back(ptr);
453 pkg_map_[""].push_back(std::move(ptr));
456 std::vector<std::shared_ptr<application_x>> DBHandleProvider::GetApplications(
457 pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
458 const std::string& app) {
459 /* make metadata filter map */
460 std::unordered_map<std::string, std::string> metadata_map;
461 for (auto* it = filter->list_metadata; it != nullptr; it = g_slist_next(it)) {
462 auto node = reinterpret_cast<pkgmgrinfo_metadata_node_x*>(it->data);
463 if (node->key == nullptr)
466 metadata_map[node->key] = (node->value ? node->value : "");
469 std::vector<std::shared_ptr<application_x>> ret;
470 auto map_it = app_map_.find(app);
471 if (map_it == app_map_.end())
474 if (pkgmgr_server::internal::check_app_storage_status(filter)) {
475 if (pkgmgrinfo_appinfo_filter_add_bool(filter,
476 PMINFO_APPINFO_PROP_APP_CHECK_STORAGE, true) != PMINFO_R_OK) {
477 LOG(ERROR) << "Fail to add check storage value to filter";
482 for (auto& info : map_it->second) {
484 for (auto* it = filter->list; it != nullptr; it = g_slist_next(it)) {
485 auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
486 auto* checker = FilterCheckerProvider::GetInst().
487 GetAppFilterChecker(node->prop);
488 if (!checker->CheckFilter(node, info.get())) {
496 if (!metadata_map.empty()) {
498 for (auto* it = info->metadata; it != nullptr; it = g_list_next(it)) {
499 auto* node = reinterpret_cast<metadata_x*>(it->data);
500 if (node->key != nullptr) {
501 auto metadata = metadata_map.find(node->key);
502 if (metadata == metadata_map.end())
505 if (metadata->second.empty() ||
506 strcmp(node->value ? node->value : "",
507 metadata->second.c_str()) == 0) {
521 void DBHandleProvider::AddApplication(std::string app, std::shared_ptr<application_x> info) {
522 app_map_[app].push_back(info);
523 app_map_[""].push_back(std::move(info));
526 void DBHandleProvider::InsertPID(pid_t pid) {
527 std::unique_lock<std::mutex> u(pid_list_lock_);
529 writer_pid_list_.insert(pid);
532 bool DBHandleProvider::ErasePID(pid_t pid) {
533 std::unique_lock<std::mutex> u(pid_list_lock_);
535 return writer_pid_list_.erase(pid) == 1;
538 } // namespace database
539 } // namespace pkgmgr_server