Add more functionality into Rust

This commit is contained in:
topjohnwu 2025-01-06 02:48:06 -08:00 committed by John Wu
parent 0d31d356ef
commit 7098248c64
6 changed files with 53 additions and 37 deletions

View File

@ -219,8 +219,6 @@ def run_ndk_build(cmds: list):
def build_cpp_src(targets: set):
dump_flag_header()
cmds = []
clean = False
@ -336,7 +334,11 @@ def dump_flag_header():
flag_txt += f"#define MAGISK_DEBUG {0 if args.release else 1}\n"
native_gen_path.mkdir(mode=0o755, parents=True, exist_ok=True)
write_if_diff(Path(native_gen_path, "flags.h"), flag_txt)
write_if_diff(native_gen_path / "flags.h", flag_txt)
rust_flag_txt = f'pub const MAGISK_VERSION: &str = "{config["version"]}";\n'
rust_flag_txt += f'pub const MAGISK_VER_CODE: i32 = {config["versionCode"]};\n'
write_if_diff(native_gen_path / "flags.rs", rust_flag_txt)
def build_native():
@ -363,6 +365,7 @@ def build_native():
if ccache := shutil.which("ccache"):
os.environ["NDK_CCACHE"] = ccache
dump_flag_header()
build_rust_src(targets)
build_cpp_src(targets)

View File

@ -328,8 +328,7 @@ static void daemon_entry() {
setcon(MAGISK_PROC_CON);
rust::daemon_entry();
LOGI(NAME_WITH_VER(Magisk) " daemon started\n");
SDK_INT = MagiskD().sdk_int();
// Escape from cgroup
int pid = getpid();
@ -343,23 +342,6 @@ static void daemon_entry() {
// Get self stat
xstat("/proc/self/exe", &self_st);
// Get API level
parse_prop_file("/system/build.prop", [](auto key, auto val) -> bool {
if (key == "ro.build.version.sdk") {
SDK_INT = parse_int(val);
return false;
}
return true;
});
if (SDK_INT < 0) {
// In case some devices do not store this info in build.prop, fallback to getprop
auto sdk = get_prop("ro.build.version.sdk");
if (!sdk.empty()) {
SDK_INT = parse_int(sdk);
}
}
LOGI("* Device API level: %d\n", SDK_INT);
// Samsung workaround #7887
if (access("/system_ext/app/mediatek-res/mediatek-res.apk", F_OK) == 0) {
set_prop("ro.vendor.mtk_model", "0");

View File

@ -1,7 +1,12 @@
use crate::consts::{MAGISK_FULL_VER, MAIN_CONFIG};
use crate::db::Sqlite3;
use crate::ffi::{get_magisk_tmp, RequestCode};
use crate::get_prop;
use crate::logging::magisk_logging;
use base::libc::{O_CLOEXEC, O_RDONLY};
use base::{
cstr, libc, open_fd, BufReadExt, Directory, FsPathBuf, ReadExt, ResultExt, Utf8CStr,
Utf8CStrBuf, Utf8CStrBufArr, Utf8CStrBufRef, WalkResult,
cstr, info, libc, open_fd, BufReadExt, Directory, FsPath, FsPathBuf, ReadExt, ResultExt,
Utf8CStr, Utf8CStrBuf, Utf8CStrBufArr, Utf8CStrBufRef, WalkResult,
};
use bytemuck::bytes_of;
use std::fs::File;
@ -9,12 +14,6 @@ use std::io;
use std::io::{BufReader, Read, Write};
use std::sync::{Mutex, OnceLock};
use crate::consts::MAIN_CONFIG;
use crate::db::Sqlite3;
use crate::ffi::{get_magisk_tmp, RequestCode};
use crate::get_prop;
use crate::logging::magisk_logging;
// Global magiskd singleton
pub static MAGISKD: OnceLock<MagiskD> = OnceLock::new();
@ -45,19 +44,20 @@ pub struct MagiskD {
pub logd: Mutex<Option<File>>,
pub sql_connection: Mutex<Option<Sqlite3>>,
boot_stage_lock: Mutex<BootStateFlags>,
is_emulator: bool,
sdk_int: i32,
pub is_emulator: bool,
is_recovery: bool,
}
impl MagiskD {
pub fn is_emulator(&self) -> bool {
self.is_emulator
}
pub fn is_recovery(&self) -> bool {
self.is_recovery
}
pub fn sdk_int(&self) -> i32 {
self.sdk_int
}
pub fn boot_stage_handler(&self, client: i32, code: i32) {
// Make sure boot stage execution is always serialized
let mut state = self.boot_stage_lock.lock().unwrap();
@ -117,7 +117,26 @@ pub fn daemon_entry() {
});
}
let mut sdk_int = -1;
if let Ok(file) = FsPath::from(cstr!("/system/build.prop")).open(O_RDONLY | O_CLOEXEC) {
let mut file = BufReader::new(file);
file.foreach_props(|key, val| {
if key == "ro.build.version.sdk" {
sdk_int = val.parse::<i32>().unwrap_or(-1);
return false;
}
true
});
}
if sdk_int < 0 {
// In case some devices do not store this info in build.prop, fallback to getprop
sdk_int = get_prop(cstr!("ro.build.version.sdk"), false)
.parse::<i32>()
.unwrap_or(-1);
}
let magiskd = MagiskD {
sdk_int,
is_emulator,
is_recovery,
..Default::default()
@ -125,6 +144,9 @@ pub fn daemon_entry() {
magiskd.start_log_daemon();
MAGISKD.set(magiskd).ok();
magisk_logging();
info!("Magisk {} daemon started", MAGISK_FULL_VER);
info!("* Device API level: {}", sdk_int);
}
fn check_data() -> bool {

View File

@ -232,7 +232,7 @@ impl MagiskD {
DbEntryKey::SuMultiuserMode => MultiuserMode::default().repr,
DbEntryKey::SuMntNs => MntNsMode::default().repr,
DbEntryKey::DenylistConfig => 0,
DbEntryKey::ZygiskConfig => self.is_emulator() as i32,
DbEntryKey::ZygiskConfig => self.is_emulator as i32,
DbEntryKey::BootloopCount => 0,
_ => -1,
};
@ -251,7 +251,7 @@ impl MagiskD {
}
pub fn get_db_settings(&self, cfg: &mut DbSettings) -> SqliteResult {
cfg.zygisk = self.is_emulator();
cfg.zygisk = self.is_emulator;
self.db_exec_with_rows("SELECT * FROM settings", &[], cfg)
.sql_result()
}

View File

@ -182,6 +182,7 @@ pub mod ffi {
type MagiskD;
fn setup_logfile(&self);
fn is_recovery(&self) -> bool;
fn sdk_int(&self) -> i32;
fn boot_stage_handler(&self, client: i32, code: i32);
#[cxx_name = "get_db_settings"]

View File

@ -1,5 +1,13 @@
#![allow(dead_code)]
use base::const_format::concatcp;
#[path = "../../out/generated/flags.rs"]
mod flags;
// versions
pub use flags::*;
pub const MAGISK_FULL_VER: &str = concatcp!(MAGISK_VERSION, "(", MAGISK_VER_CODE, ")");
pub const LOGFILE: &str = "/cache/magisk.log";
// data paths