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