From 088067566a0f2296cb8e78e0d261cc89f21f21fa Mon Sep 17 00:00:00 2001 From: Nicole LeGare Date: Thu, 13 Feb 2025 16:24:01 -0800 Subject: [PATCH] Do the more complex unix socket setup so we can set the fd transport options --- src/android/aidl/birthday_service/Android.bp | 2 ++ .../aidl/birthday_service/src/client.rs | 21 +++++++++++++++---- .../aidl/birthday_service/src/server.rs | 17 +++++++++++---- src/android/interoperability/java/Android.bp | 2 +- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/src/android/aidl/birthday_service/Android.bp b/src/android/aidl/birthday_service/Android.bp index e4f6d8e2..2ee81e15 100644 --- a/src/android/aidl/birthday_service/Android.bp +++ b/src/android/aidl/birthday_service/Android.bp @@ -18,6 +18,7 @@ rust_binary { rustlibs: [ "com.example.birthdayservice-rust", "libbinder_rs", + "librpcbinder_rs", "libbirthdayservice", ], prefer_rlib: true, // To avoid dynamic link error. @@ -32,6 +33,7 @@ rust_binary { rustlibs: [ "com.example.birthdayservice-rust", "libbinder_rs", + "librpcbinder_rs", ], prefer_rlib: true, // To avoid dynamic link error. } diff --git a/src/android/aidl/birthday_service/src/client.rs b/src/android/aidl/birthday_service/src/client.rs index be9bbfcd..c3c3ad7a 100644 --- a/src/android/aidl/birthday_service/src/client.rs +++ b/src/android/aidl/birthday_service/src/client.rs @@ -19,12 +19,15 @@ use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayI }; use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayService::IBirthdayService; use com_example_birthdayservice::binder::{self, BinderFeatures, ParcelFileDescriptor}; +use rpcbinder::{FileDescriptorTransportMode, RpcSession}; use std::error::Error; use std::fs::File; use std::io::prelude::*; +use std::os::unix::net::UnixStream; +use std::os::fd::AsRawFd as _; // ANCHOR: main -const SERVICE_IDENTIFIER: &str = "birthdayservice"; +const SERVICE_IDENTIFIER: &str = "/tmp/birthdayservice"; /// Call the birthday service. fn main() -> Result<(), Box> { @@ -34,9 +37,19 @@ fn main() -> Result<(), Box> { .and_then(|arg| arg.parse::().ok()) .unwrap_or(42); - binder::ProcessState::start_thread_pool(); - let service = binder::get_interface::(SERVICE_IDENTIFIER) - .map_err(|_| "Failed to connect to BirthdayService")?; + let stream = UnixStream::connect(SERVICE_IDENTIFIER).expect("Failed to connect UnixStream"); + println!("UnixStream connected successfully"); + + // binder::ProcessState::start_thread_pool(); + // let service = binder::get_interface::(SERVICE_IDENTIFIER) + // .map_err(|_| "Failed to connect to BirthdayService")?; + + let session = RpcSession::new(); + session.set_file_descriptor_transport_mode(FileDescriptorTransportMode::Unix); + let service = session + // .setup_unix_domain_client::(SERVICE_IDENTIFIER) + .setup_preconnected_client::(|| Some(stream.as_raw_fd())) + .map_err(|e| format!("Failed to connect to BirthdayService: {e}"))?; // Call the service. let msg = service.wishHappyBirthday(&name, years)?; diff --git a/src/android/aidl/birthday_service/src/server.rs b/src/android/aidl/birthday_service/src/server.rs index 291b11ae..922d529b 100644 --- a/src/android/aidl/birthday_service/src/server.rs +++ b/src/android/aidl/birthday_service/src/server.rs @@ -17,8 +17,10 @@ use birthdayservice::BirthdayService; use com_example_birthdayservice::aidl::com::example::birthdayservice::IBirthdayService::BnBirthdayService; use com_example_birthdayservice::binder; +use rpcbinder::{FileDescriptorTransportMode, RpcServer}; +use std::os::unix::net::UnixStream; -const SERVICE_IDENTIFIER: &str = "birthdayservice"; +const SERVICE_IDENTIFIER: &str = "/tmp/birthdayservice"; /// Entry point for birthday service. fn main() { @@ -27,7 +29,14 @@ fn main() { birthday_service, binder::BinderFeatures::default(), ); - binder::add_service(SERVICE_IDENTIFIER, birthday_service_binder.as_binder()) - .expect("Failed to register service"); - binder::ProcessState::join_thread_pool(); + // binder::add_service(SERVICE_IDENTIFIER, birthday_service_binder.as_binder()) + // .expect("Failed to register service"); + // binder::ProcessState::join_thread_pool(); + + std::fs::write(SERVICE_IDENTIFIER, "").expect("Failed to create socket file"); + let socket = UnixStream::connect(SERVICE_IDENTIFIER).expect("Failed to create socket"); + // let socket = UnixListener::bind(SERVICE_IDENTIFIER).expect("Failed to create socket"); + let server = RpcServer::new_bound_socket(birthday_service_binder.as_binder(), socket.into()).expect("Failed to create server"); + server.set_supported_file_descriptor_transport_modes(&[FileDescriptorTransportMode::Unix]); + server.join(); } diff --git a/src/android/interoperability/java/Android.bp b/src/android/interoperability/java/Android.bp index 75f10491..89b099cd 100644 --- a/src/android/interoperability/java/Android.bp +++ b/src/android/interoperability/java/Android.bp @@ -12,6 +12,6 @@ java_binary { name: "helloworld_jni", srcs: ["HelloWorld.java"], main_class: "HelloWorld", - required: ["libhello_jni"], + jni_libs: ["libhello_jni"], } // ANCHOR_END: helloworld_jni