blob: d123fd1f2be854c279308a596dc237b778743f8f [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
24#include <android-base/macros.h>
25
Yifan Honge0b37bd2021-09-23 17:23:02 -070026#include "RpcState.h"
Yifan Hong8c950422021-08-05 17:13:55 -070027namespace android {
28
29std::unique_ptr<FdTrigger> FdTrigger::make() {
30 auto ret = std::make_unique<FdTrigger>();
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000031#ifndef BINDER_RPC_SINGLE_THREADED
Yifan Hong8c950422021-08-05 17:13:55 -070032 if (!android::base::Pipe(&ret->mRead, &ret->mWrite)) {
33 ALOGE("Could not create pipe %s", strerror(errno));
34 return nullptr;
35 }
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000036#endif
Yifan Hong8c950422021-08-05 17:13:55 -070037 return ret;
38}
39
40void FdTrigger::trigger() {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000041#ifdef BINDER_RPC_SINGLE_THREADED
42 mTriggered = true;
43#else
Yifan Hong8c950422021-08-05 17:13:55 -070044 mWrite.reset();
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000045#endif
Yifan Hong8c950422021-08-05 17:13:55 -070046}
47
48bool FdTrigger::isTriggered() {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000049#ifdef BINDER_RPC_SINGLE_THREADED
50 return mTriggered;
51#else
Yifan Hong8c950422021-08-05 17:13:55 -070052 return mWrite == -1;
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000053#endif
Yifan Hong8c950422021-08-05 17:13:55 -070054}
55
56status_t FdTrigger::triggerablePoll(base::borrowed_fd fd, int16_t event) {
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000057#ifdef BINDER_RPC_SINGLE_THREADED
58 if (mTriggered) {
59 return DEAD_OBJECT;
60 }
61#endif
62
Yifan Honge0b37bd2021-09-23 17:23:02 -070063 LOG_ALWAYS_FATAL_IF(event == 0, "triggerablePoll %d with event 0 is not allowed", fd.get());
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000064 pollfd pfd[]{
65 {.fd = fd.get(), .events = static_cast<int16_t>(event), .revents = 0},
66#ifndef BINDER_RPC_SINGLE_THREADED
67 {.fd = mRead.get(), .events = 0, .revents = 0},
68#endif
69 };
Yifan Honge0b37bd2021-09-23 17:23:02 -070070 int ret = TEMP_FAILURE_RETRY(poll(pfd, arraysize(pfd), -1));
71 if (ret < 0) {
72 return -errno;
Yifan Hong8c950422021-08-05 17:13:55 -070073 }
Yifan Honge0b37bd2021-09-23 17:23:02 -070074 LOG_ALWAYS_FATAL_IF(ret == 0, "poll(%d) returns 0 with infinite timeout", fd.get());
75
76 // At least one FD has events. Check them.
77
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000078#ifndef BINDER_RPC_SINGLE_THREADED
Yifan Honge0b37bd2021-09-23 17:23:02 -070079 // Detect explicit trigger(): DEAD_OBJECT
80 if (pfd[1].revents & POLLHUP) {
81 return DEAD_OBJECT;
82 }
83 // See unknown flags in trigger FD's revents (POLLERR / POLLNVAL).
84 // Treat this error condition as UNKNOWN_ERROR.
85 if (pfd[1].revents != 0) {
86 ALOGE("Unknown revents on trigger FD %d: revents = %d", pfd[1].fd, pfd[1].revents);
87 return UNKNOWN_ERROR;
88 }
89
90 // pfd[1].revents is 0, hence pfd[0].revents must be set, and only possible values are
91 // a subset of event | POLLHUP | POLLERR | POLLNVAL.
Andrei Homescuffa3aaa2022-04-07 05:06:33 +000092#endif
Yifan Honge0b37bd2021-09-23 17:23:02 -070093
94 // POLLNVAL: invalid FD number, e.g. not opened.
95 if (pfd[0].revents & POLLNVAL) {
96 return BAD_VALUE;
97 }
98
99 // Error condition. It wouldn't be possible to do I/O on |fd| afterwards.
100 // Note: If this is the write end of a pipe then POLLHUP may also be set simultaneously. We
101 // still want DEAD_OBJECT in this case.
102 if (pfd[0].revents & POLLERR) {
103 LOG_RPC_DETAIL("poll() incoming FD %d results in revents = %d", pfd[0].fd, pfd[0].revents);
104 return DEAD_OBJECT;
105 }
106
107 // Success condition; event flag(s) set. Even though POLLHUP may also be set,
108 // treat it as a success condition to ensure data is drained.
109 if (pfd[0].revents & event) {
110 return OK;
111 }
112
113 // POLLHUP: Peer closed connection. Treat as DEAD_OBJECT.
114 // This is a very common case, so don't log.
115 return DEAD_OBJECT;
Yifan Hong8c950422021-08-05 17:13:55 -0700116}
117
Yifan Hong8c950422021-08-05 17:13:55 -0700118} // namespace android