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 <android-base/macros.h>
25 #include <android-base/scopeguard.h>
26
27 #include "RpcState.h"
28 namespace android {
29
make()30 std::unique_ptr<FdTrigger> FdTrigger::make() {
31 auto ret = std::make_unique<FdTrigger>();
32 #ifndef BINDER_RPC_SINGLE_THREADED
33 if (!android::base::Pipe(&ret->mRead, &ret->mWrite)) {
34 ALOGE("Could not create pipe %s", strerror(errno));
35 return nullptr;
36 }
37 #endif
38 return ret;
39 }
40
trigger()41 void FdTrigger::trigger() {
42 #ifdef BINDER_RPC_SINGLE_THREADED
43 mTriggered = true;
44 #else
45 mWrite.reset();
46 #endif
47 }
48
isTriggered()49 bool FdTrigger::isTriggered() {
50 #ifdef BINDER_RPC_SINGLE_THREADED
51 return mTriggered;
52 #else
53 return mWrite == -1;
54 #endif
55 }
56
triggerablePoll(const android::RpcTransportFd & transportFd,int16_t event)57 status_t FdTrigger::triggerablePoll(const android::RpcTransportFd& transportFd, int16_t event) {
58 #ifdef BINDER_RPC_SINGLE_THREADED
59 if (mTriggered) {
60 return DEAD_OBJECT;
61 }
62 #endif
63
64 LOG_ALWAYS_FATAL_IF(event == 0, "triggerablePoll %d with event 0 is not allowed",
65 transportFd.fd.get());
66 pollfd pfd[]{
67 {.fd = transportFd.fd.get(), .events = static_cast<int16_t>(event), .revents = 0},
68 #ifndef BINDER_RPC_SINGLE_THREADED
69 {.fd = mRead.get(), .events = 0, .revents = 0},
70 #endif
71 };
72
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
80 int ret = TEMP_FAILURE_RETRY(poll(pfd, arraysize(pfd), -1));
81 if (ret < 0) {
82 return -errno;
83 }
84 LOG_ALWAYS_FATAL_IF(ret == 0, "poll(%d) returns 0 with infinite timeout", transportFd.fd.get());
85
86 // At least one FD has events. Check them.
87
88 #ifndef BINDER_RPC_SINGLE_THREADED
89 // 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.
102 #endif
103
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;
126 }
127
128 } // namespace android
129