summaryrefslogtreecommitdiffhomepage
path: root/mullvad-daemon/src/shutdown.rs
blob: 2e7473b34facc1dbda9b4c8cfa9b5ecec65e6b8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#[cfg(unix)]
mod platform {
    use simple_signal::Signal;
    use std::io;

    pub fn set_shutdown_signal_handler(f: impl Fn() + 'static + Send) -> Result<(), io::Error> {
        simple_signal::set_handler(&[Signal::Term, Signal::Int], move |s| {
            log::debug!("Process received signal: {:?}", s);
            f();
        });
        Ok(())
    }
}

#[cfg(windows)]
mod platform {
    #[derive(thiserror::Error, Debug)]
    #[error("Unable to attach ctrl-c handler")]
    pub struct Error(#[from] ctrlc::Error);

    pub fn set_shutdown_signal_handler(f: impl Fn() + 'static + Send) -> Result<(), Error> {
        ctrlc::set_handler(move || {
            log::debug!("Process received Ctrl-c");
            f();
        })
        .map_err(Error)
    }
}

/// Returns true if systemd successfully reported that the machine is not shutting down or entering
/// maintenance. If obtaining this information fails, the return value will be `false` and it will
/// be assumed that the machine is shutting down.
#[cfg(target_os = "linux")]
pub fn is_shutdown_user_initiated() -> bool {
    match talpid_dbus::systemd::is_host_running() {
        Ok(is_host_running) => is_host_running,
        Err(err) => {
            log::error!(
                "{}",
                talpid_types::ErrorExt::display_chain_with_msg(
                    &err,
                    "Failed to determine if host is shutting down, assuming it is shutting down"
                )
            );
            false
        }
    }
}

/// Currently returns false all of the time to ensure that no leaks occur during shutdown.
// TODO: implement shutdown detection
#[cfg(target_os = "macos")]
pub fn is_shutdown_user_initiated() -> bool {
    false
}

pub use self::platform::*;