Moved binder_common to rpcbinder package under binder directory.

Bug: 234019127
Bug: 184872979
Test: atest compos_key_tests MicrodroidHostTestCases MicrodroidTestApp libbinder_rs-internal_test
Change-Id: Iceac394d2341f463362e9bbe8908e5d8a1b3f34f
diff --git a/authfs/Android.bp b/authfs/Android.bp
index cb7f119..4c5b70e 100644
--- a/authfs/Android.bp
+++ b/authfs/Android.bp
@@ -14,7 +14,6 @@
         "libandroid_logger",
         "libanyhow",
         "libauthfs_fsverity_metadata",
-        "libbinder_common",
         "libbinder_rs",
         "libcfg_if",
         "libfsverity_digests_proto_rust",
@@ -24,6 +23,7 @@
         "libnix",
         "libopenssl",
         "libprotobuf",
+        "librpcbinder_rs",
         "libstructopt",
         "libthiserror",
     ],
diff --git a/authfs/fd_server/Android.bp b/authfs/fd_server/Android.bp
index 943eec1..44407a2 100644
--- a/authfs/fd_server/Android.bp
+++ b/authfs/fd_server/Android.bp
@@ -10,12 +10,12 @@
         "libandroid_logger",
         "libanyhow",
         "libauthfs_fsverity_metadata",
-        "libbinder_common",
         "libbinder_rs",
         "libclap",
         "liblibc",
         "liblog_rust",
         "libnix",
