1 /*
2 * Copyright (c) 2022-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
16 #include "plugin_manager.h"
17 #include <cstring>
18 #include <dirent.h>
19 #include <dlfcn.h>
20 #include <iostream>
21 #include <memory>
22 #include <mutex>
23 #include <string_ex.h>
24 #include <unistd.h>
25 #include "edm_log.h"
26 #include "func_code_utils.h"
27 #include "permission_manager.h"
28
29 namespace OHOS {
30 namespace EDM {
31 std::shared_ptr<PluginManager> PluginManager::instance_;
32 std::mutex PluginManager::mutexLock_;
33
PluginManager()34 PluginManager::PluginManager()
35 {
36 EDMLOGD("PluginManager::PluginManager.");
37 }
38
~PluginManager()39 PluginManager::~PluginManager()
40 {
41 EDMLOGD("PluginManager::~PluginManager.");
42 for (auto entry : pluginsCode_) {
43 entry.second.reset();
44 entry.second = nullptr;
45 }
46 pluginsCode_.clear();
47 for (auto handle : pluginHandles_) {
48 dlclose(handle);
49 }
50 pluginHandles_.clear();
51 }
52
GetInstance()53 std::shared_ptr<PluginManager> PluginManager::GetInstance()
54 {
55 if (instance_ == nullptr) {
56 std::lock_guard<std::mutex> autoLock(mutexLock_);
57 if (instance_ == nullptr) {
58 instance_.reset(new (std::nothrow) PluginManager());
59 }
60 }
61
62 IPluginManager::pluginManagerInstance_ = instance_.get();
63 return instance_;
64 }
65
GetPluginByFuncCode(std::uint32_t funcCode)66 std::shared_ptr<IPlugin> PluginManager::GetPluginByFuncCode(std::uint32_t funcCode)
67 {
68 FuncCodeUtils::PrintFuncCode(funcCode);
69 FuncFlag flag = FuncCodeUtils::GetSystemFlag(funcCode);
70 if (flag == FuncFlag::POLICY_FLAG) {
71 std::uint32_t code = FuncCodeUtils::GetPolicyCode(funcCode);
72 EDMLOGD("PluginManager::code %{public}u", code);
73 auto it = pluginsCode_.find(code);
74 if (it != pluginsCode_.end()) {
75 return it->second;
76 }
77 }
78 EDMLOGD("GetPluginByFuncCode::return nullptr");
79 return nullptr;
80 }
81
GetPluginByPolicyName(const std::string & policyName)82 std::shared_ptr<IPlugin> PluginManager::GetPluginByPolicyName(const std::string &policyName)
83 {
84 auto it = pluginsName_.find(policyName);
85 if (it != pluginsName_.end()) {
86 return it->second;
87 }
88 return nullptr;
89 }
90
AddPlugin(std::shared_ptr<IPlugin> plugin)91 bool PluginManager::AddPlugin(std::shared_ptr<IPlugin> plugin)
92 {
93 EDMLOGD("AddPlugin");
94 if (plugin == nullptr) {
95 return false;
96 }
97 std::string permission = plugin->GetPermission(FuncOperateType::SET);
98 ErrCode result = PermissionManager::GetInstance()->AddPermission(permission,
99 plugin->GetPermissionType(FuncOperateType::SET));
100 if (result == ERR_OK && plugin->GetPermission(FuncOperateType::GET) != permission) {
101 result = PermissionManager::GetInstance()->AddPermission(plugin->GetPermission(FuncOperateType::GET),
102 plugin->GetPermissionType(FuncOperateType::GET));
103 }
104 if (result == ERR_OK) {
105 pluginsCode_.insert(std::make_pair(plugin->GetCode(), plugin));
106 pluginsName_.insert(std::make_pair(plugin->GetPolicyName(), plugin));
107 return true;
108 }
109 return false;
110 }
111
Init()112 void PluginManager::Init()
113 {
114 LoadPlugin();
115 }
116
LoadPlugin()117 void PluginManager::LoadPlugin()
118 {
119 #ifdef _ARM64_
120 std::string pluginDir = "/system/lib64/edm_plugin/";
121 #else
122 std::string pluginDir = "/system/lib/edm_plugin/";
123 #endif
124 DIR *dir = opendir(pluginDir.c_str());
125 if (dir == nullptr) {
126 EDMLOGE("PluginManager::LoadPlugin open edm_plugin dir fail.");
127 return;
128 }
129 struct dirent *entry;
130 while ((entry = readdir(dir)) != nullptr) {
131 if (entry->d_type == DT_REG && strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0) {
132 LoadPlugin(pluginDir + entry->d_name);
133 }
134 }
135 closedir(dir);
136 }
137
LoadPlugin(const std::string & pluginPath)138 void PluginManager::LoadPlugin(const std::string &pluginPath)
139 {
140 void *handle = dlopen(pluginPath.c_str(), RTLD_LAZY);
141 if (!handle) {
142 EDMLOGE("PluginManager::open plugin so fail. %{public}s.", dlerror());
143 return;
144 }
145 char *szError = dlerror();
146 if (szError != nullptr) {
147 EDMLOGW("PluginManager::loading plugin fail. %{public}s.", szError);
148 }
149 pluginHandles_.push_back(handle);
150 }
151
DumpPlugin()152 void PluginManager::DumpPlugin()
153 {
154 for (auto it = pluginsCode_.begin(); it != pluginsCode_.end(); it++) {
155 EDMLOGD("PluginManager::Dump plugins_code.code:%{public}u,name:%{public}s,get permission:%{public}s, "
156 "set permission:%{public}s",
157 it->first, it->second->GetPolicyName().c_str(),
158 it->second->GetPermission(FuncOperateType::GET).c_str(),
159 it->second->GetPermission(FuncOperateType::SET).c_str());
160 }
161 for (auto it = pluginsName_.begin(); it != pluginsName_.end(); it++) {
162 EDMLOGD("PluginManager::Dump plugins_name.name:%{public}s,code:%{public}u,get permission type:%{public}s, "
163 "set permission type:%{public}s",
164 it->first.c_str(), it->second->GetCode(),
165 it->second->GetPermission(FuncOperateType::GET).c_str(),
166 it->second->GetPermission(FuncOperateType::SET).c_str());
167 }
168 }
169 } // namespace EDM
170 } // namespace OHOS
171