Use Rust for formatting

The fprintf implementation included in crt0 is too rudimental
This commit is contained in:
topjohnwu 2024-02-26 00:26:23 -08:00
parent 0ccd6e7381
commit be433fa667
3 changed files with 51 additions and 18 deletions

View File

@ -1,13 +1,26 @@
#![feature(format_args_nl)]
use logging::setup_klog;
use rootdir::inject_magisk_rc;
// Has to be pub so all symbols in that crate is included
pub use magiskpolicy;
mod logging;
mod rootdir;
#[cxx::bridge]
pub mod ffi {
#[namespace = "rust"]
extern "Rust" {
fn setup_klog();
fn inject_magisk_rc(fd: i32, tmp_dir: Utf8CStrRef);
}
unsafe extern "C++" {
include!("../base/include/base.hpp");
#[namespace = "rust"]
#[cxx_name = "Utf8CStr"]
type Utf8CStrRef<'a> = base::ffi::Utf8CStrRef<'a>;
}
}

View File

@ -77,24 +77,7 @@ static void patch_rc_scripts(const char *src_path, const char *tmp_path, bool wr
rc_list.clear();
// Inject Magisk rc scripts
LOGD("Inject magisk rc\n");
fprintf(dest.get(), R"EOF(
on post-fs-data
start logd
exec %2$s 0 0 -- %1$s/magisk --post-fs-data
on property:vold.decrypt=trigger_restart_framework
exec %2$s 0 0 -- %1$s/magisk --service
on nonencrypted
exec %2$s 0 0 -- %1$s/magisk --service
on property:sys.boot_completed=1
exec %2$s 0 0 -- %1$s/magisk --boot-complete
on property:init.svc.zygote=stopped
exec %2$s 0 0 -- %1$s/magisk --zygote-restart
)EOF", tmp_path, MAGISK_PROC_CON);
rust::inject_magisk_rc(fileno(dest.get()), tmp_path);
fclone_attr(fileno(src.get()), fileno(dest.get()));
}

View File

@ -0,0 +1,37 @@
use std::fs::File;
use std::io::Write;
use std::mem;
use std::os::fd::{FromRawFd, RawFd};
use base::{debug, Utf8CStr};
pub fn inject_magisk_rc(fd: RawFd, tmp_dir: &Utf8CStr) {
debug!("Injecting magisk rc");
let mut file = unsafe { File::from_raw_fd(fd) };
write!(
file,
r#"
on post-fs-data
start logd
exec {1} 0 0 -- {0}/magisk --post-fs-data
on property:vold.decrypt=trigger_restart_framework
exec {1} 0 0 -- {0}/magisk --service
on nonencrypted
exec {1} 0 0 -- {0}/magisk --service
on property:sys.boot_completed=1
exec {1} 0 0 -- {0}/magisk --boot-complete
on property:init.svc.zygote=stopped
exec {1} 0 0 -- {0}/magisk --zygote-restart
"#,
tmp_dir, "u:r:magisk:s0"
)
.ok();
mem::forget(file)
}