feat: do not ask for sudo prompt when not needed ()

sudo password are not needed when:
- the user is granted permission to use sudo without password in visudo
- the sudo cache still grant the user privileges after having entered
the password
This commit is contained in:
Cyrix126 2025-01-28 15:57:31 +01:00 committed by GitHub
parent 442c1fc747
commit 853e13df39
3 changed files with 20 additions and 14 deletions
src
app/panels
helper/xrig
inits.rs

View file

@ -268,7 +268,7 @@ impl crate::app::App {
);
}
ProcessName::Xmrig => {
if cfg!(windows) {
if cfg!(windows) || !Helper::password_needed() {
Helper::restart_xmrig(
&self.helper,
&self.state.xmrig,
@ -339,14 +339,14 @@ impl crate::app::App {
),
ProcessName::Xmrig => {
if cfg!(windows) {
if cfg!(windows) || !Helper::password_needed() {
Helper::start_xmrig(
&self.helper,
&self.state.xmrig,
&self.state.gupax.absolute_xmrig_path,
Arc::clone(&self.sudo),
);
} else if cfg!(unix) {
} else {
self.sudo.lock().unwrap().signal = ProcessSignal::Start;
self.error_state.ask_sudo(&self.sudo);
}

View file

@ -117,18 +117,27 @@ impl Helper {
.stdin(Stdio::piped())
.spawn()
.unwrap();
// Write the [sudo] password to STDIN.
let mut stdin = child.stdin.take().unwrap();
use std::io::Write;
if let Err(e) = writeln!(stdin, "{}\n", sudo.lock().unwrap().pass) {
error!("Sudo Kill | STDIN error: {}", e);
if Self::password_needed() {
// Write the [sudo] password to STDIN.
let mut stdin = child.stdin.take().unwrap();
use std::io::Write;
if let Err(e) = writeln!(stdin, "{}\n", sudo.lock().unwrap().pass) {
error!("Sudo Kill | STDIN error: {}", e);
}
}
// Return exit code of [sudo/kill].
child.wait().unwrap().success()
}
#[cold]
#[inline(never)]
pub fn password_needed() -> bool {
let cmd = std::process::Command::new("sudo")
.args(["-n", "true"])
.spawn();
cmd.is_err()
}
#[cold]
#[inline(never)]
// Just sets some signals for the watchdog thread to pick up on.
@ -455,11 +464,8 @@ impl Helper {
let mut stdin = pair.master.take_writer().unwrap();
// 2. Input [sudo] pass, wipe, then drop.
if cfg!(unix) {
if cfg!(unix) && Self::password_needed() {
debug!("XMRig | Inputting [sudo] and wiping...");
// a) Sleep to wait for [sudo]'s non-echo prompt (on Unix).
// this prevents users pass from showing up in the STDOUT.
sleep!(3000);
if let Err(e) = writeln!(stdin, "{}", sudo.lock().unwrap().pass) {
error!("XMRig | Sudo STDIN error: {}", e);
};

View file

@ -230,7 +230,7 @@ pub fn init_auto(app: &mut App) {
warn!(
"Gupaxx | Xmrig instance is already running outside of Gupaxx ! Skipping auto-node..."
);
} else if cfg!(windows) {
} else if cfg!(windows) || !Helper::password_needed() {
Helper::start_xmrig(
&app.helper,
&app.state.xmrig,