Cleanup db handlers and db handle provider
[platform/core/appfw/pkgmgr-info.git] / src / server / database / db_handle_provider.hh
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 #ifndef SERVER_DB_HANDLE_PROVIDER_HH_
18 #define SERVER_DB_HANDLE_PROVIDER_HH_
19
20 #include <sqlite3.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23
24 #include <string>
25 #include <memory>
26 #include <mutex>
27 #include <unordered_map>
28 #include <unordered_set>
29 #include <vector>
30
31 #include "shared_object.hh"
32
33 namespace pkgmgr_server {
34 namespace database {
35
36 #ifndef EXPORT_API
37 #define EXPORT_API __attribute__((visibility("default")))
38 #endif
39
40 class EXPORT_API DBHandleProvider {
41  public:
42   ~DBHandleProvider() = default;
43   static DBHandleProvider& GetInst(uid_t uid);
44   static bool IsCrashedWriteRequest();
45   std::vector<std::pair<std::string, uid_t>> GetParserDBPath(
46       int pid, bool write);
47   std::string GetCertDBPath(int pid, bool write);
48   void SetMemoryMode(pid_t pid, bool flag);
49
50  private:
51   DBHandleProvider(uid_t uid);
52   sqlite3* GetMemoryDBHandle(const std::string& filedb_path,
53       const std::string& memorydb_path);
54
55  private:
56   static std::unordered_map<uid_t,
57       std::unique_ptr<DBHandleProvider>> provider_;
58   static bool is_memory_global_;
59   static std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
60       global_parser_memory_db_handle_;
61   static std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
62       cert_memory_db_handle_;
63   static std::string global_parser_memory_db_path_;
64   static std::string global_parser_file_db_path_;
65   static std::string cert_memory_db_path_;
66   static std::string cert_file_db_path_;
67   static std::unordered_set<pid_t> pid_list_;
68   static std::recursive_mutex lock_;
69
70   uid_t uid_;
71   bool is_memory_;
72   std::unique_ptr<sqlite3,
73       decltype(sqlite3_close_v2)*> parser_memory_db_handle_;
74   std::string parser_memory_db_path_;
75   std::string parser_file_db_path_;
76 };
77
78 }  // namespace database
79 }  // namespace pkgmgr_server
80
81 #endif  // SERVER_DB_HANDLE_PROVIDER_HH_