blob: a84a0c6e0b18d5f65dab68b74252bd7518ca7b0e [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/binder_libbinder.h>
20#include <binder/RpcServer.h>
21#include <binder/RpcSession.h>
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070022#include <binder/unique_fd.h>
Andrei Homescu029c1762023-05-11 03:27:34 +000023
24#ifndef __TRUSTY__
Alice Wang893a9912022-10-24 10:44:09 +000025#include <cutils/sockets.h>
Andrei Homescu029c1762023-05-11 03:27:34 +000026#endif
27
28#ifdef __linux__
Inseob Kim091050a2021-10-25 14:25:25 +000029#include <linux/vm_sockets.h>
Andrei Homescu029c1762023-05-11 03:27:34 +000030#endif // __linux__
Victor Hsiehd35d31d2021-06-03 11:24:31 -070031
Steven Moreland2372f9d2021-08-05 15:42:01 -070032using android::OK;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070033using android::RpcServer;
34using android::RpcSession;
David Brazdilefb56832022-11-16 11:47:00 +000035using android::sp;
Steven Moreland2372f9d2021-08-05 15:42:01 -070036using android::status_t;
37using android::statusToString;
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070038using android::binder::unique_fd;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070039
David Brazdilefb56832022-11-16 11:47:00 +000040// Opaque handle for RpcServer.
41struct ARpcServer {};
Victor Hsiehd35d31d2021-06-03 11:24:31 -070042
David Brazdilae3ec6a2022-12-14 13:13:32 +000043// Opaque handle for RpcSession.
44struct ARpcSession {};
Alice Wang893a9912022-10-24 10:44:09 +000045
David Brazdilae3ec6a2022-12-14 13:13:32 +000046template <typename A, typename T>
47static A* createObjectHandle(sp<T>& server) {
David Brazdilefb56832022-11-16 11:47:00 +000048 auto ref = server.get();
49 ref->incStrong(ref);
David Brazdilae3ec6a2022-12-14 13:13:32 +000050 return reinterpret_cast<A*>(ref);
David Brazdilefb56832022-11-16 11:47:00 +000051}
52
David Brazdilae3ec6a2022-12-14 13:13:32 +000053template <typename T, typename A>
54static void freeObjectHandle(A* handle) {
55 LOG_ALWAYS_FATAL_IF(handle == nullptr, "Handle cannot be null");
56 auto ref = reinterpret_cast<T*>(handle);
David Brazdilefb56832022-11-16 11:47:00 +000057 ref->decStrong(ref);
58}
59
David Brazdilae3ec6a2022-12-14 13:13:32 +000060template <typename T, typename A>
61static sp<T> handleToStrongPointer(A* handle) {
62 LOG_ALWAYS_FATAL_IF(handle == nullptr, "Handle cannot be null");
63 auto ref = reinterpret_cast<T*>(handle);
64 return sp<T>::fromExisting(ref);
65}
66
67RpcSession::FileDescriptorTransportMode toTransportMode(
68 ARpcSession_FileDescriptorTransportMode mode) {
69 switch (mode) {
70 case ARpcSession_FileDescriptorTransportMode::None:
71 return RpcSession::FileDescriptorTransportMode::NONE;
72 case ARpcSession_FileDescriptorTransportMode::Unix:
73 return RpcSession::FileDescriptorTransportMode::UNIX;
74 case ARpcSession_FileDescriptorTransportMode::Trusty:
75 return RpcSession::FileDescriptorTransportMode::TRUSTY;
76 default:
77 return RpcSession::FileDescriptorTransportMode::NONE;
78 }
79}
80
David Brazdilefb56832022-11-16 11:47:00 +000081extern "C" {
82
Andrei Homescu029c1762023-05-11 03:27:34 +000083#ifndef __TRUSTY__
Devin Moore398f9f52024-09-16 22:49:15 +000084ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, unsigned int port,
85 unsigned int* assignedPort) {
Victor Hsiehd35d31d2021-06-03 11:24:31 -070086 auto server = RpcServer::make();
David Brazdila47dfda2022-11-22 22:52:19 +000087
88 unsigned int bindCid = VMADDR_CID_ANY; // bind to the remote interface
89 if (cid == VMADDR_CID_LOCAL) {
90 bindCid = VMADDR_CID_LOCAL; // bind to the local interface
91 cid = VMADDR_CID_ANY; // no need for a connection filter
92 }
93
Devin Moore398f9f52024-09-16 22:49:15 +000094 if (status_t status = server->setupVsockServer(bindCid, port, assignedPort); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070095 ALOGE("Failed to set up vsock server with port %u error: %s", port,
96 statusToString(status).c_str());
David Brazdilefb56832022-11-16 11:47:00 +000097 return nullptr;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070098 }
David Brazdila47dfda2022-11-22 22:52:19 +000099 if (cid != VMADDR_CID_ANY) {
100 server->setConnectionFilter([=](const void* addr, size_t addrlen) {
David Brazdilb96737d2022-12-14 14:03:42 +0000101 LOG_ALWAYS_FATAL_IF(addrlen < sizeof(sockaddr_vm), "sockaddr is truncated");
102 const sockaddr_vm* vaddr = reinterpret_cast<const sockaddr_vm*>(addr);
103 LOG_ALWAYS_FATAL_IF(vaddr->svm_family != AF_VSOCK, "address is not a vsock");
104 if (cid != vaddr->svm_cid) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700105 ALOGE("Rejected vsock connection from CID %u", vaddr->svm_cid);
David Brazdila47dfda2022-11-22 22:52:19 +0000106 return false;
107 }
108 return true;
109 });
110 }
David Brazdilefb56832022-11-16 11:47:00 +0000111 server->setRootObject(AIBinder_toPlatformBinder(service));
David Brazdilae3ec6a2022-12-14 13:13:32 +0000112 return createObjectHandle<ARpcServer>(server);
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700113}
114
Alice Wang072c3982023-05-25 09:45:13 +0000115ARpcServer* ARpcServer_newBoundSocket(AIBinder* service, int socketFd) {
David Brazdilefb56832022-11-16 11:47:00 +0000116 auto server = RpcServer::make();
Alice Wang072c3982023-05-25 09:45:13 +0000117 auto fd = unique_fd(socketFd);
David Brazdilefb56832022-11-16 11:47:00 +0000118 if (!fd.ok()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700119 ALOGE("Invalid socket fd %d", socketFd);
David Brazdilefb56832022-11-16 11:47:00 +0000120 return nullptr;
121 }
122 if (status_t status = server->setupRawSocketServer(std::move(fd)); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700123 ALOGE("Failed to set up RPC server with fd %d error: %s", socketFd,
124 statusToString(status).c_str());
David Brazdilefb56832022-11-16 11:47:00 +0000125 return nullptr;
126 }
127 server->setRootObject(AIBinder_toPlatformBinder(service));
David Brazdilae3ec6a2022-12-14 13:13:32 +0000128 return createObjectHandle<ARpcServer>(server);
129}
130
David Brazdile1bd9832022-12-14 20:22:09 +0000131ARpcServer* ARpcServer_newUnixDomainBootstrap(AIBinder* service, int bootstrapFd) {
132 auto server = RpcServer::make();
133 auto fd = unique_fd(bootstrapFd);
134 if (!fd.ok()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700135 ALOGE("Invalid bootstrap fd %d", bootstrapFd);
David Brazdile1bd9832022-12-14 20:22:09 +0000136 return nullptr;
137 }
138 if (status_t status = server->setupUnixDomainSocketBootstrapServer(std::move(fd));
139 status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700140 ALOGE("Failed to set up Unix Domain RPC server with bootstrap fd %d error: %s", bootstrapFd,
141 statusToString(status).c_str());
David Brazdile1bd9832022-12-14 20:22:09 +0000142 return nullptr;
143 }
144 server->setRootObject(AIBinder_toPlatformBinder(service));
145 return createObjectHandle<ARpcServer>(server);
146}
147
Yiming Jing34637062023-02-05 14:05:32 -0800148ARpcServer* ARpcServer_newInet(AIBinder* service, const char* address, unsigned int port) {
149 auto server = RpcServer::make();
150 if (status_t status = server->setupInetServer(address, port, nullptr); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700151 ALOGE("Failed to set up inet RPC server with address %s and port %u error: %s", address,
152 port, statusToString(status).c_str());
Yiming Jing34637062023-02-05 14:05:32 -0800153 return nullptr;
154 }
155 server->setRootObject(AIBinder_toPlatformBinder(service));
156 return createObjectHandle<ARpcServer>(server);
157}
Andrei Homescu029c1762023-05-11 03:27:34 +0000158#endif // __TRUSTY__
Yiming Jing34637062023-02-05 14:05:32 -0800159
David Brazdilae3ec6a2022-12-14 13:13:32 +0000160void ARpcServer_setSupportedFileDescriptorTransportModes(
161 ARpcServer* handle, const ARpcSession_FileDescriptorTransportMode modes[],
162 size_t modes_len) {
163 auto server = handleToStrongPointer<RpcServer>(handle);
164 std::vector<RpcSession::FileDescriptorTransportMode> modevec;
165 for (size_t i = 0; i < modes_len; i++) {
166 modevec.push_back(toTransportMode(modes[i]));
167 }
168 server->setSupportedFileDescriptorTransportModes(modevec);
David Brazdilefb56832022-11-16 11:47:00 +0000169}
170
Ashutosh Agarwal55d76342024-04-17 21:16:06 +0000171void ARpcServer_setMaxThreads(ARpcServer* handle, size_t threads) {
172 handleToStrongPointer<RpcServer>(handle)->setMaxThreads(threads);
173}
174
David Brazdilefb56832022-11-16 11:47:00 +0000175void ARpcServer_start(ARpcServer* handle) {
David Brazdilae3ec6a2022-12-14 13:13:32 +0000176 handleToStrongPointer<RpcServer>(handle)->start();
David Brazdilefb56832022-11-16 11:47:00 +0000177}
178
179void ARpcServer_join(ARpcServer* handle) {
David Brazdilae3ec6a2022-12-14 13:13:32 +0000180 handleToStrongPointer<RpcServer>(handle)->join();
David Brazdilefb56832022-11-16 11:47:00 +0000181}
182
David Brazdil793e8792022-12-19 23:16:30 +0000183bool ARpcServer_shutdown(ARpcServer* handle) {
184 return handleToStrongPointer<RpcServer>(handle)->shutdown();
David Brazdilefb56832022-11-16 11:47:00 +0000185}
186
187void ARpcServer_free(ARpcServer* handle) {
David Brazdil4766a1f2022-12-19 21:58:25 +0000188 // Ignore the result of ARpcServer_shutdown - either it had been called
189 // earlier, or the RpcServer destructor will panic.
190 (void)ARpcServer_shutdown(handle);
David Brazdilae3ec6a2022-12-14 13:13:32 +0000191 freeObjectHandle<RpcServer>(handle);
Inseob Kim172473f2021-09-07 21:01:33 +0900192}
193
David Brazdilae3ec6a2022-12-14 13:13:32 +0000194ARpcSession* ARpcSession_new() {
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700195 auto session = RpcSession::make();
David Brazdilae3ec6a2022-12-14 13:13:32 +0000196 return createObjectHandle<ARpcSession>(session);
197}
198
199void ARpcSession_free(ARpcSession* handle) {
200 freeObjectHandle<RpcSession>(handle);
201}
202
Andrei Homescu029c1762023-05-11 03:27:34 +0000203#ifndef __TRUSTY__
David Brazdilae3ec6a2022-12-14 13:13:32 +0000204AIBinder* ARpcSession_setupVsockClient(ARpcSession* handle, unsigned int cid, unsigned int port) {
205 auto session = handleToStrongPointer<RpcSession>(handle);
Steven Moreland2372f9d2021-08-05 15:42:01 -0700206 if (status_t status = session->setupVsockClient(cid, port); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700207 ALOGE("Failed to set up vsock client with CID %u and port %u error: %s", cid, port,
208 statusToString(status).c_str());
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700209 return nullptr;
210 }
211 return AIBinder_fromPlatformBinder(session->getRootObject());
212}
Inseob Kime12fccc2021-09-02 17:23:33 +0900213
David Brazdilae3ec6a2022-12-14 13:13:32 +0000214AIBinder* ARpcSession_setupUnixDomainClient(ARpcSession* handle, const char* name) {
Alice Wang893a9912022-10-24 10:44:09 +0000215 std::string pathname(name);
216 pathname = ANDROID_SOCKET_DIR "/" + pathname;
David Brazdilae3ec6a2022-12-14 13:13:32 +0000217 auto session = handleToStrongPointer<RpcSession>(handle);
Alice Wang893a9912022-10-24 10:44:09 +0000218 if (status_t status = session->setupUnixDomainClient(pathname.c_str()); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700219 ALOGE("Failed to set up Unix Domain RPC client with path: %s error: %s", pathname.c_str(),
220 statusToString(status).c_str());
Alice Wang893a9912022-10-24 10:44:09 +0000221 return nullptr;
222 }
223 return AIBinder_fromPlatformBinder(session->getRootObject());
224}
225
David Brazdile1bd9832022-12-14 20:22:09 +0000226AIBinder* ARpcSession_setupUnixDomainBootstrapClient(ARpcSession* handle, int bootstrapFd) {
227 auto session = handleToStrongPointer<RpcSession>(handle);
228 auto fd = unique_fd(dup(bootstrapFd));
229 if (!fd.ok()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700230 ALOGE("Invalid bootstrap fd %d", bootstrapFd);
David Brazdile1bd9832022-12-14 20:22:09 +0000231 return nullptr;
232 }
233 if (status_t status = session->setupUnixDomainSocketBootstrapClient(std::move(fd));
234 status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700235 ALOGE("Failed to set up Unix Domain RPC client with bootstrap fd: %d error: %s",
236 bootstrapFd, statusToString(status).c_str());
David Brazdile1bd9832022-12-14 20:22:09 +0000237 return nullptr;
238 }
239 return AIBinder_fromPlatformBinder(session->getRootObject());
240}
241
Yiming Jing34637062023-02-05 14:05:32 -0800242AIBinder* ARpcSession_setupInet(ARpcSession* handle, const char* address, unsigned int port) {
243 auto session = handleToStrongPointer<RpcSession>(handle);
244 if (status_t status = session->setupInetClient(address, port); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700245 ALOGE("Failed to set up inet RPC client with address %s and port %u error: %s", address,
246 port, statusToString(status).c_str());
Yiming Jing34637062023-02-05 14:05:32 -0800247 return nullptr;
248 }
249 return AIBinder_fromPlatformBinder(session->getRootObject());
250}
Andrei Homescu029c1762023-05-11 03:27:34 +0000251#endif // __TRUSTY__
Yiming Jing34637062023-02-05 14:05:32 -0800252
David Brazdilae3ec6a2022-12-14 13:13:32 +0000253AIBinder* ARpcSession_setupPreconnectedClient(ARpcSession* handle, int (*requestFd)(void* param),
254 void* param) {
255 auto session = handleToStrongPointer<RpcSession>(handle);
Inseob Kime12fccc2021-09-02 17:23:33 +0900256 auto request = [=] { return unique_fd{requestFd(param)}; };
257 if (status_t status = session->setupPreconnectedClient(unique_fd{}, request); status != OK) {
Andrei Homescu029c1762023-05-11 03:27:34 +0000258 ALOGE("Failed to set up preconnected client. error: %s", statusToString(status).c_str());
Inseob Kime12fccc2021-09-02 17:23:33 +0900259 return nullptr;
260 }
261 return AIBinder_fromPlatformBinder(session->getRootObject());
262}
David Brazdilae3ec6a2022-12-14 13:13:32 +0000263
264void ARpcSession_setFileDescriptorTransportMode(ARpcSession* handle,
265 ARpcSession_FileDescriptorTransportMode mode) {
266 auto session = handleToStrongPointer<RpcSession>(handle);
267 session->setFileDescriptorTransportMode(toTransportMode(mode));
268}
269
270void ARpcSession_setMaxIncomingThreads(ARpcSession* handle, size_t threads) {
271 auto session = handleToStrongPointer<RpcSession>(handle);
272 session->setMaxIncomingThreads(threads);
273}
274
Steven Morelande65f73c2023-03-04 01:34:50 +0000275void ARpcSession_setMaxOutgoingConnections(ARpcSession* handle, size_t connections) {
David Brazdilae3ec6a2022-12-14 13:13:32 +0000276 auto session = handleToStrongPointer<RpcSession>(handle);
Steven Morelande65f73c2023-03-04 01:34:50 +0000277 session->setMaxOutgoingConnections(connections);
David Brazdilae3ec6a2022-12-14 13:13:32 +0000278}
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700279}