blob: ddbcb573b1e6be6d7d9dab621cb2347545d5357e [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>
Frederick Mayle69a0c992022-05-26 20:38:39 +000021#include <stddef.h>
Yifan Hong8c950422021-08-05 17:13:55 -070022
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070023#include <binder/RpcTransportRaw.h>
24
Yifan Hong8c950422021-08-05 17:13:55 -070025#include "FdTrigger.h"
David Brazdil21c887c2022-09-23 12:25:18 +010026#include "OS.h"
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070027#include "RpcState.h"
Andrei Homescu9b404192022-07-21 00:55:10 +000028#include "RpcTransportUtils.h"
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070029
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070030namespace android {
31
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070032// RpcTransport with TLS disabled.
33class RpcTransportRaw : public RpcTransport {
34public:
Pawan3e0061c2022-08-26 21:08:34 +000035 explicit RpcTransportRaw(android::RpcTransportFd socket) : mSocket(std::move(socket)) {}
Andrei Homescu1975aaa2022-03-19 02:34:57 +000036 status_t pollRead(void) override {
37 uint8_t buf;
38 ssize_t ret = TEMP_FAILURE_RETRY(
Pawan49d74cb2022-08-03 21:19:11 +000039 ::recv(mSocket.fd.get(), &buf, sizeof(buf), MSG_PEEK | MSG_DONTWAIT));
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070040 if (ret < 0) {
Andrei Homescu5ad71b52022-03-11 03:49:12 +000041 int savedErrno = errno;
42 if (savedErrno == EAGAIN || savedErrno == EWOULDBLOCK) {
43 return WOULD_BLOCK;
44 }
45
Andrei Homescu1975aaa2022-03-19 02:34:57 +000046 LOG_RPC_DETAIL("RpcTransport poll(): %s", strerror(savedErrno));
Andrei Homescu5ad71b52022-03-11 03:49:12 +000047 return -savedErrno;
Andrei Homescu1975aaa2022-03-19 02:34:57 +000048 } else if (ret == 0) {
49 return DEAD_OBJECT;
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070050 }
Andrei Homescu5ad71b52022-03-11 03:49:12 +000051
Andrei Homescu5ad71b52022-03-11 03:49:12 +000052 return OK;
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070053 }
Yifan Hong8c950422021-08-05 17:13:55 -070054
Devin Moore695368f2022-06-03 22:29:14 +000055 status_t interruptableWriteFully(
56 FdTrigger* fdTrigger, iovec* iovs, int niovs,
Frederick Mayle69a0c992022-05-26 20:38:39 +000057 const std::optional<android::base::function_ref<status_t()>>& altPoll,
58 const std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds)
59 override {
60 bool sentFds = false;
61 auto send = [&](iovec* iovs, int niovs) -> ssize_t {
David Brazdilc3964f62022-10-24 23:06:14 +010062 ssize_t ret =
63 sendMessageOnSocket(mSocket, iovs, niovs, sentFds ? nullptr : ancillaryFds);
David Brazdil21c887c2022-09-23 12:25:18 +010064 sentFds |= ret > 0;
65 return ret;
Frederick Mayle69a0c992022-05-26 20:38:39 +000066 };
Pawan49d74cb2022-08-03 21:19:11 +000067 return interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, send, "sendmsg", POLLOUT,
68 altPoll);
Steven Moreland301c3f02021-09-14 17:49:04 -070069 }
70
Devin Moore695368f2022-06-03 22:29:14 +000071 status_t interruptableReadFully(
72 FdTrigger* fdTrigger, iovec* iovs, int niovs,
Frederick Mayle69a0c992022-05-26 20:38:39 +000073 const std::optional<android::base::function_ref<status_t()>>& altPoll,
Frederick Mayleffe9ac22022-06-30 02:07:36 +000074 std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds) override {
Frederick Mayle69a0c992022-05-26 20:38:39 +000075 auto recv = [&](iovec* iovs, int niovs) -> ssize_t {
David Brazdil21c887c2022-09-23 12:25:18 +010076 return receiveMessageFromSocket(mSocket, iovs, niovs, ancillaryFds);
Frederick Mayle69a0c992022-05-26 20:38:39 +000077 };
Pawan49d74cb2022-08-03 21:19:11 +000078 return interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, recv, "recvmsg", POLLIN,
79 altPoll);
Frederick Mayle69a0c992022-05-26 20:38:39 +000080 }
81
Hao Chen97649ae2023-05-31 14:12:33 -070082 bool isWaiting() override { return mSocket.isInPollingState(); }
Pawan49d74cb2022-08-03 21:19:11 +000083
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070084private:
Pawan3e0061c2022-08-26 21:08:34 +000085 android::RpcTransportFd mSocket;
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070086};
87
88// RpcTransportCtx with TLS disabled.
89class RpcTransportCtxRaw : public RpcTransportCtx {
90public:
Hao Chen97649ae2023-05-31 14:12:33 -070091 std::unique_ptr<RpcTransport> newTransport(android::RpcTransportFd socket,
92 FdTrigger*) const override {
Pawan49d74cb2022-08-03 21:19:11 +000093 return std::make_unique<RpcTransportRaw>(std::move(socket));
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070094 }
Yifan Hong9734cfc2021-09-13 16:14:09 -070095 std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070096};
Yifan Hong588d59c2021-08-16 17:13:58 -070097
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070098std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newServerCtx() const {
99 return std::make_unique<RpcTransportCtxRaw>();
100}
101
102std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newClientCtx() const {
103 return std::make_unique<RpcTransportCtxRaw>();
104}
105
106const char *RpcTransportCtxFactoryRaw::toCString() const {
107 return "raw";
108}
109
110std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryRaw::make() {
111 return std::unique_ptr<RpcTransportCtxFactoryRaw>(new RpcTransportCtxFactoryRaw());
112}
113
114} // namespace android