From 498fb82bfbd5fc8c33874daa2f3cebc2c8cc2e3d Mon Sep 17 00:00:00 2001 From: Cyrix126 <58007246+Cyrix126@users.noreply.github.com> Date: Sat, 8 Feb 2025 15:31:27 +0100 Subject: [PATCH] fix: sudo less for user permitted in visudo, reduce time of first xmrig output by 3 seconds --- src/helper/p2pool.rs | 3 --- src/helper/xrig/xmrig.rs | 51 +++++++++++++++++++++++++++------------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/src/helper/p2pool.rs b/src/helper/p2pool.rs index 9670838..8044120 100644 --- a/src/helper/p2pool.rs +++ b/src/helper/p2pool.rs @@ -463,9 +463,6 @@ impl Helper { StartOptionsMode::Simple if state.local_node || override_to_local_node => { // use the local node // Build the p2pool argument - // dbg!("Simple mode/local node"); - // dbg!(local_node_rpc); - // dbg!(local_node_zmq); args.push("--wallet".to_string()); args.push(state.address.clone()); // Wallet address args.push("--host".to_string()); diff --git a/src/helper/xrig/xmrig.rs b/src/helper/xrig/xmrig.rs index 7168391..de98b1f 100644 --- a/src/helper/xrig/xmrig.rs +++ b/src/helper/xrig/xmrig.rs @@ -56,6 +56,11 @@ impl Helper { let mut i = 0; while let Some(Ok(line)) = stdout.next() { let line = strip_ansi_escapes::strip_str(line); + // skip until the first line of xmrig is appearing, hiding input for sudo + #[cfg(target_family = "unix")] + if i == 0 && !line.contains("ABOUT") { + continue; + } if let Err(e) = writeln!(output_parse.lock().unwrap(), "{}", line) { error!("XMRig PTY Parse | Output error: {}", e); } @@ -139,6 +144,7 @@ impl Helper { .stdin(Stdio::piped()) .spawn() .unwrap(); + // only insert the password if the user is required to if Self::password_needed() { // Write the [sudo] password to STDIN. let mut stdin = child.stdin.take().unwrap(); @@ -154,11 +160,26 @@ impl Helper { #[cold] #[inline(never)] + /// if the user has his visudo configured to not ask a password using sudo, this will return false pub fn password_needed() -> bool { + // Make sure sudo timestamp is reset + let reset = std::process::Command::new("sudo") + .arg("--reset-timestamp") + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .stdin(Stdio::piped()) + .status(); + match reset { + Ok(_) => {} + Err(_) => return true, + }; let cmd = std::process::Command::new("sudo") .args(["-n", "true"]) - .spawn(); - cmd.is_err() + .status(); + if cmd.is_ok_and(|s| s.success()) { + return false; + } + true } #[cold] #[inline(never)] @@ -483,8 +504,12 @@ impl Helper { // to emptiness so that it doesn't show up in the output. if cfg!(unix) { args.splice(..0, vec![path.display().to_string()]); - args.splice(..0, vec![r#"--"#.to_string()]); - args.splice(..0, vec![r#"--prompt="#.to_string()]); + // do not use prompt when sudo is not needed + // success is still to false if sudo has not been used to start xmrig + if sudo.lock().unwrap().success { + args.splice(..0, vec![r#"--"#.to_string()]); + args.splice(..0, vec![r#"--prompt="#.to_string()]); + } } // 1a. Create PTY debug!("XMRig | Creating PTY..."); @@ -519,19 +544,20 @@ impl Helper { drop(pair.slave); let mut stdin = pair.master.take_writer().unwrap(); - // 2. Input [sudo] pass, wipe, then drop. - if cfg!(unix) && Self::password_needed() { + if cfg!(unix) && sudo.lock().unwrap().success { debug!("XMRig | Inputting [sudo] and wiping..."); if let Err(e) = writeln!(stdin, "{}", sudo.lock().unwrap().pass) { error!("XMRig | Sudo STDIN error: {}", e); }; SudoState::wipe(&sudo); + SudoState::reset(&sudo); - // b) Reset GUI STDOUT just in case. - debug!("XMRig | Clearing GUI output..."); - gui_api.lock().unwrap().output.clear(); + info!("sudo wipe and output cleared"); } + // b) Reset GUI STDOUT just in case. + debug!("XMRig | Clearing GUI output..."); + gui_api.lock().unwrap().output.clear(); // 3. Set process state debug!("XMRig | Setting process state..."); @@ -541,13 +567,6 @@ impl Helper { lock.start = Instant::now(); drop(lock); - // // 4. Spawn PTY read thread - // debug!("XMRig | Spawning PTY read thread..."); - // let output_parse = Arc::clone(&process.lock().unwrap().output_parse); - // let output_pub = Arc::clone(&process.lock().unwrap().output_pub); - // spawn(enclose!((pub_api_xvb) async move { - // Self::read_pty_xmrig(output_parse, output_pub, reader, process_xvb, &pub_api_xvb).await; - // })); let output_parse = Arc::clone(&process.lock().unwrap().output_parse); let output_pub = Arc::clone(&process.lock().unwrap().output_pub);