Magisk/native/src/core/daemon.rs

197 lines
5.5 KiB
Rust
Raw Normal View History

2023-05-09 18:54:38 -07:00
use std::fs::File;
2023-11-15 15:44:43 -08:00
use std::io::BufReader;
2023-05-09 18:54:38 -07:00
use std::sync::{Mutex, OnceLock};
2023-11-17 13:35:50 -08:00
use std::{io, mem};
2023-05-09 18:54:38 -07:00
2023-11-15 15:44:43 -08:00
use base::libc::{O_CLOEXEC, O_RDONLY};
use base::{
2023-11-17 13:35:50 -08:00
cstr, libc, open_fd, BufReadExt, Directory, FsPathBuf, ResultExt, Utf8CStr, Utf8CStrBuf,
Utf8CStrBufArr, Utf8CStrBufRef, WalkResult,
2023-11-15 15:44:43 -08:00
};
2023-06-10 01:40:45 -07:00
2024-03-26 18:03:40 -07:00
use crate::consts::MAIN_CONFIG;
2023-12-26 23:08:06 +08:00
use crate::ffi::{get_magisk_tmp, CxxMagiskD, RequestCode};
2024-03-26 18:03:40 -07:00
use crate::get_prop;
use crate::logging::magisk_logging;
2023-06-10 01:40:45 -07:00
2023-05-09 18:54:38 -07:00
// Global magiskd singleton
pub static MAGISKD: OnceLock<MagiskD> = OnceLock::new();
2023-11-17 13:35:50 -08:00
#[repr(u32)]
enum BootState {
PostFsDataDone = (1 << 0),
LateStartDone = (1 << 1),
BootComplete = (1 << 2),
SafeMode = (1 << 3),
}
#[derive(Default)]
#[repr(transparent)]
2023-11-27 17:40:58 +08:00
struct BootStateFlags(u32);
2023-11-17 13:35:50 -08:00
impl BootStateFlags {
fn contains(&self, stage: BootState) -> bool {
2023-11-27 17:40:58 +08:00
(self.0 & stage as u32) != 0
2023-11-17 13:35:50 -08:00
}
2023-11-27 17:40:58 +08:00
fn set(&mut self, stage: BootState) {
self.0 |= stage as u32;
2023-11-17 13:35:50 -08:00
}
}
2023-05-09 18:54:38 -07:00
#[derive(Default)]
pub struct MagiskD {
2023-10-30 14:24:21 -07:00
pub logd: Mutex<Option<File>>,
2023-11-27 17:40:58 +08:00
boot_stage_lock: Mutex<BootStateFlags>,
is_emulator: bool,
2023-11-15 15:44:43 -08:00
is_recovery: bool,
2023-05-09 18:54:38 -07:00
}
impl MagiskD {
pub fn is_emulator(&self) -> bool {
self.is_emulator
}
2023-11-15 15:44:43 -08:00
pub fn is_recovery(&self) -> bool {
self.is_recovery
}
2023-11-17 13:35:50 -08:00
pub fn boot_stage_handler(&self, client: i32, code: i32) {
// Make sure boot stage execution is always serialized
2023-11-27 17:40:58 +08:00
let mut state = self.boot_stage_lock.lock().unwrap();
2023-11-17 13:35:50 -08:00
let code = RequestCode { repr: code };
match code {
RequestCode::POST_FS_DATA => {
2023-11-27 17:40:58 +08:00
if check_data() && !state.contains(BootState::PostFsDataDone) {
if self.as_cxx().post_fs_data() {
state.set(BootState::SafeMode);
}
state.set(BootState::PostFsDataDone);
2023-11-17 13:35:50 -08:00
}
unsafe { libc::close(client) };
}
RequestCode::LATE_START => {
unsafe { libc::close(client) };
2023-11-27 17:40:58 +08:00
if state.contains(BootState::PostFsDataDone) && !state.contains(BootState::SafeMode)
2023-11-17 13:35:50 -08:00
{
self.as_cxx().late_start();
2023-11-27 17:40:58 +08:00
state.set(BootState::LateStartDone);
2023-11-17 13:35:50 -08:00
}
}
RequestCode::BOOT_COMPLETE => {
unsafe { libc::close(client) };
2023-11-27 17:40:58 +08:00
if !state.contains(BootState::SafeMode) {
state.set(BootState::BootComplete);
2023-11-17 13:35:50 -08:00
self.as_cxx().boot_complete()
}
}
_ => {
unsafe { libc::close(client) };
}
}
}
#[inline(always)]
fn as_cxx(&self) -> &CxxMagiskD {
unsafe { mem::transmute(self) }
}
}
2023-05-09 18:54:38 -07:00
pub fn daemon_entry() {
let mut qemu = get_prop(cstr!("ro.kernel.qemu"), false);
if qemu.is_empty() {
qemu = get_prop(cstr!("ro.boot.qemu"), false);
}
let is_emulator = qemu == "1";
2023-11-15 15:44:43 -08:00
// Load config status
let mut buf = Utf8CStrBufArr::<64>::new();
let path = FsPathBuf::new(&mut buf)
.join(get_magisk_tmp())
2024-03-26 18:03:40 -07:00
.join(MAIN_CONFIG);
2023-11-15 15:44:43 -08:00
let mut is_recovery = false;
if let Ok(file) = path.open(O_RDONLY | O_CLOEXEC) {
let mut file = BufReader::new(file);
file.foreach_props(|key, val| {
if key == "RECOVERYMODE" {
is_recovery = val == "true";
return false;
}
true
});
}
let magiskd = MagiskD {
is_emulator,
2023-11-15 15:44:43 -08:00
is_recovery,
2023-11-17 13:35:50 -08:00
..Default::default()
};
2023-05-09 18:54:38 -07:00
magiskd.start_log_daemon();
MAGISKD.set(magiskd).ok();
magisk_logging();
}
2023-11-17 13:35:50 -08:00
fn check_data() -> bool {
if let Ok(fd) = open_fd!(cstr!("/proc/mounts"), O_RDONLY | O_CLOEXEC) {
let file = File::from(fd);
let mut mnt = false;
BufReader::new(file).foreach_lines(|line| {
if line.contains(" /data ") && !line.contains("tmpfs") {
mnt = true;
return false;
}
true
});
if !mnt {
return false;
}
let crypto = get_prop(cstr!("ro.crypto.state"), false);
return if !crypto.is_empty() {
if crypto != "encrypted" {
// Unencrypted, we can directly access data
true
} else {
// Encrypted, check whether vold is started
!get_prop(cstr!("init.svc.vold"), false).is_empty()
}
} else {
// ro.crypto.state is not set, assume it's unencrypted
true
};
}
false
}
2023-05-09 18:54:38 -07:00
pub fn get_magiskd() -> &'static MagiskD {
unsafe { MAGISKD.get().unwrap_unchecked() }
2023-05-09 18:54:38 -07:00
}
2023-12-26 23:08:06 +08:00
pub fn find_apk_path(pkg: &Utf8CStr, data: &mut [u8]) -> usize {
2023-06-10 01:40:45 -07:00
use WalkResult::*;
2023-12-26 23:08:06 +08:00
fn inner(pkg: &Utf8CStr, buf: &mut dyn Utf8CStrBuf) -> io::Result<usize> {
2023-06-10 01:40:45 -07:00
Directory::open(cstr!("/data/app"))?.pre_order_walk(|e| {
if !e.is_dir() {
return Ok(Skip);
}
let d_name = e.d_name().to_bytes();
2023-06-14 18:44:53 +08:00
if d_name.starts_with(pkg.as_bytes()) && d_name[pkg.len()] == b'-' {
2023-06-10 01:40:45 -07:00
// Found the APK path, we can abort now
e.path(buf)?;
2023-06-10 01:40:45 -07:00
return Ok(Abort);
}
if d_name.starts_with(b"~~") {
return Ok(Continue);
}
Ok(Skip)
})?;
if !buf.is_empty() {
2023-09-27 15:21:24 -07:00
buf.push_str("/base.apk");
2023-06-10 01:40:45 -07:00
}
Ok(buf.len())
2023-06-10 01:40:45 -07:00
}
2023-09-27 15:21:24 -07:00
inner(pkg, &mut Utf8CStrBufRef::from(data))
.log()
.unwrap_or(0)
2023-06-10 01:40:45 -07:00
}