Refactored subprocess spawning and fixed bug that prevented shell commands with arguments from running

This commit is contained in:
cyber-sushi 2024-05-09 02:31:01 +02:00
parent d45025f0d2
commit 7566e34efc

View file

@ -424,32 +424,27 @@ impl EventReader {
async fn spawn_subprocess(&self, command_list: &Vec<String>) { async fn spawn_subprocess(&self, command_list: &Vec<String>) {
let mut modifier_was_activated = self.modifier_was_activated.lock().await; let mut modifier_was_activated = self.modifier_was_activated.lock().await;
*modifier_was_activated = true; *modifier_was_activated = true;
match &self.environment.user { match (&self.environment.user, &self.environment.sudo_user) {
Ok(user) if user == &"root".to_string() => { (_, Ok(sudo_user)) => {
match &self.environment.sudo_user { for command in command_list {
Ok(sudo_user) => { match fork() {
for command in command_list { Ok(Fork::Child) => {
match fork() { Command::new("sh")
Ok(Fork::Child) => { .arg("-c")
Command::new("sh") .arg(format!("runuser {} -c '{}'", sudo_user.as_str(), command))
.arg("-c") .stdin(Stdio::null())
.arg(format!("runuser {} -c {}", sudo_user.as_str(), command)) .stdout(Stdio::null())
.stdin(Stdio::null()) .stderr(Stdio::null())
.stdout(Stdio::null()) .spawn()
.stderr(Stdio::null()) .expect("Failed to run command.");
.spawn() std::process::exit(0);
.expect("Failed to run command."); },
std::process::exit(0); Ok(Fork::Parent(_)) => (),
}, Err(_) => std::process::exit(1),
Ok(Fork::Parent(_)) => (), }
Err(_) => std::process::exit(1),
}
}
},
_ => {}
} }
}, },
Ok(_) => { (Ok(_), Err(_)) => {
for command in command_list { for command in command_list {
match fork() { match fork() {
Ok(Fork::Child) => { Ok(Fork::Child) => {
@ -467,8 +462,8 @@ impl EventReader {
Err(_) => std::process::exit(1), Err(_) => std::process::exit(1),
} }
} }
} },
Err(_) => {}, (_, _) => {}
} }
} }