Release version 0.26.11
[platform/core/appfw/pkgmgr-info.git] / src / server / database / db_handle_provider.cc
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include "db_handle_provider.hh"
18
19 #include <fcntl.h>
20 #include <glib.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23
24 #include <tzplatform_config.h>
25
26 #include <algorithm>
27 #include <string>
28 #include <vector>
29
30 #include "cache_flag.hh"
31 #include "utils/logging.hh"
32
33 #include "pkgmgr-info.h"
34 #include "pkgmgrinfo_debug.h"
35 #include "pkgmgrinfo_internal.h"
36 #include "pkgmgrinfo_private.h"
37
38 #ifdef LOG_TAG
39 #undef LOG_TAG
40 #endif
41 #define LOG_TAG "PKGMGR_INFO"
42
43 namespace {
44
45 uid_t globaluser_uid = -1;
46
47 uid_t GetGlobalUID() {
48   if (globaluser_uid == (uid_t)-1)
49     globaluser_uid = tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
50
51   return globaluser_uid;
52 }
53
54 uid_t ConvertUID(uid_t uid) {
55   if (uid < REGULAR_USER)
56     return GetGlobalUID();
57   else
58     return uid;
59 }
60
61 bool GetModifiedTime(const char* dbpath, timespec* t) {
62   if (dbpath == nullptr || t == nullptr) {
63     LOG(ERROR) << "Invalid argument";
64     return false;
65   }
66
67   struct stat attr;
68   if (stat(dbpath, &attr)) {
69     LOG(ERROR) << "Fail to get status from file "
70         << dbpath << " errno : " << errno;
71     return false;
72   }
73
74   *t = attr.st_mtim;
75
76   return true;
77 }
78
79 }  // namespace
80
81 namespace pkgmgr_server {
82 namespace database {
83
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_;
90
91 DBHandleProvider::DBHandleProvider(uid_t uid) : uid_(uid) {
92   char* tmp_path;
93
94   if (global_parser_filedb_path_.empty()) {
95     tmp_path = getUserPkgParserDBPathUID(GetGlobalUID());
96     global_parser_filedb_path_ = tmp_path;
97     free(tmp_path);
98   }
99
100   if (cert_filedb_path_.empty()) {
101     tmp_path = getUserPkgCertDBPath();
102     cert_filedb_path_ = tmp_path;
103     free(tmp_path);
104   }
105
106   tmp_path = getUserPkgParserDBPathUID(uid_);
107   user_parser_filedb_path_ = tmp_path;
108   free(tmp_path);
109 }
110
111 DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
112   static std::shared_mutex singleton_lock_;
113   std::shared_lock<std::shared_mutex> s(singleton_lock_);
114
115   uid = ConvertUID(uid);
116   auto it = provider_map_.find(uid);
117   if (it != provider_map_.end())
118     return *(it->second);
119
120   s.unlock();
121   std::unique_lock<std::shared_mutex> u(singleton_lock_);
122   auto& prov = provider_map_[uid];
123   if (prov == nullptr)
124     prov.reset(new DBHandleProvider(uid));
125
126   return *prov;
127 }
128
129 std::unordered_set<pid_t> DBHandleProvider::CrashedWriteRequestPIDs() {
130   std::unique_lock<std::shared_mutex> u(pid_list_lock_);
131
132   if (writer_pid_list_.empty())
133     return {};
134
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";
139
140     int fd = open(status_path.c_str(), O_RDONLY);
141     if (fd < 0) {
142       LOG(ERROR) << "Process is crashed : " << pid;
143       remove_pids.emplace(pid);
144     } else {
145       close(fd);
146     }
147   }
148
149   return remove_pids;
150 }
151
152 std::vector<std::pair<std::string, uid_t>> DBHandleProvider::GetParserDBPath() {
153   std::vector<std::pair<std::string, uid_t>> db_path_list;
154
155   if (uid_ > REGULAR_USER)
156     db_path_list.emplace_back(std::make_pair(user_parser_filedb_path_, uid_));
157
158   db_path_list.emplace_back(
159       std::make_pair(global_parser_filedb_path_, GetGlobalUID()));
160
161   if (db_path_list.size() == 1) {
162     LOG(DEBUG) << "global db path : " << db_path_list[0].first;
163   } else {
164     LOG(DEBUG) << "local db path : " << db_path_list[0].first;
165     LOG(DEBUG) << "global db path : " << db_path_list[1].first;
166   }
167
168   return db_path_list;
169 }
170
171 std::string DBHandleProvider::GetCertDBPath() {
172   return cert_filedb_path_;
173 }
174
175 void DBHandleProvider::TrimCache() {
176   if (!released_)
177     ReleaseCache();
178 }
179
180 void DBHandleProvider::ReleaseCache() {
181   app_map_.clear();
182   pkg_map_.clear();
183   pending_pkg_.clear();
184   pkg_app_map_.clear();
185
186   released_ = true;
187 }
188
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();
192 }
193
194 int DBHandleProvider::UpdateCache(const tizen_base::Database& db, pid_t pid,
195     uid_t uid, bool write, const std::string& locale) {
196   pkg_map_.clear();
197   app_map_.clear();
198   pending_pkg_.clear();
199   pkg_app_map_.clear();
200
201   const char* dbpath = sqlite3_db_filename(db.GetRaw(), "main");
202
203   timespec start_time = { 0, };
204   timespec end_time = { 0, };
205   if (!GetModifiedTime(dbpath, &start_time))
206     return PMINFO_R_ERROR;
207
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));
215   }
216
217   if (!GetModifiedTime(dbpath, &end_time))
218     return PMINFO_R_ERROR;
219
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;
224   }
225
226   std::vector<std::shared_ptr<application_x>> app_list;
227   ret = internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
228
229   if (!GetModifiedTime(dbpath, &end_time))
230     return PMINFO_R_ERROR;
231
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;
236   }
237
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;
244       }
245
246       app->privileges = it->second->privileges;
247       AddApplication(std::move(app));
248     }
249   }
250
251   released_ = false;
252
253   return ret;
254 }
255
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())
263         continue;
264
265       if (metadata->second.empty() ||
266           strcmp(node->value ? node->value : "", metadata->second.c_str()) == 0)
267         return true;
268     }
269   }
270   return false;
271 }
272
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()))
281       return false;
282   }
283
284   bool pass = true;
285   if (!metadata_map.empty())
286     pass = CheckMetadataFilter(info->metadata, metadata_map);
287
288   return pass;
289 }
290
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;
295
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)
302       continue;
303     LOG(ERROR) << "add metadata filter";
304     metadata_map[node->key] = (node->value ? node->value : "");
305   }
306
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";
311       return {};
312     }
313   }
314   if (package.empty()) {
315     for (auto& info : pkg_map_) {
316       if (CheckPkgFilters(filter, info.second, metadata_map))
317         ret.push_back(info.second);
318     }
319   } else {
320     auto map_it = pkg_map_.find(package);
321     if (map_it != pkg_map_.end() && CheckPkgFilters(filter, map_it->second,
322         metadata_map))
323       ret.push_back(map_it->second);
324   }
325
326   return ret;
327 }
328
329 void DBHandleProvider::AddPackage(std::shared_ptr<package_x> info) {
330   pkg_map_[info->package] = std::move(info);
331 }
332
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));
338 }
339
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)
344       app_map_.erase(app);
345     pkg_app_map_.erase(it);
346   }
347
348   pkg_map_.erase(pkgid);
349 }
350
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()))
360       return false;
361   }
362
363   bool pass = true;
364   if (!metadata_map.empty())
365     pass = CheckMetadataFilter(app_info->metadata, metadata_map);
366   return pass;
367 }
368
369 std::shared_ptr<package_x> DBHandleProvider::GetPackageByApp(
370     const char* appid) {
371   auto it = pkg_map_.find(appid);
372   if (it == pkg_map_.end())
373     return nullptr;
374
375   return it->second;
376 }
377
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)
386       continue;
387
388     metadata_map[node->key] = (node->value ? node->value : "");
389   }
390
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";
396       return {};
397     }
398   }
399
400   if (app.empty()) {
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);
405     }
406   } else {
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);
412   }
413
414   return ret;
415 }
416
417 void DBHandleProvider::InsertWriterPID(pid_t pid) {
418   std::unique_lock<std::shared_mutex> u(pid_list_lock_);
419
420   writer_pid_list_.insert(pid);
421 }
422
423 bool DBHandleProvider::EraseWriterPID(pid_t pid) {
424   std::unique_lock<std::shared_mutex> u(pid_list_lock_);
425
426   return writer_pid_list_.erase(pid) == 1;
427 }
428
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)
433     return;
434
435   InsertWriterPID(pid);
436
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
441     };
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;
445
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);
453
454       for (auto& app : app_list)
455         app->privileges = pkg_handle->privileges;
456
457       pending_pkg_[pkgid] = pid;
458     }
459
460     g_slist_free(tmp_filter.list);
461   } else {
462     pending_pkg_[info->package] = pid;
463   }
464 }
465
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())
476       continue;
477
478     LOG(WARNING) << "Update package : " << pkgid;
479
480     ErasePackage(pkgid);
481     std::map<std::string, std::shared_ptr<package_x>> pkgs;
482
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);
490
491       for (auto& app : app_list) {
492         app->privileges = pkg_handle->privileges;
493         AddApplication(std::move(app));
494       }
495
496       AddPackage(std::move(pkg_handle));
497     }
498
499     EraseWriterPID(pkg_it->second);
500     pending_pkg_.erase(pkg_it);
501   }
502
503   g_slist_free(tmp_filter.list);
504 }
505
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())
513       continue;
514
515     EraseWriterPID(pid);
516     pkgids.emplace_back(pkgid);
517   }
518
519   UpdatePendingPackageInfo(db, uid, locale, pkgids);
520 }
521
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())
528   };
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) {
534     AddPackage(val);
535     for (auto& appid : pkg_app_map_[key])
536       app_map_.erase(appid);
537
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));
546     }
547   }
548
549   return true;
550 }
551
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())
558   };
559   tmp_filter.cache_flag = true;
560   tmp_filter.list = g_slist_append(tmp_filter.list, (gpointer)&node);
561
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);
566
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";
571       return false;
572     }
573
574     pkg_app_map_[app->package].erase(app->appid);
575     app->privileges = it->second->privileges;
576     AddApplication(std::move(app));
577   }
578
579   return true;
580 }
581
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";
587     return false;
588   }
589
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
595   };
596
597   tmp_filter.cache_flag = true;
598   tmp_filter.list = g_slist_append(tmp_filter.list, (gpointer)&node);
599
600   std::vector<std::shared_ptr<application_x>> app_list;
601   internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
602
603   for (auto& appid : pkg_app_map_[pkgid]) {
604     app_map_.erase(appid);
605   }
606   pkg_app_map_.erase(pkgid);
607
608   for (auto& app : app_list) {
609     app->privileges = pkg->privileges;
610     AddApplication(std::move(app));
611   }
612
613   g_slist_free(tmp_filter.list);
614   return true;
615 }
616
617 }  // namespace database
618 }  // namespace pkgmgr_server