Remote server and process control bindings

This commit is contained in:
Jackson Coxson
2025-03-26 11:56:31 -06:00
parent 660f42adc2
commit e2c2df7325
4 changed files with 507 additions and 0 deletions

View File

@@ -8,7 +8,9 @@ pub mod installation_proxy;
pub mod logging;
pub mod mounter;
mod pairing_file;
pub mod process_control;
pub mod provider;
pub mod remote_server;
pub mod remotexpc;
pub mod usbmuxd;
pub mod util;

205
ffi/src/process_control.rs Normal file
View File

@@ -0,0 +1,205 @@
// Jackson Coxson
use std::ffi::{CStr, c_char};
use idevice::{dvt::process_control::ProcessControlClient, tcp::adapter::Adapter};
use plist::{Dictionary, Value};
use crate::{IdeviceErrorCode, RUNTIME, remote_server::RemoteServerAdapterHandle};
/// Opaque handle to a ProcessControlClient
pub struct ProcessControlAdapterHandle<'a>(pub ProcessControlClient<'a, Adapter>);
/// Creates a new ProcessControlClient from a RemoteServerClient
///
/// # Arguments
/// * [`server`] - The RemoteServerClient to use
/// * [`handle`] - Pointer to store the newly created ProcessControlClient handle
///
/// # Returns
/// An error code indicating success or failure
///
/// # Safety
/// `server` must be a valid pointer to a handle allocated by this library
/// `handle` must be a valid pointer to a location where the handle will be stored
#[unsafe(no_mangle)]
pub unsafe extern "C" fn process_control_new(
server: *mut RemoteServerAdapterHandle,
handle: *mut *mut ProcessControlAdapterHandle<'static>,
) -> IdeviceErrorCode {
if server.is_null() || handle.is_null() {
return IdeviceErrorCode::InvalidArg;
}
let server = unsafe { &mut (*server).0 };
let res = RUNTIME.block_on(async move { ProcessControlClient::new(server).await });
match res {
Ok(client) => {
let boxed = Box::new(ProcessControlAdapterHandle(client));
unsafe { *handle = Box::into_raw(boxed) };
IdeviceErrorCode::IdeviceSuccess
}
Err(e) => e.into(),
}
}
/// Frees a ProcessControlClient handle
///
/// # Arguments
/// * [`handle`] - The handle to free
///
/// # Safety
/// `handle` must be a valid pointer to a handle allocated by this library or NULL
#[unsafe(no_mangle)]
pub unsafe extern "C" fn process_control_free(handle: *mut ProcessControlAdapterHandle<'static>) {
if !handle.is_null() {
let _ = unsafe { Box::from_raw(handle) };
}
}
/// Launches an application on the device
///
/// # Arguments
/// * [`handle`] - The ProcessControlClient handle
/// * [`bundle_id`] - The bundle identifier of the app to launch
/// * [`env_vars`] - NULL-terminated array of environment variables (format "KEY=VALUE")
/// * [`arguments`] - NULL-terminated array of arguments
/// * [`start_suspended`] - Whether to start the app suspended
/// * [`kill_existing`] - Whether to kill existing instances of the app
/// * [`pid`] - Pointer to store the process ID of the launched app
///
/// # Returns
/// An error code indicating success or failure
///
/// # Safety
/// All pointers must be valid or NULL where appropriate
#[unsafe(no_mangle)]
pub unsafe extern "C" fn process_control_launch_app(
handle: *mut ProcessControlAdapterHandle<'static>,
bundle_id: *const c_char,
env_vars: *const *const c_char,
env_vars_count: usize,
arguments: *const *const c_char,
arguments_count: usize,
start_suspended: bool,
kill_existing: bool,
pid: *mut u64,
) -> IdeviceErrorCode {
if handle.is_null() || bundle_id.is_null() || pid.is_null() {
return IdeviceErrorCode::InvalidArg;
}
let bundle_id = unsafe { CStr::from_ptr(bundle_id) };
let bundle_id = match bundle_id.to_str() {
Ok(s) => s.to_string(),
Err(_) => return IdeviceErrorCode::InvalidArg,
};
let mut env_dict = Dictionary::new();
if !env_vars.is_null() {
let env_vars_slice = unsafe { std::slice::from_raw_parts(env_vars, env_vars_count) };
for &env_var in env_vars_slice {
if !env_var.is_null() {
let env_var = unsafe { CStr::from_ptr(env_var) };
if let Ok(env_var) = env_var.to_str() {
if let Some((key, value)) = env_var.split_once('=') {
env_dict.insert(key.to_string(), Value::String(value.to_string()));
}
}
}
}
}
let mut args_dict = Dictionary::new();
if !arguments.is_null() {
let args_slice = unsafe { std::slice::from_raw_parts(arguments, arguments_count) };
for (i, &arg) in args_slice.iter().enumerate() {
if !arg.is_null() {
let arg = unsafe { CStr::from_ptr(arg) };
if let Ok(arg) = arg.to_str() {
args_dict.insert(i.to_string(), Value::String(arg.to_string()));
}
}
}
}
let client = unsafe { &mut (*handle).0 };
let res = RUNTIME.block_on(async move {
client
.launch_app(
bundle_id,
Some(env_dict),
Some(args_dict),
start_suspended,
kill_existing,
)
.await
});
match res {
Ok(p) => {
unsafe { *pid = p };
IdeviceErrorCode::IdeviceSuccess
}
Err(e) => e.into(),
}
}
/// Kills a running process
///
/// # Arguments
/// * [`handle`] - The ProcessControlClient handle
/// * [`pid`] - The process ID to kill
///
/// # Returns
/// An error code indicating success or failure
///
/// # Safety
/// `handle` must be a valid pointer to a handle allocated by this library
#[unsafe(no_mangle)]
pub unsafe extern "C" fn process_control_kill_app(
handle: *mut ProcessControlAdapterHandle<'static>,
pid: u64,
) -> IdeviceErrorCode {
if handle.is_null() {
return IdeviceErrorCode::InvalidArg;
}
let client = unsafe { &mut (*handle).0 };
let res = RUNTIME.block_on(async move { client.kill_app(pid).await });
match res {
Ok(_) => IdeviceErrorCode::IdeviceSuccess,
Err(e) => e.into(),
}
}
/// Disables memory limits for a process
///
/// # Arguments
/// * [`handle`] - The ProcessControlClient handle
/// * [`pid`] - The process ID to modify
///
/// # Returns
/// An error code indicating success or failure
///
/// # Safety
/// `handle` must be a valid pointer to a handle allocated by this library
#[unsafe(no_mangle)]
pub unsafe extern "C" fn process_control_disable_memory_limit(
handle: *mut ProcessControlAdapterHandle<'static>,
pid: u64,
) -> IdeviceErrorCode {
if handle.is_null() {
return IdeviceErrorCode::InvalidArg;
}
let client = unsafe { &mut (*handle).0 };
let res = RUNTIME.block_on(async move { client.disable_memory_limit(pid).await });
match res {
Ok(_) => IdeviceErrorCode::IdeviceSuccess,
Err(e) => e.into(),
}
}

