Victor Hsieh | bb7dd08 | 2021-10-12 16:39:02 -0700 | [diff] [blame] | 1 | /* |
| 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 Hsieh | bb7dd08 | 2021-10-12 16:39:02 -0700 | [diff] [blame] | 19 | use binder::unstable_api::{new_spibinder, AIBinder}; |
Andrew Walbran | c944fae | 2022-08-02 16:16:28 +0000 | [diff] [blame^] | 20 | use binder::{FromIBinder, StatusCode, Strong}; |
| 21 | use std::os::{raw, unix::io::RawFd}; |
Victor Hsieh | bb7dd08 | 2021-10-12 16:39:02 -0700 | [diff] [blame] | 22 | |
| 23 | /// Connects to a binder RPC server. |
Andrew Walbran | c944fae | 2022-08-02 16:16:28 +0000 | [diff] [blame^] | 24 | pub fn connect_rpc_binder<T: FromIBinder + ?Sized>( |
Victor Hsieh | bb7dd08 | 2021-10-12 16:39:02 -0700 | [diff] [blame] | 25 | cid: u32, |
| 26 | port: u32, |
Stephen Crane | 44fd9bc | 2022-01-19 17:49:46 +0000 | [diff] [blame] | 27 | ) -> Result<Strong<T>, StatusCode> { |
Victor Hsieh | bb7dd08 | 2021-10-12 16:39:02 -0700 | [diff] [blame] | 28 | // 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 Walbran | c944fae | 2022-08-02 16:16:28 +0000 | [diff] [blame^] | 39 | |
| 40 | type 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. |
| 44 | pub 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 | |
| 68 | unsafe 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 | } |