Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 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 | #define LOG_TAG "FdTrigger" |
| 18 | #include <log/log.h> |
| 19 | |
Yifan Hong | e0b37bd | 2021-09-23 17:23:02 -0700 | [diff] [blame] | 20 | #include "FdTrigger.h" |
| 21 | |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 22 | #include <poll.h> |
| 23 | |
Tomasz Wasilczyk | 1de48a2 | 2023-10-30 14:19:19 +0000 | [diff] [blame] | 24 | #include <binder/Functional.h> |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 25 | |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 26 | #include "FdUtils.h" |
Yifan Hong | e0b37bd | 2021-09-23 17:23:02 -0700 | [diff] [blame] | 27 | #include "RpcState.h" |
Tomasz Wasilczyk | df07f94 | 2023-11-02 15:07:45 -0700 | [diff] [blame] | 28 | #include "Utils.h" |
| 29 | |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 30 | namespace android { |
| 31 | |
Tomasz Wasilczyk | 1de48a2 | 2023-10-30 14:19:19 +0000 | [diff] [blame] | 32 | using namespace android::binder::impl; |
| 33 | |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 34 | std::unique_ptr<FdTrigger> FdTrigger::make() { |
| 35 | auto ret = std::make_unique<FdTrigger>(); |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 36 | #ifndef BINDER_RPC_SINGLE_THREADED |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame] | 37 | if (!binder::Pipe(&ret->mRead, &ret->mWrite)) { |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 38 | ALOGE("Could not create pipe %s", strerror(errno)); |
| 39 | return nullptr; |
| 40 | } |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 41 | #endif |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 42 | return ret; |
| 43 | } |
| 44 | |
| 45 | void FdTrigger::trigger() { |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 46 | #ifdef BINDER_RPC_SINGLE_THREADED |
| 47 | mTriggered = true; |
| 48 | #else |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 49 | mWrite.reset(); |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 50 | #endif |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | bool FdTrigger::isTriggered() { |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 54 | #ifdef BINDER_RPC_SINGLE_THREADED |
| 55 | return mTriggered; |
| 56 | #else |
Tomasz Wasilczyk | bfb13a8 | 2023-11-14 11:33:10 -0800 | [diff] [blame] | 57 | return !mWrite.ok(); |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 58 | #endif |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 59 | } |
| 60 | |
Pawan | 3e0061c | 2022-08-26 21:08:34 +0000 | [diff] [blame] | 61 | status_t FdTrigger::triggerablePoll(const android::RpcTransportFd& transportFd, int16_t event) { |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 62 | #ifdef BINDER_RPC_SINGLE_THREADED |
| 63 | if (mTriggered) { |
| 64 | return DEAD_OBJECT; |
| 65 | } |
| 66 | #endif |
| 67 | |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 68 | LOG_ALWAYS_FATAL_IF(event == 0, "triggerablePoll %d with event 0 is not allowed", |
| 69 | transportFd.fd.get()); |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 70 | pollfd pfd[]{ |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 71 | {.fd = transportFd.fd.get(), .events = static_cast<int16_t>(event), .revents = 0}, |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 72 | #ifndef BINDER_RPC_SINGLE_THREADED |
| 73 | {.fd = mRead.get(), .events = 0, .revents = 0}, |
| 74 | #endif |
| 75 | }; |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 76 | |
| 77 | LOG_ALWAYS_FATAL_IF(transportFd.isInPollingState() == true, |
| 78 | "Only one thread should be polling on Fd!"); |
| 79 | |
| 80 | transportFd.setPollingState(true); |
Tomasz Wasilczyk | 1de48a2 | 2023-10-30 14:19:19 +0000 | [diff] [blame] | 81 | auto pollingStateGuard = make_scope_guard([&]() { transportFd.setPollingState(false); }); |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 82 | |
Tomasz Wasilczyk | df07f94 | 2023-11-02 15:07:45 -0700 | [diff] [blame] | 83 | int ret = TEMP_FAILURE_RETRY(poll(pfd, countof(pfd), -1)); |
Yifan Hong | e0b37bd | 2021-09-23 17:23:02 -0700 | [diff] [blame] | 84 | if (ret < 0) { |
Devin Moore | d08d9de | 2024-09-09 20:09:21 +0000 | [diff] [blame] | 85 | int saved_errno = errno; |
| 86 | ALOGE("FdTrigger poll returned error: %d, with error: %s", ret, strerror(saved_errno)); |
| 87 | return -saved_errno; |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 88 | } |
Pawan | 49d74cb | 2022-08-03 21:19:11 +0000 | [diff] [blame] | 89 | LOG_ALWAYS_FATAL_IF(ret == 0, "poll(%d) returns 0 with infinite timeout", transportFd.fd.get()); |
Yifan Hong | e0b37bd | 2021-09-23 17:23:02 -0700 | [diff] [blame] | 90 | |
| 91 | // At least one FD has events. Check them. |
| 92 | |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 93 | #ifndef BINDER_RPC_SINGLE_THREADED |
Yifan Hong | e0b37bd | 2021-09-23 17:23:02 -0700 | [diff] [blame] | 94 | // Detect explicit trigger(): DEAD_OBJECT |
| 95 | if (pfd[1].revents & POLLHUP) { |
| 96 | return DEAD_OBJECT; |
| 97 | } |
| 98 | // See unknown flags in trigger FD's revents (POLLERR / POLLNVAL). |
| 99 | // Treat this error condition as UNKNOWN_ERROR. |
| 100 | if (pfd[1].revents != 0) { |
| 101 | ALOGE("Unknown revents on trigger FD %d: revents = %d", pfd[1].fd, pfd[1].revents); |
| 102 | return UNKNOWN_ERROR; |
| 103 | } |
| 104 | |
| 105 | // pfd[1].revents is 0, hence pfd[0].revents must be set, and only possible values are |
| 106 | // a subset of event | POLLHUP | POLLERR | POLLNVAL. |
Andrei Homescu | ffa3aaa | 2022-04-07 05:06:33 +0000 | [diff] [blame] | 107 | #endif |
Yifan Hong | e0b37bd | 2021-09-23 17:23:02 -0700 | [diff] [blame] | 108 | |
| 109 | // POLLNVAL: invalid FD number, e.g. not opened. |
| 110 | if (pfd[0].revents & POLLNVAL) { |
Devin Moore | ddaccb7 | 2024-09-09 20:50:07 +0000 | [diff] [blame] | 111 | LOG_ALWAYS_FATAL("Invalid FD number (%d) in FdTrigger (POLLNVAL)", pfd[0].fd); |
Yifan Hong | e0b37bd | 2021-09-23 17:23:02 -0700 | [diff] [blame] | 112 | return BAD_VALUE; |
| 113 | } |
| 114 | |
| 115 | // Error condition. It wouldn't be possible to do I/O on |fd| afterwards. |
| 116 | // Note: If this is the write end of a pipe then POLLHUP may also be set simultaneously. We |
| 117 | // still want DEAD_OBJECT in this case. |
| 118 | if (pfd[0].revents & POLLERR) { |
| 119 | LOG_RPC_DETAIL("poll() incoming FD %d results in revents = %d", pfd[0].fd, pfd[0].revents); |
| 120 | return DEAD_OBJECT; |
| 121 | } |
| 122 | |
| 123 | // Success condition; event flag(s) set. Even though POLLHUP may also be set, |
| 124 | // treat it as a success condition to ensure data is drained. |
| 125 | if (pfd[0].revents & event) { |
| 126 | return OK; |
| 127 | } |
| 128 | |
| 129 | // POLLHUP: Peer closed connection. Treat as DEAD_OBJECT. |
| 130 | // This is a very common case, so don't log. |
| 131 | return DEAD_OBJECT; |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 134 | } // namespace android |