summaryrefslogtreecommitdiffstats
path: root/crates/core/tedge/src/system_services/manager.rs
blob: 7c39ea8cf74c4ea59d06f94641301307fee0f48d (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
use crate::system_services::*;
use std::fmt::Debug;
use std::path::PathBuf;
use std::sync::Arc;
use tedge_users::UserManager;

/// Abstraction over the system-provided facility that manages starting, stopping as well as other
/// service-related management functions of system services.
pub trait SystemServiceManager: Debug {
    /// Returns the name of the system service manager facility (e.g. "systemd" or "openrc").
    fn name(&self) -> &str;

    /// Checks whether the system service manager facility is available and operational.
    fn check_operational(&self) -> Result<(), SystemServiceError>;

    /// Stops the specified system service.
    fn stop_service(&self, service: SystemService) -> Result<(), SystemServiceError>;

    /// Restarts the specified system service.
    fn restart_service(&self, service: SystemService) -> Result<(), SystemServiceError>;

    /// Enables the specified system service. This does not start the service, unless you reboot.
    fn enable_service(&self, service: SystemService) -> Result<(), SystemServiceError>;

    /// Disables the specified system service. This does not stop the service.
    fn disable_service(&self, service: SystemService) -> Result<(), SystemServiceError>;

    /// Queries status of the specified system service. "Running" here means the same as "active".
    fn is_service_running(&self, service: SystemService) -> Result<bool, SystemServiceError>;

    /// Utility method that only restarts the `service` if it is already running.
    fn restart_service_if_running(
        &self,
        service: SystemService,
    ) -> Result<bool, SystemServiceError> {
        if self.is_service_running(service)? {
            let () = self.restart_service(service)?;
            Ok(true)
        } else {
            Ok(false)
        }
    }
}

pub fn service_manager(
    user_manager: UserManager,
    config_root: PathBuf,
) -> Result<Arc<dyn SystemServiceManager>, SystemServiceError> {
    Ok(Arc::new(GeneralServiceManager::try_new(
        user_manager,
        config_root,
    )?))
}