blob: a22bc6fdc9a22e4f2fbb9907ccf0393d482381a3 [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
27using android::base::ErrnoError;
28using android::base::Result;
29
30namespace android {
31
32namespace {
33
34// RpcTransport with TLS disabled.
35class RpcTransportRaw : public RpcTransport {
36public:
37 explicit RpcTransportRaw(android::base::unique_fd socket) : mSocket(std::move(socket)) {}
Yifan Hong218c4072021-08-04 14:59:10 -070038 Result<size_t> peek(void *buf, size_t size) override {
Yifan Hongb675ffe2021-08-05 16:37:17 -070039 ssize_t ret = TEMP_FAILURE_RETRY(::recv(mSocket.get(), buf, size, MSG_PEEK));
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070040 if (ret < 0) {
41 return ErrnoError() << "recv(MSG_PEEK)";
42 }
43 return ret;
44 }
Yifan Hong8c950422021-08-05 17:13:55 -070045
Steven Moreland301c3f02021-09-14 17:49:04 -070046 template <typename Buffer, typename SendOrReceive>
47 status_t interruptableReadOrWrite(FdTrigger* fdTrigger, Buffer buffer, size_t size,
48 SendOrReceive sendOrReceiveFun, const char* funName,
49 int16_t event) {
50 const Buffer end = buffer + size;
Yifan Hong8c950422021-08-05 17:13:55 -070051
52 MAYBE_WAIT_IN_FLAKE_MODE;
53
Steven Moreland301c3f02021-09-14 17:49:04 -070054 // Since we didn't poll, we need to manually check to see if it was triggered. Otherwise, we
55 // may never know we should be shutting down.
56 if (fdTrigger->isTriggered()) {
57 return DEAD_OBJECT;
58 }
59
60 bool first = true;
Yifan Hong8c950422021-08-05 17:13:55 -070061 status_t status;
Steven Moreland301c3f02021-09-14 17:49:04 -070062 do {
63 ssize_t processSize = TEMP_FAILURE_RETRY(
64 sendOrReceiveFun(mSocket.get(), buffer, end - buffer, MSG_NOSIGNAL));
65
66 if (processSize < 0) {
Steven Moreland5252e752021-09-14 14:06:03 -070067 int savedErrno = errno;
Steven Moreland301c3f02021-09-14 17:49:04 -070068
69 // Still return the error on later passes, since it would expose
70 // a problem with polling
71 if (!first || (first && savedErrno != EAGAIN && savedErrno != EWOULDBLOCK)) {
72 LOG_RPC_DETAIL("RpcTransport %s(): %s", funName, strerror(savedErrno));
73 return -savedErrno;
74 }
75 } else if (processSize == 0) {
76 return DEAD_OBJECT;
77 } else {
78 buffer += processSize;
79 if (buffer == end) {
80 return OK;
81 }
Yifan Hong8c950422021-08-05 17:13:55 -070082 }
83
Steven Moreland301c3f02021-09-14 17:49:04 -070084 if (first) first = false;
85 } while ((status = fdTrigger->triggerablePoll(mSocket.get(), event)) == OK);
Yifan Hong8c950422021-08-05 17:13:55 -070086 return status;
87 }
88
Steven Moreland301c3f02021-09-14 17:49:04 -070089 status_t interruptableWriteFully(FdTrigger* fdTrigger, const void* data, size_t size) override {
90 return interruptableReadOrWrite(fdTrigger, reinterpret_cast<const uint8_t*>(data), size,
91 send, "send", POLLOUT);
92 }
93
Yifan Hong8c950422021-08-05 17:13:55 -070094 status_t interruptableReadFully(FdTrigger* fdTrigger, void* data, size_t size) override {
Steven Moreland301c3f02021-09-14 17:49:04 -070095 return interruptableReadOrWrite(fdTrigger, reinterpret_cast<uint8_t*>(data), size, recv,
96 "recv", POLLIN);
Yifan Hong8c950422021-08-05 17:13:55 -070097 }
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070098
99private:
Steven Moreland301c3f02021-09-14 17:49:04 -0700100 base::unique_fd mSocket;
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700101};
102
103// RpcTransportCtx with TLS disabled.
104class RpcTransportCtxRaw : public RpcTransportCtx {
105public:
Yifan Hongf6d42292021-08-05 23:43:05 -0700106 std::unique_ptr<RpcTransport> newTransport(android::base::unique_fd fd, FdTrigger*) const {
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700107 return std::make_unique<RpcTransportRaw>(std::move(fd));
108 }
Yifan Hong9734cfc2021-09-13 16:14:09 -0700109 std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700110};
Yifan Hong588d59c2021-08-16 17:13:58 -0700111
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700112} // namespace
113
114std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newServerCtx() const {
115 return std::make_unique<RpcTransportCtxRaw>();
116}
117
118std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newClientCtx() const {
119 return std::make_unique<RpcTransportCtxRaw>();
120}
121
122const char *RpcTransportCtxFactoryRaw::toCString() const {
123 return "raw";
124}
125
126std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryRaw::make() {
127 return std::unique_ptr<RpcTransportCtxFactoryRaw>(new RpcTransportCtxFactoryRaw());
128}
129
130} // namespace android