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