Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 1 | /* |
| 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 Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 20 | #include <poll.h> |
| 21 | |
Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 22 | #include <binder/RpcTransportRaw.h> |
| 23 | |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 24 | #include "FdTrigger.h" |
Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 25 | #include "RpcState.h" |
| 26 | |
| 27 | using android::base::ErrnoError; |
| 28 | using android::base::Result; |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | namespace { |
| 33 | |
| 34 | // RpcTransport with TLS disabled. |
| 35 | class RpcTransportRaw : public RpcTransport { |
| 36 | public: |
| 37 | explicit RpcTransportRaw(android::base::unique_fd socket) : mSocket(std::move(socket)) {} |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 38 | Result<size_t> send(const void* buf, size_t size) { |
Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 39 | ssize_t ret = TEMP_FAILURE_RETRY(::send(mSocket.get(), buf, size, MSG_NOSIGNAL)); |
| 40 | if (ret < 0) { |
| 41 | return ErrnoError() << "send()"; |
| 42 | } |
| 43 | return ret; |
| 44 | } |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 45 | Result<size_t> recv(void* buf, size_t size) { |
Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 46 | ssize_t ret = TEMP_FAILURE_RETRY(::recv(mSocket.get(), buf, size, MSG_NOSIGNAL)); |
| 47 | if (ret < 0) { |
| 48 | return ErrnoError() << "recv()"; |
| 49 | } |
| 50 | return ret; |
| 51 | } |
Yifan Hong | 218c407 | 2021-08-04 14:59:10 -0700 | [diff] [blame] | 52 | Result<size_t> peek(void *buf, size_t size) override { |
Yifan Hong | b675ffe | 2021-08-05 16:37:17 -0700 | [diff] [blame] | 53 | ssize_t ret = TEMP_FAILURE_RETRY(::recv(mSocket.get(), buf, size, MSG_PEEK)); |
Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 54 | if (ret < 0) { |
| 55 | return ErrnoError() << "recv(MSG_PEEK)"; |
| 56 | } |
| 57 | return ret; |
| 58 | } |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 59 | |
| 60 | status_t interruptableWriteFully(FdTrigger* fdTrigger, const void* data, size_t size) override { |
| 61 | const uint8_t* buffer = reinterpret_cast<const uint8_t*>(data); |
| 62 | const uint8_t* end = buffer + size; |
| 63 | |
| 64 | MAYBE_WAIT_IN_FLAKE_MODE; |
| 65 | |
| 66 | status_t status; |
| 67 | while ((status = fdTrigger->triggerablePoll(mSocket.get(), POLLOUT)) == OK) { |
| 68 | auto writeSize = this->send(buffer, end - buffer); |
| 69 | if (!writeSize.ok()) { |
| 70 | LOG_RPC_DETAIL("RpcTransport::send(): %s", writeSize.error().message().c_str()); |
| 71 | return writeSize.error().code() == 0 ? UNKNOWN_ERROR : -writeSize.error().code(); |
| 72 | } |
| 73 | |
| 74 | if (*writeSize == 0) return DEAD_OBJECT; |
| 75 | |
| 76 | buffer += *writeSize; |
| 77 | if (buffer == end) return OK; |
| 78 | } |
| 79 | return status; |
| 80 | } |
| 81 | |
| 82 | status_t interruptableReadFully(FdTrigger* fdTrigger, void* data, size_t size) override { |
| 83 | uint8_t* buffer = reinterpret_cast<uint8_t*>(data); |
| 84 | uint8_t* end = buffer + size; |
| 85 | |
| 86 | MAYBE_WAIT_IN_FLAKE_MODE; |
| 87 | |
| 88 | status_t status; |
| 89 | while ((status = fdTrigger->triggerablePoll(mSocket.get(), POLLIN)) == OK) { |
| 90 | auto readSize = this->recv(buffer, end - buffer); |
| 91 | if (!readSize.ok()) { |
| 92 | LOG_RPC_DETAIL("RpcTransport::recv(): %s", readSize.error().message().c_str()); |
| 93 | return readSize.error().code() == 0 ? UNKNOWN_ERROR : -readSize.error().code(); |
| 94 | } |
| 95 | |
| 96 | if (*readSize == 0) return DEAD_OBJECT; // EOF |
| 97 | |
| 98 | buffer += *readSize; |
| 99 | if (buffer == end) return OK; |
| 100 | } |
| 101 | return status; |
| 102 | } |
Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 103 | |
| 104 | private: |
| 105 | android::base::unique_fd mSocket; |
| 106 | }; |
| 107 | |
| 108 | // RpcTransportCtx with TLS disabled. |
| 109 | class RpcTransportCtxRaw : public RpcTransportCtx { |
| 110 | public: |
Yifan Hong | f6d4229 | 2021-08-05 23:43:05 -0700 | [diff] [blame] | 111 | std::unique_ptr<RpcTransport> newTransport(android::base::unique_fd fd, FdTrigger*) const { |
Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 112 | return std::make_unique<RpcTransportRaw>(std::move(fd)); |
| 113 | } |
Yifan Hong | 9734cfc | 2021-09-13 16:14:09 -0700 | [diff] [blame^] | 114 | std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; } |
Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 115 | }; |
Yifan Hong | 588d59c | 2021-08-16 17:13:58 -0700 | [diff] [blame] | 116 | |
Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 117 | } // namespace |
| 118 | |
| 119 | std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newServerCtx() const { |
| 120 | return std::make_unique<RpcTransportCtxRaw>(); |
| 121 | } |
| 122 | |
| 123 | std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newClientCtx() const { |
| 124 | return std::make_unique<RpcTransportCtxRaw>(); |
| 125 | } |
| 126 | |
| 127 | const char *RpcTransportCtxFactoryRaw::toCString() const { |
| 128 | return "raw"; |
| 129 | } |
| 130 | |
| 131 | std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryRaw::make() { |
| 132 | return std::unique_ptr<RpcTransportCtxFactoryRaw>(new RpcTransportCtxFactoryRaw()); |
| 133 | } |
| 134 | |
| 135 | } // namespace android |