• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_adapter.h"
17 #include "accesstoken_common_log.h"
18 #include "hisysevent.h"
19 #include "time_util.h"
20 
21 namespace OHOS {
22 namespace Security {
23 namespace AccessToken {
24 namespace {
25 static const std::string ACCESSTOKEN_PROCESS_NAME = "accesstoken_service";
26 static constexpr char ADD_DOMAIN[] = "PERFORMANCE";
27 }
28 
ReportSysEventPerformance()29 void ReportSysEventPerformance()
30 {
31     // accesstoken_service add CPU_SCENE_ENTRY system event in OnStart, avoid CPU statistics
32     long id = 1 << 0; // first scene
33     int64_t time = AccessToken::TimeUtil::GetCurrentTimestamp();
34 
35     int32_t ret = HiSysEventWrite(ADD_DOMAIN, "CPU_SCENE_ENTRY", HiviewDFX::HiSysEvent::EventType::BEHAVIOR,
36         "PACKAGE_NAME", ACCESSTOKEN_PROCESS_NAME, "SCENE_ID", std::to_string(id).c_str(), "HAPPEN_TIME", time);
37     if (ret != 0) {
38         LOGE(ATM_DOMAIN, ATM_TAG, "Failed to report performance, ret %{public}d.", ret);
39     }
40 }
41 
ReportSysEventServiceStart(const InitDfxInfo & info)42 void ReportSysEventServiceStart(const InitDfxInfo& info)
43 {
44     int32_t ret = HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "ACCESSTOKEN_SERVICE_START",
45         HiviewDFX::HiSysEvent::EventType::STATISTIC,
46         "PID", info.pid,
47         "HAP_SIZE", info.hapSize,
48         "NATIVE_SIZE", info.nativeSize,
49         "PERM_DEFINITION_SIZE", info.permDefSize,
50         "DLP_PERMISSION_SIZE", info.dlpSize,
51         "PARSE_CONFIG_FLAG", info.parseConfigFlag);
52     if (ret != 0) {
53         LOGE(ATM_DOMAIN, ATM_TAG, "Failed to write hisysevent write, ret %{public}d.", ret);
54     }
55 }
56 
ReportSysEventServiceStartError(SceneCode scene,const std::string & errMsg,int32_t errCode)57 void ReportSysEventServiceStartError(SceneCode scene, const std::string& errMsg, int32_t errCode)
58 {
59     int32_t ret = HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "ACCESSTOKEN_SERVICE_START_ERROR",
60         HiviewDFX::HiSysEvent::EventType::FAULT, "SCENE_CODE", scene, "ERROR_CODE", errCode, "ERROR_MSG", errMsg);
61     if (ret != 0) {
62         LOGE(ATM_DOMAIN, ATM_TAG, "Failed to write hisysevent write, ret %{public}d.", ret);
63     }
64 }
65 
ReportSysCommonEventError(int32_t ipcCode,int32_t errCode)66 void ReportSysCommonEventError(int32_t ipcCode, int32_t errCode)
67 {
68     if (GetThreadErrorMsgLen() == 0) {
69         return;
70     }
71     int32_t ret = HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "ACCESSTOKEN_EXCEPTION",
72         HiviewDFX::HiSysEvent::EventType::FAULT, "SCENE_CODE", ipcCode, "ERROR_CODE", errCode,
73         "ERROR_MSG", GetThreadErrorMsg());
74     if (ret != 0) {
75         LOGE(ATM_DOMAIN, ATM_TAG, "Failed to write hisysevent write, ret %{public}d.", ret);
76     }
77     ClearThreadErrorMsg();
78 }
79 
ReportSysEventAddHap(int32_t errorCode,const HapDfxInfo & info,bool needReportFault)80 void ReportSysEventAddHap(int32_t errorCode, const HapDfxInfo& info, bool needReportFault)
81 {
82     int32_t res = HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "ADD_HAP",
83         HiviewDFX::HiSysEvent::EventType::STATISTIC,
84         "SCENE_CODE", info.sceneCode,
85         "TOKENID", info.tokenId,
86         "ORI_TOKENID", info.oriTokenId,
87         "TOKENIDEX", static_cast<uint64_t>(info.tokenIdEx.tokenIDEx),
88         "USERID", info.userID,
89         "BUNDLENAME", info.bundleName,
90         "INSTINDEX", info.instIndex,
91         "DLP_TYPE", info.dlpType,
92         "IS_RESTORE", info.isRestore,
93         "PERM_INFO", info.permInfo,
94         "ACL_INFO", info.aclInfo,
95         "PREAUTH_INFO", info.preauthInfo,
96         "EXTEND_INFO", info.extendInfo,
97         "DURATION", info.duration,
98         "ERROR_CODE", errorCode);
99     if (res != 0) {
100         LOGE(ATM_DOMAIN, ATM_TAG, "Failed to write hisysevent write, ret %{public}d.", res);
101     }
102     if (needReportFault) {
103         ReportSysCommonEventError(info.ipcCode, errorCode);
104     }
105 }
106 
ReportSysEventUpdateHap(int32_t errorCode,const HapDfxInfo & info)107 void ReportSysEventUpdateHap(int32_t errorCode, const HapDfxInfo& info)
108 {
109     int32_t res = HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "UPDATE_HAP",
110         HiviewDFX::HiSysEvent::EventType::STATISTIC,
111         "TOKENID", info.tokenId,
112         "USERID", info.userID,
113         "BUNDLENAME", info.bundleName,
114         "INSTINDEX", info.instIndex,
115         "SCENE_CODE", CommonSceneCode::AT_COMMON_FINISH,
116         "ERROR_CODE", errorCode,
117         "TOKENIDEX", static_cast<uint64_t>(info.tokenIdEx.tokenIDEx),
118         "PERM_INFO", info.permInfo,
119         "ACL_INFO", info.aclInfo,
120         "PREAUTH_INFO", info.preauthInfo,
121         "EXTEND_INFO", info.extendInfo,
122         "DURATION", info.duration);
123     if (res != 0) {
124         LOGE(ATM_DOMAIN, ATM_TAG, "Failed to write hisysevent write, ret %{public}d.", res);
125     }
126     ReportSysCommonEventError(info.ipcCode, errorCode);
127 }
128 
ReportSysEventDelHap(int32_t errorCode,const HapDfxInfo & info)129 void ReportSysEventDelHap(int32_t errorCode, const HapDfxInfo& info)
130 {
131     int32_t res = HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "DEL_HAP",
132         HiviewDFX::HiSysEvent::EventType::STATISTIC,
133         "TOKENID", info.tokenId,
134         "USERID", info.userID,
135         "BUNDLENAME", info.bundleName,
136         "INSTINDEX", info.instIndex,
137         "SCENE_CODE", CommonSceneCode::AT_COMMON_FINISH,
138         "ERROR_CODE", errorCode,
139         "DURATION", info.duration);
140     if (res != 0) {
141         LOGE(ATM_DOMAIN, ATM_TAG, "Failed to write hisysevent write, ret %{public}d.", res);
142     }
143     ReportSysCommonEventError(info.ipcCode, errorCode);
144 }
145 
ReportSysEventDbException(AccessTokenDbSceneCode sceneCode,int32_t errCode,const std::string & tableName)146 void ReportSysEventDbException(AccessTokenDbSceneCode sceneCode, int32_t errCode, const std::string& tableName)
147 {
148     (void)HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "DATABASE_EXCEPTION",
149         HiviewDFX::HiSysEvent::EventType::FAULT, "SCENE_CODE", sceneCode, "ERROR_CODE", errCode,
150         "TABLE_NAME", tableName);
151 }
152 } // namespace AccessToken
153 } // namespace Security
154 } // namespace OHOS