1 /*
2 * Copyright (c) 2023-2025 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 #include "module_loader.h"
16
17 #include <dlfcn.h>
18
19 #include "app_event_processor_proxy.h"
20 #include "file_util.h"
21 #include "hiappevent_base.h"
22 #include "hilog/log.h"
23
24 #undef LOG_DOMAIN
25 #define LOG_DOMAIN 0xD002D07
26
27 #undef LOG_TAG
28 #define LOG_TAG "ModuleLoader"
29
30 namespace OHOS {
31 namespace HiviewDFX {
32 namespace HiAppEvent {
33 namespace {
GetModulePath(const std::string & moduleName)34 std::string GetModulePath(const std::string& moduleName)
35 {
36 const std::string searchDirs[] = {
37 "/system/lib/platformsdk/", "/system/lib64/platformsdk/", "/system/lib/", "/system/lib64/"
38 };
39 std::string modulePath;
40 std::string libName = "lib" + moduleName + ".z.so";
41 for (auto& searchDir : searchDirs) {
42 std::string tempModulePath = searchDir + libName;
43 if (FileUtil::IsFileExists(tempModulePath)) {
44 modulePath = tempModulePath;
45 break;
46 }
47 }
48 return modulePath;
49 }
50 }
51
~ModuleLoader()52 ModuleLoader::~ModuleLoader()
53 {
54 processors_.clear();
55
56 for (auto it = modules_.begin(); it != modules_.end(); ++it) {
57 dlclose(it->second);
58 HILOG_INFO(LOG_CORE, "succ to unload module=%{public}s", it->first.c_str());
59 }
60 modules_.clear();
61 }
62
Load(const std::string & moduleName)63 int ModuleLoader::Load(const std::string& moduleName)
64 {
65 std::lock_guard<std::mutex> lock(moduleMutex_);
66 if (modules_.find(moduleName) != modules_.end()) {
67 HILOG_DEBUG(LOG_CORE, "the module=%{public}s already exists", moduleName.c_str());
68 return 0;
69 }
70
71 std::string modulePath = GetModulePath(moduleName);
72 if (modulePath.empty()) {
73 HILOG_WARN(LOG_CORE, "the module=%{public}s does not exist.", moduleName.c_str());
74 return -1;
75 }
76 void* handler = nullptr;
77 if (handler = dlopen(modulePath.c_str(), RTLD_GLOBAL); handler == nullptr) {
78 HILOG_ERROR(LOG_CORE, "failed to load module=%{public}s, error=%{public}s.", modulePath.c_str(), dlerror());
79 return -1;
80 }
81 HILOG_INFO(LOG_CORE, "succ to load module=%{public}s.", modulePath.c_str());
82 modules_[moduleName] = handler;
83 return 0;
84 }
85
Unload(const std::string & moduleName)86 int ModuleLoader::Unload(const std::string& moduleName)
87 {
88 std::lock_guard<std::mutex> lock(moduleMutex_);
89 if (modules_.find(moduleName) == modules_.end()) {
90 HILOG_WARN(LOG_CORE, "the module=%{public}s does not exists", moduleName.c_str());
91 return -1;
92 }
93 dlclose(modules_[moduleName]);
94 modules_.erase(moduleName);
95 HILOG_INFO(LOG_CORE, "succ to unload module=%{public}s", moduleName.c_str());
96 return 0;
97 }
98
RegisterProcessor(const std::string & name,std::shared_ptr<AppEventProcessor> processor)99 int ModuleLoader::RegisterProcessor(const std::string& name, std::shared_ptr<AppEventProcessor> processor)
100 {
101 if (name.empty() || processor == nullptr) {
102 HILOG_WARN(LOG_CORE, "the name or processor is invalid");
103 return -1;
104 }
105 std::lock_guard<std::mutex> lock(processorMutex_);
106 if (processors_.find(name) != processors_.end()) {
107 HILOG_WARN(LOG_CORE, "the processor already exists");
108 return -1;
109 }
110 processors_[name] = processor;
111 return 0;
112 }
113
UnregisterProcessor(const std::string & name)114 int ModuleLoader::UnregisterProcessor(const std::string& name)
115 {
116 std::lock_guard<std::mutex> lock(processorMutex_);
117 if (processors_.find(name) == processors_.end()) {
118 HILOG_WARN(LOG_CORE, "the name is invalid");
119 return -1;
120 }
121 processors_.erase(name);
122 return 0;
123 }
124
CreateProcessorProxy(const std::string & name)125 std::shared_ptr<AppEventProcessorProxy> ModuleLoader::CreateProcessorProxy(const std::string& name)
126 {
127 std::lock_guard<std::mutex> lock(processorMutex_);
128 if (processors_.find(name) == processors_.end()) {
129 HILOG_WARN(LOG_CORE, "the name is invalid");
130 return nullptr;
131 }
132 return std::make_shared<AppEventProcessorProxy>(name, processors_[name]);
133 }
134 } // namespace HiAppEvent
135 } // namespace HiviewDFX
136 } // namespace OHOS
137