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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
use super::{
    ServiceInstallCtx, ServiceLevel, ServiceManager, ServiceStartCtx, ServiceStopCtx,
    ServiceUninstallCtx,
};
use std::{
    borrow::Cow,
    ffi::{OsStr, OsString},
    fmt, io,
    process::{Command, Stdio},
};

#[cfg(windows)]
mod shell_escape;

#[cfg(not(windows))]
mod shell_escape {
    use std::{borrow::Cow, ffi::OsStr};

    /// When not on windows, this will do nothing but return the input str
    pub fn escape(s: Cow<'_, OsStr>) -> Cow<'_, OsStr> {
        s
    }
}

static SC_EXE: &str = "sc.exe";

/// Configuration settings tied to sc.exe services
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ScConfig {
    pub install: ScInstallConfig,
}

/// Configuration settings tied to sc.exe services during installation
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ScInstallConfig {
    /// Type of windows service for install
    pub service_type: WindowsServiceType,

    /// Start type for windows service for install
    pub start_type: WindowsStartType,

    /// Severity of the error if the windows service fails when the computer is started
    pub error_severity: WindowsErrorSeverity,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum WindowsServiceType {
    /// Service runs in its own process. It does not share an executable file with other services
    Own,

    /// Service runs as a shared process. It shares an executable file with other services
    Share,

    /// Service is a driver
    Kernel,

    /// Service is a file-system driver
    FileSys,

    /// Server is a file system recognized driver (identifies file systems used on the computer)
    Rec,
}

impl Default for WindowsServiceType {
    fn default() -> Self {
        Self::Own
    }
}

impl fmt::Display for WindowsServiceType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Own => write!(f, "own"),
            Self::Share => write!(f, "share"),
            Self::Kernel => write!(f, "kernel"),
            Self::FileSys => write!(f, "filesys"),
            Self::Rec => write!(f, "rec"),
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum WindowsStartType {
    /// Specifies a device driver that is loaded by the boot loader
    Boot,

    /// Specifies a device driver that is started during kernel initialization
    System,

    /// Specifies a service that automatically starts each time the computer is restarted. Note
    /// that the service runs even if no one logs on to the computer
    Auto,

    /// Specifies a service that must be started manually
    Demand,

    /// Specifies a service that cannot be started. To start a disabled service, change the start
    /// type to some other value.
    Disabled,
}

impl Default for WindowsStartType {
    fn default() -> Self {
        Self::Auto
    }
}

impl fmt::Display for WindowsStartType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Boot => write!(f, "boot"),
            Self::System => write!(f, "system"),
            Self::Auto => write!(f, "auto"),
            Self::Demand => write!(f, "demand"),
            Self::Disabled => write!(f, "disabled"),
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub enum WindowsErrorSeverity {
    /// Specifies that the error is logged. A message box is displayed, informing the user that a service has failed to start. Startup will continue
    Normal,

    /// Specifies that the error is logged (if possible). The computer attempts to restart with the
    /// last-known good configuration. This could result in the computer being able to restart, but
    /// the service may still be unable to run
    Severe,

    /// Specifies that the error is logged (if possible). The computer attempts to restart with the
    /// last-known good configuration. If the last-known good configuration fails, startup also
    /// fails, and the boot process halts with a Stop error
    Critical,

    /// Specifies that the error is logged and startup continues. No notification is given to the
    /// user beyond recording the error in the event log
    Ignore,
}

impl Default for WindowsErrorSeverity {
    fn default() -> Self {
        Self::Normal
    }
}

impl fmt::Display for WindowsErrorSeverity {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Normal => write!(f, "normal"),
            Self::Severe => write!(f, "severe"),
            Self::Critical => write!(f, "critical"),
            Self::Ignore => write!(f, "ignore"),
        }
    }
}

