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