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