Introduce path! macro for FsPath

This commit is contained in:
topjohnwu 2025-02-17 01:46:19 -08:00
parent 18cb659ff3
commit fc2ef21660
11 changed files with 92 additions and 116 deletions

View File

@ -704,26 +704,27 @@ impl_str_buf_with_slice!(
(Utf8CStrBufArr<N>, const N: usize) (Utf8CStrBufArr<N>, const N: usize)
); );
// The cstr! macro is copied from https://github.com/bytecodealliance/rustix/blob/main/src/cstr.rs
#[macro_export] #[macro_export]
macro_rules! cstr { macro_rules! cstr {
($($str:tt)*) => {{ ($str:expr) => {{
assert!( const NULL_STR: &str = $crate::const_format::concatcp!($str, "\0");
!($($str)*).bytes().any(|b| b == b'\0'),
"cstr argument contains embedded NUL bytes",
);
#[allow(unused_unsafe)] #[allow(unused_unsafe)]
unsafe { unsafe {
$crate::Utf8CStr::from_bytes_unchecked($crate::const_format::concatcp!($($str)*, "\0") $crate::Utf8CStr::from_bytes_unchecked(NULL_STR.as_bytes())
.as_bytes())
} }
}}; }};
} }
#[macro_export] #[macro_export]
macro_rules! raw_cstr { macro_rules! raw_cstr {
($($str:tt)*) => {{ ($str:expr) => {{
$crate::cstr!($($str)*).as_ptr() $crate::cstr!($str).as_ptr()
}};
}
#[macro_export]
macro_rules! path {
($str:expr) => {{
$crate::FsPath::from($crate::cstr!($str))
}}; }};
} }

View File

