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 <unistd.h>
17 #include "faultlogger_ffi.h"
18 #include "faultlogger_client.h"
19 #include "faultlogger_info.h"
20 #include "hiview_logger.h"
21 #include <mutex>
22 #include "faultlog_query_result.h"
23 #include "faultlog_info.h"
24 #include "common_utils.h"
25 
26 using namespace OHOS::HiviewDFX;
27 
28 DEFINE_LOG_LABEL(0xD002D11, "Faultlogger-cj");
29 std::mutex g_mutex;
30 
MallocCString(const std::string& origin)31 char* MallocCString(const std::string& origin)
32 {
33     if (origin.empty()) {
34         return nullptr;
35     }
36     auto length = origin.length() + 1;
37     char* res =  static_cast<char*>(malloc(sizeof(char) * length));
38     if (res == nullptr) {
39         return nullptr;
40     }
41     return std::char_traits<char>::copy(res, origin.c_str(), length);
42 }
43 
ConvertInform(std::unique_ptr<FaultLogInfo> faultLogInfo)44 CJFaultLogInfo ConvertInform(std::unique_ptr<FaultLogInfo> faultLogInfo)
45 {
46     CJFaultLogInfo ret = {
47         .pid = faultLogInfo->GetProcessId(),
48         .uid = faultLogInfo->GetId(),
49         .type = faultLogInfo->GetFaultType(),
50         .ts = faultLogInfo->GetTimeStamp(),
51         .reason = faultLogInfo->GetFaultReason(),
52         .module = faultLogInfo->GetModuleName(),
53         .summary = faultLogInfo->GetFaultSummary(),
54     };
55 
56     int fd = faultLogInfo->GetRawFileDescriptor();
57     if (fd < 0) {
58         HIVIEW_LOGE("pid %{public}d Fail to get fd:%{public}d\n", faultLogInfo->GetProcessId(), fd);
59         ret.fullLog = "Fail to get log, fd is " + std::to_string(fd);
60         return ret;
61     }
62     while (fd >= 0) {
63         char buf[BUF_SIZE_512] = {0};
64         int nread = TEMP_FAILURE_RETRY(read((fd), buf, BUF_SIZE_512 - 1));
65         if (nread == -1) {
66             if (errno == EAGAIN) {
67                 continue;
68             } else {
69                 break;
70             }
71         } else if (nread == 0) {
72             break;
73         }
74         ret.fullLog += buf;
75     }
76     return ret;
77 }
78 
FaultLogExecute(std::unique_ptr<CFaultLogInfoContext>& faultLogInfoContext)79 void FaultLogExecute(std::unique_ptr<CFaultLogInfoContext>& faultLogInfoContext)
80 {
81     std::lock_guard<std::mutex> lock(g_mutex);
82     const int maxQueryCount = 10;
83     int currentCount = 0;
84     auto faultLogResult = QuerySelfFaultLog((FaultLogType)faultLogInfoContext->faultType,
85         maxQueryCount);
86     if (faultLogResult == nullptr) {
87         faultLogInfoContext->resolved = true;
88         return;
89     }
90 
91     while (faultLogResult->HasNext()) {
92         if (currentCount >= maxQueryCount) {
93             break;
94         }
95         auto faultLogInfo = faultLogResult->Next();
96         if (faultLogInfo == nullptr) {
97             break;
98         }
99         currentCount++;
100         faultLogInfoContext->infoVector.push_back(ConvertInform(std::move(faultLogInfo)));
101     }
102     faultLogInfoContext->resolved = true;
103 }
104 
105 extern "C" {
FfiFaultLoggerQuery(int32_t faultType, int32_t &code)106     CArrFaultLogInfo FfiFaultLoggerQuery(int32_t faultType, int32_t &code)
107     {
108         CArrFaultLogInfo faultInfos = {.head = nullptr, .size = 0};
109         if (!CheckFaultloggerStatus()) {
110             code = ERR_SERVICE_STATUS;
111             return faultInfos;
112         }
113 
114         auto faultLogInfoContext = std::make_unique<CFaultLogInfoContext>();
115 
116         faultLogInfoContext->faultType = faultType;
117         FaultLogExecute(faultLogInfoContext);
118         faultInfos.size = static_cast<int64_t>(faultLogInfoContext->infoVector.size());
119         CFaultLogInfo *retValue = static_cast<CFaultLogInfo *>(malloc(sizeof(CFaultLogInfo) * faultInfos.size));
120         if (faultLogInfoContext->resolved) {
121             int i = 0;
122             for (auto& infoItem : faultLogInfoContext->infoVector) {
123                 retValue[i].pid = infoItem.pid;
124                 retValue[i].uid = infoItem.uid;
125                 retValue[i].faultLogType = infoItem.type;
126                 retValue[i].timestamp = infoItem.ts;
127                 retValue[i].reason = MallocCString(infoItem.reason);
128                 retValue[i].module = MallocCString(infoItem.module);
129                 retValue[i].summary = MallocCString(infoItem.summary);
130                 retValue[i].fullLog = MallocCString(infoItem.fullLog);
131                 ++i;
132                 HIVIEW_LOGI("add element when resovled pid = %{public}d, uid = %{public}d, ts = %{public}" PRId64,
133                 infoItem.pid, infoItem.uid, infoItem.ts);
134             }
135             faultInfos.head = retValue;
136         } else {
137             HIVIEW_LOGE("get signal info list failed");
138         }
139         return faultInfos;
140     }
141 }