blob: ffa315191d6ec154c82b71b9eab395222bee1e9c [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
Tomasz Wasilczyk35804862023-10-30 14:19:19 +000032using namespace android::binder::impl;
33
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070034// 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,
Tomasz Wasilczyk35804862023-10-30 14:19:19 +000059 const std::optional<SmallFunction<status_t()>>& altPoll,
Frederick Mayle69a0c992022-05-26 20:38:39 +000060 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 {
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +000064 ssize_t ret = binder::os::sendMessageOnSocket(mSocket, iovs, niovs,
65 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,
Tomasz Wasilczyk35804862023-10-30 14:19:19 +000075 const std::optional<SmallFunction<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 {
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +000078 return binder::os::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
Hao Chen97649ae2023-05-31 14:12:33 -070084 bool isWaiting() override { return mSocket.isInPollingState(); }
Pawan49d74cb2022-08-03 21:19:11 +000085
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:
Hao Chen97649ae2023-05-31 14:12:33 -070093 std::unique_ptr<RpcTransport> newTransport(android::RpcTransportFd socket,
94 FdTrigger*) const override {
Pawan49d74cb2022-08-03 21:19:11 +000095 return std::make_unique<RpcTransportRaw>(std::move(socket));
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070096 }
Yifan Hong9734cfc2021-09-13 16:14:09 -070097 std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070098};
Yifan Hong588d59c2021-08-16 17:13:58 -070099
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700100std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newServerCtx() const {
101 return std::make_unique<RpcTransportCtxRaw>();
102}
103
104std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newClientCtx() const {
105 return std::make_unique<RpcTransportCtxRaw>();
106}
107
108const char *RpcTransportCtxFactoryRaw::toCString() const {
109 return "raw";
110}
111
112std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryRaw::make() {
113 return std::unique_ptr<RpcTransportCtxFactoryRaw>(new RpcTransportCtxFactoryRaw());
114}
115
116} // namespace android