@ -616,6 +616,7 @@ impl FsPath {
self.remove() self.remove()
} }
#[allow(clippy::unnecessary_cast)]
pub fn read_link(&self, buf: &mut dyn Utf8CStrBuf) -> io::Result<()> { pub fn read_link(&self, buf: &mut dyn Utf8CStrBuf) -> io::Result<()> {
buf.clear(); buf.clear();
unsafe { unsafe {

View File

@ -11,7 +11,7 @@ use crate::package::ManagerInfo;
use crate::su::SuInfo; use crate::su::SuInfo;
use base::libc::{O_CLOEXEC, O_RDONLY}; use base::libc::{O_CLOEXEC, O_RDONLY};
use base::{ use base::{
cstr, error, info, libc, open_fd, AtomicArc, BufReadExt, FsPath, FsPathBuf, ResultExt, Utf8CStr, cstr, error, info, libc, open_fd, path, AtomicArc, BufReadExt, FsPathBuf, ResultExt, Utf8CStr,
}; };
use std::fs::File; use std::fs::File;
use std::io::BufReader; use std::io::BufReader;
@ -105,7 +105,7 @@ impl MagiskD {
self.preserve_stub_apk(); self.preserve_stub_apk();
// Check secure dir // Check secure dir
let secure_dir = FsPath::from(cstr!(SECURE_DIR)); let secure_dir = path!(SECURE_DIR);
if !secure_dir.exists() { if !secure_dir.exists() {
if self.sdk_int < 24 { if self.sdk_int < 24 {
secure_dir.mkdir(0o700).log_ok(); secure_dir.mkdir(0o700).log_ok();
@ -172,7 +172,7 @@ impl MagiskD {
self.set_db_setting(DbEntryKey::BootloopCount, 0).log_ok(); self.set_db_setting(DbEntryKey::BootloopCount, 0).log_ok();
// At this point it's safe to create the folder // At this point it's safe to create the folder
let secure_dir = FsPath::from(cstr!(SECURE_DIR)); let secure_dir = path!(SECURE_DIR);
if !secure_dir.exists() { if !secure_dir.exists() {
secure_dir.mkdir(0o700).log_ok(); secure_dir.mkdir(0o700).log_ok();
} }
@ -244,7 +244,7 @@ pub fn daemon_entry() {
} }
let mut sdk_int = -1; let mut sdk_int = -1;
if let Ok(file) = FsPath::from(cstr!("/system/build.prop")).open(O_RDONLY | O_CLOEXEC) { if let Ok(file) = path!("/system/build.prop").open(O_RDONLY | O_CLOEXEC) {
let mut file = BufReader::new(file); let mut file = BufReader::new(file);
file.foreach_props(|key, val| { file.foreach_props(|key, val| {
if key == "ro.build.version.sdk" { if key == "ro.build.version.sdk" {

View File

@ -9,28 +9,19 @@ use std::{
use quick_protobuf::{BytesReader, MessageRead, MessageWrite, Writer}; use quick_protobuf::{BytesReader, MessageRead, MessageWrite, Writer};
use base::libc::{O_CLOEXEC, O_RDONLY};
use base::{
clone_attr, cstr, debug, libc::mkstemp, Directory, FsPath, FsPathBuf, LibcReturn, LoggedResult,
MappedFile, SilentResultExt, Utf8CStr, WalkResult,
};
use crate::ffi::{prop_cb_exec, PropCb}; use crate::ffi::{prop_cb_exec, PropCb};
use crate::resetprop::proto::persistent_properties::{ use crate::resetprop::proto::persistent_properties::{
mod_PersistentProperties::PersistentPropertyRecord, PersistentProperties, mod_PersistentProperties::PersistentPropertyRecord, PersistentProperties,
}; };
use base::const_format::concatcp;
use base::libc::{O_CLOEXEC, O_RDONLY};
use base::{
clone_attr, cstr, debug, libc::mkstemp, path, Directory, FsPathBuf, LibcReturn, LoggedResult,
MappedFile, SilentResultExt, Utf8CStr, WalkResult,
};
macro_rules! PERSIST_PROP_DIR { const PERSIST_PROP_DIR: &str = "/data/property";
() => { const PERSIST_PROP: &str = concatcp!(PERSIST_PROP_DIR, "/persistent_properties");
"/data/property"
};
}
macro_rules! PERSIST_PROP {
() => {
concat!(PERSIST_PROP_DIR!(), "/persistent_properties")
};
}
trait PropCbExec { trait PropCbExec {
fn exec(&mut self, name: &Utf8CStr, value: &Utf8CStr); fn exec(&mut self, name: &Utf8CStr, value: &Utf8CStr);
@ -73,11 +64,11 @@ impl PropExt for PersistentProperties {
} }
fn check_proto() -> bool { fn check_proto() -> bool {
FsPath::from(cstr!(PERSIST_PROP!())).exists() path!(PERSIST_PROP).exists()
} }
fn file_get_prop(name: &Utf8CStr) -> LoggedResult<String> { fn file_get_prop(name: &Utf8CStr) -> LoggedResult<String> {
let path = FsPathBuf::default().join(PERSIST_PROP_DIR!()).join(name); let path = FsPathBuf::default().join(PERSIST_PROP_DIR).join(name);
let mut file = path.open(O_RDONLY | O_CLOEXEC).silent()?; let mut file = path.open(O_RDONLY | O_CLOEXEC).silent()?;
debug!("resetprop: read prop from [{}]", path); debug!("resetprop: read prop from [{}]", path);
let mut s = String::new(); let mut s = String::new();
@ -86,10 +77,10 @@ fn file_get_prop(name: &Utf8CStr) -> LoggedResult<String> {
} }
fn file_set_prop(name: &Utf8CStr, value: Option<&Utf8CStr>) -> LoggedResult<()> { fn file_set_prop(name: &Utf8CStr, value: Option<&Utf8CStr>) -> LoggedResult<()> {
let path = FsPathBuf::default().join(PERSIST_PROP_DIR!()).join(name); let path = FsPathBuf::default().join(PERSIST_PROP_DIR).join(name);
if let Some(value) = value { if let Some(value) = value {
let mut tmp = FsPathBuf::default() let mut tmp = FsPathBuf::default()
.join(PERSIST_PROP_DIR!()) .join(PERSIST_PROP_DIR)
.join("prop.XXXXXX"); .join("prop.XXXXXX");
{ {
let mut f = unsafe { let mut f = unsafe {
@ -108,8 +99,8 @@ fn file_set_prop(name: &Utf8CStr, value: Option<&Utf8CStr>) -> LoggedResult<()>
} }
fn proto_read_props() -> LoggedResult<PersistentProperties> { fn proto_read_props() -> LoggedResult<PersistentProperties> {
debug!("resetprop: decode with protobuf [{}]", PERSIST_PROP!()); debug!("resetprop: decode with protobuf [{}]", PERSIST_PROP);
let m = MappedFile::open(cstr!(PERSIST_PROP!()))?; let m = MappedFile::open(cstr!(PERSIST_PROP))?;
let m = m.as_ref(); let m = m.as_ref();
let mut r = BytesReader::from_bytes(m); let mut r = BytesReader::from_bytes(m);
let mut props = PersistentProperties::from_reader(&mut r, m)?; let mut props = PersistentProperties::from_reader(&mut r, m)?;
@ -119,7 +110,7 @@ fn proto_read_props() -> LoggedResult<PersistentProperties> {
} }
fn proto_write_props(props: &PersistentProperties) -> LoggedResult<()> { fn proto_write_props(props: &PersistentProperties) -> LoggedResult<()> {
let mut tmp = FsPathBuf::default().join(concat!(PERSIST_PROP!(), ".XXXXXX")); let mut tmp = FsPathBuf::default().join(concatcp!(PERSIST_PROP, ".XXXXXX"));
{ {
let f = unsafe { let f = unsafe {
let fd = mkstemp(tmp.as_mut_ptr()).check_os_err()?; let fd = mkstemp(tmp.as_mut_ptr()).check_os_err()?;
@ -128,8 +119,8 @@ fn proto_write_props(props: &PersistentProperties) -> LoggedResult<()> {
debug!("resetprop: encode with protobuf [{}]", tmp); debug!("resetprop: encode with protobuf [{}]", tmp);
props.write_message(&mut Writer::new(BufWriter::new(f)))?; props.write_message(&mut Writer::new(BufWriter::new(f)))?;
} }
clone_attr(FsPath::from(cstr!(PERSIST_PROP!())), &tmp)?; clone_attr(path!(PERSIST_PROP), &tmp)?;
tmp.rename_to(cstr!(PERSIST_PROP!()))?; tmp.rename_to(cstr!(PERSIST_PROP))?;
Ok(()) Ok(())
} }
@ -168,7 +159,7 @@ pub fn persist_get_props(mut prop_cb: Pin<&mut PropCb>) {
} }
}); });
} else { } else {
let mut dir = Directory::open(cstr!(PERSIST_PROP_DIR!()))?; let mut dir = Directory::open(cstr!(PERSIST_PROP_DIR))?;
dir.pre_order_walk(|e| { dir.pre_order_walk(|e| {
if e.is_file() { if e.is_file() {
if let Ok(name) = Utf8CStr::from_cstr(e.d_name()) { if let Ok(name) = Utf8CStr::from_cstr(e.d_name()) {

View File

@ -1,5 +1,5 @@
use crate::ffi::{backup_init, BootConfig, MagiskInit}; use crate::ffi::{backup_init, BootConfig, MagiskInit};
use base::{cstr, debug, BytesExt, FsPath, MappedFile}; use base::{debug, path, BytesExt, MappedFile};
use std::ffi::CStr; use std::ffi::CStr;
impl BootConfig { impl BootConfig {
@ -27,11 +27,11 @@ impl BootConfig {
impl MagiskInit { impl MagiskInit {
pub(crate) fn check_two_stage(&self) -> bool { pub(crate) fn check_two_stage(&self) -> bool {
FsPath::from(cstr!("/first_stage_ramdisk")).exists() || path!("/first_stage_ramdisk").exists() ||
FsPath::from(cstr!("/second_stage_resources")).exists() || path!("/second_stage_resources").exists() ||
FsPath::from(cstr!("/system/bin/init")).exists() || path!("/system/bin/init").exists() ||
// Use the apex folder to determine whether 2SI (Android 10+) // Use the apex folder to determine whether 2SI (Android 10+)
FsPath::from(cstr!("/apex")).exists() || path!("/apex").exists() ||
// If we still have no indication, parse the original init and see what's up // If we still have no indication, parse the original init and see what's up
MappedFile::open(backup_init()) MappedFile::open(backup_init())
.map(|data| data.contains(b"selinux_setup")) .map(|data| data.contains(b"selinux_setup"))

View File

@ -4,9 +4,9 @@ use crate::{
logging::setup_klog, logging::setup_klog,
}; };
use base::{ use base::{
cstr, debug, info, debug, info,
libc::{basename, getpid, mount, umask}, libc::{basename, getpid, mount, umask},
raw_cstr, FsPath, LibcReturn, LoggedResult, ResultExt, path, raw_cstr, FsPath, LibcReturn, LoggedResult, ResultExt,
}; };
use std::{ use std::{
ffi::{c_char, CStr}, ffi::{c_char, CStr},
@ -48,40 +48,36 @@ impl MagiskInit {
info!("RootFS Init"); info!("RootFS Init");
self.prepare_data(); self.prepare_data();
debug!("Restoring /init\n"); debug!("Restoring /init\n");
FsPath::from(cstr!("/.backup/init")) path!("/.backup/init").rename_to(path!("/init")).log_ok();
.rename_to(FsPath::from(cstr!("/init")))
.log()
.ok();
self.patch_rw_root(); self.patch_rw_root();
} }
pub(crate) fn recovery(&self) { pub(crate) fn recovery(&self) {
info!("Ramdisk is recovery, abort"); info!("Ramdisk is recovery, abort");
self.restore_ramdisk_init(); self.restore_ramdisk_init();
FsPath::from(cstr!("/.backup")).remove_all().ok(); path!("/.backup").remove_all().ok();
} }
pub(crate) fn restore_ramdisk_init(&self) { pub(crate) fn restore_ramdisk_init(&self) {
FsPath::from(cstr!("/init")).remove().ok(); path!("/init").remove().ok();
let orig_init = FsPath::from(backup_init()); let orig_init = FsPath::from(backup_init());
if orig_init.exists() { if orig_init.exists() {
orig_init.rename_to(FsPath::from(cstr!("/init"))).log_ok(); orig_init.rename_to(path!("/init")).log_ok();
} else { } else {
// If the backup init is missing, this means that the boot ramdisk // If the backup init is missing, this means that the boot ramdisk
// was created from scratch, and the real init is in a separate CPIO, // was created from scratch, and the real init is in a separate CPIO,
// which is guaranteed to be placed at /system/bin/init. // which is guaranteed to be placed at /system/bin/init.
FsPath::from(cstr!("/system/bin/init")) path!("/system/bin/init")
.symlink_to(FsPath::from(cstr!("/init"))) .symlink_to(path!("/init"))
.log() .log_ok();
.ok();
} }
} }
fn start(&mut self) -> LoggedResult<()> { fn start(&mut self) -> LoggedResult<()> {
if !FsPath::from(cstr!("/proc/cmdline")).exists() { if !path!("/proc/cmdline").exists() {
FsPath::from(cstr!("/proc")).mkdir(0o755)?; path!("/proc").mkdir(0o755)?;
unsafe { unsafe {
mount( mount(
raw_cstr!("proc"), raw_cstr!("proc"),
@ -94,8 +90,8 @@ impl MagiskInit {
.as_os_err()?; .as_os_err()?;
self.mount_list.push("/proc".to_string()); self.mount_list.push("/proc".to_string());
} }
if !FsPath::from(cstr!("/sys/block")).exists() { if !path!("/sys/block").exists() {
FsPath::from(cstr!("/sys")).mkdir(0o755)?; path!("/sys").mkdir(0o755)?;
unsafe { unsafe {
mount( mount(
raw_cstr!("sysfs"), raw_cstr!("sysfs"),
@ -120,9 +116,7 @@ impl MagiskInit {
self.legacy_system_as_root(); self.legacy_system_as_root();
} else if self.config.force_normal_boot { } else if self.config.force_normal_boot {
self.first_stage(); self.first_stage();
} else if FsPath::from(cstr!("/sbin/recovery")).exists() } else if path!("/sbin/recovery").exists() || path!("/system/bin/recovery").exists() {
|| FsPath::from(cstr!("/system/bin/recovery")).exists()
{
self.recovery(); self.recovery();
} else if self.check_two_stage() { } else if self.check_two_stage() {
self.first_stage(); self.first_stage();

View File

@ -4,7 +4,7 @@ use base::{
makedev, mknod, syscall, SYS_dup3, O_CLOEXEC, O_RDWR, O_WRONLY, STDERR_FILENO, makedev, mknod, syscall, SYS_dup3, O_CLOEXEC, O_RDWR, O_WRONLY, STDERR_FILENO,
STDIN_FILENO, STDOUT_FILENO, S_IFCHR, STDIN_FILENO, STDOUT_FILENO, S_IFCHR,
}, },
open_fd, raw_cstr, FsPath, LogLevel, Logger, Utf8CStr, LOGGER, open_fd, path, raw_cstr, LogLevel, Logger, Utf8CStr, LOGGER,
}; };
use std::{ use std::{
fs::File, fs::File,
@ -23,7 +23,7 @@ pub fn setup_klog() {
if fd.is_err() { if fd.is_err() {
mknod(raw_cstr!("/null"), S_IFCHR | 0o666, makedev(1, 3)); mknod(raw_cstr!("/null"), S_IFCHR | 0o666, makedev(1, 3));
fd = open_fd!(cstr!("/null"), O_RDWR | O_CLOEXEC); fd = open_fd!(cstr!("/null"), O_RDWR | O_CLOEXEC);
FsPath::from(cstr!("/null")).remove().ok(); path!("/null").remove().ok();
} }
if let Ok(ref fd) = fd { if let Ok(ref fd) = fd {
syscall(SYS_dup3, fd, STDIN_FILENO, O_CLOEXEC); syscall(SYS_dup3, fd, STDIN_FILENO, O_CLOEXEC);
@ -36,7 +36,7 @@ pub fn setup_klog() {
if fd.is_err() { if fd.is_err() {
mknod(raw_cstr!("/kmsg"), S_IFCHR | 0o666, makedev(1, 11)); mknod(raw_cstr!("/kmsg"), S_IFCHR | 0o666, makedev(1, 11));
fd = open_fd!(cstr!("/kmsg"), O_WRONLY | O_CLOEXEC); fd = open_fd!(cstr!("/kmsg"), O_WRONLY | O_CLOEXEC);
FsPath::from(cstr!("/kmsg")).remove().ok(); path!("/kmsg").remove().ok();
} }
KMSG = fd.map(|fd| fd.into_raw_fd()).unwrap_or(-1); KMSG = fd.map(|fd| fd.into_raw_fd()).unwrap_or(-1);
} }

View File

@ -2,7 +2,7 @@ use crate::ffi::MagiskInit;
use base::{ use base::{
cstr, debug, libc, cstr, debug, libc,
libc::{chdir, chroot, execve, exit, mount, umount2, MNT_DETACH, MS_MOVE}, libc::{chdir, chroot, execve, exit, mount, umount2, MNT_DETACH, MS_MOVE},
parse_mount_info, raw_cstr, Directory, FsPath, LibcReturn, LoggedResult, ResultExt, StringExt, parse_mount_info, path, raw_cstr, Directory, LibcReturn, LoggedResult, ResultExt, StringExt,
Utf8CStr, Utf8CStr,
}; };
use cxx::CxxString; use cxx::CxxString;
@ -76,7 +76,7 @@ pub fn is_device_mounted(dev: u64, target: Pin<&mut CxxString>) -> bool {
impl MagiskInit { impl MagiskInit {
pub(crate) fn prepare_data(&self) { pub(crate) fn prepare_data(&self) {
debug!("Setup data tmp"); debug!("Setup data tmp");
FsPath::from(cstr!("/data")).mkdir(0o755).log_ok(); path!("/data").mkdir(0o755).log_ok();
unsafe { unsafe {
mount( mount(
raw_cstr!("magisk"), raw_cstr!("magisk"),
@ -85,11 +85,15 @@ impl MagiskInit {
0, 0,
raw_cstr!("mode=755").cast(), raw_cstr!("mode=755").cast(),
) )
}.as_os_err().log_ok(); }
.as_os_err()
.log_ok();
FsPath::from(cstr!("/init")).copy_to(FsPath::from(cstr!("/data/magiskinit"))).log_ok(); path!("/init").copy_to(path!("/data/magiskinit")).log_ok();
FsPath::from(cstr!("/.backup")).copy_to(FsPath::from(cstr!("/data/.backup"))).log_ok(); path!("/.backup").copy_to(path!("/data/.backup")).log_ok();
FsPath::from(cstr!("/overlay.d")).copy_to(FsPath::from(cstr!("/data/overlay.d"))).log_ok(); path!("/overlay.d")
.copy_to(path!("/data/overlay.d"))
.log_ok();
} }
pub(crate) fn exec_init(&self) { pub(crate) fn exec_init(&self) {

View File

@ -1,8 +1,8 @@
use crate::ffi::MagiskInit; use crate::ffi::MagiskInit;
use base::libc::O_RDONLY; use base::libc::O_RDONLY;
use base::{ use base::{
cstr, debug, libc, BufReadExt, Directory, FsPath, LibcReturn, LoggedResult, ResultExt, debug, libc, path, BufReadExt, Directory, LibcReturn, LoggedResult, ResultExt, Utf8CStr,
Utf8CStr, Utf8CStrBuf, Utf8CStrBufArr, WalkResult, Utf8CStrBuf, Utf8CStrBufArr, WalkResult,
}; };
use std::io::BufReader; use std::io::BufReader;
use std::{ use std::{
@ -116,7 +116,7 @@ on property:init.svc.zygote=stopped
impl MagiskInit { impl MagiskInit {
pub(crate) fn parse_config_file(&mut self) { pub(crate) fn parse_config_file(&mut self) {
if let Ok(fd) = FsPath::from(cstr!("/data/.backup/.magisk")).open(O_RDONLY) { if let Ok(fd) = path!("/data/.backup/.magisk").open(O_RDONLY) {
let mut reader = BufReader::new(fd); let mut reader = BufReader::new(fd);
reader.foreach_props(|key, val| { reader.foreach_props(|key, val| {
if key == "PREINITDEVICE" { if key == "PREINITDEVICE" {

View File

@ -1,5 +1,5 @@
use crate::ffi::MagiskInit; use crate::ffi::MagiskInit;
use base::{cstr, debug, FsPath, Utf8CStr}; use base::{debug, path, Utf8CStr};
use magiskpolicy::ffi::SePolicy; use magiskpolicy::ffi::SePolicy;
impl MagiskInit { impl MagiskInit {
@ -10,7 +10,7 @@ impl MagiskInit {
sepol.magisk_rules(); sepol.magisk_rules();
// Custom rules // Custom rules
let rule = FsPath::from(cstr!("/data/.magisk/preinit/sepolicy.rule")); let rule = path!("/data/.magisk/preinit/sepolicy.rule");
if rule.exists() { if rule.exists() {
debug!("Loading custom sepolicy patch: [{}]", rule); debug!("Loading custom sepolicy patch: [{}]", rule);
sepol.load_rule_file(rule); sepol.load_rule_file(rule);
@ -20,10 +20,10 @@ impl MagiskInit {
sepol.to_file(out); sepol.to_file(out);
// Remove OnePlus stupid debug sepolicy and use our own // Remove OnePlus stupid debug sepolicy and use our own
let sepol_debug = FsPath::from(cstr!("/sepolicy_debug")); let sepol_debug = path!("/sepolicy_debug");
if sepol_debug.exists() { if sepol_debug.exists() {
sepol_debug.remove().ok(); sepol_debug.remove().ok();
FsPath::from(cstr!("/sepolicy")).link_to(sepol_debug).ok(); path!("/sepolicy").link_to(sepol_debug).ok();
} }
} }
} }

View File

@ -5,7 +5,7 @@ use base::{
fstatat, mount, stat, statfs, umount2, AT_SYMLINK_NOFOLLOW, MNT_DETACH, MS_BIND, O_CLOEXEC, fstatat, mount, stat, statfs, umount2, AT_SYMLINK_NOFOLLOW, MNT_DETACH, MS_BIND, O_CLOEXEC,
O_CREAT, O_RDONLY, O_WRONLY, TMPFS_MAGIC, O_CREAT, O_RDONLY, O_WRONLY, TMPFS_MAGIC,
}, },
raw_cstr, FsPath, LibcReturn, MappedFile, MutBytesExt, ResultExt, path, raw_cstr, LibcReturn, MappedFile, MutBytesExt, ResultExt,
}; };
use std::{ffi::c_long, io::Write, ptr::null}; use std::{ffi::c_long, io::Write, ptr::null};
@ -25,38 +25,26 @@ impl MagiskInit {
) != 0 ) != 0
} { } {
if self.config.force_normal_boot { if self.config.force_normal_boot {
FsPath::from(cstr!("/first_stage_ramdisk/storage/self")) path!("/first_stage_ramdisk/storage/self")
.mkdirs(0o755) .mkdirs(0o755)
.log() .log_ok();
.ok(); path!("/system/system/bin/init")
FsPath::from(cstr!("/system/system/bin/init")) .symlink_to(path!("/first_stage_ramdisk/storage/self/primary"))
.symlink_to(FsPath::from(cstr!( .log_ok();
"/first_stage_ramdisk/storage/self/primary"
)))
.log()
.ok();
debug!( debug!(
"Symlink /first_stage_ramdisk/storage/self/primary -> /system/system/bin/init" "Symlink /first_stage_ramdisk/storage/self/primary -> /system/system/bin/init"
); );
FsPath::from(cstr!("/first_stage_ramdisk/sdcard")) path!("/first_stage_ramdisk/sdcard")
.create(O_RDONLY | O_CREAT | O_CLOEXEC, 0) .create(O_RDONLY | O_CREAT | O_CLOEXEC, 0)
.log() .log_ok();
.ok();
} else { } else {
FsPath::from(cstr!("/storage/self")) path!("/storage/self").mkdirs(0o755).log_ok();
.mkdirs(0o755) path!("/system/system/bin/init")
.log() .symlink_to(path!("/storage/self/primary"))
.ok(); .log_ok();
FsPath::from(cstr!("/system/system/bin/init"))
.symlink_to(FsPath::from(cstr!("/storage/self/primary")))
.log()
.ok();
debug!("Symlink /storage/self/primary -> /system/system/bin/init"); debug!("Symlink /storage/self/primary -> /system/system/bin/init");
} }
FsPath::from(cstr!("/init")) path!("/init").rename_to(path!("/sdcard")).log_ok();
.rename_to(FsPath::from(cstr!("/sdcard")))
.log()
.ok();
// Try to keep magiskinit in rootfs for samsung RKP // Try to keep magiskinit in rootfs for samsung RKP
if unsafe { if unsafe {
mount( mount(
@ -103,8 +91,8 @@ impl MagiskInit {
} }
pub(crate) fn redirect_second_stage(&self) { pub(crate) fn redirect_second_stage(&self) {
let src = FsPath::from(cstr!("/init")); let src = path!("/init");
let dest = FsPath::from(cstr!("/data/init")); let dest = path!("/data/init");
// Patch init binary // Patch init binary
if let Ok(mut map) = MappedFile::open(src) { if let Ok(mut map) = MappedFile::open(src) {
let from = "/system/bin/init"; let from = "/system/bin/init";
@ -136,7 +124,7 @@ impl MagiskInit {
unsafe { unsafe {
umount2(raw_cstr!("/init"), MNT_DETACH); umount2(raw_cstr!("/init"), MNT_DETACH);
umount2(raw_cstr!("/system/bin/init"), MNT_DETACH); // just in case umount2(raw_cstr!("/system/bin/init"), MNT_DETACH); // just in case
FsPath::from(cstr!("/data/init")).remove().ok(); path!("/data/init").remove().ok();
// Make sure init dmesg logs won't get messed up // Make sure init dmesg logs won't get messed up
*self.argv = raw_cstr!("/system/bin/init") as *mut _; *self.argv = raw_cstr!("/system/bin/init") as *mut _;
@ -146,12 +134,9 @@ impl MagiskInit {
statfs(raw_cstr!("/"), &mut sfs); statfs(raw_cstr!("/"), &mut sfs);
if sfs.f_type == 0x858458f6 || sfs.f_type as c_long == TMPFS_MAGIC { if sfs.f_type == 0x858458f6 || sfs.f_type as c_long == TMPFS_MAGIC {
// We are still on rootfs, so make sure we will execute the init of the 2nd stage // We are still on rootfs, so make sure we will execute the init of the 2nd stage
let init_path = FsPath::from(cstr!("/init")); let init_path = path!("/init");
init_path.remove().ok(); init_path.remove().ok();
FsPath::from(cstr!("/system/bin/init")) path!("/system/bin/init").symlink_to(init_path).log_ok();
.symlink_to(init_path)
.log()
.ok();
self.patch_rw_root(); self.patch_rw_root();
} else { } else {
self.patch_ro_root(); self.patch_ro_root();