• 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 "fault_logger_server.h"
17 
18 #include <sys/socket.h>
19 #include <sys/un.h>
20 #include <unistd.h>
21 
22 #include "dfx_define.h"
23 #include "dfx_log.h"
24 #include "faultloggerd_socket.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 
29 namespace {
30 constexpr const char* const FAULTLOGGERD_SERVER_TAG = "FAULT_LOGGER_SERVER";
31 }
32 
SocketServer(EpollManager & epollManager)33 SocketServer::SocketServer(EpollManager& epollManager) : epollManager_(epollManager) {}
34 
Init()35 bool SocketServer::Init()
36 {
37     std::unique_ptr<IFaultLoggerService> logFileDesService(new (std::nothrow) FileDesService());
38     AddService(LOG_FILE_DES_CLIENT, std::move(logFileDesService));
39 #ifndef HISYSEVENT_DISABLE
40     std::unique_ptr<IFaultLoggerService> reportExceptionService(new (std::nothrow) ExceptionReportService());
41     AddService(REPORT_EXCEPTION_CLIENT, std::move(reportExceptionService));
42     std::unique_ptr<IFaultLoggerService> statsClientService(new (std::nothrow) StatsService());
43     AddService(DUMP_STATS_CLIENT, std::move(statsClientService));
44 #endif
45     if (!AddServerListener(SERVER_SOCKET_NAME) || !AddServerListener(SERVER_CRASH_SOCKET_NAME)) {
46         return false;
47     }
48 #ifndef is_ohos_lite
49     AddService(PIPE_FD_CLIENT, std::make_unique<PipeService>());
50     AddService(SDK_DUMP_CLIENT, std::make_unique<SdkDumpService>());
51     if (!AddServerListener(SERVER_SDKDUMP_SOCKET_NAME)) {
52         return false;
53     }
54 #endif
55     return true;
56 }
57 
AddService(int32_t clientType,std::unique_ptr<IFaultLoggerService> service)58 void SocketServer::AddService(int32_t clientType, std::unique_ptr<IFaultLoggerService> service)
59 {
60     if (service) {
61         faultLoggerServices_.emplace_back(clientType, std::move(service));
62     }
63 }
64 
AddServerListener(const char * socketName)65 bool SocketServer::AddServerListener(const char* socketName)
66 {
67     int32_t fd;
68     constexpr int32_t maxConnection = 30;
69     if (!StartListen(fd, socketName, maxConnection)) {
70         return false;
71     }
72     std::unique_ptr<EpollListener> serverListener(new (std::nothrow)SocketServerListener(*this, fd, socketName));
73     return epollManager_.AddListener(std::move(serverListener));
74 }
75 
SocketServerListener(SocketServer & socketServer,int32_t fd,std::string socketName)76 SocketServer::SocketServerListener::SocketServerListener(SocketServer& socketServer, int32_t fd, std::string socketName)
77     : EpollListener(fd), socketServer_(socketServer), socketName_(std::move(socketName)) {}
78 
ClientRequestListener(SocketServerListener & socketServerListener,int32_t fd)79 SocketServer::ClientRequestListener::ClientRequestListener(SocketServerListener& socketServerListener, int32_t fd)
80     : EpollListener(fd), socketServerListener_(socketServerListener) {}
81 
GetTargetService(int32_t faultLoggerClientType) const82 IFaultLoggerService* SocketServer::ClientRequestListener::GetTargetService(int32_t faultLoggerClientType) const
83 {
84     for (const auto& faultLoggerServicePair : socketServerListener_.socketServer_.faultLoggerServices_) {
85         if (faultLoggerServicePair.first == faultLoggerClientType) {
86             return faultLoggerServicePair.second.get();
87         }
88     }
89     return nullptr;
90 }
91 
OnEventPoll()92 void SocketServer::ClientRequestListener::OnEventPoll()
93 {
94     constexpr int32_t maxBuffSize = 2048;
95     std::vector<uint8_t> buf(maxBuffSize, 0);
96     ssize_t nread = OHOS_TEMP_FAILURE_RETRY(read(GetFd(), buf.data(), maxBuffSize));
97     if (nread >= sizeof(RequestDataHead)) {
98         auto dataHead = reinterpret_cast<RequestDataHead*>(buf.data());
99         DFXLOGI("%{public}s :: %{public}s receive request from pid: %{public}d, clientType: %{public}d",
100                 FAULTLOGGERD_SERVER_TAG, socketServerListener_.socketName_.c_str(),
101                 dataHead->clientPid, dataHead->clientType);
102         IFaultLoggerService* service = GetTargetService(dataHead->clientType);
103         int32_t retCode = service ? service->OnReceiveMsg(socketServerListener_.socketName_, GetFd(), nread, buf)
104             : ResponseCode::UNKNOWN_CLIENT_TYPE;
105         if (retCode != ResponseCode::REQUEST_SUCCESS) {
106             DFXLOGW("%{public}s :: Failed to resolve the request for clientType: %{public}d, "
107                     "and retCode %{public}d", FAULTLOGGERD_SERVER_TAG, dataHead->clientType, retCode);
108             SendMsgToSocket(GetFd(), &retCode, sizeof(retCode));
109         }
110     }
111     socketServerListener_.socketServer_.epollManager_.RemoveListener(GetFd());
112 }
113 
OnEventPoll()114 void SocketServer::SocketServerListener::OnEventPoll()
115 {
116     struct sockaddr_un clientAddr;
117     socklen_t clientAddrSize = static_cast<socklen_t>(sizeof(clientAddr));
118     int connectionFd =
119         OHOS_TEMP_FAILURE_RETRY(accept(GetFd(), reinterpret_cast<struct sockaddr*>(&clientAddr), &clientAddrSize));
120     if (connectionFd < 0) {
121         DFXLOGW("%{public}s :: Failed to accept connection from %{public}s",
122             FAULTLOGGERD_SERVER_TAG, socketName_.c_str());
123         return;
124     }
125     std::unique_ptr<EpollListener> clientRequestListener(new (std::nothrow) ClientRequestListener(*this, connectionFd));
126     socketServer_.epollManager_.AddListener(std::move(clientRequestListener));
127 }
128 }
129 }