Restructure SIGTERM signalling logic for Unix

This commit is contained in:
timvisee
2021-11-09 01:11:07 +01:00
parent 438e6e208c
commit ba2100f015
4 changed files with 33 additions and 1 deletions

View File

@@ -12,6 +12,7 @@ pub(crate) mod cli;
pub(crate) mod config;
pub(crate) mod mc;
pub(crate) mod monitor;
pub(crate) mod os;
pub(crate) mod proto;
pub(crate) mod proxy;
pub(crate) mod server;

20
src/os/mod.rs Normal file
View File

@@ -0,0 +1,20 @@
#[cfg(unix)]
pub mod unix;
/// Gracefully kill process.
///
/// # Panics
///
/// Panics on platforms other than Unix.
#[allow(unreachable_code)]
pub fn kill_gracefully(pid: u32) {
#[cfg(unix)]
unsafe {
unix::kill_gracefully(pid);
return;
}
unimplemented!(
"gracefully killing Minecraft server process not implemented on non-Unix platforms"
);
}

11
src/os/unix.rs Normal file
View File

@@ -0,0 +1,11 @@
/// Gracefully kill process on Unix by sending SIGTERM.
///
/// This is unsafe because the PID isn't checked.
pub unsafe fn kill_gracefully(pid: u32) {
debug!(target: "lazymc", "Sending SIGTERM signal to {} to kill server", pid);
let result = libc::kill(pid as i32, libc::SIGTERM);
trace!(target: "lazymc", "SIGTERM result: {}", result);
// TODO: send sigterm to childs as well?
// TODO: handle error if result != 0
}

View File

@@ -64,7 +64,7 @@ impl ServerState {
pub fn kill_server(&self) -> bool {
if let Some(pid) = *self.pid.lock().unwrap() {
debug!(target: "lazymc", "Sending kill signal to server");
kill_gracefully(pid);
crate::os::unix::kill_gracefully(pid);
// TODO: should we set this?
self.set_online(false);