diff options
-rw-r--r-- | Cargo.lock | 11 | ||||
-rw-r--r-- | hbakd/Cargo.toml | 2 | ||||
-rw-r--r-- | hbakd/src/main.rs | 31 |
3 files changed, 41 insertions, 3 deletions
@@ -309,6 +309,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5" [[package]] +name = "fork" +version = "0.1.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bf2ca97a59201425e7ee4d197c9c4fea282fe87a97d666a580bda889b95b8e88" +dependencies = [ + "libc", +] + +[[package]] name = "generic-array" version = "0.14.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -369,7 +378,9 @@ dependencies = [ name = "hbakd" version = "0.3.0-dev" dependencies = [ + "clap", "ctrlc", + "fork", "hbak_common", "thiserror", ] diff --git a/hbakd/Cargo.toml b/hbakd/Cargo.toml index 7d4a520..0ee1260 100644 --- a/hbakd/Cargo.toml +++ b/hbakd/Cargo.toml @@ -6,6 +6,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +clap = { version = "4.4.18", features = ["derive"] } ctrlc = { version = "3.4.2", features = ["termination"] } hbak_common = { path = "../hbak_common" } +fork = "0.1.22" thiserror = "1.0" diff --git a/hbakd/src/main.rs b/hbakd/src/main.rs index 21f803d..51ae4d9 100644 --- a/hbakd/src/main.rs +++ b/hbakd/src/main.rs @@ -29,10 +29,35 @@ use std::sync::atomic::{AtomicBool, Ordering}; use std::sync::{Arc, Mutex}; use std::{cmp, io, thread}; +use clap::Parser; +use fork::{daemon, Fork}; + +#[derive(Debug, Parser)] +#[command(author, version, about, long_about = None)] +/// Background process to serve push and pull requests. +struct Args { + /// Stay attached to the terminal instead of daemonizing. + #[arg(short, long)] + debug: bool, +} + fn main() { - match serve() { - Ok(_) => {} - Err(e) => eprintln!("Error: {}", e), + let args = Args::parse(); + + if args.debug { + match serve() { + Ok(_) => {} + Err(e) => eprintln!("Error: {}", e), + } + } else { + match daemon(false, false) { + Ok(Fork::Parent(_)) => {} + Ok(Fork::Child) => match serve() { + Ok(_) => {} + Err(e) => eprintln!("Error: {}", e), + }, + Err(e) => eprintln!("Error: {}", io::Error::from_raw_os_error(e)), + } } } |