Ensure the launched commands are not terminated by the stop of xremap. (#214)

* Ensure the launched commands are not terminated by the stop of xremap.

Fix #213.

Signed-off-by: Kai Xia <kaix+github@fastmail.com>

* also stop child processes on unhappy path.

Signed-off-by: Kai Xia <kaix+github@fastmail.com>

* fix fmt

Signed-off-by: Kai Xia <kaix+github@fastmail.com>

Signed-off-by: Kai Xia <kaix+github@fastmail.com>
pull/216/head
Kai Xia(夏恺) 1 year ago committed by GitHub
parent cbf5cd027c
commit 9f5002ee1b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

10
Cargo.lock generated

@ -311,6 +311,15 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "fork"
version = "0.1.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9788ce090af4bf8d6e8f43d3f7d12305c787456387bd2d88856fcda3aa1f0dca"
dependencies = [
"libc",
]
[[package]]
name = "funty"
version = "2.0.0"
@ -1153,6 +1162,7 @@ dependencies = [
"derive-where",
"env_logger",
"evdev",
"fork",
"indoc",
"lazy_static",
"log",

@ -13,6 +13,7 @@ clap_complete = "3.2.5"
derive-where = "1.0.0"
env_logger = "0.9.1"
evdev = "0.12.0"
fork = "0.1"
indoc = "1.0"
lazy_static = "1.4.0"
log = "0.4.17"

@ -1,11 +1,12 @@
use std::thread;
use evdev::{uinput::VirtualDevice, EventType, InputEvent, Key};
use fork::{fork, setsid, Fork};
use log::debug;
use log::error;
use nix::sys::signal;
use nix::sys::signal::{sigaction, SaFlags, SigAction, SigHandler, SigSet};
use std::process::{Command, Stdio};
use std::process::{exit, Command, Stdio};
use crate::{action::Action, event::KeyEvent};
@ -58,15 +59,40 @@ impl ActionDispatcher {
}
debug!("Running command: {:?}", command);
match Command::new(&command[0])
.args(&command[1..])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
{
Ok(child) => debug!("Process spawned: {:?}, pid {}", command, child.id()),
Err(e) => error!("Error running command: {:?}", e),
match fork() {
Ok(Fork::Child) => {
// Child process should fork again, and the parent should exit 0, while the child
// should spawn the user command then exit as well.
match fork() {
Ok(Fork::Child) => {
setsid().expect("Failed to setsid.");
match Command::new(&command[0])
.args(&command[1..])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
{
Ok(child) => {
debug!("Process started: {:?}, pid {}", command, child.id());
exit(0);
}
Err(e) => {
error!("Error running command: {:?}", e);
exit(1);
}
}
}
Ok(Fork::Parent(_)) => exit(0),
Err(e) => {
error!("Error spawning process: {:?}", e);
exit(1);
}
}
}
// Parent should simply continue.
Ok(Fork::Parent(_)) => (),
Err(e) => error!("Error spawning process: {:?}", e),
}
}
}

Loading…
Cancel
Save