@@ -18,7 +18,7 @@ use crate::session::FileDescriptorTransportMode;
1818use binder:: { unstable_api:: AsNative , SpIBinder } ;
1919use binder_rpc_unstable_bindgen:: ARpcServer ;
2020use foreign_types:: { foreign_type, ForeignType , ForeignTypeRef } ;
21- use std:: ffi:: CString ;
21+ use std:: ffi:: { c_uint , CString } ;
2222use std:: io:: { Error , ErrorKind } ;
2323use 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