/// Implementation of [`ServiceManager`] for [Window Service](https://en.wikipedia.org/wiki/Windows_service)
/// leveraging [`sc.exe`](https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2012-r2-and-2012/cc754599(v=ws.11))
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct ScServiceManager {
    /// Configuration settings tied to rc.d services
    pub config: ScConfig,
}

impl ScServiceManager {
    /// Creates a new manager instance working with system services
    pub fn system() -> Self {
        Self::default()
    }

    /// Update manager to use the specified config
    pub fn with_config(self, config: ScConfig) -> Self {
        Self { config }
    }
}

impl ServiceManager for ScServiceManager {
    fn available(&self) -> io::Result<bool> {
        match which::which(SC_EXE) {
            Ok(_) => Ok(true),
            Err(which::Error::CannotFindBinaryPath) => Ok(false),
            Err(x) => Err(io::Error::new(io::ErrorKind::Other, x)),
        }
    }

    fn install(&self, ctx: ServiceInstallCtx) -> io::Result<()> {
        let service_name = ctx.label.to_qualified_name();

        let service_type = OsString::from(self.config.install.service_type.to_string());
        let start_type = OsString::from(self.config.install.start_type.to_string());
        let error_severity = OsString::from(self.config.install.error_severity.to_string());

        // Build our binary including arguments, following similar approach as windows-service-rs
        let mut binpath = OsString::new();
        binpath.push(shell_escape::escape(Cow::Borrowed(ctx.program.as_ref())));
        for arg in ctx.args_iter() {
            binpath.push(" ");
            binpath.push(shell_escape::escape(Cow::Borrowed(arg)));
        }

        let display_name = OsStr::new(&service_name);

        sc_exe(
            "create",
            &service_name,
            [
                // type= {service_type}
                OsStr::new("type="),
                service_type.as_os_str(),
                // start= {start_type}
                OsStr::new("start="),
                start_type.as_os_str(),
                // error= {error_severity}
                OsStr::new("error="),
                error_severity.as_os_str(),
                // binpath= "{program} {args}"
                OsStr::new("binpath="),
                binpath.as_os_str(),
                // displayname= {display_name}
                OsStr::new("displayname="),
                display_name,
            ],
        )
    }

    fn uninstall(&self, ctx: ServiceUninstallCtx) -> io::Result<()> {
        let service_name = ctx.label.to_qualified_name();
        sc_exe("delete", &service_name, [])
    }

    fn start(&self, ctx: ServiceStartCtx) -> io::Result<()> {
        let service_name = ctx.label.to_qualified_name();
        sc_exe("start", &service_name, [])
    }

    fn stop(&self, ctx: ServiceStopCtx) -> io::Result<()> {
        let service_name = ctx.label.to_qualified_name();
        sc_exe("stop", &service_name, [])
    }

    fn level(&self) -> ServiceLevel {
        ServiceLevel::System
    }

    fn set_level(&mut self, level: ServiceLevel) -> io::Result<()> {
        match level {
            ServiceLevel::System => Ok(()),
            ServiceLevel::User => Err(io::Error::new(
                io::ErrorKind::Unsupported,
                "sc.exe does not support user-level services",
            )),
        }
    }
}

fn sc_exe<'a>(
    cmd: &str,
    service_name: &str,
    args: impl IntoIterator<Item = &'a OsStr>,
) -> io::Result<()> {
    let mut command = Command::new(SC_EXE);

    command
        .stdin(Stdio::null())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());

    command.arg(cmd).arg(service_name);

    for arg in args {
        command.arg(arg);
    }

    let output = command.output()?;

    if output.status.success() {
        Ok(())
    } else {
        let msg = String::from_utf8(output.stderr)
            .ok()
            .filter(|s| !s.trim().is_empty())
            .or_else(|| {
                String::from_utf8(output.stdout)
                    .ok()
                    .filter(|s| !s.trim().is_empty())
            })
            .unwrap_or_else(|| format!("Failed to {cmd} for {service_name}"));

        Err(io::Error::new(io::ErrorKind::Other, msg))
    }
}