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 int saved_errno = errno;
86 ALOGE("FdTrigger poll returned error: %d, with error: %s", ret, strerror(saved_errno));
87 return -saved_errno;
88 }
89 LOG_ALWAYS_FATAL_IF(ret == 0, "poll(%d) returns 0 with infinite timeout", transportFd.fd.get());
90
91 // At least one FD has events. Check them.
92
93 #ifndef BINDER_RPC_SINGLE_THREADED
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.
107 #endif
108
109 // POLLNVAL: invalid FD number, e.g. not opened.
110 if (pfd[0].revents & POLLNVAL) {
111 LOG_ALWAYS_FATAL("Invalid FD number (%d) in FdTrigger (POLLNVAL)", pfd[0].fd);
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;
132 }
133
134 } // namespace android
135