Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
weblate committed Jan 4, 2024
2 parents 00fc17e + 01711b4 commit 9f57fe5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 22 deletions.
36 changes: 15 additions & 21 deletions userspace/ksud/src/ksu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn print_usage(program: &str, opts: Options) {
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"))]
unsafe {
if !groups.is_empty() {
Expand All @@ -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<String> = std::env::args().collect();
let program = env_args[0].clone();
let args = env_args
Expand Down Expand Up @@ -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::<u32>::new();
for g in matches.opt_strs("G") {
if let core::result::Result::Ok(id) = g.parse::<u32>() {
groups.push(id);
} else {
println!("Invalid GID: {g}");
print_usage(&program, opts);
return Ok(());
}
}
let mut gid: Option<u32> = None;

let groups = matches
.opt_strs("G")
.into_iter()
.map(|g| g.parse::<u32>().map_err(|_| anyhow!("Invalid GID: {}", g)))
.collect::<Result<Vec<_>, _>>()?;

// if -g provided, use it.
if let Some(g) = matches.opt_str("g") {
if let core::result::Result::Ok(id) = g.parse::<u32>() {
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::<u32>().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]);
Expand Down
6 changes: 5 additions & 1 deletion userspace/ksud/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
return Ok(());
}
Expand Down

0 comments on commit 9f57fe5

Please sign in to comment.