11401458bSopenharmony_ci/* 21401458bSopenharmony_ci * Copyright (c) 2021-2022 Huawei Device Co., Ltd. 31401458bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 41401458bSopenharmony_ci * you may not use this file except in compliance with the License. 51401458bSopenharmony_ci * You may obtain a copy of the License at 61401458bSopenharmony_ci * 71401458bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 81401458bSopenharmony_ci * 91401458bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 101401458bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 111401458bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 121401458bSopenharmony_ci * See the License for the specific language governing permissions and 131401458bSopenharmony_ci * limitations under the License. 141401458bSopenharmony_ci */ 151401458bSopenharmony_ci 161401458bSopenharmony_ci#include "hisysevent_delegate.h" 171401458bSopenharmony_ci 181401458bSopenharmony_ci#include <memory> 191401458bSopenharmony_ci 201401458bSopenharmony_ci#include "file_util.h" 211401458bSopenharmony_ci#include "hilog/log.h" 221401458bSopenharmony_ci#include "hisysevent_listener_proxy.h" 231401458bSopenharmony_ci#include "hisysevent_query_proxy.h" 241401458bSopenharmony_ci#include "if_system_ability_manager.h" 251401458bSopenharmony_ci#include "ipc_skeleton.h" 261401458bSopenharmony_ci#include "iservice_registry.h" 271401458bSopenharmony_ci#include "query_argument.h" 281401458bSopenharmony_ci#include "ret_code.h" 291401458bSopenharmony_ci#ifdef STORAGE_SERVICE_ENABLE 301401458bSopenharmony_ci#include "storage_acl.h" 311401458bSopenharmony_ci#endif 321401458bSopenharmony_ci#include "sys_event_service_proxy.h" 331401458bSopenharmony_ci#include "system_ability_definition.h" 341401458bSopenharmony_ci 351401458bSopenharmony_ciusing namespace std; 361401458bSopenharmony_ci#ifdef STORAGE_SERVICE_ENABLE 371401458bSopenharmony_ciusing namespace OHOS::StorageDaemon; 381401458bSopenharmony_ci#endif 391401458bSopenharmony_ci 401401458bSopenharmony_ci#undef LOG_DOMAIN 411401458bSopenharmony_ci#define LOG_DOMAIN 0xD002D08 421401458bSopenharmony_ci 431401458bSopenharmony_ci#undef LOG_TAG 441401458bSopenharmony_ci#define LOG_TAG "HISYSEVENT_DELEGATE" 451401458bSopenharmony_ci 461401458bSopenharmony_cinamespace OHOS { 471401458bSopenharmony_cinamespace HiviewDFX { 481401458bSopenharmony_ci 491401458bSopenharmony_cinamespace { 501401458bSopenharmony_ciconst std::string EVENT_DIR = "/data/storage/el2/base/cache/hiview/event"; 511401458bSopenharmony_ci#ifdef STORAGE_SERVICE_ENABLE 521401458bSopenharmony_ciconst std::string BASE_DIR = "/data/storage/el2/base"; 531401458bSopenharmony_ciconst std::string CACHE_DIR = "/data/storage/el2/base/cache"; 541401458bSopenharmony_ciconst std::string HIVIEW_DIR = "/data/storage/el2/base/cache/hiview"; 551401458bSopenharmony_ciconst std::string PARENT_DIR_PERMISSION = "g:1201:x"; 561401458bSopenharmony_ciconst std::string SUB_DIR_PERMISSION = "g:1201:rwx"; 571401458bSopenharmony_ciconstexpr int ACL_SUCC = 0; 581401458bSopenharmony_ci#endif 591401458bSopenharmony_ci} 601401458bSopenharmony_ci 611401458bSopenharmony_ciint32_t HiSysEventDelegate::AddListener(const std::shared_ptr<HiSysEventBaseListener> listener, 621401458bSopenharmony_ci const std::vector<ListenerRule>& rules) 631401458bSopenharmony_ci{ 641401458bSopenharmony_ci auto service = GetSysEventService(); 651401458bSopenharmony_ci if (service == nullptr) { 661401458bSopenharmony_ci HILOG_ERROR(LOG_CORE, "Fail to get service."); 671401458bSopenharmony_ci return ERR_SYS_EVENT_SERVICE_NOT_FOUND; 681401458bSopenharmony_ci } 691401458bSopenharmony_ci std::vector<SysEventRule> eventRules; 701401458bSopenharmony_ci ConvertListenerRule(rules, eventRules); 711401458bSopenharmony_ci 721401458bSopenharmony_ci if (!spListenerCallBack) { 731401458bSopenharmony_ci spListenerCallBack = new OHOS::HiviewDFX::HiSysEventListenerProxy(listener); 741401458bSopenharmony_ci } 751401458bSopenharmony_ci 761401458bSopenharmony_ci SysEventServiceProxy sysEventService(service); 771401458bSopenharmony_ci service->RemoveDeathRecipient(spListenerCallBack->GetCallbackDeathRecipient()); 781401458bSopenharmony_ci return sysEventService.AddListener(eventRules, spListenerCallBack); 791401458bSopenharmony_ci} 801401458bSopenharmony_ci 811401458bSopenharmony_ciint32_t HiSysEventDelegate::RemoveListener(const std::shared_ptr<HiSysEventBaseListener> listener) 821401458bSopenharmony_ci{ 831401458bSopenharmony_ci if (!spListenerCallBack) { 841401458bSopenharmony_ci return ERR_LISTENER_NOT_EXIST; 851401458bSopenharmony_ci } 861401458bSopenharmony_ci auto service = GetSysEventService(); 871401458bSopenharmony_ci if (service == nullptr) { 881401458bSopenharmony_ci HILOG_ERROR(LOG_CORE, "Fail to get service."); 891401458bSopenharmony_ci return ERR_SYS_EVENT_SERVICE_NOT_FOUND; 901401458bSopenharmony_ci } 911401458bSopenharmony_ci SysEventServiceProxy sysEventService(service); 921401458bSopenharmony_ci return sysEventService.RemoveListener(spListenerCallBack); 931401458bSopenharmony_ci} 941401458bSopenharmony_ci 951401458bSopenharmony_ciint32_t HiSysEventDelegate::SetDebugMode(const std::shared_ptr<HiSysEventBaseListener> listener, 961401458bSopenharmony_ci const bool mode) 971401458bSopenharmony_ci{ 981401458bSopenharmony_ci if (!spListenerCallBack) { 991401458bSopenharmony_ci return ERR_LISTENER_NOT_EXIST; 1001401458bSopenharmony_ci } 1011401458bSopenharmony_ci auto service = GetSysEventService(); 1021401458bSopenharmony_ci if (service == nullptr) { 1031401458bSopenharmony_ci HILOG_ERROR(LOG_CORE, "Fail to get service."); 1041401458bSopenharmony_ci return ERR_SYS_EVENT_SERVICE_NOT_FOUND; 1051401458bSopenharmony_ci } 1061401458bSopenharmony_ci SysEventServiceProxy sysEventService(service); 1071401458bSopenharmony_ci return sysEventService.SetDebugMode(spListenerCallBack, mode); 1081401458bSopenharmony_ci} 1091401458bSopenharmony_ci 1101401458bSopenharmony_ciint32_t HiSysEventDelegate::Query(const struct QueryArg& arg, 1111401458bSopenharmony_ci const std::vector<QueryRule>& rules, 1121401458bSopenharmony_ci const std::shared_ptr<HiSysEventBaseQueryCallback> callback) const 1131401458bSopenharmony_ci{ 1141401458bSopenharmony_ci auto service = GetSysEventService(); 1151401458bSopenharmony_ci if (service == nullptr) { 1161401458bSopenharmony_ci HILOG_ERROR(LOG_CORE, "Fail to get service."); 1171401458bSopenharmony_ci return ERR_SYS_EVENT_SERVICE_NOT_FOUND; 1181401458bSopenharmony_ci } 1191401458bSopenharmony_ci 1201401458bSopenharmony_ci std::vector<SysEventQueryRule> hospRules; 1211401458bSopenharmony_ci ConvertQueryRule(rules, hospRules); 1221401458bSopenharmony_ci 1231401458bSopenharmony_ci sptr<HiSysEventQueryProxy> spCallBack(new OHOS::HiviewDFX::HiSysEventQueryProxy(callback)); 1241401458bSopenharmony_ci 1251401458bSopenharmony_ci SysEventServiceProxy sysEventService(service); 1261401458bSopenharmony_ci QueryArgument queryArgument(arg.beginTime, arg.endTime, arg.maxEvents, arg.fromSeq, arg.toSeq); 1271401458bSopenharmony_ci return sysEventService.Query(queryArgument, hospRules, spCallBack); 1281401458bSopenharmony_ci} 1291401458bSopenharmony_ci 1301401458bSopenharmony_ciint64_t HiSysEventDelegate::Export(const struct QueryArg& arg, const std::vector<QueryRule>& rules) const 1311401458bSopenharmony_ci{ 1321401458bSopenharmony_ci auto service = GetSysEventService(); 1331401458bSopenharmony_ci if (service == nullptr) { 1341401458bSopenharmony_ci HILOG_ERROR(LOG_CORE, "Fail to get service."); 1351401458bSopenharmony_ci return ERR_SYS_EVENT_SERVICE_NOT_FOUND; 1361401458bSopenharmony_ci } 1371401458bSopenharmony_ci auto res = CreateHiviewDir(); 1381401458bSopenharmony_ci if (!res) { 1391401458bSopenharmony_ci HILOG_ERROR(LOG_CORE, "Fail to create hiview dir."); 1401401458bSopenharmony_ci return ERR_SYS_EVENT_SERVICE_NOT_FOUND; 1411401458bSopenharmony_ci } 1421401458bSopenharmony_ci res = SetDirPermission(); 1431401458bSopenharmony_ci if (!res) { 1441401458bSopenharmony_ci HILOG_ERROR(LOG_CORE, "Fail to set ACL permission."); 1451401458bSopenharmony_ci return ERR_SYS_EVENT_SERVICE_NOT_FOUND; 1461401458bSopenharmony_ci } 1471401458bSopenharmony_ci std::vector<SysEventQueryRule> hospRules; 1481401458bSopenharmony_ci ConvertQueryRule(rules, hospRules); 1491401458bSopenharmony_ci SysEventServiceProxy sysEventService(service); 1501401458bSopenharmony_ci QueryArgument queryArgument(arg.beginTime, arg.endTime, arg.maxEvents, arg.fromSeq, arg.toSeq); 1511401458bSopenharmony_ci return sysEventService.Export(queryArgument, hospRules); 1521401458bSopenharmony_ci} 1531401458bSopenharmony_ci 1541401458bSopenharmony_ciint64_t HiSysEventDelegate::Subscribe(const std::vector<QueryRule>& rules) const 1551401458bSopenharmony_ci{ 1561401458bSopenharmony_ci auto service = GetSysEventService(); 1571401458bSopenharmony_ci if (service == nullptr) { 1581401458bSopenharmony_ci HILOG_ERROR(LOG_CORE, "Fail to get service."); 1591401458bSopenharmony_ci return ERR_SYS_EVENT_SERVICE_NOT_FOUND; 1601401458bSopenharmony_ci } 1611401458bSopenharmony_ci 1621401458bSopenharmony_ci auto res = CreateHiviewDir(); 1631401458bSopenharmony_ci if (!res) { 1641401458bSopenharmony_ci HILOG_ERROR(LOG_CORE, "Fail to create hiview dir."); 1651401458bSopenharmony_ci return ERR_SYS_EVENT_SERVICE_NOT_FOUND; 1661401458bSopenharmony_ci } 1671401458bSopenharmony_ci res = SetDirPermission(); 1681401458bSopenharmony_ci if (!res) { 1691401458bSopenharmony_ci HILOG_ERROR(LOG_CORE, "Fail to set ACL permission."); 1701401458bSopenharmony_ci return ERR_SYS_EVENT_SERVICE_NOT_FOUND; 1711401458bSopenharmony_ci } 1721401458bSopenharmony_ci 1731401458bSopenharmony_ci std::vector<SysEventQueryRule> hospRules; 1741401458bSopenharmony_ci ConvertQueryRule(rules, hospRules); 1751401458bSopenharmony_ci 1761401458bSopenharmony_ci SysEventServiceProxy sysEventService(service); 1771401458bSopenharmony_ci return sysEventService.AddSubscriber(hospRules); 1781401458bSopenharmony_ci} 1791401458bSopenharmony_ci 1801401458bSopenharmony_ciint32_t HiSysEventDelegate::Unsubscribe() const 1811401458bSopenharmony_ci{ 1821401458bSopenharmony_ci auto service = GetSysEventService(); 1831401458bSopenharmony_ci if (service == nullptr) { 1841401458bSopenharmony_ci HILOG_ERROR(LOG_CORE, "Fail to get service."); 1851401458bSopenharmony_ci return ERR_SYS_EVENT_SERVICE_NOT_FOUND; 1861401458bSopenharmony_ci } 1871401458bSopenharmony_ci SysEventServiceProxy sysEventService(service); 1881401458bSopenharmony_ci return sysEventService.RemoveSubscriber(); 1891401458bSopenharmony_ci} 1901401458bSopenharmony_ci 1911401458bSopenharmony_ciHiSysEventDelegate::~HiSysEventDelegate() 1921401458bSopenharmony_ci{ 1931401458bSopenharmony_ci} 1941401458bSopenharmony_ci 1951401458bSopenharmony_civoid HiSysEventDelegate::ConvertListenerRule(const std::vector<ListenerRule>& rules, 1961401458bSopenharmony_ci std::vector<SysEventRule>& sysRules) const 1971401458bSopenharmony_ci{ 1981401458bSopenharmony_ci for_each(rules.cbegin(), rules.cend(), [&sysRules](const ListenerRule& rule) { 1991401458bSopenharmony_ci if (rule.GetTag().empty()) { 2001401458bSopenharmony_ci sysRules.emplace_back(rule.GetDomain(), rule.GetEventName(), rule.GetRuleType(), rule.GetEventType()); 2011401458bSopenharmony_ci } else { 2021401458bSopenharmony_ci sysRules.emplace_back(rule.GetTag(), rule.GetRuleType(), rule.GetEventType()); 2031401458bSopenharmony_ci } 2041401458bSopenharmony_ci }); 2051401458bSopenharmony_ci} 2061401458bSopenharmony_ci 2071401458bSopenharmony_civoid HiSysEventDelegate::ConvertQueryRule(const std::vector<QueryRule>& rules, 2081401458bSopenharmony_ci std::vector<SysEventQueryRule>& sysRules) const 2091401458bSopenharmony_ci{ 2101401458bSopenharmony_ci for_each(rules.cbegin(), rules.cend(), [&sysRules](const QueryRule &rule) { 2111401458bSopenharmony_ci std::vector<std::string> events; 2121401458bSopenharmony_ci auto eventList = rule.GetEventList(); 2131401458bSopenharmony_ci for_each(eventList.cbegin(), eventList.cend(), [&](const std::string &event) { 2141401458bSopenharmony_ci events.push_back(event); 2151401458bSopenharmony_ci }); 2161401458bSopenharmony_ci sysRules.emplace_back(rule.GetDomain(), events, rule.GetRuleType(), rule.GetEventType(), rule.GetCondition()); 2171401458bSopenharmony_ci }); 2181401458bSopenharmony_ci} 2191401458bSopenharmony_ci 2201401458bSopenharmony_civoid HiSysEventDelegate::BinderFunc() 2211401458bSopenharmony_ci{ 2221401458bSopenharmony_ci IPCSkeleton::JoinWorkThread(); 2231401458bSopenharmony_ci} 2241401458bSopenharmony_ci 2251401458bSopenharmony_cisptr<IRemoteObject> HiSysEventDelegate::GetSysEventService() const 2261401458bSopenharmony_ci{ 2271401458bSopenharmony_ci sptr<ISystemAbilityManager> sam = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 2281401458bSopenharmony_ci if (sam == nullptr) { 2291401458bSopenharmony_ci return nullptr; 2301401458bSopenharmony_ci } 2311401458bSopenharmony_ci return sam->CheckSystemAbility(DFX_SYS_EVENT_SERVICE_ABILITY_ID); 2321401458bSopenharmony_ci} 2331401458bSopenharmony_ci 2341401458bSopenharmony_cibool HiSysEventDelegate::CreateHiviewDir() const 2351401458bSopenharmony_ci{ 2361401458bSopenharmony_ci if (FileUtil::IsFileExists(EVENT_DIR)) { 2371401458bSopenharmony_ci return true; 2381401458bSopenharmony_ci } 2391401458bSopenharmony_ci if (!FileUtil::ForceCreateDirectory(EVENT_DIR)) { 2401401458bSopenharmony_ci HILOG_ERROR(LOG_CORE, "failed to create event dir, errno=%{public}d.", errno); 2411401458bSopenharmony_ci return false; 2421401458bSopenharmony_ci } 2431401458bSopenharmony_ci return true; 2441401458bSopenharmony_ci} 2451401458bSopenharmony_ci 2461401458bSopenharmony_cibool HiSysEventDelegate::SetDirPermission() const 2471401458bSopenharmony_ci{ 2481401458bSopenharmony_ci#ifdef STORAGE_SERVICE_ENABLE 2491401458bSopenharmony_ci int aclBaseRet = AclSetAccess(BASE_DIR, PARENT_DIR_PERMISSION); 2501401458bSopenharmony_ci if (aclBaseRet != ACL_SUCC) { 2511401458bSopenharmony_ci HILOG_ERROR(LOG_CORE, "Set ACL failed , baseDirPath= %{public}s ret = %{public}d!!!!", 2521401458bSopenharmony_ci BASE_DIR.c_str(), aclBaseRet); 2531401458bSopenharmony_ci return false; 2541401458bSopenharmony_ci } 2551401458bSopenharmony_ci int aclCacheRet = AclSetAccess(CACHE_DIR, PARENT_DIR_PERMISSION); 2561401458bSopenharmony_ci if (aclCacheRet != ACL_SUCC) { 2571401458bSopenharmony_ci HILOG_ERROR(LOG_CORE, "Set ACL failed , cacheDirPath= %{public}s ret = %{public}d!!!!", 2581401458bSopenharmony_ci CACHE_DIR.c_str(), aclCacheRet); 2591401458bSopenharmony_ci return false; 2601401458bSopenharmony_ci } 2611401458bSopenharmony_ci int aclHiviewRet = AclSetAccess(HIVIEW_DIR, PARENT_DIR_PERMISSION); 2621401458bSopenharmony_ci if (aclHiviewRet != ACL_SUCC) { 2631401458bSopenharmony_ci HILOG_ERROR(LOG_CORE, "Set ACL failed , hiviewDirPath= %{public}s ret = %{public}d!!!!", 2641401458bSopenharmony_ci HIVIEW_DIR.c_str(), aclHiviewRet); 2651401458bSopenharmony_ci return false; 2661401458bSopenharmony_ci } 2671401458bSopenharmony_ci int aclRet = AclSetAccess(EVENT_DIR, SUB_DIR_PERMISSION); 2681401458bSopenharmony_ci if (aclRet != ACL_SUCC) { 2691401458bSopenharmony_ci HILOG_ERROR(LOG_CORE, "Set ACL failed , eventDirPath= %{public}s ret = %{public}d!!!!", 2701401458bSopenharmony_ci EVENT_DIR.c_str(), aclRet); 2711401458bSopenharmony_ci return false; 2721401458bSopenharmony_ci } 2731401458bSopenharmony_ci return true; 2741401458bSopenharmony_ci#else 2751401458bSopenharmony_ci return false; 2761401458bSopenharmony_ci#endif // STORAGE_SERVICE_ENABLE 2771401458bSopenharmony_ci} 2781401458bSopenharmony_ci 2791401458bSopenharmony_ci} // namespace HiviewDFX 2801401458bSopenharmony_ci} // namespace OHOS 281