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-base/logging.h> |
Inseob Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 20 | #include <android-base/unique_fd.h> |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 21 | #include <android/binder_libbinder.h> |
| 22 | #include <binder/RpcServer.h> |
| 23 | #include <binder/RpcSession.h> |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 24 | #include <cutils/sockets.h> |
Inseob Kim | 091050a | 2021-10-25 14:25:25 +0000 | [diff] [blame] | 25 | #include <linux/vm_sockets.h> |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 26 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 27 | using android::OK; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 28 | using android::RpcServer; |
| 29 | using android::RpcSession; |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 30 | using android::sp; |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 31 | using android::status_t; |
| 32 | using android::statusToString; |
Inseob Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 33 | using android::base::unique_fd; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 34 | |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 35 | // Opaque handle for RpcServer. |
| 36 | struct ARpcServer {}; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 37 | |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 38 | static sp<RpcServer> toRpcServer(ARpcServer* handle) { |
| 39 | auto ref = reinterpret_cast<RpcServer*>(handle); |
| 40 | return sp<RpcServer>::fromExisting(ref); |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 41 | } |
| 42 | |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 43 | static ARpcServer* createRpcServerHandle(sp<RpcServer>& server) { |
| 44 | auto ref = server.get(); |
| 45 | ref->incStrong(ref); |
| 46 | return reinterpret_cast<ARpcServer*>(ref); |
| 47 | } |
| 48 | |
| 49 | static void freeRpcServerHandle(ARpcServer* handle) { |
| 50 | auto ref = reinterpret_cast<RpcServer*>(handle); |
| 51 | ref->decStrong(ref); |
| 52 | } |
| 53 | |
| 54 | extern "C" { |
| 55 | |
Alice Wang | 7e93560 | 2022-10-17 08:06:16 +0000 | [diff] [blame] | 56 | bool RunVsockRpcServerWithFactory(AIBinder* (*factory)(unsigned int cid, void* context), |
| 57 | void* factoryContext, unsigned int port) { |
Inseob Kim | 091050a | 2021-10-25 14:25:25 +0000 | [diff] [blame] | 58 | 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 Homescu | 86124ca | 2022-04-21 22:22:48 +0000 | [diff] [blame] | 64 | server->setPerSessionRootObject([=](const void* addr, size_t addrlen) { |
Inseob Kim | 091050a | 2021-10-25 14:25:25 +0000 | [diff] [blame] | 65 | LOG_ALWAYS_FATAL_IF(addrlen < sizeof(sockaddr_vm), "sockaddr is truncated"); |
| 66 | const sockaddr_vm* vaddr = reinterpret_cast<const sockaddr_vm*>(addr); |
Andrei Homescu | 86124ca | 2022-04-21 22:22:48 +0000 | [diff] [blame] | 67 | LOG_ALWAYS_FATAL_IF(vaddr->svm_family != AF_VSOCK, "address is not a vsock"); |
Inseob Kim | 091050a | 2021-10-25 14:25:25 +0000 | [diff] [blame] | 68 | 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 Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 78 | ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int port) { |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 79 | auto server = RpcServer::make(); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 80 | 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 Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 83 | return nullptr; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 84 | } |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 85 | server->setRootObject(AIBinder_toPlatformBinder(service)); |
| 86 | return createRpcServerHandle(server); |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 87 | } |
| 88 | |
David Brazdil | efb5683 | 2022-11-16 11:47:00 +0000 | [diff] [blame] | 89 | ARpcServer* 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 | |
| 105 | void ARpcServer_start(ARpcServer* handle) { |
| 106 | toRpcServer(handle)->start(); |
| 107 | } |
| 108 | |
| 109 | void ARpcServer_join(ARpcServer* handle) { |
| 110 | toRpcServer(handle)->join(); |
| 111 | } |
| 112 | |
| 113 | void ARpcServer_shutdown(ARpcServer* handle) { |
| 114 | toRpcServer(handle)->shutdown(); |
| 115 | } |
| 116 | |
| 117 | void ARpcServer_free(ARpcServer* handle) { |
| 118 | freeRpcServerHandle(handle); |
Inseob Kim | 172473f | 2021-09-07 21:01:33 +0900 | [diff] [blame] | 119 | } |
| 120 | |
Alice Wang | 0746668 | 2022-10-19 15:28:29 +0000 | [diff] [blame] | 121 | AIBinder* VsockRpcClient(unsigned int cid, unsigned int port) { |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 122 | auto session = RpcSession::make(); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 123 | 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 Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 126 | return nullptr; |
| 127 | } |
| 128 | return AIBinder_fromPlatformBinder(session->getRootObject()); |
| 129 | } |
Inseob Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 130 | |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 131 | AIBinder* 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 Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 143 | AIBinder* 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 Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 152 | } |