[rpc_binder] Implement RPC binder over init-managed Unix domain socket
This implements an RPC binder over init-managed Unix domain sockets.
The cl adds binder tests and the new API is also used for
vm_payload_service inside Microdroid.
A previous cl aosp/2229557 sets up the binder over anonymous Unix sockets.
Test: atest MicrodroidTests ComposHostTestCases
Bug: 222479468
Change-Id: I0c7c38f4792c4536f5f88eb7e035091505f782f7
diff --git a/libs/binder/libbinder_rpc_unstable.cpp b/libs/binder/libbinder_rpc_unstable.cpp
index 1f38bb9..ae07aee 100644
--- a/libs/binder/libbinder_rpc_unstable.cpp
+++ b/libs/binder/libbinder_rpc_unstable.cpp
@@ -19,6 +19,7 @@
#include <android/binder_libbinder.h>
#include <binder/RpcServer.h>
#include <binder/RpcSession.h>
+#include <cutils/sockets.h>
#include <linux/vm_sockets.h>
using android::OK;
@@ -30,6 +31,17 @@
extern "C" {
+void RunRpcServer(android::sp<RpcServer>& server, AIBinder* service,
+ void (*readyCallback)(void* param), void* param) {
+ server->setRootObject(AIBinder_toPlatformBinder(service));
+
+ if (readyCallback) readyCallback(param);
+ server->join();
+
+ // Shutdown any open sessions since server failed.
+ (void)server->shutdown();
+}
+
bool RunVsockRpcServerWithFactory(AIBinder* (*factory)(unsigned int cid, void* context),
void* factoryContext, unsigned int port) {
auto server = RpcServer::make();
@@ -60,13 +72,7 @@
<< " error: " << statusToString(status).c_str();
return false;
}
- server->setRootObject(AIBinder_toPlatformBinder(service));
-
- if (readyCallback) readyCallback(param);
- server->join();
-
- // Shutdown any open sessions since server failed.
- (void)server->shutdown();
+ RunRpcServer(server, service, readyCallback, param);
return true;
}
@@ -84,6 +90,31 @@
return AIBinder_fromPlatformBinder(session->getRootObject());
}
+bool RunInitUnixDomainRpcServer(AIBinder* service, const char* name,
+ void (*readyCallback)(void* param), void* param) {
+ auto server = RpcServer::make();
+ auto fd = unique_fd(android_get_control_socket(name));
+ if (status_t status = server->setupRawSocketServer(std::move(fd)); status != OK) {
+ LOG(ERROR) << "Failed to set up Unix Domain RPC server with name " << name
+ << " error: " << statusToString(status).c_str();
+ return false;
+ }
+ RunRpcServer(server, service, readyCallback, param);
+ return true;
+}
+
+AIBinder* UnixDomainRpcClient(const char* name) {
+ std::string pathname(name);
+ pathname = ANDROID_SOCKET_DIR "/" + pathname;
+ auto session = RpcSession::make();
+ if (status_t status = session->setupUnixDomainClient(pathname.c_str()); status != OK) {
+ LOG(ERROR) << "Failed to set up Unix Domain RPC client with path: " << pathname
+ << " error: " << statusToString(status).c_str();
+ return nullptr;
+ }
+ return AIBinder_fromPlatformBinder(session->getRootObject());
+}
+
AIBinder* RpcPreconnectedClient(int (*requestFd)(void* param), void* param) {
auto session = RpcSession::make();
auto request = [=] { return unique_fd{requestFd(param)}; };