aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSascha Grunert <mail@saschagrunert.de>2017-02-28 14:19:11 +0100
committerSascha Grunert <mail@saschagrunert.de>2017-02-28 14:19:11 +0100
commit4ff7ee0a411796a3099e549f2f35a191283aab7b (patch)
treeb22b8187f91fa7d6076e4abaf1dd84f5dd3222fd
parentFixed indents (diff)
downloadwireguard-rs-4ff7ee0a411796a3099e549f2f35a191283aab7b.tar.xz
wireguard-rs-4ff7ee0a411796a3099e549f2f35a191283aab7b.zip
Added daemon support
-rw-r--r--Cargo.toml1
-rw-r--r--src/error.rs3
-rw-r--r--src/lib.rs3
-rw-r--r--src/main.rs31
4 files changed, 24 insertions, 14 deletions
diff --git a/Cargo.toml b/Cargo.toml
index edbc6a5..afc2faa 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -12,3 +12,4 @@ daemonize = "0"
libc = "0"
log = "0"
mowl = "1"
+nix = "0"
diff --git a/src/error.rs b/src/error.rs
index f60a7bd..64a9ce6 100644
--- a/src/error.rs
+++ b/src/error.rs
@@ -3,6 +3,7 @@ use std::error::Error;
use std::{ffi, fmt, io, net, convert};
use log;
+use daemonize;
/// Common Tunnel Result type
pub type WgResult<T> = Result<T, WgError>;
@@ -71,12 +72,14 @@ macro_rules! from_error {
}
from_error! {
+ daemonize::DaemonizeError,
io::Error,
log::SetLoggerError,
ffi::NulError,
net::AddrParseError,
}
+#[macro_export]
macro_rules! bail {
($($fmt:tt)*) => (
#[cfg_attr(feature = "cargo-clippy", allow(useless_format))]
diff --git a/src/lib.rs b/src/lib.rs
index da96029..b60ba72 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -7,9 +7,10 @@
#[macro_use]
extern crate log;
extern crate libc;
+extern crate daemonize;
#[macro_use]
-mod error;
+pub mod error;
mod uapi;
pub use error::{WgResult, WgError};
diff --git a/src/main.rs b/src/main.rs
index 762384d..1da531f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,20 +8,21 @@ extern crate libc;
#[macro_use]
extern crate log;
extern crate mowl;
+extern crate nix;
+
+#[macro_use]
extern crate wireguard;
use clap::App;
use daemonize::Daemonize;
use log::LogLevel;
-use wireguard::{WireGuard, WgResult, WgError};
+use wireguard::{WireGuard, WgResult, WgError, error};
-use std::fs::File;
-use std::io::Write;
use std::process::exit;
fn main() {
if let Err(error) = run() {
- error!("Error: {}", error);
+ error!("{}", error);
exit(1);
}
}
@@ -55,10 +56,14 @@ fn run() -> WgResult<()> {
let wireguard = WireGuard::new(interface_name)?;
// Run the instance in foreground if needed
- if matches.is_present("foreground") {
- wireguard.run()?;
- } else {
- // TODO: Daemonize the process
+ if !matches.is_present("foreground") {
+ // Check if we are the root user
+ if nix::unistd::getuid() != 0 {
+ bail!("You are not the root user which can spawn the daemon.");
+ }
+
+ debug!("Starting daemon.");
+ // Daemonize the process
let daemonize = Daemonize::new()
.pid_file("/tmp/wireguard.pid")
.chown_pid_file(true)
@@ -67,12 +72,12 @@ fn run() -> WgResult<()> {
.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),
- }?;
+ daemonize.start()?;
+ wireguard.run()?;
}
+ // Run the instance
+ wireguard.run()?;
+
Ok(())
}