diff --git a/Cargo.lock b/Cargo.lock index b9c2e91..83941d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,7 +9,7 @@ dependencies = [ "anyhow", "clap", "config", - "daemonize", + "fork", "nom", "reqwest", "serde", @@ -221,15 +221,6 @@ dependencies = [ "winnow", ] -[[package]] -name = "daemonize" -version = "0.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ab8bfdaacb3c887a54d41bdf48d3af8873b3f5566469f8ba21b92057509f116e" -dependencies = [ - "libc", -] - [[package]] name = "displaydoc" version = "0.2.5" @@ -262,6 +253,15 @@ version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" +[[package]] +name = "fork" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05dc8b302e04a1c27f4fe694439ef0f29779ca4edc205b7b58f00db04e29656d" +dependencies = [ + "libc", +] + [[package]] name = "form_urlencoded" version = "1.2.1" diff --git a/Cargo.toml b/Cargo.toml index 90f9006..d68dbfd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ ed448 = [] anyhow = { version = "1.0.94", default-features = false, features = ["std"] } clap = { version = "4.5.23", default-features = false, features = ["color", "derive", "help", "std", "string"] } config = { version = "0.15.6", default-features = false, features = ["toml"] } -daemonize = { version = "0.5.0", default-features = false } +fork = { version = "0.2.0", default-features = false } nom = { version = "7.1.3", default-features = false } reqwest = { version = "0.12.10", default-features = false, features = ["http2", "charset", "rustls-tls"] } serde = { version = "1.0.216", default-features = false, features = ["derive"] } diff --git a/src/main.rs b/src/main.rs index b9e0490..1cd792a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use crate::config::AcmedConfig; use crate::http::HttpRoutine; use anyhow::Result; use clap::Parser; -use daemonize::Daemonize; +use fork::{daemon, Fork}; use std::fs::File; use std::io::prelude::*; use std::path::Path; @@ -17,6 +17,20 @@ pub const APP_IDENTITY: &[u8] = b"acmed\0"; pub const APP_THREAD_NAME: &str = "acmed-runtime"; pub const INTERNAL_HOOK_PREFIX: &str = "internal:"; +macro_rules! run_server { + ($cfg: ident, $args: ident) => { + if let Some(pid_file_path) = $args.pid.get_pid_file() { + let _ = write_pid_file(pid_file_path); + } + let tokio_runtime = tokio::runtime::Builder::new_multi_thread() + .enable_all() + .thread_name(APP_THREAD_NAME) + .build() + .unwrap() + .block_on(start($cfg)); + }; +} + fn main() { // Load the command-line interface let args = cli::CliArgs::parse(); @@ -36,18 +50,14 @@ fn main() { std::process::exit(3) } - // Initialize the server (PID file and daemon) - if init_server(args.foreground, args.pid.get_pid_file()).is_err() { - std::process::exit(4); - } - // Starting ACMEd - tokio::runtime::Builder::new_multi_thread() - .enable_all() - .thread_name(APP_THREAD_NAME) - .build() - .unwrap() - .block_on(start(cfg)); + if args.foreground { + run_server!(cfg, args); + } else { + if let Ok(Fork::Child) = daemon(false, false) { + run_server!(cfg, args); + } + } } async fn start(cnf: AcmedConfig) { @@ -89,19 +99,6 @@ async fn debug_remove_me(http_client: crate::http::HttpClient) { // TODO: err(Alternate) #[tracing::instrument(level = "trace", err)] -fn init_server(foreground: bool, pid_file: Option<&Path>) -> Result<()> { - if !foreground { - let mut daemonize = Daemonize::new(); - if let Some(f) = pid_file { - daemonize = daemonize.pid_file(f); - } - daemonize.start()? - } else if let Some(f) = pid_file { - write_pid_file(f)? - } - Ok(()) -} - fn write_pid_file(pid_file: &Path) -> Result<()> { let data = format!("{}\n", process::id()).into_bytes(); let mut file = File::create(pid_file)?;