blob: 7cfc78082e811123caca019285c3e5ed5dfefa56 [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 Homescu5ad71b52022-03-11 03:49:12 +000035 status_t peek(void* buf, size_t size, size_t* out_size) override {
Yifan Hongb675ffe2021-08-05 16:37:17 -070036 ssize_t ret = TEMP_FAILURE_RETRY(::recv(mSocket.get(), buf, size, MSG_PEEK));
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070037 if (ret < 0) {
Andrei Homescu5ad71b52022-03-11 03:49:12 +000038 int savedErrno = errno;
39 if (savedErrno == EAGAIN || savedErrno == EWOULDBLOCK) {
40 return WOULD_BLOCK;
41 }
42
43 LOG_RPC_DETAIL("RpcTransport peek(): %s", strerror(savedErrno));
44 return -savedErrno;
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070045 }
Andrei Homescu5ad71b52022-03-11 03:49:12 +000046
47 *out_size = static_cast<size_t>(ret);
48 return OK;
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070049 }
Yifan Hong8c950422021-08-05 17:13:55 -070050
Andrei Homescua39e4ed2021-12-10 08:41:54 +000051 template <typename SendOrReceive>
Colin Cross9adfeaf2022-01-21 17:22:09 -080052 status_t interruptableReadOrWrite(FdTrigger* fdTrigger, iovec* iovs, int niovs,
Steven Moreland301c3f02021-09-14 17:49:04 -070053 SendOrReceive sendOrReceiveFun, const char* funName,
Steven Moreland43921d52021-09-27 17:15:56 -070054 int16_t event, const std::function<status_t()>& altPoll) {
Yifan Hong8c950422021-08-05 17:13:55 -070055 MAYBE_WAIT_IN_FLAKE_MODE;
56
Colin Cross9adfeaf2022-01-21 17:22:09 -080057 if (niovs < 0) {
58 return BAD_VALUE;
59 }
60
Steven Moreland301c3f02021-09-14 17:49:04 -070061 // Since we didn't poll, we need to manually check to see if it was triggered. Otherwise, we
62 // may never know we should be shutting down.
63 if (fdTrigger->isTriggered()) {
64 return DEAD_OBJECT;
65 }
66
Andrei Homescua39e4ed2021-12-10 08:41:54 +000067 // If iovs has one or more empty vectors at the end and
68 // we somehow advance past all the preceding vectors and
69 // pass some or all of the empty ones to sendmsg/recvmsg,
70 // the call will return processSize == 0. In that case
71 // we should be returning OK but instead return DEAD_OBJECT.
72 // To avoid this problem, we make sure here that the last
73 // vector at iovs[niovs - 1] has a non-zero length.
74 while (niovs > 0 && iovs[niovs - 1].iov_len == 0) {
75 niovs--;
76 }
77 if (niovs == 0) {
78 // The vectors are all empty, so we have nothing to send.
79 return OK;
80 }
81
Steven Moreland43921d52021-09-27 17:15:56 -070082 bool havePolled = false;
83 while (true) {
Andrei Homescua39e4ed2021-12-10 08:41:54 +000084 msghdr msg{
85 .msg_iov = iovs,
Colin Cross9adfeaf2022-01-21 17:22:09 -080086 // posix uses int, glibc uses size_t. niovs is a
87 // non-negative int and can be cast to either.
88 .msg_iovlen = static_cast<decltype(msg.msg_iovlen)>(niovs),
Andrei Homescua39e4ed2021-12-10 08:41:54 +000089 };
90 ssize_t processSize =
91 TEMP_FAILURE_RETRY(sendOrReceiveFun(mSocket.get(), &msg, MSG_NOSIGNAL));
Steven Moreland301c3f02021-09-14 17:49:04 -070092
93 if (processSize < 0) {
Steven Moreland5252e752021-09-14 14:06:03 -070094 int savedErrno = errno;
Steven Moreland301c3f02021-09-14 17:49:04 -070095
96 // Still return the error on later passes, since it would expose
97 // a problem with polling
Andrei Homescua39e4ed2021-12-10 08:41:54 +000098 if (havePolled || (savedErrno != EAGAIN && savedErrno != EWOULDBLOCK)) {
Steven Moreland301c3f02021-09-14 17:49:04 -070099 LOG_RPC_DETAIL("RpcTransport %s(): %s", funName, strerror(savedErrno));
100 return -savedErrno;
101 }
102 } else if (processSize == 0) {
103 return DEAD_OBJECT;
104 } else {
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000105 while (processSize > 0 && niovs > 0) {
106 auto& iov = iovs[0];
107 if (static_cast<size_t>(processSize) < iov.iov_len) {
108 // Advance the base of the current iovec
109 iov.iov_base = reinterpret_cast<char*>(iov.iov_base) + processSize;
110 iov.iov_len -= processSize;
111 break;
112 }
113
114 // The current iovec was fully written
115 processSize -= iov.iov_len;
116 iovs++;
117 niovs--;
118 }
119 if (niovs == 0) {
120 LOG_ALWAYS_FATAL_IF(processSize > 0,
121 "Reached the end of iovecs "
122 "with %zd bytes remaining",
123 processSize);
Steven Moreland301c3f02021-09-14 17:49:04 -0700124 return OK;
125 }
Yifan Hong8c950422021-08-05 17:13:55 -0700126 }
127
Steven Moreland43921d52021-09-27 17:15:56 -0700128 if (altPoll) {
129 if (status_t status = altPoll(); status != OK) return status;
130 if (fdTrigger->isTriggered()) {
131 return DEAD_OBJECT;
132 }
133 } else {
134 if (status_t status = fdTrigger->triggerablePoll(mSocket.get(), event);
135 status != OK)
136 return status;
137 if (!havePolled) havePolled = true;
138 }
139 }
Yifan Hong8c950422021-08-05 17:13:55 -0700140 }
141
Colin Cross9adfeaf2022-01-21 17:22:09 -0800142 status_t interruptableWriteFully(FdTrigger* fdTrigger, iovec* iovs, int niovs,
Steven Moreland43921d52021-09-27 17:15:56 -0700143 const std::function<status_t()>& altPoll) override {
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000144 return interruptableReadOrWrite(fdTrigger, iovs, niovs, sendmsg, "sendmsg", POLLOUT,
145 altPoll);
Steven Moreland301c3f02021-09-14 17:49:04 -0700146 }
147
Colin Cross9adfeaf2022-01-21 17:22:09 -0800148 status_t interruptableReadFully(FdTrigger* fdTrigger, iovec* iovs, int niovs,
Steven Moreland43921d52021-09-27 17:15:56 -0700149 const std::function<status_t()>& altPoll) override {
Andrei Homescua39e4ed2021-12-10 08:41:54 +0000150 return interruptableReadOrWrite(fdTrigger, iovs, niovs, recvmsg, "recvmsg", POLLIN,
151 altPoll);
Yifan Hong8c950422021-08-05 17:13:55 -0700152 }
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700153
154private:
Steven Moreland301c3f02021-09-14 17:49:04 -0700155 base::unique_fd mSocket;
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700156};
157
158// RpcTransportCtx with TLS disabled.
159class RpcTransportCtxRaw : public RpcTransportCtx {
160public:
Yifan Hongf6d42292021-08-05 23:43:05 -0700161 std::unique_ptr<RpcTransport> newTransport(android::base::unique_fd fd, FdTrigger*) const {
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700162 return std::make_unique<RpcTransportRaw>(std::move(fd));
163 }
Yifan Hong9734cfc2021-09-13 16:14:09 -0700164 std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700165};
Yifan Hong588d59c2021-08-16 17:13:58 -0700166
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700167} // namespace
168
169std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newServerCtx() const {
170 return std::make_unique<RpcTransportCtxRaw>();
171}
172
173std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newClientCtx() const {
174 return std::make_unique<RpcTransportCtxRaw>();
175}
176
177const char *RpcTransportCtxFactoryRaw::toCString() const {
178 return "raw";
179}
180
181std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryRaw::make() {
182 return std::unique_ptr<RpcTransportCtxFactoryRaw>(new RpcTransportCtxFactoryRaw());
183}
184
185} // namespace android