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 | |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 46 | template <typename SendOrReceive> |
Colin Cross | 9adfeaf | 2022-01-21 17:22:09 -0800 | [diff] [blame^] | 47 | status_t interruptableReadOrWrite(FdTrigger* fdTrigger, iovec* iovs, int niovs, |
Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 48 | SendOrReceive sendOrReceiveFun, const char* funName, |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 49 | int16_t event, const std::function<status_t()>& altPoll) { |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 50 | MAYBE_WAIT_IN_FLAKE_MODE; |
| 51 | |
Colin Cross | 9adfeaf | 2022-01-21 17:22:09 -0800 | [diff] [blame^] | 52 | if (niovs < 0) { |
| 53 | return BAD_VALUE; |
| 54 | } |
| 55 | |
Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 56 | // Since we didn't poll, we need to manually check to see if it was triggered. Otherwise, we |
| 57 | // may never know we should be shutting down. |
| 58 | if (fdTrigger->isTriggered()) { |
| 59 | return DEAD_OBJECT; |
| 60 | } |
| 61 | |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 62 | // If iovs has one or more empty vectors at the end and |
| 63 | // we somehow advance past all the preceding vectors and |
| 64 | // pass some or all of the empty ones to sendmsg/recvmsg, |
| 65 | // the call will return processSize == 0. In that case |
| 66 | // we should be returning OK but instead return DEAD_OBJECT. |
| 67 | // To avoid this problem, we make sure here that the last |
| 68 | // vector at iovs[niovs - 1] has a non-zero length. |
| 69 | while (niovs > 0 && iovs[niovs - 1].iov_len == 0) { |
| 70 | niovs--; |
| 71 | } |
| 72 | if (niovs == 0) { |
| 73 | // The vectors are all empty, so we have nothing to send. |
| 74 | return OK; |
| 75 | } |
| 76 | |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 77 | bool havePolled = false; |
| 78 | while (true) { |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 79 | msghdr msg{ |
| 80 | .msg_iov = iovs, |
Colin Cross | 9adfeaf | 2022-01-21 17:22:09 -0800 | [diff] [blame^] | 81 | // posix uses int, glibc uses size_t. niovs is a |
| 82 | // non-negative int and can be cast to either. |
| 83 | .msg_iovlen = static_cast<decltype(msg.msg_iovlen)>(niovs), |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 84 | }; |
| 85 | ssize_t processSize = |
| 86 | TEMP_FAILURE_RETRY(sendOrReceiveFun(mSocket.get(), &msg, MSG_NOSIGNAL)); |
Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 87 | |
| 88 | if (processSize < 0) { |
Steven Moreland | 5252e75 | 2021-09-14 14:06:03 -0700 | [diff] [blame] | 89 | int savedErrno = errno; |
Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 90 | |
| 91 | // Still return the error on later passes, since it would expose |
| 92 | // a problem with polling |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 93 | if (havePolled || (savedErrno != EAGAIN && savedErrno != EWOULDBLOCK)) { |
Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 94 | LOG_RPC_DETAIL("RpcTransport %s(): %s", funName, strerror(savedErrno)); |
| 95 | return -savedErrno; |
| 96 | } |
| 97 | } else if (processSize == 0) { |
| 98 | return DEAD_OBJECT; |
| 99 | } else { |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 100 | while (processSize > 0 && niovs > 0) { |
| 101 | auto& iov = iovs[0]; |
| 102 | if (static_cast<size_t>(processSize) < iov.iov_len) { |
| 103 | // Advance the base of the current iovec |
| 104 | iov.iov_base = reinterpret_cast<char*>(iov.iov_base) + processSize; |
| 105 | iov.iov_len -= processSize; |
| 106 | break; |
| 107 | } |
| 108 | |
| 109 | // The current iovec was fully written |
| 110 | processSize -= iov.iov_len; |
| 111 | iovs++; |
| 112 | niovs--; |
| 113 | } |
| 114 | if (niovs == 0) { |
| 115 | LOG_ALWAYS_FATAL_IF(processSize > 0, |
| 116 | "Reached the end of iovecs " |
| 117 | "with %zd bytes remaining", |
| 118 | processSize); |
Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 119 | return OK; |
| 120 | } |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 123 | if (altPoll) { |
| 124 | if (status_t status = altPoll(); status != OK) return status; |
| 125 | if (fdTrigger->isTriggered()) { |
| 126 | return DEAD_OBJECT; |
| 127 | } |
| 128 | } else { |
| 129 | if (status_t status = fdTrigger->triggerablePoll(mSocket.get(), event); |
| 130 | status != OK) |
| 131 | return status; |
| 132 | if (!havePolled) havePolled = true; |
| 133 | } |
| 134 | } |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Colin Cross | 9adfeaf | 2022-01-21 17:22:09 -0800 | [diff] [blame^] | 137 | status_t interruptableWriteFully(FdTrigger* fdTrigger, iovec* iovs, int niovs, |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 138 | const std::function<status_t()>& altPoll) override { |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 139 | return interruptableReadOrWrite(fdTrigger, iovs, niovs, sendmsg, "sendmsg", POLLOUT, |
| 140 | altPoll); |
Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Colin Cross | 9adfeaf | 2022-01-21 17:22:09 -0800 | [diff] [blame^] | 143 | status_t interruptableReadFully(FdTrigger* fdTrigger, iovec* iovs, int niovs, |
Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 144 | const std::function<status_t()>& altPoll) override { |
Andrei Homescu | a39e4ed | 2021-12-10 08:41:54 +0000 | [diff] [blame] | 145 | return interruptableReadOrWrite(fdTrigger, iovs, niovs, recvmsg, "recvmsg", POLLIN, |
| 146 | altPoll); |
Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 147 | } |
Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 148 | |
| 149 | private: |
Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 150 | base::unique_fd mSocket; |
Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | // RpcTransportCtx with TLS disabled. |
| 154 | class RpcTransportCtxRaw : public RpcTransportCtx { |
| 155 | public: |
Yifan Hong | f6d4229 | 2021-08-05 23:43:05 -0700 | [diff] [blame] | 156 | std::unique_ptr<RpcTransport> newTransport(android::base::unique_fd fd, FdTrigger*) const { |
Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 157 | return std::make_unique<RpcTransportRaw>(std::move(fd)); |
| 158 | } |
Yifan Hong | 9734cfc | 2021-09-13 16:14:09 -0700 | [diff] [blame] | 159 | std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; } |
Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 160 | }; |
Yifan Hong | 588d59c | 2021-08-16 17:13:58 -0700 | [diff] [blame] | 161 | |
Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 162 | } // namespace |
| 163 | |
| 164 | std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newServerCtx() const { |
| 165 | return std::make_unique<RpcTransportCtxRaw>(); |
| 166 | } |
| 167 | |
| 168 | std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newClientCtx() const { |
| 169 | return std::make_unique<RpcTransportCtxRaw>(); |
| 170 | } |
| 171 | |
| 172 | const char *RpcTransportCtxFactoryRaw::toCString() const { |
| 173 | return "raw"; |
| 174 | } |
| 175 | |
| 176 | std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryRaw::make() { |
| 177 | return std::unique_ptr<RpcTransportCtxFactoryRaw>(new RpcTransportCtxFactoryRaw()); |
| 178 | } |
| 179 | |
| 180 | } // namespace android |