diff --git a/crates/radicle-node/src/runtime.rs b/crates/radicle-node/src/runtime.rs index 8f0e5a40..f1d3cee0 100644 --- a/crates/radicle-node/src/runtime.rs +++ b/crates/radicle-node/src/runtime.rs @@ -347,7 +347,7 @@ impl Runtime { #[cfg(all(feature = "systemd", target_family = "unix"))] fn receive_listener() -> Option { use std::os::fd::FromRawFd; - match radicle_systemd::listen_fd("control") { + match radicle_systemd::listen::fd("control") { Ok(Some(fd)) => { // NOTE: Here, we should make a call to [`fstat(2)`](man:fstat(2)) // and make sure that the file descriptor we received actually diff --git a/crates/radicle-systemd/Cargo.toml b/crates/radicle-systemd/Cargo.toml index e27b39e7..1d233d97 100644 --- a/crates/radicle-systemd/Cargo.toml +++ b/crates/radicle-systemd/Cargo.toml @@ -7,3 +7,7 @@ license.workspace = true edition.workspace = true version = "0.9.0" rust-version.workspace = true + +[features] +default = ["listen"] +listen = [] \ No newline at end of file diff --git a/crates/radicle-systemd/src/lib.rs b/crates/radicle-systemd/src/lib.rs index 00ef6340..039c9586 100644 --- a/crates/radicle-systemd/src/lib.rs +++ b/crates/radicle-systemd/src/lib.rs @@ -1,41 +1,4 @@ //! Library for interaction with systemd, specialized for Radicle. -use std::env::{remove_var, var, VarError}; -use std::os::fd::RawFd; -use std::process::id; - -const LISTEN_PID: &str = "LISTEN_PID"; -const LISTEN_FDS: &str = "LISTEN_FDS"; -const LISTEN_FDNAMES: &str = "LISTEN_FDNAMES"; - -/// Minimum file descriptor used by systemd. -/// See . -const SD_LISTEN_FDS_START: RawFd = 3; - -/// Checks whether *at most one* file descriptor with given name was passed, returning it. -/// systemd sending none, more than one, or a file descriptor with a different name, all -/// results in [`Option::None`], but errors decoding environment variables or missing -/// environment variables will error. -/// This is a specialization of [`sd_listen_fds_with_names(3)`](man:sd_listen_fds_with_names(3)). -/// See: -/// - -/// - -/// - -/// - -pub fn listen_fd(name: &str) -> Result, VarError> { - let fd = match var(LISTEN_PID) { - Err(VarError::NotPresent) => Ok(None), - Err(err) => Err(err), - Ok(pid) if pid != id().to_string() => Ok(None), - _ if var(LISTEN_FDS)? != "1" || var(LISTEN_FDNAMES).ok() != Some(name.to_string()) => { - Ok(None) - } - _ => Ok(Some(SD_LISTEN_FDS_START)), - }; - - remove_var(LISTEN_PID); - remove_var(LISTEN_FDS); - remove_var(LISTEN_FDNAMES); - - fd -} +#[cfg(feature = "listen")] +pub mod listen; diff --git a/crates/radicle-systemd/src/listen.rs b/crates/radicle-systemd/src/listen.rs new file mode 100644 index 00000000..7405503a --- /dev/null +++ b/crates/radicle-systemd/src/listen.rs @@ -0,0 +1,39 @@ +use std::env::{remove_var, var, VarError}; +use std::os::fd::RawFd; +use std::process::id; + +const LISTEN_PID: &str = "LISTEN_PID"; +const LISTEN_FDS: &str = "LISTEN_FDS"; +const LISTEN_FDNAMES: &str = "LISTEN_FDNAMES"; + +/// Minimum file descriptor used by systemd. +/// See . +const SD_LISTEN_FDS_START: RawFd = 3; + +/// Checks whether *at most one* file descriptor with given name was passed, returning it. +/// systemd sending none, more than one, or a file descriptor with a different name, all +/// results in [`Option::None`], but errors decoding environment variables or missing +/// environment variables will error. +/// This is a specialization of [`sd_listen_fds_with_names(3)`](man:sd_listen_fds_with_names(3)). +/// See: +/// - +/// - +/// - +/// - +pub fn fd(name: &str) -> Result, VarError> { + let fd = match var(LISTEN_PID) { + Err(VarError::NotPresent) => Ok(None), + Err(err) => Err(err), + Ok(pid) if pid != id().to_string() => Ok(None), + _ if var(LISTEN_FDS)? != "1" || var(LISTEN_FDNAMES).ok() != Some(name.to_string()) => { + Ok(None) + } + _ => Ok(Some(SD_LISTEN_FDS_START)), + }; + + remove_var(LISTEN_PID); + remove_var(LISTEN_FDS); + remove_var(LISTEN_FDNAMES); + + fd +}