Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 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 | |
Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 19 | #include <sys/poll.h> |
Yifan Hong | b1db390 | 2018-09-26 15:49:28 -0700 | [diff] [blame] | 20 | #include <sys/types.h> |
| 21 | #include <unistd.h> |
| 22 | |
Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 23 | #include <chrono> |
| 24 | #include <optional> |
Yifan Hong | b1db390 | 2018-09-26 15:49:28 -0700 | [diff] [blame] | 25 | |
Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 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; |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 32 | |
| 33 | namespace android { |
| 34 | namespace lshal { |
Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 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; |
Yifan Hong | 8a66247 | 2020-09-24 11:50:50 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 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}; |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 61 | |
Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 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; |
Yifan Hong | b1db390 | 2018-09-26 15:49:28 -0700 | [diff] [blame] | 68 | } |
Yifan Hong | b1db390 | 2018-09-26 15:49:28 -0700 | [diff] [blame] | 69 | |
Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 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); |
Yifan Hong | b0ae543 | 2021-09-21 13:38:11 -0700 | [diff] [blame] | 85 | continue; |
Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 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 | } |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | PipeRelay::~PipeRelay() { |
Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 101 | mWrite.reset(); |
| 102 | mWriteTrigger.reset(); |
| 103 | if (mThread != nullptr && mThread->joinable()) { |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 104 | mThread->join(); |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 105 | } |
Andreas Huber | 28d3591 | 2017-03-24 13:14:11 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Yifan Hong | 891989f | 2021-06-17 18:31:42 -0700 | [diff] [blame] | 108 | } // namespace lshal |
| 109 | } // namespace android |