blob: ddd82e8ef7c1bc2813deeb569537c2466a1ad998 [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>
Alice Wang893a9912022-10-24 10:44:09 +000023#include <cutils/sockets.h>
Inseob Kim091050a2021-10-25 14:25:25 +000024#include <linux/vm_sockets.h>
Victor Hsiehd35d31d2021-06-03 11:24:31 -070025
Steven Moreland2372f9d2021-08-05 15:42:01 -070026using android::OK;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070027using android::RpcServer;
28using android::RpcSession;
David Brazdilefb56832022-11-16 11:47:00 +000029using android::sp;
Steven Moreland2372f9d2021-08-05 15:42:01 -070030using android::status_t;
31using android::statusToString;
Tomasz Wasilczyk639490b2023-11-01 13:49:41 -070032using android::binder::unique_fd;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070033
David Brazdilefb56832022-11-16 11:47:00 +000034// Opaque handle for RpcServer.
35struct ARpcServer {};
Victor Hsiehd35d31d2021-06-03 11:24:31 -070036
David Brazdilae3ec6a2022-12-14 13:13:32 +000037// Opaque handle for RpcSession.
38struct ARpcSession {};
Alice Wang893a9912022-10-24 10:44:09 +000039
David Brazdilae3ec6a2022-12-14 13:13:32 +000040template <typename A, typename T>
41static A* createObjectHandle(sp<T>& server) {
David Brazdilefb56832022-11-16 11:47:00 +000042 auto ref = server.get();
43 ref->incStrong(ref);
David Brazdilae3ec6a2022-12-14 13:13:32 +000044 return reinterpret_cast<A*>(ref);
David Brazdilefb56832022-11-16 11:47:00 +000045}
46
David Brazdilae3ec6a2022-12-14 13:13:32 +000047template <typename T, typename A>
48static void freeObjectHandle(A* handle) {
49 LOG_ALWAYS_FATAL_IF(handle == nullptr, "Handle cannot be null");
50 auto ref = reinterpret_cast<T*>(handle);
David Brazdilefb56832022-11-16 11:47:00 +000051 ref->decStrong(ref);
52}
53
David Brazdilae3ec6a2022-12-14 13:13:32 +000054template <typename T, typename A>
55static sp<T> handleToStrongPointer(A* handle) {
56 LOG_ALWAYS_FATAL_IF(handle == nullptr, "Handle cannot be null");
57 auto ref = reinterpret_cast<T*>(handle);
58 return sp<T>::fromExisting(ref);
59}
60
61RpcSession::FileDescriptorTransportMode toTransportMode(
62 ARpcSession_FileDescriptorTransportMode mode) {
63 switch (mode) {
64 case ARpcSession_FileDescriptorTransportMode::None:
65 return RpcSession::FileDescriptorTransportMode::NONE;
66 case ARpcSession_FileDescriptorTransportMode::Unix:
67 return RpcSession::FileDescriptorTransportMode::UNIX;
68 case ARpcSession_FileDescriptorTransportMode::Trusty:
69 return RpcSession::FileDescriptorTransportMode::TRUSTY;
70 default:
71 return RpcSession::FileDescriptorTransportMode::NONE;
72 }
73}
74
David Brazdilefb56832022-11-16 11:47:00 +000075extern "C" {
76
David Brazdila47dfda2022-11-22 22:52:19 +000077ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, unsigned int port) {
Victor Hsiehd35d31d2021-06-03 11:24:31 -070078 auto server = RpcServer::make();
David Brazdila47dfda2022-11-22 22:52:19 +000079
80 unsigned int bindCid = VMADDR_CID_ANY; // bind to the remote interface
81 if (cid == VMADDR_CID_LOCAL) {
82 bindCid = VMADDR_CID_LOCAL; // bind to the local interface
83 cid = VMADDR_CID_ANY; // no need for a connection filter
84 }
85
86 if (status_t status = server->setupVsockServer(bindCid, port); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070087 ALOGE("Failed to set up vsock server with port %u error: %s", port,
88 statusToString(status).c_str());
David Brazdilefb56832022-11-16 11:47:00 +000089 return nullptr;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070090 }
David Brazdila47dfda2022-11-22 22:52:19 +000091 if (cid != VMADDR_CID_ANY) {
92 server->setConnectionFilter([=](const void* addr, size_t addrlen) {
David Brazdilb96737d2022-12-14 14:03:42 +000093 LOG_ALWAYS_FATAL_IF(addrlen < sizeof(sockaddr_vm), "sockaddr is truncated");
94 const sockaddr_vm* vaddr = reinterpret_cast<const sockaddr_vm*>(addr);
95 LOG_ALWAYS_FATAL_IF(vaddr->svm_family != AF_VSOCK, "address is not a vsock");
96 if (cid != vaddr->svm_cid) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -070097 ALOGE("Rejected vsock connection from CID %u", vaddr->svm_cid);
David Brazdila47dfda2022-11-22 22:52:19 +000098 return false;
99 }
100 return true;
101 });
102 }
David Brazdilefb56832022-11-16 11:47:00 +0000103 server->setRootObject(AIBinder_toPlatformBinder(service));
David Brazdilae3ec6a2022-12-14 13:13:32 +0000104 return createObjectHandle<ARpcServer>(server);
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700105}
106
Alice Wang072c3982023-05-25 09:45:13 +0000107ARpcServer* ARpcServer_newBoundSocket(AIBinder* service, int socketFd) {
David Brazdilefb56832022-11-16 11:47:00 +0000108 auto server = RpcServer::make();
Alice Wang072c3982023-05-25 09:45:13 +0000109 auto fd = unique_fd(socketFd);
David Brazdilefb56832022-11-16 11:47:00 +0000110 if (!fd.ok()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700111 ALOGE("Invalid socket fd %d", socketFd);
David Brazdilefb56832022-11-16 11:47:00 +0000112 return nullptr;
113 }
114 if (status_t status = server->setupRawSocketServer(std::move(fd)); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700115 ALOGE("Failed to set up RPC server with fd %d error: %s", socketFd,
116 statusToString(status).c_str());
David Brazdilefb56832022-11-16 11:47:00 +0000117 return nullptr;
118 }
119 server->setRootObject(AIBinder_toPlatformBinder(service));
David Brazdilae3ec6a2022-12-14 13:13:32 +0000120 return createObjectHandle<ARpcServer>(server);
121}
122
David Brazdile1bd9832022-12-14 20:22:09 +0000123ARpcServer* ARpcServer_newUnixDomainBootstrap(AIBinder* service, int bootstrapFd) {
124 auto server = RpcServer::make();
125 auto fd = unique_fd(bootstrapFd);
126 if (!fd.ok()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700127 ALOGE("Invalid bootstrap fd %d", bootstrapFd);
David Brazdile1bd9832022-12-14 20:22:09 +0000128 return nullptr;
129 }
130 if (status_t status = server->setupUnixDomainSocketBootstrapServer(std::move(fd));
131 status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700132 ALOGE("Failed to set up Unix Domain RPC server with bootstrap fd %d error: %s", bootstrapFd,
133 statusToString(status).c_str());
David Brazdile1bd9832022-12-14 20:22:09 +0000134 return nullptr;
135 }
136 server->setRootObject(AIBinder_toPlatformBinder(service));
137 return createObjectHandle<ARpcServer>(server);
138}
139
Yiming Jing34637062023-02-05 14:05:32 -0800140ARpcServer* ARpcServer_newInet(AIBinder* service, const char* address, unsigned int port) {
141 auto server = RpcServer::make();
142 if (status_t status = server->setupInetServer(address, port, nullptr); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700143 ALOGE("Failed to set up inet RPC server with address %s and port %u error: %s", address,
144 port, statusToString(status).c_str());
Yiming Jing34637062023-02-05 14:05:32 -0800145 return nullptr;
146 }
147 server->setRootObject(AIBinder_toPlatformBinder(service));
148 return createObjectHandle<ARpcServer>(server);
149}
150
David Brazdilae3ec6a2022-12-14 13:13:32 +0000151void ARpcServer_setSupportedFileDescriptorTransportModes(
152 ARpcServer* handle, const ARpcSession_FileDescriptorTransportMode modes[],
153 size_t modes_len) {
154 auto server = handleToStrongPointer<RpcServer>(handle);
155 std::vector<RpcSession::FileDescriptorTransportMode> modevec;
156 for (size_t i = 0; i < modes_len; i++) {
157 modevec.push_back(toTransportMode(modes[i]));
158 }
159 server->setSupportedFileDescriptorTransportModes(modevec);
David Brazdilefb56832022-11-16 11:47:00 +0000160}
161
162void ARpcServer_start(ARpcServer* handle) {
David Brazdilae3ec6a2022-12-14 13:13:32 +0000163 handleToStrongPointer<RpcServer>(handle)->start();
David Brazdilefb56832022-11-16 11:47:00 +0000164}
165
166void ARpcServer_join(ARpcServer* handle) {
David Brazdilae3ec6a2022-12-14 13:13:32 +0000167 handleToStrongPointer<RpcServer>(handle)->join();
David Brazdilefb56832022-11-16 11:47:00 +0000168}
169
David Brazdil793e8792022-12-19 23:16:30 +0000170bool ARpcServer_shutdown(ARpcServer* handle) {
171 return handleToStrongPointer<RpcServer>(handle)->shutdown();
David Brazdilefb56832022-11-16 11:47:00 +0000172}
173
174void ARpcServer_free(ARpcServer* handle) {
David Brazdil4766a1f2022-12-19 21:58:25 +0000175 // Ignore the result of ARpcServer_shutdown - either it had been called
176 // earlier, or the RpcServer destructor will panic.
177 (void)ARpcServer_shutdown(handle);
David Brazdilae3ec6a2022-12-14 13:13:32 +0000178 freeObjectHandle<RpcServer>(handle);
Inseob Kim172473f2021-09-07 21:01:33 +0900179}
180
David Brazdilae3ec6a2022-12-14 13:13:32 +0000181ARpcSession* ARpcSession_new() {
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700182 auto session = RpcSession::make();
David Brazdilae3ec6a2022-12-14 13:13:32 +0000183 return createObjectHandle<ARpcSession>(session);
184}
185
186void ARpcSession_free(ARpcSession* handle) {
187 freeObjectHandle<RpcSession>(handle);
188}
189
190AIBinder* ARpcSession_setupVsockClient(ARpcSession* handle, unsigned int cid, unsigned int port) {
191 auto session = handleToStrongPointer<RpcSession>(handle);
Steven Moreland2372f9d2021-08-05 15:42:01 -0700192 if (status_t status = session->setupVsockClient(cid, port); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700193 ALOGE("Failed to set up vsock client with CID %u and port %u error: %s", cid, port,
194 statusToString(status).c_str());
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700195 return nullptr;
196 }
197 return AIBinder_fromPlatformBinder(session->getRootObject());
198}
Inseob Kime12fccc2021-09-02 17:23:33 +0900199
David Brazdilae3ec6a2022-12-14 13:13:32 +0000200AIBinder* ARpcSession_setupUnixDomainClient(ARpcSession* handle, const char* name) {
Alice Wang893a9912022-10-24 10:44:09 +0000201 std::string pathname(name);
202 pathname = ANDROID_SOCKET_DIR "/" + pathname;
David Brazdilae3ec6a2022-12-14 13:13:32 +0000203 auto session = handleToStrongPointer<RpcSession>(handle);
Alice Wang893a9912022-10-24 10:44:09 +0000204 if (status_t status = session->setupUnixDomainClient(pathname.c_str()); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700205 ALOGE("Failed to set up Unix Domain RPC client with path: %s error: %s", pathname.c_str(),
206 statusToString(status).c_str());
Alice Wang893a9912022-10-24 10:44:09 +0000207 return nullptr;
208 }
209 return AIBinder_fromPlatformBinder(session->getRootObject());
210}
211
David Brazdile1bd9832022-12-14 20:22:09 +0000212AIBinder* ARpcSession_setupUnixDomainBootstrapClient(ARpcSession* handle, int bootstrapFd) {
213 auto session = handleToStrongPointer<RpcSession>(handle);
214 auto fd = unique_fd(dup(bootstrapFd));
215 if (!fd.ok()) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700216 ALOGE("Invalid bootstrap fd %d", bootstrapFd);
David Brazdile1bd9832022-12-14 20:22:09 +0000217 return nullptr;
218 }
219 if (status_t status = session->setupUnixDomainSocketBootstrapClient(std::move(fd));
220 status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700221 ALOGE("Failed to set up Unix Domain RPC client with bootstrap fd: %d error: %s",
222 bootstrapFd, statusToString(status).c_str());
David Brazdile1bd9832022-12-14 20:22:09 +0000223 return nullptr;
224 }
225 return AIBinder_fromPlatformBinder(session->getRootObject());
226}
227
Yiming Jing34637062023-02-05 14:05:32 -0800228AIBinder* ARpcSession_setupInet(ARpcSession* handle, const char* address, unsigned int port) {
229 auto session = handleToStrongPointer<RpcSession>(handle);
230 if (status_t status = session->setupInetClient(address, port); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700231 ALOGE("Failed to set up inet RPC client with address %s and port %u error: %s", address,
232 port, statusToString(status).c_str());
Yiming Jing34637062023-02-05 14:05:32 -0800233 return nullptr;
234 }
235 return AIBinder_fromPlatformBinder(session->getRootObject());
236}
237
David Brazdilae3ec6a2022-12-14 13:13:32 +0000238AIBinder* ARpcSession_setupPreconnectedClient(ARpcSession* handle, int (*requestFd)(void* param),
239 void* param) {
240 auto session = handleToStrongPointer<RpcSession>(handle);
Inseob Kime12fccc2021-09-02 17:23:33 +0900241 auto request = [=] { return unique_fd{requestFd(param)}; };
242 if (status_t status = session->setupPreconnectedClient(unique_fd{}, request); status != OK) {
Tomasz Wasilczyke3de8802023-11-01 11:05:27 -0700243 ALOGE("Failed to set up vsock client. error: %s", statusToString(status).c_str());
Inseob Kime12fccc2021-09-02 17:23:33 +0900244 return nullptr;
245 }
246 return AIBinder_fromPlatformBinder(session->getRootObject());
247}
David Brazdilae3ec6a2022-12-14 13:13:32 +0000248
249void ARpcSession_setFileDescriptorTransportMode(ARpcSession* handle,
250 ARpcSession_FileDescriptorTransportMode mode) {
251 auto session = handleToStrongPointer<RpcSession>(handle);
252 session->setFileDescriptorTransportMode(toTransportMode(mode));
253}
254
255void ARpcSession_setMaxIncomingThreads(ARpcSession* handle, size_t threads) {
256 auto session = handleToStrongPointer<RpcSession>(handle);
257 session->setMaxIncomingThreads(threads);
258}
259
Steven Morelande65f73c2023-03-04 01:34:50 +0000260void ARpcSession_setMaxOutgoingConnections(ARpcSession* handle, size_t connections) {
David Brazdilae3ec6a2022-12-14 13:13:32 +0000261 auto session = handleToStrongPointer<RpcSession>(handle);
Steven Morelande65f73c2023-03-04 01:34:50 +0000262 session->setMaxOutgoingConnections(connections);
David Brazdilae3ec6a2022-12-14 13:13:32 +0000263}
Victor Hsiehd35d31d2021-06-03 11:24:31 -0700264}