• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2025 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 #define LOG_TAG "HiViewAdapter"
16 
17 #include "hiview_datashare.h"
18 
19 #include <algorithm>
20 #include <iomanip>
21 #include <sstream>
22 #include "access_token.h"
23 #include "accesstoken_kit.h"
24 #include "log_print.h"
25 
26 namespace OHOS {
27 namespace DataShare {
28 
29 using namespace std::chrono;
30 
31 namespace {
32     constexpr char DOMAIN[] = "DISTDATAMGR";
33     constexpr const char *EVENT_NAME = "DISTRIBUTED_DATA_SHARE_FAULT";
34     constexpr const size_t PARAMS_SIZE = 8;
35     // digits for milliseconds
36     constexpr uint8_t MILLSECOND_DIGIT = 3;
37 }
38 
ReportDataFault(const DataShareFaultInfo & faultInfo)39 void HiViewFaultAdapter::ReportDataFault(const DataShareFaultInfo &faultInfo)
40 {
41     auto now = std::chrono::system_clock::now();
42     auto t = std::chrono::system_clock::to_time_t(now);
43     std::stringstream ss;
44     ss << std::put_time(std::localtime(&t), "%F %T");
45     // get milliseconds
46     auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
47     ss << '.' << std::setfill('0') << std::setw(MILLSECOND_DIGIT) << ms.count();
48     auto time = ss.str();
49     HiSysEventParam faultTime = { .name = "FAULT_TIME", .t = HISYSEVENT_STRING,
50         .v = { .s = const_cast<char*>(time.c_str()) }, .arraySize = 0 };
51     HiSysEventParam faultType = { .name = "FAULT_TYPE", .t = HISYSEVENT_STRING,
52         .v = { .s = const_cast<char*>(faultInfo.faultType.c_str()) }, .arraySize = 0 };
53     HiSysEventParam bundleName = { .name = "BUNDLE_NAME", .t = HISYSEVENT_STRING,
54         .v = { .s = const_cast<char*>(faultInfo.bundleName.c_str()) }, .arraySize = 0 };
55     HiSysEventParam moduleName = { .name = "MODULE_NAME", .t = HISYSEVENT_STRING,
56         .v = { .s = const_cast<char*>(faultInfo.moduleName.c_str()) }, .arraySize = 0 };
57     HiSysEventParam storeName = { .name = "STORE_NAME", .t = HISYSEVENT_STRING,
58         .v = { .s = const_cast<char*>(faultInfo.storeName.c_str()) }, .arraySize = 0 };
59     HiSysEventParam businessType = { .name = "BUSINESS_TYPE", .t = HISYSEVENT_STRING,
60         .v = { .s = const_cast<char*>(faultInfo.businessType.c_str()) }, .arraySize = 0 };
61     HiSysEventParam errorCode = { .name = "ERROR_CODE", .t = HISYSEVENT_INT32,
62         .v = { .i32 = faultInfo.errorCode }, .arraySize = 0 };
63     HiSysEventParam appendix = { .name = "APPENDIX", .t = HISYSEVENT_STRING,
64         .v = { .s = const_cast<char*>(faultInfo.appendix.c_str()) }, .arraySize = 0 };
65     HiSysEventParam params[] = { faultTime, faultType, bundleName, moduleName,
66         storeName, businessType, errorCode, appendix };
67     int res = OH_HiSysEvent_Write(DOMAIN, EVENT_NAME, HISYSEVENT_FAULT, params, PARAMS_SIZE);
68     if (res != 0) {
69         ZLOGI("OH_HiSysEvent_Write fail, res = %{public}d", res);
70     }
71 }
72 
GetCallingName(uint32_t callingTokenid)73 std::pair<std::string, int> HiViewFaultAdapter::GetCallingName(uint32_t callingTokenid)
74 {
75     std::string callingName;
76     auto tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callingTokenid);
77     int result = -1;
78     if (tokenType == Security::AccessToken::TOKEN_HAP) {
79         Security::AccessToken::HapTokenInfo tokenInfo;
80         result = Security::AccessToken::AccessTokenKit::GetHapTokenInfo(callingTokenid, tokenInfo);
81         if (result == Security::AccessToken::RET_SUCCESS) {
82             callingName = tokenInfo.bundleName;
83         }
84     } else if (tokenType == Security::AccessToken::TOKEN_NATIVE || tokenType == Security::AccessToken::TOKEN_SHELL) {
85         Security::AccessToken::NativeTokenInfo tokenInfo;
86         result = Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(callingTokenid, tokenInfo);
87         if (result == Security::AccessToken::RET_SUCCESS) {
88             callingName = tokenInfo.processName;
89         }
90     } else {
91         ZLOGE("tokenType is invalid, tokenType:%{public}d, Tokenid:%{public}x", tokenType, callingTokenid);
92     }
93     return std::make_pair(callingName, result);
94 }
95 
96 } // namespace DataShare
97 } // namespace OHOS
98