diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2017-06-13 12:05:25 +0200 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2017-06-14 12:20:50 +0200 |
| commit | f7074096a7a071ee5377c6576186386fc45adc16 (patch) | |
| tree | b26d49749980f9931b7c39dc277068f6cd47e044 | |
| parent | ec591107a6b0105adf4c16720ae93434dc4b7cde (diff) | |
| download | mullvadvpn-f7074096a7a071ee5377c6576186386fc45adc16.tar.xz mullvadvpn-f7074096a7a071ee5377c6576186386fc45adc16.zip | |
Use nice_kill to kill OpenVPN gracefully
| -rw-r--r-- | talpid_core/Cargo.toml | 1 | ||||
| -rw-r--r-- | talpid_core/src/lib.rs | 2 | ||||
| -rw-r--r-- | talpid_core/src/tunnel/openvpn.rs | 25 |
3 files changed, 23 insertions, 5 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/openvpn.rs b/talpid_core/src/tunnel/openvpn.rs index cf33484f38..4669240ba8 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>, @@ -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 _ = kill_openvpn(child_kill_handle); }, ); @@ -143,13 +148,23 @@ 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); - self.child.kill() + kill_openvpn(self.child.clone()) } } +#[cfg(unix)] +fn kill_openvpn(child: Arc<duct::Handle>) -> io::Result<()> { + ::process::unix::nice_kill(child, *OPENVPN_DIE_TIMEOUT) +} + +#[cfg(not(unix))] +fn kill_openvpn(child: Arc<duct::Handle>) -> io::Result<()> { + 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<()>), } |
