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