From cafaf10bcc8d0cb1d677fbb9ccd287cb1dc02437 Mon Sep 17 00:00:00 2001
From: Cyrix126 <gupaxx@baermail.fr>
Date: Tue, 28 Jan 2025 12:40:20 +0100
Subject: [PATCH] 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
---
 src/app/panels/bottom.rs |  6 +++---
 src/helper/xrig/xmrig.rs | 26 ++++++++++++++++----------
 src/inits.rs             |  2 +-
 3 files changed, 20 insertions(+), 14 deletions(-)

diff --git a/src/app/panels/bottom.rs b/src/app/panels/bottom.rs
index d222652..eade517 100644
--- a/src/app/panels/bottom.rs
+++ b/src/app/panels/bottom.rs
@@ -258,7 +258,7 @@ impl crate::app::App {
                             );
                         }
                         ProcessName::Xmrig => {
-                            if cfg!(windows) {
+                            if cfg!(windows) || !Helper::password_needed() {
                                 Helper::restart_xmrig(
                                     &self.helper,
                                     &self.state.xmrig,
@@ -329,14 +329,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);
                                 }
diff --git a/src/helper/xrig/xmrig.rs b/src/helper/xrig/xmrig.rs
index d63d1cc..3c7d347 100644
--- a/src/helper/xrig/xmrig.rs
+++ b/src/helper/xrig/xmrig.rs
@@ -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);
             };
diff --git a/src/inits.rs b/src/inits.rs
index 20f7fc9..efc4c26 100644
--- a/src/inits.rs
+++ b/src/inits.rs
@@ -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,