1 /*
2 * Copyright (c) 2021-2022 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 "hisysevent_delegate.h"
17
18 #include <memory>
19
20 #include "file_util.h"
21 #include "hilog/log.h"
22 #include "hisysevent_listener_proxy.h"
23 #include "hisysevent_query_proxy.h"
24 #include "if_system_ability_manager.h"
25 #include "ipc_skeleton.h"
26 #include "iservice_registry.h"
27 #include "query_argument.h"
28 #include "ret_code.h"
29 #ifdef STORAGE_SERVICE_ENABLE
30 #include "storage_acl.h"
31 #endif
32 #include "sys_event_service_proxy.h"
33 #include "system_ability_definition.h"
34
35 using namespace std;
36 #ifdef STORAGE_SERVICE_ENABLE
37 using namespace OHOS::StorageDaemon;
38 #endif
39
40 #undef LOG_DOMAIN
41 #define LOG_DOMAIN 0xD002D08
42
43 #undef LOG_TAG
44 #define LOG_TAG "HISYSEVENT_DELEGATE"
45
46 namespace OHOS {
47 namespace HiviewDFX {
48
49 namespace {
50 const std::string EVENT_DIR = "/data/storage/el2/base/cache/hiview/event";
51 #ifdef STORAGE_SERVICE_ENABLE
52 const std::string BASE_DIR = "/data/storage/el2/base";
53 const std::string CACHE_DIR = "/data/storage/el2/base/cache";
54 const std::string HIVIEW_DIR = "/data/storage/el2/base/cache/hiview";
55 const std::string PARENT_DIR_PERMISSION = "g:1201:x";
56 const std::string SUB_DIR_PERMISSION = "g:1201:rwx";
57 constexpr int ACL_SUCC = 0;
58 #endif
59 }
60
AddListener(const std::shared_ptr<HiSysEventBaseListener> listener,const std::vector<ListenerRule> & rules)61 int32_t HiSysEventDelegate::AddListener(const std::shared_ptr<HiSysEventBaseListener> listener,
62 const std::vector<ListenerRule>& rules)
63 {
64 auto service = GetSysEventService();
65 if (service == nullptr) {
66 HILOG_ERROR(LOG_CORE, "Fail to get service.");
67 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
68 }
69 std::vector<SysEventRule> eventRules;
70 ConvertListenerRule(rules, eventRules);
71
72 if (!spListenerCallBack) {
73 spListenerCallBack = new OHOS::HiviewDFX::HiSysEventListenerProxy(listener);
74 }
75
76 SysEventServiceProxy sysEventService(service);
77 service->RemoveDeathRecipient(spListenerCallBack->GetCallbackDeathRecipient());
78 return sysEventService.AddListener(eventRules, spListenerCallBack);
79 }
80
RemoveListener(const std::shared_ptr<HiSysEventBaseListener> listener)81 int32_t HiSysEventDelegate::RemoveListener(const std::shared_ptr<HiSysEventBaseListener> listener)
82 {
83 if (!spListenerCallBack) {
84 return ERR_LISTENER_NOT_EXIST;
85 }
86 auto service = GetSysEventService();
87 if (service == nullptr) {
88 HILOG_ERROR(LOG_CORE, "Fail to get service.");
89 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
90 }
91 SysEventServiceProxy sysEventService(service);
92 return sysEventService.RemoveListener(spListenerCallBack);
93 }
94
SetDebugMode(const std::shared_ptr<HiSysEventBaseListener> listener,const bool mode)95 int32_t HiSysEventDelegate::SetDebugMode(const std::shared_ptr<HiSysEventBaseListener> listener,
96 const bool mode)
97 {
98 if (!spListenerCallBack) {
99 return ERR_LISTENER_NOT_EXIST;
100 }
101 auto service = GetSysEventService();
102 if (service == nullptr) {
103 HILOG_ERROR(LOG_CORE, "Fail to get service.");
104 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
105 }
106 SysEventServiceProxy sysEventService(service);
107 return sysEventService.SetDebugMode(spListenerCallBack, mode);
108 }
109
Query(const struct QueryArg & arg,const std::vector<QueryRule> & rules,const std::shared_ptr<HiSysEventBaseQueryCallback> callback) const110 int32_t HiSysEventDelegate::Query(const struct QueryArg& arg,
111 const std::vector<QueryRule>& rules,
112 const std::shared_ptr<HiSysEventBaseQueryCallback> callback) const
113 {
114 auto service = GetSysEventService();
115 if (service == nullptr) {
116 HILOG_ERROR(LOG_CORE, "Fail to get service.");
117 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
118 }
119
120 std::vector<SysEventQueryRule> hospRules;
121 ConvertQueryRule(rules, hospRules);
122
123 sptr<HiSysEventQueryProxy> spCallBack(new OHOS::HiviewDFX::HiSysEventQueryProxy(callback));
124
125 SysEventServiceProxy sysEventService(service);
126 QueryArgument queryArgument(arg.beginTime, arg.endTime, arg.maxEvents, arg.fromSeq, arg.toSeq);
127 return sysEventService.Query(queryArgument, hospRules, spCallBack);
128 }
129
Export(const struct QueryArg & arg,const std::vector<QueryRule> & rules) const130 int64_t HiSysEventDelegate::Export(const struct QueryArg& arg, const std::vector<QueryRule>& rules) const
131 {
132 auto service = GetSysEventService();
133 if (service == nullptr) {
134 HILOG_ERROR(LOG_CORE, "Fail to get service.");
135 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
136 }
137 auto res = CreateHiviewDir();
138 if (!res) {
139 HILOG_ERROR(LOG_CORE, "Fail to create hiview dir.");
140 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
141 }
142 res = SetDirPermission();
143 if (!res) {
144 HILOG_ERROR(LOG_CORE, "Fail to set ACL permission.");
145 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
146 }
147 std::vector<SysEventQueryRule> hospRules;
148 ConvertQueryRule(rules, hospRules);
149 SysEventServiceProxy sysEventService(service);
150 QueryArgument queryArgument(arg.beginTime, arg.endTime, arg.maxEvents, arg.fromSeq, arg.toSeq);
151 return sysEventService.Export(queryArgument, hospRules);
152 }
153
Subscribe(const std::vector<QueryRule> & rules) const154 int64_t HiSysEventDelegate::Subscribe(const std::vector<QueryRule>& rules) const
155 {
156 auto service = GetSysEventService();
157 if (service == nullptr) {
158 HILOG_ERROR(LOG_CORE, "Fail to get service.");
159 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
160 }
161
162 auto res = CreateHiviewDir();
163 if (!res) {
164 HILOG_ERROR(LOG_CORE, "Fail to create hiview dir.");
165 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
166 }
167 res = SetDirPermission();
168 if (!res) {
169 HILOG_ERROR(LOG_CORE, "Fail to set ACL permission.");
170 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
171 }
172
173 std::vector<SysEventQueryRule> hospRules;
174 ConvertQueryRule(rules, hospRules);
175
176 SysEventServiceProxy sysEventService(service);
177 return sysEventService.AddSubscriber(hospRules);
178 }
179
Unsubscribe() const180 int32_t HiSysEventDelegate::Unsubscribe() const
181 {
182 auto service = GetSysEventService();
183 if (service == nullptr) {
184 HILOG_ERROR(LOG_CORE, "Fail to get service.");
185 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
186 }
187 SysEventServiceProxy sysEventService(service);
188 return sysEventService.RemoveSubscriber();
189 }
190
~HiSysEventDelegate()191 HiSysEventDelegate::~HiSysEventDelegate()
192 {
193 }
194
ConvertListenerRule(const std::vector<ListenerRule> & rules,std::vector<SysEventRule> & sysRules) const195 void HiSysEventDelegate::ConvertListenerRule(const std::vector<ListenerRule>& rules,
196 std::vector<SysEventRule>& sysRules) const
197 {
198 for_each(rules.cbegin(), rules.cend(), [&sysRules](const ListenerRule& rule) {
199 if (rule.GetTag().empty()) {
200 sysRules.emplace_back(rule.GetDomain(), rule.GetEventName(), rule.GetRuleType(), rule.GetEventType());
201 } else {
202 sysRules.emplace_back(rule.GetTag(), rule.GetRuleType(), rule.GetEventType());
203 }
204 });
205 }
206
ConvertQueryRule(const std::vector<QueryRule> & rules,std::vector<SysEventQueryRule> & sysRules) const207 void HiSysEventDelegate::ConvertQueryRule(const std::vector<QueryRule>& rules,
208 std::vector<SysEventQueryRule>& sysRules) const
209 {
210 for_each(rules.cbegin(), rules.cend(), [&sysRules](const QueryRule &rule) {
211 std::vector<std::string> events;
212 auto eventList = rule.GetEventList();
213 for_each(eventList.cbegin(), eventList.cend(), [&](const std::string &event) {
214 events.push_back(event);
215 });
216 sysRules.emplace_back(rule.GetDomain(), events, rule.GetRuleType(), rule.GetEventType(), rule.GetCondition());
217 });
218 }
219
BinderFunc()220 void HiSysEventDelegate::BinderFunc()
221 {
222 IPCSkeleton::JoinWorkThread();
223 }
224
GetSysEventService() const225 sptr<IRemoteObject> HiSysEventDelegate::GetSysEventService() const
226 {
227 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
228 if (sam == nullptr) {
229 return nullptr;
230 }
231 return sam->CheckSystemAbility(DFX_SYS_EVENT_SERVICE_ABILITY_ID);
232 }
233
CreateHiviewDir() const234 bool HiSysEventDelegate::CreateHiviewDir() const
235 {
236 if (FileUtil::IsFileExists(EVENT_DIR)) {
237 return true;
238 }
239 if (!FileUtil::ForceCreateDirectory(EVENT_DIR)) {
240 HILOG_ERROR(LOG_CORE, "failed to create event dir, errno=%{public}d.", errno);
241 return false;
242 }
243 return true;
244 }
245
SetDirPermission() const246 bool HiSysEventDelegate::SetDirPermission() const
247 {
248 #ifdef STORAGE_SERVICE_ENABLE
249 int aclBaseRet = AclSetAccess(BASE_DIR, PARENT_DIR_PERMISSION);
250 if (aclBaseRet != ACL_SUCC) {
251 HILOG_ERROR(LOG_CORE, "Set ACL failed , baseDirPath= %{public}s ret = %{public}d!!!!",
252 BASE_DIR.c_str(), aclBaseRet);
253 return false;
254 }
255 int aclCacheRet = AclSetAccess(CACHE_DIR, PARENT_DIR_PERMISSION);
256 if (aclCacheRet != ACL_SUCC) {
257 HILOG_ERROR(LOG_CORE, "Set ACL failed , cacheDirPath= %{public}s ret = %{public}d!!!!",
258 CACHE_DIR.c_str(), aclCacheRet);
259 return false;
260 }
261 int aclHiviewRet = AclSetAccess(HIVIEW_DIR, PARENT_DIR_PERMISSION);
262 if (aclHiviewRet != ACL_SUCC) {
263 HILOG_ERROR(LOG_CORE, "Set ACL failed , hiviewDirPath= %{public}s ret = %{public}d!!!!",
264 HIVIEW_DIR.c_str(), aclHiviewRet);
265 return false;
266 }
267 int aclRet = AclSetAccess(EVENT_DIR, SUB_DIR_PERMISSION);
268 if (aclRet != ACL_SUCC) {
269 HILOG_ERROR(LOG_CORE, "Set ACL failed , eventDirPath= %{public}s ret = %{public}d!!!!",
270 EVENT_DIR.c_str(), aclRet);
271 return false;
272 }
273 return true;
274 #else
275 return false;
276 #endif // STORAGE_SERVICE_ENABLE
277 }
278
279 } // namespace HiviewDFX
280 } // namespace OHOS
281