blob: f55c7796a0f63354ac27acc93f212088c4168514 [file] [log] [blame]
Victor Hsiehd35d31d2021-06-03 11:24:31 -07001/*
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
David Brazdilefb56832022-11-16 11:47:00 +000017#include <binder_rpc_unstable.hpp>
18
Victor Hsiehd35d31d2021-06-03 11:24:31 -070019#include <android-base/logging.h>
Inseob Kime12fccc2021-09-02 17:23:33 +090020#include <android-base/unique_fd.h>
Victor Hsiehd35d31d2021-06-03 11:24:31 -070021#include <android/binder_libbinder.h>
22#include <binder/RpcServer.h>
23#include <binder/RpcSession.h>
Alice Wang893a9912022-10-24 10:44:09 +000024#include <cutils/sockets.h>
Inseob Kim091050a2021-10-25 14:25:25 +000025#include <linux/vm_sockets.h>
Victor Hsiehd35d31d2021-06-03 11:24:31 -070026
Steven Moreland2372f9d2021-08-05 15:42:01 -070027using android::OK;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070028using android::RpcServer;
29using android::RpcSession;
David Brazdilefb56832022-11-16 11:47:00 +000030using android::sp;
Steven Moreland2372f9d2021-08-05 15:42:01 -070031using android::status_t;
32using android::statusToString;
Inseob Kime12fccc2021-09-02 17:23:33 +090033using android::base::unique_fd;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070034
David Brazdilefb56832022-11-16 11:47:00 +000035// Opaque handle for RpcServer.
36struct ARpcServer {};
Victor Hsiehd35d31d2021-06-03 11:24:31 -070037
David Brazdilefb56832022-11-16 11:47:00 +000038static sp<RpcServer> toRpcServer(ARpcServer* handle) {
39 auto ref = reinterpret_cast<RpcServer*>(handle);
40 return sp<RpcServer>::fromExisting(ref);
Alice Wang893a9912022-10-24 10:44:09 +000041}
42
David Brazdilefb56832022-11-16 11:47:00 +000043static ARpcServer* createRpcServerHandle(sp<RpcServer>& server) {
44 auto ref = server.get();
45 ref->incStrong(ref);
46 return reinterpret_cast<ARpcServer*>(ref);
47}
48
49static void freeRpcServerHandle(ARpcServer* handle) {
50 auto ref = reinterpret_cast<RpcServer*>(handle);
51 ref->decStrong(ref);
52}
53
54extern "C" {
55
Alice Wang7e935602022-10-17 08:06:16 +000056bool RunVsockRpcServerWithFactory(AIBinder* (*factory)(unsigned int cid, void* context),
57 void* factoryContext, unsigned int port) {
Inseob Kim091050a2021-10-25 14:25:25 +000058 auto server = RpcServer::make();
59 if (status_t status = server->setupVsockServer(port); status != OK) {
60 LOG(ERROR) << "Failed to set up vsock server with port " << port
61 << " error: " << statusToString(status).c_str();
62 return false;
63 }
Andrei Homescu86124ca2022-04-21 22:22:48 +000064 server->setPerSessionRootObject([=](const void* addr, size_t addrlen) {
Inseob Kim091050a2021-10-25 14:25:25 +000065 LOG_ALWAYS_FATAL_IF(addrlen < sizeof(sockaddr_vm), "sockaddr is truncated");
66 const sockaddr_vm* vaddr = reinterpret_cast<const sockaddr_vm*>(addr);
Andrei Homescu86124ca2022-04-21 22:22:48 +000067 LOG_ALWAYS_FATAL_IF(vaddr->svm_family != AF_VSOCK, "address is not a vsock");
Inseob Kim091050a2021-10-25 14:25:25 +000068 return AIBinder_toPlatformBinder(factory(vaddr->svm_cid, factoryContext));
69 });
70
71 server->join();
72
73 // Shutdown any open sessions since server failed.
74 (void)server->shutdown();
75 return true;
76}
77
David Brazdilefb56832022-11-16 11:47:00 +000078ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int port) {
Victor Hsiehd35d31d2021-06-03 11:24:31 -070079 auto server = RpcServer::make();
Steven Moreland2372f9d2021-08-05 15:42:01 -070080 if (status_t status = server->setupVsockServer(port); status != OK) {
81 LOG(ERROR) << "Failed to set up vsock server with port " << port
82 << " error: " << statusToString(status).c_str();
David Brazdilefb56832022-11-16 11:47:00 +000083 return nullptr;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070084 }
David Brazdilefb56832022-11-16 11:47:00 +000085 server->setRootObject(AIBinder_toPlatformBinder(service));
86 return createRpcServerHandle(server);
Victor Hsiehd35d31d2021-06-03 11:24:31 -070087}
88
David Brazdilefb56832022-11-16 11:47:00 +000089ARpcServer* ARpcServer_newInitUnixDomain(AIBinder* service, const char* name) {
90 auto server = RpcServer::make();
91 auto fd = unique_fd(android_get_control_socket(name));
92 if (!fd.ok()) {
93 LOG(ERROR) << "Failed to get fd for the socket:" << name;
94 return nullptr;
95 }
96 if (status_t status = server->setupRawSocketServer(std::move(fd)); status != OK) {
97 LOG(ERROR) << "Failed to set up Unix Domain RPC server with name " << name
98 << " error: " << statusToString(status).c_str();
99 return nullptr;
100 }
101 server->setRootObject(AIBinder_toPlatformBinder(service));
102 return createRpcServerHandle(server);
103}
104
105void ARpcServer_start(ARpcServer* handle) {
106 toRpcServer(handle)->start();
107}
108
109void ARpcServer_join(ARpcServer* handle) {
110 toRpcServer(handle)->join();
111}
112
113void ARpcServer_shutdown(ARpcServer* handle) {
114 toRpcServer(handle)->shutdown();
115}
116
117void ARpcServer_free(ARpcServer* handle) {
118 freeRpcServerHandle(handle);
Inseob Kim172473f2021-09-07 21:01:33 +0900119}
120
Alice Wang07466682022-10-19 15:28:29 +0000121AIBinder* VsockRpcClient(unsigned int cid, unsigned int port) {
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700122 auto session = RpcSession::make();
Steven Moreland2372f9d2021-08-05 15:42:01 -0700123 if (status_t status = session->setupVsockClient(cid, port); status != OK) {
124 LOG(ERROR) << "Failed to set up vsock client with CID " << cid << " and port " << port
125 << " error: " << statusToString(status).c_str();
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700126 return nullptr;
127 }
128 return AIBinder_fromPlatformBinder(session->getRootObject());
129}
Inseob Kime12fccc2021-09-02 17:23:33 +0900130
Alice Wang893a9912022-10-24 10:44:09 +0000131AIBinder* UnixDomainRpcClient(const char* name) {
132 std::string pathname(name);
133 pathname = ANDROID_SOCKET_DIR "/" + pathname;
134 auto session = RpcSession::make();
135 if (status_t status = session->setupUnixDomainClient(pathname.c_str()); status != OK) {
136 LOG(ERROR) << "Failed to set up Unix Domain RPC client with path: " << pathname
137 << " error: " << statusToString(status).c_str();
138 return nullptr;
139 }
140 return AIBinder_fromPlatformBinder(session->getRootObject());
141}
142
Inseob Kime12fccc2021-09-02 17:23:33 +0900143AIBinder* RpcPreconnectedClient(int (*requestFd)(void* param), void* param) {
144 auto session = RpcSession::make();
145 auto request = [=] { return unique_fd{requestFd(param)}; };
146 if (status_t status = session->setupPreconnectedClient(unique_fd{}, request); status != OK) {
147 LOG(ERROR) << "Failed to set up vsock client. error: " << statusToString(status).c_str();
148 return nullptr;
149 }
150 return AIBinder_fromPlatformBinder(session->getRootObject());
151}
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700152}