blob: 37c21bb5cda670cf2ce300c11808ba61ed2cbd9f [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
Sebastian Pickl25c1a3b2023-10-30 08:02:24 +000024#include <android-base/scopeguard.h>
Yifan Hong8c950422021-08-05 17:13:55 -070025
Yifan Honge0b37bd2021-09-23 17:23:02 -070026#include "RpcState.h"
Tomasz Wasilczykdf07f942023-11-02 15:07:45 -070027#include "Utils.h"
28
Yifan Hong8c950422021-08-05 17:13:55 -070029namespace android {
30
31std::unique_ptr<FdTrigger> FdTrigger::make() {
32 auto ret = std::make_unique<FdTrigger>();
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000033#ifndef BINDER_RPC_SINGLE_THREADED
Yifan Hong8c950422021-08-05 17:13:55 -070034 if (!android::base::Pipe(&ret->mRead, &ret->mWrite)) {
35 ALOGE("Could not create pipe %s", strerror(errno));
36 return nullptr;
37 }
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000038#endif
Yifan Hong8c950422021-08-05 17:13:55 -070039 return ret;
40}
41
42void FdTrigger::trigger() {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000043#ifdef BINDER_RPC_SINGLE_THREADED
44 mTriggered = true;
45#else
Yifan Hong8c950422021-08-05 17:13:55 -070046 mWrite.reset();
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000047#endif
Yifan Hong8c950422021-08-05 17:13:55 -070048}
49
50bool FdTrigger::isTriggered() {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000051#ifdef BINDER_RPC_SINGLE_THREADED
52 return mTriggered;
53#else
Yifan Hong8c950422021-08-05 17:13:55 -070054 return mWrite == -1;
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000055#endif
Yifan Hong8c950422021-08-05 17:13:55 -070056}
57
Pawan3e0061c2022-08-26 21:08:34 +000058status_t FdTrigger::triggerablePoll(const android::RpcTransportFd& transportFd, int16_t event) {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000059#ifdef BINDER_RPC_SINGLE_THREADED
60 if (mTriggered) {
61 return DEAD_OBJECT;
62 }
63#endif
64
Pawan49d74cb2022-08-03 21:19:11 +000065 LOG_ALWAYS_FATAL_IF(event == 0, "triggerablePoll %d with event 0 is not allowed",
66 transportFd.fd.get());
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000067 pollfd pfd[]{
Pawan49d74cb2022-08-03 21:19:11 +000068 {.fd = transportFd.fd.get(), .events = static_cast<int16_t>(event), .revents = 0},
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000069#ifndef BINDER_RPC_SINGLE_THREADED
70 {.fd = mRead.get(), .events = 0, .revents = 0},
71#endif
72 };
Pawan49d74cb2022-08-03 21:19:11 +000073
74 LOG_ALWAYS_FATAL_IF(transportFd.isInPollingState() == true,
75 "Only one thread should be polling on Fd!");
76
77 transportFd.setPollingState(true);
Sebastian Pickl25c1a3b2023-10-30 08:02:24 +000078 auto pollingStateGuard =
79 android::base::make_scope_guard([&]() { transportFd.setPollingState(false); });
Pawan49d74cb2022-08-03 21:19:11 +000080
Tomasz Wasilczykdf07f942023-11-02 15:07:45 -070081 int ret = TEMP_FAILURE_RETRY(poll(pfd, countof(pfd), -1));
Yifan Honge0b37bd2021-09-23 17:23:02 -070082 if (ret < 0) {
83 return -errno;
Yifan Hong8c950422021-08-05 17:13:55 -070084 }
Pawan49d74cb2022-08-03 21:19:11 +000085 LOG_ALWAYS_FATAL_IF(ret == 0, "poll(%d) returns 0 with infinite timeout", transportFd.fd.get());
Yifan Honge0b37bd2021-09-23 17:23:02 -070086
87 // At least one FD has events. Check them.
88
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000089#ifndef BINDER_RPC_SINGLE_THREADED
Yifan Honge0b37bd2021-09-23 17:23:02 -070090 // Detect explicit trigger(): DEAD_OBJECT
91 if (pfd[1].revents & POLLHUP) {
92 return DEAD_OBJECT;
93 }
94 // See unknown flags in trigger FD's revents (POLLERR / POLLNVAL).
95 // Treat this error condition as UNKNOWN_ERROR.
96 if (pfd[1].revents != 0) {
97 ALOGE("Unknown revents on trigger FD %d: revents = %d", pfd[1].fd, pfd[1].revents);
98 return UNKNOWN_ERROR;
99 }
100
101 // pfd[1].revents is 0, hence pfd[0].revents must be set, and only possible values are
102 // a subset of event | POLLHUP | POLLERR | POLLNVAL.
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000103#endif
Yifan Honge0b37bd2021-09-23 17:23:02 -0700104
105 // POLLNVAL: invalid FD number, e.g. not opened.
106 if (pfd[0].revents & POLLNVAL) {
107 return BAD_VALUE;
108 }
109
110 // Error condition. It wouldn't be possible to do I/O on |fd| afterwards.
111 // Note: If this is the write end of a pipe then POLLHUP may also be set simultaneously. We
112 // still want DEAD_OBJECT in this case.
113 if (pfd[0].revents & POLLERR) {
114 LOG_RPC_DETAIL("poll() incoming FD %d results in revents = %d", pfd[0].fd, pfd[0].revents);
115 return DEAD_OBJECT;
116 }
117
118 // Success condition; event flag(s) set. Even though POLLHUP may also be set,
119 // treat it as a success condition to ensure data is drained.
120 if (pfd[0].revents & event) {
121 return OK;
122 }
123
124 // POLLHUP: Peer closed connection. Treat as DEAD_OBJECT.
125 // This is a very common case, so don't log.
126 return DEAD_OBJECT;
Yifan Hong8c950422021-08-05 17:13:55 -0700127}
128
Yifan Hong8c950422021-08-05 17:13:55 -0700129} // namespace android