From 9d88c95f3355a152e161700b0e27d776bdf566b8 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Sun, 18 Aug 2024 22:22:31 +0200 Subject: [PATCH] Fix systemd socket activation --- Cargo.lock | 17 +++++++++++++++++ Cargo.toml | 1 + src/server/command.rs | 16 +++++----------- 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index ad147a3..dee32b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1010,6 +1010,15 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "memoffset" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a" +dependencies = [ + "autocfg", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -1058,6 +1067,7 @@ dependencies = [ "prettytable", "rand", "ratatui", + "sd-notify", "serde", "serde_json", "sqlx", @@ -1079,6 +1089,7 @@ dependencies = [ "cfg-if", "cfg_aliases", "libc", + "memoffset", ] [[package]] @@ -1524,6 +1535,12 @@ dependencies = [ "untrusted", ] +[[package]] +name = "sd-notify" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4646d6f919800cd25c50edb49438a1381e2cd4833c027e75e8897981c50b8b5e" + [[package]] name = "serde" version = "1.0.208" diff --git a/Cargo.toml b/Cargo.toml index 5fbf4ac..34877bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ nix = { version = "0.29.0", features = ["fs", "process", "socket", "user"] } prettytable = "0.10.0" rand = "0.8.5" ratatui = { version = "0.28.0", optional = true } +sd-notify = "0.4.2" serde = "1.0.208" serde_json = { version = "1.0.125", features = ["preserve_order"] } sqlx = { version = "0.8.0", features = ["runtime-tokio", "mysql", "tls-rustls"] } diff --git a/src/server/command.rs b/src/server/command.rs index eab5ba3..5c4da06 100644 --- a/src/server/command.rs +++ b/src/server/command.rs @@ -40,10 +40,6 @@ pub async fn handle_command( ) -> anyhow::Result<()> { let config = read_config_from_path_with_arg_overrides(config_path, args.config_overrides)?; - // if let Err(e) = &result { - // eprintln!("{}", e); - // } - match args.subcmd { ServerCommand::Listen => listen_for_incoming_connections(socket_path, config).await, ServerCommand::SocketActivate => socket_activate(config).await, @@ -61,14 +57,12 @@ async fn socket_activate(config: ServerConfig) -> anyhow::Result<()> { } async fn get_socket_from_systemd() -> anyhow::Result { - let fd = std::env::var("LISTEN_FDS") - .context("LISTEN_FDS not set, not running under systemd?")? - .parse::() - .context("Failed to parse LISTEN_FDS")?; + let fd = sd_notify::listen_fds() + .context("Failed to get file descriptors from systemd")? + .next() + .context("No file descriptors received from systemd")?; - if fd != 1 { - return Err(anyhow::anyhow!("Unexpected LISTEN_FDS value: {}", fd)); - } + log::debug!("Received file descriptor from systemd: {}", fd); let std_unix_stream = unsafe { StdUnixStream::from_raw_fd(fd) }; let socket = TokioUnixStream::from_std(std_unix_stream)?;