blob: aa3a6e506d7014f04e0f32b91918d3a18e24fc5f [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>
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070022#include <sys/socket.h>
Yifan Hong8c950422021-08-05 17:13:55 -070023
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070024#include <binder/RpcTransportRaw.h>
25
Yifan Hong8c950422021-08-05 17:13:55 -070026#include "FdTrigger.h"
David Brazdil21c887c2022-09-23 12:25:18 +010027#include "OS.h"
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070028#include "RpcState.h"
Andrei Homescu9b404192022-07-21 00:55:10 +000029#include "RpcTransportUtils.h"
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070030
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070031namespace android {
32
Tomasz Wasilczyk35804862023-10-30 14:19:19 +000033using namespace android::binder::impl;
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070034using android::binder::borrowed_fd;
35using android::binder::unique_fd;
Tomasz Wasilczyk35804862023-10-30 14:19:19 +000036
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070037// RpcTransport with TLS disabled.
38class RpcTransportRaw : public RpcTransport {
39public:
Pawan3e0061c2022-08-26 21:08:34 +000040 explicit RpcTransportRaw(android::RpcTransportFd socket) : mSocket(std::move(socket)) {}
Andrei Homescu1975aaa2022-03-19 02:34:57 +000041 status_t pollRead(void) override {
42 uint8_t buf;
43 ssize_t ret = TEMP_FAILURE_RETRY(
Pawan49d74cb2022-08-03 21:19:11 +000044 ::recv(mSocket.fd.get(), &buf, sizeof(buf), MSG_PEEK | MSG_DONTWAIT));
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070045 if (ret < 0) {
Andrei Homescu5ad71b52022-03-11 03:49:12 +000046 int savedErrno = errno;
47 if (savedErrno == EAGAIN || savedErrno == EWOULDBLOCK) {
48 return WOULD_BLOCK;
49 }
50
Andrei Homescu1975aaa2022-03-19 02:34:57 +000051 LOG_RPC_DETAIL("RpcTransport poll(): %s", strerror(savedErrno));
Andrei Homescu5ad71b52022-03-11 03:49:12 +000052 return -savedErrno;
Andrei Homescu1975aaa2022-03-19 02:34:57 +000053 } else if (ret == 0) {
54 return DEAD_OBJECT;
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070055 }
Andrei Homescu5ad71b52022-03-11 03:49:12 +000056
Andrei Homescu5ad71b52022-03-11 03:49:12 +000057 return OK;
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070058 }
Yifan Hong8c950422021-08-05 17:13:55 -070059
Devin Moore695368f2022-06-03 22:29:14 +000060 status_t interruptableWriteFully(
61 FdTrigger* fdTrigger, iovec* iovs, int niovs,
Tomasz Wasilczyk35804862023-10-30 14:19:19 +000062 const std::optional<SmallFunction<status_t()>>& altPoll,
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070063 const std::vector<std::variant<unique_fd, borrowed_fd>>* ancillaryFds) override {
Frederick Mayle69a0c992022-05-26 20:38:39 +000064 bool sentFds = false;
65 auto send = [&](iovec* iovs, int niovs) -> ssize_t {
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +000066 ssize_t ret = binder::os::sendMessageOnSocket(mSocket, iovs, niovs,
67 sentFds ? nullptr : ancillaryFds);
David Brazdil21c887c2022-09-23 12:25:18 +010068 sentFds |= ret > 0;
69 return ret;
Frederick Mayle69a0c992022-05-26 20:38:39 +000070 };
Pawan49d74cb2022-08-03 21:19:11 +000071 return interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, send, "sendmsg", POLLOUT,
72 altPoll);
Steven Moreland301c3f02021-09-14 17:49:04 -070073 }
74
Devin Moore695368f2022-06-03 22:29:14 +000075 status_t interruptableReadFully(
76 FdTrigger* fdTrigger, iovec* iovs, int niovs,
Tomasz Wasilczyk35804862023-10-30 14:19:19 +000077 const std::optional<SmallFunction<status_t()>>& altPoll,
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070078 std::vector<std::variant<unique_fd, borrowed_fd>>* ancillaryFds) override {
Frederick Mayle69a0c992022-05-26 20:38:39 +000079 auto recv = [&](iovec* iovs, int niovs) -> ssize_t {
Tomasz Wasilczyk0d9dec22023-10-06 20:28:49 +000080 return binder::os::receiveMessageFromSocket(mSocket, iovs, niovs, ancillaryFds);
Frederick Mayle69a0c992022-05-26 20:38:39 +000081 };
Pawan49d74cb2022-08-03 21:19:11 +000082 return interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, recv, "recvmsg", POLLIN,
83 altPoll);
Frederick Mayle69a0c992022-05-26 20:38:39 +000084 }
85
Hao Chen97649ae2023-05-31 14:12:33 -070086 bool isWaiting() override { return mSocket.isInPollingState(); }
Pawan49d74cb2022-08-03 21:19:11 +000087
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070088private:
Pawan3e0061c2022-08-26 21:08:34 +000089 android::RpcTransportFd mSocket;
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070090};
91
92// RpcTransportCtx with TLS disabled.
93class RpcTransportCtxRaw : public RpcTransportCtx {
94public:
Hao Chen97649ae2023-05-31 14:12:33 -070095 std::unique_ptr<RpcTransport> newTransport(android::RpcTransportFd socket,
96 FdTrigger*) const override {
Pawan49d74cb2022-08-03 21:19:11 +000097 return std::make_unique<RpcTransportRaw>(std::move(socket));
Yifan Hongf6b4d5c2021-06-23 19:12:32 -070098 }
Yifan Hong9734cfc2021-09-13 16:14:09 -070099 std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700100};
Yifan Hong588d59c2021-08-16 17:13:58 -0700101
Yifan Hongf6b4d5c2021-06-23 19:12:32 -0700102std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newServerCtx() const {
103 return std::make_unique<RpcTransportCtxRaw>();
104}
105
106std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newClientCtx() const {
107 return std::make_unique<RpcTransportCtxRaw>();
108}
109
110const char *RpcTransportCtxFactoryRaw::toCString() const {
111 return "raw";
112}
113
114std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryRaw::make() {
115 return std::unique_ptr<RpcTransportCtxFactoryRaw>(new RpcTransportCtxFactoryRaw());
116}
117
118} // namespace android