blob: 256d58786629332f00e0e2378bbb5b09dbd6d98e [file] [log] [blame]
Yifan Hong8c950422021-08-05 17:13:55 -07001/*
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 Honge0b37bd2021-09-23 17:23:02 -070020#include "FdTrigger.h"
21
Yifan Hong8c950422021-08-05 17:13:55 -070022#include <poll.h>
23
24#include <android-base/macros.h>
Pawan49d74cb2022-08-03 21:19:11 +000025#include <android-base/scopeguard.h>
Yifan Hong8c950422021-08-05 17:13:55 -070026
Yifan Honge0b37bd2021-09-23 17:23:02 -070027#include "RpcState.h"
Yifan Hong8c950422021-08-05 17:13:55 -070028namespace android {
29
30std::unique_ptr<FdTrigger> FdTrigger::make() {
31 auto ret = std::make_unique<FdTrigger>();
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000032#ifndef BINDER_RPC_SINGLE_THREADED
Yifan Hong8c950422021-08-05 17:13:55 -070033 if (!android::base::Pipe(&ret->mRead, &ret->mWrite)) {
34 ALOGE("Could not create pipe %s", strerror(errno));
35 return nullptr;
36 }
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000037#endif
Yifan Hong8c950422021-08-05 17:13:55 -070038 return ret;
39}
40
41void FdTrigger::trigger() {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000042#ifdef BINDER_RPC_SINGLE_THREADED
43 mTriggered = true;
44#else
Yifan Hong8c950422021-08-05 17:13:55 -070045 mWrite.reset();
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000046#endif
Yifan Hong8c950422021-08-05 17:13:55 -070047}
48
49bool FdTrigger::isTriggered() {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000050#ifdef BINDER_RPC_SINGLE_THREADED
51 return mTriggered;
52#else
Yifan Hong8c950422021-08-05 17:13:55 -070053 return mWrite == -1;
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000054#endif
Yifan Hong8c950422021-08-05 17:13:55 -070055}
56
Pawan49d74cb2022-08-03 21:19:11 +000057status_t FdTrigger::triggerablePoll(const android::TransportFd& transportFd, int16_t event) {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000058#ifdef BINDER_RPC_SINGLE_THREADED
59 if (mTriggered) {
60 return DEAD_OBJECT;
61 }
62#endif
63
Pawan49d74cb2022-08-03 21:19:11 +000064 LOG_ALWAYS_FATAL_IF(event == 0, "triggerablePoll %d with event 0 is not allowed",
65 transportFd.fd.get());
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000066 pollfd pfd[]{
Pawan49d74cb2022-08-03 21:19:11 +000067 {.fd = transportFd.fd.get(), .events = static_cast<int16_t>(event), .revents = 0},
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000068#ifndef BINDER_RPC_SINGLE_THREADED
69 {.fd = mRead.get(), .events = 0, .revents = 0},
70#endif
71 };
Pawan49d74cb2022-08-03 21:19:11 +000072
73 LOG_ALWAYS_FATAL_IF(transportFd.isInPollingState() == true,
74 "Only one thread should be polling on Fd!");
75
76 transportFd.setPollingState(true);
77 auto pollingStateGuard =
78 android::base::make_scope_guard([&]() { transportFd.setPollingState(false); });
79
Yifan Honge0b37bd2021-09-23 17:23:02 -070080 int ret = TEMP_FAILURE_RETRY(poll(pfd, arraysize(pfd), -1));
81 if (ret < 0) {
82 return -errno;
Yifan Hong8c950422021-08-05 17:13:55 -070083 }
Pawan49d74cb2022-08-03 21:19:11 +000084 LOG_ALWAYS_FATAL_IF(ret == 0, "poll(%d) returns 0 with infinite timeout", transportFd.fd.get());
Yifan Honge0b37bd2021-09-23 17:23:02 -070085
86 // At least one FD has events. Check them.
87
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000088#ifndef BINDER_RPC_SINGLE_THREADED
Yifan Honge0b37bd2021-09-23 17:23:02 -070089 // Detect explicit trigger(): DEAD_OBJECT
90 if (pfd[1].revents & POLLHUP) {
91 return DEAD_OBJECT;
92 }
93 // See unknown flags in trigger FD's revents (POLLERR / POLLNVAL).
94 // Treat this error condition as UNKNOWN_ERROR.
95 if (pfd[1].revents != 0) {
96 ALOGE("Unknown revents on trigger FD %d: revents = %d", pfd[1].fd, pfd[1].revents);
97 return UNKNOWN_ERROR;
98 }
99
100 // pfd[1].revents is 0, hence pfd[0].revents must be set, and only possible values are
101 // a subset of event | POLLHUP | POLLERR | POLLNVAL.
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000102#endif
Yifan Honge0b37bd2021-09-23 17:23:02 -0700103
104 // POLLNVAL: invalid FD number, e.g. not opened.
105 if (pfd[0].revents & POLLNVAL) {
106 return BAD_VALUE;
107 }
108
109 // Error condition. It wouldn't be possible to do I/O on |fd| afterwards.
110 // Note: If this is the write end of a pipe then POLLHUP may also be set simultaneously. We
111 // still want DEAD_OBJECT in this case.
112 if (pfd[0].revents & POLLERR) {
113 LOG_RPC_DETAIL("poll() incoming FD %d results in revents = %d", pfd[0].fd, pfd[0].revents);
114 return DEAD_OBJECT;
115 }
116
117 // Success condition; event flag(s) set. Even though POLLHUP may also be set,
118 // treat it as a success condition to ensure data is drained.
119 if (pfd[0].revents & event) {
120 return OK;
121 }
122
123 // POLLHUP: Peer closed connection. Treat as DEAD_OBJECT.
124 // This is a very common case, so don't log.
125 return DEAD_OBJECT;
Yifan Hong8c950422021-08-05 17:13:55 -0700126}
127
Yifan Hong8c950422021-08-05 17:13:55 -0700128} // namespace android