blob: 7263e236fc9282003545a4f83daadaa47eb9fa44 [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
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +000024#include <binder/Functional.h>
Yifan Hong8c950422021-08-05 17:13:55 -070025
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070026#include "FdUtils.h"
Yifan Honge0b37bd2021-09-23 17:23:02 -070027#include "RpcState.h"
Tomasz Wasilczykdf07f942023-11-02 15:07:45 -070028#include "Utils.h"
29
Yifan Hong8c950422021-08-05 17:13:55 -070030namespace android {
31
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +000032using namespace android::binder::impl;
33
Yifan Hong8c950422021-08-05 17:13:55 -070034std::unique_ptr<FdTrigger> FdTrigger::make() {
35 auto ret = std::make_unique<FdTrigger>();
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000036#ifndef BINDER_RPC_SINGLE_THREADED
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070037 if (!binder::Pipe(&ret->mRead, &ret->mWrite)) {
Yifan Hong8c950422021-08-05 17:13:55 -070038 ALOGE("Could not create pipe %s", strerror(errno));
39 return nullptr;
40 }
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000041#endif
Yifan Hong8c950422021-08-05 17:13:55 -070042 return ret;
43}
44
45void FdTrigger::trigger() {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000046#ifdef BINDER_RPC_SINGLE_THREADED
47 mTriggered = true;
48#else
Yifan Hong8c950422021-08-05 17:13:55 -070049 mWrite.reset();
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000050#endif
Yifan Hong8c950422021-08-05 17:13:55 -070051}
52
53bool FdTrigger::isTriggered() {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000054#ifdef BINDER_RPC_SINGLE_THREADED
55 return mTriggered;
56#else
Tomasz Wasilczykbfb13a82023-11-14 11:33:10 -080057 return !mWrite.ok();
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000058#endif
Yifan Hong8c950422021-08-05 17:13:55 -070059}
60
Pawan3e0061c2022-08-26 21:08:34 +000061status_t FdTrigger::triggerablePoll(const android::RpcTransportFd& transportFd, int16_t event) {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000062#ifdef BINDER_RPC_SINGLE_THREADED
63 if (mTriggered) {
64 return DEAD_OBJECT;
65 }
66#endif
67
Pawan49d74cb2022-08-03 21:19:11 +000068 LOG_ALWAYS_FATAL_IF(event == 0, "triggerablePoll %d with event 0 is not allowed",
69 transportFd.fd.get());
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000070 pollfd pfd[]{
Pawan49d74cb2022-08-03 21:19:11 +000071 {.fd = transportFd.fd.get(), .events = static_cast<int16_t>(event), .revents = 0},
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000072#ifndef BINDER_RPC_SINGLE_THREADED
73 {.fd = mRead.get(), .events = 0, .revents = 0},
74#endif
75 };
Pawan49d74cb2022-08-03 21:19:11 +000076
77 LOG_ALWAYS_FATAL_IF(transportFd.isInPollingState() == true,
78 "Only one thread should be polling on Fd!");
79
80 transportFd.setPollingState(true);
Tomasz Wasilczyk1de48a22023-10-30 14:19:19 +000081 auto pollingStateGuard = make_scope_guard([&]() { transportFd.setPollingState(false); });
Pawan49d74cb2022-08-03 21:19:11 +000082
Tomasz Wasilczykdf07f942023-11-02 15:07:45 -070083 int ret = TEMP_FAILURE_RETRY(poll(pfd, countof(pfd), -1));
Yifan Honge0b37bd2021-09-23 17:23:02 -070084 if (ret < 0) {
Devin Moored08d9de2024-09-09 20:09:21 +000085 int saved_errno = errno;
86 ALOGE("FdTrigger poll returned error: %d, with error: %s", ret, strerror(saved_errno));
87 return -saved_errno;
Yifan Hong8c950422021-08-05 17:13:55 -070088 }
Pawan49d74cb2022-08-03 21:19:11 +000089 LOG_ALWAYS_FATAL_IF(ret == 0, "poll(%d) returns 0 with infinite timeout", transportFd.fd.get());
Yifan Honge0b37bd2021-09-23 17:23:02 -070090
91 // At least one FD has events. Check them.
92
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000093#ifndef BINDER_RPC_SINGLE_THREADED
Yifan Honge0b37bd2021-09-23 17:23:02 -070094 // 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.
Andrei Homescuffa3aaa2022-04-07 05:06:33 +0000107#endif
Yifan Honge0b37bd2021-09-23 17:23:02 -0700108
109 // POLLNVAL: invalid FD number, e.g. not opened.
110 if (pfd[0].revents & POLLNVAL) {
Devin Mooreddaccb72024-09-09 20:50:07 +0000111 LOG_ALWAYS_FATAL("Invalid FD number (%d) in FdTrigger (POLLNVAL)", pfd[0].fd);
Yifan Honge0b37bd2021-09-23 17:23:02 -0700112 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;
Yifan Hong8c950422021-08-05 17:13:55 -0700132}
133
Yifan Hong8c950422021-08-05 17:13:55 -0700134} // namespace android