blob: 41f4a9f2bf031b4f2e66f6c594de3f74cd8110e9 [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
46 status_t interruptableWriteFully(FdTrigger* fdTrigger, const void* data, size_t size) override {
47 const uint8_t* buffer = reinterpret_cast<const uint8_t*>(data);
48 const uint8_t* end = buffer + size;
49
50 MAYBE_WAIT_IN_FLAKE_MODE;
51
52 status_t status;
53 while ((status = fdTrigger->triggerablePoll(mSocket.get(), POLLOUT)) == OK) {
Steven Moreland5252e752021-09-14 14:06:03 -070054 ssize_t writeSize =
55 TEMP_FAILURE_RETRY(::send(mSocket.get(), buffer, end - buffer, MSG_NOSIGNAL));
56 if (writeSize < 0) {
57 int savedErrno = errno;
58 LOG_RPC_DETAIL("RpcTransport send(): %s", strerror(savedErrno));
59 return -savedErrno;
Yifan Hong8c950422021-08-05 17:13:55 -070060 }
61
Steven Moreland5252e752021-09-14 14:06:03 -070062 if (writeSize == 0) return DEAD_OBJECT;
Yifan Hong8c950422021-08-05 17:13:55 -070063
Steven Moreland5252e752021-09-14 14:06:03 -070064 buffer += writeSize;
Yifan Hong8c950422021-08-05 17:13:55 -070065 if (buffer == end) return OK;
66 }
67 return status;
68 }
69
70 status_t interruptableReadFully(FdTrigger* fdTrigger, void* data, size_t size) override {
71 uint8_t* buffer = reinterpret_cast<uint8_t*>(data);
72 uint8_t* end = buffer + size;
73
74 MAYBE_WAIT_IN_FLAKE_MODE;
75
76 status_t status;
77 while ((status = fdTrigger->triggerablePoll(mSocket.get(), POLLIN)) == OK) {
Steven Moreland5252e752021-09-14 14:06:03 -070078 ssize_t readSize =
79 TEMP_FAILURE_RETRY(::recv(mSocket.get(), buffer, end - buffer, MSG_NOSIGNAL));
80 if (readSize < 0) {
81 int savedErrno = errno;
82 LOG_RPC_DETAIL("RpcTransport recv(): %s", strerror(savedErrno));
83 return -savedErrno;
Yifan Hong8c950422021-08-05 17:13:55 -070084 }
85
Steven Moreland5252e752021-09-14 14:06:03 -070086 if (readSize == 0) return DEAD_OBJECT; // EOF
Yifan Hong8c950422021-08-05 17:13:55 -070087
Steven Moreland5252e752021-09-14 14:06:03 -070088 buffer += readSize;
Yifan Hong8c950422021-08-05 17:13:55 -070089 if (buffer == end) return OK;
90 }
91 return status;
92 }
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070093
94private:
95 android::base::unique_fd mSocket;
96};
97
98// RpcTransportCtx with TLS disabled.
99class RpcTransportCtxRaw : public RpcTransportCtx {
100public:
Yifan Hongf6d42292021-08-05 23:43:05 -0700101 std::unique_ptr<RpcTransport> newTransport(android::base::unique_fd fd, FdTrigger*) const {
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700102 return std::make_unique<RpcTransportRaw>(std::move(fd));
103 }
Yifan Hong9734cfc2021-09-13 16:14:09 -0700104 std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700105};
Yifan Hong588d59c2021-08-16 17:13:58 -0700106
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700107} // namespace
108
109std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newServerCtx() const {
110 return std::make_unique<RpcTransportCtxRaw>();
111}
112
113std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newClientCtx() const {
114 return std::make_unique<RpcTransportCtxRaw>();
115}
116
117const char *RpcTransportCtxFactoryRaw::toCString() const {
118 return "raw";
119}
120
121std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryRaw::make() {
122 return std::unique_ptr<RpcTransportCtxFactoryRaw>(new RpcTransportCtxFactoryRaw());
123}
124
125} // namespace android