diff options
| author | Linus Färnstrand <linus@mullvad.net> | 2019-03-20 11:19:40 +0100 |
|---|---|---|
| committer | Linus Färnstrand <linus@mullvad.net> | 2019-03-20 11:19:40 +0100 |
| commit | 8d9bcfb9964a7ddfc7e2f406f01f840549e56ada (patch) | |
| tree | 7964ce002c39be38a825fd7d716247abe6c03cd8 /talpid-core | |
| parent | a15262e47c100160c1547a3c3294592e236cb7b7 (diff) | |
| download | mullvadvpn-8d9bcfb9964a7ddfc7e2f406f01f840549e56ada.tar.xz mullvadvpn-8d9bcfb9964a7ddfc7e2f406f01f840549e56ada.zip | |
Use parking_lot in talpid-core to reduce unwrap
Diffstat (limited to 'talpid-core')
| -rw-r--r-- | talpid-core/Cargo.toml | 12 | ||||
| -rw-r--r-- | talpid-core/src/dns/linux/static_resolv_conf.rs | 17 | ||||
| -rw-r--r-- | talpid-core/src/dns/macos.rs | 9 | ||||
| -rw-r--r-- | talpid-core/src/process/openvpn.rs | 5 | ||||
| -rw-r--r-- | talpid-core/src/tunnel/openvpn.rs | 11 |
5 files changed, 26 insertions, 28 deletions
diff --git a/talpid-core/Cargo.toml b/talpid-core/Cargo.toml index 075cd0319f..11b990b90a 100644 --- a/talpid-core/Cargo.toml +++ b/talpid-core/Cargo.toml @@ -13,18 +13,18 @@ error-chain = "0.12" futures = "0.1" jsonrpc-core = { git = "https://github.com/mullvad/jsonrpc", branch = "mullvad-fork" } jsonrpc-macros = { git = "https://github.com/mullvad/jsonrpc", branch = "mullvad-fork" } - libc = "0.2.20" log = "0.4" openvpn-plugin = { git = "https://github.com/mullvad/openvpn-plugin-rs", branch = "auth-failed-event", features = ["serde"] } os_pipe = "0.7" +parking_lot = "0.7" +regex = "1.1.0" shell-escape = "0.1" +talpid-ipc = { path = "../talpid-ipc" } +talpid-types = { path = "../talpid-types" } tokio-core = "0.1" uuid = { version = "0.6", features = ["v4"] } -talpid-ipc = { path = "../talpid-ipc" } -talpid-types = { path = "../talpid-types" } -regex = "1.1.0" [target.'cfg(unix)'.dependencies] hex = "0.3" @@ -33,6 +33,7 @@ lazy_static = "1.0" tun = { git = "https://github.com/pinkisemils/rust-tun", branch = "add-raw-fd-traits" } nix = "0.12" + [target.'cfg(target_os = "linux")'.dependencies] dbus = "0.6" failure = "0.1" @@ -46,15 +47,18 @@ mnl = { git = "https://github.com/mullvad/mnl-rs", rev = "f0d19501b9b85be9a1ffae which = "2.0" err-derive = "0.1.5" + [target.'cfg(target_os = "macos")'.dependencies] # TODO: Specify 0.2.1 once the crate gets published pfctl = { git = "https://github.com/mullvad/pfctl-rs", rev = "9f31b5ddcab941862470075eab83bb398195f3d6" } system-configuration = "0.2" + [target.'cfg(windows)'.dependencies] widestring = "0.3" winreg = "0.5" winapi = { version = "0.3.6", features = ["handleapi", "libloaderapi", "synchapi", "winbase", "winuser"] } + [dev-dependencies] tempfile = "3.0" diff --git a/talpid-core/src/dns/linux/static_resolv_conf.rs b/talpid-core/src/dns/linux/static_resolv_conf.rs index cea1b1ac2f..71b6973edf 100644 --- a/talpid-core/src/dns/linux/static_resolv_conf.rs +++ b/talpid-core/src/dns/linux/static_resolv_conf.rs @@ -1,11 +1,12 @@ use super::RESOLV_CONF_PATH; use error_chain::ChainedError; use notify::{RecommendedWatcher, RecursiveMode, Watcher}; +use parking_lot::Mutex; use resolv_conf::{Config, ScopedIp}; use std::{ fs, io, net::IpAddr, - sync::{mpsc, Arc, Mutex, MutexGuard}, + sync::{mpsc, Arc}, thread, }; @@ -50,7 +51,7 @@ impl StaticResolvConf { } pub fn set_dns(&mut self, servers: Vec<IpAddr>) -> Result<()> { - let mut state = self.lock_state(); + let mut state = self.state.lock(); let new_state = match state.take() { None => { let backup = read_config().chain_err(|| ErrorKind::BackupResolvConf)?; @@ -75,19 +76,13 @@ impl StaticResolvConf { } pub fn reset(&mut self) -> Result<()> { - if let Some(state) = self.lock_state().take() { + if let Some(state) = self.state.lock().take() { write_config(&state.backup)?; let _ = fs::remove_file(RESOLV_CONF_BACKUP_PATH); } Ok(()) } - - fn lock_state(&self) -> MutexGuard<Option<State>> { - self.state - .lock() - .expect("a thread panicked while using the DNS configuration state") - } } struct State { @@ -129,9 +124,7 @@ impl DnsWatcher { fn event_loop(events: mpsc::Receiver<notify::RawEvent>, state: &Arc<Mutex<Option<State>>>) { for _ in events { - let mut locked_state = state - .lock() - .expect("a thread panicked while using the DNS configuration state"); + let mut locked_state = state.lock(); if let Err(error) = Self::update(locked_state.as_mut()) { let chained_error = error diff --git a/talpid-core/src/dns/macos.rs b/talpid-core/src/dns/macos.rs index d65fdbf6c0..3dfc8d2be4 100644 --- a/talpid-core/src/dns/macos.rs +++ b/talpid-core/src/dns/macos.rs @@ -1,11 +1,12 @@ use error_chain::ChainedError; use log::{debug, trace}; +use parking_lot::Mutex; use std::{ collections::HashMap, fmt, net::IpAddr, path::Path, - sync::{mpsc, Arc, Mutex}, + sync::{mpsc, Arc}, thread, }; use system_configuration::{ @@ -145,7 +146,7 @@ impl super::DnsMonitorT for DnsMonitor { fn set(&mut self, _interface: &str, servers: &[IpAddr]) -> Result<()> { let servers: Vec<DnsServer> = servers.iter().map(|ip| ip.to_string()).collect(); let settings = DnsSettings::from_server_addresses(&servers); - let mut state_lock = self.state.lock().unwrap(); + let mut state_lock = self.state.lock(); *state_lock = Some(match state_lock.take() { None => { let backup = read_all_dns(&self.store); @@ -177,7 +178,7 @@ impl super::DnsMonitorT for DnsMonitor { } fn reset(&mut self) -> Result<()> { - let mut state_lock = self.state.lock().unwrap(); + let mut state_lock = self.state.lock(); if let Some(state) = state_lock.take() { trace!("Restoring DNS settings to: {:#?}", state.backup); for (service_path, settings) in state.backup { @@ -253,7 +254,7 @@ fn dns_change_callback( changed_keys: CFArray<CFString>, state: &mut Arc<Mutex<Option<State>>>, ) { - let mut state_lock = state.lock().unwrap(); + let mut state_lock = state.lock(); match *state_lock { None => { trace!("Not injecting DNS at this time"); diff --git a/talpid-core/src/process/openvpn.rs b/talpid-core/src/process/openvpn.rs index 24d69d3926..3cc5e31938 100644 --- a/talpid-core/src/process/openvpn.rs +++ b/talpid-core/src/process/openvpn.rs @@ -3,12 +3,12 @@ use duct; use super::stoppable_process::StoppableProcess; use atty; use os_pipe::{pipe, PipeWriter}; +use parking_lot::Mutex; use shell_escape; use std::{ ffi::{OsStr, OsString}, fmt, io, path::{Path, PathBuf}, - sync::Mutex, }; use talpid_types::net; @@ -378,10 +378,9 @@ impl OpenVpnProcHandle { impl StoppableProcess for OpenVpnProcHandle { /// Closes STDIN to stop the openvpn process fn stop(&self) { - let mut stdin = self.stdin.lock().unwrap(); // Dropping our stdin handle so that it is closed once. Closing the handle should // gracefully stop our openvpn child process. - let _ = stdin.take(); + let _ = self.stdin.lock().take(); } fn kill(&self) -> io::Result<()> { diff --git a/talpid-core/src/tunnel/openvpn.rs b/talpid-core/src/tunnel/openvpn.rs index 39dd3f2a93..0d0c4a932f 100644 --- a/talpid-core/src/tunnel/openvpn.rs +++ b/talpid-core/src/tunnel/openvpn.rs @@ -620,9 +620,10 @@ mod event_server { mod tests { use super::*; use crate::mktemp::TempFile; + use parking_lot::Mutex; use std::{ path::{Path, PathBuf}, - sync::{Arc, Mutex}, + sync::Arc, }; #[derive(Debug, Default, Clone)] @@ -636,12 +637,12 @@ mod tests { type ProcessHandle = TestProcessHandle; fn plugin(&mut self, path: impl AsRef<Path>, _args: Vec<String>) -> &mut Self { - *self.plugin.lock().unwrap() = Some(path.as_ref().to_path_buf()); + *self.plugin.lock() = Some(path.as_ref().to_path_buf()); self } fn log(&mut self, log: Option<impl AsRef<Path>>) -> &mut Self { - *self.log.lock().unwrap() = log.as_ref().map(|path| path.as_ref().to_path_buf()); + *self.log.lock() = log.as_ref().map(|path| path.as_ref().to_path_buf()); self } @@ -686,7 +687,7 @@ mod tests { ); assert_eq!( Some(PathBuf::from("./my_test_plugin")), - *builder.plugin.lock().unwrap() + *builder.plugin.lock() ); } @@ -704,7 +705,7 @@ mod tests { ); assert_eq!( Some(PathBuf::from("./my_test_log_file")), - *builder.log.lock().unwrap() + *builder.log.lock() ); } |
