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 namespace OHOS {
41 namespace HiviewDFX {
42
43 namespace {
44 constexpr HiLogLabel LABEL = { LOG_CORE, 0xD002D08, "HiView-HiSysEventDelegate" };
45 const std::string EVENT_DIR = "/data/storage/el2/base/cache/hiview/event";
46 #ifdef STORAGE_SERVICE_ENABLE
47 const std::string BASE_DIR = "/data/storage/el2/base";
48 const std::string CACHE_DIR = "/data/storage/el2/base/cache";
49 const std::string HIVIEW_DIR = "/data/storage/el2/base/cache/hiview";
50 const std::string PARENT_DIR_PERMISSION = "g:1201:x";
51 const std::string SUB_DIR_PERMISSION = "g:1201:rwx";
52 constexpr int ACL_SUCC = 0;
53 #endif
54 }
55
AddListener(const std::shared_ptr<HiSysEventBaseListener> listener,const std::vector<ListenerRule> & rules)56 int32_t HiSysEventDelegate::AddListener(const std::shared_ptr<HiSysEventBaseListener> listener,
57 const std::vector<ListenerRule>& rules)
58 {
59 auto service = GetSysEventService();
60 if (service == nullptr) {
61 HiLog::Error(LABEL, "Fail to get service.");
62 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
63 }
64 std::vector<SysEventRule> eventRules;
65 ConvertListenerRule(rules, eventRules);
66
67 if (!spListenerCallBack) {
68 spListenerCallBack = new OHOS::HiviewDFX::HiSysEventListenerProxy(listener);
69 }
70
71 SysEventServiceProxy sysEventService(service);
72 service->RemoveDeathRecipient(spListenerCallBack->GetCallbackDeathRecipient());
73 return sysEventService.AddListener(eventRules, spListenerCallBack);
74 }
75
RemoveListener(const std::shared_ptr<HiSysEventBaseListener> listener)76 int32_t HiSysEventDelegate::RemoveListener(const std::shared_ptr<HiSysEventBaseListener> listener)
77 {
78 if (!spListenerCallBack) {
79 return ERR_LISTENER_NOT_EXIST;
80 }
81 auto service = GetSysEventService();
82 if (service == nullptr) {
83 HiLog::Error(LABEL, "Fail to get service.");
84 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
85 }
86 SysEventServiceProxy sysEventService(service);
87 return sysEventService.RemoveListener(spListenerCallBack);
88 }
89
SetDebugMode(const std::shared_ptr<HiSysEventBaseListener> listener,const bool mode)90 int32_t HiSysEventDelegate::SetDebugMode(const std::shared_ptr<HiSysEventBaseListener> listener,
91 const bool mode)
92 {
93 if (!spListenerCallBack) {
94 return ERR_LISTENER_NOT_EXIST;
95 }
96 auto service = GetSysEventService();
97 if (service == nullptr) {
98 HiLog::Error(LABEL, "Fail to get service.");
99 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
100 }
101 SysEventServiceProxy sysEventService(service);
102 return sysEventService.SetDebugMode(spListenerCallBack, mode);
103 }
104
Query(const struct QueryArg & arg,const std::vector<QueryRule> & rules,const std::shared_ptr<HiSysEventBaseQueryCallback> callback) const105 int32_t HiSysEventDelegate::Query(const struct QueryArg& arg,
106 const std::vector<QueryRule>& rules,
107 const std::shared_ptr<HiSysEventBaseQueryCallback> callback) const
108 {
109 auto service = GetSysEventService();
110 if (service == nullptr) {
111 HiLog::Error(LABEL, "Fail to get service.");
112 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
113 }
114
115 std::vector<SysEventQueryRule> hospRules;
116 ConvertQueryRule(rules, hospRules);
117
118 sptr<HiSysEventQueryProxy> spCallBack(new OHOS::HiviewDFX::HiSysEventQueryProxy(callback));
119
120 SysEventServiceProxy sysEventService(service);
121 QueryArgument queryArgument(arg.beginTime, arg.endTime, arg.maxEvents, arg.fromSeq, arg.toSeq);
122 return sysEventService.Query(queryArgument, hospRules, spCallBack);
123 }
124
Export(const struct QueryArg & arg,const std::vector<QueryRule> & rules) const125 int64_t HiSysEventDelegate::Export(const struct QueryArg& arg, const std::vector<QueryRule>& rules) const
126 {
127 auto service = GetSysEventService();
128 if (service == nullptr) {
129 HiLog::Error(LABEL, "Fail to get service.");
130 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
131 }
132 auto res = CreateHiviewDir();
133 if (!res) {
134 HiLog::Error(LABEL, "Fail to create hiview dir.");
135 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
136 }
137 res = SetDirPermission();
138 if (!res) {
139 HiLog::Error(LABEL, "Fail to set ACL permission.");
140 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
141 }
142 std::vector<SysEventQueryRule> hospRules;
143 ConvertQueryRule(rules, hospRules);
144 SysEventServiceProxy sysEventService(service);
145 QueryArgument queryArgument(arg.beginTime, arg.endTime, arg.maxEvents, arg.fromSeq, arg.toSeq);
146 return sysEventService.Export(queryArgument, hospRules);
147 }
148
Subscribe(const std::vector<QueryRule> & rules) const149 int64_t HiSysEventDelegate::Subscribe(const std::vector<QueryRule>& rules) const
150 {
151 auto service = GetSysEventService();
152 if (service == nullptr) {
153 HiLog::Error(LABEL, "Fail to get service.");
154 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
155 }
156
157 auto res = CreateHiviewDir();
158 if (!res) {
159 HiLog::Error(LABEL, "Fail to create hiview dir.");
160 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
161 }
162 res = SetDirPermission();
163 if (!res) {
164 HiLog::Error(LABEL, "Fail to set ACL permission.");
165 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
166 }
167
168 std::vector<SysEventQueryRule> hospRules;
169 ConvertQueryRule(rules, hospRules);
170
171 SysEventServiceProxy sysEventService(service);
172 return sysEventService.AddSubscriber(hospRules);
173 }
174
Unsubscribe() const175 int32_t HiSysEventDelegate::Unsubscribe() const
176 {
177 auto service = GetSysEventService();
178 if (service == nullptr) {
179 HiLog::Error(LABEL, "Fail to get service.");
180 return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
181 }
182 SysEventServiceProxy sysEventService(service);
183 return sysEventService.RemoveSubscriber();
184 }
185
~HiSysEventDelegate()186 HiSysEventDelegate::~HiSysEventDelegate()
187 {
188 HiLog::Info(LABEL, "HiSysEventDelegate destructor");
189 }
190
ConvertListenerRule(const std::vector<ListenerRule> & rules,std::vector<SysEventRule> & sysRules) const191 void HiSysEventDelegate::ConvertListenerRule(const std::vector<ListenerRule>& rules,
192 std::vector<SysEventRule>& sysRules) const
193 {
194 for_each(rules.cbegin(), rules.cend(), [&sysRules](const ListenerRule& rule) {
195 if (rule.GetTag().empty()) {
196 sysRules.emplace_back(rule.GetDomain(), rule.GetEventName(), rule.GetRuleType(), rule.GetEventType());
197 } else {
198 sysRules.emplace_back(rule.GetTag(), rule.GetRuleType(), rule.GetEventType());
199 }
200 });
201 }
202
ConvertQueryRule(const std::vector<QueryRule> & rules,std::vector<SysEventQueryRule> & sysRules) const203 void HiSysEventDelegate::ConvertQueryRule(const std::vector<QueryRule>& rules,
204 std::vector<SysEventQueryRule>& sysRules) const
205 {
206 for_each(rules.cbegin(), rules.cend(), [&sysRules](const QueryRule &rule) {
207 std::vector<std::string> events;
208 auto eventList = rule.GetEventList();
209 for_each(eventList.cbegin(), eventList.cend(), [&](const std::string &event) {
210 events.push_back(event);
211 });
212 sysRules.emplace_back(rule.GetDomain(), events, rule.GetRuleType(), rule.GetEventType(), rule.GetCondition());
213 });
214 }
215
BinderFunc()216 void HiSysEventDelegate::BinderFunc()
217 {
218 IPCSkeleton::JoinWorkThread();
219 }
220
GetSysEventService() const221 sptr<IRemoteObject> HiSysEventDelegate::GetSysEventService() const
222 {
223 sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
224 if (sam == nullptr) {
225 return nullptr;
226 }
227 return sam->CheckSystemAbility(DFX_SYS_EVENT_SERVICE_ABILITY_ID);
228 }
229
CreateHiviewDir() const230 bool HiSysEventDelegate::CreateHiviewDir() const
231 {
232 if (FileUtil::IsFileExists(EVENT_DIR)) {
233 return true;
234 }
235 if (!FileUtil::ForceCreateDirectory(EVENT_DIR)) {
236 HiLog::Error(LABEL, "failed to create event dir, errno=%{public}d.", errno);
237 return false;
238 }
239 return true;
240 }
241
SetDirPermission() const242 bool HiSysEventDelegate::SetDirPermission() const
243 {
244 #ifdef STORAGE_SERVICE_ENABLE
245 int aclBaseRet = AclSetAccess(BASE_DIR, PARENT_DIR_PERMISSION);
246 if (aclBaseRet != ACL_SUCC) {
247 HiLog::Error(LABEL, "Set ACL failed , baseDirPath= %{public}s ret = %{public}d!!!!",
248 BASE_DIR.c_str(), aclBaseRet);
249 return false;
250 }
251 int aclCacheRet = AclSetAccess(CACHE_DIR, PARENT_DIR_PERMISSION);
252 if (aclCacheRet != ACL_SUCC) {
253 HiLog::Error(LABEL, "Set ACL failed , cacheDirPath= %{public}s ret = %{public}d!!!!",
254 CACHE_DIR.c_str(), aclCacheRet);
255 return false;
256 }
257 int aclHiviewRet = AclSetAccess(HIVIEW_DIR, PARENT_DIR_PERMISSION);
258 if (aclHiviewRet != ACL_SUCC) {
259 HiLog::Error(LABEL, "Set ACL failed , hiviewDirPath= %{public}s ret = %{public}d!!!!",
260 HIVIEW_DIR.c_str(), aclHiviewRet);
261 return false;
262 }
263 int aclRet = AclSetAccess(EVENT_DIR, SUB_DIR_PERMISSION);
264 if (aclRet != ACL_SUCC) {
265 HiLog::Error(LABEL, "Set ACL failed , eventDirPath= %{public}s ret = %{public}d!!!!",
266 EVENT_DIR.c_str(), aclRet);
267 return false;
268 }
269 return true;
270 #else
271 return false;
272 #endif // STORAGE_SERVICE_ENABLE
273 }
274
275 } // namespace HiviewDFX
276 } // namespace OHOS
277