From be433fa66706928e1decd1f62cd0e72e1405b76f Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Mon, 26 Feb 2024 00:26:23 -0800 Subject: [PATCH] Use Rust for formatting The fprintf implementation included in crt0 is too rudimental --- native/src/init/lib.rs | 13 +++++++++++++ native/src/init/rootdir.cpp | 19 +------------------ native/src/init/rootdir.rs | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 18 deletions(-) create mode 100644 native/src/init/rootdir.rs diff --git a/native/src/init/lib.rs b/native/src/init/lib.rs index 7c0aeac2c..77a8ee3df 100644 --- a/native/src/init/lib.rs +++ b/native/src/init/lib.rs @@ -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>; } } diff --git a/native/src/init/rootdir.cpp b/native/src/init/rootdir.cpp index c9667c287..0fffc8d58 100644 --- a/native/src/init/rootdir.cpp +++ b/native/src/init/rootdir.cpp @@ -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())); } diff --git a/native/src/init/rootdir.rs b/native/src/init/rootdir.rs new file mode 100644 index 000000000..4fbe45dee --- /dev/null +++ b/native/src/init/rootdir.rs @@ -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) +}