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