From 7566e34efc7e5d09542db9460639fa142913de5d Mon Sep 17 00:00:00 2001 From: cyber-sushi Date: Thu, 9 May 2024 02:31:01 +0200 Subject: [PATCH] Refactored subprocess spawning and fixed bug that prevented shell commands with arguments from running --- src/event_reader.rs | 47 ++++++++++++++++++++------------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/src/event_reader.rs b/src/event_reader.rs index 16f4e98..73587d3 100644 --- a/src/event_reader.rs +++ b/src/event_reader.rs @@ -424,32 +424,27 @@ impl EventReader { async fn spawn_subprocess(&self, command_list: &Vec) { let mut modifier_was_activated = self.modifier_was_activated.lock().await; *modifier_was_activated = true; - match &self.environment.user { - Ok(user) if user == &"root".to_string() => { - match &self.environment.sudo_user { - Ok(sudo_user) => { - for command in command_list { - match fork() { - Ok(Fork::Child) => { - Command::new("sh") - .arg("-c") - .arg(format!("runuser {} -c {}", sudo_user.as_str(), command)) - .stdin(Stdio::null()) - .stdout(Stdio::null()) - .stderr(Stdio::null()) - .spawn() - .expect("Failed to run command."); - std::process::exit(0); - }, - Ok(Fork::Parent(_)) => (), - Err(_) => std::process::exit(1), - } - } - }, - _ => {} + match (&self.environment.user, &self.environment.sudo_user) { + (_, Ok(sudo_user)) => { + for command in command_list { + match fork() { + Ok(Fork::Child) => { + Command::new("sh") + .arg("-c") + .arg(format!("runuser {} -c '{}'", sudo_user.as_str(), command)) + .stdin(Stdio::null()) + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + .expect("Failed to run command."); + std::process::exit(0); + }, + Ok(Fork::Parent(_)) => (), + Err(_) => std::process::exit(1), + } } }, - Ok(_) => { + (Ok(_), Err(_)) => { for command in command_list { match fork() { Ok(Fork::Child) => { @@ -467,8 +462,8 @@ impl EventReader { Err(_) => std::process::exit(1), } } - } - Err(_) => {}, + }, + (_, _) => {} } }