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