diff options
author | Sascha Grunert <mail@saschagrunert.de> | 2017-02-27 16:07:57 +0100 |
---|---|---|
committer | Sascha Grunert <mail@saschagrunert.de> | 2017-02-27 16:07:57 +0100 |
commit | a3ad6b4009138960def0d4898843c3731323e574 (patch) | |
tree | 75653c99f1f5df48ca9bef6a5c12c60ced341ac3 | |
parent | Improved error handling (diff) | |
download | wireguard-rs-a3ad6b4009138960def0d4898843c3731323e574.tar.xz wireguard-rs-a3ad6b4009138960def0d4898843c3731323e574.zip |
Added first daemonization support
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/main.rs | 26 |
2 files changed, 24 insertions, 3 deletions
@@ -8,6 +8,7 @@ name = "userspace-wg" [dependencies] clap = { version = "2", features = ["yaml"] } +daemonize = "0" libc = "0" log = "0" mowl = "1" diff --git a/src/main.rs b/src/main.rs index 1dac51d..762384d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ #[macro_use] extern crate clap; +extern crate daemonize; extern crate libc; #[macro_use] @@ -10,9 +11,12 @@ extern crate mowl; extern crate wireguard; use clap::App; +use daemonize::Daemonize; use log::LogLevel; use wireguard::{WireGuard, WgResult, WgError}; +use std::fs::File; +use std::io::Write; use std::process::exit; fn main() { @@ -46,13 +50,29 @@ fn run() -> WgResult<()> { // Get the CLI matches let interface_name = matches.value_of("interface_name") .ok_or_else(|| WgError::new("No 'interface_name' provided"))?; - let _ = matches.is_present("foreground"); // Create a `WireGuard` instance let wireguard = WireGuard::new(interface_name)?; - // Run the instance - wireguard.run()?; + // Run the instance in foreground if needed + if matches.is_present("foreground") { + wireguard.run()?; + } else { + // TODO: Daemonize the process + let daemonize = Daemonize::new() + .pid_file("/tmp/wireguard.pid") + .chown_pid_file(true) + .working_directory("/tmp") + .user("nobody") + .group("daemon") + .umask(0o077); + + let mut f = File::create("output.txt").unwrap(); + match daemonize.start() { + Ok(_) => writeln!(f, "Success, daemonized!"), + Err(e) => writeln!(f, "{}", e), + }?; + } Ok(()) } |