1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "PipeRelay.h"
18 
19 #include <sys/poll.h>
20 #include <sys/types.h>
21 #include <unistd.h>
22 
23 #include <chrono>
24 #include <optional>
25 
26 #include <android-base/unique_fd.h>
27 
28 using android::base::borrowed_fd;
29 using android::base::Result;
30 using android::base::unique_fd;
31 using std::chrono_literals::operator""ms;
32 
33 namespace android {
34 namespace lshal {
create(std::ostream & os,const NullableOStream<std::ostream> & err,const std::string & fqName)35 Result<std::unique_ptr<PipeRelay>> PipeRelay::create(std::ostream& os,
36                                                      const NullableOStream<std::ostream>& err,
37                                                      const std::string& fqName) {
38     auto pipeRelay = std::unique_ptr<PipeRelay>(new PipeRelay());
39     unique_fd rfd;
40     if (!android::base::Pipe(&rfd, &pipeRelay->mWrite)) {
41         return android::base::ErrnoError() << "pipe()";
42     }
43     // Workaround for b/111997867: need a separate FD trigger because rfd can't receive POLLHUP
44     // when the write end is closed after the write end was sent through hwbinder.
45     unique_fd rfdTrigger;
46     if (!android::base::Pipe(&rfdTrigger, &pipeRelay->mWriteTrigger)) {
47         return android::base::ErrnoError() << "pipe() for trigger";
48     }
49     pipeRelay->mThread =
50             std::make_unique<std::thread>(&PipeRelay::thread, std::move(rfd), std::move(rfdTrigger),
51                                           &os, &err, fqName);
52     return pipeRelay;
53 }
54 
thread(unique_fd rfd,unique_fd rfdTrigger,std::ostream * out,const NullableOStream<std::ostream> * err,std::string fqName)55 void PipeRelay::thread(unique_fd rfd, unique_fd rfdTrigger, std::ostream* out,
56                        const NullableOStream<std::ostream>* err, std::string fqName) {
57     while (true) {
58         pollfd pfd[2];
59         pfd[0] = {.fd = rfd.get(), .events = POLLIN};
60         pfd[1] = {.fd = rfdTrigger.get(), .events = 0};
61 
62         int pollRes = poll(pfd, arraysize(pfd), -1 /* infinite timeout */);
63         if (pollRes < 0) {
64             int savedErrno = errno;
65             (*err) << "debug " << fqName << ": poll() failed: " << strerror(savedErrno)
66                    << std::endl;
67             break;
68         }
69 
70         if (pfd[0].revents & POLLIN) {
71             char buffer[1024];
72             ssize_t n = TEMP_FAILURE_RETRY(read(rfd.get(), buffer, sizeof(buffer)));
73             if (n < 0) {
74                 int savedErrno = errno;
75                 (*err) << "debug " << fqName << ": read() failed: " << strerror(savedErrno)
76                        << std::endl;
77                 break;
78             }
79             if (n == 0) {
80                 (*err) << "Warning: debug " << fqName << ": poll() indicates POLLIN but no data"
81                        << std::endl;
82                 continue;
83             }
84             out->write(buffer, n);
85             continue;
86         }
87         if (pfd[0].revents & POLLHUP) {
88             break;
89         }
90         if (pfd[1].revents & POLLHUP) {
91             // ~PipeRelay is called on the main thread. |mWrite| has been flushed and closed.
92             // Ensure that our read end of the pipe doesn't have pending data, then exit.
93             if ((pfd[0].revents & POLLIN) == 0) {
94                 break;
95             }
96         }
97     }
98 }
99 
~PipeRelay()100 PipeRelay::~PipeRelay() {
101     mWrite.reset();
102     mWriteTrigger.reset();
103     if (mThread != nullptr && mThread->joinable()) {
104         mThread->join();
105     }
106 }
107 
108 } // namespace lshal
109 } // namespace android
110