Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 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 | |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 17 | #include <binder_rpc_unstable.hpp> |
| 18 | |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 19 | #include <android/binder_libbinder.h> |
| 20 | #include <binder/RpcServer.h> |
| 21 | #include <binder/RpcSession.h> |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame^] | 22 | #include <binder/unique_fd.h> |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 23 | #include <cutils/sockets.h> |
Inseob Kim | 091050a | 2021-10-25 14:25:25 +0000 | [diff] [blame] | 24 | #include <linux/vm_sockets.h> |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 25 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 26 | using android::OK; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 27 | using android::RpcServer; |
| 28 | using android::RpcSession; |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 29 | using android::sp; |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 30 | using android::status_t; |
| 31 | using android::statusToString; |
Tomasz Wasilczyk | 639490b | 2023-11-01 13:49:41 -0700 | [diff] [blame^] | 32 | using android::binder::unique_fd; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 33 | |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 34 | // Opaque handle for RpcServer. |
| 35 | struct ARpcServer {}; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 36 | |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 37 | // Opaque handle for RpcSession. |
| 38 | struct ARpcSession {}; |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 39 | |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 40 | template <typename A, typename T> |
| 41 | static A* createObjectHandle(sp<T>& server) { |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 42 | auto ref = server.get(); |
| 43 | ref->incStrong(ref); |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 44 | return reinterpret_cast<A*>(ref); |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 45 | } |
| 46 | |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 47 | template <typename T, typename A> |
| 48 | static void freeObjectHandle(A* handle) { |
| 49 | LOG_ALWAYS_FATAL_IF(handle == nullptr, "Handle cannot be null"); |
| 50 | auto ref = reinterpret_cast<T*>(handle); |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 51 | ref->decStrong(ref); |
| 52 | } |
| 53 | |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 54 | template <typename T, typename A> |
| 55 | static 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 | |
| 61 | RpcSession::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 Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 75 | extern "C" { |
| 76 | |
David Brazdil | a47dfda | 2022-11-22 22:52:19 +0000 | [diff] [blame] | 77 | ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, unsigned int port) { |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 78 | auto server = RpcServer::make(); |
David Brazdil | a47dfda | 2022-11-22 22:52:19 +0000 | [diff] [blame] | 79 | |
| 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 Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 87 | ALOGE("Failed to set up vsock server with port %u error: %s", port, |
| 88 | statusToString(status).c_str()); |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 89 | return nullptr; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 90 | } |
David Brazdil | a47dfda | 2022-11-22 22:52:19 +0000 | [diff] [blame] | 91 | if (cid != VMADDR_CID_ANY) { |
| 92 | server->setConnectionFilter([=](const void* addr, size_t addrlen) { |
David Brazdil | b96737d | 2022-12-14 14:03:42 +0000 | [diff] [blame] | 93 | 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 Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 97 | ALOGE("Rejected vsock connection from CID %u", vaddr->svm_cid); |
David Brazdil | a47dfda | 2022-11-22 22:52:19 +0000 | [diff] [blame] | 98 | return false; |
| 99 | } |
| 100 | return true; |
| 101 | }); |
| 102 | } |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 103 | server->setRootObject(AIBinder_toPlatformBinder(service)); |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 104 | return createObjectHandle<ARpcServer>(server); |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Alice Wang | 072c398 | 2023-05-25 09:45:13 +0000 | [diff] [blame] | 107 | ARpcServer* ARpcServer_newBoundSocket(AIBinder* service, int socketFd) { |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 108 | auto server = RpcServer::make(); |
Alice Wang | 072c398 | 2023-05-25 09:45:13 +0000 | [diff] [blame] | 109 | auto fd = unique_fd(socketFd); |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 110 | if (!fd.ok()) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 111 | ALOGE("Invalid socket fd %d", socketFd); |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 112 | return nullptr; |
| 113 | } |
| 114 | if (status_t status = server->setupRawSocketServer(std::move(fd)); status != OK) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 115 | ALOGE("Failed to set up RPC server with fd %d error: %s", socketFd, |
| 116 | statusToString(status).c_str()); |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 117 | return nullptr; |
| 118 | } |
| 119 | server->setRootObject(AIBinder_toPlatformBinder(service)); |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 120 | return createObjectHandle<ARpcServer>(server); |
| 121 | } |
| 122 | |
David Brazdil | e1bd983 | 2022-12-14 20:22:09 +0000 | [diff] [blame] | 123 | ARpcServer* ARpcServer_newUnixDomainBootstrap(AIBinder* service, int bootstrapFd) { |
| 124 | auto server = RpcServer::make(); |
| 125 | auto fd = unique_fd(bootstrapFd); |
| 126 | if (!fd.ok()) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 127 | ALOGE("Invalid bootstrap fd %d", bootstrapFd); |
David Brazdil | e1bd983 | 2022-12-14 20:22:09 +0000 | [diff] [blame] | 128 | return nullptr; |
| 129 | } |
| 130 | if (status_t status = server->setupUnixDomainSocketBootstrapServer(std::move(fd)); |
| 131 | status != OK) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 132 | ALOGE("Failed to set up Unix Domain RPC server with bootstrap fd %d error: %s", bootstrapFd, |
| 133 | statusToString(status).c_str()); |
David Brazdil | e1bd983 | 2022-12-14 20:22:09 +0000 | [diff] [blame] | 134 | return nullptr; |
| 135 | } |
| 136 | server->setRootObject(AIBinder_toPlatformBinder(service)); |
| 137 | return createObjectHandle<ARpcServer>(server); |
| 138 | } |
| 139 | |
Yiming Jing | 3463706 | 2023-02-05 14:05:32 -0800 | [diff] [blame] | 140 | ARpcServer* 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 Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 143 | ALOGE("Failed to set up inet RPC server with address %s and port %u error: %s", address, |
| 144 | port, statusToString(status).c_str()); |
Yiming Jing | 3463706 | 2023-02-05 14:05:32 -0800 | [diff] [blame] | 145 | return nullptr; |
| 146 | } |
| 147 | server->setRootObject(AIBinder_toPlatformBinder(service)); |
| 148 | return createObjectHandle<ARpcServer>(server); |
| 149 | } |
| 150 | |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 151 | void 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 Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | void ARpcServer_start(ARpcServer* handle) { |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 163 | handleToStrongPointer<RpcServer>(handle)->start(); |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void ARpcServer_join(ARpcServer* handle) { |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 167 | handleToStrongPointer<RpcServer>(handle)->join(); |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 168 | } |
| 169 | |
David Brazdil | 793e879 | 2022-12-19 23:16:30 +0000 | [diff] [blame] | 170 | bool ARpcServer_shutdown(ARpcServer* handle) { |
| 171 | return handleToStrongPointer<RpcServer>(handle)->shutdown(); |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void ARpcServer_free(ARpcServer* handle) { |
David Brazdil | 4766a1f | 2022-12-19 21:58:25 +0000 | [diff] [blame] | 175 | // 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 Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 178 | freeObjectHandle<RpcServer>(handle); |
Inseob Kim | 172473f | 2021-09-07 21:01:33 +0900 | [diff] [blame] | 179 | } |
| 180 | |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 181 | ARpcSession* ARpcSession_new() { |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 182 | auto session = RpcSession::make(); |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 183 | return createObjectHandle<ARpcSession>(session); |
| 184 | } |
| 185 | |
| 186 | void ARpcSession_free(ARpcSession* handle) { |
| 187 | freeObjectHandle<RpcSession>(handle); |
| 188 | } |
| 189 | |
| 190 | AIBinder* ARpcSession_setupVsockClient(ARpcSession* handle, unsigned int cid, unsigned int port) { |
| 191 | auto session = handleToStrongPointer<RpcSession>(handle); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 192 | if (status_t status = session->setupVsockClient(cid, port); status != OK) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 193 | ALOGE("Failed to set up vsock client with CID %u and port %u error: %s", cid, port, |
| 194 | statusToString(status).c_str()); |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 195 | return nullptr; |
| 196 | } |
| 197 | return AIBinder_fromPlatformBinder(session->getRootObject()); |
| 198 | } |
Inseob Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 199 | |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 200 | AIBinder* ARpcSession_setupUnixDomainClient(ARpcSession* handle, const char* name) { |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 201 | std::string pathname(name); |
| 202 | pathname = ANDROID_SOCKET_DIR "/" + pathname; |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 203 | auto session = handleToStrongPointer<RpcSession>(handle); |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 204 | if (status_t status = session->setupUnixDomainClient(pathname.c_str()); status != OK) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 205 | ALOGE("Failed to set up Unix Domain RPC client with path: %s error: %s", pathname.c_str(), |
| 206 | statusToString(status).c_str()); |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 207 | return nullptr; |
| 208 | } |
| 209 | return AIBinder_fromPlatformBinder(session->getRootObject()); |
| 210 | } |
| 211 | |
David Brazdil | e1bd983 | 2022-12-14 20:22:09 +0000 | [diff] [blame] | 212 | AIBinder* ARpcSession_setupUnixDomainBootstrapClient(ARpcSession* handle, int bootstrapFd) { |
| 213 | auto session = handleToStrongPointer<RpcSession>(handle); |
| 214 | auto fd = unique_fd(dup(bootstrapFd)); |
| 215 | if (!fd.ok()) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 216 | ALOGE("Invalid bootstrap fd %d", bootstrapFd); |
David Brazdil | e1bd983 | 2022-12-14 20:22:09 +0000 | [diff] [blame] | 217 | return nullptr; |
| 218 | } |
| 219 | if (status_t status = session->setupUnixDomainSocketBootstrapClient(std::move(fd)); |
| 220 | status != OK) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 221 | ALOGE("Failed to set up Unix Domain RPC client with bootstrap fd: %d error: %s", |
| 222 | bootstrapFd, statusToString(status).c_str()); |
David Brazdil | e1bd983 | 2022-12-14 20:22:09 +0000 | [diff] [blame] | 223 | return nullptr; |
| 224 | } |
| 225 | return AIBinder_fromPlatformBinder(session->getRootObject()); |
| 226 | } |
| 227 | |
Yiming Jing | 3463706 | 2023-02-05 14:05:32 -0800 | [diff] [blame] | 228 | AIBinder* 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 Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 231 | ALOGE("Failed to set up inet RPC client with address %s and port %u error: %s", address, |
| 232 | port, statusToString(status).c_str()); |
Yiming Jing | 3463706 | 2023-02-05 14:05:32 -0800 | [diff] [blame] | 233 | return nullptr; |
| 234 | } |
| 235 | return AIBinder_fromPlatformBinder(session->getRootObject()); |
| 236 | } |
| 237 | |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 238 | AIBinder* ARpcSession_setupPreconnectedClient(ARpcSession* handle, int (*requestFd)(void* param), |
| 239 | void* param) { |
| 240 | auto session = handleToStrongPointer<RpcSession>(handle); |
Inseob Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 241 | auto request = [=] { return unique_fd{requestFd(param)}; }; |
| 242 | if (status_t status = session->setupPreconnectedClient(unique_fd{}, request); status != OK) { |
Tomasz Wasilczyk | e3de880 | 2023-11-01 11:05:27 -0700 | [diff] [blame] | 243 | ALOGE("Failed to set up vsock client. error: %s", statusToString(status).c_str()); |
Inseob Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 244 | return nullptr; |
| 245 | } |
| 246 | return AIBinder_fromPlatformBinder(session->getRootObject()); |
| 247 | } |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 248 | |
| 249 | void ARpcSession_setFileDescriptorTransportMode(ARpcSession* handle, |
| 250 | ARpcSession_FileDescriptorTransportMode mode) { |
| 251 | auto session = handleToStrongPointer<RpcSession>(handle); |
| 252 | session->setFileDescriptorTransportMode(toTransportMode(mode)); |
| 253 | } |
| 254 | |
| 255 | void ARpcSession_setMaxIncomingThreads(ARpcSession* handle, size_t threads) { |
| 256 | auto session = handleToStrongPointer<RpcSession>(handle); |
| 257 | session->setMaxIncomingThreads(threads); |
| 258 | } |
| 259 | |
Steven Moreland | e65f73c | 2023-03-04 01:34:50 +0000 | [diff] [blame] | 260 | void ARpcSession_setMaxOutgoingConnections(ARpcSession* handle, size_t connections) { |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 261 | auto session = handleToStrongPointer<RpcSession>(handle); |
Steven Moreland | e65f73c | 2023-03-04 01:34:50 +0000 | [diff] [blame] | 262 | session->setMaxOutgoingConnections(connections); |
David Brazdil | ae3ec6a | 2022-12-14 13:13:32 +0000 | [diff] [blame] | 263 | } |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 264 | } |