+        "librpcbinder_rs",
     ],
     prefer_rlib: true,
     shared_libs: [
diff --git a/authfs/fd_server/src/main.rs b/authfs/fd_server/src/main.rs
index a1d09fc..23a76e2 100644
--- a/authfs/fd_server/src/main.rs
+++ b/authfs/fd_server/src/main.rs
@@ -27,9 +27,9 @@
 mod fsverity;
 
 use anyhow::{bail, Result};
-use binder_common::rpc_server::run_rpc_server;
 use log::debug;
 use nix::sys::stat::{umask, Mode};
+use rpcbinder::run_rpc_server;
 use std::collections::BTreeMap;
 use std::fs::File;
 use std::os::unix::io::FromRawFd;
diff --git a/authfs/src/file.rs b/authfs/src/file.rs
index df52a0e..aff47c5 100644
--- a/authfs/src/file.rs
+++ b/authfs/src/file.rs
@@ -9,7 +9,7 @@
 use crate::common::{divide_roundup, CHUNK_SIZE};
 use authfs_aidl_interface::aidl::com::android::virt::fs::IVirtFdService::IVirtFdService;
 use binder::{Status, StatusCode, Strong};
-use binder_common::rpc_client::connect_rpc_binder;
+use rpcbinder::get_vsock_rpc_interface;
 use std::convert::TryFrom;
 use std::io;
 use std::path::{Path, MAIN_SEPARATOR};
@@ -22,7 +22,7 @@
 pub const RPC_SERVICE_PORT: u32 = 3264;
 
 pub fn get_rpc_binder_service(cid: u32) -> io::Result<VirtFdService> {
-    connect_rpc_binder(cid, RPC_SERVICE_PORT).map_err(|e| match e {
+    get_vsock_rpc_interface(cid, RPC_SERVICE_PORT).map_err(|e| match e {
         StatusCode::BAD_VALUE => {
             io::Error::new(io::ErrorKind::InvalidInput, "Invalid raw AIBinder")
         }
diff --git a/compos/Android.bp b/compos/Android.bp
index 0f1675b..ec2d737 100644
--- a/compos/Android.bp
+++ b/compos/Android.bp
@@ -11,7 +11,6 @@
         "compos_aidl_interface-rust",
         "libandroid_logger",
         "libanyhow",
-        "libbinder_common",
         "libbinder_rs",
         "libclap",
         "libcompos_common",
@@ -22,6 +21,7 @@
         "libodsign_proto_rust",
         "libprotobuf",
         "libregex",
+        "librpcbinder_rs",
         "librustutils",
         "libscopeguard",
     ],
diff --git a/compos/composd/Android.bp b/compos/composd/Android.bp
index 3a6119f..937aa1a 100644
--- a/compos/composd/Android.bp
+++ b/compos/composd/Android.bp
@@ -13,7 +13,6 @@
         "compos_aidl_interface-rust",
         "libandroid_logger",
         "libanyhow",
-        "libbinder_common",
         "libbinder_rs",
         "libcompos_common",
         "libcomposd_native_rust",
diff --git a/compos/src/compsvc_main.rs b/compos/src/compsvc_main.rs
index 186977e..16d258e 100644
--- a/compos/src/compsvc_main.rs
+++ b/compos/src/compsvc_main.rs
@@ -29,9 +29,9 @@
     binder::Strong,
 };
 use anyhow::{bail, Context, Result};
-use binder_common::{rpc_client::connect_rpc_binder, rpc_server::run_rpc_server};
 use compos_common::COMPOS_VSOCK_PORT;
 use log::{debug, error};
+use rpcbinder::{get_vsock_rpc_interface, run_rpc_server};
 use std::panic;
 
 /// The CID representing the host VM
@@ -72,6 +72,6 @@
 }
 
 fn get_vm_service() -> Result<Strong<dyn IVirtualMachineService>> {
-    connect_rpc_binder(VMADDR_CID_HOST, VM_BINDER_SERVICE_PORT as u32)
+    get_vsock_rpc_interface(VMADDR_CID_HOST, VM_BINDER_SERVICE_PORT as u32)
         .context("Connecting to IVirtualMachineService")
 }
diff --git a/libs/binder_common/Android.bp b/libs/binder_common/Android.bp
deleted file mode 100644
index 47a2f21..0000000
--- a/libs/binder_common/Android.bp
+++ /dev/null
@@ -1,18 +0,0 @@
-package {
-    default_applicable_licenses: ["Android-Apache-2.0"],
-}
-
-rust_library {
-    name: "libbinder_common",
-    crate_name: "binder_common",
-    srcs: ["lib.rs"],
-    edition: "2018",
-    rustlibs: [
-        "libbinder_rs",
-        "libbinder_rpc_unstable_bindgen",
-    ],
-    apex_available: [
-        "com.android.compos",
-        "com.android.virt",
-    ],
-}
diff --git a/libs/binder_common/lib.rs b/libs/binder_common/lib.rs
deleted file mode 100644
index 14dd9f2..0000000
--- a/libs/binder_common/lib.rs
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-//! Common items useful for binder clients and/or servers.
-
-pub mod rpc_client;
-pub mod rpc_server;
diff --git a/libs/binder_common/rpc_client.rs b/libs/binder_common/rpc_client.rs
deleted file mode 100644
index 33fd732..0000000
--- a/libs/binder_common/rpc_client.rs
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-//! Helpers for implementing an RPC Binder client.
-
-use binder::unstable_api::{new_spibinder, AIBinder};
-use binder::{FromIBinder, StatusCode, Strong};
-use std::os::{raw, unix::io::RawFd};
-
-/// Connects to a binder RPC server.
-pub fn connect_rpc_binder<T: FromIBinder + ?Sized>(
-    cid: u32,
-    port: u32,
-) -> Result<Strong<T>, StatusCode> {
-    // SAFETY: AIBinder returned by RpcClient has correct reference count, and the ownership can be
-    // safely taken by new_spibinder.
-    let ibinder = unsafe {
-        new_spibinder(binder_rpc_unstable_bindgen::RpcClient(cid, port) as *mut AIBinder)
-    };
-    if let Some(ibinder) = ibinder {
-        <T>::try_from(ibinder)
-    } else {
-        Err(StatusCode::BAD_VALUE)
-    }
-}
-
-type RequestFd<'a> = &'a mut dyn FnMut() -> Option<RawFd>;
-
-/// Connects to a Binder RPC server, using the given callback to get (and take ownership of) file
-/// descriptors already connected to it.
-pub fn connect_preconnected_rpc_binder<T: FromIBinder + ?Sized>(
-    mut request_fd: impl FnMut() -> Option<RawFd>,
-) -> Result<Strong<T>, StatusCode> {
-    // Double reference the factory because trait objects aren't FFI safe.
-    let mut request_fd_ref: RequestFd = &mut request_fd;
-    let param = &mut request_fd_ref as *mut RequestFd as *mut raw::c_void;
-
-    // SAFETY: AIBinder returned by RpcPreconnectedClient has correct reference count, and the
-    // ownership can be safely taken by new_spibinder. RpcPreconnectedClient does not take ownership
-    // of param, only passing it to request_fd_wrapper.
-    let ibinder = unsafe {
-        new_spibinder(binder_rpc_unstable_bindgen::RpcPreconnectedClient(
-            Some(request_fd_wrapper),
-            param,
-        ) as *mut AIBinder)
-    };
-
-    if let Some(ibinder) = ibinder {
-        <T>::try_from(ibinder)
-    } else {
-        Err(StatusCode::BAD_VALUE)
-    }
-}
-
-unsafe extern "C" fn request_fd_wrapper(param: *mut raw::c_void) -> raw::c_int {
-    // SAFETY: This is only ever called by RpcPreconnectedClient, within the lifetime of the
-    // BinderFdFactory reference, with param being a properly aligned non-null pointer to an
-    // initialized instance.
-    let request_fd_ptr = param as *mut RequestFd;
-    let request_fd = request_fd_ptr.as_mut().unwrap();
-    request_fd().unwrap_or(-1)
-}
diff --git a/libs/binder_common/rpc_server.rs b/libs/binder_common/rpc_server.rs
deleted file mode 100644
index 4261358..0000000
--- a/libs/binder_common/rpc_server.rs
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- * Copyright (C) 2021 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-//! Helpers for implementing an RPC Binder server.
-
-use binder::unstable_api::AsNative;
-use binder::SpIBinder;
-use std::{os::raw, ptr::null_mut};
-
-/// Runs a binder RPC server, serving the supplied binder service implementation on the given vsock
-/// port.
-///
-/// If and when the server is ready for connections (it is listening on the port), `on_ready` is
-/// called to allow appropriate action to be taken - e.g. to notify clients that they may now
-/// attempt to connect.
-///
-/// The current thread is joined to the binder thread pool to handle incoming messages.
-///
-/// Returns true if the server has shutdown normally, false if it failed in some way.
-pub fn run_rpc_server<F>(service: SpIBinder, port: u32, on_ready: F) -> bool
-where
-    F: FnOnce(),
-{
-    let mut ready_notifier = ReadyNotifier(Some(on_ready));
-    ready_notifier.run_server(service, port)
-}
-
-struct ReadyNotifier<F>(Option<F>)
-where
-    F: FnOnce();
-
-impl<F> ReadyNotifier<F>
-where
-    F: FnOnce(),
-{
-    fn run_server(&mut self, mut service: SpIBinder, port: u32) -> bool {
-        let service = service.as_native_mut() as *mut binder_rpc_unstable_bindgen::AIBinder;
-        let param = self.as_void_ptr();
-
-        // SAFETY: Service ownership is transferring to the server and won't be valid afterward.
-        // Plus the binder objects are threadsafe.
-        // RunRpcServerCallback does not retain a reference to ready_callback, and only ever
-        // calls it with the param we provide during the lifetime of self.
-        unsafe {
-            binder_rpc_unstable_bindgen::RunRpcServerCallback(
-                service,
-                port,
-                Some(Self::ready_callback),
-                param,
-            )
-        }
-    }
-
-    fn as_void_ptr(&mut self) -> *mut raw::c_void {
-        self as *mut _ as *mut raw::c_void
-    }
-
-    unsafe extern "C" fn ready_callback(param: *mut raw::c_void) {
-        // SAFETY: This is only ever called by RunRpcServerCallback, within the lifetime of the
-        // ReadyNotifier, with param taking the value returned by as_void_ptr (so a properly aligned
-        // non-null pointer to an initialized instance).
-        let ready_notifier = param as *mut Self;
-        ready_notifier.as_mut().unwrap().notify()
-    }
-
-    fn notify(&mut self) {
-        if let Some(on_ready) = self.0.take() {
-            on_ready();
-        }
-    }
-}
-
-type RpcServerFactoryRef<'a> = &'a mut (dyn FnMut(u32) -> Option<SpIBinder> + Send + Sync);
-
-/// Runs a binder RPC server, using the given factory function to construct a binder service
-/// implementation for each connection.
-///
-/// The current thread is joined to the binder thread pool to handle incoming messages.
-///
-/// Returns true if the server has shutdown normally, false if it failed in some way.
-pub fn run_rpc_server_with_factory(
-    port: u32,
-    mut factory: impl FnMut(u32) -> Option<SpIBinder> + Send + Sync,
-) -> bool {
-    // Double reference the factory because trait objects aren't FFI safe.
-    // NB: The type annotation is necessary to ensure that we have a `dyn` rather than an `impl`.
-    let mut factory_ref: RpcServerFactoryRef = &mut factory;
-    let context = &mut factory_ref as *mut RpcServerFactoryRef as *mut raw::c_void;
-
-    // SAFETY: `factory_wrapper` is only ever called by `RunRpcServerWithFactory`, with context
-    // taking the pointer value above (so a properly aligned non-null pointer to an initialized
-    // `RpcServerFactoryRef`), within the lifetime of `factory_ref` (i.e. no more calls will be made
-    // after `RunRpcServerWithFactory` returns).
-    unsafe {
-        binder_rpc_unstable_bindgen::RunRpcServerWithFactory(Some(factory_wrapper), context, port)
-    }
-}
-
-unsafe extern "C" fn factory_wrapper(
-    cid: u32,
-    context: *mut raw::c_void,
-) -> *mut binder_rpc_unstable_bindgen::AIBinder {
-    // SAFETY: `context` was created from an `&mut RpcServerFactoryRef` by
-    // `run_rpc_server_with_factory`, and we are still within the lifetime of the value it is
-    // pointing to.
-    let factory_ptr = context as *mut RpcServerFactoryRef;
-    let factory = factory_ptr.as_mut().unwrap();
-
-    if let Some(mut service) = factory(cid) {
-        service.as_native_mut() as *mut binder_rpc_unstable_bindgen::AIBinder
-    } else {
-        null_mut()
-    }
-}
diff --git a/microdroid_manager/Android.bp b/microdroid_manager/Android.bp
index 3ba2700..da63434 100644
--- a/microdroid_manager/Android.bp
+++ b/microdroid_manager/Android.bp
@@ -16,7 +16,6 @@
         "libanyhow",
         "libapexutil_rust",
         "libapkverify",
-        "libbinder_common",
         "libbinder_rs",
         "libbyteorder",
         "libdiced_utils",
@@ -33,6 +32,7 @@
         "libonce_cell",
         "libopenssl",
         "libprotobuf",
+        "librpcbinder_rs",
         "librustutils",
         "libscopeguard",
         "libserde",
diff --git a/microdroid_manager/src/main.rs b/microdroid_manager/src/main.rs
index 531c707..c226e9d 100644
--- a/microdroid_manager/src/main.rs
+++ b/microdroid_manager/src/main.rs
@@ -26,7 +26,6 @@
 use anyhow::{anyhow, bail, ensure, Context, Error, Result};
 use apkverify::{get_public_key_der, verify};
 use binder::{wait_for_interface, Strong};
-use binder_common::rpc_client::connect_rpc_binder;
 use diced_utils::cbor::encode_header;
 use glob::glob;
 use idsig::V4Signature;
@@ -37,6 +36,7 @@
 use openssl::sha::Sha512;
 use payload::{get_apex_data_from_payload, load_metadata, to_metadata};
 use rand::Fill;
+use rpcbinder::get_vsock_rpc_interface;
 use rustutils::system_properties;
 use rustutils::system_properties::PropertyWatcher;
 use std::convert::TryInto;
@@ -141,7 +141,7 @@
 }
 
 fn get_vms_rpc_binder() -> Result<Strong<dyn IVirtualMachineService>> {
-    connect_rpc_binder(VMADDR_CID_HOST, VM_BINDER_SERVICE_PORT as u32)
+    get_vsock_rpc_interface(VMADDR_CID_HOST, VM_BINDER_SERVICE_PORT as u32)
         .context("Cannot connect to RPC service")
 }
 
diff --git a/virtualizationservice/Android.bp b/virtualizationservice/Android.bp
index 0a5436b..b31d4a2 100644
--- a/virtualizationservice/Android.bp
+++ b/virtualizationservice/Android.bp
@@ -25,7 +25,6 @@
         "android.os.permissions_aidl-rust",
         "libandroid_logger",
         "libanyhow",
-        "libbinder_common",
         "libbinder_rs",
         "libcommand_fds",
         "libdisk",
@@ -38,6 +37,7 @@
         "libnix",
         "libonce_cell",
         "libregex",
+        "librpcbinder_rs",
         "librustutils",
         "libsemver",
         "libselinux_bindgen",
diff --git a/virtualizationservice/src/aidl.rs b/virtualizationservice/src/aidl.rs
index bba75ac..73b3ae5 100644
--- a/virtualizationservice/src/aidl.rs
+++ b/virtualizationservice/src/aidl.rs
@@ -45,7 +45,7 @@
     },
 };
 use anyhow::{anyhow, bail, Context, Result};
