You've already forked ReZygisk
mirror of
https://github.com/PerformanC/ReZygisk.git
synced 2025-09-06 06:37:01 +00:00
reformat code
This commit is contained in:
@@ -107,7 +107,6 @@ androidComponents.onVariants { variant ->
|
|||||||
.putLong(real.length())
|
.putLong(real.length())
|
||||||
.array()
|
.array()
|
||||||
sig.update(buffer)
|
sig.update(buffer)
|
||||||
println("sha $path ${real.length()}")
|
|
||||||
real.forEachBlock { bytes, size ->
|
real.forEachBlock { bytes, size ->
|
||||||
sig.update(bytes, 0, size)
|
sig.update(bytes, 0, size)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,13 +1,12 @@
|
|||||||
|
use crate::dl;
|
||||||
|
use crate::utils::{check_unix_socket, UnixStreamExt};
|
||||||
|
use anyhow::Result;
|
||||||
|
use passfd::FdPassingExt;
|
||||||
|
use rustix::fs::fstat;
|
||||||
use std::ffi::c_void;
|
use std::ffi::c_void;
|
||||||
use std::os::fd::{AsRawFd, FromRawFd, RawFd};
|
use std::os::fd::{AsRawFd, FromRawFd, RawFd};
|
||||||
use std::os::unix::net::UnixStream;
|
use std::os::unix::net::UnixStream;
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use anyhow::Result;
|
|
||||||
use passfd::FdPassingExt;
|
|
||||||
use rustix::fs::fstat;
|
|
||||||
use tokio::io::AsyncWriteExt;
|
|
||||||
use crate::utils::{check_unix_socket, UnixStreamExt};
|
|
||||||
use crate::dl;
|
|
||||||
|
|
||||||
type ZygiskCompanionEntryFn = unsafe extern "C" fn(i32);
|
type ZygiskCompanionEntryFn = unsafe extern "C" fn(i32);
|
||||||
|
|
||||||
@@ -28,7 +27,7 @@ pub fn entry(fd: i32) {
|
|||||||
None => {
|
None => {
|
||||||
log::debug!("No companion entry for `{name}`");
|
log::debug!("No companion entry for `{name}`");
|
||||||
stream.write_u8(0).expect("reply 0");
|
stream.write_u8(0).expect("reply 0");
|
||||||
return ();
|
std::process::exit(0);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -43,7 +42,9 @@ pub fn entry(fd: i32) {
|
|||||||
stream.write_u8(1).expect("reply success");
|
stream.write_u8(1).expect("reply success");
|
||||||
thread::spawn(move || {
|
thread::spawn(move || {
|
||||||
let st0 = fstat(&stream).expect("failed to stat stream");
|
let st0 = fstat(&stream).expect("failed to stat stream");
|
||||||
unsafe { entry(stream.as_raw_fd()); }
|
unsafe {
|
||||||
|
entry(stream.as_raw_fd());
|
||||||
|
}
|
||||||
// Only close client if it is the same file so we don't
|
// Only close client if it is the same file so we don't
|
||||||
// accidentally close a re-used file descriptor.
|
// accidentally close a re-used file descriptor.
|
||||||
// This check is required because the module companion
|
// This check is required because the module companion
|
||||||
|
|||||||
@@ -1,32 +1,26 @@
|
|||||||
|
use crate::lp_select;
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
use const_format::concatcp;
|
|
||||||
use konst::primitive::parse_i32;
|
use konst::primitive::parse_i32;
|
||||||
use konst::unwrap_ctx;
|
use konst::unwrap_ctx;
|
||||||
use log::LevelFilter;
|
use log::LevelFilter;
|
||||||
use num_enum::TryFromPrimitive;
|
use num_enum::TryFromPrimitive;
|
||||||
use crate::lp_select;
|
|
||||||
|
|
||||||
pub const MIN_KSU_VERSION: i32 = unwrap_ctx!(parse_i32(env!("MIN_KSU_VERSION")));
|
pub const MIN_KSU_VERSION: i32 = unwrap_ctx!(parse_i32(env!("MIN_KSU_VERSION")));
|
||||||
pub const MAX_KSU_VERSION: i32 = unwrap_ctx!(parse_i32(env!("MAX_KSU_VERSION")));
|
pub const MAX_KSU_VERSION: i32 = unwrap_ctx!(parse_i32(env!("MAX_KSU_VERSION")));
|
||||||
pub const MIN_MAGISK_VERSION: i32 = unwrap_ctx!(parse_i32(env!("MIN_MAGISK_VERSION")));
|
pub const MIN_MAGISK_VERSION: i32 = unwrap_ctx!(parse_i32(env!("MIN_MAGISK_VERSION")));
|
||||||
pub const ZKSU_VERSION: &'static str = env!("ZKSU_VERSION");
|
pub const ZKSU_VERSION: &str = env!("ZKSU_VERSION");
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
pub const MAX_LOG_LEVEL: LevelFilter = LevelFilter::Trace;
|
pub const MAX_LOG_LEVEL: LevelFilter = LevelFilter::Trace;
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
pub const MAX_LOG_LEVEL: LevelFilter = LevelFilter::Info;
|
pub const MAX_LOG_LEVEL: LevelFilter = LevelFilter::Info;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
pub const PATH_MODULES_DIR: &str = "..";
|
pub const PATH_MODULES_DIR: &str = "..";
|
||||||
pub const PATH_MODULE_PROP: &str = "module.prop";
|
|
||||||
pub const ZYGOTE_INJECTED: i32 = lp_select!(5, 4);
|
pub const ZYGOTE_INJECTED: i32 = lp_select!(5, 4);
|
||||||
pub const DAEMON_SET_INFO: i32 = lp_select!(7, 6);
|
pub const DAEMON_SET_INFO: i32 = lp_select!(7, 6);
|
||||||
pub const DAEMON_SET_ERROR_INFO: i32 = lp_select!(9, 8);
|
pub const DAEMON_SET_ERROR_INFO: i32 = lp_select!(9, 8);
|
||||||
pub const SYSTEM_SERVER_STARTED: i32 = 10;
|
pub const SYSTEM_SERVER_STARTED: i32 = 10;
|
||||||
|
|
||||||
pub const MAX_RESTART_COUNT: i32 = 5;
|
|
||||||
|
|
||||||
#[derive(Debug, Eq, PartialEq, TryFromPrimitive)]
|
#[derive(Debug, Eq, PartialEq, TryFromPrimitive)]
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum DaemonSocketAction {
|
pub enum DaemonSocketAction {
|
||||||
|
|||||||
@@ -30,13 +30,13 @@ extern "C" {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type AndroidCreateNamespaceFn = unsafe extern "C" fn(
|
type AndroidCreateNamespaceFn = unsafe extern "C" fn(
|
||||||
*const c_char, // name
|
*const c_char, // name
|
||||||
*const c_char, // ld_library_path
|
*const c_char, // ld_library_path
|
||||||
*const c_char, // default_library_path
|
*const c_char, // default_library_path
|
||||||
u64, // type
|
u64, // type
|
||||||
*const c_char, // permitted_when_isolated_path
|
*const c_char, // permitted_when_isolated_path
|
||||||
*mut AndroidNamespace, // parent
|
*mut AndroidNamespace, // parent
|
||||||
*const c_void, // caller_addr
|
*const c_void, // caller_addr
|
||||||
) -> *mut AndroidNamespace;
|
) -> *mut AndroidNamespace;
|
||||||
|
|
||||||
pub unsafe fn dlopen(path: &str, flags: i32) -> Result<*mut c_void> {
|
pub unsafe fn dlopen(path: &str, flags: i32) -> Result<*mut c_void> {
|
||||||
@@ -57,11 +57,15 @@ pub unsafe fn dlopen(path: &str, flags: i32) -> Result<*mut c_void> {
|
|||||||
libc::RTLD_DEFAULT,
|
libc::RTLD_DEFAULT,
|
||||||
std::ffi::CString::new("__loader_android_create_namespace")?.as_ptr(),
|
std::ffi::CString::new("__loader_android_create_namespace")?.as_ptr(),
|
||||||
);
|
);
|
||||||
let android_create_namespace_fn: AndroidCreateNamespaceFn = std::mem::transmute(android_create_namespace_fn);
|
let android_create_namespace_fn: AndroidCreateNamespaceFn =
|
||||||
|
std::mem::transmute(android_create_namespace_fn);
|
||||||
let ns = android_create_namespace_fn(
|
let ns = android_create_namespace_fn(
|
||||||
filename, dir, std::ptr::null(),
|
filename,
|
||||||
|
dir,
|
||||||
|
std::ptr::null(),
|
||||||
ANDROID_NAMESPACE_TYPE_SHARED,
|
ANDROID_NAMESPACE_TYPE_SHARED,
|
||||||
std::ptr::null(), std::ptr::null_mut(),
|
std::ptr::null(),
|
||||||
|
std::ptr::null_mut(),
|
||||||
&dlopen as *const _ as *const c_void,
|
&dlopen as *const _ as *const c_void,
|
||||||
);
|
);
|
||||||
if ns != std::ptr::null_mut() {
|
if ns != std::ptr::null_mut() {
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
|
mod companion;
|
||||||
mod constants;
|
mod constants;
|
||||||
mod dl;
|
mod dl;
|
||||||
mod root_impl;
|
mod root_impl;
|
||||||
mod utils;
|
mod utils;
|
||||||
mod zygiskd;
|
mod zygiskd;
|
||||||
mod companion;
|
|
||||||
|
|
||||||
use std::future::Future;
|
|
||||||
use crate::constants::ZKSU_VERSION;
|
use crate::constants::ZKSU_VERSION;
|
||||||
|
|
||||||
fn init_android_logger(tag: &str) {
|
fn init_android_logger(tag: &str) {
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ pub fn uid_should_umount(uid: i32) -> bool {
|
|||||||
// TODO: signature
|
// TODO: signature
|
||||||
pub fn uid_is_manager(uid: i32) -> bool {
|
pub fn uid_is_manager(uid: i32) -> bool {
|
||||||
if let Ok(s) = rustix::fs::stat("/data/user_de/0/me.weishu.kernelsu") {
|
if let Ok(s) = rustix::fs::stat("/data/user_de/0/me.weishu.kernelsu") {
|
||||||
return s.st_uid == uid as u32
|
return s.st_uid == uid as u32;
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use std::process::{Command, Stdio};
|
|
||||||
use crate::constants::MIN_MAGISK_VERSION;
|
use crate::constants::MIN_MAGISK_VERSION;
|
||||||
|
use std::process::{Command, Stdio};
|
||||||
|
|
||||||
const MAGISK_VANILLA: &str = "com.topjohnwu.magisk";
|
const MAGISK_VANILLA: &str = "com.topjohnwu.magisk";
|
||||||
const MAGISK_ALPHA: &str = "io.github.vvb2060.magisk";
|
const MAGISK_ALPHA: &str = "io.github.vvb2060.magisk";
|
||||||
@@ -15,21 +15,25 @@ pub fn get_magisk() -> Option<Version> {
|
|||||||
Command::new("magisk")
|
Command::new("magisk")
|
||||||
.arg("-v")
|
.arg("-v")
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.spawn().ok()
|
.spawn()
|
||||||
|
.ok()
|
||||||
.and_then(|child| child.wait_with_output().ok())
|
.and_then(|child| child.wait_with_output().ok())
|
||||||
.and_then(|output| String::from_utf8(output.stdout).ok())
|
.and_then(|output| String::from_utf8(output.stdout).ok())
|
||||||
.map(|version| {
|
.map(|version| {
|
||||||
if (version.contains("alpha")) {
|
if version.contains("alpha") {
|
||||||
MAGISK_ALPHA
|
MAGISK_ALPHA
|
||||||
} else {
|
} else {
|
||||||
MAGISK_VANILLA
|
MAGISK_VANILLA
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.map(|variant| { unsafe { VARIANT = variant }; });
|
.map(|variant| {
|
||||||
|
unsafe { VARIANT = variant };
|
||||||
|
});
|
||||||
let version: Option<i32> = Command::new("magisk")
|
let version: Option<i32> = Command::new("magisk")
|
||||||
.arg("-V")
|
.arg("-V")
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.spawn().ok()
|
.spawn()
|
||||||
|
.ok()
|
||||||
.and_then(|child| child.wait_with_output().ok())
|
.and_then(|child| child.wait_with_output().ok())
|
||||||
.and_then(|output| String::from_utf8(output.stdout).ok())
|
.and_then(|output| String::from_utf8(output.stdout).ok())
|
||||||
.and_then(|output| output.trim().parse().ok());
|
.and_then(|output| output.trim().parse().ok());
|
||||||
@@ -45,19 +49,24 @@ pub fn get_magisk() -> Option<Version> {
|
|||||||
pub fn uid_granted_root(uid: i32) -> bool {
|
pub fn uid_granted_root(uid: i32) -> bool {
|
||||||
Command::new("magisk")
|
Command::new("magisk")
|
||||||
.arg("--sqlite")
|
.arg("--sqlite")
|
||||||
.arg(format!("select 1 from policies where uid={uid} and policy=2 limit 1"))
|
.arg(format!(
|
||||||
|
"select 1 from policies where uid={uid} and policy=2 limit 1"
|
||||||
|
))
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.spawn().ok()
|
.spawn()
|
||||||
|
.ok()
|
||||||
.and_then(|child| child.wait_with_output().ok())
|
.and_then(|child| child.wait_with_output().ok())
|
||||||
.and_then(|output| String::from_utf8(output.stdout).ok())
|
.and_then(|output| String::from_utf8(output.stdout).ok())
|
||||||
.map(|output| output.is_empty()) == Some(false)
|
.map(|output| output.is_empty())
|
||||||
|
== Some(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn uid_should_umount(uid: i32) -> bool {
|
pub fn uid_should_umount(uid: i32) -> bool {
|
||||||
let output = Command::new("pm")
|
let output = Command::new("pm")
|
||||||
.args(["list", "packages", "--uid", &uid.to_string()])
|
.args(["list", "packages", "--uid", &uid.to_string()])
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.spawn().ok()
|
.spawn()
|
||||||
|
.ok()
|
||||||
.and_then(|child| child.wait_with_output().ok())
|
.and_then(|child| child.wait_with_output().ok())
|
||||||
.and_then(|output| String::from_utf8(output.stdout).ok());
|
.and_then(|output| String::from_utf8(output.stdout).ok());
|
||||||
let line = match output {
|
let line = match output {
|
||||||
@@ -73,12 +82,16 @@ pub fn uid_should_umount(uid: i32) -> bool {
|
|||||||
};
|
};
|
||||||
Command::new("magisk")
|
Command::new("magisk")
|
||||||
.arg("--sqlite")
|
.arg("--sqlite")
|
||||||
.arg(format!("select 1 from denylist where package_name=\"{pkg}\" limit 1"))
|
.arg(format!(
|
||||||
|
"select 1 from denylist where package_name=\"{pkg}\" limit 1"
|
||||||
|
))
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.spawn().ok()
|
.spawn()
|
||||||
|
.ok()
|
||||||
.and_then(|child| child.wait_with_output().ok())
|
.and_then(|child| child.wait_with_output().ok())
|
||||||
.and_then(|output| String::from_utf8(output.stdout).ok())
|
.and_then(|output| String::from_utf8(output.stdout).ok())
|
||||||
.map(|output| output.is_empty()) == Some(false)
|
.map(|output| output.is_empty())
|
||||||
|
== Some(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: signature
|
// TODO: signature
|
||||||
@@ -86,9 +99,12 @@ pub fn uid_should_umount(uid: i32) -> bool {
|
|||||||
pub fn uid_is_manager(uid: i32) -> bool {
|
pub fn uid_is_manager(uid: i32) -> bool {
|
||||||
let output = Command::new("magisk")
|
let output = Command::new("magisk")
|
||||||
.arg("--sqlite")
|
.arg("--sqlite")
|
||||||
.arg(format!("select value from strings where key=\"requester\" limit 1"))
|
.arg(format!(
|
||||||
|
"select value from strings where key=\"requester\" limit 1"
|
||||||
|
))
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.spawn().ok()
|
.spawn()
|
||||||
|
.ok()
|
||||||
.and_then(|child| child.wait_with_output().ok())
|
.and_then(|child| child.wait_with_output().ok())
|
||||||
.and_then(|output| String::from_utf8(output.stdout).ok())
|
.and_then(|output| String::from_utf8(output.stdout).ok())
|
||||||
.map(|output| output.trim().to_string());
|
.map(|output| output.trim().to_string());
|
||||||
|
|||||||
@@ -20,21 +20,19 @@ pub fn setup() {
|
|||||||
let impl_ = match (ksu_version, magisk_version) {
|
let impl_ = match (ksu_version, magisk_version) {
|
||||||
(None, None) => RootImpl::None,
|
(None, None) => RootImpl::None,
|
||||||
(Some(_), Some(_)) => RootImpl::Multiple,
|
(Some(_), Some(_)) => RootImpl::Multiple,
|
||||||
(Some(ksu_version), None) => {
|
(Some(ksu_version), None) => match ksu_version {
|
||||||
match ksu_version {
|
kernelsu::Version::Supported => RootImpl::KernelSU,
|
||||||
kernelsu::Version::Supported => RootImpl::KernelSU,
|
kernelsu::Version::TooOld => RootImpl::TooOld,
|
||||||
kernelsu::Version::TooOld => RootImpl::TooOld,
|
kernelsu::Version::Abnormal => RootImpl::Abnormal,
|
||||||
kernelsu::Version::Abnormal => RootImpl::Abnormal,
|
},
|
||||||
}
|
(None, Some(magisk_version)) => match magisk_version {
|
||||||
}
|
magisk::Version::Supported => RootImpl::Magisk,
|
||||||
(None, Some(magisk_version)) => {
|
magisk::Version::TooOld => RootImpl::TooOld,
|
||||||
match magisk_version {
|
},
|
||||||
magisk::Version::Supported => RootImpl::Magisk,
|
|
||||||
magisk::Version::TooOld => RootImpl::TooOld,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
unsafe { ROOT_IMPL = impl_; }
|
unsafe {
|
||||||
|
ROOT_IMPL = impl_;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_impl() -> &'static RootImpl {
|
pub fn get_impl() -> &'static RootImpl {
|
||||||
|
|||||||
@@ -1,35 +1,49 @@
|
|||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use std::{fs, io::{Read, Write}, os::unix::net::UnixStream};
|
use rustix::net::{
|
||||||
use std::ffi::{c_char, c_void, CStr, CString};
|
bind_unix, connect_unix, listen, sendto_unix, socket, AddressFamily, SendFlags, SocketAddrUnix,
|
||||||
use std::os::fd::{AsFd, AsRawFd};
|
SocketType,
|
||||||
use std::os::unix::net::{UnixDatagram, UnixListener};
|
};
|
||||||
use std::process::Command;
|
|
||||||
use std::sync::OnceLock;
|
|
||||||
use bitflags::Flags;
|
|
||||||
use rustix::net::{AddressFamily, bind_unix, connect_unix, listen, SendFlags, sendto_unix, socket, SocketAddrUnix, SocketType};
|
|
||||||
use rustix::path::Arg;
|
use rustix::path::Arg;
|
||||||
use rustix::thread::gettid;
|
use rustix::thread::gettid;
|
||||||
|
use std::ffi::{c_char, c_void, CStr, CString};
|
||||||
|
use std::os::fd::{AsFd, AsRawFd};
|
||||||
|
use std::os::unix::net::{UnixListener};
|
||||||
|
use std::process::Command;
|
||||||
|
use std::sync::OnceLock;
|
||||||
|
use std::{
|
||||||
|
fs,
|
||||||
|
io::{Read, Write},
|
||||||
|
os::unix::net::UnixStream,
|
||||||
|
};
|
||||||
|
|
||||||
#[cfg(target_pointer_width = "64")]
|
#[cfg(target_pointer_width = "64")]
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! lp_select {
|
macro_rules! lp_select {
|
||||||
($lp32:expr, $lp64:expr) => { $lp64 };
|
($lp32:expr, $lp64:expr) => {
|
||||||
|
$lp64
|
||||||
|
};
|
||||||
}
|
}
|
||||||
#[cfg(target_pointer_width = "32")]
|
#[cfg(target_pointer_width = "32")]
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! lp_select {
|
macro_rules! lp_select {
|
||||||
($lp32:expr, $lp64:expr) => { $lp32 };
|
($lp32:expr, $lp64:expr) => {
|
||||||
|
$lp32
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! debug_select {
|
macro_rules! debug_select {
|
||||||
($debug:expr, $release:expr) => { $debug };
|
($debug:expr, $release:expr) => {
|
||||||
|
$debug
|
||||||
|
};
|
||||||
}
|
}
|
||||||
#[cfg(not(debug_assertions))]
|
#[cfg(not(debug_assertions))]
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! debug_select {
|
macro_rules! debug_select {
|
||||||
($debug:expr, $release:expr) => { $release };
|
($debug:expr, $release:expr) => {
|
||||||
|
$release
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct LateInit<T> {
|
pub struct LateInit<T> {
|
||||||
@@ -38,7 +52,9 @@ pub struct LateInit<T> {
|
|||||||
|
|
||||||
impl<T> LateInit<T> {
|
impl<T> LateInit<T> {
|
||||||
pub const fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
LateInit { cell: OnceLock::new() }
|
LateInit {
|
||||||
|
cell: OnceLock::new(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init(&self, value: T) {
|
pub fn init(&self, value: T) {
|
||||||
@@ -58,7 +74,10 @@ pub fn set_socket_create_context(context: &str) -> Result<()> {
|
|||||||
match fs::write(path, context) {
|
match fs::write(path, context) {
|
||||||
Ok(_) => Ok(()),
|
Ok(_) => Ok(()),
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
let path = format!("/proc/self/task/{}/attr/sockcreate", gettid().as_raw_nonzero());
|
let path = format!(
|
||||||
|
"/proc/self/task/{}/attr/sockcreate",
|
||||||
|
gettid().as_raw_nonzero()
|
||||||
|
);
|
||||||
fs::write(path, context)?;
|
fs::write(path, context)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -94,6 +113,7 @@ pub fn get_property(name: &str) -> Result<String> {
|
|||||||
Ok(prop.to_string_lossy().to_string())
|
Ok(prop.to_string_lossy().to_string())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn set_property(name: &str, value: &str) -> Result<()> {
|
pub fn set_property(name: &str, value: &str) -> Result<()> {
|
||||||
let name = CString::new(name)?;
|
let name = CString::new(name)?;
|
||||||
let value = CString::new(value)?;
|
let value = CString::new(value)?;
|
||||||
@@ -103,11 +123,10 @@ pub fn set_property(name: &str, value: &str) -> Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn wait_property(name: &str, serial: u32) -> Result<u32> {
|
pub fn wait_property(name: &str, serial: u32) -> Result<u32> {
|
||||||
let name = CString::new(name)?;
|
let name = CString::new(name)?;
|
||||||
let info = unsafe {
|
let info = unsafe { __system_property_find(name.as_ptr()) };
|
||||||
__system_property_find(name.as_ptr())
|
|
||||||
};
|
|
||||||
let mut serial = serial;
|
let mut serial = serial;
|
||||||
unsafe {
|
unsafe {
|
||||||
__system_property_wait(info, serial, &mut serial, std::ptr::null());
|
__system_property_wait(info, serial, &mut serial, std::ptr::null());
|
||||||
@@ -115,14 +134,11 @@ pub fn wait_property(name: &str, serial: u32) -> Result<u32> {
|
|||||||
Ok(serial)
|
Ok(serial)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[allow(dead_code)]
|
||||||
pub fn get_property_serial(name: &str) -> Result<u32> {
|
pub fn get_property_serial(name: &str) -> Result<u32> {
|
||||||
let name = CString::new(name)?;
|
let name = CString::new(name)?;
|
||||||
let info = unsafe {
|
let info = unsafe { __system_property_find(name.as_ptr()) };
|
||||||
__system_property_find(name.as_ptr())
|
Ok(unsafe { __system_property_serial(info) })
|
||||||
};
|
|
||||||
Ok(unsafe {
|
|
||||||
__system_property_serial(info)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn switch_mount_namespace(pid: i32) -> Result<()> {
|
pub fn switch_mount_namespace(pid: i32) -> Result<()> {
|
||||||
@@ -234,6 +250,11 @@ extern "C" {
|
|||||||
fn __system_property_get(name: *const c_char, value: *mut c_char) -> u32;
|
fn __system_property_get(name: *const c_char, value: *mut c_char) -> u32;
|
||||||
fn __system_property_set(name: *const c_char, value: *const c_char) -> u32;
|
fn __system_property_set(name: *const c_char, value: *const c_char) -> u32;
|
||||||
fn __system_property_find(name: *const c_char) -> *const c_void;
|
fn __system_property_find(name: *const c_char) -> *const c_void;
|
||||||
fn __system_property_wait(info: *const c_void, old_serial: u32, new_serial: *mut u32, timeout: *const libc::timespec) -> bool;
|
fn __system_property_wait(
|
||||||
|
info: *const c_void,
|
||||||
|
old_serial: u32,
|
||||||
|
new_serial: *mut u32,
|
||||||
|
timeout: *const libc::timespec,
|
||||||
|
) -> bool;
|
||||||
fn __system_property_serial(info: *const c_void) -> u32;
|
fn __system_property_serial(info: *const c_void) -> u32;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,23 +2,22 @@ use crate::constants::{DaemonSocketAction, ProcessFlags};
|
|||||||
use crate::utils::{check_unix_socket, LateInit, UnixStreamExt};
|
use crate::utils::{check_unix_socket, LateInit, UnixStreamExt};
|
||||||
use crate::{constants, lp_select, root_impl, utils};
|
use crate::{constants, lp_select, root_impl, utils};
|
||||||
use anyhow::{bail, Result};
|
use anyhow::{bail, Result};
|
||||||
|
use log::{debug, error, info, trace, warn};
|
||||||
use passfd::FdPassingExt;
|
use passfd::FdPassingExt;
|
||||||
use std::sync::{Arc, Mutex};
|
use rustix::fs::{fcntl_setfd, FdFlags};
|
||||||
use std::thread;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::io::Error;
|
use std::io::Error;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
use std::os::fd::{AsFd, OwnedFd, RawFd};
|
use std::os::fd::{AsFd, OwnedFd, RawFd};
|
||||||
|
use std::os::unix::process::CommandExt;
|
||||||
use std::os::unix::{
|
use std::os::unix::{
|
||||||
net::{UnixListener, UnixStream},
|
net::{UnixListener, UnixStream},
|
||||||
prelude::AsRawFd,
|
prelude::AsRawFd,
|
||||||
};
|
};
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use std::process::{Command, exit};
|
use std::process::{exit, Command};
|
||||||
use log::{debug, error, info, trace, warn};
|
use std::sync::{Arc, Mutex};
|
||||||
use std::os::unix::process::CommandExt;
|
use std::thread;
|
||||||
use bitflags::Flags;
|
|
||||||
use rustix::fs::{fcntl_setfd, FdFlags};
|
|
||||||
|
|
||||||
struct Module {
|
struct Module {
|
||||||
name: String,
|
name: String,
|
||||||
@@ -39,7 +38,11 @@ pub fn main() -> Result<()> {
|
|||||||
|
|
||||||
TMP_PATH.init(std::env::var("TMP_PATH")?);
|
TMP_PATH.init(std::env::var("TMP_PATH")?);
|
||||||
CONTROLLER_SOCKET.init(format!("{}/init_monitor", TMP_PATH.deref()));
|
CONTROLLER_SOCKET.init(format!("{}/init_monitor", TMP_PATH.deref()));
|
||||||
PATH_CP_NAME.init(format!("{}/{}", TMP_PATH.deref(), lp_select!("/cp32.sock", "/cp64.sock")));
|
PATH_CP_NAME.init(format!(
|
||||||
|
"{}/{}",
|
||||||
|
TMP_PATH.deref(),
|
||||||
|
lp_select!("/cp32.sock", "/cp64.sock")
|
||||||
|
));
|
||||||
|
|
||||||
let arch = get_arch()?;
|
let arch = get_arch()?;
|
||||||
debug!("Daemon architecture: {arch}");
|
debug!("Daemon architecture: {arch}");
|
||||||
@@ -50,9 +53,13 @@ pub fn main() -> Result<()> {
|
|||||||
let info = match root_impl::get_impl() {
|
let info = match root_impl::get_impl() {
|
||||||
root_impl::RootImpl::KernelSU | root_impl::RootImpl::Magisk => {
|
root_impl::RootImpl::KernelSU | root_impl::RootImpl::Magisk => {
|
||||||
msg.extend_from_slice(&constants::DAEMON_SET_INFO.to_le_bytes());
|
msg.extend_from_slice(&constants::DAEMON_SET_INFO.to_le_bytes());
|
||||||
let module_names: Vec<_> = modules.iter()
|
let module_names: Vec<_> = modules.iter().map(|m| m.name.as_str()).collect();
|
||||||
.map(|m| m.name.as_str()).collect();
|
format!(
|
||||||
format!("Root: {:?},module({}): {}", root_impl::get_impl(), modules.len(), module_names.join(","))
|
"Root: {:?},module({}): {}",
|
||||||
|
root_impl::get_impl(),
|
||||||
|
modules.len(),
|
||||||
|
module_names.join(",")
|
||||||
|
)
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
msg.extend_from_slice(&constants::DAEMON_SET_ERROR_INFO.to_le_bytes());
|
msg.extend_from_slice(&constants::DAEMON_SET_ERROR_INFO.to_le_bytes());
|
||||||
@@ -62,12 +69,11 @@ pub fn main() -> Result<()> {
|
|||||||
msg.extend_from_slice(&(info.len() as u32 + 1).to_le_bytes());
|
msg.extend_from_slice(&(info.len() as u32 + 1).to_le_bytes());
|
||||||
msg.extend_from_slice(info.as_bytes());
|
msg.extend_from_slice(info.as_bytes());
|
||||||
msg.extend_from_slice(&[0u8]);
|
msg.extend_from_slice(&[0u8]);
|
||||||
utils::unix_datagram_sendto(&CONTROLLER_SOCKET, msg.as_slice()).expect("failed to send info");
|
utils::unix_datagram_sendto(&CONTROLLER_SOCKET, msg.as_slice())
|
||||||
|
.expect("failed to send info");
|
||||||
}
|
}
|
||||||
|
|
||||||
let context = Context {
|
let context = Context { modules };
|
||||||
modules,
|
|
||||||
};
|
|
||||||
let context = Arc::new(context);
|
let context = Arc::new(context);
|
||||||
let listener = create_daemon_socket()?;
|
let listener = create_daemon_socket()?;
|
||||||
for stream in listener.incoming() {
|
for stream in listener.incoming() {
|
||||||
@@ -142,7 +148,11 @@ fn load_modules(arch: &str) -> Result<Vec<Module>> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
let companion = Mutex::new(None);
|
let companion = Mutex::new(None);
|
||||||
let module = Module { name, lib_fd, companion };
|
let module = Module {
|
||||||
|
name,
|
||||||
|
lib_fd,
|
||||||
|
companion,
|
||||||
|
};
|
||||||
modules.push(module);
|
modules.push(module);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -213,19 +223,21 @@ fn spawn_companion(name: &str, lib_fd: RawFd) -> Result<Option<UnixStream>> {
|
|||||||
exit(0)
|
exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_daemon_action(action: DaemonSocketAction, mut stream: UnixStream, context: &Context) -> Result<()> {
|
fn handle_daemon_action(
|
||||||
|
action: DaemonSocketAction,
|
||||||
|
mut stream: UnixStream,
|
||||||
|
context: &Context,
|
||||||
|
) -> Result<()> {
|
||||||
match action {
|
match action {
|
||||||
DaemonSocketAction::RequestLogcatFd => {
|
DaemonSocketAction::RequestLogcatFd => loop {
|
||||||
loop {
|
let level = match stream.read_u8() {
|
||||||
let level = match stream.read_u8() {
|
Ok(level) => level,
|
||||||
Ok(level) => level,
|
Err(_) => break,
|
||||||
Err(_) => break,
|
};
|
||||||
};
|
let tag = stream.read_string()?;
|
||||||
let tag = stream.read_string()?;
|
let message = stream.read_string()?;
|
||||||
let message = stream.read_string()?;
|
utils::log_raw(level as i32, &tag, &message)?;
|
||||||
utils::log_raw(level as i32, &tag, &message)?;
|
},
|
||||||
}
|
|
||||||
}
|
|
||||||
DaemonSocketAction::GetProcessFlags => {
|
DaemonSocketAction::GetProcessFlags => {
|
||||||
let uid = stream.read_u32()? as i32;
|
let uid = stream.read_u32()? as i32;
|
||||||
let mut flags = ProcessFlags::empty();
|
let mut flags = ProcessFlags::empty();
|
||||||
@@ -244,8 +256,16 @@ fn handle_daemon_action(action: DaemonSocketAction, mut stream: UnixStream, cont
|
|||||||
root_impl::RootImpl::Magisk => flags |= ProcessFlags::PROCESS_ROOT_IS_MAGISK,
|
root_impl::RootImpl::Magisk => flags |= ProcessFlags::PROCESS_ROOT_IS_MAGISK,
|
||||||
_ => panic!("wrong root impl: {:?}", root_impl::get_impl()),
|
_ => panic!("wrong root impl: {:?}", root_impl::get_impl()),
|
||||||
}
|
}
|
||||||
trace!("Uid {} granted root: {}", uid, flags.contains(ProcessFlags::PROCESS_GRANTED_ROOT));
|
trace!(
|
||||||
trace!("Uid {} on denylist: {}", uid, flags.contains(ProcessFlags::PROCESS_ON_DENYLIST));
|
"Uid {} granted root: {}",
|
||||||
|
uid,
|
||||||
|
flags.contains(ProcessFlags::PROCESS_GRANTED_ROOT)
|
||||||
|
);
|
||||||
|
trace!(
|
||||||
|
"Uid {} on denylist: {}",
|
||||||
|
uid,
|
||||||
|
flags.contains(ProcessFlags::PROCESS_ON_DENYLIST)
|
||||||
|
);
|
||||||
stream.write_u32(flags.bits())?;
|
stream.write_u32(flags.bits())?;
|
||||||
}
|
}
|
||||||
DaemonSocketAction::ReadModules => {
|
DaemonSocketAction::ReadModules => {
|
||||||
@@ -271,7 +291,10 @@ fn handle_daemon_action(action: DaemonSocketAction, mut stream: UnixStream, cont
|
|||||||
if c.is_some() {
|
if c.is_some() {
|
||||||
trace!(" Spawned companion for `{}`", module.name);
|
trace!(" Spawned companion for `{}`", module.name);
|
||||||
} else {
|
} else {
|
||||||
trace!(" No companion spawned for `{}` because it has not entry", module.name);
|
trace!(
|
||||||
|
" No companion spawned for `{}` because it has not entry",
|
||||||
|
module.name
|
||||||
|
);
|
||||||
}
|
}
|
||||||
*companion = Some(c);
|
*companion = Some(c);
|
||||||
}
|
}
|
||||||
@@ -283,7 +306,10 @@ fn handle_daemon_action(action: DaemonSocketAction, mut stream: UnixStream, cont
|
|||||||
match companion.as_ref() {
|
match companion.as_ref() {
|
||||||
Some(Some(sock)) => {
|
Some(Some(sock)) => {
|
||||||
if let Err(e) = sock.send_fd(stream.as_raw_fd()) {
|
if let Err(e) = sock.send_fd(stream.as_raw_fd()) {
|
||||||
error!("Failed to send companion fd socket of module `{}`: {}", module.name, e);
|
error!(
|
||||||
|
"Failed to send companion fd socket of module `{}`: {}",
|
||||||
|
module.name, e
|
||||||
|
);
|
||||||
stream.write_u8(0)?;
|
stream.write_u8(0)?;
|
||||||
}
|
}
|
||||||
// Ok: Send by companion
|
// Ok: Send by companion
|
||||||
|
|||||||
Reference in New Issue
Block a user