mirror of
https://github.com/topjohnwu/Magisk.git
synced 2026-01-17 15:23:31 -08:00
Introduce path! macro for FsPath
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use crate::ffi::{backup_init, BootConfig, MagiskInit};
|
||||
use base::{cstr, debug, BytesExt, FsPath, MappedFile};
|
||||
use base::{debug, path, BytesExt, MappedFile};
|
||||
use std::ffi::CStr;
|
||||
|
||||
impl BootConfig {
|
||||
@@ -27,11 +27,11 @@ impl BootConfig {
|
||||
|
||||
impl MagiskInit {
|
||||
pub(crate) fn check_two_stage(&self) -> bool {
|
||||
FsPath::from(cstr!("/first_stage_ramdisk")).exists() ||
|
||||
FsPath::from(cstr!("/second_stage_resources")).exists() ||
|
||||
FsPath::from(cstr!("/system/bin/init")).exists() ||
|
||||
path!("/first_stage_ramdisk").exists() ||
|
||||
path!("/second_stage_resources").exists() ||
|
||||
path!("/system/bin/init").exists() ||
|
||||
// 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
|
||||
MappedFile::open(backup_init())
|
||||
.map(|data| data.contains(b"selinux_setup"))
|
||||
|
||||
@@ -4,9 +4,9 @@ use crate::{
|
||||
logging::setup_klog,
|
||||
};
|
||||
use base::{
|
||||
cstr, debug, info,
|
||||
debug, info,
|
||||
libc::{basename, getpid, mount, umask},
|
||||
raw_cstr, FsPath, LibcReturn, LoggedResult, ResultExt,
|
||||
path, raw_cstr, FsPath, LibcReturn, LoggedResult, ResultExt,
|
||||
};
|
||||
use std::{
|
||||
ffi::{c_char, CStr},
|
||||
@@ -48,40 +48,36 @@ impl MagiskInit {
|
||||
info!("RootFS Init");
|
||||
self.prepare_data();
|
||||
debug!("Restoring /init\n");
|
||||
FsPath::from(cstr!("/.backup/init"))
|
||||
.rename_to(FsPath::from(cstr!("/init")))
|
||||
.log()
|
||||
.ok();
|
||||
path!("/.backup/init").rename_to(path!("/init")).log_ok();
|
||||
self.patch_rw_root();
|
||||
}
|
||||
|
||||
pub(crate) fn recovery(&self) {
|
||||
info!("Ramdisk is recovery, abort");
|
||||
self.restore_ramdisk_init();
|
||||
FsPath::from(cstr!("/.backup")).remove_all().ok();
|
||||
path!("/.backup").remove_all().ok();
|
||||
}
|
||||
|
||||
pub(crate) fn restore_ramdisk_init(&self) {
|
||||
FsPath::from(cstr!("/init")).remove().ok();
|
||||
path!("/init").remove().ok();
|
||||
|
||||
let orig_init = FsPath::from(backup_init());
|
||||
|
||||
if orig_init.exists() {
|
||||
orig_init.rename_to(FsPath::from(cstr!("/init"))).log_ok();
|
||||
orig_init.rename_to(path!("/init")).log_ok();
|
||||
} else {
|
||||
// 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,
|
||||
// which is guaranteed to be placed at /system/bin/init.
|
||||
FsPath::from(cstr!("/system/bin/init"))
|
||||
.symlink_to(FsPath::from(cstr!("/init")))
|
||||
.log()
|
||||
.ok();
|
||||
path!("/system/bin/init")
|
||||
.symlink_to(path!("/init"))
|
||||
.log_ok();
|
||||
}
|
||||
}
|
||||
|
||||
fn start(&mut self) -> LoggedResult<()> {
|
||||
if !FsPath::from(cstr!("/proc/cmdline")).exists() {
|
||||
FsPath::from(cstr!("/proc")).mkdir(0o755)?;
|
||||
if !path!("/proc/cmdline").exists() {
|
||||
path!("/proc").mkdir(0o755)?;
|
||||
unsafe {
|
||||
mount(
|
||||
raw_cstr!("proc"),
|
||||
@@ -94,8 +90,8 @@ impl MagiskInit {
|
||||
.as_os_err()?;
|
||||
self.mount_list.push("/proc".to_string());
|
||||
}
|
||||
if !FsPath::from(cstr!("/sys/block")).exists() {
|
||||
FsPath::from(cstr!("/sys")).mkdir(0o755)?;
|
||||
if !path!("/sys/block").exists() {
|
||||
path!("/sys").mkdir(0o755)?;
|
||||
unsafe {
|
||||
mount(
|
||||
raw_cstr!("sysfs"),
|
||||
@@ -120,9 +116,7 @@ impl MagiskInit {
|
||||
self.legacy_system_as_root();
|
||||
} else if self.config.force_normal_boot {
|
||||
self.first_stage();
|
||||
} else if FsPath::from(cstr!("/sbin/recovery")).exists()
|
||||
|| FsPath::from(cstr!("/system/bin/recovery")).exists()
|
||||
{
|
||||
} else if path!("/sbin/recovery").exists() || path!("/system/bin/recovery").exists() {
|
||||
self.recovery();
|
||||
} else if self.check_two_stage() {
|
||||
self.first_stage();
|
||||
|
||||
@@ -4,7 +4,7 @@ use base::{
|
||||
makedev, mknod, syscall, SYS_dup3, O_CLOEXEC, O_RDWR, O_WRONLY, STDERR_FILENO,
|
||||
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::{
|
||||
fs::File,
|
||||
@@ -23,7 +23,7 @@ pub fn setup_klog() {
|
||||
if fd.is_err() {
|
||||
mknod(raw_cstr!("/null"), S_IFCHR | 0o666, makedev(1, 3));
|
||||
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 {
|
||||
syscall(SYS_dup3, fd, STDIN_FILENO, O_CLOEXEC);
|
||||
@@ -36,7 +36,7 @@ pub fn setup_klog() {
|
||||
if fd.is_err() {
|
||||
mknod(raw_cstr!("/kmsg"), S_IFCHR | 0o666, makedev(1, 11));
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ use crate::ffi::MagiskInit;
|
||||
use base::{
|
||||
cstr, debug, libc,
|
||||
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,
|
||||
};
|
||||
use cxx::CxxString;
|
||||
@@ -76,7 +76,7 @@ pub fn is_device_mounted(dev: u64, target: Pin<&mut CxxString>) -> bool {
|
||||
impl MagiskInit {
|
||||
pub(crate) fn prepare_data(&self) {
|
||||
debug!("Setup data tmp");
|
||||
FsPath::from(cstr!("/data")).mkdir(0o755).log_ok();
|
||||
path!("/data").mkdir(0o755).log_ok();
|
||||
unsafe {
|
||||
mount(
|
||||
raw_cstr!("magisk"),
|
||||
@@ -85,11 +85,15 @@ impl MagiskInit {
|
||||
0,
|
||||
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();
|
||||
FsPath::from(cstr!("/.backup")).copy_to(FsPath::from(cstr!("/data/.backup"))).log_ok();
|
||||
FsPath::from(cstr!("/overlay.d")).copy_to(FsPath::from(cstr!("/data/overlay.d"))).log_ok();
|
||||
path!("/init").copy_to(path!("/data/magiskinit")).log_ok();
|
||||
path!("/.backup").copy_to(path!("/data/.backup")).log_ok();
|
||||
path!("/overlay.d")
|
||||
.copy_to(path!("/data/overlay.d"))
|
||||
.log_ok();
|
||||
}
|
||||
|
||||
pub(crate) fn exec_init(&self) {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
use crate::ffi::MagiskInit;
|
||||
use base::libc::O_RDONLY;
|
||||
use base::{
|
||||
cstr, debug, libc, BufReadExt, Directory, FsPath, LibcReturn, LoggedResult, ResultExt,
|
||||
Utf8CStr, Utf8CStrBuf, Utf8CStrBufArr, WalkResult,
|
||||
debug, libc, path, BufReadExt, Directory, LibcReturn, LoggedResult, ResultExt, Utf8CStr,
|
||||
Utf8CStrBuf, Utf8CStrBufArr, WalkResult,
|
||||
};
|
||||
use std::io::BufReader;
|
||||
use std::{
|
||||
@@ -116,7 +116,7 @@ on property:init.svc.zygote=stopped
|
||||
|
||||
impl MagiskInit {
|
||||
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);
|
||||
reader.foreach_props(|key, val| {
|
||||
if key == "PREINITDEVICE" {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::ffi::MagiskInit;
|
||||
use base::{cstr, debug, FsPath, Utf8CStr};
|
||||
use base::{debug, path, Utf8CStr};
|
||||
use magiskpolicy::ffi::SePolicy;
|
||||
|
||||
impl MagiskInit {
|
||||
@@ -10,7 +10,7 @@ impl MagiskInit {
|
||||
sepol.magisk_rules();
|
||||
|
||||
// Custom rules
|
||||
let rule = FsPath::from(cstr!("/data/.magisk/preinit/sepolicy.rule"));
|
||||
let rule = path!("/data/.magisk/preinit/sepolicy.rule");
|
||||
if rule.exists() {
|
||||
debug!("Loading custom sepolicy patch: [{}]", rule);
|
||||
sepol.load_rule_file(rule);
|
||||
@@ -20,10 +20,10 @@ impl MagiskInit {
|
||||
sepol.to_file(out);
|
||||
|
||||
// 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() {
|
||||
sepol_debug.remove().ok();
|
||||
FsPath::from(cstr!("/sepolicy")).link_to(sepol_debug).ok();
|
||||
path!("/sepolicy").link_to(sepol_debug).ok();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,7 +5,7 @@ use base::{
|
||||
fstatat, mount, stat, statfs, umount2, AT_SYMLINK_NOFOLLOW, MNT_DETACH, MS_BIND, O_CLOEXEC,
|
||||
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};
|
||||
|
||||
@@ -25,38 +25,26 @@ impl MagiskInit {
|
||||
) != 0
|
||||
} {
|
||||
if self.config.force_normal_boot {
|
||||
FsPath::from(cstr!("/first_stage_ramdisk/storage/self"))
|
||||
path!("/first_stage_ramdisk/storage/self")
|
||||
.mkdirs(0o755)
|
||||
.log()
|
||||
.ok();
|
||||
FsPath::from(cstr!("/system/system/bin/init"))
|
||||
.symlink_to(FsPath::from(cstr!(
|
||||
"/first_stage_ramdisk/storage/self/primary"
|
||||
)))
|
||||
.log()
|
||||
.ok();
|
||||
.log_ok();
|
||||
path!("/system/system/bin/init")
|
||||
.symlink_to(path!("/first_stage_ramdisk/storage/self/primary"))
|
||||
.log_ok();
|
||||
debug!(
|
||||
"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)
|
||||
.log()
|
||||
.ok();
|
||||
.log_ok();
|
||||
} else {
|
||||
FsPath::from(cstr!("/storage/self"))
|
||||
.mkdirs(0o755)
|
||||
.log()
|
||||
.ok();
|
||||
FsPath::from(cstr!("/system/system/bin/init"))
|
||||
.symlink_to(FsPath::from(cstr!("/storage/self/primary")))
|
||||
.log()
|
||||
.ok();
|
||||
path!("/storage/self").mkdirs(0o755).log_ok();
|
||||
path!("/system/system/bin/init")
|
||||
.symlink_to(path!("/storage/self/primary"))
|
||||
.log_ok();
|
||||
debug!("Symlink /storage/self/primary -> /system/system/bin/init");
|
||||
}
|
||||
FsPath::from(cstr!("/init"))
|
||||
.rename_to(FsPath::from(cstr!("/sdcard")))
|
||||
.log()
|
||||
.ok();
|
||||
path!("/init").rename_to(path!("/sdcard")).log_ok();
|
||||
// Try to keep magiskinit in rootfs for samsung RKP
|
||||
if unsafe {
|
||||
mount(
|
||||
@@ -103,8 +91,8 @@ impl MagiskInit {
|
||||
}
|
||||
|
||||
pub(crate) fn redirect_second_stage(&self) {
|
||||
let src = FsPath::from(cstr!("/init"));
|
||||
let dest = FsPath::from(cstr!("/data/init"));
|
||||
let src = path!("/init");
|
||||
let dest = path!("/data/init");
|
||||
// Patch init binary
|
||||
if let Ok(mut map) = MappedFile::open(src) {
|
||||
let from = "/system/bin/init";
|
||||
@@ -136,7 +124,7 @@ impl MagiskInit {
|
||||
unsafe {
|
||||
umount2(raw_cstr!("/init"), MNT_DETACH);
|
||||
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
|
||||
*self.argv = raw_cstr!("/system/bin/init") as *mut _;
|
||||
@@ -146,12 +134,9 @@ impl MagiskInit {
|
||||
statfs(raw_cstr!("/"), &mut sfs);
|
||||
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
|
||||
let init_path = FsPath::from(cstr!("/init"));
|
||||
let init_path = path!("/init");
|
||||
init_path.remove().ok();
|
||||
FsPath::from(cstr!("/system/bin/init"))
|
||||
.symlink_to(init_path)
|
||||
.log()
|
||||
.ok();
|
||||
path!("/system/bin/init").symlink_to(init_path).log_ok();
|
||||
self.patch_rw_root();
|
||||
} else {
|
||||
self.patch_ro_root();
|
||||
|
||||
Reference in New Issue
Block a user