ksud: add /data/adb/ksu/bin to PATH (#343)

This commit is contained in:
Ylarod
2023-04-05 11:47:35 +08:00
committed by GitHub
parent ddbc71b273
commit 862d12a904
3 changed files with 38 additions and 4 deletions

View File

@@ -1,11 +1,13 @@
use const_format::concatcp;
pub const ADB_DIR: &str = "/data/adb/";
pub const WORKING_DIR: &str = concatcp!(ADB_DIR, "ksu/");
pub const BINARY_DIR: &str = concatcp!(WORKING_DIR, "bin/");
pub const DAEMON_PATH: &str = concatcp!(ADB_DIR, "ksud");
pub const WORKING_DIR: &str = concatcp!(ADB_DIR, "ksu/");
pub const BINARY_DIR: &str = concatcp!(WORKING_DIR, "bin/");
#[cfg(target_os = "android")]
pub const DAEMON_LINK_PATH: &str = concatcp!(BINARY_DIR, "ksud");
pub const MODULE_DIR: &str = concatcp!(ADB_DIR, "modules/");
pub const MODULE_IMG: &str = concatcp!(WORKING_DIR, "modules.img");

View File

@@ -248,5 +248,21 @@ pub fn install() -> Result<()> {
std::fs::copy("/proc/self/exe", defs::DAEMON_PATH)?;
// install binary assets
assets::ensure_binaries().with_context(|| "Failed to extract assets")
assets::ensure_binaries().with_context(|| "Failed to extract assets")?;
#[cfg(target_os = "android")]
link_ksud_to_bin()?;
Ok(())
}
#[cfg(target_os = "android")]
fn link_ksud_to_bin() -> Result<()> {
use std::path::PathBuf;
let ksu_bin = PathBuf::from(defs::DAEMON_PATH);
let ksu_bin_link = PathBuf::from(defs::DAEMON_LINK_PATH);
if ksu_bin.exists() && !ksu_bin_link.exists() {
std::os::unix::fs::symlink(&ksu_bin, &ksu_bin_link)?;
}
Ok(())
}

View File

@@ -3,8 +3,10 @@ use anyhow::{Ok, Result};
#[cfg(unix)]
use anyhow::ensure;
use getopts::Options;
use std::env;
#[cfg(unix)]
use std::os::unix::process::CommandExt;
use std::path::PathBuf;
use std::{ffi::CStr, process::Command};
use crate::{
@@ -84,7 +86,7 @@ pub fn root_shell() -> Result<()> {
"COMMAND",
);
opts.optflag("h", "help", "display this help message and exit");
opts.optflag("l", "login", "force run in the global mount namespace");
opts.optflag("l", "login", "pretend the shell to be a login shell");
opts.optflag(
"p",
"preserve-environment",
@@ -208,6 +210,10 @@ pub fn root_shell() -> Result<()> {
}
}
// add /data/adb/ksu/bin to PATH
#[cfg(any(target_os = "linux", target_os = "android"))]
add_path_to_env(defs::BINARY_DIR)?;
// escape from the current cgroup and become session leader
// WARNING!!! This cause some root shell hang forever!
// command = command.process_group(0);
@@ -233,6 +239,16 @@ pub fn root_shell() -> Result<()> {
Err(command.exec().into())
}
fn add_path_to_env(path: &str) -> Result<()> {
let mut paths =
env::var_os("PATH").map_or(Vec::new(), |val| env::split_paths(&val).collect::<Vec<_>>());
let new_path = PathBuf::from(path);
paths.push(new_path);
let new_path_env = env::join_paths(paths)?;
env::set_var("PATH", new_path_env);
Ok(())
}
pub fn get_version() -> i32 {
let mut result: i32 = 0;
#[cfg(any(target_os = "linux", target_os = "android"))]