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 "server_socket.h"
17
18 #include <cerrno>
19 #include <iostream>
20 #include <sys/socket.h>
21
22 #include "hilog/log.h"
23 #include "init_socket.h"
24 #include "securec.h"
25
26 namespace OHOS {
27 namespace AppSpawn {
28 using namespace OHOS::HiviewDFX;
29 static constexpr HiLogLabel LABEL = {LOG_CORE, 0, "ServerSocket"};
30
ServerSocket(const std::string & server)31 ServerSocket::ServerSocket(const std::string &server) : AppSpawnSocket(server)
32 {}
33
~ServerSocket()34 ServerSocket::~ServerSocket()
35 {
36 CloseServer();
37 }
38
VerifyConnection(int connectFd)39 int ServerSocket::VerifyConnection(int connectFd)
40 {
41 std::lock_guard<std::mutex> lock(mutexConnect_);
42
43 std::vector<int>::iterator it = find(connectFds_.begin(), connectFds_.end(), connectFd);
44 if (it == connectFds_.end()) {
45 return -1;
46 }
47
48 return 0;
49 }
50
CloseConnection(int connectFd)51 void ServerSocket::CloseConnection(int connectFd)
52 {
53 if (connectFd < 0) {
54 HiLog::Error(LABEL, "Server: Invalid connectFd %d", connectFd);
55 return;
56 }
57
58 std::lock_guard<std::mutex> lock(mutexConnect_);
59
60 std::vector<int>::iterator it = find(connectFds_.begin(), connectFds_.end(), connectFd);
61 if (it == connectFds_.end()) {
62 close(connectFd);
63 return;
64 }
65
66 close(connectFd);
67 connectFds_.erase(it);
68 HiLog::Debug(LABEL, "Server: Erase connect fd %d from list", connectFd);
69 }
70
SaveConnection(int connectFd)71 void ServerSocket::SaveConnection(int connectFd)
72 {
73 if (connectFd >= 0) {
74 std::lock_guard<std::mutex> lock(mutexConnect_);
75 connectFds_.push_back(connectFd);
76 }
77 }
78
CloseServer()79 void ServerSocket::CloseServer()
80 {
81 std::lock_guard<std::mutex> lock(mutexConnect_);
82
83 for (const int &fd : connectFds_) {
84 HiLog::Debug(LABEL, "Server: Closed connection fd %d", fd);
85 close(fd);
86 }
87
88 if ((unlink(socketAddr_.sun_path) != 0) && (errno != ENOENT)) {
89 HiLog::Error(LABEL, "Server: Failed to unlink, err %d", errno);
90 }
91
92 connectFds_.clear();
93 if (socketFd_ >= 0) {
94 CloseSocket(socketFd_);
95 socketFd_ = -1;
96 }
97 }
98
CloseServerMonitor()99 void ServerSocket::CloseServerMonitor()
100 {
101 if (socketFd_ >= 0) {
102 CloseSocket(socketFd_);
103 socketFd_ = -1;
104 }
105 }
106
BindSocket(int connectFd)107 int ServerSocket::BindSocket(int connectFd)
108 {
109 if (connectFd < 0) {
110 HiLog::Error(LABEL, "Server: Invalid socket fd: %d", connectFd);
111 return -EINVAL;
112 }
113
114 if (PackSocketAddr() != 0) {
115 return -1;
116 }
117
118 if ((unlink(socketAddr_.sun_path) != 0) && (errno != ENOENT)) {
119 HiLog::Error(LABEL, "Server: Failed to unlink, err %d", errno);
120 return (-errno);
121 }
122
123 int reuseAddr = 0;
124 if ((setsockopt(connectFd, SOL_SOCKET, SO_REUSEADDR, &reuseAddr, sizeof(reuseAddr)) != 0) ||
125 (setsockopt(connectFd, SOL_SOCKET, SO_RCVTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) != 0) ||
126 (setsockopt(connectFd, SOL_SOCKET, SO_SNDTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) != 0)) {
127 HiLog::Warn(LABEL, "Server: Failed to set opt of socket %d, err %d", connectFd, errno);
128 return (-errno);
129 }
130
131 if (bind(connectFd, reinterpret_cast<struct sockaddr *>(&socketAddr_), socketAddrLen_) < 0) {
132 HiLog::Error(LABEL, "Server: Bind socket fd %d, failed: %d", connectFd, errno);
133 return (-errno);
134 }
135
136 if (chown(socketAddr_.sun_path, APPSPAWN_ID_ROOT, APPSPAWN_GROUP_ID)) {
137 HiLog::Error(LABEL, "Server: failed to chown socket fd %d, failed: %d", connectFd, errno);
138 return (-errno);
139 }
140 if (chmod(socketAddr_.sun_path, SOCKET_PERM)) {
141 HiLog::Error(LABEL, "Server: failed to chmod socket fd %d, failed: %d", connectFd, errno);
142 return (-errno);
143 }
144
145 HiLog::Debug(LABEL, "Server: Bind socket fd %d success", connectFd);
146 return 0;
147 }
148
RegisterServerSocket(int & connectFd)149 int ServerSocket::RegisterServerSocket(int &connectFd)
150 {
151 if (socketName_.empty()) {
152 HiLog::Error(LABEL, "Server: Invalid socket name: empty");
153 return -EINVAL;
154 }
155
156 #ifdef NWEB_SPAWN
157 connectFd = GetControlSocket("NWebSpawn");
158 #else
159 connectFd = CreateSocket();
160 #endif
161 if (connectFd < 0) {
162 return connectFd;
163 }
164
165 #ifndef NWEB_SPAWN
166 if ((BindSocket(connectFd) != 0) || (listen(connectFd, listenBacklog_) < 0)) {
167 HiLog::Error(LABEL,
168 "Server: Register socket fd %d with backlog %d error: %d",
169 connectFd,
170 listenBacklog_,
171 errno);
172 if ((unlink(socketAddr_.sun_path) != 0) && (errno != ENOENT)) {
173 HiLog::Error(LABEL, "Server: Failed to unlink, err %d", errno);
174 }
175 close(connectFd);
176 connectFd = -1;
177 return (-errno);
178 }
179 #endif
180 HiLog::Debug(LABEL, "Server: Suc to register server socket fd %d", connectFd);
181 return 0;
182 }
183
RegisterServerSocket()184 int ServerSocket::RegisterServerSocket()
185 {
186 if (socketFd_ >= 0) {
187 HiLog::Info(LABEL, "Server: Already register server socket %d", socketFd_);
188 return 0;
189 }
190
191 return RegisterServerSocket(socketFd_);
192 }
193
WaitForConnection(int connectFd)194 int ServerSocket::WaitForConnection(int connectFd)
195 {
196 if (connectFd < 0) {
197 HiLog::Error(LABEL, "Server: Invalid args: connectFd %d", connectFd);
198 return -EINVAL;
199 }
200
201 struct sockaddr_un clientAddr;
202 socklen_t clientLen = sizeof(clientAddr);
203 if (memset_s(&clientAddr, clientLen, 0, clientLen) != EOK) {
204 HiLog::Warn(LABEL, "Server: Failed to memset client addr");
205 return -EINVAL;
206 }
207
208 int connFd = accept(connectFd, reinterpret_cast<struct sockaddr *>(&clientAddr), &clientLen);
209 if (connFd < 0) {
210 return (-errno);
211 }
212
213 if ((setsockopt(connFd, SOL_SOCKET, SO_RCVTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) < 0) ||
214 (setsockopt(connFd, SOL_SOCKET, SO_SNDTIMEO, &SOCKET_TIMEOUT, sizeof(SOCKET_TIMEOUT)) < 0)) {
215 HiLog::Warn(LABEL, "Server: Failed to set opt of Connection %d, err %d", connFd, errno);
216 close(connFd);
217 return (-errno);
218 }
219
220 HiLog::Debug(LABEL, "Server: Connection accepted, connect fd %d", connFd);
221 return connFd;
222 }
223
WaitForConnection()224 int ServerSocket::WaitForConnection()
225 {
226 int connectFd = WaitForConnection(socketFd_);
227 SaveConnection(connectFd);
228
229 return connectFd;
230 }
231 } // namespace AppSpawn
232 } // namespace OHOS
233