diff options
| author | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-05-12 10:58:23 -0300 |
|---|---|---|
| committer | Janito Vaqueiro Ferreira Filho <janito@mullvad.net> | 2018-06-20 08:08:31 -0300 |
| commit | 2632bf5a6b2cbae1d486cb27ddc7bf59dee8cca2 (patch) | |
| tree | f696fa2ef875d034ea090b2569141d60616d7873 | |
| parent | 717f315031630e9bdeeab7e39c4ad481e34ff3bc (diff) | |
| download | mullvadvpn-2632bf5a6b2cbae1d486cb27ddc7bf59dee8cca2.tar.xz mullvadvpn-2632bf5a6b2cbae1d486cb27ddc7bf59dee8cca2.zip | |
Wait for RPC connection info file to start client
| -rw-r--r-- | Cargo.lock | 1 | ||||
| -rw-r--r-- | mullvad-daemon/Cargo.toml | 1 | ||||
| -rw-r--r-- | mullvad-daemon/tests/common/mod.rs | 63 |
3 files changed, 60 insertions, 5 deletions
diff --git a/Cargo.lock b/Cargo.lock index 94737eb8cc..46cd320f69 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -769,6 +769,7 @@ dependencies = [ "mullvad-paths 0.1.0", "mullvad-rpc 0.1.0", "mullvad-types 0.1.0", + "notify 4.0.3 (registry+https://github.com/rust-lang/crates.io-index)", "os_pipe 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/mullvad-daemon/Cargo.toml b/mullvad-daemon/Cargo.toml index 1c567052d1..0bcc98b573 100644 --- a/mullvad-daemon/Cargo.toml +++ b/mullvad-daemon/Cargo.toml @@ -46,4 +46,5 @@ winapi = "0.3" [dev-dependencies] assert_matches = "1.0" duct = "0.10" +notify = "4.0" os_pipe = "0.6" diff --git a/mullvad-daemon/tests/common/mod.rs b/mullvad-daemon/tests/common/mod.rs index 27d74248b8..31107955bb 100644 --- a/mullvad-daemon/tests/common/mod.rs +++ b/mullvad-daemon/tests/common/mod.rs @@ -2,9 +2,9 @@ #[cfg(unix)] extern crate libc; -#[cfg(not(unix))] extern crate mullvad_ipc_client; extern crate mullvad_paths; +extern crate notify; extern crate os_pipe; use std::fs::{self, File}; @@ -12,10 +12,12 @@ use std::io::{BufRead, BufReader, Write}; use std::path::Path; use std::sync::{mpsc, Arc, Mutex}; use std::thread; -use std::time::Duration; +use std::time::{Duration, Instant}; use duct; +use self::mullvad_ipc_client::DaemonRpcClient; +use self::notify::{op, RawEvent, RecursiveMode, Watcher}; use self::os_pipe::{pipe, PipeReader}; #[cfg(unix)] @@ -24,6 +26,47 @@ pub static DAEMON_EXECUTABLE_PATH: &str = "../target/debug/mullvad-daemon"; #[cfg(not(unix))] pub static DAEMON_EXECUTABLE_PATH: &str = r"..\target\debug\mullvad-daemon.exe"; +pub fn wait_for_file_write_finish<P: AsRef<Path>>(file_path: P, timeout: Duration) { + let file_path = file_path.as_ref(); + let parent_dir = file_path.parent().expect("Missing file parent directory"); + + let absolute_parent_dir = parent_dir + .canonicalize() + .expect("Failed to get absolute path to watch"); + let file_name = file_path + .file_name() + .expect("Missing file name of file path to watch"); + let absolute_file_path = absolute_parent_dir.join(file_name); + + let (tx, rx) = mpsc::channel(); + let mut watcher = notify::raw_watcher(tx).expect("Failed to listen for file system events"); + let start = Instant::now(); + let mut remaining_time = Some(timeout); + + watcher + .watch(absolute_parent_dir, RecursiveMode::NonRecursive) + .expect("Failed to listen for file system events on directory"); + + if !file_path.exists() { + while let Some(wait_time) = remaining_time { + let event = rx.recv_timeout(wait_time); + + if let Ok(RawEvent { + path: Some(path), + op: Ok(op), + .. + }) = event + { + if op.contains(op::CLOSE_WRITE) && path == absolute_file_path { + break; + } + } + + remaining_time = timeout.checked_sub(start.elapsed()); + } + } +} + fn prepare_relay_list<T: AsRef<Path>>(path: T) { let path = path.as_ref(); @@ -88,6 +131,18 @@ impl DaemonRunner { } } + pub fn rpc_client(&mut self) -> Result<DaemonRpcClient, String> { + let rpc_file = mullvad_paths::get_rpc_address_path() + .map_err(|error| format!("Failed to build RPC connection file path: {}", error))?; + + if !rpc_file.exists() { + wait_for_file_write_finish(rpc_file, Duration::from_secs(10)); + } + + DaemonRpcClient::without_rpc_file_security_check() + .map_err(|error| format!("Failed to create RPC client: {}", error)) + } + #[cfg(unix)] fn request_clean_shutdown(&mut self, process: &mut duct::Handle) -> bool { use duct::unix::HandleExt; @@ -97,9 +152,7 @@ impl DaemonRunner { #[cfg(not(unix))] fn request_clean_shutdown(&mut self, _: &mut duct::Handle) -> bool { - use self::mullvad_ipc_client::DaemonRpcClient; - - if let Ok(mut rpc_client) = DaemonRpcClient::new() { + if let Ok(mut rpc_client) = self.rpc_client() { rpc_client.shutdown().is_ok() } else { false |
