1 /*
2 * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 "unix_socket_server.h"
17
18 #include <cstdio>
19 #include <pthread.h>
20 #include <sys/epoll.h>
21 #include <sys/socket.h>
22 #include <unistd.h>
23 #include <linux/un.h>
24
25 #include "init_socket.h"
26 #include "logging.h"
27 #include "securec.h"
28
29 namespace {
30 constexpr int UNIX_SOCKET_LISTEN_COUNT = 5;
31 constexpr int EPOLL_MAX_TASK_COUNT = 10;
32 constexpr int EPOLL_WAIT_TIMEOUT = 1000;
33 constexpr int RETRY_MAX_COUNT = 5;
34 }
35
UnixSocketServer()36 UnixSocketServer::UnixSocketServer()
37 {
38 sAddrName_ = "";
39 socketHandle_ = -1;
40 serviceEntry_ = nullptr;
41 }
42
~UnixSocketServer()43 UnixSocketServer::~UnixSocketServer()
44 {
45 socketHandle_ = -1;
46
47 if (acceptThread_.joinable()) {
48 acceptThread_.join();
49 }
50 PROFILER_LOG_DEBUG(LOG_CORE, "acceptThread finish");
51 if (epfd_ != -1) {
52 close(epfd_);
53 epfd_ = -1;
54 }
55 std::unique_lock<std::mutex> lock(mtx_);
56 if (socketClients_.size() > 0) {
57 PROFILER_LOG_DEBUG(LOG_CORE, "socketClients_.size() = %zu delete map", socketClients_.size());
58 socketClients_.clear();
59 }
60 }
61
RemoveContext(int fd)62 void UnixSocketServer::RemoveContext(int fd)
63 {
64 std::unique_lock<std::mutex> lock(mtx_);
65 if (socketClients_.find(fd) != socketClients_.end()) {
66 PROFILER_LOG_DEBUG(LOG_CORE, "RemoveContext socketClients disconnect socketClients_.size() = %zu",
67 socketClients_.size());
68 if (epfd_ != -1) {
69 struct epoll_event delEvt;
70 delEvt.data.fd = fd;
71 delEvt.events = EPOLLHUP;
72 if (epoll_ctl(epfd_, EPOLL_CTL_DEL, fd, &delEvt) == -1) {
73 PROFILER_LOG_ERROR(LOG_CORE, "RemoveContext epoll_ctl failed, errno: %s", strerror(errno));
74 }
75 }
76 socketClients_.erase(fd);
77 } else {
78 PROFILER_LOG_ERROR(LOG_CORE, "RemoveContext Client %d not exist", fd);
79 }
80 }
81
UnixSocketAccept(void (* callback)(int))82 void UnixSocketServer::UnixSocketAccept(void (*callback)(int))
83 {
84 pthread_setname_np(pthread_self(), "UnixSocketAccept");
85 CHECK_TRUE(socketHandle_ != -1, NO_RETVAL, "Unix Socket Accept socketHandle_ == -1");
86 epfd_ = epoll_create(1);
87 struct epoll_event evt;
88 evt.data.fd = socketHandle_;
89 evt.events = EPOLLIN | EPOLLHUP;
90 CHECK_TRUE(epoll_ctl(epfd_, EPOLL_CTL_ADD, socketHandle_, &evt) != -1, NO_RETVAL, "Unix Socket Server Exit");
91 struct epoll_event events[EPOLL_MAX_TASK_COUNT];
92 int retryCount = 0;
93 while (socketHandle_ != -1) {
94 int nfds = epoll_wait(epfd_, events, EPOLL_MAX_TASK_COUNT, EPOLL_WAIT_TIMEOUT); // timeout value set 1000.
95 if (nfds == -1) {
96 if (errno == EINTR && retryCount < RETRY_MAX_COUNT) {
97 ++retryCount;
98 continue;
99 } else {
100 PROFILER_LOG_ERROR(LOG_CORE, "UnixSocketServer epoll_wait failed, errno: %s", strerror(errno));
101 return;
102 }
103 }
104 for (int32_t i = 0; i < nfds; ++i) {
105 if (events[i].events & EPOLLIN) {
106 int clientSocket = accept(socketHandle_, nullptr, nullptr);
107 CHECK_TRUE(clientSocket != -1, NO_RETVAL, "Accept Failed");
108 PROFILER_LOG_INFO(LOG_CORE, "Accept A Client %d", clientSocket);
109
110 struct epoll_event clientEvt;
111 clientEvt.data.fd = clientSocket;
112 clientEvt.events = EPOLLHUP;
113 CHECK_TRUE(epoll_ctl(epfd_, EPOLL_CTL_ADD, clientSocket, &clientEvt) != -1,
114 NO_RETVAL, "Unix Socket Server Exit");
115 std::unique_lock<std::mutex> lock(mtx_);
116 if (socketClients_.find(clientSocket) == socketClients_.end()) {
117 PROFILER_LOG_DEBUG(LOG_CORE, "new socketClients_ socketClients_.size() = %zu",
118 socketClients_.size());
119 socketClients_[clientSocket] = std::make_shared<ClientConnection>(clientSocket, *serviceEntry_);
120 } else {
121 PROFILER_LOG_ERROR(LOG_CORE, "Client %d exist", clientSocket);
122 }
123 } else if (events[i].events & EPOLLHUP) {
124 std::unique_lock<std::mutex> lock(mtx_);
125 if (socketClients_.find(events[i].data.fd) != socketClients_.end()) {
126 struct epoll_event delEvt;
127 delEvt.data.fd = events[i].data.fd;
128 delEvt.events = EPOLLHUP;
129 if (epoll_ctl(epfd_, EPOLL_CTL_DEL, events[i].data.fd, &delEvt) == -1) {
130 PROFILER_LOG_ERROR(LOG_CORE, "UnixSocketServer epoll_ctl failed, errno: %s", strerror(errno));
131 }
132 lock.unlock();
133 if (callback != nullptr) {
134 callback(events[i].data.fd);
135 }
136 std::unique_lock<std::mutex> socketMapLock(mtx_);
137 PROFILER_LOG_DEBUG(LOG_CORE, "socketClients disconnect socketClients_.size() = %zu",
138 socketClients_.size());
139 socketClients_.erase(events[i].data.fd);
140 } else {
141 PROFILER_LOG_ERROR(LOG_CORE, "Client %d not exist", events[i].data.fd);
142 }
143 }
144 }
145 }
146 }
147
StartServer(const std::string & addrname,ServiceEntry & p,void (* callback)(int))148 bool UnixSocketServer::StartServer(const std::string& addrname, ServiceEntry& p, void (*callback)(int))
149 {
150 CHECK_TRUE(socketHandle_ == -1, false, "StartServer FAIL socketHandle_ != -1");
151 int sock = GetControlSocket(addrname.c_str());
152 CHECK_TRUE(sock != -1, false, "StartServer FAIL GetControlSocket return : %d", sock);
153
154 CHECK_TRUE(listen(sock, UNIX_SOCKET_LISTEN_COUNT) != -1, close(sock) != 0 && unlink(addrname.c_str()) == 0,
155 "StartServer FAIL listen ERR : %d", errno);
156
157 socketHandle_ = sock;
158 acceptThread_ = std::thread([this, callback] { this->UnixSocketAccept(callback); });
159 if (acceptThread_.get_id() == std::thread::id()) {
160 close(socketHandle_);
161 unlink(addrname.c_str());
162 const int bufSize = 256;
163 char buf[bufSize] = { 0 };
164 strerror_r(errno, buf, bufSize);
165 PROFILER_LOG_ERROR(LOG_CORE, "StartServer FAIL pthread_create ERR : %s", buf);
166 socketHandle_ = -1;
167 return false;
168 }
169 serviceEntry_ = &p;
170 sAddrName_ = addrname;
171 return true;
172 }
173