1 /* 2 * Copyright (c) 2021-2023 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 #ifndef DFX_FAULTLOGGERD_SOCKET_H 17 #define DFX_FAULTLOGGERD_SOCKET_H 18 19 #include <cstdint> 20 #include "smart_fd.h" 21 namespace OHOS { 22 namespace HiviewDFX { 23 const char* const FAULTLOGGERD_SOCK_BASE_PATH = "/dev/unix/socket/"; 24 25 #ifdef FAULTLOGGERD_TEST 26 const char* const SERVER_SOCKET_NAME = "test.faultloggerd.server"; 27 const char* const SERVER_CRASH_SOCKET_NAME = "test.faultloggerd.crash.server"; 28 const char* const SERVER_SDKDUMP_SOCKET_NAME = "test.faultloggerd.sdkdump.server"; 29 #else 30 const char* const SERVER_SOCKET_NAME = "faultloggerd.server"; 31 const char* const SERVER_CRASH_SOCKET_NAME = "faultloggerd.crash.server"; 32 const char* const SERVER_SDKDUMP_SOCKET_NAME = "faultloggerd.sdkdump.server"; 33 #endif 34 35 SmartFd StartListen(const char* name, uint32_t listenCnt); 36 bool SendFileDescriptorToSocket(int32_t sockFd, const int32_t* fds, uint32_t nFds); 37 bool SendMsgToSocket(int32_t sockFd, const void* data, uint32_t dataLength); 38 bool GetMsgFromSocket(int32_t sockFd, void* data, uint32_t dataLength); 39 40 typedef struct SocketRequestData { 41 /** request data **/ 42 const void* requestData; 43 /** request data size */ 44 uint32_t requestSize; 45 } __attribute__((packed)) SocketRequestData; 46 47 typedef struct SocketFdData { 48 /** socket fd data **/ 49 int* fds; 50 /** socket fd size */ 51 uint32_t nFds; 52 } __attribute__((packed)) SocketFdData; 53 54 class FaultLoggerdSocket { 55 public: signalSafely_(signalSafely)56 explicit FaultLoggerdSocket(bool signalSafely = false) : signalSafely_(signalSafely) {} socketFd_(fd)57 explicit FaultLoggerdSocket(int32_t fd, bool signalSafely = false) : socketFd_(fd), signalSafely_(signalSafely) {} 58 void CloseSocketFileDescriptor(); 59 bool CreateSocketFileDescriptor(uint32_t timeout); 60 bool StartConnect(const char* socketName); 61 bool SendFileDescriptorToSocket(const int32_t* fds, uint32_t nFds) const; 62 bool ReadFileDescriptorFromSocket(int32_t* fds, uint32_t nFds) const; 63 bool SendMsgToSocket(const void* data, uint32_t dataLength) const; 64 bool GetMsgFromSocket(void* data, uint32_t dataLength) const; 65 int32_t RequestServer(const SocketRequestData& socketRequestData) const; 66 int32_t RequestFdsFromServer(const SocketRequestData& socketRequestData, SocketFdData& socketFdData) const; 67 private: 68 bool ConcatenateSocketName(char* dst, uint32_t dstSize, const char* src, uint32_t srcSize) const; 69 bool SetSocketTimeOut(uint32_t timeout, int optName); 70 int32_t socketFd_ = -1; 71 bool signalSafely_; 72 }; 73 74 } 75 } 76 77 #endif