From 4cb80ab1fbb66072bb6920383a2cbe134c766547 Mon Sep 17 00:00:00 2001 From: Sascha Grunert Date: Sat, 1 Apr 2017 11:25:48 +0200 Subject: Added API function for retrieving the 'run path' --- src/lib.rs | 16 ++++++++++------ src/main.rs | 6 ++++-- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 6075f9b..74b9239 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -74,12 +74,7 @@ impl WireGuard { debug!("Created local socket."); // Create the socket directory if not existing - let mut socket_path = if Path::new("/run").exists() { - PathBuf::from("/run") - } else { - PathBuf::from("/var").join("run") - }; - socket_path = socket_path.join("wireguard"); + let mut socket_path = Self::get_run_path().join("wireguard"); if !socket_path.exists() { debug!("Creating socket path: {}", socket_path.display()); @@ -235,4 +230,13 @@ impl WireGuard { fn chmod(_path: &Path, _perms: u32) -> Result<()> { Ok(()) } + + /// Returns the path where the socket and pid file will be stored + pub fn get_run_path() -> PathBuf { + if Path::new("/run").exists() { + PathBuf::from("/run") + } else { + PathBuf::from("/var").join("run") + } + } } diff --git a/src/main.rs b/src/main.rs index 326639c..2c66085 100644 --- a/src/main.rs +++ b/src/main.rs @@ -61,11 +61,13 @@ fn run() -> Result<()> { } debug!("Starting daemon."); + // Daemonize the process + let pid_path = WireGuard::get_run_path(); let daemonize = Daemonize::new() - .pid_file("/tmp/wireguard.pid") + .pid_file(pid_path.join("wireguard.pid")) .chown_pid_file(true) - .working_directory("/tmp") + .working_directory(pid_path) .user("nobody") .group("daemon") .umask(0o077); -- cgit v1.2.3-59-g8ed1b