Skip to content

Commit 95bb256

Browse files
Howard20181topjohnwu
authored andcommitted
Support replace rc scripts in system partition
1 parent 2da68f7 commit 95bb256

4 files changed

Lines changed: 43 additions & 27 deletions

File tree

native/src/init/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub mod ffi {
9292
fn mount_overlay(self: &mut MagiskInit, dest: Utf8CStrRef);
9393
fn handle_sepolicy(self: &mut MagiskInit);
9494
fn restore_overlay_contexts(self: &MagiskInit);
95-
fn load_overlay_rc(self: &mut MagiskInit, overlay: &Utf8CStrRef);
95+
fn load_overlay_rc(self: &mut MagiskInit, overlay: &Utf8CStrRef, module_path: &Utf8CStrRef);
9696
fn handle_modules_rc(self: &mut MagiskInit, root_dir: &Utf8CStrRef);
9797
}
9898
unsafe extern "C++" {

native/src/init/rootdir.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ void MagiskInit::patch_ro_root() noexcept {
275275
close(dest);
276276
}
277277

278-
load_overlay_rc(ROOTOVL);
278+
load_overlay_rc(ROOTOVL, "");
279279
if (access(ROOTOVL "/sbin", F_OK) == 0) {
280280
// Move files in overlay.d/sbin into tmp_dir
281281
mv_path(ROOTOVL "/sbin", ".");
@@ -318,7 +318,7 @@ void MagiskInit::patch_rw_root() noexcept {
318318
link_path("/sbin", "/root");
319319

320320
// Handle overlays
321-
load_overlay_rc("/overlay.d");
321+
load_overlay_rc("/overlay.d", "");
322322
mv_path("/overlay.d", "/");
323323
rm_rf("/data/overlay.d");
324324
rm_rf("/.backup");

native/src/init/rootdir.rs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl MagiskInit {
6666
}
6767
}
6868

69-
pub(crate) fn load_overlay_rc(&mut self, overlay: &Utf8CStr) {
69+
pub(crate) fn load_overlay_rc(&mut self, overlay: &Utf8CStr, module_path: &Utf8CStr) {
7070
if let Ok(mut dir) = Directory::open(overlay) {
7171
let init_rc = cstr::buf::dynamic(256)
7272
.join_path(overlay)
@@ -78,24 +78,40 @@ impl MagiskInit {
7878
loop {
7979
match dir.read() {
8080
Ok(None) => break,
81-
Ok(Some(e)) if e.is_file() && e.name().ends_with(".rc") => {
82-
let name = e.name();
83-
let mut path = cstr::buf::dynamic(256).join_path("/").join_path(name);
84-
if path.exists() {
85-
debug!("Replace rc script [{}]", name);
86-
} else {
87-
debug!("Found rc script [{}]", name);
88-
path = cstr::buf::dynamic(256).join_path(overlay).join_path(name);
89-
let mut rc_content = String::new();
90-
if let Ok(mut file) = path.open(O_RDONLY | O_CLOEXEC) {
91-
file.read_to_string(&mut rc_content).log_ok();
92-
self.rc_list.push(rc_content);
93-
drop(file);
94-
path.remove().log_ok();
81+
Ok(Some(e)) => {
82+
let recursive = cstr::buf::dynamic(256).join_path(module_path).exists();
83+
if (recursive && e.is_dir()) || e.is_file() {
84+
let buf = &mut cstr::buf::dynamic(256);
85+
e.resolve_path(buf).log_ok();
86+
if e.is_file() && buf.ends_with(".rc") {
87+
let mut path;
88+
if recursive {
89+
path = cstr::buf::dynamic(256)
90+
.join_path(buf.replace(module_path.as_str(), ""));
91+
} else {
92+
path =
93+
cstr::buf::dynamic(256).join_path("/").join_path(e.name());
94+
}
95+
if path.exists() {
96+
debug!("Replace rc script [{}] -> [{}]", path, buf);
97+
} else {
98+
path = cstr::buf::dynamic(256).join_path(buf);
99+
debug!("Found rc script [{}]", path);
100+
let mut rc_content = String::new();
101+
if let Ok(mut file) = path.open(O_RDONLY | O_CLOEXEC) {
102+
file.read_to_string(&mut rc_content).log_ok();
103+
self.rc_list.push(rc_content);
104+
drop(file);
105+
path.remove().log_ok();
106+
}
107+
}
108+
} else if e.is_dir() {
109+
self.load_overlay_rc(buf, module_path);
95110
}
111+
} else {
112+
continue;
96113
}
97114
}
98-
Ok(_) => continue,
99115
Err(_) => break,
100116
}
101117
}
@@ -108,12 +124,10 @@ impl MagiskInit {
108124
match dir.read() {
109125
Ok(None) => break,
110126
Ok(Some(e)) if e.is_dir() => {
111-
let name = e.name();
112-
let path = cstr::buf::dynamic(256)
113-
.join_path("/data")
114-
.join_path(PREINITMIRR)
115-
.join_path(name);
116-
self.load_overlay_rc(&path);
127+
let buf = &mut cstr::buf::dynamic(256);
128+
e.resolve_path(buf).log_ok();
129+
let path = cstr::buf::dynamic(256).join_path(&mut *buf);
130+
self.load_overlay_rc(&path, buf);
117131
let desc_path = cstr::buf::dynamic(256).join_path(root_dir);
118132
path.copy_to(&desc_path).log_ok();
119133
}

scripts/util_functions.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,8 +593,10 @@ copy_preinit_files() {
593593
[ -f "$MODDIR"/remove ] && continue
594594
[ -f "$MODDIR"/update ] && continue
595595
find "$MODDIR" -mindepth 1 -type f -name "*.rc" | while read -r file; do
596-
mkdir -p "$PREINITDIR/$MODID"
597-
cp "$file" "$PREINITDIR/$MODID/"
596+
local path="${file%/*}"
597+
path="$PREINITDIR/$MODID${path/$MODDIR/}"
598+
mkdir -p "$path"
599+
cp "$file" "$path/"
598600
done
599601
done
600602
}

0 commit comments

Comments
 (0)