Use fork() to spawn subprocess

This commit is contained in:
cyber-sushi 2024-05-08 17:55:37 +02:00
parent 2d3f4785b9
commit 4529b0499c

View file

@ -1,6 +1,7 @@
use std::{collections::HashMap, sync::Arc, option::Option, process::Command}; use std::{collections::HashMap, sync::Arc, option::Option, process::{Command, Stdio}};
use tokio::sync::Mutex; use tokio::sync::Mutex;
use tokio_stream::StreamExt; use tokio_stream::StreamExt;
use fork::{fork, setsid, Fork};
use evdev::{EventStream, Key, RelativeAxisType, AbsoluteAxisType, EventType, InputEvent}; use evdev::{EventStream, Key, RelativeAxisType, AbsoluteAxisType, EventType, InputEvent};
use crate::virtual_devices::VirtualDevices; use crate::virtual_devices::VirtualDevices;
use crate::Config; use crate::Config;
@ -440,11 +441,22 @@ impl EventReader {
}, },
Ok(_) => { Ok(_) => {
for command in command_list { for command in command_list {
Command::new("sh") match fork() {
.arg("-c") Ok(Fork::Child) => {
.arg(command) setsid().unwrap();
.spawn() Command::new("sh")
.expect("Failed to run command."); .arg("-c")
.arg(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),
}
} }
} }
Err(_) => {}, Err(_) => {},
@ -561,4 +573,3 @@ impl EventReader {
} }
} }
} }