• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "database_adapter_loader.h"
17 
18 #include <dlfcn.h>
19 #include <thread>
20 #include "account_log_wrapper.h"
21 #include "account_hisysevent_adapter.h"
22 #include "event_handler.h"
23 #include "event_runner.h"
24 
25 namespace OHOS {
26 namespace AccountSA {
27 namespace {
28 const uint32_t DLCLOSE_SLEEP_TIME = 100;
29 };
GetInstance()30 DatabaseAdapterLoader &DatabaseAdapterLoader::GetInstance()
31 {
32     static DatabaseAdapterLoader instance;
33     return instance;
34 }
35 
~DatabaseAdapterLoader()36 DatabaseAdapterLoader::~DatabaseAdapterLoader()
37 {
38     CheckAndUnload();
39 }
40 
CheckAndUnload()41 bool DatabaseAdapterLoader::CheckAndUnload()
42 {
43     std::lock_guard<std::mutex> lock(loadMutex_);
44     if (handle_ == nullptr) {
45         ACCOUNT_LOGW("DatabaseAdapterLoader has not been loaded");
46     } else {
47         int32_t err = dlclose(handle_);
48         if (err != ERR_OK) {
49             char* errMsg = dlerror();
50             ACCOUNT_LOGE("Dlclose failed, err: %{public}d, errmsg: %{public}s", err, errMsg);
51             return false;
52         }
53         handle_ = nullptr;
54         std::this_thread::sleep_for(std::chrono::milliseconds(DLCLOSE_SLEEP_TIME));
55     }
56     createFunc_ = nullptr;
57     destroyFunc_ = nullptr;
58     return true;
59 }
60 
CheckAndLoad()61 bool DatabaseAdapterLoader::CheckAndLoad()
62 {
63     std::lock_guard<std::mutex> lock(loadMutex_);
64     if ((handle_ != nullptr) && (createFunc_ != nullptr) && (destroyFunc_ != nullptr)) {
65         return true;
66     }
67 
68     if (handle_ == nullptr) {
69         handle_ = dlopen("libaccount_database_adapter.z.so", RTLD_LAZY);
70         if (handle_ == nullptr) {
71             char* errMsg = dlerror();
72             ACCOUNT_LOGE("Load libaccount_database_adapter.z.so failed, errMsg: %{public}s", errMsg);
73             return false;
74         }
75     }
76     createFunc_ = reinterpret_cast<FUNC_CREATE>(dlsym(handle_, "CreateDataManager"));
77     if (createFunc_ == nullptr) {
78         char* errMsg = dlerror();
79         ACCOUNT_LOGE("Get createDataManager failed, errMsg: %{public}s", errMsg);
80         return false;
81     }
82     destroyFunc_ = reinterpret_cast<FUNC_DESTROY>(dlsym(handle_, "DestroyDataManager"));
83     if (destroyFunc_ == nullptr) {
84         char* errMsg = dlerror();
85         ACCOUNT_LOGE("Get destroyDataManager failed, errMsg: %{public}s", errMsg);
86         return false;
87     }
88     return true;
89 }
90 
GetDataManager()91 std::shared_ptr<IDbAdapterDataManager> DatabaseAdapterLoader::GetDataManager()
92 {
93     if (!CheckAndLoad()) {
94         ACCOUNT_LOGE("Load kvstore adapter failed.");
95         return nullptr;
96     }
97     std::shared_ptr<IDbAdapterDataManager> dataManager(
98         createFunc_(),
99         [](IDbAdapterDataManager *ptr) {DatabaseAdapterLoader::GetInstance().DestroyDataManager(ptr);});
100     return dataManager;
101 }
102 
DestroyDataManager(IDbAdapterDataManager * dataManager)103 void DatabaseAdapterLoader::DestroyDataManager(IDbAdapterDataManager* dataManager)
104 {
105     std::lock_guard<std::mutex> lock(loadMutex_);
106     if (destroyFunc_ == nullptr) {
107         ACCOUNT_LOGE("Destroy func is null, destroy failed.");
108         return;
109     }
110     destroyFunc_(dataManager);
111 }
112 } // namespace AccountSA
113 } // namespace OHOS