blob: 9edb3b6f9997590e9f40650db91e02fdc2e6692d [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
17#include <android-base/logging.h>
Inseob Kime12fccc2021-09-02 17:23:33 +090018#include <android-base/unique_fd.h>
Victor Hsiehd35d31d2021-06-03 11:24:31 -070019#include <android/binder_libbinder.h>
20#include <binder/RpcServer.h>
21#include <binder/RpcSession.h>
Alice Wang893a9912022-10-24 10:44:09 +000022#include <cutils/sockets.h>
Inseob Kim091050a2021-10-25 14:25:25 +000023#include <linux/vm_sockets.h>
Victor Hsiehd35d31d2021-06-03 11:24:31 -070024
Steven Moreland2372f9d2021-08-05 15:42:01 -070025using android::OK;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070026using android::RpcServer;
27using android::RpcSession;
Steven Moreland2372f9d2021-08-05 15:42:01 -070028using android::status_t;
29using android::statusToString;
Inseob Kime12fccc2021-09-02 17:23:33 +090030using android::base::unique_fd;
Victor Hsiehd35d31d2021-06-03 11:24:31 -070031
32extern "C" {
33
Alice Wang893a9912022-10-24 10:44:09 +000034void 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 Wang7e935602022-10-17 08:06:16 +000045bool RunVsockRpcServerWithFactory(AIBinder* (*factory)(unsigned int cid, void* context),
46 void* factoryContext, unsigned int port) {
Inseob Kim091050a2021-10-25 14:25:25 +000047 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 Homescu86124ca2022-04-21 22:22:48 +000053 server->setPerSessionRootObject([=](const void* addr, size_t addrlen) {
Inseob Kim091050a2021-10-25 14:25:25 +000054 LOG_ALWAYS_FATAL_IF(addrlen < sizeof(sockaddr_vm), "sockaddr is truncated");
55 const sockaddr_vm* vaddr = reinterpret_cast<const sockaddr_vm*>(addr);
Andrei Homescu86124ca2022-04-21 22:22:48 +000056 LOG_ALWAYS_FATAL_IF(vaddr->svm_family != AF_VSOCK, "address is not a vsock");
Inseob Kim091050a2021-10-25 14:25:25 +000057 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 Wang7e935602022-10-17 08:06:16 +000067bool RunVsockRpcServerCallback(AIBinder* service, unsigned int port,
68 void (*readyCallback)(void* param), void* param) {
Victor Hsiehd35d31d2021-06-03 11:24:31 -070069 auto server = RpcServer::make();
Steven Moreland2372f9d2021-08-05 15:42:01 -070070 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 Hsiehd35d31d2021-06-03 11:24:31 -070073 return false;
74 }
Alice Wang893a9912022-10-24 10:44:09 +000075 RunRpcServer(server, service, readyCallback, param);
Victor Hsiehd35d31d2021-06-03 11:24:31 -070076 return true;
77}
78
Alice Wang7e935602022-10-17 08:06:16 +000079bool RunVsockRpcServer(AIBinder* service, unsigned int port) {
80 return RunVsockRpcServerCallback(service, port, nullptr, nullptr);
Inseob Kim172473f2021-09-07 21:01:33 +090081}
82
Alice Wang07466682022-10-19 15:28:29 +000083AIBinder* VsockRpcClient(unsigned int cid, unsigned int port) {
Victor Hsiehd35d31d2021-06-03 11:24:31 -070084 auto session = RpcSession::make();
Steven Moreland2372f9d2021-08-05 15:42:01 -070085 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 Hsiehd35d31d2021-06-03 11:24:31 -070088 return nullptr;
89 }
90 return AIBinder_fromPlatformBinder(session->getRootObject());
91}
Inseob Kime12fccc2021-09-02 17:23:33 +090092
Alice Wang893a9912022-10-24 10:44:09 +000093bool 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 Wang30c204d2022-11-15 12:50:51 +000097 if (!fd.ok()) {
98 LOG(ERROR) << "Failed to get fd for the socket:" << name;
99 return false;
100 }
Alice Wang893a9912022-10-24 10:44:09 +0000101 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
110AIBinder* 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 Kime12fccc2021-09-02 17:23:33 +0900122AIBinder* 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 Hsiehd35d31d2021-06-03 11:24:31 -0700131}