Skip to content

Commit

Permalink
fix: make the use of sudo only work on unix systems
Browse files Browse the repository at this point in the history
  • Loading branch information
ripytide committed Nov 7, 2024
1 parent d720d29 commit f780ef0
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ where
S: Into<String>,
I: IntoIterator<Item = S>,
{
let we_are_root = {
let uid = unsafe { libc::geteuid() };
uid == 0
};
#[cfg(unix)]
let use_sudo = matches!(perms, Perms::Sudo) && unsafe { libc::geteuid() } != 0;
#[cfg(windows)]
if matches!(perms, Perms::Sudo) {
return eyre!("sudo for privilege escalation is not supported on windows");
}
#[cfg(windows)]
let use_sudo = false;

let args: Vec<String> = args.into_iter().map(Into::into).collect::<Vec<_>>();

Expand All @@ -38,7 +42,7 @@ where
}

let args = Some("sudo".to_string())
.filter(|_| matches!(perms, Perms::Sudo) && !we_are_root)
.filter(|_| use_sudo)
.into_iter()
.chain(args)
.collect::<Vec<_>>();
Expand Down

0 comments on commit f780ef0

Please sign in to comment.