• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #include <poll.h>
21 #include <stddef.h>
22 
23 #include <binder/RpcTransportRaw.h>
24 
25 #include "FdTrigger.h"
26 #include "OS.h"
27 #include "RpcState.h"
28 #include "RpcTransportUtils.h"
29 
30 namespace android {
31 
32 namespace {
33 
34 // RpcTransport with TLS disabled.
35 class RpcTransportRaw : public RpcTransport {
36 public:
RpcTransportRaw(android::RpcTransportFd socket)37     explicit RpcTransportRaw(android::RpcTransportFd socket) : mSocket(std::move(socket)) {}
pollRead(void)38     status_t pollRead(void) override {
39         uint8_t buf;
40         ssize_t ret = TEMP_FAILURE_RETRY(
41                 ::recv(mSocket.fd.get(), &buf, sizeof(buf), MSG_PEEK | MSG_DONTWAIT));
42         if (ret < 0) {
43             int savedErrno = errno;
44             if (savedErrno == EAGAIN || savedErrno == EWOULDBLOCK) {
45                 return WOULD_BLOCK;
46             }
47 
48             LOG_RPC_DETAIL("RpcTransport poll(): %s", strerror(savedErrno));
49             return -savedErrno;
50         } else if (ret == 0) {
51             return DEAD_OBJECT;
52         }
53 
54         return OK;
55     }
56 
interruptableWriteFully(FdTrigger * fdTrigger,iovec * iovs,int niovs,const std::optional<android::base::function_ref<status_t ()>> & altPoll,const std::vector<std::variant<base::unique_fd,base::borrowed_fd>> * ancillaryFds)57     status_t interruptableWriteFully(
58             FdTrigger* fdTrigger, iovec* iovs, int niovs,
59             const std::optional<android::base::function_ref<status_t()>>& altPoll,
60             const std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds)
61             override {
62         bool sentFds = false;
63         auto send = [&](iovec* iovs, int niovs) -> ssize_t {
64             ssize_t ret =
65                     sendMessageOnSocket(mSocket, iovs, niovs, sentFds ? nullptr : ancillaryFds);
66             sentFds |= ret > 0;
67             return ret;
68         };
69         return interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, send, "sendmsg", POLLOUT,
70                                         altPoll);
71     }
72 
interruptableReadFully(FdTrigger * fdTrigger,iovec * iovs,int niovs,const std::optional<android::base::function_ref<status_t ()>> & altPoll,std::vector<std::variant<base::unique_fd,base::borrowed_fd>> * ancillaryFds)73     status_t interruptableReadFully(
74             FdTrigger* fdTrigger, iovec* iovs, int niovs,
75             const std::optional<android::base::function_ref<status_t()>>& altPoll,
76             std::vector<std::variant<base::unique_fd, base::borrowed_fd>>* ancillaryFds) override {
77         auto recv = [&](iovec* iovs, int niovs) -> ssize_t {
78             return receiveMessageFromSocket(mSocket, iovs, niovs, ancillaryFds);
79         };
80         return interruptableReadOrWrite(mSocket, fdTrigger, iovs, niovs, recv, "recvmsg", POLLIN,
81                                         altPoll);
82     }
83 
isWaiting()84     virtual bool isWaiting() { return mSocket.isInPollingState(); }
85 
86 private:
87     android::RpcTransportFd mSocket;
88 };
89 
90 // RpcTransportCtx with TLS disabled.
91 class RpcTransportCtxRaw : public RpcTransportCtx {
92 public:
newTransport(android::RpcTransportFd socket,FdTrigger *) const93     std::unique_ptr<RpcTransport> newTransport(android::RpcTransportFd socket, FdTrigger*) const {
94         return std::make_unique<RpcTransportRaw>(std::move(socket));
95     }
getCertificate(RpcCertificateFormat) const96     std::vector<uint8_t> getCertificate(RpcCertificateFormat) const override { return {}; }
97 };
98 
99 } // namespace
100 
newServerCtx() const101 std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newServerCtx() const {
102     return std::make_unique<RpcTransportCtxRaw>();
103 }
104 
newClientCtx() const105 std::unique_ptr<RpcTransportCtx> RpcTransportCtxFactoryRaw::newClientCtx() const {
106     return std::make_unique<RpcTransportCtxRaw>();
107 }
108 
toCString() const109 const char *RpcTransportCtxFactoryRaw::toCString() const {
110     return "raw";
111 }
112 
make()113 std::unique_ptr<RpcTransportCtxFactory> RpcTransportCtxFactoryRaw::make() {
114     return std::unique_ptr<RpcTransportCtxFactoryRaw>(new RpcTransportCtxFactoryRaw());
115 }
116 
117 } // namespace android
118