• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (socketClients_.size() > 0) {
52         PROFILER_LOG_DEBUG(LOG_CORE, "socketClients_.size() = %zu delete map", socketClients_.size());
53         socketClients_.clear();
54     }
55 }
56 
UnixSocketAccept()57 void UnixSocketServer::UnixSocketAccept()
58 {
59     pthread_setname_np(pthread_self(), "UnixSocketAccept");
60     CHECK_TRUE(socketHandle_ != -1, NO_RETVAL, "Unix Socket Accept socketHandle_ == -1");
61     int epfd = epoll_create(1);
62     struct epoll_event evt;
63     evt.data.fd = socketHandle_;
64     evt.events = EPOLLIN | EPOLLHUP;
65     CHECK_TRUE(epoll_ctl(epfd, EPOLL_CTL_ADD, socketHandle_, &evt) != -1, NO_RETVAL, "Unix Socket Server Exit");
66     struct epoll_event events[EPOLL_MAX_TASK_COUNT];
67     int retryCount = 0;
68     while (socketHandle_ != -1) {
69         int nfds = epoll_wait(epfd, events, EPOLL_MAX_TASK_COUNT, EPOLL_WAIT_TIMEOUT);  // timeout value set 1000.
70         if (nfds == -1) {
71             if (errno == EINTR && retryCount < RETRY_MAX_COUNT) {
72                 ++retryCount;
73                 continue;
74             } else {
75                 PROFILER_LOG_ERROR(LOG_CORE, "UnixSocketServer epoll_wait failed, errno: %s", strerror(errno));
76                 return;
77             }
78         }
79         for (int32_t i = 0; i < nfds; ++i) {
80             if (events[i].events & EPOLLIN) {
81                 int clientSocket = accept(socketHandle_, nullptr, nullptr);
82                 CHECK_TRUE(clientSocket != -1, NO_RETVAL, "Accept Failed");
83                 PROFILER_LOG_INFO(LOG_CORE, "Accept A Client %d", clientSocket);
84 
85                 struct epoll_event clientEvt;
86                 clientEvt.data.fd = clientSocket;
87                 clientEvt.events = EPOLLHUP;
88                 CHECK_TRUE(epoll_ctl(epfd, EPOLL_CTL_ADD, clientSocket, &clientEvt) != -1,
89                     NO_RETVAL, "Unix Socket Server Exit");
90                 if (socketClients_.find(clientSocket) == socketClients_.end()) {
91                     PROFILER_LOG_DEBUG(LOG_CORE, "new socketClients_ socketClients_.size() = %zu",
92                                        socketClients_.size());
93                     socketClients_[clientSocket] = std::make_shared<ClientConnection>(clientSocket, *serviceEntry_);
94                 } else {
95                     PROFILER_LOG_ERROR(LOG_CORE, "Client %d exist", clientSocket);
96                 }
97             } else if (events[i].events & EPOLLHUP) {
98                 struct epoll_event delEvt;
99                 delEvt.data.fd = events[i].data.fd;
100                 delEvt.events = EPOLLHUP;
101                 epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, &delEvt);
102                 if (socketClients_.find(events[i].data.fd) != socketClients_.end()) {
103                     PROFILER_LOG_DEBUG(LOG_CORE, "socketClients disconnect socketClients_.size() = %zu",
104                                        socketClients_.size());
105                     socketClients_.erase(events[i].data.fd);
106                 } else {
107                     PROFILER_LOG_ERROR(LOG_CORE, "Client %d not exist", events[i].data.fd);
108                 }
109             }
110         }
111     }
112     close(epfd);
113 }
114 
StartServer(const std::string & addrname,ServiceEntry & p)115 bool UnixSocketServer::StartServer(const std::string& addrname, ServiceEntry& p)
116 {
117     CHECK_TRUE(socketHandle_ == -1, false, "StartServer FAIL socketHandle_ != -1");
118     int sock = GetControlSocket(addrname.c_str());
119     CHECK_TRUE(sock != -1, false, "StartServer FAIL GetControlSocket return : %d", sock);
120 
121     CHECK_TRUE(listen(sock, UNIX_SOCKET_LISTEN_COUNT) != -1, close(sock) != 0 && unlink(addrname.c_str()) == 0,
122                "StartServer FAIL listen ERR : %d", errno);
123 
124     socketHandle_ = sock;
125     acceptThread_ = std::thread([this] { this->UnixSocketAccept(); });
126     if (acceptThread_.get_id() == std::thread::id()) {
127         close(socketHandle_);
128         unlink(addrname.c_str());
129         const int bufSize = 256;
130         char buf[bufSize] = { 0 };
131         strerror_r(errno, buf, bufSize);
132         PROFILER_LOG_ERROR(LOG_CORE, "StartServer FAIL pthread_create ERR : %s", buf);
133         socketHandle_ = -1;
134         return false;
135     }
136     serviceEntry_ = &p;
137     sAddrName_ = addrname;
138     return true;
139 }
140