diff options
| author | David Lönnhager <david.l@mullvad.net> | 2023-10-11 17:11:23 +0200 |
|---|---|---|
| committer | David Lönnhager <david.l@mullvad.net> | 2023-10-11 17:11:23 +0200 |
| commit | 51e06a48b68c7aeb2fd2c19205a107c030ac649e (patch) | |
| tree | 274a3118b124eee271cdd940fc5dffb77ed08078 /talpid-openvpn/src/process/stoppable_process.rs | |
| parent | 9383325ea310ded42066b68402c1c3fc7ff63955 (diff) | |
| parent | 968c1ebaf942976755974905553cde8f20cb7678 (diff) | |
| download | mullvadvpn-51e06a48b68c7aeb2fd2c19205a107c030ac649e.tar.xz mullvadvpn-51e06a48b68c7aeb2fd2c19205a107c030ac649e.zip | |
Merge branch 'weekly-cleanup/rewrite-talpid-openvpn-to-be-more-async-des-285' into main
Diffstat (limited to 'talpid-openvpn/src/process/stoppable_process.rs')
| -rw-r--r-- | talpid-openvpn/src/process/stoppable_process.rs | 53 |
1 files changed, 0 insertions, 53 deletions
diff --git a/talpid-openvpn/src/process/stoppable_process.rs b/talpid-openvpn/src/process/stoppable_process.rs deleted file mode 100644 index 3681c6cfa8..0000000000 --- a/talpid-openvpn/src/process/stoppable_process.rs +++ /dev/null @@ -1,53 +0,0 @@ -use std::{ - io, thread, - time::{Duration, Instant}, -}; - -static POLL_INTERVAL_MS: Duration = Duration::from_millis(50); - -/// A best effort attempt at stopping a subprocess whilst also ensuring that the subprocess is -/// killed eventually. -pub trait StoppableProcess -where - Self: Sized, -{ - /// Gracefully stops a process. - fn stop(&self); - - /// Kills a process unconditionally. Implementations should strive to never fail. - fn kill(&self) -> io::Result<()>; - - /// Check if process is stopped. This method must not block. - fn has_stopped(&self) -> io::Result<bool>; - - /// Attempts to stop a process gracefully in the given time period, otherwise kills the - /// process. - fn nice_kill(&self, timeout: Duration) -> io::Result<()> { - log::debug!("Trying to stop child process gracefully"); - self.stop(); - if wait_timeout(self, timeout)? { - log::debug!("Child process terminated gracefully"); - } else { - log::warn!( - "Child process did not terminate gracefully within timeout, forcing termination" - ); - self.kill()?; - } - Ok(()) - } -} -/// Wait for a process to die for a maximum of `timeout`. Returns true if the process died within -/// the timeout. -fn wait_timeout<T>(process: &T, timeout: Duration) -> io::Result<bool> -where - T: StoppableProcess + Sized, -{ - let timer = Instant::now(); - while timer.elapsed() < timeout { - if process.has_stopped()? { - return Ok(true); - } - thread::sleep(POLL_INTERVAL_MS); - } - Ok(false) -} |
