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