summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorEmīls Piņķis <emils@mullvad.net>2018-05-04 16:52:13 +0100
committerEmīls Piņķis <emils@mullvad.net>2018-05-22 12:49:20 +0200
commit9b2966052d2ea1bd2303987287bbca01922da185 (patch)
tree3eb6d019c2c25251607ec91467afafb78cd257b2
parent3b7a575d939213f3e147eaa61564146220aeef4b (diff)
downloadmullvadvpn-9b2966052d2ea1bd2303987287bbca01922da185.tar.xz
mullvadvpn-9b2966052d2ea1bd2303987287bbca01922da185.zip
Replace duct::Handle dependency with OpenVpnProcHandle
-rw-r--r--talpid-core/src/process/mod.rs3
-rw-r--r--talpid-core/src/process/proc_handle.rs40
-rw-r--r--talpid-core/src/process/unix.rs14
-rw-r--r--talpid-core/src/tunnel/openvpn.rs19
4 files changed, 57 insertions, 19 deletions
diff --git a/talpid-core/src/process/mod.rs b/talpid-core/src/process/mod.rs
index 88bdc12ea4..29fc633a49 100644
--- a/talpid-core/src/process/mod.rs
+++ b/talpid-core/src/process/mod.rs
@@ -1,6 +1,9 @@
/// A module for all OpenVPN related process management.
pub mod openvpn;
+/// A module for OpenVPN process
+pub mod proc_handle;
/// Unix specific process management features.
#[cfg(unix)]
pub mod unix;
+
diff --git a/talpid-core/src/process/proc_handle.rs b/talpid-core/src/process/proc_handle.rs
new file mode 100644
index 0000000000..0893b85435
--- /dev/null
+++ b/talpid-core/src/process/proc_handle.rs
@@ -0,0 +1,40 @@
+/// Some docs
+extern crate duct;
+use std::io;
+
+/// Proc handle for an openvpn process
+pub struct OpenVpnProcHandle {
+ /// Duct handle
+ pub inner: duct::Handle,
+}
+
+/// Impl for proc handle
+impl OpenVpnProcHandle {
+ /// Constructor for a new openvpn proc handle
+ pub fn new(expr: duct::Expression) -> io::Result<Self> {
+ Ok(Self {
+ inner: expr.start()?,
+ })
+ }
+
+ /// Sends a SIGTERM signal to the OpenVPN process. Does not block, no guarantee that the
+ /// process will have exited after this function returns.
+ #[cfg(unix)]
+ pub fn try_stop(&self) -> io::Result<()> {
+ use duct::unix::HandleExt;
+ extern crate libc;
+ self.inner.send_signal(libc::SIGTERM)
+ }
+
+ /// Tries to shut down the OpenVPN process in a nondestructive manner. Does not block, no
+ /// guarantee that the process will have exited after this function returns.
+ #[cfg(windows)]
+ pub fn try_stop(&self) -> io::Result<()> {
+ Ok(())
+ }
+
+ /// Brutally kills the underlinyg process
+ pub fn kill_process(&self) -> io::Result<()> {
+ self.inner.kill()
+ }
+}
diff --git a/talpid-core/src/process/unix.rs b/talpid-core/src/process/unix.rs
index 4dea0f5a79..71e96c1246 100644
--- a/talpid-core/src/process/unix.rs
+++ b/talpid-core/src/process/unix.rs
@@ -1,32 +1,32 @@
extern crate libc;
-use duct;
-use duct::unix::HandleExt;
+use process::proc_handle::OpenVpnProcHandle;
+use duct;
use std::io;
use std::thread;
use std::time::{Duration, Instant};
static POLL_INTERVAL_MS: u64 = 50;
-/// Extra methods for terminating `duct::Handle` instances.
+/// Extra methods for terminating `OpenVpnProcHandle` instances.
pub trait HandleKillExt {
/// Kills a process by first sending it the `SIGTERM` signal and then wait up to `timeout`.
/// If the process has not died after the timeout has expired it is killed.
fn nice_kill(&self, timeout: Duration) -> io::Result<()>;
}
-impl HandleKillExt for duct::Handle {
+impl HandleKillExt for OpenVpnProcHandle {
fn nice_kill(&self, timeout: Duration) -> io::Result<()> {
trace!("Sending SIGTERM to child process");
- self.send_signal(libc::SIGTERM)?;
+ self.try_stop()?;
- if wait_timeout(self, timeout)? {
+ if wait_timeout(&self.inner, timeout)? {
debug!("Child process exited from SIGTERM");
Ok(())
} else {
warn!("Child process did not exit from SIGTERM, sending SIGKILL");
- self.kill()
+ self.kill_process()
}
}
}
diff --git a/talpid-core/src/tunnel/openvpn.rs b/talpid-core/src/tunnel/openvpn.rs
index 34b6979d77..a272164d65 100644
--- a/talpid-core/src/tunnel/openvpn.rs
+++ b/talpid-core/src/tunnel/openvpn.rs
@@ -1,6 +1,7 @@
use duct;
use openvpn_plugin::types::OpenVpnPluginEvent;
use process::openvpn::OpenVpnCommand;
+use process::proc_handle::OpenVpnProcHandle;
use std::collections::HashMap;
use std::io;
@@ -150,7 +151,7 @@ impl<C: OpenVpnBuilder> OpenVpnMonitor<C> {
/// A handle to an `OpenVpnMonitor` for closing it.
#[derive(Debug, Clone)]
-pub struct OpenVpnCloseHandle<H: ProcessHandle = duct::Handle> {
+pub struct OpenVpnCloseHandle<H: ProcessHandle = OpenVpnProcHandle> {
child: Arc<H>,
closed: Arc<AtomicBool>,
}
@@ -195,32 +196,26 @@ pub trait ProcessHandle: Send + Sync + 'static {
}
impl OpenVpnBuilder for OpenVpnCommand {
- type ProcessHandle = duct::Handle;
+ type ProcessHandle = OpenVpnProcHandle;
fn plugin<P: AsRef<Path>>(&mut self, path: P, args: Vec<String>) -> &mut Self {
self.plugin(path, args)
}
- fn start(&self) -> io::Result<duct::Handle> {
- self.build().start()
+ fn start(&self) -> io::Result<OpenVpnProcHandle> {
+ OpenVpnProcHandle::new(self.build())
}
}
-impl ProcessHandle for duct::Handle {
+impl ProcessHandle for OpenVpnProcHandle {
fn wait(&self) -> io::Result<ExitStatus> {
- self.wait().map(|output| output.status)
+ self.inner.wait().map(|output| output.status)
}
- #[cfg(unix)]
fn kill(&self) -> io::Result<()> {
use process::unix::HandleKillExt;
self.nice_kill(OPENVPN_DIE_TIMEOUT)
}
-
- #[cfg(not(unix))]
- fn kill(&self) -> io::Result<()> {
- duct::Handle::kill(self)
- }
}