-use binder_common::rpc_server::run_rpc_server_with_factory;
+use rpcbinder::run_rpc_server_with_factory;
 use disk::QcowFile;
 use idsig::{HashAlgorithm, V4Signature};
 use log::{debug, error, info, warn, trace};
diff --git a/vmclient/Android.bp b/vmclient/Android.bp
index c219198..213125e 100644
--- a/vmclient/Android.bp
+++ b/vmclient/Android.bp
@@ -9,9 +9,9 @@
     edition: "2021",
     rustlibs: [
         "android.system.virtualizationservice-rust",
-        "libbinder_common",
         "libbinder_rs",
         "liblog_rust",
+        "librpcbinder_rs",
         "libthiserror",
     ],
     shared_libs: [
diff --git a/vmclient/src/lib.rs b/vmclient/src/lib.rs
index 129e6c3..16b5d5a 100644
--- a/vmclient/src/lib.rs
+++ b/vmclient/src/lib.rs
@@ -35,8 +35,8 @@
         ParcelFileDescriptor, Result as BinderResult, StatusCode, Strong,
     },
 };
-use binder_common::rpc_client::connect_preconnected_rpc_binder;
 use log::warn;
+use rpcbinder::get_preconnected_rpc_interface;
 use std::{
     fmt::{self, Debug, Formatter},
     fs::File,
@@ -174,7 +174,7 @@
         &self,
         port: u32,
     ) -> Result<Strong<T>, StatusCode> {
-        connect_preconnected_rpc_binder(|| {
+        get_preconnected_rpc_interface(|| {
             match self.vm.connectVsock(port as i32) {
                 Ok(vsock) => {
                     // Ownership of the fd is transferred to binder