diff options
-rw-r--r-- | libs/binder/include_rpc_unstable/binder_rpc_unstable.hpp | 6 | ||||
-rw-r--r-- | libs/binder/libbinder_rpc_unstable.cpp | 5 | ||||
-rw-r--r-- | libs/binder/ndk/binder_rpc.cpp | 4 | ||||
-rw-r--r-- | libs/binder/ndk/include_platform/android/binder_rpc.h | 5 | ||||
-rw-r--r-- | libs/binder/rust/rpcbinder/src/server/android.rs | 27 |
5 files changed, 32 insertions, 15 deletions
diff --git a/libs/binder/include_rpc_unstable/binder_rpc_unstable.hpp b/libs/binder/include_rpc_unstable/binder_rpc_unstable.hpp index 392ebb5b0a..48c0ea636b 100644 --- a/libs/binder/include_rpc_unstable/binder_rpc_unstable.hpp +++ b/libs/binder/include_rpc_unstable/binder_rpc_unstable.hpp @@ -37,8 +37,12 @@ enum class ARpcSession_FileDescriptorTransportMode { // Set `cid` to VMADDR_CID_LOCAL to only bind to the local vsock interface. // Returns an opaque handle to the running server instance, or null if the server // could not be started. +// Set |port| to VMADDR_PORT_ANY to pick an available ephemeral port. +// |assignedPort| will be set to the assigned port number if it is not null. +// This will be the provided |port|, or the chosen available ephemeral port when +// |port| is VMADDR_PORT_ANY. [[nodiscard]] ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, - unsigned int port); + unsigned int port, unsigned int* assignedPort); // Starts a Unix domain RPC server with an open raw socket file descriptor // and a given root IBinder object. diff --git a/libs/binder/libbinder_rpc_unstable.cpp b/libs/binder/libbinder_rpc_unstable.cpp index 21537fc50d..a84a0c6e0b 100644 --- a/libs/binder/libbinder_rpc_unstable.cpp +++ b/libs/binder/libbinder_rpc_unstable.cpp @@ -81,7 +81,8 @@ RpcSession::FileDescriptorTransportMode toTransportMode( extern "C" { #ifndef __TRUSTY__ -ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, unsigned int port) { +ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, unsigned int port, + unsigned int* assignedPort) { auto server = RpcServer::make(); unsigned int bindCid = VMADDR_CID_ANY; // bind to the remote interface @@ -90,7 +91,7 @@ ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, unsigned in cid = VMADDR_CID_ANY; // no need for a connection filter } - if (status_t status = server->setupVsockServer(bindCid, port); status != OK) { + if (status_t status = server->setupVsockServer(bindCid, port, assignedPort); status != OK) { ALOGE("Failed to set up vsock server with port %u error: %s", port, statusToString(status).c_str()); return nullptr; diff --git a/libs/binder/ndk/binder_rpc.cpp b/libs/binder/ndk/binder_rpc.cpp index 886eb4bdf8..53ab68e643 100644 --- a/libs/binder/ndk/binder_rpc.cpp +++ b/libs/binder/ndk/binder_rpc.cpp @@ -104,8 +104,8 @@ struct OnDeleteProviderHolder { }; ABinderRpc_AccessorProvider* ABinderRpc_registerAccessorProvider( - ABinderRpc_AccessorProvider_getAccessorCallback provider, const char** instances, - size_t numInstances, void* data, + ABinderRpc_AccessorProvider_getAccessorCallback provider, + const char* const* const instances, size_t numInstances, void* data, ABinderRpc_AccessorProviderUserData_deleteCallback onDelete) { if (provider == nullptr) { ALOGE("Null provider passed to ABinderRpc_registerAccessorProvider"); diff --git a/libs/binder/ndk/include_platform/android/binder_rpc.h b/libs/binder/ndk/include_platform/android/binder_rpc.h index 66667d33bd..7d54e2dad9 100644 --- a/libs/binder/ndk/include_platform/android/binder_rpc.h +++ b/libs/binder/ndk/include_platform/android/binder_rpc.h @@ -144,8 +144,9 @@ typedef void (*ABinderRpc_AccessorProviderUserData_deleteCallback)(void* _Nullab */ ABinderRpc_AccessorProvider* _Nullable ABinderRpc_registerAccessorProvider( ABinderRpc_AccessorProvider_getAccessorCallback _Nonnull provider, - const char* _Nullable* _Nonnull instances, size_t numInstances, void* _Nullable data, - ABinderRpc_AccessorProviderUserData_deleteCallback _Nullable onDelete) __INTRODUCED_IN(36); + const char* _Nullable const* const _Nonnull instances, size_t numInstances, + void* _Nullable data, ABinderRpc_AccessorProviderUserData_deleteCallback _Nullable onDelete) + __INTRODUCED_IN(36); /** * Remove an ABinderRpc_AccessorProvider from libbinder. This will remove references diff --git a/libs/binder/rust/rpcbinder/src/server/android.rs b/libs/binder/rust/rpcbinder/src/server/android.rs index 2ab34472a9..74ce315e30 100644 --- a/libs/binder/rust/rpcbinder/src/server/android.rs +++ b/libs/binder/rust/rpcbinder/src/server/android.rs @@ -18,7 +18,7 @@ use crate::session::FileDescriptorTransportMode; use binder::{unstable_api::AsNative, SpIBinder}; use binder_rpc_unstable_bindgen::ARpcServer; use foreign_types::{foreign_type, ForeignType, ForeignTypeRef}; -use std::ffi::CString; +use std::ffi::{c_uint, CString}; use std::io::{Error, ErrorKind}; use std::os::unix::io::{IntoRawFd, OwnedFd}; @@ -42,18 +42,29 @@ impl RpcServer { /// Creates a binder RPC server, serving the supplied binder service implementation on the given /// vsock port. Only connections from the given CID are accepted. /// - // Set `cid` to libc::VMADDR_CID_ANY to accept connections from any client. - // Set `cid` to libc::VMADDR_CID_LOCAL to only bind to the local vsock interface. - pub fn new_vsock(mut service: SpIBinder, cid: u32, port: u32) -> Result<RpcServer, Error> { + /// Set `cid` to [`libc::VMADDR_CID_ANY`] to accept connections from any client. + /// Set `cid` to [`libc::VMADDR_CID_LOCAL`] to only bind to the local vsock interface. + /// Set `port` to [`libc::VMADDR_PORT_ANY`] to pick an ephemeral port. + /// The assigned port is returned with RpcServer. + pub fn new_vsock( + mut service: SpIBinder, + cid: u32, + port: u32, + ) -> Result<(RpcServer, u32 /* assigned_port */), Error> { let service = service.as_native_mut(); + let mut assigned_port: c_uint = 0; // SAFETY: Service ownership is transferring to the server and won't be valid afterward. // Plus the binder objects are threadsafe. - unsafe { + let server = unsafe { Self::checked_from_ptr(binder_rpc_unstable_bindgen::ARpcServer_newVsock( - service, cid, port, - )) - } + service, + cid, + port, + &mut assigned_port, + ))? + }; + Ok((server, assigned_port as _)) } /// Creates a binder RPC server, serving the supplied binder service implementation on the given |