• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 namespace OHOS {
25 namespace HiviewDFX {
26 namespace HiAppEvent {
27 namespace {
28 constexpr HiLogLabel LABEL = { LOG_CORE, HIAPPEVENT_DOMAIN, "HiAppEvent_ModuleLoader" };
29 
GetModulePath(const std::string & moduleName)30 std::string GetModulePath(const std::string& moduleName)
31 {
32     const std::string searchDirs[] = {
33         "/system/lib/", "/system/lib64/"
34     };
35     std::string modulePath;
36     std::string libName = "lib" + moduleName + ".z.so";
37     for (auto& searchDir : searchDirs) {
38         std::string tempModulePath = searchDir + libName;
39         if (FileUtil::IsFileExists(tempModulePath)) {
40             modulePath = tempModulePath;
41             break;
42         }
43     }
44     return modulePath;
45 }
46 }
47 std::mutex ModuleLoader::instanceMutex_;
48 
GetInstance()49 ModuleLoader& ModuleLoader::GetInstance()
50 {
51     std::lock_guard<std::mutex> lock(instanceMutex_);
52     static ModuleLoader instance;
53     return instance;
54 }
55 
~ModuleLoader()56 ModuleLoader::~ModuleLoader()
57 {
58     processors_.clear();
59 
60     for (auto it = modules_.begin(); it != modules_.end(); ++it) {
61         dlclose(it->second);
62         HiLog::Info(LABEL, "succ to unload module=%{public}s", it->first.c_str());
63     }
64     modules_.clear();
65 }
66 
Load(const std::string & moduleName)67 int ModuleLoader::Load(const std::string& moduleName)
68 {
69     if (modules_.find(moduleName) != modules_.end()) {
70         HiLog::Info(LABEL, "the module=%{public}s already exists", moduleName.c_str());
71         return 0;
72     }
73 
74     std::string modulePath = GetModulePath(moduleName);
75     if (modulePath.empty()) {
76         HiLog::Warn(LABEL, "the module=%{public}s does not exist.", moduleName.c_str());
77         return -1;
78     }
79     void* handler = nullptr;
80     if (handler = dlopen(modulePath.c_str(), RTLD_GLOBAL); handler == nullptr) {
81         HiLog::Error(LABEL, "failed to load module=%{public}s, error=%{public}s.", modulePath.c_str(), dlerror());
82         return -1;
83     }
84     HiLog::Info(LABEL, "succ to load module=%{public}s.", modulePath.c_str());
85     modules_[moduleName] = handler;
86     return 0;
87 }
88 
Unload(const std::string & moduleName)89 int ModuleLoader::Unload(const std::string& moduleName)
90 {
91     if (modules_.find(moduleName) == modules_.end()) {
92         HiLog::Warn(LABEL, "the module=%{public}s does not exists", moduleName.c_str());
93         return -1;
94     }
95     dlclose(modules_[moduleName]);
96     modules_.erase(moduleName);
97     HiLog::Info(LABEL, "succ to unload module=%{public}s", moduleName.c_str());
98     return 0;
99 }
100 
RegisterProcessor(const std::string & name,std::shared_ptr<AppEventProcessor> processor)101 int ModuleLoader::RegisterProcessor(const std::string& name, std::shared_ptr<AppEventProcessor> processor)
102 {
103     if (name.empty() || processor == nullptr) {
104         HiLog::Warn(LABEL, "the name or processor is invalid");
105         return -1;
106     }
107     if (processors_.find(name) != processors_.end()) {
108         HiLog::Warn(LABEL, "the processor already exists");
109         return -1;
110     }
111     processors_[name] = processor;
112     return 0;
113 }
114 
UnregisterProcessor(const std::string & name)115 int ModuleLoader::UnregisterProcessor(const std::string& name)
116 {
117     if (processors_.find(name) == processors_.end()) {
118         HiLog::Warn(LABEL, "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<AppEventObserver> ModuleLoader::CreateProcessorProxy(const std::string& name)
126 {
127     if (processors_.find(name) == processors_.end()) {
128         HiLog::Warn(LABEL, "the name is invalid");
129         return nullptr;
130     }
131     return std::make_shared<AppEventProcessorProxy>(name, processors_[name]);
132 }
133 } // namespace HiAppEvent
134 } // namespace HiviewDFX
135 } // namespace OHOS
136