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