1 /*
2 * Copyright (c) 2021-2022 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 "client_socket.h"
17
18 #include <sys/socket.h>
19 #include <cerrno>
20
21 #include "appspawn_server.h"
22 #include "parameters.h"
23
24 namespace OHOS {
25 #ifdef APPSPAWN_ASAN
26 static constexpr int TIMEOUT_DEF = 5;
27 #else
28 static constexpr int TIMEOUT_DEF = 2;
29 #endif
30 namespace AppSpawn {
ClientSocket(const std::string & client)31 ClientSocket::ClientSocket(const std::string &client) : AppSpawnSocket(client)
32 {}
33
CreateClient()34 int ClientSocket::CreateClient()
35 {
36 if (socketFd_ < 0) {
37 socketFd_ = CreateSocket();
38 APPSPAWN_CHECK(socketFd_ >= 0, return socketFd_, "Client: Create socket failed");
39 }
40
41 int opt = 1;
42 int ret = setsockopt(socketFd_, SOL_SOCKET, SO_PASSCRED, &opt, sizeof(opt));
43 APPSPAWN_CHECK(ret == 0, CloseClient(); return ret,
44 "Client: set socket opt socket fd: %{public}d error: %{public}d", socketFd_, errno);
45 APPSPAWN_LOGV("Client: CreateClient socket fd %{public}d ret %{public}d", socketFd_, ret);
46 return ret;
47 }
48
CloseClient()49 void ClientSocket::CloseClient()
50 {
51 if (socketFd_ < 0) {
52 APPSPAWN_LOGE("Client: Invalid connectFd %{public}d", socketFd_);
53 return;
54 }
55
56 CloseSocket(socketFd_);
57 socketFd_ = -1;
58 }
59
ConnectSocket(int connectFd)60 int ClientSocket::ConnectSocket(int connectFd)
61 {
62 if (connectFd < 0) {
63 APPSPAWN_LOGE("Client: Invalid socket fd: %{public}d", connectFd);
64 return -1;
65 }
66
67 APPSPAWN_CHECK(PackSocketAddr() == 0, return -1, "pack socket failed");
68
69 struct timeval timeout = {TIMEOUT_DEF, 0};
70 int value = OHOS::system::GetIntParameter("persist.appspawn.client.timeout", TIMEOUT_DEF);
71 if (value != TIMEOUT_DEF && value != 0) {
72 timeout.tv_sec = value;
73 }
74 APPSPAWN_LOGI("Client: Connected on socket fd %{public}d value %{public}d", connectFd, value);
75 bool isRet = (setsockopt(connectFd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) != 0) ||
76 (setsockopt(connectFd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) != 0);
77 APPSPAWN_CHECK(!isRet, return (-1),
78 "Client: Failed to set opt of socket %{public}d, err %{public}d", connectFd, errno);
79
80 if (connect(connectFd, reinterpret_cast<struct sockaddr *>(&socketAddr_), socketAddrLen_) < 0) {
81 APPSPAWN_LOGW("Client: Connect on socket fd %{public}d, failed: %{public}d", connectFd, errno);
82 return -1;
83 }
84 return 0;
85 }
86
ConnectSocket()87 int ClientSocket::ConnectSocket()
88 {
89 int ret = ConnectSocket(socketFd_);
90 if (ret != 0) {
91 CloseClient();
92 }
93 return ret;
94 }
95
WriteSocketMessage(const void * buf,int len)96 int ClientSocket::WriteSocketMessage(const void *buf, int len)
97 {
98 return WriteSocketMessage(socketFd_, buf, len);
99 }
100
ReadSocketMessage(void * buf,int len)101 int ClientSocket::ReadSocketMessage(void *buf, int len)
102 {
103 return ReadSocketMessage(socketFd_, buf, len);
104 }
105 } // namespace AppSpawn
106 } // namespace OHOS
107