blob: 21537fc50d3f0ba0dee672b1eaaeb883019bd2cf [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__
David Brazdila47dfda2022-11-22 22:52:19 +000084ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, unsigned int port) {
Victor Hsiehd35d31d2021-06-03 11:24:31 -070085 auto server = RpcServer::make();
David Brazdila47dfda2022-11-22 22:52:19 +000086
87 unsigned int bindCid = VMADDR_CID_ANY; // bind to the remote interface
88 if (cid == VMADDR_CID_LOCAL) {
89 bindCid = VMADDR_CID_LOCAL; // bind to the local interface
90 cid = VMADDR_CID_ANY; // no need for a connection filter
91 }
92
93 if (status_t status = server->setupVsockServer(bindCid, port); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070094 ALOGE("Failed to set up vsock server with port %u error: %s", port,
95 statusToString(status).c_str());
David Brazdilefb56832022-11-16 11:47:00 +000096 return nullptr;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070097 }
David Brazdila47dfda2022-11-22 22:52:19 +000098 if (cid != VMADDR_CID_ANY) {
99 server->setConnectionFilter([=](const void* addr, size_t addrlen) {
David Brazdilb96737d2022-12-14 14:03:42 +0000100 LOG_ALWAYS_FATAL_IF(addrlen < sizeof(sockaddr_vm), "sockaddr is truncated");
101 const sockaddr_vm* vaddr = reinterpret_cast<const sockaddr_vm*>(addr);
102 LOG_ALWAYS_FATAL_IF(vaddr->svm_family != AF_VSOCK, "address is not a vsock");
103 if (cid != vaddr->svm_cid) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700104 ALOGE("Rejected vsock connection from CID %u", vaddr->svm_cid);
David Brazdila47dfda2022-11-22 22:52:19 +0000105 return false;
106 }
107 return true;
108 });
109 }
David Brazdilefb56832022-11-16 11:47:00 +0000110 server->setRootObject(AIBinder_toPlatformBinder(service));
David Brazdilae3ec6a2022-12-14 13:13:32 +0000111 return createObjectHandle<ARpcServer>(server);
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700112}
113
Alice Wang072c3982023-05-25 09:45:13 +0000114ARpcServer* ARpcServer_newBoundSocket(AIBinder* service, int socketFd) {
David Brazdilefb56832022-11-16 11:47:00 +0000115 auto server = RpcServer::make();
Alice Wang072c3982023-05-25 09:45:13 +0000116 auto fd = unique_fd(socketFd);
David Brazdilefb56832022-11-16 11:47:00 +0000117 if (!fd.ok()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700118 ALOGE("Invalid socket fd %d", socketFd);
David Brazdilefb56832022-11-16 11:47:00 +0000119 return nullptr;
120 }
121 if (status_t status = server->setupRawSocketServer(std::move(fd)); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700122 ALOGE("Failed to set up RPC server with fd %d error: %s", socketFd,
123 statusToString(status).c_str());
David Brazdilefb56832022-11-16 11:47:00 +0000124 return nullptr;
125 }
126 server->setRootObject(AIBinder_toPlatformBinder(service));
David Brazdilae3ec6a2022-12-14 13:13:32 +0000127 return createObjectHandle<ARpcServer>(server);
128}
129
David Brazdile1bd9832022-12-14 20:22:09 +0000130ARpcServer* ARpcServer_newUnixDomainBootstrap(AIBinder* service, int bootstrapFd) {
131 auto server = RpcServer::make();
132 auto fd = unique_fd(bootstrapFd);
133 if (!fd.ok()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700134 ALOGE("Invalid bootstrap fd %d", bootstrapFd);
David Brazdile1bd9832022-12-14 20:22:09 +0000135 return nullptr;
136 }
137 if (status_t status = server->setupUnixDomainSocketBootstrapServer(std::move(fd));
138 status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700139 ALOGE("Failed to set up Unix Domain RPC server with bootstrap fd %d error: %s", bootstrapFd,
140 statusToString(status).c_str());
David Brazdile1bd9832022-12-14 20:22:09 +0000141 return nullptr;
142 }
143 server->setRootObject(AIBinder_toPlatformBinder(service));
144 return createObjectHandle<ARpcServer>(server);
145}
146
Yiming Jing34637062023-02-05 14:05:32 -0800147ARpcServer* ARpcServer_newInet(AIBinder* service, const char* address, unsigned int port) {
148 auto server = RpcServer::make();
149 if (status_t status = server->setupInetServer(address, port, nullptr); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700150 ALOGE("Failed to set up inet RPC server with address %s and port %u error: %s", address,
151 port, statusToString(status).c_str());
Yiming Jing34637062023-02-05 14:05:32 -0800152 return nullptr;
153 }
154 server->setRootObject(AIBinder_toPlatformBinder(service));
155 return createObjectHandle<ARpcServer>(server);
156}
Andrei Homescu029c1762023-05-11 03:27:34 +0000157#endif // __TRUSTY__
Yiming Jing34637062023-02-05 14:05:32 -0800158
David Brazdilae3ec6a2022-12-14 13:13:32 +0000159void ARpcServer_setSupportedFileDescriptorTransportModes(
160 ARpcServer* handle, const ARpcSession_FileDescriptorTransportMode modes[],
161 size_t modes_len) {
162 auto server = handleToStrongPointer<RpcServer>(handle);
163 std::vector<RpcSession::FileDescriptorTransportMode> modevec;
164 for (size_t i = 0; i < modes_len; i++) {
165 modevec.push_back(toTransportMode(modes[i]));
166 }
167 server->setSupportedFileDescriptorTransportModes(modevec);
David Brazdilefb56832022-11-16 11:47:00 +0000168}
169
Ashutosh Agarwal55d76342024-04-17 21:16:06 +0000170void ARpcServer_setMaxThreads(ARpcServer* handle, size_t threads) {
171 handleToStrongPointer<RpcServer>(handle)->setMaxThreads(threads);
172}
173
David Brazdilefb56832022-11-16 11:47:00 +0000174void ARpcServer_start(ARpcServer* handle) {
David Brazdilae3ec6a2022-12-14 13:13:32 +0000175 handleToStrongPointer<RpcServer>(handle)->start();
David Brazdilefb56832022-11-16 11:47:00 +0000176}
177
178void ARpcServer_join(ARpcServer* handle) {
David Brazdilae3ec6a2022-12-14 13:13:32 +0000179 handleToStrongPointer<RpcServer>(handle)->join();
David Brazdilefb56832022-11-16 11:47:00 +0000180}
181
David Brazdil793e8792022-12-19 23:16:30 +0000182bool ARpcServer_shutdown(ARpcServer* handle) {
183 return handleToStrongPointer<RpcServer>(handle)->shutdown();
David Brazdilefb56832022-11-16 11:47:00 +0000184}
185
186void ARpcServer_free(ARpcServer* handle) {
David Brazdil4766a1f2022-12-19 21:58:25 +0000187 // Ignore the result of ARpcServer_shutdown - either it had been called
188 // earlier, or the RpcServer destructor will panic.
189 (void)ARpcServer_shutdown(handle);
David Brazdilae3ec6a2022-12-14 13:13:32 +0000190 freeObjectHandle<RpcServer>(handle);
Inseob Kim172473f2021-09-07 21:01:33 +0900191}
192
David Brazdilae3ec6a2022-12-14 13:13:32 +0000193ARpcSession* ARpcSession_new() {
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700194 auto session = RpcSession::make();
David Brazdilae3ec6a2022-12-14 13:13:32 +0000195 return createObjectHandle<ARpcSession>(session);
196}
197
198void ARpcSession_free(ARpcSession* handle) {
199 freeObjectHandle<RpcSession>(handle);
200}
201
Andrei Homescu029c1762023-05-11 03:27:34 +0000202#ifndef __TRUSTY__
David Brazdilae3ec6a2022-12-14 13:13:32 +0000203AIBinder* ARpcSession_setupVsockClient(ARpcSession* handle, unsigned int cid, unsigned int port) {
204 auto session = handleToStrongPointer<RpcSession>(handle);
Steven Moreland2372f9d2021-08-05 15:42:01 -0700205 if (status_t status = session->setupVsockClient(cid, port); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700206 ALOGE("Failed to set up vsock client with CID %u and port %u error: %s", cid, port,
207 statusToString(status).c_str());
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700208 return nullptr;
209 }
210 return AIBinder_fromPlatformBinder(session->getRootObject());
211}
Inseob Kime12fccc2021-09-02 17:23:33 +0900212
David Brazdilae3ec6a2022-12-14 13:13:32 +0000213AIBinder* ARpcSession_setupUnixDomainClient(ARpcSession* handle, const char* name) {
Alice Wang893a9912022-10-24 10:44:09 +0000214 std::string pathname(name);
215 pathname = ANDROID_SOCKET_DIR "/" + pathname;
David Brazdilae3ec6a2022-12-14 13:13:32 +0000216 auto session = handleToStrongPointer<RpcSession>(handle);
Alice Wang893a9912022-10-24 10:44:09 +0000217 if (status_t status = session->setupUnixDomainClient(pathname.c_str()); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700218 ALOGE("Failed to set up Unix Domain RPC client with path: %s error: %s", pathname.c_str(),
219 statusToString(status).c_str());
Alice Wang893a9912022-10-24 10:44:09 +0000220 return nullptr;
221 }
222 return AIBinder_fromPlatformBinder(session->getRootObject());
223}
224
David Brazdile1bd9832022-12-14 20:22:09 +0000225AIBinder* ARpcSession_setupUnixDomainBootstrapClient(ARpcSession* handle, int bootstrapFd) {
226 auto session = handleToStrongPointer<RpcSession>(handle);
227 auto fd = unique_fd(dup(bootstrapFd));
228 if (!fd.ok()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700229 ALOGE("Invalid bootstrap fd %d", bootstrapFd);
David Brazdile1bd9832022-12-14 20:22:09 +0000230 return nullptr;
231 }
232 if (status_t status = session->setupUnixDomainSocketBootstrapClient(std::move(fd));
233 status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700234 ALOGE("Failed to set up Unix Domain RPC client with bootstrap fd: %d error: %s",
235 bootstrapFd, statusToString(status).c_str());
David Brazdile1bd9832022-12-14 20:22:09 +0000236 return nullptr;
237 }
238 return AIBinder_fromPlatformBinder(session->getRootObject());
239}
240
Yiming Jing34637062023-02-05 14:05:32 -0800241AIBinder* ARpcSession_setupInet(ARpcSession* handle, const char* address, unsigned int port) {
242 auto session = handleToStrongPointer<RpcSession>(handle);
243 if (status_t status = session->setupInetClient(address, port); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700244 ALOGE("Failed to set up inet RPC client with address %s and port %u error: %s", address,
245 port, statusToString(status).c_str());
Yiming Jing34637062023-02-05 14:05:32 -0800246 return nullptr;
247 }
248 return AIBinder_fromPlatformBinder(session->getRootObject());
249}
Andrei Homescu029c1762023-05-11 03:27:34 +0000250#endif // __TRUSTY__
Yiming Jing34637062023-02-05 14:05:32 -0800251
David Brazdilae3ec6a2022-12-14 13:13:32 +0000252AIBinder* ARpcSession_setupPreconnectedClient(ARpcSession* handle, int (*requestFd)(void* param),
253 void* param) {
254 auto session = handleToStrongPointer<RpcSession>(handle);
Inseob Kime12fccc2021-09-02 17:23:33 +0900255 auto request = [=] { return unique_fd{requestFd(param)}; };
256 if (status_t status = session->setupPreconnectedClient(unique_fd{}, request); status != OK) {
Andrei Homescu029c1762023-05-11 03:27:34 +0000257 ALOGE("Failed to set up preconnected client. error: %s", statusToString(status).c_str());
Inseob Kime12fccc2021-09-02 17:23:33 +0900258 return nullptr;
259 }
260 return AIBinder_fromPlatformBinder(session->getRootObject());
261}
David Brazdilae3ec6a2022-12-14 13:13:32 +0000262
263void ARpcSession_setFileDescriptorTransportMode(ARpcSession* handle,
264 ARpcSession_FileDescriptorTransportMode mode) {
265 auto session = handleToStrongPointer<RpcSession>(handle);
266 session->setFileDescriptorTransportMode(toTransportMode(mode));
267}
268
269void ARpcSession_setMaxIncomingThreads(ARpcSession* handle, size_t threads) {
270 auto session = handleToStrongPointer<RpcSession>(handle);
271 session->setMaxIncomingThreads(threads);
272}
273
Steven Morelande65f73c2023-03-04 01:34:50 +0000274void ARpcSession_setMaxOutgoingConnections(ARpcSession* handle, size_t connections) {
David Brazdilae3ec6a2022-12-14 13:13:32 +0000275 auto session = handleToStrongPointer<RpcSession>(handle);
Steven Morelande65f73c2023-03-04 01:34:50 +0000276 session->setMaxOutgoingConnections(connections);
David Brazdilae3ec6a2022-12-14 13:13:32 +0000277}
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700278}