ksud: refine su and make it compitable with MagiskSU

Supported features:

1.  --mount-master, -M, -mm which would make the command run in global mount namespace.
2. - user to switch a specific user's shell.
3. -v, -V to print version code and name.

fix #330 #306 #305 #32
This commit is contained in:
tiann
2023-04-04 13:50:36 +08:00
parent 6fc8cc7e8e
commit 48e76f9096
6 changed files with 227 additions and 32 deletions

View File

@@ -1,7 +1,7 @@
use anyhow::{bail, Context, Error, Ok, Result};
use std::{
fs::{create_dir_all, write, File},
io::ErrorKind::AlreadyExists,
fs::{create_dir_all, write, File, OpenOptions},
io::{ErrorKind::AlreadyExists, Write},
path::Path,
};
@@ -116,6 +116,32 @@ pub fn unshare_mnt_ns() -> Result<()> {
Ok(())
}
fn switch_cgroup(grp: &str, pid: u32) {
let path = Path::new(grp).join("cgroup.procs");
if !path.exists() {
return;
}
let fp = OpenOptions::new().append(true).open(path);
if let std::result::Result::Ok(mut fp) = fp {
let _ = writeln!(fp, "{pid}");
}
}
pub fn switch_cgroups() {
let pid = std::process::id();
switch_cgroup("/acct", pid);
switch_cgroup("/dev/cg2_bpf", pid);
switch_cgroup("/sys/fs/cgroup", pid);
if getprop("ro.config.per_app_memcg")
.filter(|prop| prop == "false")
.is_none()
{
switch_cgroup("/dev/memcg/apps", pid);
}
}
#[cfg(any(target_os = "linux", target_os = "android"))]
pub fn umask(mask: u32) {
unsafe { libc::umask(mask) };