1 /*
2 * Copyright (c) 2021 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 "socket_server.h"
17
18 #include <cstdint>
19 #include <chrono>
20 #include <iosfwd>
21 #include <poll.h>
22 #include <securec.h>
23 #include <string>
24 #include <sys/socket.h>
25 #include <sys/un.h>
26 #include <unistd.h>
27
28 #include "hilog_common.h"
29
30 extern "C" {
31 #include "init_socket.h"
32 }
33
34 namespace OHOS {
35 namespace HiviewDFX {
SocketServer(const std::string & _socketName,uint32_t socketType)36 SocketServer::SocketServer(const std::string& _socketName, uint32_t socketType)
37 : socketHandler(0), socketType(socketType), socketName(_socketName)
38 {
39 serverAddr.sun_family = AF_UNIX;
40
41 std::string sockPath(SOCKET_FILE_DIR);
42 sockPath += socketName;
43 if (strcpy_s(serverAddr.sun_path, sizeof(serverAddr.sun_path), sockPath.c_str()) != EOK) {
44 return;
45 }
46 }
47
Init()48 int SocketServer::Init()
49 {
50 if (socketName.length()) {
51 socketHandler = GetControlSocket(socketName.c_str());
52 if (socketHandler >= 0) {
53 return socketHandler;
54 }
55 }
56
57 socketHandler = TEMP_FAILURE_RETRY(socket(AF_UNIX, socketType, 0));
58 if (socketHandler < 0) {
59 return socketHandler;
60 }
61
62 (void)unlink(serverAddr.sun_path);
63
64 int optval = 1;
65 int ret = setsockopt(socketHandler, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval));
66 if (ret < 0) {
67 return ret;
68 }
69
70 return TEMP_FAILURE_RETRY(bind(socketHandler, (struct sockaddr *)&serverAddr, sizeof(sockaddr_un)));
71 }
72
Recv(void * buffer,unsigned int bufferLen,int flags)73 int SocketServer::Recv(void *buffer, unsigned int bufferLen, int flags)
74 {
75 return TEMP_FAILURE_RETRY(recv(socketHandler, buffer, bufferLen, flags));
76 }
77
RecvMsg(struct msghdr * hdr,int flags)78 int SocketServer::RecvMsg(struct msghdr *hdr, int flags)
79 {
80 return TEMP_FAILURE_RETRY(recvmsg(socketHandler, hdr, flags));
81 }
82
Listen(unsigned int backlog)83 int SocketServer::Listen(unsigned int backlog)
84 {
85 return TEMP_FAILURE_RETRY(listen(socketHandler, backlog));
86 }
87
Poll(short inEvent,short & outEvent,const std::chrono::milliseconds & timeout)88 int SocketServer::Poll(short inEvent, short& outEvent, const std::chrono::milliseconds& timeout)
89 {
90 pollfd info {socketHandler, inEvent, outEvent};
91 int result = TEMP_FAILURE_RETRY(poll(&info, 1, timeout.count()));
92 outEvent = info.revents;
93 return result;
94 }
95
Accept()96 int SocketServer::Accept()
97 {
98 socklen_t addressSize = static_cast<socklen_t>(sizeof(serverAddr));
99 return TEMP_FAILURE_RETRY(accept(socketHandler, (struct sockaddr*)&serverAddr, &addressSize));
100 }
101
~SocketServer()102 SocketServer::~SocketServer()
103 {
104 close(socketHandler);
105 socketHandler = -1;
106 }
107 } // namespace HiviewDFX
108 } // namespace OHOS
109