From 01711b4114074f89e4660f46d4f06d8c621a1e5e Mon Sep 17 00:00:00 2001 From: weishu Date: Thu, 4 Jan 2024 16:12:15 +0800 Subject: [PATCH] ksud: minor tweaks --- userspace/ksud/src/ksu.rs | 36 +++++++++++++++--------------------- userspace/ksud/src/utils.rs | 6 +++++- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/userspace/ksud/src/ksu.rs b/userspace/ksud/src/ksu.rs index 3753e53809b7..6e2425f9f05a 100644 --- a/userspace/ksud/src/ksu.rs +++ b/userspace/ksud/src/ksu.rs @@ -58,7 +58,7 @@ fn print_usage(program: &str, opts: Options) { print!("{}", opts.usage(&brief)); } -fn set_identity(uid: u32, gid: u32, groups: &Vec) { +fn set_identity(uid: u32, gid: u32, groups: &[u32]) { #[cfg(any(target_os = "linux", target_os = "android"))] unsafe { if !groups.is_empty() { @@ -77,6 +77,8 @@ pub fn root_shell() -> Result<()> { #[cfg(unix)] pub fn root_shell() -> Result<()> { // we are root now, this was set in kernel! + + use anyhow::anyhow; let env_args: Vec = std::env::args().collect(); let program = env_args[0].clone(); let args = env_args @@ -169,27 +171,19 @@ pub fn root_shell() -> Result<()> { let mut is_login = matches.opt_present("l"); let preserve_env = matches.opt_present("p"); let mount_master = matches.opt_present("M"); - let mut groups = Vec::::new(); - for g in matches.opt_strs("G") { - if let core::result::Result::Ok(id) = g.parse::() { - groups.push(id); - } else { - println!("Invalid GID: {g}"); - print_usage(&program, opts); - return Ok(()); - } - } - let mut gid: Option = None; + + let groups = matches + .opt_strs("G") + .into_iter() + .map(|g| g.parse::().map_err(|_| anyhow!("Invalid GID: {}", g))) + .collect::, _>>()?; + // if -g provided, use it. - if let Some(g) = matches.opt_str("g") { - if let core::result::Result::Ok(id) = g.parse::() { - gid = Some(id); - } else { - println!("Invalid GID: {g}"); - print_usage(&program, opts); - return Ok(()); - } - } + let mut gid = matches + .opt_str("g") + .map(|g| g.parse::().map_err(|_| anyhow!("Invalid GID: {}", g))) + .transpose()?; + // otherwise, use the first gid of groups. if gid.is_none() && !groups.is_empty() { gid = Some(groups[0]); diff --git a/userspace/ksud/src/utils.rs b/userspace/ksud/src/utils.rs index 55c7d67a26e6..510f0fd9eafd 100644 --- a/userspace/ksud/src/utils.rs +++ b/userspace/ksud/src/utils.rs @@ -45,7 +45,11 @@ pub fn ensure_dir_exists>(dir: T) -> Result<()> { } } -pub fn ensure_binary>(path: T, contents: &[u8], ignore_if_exist: bool) -> Result<()> { +pub fn ensure_binary>( + path: T, + contents: &[u8], + ignore_if_exist: bool, +) -> Result<()> { if ignore_if_exist && path.as_ref().exists() { return Ok(()); }