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