• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "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 "logging.h"
26 #include "securec.h"
27 
UnixSocketServer()28 UnixSocketServer::UnixSocketServer()
29 {
30     sAddrName_ = "";
31     socketHandle_ = -1;
32     serviceEntry_ = nullptr;
33 }
34 
~UnixSocketServer()35 UnixSocketServer::~UnixSocketServer()
36 {
37     if (socketHandle_ != 1) {
38         HILOG_DEBUG(LOG_CORE, "close UnixSocketServer");
39         close(socketHandle_);
40         socketHandle_ = -1;
41         unlink(sAddrName_.c_str());
42     }
43 
44     if (acceptThread_.joinable()) {
45         acceptThread_.join();
46     }
47     HILOG_DEBUG(LOG_CORE, "acceptThread finish");
48     if (socketClients_.size() > 0) {
49         HILOG_DEBUG(LOG_CORE, "socketClients_.size() = %zu delete map", socketClients_.size());
50         socketClients_.clear();
51     }
52 }
53 
UnixSocketAccept()54 void UnixSocketServer::UnixSocketAccept()
55 {
56     pthread_setname_np(pthread_self(), "UnixSocketAccept");
57     CHECK_TRUE(socketHandle_ != -1, NO_RETVAL, "Unix Socket Accept socketHandle_ == -1");
58     int epfd = epoll_create(1);
59     struct epoll_event evt;
60     evt.data.fd = socketHandle_;
61     evt.events = EPOLLIN | EPOLLET | EPOLLHUP;
62     CHECK_TRUE(epoll_ctl(epfd, EPOLL_CTL_ADD, socketHandle_, &evt) != -1, NO_RETVAL, "Unix Socket Server Exit");
63     while (socketHandle_ != -1) {
64         struct epoll_event events[10];
65         int nfds = epoll_wait(epfd, &events[0], 1, 1000);  // timeout value set 1000.
66         if (nfds > 0) {
67             if (events[0].events & EPOLLIN) {
68                 int clientSocket = accept(socketHandle_, nullptr, nullptr);
69                 HILOG_INFO(LOG_CORE, "Accept A Client %d", clientSocket);
70 
71                 struct epoll_event clientEvt;
72                 clientEvt.data.fd = clientSocket;
73                 clientEvt.events = EPOLLHUP;
74                 epoll_ctl(epfd, EPOLL_CTL_ADD, clientSocket, &clientEvt);
75 
76                 if (socketClients_.find(clientSocket) == socketClients_.end()) {
77                     HILOG_DEBUG(LOG_CORE, "new socketClients_ socketClients_.size() = %zu", socketClients_.size());
78                     socketClients_[clientSocket] = std::make_shared<ClientConnection>(clientSocket, *serviceEntry_);
79                 } else {
80                     HILOG_ERROR(LOG_CORE, "Client %d exist", clientSocket);
81                 }
82             } else if (events[0].events & EPOLLHUP) {
83                 struct epoll_event delEvt;
84                 delEvt.data.fd = events[0].data.fd;
85                 delEvt.events = EPOLLHUP;
86                 epoll_ctl(epfd, EPOLL_CTL_DEL, events[0].data.fd, &delEvt);
87 
88                 if (socketClients_.find(events[0].data.fd) != socketClients_.end()) {
89                     HILOG_DEBUG(LOG_CORE, "socketClients disconnect socketClients_.size() = %zu", socketClients_.size());
90                     socketClients_.erase(events[0].data.fd);
91                 } else {
92                     HILOG_ERROR(LOG_CORE, "Client %d not exist", events[0].data.fd);
93                 }
94             }
95         }
96     }
97     close(epfd);
98 }
99 
100 namespace {
101 const int UNIX_SOCKET_LISTEN_COUNT = 5;
102 }
StartServer(const std::string & addrname,ServiceEntry & p)103 bool UnixSocketServer::StartServer(const std::string& addrname, ServiceEntry& p)
104 {
105     CHECK_TRUE(socketHandle_ == -1, false, "StartServer FAIL socketHandle_ != -1");
106 
107     struct sockaddr_un addr;
108     int sock = socket(AF_UNIX, SOCK_STREAM, 0);
109     CHECK_TRUE(sock != -1, false, "StartServer FAIL create socket ERR : %d", errno);
110 
111     if (memset_s(&addr, sizeof(struct sockaddr_un), 0, sizeof(struct sockaddr_un)) != EOK) {
112         HILOG_ERROR(LOG_CORE, "memset_s error!");
113     }
114     addr.sun_family = AF_UNIX;
115     if (strncpy_s(addr.sun_path, sizeof(addr.sun_path), addrname.c_str(), sizeof(addr.sun_path) - 1) != EOK) {
116         HILOG_ERROR(LOG_CORE, "strncpy_s error!");
117     }
118     unlink(addrname.c_str());
119     CHECK_TRUE(bind(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) == 0, close(sock) != 0,
120                "StartServer FAIL bind ERR : %d", errno);
121 
122     CHECK_TRUE(listen(sock, UNIX_SOCKET_LISTEN_COUNT) != -1, close(sock) != 0 && unlink(addrname.c_str()) == 0,
123                "StartServer FAIL listen ERR : %d", errno);
124 
125     socketHandle_ = sock;
126     acceptThread_ = std::thread(&UnixSocketServer::UnixSocketAccept, this);
127     if (acceptThread_.get_id() == std::thread::id()) {
128         close(socketHandle_);
129         unlink(addrname.c_str());
130         const int bufSize = 1024;
131         char buf[bufSize] = { 0 };
132         strerror_r(errno, buf, bufSize);
133         HILOG_ERROR(LOG_CORE, "StartServer FAIL pthread_create ERR : %s", buf);
134         socketHandle_ = -1;
135         return false;
136     }
137 
138     serviceEntry_ = &p;
139     sAddrName_ = addrname;
140     return true;
141 }
142