Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ksud: replace some utils with rust libraries #142

Merged
merged 1 commit into from
Jan 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
400 changes: 364 additions & 36 deletions userspace/ksud/Cargo.lock

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion userspace/ksud/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@ encoding = "0.2.33"
retry = "2.0.0"
humansize = "2.0.0"
libc = "0.2"
sys-mount = "2.0.1"
android-properties = { version = "0.2.2", features = ["bionic-deprecated"] }
extattr = "1.0.0"
jwalk = "0.8.1"
is_executable = "1.0.1"

[profile.release]
strip = true
opt-level = "z"
lto = true
lto = true
6 changes: 3 additions & 3 deletions userspace/ksud/src/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ fn set_kernel_param(size: u32, hash: u32) -> Result<()> {
}

fn get_apk_path(package_name: &str) -> Result<String> {
let cmd = format!("pm path {}", package_name);
let output = Command::new("sh").arg("-c").arg(cmd).output()?;
// `cmd package path` is not available below Android 9
let output = Command::new("pm").args(["path", package_name]).output()?;

// package:/data/app/<xxxx>/base.apk
let output = String::from_utf8_lossy(&output.stdout);
Expand All @@ -54,7 +54,7 @@ pub fn set_manager(pkg: &str) -> Result<()> {
"CONFIG_KSU_DEBUG is not enabled"
);

let path = get_apk_path(pkg).with_context(|| format!("{} not exist!", pkg))?;
let path = get_apk_path(pkg).with_context(|| format!("{pkg} does not exist!"))?;
let sign = get_apk_signature(path.as_str())?;
set_kernel_param(sign.0, sign.1)?;
Ok(())
Expand Down
31 changes: 14 additions & 17 deletions userspace/ksud/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use crate::{
utils::{ensure_clean_dir, mount_image},
};
use anyhow::{bail, Result};
use subprocess::Exec;
use sys_mount::{FilesystemType, Mount, MountFlags};

fn mount_partition(partition: &str, lowerdir: &mut Vec<String>) {
if lowerdir.is_empty() {
println!("partition: {} lowerdir is empty", partition);
println!("partition: {partition} lowerdir is empty");
return;
}

Expand All @@ -19,22 +19,19 @@ fn mount_partition(partition: &str, lowerdir: &mut Vec<String>) {
return;
}
// add /partition as the lowerest dir
let lowest_dir = format!("/{}", partition);
let lowest_dir = format!("/{partition}");
lowerdir.push(lowest_dir.clone());

let lowerdir = lowerdir.join(":");
println!("partition: {} lowerdir: {}", partition, lowerdir);

let mount_args = format!(
"mount -t overlay overlay -o ro,lowerdir={} {}",
lowerdir, lowest_dir
);
if let Ok(result) = Exec::shell(mount_args).join() {
if !result.success() {
println!("mount partition: {} overlay failed", partition);
}
} else {
println!("mount partition: {} overlay failed", partition);
println!("partition: {partition} lowerdir: {lowerdir}");

if let Err(err) = Mount::builder()
.fstype(FilesystemType::from("overlay"))
.flags(MountFlags::RDONLY)
.data(&format!("lowerdir={lowerdir}"))
.mount("overlay", lowest_dir)
{
println!("mount partition: {partition} overlay failed: {err}");
}
}

Expand Down Expand Up @@ -136,7 +133,7 @@ pub fn on_post_data_fs() -> Result<()> {
}

// module mounted, exec modules post-fs-data scripts
if !crate::utils::is_safe_mode().unwrap_or(false) {
if !crate::utils::is_safe_mode() {
// todo: Add timeout
let _ = crate::module::exec_post_fs_data();
let _ = crate::module::load_system_prop();
Expand All @@ -149,7 +146,7 @@ pub fn on_post_data_fs() -> Result<()> {

pub fn on_services() -> Result<()> {
// exec modules service.sh scripts
if !crate::utils::is_safe_mode().unwrap_or(false) {
if !crate::utils::is_safe_mode() {
let _ = crate::module::exec_services();
} else {
println!("safe mode, skip module service scripts");
Expand Down
6 changes: 4 additions & 2 deletions userspace/ksud/src/installer.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#!/system/bin/sh
############################################
# KernelSU installer script
# Credit to Magisk!!!
Expand Down Expand Up @@ -89,14 +90,15 @@ setup_flashable() {
}

ensure_bb() {
:
}

recovery_actions() {

:
}

recovery_cleanup() {

:
}

#######################
Expand Down
13 changes: 4 additions & 9 deletions userspace/ksud/src/ksu.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(dead_code, unused_mut, unused_variables, unused_imports)]

use anyhow::{ensure, Result};
use std::os::unix::process::CommandExt;
use anyhow::{Result, ensure};

const KERNEL_SU_OPTION: u32 = 0xDEADBEEF;

Expand Down Expand Up @@ -31,8 +31,7 @@ pub fn grant_root() -> Result<()> {
}

ensure!(result == KERNEL_SU_OPTION, "grant root failed");
std::process::Command::new("/system/bin/sh").exec();
Ok(())
return Err(std::process::Command::new("sh").exec().into());
}

#[cfg(not(target_os = "android"))]
Expand All @@ -56,11 +55,7 @@ pub fn get_version() -> i32 {
fn report_event(event: u64) {
#[cfg(target_os = "android")]
unsafe {
libc::prctl(
KERNEL_SU_OPTION as i32,
CMD_REPORT_EVENT,
event,
);
libc::prctl(KERNEL_SU_OPTION as i32, CMD_REPORT_EVENT, event);
}
}

Expand All @@ -70,4 +65,4 @@ pub fn report_post_fs_data() {

pub fn report_boot_complete() {
report_event(EVENT_BOOT_COMPLETED);
}
}
Loading