diff --git a/CHANGELOG.md b/CHANGELOG.md index d7d6272..6aae164 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,3 +16,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - `post_operation_hook` has been renamed `post_operation_hooks`. - By default, logs are now sent to syslog instead of stderr. +- The process is now daemonized by default. It is possible to still run it in the foreground using the `--foregroung` flag. diff --git a/acmed/Cargo.toml b/acmed/Cargo.toml index 327a91a..77224e2 100644 --- a/acmed/Cargo.toml +++ b/acmed/Cargo.toml @@ -13,6 +13,7 @@ include = ["src/**/*", "Cargo.toml", "LICENSE-*.txt"] [dependencies] acme-lib = "0.4" clap = "2.32" +daemonize = "0.3" env_logger = "0.6" handlebars = "2.0.0-beta.1" log = "0.4" diff --git a/acmed/src/main.rs b/acmed/src/main.rs index 2d1410a..daf3e9b 100644 --- a/acmed/src/main.rs +++ b/acmed/src/main.rs @@ -1,4 +1,5 @@ use clap::{App, Arg}; +use daemonize::Daemonize; use log::{error, LevelFilter}; mod acmed; @@ -10,6 +11,7 @@ mod logs; mod storage; pub const APP_NAME: &str = "acmed"; +pub const DEFAULT_PID_FILE: &str = "/var/run/admed.pid"; pub const DEFAULT_CONFIG_FILE: &str = "/etc/acmed/acmed.toml"; pub const DEFAULT_ACCOUNTS_DIR: &str = "/etc/acmed/accounts"; pub const DEFAULT_CERT_DIR: &str = "/etc/acmed/certs"; @@ -31,14 +33,14 @@ fn main() { Arg::with_name("config") .short("c") .long("config") - .help("Specify an alternative configuration file.") + .help("Specify an alternative configuration file") .takes_value(true) .value_name("FILE"), ) .arg( Arg::with_name("log-level") .long("log-level") - .help("Specify the log level.") + .help("Specify the log level") .takes_value(true) .value_name("LEVEL") .possible_values(&["error", "warn", "info", "debug", "trace"]), @@ -46,15 +48,30 @@ fn main() { .arg( Arg::with_name("to-syslog") .long("log-syslog") - .help("Send log messages via syslog.") + .help("Sends log messages via syslog") .conflicts_with("to-stderr"), ) .arg( Arg::with_name("to-stderr") .long("log-stderr") - .help("Print log messages to the standard error output.") + .help("Prints log messages to the standard error output") .conflicts_with("log-syslog"), ) + .arg( + Arg::with_name("foregroung") + .short("f") + .long("foregroung") + .help("Runs in the foregroung") + .conflicts_with("to-stderr"), + ) + .arg( + Arg::with_name("pid-file") + .long("pid-file") + .help("Specifies the location of the PID file") + .takes_value(true) + .value_name("FILE") + .conflicts_with("foregroung"), + ) .get_matches(); match logs::set_log_system( @@ -65,10 +82,22 @@ fn main() { Ok(_) => {} Err(e) => { eprintln!("Error: {}", e); - std::process::exit(1); + std::process::exit(2); } }; + if !matches.is_present("foregroung") { + let pid_file = matches.value_of("pid-file").unwrap_or(DEFAULT_PID_FILE); + let daemonize = Daemonize::new().pid_file(pid_file); + match daemonize.start() { + Ok(_) => {} + Err(e) => { + eprintln!("Error: {}", e); + std::process::exit(3); + } + } + } + let config_file = matches.value_of("config").unwrap_or(DEFAULT_CONFIG_FILE); let mut srv = match acmed::Acmed::new(&config_file) { Ok(s) => s,