Fix systemd socket activation

This commit is contained in:
Oystein Kristoffer Tveit 2024-08-18 22:22:31 +02:00
parent 53f19b3d05
commit 9d88c95f33
Signed by: oysteikt
GPG Key ID: 9F2F7D8250F35146
3 changed files with 23 additions and 11 deletions

17
Cargo.lock generated
View File

@ -1010,6 +1010,15 @@ version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "memoffset"
version = "0.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "488016bfae457b036d996092f6cb448677611ce4449e970ceaf42695203f218a"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "minimal-lexical" name = "minimal-lexical"
version = "0.2.1" version = "0.2.1"
@ -1058,6 +1067,7 @@ dependencies = [
"prettytable", "prettytable",
"rand", "rand",
"ratatui", "ratatui",
"sd-notify",
"serde", "serde",
"serde_json", "serde_json",
"sqlx", "sqlx",
@ -1079,6 +1089,7 @@ dependencies = [
"cfg-if", "cfg-if",
"cfg_aliases", "cfg_aliases",
"libc", "libc",
"memoffset",
] ]
[[package]] [[package]]
@ -1524,6 +1535,12 @@ dependencies = [
"untrusted", "untrusted",
] ]
[[package]]
name = "sd-notify"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4646d6f919800cd25c50edb49438a1381e2cd4833c027e75e8897981c50b8b5e"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.208" version = "1.0.208"

View File

@ -20,6 +20,7 @@ nix = { version = "0.29.0", features = ["fs", "process", "socket", "user"] }
prettytable = "0.10.0" prettytable = "0.10.0"
rand = "0.8.5" rand = "0.8.5"
ratatui = { version = "0.28.0", optional = true } ratatui = { version = "0.28.0", optional = true }
sd-notify = "0.4.2"
serde = "1.0.208" serde = "1.0.208"
serde_json = { version = "1.0.125", features = ["preserve_order"] } serde_json = { version = "1.0.125", features = ["preserve_order"] }
sqlx = { version = "0.8.0", features = ["runtime-tokio", "mysql", "tls-rustls"] } sqlx = { version = "0.8.0", features = ["runtime-tokio", "mysql", "tls-rustls"] }

View File

@ -40,10 +40,6 @@ pub async fn handle_command(
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let config = read_config_from_path_with_arg_overrides(config_path, args.config_overrides)?; let config = read_config_from_path_with_arg_overrides(config_path, args.config_overrides)?;
// if let Err(e) = &result {
// eprintln!("{}", e);
// }
match args.subcmd { match args.subcmd {
ServerCommand::Listen => listen_for_incoming_connections(socket_path, config).await, ServerCommand::Listen => listen_for_incoming_connections(socket_path, config).await,
ServerCommand::SocketActivate => socket_activate(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<TokioUnixStream> { async fn get_socket_from_systemd() -> anyhow::Result<TokioUnixStream> {
let fd = std::env::var("LISTEN_FDS") let fd = sd_notify::listen_fds()
.context("LISTEN_FDS not set, not running under systemd?")? .context("Failed to get file descriptors from systemd")?
.parse::<i32>() .next()
.context("Failed to parse LISTEN_FDS")?; .context("No file descriptors received from systemd")?;
if fd != 1 { log::debug!("Received file descriptor from systemd: {}", fd);
return Err(anyhow::anyhow!("Unexpected LISTEN_FDS value: {}", fd));
}
let std_unix_stream = unsafe { StdUnixStream::from_raw_fd(fd) }; let std_unix_stream = unsafe { StdUnixStream::from_raw_fd(fd) };
let socket = TokioUnixStream::from_std(std_unix_stream)?; let socket = TokioUnixStream::from_std(std_unix_stream)?;