summaryrefslogtreecommitdiffhomepage
path: root/test/test-runner/src/sys/mod.rs
blob: 48b539739e0e8ffa3c77af7c271b000ce3d2df48 (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
58
59
60
61
62
use std::path::Path;
use test_rpc::mullvad_daemon::SOCKET_PATH;
use test_rpc::mullvad_daemon::ServiceStatus;

#[cfg(target_os = "windows")]
mod windows;

#[cfg(target_os = "windows")]
pub use windows::*;

#[cfg(target_os = "linux")]
mod linux;

#[cfg(target_os = "linux")]
pub use linux::*;

#[cfg(target_os = "macos")]
mod macos;

#[cfg(target_os = "macos")]
pub use macos::*;

#[cfg(unix)]
pub fn reboot() -> Result<(), test_rpc::Error> {
    log::debug!("Rebooting system");

    std::thread::spawn(|| {
        #[cfg(target_os = "linux")]
        let mut cmd = std::process::Command::new("/usr/sbin/shutdown");
        #[cfg(target_os = "macos")]
        let mut cmd = std::process::Command::new("/sbin/shutdown");
        cmd.args(["-r", "now"]);

        std::thread::sleep(std::time::Duration::from_secs(5));

        let _ = cmd.spawn().map_err(|error| {
            log::error!("Failed to spawn shutdown command: {error}");
            error
        });
    });

    Ok(())
}

pub fn get_daemon_status() -> ServiceStatus {
    let rpc_socket_exists = Path::new(SOCKET_PATH).exists();

    // On Windows, we must also make sure service isn't in a pending state, since interacting with
    // the service may fail even if there is a working named pipe.
    #[cfg(target_os = "windows")]
    let service_is_started =
        get_daemon_system_service_status().unwrap_or(ServiceStatus::NotRunning);

    // NOTE: May not be necessary on non-Windows
    #[cfg(not(target_os = "windows"))]
    let service_is_started = ServiceStatus::Running;

    match (rpc_socket_exists, service_is_started) {
        (true, ServiceStatus::Running) => ServiceStatus::Running,
        _ => ServiceStatus::NotRunning,
    }
}