Magisk/native/src/core/daemon.rs

68 lines
1.8 KiB
Rust
Raw Normal View History

2023-05-09 18:54:38 -07:00
use std::cell::RefCell;
use std::fs::File;
2023-06-10 01:40:45 -07:00
use std::io;
2023-05-09 18:54:38 -07:00
use std::sync::{Mutex, OnceLock};
2023-09-27 15:21:24 -07:00
use base::{cstr, Directory, ResultExt, Utf8CStr, Utf8CStrBuf, Utf8CStrBufRef, WalkResult};
2023-06-10 01:40:45 -07:00
use crate::logging::{magisk_logging, zygisk_logging};
2023-05-09 18:54:38 -07:00
// Global magiskd singleton
pub static MAGISKD: OnceLock<MagiskD> = OnceLock::new();
#[derive(Default)]
pub struct MagiskD {
pub logd: Mutex<RefCell<Option<File>>>,
}
pub fn daemon_entry() {
let magiskd = MagiskD::default();
magiskd.start_log_daemon();
MAGISKD.set(magiskd).ok();
magisk_logging();
}
pub fn zygisk_entry() {
let magiskd = MagiskD::default();
MAGISKD.set(magiskd).ok();
zygisk_logging();
}
pub fn get_magiskd() -> &'static MagiskD {
MAGISKD.get().unwrap()
}
impl MagiskD {}
2023-06-10 01:40:45 -07:00
pub fn find_apk_path(pkg: &[u8], data: &mut [u8]) -> usize {
use WalkResult::*;
fn inner(pkg: &[u8], buf: &mut dyn Utf8CStrBuf) -> io::Result<usize> {
2023-06-14 18:44:53 +08:00
let pkg = match Utf8CStr::from_bytes(pkg) {
Ok(pkg) => pkg,
Err(e) => return Err(io::Error::new(io::ErrorKind::Other, e)),
};
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
}