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_private.h"
41 #define LOG_TAG "PKGMGR_INFO"
45 uid_t globaluser_uid = -1;
47 uid_t GetGlobalUID() {
48 if (globaluser_uid == (uid_t)-1)
49 globaluser_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
51 return globaluser_uid;
54 uid_t ConvertUID(uid_t uid) {
55 if (uid < REGULAR_USER)
56 return GetGlobalUID();
61 bool GetModifiedTime(const char* dbpath, timespec* t) {
62 if (dbpath == nullptr || t == nullptr) {
63 LOG(ERROR) << "Invalid argument";
68 if (stat(dbpath, &attr)) {
69 LOG(ERROR) << "Fail to get status from file "
70 << dbpath << " errno : " << errno;
81 namespace pkgmgr_server {
84 std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>>
85 DBHandleProvider::provider_map_;
86 std::string DBHandleProvider::global_parser_filedb_path_;
87 std::string DBHandleProvider::cert_filedb_path_;
88 std::unordered_set<pid_t> DBHandleProvider::writer_pid_list_;
89 std::shared_mutex DBHandleProvider::pid_list_lock_;
91 DBHandleProvider::DBHandleProvider(uid_t uid) : uid_(uid) {
94 if (global_parser_filedb_path_.empty()) {
95 tmp_path = getUserPkgParserDBPathUID(GetGlobalUID());
96 global_parser_filedb_path_ = tmp_path;
100 if (cert_filedb_path_.empty()) {
101 tmp_path = getUserPkgCertDBPath();
102 cert_filedb_path_ = tmp_path;
106 tmp_path = getUserPkgParserDBPathUID(uid_);
107 user_parser_filedb_path_ = tmp_path;
111 DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
112 static std::shared_mutex singleton_lock_;
113 std::shared_lock<std::shared_mutex> s(singleton_lock_);
115 uid = ConvertUID(uid);
116 auto it = provider_map_.find(uid);
117 if (it != provider_map_.end())
118 return *(it->second);
121 std::unique_lock<std::shared_mutex> u(singleton_lock_);
122 auto& prov = provider_map_[uid];
124 prov.reset(new DBHandleProvider(uid));
129 std::unordered_set<pid_t> DBHandleProvider::CrashedWriteRequestPIDs() {
130 std::unique_lock<std::shared_mutex> u(pid_list_lock_);
132 if (writer_pid_list_.empty())
135 LOG(DEBUG) << "Check process count : " << writer_pid_list_.size();
136 std::unordered_set<pid_t> remove_pids;
137 for (pid_t pid : writer_pid_list_) {
138 std::string status_path = "/proc/" + std::to_string(pid) + "/status";
140 int fd = open(status_path.c_str(), O_RDONLY);
142 LOG(ERROR) << "Process is crashed : " << pid;
143 remove_pids.emplace(pid);
152 std::vector<std::pair<std::string, uid_t>> DBHandleProvider::GetParserDBPath() {
153 std::vector<std::pair<std::string, uid_t>> db_path_list;
155 if (uid_ > REGULAR_USER)
156 db_path_list.emplace_back(std::make_pair(user_parser_filedb_path_, uid_));
158 db_path_list.emplace_back(
159 std::make_pair(global_parser_filedb_path_, GetGlobalUID()));
161 if (db_path_list.size() == 1) {
162 LOG(DEBUG) << "global db path : " << db_path_list[0].first;
164 LOG(DEBUG) << "local db path : " << db_path_list[0].first;
165 LOG(DEBUG) << "global db path : " << db_path_list[1].first;
171 std::string DBHandleProvider::GetCertDBPath() {
172 return cert_filedb_path_;
175 void DBHandleProvider::TrimCache() {
180 void DBHandleProvider::ReleaseCache() {
183 pending_pkg_.clear();
184 pkg_app_map_.clear();
189 bool DBHandleProvider::IsWriter(pid_t pid) {
190 std::unique_lock<std::shared_mutex> s(pid_list_lock_);
191 return writer_pid_list_.find(pid) != writer_pid_list_.end();
194 int DBHandleProvider::UpdateCache(const tizen_base::Database& db, pid_t pid,
195 uid_t uid, bool write, const std::string& locale) {
198 pending_pkg_.clear();
199 pkg_app_map_.clear();
201 const char* dbpath = sqlite3_db_filename(db.GetRaw(), "main");
203 timespec start_time = { 0, };
204 timespec end_time = { 0, };
205 if (!GetModifiedTime(dbpath, &start_time))
206 return PMINFO_R_ERROR;
208 pkgmgrinfo_filter_x tmp_filter = { 0, };
209 tmp_filter.cache_flag = true;
210 std::map<std::string, std::shared_ptr<package_x>> pkgs;
211 int ret = internal::GetPkgInfo(db, &tmp_filter, uid_, locale, pkgs);
212 if (ret == PMINFO_R_OK) {
213 for (auto& [key, val] : pkgs)
214 AddPackage(std::move(val));
217 if (!GetModifiedTime(dbpath, &end_time))
218 return PMINFO_R_ERROR;
220 if (start_time.tv_sec != end_time.tv_sec ||
221 start_time.tv_nsec != end_time.tv_nsec) {
222 LOG(ERROR) << "Database(" << dbpath << ") modification was detected";
223 return PMINFO_R_ERROR;
226 std::vector<std::shared_ptr<application_x>> app_list;
227 ret = internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
229 if (!GetModifiedTime(dbpath, &end_time))
230 return PMINFO_R_ERROR;
232 if (start_time.tv_sec != end_time.tv_sec ||
233 start_time.tv_nsec != end_time.tv_nsec) {
234 LOG(ERROR) << "Database(" << dbpath << ") modification was detected";
235 return PMINFO_R_ERROR;
238 if (ret == PMINFO_R_OK) {
239 for (auto& app : app_list) {
240 auto it = pkg_map_.find(app->package);
241 if (it == pkg_map_.end()) {
242 LOG(ERROR) << "Can not find package from pkg_map";
243 return PMINFO_R_ERROR;
246 app->privileges = it->second->privileges;
247 AddApplication(std::move(app));
256 inline bool CheckMetadataFilter(GList* metadata_list,
257 const std::unordered_map<std::string, std::string>& metadata_map) {
258 for (auto* it = metadata_list; it != nullptr; it = g_list_next(it)) {
259 auto* node = reinterpret_cast<metadata_x*>(it->data);
260 if (node->key != nullptr) {
261 auto metadata = metadata_map.find(node->key);
262 if (metadata == metadata_map.end())
265 if (metadata->second.empty() ||
266 strcmp(node->value ? node->value : "", metadata->second.c_str()) == 0)
273 inline bool CheckPkgFilters(pkgmgrinfo_filter_x* filter,
274 const std::shared_ptr<package_x>& info,
275 const std::unordered_map<std::string, std::string>& metadata_map) {
276 for (auto* it = filter->list; it != nullptr; it = g_slist_next(it)) {
277 auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
278 auto* checker = FilterCheckerProvider::GetInst().
279 GetPkgFilterChecker(node->prop);
280 if (!checker->CheckFilter(node, info.get()))
285 if (!metadata_map.empty())
286 pass = CheckMetadataFilter(info->metadata, metadata_map);
291 std::vector<std::shared_ptr<package_x>> DBHandleProvider::GetPackages(
292 pid_t pid, pkgmgrinfo_filter_x* filter,
293 const std::string& package) {
294 std::vector<std::shared_ptr<package_x>> ret;
296 /* make metadata filter map */
297 std::unordered_map<std::string, std::string> metadata_map;
298 for (auto* it = filter->list_pkg_metadata; it != nullptr;
299 it = g_slist_next(it)) {
300 auto node = reinterpret_cast<pkgmgrinfo_metadata_node_x*>(it->data);
301 if (node->key == nullptr)
303 LOG(ERROR) << "add metadata filter";
304 metadata_map[node->key] = (node->value ? node->value : "");
307 if (internal::CheckPackageStorageStatus(filter)) {
308 if (pkgmgrinfo_pkginfo_filter_add_bool(filter,
309 PMINFO_PKGINFO_PROP_PACKAGE_CHECK_STORAGE, true) != PMINFO_R_OK) {
310 LOG(ERROR) << "Fail to add check storage value to filter";
314 if (package.empty()) {
315 for (auto& info : pkg_map_) {
316 if (CheckPkgFilters(filter, info.second, metadata_map))
317 ret.push_back(info.second);
320 auto map_it = pkg_map_.find(package);
321 if (map_it != pkg_map_.end() && CheckPkgFilters(filter, map_it->second,
323 ret.push_back(map_it->second);
329 void DBHandleProvider::AddPackage(std::shared_ptr<package_x> info) {
330 pkg_map_[info->package] = std::move(info);
333 void DBHandleProvider::AddApplication(std::shared_ptr<application_x> info) {
334 std::string appid = info->appid;
335 std::string pkgid = info->package;
336 app_map_[appid] = std::move(info);
337 pkg_app_map_[pkgid].emplace(std::move(appid));
340 void DBHandleProvider::ErasePackage(const std::string& pkgid) {
341 auto it = pkg_app_map_.find(pkgid);
342 if (it != pkg_app_map_.end()) {
343 for (const auto& app : it->second)
345 pkg_app_map_.erase(it);
348 pkg_map_.erase(pkgid);
351 inline bool CheckAppFilters(pkgmgrinfo_filter_x* filter,
352 const std::shared_ptr<application_x>& app_info,
353 const std::shared_ptr<package_x>& pkg_info,
354 const std::unordered_map<std::string, std::string>& metadata_map) {
355 for (auto* it = filter->list; it != nullptr; it = g_slist_next(it)) {
356 auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
357 auto* checker = FilterCheckerProvider::GetInst().
358 GetAppFilterChecker(node->prop);
359 if (!checker->CheckFilter(node, app_info.get(), pkg_info.get()))
364 if (!metadata_map.empty())
365 pass = CheckMetadataFilter(app_info->metadata, metadata_map);
369 std::shared_ptr<package_x> DBHandleProvider::GetPackageByApp(
371 auto it = pkg_map_.find(appid);
372 if (it == pkg_map_.end())
378 std::vector<std::shared_ptr<application_x>> DBHandleProvider::GetApplications(
379 pid_t pid, pkgmgrinfo_filter_x* filter,
380 const std::string& app) {
381 /* make metadata filter map */
382 std::unordered_map<std::string, std::string> metadata_map;
383 for (auto* it = filter->list_metadata; it != nullptr; it = g_slist_next(it)) {
384 auto node = reinterpret_cast<pkgmgrinfo_metadata_node_x*>(it->data);
385 if (node->key == nullptr)
388 metadata_map[node->key] = (node->value ? node->value : "");
391 std::vector<std::shared_ptr<application_x>> ret;
392 if (internal::CheckAppStorageStatus(filter)) {
393 if (pkgmgrinfo_appinfo_filter_add_bool(filter,
394 PMINFO_APPINFO_PROP_APP_CHECK_STORAGE, true) != PMINFO_R_OK) {
395 LOG(ERROR) << "Fail to add check storage value to filter";
401 for (auto& info : app_map_) {
402 if (CheckAppFilters(filter, info.second,
403 GetPackageByApp(info.second->package), metadata_map))
404 ret.push_back(info.second);
407 auto map_it = app_map_.find(app);
408 if (map_it != app_map_.end() &&
409 CheckAppFilters(filter, map_it->second,
410 GetPackageByApp(map_it->second->package), metadata_map))
411 ret.push_back(map_it->second);
417 void DBHandleProvider::InsertWriterPID(pid_t pid) {
418 std::unique_lock<std::shared_mutex> u(pid_list_lock_);
420 writer_pid_list_.insert(pid);
423 bool DBHandleProvider::EraseWriterPID(pid_t pid) {
424 std::unique_lock<std::shared_mutex> u(pid_list_lock_);
426 return writer_pid_list_.erase(pid) == 1;
429 void DBHandleProvider::RegisterPendingPackageInfo(
430 const tizen_base::Database& db, package_x* info, pid_t pid, uid_t uid,
431 const std::string& locale, pkgmgr_common::PkgWriteType write_type) {
432 if (!info || !info->package)
435 InsertWriterPID(pid);
437 if (write_type != pkgmgr_common::PkgWriteType::Delete) {
438 pkgmgrinfo_filter_x tmp_filter = { 0, };
439 pkgmgrinfo_node_x node = {
440 .prop = E_PMINFO_PKGINFO_PROP_PACKAGE_ID
442 tmp_filter.cache_flag = true;
443 tmp_filter.list = g_slist_append(tmp_filter.list, (gpointer)&node);
444 std::map<std::string, std::shared_ptr<package_x>> pkgs;
446 node.value = const_cast<char*>(info->package);
447 internal::GetPkgInfo(db, &tmp_filter, uid_, locale, pkgs);
448 node.prop = E_PMINFO_APPINFO_PROP_APP_PACKAGE;
449 for (auto& [pkgid, pkg_handle] : pkgs) {
450 node.value = const_cast<char*>(pkgid.c_str());
451 std::vector<std::shared_ptr<application_x>> app_list;
452 internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
454 for (auto& app : app_list) {
455 app->privileges = pkg_handle->privileges;
456 pending_app_[pkgid].emplace_back(std::move(app));
459 pending_pkg_[pkgid] = std::make_pair(pid, std::move(pkg_handle));
462 g_slist_free(tmp_filter.list);
464 pending_pkg_[info->package] = std::make_pair(pid, nullptr);
465 pending_app_.erase(info->package);
469 void DBHandleProvider::UpdatePendingPackageInfo(
470 const std::vector<std::string>& pkgids) {
471 for (const auto& pkgid : pkgids) {
472 auto pkg_it = pending_pkg_.find(pkgid);
473 if (pkg_it == pending_pkg_.end())
477 auto app_it = pending_app_.find(pkgid);
478 if (app_it != pending_app_.end()) {
479 for (auto& app : app_it->second)
480 AddApplication(std::move(app));
481 pending_app_.erase(app_it);
484 if (pkg_it->second.second)
485 AddPackage(std::move(pkg_it->second.second));
487 EraseWriterPID(pkg_it->second.first);
488 pending_pkg_.erase(pkg_it);
492 void DBHandleProvider::UpdateCrashedWriterPackageInfo(
493 const std::unordered_set<pid_t>& pids) {
494 for (auto it = pending_pkg_.begin(); it != pending_pkg_.end();) {
495 pid_t pid = it->second.first;
496 if (pids.find(pid) == pids.end()) {
501 const auto& pkgid = it->first;
502 auto old_app_it = pkg_app_map_.find(pkgid);
503 if (old_app_it != pkg_app_map_.end()) {
504 for (const auto& app : old_app_it->second)
507 pkg_app_map_.erase(old_app_it);
510 auto app_it = pending_app_.find(pkgid);
511 if (app_it != pending_app_.end()) {
512 for (auto& app : app_it->second)
513 AddApplication(std::move(app));
514 pending_app_.erase(app_it);
517 if (it->second.second)
518 AddPackage(std::move(it->second.second));
520 EraseWriterPID(it->second.first);
521 it = pending_pkg_.erase(it);
525 bool DBHandleProvider::UpdateCachePkg(const tizen_base::Database& db, uid_t uid,
526 const std::string& pkgid, const std::string& locale) {
527 pkgmgrinfo_filter_x tmp_filter = { 0, };
528 pkgmgrinfo_node_x node = {
529 .prop = E_PMINFO_PKGINFO_PROP_PACKAGE_ID,
530 .value = const_cast<char*>(pkgid.c_str())
532 tmp_filter.cache_flag = true;
533 tmp_filter.list = g_slist_append(tmp_filter.list, (gpointer)&node);
534 std::map<std::string, std::shared_ptr<package_x>> pkgs;
535 internal::GetPkgInfo(db, &tmp_filter, uid_, locale, pkgs);
536 for (auto& [key, val] : pkgs) {
538 for (auto& appid : pkg_app_map_[key])
539 app_map_.erase(appid);
541 pkg_app_map_.erase(key);
542 std::vector<std::shared_ptr<application_x>> app_list;
543 node.prop = E_PMINFO_APPINFO_PROP_APP_PACKAGE;
544 node.value = const_cast<char*>(key.c_str());
545 internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
546 for (auto& app : app_list) {
547 app->privileges = val->privileges;
548 AddApplication(std::move(app));
555 bool DBHandleProvider::UpdateCacheApp(const tizen_base::Database& db, uid_t uid,
556 const std::string& appid, const std::string& locale) {
557 pkgmgrinfo_filter_x tmp_filter = { 0, };
558 pkgmgrinfo_node_x node = {
559 .prop = E_PMINFO_APPINFO_PROP_APP_ID,
560 .value = const_cast<char*>(appid.c_str())
562 tmp_filter.cache_flag = true;
563 tmp_filter.list = g_slist_append(tmp_filter.list, (gpointer)&node);
565 std::vector<std::shared_ptr<application_x>> app_list;
566 app_map_.erase(appid);
567 internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
568 g_slist_free(tmp_filter.list);
570 for (auto& app : app_list) {
571 auto it = pkg_map_.find(app->package);
572 if (it == pkg_map_.end()) {
573 LOG(ERROR) << "Can not find package from pkg_map";
577 pkg_app_map_[app->package].erase(app->appid);
578 app->privileges = it->second->privileges;
579 AddApplication(std::move(app));
585 bool DBHandleProvider::UpdateCacheAppByPkgid(const tizen_base::Database& db,
586 uid_t uid, const std::string& pkgid, const std::string& locale) {
587 auto it = pkg_map_.find(pkgid);
588 if (it == pkg_map_.end()) {
589 LOG(ERROR) << "Can not find package from pkg_map";
593 auto& pkg = it->second;
594 pkgmgrinfo_filter_x tmp_filter = { 0, };
595 pkgmgrinfo_node_x node = {
596 .prop = E_PMINFO_APPINFO_PROP_APP_PACKAGE,
597 .value = pkg->package
600 tmp_filter.cache_flag = true;
601 tmp_filter.list = g_slist_append(tmp_filter.list, (gpointer)&node);
603 std::vector<std::shared_ptr<application_x>> app_list;
604 internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
606 for (auto& appid : pkg_app_map_[pkgid]) {
607 app_map_.erase(appid);
609 pkg_app_map_.erase(pkgid);
611 for (auto& app : app_list) {
612 app->privileges = pkg->privileges;
613 AddApplication(std::move(app));
616 g_slist_free(tmp_filter.list);
620 } // namespace database
621 } // namespace pkgmgr_server