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 #ifndef SERVER_DB_HANDLE_PROVIDER_HH_
18 #define SERVER_DB_HANDLE_PROVIDER_HH_
22 #include <sys/types.h>
27 #include <unordered_map>
28 #include <unordered_set>
31 #include "filter_checker_provider.hh"
32 #include "pkgmgrinfo_basic.h"
33 #include "pkgmgrinfo_private.h"
34 #include "shared_object.hh"
36 namespace pkgmgr_server {
40 #define EXPORT_API __attribute__((visibility("default")))
43 class EXPORT_API DBHandleProvider {
45 ~DBHandleProvider() = default;
46 static DBHandleProvider& GetInst(uid_t uid);
47 static bool IsCrashedWriteRequest();
48 std::vector<std::pair<std::string, uid_t>> GetParserDBPath(int pid,
50 std::string GetCertDBPath(int pid, bool write);
51 void SetMemoryMode(pid_t pid);
52 void UnsetMemoryMode(pid_t pid);
53 int UpdateCache(sqlite3* db, pid_t pid, uid_t uid, bool write,
54 const std::string& locale);
55 std::vector<std::shared_ptr<package_x>> GetPackages(
56 pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
57 const std::string& package);
58 std::vector<std::shared_ptr<application_x>> GetApplications(
59 pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
60 const std::string& app);
62 bool IsWriter(pid_t pid);
65 explicit DBHandleProvider(uid_t uid);
66 sqlite3* CreateMemoryDBHandle(const std::string& filedb_path,
67 const std::string& memorydb_path);
68 bool IsMemoryDBActive(pid_t pid, bool write);
70 void AddPackage(std::string package, package_x* info);
71 void AddApplication(std::string app, std::shared_ptr<application_x> info);
72 void InsertPID(pid_t pid);
73 bool ErasePID(pid_t pid);
76 static std::unordered_map<uid_t,
77 std::unique_ptr<DBHandleProvider>> provider_map_;
78 static bool is_global_memdb_set_;
79 static std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
80 global_parser_memdb_handle_;
81 static std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
83 static std::string global_parser_filedb_path_;
84 static std::string cert_filedb_path_;
85 static std::unordered_set<pid_t> writer_pid_list_;
86 static std::recursive_mutex lock_;
87 static std::mutex pid_list_lock_;
90 bool is_user_memdb_set_;
91 std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
93 std::string user_parser_memdb_path_;
94 std::string user_parser_filedb_path_;
95 bool released_ = true;
96 std::unordered_map<std::string, std::vector<std::shared_ptr<package_x>>>
98 std::unordered_map<std::string, std::vector<std::shared_ptr<application_x>>>
102 } // namespace database
103 } // namespace pkgmgr_server
105 #endif // SERVER_DB_HANDLE_PROVIDER_HH_