blob: 33fd7325e707167307c9e100c86840437b3a89d4 [file] [log] [blame]
Victor Hsiehbb7dd082021-10-12 16:39:02 -07001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Helpers for implementing an RPC Binder client.
18
Victor Hsiehbb7dd082021-10-12 16:39:02 -070019use binder::unstable_api::{new_spibinder, AIBinder};
Andrew Walbranc944fae2022-08-02 16:16:28 +000020use binder::{FromIBinder, StatusCode, Strong};
21use std::os::{raw, unix::io::RawFd};
Victor Hsiehbb7dd082021-10-12 16:39:02 -070022
23/// Connects to a binder RPC server.
Andrew Walbranc944fae2022-08-02 16:16:28 +000024pub fn connect_rpc_binder<T: FromIBinder + ?Sized>(
Victor Hsiehbb7dd082021-10-12 16:39:02 -070025 cid: u32,
26 port: u32,
Stephen Crane44fd9bc2022-01-19 17:49:46 +000027) -> Result<Strong<T>, StatusCode> {
Victor Hsiehbb7dd082021-10-12 16:39:02 -070028 // SAFETY: AIBinder returned by RpcClient has correct reference count, and the ownership can be
29 // safely taken by new_spibinder.
30 let ibinder = unsafe {
31 new_spibinder(binder_rpc_unstable_bindgen::RpcClient(cid, port) as *mut AIBinder)
32 };
33 if let Some(ibinder) = ibinder {
34 <T>::try_from(ibinder)
35 } else {
36 Err(StatusCode::BAD_VALUE)
37 }
38}
Andrew Walbranc944fae2022-08-02 16:16:28 +000039
40type RequestFd<'a> = &'a mut dyn FnMut() -> Option<RawFd>;
41
42/// Connects to a Binder RPC server, using the given callback to get (and take ownership of) file
43/// descriptors already connected to it.
44pub fn connect_preconnected_rpc_binder<T: FromIBinder + ?Sized>(
45 mut request_fd: impl FnMut() -> Option<RawFd>,
46) -> Result<Strong<T>, StatusCode> {
47 // Double reference the factory because trait objects aren't FFI safe.
48 let mut request_fd_ref: RequestFd = &mut request_fd;
49 let param = &mut request_fd_ref as *mut RequestFd as *mut raw::c_void;
50
51 // SAFETY: AIBinder returned by RpcPreconnectedClient has correct reference count, and the
52 // ownership can be safely taken by new_spibinder. RpcPreconnectedClient does not take ownership
53 // of param, only passing it to request_fd_wrapper.
54 let ibinder = unsafe {
55 new_spibinder(binder_rpc_unstable_bindgen::RpcPreconnectedClient(
56 Some(request_fd_wrapper),
57 param,
58 ) as *mut AIBinder)
59 };
60
61 if let Some(ibinder) = ibinder {
62 <T>::try_from(ibinder)
63 } else {
64 Err(StatusCode::BAD_VALUE)
65 }
66}
67
68unsafe extern "C" fn request_fd_wrapper(param: *mut raw::c_void) -> raw::c_int {
69 // SAFETY: This is only ever called by RpcPreconnectedClient, within the lifetime of the
70 // BinderFdFactory reference, with param being a properly aligned non-null pointer to an
71 // initialized instance.
72 let request_fd_ptr = param as *mut RequestFd;
73 let request_fd = request_fd_ptr.as_mut().unwrap();
74 request_fd().unwrap_or(-1)
75}