1800b99b8Sopenharmony_ci/*
2800b99b8Sopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License.
5800b99b8Sopenharmony_ci * You may obtain a copy of the License at
6800b99b8Sopenharmony_ci *
7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0
8800b99b8Sopenharmony_ci *
9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and
13800b99b8Sopenharmony_ci * limitations under the License.
14800b99b8Sopenharmony_ci */
15800b99b8Sopenharmony_ci#ifndef DFX_FAULTLOGGERD_H
16800b99b8Sopenharmony_ci#define DFX_FAULTLOGGERD_H
17800b99b8Sopenharmony_ci
18800b99b8Sopenharmony_ci#include <cinttypes>
19800b99b8Sopenharmony_ci#include <map>
20800b99b8Sopenharmony_ci#include <string>
21800b99b8Sopenharmony_ci
22800b99b8Sopenharmony_ci#include "faultloggerd_client.h"
23800b99b8Sopenharmony_ci#include "fault_logger_pipe.h"
24800b99b8Sopenharmony_ci
25800b99b8Sopenharmony_cinamespace OHOS {
26800b99b8Sopenharmony_cinamespace HiviewDFX {
27800b99b8Sopenharmony_ciclass DumpStats {
28800b99b8Sopenharmony_cipublic:
29800b99b8Sopenharmony_ci    int32_t pid = 0;
30800b99b8Sopenharmony_ci    uint64_t requestTime = 0;
31800b99b8Sopenharmony_ci    uint64_t signalTime = 0;
32800b99b8Sopenharmony_ci    uint64_t processdumpStartTime = 0;
33800b99b8Sopenharmony_ci    uint64_t processdumpFinishTime = 0;
34800b99b8Sopenharmony_ci    uint64_t dumpCatcherFinishTime = 0;
35800b99b8Sopenharmony_ci    int32_t result = 0;
36800b99b8Sopenharmony_ci    std::string summary;
37800b99b8Sopenharmony_ci    std::string callerProcessName;
38800b99b8Sopenharmony_ci    std::string callerElfName;
39800b99b8Sopenharmony_ci    std::string targetProcessName;
40800b99b8Sopenharmony_ci};
41800b99b8Sopenharmony_ciclass FaultLoggerDaemon {
42800b99b8Sopenharmony_cipublic:
43800b99b8Sopenharmony_ci    FaultLoggerDaemon();
44800b99b8Sopenharmony_ci    virtual ~FaultLoggerDaemon() {};
45800b99b8Sopenharmony_ci    int32_t StartServer();
46800b99b8Sopenharmony_ci    virtual bool InitEnvironment();
47800b99b8Sopenharmony_ci    void RecordFileCreation(int32_t type, int32_t pid);
48800b99b8Sopenharmony_ci    void ClearTimeOutRecords();
49800b99b8Sopenharmony_ci    bool IsCrashed(int32_t pid);
50800b99b8Sopenharmony_ci
51800b99b8Sopenharmony_ci    int32_t CreateFileForRequest(int32_t type, int32_t pid, int32_t tid, uint64_t time) const;
52800b99b8Sopenharmony_ci#ifdef FAULTLOGGERD_FUZZER
53800b99b8Sopenharmony_ci    bool HandleStaticForFuzzer(int32_t type, uint32_t callerUid);
54800b99b8Sopenharmony_ci    void HandleRequestForFuzzer(int32_t epollFd, int32_t connectionFd,
55800b99b8Sopenharmony_ci                                const FaultLoggerdRequest *requestConst, FaultLoggerdRequest *request);
56800b99b8Sopenharmony_ci#endif
57800b99b8Sopenharmony_ci
58800b99b8Sopenharmony_ciprivate:
59800b99b8Sopenharmony_ci    void AddEvent(int32_t epollFd, int32_t addFd, uint32_t event);
60800b99b8Sopenharmony_ci    void DelEvent(int32_t epollFd, int32_t delFd, uint32_t event);
61800b99b8Sopenharmony_ci    void HandleAccept(int32_t epollFd, int32_t socketFd);
62800b99b8Sopenharmony_ci    void HandleRequest(int32_t epollFd, int32_t connectionFd);
63800b99b8Sopenharmony_ci
64800b99b8Sopenharmony_ci    void RemoveTempFileIfNeed() const;
65800b99b8Sopenharmony_ci    void HandleDefaultClientRequest(int32_t connectionFd, const FaultLoggerdRequest* request);
66800b99b8Sopenharmony_ci    void HandleLogFileDesClientRequest(int32_t connectionFd, const FaultLoggerdRequest* request);
67800b99b8Sopenharmony_ci    void HandlePrintTHilogClientRequest(int32_t const connectionFd, FaultLoggerdRequest* request);
68800b99b8Sopenharmony_ci    FaultLoggerCheckPermissionResp SecurityCheck(int32_t connectionFd, FaultLoggerdRequest* request);
69800b99b8Sopenharmony_ci    void HandlePermissionRequest(int32_t connectionFd, FaultLoggerdRequest* request);
70800b99b8Sopenharmony_ci    void HandleSdkDumpRequest(int32_t connectionFd, FaultLoggerdRequest* request);
71800b99b8Sopenharmony_ci    void HandlePipeFdClientRequest(int32_t connectionFd, FaultLoggerdRequest* request);
72800b99b8Sopenharmony_ci    void HandleReadBuf(int& fd, int32_t connectionFd, FaultLoggerdRequest* request,
73800b99b8Sopenharmony_ci        FaultLoggerPipe2* faultLoggerPipe);
74800b99b8Sopenharmony_ci    void HandleWriteBuf(int& fd, FaultLoggerPipe2* faultLoggerPipe);
75800b99b8Sopenharmony_ci    void HandleReadRes(int& fd, int32_t connectionFd, FaultLoggerdRequest* request,
76800b99b8Sopenharmony_ci        FaultLoggerPipe2* faultLoggerPipe);
77800b99b8Sopenharmony_ci    void HandleWriteRes(int& fd, FaultLoggerPipe2* faultLoggerPipe);
78800b99b8Sopenharmony_ci    void HandleJsonReadBuf(int& fd, int32_t connectionFd, FaultLoggerdRequest* request,
79800b99b8Sopenharmony_ci        FaultLoggerPipe2* faultLoggerPipe);
80800b99b8Sopenharmony_ci    void HandleJsonWriteBuf(int& fd, FaultLoggerPipe2* faultLoggerPipe);
81800b99b8Sopenharmony_ci    void HandleJsonReadRes(int& fd, int32_t connectionFd, FaultLoggerdRequest* request,
82800b99b8Sopenharmony_ci        FaultLoggerPipe2* faultLoggerPipe);
83800b99b8Sopenharmony_ci    void HandleJsonWriteRes(int& fd, FaultLoggerPipe2* faultLoggerPipe);
84800b99b8Sopenharmony_ci    void HandleDelete(FaultLoggerdRequest* request);
85800b99b8Sopenharmony_ci    void HandleRequestByPipeType(int& fd, int32_t connectionFd, FaultLoggerdRequest* request,
86800b99b8Sopenharmony_ci        FaultLoggerPipe2* faultLoggerPipe);
87800b99b8Sopenharmony_ci    void HandleExceptionRequest(int32_t connectionFd, FaultLoggerdRequest* request);
88800b99b8Sopenharmony_ci    void HandleRequestByClientType(int32_t connectionFd, FaultLoggerdRequest* request);
89800b99b8Sopenharmony_ci    bool CheckRequestCredential(int32_t connectionFd, FaultLoggerdRequest* request);
90800b99b8Sopenharmony_ci    virtual bool CreateSockets();
91800b99b8Sopenharmony_ci    virtual bool CreateEventFd();
92800b99b8Sopenharmony_ci    void CleanupSockets();
93800b99b8Sopenharmony_ci    virtual void WaitForRequest();
94800b99b8Sopenharmony_ci    void CleanupEventFd();
95800b99b8Sopenharmony_ci    void HandleDumpStats(int32_t connectionFd, FaultLoggerdStatsRequest* request);
96800b99b8Sopenharmony_ci    void RemoveTimeoutDumpStats();
97800b99b8Sopenharmony_ci    void ReportDumpStats(const DumpStats& stat);
98800b99b8Sopenharmony_ci
99800b99b8Sopenharmony_ciprivate:
100800b99b8Sopenharmony_ci    int32_t defaultSocketFd_ = -1;
101800b99b8Sopenharmony_ci    int32_t crashSocketFd_ = -1;
102800b99b8Sopenharmony_ci    int32_t sdkdumpSocketFd_ = -1;
103800b99b8Sopenharmony_ci    int32_t eventFd_ = -1;
104800b99b8Sopenharmony_ci    bool isBeta_ = false;
105800b99b8Sopenharmony_ci    std::map<int32_t, int32_t> connectionMap_;
106800b99b8Sopenharmony_ci    std::map<int32_t, int64_t> crashTimeMap_;
107800b99b8Sopenharmony_ci    std::vector<DumpStats> stats_;
108800b99b8Sopenharmony_ci};
109800b99b8Sopenharmony_ci} // namespace HiviewDFX
110800b99b8Sopenharmony_ci} // namespace OHOS
111800b99b8Sopenharmony_ci#endif
112