blob: cd067bfee76c30d6eee985397dd24b596cc4d5a6 [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
32namespace {
33
34// RpcTransport with TLS disabled.
35class RpcTransportRaw : public RpcTransport {
36public:
Pawan3e0061c2022-08-26 21:08:34 +000037 explicit RpcTransportRaw(android::RpcTransportFd socket) : mSocket(std::move(socket)) {}
Andrei Homescu1975aaa2022-03-19 02:34:57 +000038 status_t pollRead(void) override {
39 uint8_t buf;
40 ssize_t ret = TEMP_FAILURE_RETRY(
Pawan49d74cb2022-08-03 21:19:11 +000041 ::recv(mSocket.fd.get(), &buf, sizeof(buf), MSG_PEEK | MSG_DONTWAIT));
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070042 if (ret < 0) {
Andrei Homescu5ad71b52022-03-11 03:49:12 +000043 int savedErrno = errno;
44 if (savedErrno == EAGAIN || savedErrno == EWOULDBLOCK) {
45 return WOULD_BLOCK;
46 }
47
Andrei Homescu1975aaa2022-03-19 02:34:57 +000048 LOG_RPC_DETAIL("RpcTransport poll(): %s", strerror(savedErrno));
Andrei Homescu5ad71b52022-03-11 03:49:12 +000049 return -savedErrno;
Andrei Homescu1975aaa2022-03-19 02:34:57 +000050 } else if (ret == 0) {
51 return DEAD_OBJECT;
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070052 }
Andrei Homescu5ad71b52022-03-11 03:49:12 +000053
Andrei Homescu5ad71b52022-03-11 03:49:12 +000054 return OK;
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070055 }
Yifan Hong8c950422021-08-05 17:13:55 -070056
Devin Moore695368f2022-06-03 22:29:14 +000057 status_t interruptableWriteFully(
58 FdTrigger* fdTrigger, iovec* iovs, int niovs,
Frederick Mayle69a0c992022-05-26 20:38:39 +000059 const std::optional<android::base::function_ref<status_t()>>& altPoll,
60 const std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds)
61 override {
62 bool sentFds = false;
63 auto send = [&](iovec* iovs, int niovs) -> ssize_t {
David Brazdilc3964f62022-10-24 23:06:14 +010064 ssize_t ret =
65 sendMessageOnSocket(mSocket, iovs, niovs, sentFds ? nullptr : ancillaryFds);
David Brazdil21c887c2022-09-23 12:25:18 +010066 sentFds |= ret > 0;
67 return ret;
Frederick Mayle69a0c992022-05-26 20:38:39 +000068 };
Pawan49d74cb2022-08-03 21:19:11 +000069 return interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, send, "sendmsg", POLLOUT,
70 altPoll);
Steven Moreland301c3f02021-09-14 17:49:04 -070071 }
72
Devin Moore695368f2022-06-03 22:29:14 +000073 status_t interruptableReadFully(
74 FdTrigger* fdTrigger, iovec* iovs, int niovs,
Frederick Mayle69a0c992022-05-26 20:38:39 +000075 const std::optional<android::base::function_ref<status_t()>>& altPoll,
Frederick Mayleffe9ac22022-06-30 02:07:36 +000076 std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds) override {
Frederick Mayle69a0c992022-05-26 20:38:39 +000077 auto recv = [&](iovec* iovs, int niovs) -> ssize_t {
David Brazdil21c887c2022-09-23 12:25:18 +010078 return receiveMessageFromSocket(mSocket, iovs, niovs, ancillaryFds);
Frederick Mayle69a0c992022-05-26 20:38:39 +000079 };
Pawan49d74cb2022-08-03 21:19:11 +000080 return interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, recv, "recvmsg", POLLIN,
81 altPoll);
Frederick Mayle69a0c992022-05-26 20:38:39 +000082 }
83
Pawan49d74cb2022-08-03 21:19:11 +000084 virtual bool isWaiting() { return mSocket.isInPollingState(); }
85
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070086private:
Pawan3e0061c2022-08-26 21:08:34 +000087 android::RpcTransportFd mSocket;
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070088};
89
90// RpcTransportCtx with TLS disabled.
91class RpcTransportCtxRaw : public RpcTransportCtx {
92public:
Pawan3e0061c2022-08-26 21:08:34 +000093 std::unique_ptr<RpcTransport> newTransport(android::RpcTransportFd socket, FdTrigger*) const {
Pawan49d74cb2022-08-03 21:19:11 +000094 return std::make_unique<RpcTransportRaw>(std::move(socket));
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070095 }
Yifan Hong9734cfc2021-09-13 16:14:09 -070096 std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070097};
Yifan Hong588d59c2021-08-16 17:13:58 -070098
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070099} // namespace
100
101std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newServerCtx() const {
102 return std::make_unique<RpcTransportCtxRaw>();
103}
104
105std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newClientCtx() const {
106 return std::make_unique<RpcTransportCtxRaw>();
107}
108
109const char *RpcTransportCtxFactoryRaw::toCString() const {
110 return "raw";
111}
112
113std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryRaw::make() {
114 return std::unique_ptr<RpcTransportCtxFactoryRaw>(new RpcTransportCtxFactoryRaw());
115}
116
117} // namespace android