• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
Query(const struct QueryArg & arg,const std::vector<QueryRule> & rules,const std::shared_ptr<HiSysEventBaseQueryCallback> callback) const95 int32_t HiSysEventDelegate::Query(const struct QueryArg& arg,
96     const std::vector<QueryRule>& rules,
97     const std::shared_ptr<HiSysEventBaseQueryCallback> callback) const
98 {
99     auto service = GetSysEventService();
100     if (service == nullptr) {
101         HILOG_ERROR(LOG_CORE, "Fail to get service.");
102         return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
103     }
104 
105     std::vector<SysEventQueryRule> hospRules;
106     ConvertQueryRule(rules, hospRules);
107 
108     sptr<HiSysEventQueryProxy> spCallBack(new OHOS::HiviewDFX::HiSysEventQueryProxy(callback));
109 
110     SysEventServiceProxy sysEventService(service);
111     QueryArgument queryArgument(arg.beginTime, arg.endTime, arg.maxEvents, arg.fromSeq, arg.toSeq);
112     return sysEventService.Query(queryArgument, hospRules, spCallBack);
113 }
114 
Export(const struct QueryArg & arg,const std::vector<QueryRule> & rules) const115 int64_t HiSysEventDelegate::Export(const struct QueryArg& arg, const std::vector<QueryRule>& rules) const
116 {
117     auto service = GetSysEventService();
118     if (service == nullptr) {
119         HILOG_ERROR(LOG_CORE, "Fail to get service.");
120         return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
121     }
122     auto res = CreateHiviewDir();
123     if (!res) {
124         HILOG_ERROR(LOG_CORE, "Fail to create hiview dir.");
125         return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
126     }
127     res = SetDirPermission();
128     if (!res) {
129         HILOG_ERROR(LOG_CORE, "Fail to set ACL permission.");
130         return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
131     }
132     std::vector<SysEventQueryRule> hospRules;
133     ConvertQueryRule(rules, hospRules);
134     SysEventServiceProxy sysEventService(service);
135     QueryArgument queryArgument(arg.beginTime, arg.endTime, arg.maxEvents, arg.fromSeq, arg.toSeq);
136     int64_t result = 0;
137     if (auto retCode = sysEventService.Export(queryArgument, hospRules, result); retCode != IPC_CALL_SUCCEED) {
138         HILOG_ERROR(LOG_CORE, "Fail to invoke interface Export.");
139         return retCode;
140     }
141     return result;
142 }
143 
Subscribe(const std::vector<QueryRule> & rules) const144 int64_t HiSysEventDelegate::Subscribe(const std::vector<QueryRule>& rules) const
145 {
146     auto service = GetSysEventService();
147     if (service == nullptr) {
148         HILOG_ERROR(LOG_CORE, "Fail to get service.");
149         return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
150     }
151 
152     auto res = CreateHiviewDir();
153     if (!res) {
154         HILOG_ERROR(LOG_CORE, "Fail to create hiview dir.");
155         return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
156     }
157     res = SetDirPermission();
158     if (!res) {
159         HILOG_ERROR(LOG_CORE, "Fail to set ACL permission.");
160         return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
161     }
162 
163     std::vector<SysEventQueryRule> hospRules;
164     ConvertQueryRule(rules, hospRules);
165 
166     SysEventServiceProxy sysEventService(service);
167     int64_t result = 0;
168     if (auto retCode = sysEventService.AddSubscriber(hospRules, result); retCode != IPC_CALL_SUCCEED) {
169         HILOG_ERROR(LOG_CORE, "Fail to invoke interface Subscribe.");
170         return retCode;
171     }
172     return result;
173 }
174 
Unsubscribe() const175 int32_t HiSysEventDelegate::Unsubscribe() const
176 {
177     auto service = GetSysEventService();
178     if (service == nullptr) {
179         HILOG_ERROR(LOG_CORE, "Fail to get service.");
180         return ERR_SYS_EVENT_SERVICE_NOT_FOUND;
181     }
182     SysEventServiceProxy sysEventService(service);
183     return sysEventService.RemoveSubscriber();
184 }
185 
ConvertListenerRule(const std::vector<ListenerRule> & rules,std::vector<SysEventRule> & sysRules) const186 void HiSysEventDelegate::ConvertListenerRule(const std::vector<ListenerRule>& rules,
187     std::vector<SysEventRule>& sysRules) const
188 {
189     for_each(rules.cbegin(), rules.cend(), [&sysRules](const ListenerRule& rule) {
190         if (rule.GetTag().empty()) {
191             sysRules.emplace_back(rule.GetDomain(), rule.GetEventName(), rule.GetRuleType(), rule.GetEventType());
192         } else {
193             sysRules.emplace_back(rule.GetTag(), rule.GetRuleType(), rule.GetEventType());
194         }
195     });
196 }
197 
ConvertQueryRule(const std::vector<QueryRule> & rules,std::vector<SysEventQueryRule> & sysRules) const198 void HiSysEventDelegate::ConvertQueryRule(const std::vector<QueryRule>& rules,
199     std::vector<SysEventQueryRule>& sysRules) const
200 {
201     for_each(rules.cbegin(), rules.cend(), [&sysRules](const QueryRule &rule) {
202         std::vector<std::string> events;
203         auto eventList = rule.GetEventList();
204         for_each(eventList.cbegin(), eventList.cend(), [&](const std::string &event) {
205             events.push_back(event);
206         });
207         sysRules.emplace_back(rule.GetDomain(), events, rule.GetRuleType(), rule.GetEventType(), rule.GetCondition());
208     });
209 }
210 
BinderFunc()211 void HiSysEventDelegate::BinderFunc()
212 {
213     IPCSkeleton::JoinWorkThread();
214 }
215 
GetSysEventService() const216 sptr<IRemoteObject> HiSysEventDelegate::GetSysEventService() const
217 {
218     sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
219     if (sam == nullptr) {
220         return nullptr;
221     }
222     return sam->CheckSystemAbility(DFX_SYS_EVENT_SERVICE_ABILITY_ID);
223 }
224 
CreateHiviewDir() const225 bool HiSysEventDelegate::CreateHiviewDir() const
226 {
227     if (FileUtil::IsFileExists(EVENT_DIR)) {
228         return true;
229     }
230     if (!FileUtil::ForceCreateDirectory(EVENT_DIR)) {
231         HILOG_ERROR(LOG_CORE, "failed to create event dir, errno=%{public}d.", errno);
232         return false;
233     }
234     return true;
235 }
236 
SetDirPermission() const237 bool HiSysEventDelegate::SetDirPermission() const
238 {
239 #ifdef STORAGE_SERVICE_ENABLE
240     int aclBaseRet = AclSetAccess(BASE_DIR, PARENT_DIR_PERMISSION);
241     if (aclBaseRet != ACL_SUCC) {
242         HILOG_ERROR(LOG_CORE, "Set ACL failed , baseDirPath= %{public}s ret = %{public}d!!!!",
243             BASE_DIR.c_str(), aclBaseRet);
244         return false;
245     }
246     int aclCacheRet = AclSetAccess(CACHE_DIR, PARENT_DIR_PERMISSION);
247     if (aclCacheRet != ACL_SUCC) {
248         HILOG_ERROR(LOG_CORE, "Set ACL failed , cacheDirPath= %{public}s ret = %{public}d!!!!",
249             CACHE_DIR.c_str(), aclCacheRet);
250         return false;
251     }
252     int aclHiviewRet = AclSetAccess(HIVIEW_DIR, PARENT_DIR_PERMISSION);
253     if (aclHiviewRet != ACL_SUCC) {
254         HILOG_ERROR(LOG_CORE, "Set ACL failed , hiviewDirPath= %{public}s ret = %{public}d!!!!",
255             HIVIEW_DIR.c_str(), aclHiviewRet);
256         return false;
257     }
258     int aclRet = AclSetAccess(EVENT_DIR, SUB_DIR_PERMISSION);
259     if (aclRet != ACL_SUCC) {
260         HILOG_ERROR(LOG_CORE, "Set ACL failed , eventDirPath= %{public}s ret = %{public}d!!!!",
261             EVENT_DIR.c_str(), aclRet);
262         return false;
263     }
264     return true;
265 #else
266     return false;
267 #endif // STORAGE_SERVICE_ENABLE
268 }
269 
270 } // namespace HiviewDFX
271 } // namespace OHOS
272