230 lines
5.9 KiB
Rust
Raw Normal View History

#![feature(format_args_nl)]
#![feature(try_blocks)]
2024-07-16 22:58:11 +08:00
#![feature(let_chains)]
2025-01-03 11:38:15 -08:00
#![feature(fn_traits)]
2023-05-30 22:23:11 -07:00
#![allow(clippy::missing_safety_doc)]
2023-06-12 05:59:50 -07:00
use base::Utf8CStr;
2025-01-09 00:14:08 +08:00
use daemon::{daemon_entry, get_magiskd, MagiskD};
2025-01-03 11:38:15 -08:00
use db::get_default_db_settings;
2025-01-07 00:53:21 -08:00
use logging::{android_logging, setup_logfile, zygisk_close_logd, zygisk_get_logd, zygisk_logging};
2025-01-03 11:38:15 -08:00
use mount::{clean_mounts, find_preinit_device, revert_unmount, setup_mounts};
2023-09-06 15:52:14 -07:00
use resetprop::{persist_delete_prop, persist_get_prop, persist_get_props, persist_set_prop};
2025-01-05 00:44:05 -08:00
use su::get_default_root_settings;
2022-07-05 21:13:09 -07:00
2023-05-23 21:30:30 -07:00
#[path = "../include/consts.rs"]
mod consts;
2023-05-09 18:54:38 -07:00
mod daemon;
2025-01-03 11:38:15 -08:00
mod db;
2022-07-05 21:13:09 -07:00
mod logging;
mod mount;
2025-01-09 00:14:08 +08:00
mod package;
mod resetprop;
2025-01-05 00:44:05 -08:00
mod su;
2022-07-01 04:53:41 -07:00
#[cxx::bridge]
pub mod ffi {
2023-11-17 13:35:50 -08:00
#[repr(i32)]
enum RequestCode {
START_DAEMON,
CHECK_VERSION,
CHECK_VERSION_CODE,
STOP_DAEMON,
_SYNC_BARRIER_,
SUPERUSER,
ZYGOTE_RESTART,
DENYLIST,
SQLITE_CMD,
REMOVE_MODULES,
ZYGISK,
_STAGE_BARRIER_,
POST_FS_DATA,
LATE_START,
BOOT_COMPLETE,
END,
}
2023-05-21 23:51:30 -07:00
extern "C++" {
2023-11-08 01:46:02 -08:00
include!("include/resetprop.hpp");
2023-09-06 15:52:14 -07:00
#[cxx_name = "prop_cb"]
type PropCb;
unsafe fn get_prop_rs(name: *const c_char, persist: bool) -> String;
unsafe fn prop_cb_exec(
cb: Pin<&mut PropCb>,
name: *const c_char,
value: *const c_char,
serial: u32,
);
2023-05-21 23:51:30 -07:00
}
2023-11-17 13:35:50 -08:00
unsafe extern "C++" {
2023-12-26 23:08:06 +08:00
#[namespace = "rust"]
#[cxx_name = "Utf8CStr"]
type Utf8CStrRef<'a> = base::ffi::Utf8CStrRef<'a>;
2023-12-03 19:32:58 +08:00
include!("include/daemon.hpp");
2023-12-26 23:08:06 +08:00
#[cxx_name = "get_magisk_tmp_rs"]
fn get_magisk_tmp() -> Utf8CStrRef<'static>;
#[cxx_name = "resolve_preinit_dir_rs"]
fn resolve_preinit_dir(base_dir: Utf8CStrRef) -> String;
2025-01-09 00:14:08 +08:00
fn install_apk(apk: Utf8CStrRef);
fn uninstall_pkg(apk: Utf8CStrRef);
2023-11-17 13:35:50 -08:00
fn switch_mnt_ns(pid: i32) -> i32;
2023-11-17 13:35:50 -08:00
}
2025-01-03 11:38:15 -08:00
enum DbEntryKey {
RootAccess,
SuMultiuserMode,
SuMntNs,
DenylistConfig,
ZygiskConfig,
BootloopCount,
SuManager,
}
#[repr(i32)]
enum RootAccess {
Disabled,
AppsOnly,
AdbOnly,
AppsAndAdb,
}
#[repr(i32)]
enum MultiuserMode {
OwnerOnly,
OwnerManaged,
User,
}
#[repr(i32)]
enum MntNsMode {
Global,
Requester,
Isolate,
}
#[derive(Default)]
struct DbSettings {
root_access: RootAccess,
multiuser_mode: MultiuserMode,
mnt_ns: MntNsMode,
boot_count: i32,
denylist: bool,
zygisk: bool,
}
2025-01-05 00:44:05 -08:00
#[repr(i32)]
enum SuPolicy {
Query,
Deny,
Allow,
}
struct RootSettings {
policy: SuPolicy,
log: bool,
notify: bool,
}
2025-01-26 19:41:34 +08:00
struct ModuleInfo {
name: String,
z32: i32,
z64: i32,
}
2025-01-03 11:38:15 -08:00
unsafe extern "C++" {
include!("include/sqlite.hpp");
fn sqlite3_errstr(code: i32) -> *const c_char;
type sqlite3;
fn open_and_init_db() -> *mut sqlite3;
type DbValues;
type DbStatement;
fn get_int(self: &DbValues, index: i32) -> i32;
#[cxx_name = "get_str"]
fn get_text(self: &DbValues, index: i32) -> &str;
fn bind_text(self: Pin<&mut DbStatement>, index: i32, val: &str) -> i32;
fn bind_int64(self: Pin<&mut DbStatement>, index: i32, val: i64) -> i32;
}
2022-07-01 04:53:41 -07:00
extern "Rust" {
fn rust_test_entry();
2022-07-05 21:13:09 -07:00
fn android_logging();
fn zygisk_logging();
2023-11-04 23:59:11 -07:00
fn zygisk_close_logd();
fn zygisk_get_logd() -> i32;
2025-01-07 00:53:21 -08:00
fn setup_logfile();
fn setup_mounts();
2024-08-14 21:36:15 +08:00
fn clean_mounts();
fn find_preinit_device() -> String;
fn revert_unmount(pid: i32);
2023-12-26 23:08:06 +08:00
unsafe fn persist_get_prop(name: Utf8CStrRef, prop_cb: Pin<&mut PropCb>);
2023-09-06 15:52:14 -07:00
unsafe fn persist_get_props(prop_cb: Pin<&mut PropCb>);
2023-12-26 23:08:06 +08:00
unsafe fn persist_delete_prop(name: Utf8CStrRef) -> bool;
unsafe fn persist_set_prop(name: Utf8CStrRef, value: Utf8CStrRef) -> bool;
2022-07-05 21:13:09 -07:00
2024-12-03 15:51:17 -08:00
#[namespace = "rust"]
2023-05-09 18:54:38 -07:00
fn daemon_entry();
2024-12-03 15:51:17 -08:00
}
2023-05-09 18:54:38 -07:00
2024-12-03 15:51:17 -08:00
// FFI for MagiskD
extern "Rust" {
2023-05-09 18:54:38 -07:00
type MagiskD;
2025-01-05 00:44:05 -08:00
fn is_recovery(&self) -> bool;
2025-01-06 02:48:06 -08:00
fn sdk_int(&self) -> i32;
2025-01-05 00:44:05 -08:00
fn boot_stage_handler(&self, client: i32, code: i32);
2025-01-09 00:14:08 +08:00
fn preserve_stub_apk(&self);
2025-01-10 09:04:56 +08:00
fn prune_su_access(&self);
2025-01-10 09:38:06 +08:00
fn uid_granted_root(&self, mut uid: i32) -> bool;
2025-01-09 00:14:08 +08:00
#[cxx_name = "get_manager"]
unsafe fn get_manager_for_cxx(&self, user: i32, ptr: *mut CxxString, install: bool) -> i32;
2025-01-26 19:41:34 +08:00
fn set_module_list(&self, module_list: Vec<ModuleInfo>);
fn module_list(&self) -> &Vec<ModuleInfo>;
2024-12-03 15:51:17 -08:00
2025-01-03 11:38:15 -08:00
#[cxx_name = "get_db_settings"]
2025-01-05 00:44:05 -08:00
fn get_db_settings_for_cxx(&self, cfg: &mut DbSettings) -> bool;
2025-01-03 11:38:15 -08:00
fn get_db_setting(&self, key: DbEntryKey) -> i32;
#[cxx_name = "set_db_setting"]
fn set_db_setting_for_cxx(&self, key: DbEntryKey, value: i32) -> bool;
#[cxx_name = "db_exec"]
fn db_exec_for_cxx(&self, client_fd: i32);
2025-01-05 00:44:05 -08:00
#[cxx_name = "get_root_settings"]
fn get_root_settings_for_cxx(&self, uid: i32, settings: &mut RootSettings) -> bool;
2025-01-03 11:38:15 -08:00
#[cxx_name = "DbSettings"]
fn get_default_db_settings() -> DbSettings;
2025-01-05 00:44:05 -08:00
#[cxx_name = "RootSettings"]
fn get_default_root_settings() -> RootSettings;
2024-12-03 15:51:17 -08:00
#[cxx_name = "MagiskD"]
fn get_magiskd() -> &'static MagiskD;
}
unsafe extern "C++" {
#[allow(dead_code)]
fn reboot(self: &MagiskD);
fn post_fs_data(self: &MagiskD) -> bool;
fn late_start(self: &MagiskD);
fn boot_complete(self: &MagiskD);
2025-01-26 19:41:34 +08:00
#[allow(dead_code)]
fn handle_modules(self: &MagiskD);
2023-05-09 18:54:38 -07:00
}
}
2022-07-01 04:53:41 -07:00
fn rust_test_entry() {}
2023-05-21 23:51:30 -07:00
2023-06-12 05:59:50 -07:00
pub fn get_prop(name: &Utf8CStr, persist: bool) -> String {
2023-05-21 23:51:30 -07:00
unsafe { ffi::get_prop_rs(name.as_ptr(), persist) }
}