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 | |
| 17 | #include <android-base/logging.h> |
Inseob Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 18 | #include <android-base/unique_fd.h> |
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> |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 22 | #include <cutils/sockets.h> |
Inseob Kim | 091050a | 2021-10-25 14:25:25 +0000 | [diff] [blame] | 23 | #include <linux/vm_sockets.h> |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 24 | |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 25 | using android::OK; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 26 | using android::RpcServer; |
| 27 | using android::RpcSession; |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 28 | using android::status_t; |
| 29 | using android::statusToString; |
Inseob Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 30 | using android::base::unique_fd; |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 31 | |
| 32 | extern "C" { |
| 33 | |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 34 | void RunRpcServer(android::sp<RpcServer>& server, AIBinder* service, |
| 35 | void (*readyCallback)(void* param), void* param) { |
| 36 | server->setRootObject(AIBinder_toPlatformBinder(service)); |
| 37 | |
| 38 | if (readyCallback) readyCallback(param); |
| 39 | server->join(); |
| 40 | |
| 41 | // Shutdown any open sessions since server failed. |
| 42 | (void)server->shutdown(); |
| 43 | } |
| 44 | |
Alice Wang | 7e93560 | 2022-10-17 08:06:16 +0000 | [diff] [blame] | 45 | bool RunVsockRpcServerWithFactory(AIBinder* (*factory)(unsigned int cid, void* context), |
| 46 | void* factoryContext, unsigned int port) { |
Inseob Kim | 091050a | 2021-10-25 14:25:25 +0000 | [diff] [blame] | 47 | auto server = RpcServer::make(); |
| 48 | if (status_t status = server->setupVsockServer(port); status != OK) { |
| 49 | LOG(ERROR) << "Failed to set up vsock server with port " << port |
| 50 | << " error: " << statusToString(status).c_str(); |
| 51 | return false; |
| 52 | } |
Andrei Homescu | 86124ca | 2022-04-21 22:22:48 +0000 | [diff] [blame] | 53 | server->setPerSessionRootObject([=](const void* addr, size_t addrlen) { |
Inseob Kim | 091050a | 2021-10-25 14:25:25 +0000 | [diff] [blame] | 54 | LOG_ALWAYS_FATAL_IF(addrlen < sizeof(sockaddr_vm), "sockaddr is truncated"); |
| 55 | const sockaddr_vm* vaddr = reinterpret_cast<const sockaddr_vm*>(addr); |
Andrei Homescu | 86124ca | 2022-04-21 22:22:48 +0000 | [diff] [blame] | 56 | 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] | 57 | return AIBinder_toPlatformBinder(factory(vaddr->svm_cid, factoryContext)); |
| 58 | }); |
| 59 | |
| 60 | server->join(); |
| 61 | |
| 62 | // Shutdown any open sessions since server failed. |
| 63 | (void)server->shutdown(); |
| 64 | return true; |
| 65 | } |
| 66 | |
Alice Wang | 7e93560 | 2022-10-17 08:06:16 +0000 | [diff] [blame] | 67 | bool RunVsockRpcServerCallback(AIBinder* service, unsigned int port, |
| 68 | void (*readyCallback)(void* param), void* param) { |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 69 | auto server = RpcServer::make(); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 70 | if (status_t status = server->setupVsockServer(port); status != OK) { |
| 71 | LOG(ERROR) << "Failed to set up vsock server with port " << port |
| 72 | << " error: " << statusToString(status).c_str(); |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 73 | return false; |
| 74 | } |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 75 | RunRpcServer(server, service, readyCallback, param); |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 76 | return true; |
| 77 | } |
| 78 | |
Alice Wang | 7e93560 | 2022-10-17 08:06:16 +0000 | [diff] [blame] | 79 | bool RunVsockRpcServer(AIBinder* service, unsigned int port) { |
| 80 | return RunVsockRpcServerCallback(service, port, nullptr, nullptr); |
Inseob Kim | 172473f | 2021-09-07 21:01:33 +0900 | [diff] [blame] | 81 | } |
| 82 | |
Alice Wang | 0746668 | 2022-10-19 15:28:29 +0000 | [diff] [blame] | 83 | AIBinder* VsockRpcClient(unsigned int cid, unsigned int port) { |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 84 | auto session = RpcSession::make(); |
Steven Moreland | 2372f9d | 2021-08-05 15:42:01 -0700 | [diff] [blame] | 85 | if (status_t status = session->setupVsockClient(cid, port); status != OK) { |
| 86 | LOG(ERROR) << "Failed to set up vsock client with CID " << cid << " and port " << port |
| 87 | << " error: " << statusToString(status).c_str(); |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 88 | return nullptr; |
| 89 | } |
| 90 | return AIBinder_fromPlatformBinder(session->getRootObject()); |
| 91 | } |
Inseob Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 92 | |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 93 | bool RunInitUnixDomainRpcServer(AIBinder* service, const char* name, |
| 94 | void (*readyCallback)(void* param), void* param) { |
| 95 | auto server = RpcServer::make(); |
| 96 | auto fd = unique_fd(android_get_control_socket(name)); |
Alice Wang | 30c204d | 2022-11-15 12:50:51 +0000 | [diff] [blame] | 97 | if (!fd.ok()) { |
| 98 | LOG(ERROR) << "Failed to get fd for the socket:" << name; |
| 99 | return false; |
| 100 | } |
Alice Wang | 893a991 | 2022-10-24 10:44:09 +0000 | [diff] [blame] | 101 | if (status_t status = server->setupRawSocketServer(std::move(fd)); status != OK) { |
| 102 | LOG(ERROR) << "Failed to set up Unix Domain RPC server with name " << name |
| 103 | << " error: " << statusToString(status).c_str(); |
| 104 | return false; |
| 105 | } |
| 106 | RunRpcServer(server, service, readyCallback, param); |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | AIBinder* UnixDomainRpcClient(const char* name) { |
| 111 | std::string pathname(name); |
| 112 | pathname = ANDROID_SOCKET_DIR "/" + pathname; |
| 113 | auto session = RpcSession::make(); |
| 114 | if (status_t status = session->setupUnixDomainClient(pathname.c_str()); status != OK) { |
| 115 | LOG(ERROR) << "Failed to set up Unix Domain RPC client with path: " << pathname |
| 116 | << " error: " << statusToString(status).c_str(); |
| 117 | return nullptr; |
| 118 | } |
| 119 | return AIBinder_fromPlatformBinder(session->getRootObject()); |
| 120 | } |
| 121 | |
Inseob Kim | e12fccc | 2021-09-02 17:23:33 +0900 | [diff] [blame] | 122 | AIBinder* RpcPreconnectedClient(int (*requestFd)(void* param), void* param) { |
| 123 | auto session = RpcSession::make(); |
| 124 | auto request = [=] { return unique_fd{requestFd(param)}; }; |
| 125 | if (status_t status = session->setupPreconnectedClient(unique_fd{}, request); status != OK) { |
| 126 | LOG(ERROR) << "Failed to set up vsock client. error: " << statusToString(status).c_str(); |
| 127 | return nullptr; |
| 128 | } |
| 129 | return AIBinder_fromPlatformBinder(session->getRootObject()); |
| 130 | } |
Victor Hsieh | d35d31d | 2021-06-03 11:24:31 -0700 | [diff] [blame] | 131 | } |