summaryrefslogtreecommitdiffhomepage
path: root/talpid_core
diff options
context:
space:
mode:
authorLinus Färnstrand <linus@mullvad.net>2017-06-16 15:25:19 +0200
committerLinus Färnstrand <linus@mullvad.net>2017-06-16 15:25:19 +0200
commit44346a5d28264ef15915336096a4e0d622d662ec (patch)
tree11f80ee610fb2e3ee73a8361ef5d03491bdb8830 /talpid_core
parent42bf586735f093b086a8c109cf7d9d664b8f849c (diff)
parentf642c9b3acb2b3d041634ec877b60c496198e5c1 (diff)
downloadmullvadvpn-44346a5d28264ef15915336096a4e0d622d662ec.tar.xz
mullvadvpn-44346a5d28264ef15915336096a4e0d622d662ec.zip
Merge branch 'daemon-struct' into master-new-daemon
Diffstat (limited to 'talpid_core')
-rw-r--r--talpid_core/Cargo.toml1
-rw-r--r--talpid_core/src/lib.rs2
-rw-r--r--talpid_core/src/tunnel/mod.rs2
-rw-r--r--talpid_core/src/tunnel/openvpn.rs32
4 files changed, 29 insertions, 8 deletions
diff --git a/talpid_core/Cargo.toml b/talpid_core/Cargo.toml
index 4b3afa4d80..573cc0b328 100644
--- a/talpid_core/Cargo.toml
+++ b/talpid_core/Cargo.toml
@@ -8,6 +8,7 @@ description = "Core backend functionality of the Mullvad VPN client"
duct = "0.9.1"
error-chain = "0.10"
log = "0.3"
+lazy_static = "0.2"
jsonrpc-core = { git = "https://github.com/faern/jsonrpc", branch = "ws-close-handle" }
jsonrpc-macros = { git = "https://github.com/faern/jsonrpc", branch = "ws-close-handle" }
diff --git a/talpid_core/src/lib.rs b/talpid_core/src/lib.rs
index 8a053bffa8..5496d038ce 100644
--- a/talpid_core/src/lib.rs
+++ b/talpid_core/src/lib.rs
@@ -9,6 +9,8 @@ extern crate assert_matches;
extern crate duct;
#[macro_use]
+extern crate lazy_static;
+#[macro_use]
extern crate log;
#[macro_use]
diff --git a/talpid_core/src/tunnel/mod.rs b/talpid_core/src/tunnel/mod.rs
index 3c955c563d..aead09b13b 100644
--- a/talpid_core/src/tunnel/mod.rs
+++ b/talpid_core/src/tunnel/mod.rs
@@ -90,7 +90,7 @@ pub struct CloseHandle(OpenVpnCloseHandle);
impl CloseHandle {
/// Closes the underlying tunnel, making the `TunnelMonitor::wait` method return.
- pub fn close(&self) -> io::Result<()> {
+ pub fn close(self) -> io::Result<()> {
self.0.close()
}
}
diff --git a/talpid_core/src/tunnel/openvpn.rs b/talpid_core/src/tunnel/openvpn.rs
index cf33484f38..08c2c14a32 100644
--- a/talpid_core/src/tunnel/openvpn.rs
+++ b/talpid_core/src/tunnel/openvpn.rs
@@ -2,14 +2,14 @@ use duct;
use jsonrpc_core::{Error, IoHandler};
use openvpn_ffi::{OpenVpnEnv, OpenVpnPluginEvent};
use process::openvpn::OpenVpnCommand;
-use std::io;
+use std::io;
use std::path::Path;
-use std::process;
use std::result::Result as StdResult;
use std::sync::{Arc, mpsc};
use std::sync::atomic::{AtomicBool, Ordering};
use std::thread;
+use std::time::Duration;
use talpid_ipc;
@@ -31,6 +31,11 @@ mod errors {
pub use self::errors::*;
+lazy_static!{
+ static ref OPENVPN_DIE_TIMEOUT: Duration = Duration::from_secs(2);
+}
+
+
/// Struct for monitoring an OpenVPN process.
pub struct OpenVpnMonitor {
child: Arc<duct::Handle>,
@@ -105,7 +110,7 @@ impl OpenVpnMonitor {
/// returned this returns the earliest result.
fn wait_result(&mut self) -> WaitResult {
let child_wait_handle = self.child.clone();
- let child_kill_handle = self.child.clone();
+ let child_close_handle = self.close_handle();
let event_dispatcher = self.event_dispatcher.take().unwrap();
let dispatcher_handle = event_dispatcher.close_handle();
@@ -123,7 +128,7 @@ impl OpenVpnMonitor {
move || {
let result = event_dispatcher.wait();
dispatcher_tx.send(WaitResult::EventDispatcher(result)).unwrap();
- let _ = child_kill_handle.kill();
+ let _ = child_close_handle.close();
},
);
@@ -141,15 +146,28 @@ pub struct OpenVpnCloseHandle {
impl OpenVpnCloseHandle {
/// Kills the underlying OpenVPN process, making the `OpenVpnMonitor::wait` method return.
- pub fn close(&self) -> io::Result<()> {
- self.closed.store(true, Ordering::SeqCst);
+ pub fn close(self) -> io::Result<()> {
+ if !self.closed.swap(true, Ordering::SeqCst) {
+ self.kill_openvpn()
+ } else {
+ Ok(())
+ }
+ }
+
+ #[cfg(unix)]
+ fn kill_openvpn(self) -> io::Result<()> {
+ ::process::unix::nice_kill(self.child, *OPENVPN_DIE_TIMEOUT)
+ }
+
+ #[cfg(not(unix))]
+ fn kill_openvpn(self) -> io::Result<()> {
self.child.kill()
}
}
/// Internal enum to differentiate between if the child process or the event dispatcher died first.
enum WaitResult {
- Child(io::Result<process::ExitStatus>),
+ Child(io::Result<::std::process::ExitStatus>),
EventDispatcher(talpid_ipc::Result<()>),
}