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 "rdb_radar_reporter.h"
17 #include "rdb_errno.h"
18 #include "ipc_skeleton.h"
19 #include "accesstoken_kit.h"
20 #include "hisysevent_c.h"
21 
22 namespace OHOS::NativeRdb {
23 
24 using namespace Security::AccessToken;
25 
26 static constexpr const char* ORG_PKG_VALUE = "distributeddata";
27 static constexpr const char* EVENT_NAME = "DISTRIBUTED_RDB_BEHAVIOR";
28 static constexpr const char* UNKNOW = "unknow";
29 static constexpr const char* DISTRIBUTED_DATAMGR = "DISTDATAMGR";
30 
31 std::string RdbRadar::hostPkg_{ "" };
32 std::mutex RdbRadar::mutex_;
33 
RdbRadar(Scene scene,const char * funcName,std::string bundleName)34 RdbRadar::RdbRadar(Scene scene, const char *funcName, std::string bundleName)
35     : scene_(scene), funcName_(funcName), bundleName_(bundleName)
36 {
37     if (funcName_ == nullptr) {
38         funcName_ = UNKNOW;
39     }
40     LocalReport(scene_, funcName_, STATE_START);
41 }
42 
~RdbRadar()43 RdbRadar::~RdbRadar()
44 {
45     LocalReport(scene_, funcName_, STATE_FINISH, errCode_);
46 }
47 
operator =(int errCode)48 RdbRadar& RdbRadar::operator=(int errCode)
49 {
50     errCode_ = errCode;
51     return *this;
52 }
53 
54 RdbRadar::operator int() const
55 {
56     return errCode_;
57 }
58 
LocalReport(int bizSence,const char * funcName,int state,int errCode)59 void RdbRadar::LocalReport(int bizSence, const char* funcName, int state, int errCode)
60 {
61     int stageRes = static_cast<int>(StageRes::RES_SUCCESS);
62     if (errCode != E_OK) {
63         stageRes = static_cast<int>(StageRes::RES_FAILED);
64     }
65 
66     std::string hostPkg = GetHostPkgInfo();
67     char *hostPkgPtr = hostPkg.data();
68     if (hostPkgPtr == nullptr) {
69         return;
70     }
71     HiSysEventParam params[] = {
72         {.name = "ORG_PKG", .t = HISYSEVENT_STRING, .v = { .s = const_cast<char *>(ORG_PKG_VALUE) }, .arraySize = 0, },
73         {.name = "FUNC", .t = HISYSEVENT_STRING, .v = { .s = const_cast<char *>(funcName) }, .arraySize = 0, },
74         {.name = "BIZ_SCENE", .t = HISYSEVENT_INT32, .v = { .i32 = bizSence }, .arraySize = 0, },
75         {.name = "BIZ_STAGE", .t = HISYSEVENT_INT32, .v = { .i32 = SYNC_STAGE_RUN }, .arraySize = 0, },
76         {.name = "STAGE_RES", .t = HISYSEVENT_INT32, .v = { .i32 = stageRes }, .arraySize = 0, },
77         {.name = "ERROR_CODE", .t = HISYSEVENT_INT32, .v = { .i32 = errCode }, .arraySize = 0, },
78         {.name = "BIZ_STATE", .t = HISYSEVENT_INT32, .v = { .i32 = state }, .arraySize = 0, },
79         {.name = "HOST_PKG", .t = HISYSEVENT_STRING, .v = { .s = hostPkgPtr }, .arraySize = 0, },
80     };
81 
82     OH_HiSysEvent_Write(DISTRIBUTED_DATAMGR, EVENT_NAME,
83         HISYSEVENT_BEHAVIOR, params, sizeof(params) / sizeof(params[0]));
84     return;
85 }
86 
GetHostPkgInfo()87 std::string RdbRadar::GetHostPkgInfo()
88 {
89     std::lock_guard<std::mutex> lockGuard(mutex_);
90     if (!hostPkg_.empty()) {
91         return hostPkg_;
92     }
93     auto tokenId = IPCSkeleton::GetCallingTokenID();
94     auto tokenType = AccessTokenKit::GetTokenTypeFlag(tokenId);
95     if ((tokenType == TOKEN_NATIVE) || (tokenType == TOKEN_SHELL)) {
96         NativeTokenInfo tokenInfo;
97         if (AccessTokenKit::GetNativeTokenInfo(tokenId, tokenInfo) == 0) {
98             hostPkg_ = tokenInfo.processName;
99         }
100     } else {
101         hostPkg_ = bundleName_;
102     }
103     return hostPkg_;
104 }
105 }