Browse Source

Allow to daemonize

ng
Rodolphe Bréard 1 month ago
parent
commit
80bd4e6478
Failed to extract signature
  1. 17
      Cargo.lock
  2. 4
      Cargo.toml
  3. 41
      src/main.rs

17
Cargo.lock

@ -6,7 +6,9 @@ version = 3
name = "acmed"
version = "0.25.0-dev"
dependencies = [
"anyhow",
"clap",
"daemonize",
"syslog-tracing",
"tokio",
"tracing",
@ -77,6 +79,12 @@ dependencies = [
"windows-sys",
]
[[package]]
name = "anyhow"
version = "1.0.94"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7"
[[package]]
name = "backtrace"
version = "0.3.74"
@ -143,6 +151,15 @@ version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
[[package]]
name = "daemonize"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab8bfdaacb3c887a54d41bdf48d3af8873b3f5566469f8ba21b92057509f116e"
dependencies = [
"libc",
]
[[package]]
name = "gimli"
version = "0.31.1"

4
Cargo.toml

@ -22,9 +22,11 @@ ed25519 = []
ed448 = []
[dependencies]
anyhow = { version = "1.0.94", default-features = false, features = ["std"] }
clap = { version = "4.5.23", default-features = false, features = ["color", "derive", "help", "std"] }
daemonize = { version = "0.5.0", default-features = false }
syslog-tracing = { version = "0.3.1", default-features = false }
tokio = { version = "1.42.0", default-features = false, features = ["rt", "rt-multi-thread"] }
tokio = { version = "1.42.0", default-features = false, features = ["rt", "rt-multi-thread", "time"] }
tracing = { version = "0.1.41", default-features = false, features = ["std"] }
tracing-subscriber = { version = "0.3.19", default-features = false, features = ["ansi", "fmt", "std"] }

41
src/main.rs

@ -1,7 +1,13 @@
mod cli;
mod log;
use anyhow::{Context, Result};
use clap::Parser;
use daemonize::Daemonize;
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;
use std::process;
pub const APP_IDENTITY: &[u8] = b"acmed\0";
pub const APP_THREAD_NAME: &str = "acmed-runtime";
@ -15,7 +21,10 @@ fn main() {
println!("Debug: args: {args:?}");
// Initialize the logging system
log::init(args.log_level, args.log.log_syslog);
log::init(args.log_level, !args.log.log_stderr);
// Initialize the server (PID file and daemon)
init_server(args.foreground, args.pid.get_pid_file());
// Starting ACMEd
tokio::runtime::Builder::new_multi_thread()
@ -27,5 +36,33 @@ fn main() {
}
async fn start() {
tracing::info!("Starting ACMEd.");
tracing::info!("starting ACMEd");
}
fn init_server(foreground: bool, pid_file: Option<&Path>) {
if !foreground {
let mut daemonize = Daemonize::new();
if let Some(f) = pid_file {
daemonize = daemonize.pid_file(f);
}
if let Err(e) = daemonize.start() {
tracing::error!("error: {e:#}");
std::process::exit(3);
}
} else if let Some(f) = pid_file {
if let Err(e) = write_pid_file(f) {
tracing::error!("error: {e:#}");
std::process::exit(3);
}
}
}
fn write_pid_file(pid_file: &Path) -> Result<()> {
let data = format!("{}\n", process::id()).into_bytes();
let mut file = File::create(pid_file).with_context(|| format!("{}", pid_file.display()))?;
file.write_all(&data)
.with_context(|| format!("{}", pid_file.display()))?;
file.sync_all()
.with_context(|| format!("{}", pid_file.display()))?;
Ok(())
}
Loading…
Cancel
Save