90
ffi/src/remote_server.rs Normal file
View File

@@ -0,0 +1,90 @@
// Jackson Coxson
use crate::core_device_proxy::AdapterHandle;
use crate::{IdeviceErrorCode, RUNTIME};
use idevice::IdeviceError;
use idevice::dvt::remote_server::RemoteServerClient;
use idevice::tcp::adapter::Adapter;
/// Opaque handle to a RemoteServerClient
pub struct RemoteServerAdapterHandle(pub RemoteServerClient<Adapter>);
/// Creates a new RemoteServerClient from a ReadWrite connection
///
/// # Arguments
/// * [`connection`] - The connection to use for communication
/// * [`handle`] - Pointer to store the newly created RemoteServerClient handle
///
/// # Returns
/// An error code indicating success or failure
///
/// # Safety
/// `connection` must be a valid pointer to a handle allocated by this library
/// `handle` must be a valid pointer to a location where the handle will be stored
#[unsafe(no_mangle)]
pub unsafe extern "C" fn remote_server_adapter_new(
adapter: *mut crate::core_device_proxy::AdapterHandle,
handle: *mut *mut RemoteServerAdapterHandle,
) -> IdeviceErrorCode {
if adapter.is_null() {
return IdeviceErrorCode::InvalidArg;
}
let connection = unsafe { Box::from_raw(adapter) };
let res: Result<RemoteServerClient<Adapter>, IdeviceError> = RUNTIME.block_on(async move {
let mut client = RemoteServerClient::new(connection.0);
client.read_message(0).await?; // Until Message has bindings, we'll do the first read
Ok(client)
});
match res {
Ok(client) => {
let boxed = Box::new(RemoteServerAdapterHandle(client));
unsafe { *handle = Box::into_raw(boxed) };
IdeviceErrorCode::IdeviceSuccess
}
Err(e) => e.into(),
}
}
/// Frees a RemoteServerClient handle
///
/// # Arguments
/// * [`handle`] - The handle to free
///
/// # Safety
/// `handle` must be a valid pointer to a handle allocated by this library or NULL
#[unsafe(no_mangle)]
pub unsafe extern "C" fn remote_server_free(handle: *mut RemoteServerAdapterHandle) {
if !handle.is_null() {
let _ = unsafe { Box::from_raw(handle) };
}
}
/// Returns the underlying connection from a RemoteServerClient
///
/// # Arguments
/// * [`handle`] - The handle to get the connection from
/// * [`connection`] - The newly allocated ConnectionHandle
///
/// # Returns
/// An error code indicating success or failure
///
/// # Safety
/// `handle` must be a valid pointer to a handle allocated by this library or NULL, and never used again
#[unsafe(no_mangle)]
pub unsafe extern "C" fn remote_server_adapter_into_inner(
handle: *mut RemoteServerAdapterHandle,
connection: *mut *mut AdapterHandle,
) -> IdeviceErrorCode {
if handle.is_null() || connection.is_null() {
return IdeviceErrorCode::InvalidArg;
}
let server = unsafe { Box::from_raw(handle) };
let connection_obj = server.0.into_inner();
let boxed = Box::new(AdapterHandle(connection_obj));
unsafe { *connection = Box::into_raw(boxed) };
IdeviceErrorCode::IdeviceSuccess
}