| 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 |  | 
| Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 46 | template <typename Buffer, typename SendOrReceive> | 
|  | 47 | status_t interruptableReadOrWrite(FdTrigger* fdTrigger, Buffer buffer, size_t size, | 
|  | 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) { | 
| Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 50 | const Buffer end = buffer + size; | 
| Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 51 |  | 
|  | 52 | MAYBE_WAIT_IN_FLAKE_MODE; | 
|  | 53 |  | 
| Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 54 | // Since we didn't poll, we need to manually check to see if it was triggered. Otherwise, we | 
|  | 55 | // may never know we should be shutting down. | 
|  | 56 | if (fdTrigger->isTriggered()) { | 
|  | 57 | return DEAD_OBJECT; | 
|  | 58 | } | 
|  | 59 |  | 
| Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 60 | bool havePolled = false; | 
|  | 61 | while (true) { | 
| Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 62 | ssize_t processSize = TEMP_FAILURE_RETRY( | 
|  | 63 | sendOrReceiveFun(mSocket.get(), buffer, end - buffer, MSG_NOSIGNAL)); | 
|  | 64 |  | 
|  | 65 | if (processSize < 0) { | 
| Steven Moreland | 5252e75 | 2021-09-14 14:06:03 -0700 | [diff] [blame] | 66 | int savedErrno = errno; | 
| Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 67 |  | 
|  | 68 | // Still return the error on later passes, since it would expose | 
|  | 69 | // a problem with polling | 
| Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 70 | if (havePolled || | 
|  | 71 | (!havePolled && savedErrno != EAGAIN && savedErrno != EWOULDBLOCK)) { | 
| Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 72 | LOG_RPC_DETAIL("RpcTransport %s(): %s", funName, strerror(savedErrno)); | 
|  | 73 | return -savedErrno; | 
|  | 74 | } | 
|  | 75 | } else if (processSize == 0) { | 
|  | 76 | return DEAD_OBJECT; | 
|  | 77 | } else { | 
|  | 78 | buffer += processSize; | 
|  | 79 | if (buffer == end) { | 
|  | 80 | return OK; | 
|  | 81 | } | 
| Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
| Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 84 | if (altPoll) { | 
|  | 85 | if (status_t status = altPoll(); status != OK) return status; | 
|  | 86 | if (fdTrigger->isTriggered()) { | 
|  | 87 | return DEAD_OBJECT; | 
|  | 88 | } | 
|  | 89 | } else { | 
|  | 90 | if (status_t status = fdTrigger->triggerablePoll(mSocket.get(), event); | 
|  | 91 | status != OK) | 
|  | 92 | return status; | 
|  | 93 | if (!havePolled) havePolled = true; | 
|  | 94 | } | 
|  | 95 | } | 
| Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 96 | } | 
|  | 97 |  | 
| Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 98 | status_t interruptableWriteFully(FdTrigger* fdTrigger, const void* data, size_t size, | 
|  | 99 | const std::function<status_t()>& altPoll) override { | 
| Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 100 | return interruptableReadOrWrite(fdTrigger, reinterpret_cast<const uint8_t*>(data), size, | 
| Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 101 | send, "send", POLLOUT, altPoll); | 
| Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 102 | } | 
|  | 103 |  | 
| Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 104 | status_t interruptableReadFully(FdTrigger* fdTrigger, void* data, size_t size, | 
|  | 105 | const std::function<status_t()>& altPoll) override { | 
| Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 106 | return interruptableReadOrWrite(fdTrigger, reinterpret_cast<uint8_t*>(data), size, recv, | 
| Steven Moreland | 43921d5 | 2021-09-27 17:15:56 -0700 | [diff] [blame] | 107 | "recv", POLLIN, altPoll); | 
| Yifan Hong | 8c95042 | 2021-08-05 17:13:55 -0700 | [diff] [blame] | 108 | } | 
| Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 109 |  | 
|  | 110 | private: | 
| Steven Moreland | 301c3f0 | 2021-09-14 17:49:04 -0700 | [diff] [blame] | 111 | base::unique_fd mSocket; | 
| Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 112 | }; | 
|  | 113 |  | 
|  | 114 | // RpcTransportCtx with TLS disabled. | 
|  | 115 | class RpcTransportCtxRaw : public RpcTransportCtx { | 
|  | 116 | public: | 
| Yifan Hong | f6d4229 | 2021-08-05 23:43:05 -0700 | [diff] [blame] | 117 | std::unique_ptr<RpcTransport> newTransport(android::base::unique_fd fd, FdTrigger*) const { | 
| Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 118 | return std::make_unique<RpcTransportRaw>(std::move(fd)); | 
|  | 119 | } | 
| Yifan Hong | 9734cfc | 2021-09-13 16:14:09 -0700 | [diff] [blame] | 120 | std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; } | 
| Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 121 | }; | 
| Yifan Hong | 588d59c | 2021-08-16 17:13:58 -0700 | [diff] [blame] | 122 |  | 
| Yifan Hong | f6b4d5c | 2021-06-23 19:12:32 -0700 | [diff] [blame] | 123 | } // namespace | 
|  | 124 |  | 
|  | 125 | std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newServerCtx() const { | 
|  | 126 | return std::make_unique<RpcTransportCtxRaw>(); | 
|  | 127 | } | 
|  | 128 |  | 
|  | 129 | std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newClientCtx() const { | 
|  | 130 | return std::make_unique<RpcTransportCtxRaw>(); | 
|  | 131 | } | 
|  | 132 |  | 
|  | 133 | const char *RpcTransportCtxFactoryRaw::toCString() const { | 
|  | 134 | return "raw"; | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryRaw::make() { | 
|  | 138 | return std::unique_ptr<RpcTransportCtxFactoryRaw>(new RpcTransportCtxFactoryRaw()); | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | } // namespace android |