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
#![allow(clippy::missing_errors_doc)]
// ^needed as we have a lib and a main, pub crate would
// only allow access from the lib. However since the lib is not
// public it makes no sense to document errors.

use std::io::ErrorKind;
use std::path::{Component, Path, PathBuf};
use std::process::Command;
use std::str::FromStr;
use std::thread;
use std::time::{Duration, Instant};
use std::{fs, io};

use crate::install::builder::Trigger;
use crate::install::files::NoHomeError;
use crate::install::init::extract_path;

use super::{ExeLocation, Mode, Params, RSteps, SetupError, Steps, TearDownError};

mod setup;
mod teardown;

#[derive(thiserror::Error, Debug)]
pub enum SystemCtlError {
    #[error("Could not run systemctl")]
    Io(#[from] std::io::Error),
    #[error("Systemctl failed: {reason}")]
    Failed { reason: String },
    #[error("Timed out trying to enable service")]
    EnableTimeOut,
    #[error("Timed out trying to disable service")]
    DisableTimeOut,
    #[error("Something send a signal to systemctl ending it")]
    Terminated,
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
    #[error("Could not configure systemd: {0}")]
    SystemCtl(#[from] SystemCtlError),
    #[error("Could not write out unit file to {path}. Error: {e}")]
    Writing { e: io::Error, path: PathBuf },
    #[error("Could not remove the unit files")]
    Removing(io::Error),
    #[error("Could not verify unit files where created by us")]
    Verifying(io::Error),
    #[error("Could not check if this system uses systemd (init system path could not be resolved")]
    CheckingInitSys(io::Error),
}

// check if systemd is the init system (pid 1)
pub(super) fn not_available() -> Result<bool, SetupError> {
    use sysinfo::{Pid, ProcessRefreshKind, System, UpdateKind};
    let mut s = System::new();
    s.refresh_pids_specifics(
        &[Pid::from(1)],
        ProcessRefreshKind::new().with_cmd(UpdateKind::Always),
    );
    let init_sys = &s
        .process(Pid::from(1))
        .expect("there should always be an init system")
        .cmd()[0];
    let init_path = Path::new(init_sys.as_str())
        .canonicalize()
        .map_err(Error::CheckingInitSys)?;

    Ok(!init_path
        .components()
        .filter_map(|c| match c {
            Component::Normal(cmp) => Some(cmp),
            _other => None,
        })
        .filter_map(|c| c.to_str())
        .any(|c| c == "systemd"))
}

pub(super) fn set_up_steps(params: &Params) -> Result<Steps, SetupError> {
    let path_without_extension = match params.mode {
        Mode::User => user_path()?,
        Mode::System => system_path(),
    }
    .join(&params.name);

    Ok(match params.trigger {
        Trigger::OnSchedule(ref schedule) => {
            setup::with_timer(&path_without_extension, params, schedule)
        }
        Trigger::OnBoot => setup::without_timer(&path_without_extension, params),
    })
}

pub(super) fn tear_down_steps(
    name: &str,
    mode: Mode,
) -> Result<Option<(RSteps, ExeLocation)>, TearDownError> {
    let without_extension = match mode {
        Mode::User => user_path()?,
        Mode::System => system_path(),
    }
    .join(name);

    let mut steps = Vec::new();

    let timer_path = without_extension.with_extension("timer");
    let has_timer = our_service(&timer_path)?;
    if has_timer {
        steps.extend(teardown::disable_then_remove_with_timer(
            timer_path, name, mode,
        ));
    }

    let service_path = without_extension.with_extension("service");
    let exe_path = if our_service(&service_path)? {
        steps.extend(teardown::disable_then_remove_service(
            service_path.clone(),
            name,
            mode,
        ));
        exe_path(service_path).map_err(TearDownError::FindingExePath)?
    } else if has_timer {
        return Err(TearDownError::TimerWithoutService);
    } else {
        return Ok(None);
    };

    Ok(Some((steps, exe_path)))
}

/// The executables location could not be found. It is needed to safely
/// uninstall.
#[derive(Debug, thiserror::Error)]
pub enum FindExeError {
    #[error("Could not read systemd unit file at: {path}, error: {err}")]
    ReadingUnit { err: std::io::Error, path: PathBuf },
    #[error("ExecStart (use to find binary) is missing from servic unit at: {0}")]
    ExecLineMissing(PathBuf),
    #[error("Path to binary extracted from systemd unit does not lead to a file, path: {0}")]
    ExacPathNotFile(PathBuf),
}

fn exe_path(service_unit: PathBuf) -> Result<PathBuf, FindExeError> {
    let unit = std::fs::read_to_string(&service_unit).map_err(|err| FindExeError::ReadingUnit {
        err,
        path: service_unit.clone(),
    })?;
    let path = unit
        .lines()
        .map(str::trim)
        .find_map(|l| l.strip_prefix("ExecStart="))
        .map(extract_path::split_unescaped_whitespace_once)
        .ok_or(FindExeError::ExecLineMissing(service_unit))?;
    let path = PathBuf::from_str(&path).expect("infallible");
    if path.is_file() {
        Ok(path)
    } else {
        Err(FindExeError::ExacPathNotFile(path))
    }
}

fn user_path() -> Result<PathBuf, NoHomeError> {
    Ok(home::home_dir()
        .ok_or(NoHomeError)?
        .join(".config/systemd/user/"))
}

fn system_path() -> PathBuf {
    PathBuf::from("/etc/systemd/system")
}

fn our_service(service_path: &Path) -> Result<bool, Error> {
    use super::{COMMENT_PREAMBLE, COMMENT_SUFFIX};
    let service = match fs::read_to_string(service_path) {
        Ok(service) => service,
        Err(e) if e.kind() == ErrorKind::NotFound => return Ok(false),
        Err(e) => return Err(Error::Verifying(e)),
    };
    Ok(service.contains(COMMENT_PREAMBLE) && service.contains(COMMENT_SUFFIX))
}

fn systemctl(args: &[&'static str], service: &str) -> Result<(), SystemCtlError> {
    let output = Command::new("systemctl").args(args).arg(service).output()?;

    if output.status.success() {
        return Ok(());
    }

    let reason = String::from_utf8_lossy(&output.stderr).to_string();
    Err(SystemCtlError::Failed { reason })
}

fn is_active(service: &str, mode: Mode) -> Result<bool, SystemCtlError> {
    let args = match mode {
        Mode::System => &["is-active"][..],
        Mode::User => &["is-active", "--user"][..],
    };

    let output = Command::new("systemctl").args(args).arg(service).output()?;
    Ok(output.status.code().ok_or(SystemCtlError::Terminated)? == 0)
}

fn wait_for(
    service: &str,
    state: bool,
    mode: Mode,
    timeout_error: SystemCtlError,
) -> Result<(), SystemCtlError> {
    let start = Instant::now();
    while start.elapsed() < Duration::from_secs(1) {
        if state == is_active(service, mode)? {
            return Ok(());
        }
        thread::sleep(Duration::from_millis(50));
    }
    Err(timeout_error)
}

fn enable(unit: &str, mode: Mode) -> Result<(), SystemCtlError> {
    let args = match mode {
        Mode::System => &["enable", "--now"][..],
        Mode::User => &["enable", "--user", "--now"][..],
    };
    systemctl(args, unit)?;
    wait_for(unit, true, mode, SystemCtlError::EnableTimeOut)
}

fn disable(unit: &str, mode: Mode) -> Result<(), SystemCtlError> {
    let args = match mode {
        Mode::System => &["disable", "--now"][..],
        Mode::User => &["disable", "--user", "--now"][..],
    };
    systemctl(args, unit)?;
    wait_for(unit, false, mode, SystemCtlError::DisableTimeOut)
}