Fix magisk loading

This commit is contained in:
Nullptr
2023-10-21 13:29:43 +08:00
parent 218659dcbf
commit 8a80586fb8
7 changed files with 33 additions and 24 deletions

View File

@@ -305,6 +305,7 @@ fn ptrace_zygote(pid: u32) -> Result<()> {
}
pub fn main() -> Result<()> {
info!("Start zygisk fuse");
fs::create_dir(constants::PATH_WORK_DIR)?;
fs::create_dir(constants::PATH_FUSE_DIR)?;
PCL_CONTENT.init(fs::read(constants::PATH_PCL)?);
@@ -320,6 +321,8 @@ pub fn main() -> Result<()> {
&options,
)?;
mount_bind(constants::PATH_FUSE_PCL, constants::PATH_PCL)?;
session.join();
bail!("Fuse mount exited unexpectedly");
match session.guard.join() {
Err(e) => bail!("Fuse mount crashed: {:?}", e),
_ => bail!("Fuse mount exited unexpectedly"),
}
}

View File

@@ -9,6 +9,7 @@ mod utils;
mod watchdog;
mod zygiskd;
use std::future::Future;
use anyhow::Result;
@@ -20,10 +21,16 @@ fn init_android_logger(tag: &str) {
);
}
async fn start(name: &str) -> Result<()> {
fn async_start<F: Future>(future: F) -> F::Output {
let async_runtime = tokio::runtime::Runtime::new().unwrap();
async_runtime.block_on(future)
}
fn start(name: &str) -> Result<()> {
utils::switch_mount_namespace(1)?;
root_impl::setup();
match name.trim_start_matches("zygisk-") {
"wd" => watchdog::main().await?,
"wd" => async_start(watchdog::main())?,
"fuse" => fuse::main()?,
"cp" => zygiskd::main()?,
_ => println!("Available commands: wd, fuse, cp"),
@@ -31,13 +38,12 @@ async fn start(name: &str) -> Result<()> {
Ok(())
}
#[tokio::main]
async fn main() {
fn main() {
let process = std::env::args().next().unwrap();
let nice_name = process.split('/').last().unwrap();
init_android_logger(nice_name);
if let Err(e) = start(nice_name).await {
if let Err(e) = start(nice_name) {
log::error!("Crashed: {}\n{}", e, e.backtrace());
}
}

View File

@@ -1,6 +1,7 @@
use anyhow::Result;
use std::{fs, io::{Read, Write}, os::unix::net::UnixStream};
use std::ffi::{c_char, CStr, CString};
use std::os::fd::AsFd;
use std::os::unix::net::UnixListener;
use std::process::Command;
use std::sync::OnceLock;
@@ -84,6 +85,14 @@ pub fn set_property(name: &str, value: &str) -> Result<()> {
Ok(())
}
pub fn switch_mount_namespace(pid: i32) -> Result<()> {
let cwd = std::env::current_dir()?;
let mnt = fs::File::open(format!("/proc/{}/ns/mnt", pid))?;
rustix::thread::move_into_link_name_space(mnt.as_fd(), None)?;
std::env::set_current_dir(cwd)?;
Ok(())
}
pub trait UnixStreamExt {
fn read_u8(&mut self) -> Result<u8>;
fn read_u32(&mut self) -> Result<u32>;

View File

@@ -23,7 +23,7 @@ pub async fn main() -> Result<()> {
}
async fn run() -> Result<()> {
info!("Start zygisksu watchdog");
info!("Start zygisk watchdog");
check_permission()?;
mount_prop().await?;
if check_and_set_hint()? == false {
@@ -61,18 +61,7 @@ fn check_permission() -> Result<()> {
}
async fn mount_prop() -> Result<()> {
let module_prop = if let root_impl::RootImpl::Magisk = root_impl::get_impl() {
let magisk_path = Command::new("magisk").arg("--path").output().await?;
let mut magisk_path = String::from_utf8(magisk_path.stdout)?;
magisk_path.pop(); // Removing '\n'
let cwd = std::env::current_dir()?;
let dir = cwd.file_name().unwrap().to_string_lossy();
format!("{magisk_path}/.magisk/modules/{dir}/{}", constants::PATH_MODULE_PROP)
} else {
constants::PATH_MODULE_PROP.to_string()
};
info!("Mount {module_prop}");
let module_prop_file = fs::File::open(&module_prop)?;
let module_prop_file = fs::File::open(constants::PATH_MODULE_PROP)?;
let mut section = 0;
let mut sections: [String; 2] = [String::new(), String::new()];
let lines = BufReader::new(module_prop_file).lines();
@@ -90,8 +79,9 @@ async fn mount_prop() -> Result<()> {
}
PROP_SECTIONS.init(sections);
info!("Mount {} -> {}", constants::PATH_PROP_OVERLAY, constants::PATH_MODULE_PROP);
fs::File::create(constants::PATH_PROP_OVERLAY)?;
mount_bind(constants::PATH_PROP_OVERLAY, &module_prop)?;
mount_bind(constants::PATH_PROP_OVERLAY, constants::PATH_MODULE_PROP)?;
Ok(())
}

View File

@@ -29,6 +29,7 @@ struct Context {
}
pub fn main() -> Result<()> {
log::info!("Start zygisk companion");
set_parent_process_death_signal(Some(Signal::Kill))?;
let arch = utils::get_property("ro.product.cpu.abi")?;