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
63
64
65
66
67
68
69
70
use std::io;

/// Represents the kind of service manager
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
#[cfg_attr(feature = "clap", derive(::clap::ValueEnum))]
#[cfg_attr(feature = "serde", derive(::serde::Serialize, ::serde::Deserialize))]
#[cfg_attr(feature = "clap", clap(rename_all = "lowercase"))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum ServiceManagerKind {
    /// Use launchd to manage the service
    Launchd,

    /// Use OpenRC to manage the service
    OpenRc,

    /// Use rc.d to manage the service
    Rcd,

    /// Use Windows service controller to manage the service
    Sc,

    /// Use systemd to manage the service
    Systemd,
}

impl ServiceManagerKind {
    /// Looks up the kind of service management platform native to the operating system
    #[cfg(target_os = "macos")]
    pub fn native() -> io::Result<Self> {
        Ok(Self::Launchd)
    }

    /// Looks up the kind of service management platform native to the operating system
    #[cfg(target_os = "windows")]
    pub fn native() -> io::Result<Self> {
        Ok(Self::Sc)
    }

    /// Looks up the kind of service management platform native to the operating system
    #[cfg(any(
        target_os = "freebsd",
        target_os = "dragonfly",
        target_os = "openbsd",
        target_os = "netbsd"
    ))]
    pub fn native() -> io::Result<Self> {
        Ok(Self::Rcd)
    }

    /// Looks up the kind of service management platform native to the operating system
    #[cfg(target_os = "linux")]
    pub fn native() -> io::Result<Self> {
        use super::{ServiceManager, TypedServiceManager};

        let manager = TypedServiceManager::target(Self::Systemd);
        if let Ok(true) = manager.available() {
            return Ok(Self::Systemd);
        }

        let manager = TypedServiceManager::target(Self::OpenRc);
        if let Ok(true) = manager.available() {
            return Ok(Self::OpenRc);
        }

        Err(io::Error::new(
            io::ErrorKind::Unsupported,
            "Only systemd and openrc are supported on Linux",
        ))
    }
}