1 /*
2 * Copyright (c) 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 "wrapper_listener.h"
17
18 #include <cerrno>
19 #include <cstdlib>
20 #include <fcntl.h>
21 #include <memory>
22 #include <poll.h>
23 #include <sys/socket.h>
24 #include <unistd.h>
25 #include <vector>
26
27 #include "netlink_define.h"
28 #include "netnative_log_wrapper.h"
29
30 namespace OHOS::nmd {
31 namespace {
32 constexpr int32_t PRET_SIZE = 2;
33 } // namespace
34
WrapperListener(int32_t socket,std::function<void (int32_t)> recvFunc)35 WrapperListener::WrapperListener(int32_t socket, std::function<void(int32_t)> recvFunc)
36 {
37 socket_ = socket;
38 startReceiveFunc_ = recvFunc;
39 }
40
~WrapperListener()41 WrapperListener::~WrapperListener()
42 {
43 Stop();
44 }
45
Start()46 int32_t WrapperListener::Start()
47 {
48 if (socket_ < 0) {
49 NETNATIVE_LOGE("listener socket_ < 0 %{public}d", socket_);
50 return NetlinkResult::ERROR;
51 }
52
53 int pipeRet = pipe2(pipe_, O_CLOEXEC);
54 if (pipeRet != 0) {
55 NETNATIVE_LOGE("pipeRes = %{public}d, pipe create failed errno = %{public}d, %{public}s", pipeRet, errno,
56 strerror(errno));
57 return NetlinkResult::ERROR;
58 }
59 thread_ = std::thread(WrapperListener::ListenThread, this);
60 return NetlinkResult::OK;
61 }
62
Stop()63 int32_t WrapperListener::Stop()
64 {
65 NETNATIVE_LOGI("WrapperListener: Stop");
66 char pipe = PIPE_SHUTDOWN;
67 if (TEMP_FAILURE_RETRY(write(pipe_[1], &pipe, sizeof(pipe))) != 1) {
68 NETNATIVE_LOGE("write pipe failed errno = %{public}d, %{public}s", errno, strerror(errno));
69 return NetlinkResult::ERROR;
70 }
71
72 if (thread_.joinable()) {
73 thread_.join();
74 }
75 for (auto &pi : pipe_) {
76 if (pi > 0) {
77 close(pi);
78 }
79 }
80
81 if (socket_ > -1) {
82 close(socket_);
83 }
84
85 return NetlinkResult::OK;
86 }
87
ListenThread(WrapperListener * listener)88 void WrapperListener::ListenThread(WrapperListener *listener)
89 {
90 listener->Listen();
91 }
92
Listen()93 void WrapperListener::Listen()
94 {
95 if (startReceiveFunc_ == nullptr) {
96 NETNATIVE_LOGE("startReceiveFunc_ is nullptr start listen failed");
97 return;
98 }
99 while (true) {
100 std::vector<pollfd> pollFds;
101 std::unique_lock<std::mutex> lock(clientsLock_);
102 pollFds.reserve(PRET_SIZE + 1);
103 pollfd polfd;
104 polfd.fd = pipe_[0];
105 polfd.events = POLLIN;
106 pollFds.emplace_back(polfd);
107 polfd.fd = socket_;
108 polfd.events = POLLIN;
109 pollFds.emplace_back(polfd);
110 int32_t ret = TEMP_FAILURE_RETRY(poll(pollFds.data(), pollFds.size(), -1));
111 if (ret < 0) {
112 std::this_thread::sleep_for(std::chrono::seconds(1));
113 }
114
115 if (pollFds[0].revents & (POLLIN | POLLERR)) {
116 char ctlp = PIPE_SHUTDOWN;
117 TEMP_FAILURE_RETRY(read(pipe_[0], &ctlp, sizeof(ctlp)));
118 if (ctlp == PIPE_SHUTDOWN) {
119 break;
120 }
121 continue;
122 }
123 startReceiveFunc_(socket_);
124 }
125 }
126 } // namespace OHOS::nmd
127