ksud: minor tweaks

This commit is contained in:
weishu
2024-01-04 16:12:15 +08:00
parent 153ce9a39a
commit 01711b4114
2 changed files with 20 additions and 22 deletions

View File

@@ -58,7 +58,7 @@ fn print_usage(program: &str, opts: Options) {
print!("{}", opts.usage(&brief)); print!("{}", opts.usage(&brief));
} }
fn set_identity(uid: u32, gid: u32, groups: &Vec<u32>) { fn set_identity(uid: u32, gid: u32, groups: &[u32]) {
#[cfg(any(target_os = "linux", target_os = "android"))] #[cfg(any(target_os = "linux", target_os = "android"))]
unsafe { unsafe {
if !groups.is_empty() { if !groups.is_empty() {
@@ -77,6 +77,8 @@ pub fn root_shell() -> Result<()> {
#[cfg(unix)] #[cfg(unix)]
pub fn root_shell() -> Result<()> { pub fn root_shell() -> Result<()> {
// we are root now, this was set in kernel! // we are root now, this was set in kernel!
use anyhow::anyhow;
let env_args: Vec<String> = std::env::args().collect(); let env_args: Vec<String> = std::env::args().collect();
let program = env_args[0].clone(); let program = env_args[0].clone();
let args = env_args let args = env_args
@@ -169,27 +171,19 @@ pub fn root_shell() -> Result<()> {
let mut is_login = matches.opt_present("l"); let mut is_login = matches.opt_present("l");
let preserve_env = matches.opt_present("p"); let preserve_env = matches.opt_present("p");
let mount_master = matches.opt_present("M"); let mount_master = matches.opt_present("M");
let mut groups = Vec::<u32>::new();
for g in matches.opt_strs("G") { let groups = matches
if let core::result::Result::Ok(id) = g.parse::<u32>() { .opt_strs("G")
groups.push(id); .into_iter()
} else { .map(|g| g.parse::<u32>().map_err(|_| anyhow!("Invalid GID: {}", g)))
println!("Invalid GID: {g}"); .collect::<Result<Vec<_>, _>>()?;
print_usage(&program, opts);
return Ok(());
}
}
let mut gid: Option<u32> = None;
// if -g provided, use it. // if -g provided, use it.
if let Some(g) = matches.opt_str("g") { let mut gid = matches
if let core::result::Result::Ok(id) = g.parse::<u32>() { .opt_str("g")
gid = Some(id); .map(|g| g.parse::<u32>().map_err(|_| anyhow!("Invalid GID: {}", g)))
} else { .transpose()?;
println!("Invalid GID: {g}");
print_usage(&program, opts);
return Ok(());
}
}
// otherwise, use the first gid of groups. // otherwise, use the first gid of groups.
if gid.is_none() && !groups.is_empty() { if gid.is_none() && !groups.is_empty() {
gid = Some(groups[0]); gid = Some(groups[0]);

View File

@@ -45,7 +45,11 @@ pub fn ensure_dir_exists<T: AsRef<Path>>(dir: T) -> Result<()> {
} }
} }
pub fn ensure_binary<T: AsRef<Path>>(path: T, contents: &[u8], ignore_if_exist: bool) -> Result<()> { pub fn ensure_binary<T: AsRef<Path>>(
path: T,
contents: &[u8],
ignore_if_exist: bool,
) -> Result<()> {
if ignore_if_exist && path.as_ref().exists() { if ignore_if_exist && path.as_ref().exists() {
return Ok(()); return Ok(());
} }