Skip to content

Commit 398f9f5

Browse files
Rust new_vsock use assigned port feature
The RpcServer::setupVsockServer allows callers to pass VMADDR_PORT_ANY as the port to use any available port number to create the vsock connection on. This change uses that functionality in the available Rust types. Test: atest vm_accessor_test Bug: 372381104 Change-Id: I482ea58149f1989258164511191f256fc563a8d9
1 parent b7c7d8a commit 398f9f5

5 files changed

Lines changed: 32 additions & 15 deletions

File tree

libs/binder/include_rpc_unstable/binder_rpc_unstable.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ enum class ARpcSession_FileDescriptorTransportMode {
3737
// Set `cid` to VMADDR_CID_LOCAL to only bind to the local vsock interface.
3838
// Returns an opaque handle to the running server instance, or null if the server
3939
// could not be started.
40+
// Set |port| to VMADDR_PORT_ANY to pick an available ephemeral port.
41+
// |assignedPort| will be set to the assigned port number if it is not null.
42+
// This will be the provided |port|, or the chosen available ephemeral port when
43+
// |port| is VMADDR_PORT_ANY.
4044
[[nodiscard]] ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid,
41-
unsigned int port);
45+
unsigned int port, unsigned int* assignedPort);
4246

4347
// Starts a Unix domain RPC server with an open raw socket file descriptor
4448
// and a given root IBinder object.

libs/binder/libbinder_rpc_unstable.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ RpcSession::FileDescriptorTransportMode toTransportMode(
8181
extern "C" {
8282

8383
#ifndef __TRUSTY__
84-
ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, unsigned int port) {
84+
ARpcServer* ARpcServer_newVsock(AIBinder* service, unsigned int cid, unsigned int port,
85+
unsigned int* assignedPort) {
8586
auto server = RpcServer::make();
8687

8788
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
9091
cid = VMADDR_CID_ANY; // no need for a connection filter
9192
}
9293

93-
if (status_t status = server->setupVsockServer(bindCid, port); status != OK) {
94+
if (status_t status = server->setupVsockServer(bindCid, port, assignedPort); status != OK) {
9495
ALOGE("Failed to set up vsock server with port %u error: %s", port,
9596
statusToString(status).c_str());
9697
return nullptr;

libs/binder/ndk/binder_rpc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ struct OnDeleteProviderHolder {
104104
};
105105

106106
ABinderRpc_AccessorProvider* ABinderRpc_registerAccessorProvider(
107-
ABinderRpc_AccessorProvider_getAccessorCallback provider, const char** instances,
108-
size_t numInstances, void* data,
107+
ABinderRpc_AccessorProvider_getAccessorCallback provider,
108+
const char* const* const instances, size_t numInstances, void* data,
109109
ABinderRpc_AccessorProviderUserData_deleteCallback onDelete) {
110110
if (provider == nullptr) {
111111
ALOGE("Null provider passed to ABinderRpc_registerAccessorProvider");

libs/binder/ndk/include_platform/android/binder_rpc.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,9 @@ typedef void (*ABinderRpc_AccessorProviderUserData_deleteCallback)(void* _Nullab
144144
*/
145145
ABinderRpc_AccessorProvider* _Nullable ABinderRpc_registerAccessorProvider(
146146
ABinderRpc_AccessorProvider_getAccessorCallback _Nonnull provider,
147-
const char* _Nullable* _Nonnull instances, size_t numInstances, void* _Nullable data,
148-
ABinderRpc_AccessorProviderUserData_deleteCallback _Nullable onDelete) __INTRODUCED_IN(36);
147+
const char* _Nullable const* const _Nonnull instances, size_t numInstances,
148+
void* _Nullable data, ABinderRpc_AccessorProviderUserData_deleteCallback _Nullable onDelete)
149+
__INTRODUCED_IN(36);
149150

150151
/**
151152
* Remove an ABinderRpc_AccessorProvider from libbinder. This will remove references

libs/binder/rust/rpcbinder/src/server/android.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::session::FileDescriptorTransportMode;
1818
use binder::{unstable_api::AsNative, SpIBinder};
1919
use binder_rpc_unstable_bindgen::ARpcServer;
2020
use foreign_types::{foreign_type, ForeignType, ForeignTypeRef};
21-
use std::ffi::CString;
21+
use std::ffi::{c_uint, CString};
2222
use std::io::{Error, ErrorKind};
2323
use std::os::unix::io::{IntoRawFd, OwnedFd};
2424

@@ -42,18 +42,29 @@ impl RpcServer {
4242
/// Creates a binder RPC server, serving the supplied binder service implementation on the given
4343
/// vsock port. Only connections from the given CID are accepted.
4444
///
45-
// Set `cid` to libc::VMADDR_CID_ANY to accept connections from any client.
46-
// Set `cid` to libc::VMADDR_CID_LOCAL to only bind to the local vsock interface.
47-
pub fn new_vsock(mut service: SpIBinder, cid: u32, port: u32) -> Result<RpcServer, Error> {
45+
/// Set `cid` to [`libc::VMADDR_CID_ANY`] to accept connections from any client.
46+
/// Set `cid` to [`libc::VMADDR_CID_LOCAL`] to only bind to the local vsock interface.
47+
/// Set `port` to [`libc::VMADDR_PORT_ANY`] to pick an ephemeral port.
48+
/// The assigned port is returned with RpcServer.
49+
pub fn new_vsock(
50+
mut service: SpIBinder,
51+
cid: u32,
52+
port: u32,
53+
) -> Result<(RpcServer, u32 /* assigned_port */), Error> {
4854
let service = service.as_native_mut();
4955

56+
let mut assigned_port: c_uint = 0;
5057
// SAFETY: Service ownership is transferring to the server and won't be valid afterward.
5158
// Plus the binder objects are threadsafe.
52-
unsafe {
59+
let server = unsafe {
5360
Self::checked_from_ptr(binder_rpc_unstable_bindgen::ARpcServer_newVsock(
54-
service, cid, port,
55-
))
56-
}
61+
service,
62+
cid,
63+
port,
64+
&mut assigned_port,
65+
))?
66+
};
67+
Ok((server, assigned_port as _))
5768
}
5869

5970
/// Creates a binder RPC server, serving the supplied binder service implementation on the given

0 commit comments

Comments
 (0)