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;
457 pending_pkg_[pkgid] = pid;
460 g_slist_free(tmp_filter.list);
462 pending_pkg_[info->package] = pid;
466 void DBHandleProvider::UpdatePendingPackageInfo(const tizen_base::Database& db,
467 uid_t uid, const std::string& locale,
468 const std::vector<std::string>& pkgids) {
469 pkgmgrinfo_filter_x tmp_filter = { 0, };
470 pkgmgrinfo_node_x node = { 0, };
471 tmp_filter.cache_flag = true;
472 tmp_filter.list = g_slist_append(tmp_filter.list, (gpointer)&node);
473 for (const auto& pkgid : pkgids) {
474 auto pkg_it = pending_pkg_.find(pkgid);
475 if (pkg_it == pending_pkg_.end())
478 LOG(WARNING) << "Update package : " << pkgid;
481 std::map<std::string, std::shared_ptr<package_x>> pkgs;
483 node.prop = E_PMINFO_PKGINFO_PROP_PACKAGE_ID;
484 node.value = const_cast<char*>(pkgid.c_str());
485 internal::GetPkgInfo(db, &tmp_filter, uid_, locale, pkgs);
486 node.prop = E_PMINFO_APPINFO_PROP_APP_PACKAGE;
487 for (auto& [pkgid, pkg_handle] : pkgs) {
488 std::vector<std::shared_ptr<application_x>> app_list;
489 internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
491 for (auto& app : app_list) {
492 app->privileges = pkg_handle->privileges;
493 AddApplication(std::move(app));
496 AddPackage(std::move(pkg_handle));
499 EraseWriterPID(pkg_it->second);
500 pending_pkg_.erase(pkg_it);
503 g_slist_free(tmp_filter.list);
506 void DBHandleProvider::UpdateCrashedWriterPackageInfo(
507 const tizen_base::Database& db,
508 uid_t uid, const std::string& locale,
509 const std::unordered_set<pid_t>& pids) {
510 std::vector<std::string> pkgids;
511 for (const auto& [pkgid, pid] : pending_pkg_) {
512 if (pids.find(pid) == pids.end())
516 pkgids.emplace_back(pkgid);
519 UpdatePendingPackageInfo(db, uid, locale, pkgids);
522 bool DBHandleProvider::UpdateCachePkg(const tizen_base::Database& db, uid_t uid,
523 const std::string& pkgid, const std::string& locale) {
524 pkgmgrinfo_filter_x tmp_filter = { 0, };
525 pkgmgrinfo_node_x node = {
526 .prop = E_PMINFO_PKGINFO_PROP_PACKAGE_ID,
527 .value = const_cast<char*>(pkgid.c_str())
529 tmp_filter.cache_flag = true;
530 tmp_filter.list = g_slist_append(tmp_filter.list, (gpointer)&node);
531 std::map<std::string, std::shared_ptr<package_x>> pkgs;
532 internal::GetPkgInfo(db, &tmp_filter, uid_, locale, pkgs);
533 for (auto& [key, val] : pkgs) {
535 for (auto& appid : pkg_app_map_[key])
536 app_map_.erase(appid);
538 pkg_app_map_.erase(key);
539 std::vector<std::shared_ptr<application_x>> app_list;
540 node.prop = E_PMINFO_APPINFO_PROP_APP_PACKAGE;
541 node.value = const_cast<char*>(key.c_str());
542 internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
543 for (auto& app : app_list) {
544 app->privileges = val->privileges;
545 AddApplication(std::move(app));
552 bool DBHandleProvider::UpdateCacheApp(const tizen_base::Database& db, uid_t uid,
553 const std::string& appid, const std::string& locale) {
554 pkgmgrinfo_filter_x tmp_filter = { 0, };
555 pkgmgrinfo_node_x node = {
556 .prop = E_PMINFO_APPINFO_PROP_APP_ID,
557 .value = const_cast<char*>(appid.c_str())
559 tmp_filter.cache_flag = true;
560 tmp_filter.list = g_slist_append(tmp_filter.list, (gpointer)&node);
562 std::vector<std::shared_ptr<application_x>> app_list;
563 app_map_.erase(appid);
564 internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
565 g_slist_free(tmp_filter.list);
567 for (auto& app : app_list) {
568 auto it = pkg_map_.find(app->package);
569 if (it == pkg_map_.end()) {
570 LOG(ERROR) << "Can not find package from pkg_map";
574 pkg_app_map_[app->package].erase(app->appid);
575 app->privileges = it->second->privileges;
576 AddApplication(std::move(app));
582 bool DBHandleProvider::UpdateCacheAppByPkgid(const tizen_base::Database& db,
583 uid_t uid, const std::string& pkgid, const std::string& locale) {
584 auto it = pkg_map_.find(pkgid);
585 if (it == pkg_map_.end()) {
586 LOG(ERROR) << "Can not find package from pkg_map";
590 auto& pkg = it->second;
591 pkgmgrinfo_filter_x tmp_filter = { 0, };
592 pkgmgrinfo_node_x node = {
593 .prop = E_PMINFO_APPINFO_PROP_APP_PACKAGE,
594 .value = pkg->package
597 tmp_filter.cache_flag = true;
598 tmp_filter.list = g_slist_append(tmp_filter.list, (gpointer)&node);
600 std::vector<std::shared_ptr<application_x>> app_list;
601 internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
603 for (auto& appid : pkg_app_map_[pkgid]) {
604 app_map_.erase(appid);
606 pkg_app_map_.erase(pkgid);
608 for (auto& app : app_list) {
609 app->privileges = pkg->privileges;
610 AddApplication(std::move(app));
613 g_slist_free(tmp_filter.list);
617 } // namespace database
618 } // namespace pkgmgr_server