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
UnixSocketServer()29 UnixSocketServer::UnixSocketServer()
30 {
31 sAddrName_ = "";
32 socketHandle_ = -1;
33 serviceEntry_ = nullptr;
34 }
35
~UnixSocketServer()36 UnixSocketServer::~UnixSocketServer()
37 {
38 if (getuid() == 0) {
39 if (socketHandle_ != -1) {
40 PROFILER_LOG_DEBUG(LOG_CORE, "close UnixSocketServer");
41 close(socketHandle_);
42 unlink(sAddrName_.c_str());
43 }
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 while (socketHandle_ != -1) {
67 struct epoll_event events[10];
68 int nfds = epoll_wait(epfd, events, 10, 1000); // timeout value set 1000.
69 if (nfds == -1) {
70 PROFILER_LOG_ERROR(LOG_CORE, "UnixSocketServer epoll_wait failed, errno: %s", strerror(errno));
71 return;
72 }
73 for (int32_t i = 0; i < nfds; ++i) {
74 if (events[i].events & EPOLLIN) {
75 int clientSocket = accept(socketHandle_, nullptr, nullptr);
76 PROFILER_LOG_INFO(LOG_CORE, "Accept A Client %d", clientSocket);
77
78 struct epoll_event clientEvt;
79 clientEvt.data.fd = clientSocket;
80 clientEvt.events = EPOLLHUP;
81 epoll_ctl(epfd, EPOLL_CTL_ADD, clientSocket, &clientEvt);
82
83 if (socketClients_.find(clientSocket) == socketClients_.end()) {
84 PROFILER_LOG_DEBUG(LOG_CORE, "new socketClients_ socketClients_.size() = %zu",
85 socketClients_.size());
86 socketClients_[clientSocket] = std::make_shared<ClientConnection>(clientSocket, *serviceEntry_);
87 } else {
88 PROFILER_LOG_ERROR(LOG_CORE, "Client %d exist", clientSocket);
89 }
90 } else if (events[i].events & EPOLLHUP) {
91 struct epoll_event delEvt;
92 delEvt.data.fd = events[i].data.fd;
93 delEvt.events = EPOLLHUP;
94 epoll_ctl(epfd, EPOLL_CTL_DEL, events[i].data.fd, &delEvt);
95 if (socketClients_.find(events[i].data.fd) != socketClients_.end()) {
96 PROFILER_LOG_DEBUG(LOG_CORE, "socketClients disconnect socketClients_.size() = %zu",
97 socketClients_.size());
98 socketClients_.erase(events[i].data.fd);
99 } else {
100 PROFILER_LOG_ERROR(LOG_CORE, "Client %d not exist", events[i].data.fd);
101 }
102 }
103 }
104 }
105 close(epfd);
106 }
107
108 namespace {
109 const int UNIX_SOCKET_LISTEN_COUNT = 5;
110 }
111
StartServer(const std::string & addrname,ServiceEntry & p)112 bool UnixSocketServer::StartServer(const std::string& addrname, ServiceEntry& p)
113 {
114 CHECK_TRUE(socketHandle_ == -1, false, "StartServer FAIL socketHandle_ != -1");
115 int sock = -1;
116 if (getuid() == 0) {
117 struct sockaddr_un addr;
118 sock = socket(AF_UNIX, SOCK_STREAM, 0);
119 CHECK_TRUE(sock != -1, false, "StartServer FAIL create socket ERR : %d", errno);
120
121 if (memset_s(&addr, sizeof(struct sockaddr_un), 0, sizeof(struct sockaddr_un)) != EOK) {
122 PROFILER_LOG_ERROR(LOG_CORE, "memset_s error!");
123 }
124 addr.sun_family = AF_UNIX;
125 if (strncpy_s(addr.sun_path, sizeof(addr.sun_path), addrname.c_str(), sizeof(addr.sun_path) - 1) != EOK) {
126 PROFILER_LOG_ERROR(LOG_CORE, "strncpy_s error!");
127 }
128 unlink(addrname.c_str());
129 CHECK_TRUE(bind(sock, (struct sockaddr*)&addr, sizeof(struct sockaddr_un)) == 0, close(sock) != 0,
130 "StartServer FAIL bind ERR : %d", errno);
131
132 std::string chmodCmd = "chmod 666 " + addrname;
133 PROFILER_LOG_INFO(LOG_CORE, "chmod command : %s", chmodCmd.c_str());
134 std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(chmodCmd.c_str(), "r"), pclose);
135 } else {
136 sock = GetControlSocket(addrname.c_str());
137 CHECK_TRUE(sock != -1, false, "StartServer FAIL GetControlSocket return : %d", sock);
138 }
139
140 CHECK_TRUE(listen(sock, UNIX_SOCKET_LISTEN_COUNT) != -1, close(sock) != 0 && unlink(addrname.c_str()) == 0,
141 "StartServer FAIL listen ERR : %d", errno);
142
143 socketHandle_ = sock;
144 acceptThread_ = std::thread(&UnixSocketServer::UnixSocketAccept, this);
145 if (acceptThread_.get_id() == std::thread::id()) {
146 close(socketHandle_);
147 unlink(addrname.c_str());
148 const int bufSize = 256;
149 char buf[bufSize] = { 0 };
150 strerror_r(errno, buf, bufSize);
151 PROFILER_LOG_ERROR(LOG_CORE, "StartServer FAIL pthread_create ERR : %s", buf);
152 socketHandle_ = -1;
153 return false;
154 }
155
156 serviceEntry_ = &p;
157 sAddrName_ = addrname;
158 return true;
159 }
160