Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b96737d6 authored by David Brazdil's avatar David Brazdil
Browse files

rpc_binder: Remove unused VsockFactory server code

Previous changes removed the use of RunVsockRpcServerWithFactory. Remove
the now unused code from libbinder_rpc.

Bug: 245727626
Test: builds
Change-Id: Icda0298e50cdae101d7929bb5de310e6e08ceb42
parent ef1ef8b0
Loading
Loading
Loading
Loading
+0 −8
Original line number Original line Diff line number Diff line
@@ -57,14 +57,6 @@ void ARpcServer_shutdown(ARpcServer* server);
// This automatically calls ARpcServer_shutdown().
// This automatically calls ARpcServer_shutdown().
void ARpcServer_free(ARpcServer* server);
void ARpcServer_free(ARpcServer* server);


// Starts an RPC server on a given port and a given root IBinder factory.
// RunVsockRpcServerWithFactory acts like RunVsockRpcServerCallback, but instead of
// assigning single root IBinder object to all connections, factory is called
// whenever a client connects, making it possible to assign unique IBinder
// object to each client.
bool RunVsockRpcServerWithFactory(AIBinder* (*factory)(unsigned int cid, void* context),
                                  void* factoryContext, unsigned int port);

AIBinder* VsockRpcClient(unsigned int cid, unsigned int port);
AIBinder* VsockRpcClient(unsigned int cid, unsigned int port);


// Gets the service via the RPC binder with Unix domain socket with the given
// Gets the service via the RPC binder with Unix domain socket with the given
+5 −30
Original line number Original line Diff line number Diff line
@@ -51,35 +51,8 @@ static void freeRpcServerHandle(ARpcServer* handle) {
    ref->decStrong(ref);
    ref->decStrong(ref);
}
}


static unsigned int cidFromStructAddr(const void* addr, size_t addrlen) {
    LOG_ALWAYS_FATAL_IF(addrlen < sizeof(sockaddr_vm), "sockaddr is truncated");
    const sockaddr_vm* vaddr = reinterpret_cast<const sockaddr_vm*>(addr);
    LOG_ALWAYS_FATAL_IF(vaddr->svm_family != AF_VSOCK, "address is not a vsock");
    return vaddr->svm_cid;
}

extern "C" {
extern "C" {


bool RunVsockRpcServerWithFactory(AIBinder* (*factory)(unsigned int cid, void* context),
                                  void* factoryContext, unsigned int port) {
    auto server = RpcServer::make();
    if (status_t status = server->setupVsockServer(VMADDR_CID_ANY, port); status != OK) {
        LOG(ERROR) << "Failed to set up vsock server with port " << port
                   << " error: " << statusToString(status).c_str();
        return false;
    }
    server->setPerSessionRootObject([=](const void* addr, size_t addrlen) {
        unsigned int cid = cidFromStructAddr(addr, addrlen);
        return AIBinder_toPlatformBinder(factory(cid, factoryContext));
    });

    server->join();

    // Shutdown any open sessions since server failed.
    (void)server->shutdown();
    return true;
}

ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, unsigned int port) {
ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, unsigned int port) {
    auto server = RpcServer::make();
    auto server = RpcServer::make();


@@ -96,9 +69,11 @@ ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, unsigned in
    }
    }
    if (cid != VMADDR_CID_ANY) {
    if (cid != VMADDR_CID_ANY) {
        server->setConnectionFilter([=](const void* addr, size_t addrlen) {
        server->setConnectionFilter([=](const void* addr, size_t addrlen) {
            unsigned int remoteCid = cidFromStructAddr(addr, addrlen);
            LOG_ALWAYS_FATAL_IF(addrlen < sizeof(sockaddr_vm), "sockaddr is truncated");
            if (cid != remoteCid) {
            const sockaddr_vm* vaddr = reinterpret_cast<const sockaddr_vm*>(addr);
                LOG(ERROR) << "Rejected vsock connection from CID " << remoteCid;
            LOG_ALWAYS_FATAL_IF(vaddr->svm_family != AF_VSOCK, "address is not a vsock");
            if (cid != vaddr->svm_cid) {
                LOG(ERROR) << "Rejected vsock connection from CID " << vaddr->svm_cid;
                return false;
                return false;
            }
            }
            return true;
            return true;
+1 −1
Original line number Original line Diff line number Diff line
@@ -23,4 +23,4 @@ pub use client::{
    get_preconnected_rpc_interface, get_preconnected_rpc_service, get_unix_domain_rpc_interface,
    get_preconnected_rpc_interface, get_preconnected_rpc_service, get_unix_domain_rpc_interface,
    get_unix_domain_rpc_service, get_vsock_rpc_interface, get_vsock_rpc_service,
    get_unix_domain_rpc_service, get_vsock_rpc_interface, get_vsock_rpc_service,
};
};
pub use server::{run_vsock_rpc_server_with_factory, RpcServer, RpcServerRef};
pub use server::{RpcServer, RpcServerRef};
+2 −49
Original line number Original line Diff line number Diff line
@@ -14,14 +14,11 @@
 * limitations under the License.
 * limitations under the License.
 */
 */


use binder::{
use binder::{unstable_api::AsNative, SpIBinder};
    unstable_api::{AIBinder, AsNative},
    SpIBinder,
};
use binder_rpc_unstable_bindgen::ARpcServer;
use binder_rpc_unstable_bindgen::ARpcServer;
use foreign_types::{foreign_type, ForeignType, ForeignTypeRef};
use foreign_types::{foreign_type, ForeignType, ForeignTypeRef};
use std::ffi::CString;
use std::io::{Error, ErrorKind};
use std::io::{Error, ErrorKind};
use std::{ffi::CString, os::raw, ptr::null_mut};


foreign_type! {
foreign_type! {
    type CType = binder_rpc_unstable_bindgen::ARpcServer;
    type CType = binder_rpc_unstable_bindgen::ARpcServer;
@@ -108,47 +105,3 @@ impl RpcServerRef {
        unsafe { binder_rpc_unstable_bindgen::ARpcServer_shutdown(self.as_ptr()) };
        unsafe { binder_rpc_unstable_bindgen::ARpcServer_shutdown(self.as_ptr()) };
    }
    }
}
}

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_vsock_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 `RunVsockRpcServerWithFactory`, 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 `RunVsockRpcServerWithFactory` returns).
    unsafe {
        binder_rpc_unstable_bindgen::RunVsockRpcServerWithFactory(
            Some(factory_wrapper),
            context,
            port,
        )
    }
}

unsafe extern "C" fn factory_wrapper(cid: u32, context: *mut raw::c_void) -> *mut AIBinder {
    // SAFETY: `context` was created from an `&mut RpcServerFactoryRef` by
    // `run_vsock_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()
    } else {
        null_mut()
    }
}