diff --git a/CHANGELOG.md b/CHANGELOG.md index 5bdabc9..f4a0170 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added - Wildcard certificates are now supported. In the file name, the `*` is replaced by `_`. +### Changed +- The PID file is now always written whether or not ACMEd is running in the foreground. Previously, it was written only when running in the background. + ### Fixed - In the directory, the `externalAccountRequired` field is now a boolean instead of a string. diff --git a/acme_common/src/lib.rs b/acme_common/src/lib.rs index 62d38f7..9dfb104 100644 --- a/acme_common/src/lib.rs +++ b/acme_common/src/lib.rs @@ -1,9 +1,24 @@ use daemonize::Daemonize; +use std::fs::File; +use std::io::prelude::*; +use std::process; pub mod crypto; pub mod error; pub mod logs; +macro_rules! exit_match { + ($e: expr) => { + match $e { + Ok(_) => {} + Err(e) => { + eprintln!("Error: {}", e); + std::process::exit(3); + } + } + }; +} + pub fn b64_encode>(input: &T) -> String { base64::encode_config(input, base64::URL_SAFE_NO_PAD) } @@ -11,12 +26,16 @@ pub fn b64_encode>(input: &T) -> String { pub fn init_server(foreground: bool, pid_file: &str) { if !foreground { let daemonize = Daemonize::new().pid_file(pid_file); - match daemonize.start() { - Ok(_) => {} - Err(e) => { - eprintln!("Error: {}", e); - std::process::exit(3); - } - } + exit_match!(daemonize.start()); + } else { + exit_match!(write_pid_file(pid_file)); } } + +fn write_pid_file(pid_file: &str) -> Result<(), error::Error> { + let data = format!("{}\n", process::id()).into_bytes(); + let mut file = File::create(pid_file)?; + file.write_all(&data)?; + file.sync_all()?; + Ok(()) +} diff --git a/acmed/src/main.rs b/acmed/src/main.rs index bf7c4c6..fd351e0 100644 --- a/acmed/src/main.rs +++ b/acmed/src/main.rs @@ -86,8 +86,7 @@ fn main() { .long("pid-file") .help("Specifies the location of the PID file") .takes_value(true) - .value_name("FILE") - .conflicts_with("foreground"), + .value_name("FILE"), ) .arg( Arg::with_name("root-cert") diff --git a/tacd/src/main.rs b/tacd/src/main.rs index 4462740..a4225c5 100644 --- a/tacd/src/main.rs +++ b/tacd/src/main.rs @@ -128,8 +128,7 @@ fn main() { .long("pid-file") .help("Specifies the location of the PID file") .takes_value(true) - .value_name("FILE") - .conflicts_with("foreground"), + .value_name("FILE"), ) .get_matches();