diff options
| author | David Lönnhager <david.l@mullvad.net> | 2022-05-10 13:30:40 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2022-05-10 13:30:40 +0200 |
| commit | f285f6a16c6ed49a126bed5368b901dbcac116fc (patch) | |
| tree | 3ca656638e1cf50b0d8e39e1def77d00dcb90a8b | |
| parent | e817b5e83072df627235397f50b0597e2066cd52 (diff) | |
| parent | f9d8eabda5ce659ffc9d8e9801744aad6e8dd3c8 (diff) | |
| download | mullvadvpn-f285f6a16c6ed49a126bed5368b901dbcac116fc.tar.xz mullvadvpn-f285f6a16c6ed49a126bed5368b901dbcac116fc.zip | |
Merge branch 'refactor-tsm-forwarders'
| -rw-r--r-- | mullvad-daemon/src/api.rs | 40 | ||||
| -rw-r--r-- | mullvad-daemon/src/lib.rs | 83 | ||||
| -rw-r--r-- | mullvad-daemon/src/macos.rs (renamed from mullvad-daemon/src/exclusion_gid.rs) | 37 |
3 files changed, 82 insertions, 78 deletions
diff --git a/mullvad-daemon/src/api.rs b/mullvad-daemon/src/api.rs index 563a53fab4..f2b8708ae6 100644 --- a/mullvad-daemon/src/api.rs +++ b/mullvad-daemon/src/api.rs @@ -1,8 +1,11 @@ +#[cfg(target_os = "android")] +use crate::{DaemonCommand, DaemonEventSender}; use futures::{ channel::{mpsc, oneshot}, - Future, Stream, + Future, Stream, StreamExt, }; use mullvad_api::{ + availability::ApiAvailabilityHandle, proxy::{ApiConnectionMode, ProxyConfig}, ApiEndpointUpdateCallback, }; @@ -14,6 +17,8 @@ use std::{ sync::{Arc, Mutex, Weak}, task::Poll, }; +#[cfg(target_os = "android")] +use talpid_core::mpsc::Sender; use talpid_core::tunnel_state_machine::TunnelCommand; use talpid_types::{ net::{openvpn::ProxySettings, AllowedEndpoint, Endpoint, TransportProtocol}, @@ -175,3 +180,36 @@ pub(super) fn get_allowed_endpoint(api_address: SocketAddr) -> AllowedEndpoint { endpoint, } } + +pub(crate) fn forward_offline_state( + api_availability: ApiAvailabilityHandle, + mut offline_state_rx: mpsc::UnboundedReceiver<bool>, +) { + tokio::spawn(async move { + let initial_state = offline_state_rx + .next() + .await + .expect("missing initial offline state"); + api_availability.set_offline(initial_state); + while let Some(is_offline) = offline_state_rx.next().await { + api_availability.set_offline(is_offline); + } + }); +} + +#[cfg(target_os = "android")] +pub(crate) fn create_bypass_tx( + event_sender: &DaemonEventSender, +) -> Option<mpsc::Sender<mullvad_api::SocketBypassRequest>> { + let (bypass_tx, mut bypass_rx) = mpsc::channel(1); + let daemon_tx = event_sender.to_specialized_sender(); + tokio::spawn(async move { + while let Some((raw_fd, done_tx)) = bypass_rx.next().await { + if let Err(_) = daemon_tx.send(DaemonCommand::BypassSocket(raw_fd, done_tx)) { + log::error!("Can't send socket bypass request to daemon"); + break; + } + } + }); + Some(bypass_tx) +} diff --git a/mullvad-daemon/src/lib.rs b/mullvad-daemon/src/lib.rs index fee22f1d70..d950f20663 100644 --- a/mullvad-daemon/src/lib.rs +++ b/mullvad-daemon/src/lib.rs @@ -11,10 +11,10 @@ mod cleanup; pub mod device; mod dns; pub mod exception_logging; -#[cfg(target_os = "macos")] -pub mod exclusion_gid; mod geoip; pub mod logging; +#[cfg(target_os = "macos")] +mod macos; #[cfg(not(target_os = "android"))] pub mod management_interface; mod migrations; @@ -34,7 +34,6 @@ use futures::{ future::{abortable, AbortHandle, Future}, StreamExt, }; -use mullvad_api::availability::ApiAvailabilityHandle; use mullvad_relay_selector::{ updater::{RelayListUpdater, RelayListUpdaterHandle}, RelaySelector, SelectorConfig, @@ -545,8 +544,8 @@ where ) -> Result<Self, Error> { #[cfg(target_os = "macos")] let exclusion_gid = { - bump_filehandle_limit(); - exclusion_gid::set_exclusion_gid().map_err(Error::GroupIdError)? + macos::bump_filehandle_limit(); + macos::set_exclusion_gid().map_err(Error::GroupIdError)? }; mullvad_api::proxy::ApiConnectionMode::try_delete_cache(&cache_dir).await; @@ -557,7 +556,7 @@ where &cache_dir, true, #[cfg(target_os = "android")] - Self::create_bypass_tx(&internal_event_tx), + api::create_bypass_tx(&internal_event_tx), ) .await .map_err(Error::InitRpcFactory)?; @@ -673,7 +672,7 @@ where endpoint_updater.set_tunnel_command_tx(Arc::downgrade(&tunnel_command_tx)); - Self::forward_offline_state(api_availability.clone(), offline_state_rx).await; + api::forward_offline_state(api_availability.clone(), offline_state_rx); let relay_list_listener = event_listener.clone(); let on_relay_list_update = move |relay_list: &RelayList| { @@ -2086,39 +2085,6 @@ where } } - #[cfg(target_os = "android")] - fn create_bypass_tx( - event_sender: &DaemonEventSender, - ) -> Option<mpsc::Sender<mullvad_api::SocketBypassRequest>> { - let (bypass_tx, mut bypass_rx) = mpsc::channel(1); - let daemon_tx = event_sender.to_specialized_sender(); - tokio::spawn(async move { - while let Some((raw_fd, done_tx)) = bypass_rx.next().await { - if let Err(_) = daemon_tx.send(DaemonCommand::BypassSocket(raw_fd, done_tx)) { - log::error!("Can't send socket bypass request to daemon"); - break; - } - } - }); - Some(bypass_tx) - } - - async fn forward_offline_state( - api_availability: ApiAvailabilityHandle, - mut offline_state_rx: mpsc::UnboundedReceiver<bool>, - ) { - let initial_state = offline_state_rx - .next() - .await - .expect("missing initial offline state"); - api_availability.set_offline(initial_state); - tokio::spawn(async move { - while let Some(is_offline) = offline_state_rx.next().await { - api_availability.set_offline(is_offline); - } - }); - } - /// Set the target state of the client. If it changed trigger the operations needed to /// progress towards that state. /// Returns a bool representing whether or not a state change was initiated. @@ -2210,40 +2176,3 @@ fn new_selector_config(settings: &Settings) -> SelectorConfig { obfuscation_settings: settings.obfuscation_settings.clone(), } } - -/// Bump filehandle limit -#[cfg(target_os = "macos")] -pub fn bump_filehandle_limit() { - let mut limits = libc::rlimit { - rlim_cur: 0, - rlim_max: 0, - }; - // SAFETY: `&mut limits` is a valid pointer parameter for the getrlimit syscall - let status = unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut limits) }; - if status != 0 { - log::error!( - "Failed to get file handle limits: {}-{}", - io::Error::from_raw_os_error(status), - status - ); - return; - } - - const INCREASED_FILEHANDLE_LIMIT: u64 = 1024; - // if file handle limit is already big enough, there's no reason to decrease it. - if limits.rlim_cur >= INCREASED_FILEHANDLE_LIMIT { - return; - } - - limits.rlim_cur = INCREASED_FILEHANDLE_LIMIT; - // SAFETY: `&limits` is a valid pointer parameter for the getrlimit syscall - let status = unsafe { libc::setrlimit(libc::RLIMIT_NOFILE, &limits) }; - if status != 0 { - log::error!( - "Failed to set file handle limit to {}: {}-{}", - INCREASED_FILEHANDLE_LIMIT, - io::Error::from_raw_os_error(status), - status - ); - } -} diff --git a/mullvad-daemon/src/exclusion_gid.rs b/mullvad-daemon/src/macos.rs index ec87c5a7c6..24511fe337 100644 --- a/mullvad-daemon/src/exclusion_gid.rs +++ b/mullvad-daemon/src/macos.rs @@ -1,7 +1,44 @@ use std::{ffi::CStr, io}; + /// name of the group that should be excluded const EXCLUSION_GROUP: &[u8] = b"mullvad-exclusion\0"; +/// Bump filehandle limit +pub fn bump_filehandle_limit() { + let mut limits = libc::rlimit { + rlim_cur: 0, + rlim_max: 0, + }; + // SAFETY: `&mut limits` is a valid pointer parameter for the getrlimit syscall + let status = unsafe { libc::getrlimit(libc::RLIMIT_NOFILE, &mut limits) }; + if status != 0 { + log::error!( + "Failed to get file handle limits: {}-{}", + io::Error::from_raw_os_error(status), + status + ); + return; + } + + const INCREASED_FILEHANDLE_LIMIT: u64 = 1024; + // if file handle limit is already big enough, there's no reason to decrease it. + if limits.rlim_cur >= INCREASED_FILEHANDLE_LIMIT { + return; + } + + limits.rlim_cur = INCREASED_FILEHANDLE_LIMIT; + // SAFETY: `&limits` is a valid pointer parameter for the getrlimit syscall + let status = unsafe { libc::setrlimit(libc::RLIMIT_NOFILE, &limits) }; + if status != 0 { + log::error!( + "Failed to set file handle limit to {}: {}-{}", + INCREASED_FILEHANDLE_LIMIT, + io::Error::from_raw_os_error(status), + status + ); + } +} + /// Returns the GID of `mullvad-exclusion` group if it exists. pub fn get_exclusion_gid() -> io::Result<u32> { let exclusion_group_name = CStr::from_bytes_with_nul(EXCLUSION_GROUP).unwrap(); |
