blob: f9b73fce2117a8eeda9e8fdd2cb4107647df72f7 [file] [log] [blame]
Yifan Hongf6b4d5c2021-06-23 19:12:32 -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 "RpcRawTransport"
18#include <log/log.h>
19
Yifan Hong8c950422021-08-05 17:13:55 -070020#include <poll.h>
21
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070022#include <binder/RpcTransportRaw.h>
23
Yifan Hong8c950422021-08-05 17:13:55 -070024#include "FdTrigger.h"
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070025#include "RpcState.h"
26
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070027namespace android {
28
29namespace {
30
31// RpcTransport with TLS disabled.
32class RpcTransportRaw : public RpcTransport {
33public:
34 explicit RpcTransportRaw(android::base::unique_fd socket) : mSocket(std::move(socket)) {}
Andrei Homescu1975aaa2022-03-19 02:34:57 +000035 status_t pollRead(void) override {
36 uint8_t buf;
37 ssize_t ret = TEMP_FAILURE_RETRY(
38 ::recv(mSocket.get(), &buf, sizeof(buf), MSG_PEEK | MSG_DONTWAIT));
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070039 if (ret < 0) {
Andrei Homescu5ad71b52022-03-11 03:49:12 +000040 int savedErrno = errno;
41 if (savedErrno == EAGAIN || savedErrno == EWOULDBLOCK) {
42 return WOULD_BLOCK;
43 }
44
Andrei Homescu1975aaa2022-03-19 02:34:57 +000045 LOG_RPC_DETAIL("RpcTransport poll(): %s", strerror(savedErrno));
Andrei Homescu5ad71b52022-03-11 03:49:12 +000046 return -savedErrno;
Andrei Homescu1975aaa2022-03-19 02:34:57 +000047 } else if (ret == 0) {
48 return DEAD_OBJECT;
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070049 }
Andrei Homescu5ad71b52022-03-11 03:49:12 +000050
Andrei Homescu5ad71b52022-03-11 03:49:12 +000051 return OK;
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070052 }
Yifan Hong8c950422021-08-05 17:13:55 -070053
Andrei Homescua39e4ed2021-12-10 08:41:54 +000054 template <typename SendOrReceive>
Devin Moore695368f2022-06-03 22:29:14 +000055 status_t interruptableReadOrWrite(
56 FdTrigger* fdTrigger, iovec* iovs, int niovs, SendOrReceive sendOrReceiveFun,
57 const char* funName, int16_t event,
58 const std::optional<android::base::function_ref<status_t()>>& altPoll) {
Yifan Hong8c950422021-08-05 17:13:55 -070059 MAYBE_WAIT_IN_FLAKE_MODE;
60
Colin Cross9adfeaf2022-01-21 17:22:09 -080061 if (niovs < 0) {
62 return BAD_VALUE;
63 }
64
Steven Moreland301c3f02021-09-14 17:49:04 -070065 // Since we didn't poll, we need to manually check to see if it was triggered. Otherwise, we
66 // may never know we should be shutting down.
67 if (fdTrigger->isTriggered()) {
68 return DEAD_OBJECT;
69 }
70
Andrei Homescua39e4ed2021-12-10 08:41:54 +000071 // If iovs has one or more empty vectors at the end and
72 // we somehow advance past all the preceding vectors and
73 // pass some or all of the empty ones to sendmsg/recvmsg,
74 // the call will return processSize == 0. In that case
75 // we should be returning OK but instead return DEAD_OBJECT.
76 // To avoid this problem, we make sure here that the last
77 // vector at iovs[niovs - 1] has a non-zero length.
78 while (niovs > 0 && iovs[niovs - 1].iov_len == 0) {
79 niovs--;
80 }
81 if (niovs == 0) {
82 // The vectors are all empty, so we have nothing to send.
83 return OK;
84 }
85
Steven Moreland43921d52021-09-27 17:15:56 -070086 bool havePolled = false;
87 while (true) {
Andrei Homescua39e4ed2021-12-10 08:41:54 +000088 msghdr msg{
89 .msg_iov = iovs,
Colin Cross9adfeaf2022-01-21 17:22:09 -080090 // posix uses int, glibc uses size_t. niovs is a
91 // non-negative int and can be cast to either.
92 .msg_iovlen = static_cast<decltype(msg.msg_iovlen)>(niovs),
Andrei Homescua39e4ed2021-12-10 08:41:54 +000093 };
94 ssize_t processSize =
95 TEMP_FAILURE_RETRY(sendOrReceiveFun(mSocket.get(), &msg, MSG_NOSIGNAL));
Steven Moreland301c3f02021-09-14 17:49:04 -070096
97 if (processSize < 0) {
Steven Moreland5252e752021-09-14 14:06:03 -070098 int savedErrno = errno;
Steven Moreland301c3f02021-09-14 17:49:04 -070099
100 // Still return the error on later passes, since it would expose
101 // a problem with polling
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000102 if (havePolled || (savedErrno != EAGAIN && savedErrno != EWOULDBLOCK)) {
Steven Moreland301c3f02021-09-14 17:49:04 -0700103 LOG_RPC_DETAIL("RpcTransport %s(): %s", funName, strerror(savedErrno));
104 return -savedErrno;
105 }
106 } else if (processSize == 0) {
107 return DEAD_OBJECT;
108 } else {
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000109 while (processSize > 0 && niovs > 0) {
110 auto& iov = iovs[0];
111 if (static_cast<size_t>(processSize) < iov.iov_len) {
112 // Advance the base of the current iovec
113 iov.iov_base = reinterpret_cast<char*>(iov.iov_base) + processSize;
114 iov.iov_len -= processSize;
115 break;
116 }
117
118 // The current iovec was fully written
119 processSize -= iov.iov_len;
120 iovs++;
121 niovs--;
122 }
123 if (niovs == 0) {
124 LOG_ALWAYS_FATAL_IF(processSize > 0,
125 "Reached the end of iovecs "
126 "with %zd bytes remaining",
127 processSize);
Steven Moreland301c3f02021-09-14 17:49:04 -0700128 return OK;
129 }
Yifan Hong8c950422021-08-05 17:13:55 -0700130 }
131
Steven Moreland43921d52021-09-27 17:15:56 -0700132 if (altPoll) {
Devin Moore695368f2022-06-03 22:29:14 +0000133 if (status_t status = (*altPoll)(); status != OK) return status;
Steven Moreland43921d52021-09-27 17:15:56 -0700134 if (fdTrigger->isTriggered()) {
135 return DEAD_OBJECT;
136 }
137 } else {
138 if (status_t status = fdTrigger->triggerablePoll(mSocket.get(), event);
139 status != OK)
140 return status;
141 if (!havePolled) havePolled = true;
142 }
143 }
Yifan Hong8c950422021-08-05 17:13:55 -0700144 }
145
Devin Moore695368f2022-06-03 22:29:14 +0000146 status_t interruptableWriteFully(
147 FdTrigger* fdTrigger, iovec* iovs, int niovs,
148 const std::optional<android::base::function_ref<status_t()>>& altPoll) override {
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000149 return interruptableReadOrWrite(fdTrigger, iovs, niovs, sendmsg, "sendmsg", POLLOUT,
150 altPoll);
Steven Moreland301c3f02021-09-14 17:49:04 -0700151 }
152
Devin Moore695368f2022-06-03 22:29:14 +0000153 status_t interruptableReadFully(
154 FdTrigger* fdTrigger, iovec* iovs, int niovs,
155 const std::optional<android::base::function_ref<status_t()>>& altPoll) override {
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000156 return interruptableReadOrWrite(fdTrigger, iovs, niovs, recvmsg, "recvmsg", POLLIN,
157 altPoll);
Yifan Hong8c950422021-08-05 17:13:55 -0700158 }
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700159
160private:
Steven Moreland301c3f02021-09-14 17:49:04 -0700161 base::unique_fd mSocket;
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700162};
163
164// RpcTransportCtx with TLS disabled.
165class RpcTransportCtxRaw : public RpcTransportCtx {
166public:
Yifan Hongf6d42292021-08-05 23:43:05 -0700167 std::unique_ptr<RpcTransport> newTransport(android::base::unique_fd fd, FdTrigger*) const {
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700168 return std::make_unique<RpcTransportRaw>(std::move(fd));
169 }
Yifan Hong9734cfc2021-09-13 16:14:09 -0700170 std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700171};
Yifan Hong588d59c2021-08-16 17:13:58 -0700172
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700173} // namespace
174
175std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newServerCtx() const {
176 return std::make_unique<RpcTransportCtxRaw>();
177}
178
179std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newClientCtx() const {
180 return std::make_unique<RpcTransportCtxRaw>();
181}
182
183const char *RpcTransportCtxFactoryRaw::toCString() const {
184 return "raw";
185}
186
187std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryRaw::make() {
188 return std::unique_ptr<RpcTransportCtxFactoryRaw>(new RpcTransportCtxFactoryRaw());
189}
190
191} // namespace android