Files
horizon-airdef/crates/horizon-server/src/signals.rs
T
Ludovic Marie 9d21209ae8 horizon-server: server.toml, init, and a supervisor that runs the game
The first part of hosting a Horizon server with one small program
(docs/server.md). horizon-server is a new 1.3 MB binary with no window,
GPU, audio or controller code; it runs the game headless
(horizon-client --dedicated) as its engine.

- horizon-server-files: server.properties, eula.txt and the user lists,
  moved out of horizon-client unchanged so both programs share vanilla's
  exact reading and writing; horizon_client::server re-exports it.
- server.toml: typed settings for what owners change, [properties] for
  any other vanilla key, vanilla 26.3's defaults (a test checks them
  against ServerProperties). It is written into server.properties before
  every start; keys the game changes there itself (/whitelist on) are
  copied back afterwards, keeping the owner's comments (toml_edit).
  `import` turns a vanilla server.properties into server.toml.
- init: the EULA (only a yes writes eula=true), world, seed, port (checked
  free), whitelist and first operator; every question has a flag, so it
  also runs unattended.
- run: the console on stdin, a clean `stop` on Ctrl-C, SIGTERM or SIGHUP
  (the game runs in its own process group), and a restart when the game
  crashes: 5 s, doubling to 5 min, giving up after 5 crashes in 10 min.
  One supervisor per folder (a file lock).
- op, deop and whitelist for a stopped server, with vanilla's messages.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 20:20:09 +02:00

42 lines
1.3 KiB
Rust

//! Stop requests from outside: Ctrl-C in the terminal, SIGTERM from systemd or launchd, SIGHUP when the
//! terminal closes, the console window closing on Windows. The handler only counts them; the supervisor
//! sees the count and stops the game cleanly (a second request stops it at once).
use std::sync::atomic::{AtomicU32, Ordering};
static REQUESTS: AtomicU32 = AtomicU32::new(0);
/// How many stop requests arrived.
pub fn requests() -> u32 {
REQUESTS.load(Ordering::SeqCst)
}
#[cfg(unix)]
pub fn install() {
extern "C" fn handle(_: libc::c_int) {
REQUESTS.fetch_add(1, Ordering::SeqCst);
}
let handler = handle as extern "C" fn(libc::c_int);
for signal in [libc::SIGINT, libc::SIGTERM, libc::SIGHUP] {
// SAFETY: the handler only increments an atomic, which is async-signal-safe.
unsafe {
libc::signal(signal, handler as libc::sighandler_t);
}
}
}
#[cfg(windows)]
pub fn install() {
unsafe extern "system" fn handle(_: u32) -> windows_sys::core::BOOL {
REQUESTS.fetch_add(1, Ordering::SeqCst);
1
}
// SAFETY: the handler only increments an atomic; it stays valid for the life of the process.
unsafe {
windows_sys::Win32::System::Console::SetConsoleCtrlHandler(Some(handle), 1);
}
}
#[cfg(not(any(unix, windows)))]
pub fn install() {}