diff --git a/module/src/post-fs-data.sh b/module/src/post-fs-data.sh index 614a7fd..1dd0a70 100644 --- a/module/src/post-fs-data.sh +++ b/module/src/post-fs-data.sh @@ -20,4 +20,5 @@ if [ "$(which magisk)" ]; then done fi -sh -c "bin/zygisk-fuse &" +[ "$DEBUG" = true ] && export RUST_BACKTRACE=1 +unshare -m sh -c "bin/zygisk-fuse &" diff --git a/module/src/service.sh b/module/src/service.sh index e9c9866..e892dea 100644 --- a/module/src/service.sh +++ b/module/src/service.sh @@ -22,6 +22,5 @@ if [ "$(which magisk)" ]; then done fi -log -p i -t "zygisksu" "Start watchdog" [ "$DEBUG" = true ] && export RUST_BACKTRACE=1 -exec "bin/zygisk-wd" >/dev/null 2>&1 +unshare -m sh -c "bin/zygisk-wd &" diff --git a/zygiskd/src/fuse.rs b/zygiskd/src/fuse.rs index d0c2ea3..19a2a00 100644 --- a/zygiskd/src/fuse.rs +++ b/zygiskd/src/fuse.rs @@ -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"), + } } diff --git a/zygiskd/src/main.rs b/zygiskd/src/main.rs index 22a5983..3a98b4d 100644 --- a/zygiskd/src/main.rs +++ b/zygiskd/src/main.rs @@ -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(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()); } } diff --git a/zygiskd/src/utils.rs b/zygiskd/src/utils.rs index 991cd43..d553d83 100644 --- a/zygiskd/src/utils.rs +++ b/zygiskd/src/utils.rs @@ -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; fn read_u32(&mut self) -> Result; diff --git a/zygiskd/src/watchdog.rs b/zygiskd/src/watchdog.rs index 4382ee4..b1dcd4c 100644 --- a/zygiskd/src/watchdog.rs +++ b/zygiskd/src/watchdog.rs @@ -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(()) } diff --git a/zygiskd/src/zygiskd.rs b/zygiskd/src/zygiskd.rs index 08d63db..dcc43e5 100644 --- a/zygiskd/src/zygiskd.rs +++ b/zygiskd/src/zygiskd.rs @@ -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")?;